Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.4 kB
2
Indexable
Never
package br.com.senior.erpxcomfat.saleinvoice.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

import javax.inject.Inject;

import org.lindberg.jpa.entitycloner.persistence.EntityCloner;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import br.com.senior.erpxcomfat.saleinvoice.E140nfsBatchEntity;
import br.com.senior.erpxcomfat.saleinvoice.E140nfsBatchInvoiceEntity;
import br.com.senior.erpxcomfat.saleinvoice.E140nfsEntity;
import br.com.senior.erpxcomfat.saleinvoice.E140obsEntity;
import br.com.senior.erpxcomfat.saleinvoice.EnumBatchProcessingType;
import br.com.senior.erpxcomfat.saleinvoice.EnumBatchStatus;
import br.com.senior.erpxcomfat.saleinvoice.EnumPrcNfs;
import br.com.senior.erpxcomfat.saleinvoice.EnumSitNfs;
import br.com.senior.erpxcomfat.saleinvoice.EnumTipNfs;
import br.com.senior.erpxcomfat.saleinvoice.dto.GenerateNumberInvoice;
import br.com.senior.erpxcomfat.saleinvoice.repository.E140nfsBatchRepository;
import br.com.senior.erpxcomfat.saleinvoice.repository.E140nfsRepository;
import br.com.senior.erpxcomfat.saleinvoice.repository.E140obsRepository;
import br.com.senior.erpxcomfat.saleinvoice.service.CloneSaleInvoiceService;
import br.com.senior.erpxcomfat.saleinvoice.service.FiscalDocumentIssuanceService;
import br.com.senior.erpxcomfat.saleinvoice.service.RecalculateInstallmentsService;
import br.com.senior.erpxcomfat.utils.DateUtils;
import br.com.senior.erpxcomfat.utils.NumberUtils;

@Service
public class CloneSaleInvoiceServiceImpl implements CloneSaleInvoiceService {
	
	private List<EnumTipNfs> typesInvoiceOfService = Arrays.asList(EnumTipNfs.V21, EnumTipNfs.V20); 

    @Inject
    private E140nfsRepository saleInvoiceRepository;
    @Inject
    private FiscalDocumentIssuanceService fiscalDocumentIssuanceService;    
    @Inject
    private RecalculateInstallmentsService recalculateInstallmentsService;
    @Inject
    private E140obsRepository observationRepository;
    @Inject
    private E140nfsBatchRepository batchRepository;
    
	@Override
    @Transactional
	public E140nfsEntity clone(E140nfsEntity saleInvoiceOrigin, Boolean copyData, EnumTipNfs type, EnumSitNfs situation, HashMap<Class, List<String>> fieldsToIgnore) {
		E140nfsEntity saleInvoice = this.cloneSaleInvoice(saleInvoiceOrigin, fieldsToIgnore);
		saleInvoice = this.setNewValuesAndUpdate(saleInvoice, type, situation);
		if (Boolean.TRUE.equals(copyData)) {
	        this.cloneObservations(saleInvoiceOrigin.getId(), saleInvoice);
		}
		if (EnumTipNfs.V21 == type) {
			this.recalculateInstallmentsService.recalculateInstallments(saleInvoice);
			saleInvoice.setFatCon(saleInvoiceOrigin.getId());
			if (EnumPrcNfs.V3 == saleInvoiceOrigin.getPrcNfs()) {
			    E140nfsBatchEntity e140nfsBatch = batchRepository.findBatchByInvoiceAndType(saleInvoiceOrigin.getId(), EnumBatchProcessingType.GERACAO_VIA_CONTRATO);
			    if (Objects.nonNull(e140nfsBatch)) {
			        cloneSaleAgreementBatch(e140nfsBatch.getIdeExt(), saleInvoice);
			    }
			}
		}
		this.generateNumNfsSaleInvoice(saleInvoice);
		return saleInvoiceRepository.saveAndFlush(saleInvoice);
	}
    
	private E140nfsEntity setNewValuesAndUpdate(E140nfsEntity saleInvoice, EnumTipNfs type, EnumSitNfs situation) {
		saleInvoice.setTipNfs(type);
		saleInvoice.setSitNfs(situation);
		saleInvoice.setDthEmi(DateUtils.getDateEmi());
		saleInvoice.setE140par(Collections.emptyList());
		return saleInvoiceRepository.saveAndFlush(saleInvoice);
	}

	private void cloneObservations(UUID saleInvoiceOriginId, E140nfsEntity saleInvoice) {
		List<E140obsEntity> observationsOrigin = observationRepository.findByE140nfs_Id(saleInvoiceOriginId);
		if (Objects.nonNull(observationsOrigin)) {
			List<E140obsEntity> observations = observationsOrigin.parallelStream().map(observationOrigin -> cloneObservation(observationOrigin, saleInvoice)).collect(Collectors.toList());
			observationRepository.saveAll(observations);
		}
	}
	
    public E140nfsBatchEntity cloneSaleAgreementBatch(String externalId, E140nfsEntity clonedInvoice) {
        var batch = new E140nfsBatchEntity();
        batch.setIdeExt(externalId);
        batch.setTipBat(EnumBatchProcessingType.GERACAO_VIA_CONTRATO);
        batch.setSitBat(EnumBatchStatus.SUCESSO);
        batch.setCodBat(getLastBatchCode());
        var e140nfsBatchInvoiceEntity = new E140nfsBatchInvoiceEntity();
        e140nfsBatchInvoiceEntity.setE140nfs(clonedInvoice);
        e140nfsBatchInvoiceEntity.setE140nfsBatch(batch);
        batch.setE140nfsBatchInvoice(Arrays.asList(e140nfsBatchInvoiceEntity));
        return batchRepository.saveAndFlush(batch);
    }

    public Long getLastBatchCode() {
        return batchRepository.lastBatchCode();
    }

	private E140obsEntity cloneObservation(E140obsEntity observation, E140nfsEntity saleInvoice) {
		HashMap<Class, List<String>> fieldsToIgnore = new HashMap<>();
		fieldsToIgnore.put(E140obsEntity.class, Arrays.asList("id"));

		EntityCloner<E140obsEntity> observationCloner = new EntityCloner<>(observation, fieldsToIgnore);
		E140obsEntity observationClone = observationCloner.generateClone();
		observationClone.setE140nfs(saleInvoice);
		return observationClone;
	}
	
	private E140nfsEntity cloneSaleInvoice(E140nfsEntity saleInvoiceOrigin, HashMap<Class, List<String>> fieldsToIgnore) {
		EntityCloner<E140nfsEntity> saleInvoiceCloner = new EntityCloner<>(saleInvoiceOrigin, fieldsToIgnore);
		return saleInvoiceCloner.generateClone();
	}
	

	private void generateNumNfsSaleInvoice(E140nfsEntity saleInvoice) {
        if (NumberUtils.convertNull(saleInvoice.getNumNfs()) == NumberUtils.ZERO) {
            GenerateNumberInvoice response = fiscalDocumentIssuanceService.generateNumberSaleInvoice(saleInvoice.getE070fil(), saleInvoice.getE020snf(), saleInvoice.getDthEmi());
            saleInvoice.setDthEmi(response.getIssueDate());
            if (typesInvoiceOfService.contains(saleInvoice.getTipNfs())) {
            	saleInvoice.setNumRps(response.getIssueNumber());
            } else {
            	saleInvoice.setNumNfs(response.getIssueNumber());            	
            }
        }
    }	
 		
}