Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.0 kB
2
Indexable
Never
package br.com.senior.erpxcomven.pedido.converter;

import static br.com.senior.erpxcomven.utils.ConvertUtils.convertNull;

import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import br.com.senior.erpxcomven.foundation.E001endEntity;
import br.com.senior.erpxcomven.foundation.E028cpgEntity;
import br.com.senior.erpxcomven.foundation.E070empEntity;
import br.com.senior.erpxcomven.foundation.E070filEntity;
import br.com.senior.erpxcomven.foundation.E073pesEntity;
import br.com.senior.erpxcomven.foundation.E085pesEntity;
import br.com.senior.erpxcomven.pedido.E120imdEntity;
import br.com.senior.erpxcomven.pedido.E120infEntity;
import br.com.senior.erpxcomven.pedido.E120pedEntity;
import br.com.senior.erpxcomven.pedido.E120pedIntegrationEntity;
import br.com.senior.erpxcomven.pedido.EnumSaleIntegrationStatus;
import br.com.senior.erpxcomven.pedido.EnumSitPed;
import br.com.senior.erpxcomven.pedido.Order;
import br.com.senior.erpxcomven.pedido.OrderStatus;
import br.com.senior.erpxcomven.pedido.converter.impl.ConvertBranch;
import br.com.senior.erpxcomven.pedido.converter.impl.ConvertCompany;
import br.com.senior.erpxcomven.pedido.converter.impl.ConvertCustomer;
import br.com.senior.erpxcomven.pedido.converter.impl.ConvertItem;
import br.com.senior.erpxcomven.pedido.converter.impl.ConvertTax;
import br.com.senior.erpxcomven.pedido.converter.impl.ShippingAddressConverter;
import br.com.senior.erpxcomven.pedido.converter.impl.TermPaymentConverter;
import br.com.senior.erpxcomven.pedido.converter.impl.TransporterConverter;
import br.com.senior.erpxcomven.pedido.dto.OrderProjection;
import br.com.senior.erpxcomven.pedido.repository.E120imdBaseImpostoRepository;
import br.com.senior.erpxcomven.pedido.repository.E120pedIntegrationRepository;

@Component
public class OrderConverter {
    
    @Inject
    private ConvertBranch branchConverter;
    @Inject
    private ConvertCompany companyConverter;
    @Inject
    private ConvertCustomer customerConverter;
    @Inject
    private TransporterConverter transporterConverter;
    @Inject
    private ShippingAddressConverter shippingAddressConverter;
    @Inject
    private TermPaymentConverter termPaymentConverter;
    @Inject
    private ConvertItem convertItem;
    @Inject
    private ConvertTax convertTax;
    @Inject
    private E120imdBaseImpostoRepository e120imdBaseImpostoRepository;
    @Inject
    private E120pedIntegrationRepository e120pedIntegrationRepository;

    public Order convertOrder(OrderProjection orderProjection) {
        E120pedEntity e120ped = orderProjection.getE120ped();
        E070empEntity e070emp = orderProjection.getE070emp();
        E070filEntity e070fil = orderProjection.getE070fil();
        E120infEntity e120inf = orderProjection.getE120inf();
        E001endEntity e001end = orderProjection.getE001end();
        E073pesEntity e073pes = orderProjection.getE073pes();
        E085pesEntity e085pes = orderProjection.getE085pes();
        E028cpgEntity e028cpg = orderProjection.getE028cpg();

        var order = new Order();
        order.id = e120ped.getId().toString();
        order.number = e120ped.getNumPed();
        order.issueDate = e120ped.getDthEmi();
        order.discount = convertNull(e120ped.getVlrDpr());
        order.totalPaid = convertNull(e120ped.getVlrLiq());
        
        if (Objects.nonNull(e120inf)) {
            order.expectedDeliveryDate = e120inf.getDatEnt();
            order.shippingCost = convertNull(e120inf.getVlrFre());
            order.observation = e120inf.getObsPed();
        }
        
        order.customer = customerConverter.convert(Optional.ofNullable(e085pes).map(E085pesEntity::getE001pes).orElse(null));
        order.branch = branchConverter.convert(e070fil);
        order.company = companyConverter.convert(e070emp);
        order.shippingCompany = transporterConverter.convert(Optional.ofNullable(e073pes).map(E073pesEntity::getE001pes).orElse(null));
        order.shippingAddress = shippingAddressConverter.convert(e001end);
        order.paymentTerms = termPaymentConverter.convert(e028cpg);
        
        order.itens = e120ped.getE120ipd().stream().map(convertItem::convert).collect(Collectors.toList());
        List<E120imdEntity> taxes = e120imdBaseImpostoRepository.findAllByE120ped(e120ped.getId());
        if (CollectionUtils.isNotEmpty(taxes)) {
            order.taxes = taxes.stream().map(convertTax::convert).collect(Collectors.toList());
        }
        E120pedIntegrationEntity errorIntegration = e120pedIntegrationRepository.findFirstByE120pedAndSitInt(e120ped, EnumSaleIntegrationStatus.INTEGRACAO_FINALIZADA_ERRO);
        order.status = getStatus(e120ped, errorIntegration);
        order.errorMessage = errorIntegration.getMsgInt();
        return order;
    }
    
    private static OrderStatus getStatus(E120pedEntity e120ped, E120pedIntegrationEntity errorIntegration) {
        OrderStatus status = OrderStatus.IN_PROGRESS;
        if(EnumSitPed.V4 == e120ped.getSitPed()) {
            status = OrderStatus.INVOICED;
        } else if (EnumSitPed.V5 == e120ped.getSitPed()) {
            status = OrderStatus.CANCELED;
        } else if (EnumSet.of(EnumSitPed.V1, EnumSitPed.V2).contains(e120ped.getSitPed()) && Boolean.TRUE.equals(e120ped.getPedBlo())) {
            status = OrderStatus.NOT_PAID;
        } else if (EnumSet.of(EnumSitPed.V1, EnumSitPed.V2).contains(e120ped.getSitPed()) && Boolean.FALSE.equals(e120ped.getPedBlo())) {
            status = OrderStatus.PAID;
        } else {
            if (Objects.nonNull(errorIntegration)) {
                status = OrderStatus.ERROR;
            }
        }
        return status;        
    }
    
}