Subversion Repositories SmartDukaan

Rev

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