Subversion Repositories SmartDukaan

Rev

Rev 4846 | Rev 5361 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4687 mandeep.dh 1
package in.shop2020.inventory.controllers;
2
 
5110 mandeep.dh 3
import in.shop2020.model.v1.catalog.InventoryType;
4846 mandeep.dh 4
import in.shop2020.model.v1.catalog.Item;
5110 mandeep.dh 5
import in.shop2020.model.v1.catalog.Warehouse;
6
import in.shop2020.model.v1.catalog.WarehouseType;
7
import in.shop2020.model.v1.order.LineItem;
8
import in.shop2020.purchase.PurchaseOrder;
4687 mandeep.dh 9
import in.shop2020.purchase.PurchaseServiceException;
10
import in.shop2020.thrift.clients.CatalogClient;
11
import in.shop2020.thrift.clients.PurchaseClient;
12
import in.shop2020.thrift.clients.WarehouseClient;
4846 mandeep.dh 13
import in.shop2020.utils.ModelUtils;
4687 mandeep.dh 14
import in.shop2020.warehouse.InventoryItem;
15
import in.shop2020.warehouse.ScanType;
16
import in.shop2020.warehouse.WarehouseService.Client;
17
import in.shop2020.warehouse.WarehouseServiceException;
18
 
4846 mandeep.dh 19
import java.util.ArrayList;
5110 mandeep.dh 20
import java.util.HashMap;
4846 mandeep.dh 21
import java.util.List;
5110 mandeep.dh 22
import java.util.Map;
4846 mandeep.dh 23
 
4687 mandeep.dh 24
import javax.servlet.ServletContext;
25
 
26
import org.apache.struts2.convention.annotation.Result;
27
import org.apache.struts2.convention.annotation.Results;
28
import org.apache.thrift.TException;
29
import org.apache.thrift.transport.TTransportException;
30
import org.slf4j.Logger;
31
import org.slf4j.LoggerFactory;
32
 
33
@SuppressWarnings("serial")
34
@Results({ @Result(name = "redirect", type = "redirectAction", params = {
35
        "actionName", "warehouse" }) })
