Subversion Repositories SmartDukaan

Rev

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