abirama62

temp modif BT Submit SO

Mar 8th, 2021 (edited)
221
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.jleaf.erp.sls.bo.salesorder;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.UUID;
  6.  
  7. import org.jleaf.core.*;
  8. import org.jleaf.core.dao.QueryBuilder;
  9. import org.jleaf.erp.sls.SalesConstants;
  10. import org.jleaf.erp.sls.SalesConstantsForSasa;
  11. import org.jleaf.erp.sls.SalesExceptionConstants;
  12. import org.jleaf.erp.sls.SalesExceptionConstantsForSasa;
  13. import org.jleaf.erp.sls.dao.SalesOrderCustomDao;
  14. import org.jleaf.erp.sls.dao.SalesOrderDao;
  15. import org.jleaf.erp.sls.dao.SalesOrderItemCustomDao;
  16. import org.jleaf.erp.sls.dao.SalesOrderItemDao;
  17. import org.jleaf.erp.inv.dao.ProductBalanceStockReservedDao;
  18. import org.jleaf.erp.inv.dao.LogProductBalanceStockReservedDao;
  19. import org.jleaf.erp.sls.entity.SalesOrder;
  20. import org.jleaf.erp.sls.entity.SalesOrderCustom;
  21. import org.jleaf.erp.sls.entity.SalesOrderItem;
  22. import org.jleaf.erp.sls.entity.SalesOrderItemCustom;
  23. import org.jleaf.erp.inv.entity.ProductBalanceStockReserved;
  24. import org.jleaf.erp.inv.entity.LogProductBalanceStockReserved;
  25. import org.jleaf.common.CommonExceptionConstants;
  26. import org.jleaf.core.annotation.ErrorList;
  27. import org.jleaf.core.annotation.Info;
  28. import org.jleaf.core.annotation.InfoIn;
  29. import org.jleaf.core.annotation.InfoOut;
  30. import org.jleaf.util.DateUtil;
  31. import org.jleaf.util.DtoUtil;
  32. import org.jleaf.util.GsonUtil;
  33. import org.jleaf.validator.CommonBusinessValidator;
  34. import org.slf4j.Logger;
  35. import org.slf4j.LoggerFactory;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.beans.factory.annotation.Qualifier;
  38. import org.springframework.stereotype.Service;
  39.  
  40. import javax.persistence.NoResultException;
  41. import javax.persistence.Query;
  42.  
  43. /**
  44.  *
  45.  * @author Ivan Dec 05, 2019
  46.  *
  47.  */
  48. //@formatter:off
  49. @Service("submitSalesOrder")
  50. @InfoIn(value = {
  51.         @Info(name = "id", description = "sales order id", type = Long.class),
  52.         @Info(name = "version", description = "sales order version", type = Long.class),
  53.         @Info(name = "workflowStatus", description = "Workflow Status", type = String.class),
  54.         @Info(name = "tenantLoginId", description = "tenant login id", type = Long.class),
  55.         @Info(name = "userLoginId", description = "user login id", type = Long.class),
  56.         @Info(name = "roleLoginId", description = "role login id", type = Long.class),
  57.         @Info(name = "datetime", description = "datetime", type = String.class)
  58. })
  59. @InfoOut(value = {
  60.         @Info(name = "id", description = "so id", type = Long.class)
  61. })
  62. @ErrorList(errorKeys = {
  63.         CommonExceptionConstants.TENANT_LOGIN_CANT_USED,
  64.         CommonExceptionConstants.DATA_CANT_CHANGE_CAUSE_TENANT,
  65.         SalesExceptionConstants.SALES_ORDER_ID_NOT_FOUND,
  66.         SalesExceptionConstants.SALES_ORDER_MUST_HAVE_AT_LEAST_ONE_DETAIL_ITEM
  67. })
  68. //@formatter:on
  69. public class SubmitSalesOrderForSasa extends DefaultBusinessTransaction implements BusinessTransaction {
  70.  
  71.     private static final Logger log = LoggerFactory.getLogger(SubmitSalesOrderForSasa.class);
  72.    
  73.     @Autowired
  74.     SalesOrderDao salesOrderDao;
  75.  
  76.     @Autowired
  77.     SalesOrderCustomDao salesOrderCustomDao;
  78.  
  79.     @Autowired
  80.     ProductBalanceStockReservedDao productBalanceStockReservedDao;
  81.  
  82.     @Autowired
  83.     LogProductBalanceStockReservedDao logProductBalanceStockReservedDao;
  84.  
  85.     @Autowired
  86.     SalesOrderItemDao salesOrderItemDao;
  87.    
  88.     @Autowired
  89.     SalesOrderItemCustomDao salesOrderItemCustomDao;
  90.  
  91.     @Autowired
  92.     @Qualifier("findSalesOrderById")
  93.     BusinessFunction findSalesOrderById;
  94.    
  95.     @Autowired
  96.     @Qualifier("valTenantLoginCanUse")
  97.     BusinessFunction valTenantLoginCanUse;
  98.  
  99.     @Autowired
  100.     @Qualifier("getSalesOrderItemListBySalesOrder")
  101.     BusinessFunction getSalesOrderItemListBySalesOrder;
  102.    
  103.     @Autowired
  104.     @Qualifier("getSalesOrderItemPurchasingListBySalesOrderItem")
  105.     BusinessFunction getSalesOrderItemPurchasingListBySalesOrderItem;
  106.    
  107.     @Autowired
  108.     @Qualifier("findProductMasterPriceByProductForSasa")
  109.     BusinessFunction findProductMasterPriceByProductForSasa;
  110.    
  111.     @Autowired
  112.     @Qualifier("findSalesOrderItemCustomById")
  113.     BusinessFunction findSalesOrderItemCustomById;
  114.    
  115.     @Autowired
  116.     @Qualifier("findSalesOrderItemById")
  117.     BusinessFunction findSalesOrderItemById;
  118.    
  119.     @Autowired
  120.     @Qualifier("findPartnerById")
  121.     BusinessFunction findPartnerById;
  122.  
  123.     @Autowired
  124.     @Qualifier("valOverPlafonCustomerBySOId")
  125.     BusinessFunction valOverPlafonCustomerBySOId;
  126.    
  127.     @Override
  128.     public String getDescription() {
  129.         return "submit sales order";
  130.     }
  131.    
  132.     @SuppressWarnings("unchecked")
  133.     @Override
  134.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  135.         // Validation tenant login id can be use
  136.         log.info("START EXECUTE BT SUBMIT SALES ORDER>>>");
  137.  
  138.         Dto loginDto = new Dto();
  139.         loginDto.put("tenantLoginId", inputDto.getLong("tenantLoginId"));
  140.         loginDto.put("userLoginId", inputDto.getLong("userLoginId"));
  141.         loginDto.put("roleLoginId", inputDto.getLong("roleLoginId"));
  142.         valTenantLoginCanUse.execute(loginDto);
  143.  
  144.         Dto salesOrderDto = findSalesOrderById.execute(inputDto);
  145.         log.debug("salesOrderDto>>>"+salesOrderDto);
  146.  
  147.         int termOfPayment = salesOrderDto.getInteger("termOfPayment");
  148.        
  149.         // validate version
  150.         if(! salesOrderDto.getLong("version").equals(inputDto.get("version"))){
  151.             throw new CoreException(SalesExceptionConstants.DOCUMENT_VERSION_NOT_UPTODATE);
  152.         }
  153.        
  154.         // validation draft document
  155.         if (!SalesConstants.DRAFT_TRANSACTION.equals(salesOrderDto.getString("statusDoc"))) {
  156.             throw new CoreException(SalesExceptionConstants.DOC_ALREADY_SUBMITTED);
  157.         }
  158.        
  159.         // validasi tanggal dokumen <= tanggal hari ini
  160.         CommonBusinessValidator.valCompareDate(salesOrderDto.getString("docDate"), DateUtil.dateNow(), CommonBusinessValidator.COMPARE_LESS_EQUAL,
  161.                 "Doc Date For Submit", "Today");
  162.  
  163.         if (!salesOrderDto.getLong("tenantId").equals(inputDto.getLong("tenantLoginId"))) {
  164.             throw new CoreException(CommonExceptionConstants.DATA_CANT_CHANGE_CAUSE_TENANT, "Sales Order");
  165.         }
  166.        
  167.         Dto partnerBillToDto = findPartnerById.execute(new Dto().put("id", salesOrderDto.getLong("partnerBillToId")));
  168.         log.debug("partnerBillToDto>>>"+partnerBillToDto);
  169.        
  170.         //validasi jika partnerBillTo ="P" maka tidak bisa add Sales Order
  171.         if (SalesConstantsForSasa.POOR.equals(partnerBillToDto.getString("rank"))) {
  172.             throw new CoreException(SalesExceptionConstantsForSasa.CANNOT_SUBMIT_SALES_ORDER_WITH_CUSTOMER_BILL_TO_RANK_P, partnerBillToDto.getString("name"));
  173.         }
  174.  
  175.         //validasi verifikasi NIK
  176.  
  177.         log.info("START VALIDATION OF VERIFIED NIK>>>");
  178.         if (termOfPayment != 0){
  179.             String verifiedNik = GeneralConstants.NO;
  180.             QueryBuilder builder = new QueryBuilder();
  181.             builder.add("SELECT flag_verified_nik ")
  182.                     .add(" FROM ").add(SalesConstantsForSasa.TABLE_PARTNER_CUSTOM).add(" A ")
  183.                     .add(" WHERE A.partner_id = :partnerId ");
  184.  
  185.             Query queryVerifiedNik = salesOrderDao.createNativeQuery(builder.toString());
  186.             queryVerifiedNik.setParameter("partnerId", salesOrderDto.getLong("partnerId"));
  187.  
  188.             Object result = null;
  189.             log.info("result of flag verified nik: "+result);
  190.             try {
  191.                 result = queryVerifiedNik.getSingleResult();
  192.                 if(result != null) {
  193.                     verifiedNik = result.toString();
  194.                 }
  195.             } catch (NoResultException e) {}
  196.  
  197.             if (verifiedNik.equals(GeneralConstants.NO)) {
  198.                 throw new CoreException(SalesExceptionConstantsForSasa.PARTNER_NIK_NOT_VERIVIED);
  199.             }
  200.         }
  201.         log.info("END VALIDATION OF VERIFIED NIK>>>");
  202.  
  203.         log.info("START CALLING BF getSalesOrderItemListBySalesOrder >>> ");
  204.         //validasi item SO minimal 1
  205.         Dto itemListDto = getSalesOrderItemListBySalesOrder.execute(new Dto().put("tenantId", inputDto.getLong("tenantLoginId")).put("soId",
  206.                 inputDto.getLong("id")));
  207.         List<Dto> salesOrderItemList = itemListDto.getList("salesOrderItemList");
  208.         if (salesOrderItemList == null || salesOrderItemList.isEmpty()) {
  209.             throw new CoreException(SalesExceptionConstants.SALES_ORDER_MUST_HAVE_AT_LEAST_ONE_DETAIL_ITEM);
  210.         }
  211.         log.info("END CALLING BF getSalesOrderItemListBySalesOrder >>>> ");
  212.        
  213.         List<Dto> soItemList = new ArrayList<Dto>();
  214.         List<Dto> salesOrderItemCustomList = new ArrayList<Dto>();
  215.         log.info("salesOrderItemList: "+salesOrderItemList);
  216.         log.debug("version: "+inputDto.getLong("version"));
  217.        
  218.         Double totalSOAmount = 0D;
  219.  
  220.         log.info("START PREPARE DATA FOR SO ITEM>>>");
  221.         for (Dto salesOrderItemDto : salesOrderItemList) {
  222.             Dto outputSoItemDto = findSalesOrderItemById.execute(new Dto().put("id", salesOrderItemDto.getLong("id")));
  223.             totalSOAmount = totalSOAmount + outputSoItemDto.getDouble("nettItemAmount");
  224.             log.info("totalSOAmount: "+totalSOAmount);
  225.            
  226.             //outputSoItemDto.put("version", inputDto.getLong("version"));
  227.             log.debug("version outputSoItemDto: "+outputSoItemDto.getLong("version"));
  228.             this.prepareUpdateAudit(outputSoItemDto, inputDto.getLong("userLoginId"), inputDto.getString("datetime"));
  229.             log.debug("outputSoItemDto: "+outputSoItemDto);
  230.  
  231.             log.info("START CALLING BF FindProductMasterPriceByProductForSasa >> Product Id: "+salesOrderItemDto.getLong("productId"));
  232.             Dto inputFindProductMasterPriceByProductForSasa = new Dto();
  233.             inputFindProductMasterPriceByProductForSasa.put("id", salesOrderItemDto.getLong("productId"));
  234.             inputFindProductMasterPriceByProductForSasa.put("tenantId", inputDto.getLong("tenantLoginId"));
  235.             inputFindProductMasterPriceByProductForSasa.put("partnerId", salesOrderDto.getLong("partnerId"));
  236.             inputFindProductMasterPriceByProductForSasa.put("ouId", salesOrderDto.getLong("ouId"));
  237.             inputFindProductMasterPriceByProductForSasa.put("docDate", salesOrderDto.getLong("docDate"));
  238.             log.debug("inputFindProductMasterPriceByProductForSasa: "+inputFindProductMasterPriceByProductForSasa);
  239.  
  240.             Dto outputFindProductMasterPriceByProductForSasa = findProductMasterPriceByProductForSasa.execute(inputFindProductMasterPriceByProductForSasa);
  241.             log.debug("outputFindProductMasterPriceByProductForSasa: "+outputFindProductMasterPriceByProductForSasa);
  242.             log.info("END CALLING BF FindProductMasterPriceByProductForSasa >>>");
  243.            
  244.             Dto outputSOItemCustomDto = findSalesOrderItemCustomById.execute(new Dto().put("id", salesOrderItemDto.getLong("id")));
  245.             outputSOItemCustomDto.put("masterSellPrice", outputFindProductMasterPriceByProductForSasa.get("masterSellPrice"));
  246.             outputSOItemCustomDto.put("masterMinSellPrice", outputFindProductMasterPriceByProductForSasa.get("masterMinSellPrice"));
  247.             this.prepareUpdateAudit(outputSOItemCustomDto, inputDto.getLong("userLoginId"), inputDto.getString("datetime"));
  248.             log.debug("outputSOItemCustomDto: "+outputSOItemCustomDto);
  249.            
  250.             soItemList.add(outputSoItemDto);
  251.             salesOrderItemCustomList.add(outputSOItemCustomDto);
  252.         }
  253.         log.info("END PREPARE DATA FOR SO ITEM>>>");
  254.  
  255.         log.debug("salesOrderItemCustomList: "+salesOrderItemCustomList);
  256.         log.debug("salesOrderItemList: "+salesOrderItemList);
  257.         log.debug("soItemList: "+soItemList);
  258.         log.debug("totalSOAmount: "+totalSOAmount);
  259.        
  260.         if(totalSOAmount.equals(Double.valueOf("0"))) {
  261.             throw new CoreException(SalesExceptionConstantsForSasa.TOTAL_SALES_ORDER_AMOUNT_MUST_NOT_ZERO);
  262.         }
  263.  
  264.         log.info("START Over Plafon Validation>>>");
  265.         // Validasi overplafon atas customer bill to SO ( hanya divalidasi untuk kasus cara bayar SO = CREDIT )
  266.         valOverPlafonCustomerBySOId.execute(new Dto().put("soId", inputDto.getLong("id")));
  267.         log.info("END Over Plafon Validation>>>");
  268.  
  269.  
  270.         salesOrderDto.put("statusDoc", SalesConstants.IN_PROGRESS_TRANSACTION);
  271.         salesOrderDto.put("workflowStatus", inputDto.get("workflowStatus"));
  272.         salesOrderDto.put("version", inputDto.getLong("version"));
  273.  
  274.         this.prepareUpdateAudit(salesOrderDto, inputDto.getLong("userLoginId"), inputDto.getString("datetime"));
  275.  
  276.         inputDto.put("salesOrderDto", salesOrderDto);
  277.         inputDto.put("salesOrderItemCustomList", salesOrderItemCustomList);
  278.         inputDto.put("salesOrderItemList", soItemList);
  279.         log.info("inputDto before process: "+inputDto);
  280.         log.debug("inputDto process: "+inputDto);
  281.  
  282.         log.info("END EXCEUTE BT SUBMIT SALES ORDER<<<");
  283.         return null;
  284.     }
  285.  
  286.     @SuppressWarnings("unchecked")
  287.     @Override
  288.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  289.         Long tenantId = inputDto.getLong("tenantLoginId");
  290.         Long userId = inputDto.getLong("userLoginId");
  291.         String currDateTime = inputDto.getString("datetime");
  292.         String sessionId = UUID.randomUUID().toString();
  293.         log.debug("sessionId>>>"+sessionId);
  294.         Long soId = inputDto.getLong("id");
  295.  
  296.         log.info("START UPDATE DATA SALES ORDER HEADER AND ITEM >>>");
  297.         Dto salesOrderDto = inputDto.getDto("salesOrderDto");
  298.         SalesOrder salesOrder = GsonUtil.fromDto(salesOrderDto, SalesOrder.class);
  299.         salesOrder = salesOrderDao.merge(salesOrderDto.getLong("id"), salesOrder);
  300.        
  301.         List<Dto> salesOrderItemList = inputDto.getList("salesOrderItemList");
  302.         log.debug("salesOrderItemList: "+salesOrderItemList);
  303.         for (Dto salesOrderItemDto : salesOrderItemList) {
  304.             log.debug("salesOrderItemDto: "+salesOrderItemDto);
  305.             SalesOrderItem salesOrderItem = GsonUtil.fromDto(salesOrderItemDto, SalesOrderItem.class);
  306.             salesOrderItem = salesOrderItemDao.merge(salesOrderItemDto.getLong("id"), salesOrderItem);
  307.         }
  308.        
  309.         List<Dto> salesOrderItemCustomList = inputDto.getList("salesOrderItemCustomList");
  310.         for (Dto salesOrderItemCustomDto : salesOrderItemCustomList) {
  311.             SalesOrderItemCustom salesOrderItemCustom = GsonUtil.fromDto(salesOrderItemCustomDto, SalesOrderItemCustom.class);
  312.             salesOrderItemCustom = salesOrderItemCustomDao.merge(salesOrderItemCustomDto.getLong("id"), salesOrderItemCustom);
  313.         }
  314.         log.info("END UPDATE DATA SALES ORDER HEADER AND ITEM <<<");
  315.  
  316.         log.info("START EXECUTE FUNC f_update_and_insert_balance_stock_reserved_for_so >>>");
  317.         QueryBuilder builderFunction = new QueryBuilder();
  318.         builderFunction.add(" SELECT f_update_and_insert_balance_stock_reserved_for_so(:sessionId, :soId, :tenantId, :dateTime, :userId) ");
  319.  
  320.         Query queryFunction = productBalanceStockReservedDao.createNativeQuery(builderFunction.toString());
  321.         queryFunction.setParameter("sessionId", sessionId);
  322.         queryFunction.setParameter("soId", soId);
  323.         queryFunction.setParameter("tenantId", tenantId);
  324.         queryFunction.setParameter("dateTime", currDateTime);
  325.         queryFunction.setParameter("userId", userId);
  326.        
  327.         Object returnFunction = queryFunction.getSingleResult();
  328.         log.debug("returnFunction {}", returnFunction);
  329.  
  330.         log.info("END EXECUTE FUNC f_update_and_insert_balance_stock_reserved_for_so <<<");
  331.  
  332.  
  333.         return new Dto().put("id", salesOrder.getId());
  334.     }
  335. }
  336.  
RAW Paste Data