Advertisement
samuel025

BT Create SQ Revision

Jul 19th, 2021
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.98 KB | None | 0 0
  1. package org.jleaf.erp.sls.bo.salesquotation;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.jleaf.core.BusinessFunction;
  7. import org.jleaf.core.BusinessTransaction;
  8. import org.jleaf.core.CoreException;
  9. import org.jleaf.core.DefaultBusinessTransaction;
  10. import org.jleaf.core.Dto;
  11. import org.jleaf.core.GeneralConstants;
  12. import org.jleaf.core.annotation.Info;
  13. import org.jleaf.core.annotation.InfoIn;
  14. import org.jleaf.core.annotation.InfoOut;
  15. import org.jleaf.erp.sls.SalesConstants;
  16. import org.jleaf.erp.sls.SalesExceptionConstants;
  17. import org.jleaf.erp.sls.dao.SalesQuotationDao;
  18. import org.jleaf.erp.sls.dao.SalesQuotationItemDao;
  19. import org.jleaf.erp.sls.dao.SalesQuotationTcDao;
  20. import org.jleaf.erp.sls.entity.SalesQuotation;
  21. import org.jleaf.erp.sls.entity.SalesQuotationItem;
  22. import org.jleaf.erp.sls.entity.SalesQuotationTc;
  23. import org.jleaf.erp.sls.entity.BalanceQuotation;
  24. import org.jleaf.erp.sls.dao.BalanceQuotationDao;
  25. import org.jleaf.util.DtoUtil;
  26. import org.jleaf.util.GsonUtil;
  27. import org.jleaf.util.ValidationUtil;
  28. import org.springframework.beans.factory.annotation.Autowired;
  29. import org.springframework.beans.factory.annotation.Qualifier;
  30. import org.springframework.stereotype.Service;
  31. import org.slf4j.Logger;
  32. import org.slf4j.LoggerFactory;
  33.  
  34.  
  35. /**
  36.  *
  37.  * @author Widana, Sep 15, 2017
  38.  *
  39.  **/
  40.  
  41. @Service
  42. @InfoIn(value = {
  43.         @Info(name = "tenantLoginId", description = "tenant login id", type = Long.class),
  44.         @Info(name = "userLoginId", description = "User Login Id", type = Long.class),
  45.         @Info(name = "roleLoginId", description = "roleLoginId", type = Long.class),
  46.         @Info(name = "datetime", description = "datetime", type = String.class),
  47.        
  48.         @Info(name = "docNo", description = "doc No", type = String.class),
  49.         @Info(name = "docDate", description = "doc Date", type = String.class),
  50.         @Info(name = "ouId", description = "ou Id", type = Long.class),
  51.         @Info(name = "quotationId", description = "ou Id", type = Long.class),
  52.         @Info(name = "remark", description = "remark", type = String.class)
  53. })
  54. @InfoOut(value = {
  55.         @Info(name = "id", description = "quotation Id", type = Long.class)
  56. })
  57. public class AddSalesQuotationFromRevision extends DefaultBusinessTransaction implements BusinessTransaction {
  58.  
  59.     private static final Logger log = LoggerFactory.getLogger(AddSalesQuotationFromRevision.class);
  60.    
  61.     @Autowired
  62.     SalesQuotationDao salesQuotationDao;
  63.    
  64.     @Autowired
  65.     SalesQuotationItemDao salesQuotationItemDao;
  66.    
  67.     @Autowired
  68.     SalesQuotationTcDao salesQuotationTcDao;
  69.    
  70.     @Autowired
  71.     BalanceQuotationDao balanceQuotationDao;
  72.    
  73.     @Autowired
  74.     @Qualifier("findSalesQuotationById")
  75.     BusinessFunction findSalesQuotationById;
  76.    
  77.     @Autowired
  78.     @Qualifier("findSalesQuotationItemById")
  79.     BusinessFunction findSalesQuotationItemById;
  80.    
  81.     @Autowired
  82.     @Qualifier("findSalesQuotationTcById")
  83.     BusinessFunction findSalesQuotationTcById;
  84.    
  85.     @Autowired
  86.     @Qualifier("valTenantLoginCanUse")
  87.     BusinessFunction valTenantLoginCanUse;
  88.    
  89.     @Autowired
  90.     @Qualifier("findOUById")
  91.     BusinessFunction findOUById;
  92.    
  93.     @Autowired
  94.     @Qualifier("valOUAsMainBusinessUnitOrBranch")
  95.     BusinessFunction valOUAsMainBusinessUnitOrBranch;
  96.  
  97.     @Autowired
  98.     @Qualifier("findPartnerById")
  99.     BusinessFunction findPartnerById;
  100.  
  101.     @Autowired
  102.     @Qualifier("findPartnerCpById")
  103.     BusinessFunction findPartnerCpById;
  104.  
  105.     @Autowired
  106.     @Qualifier("findPartnerAddressById")
  107.     BusinessFunction findPartnerAddressById;
  108.  
  109.     @Autowired
  110.     @Qualifier("getSalesQuotationItemListBySalesQuotation")
  111.     BusinessFunction getSalesQuotationItemListBySalesQuotation;
  112.    
  113.     @Autowired
  114.     @Qualifier("getTermsAndConditionSalesQuotation")
  115.     BusinessFunction getTermsAndConditionSalesQuotation;
  116.    
  117.     @Autowired
  118.     @Qualifier("findBalanceQuotationById")
  119.     BusinessFunction findBalanceQuotationById;
  120.    
  121.     @Override
  122.     public String getDescription() {
  123.         return "Add Sales Quotation From Revision";
  124.     }
  125.  
  126.     @SuppressWarnings("unchecked")
  127.     @Override
  128.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  129.         log.info("prepare inputDto addSalesQuotationFromRevision " + inputDto.toString());
  130.        
  131.         this.validationUtil(inputDto);
  132.  
  133.         Long tenantId = inputDto.getLong("tenantLoginId");
  134.         Long userId = inputDto.getLong("userLoginId");
  135.         Long roleId = inputDto.getLong("roleLoginId");
  136.         Long quotationId = inputDto.getLong("quotationId");
  137.         Long ouId = inputDto.getLong("ouId");
  138.         String datetime = inputDto.getString("datetime");
  139.         String docNo = inputDto.getString("docNo");
  140.         String docDate = inputDto.getString("docDate");
  141.         String remark = inputDto.getString("remark");
  142.        
  143.         Dto loginDto = new Dto();
  144.         loginDto.put("tenantLoginId", tenantId);
  145.         loginDto.put("userLoginId", userId);
  146.         loginDto.put("roleLoginId", roleId);
  147.         valTenantLoginCanUse.execute(loginDto);
  148.  
  149.         Dto ouAsMainBusinessOrBranchDto = new Dto();
  150.         ouAsMainBusinessOrBranchDto.put("id", ouId);
  151.         valOUAsMainBusinessUnitOrBranch.execute(ouAsMainBusinessOrBranchDto);
  152.  
  153.         Dto ouDto = new Dto();
  154.         ouDto.put("id", ouId);
  155.         findOUById.execute(ouDto);
  156.        
  157.         Dto salesQuotationDto = new Dto();
  158.         salesQuotationDto.put("id", quotationId);
  159.         Dto resultSalesQuotation = findSalesQuotationById.execute(salesQuotationDto);
  160.        
  161.         if(!resultSalesQuotation.getLong("partnerId").equals(GeneralConstants.NULL_REF_VALUE_LONG)){
  162.             Dto partnerDto = new Dto();
  163.             partnerDto.put("id", resultSalesQuotation.getLong("partnerId"));
  164.             findPartnerById.execute(partnerDto);
  165.         }
  166.  
  167.         if(!resultSalesQuotation.getLong("partnerCpId").equals(GeneralConstants.NULL_REF_VALUE_LONG)){
  168.             Dto partnerCpDto = new Dto();
  169.             partnerCpDto.put("id", resultSalesQuotation.getLong("partnerCpId"));
  170.             findPartnerCpById.execute(partnerCpDto);
  171.         }
  172.  
  173.         if(!resultSalesQuotation.getLong("partnerAddressId").equals(GeneralConstants.NULL_REF_VALUE_LONG)){
  174.             Dto partnerAddressDto = new Dto();
  175.             partnerAddressDto.put("id", resultSalesQuotation.getLong("partnerAddressId"));
  176.             findPartnerAddressById.execute(partnerAddressDto);
  177.         }
  178.        
  179.        
  180.         Dto balanceQuotationDto = new Dto();
  181.         balanceQuotationDto.put("id", quotationId);
  182.         Dto resultBalanceQuotation = findBalanceQuotationById.execute(balanceQuotationDto);
  183.        
  184.         if(resultBalanceQuotation.getString("statusDoc").equals(SalesConstants.IN_PROGRESS_TRANSACTION)) {
  185.             throw new CoreException(SalesExceptionConstants.SALES_QUOTATION_ALREADY_IN_USE_OTHER_TRANSACTION);
  186.         }
  187.        
  188.         if(resultBalanceQuotation.getString("flgRevision").equals(GeneralConstants.YES)) {
  189.             throw new CoreException(SalesExceptionConstants.SALES_QUOTATION_ALREADY_REVISION_WITH_DOCUMENT_NO, resultSalesQuotation.getString("docNo"));
  190.         }
  191.         resultBalanceQuotation.put("statusDoc", SalesConstants.IN_PROGRESS_TRANSACTION);
  192.         this.prepareUpdateAudit(resultBalanceQuotation, userId, datetime);
  193.        
  194.         Dto salesQuotationItemDto = new Dto();
  195.         salesQuotationItemDto.put("tenantId", tenantId);
  196.         salesQuotationItemDto.put("quotationId", quotationId);
  197.         Dto salesQuotationItemListDto = getSalesQuotationItemListBySalesQuotation.execute(salesQuotationItemDto);
  198.        
  199.         List<Dto> itemSalesQuotationList = salesQuotationItemListDto.getList("salesQuotationItemList");
  200.         List<Dto> inputSalesQuotationItemDto = new ArrayList<Dto>();
  201.         List<Dto> failList = new ArrayList<Dto>();
  202.        
  203.         if(itemSalesQuotationList.size() == 0 || itemSalesQuotationList == null) {
  204.             throw new CoreException(SalesExceptionConstants.SALES_ORDER_MUST_HAVE_AT_LEAST_ONE_DETAIL_ITEM);
  205.         }
  206.        
  207.         Long lineNo = 0L;
  208.         for(Dto itemDto : itemSalesQuotationList) {
  209.             lineNo++;
  210.             try {
  211.  
  212.                 Dto quotationItemDto = new Dto();
  213.                 quotationItemDto.put("id", Long.valueOf(itemDto.get("quotationItemId").toString()));
  214.                 Dto resultSalesQuotationItem =  findSalesQuotationItemById.execute(quotationItemDto);
  215.                 log.debug("isiitemSQRev : "+resultSalesQuotationItem);
  216.  
  217.                 if(resultSalesQuotationItem.get("flgStock").equals(GeneralConstants.NO)) {
  218.                     if(resultSalesQuotationItem.get("indentDays").equals("0")){
  219.                         throw new CoreException(SalesExceptionConstants.INDENT_DAYS_MUST_BE_GREATER_THAN_ZERRO);
  220.                     }
  221.                 }
  222.                
  223.                 Dto inputItemDto = new Dto();
  224.                 inputItemDto.put("tenantId", tenantId);
  225.                 inputItemDto.put("lineNo", lineNo);
  226.                 inputItemDto.put("refId", resultSalesQuotationItem.getLong("id"));
  227.                 inputItemDto.put("refDocTypeId", SalesConstants.DOCUMENT_SALES_QUOTATION);
  228.                 inputItemDto.put("productId", resultSalesQuotationItem.getLong("productId"));
  229.                 inputItemDto.put("productCode", resultSalesQuotationItem.getString("productCode"));
  230.                 inputItemDto.put("productDesc", resultSalesQuotationItem.getString("productDesc"));
  231.                 inputItemDto.put("qty", resultSalesQuotationItem.getDouble("qty"));
  232.                 inputItemDto.put("uomId", resultSalesQuotationItem.getLong("uomId"));
  233.                 inputItemDto.put("sellPrice", resultSalesQuotationItem.getDouble("sellPrice"));
  234.                 inputItemDto.put("amount", resultSalesQuotationItem.getDouble("amount"));
  235.                 inputItemDto.put("brandCode", resultSalesQuotationItem.getString("brandCode"));
  236.                 inputItemDto.put("brandName", resultSalesQuotationItem.getString("brandName"));
  237.                 inputItemDto.put("remark", resultSalesQuotationItem.getString("remark"));
  238.                 inputItemDto.put("flgStock", resultSalesQuotationItem.getString("flgStock"));
  239.                 this.prepareInsertAudit(inputItemDto, userId, datetime);
  240.                 this.prepareUpdateAudit(inputItemDto, userId, datetime);
  241.                 log.debug("resultSalesQuotationItemIsi >>> : "+resultSalesQuotationItem);
  242.  
  243.  
  244.                 inputSalesQuotationItemDto.add(inputItemDto);
  245.                
  246.             } catch(CoreException e) {
  247.                 Dto errorDto = new Dto();
  248.            
  249.                 errorDto.put("productCode", itemDto.getString("productCode"));
  250.                 errorDto.put("productName", itemDto.getString("productDesc"));
  251.                 errorDto.put("flgStock", itemDto.getString("flgStock"));
  252.                 errorDto.put("indentDays", itemDto.getString("indentDays"));
  253.                 errorDto.put("qty", Double.valueOf(itemDto.getString("qty").toString()));
  254.                 errorDto.put("sellPrice", Double.valueOf(itemDto.getString("sellPrice").toString()));
  255.                 errorDto.put("amount", Double.valueOf(itemDto.getString("amount").toString()));
  256.                 errorDto.put("errorKey", e.getErrorKey());
  257.  
  258.                 List<Object> argsList = new ArrayList<Object>();
  259.                 if (e.getParamValues() != null && e.getParamValues().length > 0)
  260.                     for (Object paramValue : e.getParamValues()) {
  261.                         argsList.add(paramValue);
  262.                     }
  263.                 errorDto.put("args", argsList);
  264.  
  265.                 failList.add(errorDto);
  266.                
  267.             }
  268.         }
  269.        
  270.         Dto salesQuotationTcDto = new Dto();
  271.         salesQuotationTcDto.put("tenantId", tenantId);
  272.         salesQuotationTcDto.put("quotationId", quotationId);
  273.         Dto salesQuotationTcItemListDto = getTermsAndConditionSalesQuotation.execute(salesQuotationTcDto);
  274.        
  275.         List<Dto> itemSalesQuotationDto = salesQuotationTcItemListDto.getList("termsAndConditionList");
  276.         List<Dto> inputSalesQuotationTcDto = new ArrayList<Dto>();
  277.        
  278.         if(itemSalesQuotationDto.size() > 0) {
  279.             Long lineNoTc = 0L;
  280.             for(Dto itemDto : itemSalesQuotationDto) {
  281.                 lineNoTc++;
  282.                
  283.                 Dto inputItemDto = new Dto();
  284.                 inputItemDto.put("tenantId", tenantId);
  285.                 inputItemDto.put("lineNo", lineNoTc);
  286.                 inputItemDto.put("termCondition", itemDto.getString("termCondition"));
  287.                 this.prepareInsertAudit(inputItemDto, userId, datetime);
  288.                 this.prepareUpdateAudit(inputItemDto, userId, datetime);
  289.                
  290.                 inputSalesQuotationTcDto.add(inputItemDto);
  291.             }
  292.         }
  293.        
  294.         if (!failList.isEmpty()) {
  295.             Dto errorParamDto = new Dto();
  296.             DtoUtil.putInErrorParamDto(errorParamDto, "failList",
  297.                     new String[] { "productCode", "productName", "flgStock", "indentDays", "qty", "sellPrice", "amount", "errorKey", "args" }, failList);
  298.             throw new CoreException(SalesExceptionConstants.SALES_QUOTATION_ITEMS_ERROR, errorParamDto);
  299.         }
  300.        
  301.         Dto inputSalesQuotationDto = new Dto();
  302.         inputSalesQuotationDto.put("tenantId", tenantId);
  303.         inputSalesQuotationDto.put("ouId", ouId);
  304.         inputSalesQuotationDto.put("docTypeId", SalesConstants.DOCUMENT_SALES_QUOTATION);
  305.         inputSalesQuotationDto.put("docNo", docNo);
  306.         inputSalesQuotationDto.put("docDate", docDate);
  307.         inputSalesQuotationDto.put("extDocNo", resultSalesQuotation.getString("extDocNo"));
  308.         inputSalesQuotationDto.put("extDocDate", resultSalesQuotation.getString("extDocDate"));
  309.         inputSalesQuotationDto.put("refDocTypeId", SalesConstants.DOCUMENT_SALES_QUOTATION);
  310.         inputSalesQuotationDto.put("refId", quotationId);
  311.         inputSalesQuotationDto.put("partnerId", resultSalesQuotation.getLong("partnerId"));
  312.         inputSalesQuotationDto.put("partnerAddressId", resultSalesQuotation.getLong("partnerAddressId"));
  313.         inputSalesQuotationDto.put("partnerName", resultSalesQuotation.getString("partnerName"));
  314.         inputSalesQuotationDto.put("partnerAddress1", resultSalesQuotation.getString("partnerAddress1"));
  315.         inputSalesQuotationDto.put("partnerAddress2", resultSalesQuotation.getString("partnerAddress2"));
  316.         inputSalesQuotationDto.put("partnerAddress3", resultSalesQuotation.getString("partnerAddress3"));
  317.         inputSalesQuotationDto.put("partnerCpId", resultSalesQuotation.getLong("partnerCpId"));
  318.         inputSalesQuotationDto.put("partnerCpName", resultSalesQuotation.getString("partnerCpName"));
  319.         inputSalesQuotationDto.put("partnerCpPhone1", resultSalesQuotation.getString("partnerCpPhone1"));
  320.         inputSalesQuotationDto.put("partnerCpPhone2", resultSalesQuotation.getString("partnerCpPhone2"));
  321.         inputSalesQuotationDto.put("partnerCpFax1", resultSalesQuotation.getString("partnerCpFax1"));
  322.         inputSalesQuotationDto.put("partnerCpFax2", resultSalesQuotation.getString("partnerCpFax2"));
  323.         inputSalesQuotationDto.put("partnerCpEmail", resultSalesQuotation.getString("partnerCpEmail"));
  324.         inputSalesQuotationDto.put("partnerCpMobilePhone1", resultSalesQuotation.getString("partnerCpMobilePhone1"));
  325.         inputSalesQuotationDto.put("partnerCpMobilePhone2", resultSalesQuotation.getString("partnerCpMobilePhone2"));
  326.         inputSalesQuotationDto.put("salesmanId", resultSalesQuotation.getLong("salesmanId"));
  327.         inputSalesQuotationDto.put("currCode", resultSalesQuotation.getString("currCode"));
  328.         inputSalesQuotationDto.put("flgTaxAmount", resultSalesQuotation.getString("flgTaxAmount"));
  329.         inputSalesQuotationDto.put("taxId", resultSalesQuotation.getString("taxId"));
  330.         inputSalesQuotationDto.put("remark", remark);
  331.         inputSalesQuotationDto.put("statusDoc", SalesConstants.DRAFT_TRANSACTION);
  332.         inputSalesQuotationDto.put("workflowStatus", SalesConstants.WORKFLOW_STATUS_DRAFT);
  333.         this.prepareInsertAudit(inputSalesQuotationDto, userId, datetime);
  334.         this.prepareUpdateAudit(inputSalesQuotationDto, userId, datetime);
  335.        
  336.         inputDto.put("inputSalesQuotationDto", inputSalesQuotationDto);
  337.         inputDto.put("inputSalesQuotationItemDto", inputSalesQuotationItemDto);
  338.         inputDto.put("inputSalesQuotationTcDto", inputSalesQuotationTcDto);
  339.         inputDto.put("balanceQuotationDto", resultBalanceQuotation);
  340.        
  341.         return null;
  342.     }
  343.  
  344.     @SuppressWarnings("unchecked")
  345.     @Override
  346.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  347.         Dto inputSalesQuotationDto = inputDto.getDto("inputSalesQuotationDto");
  348.         SalesQuotation salesQuotation = GsonUtil.fromDto(inputSalesQuotationDto, SalesQuotation.class);
  349.         salesQuotationDao.persist(salesQuotation);
  350.        
  351.         List<Dto> inputSalesQuotationItemDto = inputDto.getList("inputSalesQuotationItemDto");
  352.         for (Dto salesQuotationItemDto : inputSalesQuotationItemDto) {
  353.             salesQuotationItemDto.put("quotationId", salesQuotation.getId());
  354.             SalesQuotationItem salesQuotationItem = GsonUtil.fromDto(salesQuotationItemDto, SalesQuotationItem.class);
  355.             salesQuotationItemDao.persist(salesQuotationItem);
  356.         }
  357.        
  358.         List<Dto> inputSalesQuotationTcDto = inputDto.getList("inputSalesQuotationTcDto");
  359.         if(inputSalesQuotationTcDto.size() != 0) {
  360.             for(Dto salesQuotationTcDto : inputSalesQuotationTcDto) {
  361.                 salesQuotationTcDto.put("quotationId", salesQuotation.getId());
  362.                 SalesQuotationTc salesQuotationTc = GsonUtil.fromDto(salesQuotationTcDto, SalesQuotationTc.class);
  363.                 salesQuotationTcDao.persist(salesQuotationTc);
  364.             }
  365.         }
  366.        
  367.         Dto balanceQuotationDto = inputDto.getDto("balanceQuotationDto");
  368.         BalanceQuotation balanceQuotation = GsonUtil.fromDto(balanceQuotationDto, BalanceQuotation.class);
  369.         balanceQuotationDao.merge(balanceQuotation.getId(), balanceQuotation);
  370.        
  371.         return new Dto().put("id", salesQuotation.getId());
  372.     }
  373.    
  374.     public void validationUtil(Dto inputDto) {
  375.    
  376.         ValidationUtil.valDtoContainsKey(inputDto, "tenantLoginId");
  377.         ValidationUtil.valDtoContainsKey(inputDto, "roleLoginId");
  378.         ValidationUtil.valDtoContainsKey(inputDto, "userLoginId");
  379.         ValidationUtil.valDtoContainsKey(inputDto, "datetime");
  380.         ValidationUtil.valDtoContainsKey(inputDto, "docNo");
  381.         ValidationUtil.valDtoContainsKey(inputDto, "docDate");
  382.         ValidationUtil.valDtoContainsKey(inputDto, "ouId");
  383.         ValidationUtil.valDtoContainsKey(inputDto, "quotationId");
  384.         ValidationUtil.valDtoContainsKey(inputDto, "remark");
  385.        
  386.         ValidationUtil.valBlankOrNull(inputDto, "tenantLoginId");
  387.         ValidationUtil.valBlankOrNull(inputDto, "roleLoginId");
  388.         ValidationUtil.valBlankOrNull(inputDto, "userLoginId");
  389.         ValidationUtil.valBlankOrNull(inputDto, "ouId");
  390.         ValidationUtil.valBlankOrNull(inputDto, "quotationId");
  391.        
  392.     }
  393.    
  394.  
  395. }
  396.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement