Subversion Repositories SmartDukaan

Rev

Rev 32974 | Rev 34372 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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