Subversion Repositories SmartDukaan

Rev

Rev 4846 | Rev 5361 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4846 Rev 5110
Line 1... Line 1...
1
package in.shop2020.inventory.controllers;
1
package in.shop2020.inventory.controllers;
2
 
2
 
3
import in.shop2020.generic.ExceptionType;
-
 
4
import in.shop2020.model.v1.catalog.InventoryServiceException;
3
import in.shop2020.model.v1.catalog.InventoryType;
5
import in.shop2020.model.v1.catalog.Item;
4
import in.shop2020.model.v1.catalog.Item;
-
 
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;
6
import in.shop2020.purchase.PurchaseServiceException;
9
import in.shop2020.purchase.PurchaseServiceException;
7
import in.shop2020.thrift.clients.CatalogClient;
10
import in.shop2020.thrift.clients.CatalogClient;
8
import in.shop2020.thrift.clients.PurchaseClient;
11
import in.shop2020.thrift.clients.PurchaseClient;
9
import in.shop2020.thrift.clients.WarehouseClient;
12
import in.shop2020.thrift.clients.WarehouseClient;
10
import in.shop2020.utils.ModelUtils;
13
import in.shop2020.utils.ModelUtils;
Line 12... Line 15...
12
import in.shop2020.warehouse.ScanType;
15
import in.shop2020.warehouse.ScanType;
13
import in.shop2020.warehouse.WarehouseService.Client;
16
import in.shop2020.warehouse.WarehouseService.Client;
14
import in.shop2020.warehouse.WarehouseServiceException;
17
import in.shop2020.warehouse.WarehouseServiceException;
15
 
18
 
16
import java.util.ArrayList;
19
import java.util.ArrayList;
-
 
20
import java.util.HashMap;
17
import java.util.List;
21
import java.util.List;
-
 
22
import java.util.Map;
18
 
23
 
19
import javax.servlet.ServletContext;
24
import javax.servlet.ServletContext;
20
 
25
 
21
import org.apache.struts2.convention.annotation.Result;
26
import org.apache.struts2.convention.annotation.Result;
22
import org.apache.struts2.convention.annotation.Results;
27
import org.apache.struts2.convention.annotation.Results;
Line 27... Line 32...
27
 
32
 
28
@SuppressWarnings("serial")
33
@SuppressWarnings("serial")
29
@Results({ @Result(name = "redirect", type = "redirectAction", params = {
34
@Results({ @Result(name = "redirect", type = "redirectAction", params = {
30
        "actionName", "warehouse" }) })
35
        "actionName", "warehouse" }) })
31
public class PurchaseController extends BaseController {
36
public class PurchaseController extends BaseController {
-
 
37
    /**
-
 
38
     * 
32
 
39
     */
33
    private static Logger logger = LoggerFactory
40
    private static final int NUM_BULK__SCAN_ITEMS = 10;
34
            .getLogger(PurchaseController.class);
41
    private static Logger logger = LoggerFactory.getLogger(PurchaseController.class);
-
 
42
    private static enum ScanRecordType { VALID, BLANK };
35
 
43
 
36
    private ServletContext context;
44
    private ServletContext context;
37
    private String id;
45
    private String id;
38
    private String itemId;
46
    private String itemId;
39
    private String itemNo;
47
    private String itemNo;
40
    private String errorMsg = "";
48
    private String errorMsg = "";
41
    private List<Item> items;
49
    private List<Item> items;
-
 
50
    private List<LineItem> lineItems;
42
 
51
 
43
    private String purchaseOrderId;
52
    private String purchaseOrderId;
-
 
53
    private Warehouse warehouse;
44
 
54
 
45
    public String editNew() {
55
    public String editNew() {
46
        purchaseOrderId = request.getParameter("poId");
56
        purchaseOrderId = request.getParameter("poId");
47
        return "new";
57
        return "new";
48
    }
58
    }
Line 79... Line 89...
79
        }
89
        }
80
 
90
 
81
    }
91
    }
82
 
92
 
83
    public String show() {
93
    public String show() {
-
 
94
        resetLineItems();
-
 
95
        setItemsFromPO(Long.parseLong(id));
84
        return "show";
96
        return SHOW;
-
 
97
    }
-
 
98
 
-
 
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
        }
85
    }
111
    }
86
 
112
 
