| Line 1... |
Line 1... |
| 1 |
package com.smartdukaan.cron.migrations;
|
1 |
package com.smartdukaan.cron.migrations;
|
| 2 |
|
2 |
|
| - |
|
3 |
import java.io.Serializable;
|
| 3 |
import java.time.LocalDate;
|
4 |
import java.time.LocalDate;
|
| 4 |
import java.time.LocalDateTime;
|
5 |
import java.time.LocalDateTime;
|
| 5 |
import java.time.LocalTime;
|
6 |
import java.time.LocalTime;
|
| - |
|
7 |
import java.util.ArrayList;
|
| 6 |
import java.util.Arrays;
|
8 |
import java.util.Arrays;
|
| 7 |
import java.util.Collections;
|
9 |
import java.util.Collections;
|
| 8 |
import java.util.HashMap;
|
10 |
import java.util.HashMap;
|
| 9 |
import java.util.HashSet;
|
11 |
import java.util.HashSet;
|
| 10 |
import java.util.List;
|
12 |
import java.util.List;
|
| 11 |
import java.util.Map;
|
13 |
import java.util.Map;
|
| 12 |
import java.util.stream.Collectors;
|
14 |
import java.util.stream.Collectors;
|
| 13 |
|
15 |
|
| - |
|
16 |
import org.apache.commons.io.output.ByteArrayOutputStream;
|
| 14 |
import org.apache.commons.lang.StringUtils;
|
17 |
import org.apache.commons.lang.StringUtils;
|
| 15 |
import org.apache.logging.log4j.LogManager;
|
18 |
import org.apache.logging.log4j.LogManager;
|
| 16 |
import org.apache.logging.log4j.Logger;
|
19 |
import org.apache.logging.log4j.Logger;
|
| 17 |
import org.springframework.beans.factory.annotation.Autowired;
|
20 |
import org.springframework.beans.factory.annotation.Autowired;
|
| - |
|
21 |
import org.springframework.core.io.ByteArrayResource;
|
| 18 |
import org.springframework.mail.javamail.JavaMailSender;
|
22 |
import org.springframework.mail.javamail.JavaMailSender;
|
| 19 |
import org.springframework.stereotype.Component;
|
23 |
import org.springframework.stereotype.Component;
|
| 20 |
import org.springframework.transaction.annotation.Transactional;
|
24 |
import org.springframework.transaction.annotation.Transactional;
|
| 21 |
|
25 |
|
| - |
|
26 |
import com.spice.profitmandi.common.util.FileUtil;
|
| - |
|
27 |
import com.spice.profitmandi.common.util.Utils;
|
| 22 |
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
|
28 |
import com.spice.profitmandi.dao.entity.fofo.CurrentInventorySnapshot;
|
| 23 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
29 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrder;
|
| 24 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
30 |
import com.spice.profitmandi.dao.entity.fofo.FofoOrderItem;
|
| 25 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
31 |
import com.spice.profitmandi.dao.entity.fofo.InventoryItem;
|
| 26 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
32 |
import com.spice.profitmandi.dao.entity.fofo.Purchase;
|
| Line 129... |
Line 135... |
| 129 |
@Autowired
|
135 |
@Autowired
|
| 130 |
private PurchaseRepository purchaseRepository;
|
136 |
private PurchaseRepository purchaseRepository;
|
| 131 |
|
137 |
|
| 132 |
@Autowired
|
138 |
@Autowired
|
| 133 |
private PriceDropService priceDropService;
|
139 |
private PriceDropService priceDropService;
|
| - |
|
140 |
|
| - |
|
141 |
@Autowired
|
| - |
|
142 |
private JavaMailSender googleMailSender;
|
| 134 |
|
143 |
|
| 135 |
@Autowired
|
144 |
@Autowired
|
| 136 |
private SchemeService schemeService;
|
145 |
private SchemeService schemeService;
|
| 137 |
|
146 |
|
| 138 |
@Autowired
|
147 |
@Autowired
|
| Line 304... |
Line 313... |
| 304 |
public void migrateChallansToInvoices() throws Exception {
|
313 |
public void migrateChallansToInvoices() throws Exception {
|
| 305 |
Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectAllChallans().stream()
|
314 |
Map<String, List<Order>> invoiceOrdersMap = orderRepository.selectAllChallans().stream()
|
| 306 |
.filter(x -> !x.getLineItem().getHsnCode().equals("NOGST"))
|
315 |
.filter(x -> !x.getLineItem().getHsnCode().equals("NOGST"))
|
| 307 |
.collect(Collectors.groupingBy(Order::getInvoiceNumber, Collectors.toList()));
|
316 |
.collect(Collectors.groupingBy(Order::getInvoiceNumber, Collectors.toList()));
|
| 308 |
|
317 |
|
| - |
|
318 |
List<List<? extends Serializable>> rows = new ArrayList<>();
|
| 309 |
for (String invoice : invoiceOrdersMap.keySet()) {
|
319 |
for (String invoice : invoiceOrdersMap.keySet()) {
|
| 310 |
Order oneOrder = invoiceOrdersMap.get(invoice).get(0);
|
320 |
Order oneOrder = invoiceOrdersMap.get(invoice).get(0);
|
| 311 |
int totalOrders = invoiceOrdersMap.get(invoice).size();
|
321 |
int totalOrders = invoiceOrdersMap.get(invoice).size();
|
| 312 |
LineItem lineItem = oneOrder.getLineItem();
|
322 |
LineItem lineItem = oneOrder.getLineItem();
|
| 313 |
oneOrder.setBillingTimestamp(LocalDateTime.now());
|
323 |
oneOrder.setBillingTimestamp(LocalDateTime.now());
|
| 314 |
oneOrder.setInvoiceNumber(getInvoiceNumber(oneOrder));
|
324 |
oneOrder.setInvoiceNumber(getInvoiceNumber(oneOrder));
|
| - |
|
325 |
rows.add(Arrays.asList(oneOrder.getId(), invoice, oneOrder.getInvoiceNumber(), lineItem.getQuantity()));
|
| 315 |
LOGGER.info(invoice + "\t" + oneOrder.getInvoiceNumber() + "\t" + totalOrders + "\t" + lineItem.getQuantity());
|
326 |
LOGGER.info(invoice + "\t" + oneOrder.getInvoiceNumber() + oneOrder.getId() + "\t", "\t" + totalOrders + "\t" + lineItem.getQuantity());
|
| 316 |
Purchase p = null;
|
327 |
Purchase p = null;
|
| 317 |
try {
|
328 |
try {
|
| 318 |
p = purchaseRepository.selectByPurchaseReferenceAndFofoId(invoice, oneOrder.getRetailerId());
|
329 |
p = purchaseRepository.selectByPurchaseReferenceAndFofoId(invoice, oneOrder.getRetailerId());
|
| 319 |
} catch (Exception e) {
|
330 |
} catch (Exception e) {
|
| 320 |
LOGGER.info("Could not find purchase for invoice {}", invoice);
|
331 |
LOGGER.info("Could not find purchase for invoice {}", invoice);
|
| Line 327... |
Line 338... |
| 327 |
}
|
338 |
}
|
| 328 |
}
|
339 |
}
|
| 329 |
|
340 |
|
| 330 |
}
|
341 |
}
|
| 331 |
changePartnerInvoices();
|
342 |
changePartnerInvoices();
|
| - |
|
343 |
ByteArrayOutputStream baos = FileUtil.getCSVByteStream(Arrays.asList("Order id", "Challan", "Invoice", "Quantity"), rows);
|
| - |
|
344 |
|
| - |
|
345 |
Utils.sendMailWithAttachment(googleMailSender,
|
| - |
|
346 |
new String[] { "amit.gupta@shop2020.in", "sunny.yadav@smartdukaan.com" }, null,
|
| - |
|
347 |
"Challans Converted to Invoice", "PFA", "Challans-To-Invoice.csv", new ByteArrayResource(baos.toByteArray()));
|
| 332 |
throw new Exception();
|
348 |
//throw new Exception();
|
| 333 |
}
|
349 |
}
|
| 334 |
|
350 |
|
| 335 |
private String getInvoiceNumber(Order oneOrder) {
|
351 |
private String getInvoiceNumber(Order oneOrder) {
|
| 336 |
String prefix = oneOrder.getInvoiceNumber().split("-")[1].replaceAll("\\d*", "");
|
352 |
String prefix = oneOrder.getInvoiceNumber().split("-")[1].replaceAll("\\d*", "");
|
| 337 |
System.out.println("Prefix is " + prefix);
|
353 |
System.out.println("Prefix is " + prefix);
|