Subversion Repositories SmartDukaan

Rev

Rev 34373 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.service;

import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.dao.entity.logistics.*;
import com.spice.profitmandi.dao.entity.transaction.Order;
import com.spice.profitmandi.dao.entity.transaction.Transaction;
import com.spice.profitmandi.dao.entity.transaction.TransactionShipmentSequence;
import com.spice.profitmandi.dao.model.DispatchNotificationModel;
import com.spice.profitmandi.dao.repository.logistics.*;
import com.spice.profitmandi.dao.repository.transaction.TransactionRepository;
import com.spice.profitmandi.dao.repository.transaction.TransactionShipmentSequenceRepository;
import com.spice.profitmandi.service.integrations.gstpro.api.model.E_InvoiceCommon;
import in.shop2020.model.v1.order.OrderStatus;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class LogisticsServiceImpl implements LogisticsService {

    @Autowired
    private PublicHolidaysRepository publicHolidaysRepository;

    @Autowired
    private ProviderTatRepository providerTatRepository;

    @Autowired
    ProviderRepository providerRepository;

    private static final Logger LOGGER = LogManager.getLogger(LogisticsServiceImpl.class);


    @Override
    public LocalDate calculateDeliveryTimeline(LocalDate date, ProviderTat providerTat, int delayDays) {
        int businessDays = 0;
        int deliveryTat = 3;
        if (providerTat != null) {
            deliveryTat = providerTat.getDeliveryTime();
        }
        deliveryTat = deliveryTat + delayDays;
        LocalDate currDate = date.plusDays(1);
        List<LocalDate> ph = publicHolidaysRepository.selectAllByDate(currDate).stream().map(x -> x.getDate())
                .collect(Collectors.toList());
        while (businessDays < deliveryTat) {
            if (ph.contains(currDate) || currDate.getDayOfWeek().equals(DayOfWeek.SUNDAY)) {
            } else {
                businessDays++;
            }
            currDate = currDate.plusDays(1);
        }
        return currDate;
    }

    @Override
    public Map<String, DispatchNotificationModel> markedOrderShippedDetail(List<Order> orders, Provider provider,
                                                                           Map<String, DispatchNotificationModel> dispatchNotication, String airwaybillNo, LocalDateTime shippingTimestamp)
            throws ProfitMandiBusinessException {

        for (Order order : orders) {
            if (order.getStatus().equals(OrderStatus.BILLED)) {
                if (order.getBillingTimestamp().isAfter(shippingTimestamp)) {
                    throw new ProfitMandiBusinessException("Uploaded File", "",
                            "Invoice Number " + order.getInvoiceNumber() + " cannot be shipped before billing date");
                }
                if (StringUtils.isEmpty(airwaybillNo)) {
                    throw new ProfitMandiBusinessException("Uploaded File", "",
                            "Invoice Number " + order.getInvoiceNumber() + " airwaybill_no is empty");
                }
                if (airwaybillNo.equals("Error")) {
                    break;
                }
                order.setLogisticsProviderId(provider.getId());
                order.setAirwayBillNumber(airwaybillNo);
                order.setStatus(OrderStatus.SHIPPED_FROM_WH);
                order.setStatusDescription("Order shipped from warehouses");

                ProviderTat pt = providerTatRepository.selectByProviderId(order.getLogisticsProviderId(),
                        order.getWarehouseId(), order.getRetailerPinCode());
                order.setShippingTimestamp(shippingTimestamp);

                LocalDate deliveryDate = this.calculateDeliveryTimeline(LocalDate.now(), pt, 0);
                order.setExpectedDeliveryTime(deliveryDate.atStartOfDay());

                if (dispatchNotication.get(airwaybillNo) != null) {
                    DispatchNotificationModel dnm = dispatchNotication.get(airwaybillNo);
                    List<String> invoiceNumbers = dnm.getInvoiceNumber();
                    invoiceNumbers.add(order.getInvoiceNumber());
                    dnm.setInvoiceNumber(invoiceNumbers);
                    Float totalAmount = dnm.getTotalAmount();
                    dnm.setTotalAmount(totalAmount + order.getTotalAmount());

                    int totalQty = dnm.getTotalQty();
                    dnm.setTotalQty(totalQty + order.getLineItem().getQuantity());

                    dispatchNotication.put(airwaybillNo, dnm);

                } else {
                    DispatchNotificationModel dnm = new DispatchNotificationModel();
                    List<String> invoiceNumber = new ArrayList<>();
                    invoiceNumber.add(order.getInvoiceNumber());
                    dnm.setInvoiceNumber(invoiceNumber);
                    dnm.setTotalAmount(order.getTotalAmount());
                    dnm.setTotalQty(order.getLineItem().getQuantity());
                    dnm.setFofoId(order.getRetailerId());
                    dnm.setProviderId(order.getLogisticsProviderId());
                    dnm.setDate(deliveryDate);
                    dnm.setShippingDate(shippingTimestamp.toLocalDate());
                    dispatchNotication.put(airwaybillNo, dnm);

                }

            }

        }
        return dispatchNotication;

    }

    @Autowired
    WarehouseProviderRepository warehouseProviderRepository;

    @Autowired
    WarehouseRiderRepository warehouseRiderRepository;

    @Autowired
    TransactionShipmentSequenceRepository transactionShipmentSequenceRepository;

    @Autowired
    TransactionRepository transactionRepository;

    @Autowired
    WarehousePartnerDistanceMappingRepository warehousePartnerDistanceMappingRepository;

    @Override
    public E_InvoiceCommon.ReqPlGenIRN.EwbDetails getEwbDetails(Order order) throws ProfitMandiBusinessException {

        E_InvoiceCommon.ReqPlGenIRN.EwbDetails ewbDetails = new E_InvoiceCommon.ReqPlGenIRN.EwbDetails();
        //ewbDetails.Distance = String.valueOf(200);
        //1 is road, 2 is rail, 3 is air, 4 is ship

        if (order.getLogisticsProviderId() == ProfitMandiConstants.LOGISTICS_PROVIDER_SELF_PICKUP
                || order.getLogisticsProviderId() == ProfitMandiConstants.LOGISTICS_PROVIDER_RUNNER) {
            TransactionShipmentSequence transactionShipmentSequence = transactionShipmentSequenceRepository.selectByTransactionShipmentSequence(order.getLogisticsTransactionId());
            if (transactionShipmentSequence != null) {
                ewbDetails.VehNo = transactionShipmentSequence.getVehicleNumber();
                ewbDetails.VehType = "R";
                ewbDetails.TransMode = "1";
                ewbDetails.TransDocDt = com.spice.profitmandi.common.util.StringUtils.toString(order.getBillingTimestamp().toLocalDate());
                ewbDetails.TransDocNo = order.getAirwayBillNumber();
            } else {
                LOGGER.warn("No shipment sequence found for logistics transaction: {}, order will get EWB Part A only", order.getLogisticsTransactionId());
            }
        } else {
            WarehouseProvider warehouseProvider = warehouseProviderRepository.selectByProviderWarehouse(order.getLogisticsProviderId(), order.getWarehouseId());
            Provider provider = providerRepository.selectById(order.getLogisticsProviderId());
            if (warehouseProvider != null && provider != null) {
                ewbDetails.TransId = warehouseProvider.getGstin();
                ewbDetails.TransName = provider.getName();
            } else {
                LOGGER.warn("No provider details for logistics provider: {}, warehouse: {}", order.getLogisticsProviderId(), order.getWarehouseId());
            }
        }

        String distance = "0";
        WarehousePartnerDistanceMapping distanceMapping = warehousePartnerDistanceMappingRepository.selectByWarehouseIdAndFofoId(order.getWarehouseId(), order.getRetailerId());
        if (distanceMapping != null) {
            distance = distanceMapping.getDistance();
        }
        LOGGER.info("distance - " + distance);
        ewbDetails.Distance = distance;
        return ewbDetails;

    }

}