| 29222 |
tejbeer |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
|
|
3 |
import java.time.DayOfWeek;
|
|
|
4 |
import java.time.LocalDate;
|
| 29250 |
tejbeer |
5 |
import java.time.LocalDateTime;
|
| 29222 |
tejbeer |
6 |
import java.util.ArrayList;
|
|
|
7 |
import java.util.HashMap;
|
|
|
8 |
import java.util.List;
|
|
|
9 |
import java.util.Map;
|
|
|
10 |
import java.util.stream.Collectors;
|
|
|
11 |
|
| 29578 |
tejbeer |
12 |
import org.apache.commons.lang.StringUtils;
|
| 29222 |
tejbeer |
13 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
14 |
import org.springframework.stereotype.Component;
|
|
|
15 |
|
| 29578 |
tejbeer |
16 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 29222 |
tejbeer |
17 |
import com.spice.profitmandi.dao.entity.logistics.Provider;
|
|
|
18 |
import com.spice.profitmandi.dao.entity.logistics.ProviderTat;
|
|
|
19 |
import com.spice.profitmandi.dao.entity.logistics.PublicHolidays;
|
|
|
20 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
21 |
import com.spice.profitmandi.dao.model.DispatchNotificationModel;
|
|
|
22 |
import com.spice.profitmandi.dao.repository.logistics.ProviderTatRepository;
|
|
|
23 |
import com.spice.profitmandi.dao.repository.logistics.PublicHolidaysRepository;
|
|
|
24 |
|
|
|
25 |
import in.shop2020.model.v1.order.OrderStatus;
|
|
|
26 |
|
|
|
27 |
@Component
|
|
|
28 |
public class LogisticsServiceImpl implements LogisticsService {
|
|
|
29 |
|
|
|
30 |
@Autowired
|
|
|
31 |
private PublicHolidaysRepository publicHolidaysRepository;
|
|
|
32 |
|
|
|
33 |
@Autowired
|
|
|
34 |
private ProviderTatRepository providerTatRepository;
|
|
|
35 |
|
|
|
36 |
@Override
|
| 29250 |
tejbeer |
37 |
public LocalDate calculateDeliveryTimeline(LocalDate date, ProviderTat providerTat, int delayDays) {
|
| 29222 |
tejbeer |
38 |
int businessDays = 0;
|
| 29255 |
tejbeer |
39 |
int deliveryTat = 3;
|
| 29222 |
tejbeer |
40 |
if (providerTat != null) {
|
|
|
41 |
deliveryTat = providerTat.getDeliveryTime();
|
|
|
42 |
}
|
| 29250 |
tejbeer |
43 |
deliveryTat = deliveryTat + delayDays;
|
|
|
44 |
LocalDate currDate = date.plusDays(1);
|
| 29222 |
tejbeer |
45 |
List<LocalDate> ph = publicHolidaysRepository.selectAllByDate(currDate).stream().map(x -> x.getDate())
|
|
|
46 |
.collect(Collectors.toList());
|
|
|
47 |
while (businessDays < deliveryTat) {
|
|
|
48 |
if (ph.contains(currDate) || currDate.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
|
|
|
49 |
} else {
|
|
|
50 |
businessDays++;
|
|
|
51 |
}
|
|
|
52 |
currDate = currDate.plusDays(1);
|
|
|
53 |
}
|
|
|
54 |
return currDate;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
@Override
|
|
|
58 |
public Map<String, DispatchNotificationModel> markedOrderShippedDetail(List<Order> orders, Provider provider,
|
| 29578 |
tejbeer |
59 |
Map<String, DispatchNotificationModel> dispatchNotication, String airwaybillNo)
|
|
|
60 |
throws ProfitMandiBusinessException {
|
| 29222 |
tejbeer |
61 |
|
|
|
62 |
for (Order order : orders) {
|
|
|
63 |
if (order.getStatus().equals(OrderStatus.BILLED)) {
|
| 29578 |
tejbeer |
64 |
|
|
|
65 |
if (StringUtils.isEmpty(airwaybillNo)) {
|
|
|
66 |
throw new ProfitMandiBusinessException("Uploaded File", "",
|
|
|
67 |
"Invoice Number " + order.getInvoiceNumber() + " airwaybill_no is empty");
|
|
|
68 |
|
|
|
69 |
}
|
| 31077 |
tejbeer |
70 |
|
|
|
71 |
if (airwaybillNo.equals("Error")) {
|
|
|
72 |
break;
|
|
|
73 |
}
|
| 29222 |
tejbeer |
74 |
order.setLogisticsProviderId(provider.getId());
|
|
|
75 |
order.setAirwayBillNumber(airwaybillNo);
|
|
|
76 |
order.setStatus(OrderStatus.SHIPPED_FROM_WH);
|
| 29305 |
tejbeer |
77 |
order.setStatusDescription("Order shipped from warehouses");
|
| 29222 |
tejbeer |
78 |
|
|
|
79 |
ProviderTat pt = providerTatRepository.selectByProviderId(order.getLogisticsProviderId(),
|
|
|
80 |
order.getWarehouseId(), order.getRetailerPinCode());
|
| 29250 |
tejbeer |
81 |
order.setShippingTimestamp(LocalDateTime.now());
|
| 29222 |
tejbeer |
82 |
|
| 29250 |
tejbeer |
83 |
LocalDate deliveryDate = this.calculateDeliveryTimeline(LocalDate.now(), pt, 0);
|
| 29222 |
tejbeer |
84 |
order.setExpectedDeliveryTime(deliveryDate.atStartOfDay());
|
|
|
85 |
|
|
|
86 |
if (dispatchNotication.get(airwaybillNo) != null) {
|
|
|
87 |
DispatchNotificationModel dnm = dispatchNotication.get(airwaybillNo);
|
|
|
88 |
List<String> invoiceNumbers = dnm.getInvoiceNumber();
|
|
|
89 |
invoiceNumbers.add(order.getInvoiceNumber());
|
|
|
90 |
dnm.setInvoiceNumber(invoiceNumbers);
|
|
|
91 |
Float totalAmount = dnm.getTotalAmount();
|
|
|
92 |
dnm.setTotalAmount(totalAmount + order.getTotalAmount());
|
|
|
93 |
|
|
|
94 |
int totalQty = dnm.getTotalQty();
|
|
|
95 |
dnm.setTotalQty(totalQty + order.getLineItem().getQuantity());
|
|
|
96 |
|
|
|
97 |
dispatchNotication.put(airwaybillNo, dnm);
|
|
|
98 |
|
|
|
99 |
} else {
|
|
|
100 |
DispatchNotificationModel dnm = new DispatchNotificationModel();
|
|
|
101 |
List<String> invoiceNumber = new ArrayList<>();
|
|
|
102 |
invoiceNumber.add(order.getInvoiceNumber());
|
|
|
103 |
dnm.setInvoiceNumber(invoiceNumber);
|
|
|
104 |
dnm.setTotalAmount(order.getTotalAmount());
|
|
|
105 |
dnm.setTotalQty(order.getLineItem().getQuantity());
|
|
|
106 |
dnm.setFofoId(order.getRetailerId());
|
|
|
107 |
dnm.setProviderId(order.getLogisticsProviderId());
|
|
|
108 |
dnm.setDate(deliveryDate);
|
|
|
109 |
|
|
|
110 |
dispatchNotication.put(airwaybillNo, dnm);
|
|
|
111 |
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
}
|
|
|
117 |
return dispatchNotication;
|
|
|
118 |
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
}
|