87
    public String update() {
113
    public String update() {
88
        long id = Long.parseLong(this.id);
114
        long id = Long.parseLong(this.id);
89
        String itemNumber = request.getParameter("itemNo");
115
        setItemsFromPO(id);
90
        String imeiNumber = request.getParameter("imeiNo");
-
 
91
 
116
 
92
        try {
117
        try {
-
 
118
            if (!areValidScans()) {
-
 
119
                return SHOW;
-
 
120
            }
-
 
121
 
93
            WarehouseClient warehouseClient = new WarehouseClient();
122
            WarehouseClient warehouseClient = new WarehouseClient();
94
            Client client = warehouseClient.getClient();
123
            Client client = warehouseClient.getClient();
95
            InventoryItem inventoryItem = null;
-
 
96
            if (itemId != null) {
-
 
97
                inventoryItem = client.createSerializedInventoryItem(Long.parseLong(itemId), itemNumber, imeiNumber, id);
-
 
98
            }
-
 
99
            else {
-
 
100
                inventoryItem = client.createSerializedInventoryItemFromItemNumber(itemNumber, imeiNumber, id);
-
 
101
            }
-
 
102
 
124
 
-
 
125
            for (LineItem lineItem : lineItems) {
-
 
126
                if (ScanRecordType.BLANK.name().equals(lineItem.getExtra_info())) {
-
 
127
                    continue;
-
 
128
                }
-
 
129
 
103
            client.scanSerializedItem(inventoryItem.getId(), ScanType.PURCHASE,
130
                InventoryItem inventoryItem = null;
-
 
131
 
104
                    PurchaseOrderController.WAREHOUSE_ID);
132
                if (lineItem.getItem_id() != -1) {
105
            in.shop2020.model.v1.catalog.InventoryService.Client catalaogClient = new CatalogClient()
133
                    inventoryItem = client.createSerializedInventoryItem(lineItem.getItem_id(), lineItem.getItem_number(), lineItem.getSerial_number(), id);
-
 
134
                }
106
                    .getClient();
135
                else {
-
 
136
                    inventoryItem = client.createSerializedInventoryItemFromItemNumber(lineItem.getItem_number(), lineItem.getSerial_number(), id);
-
 
137
                }
-
 
138
 
107
            catalaogClient.addInventory(inventoryItem.getItemId(), PurchaseOrderController.WAREHOUSE_ID,
139
                client.scanSerializedItem(inventoryItem, ScanType.PURCHASE, PurchaseOrderController.WAREHOUSE_ID);
-
 
140
            }
-
 
141
            
108
                    1);
142
            resetLineItems();
109
        } catch (TTransportException e) {
143
        } catch (TTransportException e) {
110
            errorMsg = "Error while establishing connection to the warehouse server";
144
            errorMsg = "Error while establishing connection to the warehouse server";
111
            logger.error(errorMsg, e);
145
            logger.error(errorMsg, e);
112
        } catch (WarehouseServiceException e) {
146
        } catch (WarehouseServiceException e) {
113
            errorMsg = e.getMessage();
147
            errorMsg = e.getMessage();
114
            logger.error(errorMsg, e);
148
            logger.error(errorMsg, e);
115
            if (ExceptionType.MULTIPLE_ITEMS_FOUND.equals(e.getExceptionType())) {
-
 
116
                try {
-
 
117
                    items = new ArrayList<Item>();
-
 
118
                    in.shop2020.model.v1.catalog.InventoryService.Client catalogClient = new CatalogClient().getClient();
-
 
119
                    for (String itemId : e.getMessage().split(",")) {
-
 
120
                        items.add(catalogClient.getItem(Long.parseLong(itemId)));
-
 
121
                    }
-
 
122
                }
-
 
123
                catch(Exception e1) {
-
 
124
                    logger.error("Could not lookup items: " + e.getMessage(), e1);
-
 
125
                }
-
 
126
                errorMsg = "Multiple items found for Item number: " + itemNumber + ". Please select appropriate item.";
-
 
127
            }
-
 
128
        } catch (TException e) {
149
        } catch (TException e) {
129
            errorMsg = "Error while scanning in the item";
150
            errorMsg = "Error while scanning in the item";
130
            logger.error(errorMsg, e);
151
            logger.error(errorMsg, e);
131
        } catch (InventoryServiceException e) {
-
 
132
            errorMsg = "Error while incresing item's inventory availability";
-
 
133
            logger.error(errorMsg, e);
-
 
134
        }
152
        }
-
 
153
 
135
        if (!errorMsg.isEmpty()) {
154
        if (!errorMsg.isEmpty()) {
136
            addActionError(errorMsg);
155
            addActionError(errorMsg);
137
        }
156
        }
138
 
157
 
139
        return "show";
158
        return SHOW;
-
 
159
    }
-
 
160
 
-
 
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
        }
140
    }
268
    }
141
 
269
 
142
    public String destroy() {
270
    public String destroy() {
143
        long id = Long.parseLong(this.id);
271
        long id = Long.parseLong(this.id);
144
 
272
 
Line 173... Line 301...
173
        }
301
        }
174
 
302
 
175
        return show();
303
        return show();
176
    }
304
    }
177
 
305
 
-
 
306
    public String itemNumberMappingEditNew() {
-
 
307
        return "item-number-mapping";
-
 
308
    }
-
 
309
 
178
    public String getName(Item item){
310
    public String getName(Item item){
179
        return ModelUtils.extractProductNameFromItem(item);
311
        return ModelUtils.extractProductNameFromItem(item);
180
    }
312
    }
181
 
313
 
182
    public void setId(String id) {
314
    public void setId(String id) {
Line 221... Line 353...
221
 
353
 
222
    public void setItems(List<Item> items) {
354
    public void setItems(List<Item> items) {
223
        this.items = items;
355
        this.items = items;
224
    }
356
    }
225
 
357
 
-
 
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
    }
226
}
369
}