Subversion Repositories SmartDukaan

Rev

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