| 29222 |
tejbeer |
1 |
package com.spice.profitmandi.service;
|
|
|
2 |
|
| 29578 |
tejbeer |
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
| 33741 |
amit.gupta |
4 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 34373 |
tejus.loha |
5 |
import com.spice.profitmandi.dao.entity.logistics.*;
|
| 29222 |
tejbeer |
6 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 34373 |
tejus.loha |
7 |
import com.spice.profitmandi.dao.entity.transaction.Transaction;
|
| 33741 |
amit.gupta |
8 |
import com.spice.profitmandi.dao.entity.transaction.TransactionShipmentSequence;
|
| 29222 |
tejbeer |
9 |
import com.spice.profitmandi.dao.model.DispatchNotificationModel;
|
| 33741 |
amit.gupta |
10 |
import com.spice.profitmandi.dao.repository.logistics.*;
|
| 34373 |
tejus.loha |
11 |
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
|
| 33741 |
amit.gupta |
12 |
import com.spice.profitmandi.dao.repository.transaction.TransactionShipmentSequenceRepository;
|
|
|
13 |
import com.spice.profitmandi.service.integrations.gstpro.api.model.E_InvoiceCommon;
|
| 29222 |
tejbeer |
14 |
import in.shop2020.model.v1.order.OrderStatus;
|
| 32609 |
raveendra. |
15 |
import org.apache.commons.lang.StringUtils;
|
| 34373 |
tejus.loha |
16 |
import org.apache.logging.log4j.LogManager;
|
|
|
17 |
import org.apache.logging.log4j.Logger;
|
| 32609 |
raveendra. |
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
19 |
import org.springframework.stereotype.Component;
|
| 29222 |
tejbeer |
20 |
|
| 32609 |
raveendra. |
21 |
import java.time.DayOfWeek;
|
|
|
22 |
import java.time.LocalDate;
|
|
|
23 |
import java.time.LocalDateTime;
|
|
|
24 |
import java.util.ArrayList;
|
|
|
25 |
import java.util.List;
|
|
|
26 |
import java.util.Map;
|
|
|
27 |
import java.util.stream.Collectors;
|
|
|
28 |
|
| 29222 |
tejbeer |
29 |
@Component
|
|
|
30 |
public class LogisticsServiceImpl implements LogisticsService {
|
|
|
31 |
|
| 33741 |
amit.gupta |
32 |
@Autowired
|
|
|
33 |
private PublicHolidaysRepository publicHolidaysRepository;
|
| 29222 |
tejbeer |
34 |
|
| 33741 |
amit.gupta |
35 |
@Autowired
|
|
|
36 |
private ProviderTatRepository providerTatRepository;
|
| 29222 |
tejbeer |
37 |
|
| 33741 |
amit.gupta |
38 |
@Autowired
|
|
|
39 |
ProviderRepository providerRepository;
|
| 29222 |
tejbeer |
40 |
|
| 34373 |
tejus.loha |
41 |
private static final Logger LOGGER = LogManager.getLogger(LogisticsServiceImpl.class);
|
|
|
42 |
|
|
|
43 |
|
| 33741 |
amit.gupta |
44 |
@Override
|
|
|
45 |
public LocalDate calculateDeliveryTimeline(LocalDate date, ProviderTat providerTat, int delayDays) {
|
|
|
46 |
int businessDays = 0;
|
|
|
47 |
int deliveryTat = 3;
|
|
|
48 |
if (providerTat != null) {
|
|
|
49 |
deliveryTat = providerTat.getDeliveryTime();
|
|
|
50 |
}
|
|
|
51 |
deliveryTat = deliveryTat + delayDays;
|
|
|
52 |
LocalDate currDate = date.plusDays(1);
|
|
|
53 |
List<LocalDate> ph = publicHolidaysRepository.selectAllByDate(currDate).stream().map(x -> x.getDate())
|
|
|
54 |
.collect(Collectors.toList());
|
|
|
55 |
while (businessDays < deliveryTat) {
|
|
|
56 |
if (ph.contains(currDate) || currDate.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
|
|
|
57 |
} else {
|
|
|
58 |
businessDays++;
|
|
|
59 |
}
|
|
|
60 |
currDate = currDate.plusDays(1);
|
|
|
61 |
}
|
|
|
62 |
return currDate;
|
|
|
63 |
}
|
| 29222 |
tejbeer |
64 |
|
| 33741 |
amit.gupta |
65 |
@Override
|
|
|
66 |
public Map<String, DispatchNotificationModel> markedOrderShippedDetail(List<Order> orders, Provider provider,
|
|
|
67 |
Map<String, DispatchNotificationModel> dispatchNotication, String airwaybillNo, LocalDateTime shippingTimestamp)
|
|
|
68 |
throws ProfitMandiBusinessException {
|
| 29578 |
tejbeer |
69 |
|
| 33741 |
amit.gupta |
70 |
for (Order order : orders) {
|
|
|
71 |
if (order.getStatus().equals(OrderStatus.BILLED)) {
|
|
|
72 |
if (order.getBillingTimestamp().isAfter(shippingTimestamp)) {
|
|
|
73 |
throw new ProfitMandiBusinessException("Uploaded File", "",
|
|
|
74 |
"Invoice Number " + order.getInvoiceNumber() + " cannot be shipped before billing date");
|
|
|
75 |
}
|
|
|
76 |
if (StringUtils.isEmpty(airwaybillNo)) {
|
|
|
77 |
throw new ProfitMandiBusinessException("Uploaded File", "",
|
|
|
78 |
"Invoice Number " + order.getInvoiceNumber() + " airwaybill_no is empty");
|
|
|
79 |
}
|
|
|
80 |
if (airwaybillNo.equals("Error")) {
|
|
|
81 |
break;
|
|
|
82 |
}
|
|
|
83 |
order.setLogisticsProviderId(provider.getId());
|
|
|
84 |
order.setAirwayBillNumber(airwaybillNo);
|
|
|
85 |
order.setStatus(OrderStatus.SHIPPED_FROM_WH);
|
|
|
86 |
order.setStatusDescription("Order shipped from warehouses");
|
| 31077 |
tejbeer |
87 |
|
| 33741 |
amit.gupta |
88 |
ProviderTat pt = providerTatRepository.selectByProviderId(order.getLogisticsProviderId(),
|
|
|
89 |
order.getWarehouseId(), order.getRetailerPinCode());
|
|
|
90 |
order.setShippingTimestamp(shippingTimestamp);
|
| 29222 |
tejbeer |
91 |
|
| 33741 |
amit.gupta |
92 |
LocalDate deliveryDate = this.calculateDeliveryTimeline(LocalDate.now(), pt, 0);
|
|
|
93 |
order.setExpectedDeliveryTime(deliveryDate.atStartOfDay());
|
| 29222 |
tejbeer |
94 |
|
| 33741 |
amit.gupta |
95 |
if (dispatchNotication.get(airwaybillNo) != null) {
|
|
|
96 |
DispatchNotificationModel dnm = dispatchNotication.get(airwaybillNo);
|
|
|
97 |
List<String> invoiceNumbers = dnm.getInvoiceNumber();
|
|
|
98 |
invoiceNumbers.add(order.getInvoiceNumber());
|
|
|
99 |
dnm.setInvoiceNumber(invoiceNumbers);
|
|
|
100 |
Float totalAmount = dnm.getTotalAmount();
|
|
|
101 |
dnm.setTotalAmount(totalAmount + order.getTotalAmount());
|
| 29222 |
tejbeer |
102 |
|
| 33741 |
amit.gupta |
103 |
int totalQty = dnm.getTotalQty();
|
|
|
104 |
dnm.setTotalQty(totalQty + order.getLineItem().getQuantity());
|
| 29222 |
tejbeer |
105 |
|
| 33741 |
amit.gupta |
106 |
dispatchNotication.put(airwaybillNo, dnm);
|
| 29222 |
tejbeer |
107 |
|
| 33741 |
amit.gupta |
108 |
} else {
|
|
|
109 |
DispatchNotificationModel dnm = new DispatchNotificationModel();
|
|
|
110 |
List<String> invoiceNumber = new ArrayList<>();
|
|
|
111 |
invoiceNumber.add(order.getInvoiceNumber());
|
|
|
112 |
dnm.setInvoiceNumber(invoiceNumber);
|
|
|
113 |
dnm.setTotalAmount(order.getTotalAmount());
|
|
|
114 |
dnm.setTotalQty(order.getLineItem().getQuantity());
|
|
|
115 |
dnm.setFofoId(order.getRetailerId());
|
|
|
116 |
dnm.setProviderId(order.getLogisticsProviderId());
|
|
|
117 |
dnm.setDate(deliveryDate);
|
|
|
118 |
dnm.setShippingDate(shippingTimestamp.toLocalDate());
|
|
|
119 |
dispatchNotication.put(airwaybillNo, dnm);
|
| 29222 |
tejbeer |
120 |
|
| 33741 |
amit.gupta |
121 |
}
|
| 29222 |
tejbeer |
122 |
|
| 33741 |
amit.gupta |
123 |
}
|
| 29222 |
tejbeer |
124 |
|
| 33741 |
amit.gupta |
125 |
}
|
|
|
126 |
return dispatchNotication;
|
| 29222 |
tejbeer |
127 |
|
| 33741 |
amit.gupta |
128 |
}
|
| 29222 |
tejbeer |
129 |
|
| 33741 |
amit.gupta |
130 |
@Autowired
|
|
|
131 |
WarehouseProviderRepository warehouseProviderRepository;
|
| 29222 |
tejbeer |
132 |
|
| 33741 |
amit.gupta |
133 |
@Autowired
|
|
|
134 |
WarehouseRiderRepository warehouseRiderRepository;
|
|
|
135 |
|
|
|
136 |
@Autowired
|
|
|
137 |
TransactionShipmentSequenceRepository transactionShipmentSequenceRepository;
|
|
|
138 |
|
| 34373 |
tejus.loha |
139 |
@Autowired
|
|
|
140 |
TransactionRepository transactionRepository;
|
| 33741 |
amit.gupta |
141 |
|
| 34373 |
tejus.loha |
142 |
@Autowired
|
|
|
143 |
WarehousePartnerDistanceMappingRepository warehousePartnerDistanceMappingRepository;
|
|
|
144 |
|
| 33741 |
amit.gupta |
145 |
@Override
|
| 34373 |
tejus.loha |
146 |
public E_InvoiceCommon.ReqPlGenIRN.EwbDetails getEwbDetails(Order order) throws ProfitMandiBusinessException {
|
| 33741 |
amit.gupta |
147 |
|
|
|
148 |
E_InvoiceCommon.ReqPlGenIRN.EwbDetails ewbDetails = new E_InvoiceCommon.ReqPlGenIRN.EwbDetails();
|
|
|
149 |
//ewbDetails.Distance = String.valueOf(200);
|
|
|
150 |
//1 is road, 2 is rail, 3 is air, 4 is ship
|
|
|
151 |
|
|
|
152 |
if (order.getLogisticsProviderId() == ProfitMandiConstants.LOGISTICS_PROVIDER_SELF_PICKUP
|
|
|
153 |
|| order.getLogisticsProviderId() == ProfitMandiConstants.LOGISTICS_PROVIDER_RUNNER) {
|
|
|
154 |
TransactionShipmentSequence transactionShipmentSequence = transactionShipmentSequenceRepository.selectByTransactionShipmentSequence(order.getLogisticsTransactionId());
|
| 35797 |
amit |
155 |
if (transactionShipmentSequence != null) {
|
|
|
156 |
ewbDetails.VehNo = transactionShipmentSequence.getVehicleNumber();
|
|
|
157 |
ewbDetails.VehType = "R";
|
|
|
158 |
ewbDetails.TransMode = "1";
|
|
|
159 |
ewbDetails.TransDocDt = com.spice.profitmandi.common.util.StringUtils.toString(order.getBillingTimestamp().toLocalDate());
|
|
|
160 |
ewbDetails.TransDocNo = order.getAirwayBillNumber();
|
|
|
161 |
} else {
|
|
|
162 |
LOGGER.warn("No shipment sequence found for logistics transaction: {}, order will get EWB Part A only", order.getLogisticsTransactionId());
|
|
|
163 |
}
|
| 33741 |
amit.gupta |
164 |
} else {
|
|
|
165 |
WarehouseProvider warehouseProvider = warehouseProviderRepository.selectByProviderWarehouse(order.getLogisticsProviderId(), order.getWarehouseId());
|
|
|
166 |
Provider provider = providerRepository.selectById(order.getLogisticsProviderId());
|
| 35797 |
amit |
167 |
if (warehouseProvider != null && provider != null) {
|
|
|
168 |
ewbDetails.TransId = warehouseProvider.getGstin();
|
|
|
169 |
ewbDetails.TransName = provider.getName();
|
|
|
170 |
} else {
|
|
|
171 |
LOGGER.warn("No provider details for logistics provider: {}, warehouse: {}", order.getLogisticsProviderId(), order.getWarehouseId());
|
|
|
172 |
}
|
| 33741 |
amit.gupta |
173 |
}
|
|
|
174 |
|
| 34373 |
tejus.loha |
175 |
String distance = "0";
|
|
|
176 |
WarehousePartnerDistanceMapping distanceMapping = warehousePartnerDistanceMappingRepository.selectByWarehouseIdAndFofoId(order.getWarehouseId(), order.getRetailerId());
|
|
|
177 |
if (distanceMapping != null) {
|
|
|
178 |
distance = distanceMapping.getDistance();
|
|
|
179 |
}
|
|
|
180 |
LOGGER.info("distance - " + distance);
|
|
|
181 |
ewbDetails.Distance = distance;
|
| 33741 |
amit.gupta |
182 |
return ewbDetails;
|
|
|
183 |
|
|
|
184 |
}
|
|
|
185 |
|
| 29222 |
tejbeer |
186 |
}
|