36
public class PurchaseController extends BaseController {
5110 mandeep.dh 37
    /**
38
     * 
39
     */
40
    private static final int NUM_BULK__SCAN_ITEMS = 10;
41
    private static Logger logger = LoggerFactory.getLogger(PurchaseController.class);
42
    private static enum ScanRecordType { VALID, BLANK };
4687 mandeep.dh 43
 
44
    private ServletContext context;
45
    private String id;
46
    private String itemId;
47
    private String itemNo;
48
    private String errorMsg = "";
4846 mandeep.dh 49
    private List<Item> items;
5110 mandeep.dh 50
    private List<LineItem> lineItems;
4687 mandeep.dh 51
 
52
    private String purchaseOrderId;
5110 mandeep.dh 53
    private Warehouse warehouse;
4687 mandeep.dh 54
 
55
    public String editNew() {
56
        purchaseOrderId = request.getParameter("poId");
57
        return "new";
58
    }
59
 
60
    public String create() {
61
        this.purchaseOrderId = request.getParameter("poId");
62
        long poId = Long.parseLong(purchaseOrderId);
63
        String invoiceNumber = request.getParameter("invoiceNo");
64
        String fc = request.getParameter("freightCharges").trim();
65
        double freightCharges = 0;
66
        if (fc != null && !fc.isEmpty())
67
            freightCharges = Double.parseDouble(fc);
68
        try {
69
            PurchaseClient purchaseClient = new PurchaseClient();
70
            in.shop2020.purchase.PurchaseService.Client client = purchaseClient
71
                    .getClient();
72
            id = "" + client.startPurchase(poId, invoiceNumber, freightCharges);
73
        } catch (TTransportException e) {
74
            errorMsg = "Error while establishing connection to the warehouse server";
75
            logger.error(errorMsg, e);
76
        } catch (TException e) {
77
            errorMsg = "Error while scanning in the item";
78
            logger.error(errorMsg, e);
79
        } catch (PurchaseServiceException e) {
80
            errorMsg = e.getMessage();
81
            logger.error(errorMsg, e);
82
        }
83
 
84
        if (errorMsg.isEmpty())
85
            return "create";
86
        else {
87
            addActionError(errorMsg);
88
            return "new";
89
        }
90
 
91
    }
92
 
93
    public String show() {
5110 mandeep.dh 94
        resetLineItems();
95
        setItemsFromPO(Long.parseLong(id));
96
        return SHOW;
4687 mandeep.dh 97
    }
98
 
5110 mandeep.dh 99
    private void resetLineItems() {
100
        lineItems = new ArrayList<LineItem>();
101
 
102
        for (int i = 0; i < NUM_BULK__SCAN_ITEMS; i++) {
103
            LineItem lineItem = new LineItem();
104
            lineItem.setId(i);
105
            lineItem.setExtra_info("");
106
            lineItem.setSerial_number("");
107
            lineItem.setItem_number("");
108
            lineItem.setItem_id(-1);
109
            lineItems.add(lineItem);
110
        }
111
    }
112
 
4687 mandeep.dh 113
    public String update() {
114
        long id = Long.parseLong(this.id);
5110 mandeep.dh 115
        setItemsFromPO(id);
4687 mandeep.dh 116
 
117
        try {
5110 mandeep.dh 118
            if (!areValidScans()) {
119
                return SHOW;
120
            }
121
 
4687 mandeep.dh 122
            WarehouseClient warehouseClient = new WarehouseClient();
123
            Client client = warehouseClient.getClient();
5110 mandeep.dh 124
 
125
            for (LineItem lineItem : lineItems) {
126
                if (ScanRecordType.BLANK.name().equals(lineItem.getExtra_info())) {
127
                    continue;
128
                }
129
 
130
                InventoryItem inventoryItem = null;
131
 
132
                if (lineItem.getItem_id() != -1) {
133
                    inventoryItem = client.createSerializedInventoryItem(lineItem.getItem_id(), lineItem.getItem_number(), lineItem.getSerial_number(), id);
134
                }
135
                else {
136
                    inventoryItem = client.createSerializedInventoryItemFromItemNumber(lineItem.getItem_number(), lineItem.getSerial_number(), id);
137
                }
138
 
139
                client.scanSerializedItem(inventoryItem, ScanType.PURCHASE, PurchaseOrderController.WAREHOUSE_ID);
4846 mandeep.dh 140
            }
5110 mandeep.dh 141
 
142
            resetLineItems();
4687 mandeep.dh 143
        } catch (TTransportException e) {
144
            errorMsg = "Error while establishing connection to the warehouse server";
145
            logger.error(errorMsg, e);
146
        } catch (WarehouseServiceException e) {
147
            errorMsg = e.getMessage();
148
            logger.error(errorMsg, e);
149
        } catch (TException e) {
150
            errorMsg = "Error while scanning in the item";
151
            logger.error(errorMsg, e);
152
        }
5110 mandeep.dh 153
 
4687 mandeep.dh 154
        if (!errorMsg.isEmpty()) {
155
            addActionError(errorMsg);
156
        }
4846 mandeep.dh 157
 
5110 mandeep.dh 158
        return SHOW;
4687 mandeep.dh 159
    }
160
 
5110 mandeep.dh 161
    /**
162
     * @return
163
     * @throws TException 
164
     * @throws NumberFormatException 
165
     */
166
    private boolean areValidScans() throws NumberFormatException, TException {
167
        boolean areValidScans = true;
168
        in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();
169
        PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrderForPurchase(Long.parseLong(id));
170
        Map<Long, Long> itemsQuantityMapFromPO = new HashMap<Long, Long>();
171
        for (in.shop2020.purchase.LineItem lineItem : purchaseOrder.getLineitems()) {
172
            itemsQuantityMapFromPO.put(lineItem.getItemId(), (long) lineItem.getUnfulfilledQuantity());
173
        }
174
 
175
        Client warehouseClient = new WarehouseClient().getClient();
176
 
177
        for (LineItem lineItem : lineItems) {
178
            if (lineItem.getItem_id() == -1 && lineItem.getItem_number().isEmpty() && lineItem.getSerial_number().isEmpty()) {
179
                lineItem.setExtra_info(ScanRecordType.BLANK.name());
180
            }
181
            else {
182
                lineItem.setExtra_info(ScanRecordType.VALID.name());
183
                if (lineItem.getSerial_number().isEmpty()) {
184
                    lineItem.setExtra_info("Serial number not present.");
185
                    areValidScans = false;
186
                    continue;
187
                }
188
 
189
                if (lineItem.getItem_id() == -1 && lineItem.getItem_number().isEmpty()) {
190
                    lineItem.setExtra_info("Item not selected/Item number not present.");
191
                    areValidScans = false;
192
                    continue;
193
                }
194
 
195
                if (lineItem.getItem_id() == -1) {
196
                    List<Long> itemIds = warehouseClient.getItemIds(lineItem.getItem_number());
197
                    if (itemIds.isEmpty()) {
198
                        lineItem.setExtra_info("Unknown item number");
199
                        areValidScans = false;
200
                        continue;
201
                    }
202
 
203
                    if (itemIds.size() > 0) {
204
                        int numItemsInPO = 0;
205
                        for (long itemId : itemIds) {
206
                            if (itemsQuantityMapFromPO.containsKey(itemId)) {
207
                                numItemsInPO++;
208
                                lineItem.setItem_id(itemId);
209
                            }
210
                        }
211
 
212
                        if (numItemsInPO > 1) {
213
                            lineItem.setExtra_info("Multiple items found for given item Number. Choose item explicitly.");
214
                            areValidScans = false;
215
                            continue;                            
216
                        }
217
                    }
218
                }
219
 
220
                if (!itemsQuantityMapFromPO.containsKey(lineItem.getItem_id())) {
221
                    lineItem.setExtra_info("Item not present in PO.");
222
                    areValidScans = false;
223
                    continue;
224
                }
225
 
226
                itemsQuantityMapFromPO.put(lineItem.getItem_id(), itemsQuantityMapFromPO.get(lineItem.getItem_id()) - 1);
227
 
228
                if (itemsQuantityMapFromPO.get(lineItem.getItem_id()) < 0) {
229
                    lineItem.setExtra_info("Item already fulfilled in PO.");
230
                    areValidScans = false;
231
                    continue;
232
                }
233
 
234
                try {
235
                    warehouseClient.getInventoryItem(lineItem.getSerial_number());
236
                    lineItem.setExtra_info("Item scanned already.");
237
                    areValidScans = false;
238
                    continue;
239
                }
240
                catch (Exception e) {
241
                }
242
            }
243
        }
244
 
245
        return areValidScans;
246
    }
247
 
248
    private void setItemsFromPO(long id) {
249
        try {
250
            items = new ArrayList<Item>();
251
            in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();
252
            PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrderForPurchase(id);
253
            for (in.shop2020.purchase.LineItem lineItem : purchaseOrder.getLineitems()) {
254
                Item item = new Item();
255
                item.setId(lineItem.getItemId());
256
                item.setBrand(lineItem.getBrand());
257
                item.setModelName(lineItem.getModelName());
258
                item.setModelNumber(lineItem.getModelNumber());
259
                item.setColor(lineItem.getColor());
260
                items.add(item);
261
            }
262
 
263
            in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = new CatalogClient().getClient();
264
            warehouse = catalogClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, purchaseOrder.getSupplierId(), PurchaseOrderController.WAREHOUSE_ID, PurchaseOrderController.WAREHOUSE_ID).get(0);
265
        } catch (Exception e) {
266
            logger.error("Could not find items in PO with purchase: " + id, e);
267
        }
268
    }
