| 29349 |
tejbeer |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
| 29418 |
tejbeer |
3 |
import java.time.LocalDate;
|
|
|
4 |
import java.time.LocalDateTime;
|
|
|
5 |
import java.time.temporal.ChronoUnit;
|
| 29349 |
tejbeer |
6 |
import java.util.ArrayList;
|
| 29419 |
tejbeer |
7 |
import java.util.Arrays;
|
| 29349 |
tejbeer |
8 |
import java.util.List;
|
|
|
9 |
|
|
|
10 |
import javax.servlet.http.HttpServletRequest;
|
|
|
11 |
import javax.servlet.http.HttpServletResponse;
|
|
|
12 |
|
|
|
13 |
import org.apache.logging.log4j.LogManager;
|
|
|
14 |
import org.apache.logging.log4j.Logger;
|
| 29418 |
tejbeer |
15 |
import org.apache.thrift.TException;
|
| 29349 |
tejbeer |
16 |
import org.json.JSONArray;
|
|
|
17 |
import org.json.JSONObject;
|
|
|
18 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
19 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
20 |
import org.springframework.http.MediaType;
|
|
|
21 |
import org.springframework.http.ResponseEntity;
|
|
|
22 |
import org.springframework.stereotype.Controller;
|
|
|
23 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
24 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
25 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
26 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
27 |
|
| 29418 |
tejbeer |
28 |
import com.spice.profitmandi.common.model.CustomOrder;
|
| 29420 |
tejbeer |
29 |
import com.spice.profitmandi.common.model.TrackingModel;
|
| 29349 |
tejbeer |
30 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 29418 |
tejbeer |
31 |
import com.spice.profitmandi.dao.entity.logistics.Provider;
|
|
|
32 |
import com.spice.profitmandi.dao.entity.logistics.ProviderTat;
|
| 29349 |
tejbeer |
33 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
| 29418 |
tejbeer |
34 |
import com.spice.profitmandi.dao.model.ProductPojo;
|
|
|
35 |
import com.spice.profitmandi.dao.repository.logistics.ProviderRepository;
|
|
|
36 |
import com.spice.profitmandi.dao.repository.logistics.ProviderTatRepository;
|
|
|
37 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
|
|
38 |
import com.spice.profitmandi.dao.util.ContentPojoPopulator;
|
| 29349 |
tejbeer |
39 |
import com.spice.profitmandi.model.tracking.ScannedDetailModel;
|
|
|
40 |
import com.spice.profitmandi.model.tracking.TrackingDetailModel;
|
| 29418 |
tejbeer |
41 |
import com.spice.profitmandi.service.LogisticsService;
|
| 29349 |
tejbeer |
42 |
|
| 29418 |
tejbeer |
43 |
import in.shop2020.model.v1.order.OrderStatusGroups;
|
| 29349 |
tejbeer |
44 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
45 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
46 |
import okhttp3.OkHttpClient;
|
|
|
47 |
import okhttp3.Request;
|
|
|
48 |
import okhttp3.Response;
|
|
|
49 |
|
|
|
50 |
@Controller
|
|
|
51 |
@Transactional(rollbackFor = Throwable.class)
|
|
|
52 |
public class TrackingController {
|
|
|
53 |
|
|
|
54 |
@Value("${delhivery.tracking.token}")
|
|
|
55 |
private String token;
|
|
|
56 |
|
|
|
57 |
@Autowired
|
|
|
58 |
private ResponseSender<?> responseSender;
|
|
|
59 |
|
| 29418 |
tejbeer |
60 |
@Autowired
|
|
|
61 |
private OrderRepository orderRepository;
|
|
|
62 |
|
|
|
63 |
@Autowired
|
|
|
64 |
private ProviderRepository providerRepository;
|
|
|
65 |
|
|
|
66 |
@Autowired
|
|
|
67 |
private ContentPojoPopulator contentPojoPopulator;
|
|
|
68 |
|
|
|
69 |
@Autowired
|
|
|
70 |
private ProviderTatRepository providerTatRepository;
|
|
|
71 |
|
|
|
72 |
@Autowired
|
|
|
73 |
private LogisticsService logisticsService;
|
| 31087 |
tejbeer |
74 |
|
|
|
75 |
@Value("#{'${prod}'=='true' ? 'DEL81122' : 'DEL95932'}")
|
|
|
76 |
private String loginId;
|
|
|
77 |
|
|
|
78 |
@Value("#{'${prod}'=='true' ? '9sjnjt61hghmtjuvjio0osqkmujkphlt' : 'poehq2kiqjnllkrote9hxglo4teptfqf'}")
|
|
|
79 |
private String licencekey;
|
|
|
80 |
|
| 29349 |
tejbeer |
81 |
private static final Logger LOGGER = LogManager.getLogger(TrackingController.class);
|
|
|
82 |
|
|
|
83 |
@RequestMapping(value = "/order/tracking", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
84 |
@ApiImplicitParams({
|
|
|
85 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 29418 |
tejbeer |
86 |
public ResponseEntity<?> getDeliveryOrder(HttpServletRequest request, HttpServletResponse response,
|
| 29349 |
tejbeer |
87 |
@RequestParam String airwayBillNo) throws Throwable {
|
|
|
88 |
|
|
|
89 |
String url = "https://track.delhivery.com/api/v1/packages/json/";
|
|
|
90 |
|
|
|
91 |
OkHttpClient client = new OkHttpClient();
|
|
|
92 |
|
|
|
93 |
Request request1 = new Request.Builder()
|
|
|
94 |
.url(url + "?waybill=" + String.join(",", airwayBillNo) + "&token=" + token).get().build();
|
|
|
95 |
|
|
|
96 |
LOGGER.info("request1" + request1);
|
|
|
97 |
|
|
|
98 |
Response response1 = client.newCall(request1).execute();
|
|
|
99 |
|
|
|
100 |
LOGGER.info("jsonbj" + response1.body());
|
|
|
101 |
|
|
|
102 |
JSONObject jsonObj = new JSONObject(response1.body().string());
|
|
|
103 |
|
|
|
104 |
LOGGER.info("jsonObj" + jsonObj);
|
|
|
105 |
|
|
|
106 |
JSONArray shipmentData = jsonObj.getJSONArray("ShipmentData");
|
|
|
107 |
|
|
|
108 |
TrackingDetailModel tdm = new TrackingDetailModel();
|
|
|
109 |
|
|
|
110 |
List<ScannedDetailModel> sdms = new ArrayList<>();
|
|
|
111 |
|
|
|
112 |
for (int i = 0; i < shipmentData.length(); i++) {
|
|
|
113 |
JSONObject jsonObject = shipmentData.getJSONObject(i);
|
|
|
114 |
JSONObject shipment = jsonObject.getJSONObject("Shipment");
|
|
|
115 |
|
|
|
116 |
JSONArray scans = shipment.getJSONArray("Scans");
|
|
|
117 |
|
|
|
118 |
for (int j = 0; j < scans.length(); j++) {
|
|
|
119 |
JSONObject scanObject = scans.getJSONObject(j);
|
|
|
120 |
|
|
|
121 |
JSONObject scandetails = scanObject.getJSONObject("ScanDetail");
|
|
|
122 |
|
|
|
123 |
ScannedDetailModel sdm = new ScannedDetailModel();
|
|
|
124 |
sdm.setStatusDateTime(scandetails.getString("ScanDateTime"));
|
|
|
125 |
sdm.setStatusLocation(scandetails.getString("ScannedLocation"));
|
|
|
126 |
sdm.setInstruction(scandetails.getString("Instructions"));
|
|
|
127 |
sdms.add(sdm);
|
|
|
128 |
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
tdm.setExpectedDelivery(shipment.getString("ExpectedDeliveryDate"));
|
|
|
132 |
tdm.setAwb(shipment.getString("AWB"));
|
|
|
133 |
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
tdm.setSdm(sdms);
|
|
|
137 |
LOGGER.info("tdm" + tdm);
|
|
|
138 |
|
|
|
139 |
return responseSender.ok(tdm);
|
|
|
140 |
}
|
|
|
141 |
|
| 31087 |
tejbeer |
142 |
@RequestMapping(value = "/order/tracking/bluedart", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
143 |
@ApiImplicitParams({
|
|
|
144 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
145 |
public ResponseEntity<?> getBluedartOrderTracking(HttpServletRequest request) throws Throwable {
|
|
|
146 |
|
| 31113 |
tejbeer |
147 |
String airwayBillNo = "81007630141";
|
| 31087 |
tejbeer |
148 |
|
|
|
149 |
String url = "http://api.bluedart.com/servlet/RoutingServlet?handler=tnt&action=custawbquery";
|
|
|
150 |
|
|
|
151 |
OkHttpClient client = new OkHttpClient();
|
|
|
152 |
|
|
|
153 |
Request request1 = new Request.Builder().url(url + "&loginid=" + loginId + "&awb=awb&numbers="
|
|
|
154 |
+ String.join(",", airwayBillNo) + "&format=xml&lickey=" + licencekey + "&verno=1.3&scan=1").get()
|
|
|
155 |
.build();
|
|
|
156 |
|
|
|
157 |
LOGGER.info("request1" + request1);
|
|
|
158 |
|
|
|
159 |
Response response1 = client.newCall(request1).execute();
|
|
|
160 |
|
|
|
161 |
LOGGER.info("jsonbj" + response1);
|
|
|
162 |
|
|
|
163 |
TrackingDetailModel tdm = new TrackingDetailModel();
|
|
|
164 |
|
|
|
165 |
return responseSender.ok(tdm);
|
|
|
166 |
}
|
|
|
167 |
|
| 29418 |
tejbeer |
168 |
@RequestMapping(value = "/order/detail", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
169 |
@ApiImplicitParams({
|
|
|
170 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
171 |
public ResponseEntity<?> getOrderDetails(HttpServletRequest request, HttpServletResponse response,
|
| 29419 |
tejbeer |
172 |
@RequestParam String airwayBillNo, @RequestParam int orderId) throws Throwable {
|
|
|
173 |
|
|
|
174 |
LOGGER.info("airwayBillNo" + airwayBillNo);
|
|
|
175 |
List<Order> orders = null;
|
|
|
176 |
if (!airwayBillNo.equals("NaN")) {
|
|
|
177 |
orders = orderRepository.selectByAirwayBillNumber(airwayBillNo);
|
|
|
178 |
} else {
|
|
|
179 |
orders = orderRepository.selectByOrderIds(Arrays.asList(orderId));
|
|
|
180 |
}
|
| 29418 |
tejbeer |
181 |
List<CustomOrder> customOrders = new ArrayList<CustomOrder>();
|
|
|
182 |
|
|
|
183 |
for (Order order : orders) {
|
|
|
184 |
CustomOrder co = null;
|
|
|
185 |
|
|
|
186 |
try {
|
|
|
187 |
co = toCustomOrder(order);
|
|
|
188 |
} catch (Exception e) {
|
|
|
189 |
continue;
|
|
|
190 |
}
|
|
|
191 |
customOrders.add(co);
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
return responseSender.ok(customOrders);
|
|
|
195 |
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
private CustomOrder toCustomOrder(Order order) throws Exception {
|
|
|
199 |
CustomOrder co = new CustomOrder();
|
|
|
200 |
co.setId(order.getId());
|
|
|
201 |
co.setRetailerName(order.getRetailerName());
|
|
|
202 |
co.setExpectedDeliveryTime(order.getExpectedDeliveryTime());
|
|
|
203 |
co.setPromisedDeliveryTime(order.getPromisedDeliveryTime());
|
|
|
204 |
co.setRetailerPinCode(order.getRetailerPinCode());
|
|
|
205 |
co.setRetailerAddress1(order.getRetailerAddress1());
|
|
|
206 |
co.setRetailerCity(order.getRetailerCity());
|
|
|
207 |
co.setStatusDescription(order.getStatusDescription());
|
|
|
208 |
co.setStatus(order.getStatus());
|
|
|
209 |
co.setBrand(order.getLineItem().getBrand());
|
|
|
210 |
co.setTransactionId(order.getTransactionId());
|
|
|
211 |
co.setExtraInfo(order.getLineItem().getExtraInfo());
|
|
|
212 |
co.setItemId(order.getLineItem().getItemId());
|
|
|
213 |
co.setQuantity(order.getLineItem().getQuantity());
|
|
|
214 |
co.setTotalAmount(order.getTotalAmount());
|
|
|
215 |
co.setColor(order.getLineItem().getColor());
|
|
|
216 |
co.setShippingCost(order.getShippingCost());
|
|
|
217 |
co.setCreateTimestamp(order.getCreateTimestamp());
|
|
|
218 |
co.setModelName(order.getLineItem().getModelName());
|
|
|
219 |
co.setModelNumber(order.getLineItem().getModelNumber());
|
|
|
220 |
co.setDeliveryTimestamp(order.getDeliveryTimestamp());
|
|
|
221 |
co.setImageUrl(getImageUrl(order));
|
|
|
222 |
co.setCancellable(getCancellableRequest(order));
|
|
|
223 |
co.setProviderId(order.getLogisticsProviderId());
|
|
|
224 |
co.setDelayReason(order.getDelayReasonText());
|
|
|
225 |
Provider provider = providerRepository.selectById(order.getLogisticsProviderId());
|
|
|
226 |
co.setProviderName(provider.getName());
|
|
|
227 |
co.setAirwayBillNo(order.getAirwayBillNumber());
|
|
|
228 |
co.setCancellationTimestamp(order.getRefundTimestamp());
|
|
|
229 |
co.setBillingTimestamp(order.getBillingTimestamp());
|
|
|
230 |
co.setShippingTimestamp(order.getShippingTimestamp());
|
|
|
231 |
co.setInvoiceNumber(order.getInvoiceNumber());
|
| 29420 |
tejbeer |
232 |
co.setExpectedShipping(order.getExpectedShippingTime());
|
| 29418 |
tejbeer |
233 |
if (order.getShippingTimestamp() != null) {
|
|
|
234 |
ProviderTat pt = providerTatRepository.selectByProviderId(order.getLogisticsProviderId(),
|
|
|
235 |
order.getWarehouseId(), order.getRetailerPinCode());
|
|
|
236 |
LocalDateTime shipping = order.getShippingTimestamp();
|
|
|
237 |
|
|
|
238 |
LocalDate deliveryTime = logisticsService.calculateDeliveryTimeline(shipping.toLocalDate(), pt, 0);
|
|
|
239 |
LOGGER.info("deliveryTime" + deliveryTime);
|
|
|
240 |
LocalDateTime expected = order.getExpectedDeliveryTime();
|
|
|
241 |
LOGGER.info("expected" + expected);
|
|
|
242 |
|
|
|
243 |
long noOfDaysBetween = ChronoUnit.DAYS.between(expected, deliveryTime.atStartOfDay());
|
|
|
244 |
|
|
|
245 |
co.setDelayAfterShipping((int) noOfDaysBetween);
|
|
|
246 |
|
|
|
247 |
}
|
| 29420 |
tejbeer |
248 |
TrackingModel tm = new TrackingModel();
|
|
|
249 |
if (co.getBillingTimestamp() != null) {
|
|
|
250 |
tm.setActualBilling(co.getBillingTimestamp());
|
|
|
251 |
tm.setInTransitBilling(false);
|
|
|
252 |
} else {
|
|
|
253 |
tm.setInTransitBilling(true);
|
|
|
254 |
}
|
| 29418 |
tejbeer |
255 |
|
| 29420 |
tejbeer |
256 |
if (co.getShippingTimestamp() != null) {
|
|
|
257 |
tm.setActualShipping(co.getShippingTimestamp());
|
|
|
258 |
tm.setInTransitShipping(false);
|
|
|
259 |
} else {
|
| 29422 |
tejbeer |
260 |
if (co.getBillingTimestamp() != null) {
|
|
|
261 |
co.setExpectedShipping(co.getBillingTimestamp());
|
|
|
262 |
|
|
|
263 |
if (LocalDate.now().isAfter(co.getExpectedShipping().toLocalDate())) {
|
| 31087 |
tejbeer |
264 |
long noOfDaysBetween = ChronoUnit.DAYS.between(co.getExpectedShipping(),
|
|
|
265 |
LocalDate.now().atStartOfDay());
|
| 29429 |
tejbeer |
266 |
|
| 31087 |
tejbeer |
267 |
co.setExpectedShipping(co.getBillingTimestamp().plusDays(noOfDaysBetween + 1));
|
| 29422 |
tejbeer |
268 |
}
|
|
|
269 |
}
|
| 29420 |
tejbeer |
270 |
tm.setExpectedShipping(co.getExpectedShipping());
|
| 29421 |
tejbeer |
271 |
if (tm.isInTransitBilling()) {
|
|
|
272 |
tm.setInTransitShipping(false);
|
|
|
273 |
} else {
|
|
|
274 |
tm.setInTransitShipping(true);
|
|
|
275 |
}
|
| 29420 |
tejbeer |
276 |
}
|
|
|
277 |
|
|
|
278 |
if (co.getDeliveryTimestamp() != null) {
|
|
|
279 |
tm.setActualDelivered(co.getDeliveryTimestamp());
|
|
|
280 |
tm.setInTransitDelivered(false);
|
|
|
281 |
} else {
|
| 29423 |
tejbeer |
282 |
tm.setExpectedDelivered(co.getExpectedDeliveryTime());
|
| 29424 |
tejbeer |
283 |
if (tm.isInTransitBilling()) {
|
| 29421 |
tejbeer |
284 |
tm.setInTransitDelivered(false);
|
| 29424 |
tejbeer |
285 |
}
|
|
|
286 |
|
|
|
287 |
else if (tm.isInTransitShipping()) {
|
|
|
288 |
tm.setInTransitDelivered(false);
|
| 29421 |
tejbeer |
289 |
} else {
|
|
|
290 |
tm.setInTransitDelivered(true);
|
|
|
291 |
}
|
| 29420 |
tejbeer |
292 |
}
|
|
|
293 |
co.setTrackingModel(tm);
|
| 29418 |
tejbeer |
294 |
return co;
|
|
|
295 |
}
|
|
|
296 |
|
|
|
297 |
private String getImageUrl(Order order) throws TException {
|
|
|
298 |
|
|
|
299 |
ProductPojo pp = contentPojoPopulator.getShortContent(order.getLineItem().getItem().getCatalogItemId());
|
|
|
300 |
String imageUrl = "";
|
|
|
301 |
LOGGER.info("pp {}", pp);
|
|
|
302 |
if (pp != null) {
|
|
|
303 |
imageUrl = pp.getImageUrl();
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
return imageUrl;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
private Boolean getCancellableRequest(Order order) throws Exception {
|
|
|
310 |
OrderStatusGroups orderStatusGroups = new OrderStatusGroups();
|
|
|
311 |
if (order.isCod() == null) {
|
|
|
312 |
throw new Exception("Invalid order");
|
|
|
313 |
}
|
|
|
314 |
if (order.isCod()) {
|
|
|
315 |
if (orderStatusGroups.getCodCancellable().contains(order.getStatus())) {
|
|
|
316 |
return true;
|
|
|
317 |
} else {
|
|
|
318 |
return false;
|
|
|
319 |
}
|
|
|
320 |
} else {
|
|
|
321 |
if (orderStatusGroups.getPrepaidCancellableBeforeBilled().contains(order.getStatus())) {
|
|
|
322 |
return true;
|
|
|
323 |
} else if (orderStatusGroups.getPrepaidCancellableAfterBilled().contains(order.getStatus())) {
|
|
|
324 |
return true;
|
|
|
325 |
} else {
|
|
|
326 |
return false;
|
|
|
327 |
}
|
|
|
328 |
}
|
|
|
329 |
}
|
|
|
330 |
|
| 29349 |
tejbeer |
331 |
}
|