| 31568 |
tejbeer |
1 |
package com.spice.profitmandi.dao.service.dtdc;
|
|
|
2 |
|
|
|
3 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
4 |
import com.spice.profitmandi.common.model.CustomRetailer;
|
|
|
5 |
import com.spice.profitmandi.common.util.FileUtil;
|
|
|
6 |
import com.spice.profitmandi.common.util.Utils;
|
|
|
7 |
import com.spice.profitmandi.common.util.Utils.Attachment;
|
|
|
8 |
import com.spice.profitmandi.common.web.client.RestClient;
|
|
|
9 |
import com.spice.profitmandi.dao.entity.transaction.Order;
|
|
|
10 |
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMapping;
|
|
|
11 |
import com.spice.profitmandi.dao.entity.transaction.WarehouseAddressMaster;
|
|
|
12 |
import com.spice.profitmandi.dao.model.BilledOrderListModel;
|
|
|
13 |
import com.spice.profitmandi.dao.repository.transaction.OrderRepository;
|
| 32390 |
amit.gupta |
14 |
import com.spice.profitmandi.dao.repository.transaction.WarehouseAddressMappingRepository;
|
|
|
15 |
import com.spice.profitmandi.dao.repository.transaction.WarehouseAddressMasterRepository;
|
| 31568 |
tejbeer |
16 |
import com.spice.profitmandi.service.user.RetailerService;
|
|
|
17 |
import in.shop2020.model.v1.order.OrderStatus;
|
| 32390 |
amit.gupta |
18 |
import org.apache.logging.log4j.LogManager;
|
|
|
19 |
import org.apache.logging.log4j.Logger;
|
|
|
20 |
import org.json.JSONArray;
|
|
|
21 |
import org.json.JSONObject;
|
|
|
22 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
23 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
24 |
import org.springframework.core.io.ByteArrayResource;
|
|
|
25 |
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
26 |
import org.springframework.stereotype.Component;
|
| 31568 |
tejbeer |
27 |
|
| 32390 |
amit.gupta |
28 |
import java.util.*;
|
|
|
29 |
import java.util.Map.Entry;
|
|
|
30 |
import java.util.stream.Collectors;
|
|
|
31 |
|
| 31568 |
tejbeer |
32 |
@Component
|
|
|
33 |
public class DTDCServiceImpl implements DTDCService {
|
|
|
34 |
|
|
|
35 |
@Autowired
|
|
|
36 |
private RetailerService retailerService;
|
|
|
37 |
|
|
|
38 |
@Autowired
|
| 32390 |
amit.gupta |
39 |
private WarehouseAddressMappingRepository warehouseAddressMappingRepository;
|
| 31568 |
tejbeer |
40 |
|
|
|
41 |
@Autowired
|
| 32390 |
amit.gupta |
42 |
private WarehouseAddressMasterRepository warehouseAddressMasterRepository;
|
| 31568 |
tejbeer |
43 |
|
|
|
44 |
@Autowired
|
|
|
45 |
private OrderRepository orderRepository;
|
|
|
46 |
|
|
|
47 |
@Autowired
|
|
|
48 |
private RestClient restClient;
|
|
|
49 |
|
|
|
50 |
@Autowired
|
|
|
51 |
private JavaMailSender googleMailSender;
|
|
|
52 |
|
|
|
53 |
@Value("#{'${prod}'=='true' ? 'NL3832' : 'NL3832'}")
|
|
|
54 |
private String customerCode;
|
|
|
55 |
|
|
|
56 |
@Value("#{'${prod}'=='true' ? '4b2a38cf831edcf397629f68684da6' : '8c0800cae8401d95570d7551be9bd7'}")
|
|
|
57 |
private String apiKey;
|
|
|
58 |
|
|
|
59 |
private static final Logger LOGGER = LogManager.getLogger(DTDCServiceImpl.class);
|
|
|
60 |
|
|
|
61 |
@Override
|
|
|
62 |
public List<BilledOrderListModel> getAirwayBillNo(Map<String, List<BilledOrderListModel>> airbillBilledOrderMap,
|
|
|
63 |
String emailId) throws Exception {
|
|
|
64 |
|
|
|
65 |
List<BilledOrderListModel> billedOrderListModels = new ArrayList<>();
|
|
|
66 |
|
|
|
67 |
Consignments consignments = new Consignments();
|
|
|
68 |
List<Consignment> consignmentList = new ArrayList<>();
|
|
|
69 |
|
|
|
70 |
List<String> consigneeIds = new ArrayList<>();
|
|
|
71 |
for (Entry<String, List<BilledOrderListModel>> billedOrderEntry : airbillBilledOrderMap.entrySet()) {
|
|
|
72 |
|
|
|
73 |
List<BilledOrderListModel> billedOrders = billedOrderEntry.getValue();
|
|
|
74 |
|
|
|
75 |
double weight = billedOrders.get(0).getWeight();
|
|
|
76 |
|
|
|
77 |
List<String> invoiceNumbers = billedOrders.stream().map(x -> x.getInvoiceNumber())
|
|
|
78 |
.collect(Collectors.toList());
|
|
|
79 |
|
|
|
80 |
List<Order> orders = orderRepository.selectByInvoiceNumbers(invoiceNumbers);
|
|
|
81 |
|
|
|
82 |
Double totalAmount = orders.stream().collect(Collectors.summingDouble(x -> x.getTotalAmount()));
|
|
|
83 |
|
|
|
84 |
for (Order order : orders) {
|
|
|
85 |
if (!order.getStatus().equals(OrderStatus.BILLED)) {
|
|
|
86 |
|
|
|
87 |
throw new ProfitMandiBusinessException("Uploaded File", "",
|
|
|
88 |
"Order Number " + order.getId() + " status is not billed");
|
|
|
89 |
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
consigneeIds.add(billedOrderEntry.getKey());
|
|
|
94 |
Order order = orders.get(0);
|
|
|
95 |
|
|
|
96 |
int warehouseId = order.getWarehouseId();
|
|
|
97 |
|
|
|
98 |
CustomRetailer customRetailer = retailerService.getFofoRetailer(order.getRetailerId());
|
|
|
99 |
|
| 32390 |
amit.gupta |
100 |
WarehouseAddressMapping warehouseAddressMapping = warehouseAddressMappingRepository
|
| 31568 |
tejbeer |
101 |
.selectByWarehouseId(warehouseId);
|
|
|
102 |
|
| 32390 |
amit.gupta |
103 |
WarehouseAddressMaster warehouseAddressMaster = warehouseAddressMasterRepository
|
| 31568 |
tejbeer |
104 |
.selectById(warehouseAddressMapping.getAddressId());
|
|
|
105 |
|
|
|
106 |
Consignment consignment = new Consignment();
|
|
|
107 |
consignment.setCustomerCode(customerCode);
|
|
|
108 |
consignment.setServiceTypeId("PRIORITY");
|
|
|
109 |
consignment.setCommodityId("2");
|
|
|
110 |
consignment.setDeclaredValue(totalAmount.intValue());
|
|
|
111 |
consignment.setConsignmentType("Forward");
|
|
|
112 |
consignment.setLoadType("NON-DOCUMENT");
|
|
|
113 |
consignment.setWeghtUnit("kg");
|
|
|
114 |
consignment.setWeight(weight);
|
|
|
115 |
|
|
|
116 |
OriginDetails origionDetail = new OriginDetails();
|
|
|
117 |
origionDetail.setPincode("201301");// warehouseAddressMaster.getPin());
|
|
|
118 |
origionDetail.setName(warehouseAddressMaster.getContactPerson());
|
|
|
119 |
origionDetail.setPhone(warehouseAddressMaster.getContacNumber());
|
|
|
120 |
origionDetail.setAddressLine1(warehouseAddressMaster.getAddress());
|
|
|
121 |
|
|
|
122 |
consignment.setOriginDetail(origionDetail);
|
|
|
123 |
|
|
|
124 |
DestinationDetails destinationDetail = new DestinationDetails();
|
|
|
125 |
destinationDetail.setName(customRetailer.getBusinessName());
|
|
|
126 |
destinationDetail.setAddressLine1(customRetailer.getAddress().getLine1());
|
|
|
127 |
destinationDetail.setAddressLine2(customRetailer.getAddress().getLine2());
|
|
|
128 |
destinationDetail.setPhone(customRetailer.getMobileNumber());
|
|
|
129 |
destinationDetail.setPincode(customRetailer.getAddress().getPinCode());
|
|
|
130 |
destinationDetail.setState(customRetailer.getAddress().getState());
|
|
|
131 |
destinationDetail.setCity(customRetailer.getAddress().getCity());
|
|
|
132 |
|
|
|
133 |
consignment.setDestinationDetail(destinationDetail);
|
|
|
134 |
|
|
|
135 |
List<PiecesDetail> piecesDetails = new ArrayList<>();
|
|
|
136 |
|
|
|
137 |
PiecesDetail pieceDetail = new PiecesDetail();
|
|
|
138 |
pieceDetail.setDeclaredValue(totalAmount);
|
|
|
139 |
pieceDetail.setWidth(1.0);
|
|
|
140 |
|
|
|
141 |
pieceDetail.setHeight(1.0);
|
|
|
142 |
pieceDetail.setLength(1.0);
|
|
|
143 |
|
|
|
144 |
piecesDetails.add(pieceDetail);
|
|
|
145 |
|
|
|
146 |
consignment.setPiecesDetails(piecesDetails);
|
|
|
147 |
|
|
|
148 |
consignmentList.add(consignment);
|
|
|
149 |
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
consignments.setConsignments(consignmentList);
|
|
|
153 |
LOGGER.info("consignments" + consignments);
|
|
|
154 |
|
|
|
155 |
Map<String, String> requestheaders = new HashMap<>();
|
|
|
156 |
requestheaders.put("Content-Type", "application/json");
|
|
|
157 |
requestheaders.put("api-key", apiKey);
|
|
|
158 |
|
|
|
159 |
String response = restClient.postJson(
|
|
|
160 |
"https://demodashboardapi.shipsy.in/api/customer/integration/consignment/softdata", consignments,
|
|
|
161 |
requestheaders);
|
|
|
162 |
|
|
|
163 |
LOGGER.info("response" + response);
|
|
|
164 |
List<Attachment> attachments = new ArrayList<>();
|
|
|
165 |
|
|
|
166 |
List<List<?>> rows = new ArrayList<>();
|
|
|
167 |
|
|
|
168 |
JSONObject resp = new JSONObject(response);
|
|
|
169 |
JSONArray referenceData = resp.getJSONArray("data");
|
|
|
170 |
|
|
|
171 |
for (int j = 0; j < referenceData.length(); j++) {
|
|
|
172 |
|
|
|
173 |
String consigneeId = consigneeIds.get(j);
|
|
|
174 |
|
|
|
175 |
List<BilledOrderListModel> billedOrders = airbillBilledOrderMap.get(consigneeId);
|
|
|
176 |
|
|
|
177 |
JSONObject referenceDetail = referenceData.getJSONObject(j);
|
|
|
178 |
|
|
|
179 |
boolean success = referenceDetail.getBoolean("success");
|
|
|
180 |
String airWayBill = "Error";
|
|
|
181 |
if (success) {
|
|
|
182 |
|
|
|
183 |
airWayBill = referenceDetail.getString("reference_number");
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
for (BilledOrderListModel billedOrder : billedOrders) {
|
|
|
187 |
|
|
|
188 |
billedOrder.setAirwayBillNumber(airWayBill);
|
|
|
189 |
if (!airWayBill.equals("Error")) {
|
|
|
190 |
|
|
|
191 |
rows.add(Arrays.asList(billedOrder.getInvoiceNumber(), billedOrder.getLogisticsProviderName(),
|
|
|
192 |
billedOrder.getAirwayBillNumber(), billedOrder.getWeight()));
|
|
|
193 |
} else {
|
|
|
194 |
|
|
|
195 |
String message = referenceDetail.getString("message");
|
|
|
196 |
|
|
|
197 |
LOGGER.info("message" + message);
|
|
|
198 |
|
|
|
199 |
rows.add(Arrays.asList(billedOrder.getInvoiceNumber(), billedOrder.getLogisticsProviderName(),
|
|
|
200 |
billedOrder.getAirwayBillNumber(), billedOrder.getWeight(),
|
|
|
201 |
referenceDetail.getString("message")));
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
billedOrderListModels.addAll(billedOrders);
|
|
|
207 |
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
org.apache.commons.io.output.ByteArrayOutputStream baos = FileUtil
|
|
|
211 |
.getCSVByteStream(Arrays.asList("Invoice Number", "Provider", "AWB", "Weight", "Reason"), rows);
|
|
|
212 |
|
|
|
213 |
Attachment xlsattachment = new Attachment("uploaded bluedart sheet", new ByteArrayResource(baos.toByteArray()));
|
|
|
214 |
|
|
|
215 |
attachments.add(xlsattachment);
|
|
|
216 |
|
|
|
217 |
Map<String, List<BilledOrderListModel>> awbOrderMap = billedOrderListModels.stream()
|
|
|
218 |
.collect(Collectors.groupingBy(x -> x.getAirwayBillNumber(), Collectors.toList()));
|
|
|
219 |
|
|
|
220 |
for (Entry<String, List<BilledOrderListModel>> awbOrderEntry : awbOrderMap.entrySet()) {
|
|
|
221 |
|
|
|
222 |
if (!awbOrderEntry.getKey().equals("Error")) {
|
|
|
223 |
Map<String, String> referenceNumber = new HashMap<>();
|
|
|
224 |
referenceNumber.put("reference_number", awbOrderEntry.getKey());
|
|
|
225 |
|
|
|
226 |
String responseLabel = restClient.postJson(
|
|
|
227 |
"https://demodashboardapi.shipsy.in/api/customer/integration/consignment/label/multipiece",
|
|
|
228 |
referenceNumber, requestheaders);
|
|
|
229 |
|
|
|
230 |
JSONObject respLabel = new JSONObject(responseLabel);
|
|
|
231 |
JSONArray labelData = respLabel.getJSONArray("data");
|
|
|
232 |
|
|
|
233 |
for (int j = 0; j < labelData.length(); j++) {
|
|
|
234 |
JSONObject referenceDetail = labelData.getJSONObject(j);
|
|
|
235 |
|
|
|
236 |
String airWayBill = referenceDetail.getString("reference_number");
|
|
|
237 |
|
|
|
238 |
String label = referenceDetail.getString("label");
|
|
|
239 |
byte[] decodedString = Base64.getDecoder().decode(label.getBytes("UTF-8"));
|
|
|
240 |
|
|
|
241 |
Attachment attachment = new Attachment("DTDC" + airWayBill + ".pdf",
|
|
|
242 |
new ByteArrayResource(decodedString));
|
|
|
243 |
|
|
|
244 |
attachments.add(attachment);
|
|
|
245 |
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
}
|
|
|
249 |
}
|
|
|
250 |
|
| 34578 |
vikas.jang |
251 |
Utils.sendMailWithAttachments(googleMailSender, new String[] { "sdtech@smartdukaan.com" }, null,
|
| 31568 |
tejbeer |
252 |
"DTDC AWb", "PFA", attachments.toArray(new Utils.Attachment[attachments.size()]));
|
|
|
253 |
|
|
|
254 |
LOGGER.info("billedOrderListModels" + billedOrderListModels);
|
|
|
255 |
|
|
|
256 |
return billedOrderListModels;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
}
|