269
 
4687 mandeep.dh 270
    public String destroy() {
271
        long id = Long.parseLong(this.id);
272
 
273
        try {
274
            PurchaseClient warehouseClient = new PurchaseClient();
275
            in.shop2020.purchase.PurchaseService.Client client = warehouseClient
276
                    .getClient();
277
            client.closePurchase(id);
278
        } catch (TTransportException e) {
279
            errorMsg = "Error while establishing connection to the warehouse server";
280
            logger.error(errorMsg, e);
281
        } catch (TException e) {
282
            errorMsg = "Error while scanning in the item";
283
            logger.error(errorMsg, e);
284
        } catch (PurchaseServiceException e) {
285
            errorMsg = e.getMessage();
286
            logger.error(errorMsg, e);
287
        }
288
        return "redirect";
289
    }
290
 
291
    public String createItemNumberMapping() {
292
        long itemIdLong = Long.parseLong(itemId);
293
 
294
        try {
295
            Client warehouseClient = new WarehouseClient().getClient();
296
            warehouseClient.createItemNumberMapping(itemNo, itemIdLong);
297
        } catch (TTransportException e) {
298
            logger.error("Could not create thrift client", e);
299
        } catch (TException e) {
300
            logger.error("Could not create item number mapping", e);
301
        }
302
 
303
        return show();
304
    }
305
 
5110 mandeep.dh 306
    public String itemNumberMappingEditNew() {
307
        return "item-number-mapping";
308
    }
309
 
4846 mandeep.dh 310
    public String getName(Item item){
311
        return ModelUtils.extractProductNameFromItem(item);
312
    }
313
 
4687 mandeep.dh 314
    public void setId(String id) {
315
        this.id = id;
316
    }
317
 
318
    public String getId() {
319
        return id;
320
    }
321
 
322
    public String getErrorMessage() {
323
        return errorMsg;
324
    }
325
 
326
    public String getPurchaseOrderId() {
327
        return purchaseOrderId;
328
    }
329
 
330
    public String getServletContextPath() {
331
        return context.getContextPath();
332
    }
333
 
334
    public String getItemId() {
335
        return itemId;
336
    }
337
 
338
    public void setItemId(String itemId) {
339
        this.itemId = itemId;
340
    }
341
 
342
    public String getItemNo() {
343
        return itemNo;
344
    }
345
 
346
    public void setItemNo(String itemNo) {
347
        this.itemNo = itemNo;
348
    }
349
 
4846 mandeep.dh 350
    public List<Item> getItems() {
351
        return items;
352
    }
353
 
354
    public void setItems(List<Item> items) {
355
        this.items = items;
356
    }
357
 
5110 mandeep.dh 358
    public List<LineItem> getLineItems() {
359
        return lineItems;
360
    }
361
 
362
    public void setLineItems(List<LineItem> lineItems) {
363
        this.lineItems = lineItems;
364
    }
365
 
366
    public Warehouse getWarehouse() {
367
        return warehouse;
368
    }
4687 mandeep.dh 369
}