| 9404 |
vikram.rag |
1 |
package in.shop2020;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
4 |
import in.shop2020.model.v1.catalog.SnapdealItem;
|
|
|
5 |
import in.shop2020.model.v1.inventory.InventoryService.Client;
|
|
|
6 |
import in.shop2020.model.v1.inventory.InventoryType;
|
|
|
7 |
import in.shop2020.model.v1.inventory.ItemInventory;
|
|
|
8 |
import in.shop2020.model.v1.inventory.SnapdealInventoryItem;
|
|
|
9 |
import in.shop2020.model.v1.inventory.Warehouse;
|
|
|
10 |
import in.shop2020.model.v1.inventory.WarehouseType;
|
|
|
11 |
import in.shop2020.serving.model.OrderItems;
|
|
|
12 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
13 |
import in.shop2020.thrift.clients.InventoryClient;
|
|
|
14 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
15 |
import inventory.Inventory;
|
|
|
16 |
import inventory.InventoryItems;
|
|
|
17 |
import inventory.SnapdealItemForInventory;
|
|
|
18 |
|
|
|
19 |
import java.io.BufferedReader;
|
|
|
20 |
import java.io.File;
|
|
|
21 |
import java.io.FileInputStream;
|
|
|
22 |
import java.io.FileNotFoundException;
|
|
|
23 |
import java.io.FileOutputStream;
|
|
|
24 |
import java.io.IOException;
|
|
|
25 |
import java.io.InputStreamReader;
|
|
|
26 |
import java.io.UnsupportedEncodingException;
|
|
|
27 |
import java.math.BigInteger;
|
|
|
28 |
import java.util.ArrayList;
|
|
|
29 |
import java.util.HashMap;
|
|
|
30 |
import java.util.List;
|
|
|
31 |
import java.util.Map;
|
|
|
32 |
|
|
|
33 |
import org.apache.http.HttpEntity;
|
|
|
34 |
import org.apache.http.HttpResponse;
|
|
|
35 |
import org.apache.http.NameValuePair;
|
|
|
36 |
import org.apache.http.client.ClientProtocolException;
|
|
|
37 |
import org.apache.http.client.HttpClient;
|
|
|
38 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
39 |
import org.apache.http.client.methods.HttpGet;
|
|
|
40 |
import org.apache.http.client.methods.HttpPost;
|
|
|
41 |
import org.apache.http.entity.FileEntity;
|
|
|
42 |
import org.apache.http.entity.mime.MultipartEntity;
|
|
|
43 |
import org.apache.http.entity.mime.content.ContentBody;
|
|
|
44 |
import org.apache.http.entity.mime.content.FileBody;
|
|
|
45 |
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
46 |
import org.apache.http.message.BasicNameValuePair;
|
|
|
47 |
import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
48 |
import org.apache.poi.hssf.usermodel.HSSFRow;
|
|
|
49 |
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
|
|
50 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
51 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
52 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
53 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
54 |
import org.apache.thrift.TException;
|
|
|
55 |
import org.apache.thrift.transport.TTransportException;
|
|
|
56 |
import com.google.gson.Gson;
|
|
|
57 |
|
|
|
58 |
public class CreateSnapDealInventoryFeed {
|
| 9415 |
vikram.rag |
59 |
private static long time;
|
|
|
60 |
private static String SNAPDEAL_INVENTORY_SHEET;
|
| 9404 |
vikram.rag |
61 |
private static Map<Long, SnapdealItemForInventory> allItemsInventoryMap = new HashMap<Long, SnapdealItemForInventory>();
|
|
|
62 |
private static ArrayList<Inventory> inventoryItemList;
|
|
|
63 |
static Map<Long,Long> itemIdpendingOrdersMap = new HashMap<Long,Long>();
|
|
|
64 |
private static void calculateinventory(){
|
|
|
65 |
|
|
|
66 |
CatalogClient catalogServiceClient = null;
|
|
|
67 |
Map<Long,ItemInventory> availability= new HashMap<Long,ItemInventory>();
|
|
|
68 |
try {
|
|
|
69 |
catalogServiceClient = new CatalogClient();
|
|
|
70 |
} catch (TTransportException e) {
|
|
|
71 |
// TODO Auto-generated catch block
|
|
|
72 |
e.printStackTrace();
|
|
|
73 |
}
|
|
|
74 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = catalogServiceClient.getClient();
|
|
|
75 |
try {
|
|
|
76 |
List<Item> aliveItems = catalogClient.getAllAliveItems();
|
|
|
77 |
Map<Long,Item> aliveItemsMap = new HashMap<Long,Item>();
|
|
|
78 |
for(in.shop2020.model.v1.catalog.Item thriftItem:aliveItems){
|
|
|
79 |
aliveItemsMap.put(thriftItem.getId(), thriftItem);
|
|
|
80 |
|
|
|
81 |
}
|
|
|
82 |
InventoryClient inventoryServiceClient = null;
|
|
|
83 |
try {
|
|
|
84 |
inventoryServiceClient = new InventoryClient();
|
|
|
85 |
} catch (TTransportException e) {
|
|
|
86 |
e.printStackTrace();
|
|
|
87 |
}
|
|
|
88 |
in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = inventoryServiceClient.getClient();
|
|
|
89 |
List<in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems> ignoreItems = inventoryClient.getIgnoredWarehouseidsAndItemids();
|
| 9415 |
vikram.rag |
90 |
time = System.currentTimeMillis();
|
|
|
91 |
SNAPDEAL_INVENTORY_SHEET = "/home/snapdeal/snapdeal-inventory-"+time+".xls";
|
| 9404 |
vikram.rag |
92 |
availability = inventoryClient.getInventorySnapshot(0);
|
|
|
93 |
List<Warehouse> warehouses = inventoryClient.getWarehouses(WarehouseType.OURS_THIRDPARTY,null,0,0,0);
|
|
|
94 |
List<Warehouse> ours_warehouses = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, 0, 0, 0);
|
|
|
95 |
List<Long> thirdpartywarehouseids = new ArrayList<Long>();
|
|
|
96 |
List<Long> oursGoodWarehouse = new ArrayList<Long>();
|
|
|
97 |
for(Warehouse warehouse:warehouses){
|
|
|
98 |
thirdpartywarehouseids.add(warehouse.getId());
|
|
|
99 |
}
|
|
|
100 |
for (Warehouse warehouse:ours_warehouses){
|
|
|
101 |
oursGoodWarehouse.add(warehouse.getId());
|
|
|
102 |
}
|
|
|
103 |
long available=0;
|
|
|
104 |
long reserve=0;
|
|
|
105 |
long total_held=0;
|
|
|
106 |
long heldForSource=0;
|
|
|
107 |
double nlc=0;
|
|
|
108 |
double maxnlc=0;
|
|
|
109 |
Item thriftItem;
|
|
|
110 |
for(Inventory inventoryItem:inventoryItemList){
|
|
|
111 |
if(aliveItemsMap.get(Long.parseLong(inventoryItem.getSellerSku())) != null){
|
|
|
112 |
thriftItem = aliveItemsMap.get(Long.parseLong(inventoryItem.getSellerSku()));
|
|
|
113 |
}
|
|
|
114 |
else{
|
|
|
115 |
continue;
|
|
|
116 |
}
|
|
|
117 |
available=0;
|
|
|
118 |
reserve=0;
|
|
|
119 |
total_held=0;
|
|
|
120 |
heldForSource=0;
|
|
|
121 |
nlc=0;
|
|
|
122 |
List<Warehouse> vendor_warehouses=null;
|
|
|
123 |
SnapdealItemForInventory item;
|
|
|
124 |
if(availability.get(thriftItem.getId())!=null){
|
|
|
125 |
ItemInventory iteminventory = availability.get(thriftItem.getId());
|
|
|
126 |
Map<Long,Long> itemavailability = new HashMap<Long,Long>();
|
|
|
127 |
itemavailability = iteminventory.getAvailability();
|
|
|
128 |
if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
|
|
|
129 |
//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating availability)"+thriftItem.getPreferredVendor());
|
|
|
130 |
vendor_warehouses = inventoryClient.getWarehouses(WarehouseType.THIRD_PARTY,InventoryType.GOOD , thriftItem.getPreferredVendor(), 0, 0);
|
|
|
131 |
vendor_warehouses.addAll(ours_warehouses);
|
|
|
132 |
for (Warehouse warehouse:vendor_warehouses){
|
|
|
133 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
134 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
135 |
ignoredItem.setWarehouseId(warehouse.getId());
|
|
|
136 |
if (itemavailability.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(ignoredItem)){
|
|
|
137 |
try{
|
|
|
138 |
nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
|
|
|
139 |
}
|
|
|
140 |
catch(TTransportException e){
|
|
|
141 |
inventoryClient = inventoryServiceClient.getClient();
|
|
|
142 |
nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
|
|
|
143 |
}
|
|
|
144 |
maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
|
| 9413 |
vikram.rag |
145 |
System.out.println("itemId:" + thriftItem.getId() + "\tmaxnlc: " + maxnlc + "\tnlc:" + nlc + "\twid:" + warehouse.getId() );
|
| 9404 |
vikram.rag |
146 |
if(nlc !=0 && (maxnlc >= nlc)){
|
|
|
147 |
total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
|
|
|
148 |
heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
|
|
|
149 |
available = available + itemavailability.get(warehouse.getId()) - total_held;
|
| 9413 |
vikram.rag |
150 |
System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
|
| 9404 |
vikram.rag |
151 |
}
|
| 9413 |
vikram.rag |
152 |
else if(maxnlc==0){
|
|
|
153 |
total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 0);
|
|
|
154 |
heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), warehouse.getId(), 7);
|
|
|
155 |
available = available + itemavailability.get(warehouse.getId()) - total_held;
|
|
|
156 |
System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\tavailable:" + available);
|
| 9404 |
vikram.rag |
157 |
}
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
else{
|
|
|
162 |
for (Map.Entry<Long,Long> entry : itemavailability.entrySet()) {
|
|
|
163 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
164 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
165 |
ignoredItem.setWarehouseId(entry.getKey());
|
|
|
166 |
if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
|
|
|
167 |
nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
|
|
|
168 |
try{
|
|
|
169 |
maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
|
|
|
170 |
}
|
|
|
171 |
catch(TTransportException e){
|
|
|
172 |
catalogClient = catalogServiceClient.getClient();
|
|
|
173 |
maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
|
|
|
174 |
}
|
|
|
175 |
if(nlc !=0 && (maxnlc >= nlc)){
|
|
|
176 |
total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
|
|
|
177 |
heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
|
|
|
178 |
available = available + entry.getValue() - total_held;
|
|
|
179 |
}
|
| 9413 |
vikram.rag |
180 |
else if(maxnlc==0){
|
|
|
181 |
total_held = inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 0);
|
|
|
182 |
heldForSource = heldForSource + total_held - inventoryClient.getHoldInventoryDetailForItemForWarehouseIdExceptSource(thriftItem.getId(), entry.getKey(), 7);
|
|
|
183 |
available = available + itemavailability.get(entry.getKey()) - total_held;
|
|
|
184 |
}
|
|
|
185 |
|
| 9404 |
vikram.rag |
186 |
}
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
Map<Long,Long> itemreserve = new HashMap<Long,Long>();
|
|
|
190 |
itemreserve = iteminventory.getReserved();
|
|
|
191 |
if (thriftItem.isIsWarehousePreferenceSticky() && thriftItem.getPreferredVendor()!=0){
|
|
|
192 |
//System.out.println("Item id "+thriftItem.getId()+".Found prefered vendor and warehouse is marked sticky (Calculating Reserve)"+thriftItem.getPreferredVendor());
|
|
|
193 |
for (Warehouse warehouse:vendor_warehouses){
|
|
|
194 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
195 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
196 |
ignoredItem.setWarehouseId(warehouse.getId());
|
|
|
197 |
if (itemreserve.get(warehouse.getId())!=null && !thirdpartywarehouseids.contains(warehouse.getId()) && !ignoreItems.contains(warehouse.getId())){
|
|
|
198 |
nlc = inventoryClient.getNlcForWarehouse(warehouse.getId(),thriftItem.getId());
|
|
|
199 |
maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
|
|
|
200 |
if(nlc !=0 && (maxnlc >= nlc)){
|
|
|
201 |
reserve = reserve + itemreserve.get(warehouse.getId());
|
| 9413 |
vikram.rag |
202 |
System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
|
| 9404 |
vikram.rag |
203 |
}
|
| 9413 |
vikram.rag |
204 |
else if(maxnlc==0){
|
|
|
205 |
reserve = reserve + itemreserve.get(warehouse.getId());
|
|
|
206 |
System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
|
|
|
207 |
}
|
|
|
208 |
|
| 9404 |
vikram.rag |
209 |
}
|
|
|
210 |
}
|
|
|
211 |
}else{
|
|
|
212 |
for (Map.Entry<Long,Long> entry : itemreserve.entrySet()) {
|
|
|
213 |
in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems ignoredItem = new in.shop2020.model.v1.inventory.IgnoredInventoryUpdateItems();
|
|
|
214 |
ignoredItem.setItemId(thriftItem.getId());
|
|
|
215 |
ignoredItem.setWarehouseId(entry.getKey());
|
|
|
216 |
if(!thirdpartywarehouseids.contains(entry.getKey()) && !ignoreItems.contains(ignoredItem)){
|
|
|
217 |
nlc = inventoryClient.getNlcForWarehouse(entry.getKey(),thriftItem.getId());
|
|
|
218 |
maxnlc = catalogClient.getSnapdealItem(thriftItem.getId()).getMaxNlc();
|
|
|
219 |
if(nlc !=0 && (maxnlc >= nlc)){
|
|
|
220 |
reserve = reserve + entry.getValue();
|
| 9413 |
vikram.rag |
221 |
System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
|
| 9404 |
vikram.rag |
222 |
}
|
| 9413 |
vikram.rag |
223 |
else if(maxnlc==0){
|
|
|
224 |
reserve = reserve + entry.getValue();
|
|
|
225 |
System.out.println("itemId:" + thriftItem.getId() + "\ttotal_held: " + total_held + "\theldForSource:" + heldForSource + "\treserve:" + reserve);
|
|
|
226 |
}
|
|
|
227 |
|
| 9404 |
vikram.rag |
228 |
}
|
| 9413 |
vikram.rag |
229 |
|
| 9404 |
vikram.rag |
230 |
else{
|
|
|
231 |
//System.out.println("skipping inventory for warehouse id " + entry.getKey());
|
|
|
232 |
}
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
item= new SnapdealItemForInventory(thriftItem.getId(),available,reserve,heldForSource,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),thriftItem.isRisky(),thriftItem.getItemStatus());
|
| 9413 |
vikram.rag |
236 |
System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
|
| 9404 |
vikram.rag |
237 |
}
|
|
|
238 |
else{
|
|
|
239 |
item = new SnapdealItemForInventory(thriftItem.getId(),0,0,0,thriftItem.getHoldInventory(),thriftItem.getDefaultInventory(),thriftItem.isRisky(),thriftItem.getItemStatus());
|
| 9413 |
vikram.rag |
240 |
System.out.println("itemId:" + thriftItem.getId() + "\tavailable: " + available + "\treserve" + reserve + "\theldForSource:" + heldForSource + "\twebsite_hold:" + thriftItem.getHoldInventory() + "\tdefault_inv:" +thriftItem.getDefaultInventory());
|
| 9404 |
vikram.rag |
241 |
}
|
|
|
242 |
|
|
|
243 |
//System.out.println(" Item details are " + thriftItem.getId() +" " + available + " " + reserve + " " + thriftItem.getHoldInventory() + " "+ thriftItem.getDefaultInventory() + " " + thriftItem.isRisky());
|
|
|
244 |
//System.out.println("+++++++++++++++++++++++");
|
|
|
245 |
allItemsInventoryMap.put(thriftItem.getId(),item);
|
|
|
246 |
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
} catch (TException e) {
|
|
|
250 |
// TODO Auto-generated catch block
|
|
|
251 |
e.printStackTrace();
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
}
|
|
|
255 |
private static void fetchinventoryfromsnapdeal() throws UnsupportedEncodingException{
|
|
|
256 |
HttpClient client = new DefaultHttpClient();
|
|
|
257 |
HttpPost post = new HttpPost("http://seller.snapdeal.com/login_security_check?spring-security-redirect=http://seller.snapdeal.com/inventory&");
|
|
|
258 |
HttpGet get;
|
|
|
259 |
BufferedReader rd= null;
|
|
|
260 |
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
|
|
261 |
nameValuePairs.add(new BasicNameValuePair("j_username",
|
|
|
262 |
"khushal.bhatia@saholic.com"));
|
|
|
263 |
nameValuePairs.add(new BasicNameValuePair("j_password",
|
|
|
264 |
"sonline"));
|
|
|
265 |
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
|
|
|
266 |
HttpResponse response = null;
|
|
|
267 |
try {
|
|
|
268 |
response = client.execute(post);
|
|
|
269 |
} catch (ClientProtocolException e) {
|
|
|
270 |
// TODO Auto-generated catch block
|
|
|
271 |
e.printStackTrace();
|
|
|
272 |
} catch (IOException e) {
|
|
|
273 |
// TODO Auto-generated catch block
|
|
|
274 |
e.printStackTrace();
|
|
|
275 |
}
|
|
|
276 |
try {
|
|
|
277 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
278 |
} catch (IllegalStateException e1) {
|
|
|
279 |
// TODO Auto-generated catch block
|
|
|
280 |
e1.printStackTrace();
|
|
|
281 |
} catch (IOException e1) {
|
|
|
282 |
// TODO Auto-generated catch block
|
|
|
283 |
e1.printStackTrace();
|
|
|
284 |
}
|
|
|
285 |
String line = "";
|
|
|
286 |
try {
|
|
|
287 |
while ((line = rd.readLine()) != null) {
|
|
|
288 |
System.out.println(line);
|
|
|
289 |
}
|
|
|
290 |
} catch (IOException e) {
|
|
|
291 |
// TODO Auto-generated catch block
|
|
|
292 |
e.printStackTrace();
|
|
|
293 |
}
|
|
|
294 |
int i = 1;
|
|
|
295 |
int items=0;
|
|
|
296 |
long time = System.currentTimeMillis();
|
|
|
297 |
Gson gson = new Gson();
|
|
|
298 |
Client inventoryClient = null;
|
|
|
299 |
try {
|
|
|
300 |
inventoryClient = new InventoryClient().getClient();
|
|
|
301 |
} catch (TTransportException e) {
|
|
|
302 |
// TODO Auto-generated catch block
|
|
|
303 |
e.printStackTrace();
|
|
|
304 |
}
|
|
|
305 |
in.shop2020.model.v1.order.TransactionService.Client transactionClient = null;
|
|
|
306 |
try {
|
| 9417 |
vikram.rag |
307 |
transactionClient = new TransactionClient("support_transaction_service_server_host","transaction_service_server_port").getClient();
|
| 9404 |
vikram.rag |
308 |
} catch (TTransportException e) {
|
|
|
309 |
// TODO Auto-generated catch block
|
|
|
310 |
e.printStackTrace();
|
|
|
311 |
}
|
|
|
312 |
SnapdealInventoryItem inventory_item = null;
|
|
|
313 |
long created_orders = 0;
|
|
|
314 |
long pending_orders;
|
|
|
315 |
inventoryItemList = new ArrayList<Inventory>();
|
|
|
316 |
while(true){
|
| 9423 |
vikram.rag |
317 |
System.out.println("Fetching inventory page " +i);
|
| 9404 |
vikram.rag |
318 |
time = System.currentTimeMillis();
|
|
|
319 |
get = new HttpGet("http://seller.snapdeal.com/inventory/search?gridType=normal&_search=false&nd="+time+"&rows="+30+"&page="+i+"&sidx=&sord=asc");
|
|
|
320 |
try {
|
|
|
321 |
response = client.execute(get);
|
|
|
322 |
} catch (ClientProtocolException e) {
|
|
|
323 |
// TODO Auto-generated catch block
|
|
|
324 |
e.printStackTrace();
|
|
|
325 |
} catch (IOException e) {
|
|
|
326 |
// TODO Auto-generated catch block
|
|
|
327 |
e.printStackTrace();
|
|
|
328 |
}
|
|
|
329 |
try {
|
|
|
330 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
331 |
} catch (IllegalStateException e) {
|
|
|
332 |
// TODO Auto-generated catch block
|
|
|
333 |
e.printStackTrace();
|
|
|
334 |
} catch (IOException e) {
|
|
|
335 |
// TODO Auto-generated catch block
|
|
|
336 |
e.printStackTrace();
|
|
|
337 |
}
|
|
|
338 |
InventoryItems inventoryitems = (InventoryItems) gson.fromJson(rd, InventoryItems.class);
|
|
|
339 |
if(inventoryitems.getRows().size()!=0){
|
| 9424 |
vikram.rag |
340 |
System.out.println("Parsing page " + i);
|
| 9404 |
vikram.rag |
341 |
for(Inventory inventory : inventoryitems.getRows()){
|
|
|
342 |
/*System.out.println("SUPC " + inventory.getSupc());
|
|
|
343 |
System.out.println("SKU " + inventory.getSellerSku());
|
|
|
344 |
System.out.println("Product Name " + inventory.getProductName());
|
|
|
345 |
System.out.println("Quantity " + inventory.getAvailableInventory());
|
|
|
346 |
System.out.println("Pending " + inventory.getPendingAvailableInventory());
|
|
|
347 |
System.out.println("Status " + inventory.isPendingUpdate());
|
|
|
348 |
*/
|
|
|
349 |
pending_orders=0;
|
|
|
350 |
try{
|
|
|
351 |
inventory_item = inventoryClient.getSnapdealInventoryForItem(Long.parseLong(inventory.getSellerSku()));
|
|
|
352 |
}
|
|
|
353 |
catch(NumberFormatException nfe)
|
|
|
354 |
{
|
|
|
355 |
continue;
|
| 9413 |
vikram.rag |
356 |
} catch (Exception e) {
|
| 9404 |
vikram.rag |
357 |
// TODO Auto-generated catch block
|
| 9413 |
vikram.rag |
358 |
try {
|
|
|
359 |
inventoryClient = new InventoryClient().getClient();
|
|
|
360 |
try {
|
|
|
361 |
inventory_item = inventoryClient.getSnapdealInventoryForItem(Long.parseLong(inventory.getSellerSku()));
|
|
|
362 |
} catch (NumberFormatException e1) {
|
|
|
363 |
// TODO Auto-generated catch block
|
|
|
364 |
e1.printStackTrace();
|
|
|
365 |
} catch (TException e1) {
|
|
|
366 |
// TODO Auto-generated catch block
|
|
|
367 |
e1.printStackTrace();
|
|
|
368 |
}
|
|
|
369 |
} catch (TTransportException e1) {
|
|
|
370 |
// TODO Auto-generated catch block
|
|
|
371 |
e1.printStackTrace();
|
|
|
372 |
}
|
| 9404 |
vikram.rag |
373 |
e.printStackTrace();
|
|
|
374 |
}
|
|
|
375 |
if(inventory_item.getItem_id()!=0){
|
|
|
376 |
if(inventory.getAvailableInventory()!=inventory_item.getAvailability()){
|
|
|
377 |
try {
|
| 9413 |
vikram.rag |
378 |
if(inventory_item.getLastUpdatedOnSnapdeal()!=0){
|
|
|
379 |
created_orders = transactionClient.getOrdersCreatedAfterTimestampForSource(inventory_item.getLastUpdatedOnSnapdeal(), 7, Long.parseLong(inventory.getSellerSku()));
|
|
|
380 |
}
|
| 9404 |
vikram.rag |
381 |
} catch (NumberFormatException e) {
|
|
|
382 |
// TODO Auto-generated catch block
|
|
|
383 |
e.printStackTrace();
|
|
|
384 |
} catch (TException e) {
|
|
|
385 |
// TODO Auto-generated catch block
|
|
|
386 |
e.printStackTrace();
|
|
|
387 |
}
|
|
|
388 |
}
|
|
|
389 |
if(created_orders == (inventory_item.getAvailability() - inventory.getAvailableInventory())){
|
|
|
390 |
}
|
|
|
391 |
else{
|
|
|
392 |
pending_orders = (inventory_item.getAvailability() - inventory.getAvailableInventory() - created_orders);
|
|
|
393 |
}
|
|
|
394 |
}
|
|
|
395 |
itemIdpendingOrdersMap.put(Long.parseLong(inventory.getSellerSku()),pending_orders);
|
|
|
396 |
inventoryItemList.add(inventory);
|
|
|
397 |
items++;
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
else{
|
|
|
401 |
System.out.println("Fetched " + items);
|
|
|
402 |
break;
|
|
|
403 |
}
|
|
|
404 |
i++;
|
|
|
405 |
}
|
|
|
406 |
}
|
|
|
407 |
|
|
|
408 |
@SuppressWarnings("deprecation")
|
|
|
409 |
public static void main(String[] args) throws NumberFormatException, TException, ClientProtocolException, IOException {
|
|
|
410 |
fetchinventoryfromsnapdeal();
|
|
|
411 |
calculateinventory();
|
|
|
412 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogServiceClient = null;
|
|
|
413 |
SnapdealItem snapdealitem = null;
|
|
|
414 |
try {
|
|
|
415 |
catalogServiceClient = new CatalogClient().getClient();
|
|
|
416 |
} catch (TTransportException e) {
|
|
|
417 |
// TODO Auto-generated catch block
|
|
|
418 |
e.printStackTrace();
|
|
|
419 |
}
|
|
|
420 |
Client inventoryClient = new InventoryClient().getClient();
|
| 9420 |
vikram.rag |
421 |
FileInputStream fis = new FileInputStream("/root/code/trunk/SnapDealFeeds/SellerInventory.xls");
|
| 9413 |
vikram.rag |
422 |
Workbook hwb = new HSSFWorkbook(fis);
|
|
|
423 |
Sheet sheet = hwb.getSheetAt(0);
|
|
|
424 |
Row row;
|
| 9404 |
vikram.rag |
425 |
int iterator=1;
|
| 9415 |
vikram.rag |
426 |
long availability,reserve,defaultinventory,holdinventory,actualavailability,heldorders,sent_inventory;
|
| 9404 |
vikram.rag |
427 |
SnapdealItemForInventory inventoryItem;
|
|
|
428 |
for(Inventory inventory:inventoryItemList){
|
|
|
429 |
System.out.println(inventory.getSupc()+" "+inventory.getSellerSku()+ " " +inventory.getProductName()+" "+inventory.getAvailableInventory()+" "+inventory.isLive()+" "+itemIdpendingOrdersMap.get(Long.parseLong(inventory.getSellerSku())));
|
|
|
430 |
try{
|
|
|
431 |
snapdealitem = catalogServiceClient.getSnapdealItem(Long.parseLong(inventory.getSellerSku()));
|
|
|
432 |
}
|
|
|
433 |
catch(TTransportException e){
|
|
|
434 |
catalogServiceClient = new CatalogClient().getClient();
|
|
|
435 |
snapdealitem = catalogServiceClient.getSnapdealItem(Long.parseLong(inventory.getSellerSku()));
|
|
|
436 |
}
|
|
|
437 |
if(snapdealitem.getItem_id()!=0 && snapdealitem.isIsListedOnSnapdeal() && !snapdealitem.isSuppressInventoryFeed()){
|
|
|
438 |
inventoryItem = allItemsInventoryMap.get(Long.parseLong(inventory.getSellerSku()));
|
|
|
439 |
if(inventoryItem==null){
|
|
|
440 |
continue;
|
|
|
441 |
}
|
|
|
442 |
availability = inventoryItem.getAvailability();
|
|
|
443 |
reserve = inventoryItem.getReserve();
|
|
|
444 |
defaultinventory = inventoryItem.getDefaultInventory();
|
|
|
445 |
holdinventory = inventoryItem.getHoldInventory();
|
|
|
446 |
actualavailability = availability - reserve;
|
|
|
447 |
heldorders = itemIdpendingOrdersMap.get(Long.parseLong(inventory.getSellerSku()));
|
| 9413 |
vikram.rag |
448 |
System.out.println("itemId:" + inventoryItem.getId() + "\tavailable: " + inventoryItem.getAvailability() + "\treserve" + inventoryItem.getReserve() + "\theldForSource:" + inventoryItem.getHeldForSource() + "\twebsite_hold:" + inventoryItem.getHoldInventory() + "\tdefault_inv:" +inventoryItem.getDefaultInventory() + "\theldorders" + heldorders);
|
| 9404 |
vikram.rag |
449 |
row = sheet.getRow((short) iterator);
|
|
|
450 |
row.getCell((short) 0).setCellValue(inventory.getSupc());
|
|
|
451 |
row.getCell((short) 1).setCellValue(inventory.getSellerSku());
|
|
|
452 |
row.getCell((short) 2).setCellValue(inventory.getProductName());
|
|
|
453 |
row.getCell((short) 3).setCellValue(inventory.getAvailableInventory());
|
|
|
454 |
long inventoryForRiskyItem = actualavailability - holdinventory;
|
|
|
455 |
if(inventoryItem.getRisky()){
|
|
|
456 |
if(inventoryForRiskyItem > 0){
|
| 9413 |
vikram.rag |
457 |
if((Math.round(inventoryForRiskyItem*2/3) - heldorders + inventoryItem.getHeldForSource()) > 0){
|
|
|
458 |
row.getCell((short) 4).setCellValue(Math.round(inventoryForRiskyItem*2/3) - heldorders + inventoryItem.getHeldForSource());
|
| 9415 |
vikram.rag |
459 |
sent_inventory = Math.round(inventoryForRiskyItem*2/3) - heldorders + inventoryItem.getHeldForSource();
|
| 9404 |
vikram.rag |
460 |
}
|
|
|
461 |
else{
|
|
|
462 |
row.getCell((short) 4).setCellValue(0);
|
| 9415 |
vikram.rag |
463 |
sent_inventory = 0;
|
| 9404 |
vikram.rag |
464 |
}
|
|
|
465 |
}
|
|
|
466 |
else{
|
|
|
467 |
row.getCell((short) 4).setCellValue(0);
|
| 9415 |
vikram.rag |
468 |
sent_inventory = 0;
|
| 9404 |
vikram.rag |
469 |
}
|
|
|
470 |
}
|
|
|
471 |
else{
|
|
|
472 |
if(actualavailability > defaultinventory){
|
| 9413 |
vikram.rag |
473 |
if((Math.round(actualavailability*2/3) + inventoryItem.getHeldForSource()) > heldorders){
|
|
|
474 |
System.out.println("actual" + actualavailability + "\t2/3actual" + Math.round(actualavailability*2/3) + "\theld for source" + inventoryItem.getHeldForSource() + "\theldorders" + heldorders + "\tFinal Value is : " + (Math.round(actualavailability*2/3) + inventoryItem.getHeldForSource() - heldorders));
|
|
|
475 |
row.getCell((short) 4).setCellValue(Math.round(actualavailability*2/3) + inventoryItem.getHeldForSource() - heldorders);
|
| 9415 |
vikram.rag |
476 |
sent_inventory = Math.round(actualavailability*2/3) + inventoryItem.getHeldForSource() - heldorders;
|
| 9404 |
vikram.rag |
477 |
}
|
|
|
478 |
else{
|
|
|
479 |
row.getCell((short) 4).setCellValue(0);
|
| 9415 |
vikram.rag |
480 |
sent_inventory = 0;
|
| 9404 |
vikram.rag |
481 |
}
|
|
|
482 |
}
|
|
|
483 |
else{
|
|
|
484 |
if(defaultinventory > heldorders){
|
|
|
485 |
row.getCell((short) 4).setCellValue(defaultinventory - heldorders);
|
| 9415 |
vikram.rag |
486 |
sent_inventory = defaultinventory - heldorders;
|
| 9404 |
vikram.rag |
487 |
}
|
|
|
488 |
else{
|
|
|
489 |
row.getCell((short) 4).setCellValue(0);
|
| 9415 |
vikram.rag |
490 |
sent_inventory = 0;
|
| 9404 |
vikram.rag |
491 |
}
|
|
|
492 |
}
|
|
|
493 |
}
|
|
|
494 |
if(inventory.isLive()){
|
|
|
495 |
row.getCell((short) 5).setCellValue("Yes");
|
|
|
496 |
}
|
|
|
497 |
else{
|
|
|
498 |
row.getCell((short) 5).setCellValue("No");
|
|
|
499 |
}
|
| 9415 |
vikram.rag |
500 |
SnapdealInventoryItem snapdealinventoryitem = new SnapdealInventoryItem();
|
|
|
501 |
snapdealinventoryitem.setAvailability(sent_inventory);
|
|
|
502 |
snapdealinventoryitem.setItem_id(Long.parseLong(inventory.getSellerSku()));
|
|
|
503 |
snapdealinventoryitem.setLastUpdatedOnSnapdeal(time);
|
| 9417 |
vikram.rag |
504 |
try{
|
|
|
505 |
inventoryClient.addOrUpdateSnapdealInventoryForItem(snapdealinventoryitem);
|
|
|
506 |
}
|
|
|
507 |
catch(TTransportException e){
|
| 9422 |
vikram.rag |
508 |
inventoryClient = new InventoryClient().getClient();
|
| 9417 |
vikram.rag |
509 |
inventoryClient.addOrUpdateSnapdealInventoryForItem(snapdealinventoryitem);
|
|
|
510 |
}
|
| 9404 |
vikram.rag |
511 |
iterator++;
|
|
|
512 |
}
|
|
|
513 |
else{
|
|
|
514 |
continue;
|
|
|
515 |
}
|
|
|
516 |
}
|
|
|
517 |
FileOutputStream fileOut = null;
|
|
|
518 |
fis.close();
|
|
|
519 |
try {
|
|
|
520 |
fileOut = new FileOutputStream(SNAPDEAL_INVENTORY_SHEET);
|
|
|
521 |
} catch (FileNotFoundException e) {
|
|
|
522 |
// TODO Auto-generated catch block
|
|
|
523 |
e.printStackTrace();
|
|
|
524 |
}
|
|
|
525 |
try {
|
|
|
526 |
hwb.write(fileOut);
|
|
|
527 |
} catch (IOException e) {
|
|
|
528 |
// TODO Auto-generated catch block
|
|
|
529 |
e.printStackTrace();
|
|
|
530 |
}
|
| 9413 |
vikram.rag |
531 |
/* HttpClient client = new DefaultHttpClient();
|
| 9404 |
vikram.rag |
532 |
HttpPost post = new HttpPost("http://seller.snapdeal.com/login_security_check?spring-security-redirect=http://seller.snapdeal.com/inventory&");
|
|
|
533 |
BufferedReader rd= null;
|
| 9405 |
vikram.rag |
534 |
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
| 9404 |
vikram.rag |
535 |
nameValuePairs.add(new BasicNameValuePair("j_username",
|
|
|
536 |
"khushal.bhatia@saholic.com"));
|
|
|
537 |
nameValuePairs.add(new BasicNameValuePair("j_password",
|
|
|
538 |
"sonline"));
|
| 9405 |
vikram.rag |
539 |
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
|
| 9404 |
vikram.rag |
540 |
HttpResponse response = null;
|
|
|
541 |
String line;
|
| 9405 |
vikram.rag |
542 |
try {
|
| 9404 |
vikram.rag |
543 |
response = client.execute(post);
|
|
|
544 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
545 |
while ((line = rd.readLine()) != null) {
|
|
|
546 |
System.out.println(line);
|
|
|
547 |
}
|
|
|
548 |
|
|
|
549 |
} catch (ClientProtocolException e) {
|
|
|
550 |
// TODO Auto-generated catch block
|
|
|
551 |
e.printStackTrace();
|
|
|
552 |
} catch (IOException e) {
|
|
|
553 |
// TODO Auto-generated catch block
|
|
|
554 |
e.printStackTrace();
|
|
|
555 |
}
|
| 9405 |
vikram.rag |
556 |
post = new HttpPost("http://seller.snapdeal.com/inventory/upload");
|
| 9404 |
vikram.rag |
557 |
File file = new File("/home/vikram/snapdeal-inventory-1389608325312.xls");
|
|
|
558 |
MultipartEntity mpEntity = new MultipartEntity();
|
|
|
559 |
ContentBody cbFile = new FileBody(file,"application/vnd.ms-excel");
|
|
|
560 |
mpEntity.addPart("file", cbFile);
|
|
|
561 |
post.setEntity(mpEntity);
|
|
|
562 |
response = client.execute(post);
|
|
|
563 |
try {
|
|
|
564 |
rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
565 |
} catch (IllegalStateException e1) {
|
|
|
566 |
// TODO Auto-generated catch block
|
|
|
567 |
e1.printStackTrace();
|
|
|
568 |
} catch (IOException e1) {
|
|
|
569 |
// TODO Auto-generated catch block
|
|
|
570 |
e1.printStackTrace();
|
|
|
571 |
}
|
|
|
572 |
line = "";
|
|
|
573 |
try {
|
|
|
574 |
while ((line = rd.readLine()) != null) {
|
|
|
575 |
System.out.println(line);
|
|
|
576 |
}
|
|
|
577 |
} catch (IOException e) {
|
|
|
578 |
// TODO Auto-generated catch block
|
|
|
579 |
e.printStackTrace();
|
|
|
580 |
}
|
| 9413 |
vikram.rag |
581 |
*/
|
| 9404 |
vikram.rag |
582 |
}
|
|
|
583 |
|
|
|
584 |
}
|