| 23069 |
amit.gupta |
1 |
package in.shop2020.inventory.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.order.Order;
|
|
|
4 |
import in.shop2020.thrift.clients.InventoryClient;
|
|
|
5 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 23081 |
amit.gupta |
6 |
import in.shop2020.thrift.clients.WarehouseClient;
|
|
|
7 |
import in.shop2020.warehouse.InventoryItem;
|
|
|
8 |
import in.shop2020.warehouse.WarehouseService.Client;
|
| 23069 |
amit.gupta |
9 |
|
|
|
10 |
import java.io.BufferedInputStream;
|
|
|
11 |
import java.io.BufferedWriter;
|
|
|
12 |
import java.io.File;
|
|
|
13 |
import java.io.FileInputStream;
|
|
|
14 |
import java.io.FileWriter;
|
|
|
15 |
import java.io.InputStream;
|
|
|
16 |
import java.text.DateFormat;
|
|
|
17 |
import java.text.SimpleDateFormat;
|
| 23081 |
amit.gupta |
18 |
import java.util.ArrayList;
|
| 23069 |
amit.gupta |
19 |
import java.util.HashMap;
|
|
|
20 |
import java.util.HashSet;
|
| 23081 |
amit.gupta |
21 |
import java.util.Iterator;
|
| 23069 |
amit.gupta |
22 |
import java.util.List;
|
|
|
23 |
import java.util.Map;
|
|
|
24 |
import java.util.Set;
|
|
|
25 |
|
|
|
26 |
import javax.servlet.ServletOutputStream;
|
|
|
27 |
|
|
|
28 |
import org.apache.commons.lang.StringUtils;
|
|
|
29 |
import org.apache.commons.logging.Log;
|
|
|
30 |
import org.apache.commons.logging.LogFactory;
|
| 23081 |
amit.gupta |
31 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
32 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
33 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
34 |
import org.apache.poi.ss.usermodel.Workbook;
|
| 23069 |
amit.gupta |
35 |
|
| 23081 |
amit.gupta |
36 |
public class PriceDropController extends BaseController {
|
|
|
37 |
|
|
|
38 |
private static final DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
39 |
|
| 23069 |
amit.gupta |
40 |
private TransactionClient tsc;
|
|
|
41 |
private in.shop2020.model.v1.order.TransactionService.Client tClient;
|
| 23081 |
amit.gupta |
42 |
|
| 23069 |
amit.gupta |
43 |
private InventoryClient ic;
|
|
|
44 |
private in.shop2020.model.v1.inventory.InventoryService.Client iClient;
|
| 23081 |
amit.gupta |
45 |
|
|
|
46 |
private WarehouseClient wc;
|
|
|
47 |
private Client wClient;
|
|
|
48 |
|
| 23069 |
amit.gupta |
49 |
private static Log logger = LogFactory.getLog(PurchaseReportController.class);
|
|
|
50 |
private String closingDate;
|
|
|
51 |
private int itemId;
|
| 23081 |
amit.gupta |
52 |
|
|
|
53 |
private File imeiFile;
|
|
|
54 |
private String imeiFileContentType;
|
|
|
55 |
private String imeiFileFileName;
|
|
|
56 |
private String affectedDate;
|
|
|
57 |
|
|
|
58 |
private double priceDrop;
|
|
|
59 |
|
|
|
60 |
public File getImeiFile() {
|
|
|
61 |
return imeiFile;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public String getAffectedDate() {
|
|
|
65 |
return affectedDate;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
public void setAffectedDate(String affectedDate) {
|
|
|
69 |
this.affectedDate = affectedDate;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
public void setImeiFile(File imeiFile) {
|
|
|
73 |
this.imeiFile = imeiFile;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public String getImeiFileContentType() {
|
|
|
77 |
return imeiFileContentType;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
public void setImeiFileContentType(String imeiFileContentType) {
|
|
|
81 |
this.imeiFileContentType = imeiFileContentType;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
public String getImeiFileFileName() {
|
|
|
85 |
return imeiFileFileName;
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public void setImeiFileFileName(String imeiFileFileName) {
|
|
|
89 |
this.imeiFileFileName = imeiFileFileName;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public int getItemId() {
|
| 23069 |
amit.gupta |
93 |
return itemId;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
public void setItemId(int itemId) {
|
|
|
97 |
this.itemId = itemId;
|
|
|
98 |
}
|
|
|
99 |
|
| 23081 |
amit.gupta |
100 |
|
|
|
101 |
public String index() {
|
|
|
102 |
return INDEX;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
public String downloadImeis() throws Exception {
|
|
|
106 |
long closingDateLong;
|
| 23069 |
amit.gupta |
107 |
try {
|
|
|
108 |
closingDateLong = sdf.parse(this.closingDate).getTime();
|
|
|
109 |
} catch (Exception e) {
|
| 23081 |
amit.gupta |
110 |
this.addActionError("Invalid closing date");
|
| 23069 |
amit.gupta |
111 |
return INDEX;
|
|
|
112 |
}
|
|
|
113 |
tsc = new TransactionClient();
|
|
|
114 |
tClient = tsc.getClient();
|
| 23081 |
amit.gupta |
115 |
|
| 23069 |
amit.gupta |
116 |
List<Order> inTransitOrders;
|
|
|
117 |
|
|
|
118 |
inTransitOrders = tClient.getInTransitOrdersOnDateByItemId(closingDateLong, this.itemId);
|
| 23081 |
amit.gupta |
119 |
|
| 23069 |
amit.gupta |
120 |
Set<Long> warehouseSet = new HashSet<Long>();
|
| 23081 |
amit.gupta |
121 |
for (Order o : inTransitOrders) {
|
| 23069 |
amit.gupta |
122 |
warehouseSet.add(o.getFulfilmentWarehouseId());
|
|
|
123 |
}
|
| 23081 |
amit.gupta |
124 |
|
| 23069 |
amit.gupta |
125 |
ic = new InventoryClient();
|
|
|
126 |
iClient = ic.getClient();
|
| 23081 |
amit.gupta |
127 |
|
|
|
128 |
Map<Long, String> warehouseNameMap = new HashMap<Long, String>();
|
| 23069 |
amit.gupta |
129 |
for (Long warehouseId : warehouseSet) {
|
|
|
130 |
warehouseNameMap.put(warehouseId, iClient.getWarehouseName(warehouseId));
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
byte[] buffer = null;
|
| 23081 |
amit.gupta |
134 |
File file = createFile(inTransitOrders, warehouseNameMap);
|
|
|
135 |
buffer = new byte[(int) file.length()];
|
|
|
136 |
InputStream input = null;
|
|
|
137 |
try {
|
|
|
138 |
int totalBytesRead = 0;
|
|
|
139 |
input = new BufferedInputStream(new FileInputStream(file));
|
|
|
140 |
while (totalBytesRead < buffer.length) {
|
|
|
141 |
int bytesRemaining = buffer.length - totalBytesRead;
|
|
|
142 |
// input.read() returns -1, 0, or more :
|
|
|
143 |
int bytesRead = input.read(buffer, totalBytesRead, bytesRemaining);
|
|
|
144 |
if (bytesRead > 0) {
|
|
|
145 |
totalBytesRead = totalBytesRead + bytesRead;
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
/*
|
|
|
149 |
* the above style is a bit tricky: it places bytes into the
|
|
|
150 |
* 'buffer' array; 'buffer' is an output parameter; the while loop
|
|
|
151 |
* usually has a single iteration only.
|
|
|
152 |
*/
|
|
|
153 |
} finally {
|
|
|
154 |
input.close();
|
|
|
155 |
}
|
| 23069 |
amit.gupta |
156 |
|
| 23081 |
amit.gupta |
157 |
response.setContentType("application/vnd.ms-excel");
|
|
|
158 |
response.setHeader("Content-disposition", "inline; filename=" + file.getName());
|
| 23069 |
amit.gupta |
159 |
|
| 23081 |
amit.gupta |
160 |
ServletOutputStream sos = response.getOutputStream();
|
|
|
161 |
sos.write(buffer);
|
|
|
162 |
sos.flush();
|
| 23069 |
amit.gupta |
163 |
|
| 23081 |
amit.gupta |
164 |
return null;
|
|
|
165 |
}
|
|
|
166 |
|
| 23069 |
amit.gupta |
167 |
private File createFile(List<Order> inTransitOrders, Map<Long, String> warehouseNameMap) {
|
|
|
168 |
try {
|
| 23081 |
amit.gupta |
169 |
String tmpDir = System.getProperty("java.io.tmpdir");
|
|
|
170 |
File file = new File(tmpDir + "/IntransitIMEIS-" + this.closingDate + this.itemId + ".xls");
|
|
|
171 |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
|
|
|
172 |
bufferedWriter.write(StringUtils.join(new String[] { "Order Id", "Vendor Name", "Brand", "ModelName", "ModelNumber", "Color",
|
|
|
173 |
"UnitPrice", "IMEI" }, '\t'));
|
| 23069 |
amit.gupta |
174 |
|
| 23081 |
amit.gupta |
175 |
for (Order order : inTransitOrders) {
|
|
|
176 |
bufferedWriter.newLine();
|
|
|
177 |
in.shop2020.model.v1.order.LineItem lineitem = order.getLineitems().get(0);
|
|
|
178 |
for (String serialNumber : lineitem.getSerial_number().split(",")) {
|
|
|
179 |
bufferedWriter.write(StringUtils.join(
|
|
|
180 |
new String[] { String.valueOf(order.getId()), warehouseNameMap.get(order.getFulfilmentWarehouseId()),
|
|
|
181 |
lineitem.getBrand(), lineitem.getModel_name(), lineitem.getModel_number(), lineitem.getColor(),
|
|
|
182 |
String.valueOf(lineitem.getUnit_price()), serialNumber, order.getStatusDescription() }, '\t'));
|
|
|
183 |
}
|
|
|
184 |
}
|
| 23069 |
amit.gupta |
185 |
|
| 23081 |
amit.gupta |
186 |
bufferedWriter.close();
|
|
|
187 |
return file;
|
|
|
188 |
} catch (Exception e) {
|
|
|
189 |
return null;
|
|
|
190 |
}
|
| 23069 |
amit.gupta |
191 |
}
|
|
|
192 |
|
| 23081 |
amit.gupta |
193 |
public String uploadImeis() throws Exception {
|
|
|
194 |
long affectedDateLong;
|
|
|
195 |
try {
|
|
|
196 |
affectedDateLong = sdf.parse(this.affectedDate).getTime();
|
|
|
197 |
} catch (Exception e) {
|
|
|
198 |
e.printStackTrace();
|
|
|
199 |
this.addActionError("Invalid Affected Date");
|
|
|
200 |
return INDEX;
|
|
|
201 |
}
|
|
|
202 |
FileInputStream inputStream = new FileInputStream(this.imeiFile);
|
|
|
203 |
|
|
|
204 |
Workbook workbook = new HSSFWorkbook(inputStream);
|
|
|
205 |
Sheet firstSheet = workbook.getSheetAt(0);
|
|
|
206 |
|
|
|
207 |
Iterator<Row> iterator = firstSheet.iterator();
|
| 23089 |
amit.gupta |
208 |
Set<String> imeisSet = new HashSet<String>();
|
| 23081 |
amit.gupta |
209 |
// Ignore first row as it is header
|
|
|
210 |
if (iterator.hasNext()) {
|
|
|
211 |
iterator.next();
|
|
|
212 |
}
|
|
|
213 |
while (iterator.hasNext()) {
|
|
|
214 |
Row nextRow = iterator.next();
|
|
|
215 |
// logger.info("IMEIs " + nextRow.getCell(0).getStringCellValue());
|
| 23088 |
amit.gupta |
216 |
String imei = String.valueOf((long)(nextRow.getCell(0).getNumericCellValue()));
|
| 23081 |
amit.gupta |
217 |
if (imei.length() < 15) {
|
|
|
218 |
addActionError("IMEI size should be of 15 digit - " + imei);
|
|
|
219 |
return INDEX;
|
|
|
220 |
}
|
| 23089 |
amit.gupta |
221 |
imeisSet.add(imei);
|
| 23081 |
amit.gupta |
222 |
}
|
|
|
223 |
// Check if all imeis belong to specified item
|
|
|
224 |
wc = new WarehouseClient();
|
|
|
225 |
wClient = wc.getClient();
|
| 23089 |
amit.gupta |
226 |
List<String> imeisList= new ArrayList<String>(imeisSet);
|
|
|
227 |
if (imeisSet.size() > 0) {
|
| 23090 |
amit.gupta |
228 |
List<InventoryItem> inventoryItems = wClient.getInventoryItemsBySerailNumbers(imeisList);
|
| 23081 |
amit.gupta |
229 |
Map<String, InventoryItem> inventoryItemMap = new HashMap<String, InventoryItem>();
|
|
|
230 |
|
|
|
231 |
for(InventoryItem inventoryItem : inventoryItems) {
|
|
|
232 |
inventoryItemMap.put(inventoryItem.getSerialNumber(), inventoryItem);
|
|
|
233 |
}
|
| 23089 |
amit.gupta |
234 |
if(inventoryItems.size() != imeisSet.size()) {
|
| 23081 |
amit.gupta |
235 |
addActionError("Few IMEIs are missing");
|
| 23089 |
amit.gupta |
236 |
for (String imei : imeisSet) {
|
| 23081 |
amit.gupta |
237 |
if(!inventoryItemMap.containsKey(imei)) {
|
|
|
238 |
addActionError(imei);
|
|
|
239 |
}
|
| 23091 |
amit.gupta |
240 |
return INDEX;
|
| 23081 |
amit.gupta |
241 |
}
|
|
|
242 |
}
|
|
|
243 |
for (InventoryItem inventoryItem : inventoryItems) {
|
|
|
244 |
if (inventoryItem.getItemId() != this.getItemId()) {
|
|
|
245 |
addActionError("IMEI " + inventoryItem.getSerialNumber() + " belongs to other item - " + inventoryItem.getItemId());
|
|
|
246 |
return INDEX;
|
|
|
247 |
}
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
tsc = new TransactionClient();
|
|
|
251 |
tClient = tsc.getClient();
|
| 23089 |
amit.gupta |
252 |
tClient.addPriceDrop(itemId, imeisList, this.priceDrop, affectedDateLong);
|
| 23091 |
amit.gupta |
253 |
addActionMessage("Items Added Successfully");
|
| 23081 |
amit.gupta |
254 |
|
|
|
255 |
} else {
|
|
|
256 |
addActionError("Serial numbers should not be empty");
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
|
261 |
return INDEX;
|
|
|
262 |
}
|
|
|
263 |
|
| 23069 |
amit.gupta |
264 |
public void setClosingDate(String closingDate) {
|
|
|
265 |
this.closingDate = closingDate;
|
|
|
266 |
}
|
|
|
267 |
|
|
|
268 |
public String getClosingDate() {
|
|
|
269 |
return closingDate;
|
|
|
270 |
}
|
| 23081 |
amit.gupta |
271 |
|
|
|
272 |
|
|
|
273 |
public void setPriceDrop(double priceDrop) {
|
|
|
274 |
this.priceDrop = priceDrop;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
public double getPriceDrop() {
|
|
|
278 |
return priceDrop;
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
|
| 23069 |
amit.gupta |
282 |
}
|