| 4687 |
mandeep.dh |
1 |
package in.shop2020.inventory.controllers;
|
|
|
2 |
|
| 4846 |
mandeep.dh |
3 |
import in.shop2020.model.v1.catalog.Item;
|
| 5945 |
mandeep.dh |
4 |
import in.shop2020.model.v1.inventory.InventoryType;
|
|
|
5 |
import in.shop2020.model.v1.inventory.Warehouse;
|
|
|
6 |
import in.shop2020.model.v1.inventory.WarehouseType;
|
| 5110 |
mandeep.dh |
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;
|
| 5945 |
mandeep.dh |
12 |
import in.shop2020.thrift.clients.InventoryClient;
|
| 4687 |
mandeep.dh |
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());
|
| 5530 |
mandeep.dh |
158 |
inventoryItem.setInitialQuantity(new Double(lineItem.getQuantity()).longValue());
|
|
|
159 |
client.scan(inventoryItem, ScanType.PURCHASE, new Double(lineItem.getQuantity()).longValue(), PurchaseOrderController.WAREHOUSE_ID);
|
| 5361 |
mandeep.dh |
160 |
}
|
| 5110 |
mandeep.dh |
161 |
|
|
|
162 |
resetLineItems();
|
| 4687 |
mandeep.dh |
163 |
} catch (TTransportException e) {
|
|
|
164 |
errorMsg = "Error while establishing connection to the warehouse server";
|
|
|
165 |
logger.error(errorMsg, e);
|
|
|
166 |
} catch (WarehouseServiceException e) {
|
|
|
167 |
errorMsg = e.getMessage();
|
|
|
168 |
logger.error(errorMsg, e);
|
|
|
169 |
} catch (TException e) {
|
|
|
170 |
errorMsg = "Error while scanning in the item";
|
|
|
171 |
logger.error(errorMsg, e);
|
|
|
172 |
}
|
| 5110 |
mandeep.dh |
173 |
|
| 4687 |
mandeep.dh |
174 |
if (!errorMsg.isEmpty()) {
|
|
|
175 |
addActionError(errorMsg);
|
|
|
176 |
}
|
| 4846 |
mandeep.dh |
177 |
|
| 5110 |
mandeep.dh |
178 |
return SHOW;
|
| 4687 |
mandeep.dh |
179 |
}
|
|
|
180 |
|
| 5110 |
mandeep.dh |
181 |
/**
|
|
|
182 |
* @return
|
|
|
183 |
* @throws TException
|
|
|
184 |
* @throws NumberFormatException
|
|
|
185 |
*/
|
|
|
186 |
private boolean areValidScans() throws NumberFormatException, TException {
|
|
|
187 |
boolean areValidScans = true;
|
|
|
188 |
in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();
|
|
|
189 |
PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrderForPurchase(Long.parseLong(id));
|
|
|
190 |
Map<Long, Long> itemsQuantityMapFromPO = new HashMap<Long, Long>();
|
|
|
191 |
for (in.shop2020.purchase.LineItem lineItem : purchaseOrder.getLineitems()) {
|
|
|
192 |
itemsQuantityMapFromPO.put(lineItem.getItemId(), (long) lineItem.getUnfulfilledQuantity());
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
Client warehouseClient = new WarehouseClient().getClient();
|
|
|
196 |
for (LineItem lineItem : lineItems) {
|
|
|
197 |
if (lineItem.getItem_id() == -1 && lineItem.getItem_number().isEmpty() && lineItem.getSerial_number().isEmpty()) {
|
|
|
198 |
lineItem.setExtra_info(ScanRecordType.BLANK.name());
|
|
|
199 |
}
|
|
|
200 |
else {
|
|
|
201 |
lineItem.setExtra_info(ScanRecordType.VALID.name());
|
| 5496 |
mandeep.dh |
202 |
if (StringUtils.isNotBlank(lineItem.getSerial_number())) {
|
| 5361 |
mandeep.dh |
203 |
try {
|
|
|
204 |
warehouseClient.getInventoryItem(lineItem.getSerial_number());
|
|
|
205 |
lineItem.setExtra_info("Item scanned already.");
|
|
|
206 |
areValidScans = false;
|
|
|
207 |
continue;
|
| 5496 |
mandeep.dh |
208 |
} catch (Exception e) {
|
| 5361 |
mandeep.dh |
209 |
}
|
| 5110 |
mandeep.dh |
210 |
}
|
|
|
211 |
|
| 5496 |
mandeep.dh |
212 |
if (lineItem.getItem_id() == -1 &&
|
|
|
213 |
(StringUtils.isBlank(lineItem.getItem_number()) ||
|
|
|
214 |
StringUtils.isBlank(lineItem.getSerial_number())))
|
|
|
215 |
{
|
|
|
216 |
lineItem.setExtra_info("Item not selected/Item or serial number not present");
|
| 5110 |
mandeep.dh |
217 |
areValidScans = false;
|
|
|
218 |
continue;
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
if (lineItem.getItem_id() == -1) {
|
|
|
222 |
List<Long> itemIds = warehouseClient.getItemIds(lineItem.getItem_number());
|
|
|
223 |
if (itemIds.isEmpty()) {
|
|
|
224 |
lineItem.setExtra_info("Unknown item number");
|
|
|
225 |
areValidScans = false;
|
|
|
226 |
continue;
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
if (itemIds.size() > 0) {
|
|
|
230 |
int numItemsInPO = 0;
|
|
|
231 |
for (long itemId : itemIds) {
|
|
|
232 |
if (itemsQuantityMapFromPO.containsKey(itemId)) {
|
|
|
233 |
numItemsInPO++;
|
|
|
234 |
lineItem.setItem_id(itemId);
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
if (numItemsInPO > 1) {
|
|
|
239 |
lineItem.setExtra_info("Multiple items found for given item Number. Choose item explicitly.");
|
|
|
240 |
areValidScans = false;
|
| 5361 |
mandeep.dh |
241 |
continue;
|
| 5110 |
mandeep.dh |
242 |
}
|
|
|
243 |
}
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
if (!itemsQuantityMapFromPO.containsKey(lineItem.getItem_id())) {
|
|
|
247 |
lineItem.setExtra_info("Item not present in PO.");
|
|
|
248 |
areValidScans = false;
|
|
|
249 |
continue;
|
|
|
250 |
}
|
|
|
251 |
|
| 5361 |
mandeep.dh |
252 |
itemsQuantityMapFromPO.put(lineItem.getItem_id(),
|
|
|
253 |
itemsQuantityMapFromPO.get(lineItem.getItem_id()) - new Double(lineItem.getQuantity()).longValue());
|
| 5110 |
mandeep.dh |
254 |
|
|
|
255 |
if (itemsQuantityMapFromPO.get(lineItem.getItem_id()) < 0) {
|
|
|
256 |
lineItem.setExtra_info("Item already fulfilled in PO.");
|
|
|
257 |
areValidScans = false;
|
|
|
258 |
continue;
|
|
|
259 |
}
|
|
|
260 |
}
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
return areValidScans;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
private void setItemsFromPO(long id) {
|
|
|
267 |
try {
|
|
|
268 |
items = new ArrayList<Item>();
|
|
|
269 |
in.shop2020.purchase.PurchaseService.Client purchaseClient = new PurchaseClient().getClient();
|
|
|
270 |
PurchaseOrder purchaseOrder = purchaseClient.getPurchaseOrderForPurchase(id);
|
| 5945 |
mandeep.dh |
271 |
in.shop2020.model.v1.catalog.CatalogService.Client catalogClient = new CatalogClient().getClient();
|
| 5361 |
mandeep.dh |
272 |
|
| 5110 |
mandeep.dh |
273 |
for (in.shop2020.purchase.LineItem lineItem : purchaseOrder.getLineitems()) {
|
| 5496 |
mandeep.dh |
274 |
items.add(catalogClient.getItem(lineItem.getItemId()));
|
| 5110 |
mandeep.dh |
275 |
}
|
| 5496 |
mandeep.dh |
276 |
|
| 5945 |
mandeep.dh |
277 |
in.shop2020.model.v1.inventory.InventoryService.Client inventoryClient = new InventoryClient().getClient();
|
|
|
278 |
warehouse = inventoryClient.getWarehouses(WarehouseType.OURS, InventoryType.GOOD, purchaseOrder.getSupplierId(), PurchaseOrderController.WAREHOUSE_ID, PurchaseOrderController.WAREHOUSE_ID).get(0);
|
| 5110 |
mandeep.dh |
279 |
} catch (Exception e) {
|
|
|
280 |
logger.error("Could not find items in PO with purchase: " + id, e);
|
|
|
281 |
}
|
|
|
282 |
}
|
|
|
283 |
|
| 4687 |
mandeep.dh |
284 |
public String destroy() {
|
|
|
285 |
long id = Long.parseLong(this.id);
|
|
|
286 |
|
|
|
287 |
try {
|
|
|
288 |
PurchaseClient warehouseClient = new PurchaseClient();
|
|
|
289 |
in.shop2020.purchase.PurchaseService.Client client = warehouseClient
|
|
|
290 |
.getClient();
|
|
|
291 |
client.closePurchase(id);
|
|
|
292 |
} catch (TTransportException e) {
|
|
|
293 |
errorMsg = "Error while establishing connection to the warehouse server";
|
|
|
294 |
logger.error(errorMsg, e);
|
|
|
295 |
} catch (TException e) {
|
|
|
296 |
errorMsg = "Error while scanning in the item";
|
|
|
297 |
logger.error(errorMsg, e);
|
|
|
298 |
} catch (PurchaseServiceException e) {
|
|
|
299 |
errorMsg = e.getMessage();
|
|
|
300 |
logger.error(errorMsg, e);
|
|
|
301 |
}
|
|
|
302 |
return "redirect";
|
|
|
303 |
}
|
|
|
304 |
|
|
|
305 |
public String createItemNumberMapping() {
|
|
|
306 |
long itemIdLong = Long.parseLong(itemId);
|
|
|
307 |
|
|
|
308 |
try {
|
|
|
309 |
Client warehouseClient = new WarehouseClient().getClient();
|
|
|
310 |
warehouseClient.createItemNumberMapping(itemNo, itemIdLong);
|
|
|
311 |
} catch (TTransportException e) {
|
|
|
312 |
logger.error("Could not create thrift client", e);
|
|
|
313 |
} catch (TException e) {
|
|
|
314 |
logger.error("Could not create item number mapping", e);
|
|
|
315 |
}
|
|
|
316 |
|
|
|
317 |
return show();
|
|
|
318 |
}
|
|
|
319 |
|
| 5110 |
mandeep.dh |
320 |
public String itemNumberMappingEditNew() {
|
|
|
321 |
return "item-number-mapping";
|
|
|
322 |
}
|
|
|
323 |
|
| 4846 |
mandeep.dh |
324 |
public String getName(Item item){
|
|
|
325 |
return ModelUtils.extractProductNameFromItem(item);
|
|
|
326 |
}
|
|
|
327 |
|
| 4687 |
mandeep.dh |
328 |
public void setId(String id) {
|
|
|
329 |
this.id = id;
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
public String getId() {
|
|
|
333 |
return id;
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
public String getErrorMessage() {
|
|
|
337 |
return errorMsg;
|
|
|
338 |
}
|
|
|
339 |
|
|
|
340 |
public String getPurchaseOrderId() {
|
|
|
341 |
return purchaseOrderId;
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
public String getServletContextPath() {
|
|
|
345 |
return context.getContextPath();
|
|
|
346 |
}
|
|
|
347 |
|
|
|
348 |
public String getItemId() {
|
|
|
349 |
return itemId;
|
|
|
350 |
}
|
|
|
351 |
|
|
|
352 |
public void setItemId(String itemId) {
|
|
|
353 |
this.itemId = itemId;
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
public String getItemNo() {
|
|
|
357 |
return itemNo;
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
public void setItemNo(String itemNo) {
|
|
|
361 |
this.itemNo = itemNo;
|
|
|
362 |
}
|
|
|
363 |
|
| 4846 |
mandeep.dh |
364 |
public List<Item> getItems() {
|
|
|
365 |
return items;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
public void setItems(List<Item> items) {
|
|
|
369 |
this.items = items;
|
|
|
370 |
}
|
|
|
371 |
|
| 5110 |
mandeep.dh |
372 |
public List<LineItem> getLineItems() {
|
|
|
373 |
return lineItems;
|
|
|
374 |
}
|
|
|
375 |
|
|
|
376 |
public void setLineItems(List<LineItem> lineItems) {
|
|
|
377 |
this.lineItems = lineItems;
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
public Warehouse getWarehouse() {
|
|
|
381 |
return warehouse;
|
|
|
382 |
}
|
| 5496 |
mandeep.dh |
383 |
|
|
|
384 |
public List<Invoice> getInvoices() {
|
|
|
385 |
return invoices;
|
|
|
386 |
}
|
|
|
387 |
|
|
|
388 |
public void setInvoices(List<Invoice> invoices) {
|
|
|
389 |
this.invoices = invoices;
|
|
|
390 |
}
|
| 4687 |
mandeep.dh |
391 |
}
|