Advertisement
aadddrr

SubmitPosShop

Dec 27th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 65.48 KB | None | 0 0
  1. package org.jleaf.pos.bo.pos;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8.  
  9. import javax.persistence.Query;
  10.  
  11. import org.apache.commons.validator.GenericValidator;
  12. import org.jleaf.erp.master.MasterConstants;
  13. import org.jleaf.erp.master.MasterExceptionConstants;
  14. import org.jleaf.erp.master.bo.ComboIdMasterConstants;
  15. import org.jleaf.pos.ComboIdPosConstants;
  16. import org.jleaf.pos.PosBoConstants;
  17. import org.jleaf.pos.PosBoExceptionConstants;
  18. import org.jleaf.pos.bo.cashbalance.GetEndOfMonthCashBalanceList;
  19. import org.jleaf.pos.dao.CashBalanceDao;
  20. import org.jleaf.pos.dao.LogProductBalanceStockDao;
  21. import org.jleaf.pos.dao.NonCashBalanceDao;
  22. import org.jleaf.pos.dao.PointOfSalesAddCostDao;
  23. import org.jleaf.pos.dao.PointOfSalesAssemblyProductDao;
  24. import org.jleaf.pos.dao.PointOfSalesCashPaymentDao;
  25. import org.jleaf.pos.dao.PointOfSalesDao;
  26. import org.jleaf.pos.dao.PointOfSalesItemDao;
  27. import org.jleaf.pos.dao.PointOfSalesNonCashPaymentDao;
  28. import org.jleaf.pos.dao.PointOfSalesTaxDao;
  29. import org.jleaf.pos.dao.ProductBalanceStockDao;
  30. import org.jleaf.pos.dao.TrxItemLogDao;
  31. import org.jleaf.pos.dao.TrxLogDao;
  32. import org.jleaf.pos.entity.CashBalance;
  33. import org.jleaf.pos.entity.LogProductBalanceStock;
  34. import org.jleaf.pos.entity.NonCashBalance;
  35. import org.jleaf.pos.entity.PointOfSales;
  36. import org.jleaf.pos.entity.PointOfSalesAddCost;
  37. import org.jleaf.pos.entity.PointOfSalesAssemblyProduct;
  38. import org.jleaf.pos.entity.PointOfSalesCashPayment;
  39. import org.jleaf.pos.entity.PointOfSalesItem;
  40. import org.jleaf.pos.entity.PointOfSalesNonCashPayment;
  41. import org.jleaf.pos.entity.PointOfSalesTax;
  42. import org.jleaf.pos.entity.ProductBalanceStock;
  43. import org.jleaf.pos.entity.TrxItemLog;
  44. import org.jleaf.pos.entity.TrxLog;
  45. import org.jleaf.common.CommonConstants;
  46. import org.jleaf.common.CommonExceptionConstants;
  47. import org.jleaf.core.BusinessFunction;
  48. import org.jleaf.core.BusinessTransaction;
  49. import org.jleaf.core.CoreException;
  50. import org.jleaf.core.CoreExceptionConstants;
  51. import org.jleaf.core.DefaultBusinessTransaction;
  52. import org.jleaf.core.Dto;
  53. import org.jleaf.core.GeneralConstants;
  54. import org.jleaf.core.annotation.ErrorList;
  55. import org.jleaf.core.annotation.Info;
  56. import org.jleaf.core.annotation.InfoIn;
  57. import org.jleaf.core.annotation.InfoOut;
  58. import org.jleaf.core.dao.QueryBuilder;
  59. import org.jleaf.util.Calc;
  60. import org.jleaf.util.GsonUtil;
  61. import org.jleaf.util.ValidationUtil;
  62. import org.jleaf.validator.CommonBusinessValidator;
  63. import org.slf4j.Logger;
  64. import org.slf4j.LoggerFactory;
  65. import org.springframework.beans.factory.annotation.Autowired;
  66. import org.springframework.beans.factory.annotation.Qualifier;
  67. import org.springframework.stereotype.Service;
  68.  
  69. /**
  70.  * Submit POS Shop
  71.  *
  72.  * @author Daniel, Nov 17, 2012
  73.  * @version 1.0.0
  74.  */
  75. /**
  76.  * Ubah update product balance stock menjadi query
  77.  * modified by Adrian
  78.  * Dec 27, 2017
  79.  */
  80.  
  81. @Service
  82. //@formatter:off
  83. @InfoIn(value = {
  84.         @Info(name = "docTypeId", description = "document type id", type = Long.class),
  85.         @Info(name = "docNo", description = "document no", type = String.class),
  86.         @Info(name = "docDate", description = "document date", type = String.class),
  87.         @Info(name = "ouId", description = "organization unit id", type = Long.class),
  88.         @Info(name = "extDocNo", description = "external document no", type = String.class),
  89.         @Info(name = "extDocDate", description = "external document date", type = String.class),
  90.         @Info(name = "refDocTypeId", description = "reference doc type id", type = Long.class),
  91.         @Info(name = "refId", description = "reference id", type = Long.class),
  92.         @Info(name = "remark", description = "remark", type = String.class),
  93.         @Info(name = "partnerId", description = "partner id", type = Long.class),
  94.         @Info(name = "partnerBillToId", description = "partner bill to id", type = Long.class),
  95.         @Info(name = "partnerShipToId", description = "partner ship to id", type = Long.class),
  96.         @Info(name = "salesmanId", description = "salesman id", type = Long.class),
  97.         @Info(name = "currCode", description = "salesman id", type = String.class),
  98.         @Info(name = "addDiscountPercentage", description = "add discount percentage", type = Double.class),
  99.         @Info(name = "addDiscountAmount", description = "add discount amount", type = Double.class),
  100.         @Info(name = "grossAmount", description = "gross amount", type = Double.class),
  101.         @Info(name = "nettAmount", description = "nett amount", type = Double.class),
  102.         @Info(name = "taxAmount", description = "tax amount", type = Double.class),
  103.         @Info(name = "addAmount", description = "additional amount", type = Double.class),
  104.         @Info(name = "totalAmount", description = "total amount", type = Double.class),
  105.         @Info(name = "roundingAmount", description = "rounding amount", type = Double.class),
  106.         @Info(name = "totalPayment", description = "total payment", type = Double.class),
  107.         @Info(name = "totalRefund", description = "total refund", type = Double.class),    
  108.         @Info(name = "posItemList", description = "list of pos item (warehouseId, lineNo, productId, productBalanceId, productStatus, baseUomId, qty, grossSellPrice, flgTaxAmount, taxId, taxPercentage, taxPrice, promoCode, discountPercentage, discountAmount, nettSellPrice, taxAmount, nettAmountItem, remark, groupProductOuId, posAssemblyProductList)", type = List.class),
  109.         @Info(name = "cashList", description = "list of payment using cash (currPaymentCode, paymentAmount, dateExchangeRate, numeratorRate, denominatorRate, conversionAmount, remark)", type = List.class),
  110.         @Info(name = "nonCashList", description = "list of payment using non cash (lineNo, deviceMerchantId, cardType, bankCardCode, cardNo, currPaymentCode, paymentAmount, remark, percentage, addAmount)", type = List.class),
  111.         @Info(name = "userLoginId", description = "user login id", type = Long.class),
  112.         @Info(name = "roleLoginId", description = "role login id", type = Long.class),
  113.         @Info(name = "tenantLoginId", description = "tenant login id", type = Long.class),
  114.         @Info(name = "datetime", description = "datetime", type = String.class)
  115. })
  116. @InfoOut(value = {
  117.         @Info(name = "id", description = "Point of Sales Id", type = Long.class),
  118.         @Info(name = "tenantId", description = "PT", type = Long.class),
  119.         @Info(name = "docTypeId", description = "Document Type id", type = Long.class),
  120.         @Info(name = "docNo", description = "Document Number (Autogenerate)", type = String.class),
  121.         @Info(name = "docDate", description = "Document Date", type = String.class),
  122.         @Info(name = "ouId", description = "Organization unit Id", type = Long.class),
  123.         @Info(name = "extDocNo", description = "External Document Number", type = String.class),
  124.         @Info(name = "extDocDate", description = "External Document Date", type = String.class),
  125.         @Info(name = "refDocTypeId", description = "Reference document type Id", type = Long.class),
  126.         @Info(name = "refId", description = "Reference Id", type = Long.class),
  127.         @Info(name = "remark", description = "Remark", type = String.class),
  128.         @Info(name = "partnerId", description = "Partner id", type = Long.class),
  129.         @Info(name = "partnerBillToId", description = "Partner bill to Id", type = Long.class),
  130.         @Info(name = "partnerShipToId", description = "Partner ship to Id", type = Long.class),
  131.         @Info(name = "salesmanId", description = "Salesman Id", type = Long.class),
  132.         @Info(name = "currCode", description = "Currency code", type = String.class),
  133.         @Info(name = "addDiscountPercentage", description = "additional Discount percentage", type = Double.class),
  134.         @Info(name = "addDiscountAmount", description = "Additional Discount Amount", type = Double.class),
  135.         @Info(name = "grossAmount", description = "Gross Amount, total nilai netto item (sudah dipotong dengan disc per item belum dipotong pajak)", type = Double.class),
  136.         @Info(name = "nettAmount", description = "Net Amount, Nilai Item - additional discount", type = Double.class),
  137.         @Info(name = "taxAmount", description = "Tax Amount, Nilai Pajak berdasarkan detail tax document", type = Double.class),
  138.         @Info(name = "addAmount", description = "Additional Amount, nilai tambahan biaya yang dibebankan ke customer", type = Double.class),
  139.         @Info(name = "totalAmount", description = "TotalAmount, Net Amount + Nilai Pajak + add amount", type = Double.class),
  140.         @Info(name = "roundingAmount", description = "Rounding Amount, pembulatan nilai netto", type = Double.class),
  141.         @Info(name = "totalPayment", description = "Total Payment, Total Amount_conversion dari table cash dan non_cash", type = Double.class),
  142.         @Info(name = "totalRefund", description = "Total Refund", type = Double.class),
  143.         @Info(name = "status", description = "Status Transaction", type = String.class),
  144.         @Info(name = "version", description = "version", type = Long.class),
  145.         @Info(name = "createDateTime", description = "create date time", type = String.class),
  146.         @Info(name = "createUserId", description = "create user id", type = Long.class),
  147.         @Info(name = "updateDateTime", description = "update date time", type = String.class),
  148.         @Info(name = "updateUserId", description = "update user id", type = Long.class)
  149. })
  150. @ErrorList(errorKeys = {
  151.         CommonExceptionConstants.TENANT_LOGIN_CANT_USED,
  152.         CommonExceptionConstants.OU_ID_NOT_FOUND,
  153.         MasterExceptionConstants.PARTNER_ID_NOT_FOUND,
  154.         CoreExceptionConstants.ID_NOT_FOUND, // for Salesman
  155.         CommonExceptionConstants.COMBO_NOT_FOUND, // for currency, bank, card code
  156.         CommonExceptionConstants.USER_ID_NOT_FOUND,
  157.         MasterExceptionConstants.WAREHOUSE_ID_NOT_FOUND,
  158.         PosBoExceptionConstants.PRODUCT_BALANCE_ID_NOT_FOUND,
  159.         PosBoExceptionConstants.PRODUCT_BALANCE_STOCK_NOT_FOUND,
  160.         MasterExceptionConstants.PROMO_PRICE_PRODUCT_NOT_FOUND,
  161.         MasterExceptionConstants.TAX_ID_NOT_FOUND,
  162.         MasterExceptionConstants.GROUP_PRODUCT_OU_ID_NOT_FOUND,
  163.         MasterExceptionConstants.DEVICE_MERCHANT_ID_NOT_FOUND,
  164.         CommonExceptionConstants.SYSTEM_CONFIG_BY_CODE_NOT_FOUND, // for dbVersion and activity Gl Change
  165.         PosBoExceptionConstants.INCONSISTENT_PRODUCT_ID,
  166.         PosBoExceptionConstants.POS_CASH_NOT_FOUND,
  167.         MasterExceptionConstants.PRODUCT_ASSEMBLY_ID_NOT_FOUND,
  168.         PosBoExceptionConstants.PRODUCT_NOT_LISTED_IN_PRODUCT_ASSEMBLY_PRODUCT,
  169.         PosBoExceptionConstants.REAL_QTY_OVER_FORMULA_PRODUCT_ASSEMBLY,
  170.         PosBoExceptionConstants.REAL_QTY_BELOW_FORMULA_PRODUCT_ASSEMBLY
  171. })
  172. //@formatter:on
  173. public class SubmitPosShop extends DefaultBusinessTransaction implements BusinessTransaction {
  174.    
  175.     //private static final Logger log = LoggerFactory.getLogger(SubmitPosShop.class);
  176.  
  177.     @Autowired
  178.     private PointOfSalesDao posDao;
  179.  
  180.     @Autowired
  181.     private PointOfSalesItemDao posItemDao;
  182.  
  183.     @Autowired
  184.     private PointOfSalesAssemblyProductDao posAssemblyProductDao;
  185.  
  186.     @Autowired
  187.     private PointOfSalesCashPaymentDao posCashPaymentDao;
  188.  
  189.     @Autowired
  190.     private PointOfSalesNonCashPaymentDao posNonCashPaymentDao;
  191.  
  192.     @Autowired
  193.     private PointOfSalesTaxDao posTaxDao;
  194.  
  195.     @Autowired
  196.     private PointOfSalesAddCostDao posAddCostDao;
  197.  
  198.     @Autowired
  199.     private ProductBalanceStockDao productBalanceStockDao;
  200.  
  201.     @Autowired
  202.     private LogProductBalanceStockDao logProductBalanceStockDao;
  203.  
  204.     @Autowired
  205.     private CashBalanceDao cashBalanceDao;
  206.  
  207.     @Autowired
  208.     private NonCashBalanceDao nonCashBalanceDao;
  209.  
  210.     @Autowired
  211.     private TrxLogDao trxLogDao;
  212.  
  213.     @Autowired
  214.     private TrxItemLogDao trxItemLogDao;
  215.  
  216.     @Autowired
  217.     @Qualifier("valTenantLoginCanUse")
  218.     private BusinessFunction valTenantLoginCanUse;
  219.  
  220.     @Autowired
  221.     @Qualifier("findOUById")
  222.     private BusinessFunction findOUById;
  223.  
  224.     @Autowired
  225.     @Qualifier("findPartnerById")
  226.     private BusinessFunction findPartnerById;
  227.  
  228.     @Autowired
  229.     @Qualifier("findSalesmanById")
  230.     private BusinessFunction findSalesmanById;
  231.  
  232.     @Autowired
  233.     @Qualifier("valComboValueByCode")
  234.     private BusinessFunction valComboValueByCode;
  235.  
  236.     @Autowired
  237.     @Qualifier("findUserById")
  238.     private BusinessFunction findUserById;
  239.  
  240.     @Autowired
  241.     @Qualifier("findWarehouseById")
  242.     private BusinessFunction findWarehouseById;
  243.  
  244.     @Autowired
  245.     @Qualifier("findProductBalanceById")
  246.     private BusinessFunction findProductBalanceById;
  247.  
  248.     @Autowired
  249.     @Qualifier("findProductBalanceStockByIndex")
  250.     private BusinessFunction findProductBalanceStockByIndex;
  251.  
  252.     @Autowired
  253.     @Qualifier("findPromoPriceProductByIndex")
  254.     private BusinessFunction findPromoPriceProductByIndex;
  255.  
  256.     @Autowired
  257.     @Qualifier("findTaxById")
  258.     private BusinessFunction findTaxById;
  259.  
  260.     @Autowired
  261.     @Qualifier("findDeviceMerchantById")
  262.     private BusinessFunction findDeviceMerchantById;
  263.  
  264.     @Autowired
  265.     @Qualifier("findSystemConfigByParamCode")
  266.     private BusinessFunction findSystemConfigByParamCode;
  267.  
  268.     @Autowired
  269.     @Qualifier("getCashBankListAdvance")
  270.     private BusinessFunction getCashBankListAdvance;
  271.  
  272.     @Autowired
  273.     @Qualifier("isCashBalanceExistsByIndex")
  274.     private BusinessFunction isCashBalanceExistsByIndex;
  275.  
  276.     @Autowired
  277.     @Qualifier("isNonCashBalanceExistsByIndex")
  278.     private BusinessFunction isNonCashBalanceExistsByIndex;
  279.  
  280.     @Autowired
  281.     @Qualifier("valDocCanInputByDate")
  282.     private BusinessFunction valDocCanInputByDate;
  283.  
  284.     @Autowired
  285.     @Qualifier("findProductAssemblyById")
  286.     private BusinessFunction findProductAssemblyById;
  287.  
  288.     @Autowired
  289.     @Qualifier("getProductAssemblyDetailListByParentProduct")
  290.     private BusinessFunction getProductAssemblyDetailListByParentProduct;
  291.    
  292.     @Autowired
  293.     @Qualifier("findGroupProductOuById")
  294.     private BusinessFunction findGroupProductOuById;
  295.    
  296.     @Autowired
  297.     @Qualifier("findGroupProductOuPromoCommisionByDate")
  298.     private BusinessFunction findGroupProductOuPromoCommisionByDate;
  299.  
  300.     @Override
  301.     public String getDescription() {
  302.         return "Submit POS Shop";
  303.     }
  304.  
  305.     @SuppressWarnings("unchecked")
  306.     @Override
  307.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  308.         // Standart Validation
  309.         ValidationUtil.valDtoContainsKey(inputDto, "docTypeId");
  310.         ValidationUtil.valBlankOrNull(inputDto, "docNo");
  311.         ValidationUtil.valBlankOrNull(inputDto, "docDate");
  312.         ValidationUtil.valDtoContainsKey(inputDto, "ouId");
  313.         ValidationUtil.valDtoContainsKey(inputDto, "extDocNo");
  314.         ValidationUtil.valDtoContainsKey(inputDto, "extDocDate");
  315.         ValidationUtil.valDtoContainsKey(inputDto, "refDocTypeId");
  316.         ValidationUtil.valDtoContainsKey(inputDto, "refId");
  317.         ValidationUtil.valBlankOrNull(inputDto, "remark");
  318.         ValidationUtil.valDtoContainsKey(inputDto, "partnerId");
  319.         ValidationUtil.valDtoContainsKey(inputDto, "partnerBillToId");
  320.         ValidationUtil.valDtoContainsKey(inputDto, "partnerShipToId");
  321.         ValidationUtil.valDtoContainsKey(inputDto, "salesmanId");
  322.         ValidationUtil.valBlankOrNull(inputDto, "currCode");
  323.         ValidationUtil.valNumber(inputDto, "addDiscountPercentage");
  324.         ValidationUtil.valNumber(inputDto, "addDiscountAmount");
  325.         ValidationUtil.valNumber(inputDto, "grossAmount");
  326.         ValidationUtil.valNumber(inputDto, "nettAmount");
  327.         ValidationUtil.valNumber(inputDto, "taxAmount");
  328.         ValidationUtil.valNumber(inputDto, "addAmount");
  329.         ValidationUtil.valNumber(inputDto, "totalAmount");
  330.         ValidationUtil.valNumber(inputDto, "roundingAmount");
  331.         ValidationUtil.valNumber(inputDto, "totalPayment");
  332.         ValidationUtil.valNumber(inputDto, "totalRefund");
  333.         ValidationUtil.valDtoContainsKey(inputDto, "posItemList");
  334.         ValidationUtil.valDtoContainsKey(inputDto, "cashList");
  335.         ValidationUtil.valDtoContainsKey(inputDto, "nonCashList");
  336.         ValidationUtil.valDtoContainsKey(inputDto, "userLoginId");
  337.         ValidationUtil.valDtoContainsKey(inputDto, "roleLoginId");
  338.         ValidationUtil.valDtoContainsKey(inputDto, "tenantLoginId");
  339.         ValidationUtil.valBlankOrNull(inputDto, "datetime");
  340.  
  341.         // Preparing input parameters
  342.         Long docTypeId = inputDto.getLong("docTypeId");
  343.         String docNo = inputDto.getString("docNo");
  344.         String docDate = inputDto.getString("docDate");
  345.         Long ouId = inputDto.getLong("ouId");
  346.         // String extDocNo = inputDto.getString("extDocNo");
  347.         // String extDocDate = inputDto.getString("extDocDate");
  348.         // Long refDocTypeId = inputDto.getLong("refDocTypeId");
  349.         // Long refId = inputDto.getLong("refId");
  350.         // String remark = inputDto.getString("remark");
  351.         Long partnerId = inputDto.getLong("partnerId");
  352.         Long partnerBillToId = inputDto.getLong("partnerBillToId");
  353.         Long partnerShipToId = inputDto.getLong("partnerShipToId");
  354.         Long salesmanId = inputDto.getLong("salesmanId");
  355.         String currCode = inputDto.getString("currCode");
  356.         // Double addDiscountPercentage = inputDto.getDouble("addDiscountPercentage");
  357.         // Double addDiscountAmount = inputDto.getDouble("addDiscountAmount");
  358.         // Double grossAmount = inputDto.getDouble("grossAmount");
  359.         // Double nettAmount = inputDto.getDouble("nettAmount");
  360.         // Double taxAmount = inputDto.getDouble("taxAmount");
  361.         Double addAmount = inputDto.getDouble("addAmount");
  362.         Double totalAmount = inputDto.getDouble("totalAmount");
  363.         Double roundingAmount = inputDto.getDouble("roundingAmount");
  364.         Double totalPayment = inputDto.getDouble("totalPayment");
  365.         Double totalRefund = inputDto.getDouble("totalRefund");
  366.         List<Dto> posItemList = inputDto.getList("posItemList");
  367.         List<Dto> cashList = inputDto.getList("cashList");
  368.         List<Dto> nonCashList = inputDto.getList("nonCashList");
  369.         Long userLoginId = inputDto.getLong("userLoginId");
  370.         Long roleLoginId = inputDto.getLong("roleLoginId");
  371.         Long tenantLoginId = inputDto.getLong("tenantLoginId");
  372.         String datetime = inputDto.getString("datetime");
  373.  
  374.         // Validation tenant login id can be use
  375.         Dto loginDto = new Dto();
  376.         loginDto.put("tenantLoginId", tenantLoginId);
  377.         loginDto.put("userLoginId", userLoginId);
  378.         loginDto.put("roleLoginId", roleLoginId);
  379.         valTenantLoginCanUse.execute(loginDto);
  380.  
  381.         // Validation ou id must exists
  382.         Dto ouDto = new Dto();
  383.         ouDto.put("id", ouId);
  384.         ouDto = findOUById.execute(ouDto);
  385.  
  386.         if (!ouDto.getLong("tenantId").equals(tenantLoginId)) {
  387.             throw new CoreException(CommonExceptionConstants.DATA_CANT_USE_DIFF_TENANT, "Point of Sales", "OU");
  388.         }
  389.  
  390.         if (!GeneralConstants.YES.equals(ouDto.getString("active"))) {
  391.             throw new CoreException(MasterExceptionConstants.DATA_NOT_ACTIVE, "OU");
  392.         }
  393.  
  394.         // Validate doc date must be valid
  395.         Dto valDocDateDto = new Dto();
  396.         valDocDateDto.put("tenantId", tenantLoginId);
  397.         valDocDateDto.put("docDate", docDate);
  398.         valDocDateDto.put("ouId", ouId);
  399.         valDocCanInputByDate.execute(valDocDateDto);
  400.  
  401.         // Validation partner must exists
  402.         if (!partnerId.equals(GeneralConstants.NULL_REF_VALUE_LONG)) {
  403.             Dto partnerDto = new Dto();
  404.             partnerDto.put("id", partnerId);
  405.             partnerDto = findPartnerById.execute(partnerDto);
  406.         }
  407.  
  408.         if (!partnerBillToId.equals(GeneralConstants.NULL_REF_VALUE_LONG)) {
  409.             Dto partnerBillToDto = new Dto();
  410.             partnerBillToDto.put("id", partnerBillToId);
  411.             partnerBillToDto = findPartnerById.execute(partnerBillToDto);
  412.         }
  413.  
  414.         if (!partnerShipToId.equals(GeneralConstants.NULL_REF_VALUE_LONG)) {
  415.             Dto partnerShipToDto = new Dto();
  416.             partnerShipToDto.put("id", partnerShipToId);
  417.             partnerShipToDto = findPartnerById.execute(partnerShipToDto);
  418.         }
  419.  
  420.         // Validation salesman must exists
  421.         Dto salesmanDto = new Dto();
  422.         salesmanDto.put("id", salesmanId);
  423.         salesmanDto.put("tenantId", tenantLoginId);
  424.         salesmanDto = findSalesmanById.execute(salesmanDto);
  425.  
  426.         // Validation currency must be suitable with combo CURRENCY
  427.         Dto paramCurrencyDto = new Dto();
  428.         paramCurrencyDto.put("comboId", ComboIdMasterConstants.COMBO_CURRENCY);
  429.         paramCurrencyDto.put("code", currCode);
  430.         paramCurrencyDto.put("varName", "Currency");
  431.         valComboValueByCode.execute(paramCurrencyDto);
  432.  
  433.         // Validating Total Payment must equal or greater than zero
  434.         CommonBusinessValidator.valCompareNumber(totalPayment.toString(), "0",
  435.                 CommonBusinessValidator.COMPARE_GREATER_EQUAL, "totalPayment", "0");
  436.  
  437.         // Validating Total Amount must equal or greater than zero
  438.         CommonBusinessValidator.valCompareNumber(totalAmount.toString(), "0",
  439.                 CommonBusinessValidator.COMPARE_GREATER_EQUAL, "totalAmount", "0");
  440.  
  441.         // Validating Total Payment must equal or greater than total Amount
  442.         CommonBusinessValidator.valCompareNumber(totalPayment.toString(), totalAmount.toString(),
  443.                 CommonBusinessValidator.COMPARE_GREATER_EQUAL, "totalPayment", "totalAmount");
  444.        
  445.         Double totalAmountNeedToPay = new Calc(totalAmount).add(addAmount).doubleValue();
  446.        
  447.         // Validating Total Payment must equal or greater than total Amount neeed to pay
  448.                 CommonBusinessValidator.valCompareNumber(totalPayment.toString(), totalAmountNeedToPay.toString(),
  449.                         CommonBusinessValidator.COMPARE_GREATER_EQUAL, "totalPayment", "totalAmountNeedToPay");
  450.  
  451.         // Get Username
  452.         Dto userDto = new Dto();
  453.         userDto.put("id", userLoginId);
  454.         userDto = findUserById.execute(userDto);
  455.         String username = userDto.getString("name");
  456.  
  457.         // Preparing Input Header
  458.         Dto persistPosDto = (Dto) inputDto.clone();
  459.         persistPosDto.put("tenantId", tenantLoginId);
  460.         persistPosDto.put("status", PosBoConstants.SUBMITTED_TRANSACTION);
  461.         persistPosDto.put("createUsername", username);
  462.         persistPosDto.put("updateUsername", username);
  463.         prepareInsertAudit(persistPosDto, userLoginId, datetime);
  464.         prepareUpdateAudit(persistPosDto, userLoginId, datetime);
  465.  
  466.         // Looping detail item
  467.         List<Dto> persistPosItemList = new ArrayList<Dto>();
  468.         List<Dto> persistPosTaxList = new ArrayList<Dto>();
  469.         Map<Long, Dto> mergeProductBalanceStockList = new HashMap<Long, Dto>();
  470.         if (posItemList != null) {
  471.             for (Dto posItemDto : posItemList) {
  472.                 // Standart Validation
  473.                 ValidationUtil.valDtoContainsKey(posItemDto, "warehouseId");
  474.                 ValidationUtil.valDtoContainsKey(posItemDto, "lineNo");
  475.                 ValidationUtil.valDtoContainsKey(posItemDto, "productId");
  476.                 ValidationUtil.valDtoContainsKey(posItemDto, "productBalanceId");
  477.                 ValidationUtil.valBlankOrNull(posItemDto, "productStatus");
  478.                 ValidationUtil.valDtoContainsKey(posItemDto, "baseUomId");
  479.                 ValidationUtil.valNumber(posItemDto, "qty");
  480.                 ValidationUtil.valNumber(posItemDto, "grossSellPrice");
  481.                 ValidationUtil.valDtoContainsKey(posItemDto, "flgTaxAmount");
  482.                 ValidationUtil.valDtoContainsKey(posItemDto, "taxId");
  483.                 ValidationUtil.valNumber(posItemDto, "taxPercentage");
  484.                 ValidationUtil.valNumber(posItemDto, "taxPrice");
  485.                 ValidationUtil.valDtoContainsKey(posItemDto, "promoCode");
  486.                 ValidationUtil.valNumber(posItemDto, "discountPercentage");
  487.                 ValidationUtil.valNumber(posItemDto, "discountAmount");
  488.                 ValidationUtil.valNumber(posItemDto, "nettSellPrice");
  489.                 ValidationUtil.valNumber(posItemDto, "taxAmount");
  490.                 ValidationUtil.valNumber(posItemDto, "nettAmountItem");
  491.                 ValidationUtil.valDtoContainsKey(posItemDto, "remark");
  492.                 ValidationUtil.valDtoContainsKey(posItemDto, "groupProductOuId");
  493.  
  494.                 // Preparing Variable
  495.                 Long warehouseId = posItemDto.getLong("warehouseId");
  496.                 Long productId = posItemDto.getLong("productId");
  497.                 Long productBalanceId = posItemDto.getLong("productBalanceId");
  498.                 String productStatus = posItemDto.getString("productStatus");
  499.                 // previously, it's used for writing log product balance stock.
  500.                 // But, it already changed so that the base uom id for writing log product balance stock is got from product balance stock.
  501.                 //Long baseUomId = posItemDto.getLong("baseUomId");
  502.                 Long taxId = posItemDto.getLong("taxId");
  503.                 String promoCode = posItemDto.getString("promoCode");
  504.                 Long groupProductOuId = posItemDto.getLong("groupProductOuId");
  505.                 Double taxAmount = posItemDto.getDouble("taxAmount");
  506.                 Double qty = posItemDto.getDouble("qty");
  507.                 Double nettAmountItem = posItemDto.getDouble("nettAmountItem");
  508.                 String flgTaxAmount = posItemDto.getString("flgTaxAmount");
  509.                 Double taxPercentage = posItemDto.getDouble("taxPercentage");
  510.  
  511.                 // Validation warehouse must exists
  512.                 Dto warehouseDto = new Dto();
  513.                 warehouseDto.put("id", warehouseId);
  514.                 warehouseDto = findWarehouseById.execute(warehouseDto);
  515.  
  516.                 Dto mergeProductBalanceStockDto = null;
  517.                 List<Dto> persistPosAssemblyProductList = Collections.EMPTY_LIST;
  518.                 if (GeneralConstants.NULL_REF_VALUE_LONG.equals(productBalanceId)) {
  519.                     // Validation ext product id must exists
  520.                     Dto productAssemblyDto = new Dto();
  521.                     productAssemblyDto.put("productId", productId);
  522.                     productAssemblyDto = findProductAssemblyById.execute(productAssemblyDto);
  523.                    
  524.                     // Prepare map that contains map of child products of the assembly, with remaining qty needs to be fulfilled
  525.                     List<Dto> assemblyFormulaDetailList = getProductAssemblyDetailListByParentProduct.execute(new Dto().put("tenantId", tenantLoginId).put("parentProductId", productId)).getList("productAssemblyDetailList");
  526.                     Map<Long, Dto> assemblyFormulaDetailMap = new HashMap<Long, Dto>(assemblyFormulaDetailList.size());
  527.                     for (Dto assemblyDetail : assemblyFormulaDetailList) {
  528.                         assemblyDetail.putDouble("remainingQty", assemblyDetail.getDouble("qtyBaseUom"));
  529.                         assemblyFormulaDetailMap.put(assemblyDetail.getLong("childProductId"), assemblyDetail);
  530.                     }
  531.                    
  532.                     // Iterate the assembly product list
  533.                     List<Dto> posAssemblyProductList = posItemDto.getList("posAssemblyProductList");
  534.                     if (posAssemblyProductList != null && !posAssemblyProductList.isEmpty()) {
  535.                         persistPosAssemblyProductList = new ArrayList<Dto>(posAssemblyProductList.size());
  536.                         for (Dto posAssemblyProduct : posAssemblyProductList) {
  537.                             // Validate keys in dto
  538.                             ValidationUtil.valDtoContainsKey(posAssemblyProduct, "lineNo");
  539.                             ValidationUtil.valDtoContainsKey(posAssemblyProduct, "productId");
  540.                             ValidationUtil.valDtoContainsKey(posAssemblyProduct, "productBalanceId");
  541.                             ValidationUtil.valBlankOrNull(posAssemblyProduct, "productStatus");
  542.                             ValidationUtil.valDtoContainsKey(posAssemblyProduct, "baseUomId");
  543.                             ValidationUtil.valNumber(posAssemblyProduct, "qty");
  544.                             ValidationUtil.valDtoContainsKey(posAssemblyProduct, "remark");
  545.                            
  546.                             // Validate product balance
  547.                             Long childProductId = posAssemblyProduct.getLong("productId");
  548.                             mergeProductBalanceStockDto = getMergeProductBalanceStock(tenantLoginId, warehouseId,
  549.                                                                                       childProductId,
  550.                                                                                       posAssemblyProduct.getLong("productBalanceId"),
  551.                                                                                       posAssemblyProduct.getString("productStatus"),
  552.                                                                                       mergeProductBalanceStockList);
  553.                            
  554.                             // Update remaining qty in assembly formula map
  555.                             Dto assemblyFormulaDetail = assemblyFormulaDetailMap.get(childProductId);
  556.                             if (assemblyFormulaDetail == null)
  557.                                 throw new CoreException(PosBoExceptionConstants.PRODUCT_NOT_LISTED_IN_PRODUCT_ASSEMBLY_PRODUCT, childProductId, productId);
  558.                             else {
  559.                                 assemblyFormulaDetail.put("remainingQty",
  560.                                         new Calc(assemblyFormulaDetail.getDouble("remainingQty")).subtract(posAssemblyProduct.getDouble("qty")).doubleValue());
  561.                             }
  562.                            
  563.                             // create pos assembly product
  564.                             Dto persistPosAssemblyProduct = (Dto) posAssemblyProduct.clone();
  565.                             persistPosAssemblyProduct.put("tenantId", tenantLoginId);
  566.                             if (GenericValidator.isBlankOrNull(persistPosAssemblyProduct.getString("remark")))
  567.                                 persistPosAssemblyProduct.putString("remark", GeneralConstants.SPACE_VALUE);
  568.                             prepareInsertAudit(persistPosAssemblyProduct, userLoginId, datetime);
  569.                             prepareUpdateAudit(persistPosAssemblyProduct, userLoginId, datetime);
  570.                             persistPosAssemblyProductList.add(persistPosAssemblyProduct);
  571.                            
  572.                             // Update product balance stock and log product balance stock
  573.                             updateProductBalanceStock(docTypeId, docNo, docDate, ouId, userLoginId,
  574.                                                       tenantLoginId, datetime, posAssemblyProduct.getDouble("qty"), mergeProductBalanceStockDto);
  575.                         }
  576.                     }
  577.                    
  578.                     // Check the map for unfulfilled formula qty
  579.                     for (Dto assemblyFormulaDetail : assemblyFormulaDetailMap.values()) {
  580.                         Double remainingQty = assemblyFormulaDetail.getDouble("remainingQty");
  581.                         int compareResult = remainingQty.compareTo(0d);
  582.                         if (compareResult > 0) {
  583.                             throw new CoreException(PosBoExceptionConstants.REAL_QTY_OVER_FORMULA_PRODUCT_ASSEMBLY,
  584.                                                     assemblyFormulaDetail.getString("childProductName"),
  585.                                                     remainingQty, assemblyFormulaDetail.getDouble("qtyBaseUom"));
  586.                         } else if (compareResult < 0) {
  587.                             throw new CoreException(PosBoExceptionConstants.REAL_QTY_BELOW_FORMULA_PRODUCT_ASSEMBLY,
  588.                                                     assemblyFormulaDetail.getString("childProductName"),
  589.                                                     remainingQty, assemblyFormulaDetail.getDouble("qtyBaseUom"));
  590.                         }
  591.                     }
  592.                 } else {
  593.                     mergeProductBalanceStockDto = getMergeProductBalanceStock(tenantLoginId, warehouseId, productId, productBalanceId, productStatus, mergeProductBalanceStockList);
  594.                    
  595.                     updateProductBalanceStock(docTypeId, docNo, docDate, ouId, userLoginId,
  596.                             tenantLoginId, datetime, qty, mergeProductBalanceStockDto);
  597.                 }
  598.  
  599.                 // Validation promo code must exists
  600.                 if (!GeneralConstants.EMPTY_VALUE.equals(promoCode)) {
  601.                     Dto promoDto = new Dto();
  602.                     promoDto.put("tenantId", tenantLoginId);
  603.                     promoDto.put("code", promoCode);
  604.                     promoDto.put("productId", productId);
  605.                     promoDto.put("ouId", ouId);
  606.                     promoDto = findPromoPriceProductByIndex.execute(promoDto);
  607.                    
  608.                     // dapatkan group product ou id untuk promo (jika tidak ketemu akan dikembalikan sesuai regulernya)
  609.                     Dto paramGroupProductOuPromoDto = new Dto();
  610.                     paramGroupProductOuPromoDto.put("groupProductOuId", groupProductOuId);
  611.                     paramGroupProductOuPromoDto.put("date", docDate);
  612.                     paramGroupProductOuPromoDto.put("promoCode", promoCode);
  613.                     Dto groupProductOuPromoCommissionDto = findGroupProductOuPromoCommisionByDate.execute(paramGroupProductOuPromoDto);
  614.                    
  615.                     // override group product ou id
  616.                     posItemDto.put("groupProductOuId", groupProductOuPromoCommissionDto.getLong("groupProductPromoOuId"));
  617.                 } else {
  618.                     // better nanti diisi space saja, apabila promoCode == Empty String (Hasil chat GTalk 26 Nov 2012, 23:03
  619.                     promoCode = GeneralConstants.SPACE_VALUE;
  620.                 }
  621.  
  622.                 // Validation Group Product OU Id
  623.                 Dto groupProductOuDto = new Dto();
  624.                 groupProductOuDto.put("id", groupProductOuId);
  625.                 groupProductOuDto = findGroupProductOuById.execute(groupProductOuDto);
  626.  
  627.                 // Preparing for posItem
  628.                 Dto persistPosItemDto = (Dto) posItemDto.clone();
  629.                 persistPosItemDto.put("tenantId", tenantLoginId);
  630.                 persistPosItemDto.put("currCode", currCode);
  631.                 persistPosItemDto.put("promoCode", promoCode);
  632.                 persistPosItemDto.put("persistPosAssemblyProductList", persistPosAssemblyProductList);
  633.                 prepareUpdateAudit(persistPosItemDto, userLoginId, datetime);
  634.                 persistPosItemList.add(persistPosItemDto);
  635.  
  636.                 // Validation Tax
  637.                 if(!GeneralConstants.NULL_REF_VALUE_LONG.equals(taxId)){
  638.                     Dto taxDto = new Dto();
  639.                     taxDto.put("id", taxId);
  640.                     taxDto = findTaxById.execute(taxDto);
  641.                
  642.                     // Create Or Replace Pos Tax
  643.                     boolean isCreateNewPosTax = true;
  644.                    
  645.                     // cari nilai nett amount item sebelum tax
  646.                     Double nettAmountItemBeforeTax = new Calc(nettAmountItem).subtract(taxAmount).doubleValue();
  647.                    
  648.                     for (Dto posTax : persistPosTaxList) {
  649.                         // If taxId exists, just add the amount, else create new POSTax
  650.                         if (posTax.getLong("taxId").equals(taxId)) {
  651.                             Double newBaseAmount = new Calc(posTax.getDouble("baseAmount")).add(nettAmountItemBeforeTax).doubleValue();
  652.                             Double newTaxAmount = new Calc(posTax.getDouble("taxAmount")).add(taxAmount).doubleValue();
  653.    
  654.                             posTax.put("baseAmount", newBaseAmount);
  655.                             posTax.put("taxAmount", newTaxAmount);
  656.    
  657.                             isCreateNewPosTax = false;
  658.                             break;
  659.                         }
  660.                     }
  661.    
  662.                     if (isCreateNewPosTax == true) {
  663.                         PointOfSalesTax posTax = new PointOfSalesTax();
  664.                         posTax.setTenantId(tenantLoginId);
  665.                         posTax.setTaxId(taxId);
  666.                         posTax.setFlgAmount(taxDto.getString("flagAmount"));
  667.                         posTax.setTaxPercentage(taxPercentage);
  668.                         posTax.setBaseAmount(nettAmountItemBeforeTax);
  669.    
  670.                         posTax.setTaxAmount(taxAmount);
  671.                         posTax.setRemark(GeneralConstants.EMPTY_VALUE);
  672.    
  673.                         Dto posTaxDto = new Dto(posTax);
  674.                         prepareInsertAudit(posTaxDto, userLoginId, datetime);
  675.                         persistPosTaxList.add(posTaxDto);
  676.                     }
  677.                 }
  678.             }
  679.         }
  680.  
  681.         // Looping Cash
  682.         List<Dto> persistCashList = new ArrayList<Dto>();
  683.         List<Dto> persistCashBalanceList = new ArrayList<Dto>();
  684.         List<Dto> mergeCashBalanceList = new ArrayList<Dto>();
  685.         if (cashList != null) {
  686.             for (Dto cashDto : cashList) {
  687.                 // Standart Validation
  688.                 ValidationUtil.valBlankOrNull(cashDto, "currPaymentCode");
  689.                 ValidationUtil.valNumber(cashDto, "paymentAmount");
  690.                 ValidationUtil.valBlankOrNull(cashDto, "dateExchangeRate");
  691.                 ValidationUtil.valNumber(cashDto, "numeratorRate");
  692.                 ValidationUtil.valNumber(cashDto, "denominatorRate");
  693.                 ValidationUtil.valNumber(cashDto, "conversionAmount");
  694.                 ValidationUtil.valDtoContainsKey(cashDto, "remark");
  695.  
  696.                 // Preparing Variable
  697.                 String currPaymentCode = cashDto.getString("currPaymentCode");
  698.                 Double paymentAmount = cashDto.getDouble("paymentAmount");
  699.                 // String dateExchangeRate = cashDto.getString("dateExchangeRate");
  700.                 // Double numeratorRate = cashDto.getDouble("numeratorRate");
  701.                 // Double denominatorRate = cashDto.getDouble("denominatorRate");
  702.                 // Double conversionAmount = cashDto.getDouble("conversionAmount");
  703.  
  704.                 // Currency must exists
  705.                 Dto paramCashCurrencyDto = new Dto();
  706.                 paramCashCurrencyDto.put("comboId", ComboIdMasterConstants.COMBO_CURRENCY);
  707.                 paramCashCurrencyDto.put("code", currPaymentCode);
  708.                 paramCashCurrencyDto.put("varName", "Currency");
  709.                 valComboValueByCode.execute(paramCashCurrencyDto);
  710.  
  711.                 // Amount payment must >= 0
  712.                 CommonBusinessValidator.valCompareNumber(paymentAmount.toString(), "0", CommonBusinessValidator.COMPARE_GREATER_EQUAL,
  713.                         "CashPayment", "Zero");
  714.  
  715.                 // Preparing for Cash Payment
  716.                 Dto persistCashPaymentDto = (Dto) cashDto.clone();
  717.                 persistCashPaymentDto.put("tenantId", tenantLoginId);
  718.                 prepareInsertAudit(persistCashPaymentDto, userLoginId, datetime);
  719.                 prepareUpdateAudit(persistCashPaymentDto, userLoginId, datetime);
  720.                 persistCashList.add(persistCashPaymentDto);
  721.  
  722.                 // Check is Cash Balance exists
  723.                 Dto cashBalanceDto = new Dto();
  724.                 cashBalanceDto.put("tenantId", tenantLoginId);
  725.                 cashBalanceDto.put("ouId", ouId);
  726.                 cashBalanceDto.put("cashBankId", getCashBankCodeForCash(tenantLoginId, currPaymentCode).getLong("id"));
  727.                 cashBalanceDto.put("cashBankDate", docDate);
  728.                 cashBalanceDto.put("recType", PosBoConstants.RECORD_TYPE_CASH_BALANCE_DEBT);
  729.                 cashBalanceDto = isCashBalanceExistsByIndex.execute(cashBalanceDto);
  730.  
  731.                 // Preparing for Cash Balance
  732.                 if (cashBalanceDto.getBoolean("exists")) {
  733.                     // merge cash balance
  734.                     Dto mergeCashBalanceDto = cashBalanceDto.getDto("cashBalanceDto");
  735.                     mergeCashBalanceDto.put("amount", new Calc(Double.valueOf(mergeCashBalanceDto.get("amount").toString())).add(paymentAmount)
  736.                             .doubleValue());
  737.                     prepareUpdateAudit(mergeCashBalanceDto, userLoginId, datetime);
  738.  
  739.                     mergeCashBalanceList.add(mergeCashBalanceDto);
  740.                 } else {
  741.                     // persist cash balance
  742.                     Dto persistCashBalanceDto = new Dto();
  743.                     persistCashBalanceDto.put("tenantId", tenantLoginId);
  744.                     persistCashBalanceDto.put("ouId", ouId);
  745.                     persistCashBalanceDto.put("cashBankId", getCashBankCodeForCash(tenantLoginId, currPaymentCode).getLong("id"));
  746.                     persistCashBalanceDto.put("cashBankDate", docDate);
  747.                     persistCashBalanceDto.put("recType", PosBoConstants.RECORD_TYPE_CASH_BALANCE_DEBT);
  748.                     persistCashBalanceDto.put("currCode", currPaymentCode);
  749.                     persistCashBalanceDto.put("amount", paymentAmount);
  750.                     prepareInsertAudit(persistCashBalanceDto, userLoginId, datetime);
  751.                     prepareUpdateAudit(persistCashBalanceDto, userLoginId, datetime);
  752.  
  753.                     persistCashBalanceList.add(persistCashBalanceDto);
  754.                 }
  755.             }
  756.         }
  757.  
  758.         // Looping Non Cash
  759.         List<Dto> persistNonCashList = new ArrayList<Dto>();
  760.         List<Dto> persistAddCostList = new ArrayList<Dto>();
  761.         List<Dto> persistNonCashBalanceList = new ArrayList<Dto>();
  762.         List<Dto> mergeNonCashBalanceList = new ArrayList<Dto>();
  763.         if (nonCashList != null) {
  764.             for (Dto nonCashDto : nonCashList) {
  765.                 // Standart Validation
  766.                 ValidationUtil.valDtoContainsKey(nonCashDto, "lineNo");
  767.                 ValidationUtil.valDtoContainsKey(nonCashDto, "deviceMerchantId");
  768.                 ValidationUtil.valBlankOrNull(nonCashDto, "cardType");
  769.                 ValidationUtil.valBlankOrNull(nonCashDto, "bankCardCode");
  770.                 ValidationUtil.valBlankOrNull(nonCashDto, "cardNo");
  771.                 ValidationUtil.valBlankOrNull(nonCashDto, "currPaymentCode");
  772.                 ValidationUtil.valNumber(nonCashDto, "paymentAmount");
  773.                 ValidationUtil.valDtoContainsKey(nonCashDto, "remark");
  774.                 ValidationUtil.valNumber(nonCashDto, "percentage");
  775.                 ValidationUtil.valNumber(nonCashDto, "addAmount");
  776.  
  777.                 // Preparing Variable
  778.                 Long lineNo = nonCashDto.getLong("lineNo");
  779.                 Long deviceMerchantId = nonCashDto.getLong("deviceMerchantId");
  780.                 String cardType = nonCashDto.getString("cardType");
  781.                 String bankCardCode = nonCashDto.getString("bankCardCode");
  782.                 String currPaymentCode = nonCashDto.getString("currPaymentCode");
  783.                 Double paymentAmount = nonCashDto.getDouble("paymentAmount");
  784.                 Double percentage = nonCashDto.getDouble("percentage");
  785.                 Double addAmountNonCash = nonCashDto.getDouble("addAmount");
  786.  
  787.                 // Validate device merchant id must exist
  788.                 Dto deviceMerchantDto = new Dto();
  789.                 deviceMerchantDto.put("id", deviceMerchantId);
  790.                 deviceMerchantDto = findDeviceMerchantById.execute(deviceMerchantDto);
  791.  
  792.                 // Validate card Type must exists
  793.                 Dto cardTypeDto = new Dto();
  794.                 cardTypeDto.put("comboId", ComboIdPosConstants.COMBO_CARD_TYPE);
  795.                 cardTypeDto.put("code", cardType);
  796.                 cardTypeDto.put("varName", "Card Type");
  797.                 valComboValueByCode.execute(cardTypeDto);
  798.  
  799.                 // Validate bank card code must exists
  800.                 Dto bankCardDto = new Dto();
  801.                 bankCardDto.put("comboId", ComboIdMasterConstants.COMBO_BANK);
  802.                 bankCardDto.put("code", bankCardCode);
  803.                 bankCardDto.put("varName", "Bank Card Code");
  804.                 valComboValueByCode.execute(bankCardDto);
  805.  
  806.                 // Validate curr payment code must exists
  807.                 Dto paramCurrencyNonCashDto = new Dto();
  808.                 paramCurrencyNonCashDto.put("comboId", ComboIdMasterConstants.COMBO_CURRENCY);
  809.                 paramCurrencyNonCashDto.put("code", currPaymentCode);
  810.                 paramCurrencyNonCashDto.put("varName", "Currency");
  811.                 valComboValueByCode.execute(paramCurrencyNonCashDto);
  812.  
  813.                 // Get Activity Gl Id for Charge
  814.                 Dto activityGlIdForChargeDto = new Dto();
  815.                 activityGlIdForChargeDto.put("tenantId", tenantLoginId);
  816.                 activityGlIdForChargeDto.put("parameterCode", PosBoConstants.PARAM_ACTIVITY_GL_CODE_CHARGE);
  817.                 activityGlIdForChargeDto = findSystemConfigByParamCode.execute(activityGlIdForChargeDto);
  818.                 String activityGlIdForChargeString = activityGlIdForChargeDto.getString("value");
  819.                 Long activityGlIdForCharge = new Long(activityGlIdForChargeString);
  820.  
  821.                 // Preparing for Non Cash Payment
  822.                 Dto persistPosNonCashDto = (Dto) nonCashDto.clone();
  823.                 persistPosNonCashDto.put("tenantId", tenantLoginId);
  824.                 persistPosNonCashDto.put("dateExchangeRate", GeneralConstants.EMPTY_VALUE);
  825.                 persistPosNonCashDto.put("numeratorRate", 1D);
  826.                 persistPosNonCashDto.put("denominatorRate", 1D);
  827.                 persistPosNonCashDto.put("conversionAmount", paymentAmount);
  828.                 prepareInsertAudit(persistPosNonCashDto, userLoginId, datetime);
  829.                 prepareUpdateAudit(persistPosNonCashDto, userLoginId, datetime);
  830.                 persistNonCashList.add(persistPosNonCashDto);
  831.  
  832.                 // Preparing for Additional Cost
  833.                 Dto persistPosAddCostDto = new Dto();
  834.                 persistPosAddCostDto.put("tenantId", tenantLoginId);
  835.                 persistPosAddCostDto.put("lineNo", lineNo);
  836.                 persistPosAddCostDto.put("refDocTypeId", docTypeId);
  837.                 persistPosAddCostDto.put("activityGlId", activityGlIdForCharge);
  838.                 persistPosAddCostDto.put("currCode", currPaymentCode);
  839.                 persistPosAddCostDto.put("percentage", percentage);
  840.                 persistPosAddCostDto.put("baseAmount", paymentAmount);
  841.                 persistPosAddCostDto.put("taxId", GeneralConstants.NULL_REF_VALUE_LONG);
  842.                 persistPosAddCostDto.put("taxPercentage", 0D); // tax percentage set 0
  843.                 persistPosAddCostDto.put("addAmount", addAmountNonCash);
  844.                 persistPosAddCostDto.put("taxAmount", 0D);
  845.                 persistPosAddCostDto.put("remark", GeneralConstants.EMPTY_VALUE);
  846.                 prepareInsertAudit(persistPosAddCostDto, userLoginId, datetime);
  847.                 prepareUpdateAudit(persistPosAddCostDto, userLoginId, datetime);
  848.                 persistPosNonCashDto.putDto("posAddCost", persistPosAddCostDto);
  849.  
  850.                 // The non cash balance could be already been persisted or merged.
  851.                 // In the matter of reducing database access, try to look it first in lists
  852.                 boolean found = false;
  853.                 // Try to look in persist non cash balance list
  854.                 for (Dto tempDto : persistNonCashBalanceList) {
  855.                     if (deviceMerchantId.equals(tempDto.getLong("deviceMerchantId")) &&
  856.                             cardType.equals(tempDto.getString("cardType")) &&
  857.                             bankCardCode.equals(tempDto.getString("bankCardCode"))) {
  858.                         found = true;
  859.                         tempDto.put("amountPayment",
  860.                                 new Calc(tempDto.getDouble("amountPayment")).add(paymentAmount).doubleValue());
  861.                         break;
  862.                     }
  863.                 }
  864.                
  865.                 // Haven't found yet
  866.                 if (!found) {
  867.                     // Try to look in merge non cash balance list
  868.                     for (Dto tempDto : mergeNonCashBalanceList) {
  869.                         if (deviceMerchantId.equals(tempDto.getLong("deviceMerchantId")) &&
  870.                                 cardType.equals(tempDto.getString("cardType")) &&
  871.                                 bankCardCode.equals(tempDto.getString("bankCardCode"))) {
  872.                             found = true;
  873.                             tempDto.put("amountPayment",
  874.                                     new Calc(tempDto.getDouble("amountPayment")).add(paymentAmount).doubleValue());
  875.                             break;
  876.                         }
  877.                     }
  878.                 }
  879.                
  880.                 // Have been looked it in lists, but still haven't found it yet. Lastly, should check in database,
  881.                 // to decide whether should be persist or merge it.
  882.                 if (!found) {
  883.                     // Check is Non Cash Balance exists
  884.                     Dto nonCashBalanceDto = new Dto();
  885.                     nonCashBalanceDto.put("tenantId", tenantLoginId);
  886.                     nonCashBalanceDto.put("ouId", ouId);
  887.                     nonCashBalanceDto.put("nonCashBalanceDate", docDate);
  888.                     nonCashBalanceDto.put("deviceMerchantId", deviceMerchantId);
  889.                     nonCashBalanceDto.put("cardType", cardType);
  890.                     nonCashBalanceDto.put("bankCardCode", bankCardCode);
  891.                     nonCashBalanceDto = isNonCashBalanceExistsByIndex.execute(nonCashBalanceDto);
  892.  
  893.                     // Preparing for Non Cash Balance
  894.                     if (nonCashBalanceDto.getBoolean("exists")) {
  895.                         // merge non cash balance
  896.                         Dto mergeNonCashBalanceDto = nonCashBalanceDto.getDto("nonCashBalanceDto");
  897.                         mergeNonCashBalanceDto.put("amountPayment",
  898.                                 new Calc(mergeNonCashBalanceDto.getDouble("amountPayment")).add(paymentAmount).doubleValue());
  899.                         prepareUpdateAudit(mergeNonCashBalanceDto, userLoginId, datetime);
  900.  
  901.                         mergeNonCashBalanceList.add(mergeNonCashBalanceDto);
  902.                     } else {
  903.                         // persist non cash balance
  904.                         Dto persistNonCashBalanceDto = new Dto();
  905.                         persistNonCashBalanceDto.put("tenantId", tenantLoginId);
  906.                         persistNonCashBalanceDto.put("ouId", ouId);
  907.                         persistNonCashBalanceDto.put("nonCashBalanceDate", docDate);
  908.                         persistNonCashBalanceDto.put("deviceMerchantId", deviceMerchantId);
  909.                         persistNonCashBalanceDto.put("cardType", cardType);
  910.                         persistNonCashBalanceDto.put("bankCardCode", bankCardCode);
  911.                         persistNonCashBalanceDto.put("currPaymentCode", currPaymentCode);
  912.                         persistNonCashBalanceDto.put("amountPayment", paymentAmount);
  913.                         prepareInsertAudit(persistNonCashBalanceDto, userLoginId, datetime);
  914.                         prepareUpdateAudit(persistNonCashBalanceDto, userLoginId, datetime);
  915.  
  916.                         persistNonCashBalanceList.add(persistNonCashBalanceDto);
  917.                     }
  918.                 }
  919.             }
  920.         }
  921.  
  922.         // Preparing Cash Balance for Refund
  923.         // Check is Cash Balance exists
  924.         Dto cashBalanceDto = new Dto();
  925.         cashBalanceDto.put("tenantId", tenantLoginId);
  926.         cashBalanceDto.put("ouId", ouId);
  927.         cashBalanceDto.put("cashBankId", getCashBankCodeForCash(tenantLoginId, currCode).getLong("id"));
  928.         cashBalanceDto.put("cashBankDate", docDate);
  929.         cashBalanceDto.put("recType", PosBoConstants.RECORD_TYPE_CASH_BALANCE_CREDIT);
  930.         cashBalanceDto = isCashBalanceExistsByIndex.execute(cashBalanceDto);
  931.  
  932.         // Preparing for Cash Balance
  933.         if (cashBalanceDto.getBoolean("exists")) {
  934.             // merge cash balance
  935.             Dto mergeCashBalanceDto = cashBalanceDto.getDto("cashBalanceDto");
  936.             mergeCashBalanceDto.put("amount", new Calc(Double.valueOf(mergeCashBalanceDto.get("amount").toString())).add(totalRefund).doubleValue());
  937.             prepareUpdateAudit(mergeCashBalanceDto, userLoginId, datetime);
  938.  
  939.             mergeCashBalanceList.add(mergeCashBalanceDto);
  940.         } else {
  941.             // persist cash balance
  942.             Dto persistCashBalanceDto = new Dto();
  943.             persistCashBalanceDto.put("tenantId", tenantLoginId);
  944.             persistCashBalanceDto.put("ouId", ouId);
  945.             persistCashBalanceDto.put("cashBankId", getCashBankCodeForCash(tenantLoginId, currCode).getLong("id"));
  946.             persistCashBalanceDto.put("cashBankDate", docDate);
  947.             persistCashBalanceDto.put("recType", PosBoConstants.RECORD_TYPE_CASH_BALANCE_CREDIT);
  948.             persistCashBalanceDto.put("currCode", currCode);
  949.             persistCashBalanceDto.put("amount", totalRefund);
  950.             prepareInsertAudit(persistCashBalanceDto, userLoginId, datetime);
  951.             prepareUpdateAudit(persistCashBalanceDto, userLoginId, datetime);
  952.  
  953.             persistCashBalanceList.add(persistCashBalanceDto);
  954.         }
  955.  
  956.         // Get Activity Gl Id for Rounding
  957.         Dto activityGlIdForChargeDto = new Dto();
  958.         activityGlIdForChargeDto.put("tenantId", tenantLoginId);
  959.         activityGlIdForChargeDto.put("parameterCode", PosBoConstants.PARAM_ACTIVITY_GL_CODE_ROUNDING);
  960.         activityGlIdForChargeDto = findSystemConfigByParamCode.execute(activityGlIdForChargeDto);
  961.         String activityGlIdForRoundingString = activityGlIdForChargeDto.getString("value");
  962.         Long activityGlIdForRounding = new Long(activityGlIdForRoundingString);
  963.  
  964.         // Create Pos Add Cost for Rounding
  965.         PointOfSalesAddCost posAddCost = new PointOfSalesAddCost();
  966.         posAddCost.setTenantId(tenantLoginId);
  967.         posAddCost.setLineNo(0L);
  968.         posAddCost.setRefDocTypeId(docTypeId);
  969.         posAddCost.setActivityGlId(activityGlIdForRounding);
  970.         posAddCost.setCurrCode(currCode);
  971.         posAddCost.setPercentage(0D);
  972.         posAddCost.setBaseAmount(totalAmount);
  973.         posAddCost.setTaxId(GeneralConstants.NULL_REF_VALUE_LONG);
  974.         posAddCost.setTaxPercentage(0D);
  975.         posAddCost.setAddAmount(roundingAmount);
  976.         posAddCost.setTaxAmount(0D);
  977.         posAddCost.setRemark(GeneralConstants.EMPTY_VALUE);
  978.  
  979.         Dto persistPosAddCostDto = new Dto(posAddCost);
  980.         prepareInsertAudit(persistPosAddCostDto, userLoginId, datetime);
  981.         prepareUpdateAudit(persistPosAddCostDto, userLoginId, datetime);
  982.         persistAddCostList.add(persistPosAddCostDto);
  983.  
  984.         // Get DB Version in Parameter
  985.         Dto dbVersionDto = new Dto();
  986.         dbVersionDto.put("tenantId", tenantLoginId);
  987.         dbVersionDto.put("parameterCode", MasterConstants.PARAMETER_CODE_DB_VERSION);
  988.         dbVersionDto = findSystemConfigByParamCode.execute(dbVersionDto);
  989.         String dbVersionString = dbVersionDto.getString("value");
  990.  
  991.         // Preparing for Log Transaction Header
  992.         TrxLog trxLog = new TrxLog();
  993.         trxLog.setTenantId(tenantLoginId);
  994.         trxLog.setDocTypeId(docTypeId);
  995.         trxLog.setDocNo(docNo);
  996.         trxLog.setDocDate(docDate);
  997.         trxLog.setOuId(ouId);
  998.         trxLog.setDbVersion(dbVersionString);
  999.         trxLog.setTrfDatetime(GeneralConstants.EMPTY_VALUE);
  1000.         trxLog.setTrfUserId(GeneralConstants.NULL_REF_VALUE_LONG);
  1001.         trxLog.setTrfCount(0D);
  1002.         trxLog.setFileCount(0D);
  1003.         trxLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1004.         trxLog.setFlagSync(GeneralConstants.NO);
  1005.         trxLog.setSyncDatetime(GeneralConstants.EMPTY_VALUE);
  1006.         trxLog.setFlagSync(GeneralConstants.NO);
  1007.         trxLog.setSyncDatetime(GeneralConstants.EMPTY_VALUE);
  1008.         Dto persistTrxLogDto = new Dto(trxLog);
  1009.         prepareInsertAudit(persistTrxLogDto, userLoginId, datetime);
  1010.         prepareUpdateAudit(persistTrxLogDto, userLoginId, datetime);
  1011.  
  1012.         // Set persist Dto to inputDto
  1013.         inputDto.putDto("persistPosDto", persistPosDto);
  1014.         inputDto.putList("persistPosItemList", persistPosItemList);
  1015.         inputDto.putList("persistPosTaxList", persistPosTaxList);
  1016.         inputDto.putList("persistCashList", persistCashList);
  1017.         inputDto.putList("persistNonCashList", persistNonCashList);
  1018.         inputDto.putList("persistAddCostList", persistAddCostList);
  1019.         inputDto.putList("mergeProductBalanceStockList", new ArrayList<Dto>(mergeProductBalanceStockList.values()));
  1020.         inputDto.putList("persistCashBalanceList", persistCashBalanceList);
  1021.         inputDto.putList("mergeCashBalanceList", mergeCashBalanceList);
  1022.         inputDto.putList("persistNonCashBalanceList", persistNonCashBalanceList);
  1023.         inputDto.putList("mergeNonCashBalanceList", mergeNonCashBalanceList);
  1024.         inputDto.putDto("persistTrxLogDto", persistTrxLogDto);
  1025.  
  1026.         return null;
  1027.     }
  1028.  
  1029.     private void updateProductBalanceStock(Long docTypeId, String docNo, String docDate,
  1030.             Long ouId, Long userLoginId, Long tenantLoginId, String datetime,
  1031.             Double qty,
  1032.             Dto mergeProductBalanceStockDto) {
  1033.        
  1034.         Dto productBalanceStockDto = mergeProductBalanceStockDto.getDto("productBalanceStock");
  1035.         // Product Balance
  1036.         // Count qty opname = qty Opname - qty Item
  1037.         Double qtyOpname = new Calc(productBalanceStockDto.getDouble("qty")).subtract(qty).doubleValue();
  1038.         Double newQtyRealization = new Calc(productBalanceStockDto.getDouble("qtyRealization")).add(qty).doubleValue();
  1039.        
  1040.         // Update qty at product balance stock
  1041.         productBalanceStockDto.put("qty", qtyOpname);
  1042.         productBalanceStockDto.put("qtyRealization", newQtyRealization);
  1043.  
  1044.         Dto logProductBalanceStockDto;
  1045.         if ((logProductBalanceStockDto = mergeProductBalanceStockDto.getDto("logProductBalanceStock")) == null) {
  1046.             // Create Log for product balance stock
  1047.             LogProductBalanceStock logProductBalanceStock = new LogProductBalanceStock();
  1048.             logProductBalanceStock.setTenantId(tenantLoginId);
  1049.             logProductBalanceStock.setOuId(ouId);
  1050.             // logProductBalanceStock.setRefId(posItemId);
  1051.             logProductBalanceStock.setPartnerId(PosBoConstants.PARTNER_RETAIL);
  1052.             logProductBalanceStock.setDocTypeId(docTypeId);
  1053.             logProductBalanceStock.setDocNo(docNo);
  1054.             logProductBalanceStock.setDocDate(docDate);
  1055.             logProductBalanceStock.setProductId(productBalanceStockDto.getLong("productId"));
  1056.             logProductBalanceStock.setWarehouseId(productBalanceStockDto.getLong("warehouseId"));
  1057.             logProductBalanceStock.setProductBalanceId(productBalanceStockDto.getLong("productBalanceId"));
  1058.             logProductBalanceStock.setProductStatus(productBalanceStockDto.getString("productStatus"));
  1059.             logProductBalanceStock.setBaseUomId(productBalanceStockDto.getLong("baseUomId"));
  1060.             logProductBalanceStock.setQty(new Calc(-1).multiply(qty).doubleValue());
  1061.  
  1062.             logProductBalanceStockDto = new Dto(logProductBalanceStock);
  1063.             prepareInsertAudit(logProductBalanceStockDto, userLoginId, datetime);
  1064.             prepareUpdateAudit(logProductBalanceStockDto, userLoginId, datetime);
  1065.  
  1066.             prepareUpdateAudit(logProductBalanceStockDto, userLoginId, datetime);
  1067.             mergeProductBalanceStockDto.putDto("logProductBalanceStock", logProductBalanceStockDto);
  1068.         } else {
  1069.             // Update the qty of log for product balance stock
  1070.             logProductBalanceStockDto.put("qty", new Calc(logProductBalanceStockDto.getDouble("qty")).subtract(qty).doubleValue());
  1071.         }
  1072.     }
  1073.  
  1074.     private Dto getMergeProductBalanceStock(Long tenantLoginId, Long warehouseId, Long productId,
  1075.                                              Long productBalanceId, String productStatus,
  1076.                                              Map<Long, Dto> mergeProductBalanceStockList) throws Exception {
  1077.        
  1078.         // Validation product balance id must exists
  1079.         Dto productBalanceDto = new Dto();
  1080.         productBalanceDto.put("id", productBalanceId);
  1081.         productBalanceDto = findProductBalanceById.execute(productBalanceDto);
  1082.  
  1083.         // Validation product id = product id in product balance id
  1084.         if (!productBalanceDto.getLong("productId").equals(productId)) {
  1085.             throw new CoreException(PosBoExceptionConstants.INCONSISTENT_PRODUCT_ID);
  1086.         }
  1087.  
  1088.         // Validation product balance stock must exists
  1089.         Dto productBalanceStockDto = new Dto();
  1090.         productBalanceStockDto.put("tenantId", tenantLoginId);
  1091.         productBalanceStockDto.put("warehouseId", warehouseId);
  1092.         productBalanceStockDto.put("productId", productId);
  1093.         productBalanceStockDto.put("productBalanceId", productBalanceId);
  1094.         productBalanceStockDto.put("productStatus", productStatus);
  1095.         productBalanceStockDto = findProductBalanceStockByIndex.execute(productBalanceStockDto);
  1096.        
  1097.         Dto mergeProductBalanceStockDto = mergeProductBalanceStockList.get(productBalanceStockDto.getLong("id"));
  1098.         if (mergeProductBalanceStockDto == null) {
  1099.             productBalanceStockDto.put("qtyRealization", 0D);
  1100.             mergeProductBalanceStockDto = new Dto().put("productBalanceStock", productBalanceStockDto);
  1101.             mergeProductBalanceStockList.put(productBalanceStockDto.getLong("id"), mergeProductBalanceStockDto);
  1102.         }
  1103.        
  1104.         return mergeProductBalanceStockDto;
  1105.     }
  1106.  
  1107.     @SuppressWarnings("unchecked")
  1108.     @Override
  1109.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  1110.         Dto persistPosDto = inputDto.getDto("persistPosDto");
  1111.         List<Dto> persistPosItemList = inputDto.getList("persistPosItemList");
  1112.         List<Dto> persistPosTaxList = inputDto.getList("persistPosTaxList");
  1113.         List<Dto> persistCashList = inputDto.getList("persistCashList");
  1114.         List<Dto> persistNonCashList = inputDto.getList("persistNonCashList");
  1115.         List<Dto> persistAddCostList = inputDto.getList("persistAddCostList");
  1116.         List<Dto> mergeProductBalanceStockList = inputDto.getList("mergeProductBalanceStockList");
  1117.         List<Dto> persistCashBalanceList = inputDto.getList("persistCashBalanceList");
  1118.         List<Dto> mergeCashBalanceList = inputDto.getList("mergeCashBalanceList");
  1119.         List<Dto> persistNonCashBalanceList = inputDto.getList("persistNonCashBalanceList");
  1120.         List<Dto> mergeNonCashBalanceList = inputDto.getList("mergeNonCashBalanceList");
  1121.         Dto persistTrxLogDto = inputDto.getDto("persistTrxLogDto");
  1122.         Long userLoginId = inputDto.getLong("userLoginId");
  1123.         String datetime = inputDto.getString("datetime");
  1124.  
  1125.         // Variable for Create logging
  1126.         Dto posDto = null;
  1127.         List<Dto> persistTrxItemLogList = new ArrayList<Dto>();
  1128.         TrxItemLog trxItemLog = null;
  1129.         Dto persistTrxItemLogDto = null;
  1130.  
  1131.         // Insert header data
  1132.         PointOfSales pos = GsonUtil.fromDto(persistPosDto, PointOfSales.class);
  1133.         posDao.persist(pos);
  1134.         posDto = new Dto(pos);
  1135.  
  1136.         // Preparing Log for POS Item
  1137.         trxItemLog = new TrxItemLog();
  1138.         trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS);
  1139.         trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1140.         trxItemLog.setDataLog(posDto.toString());
  1141.         trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1142.  
  1143.         persistTrxItemLogDto = new Dto(trxItemLog);
  1144.         prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1145.         prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1146.         persistTrxItemLogList.add(persistTrxItemLogDto);
  1147.  
  1148.         Long posId = pos.getId();
  1149.  
  1150.         // Persist detail item
  1151.         for (Dto persistPosItemDto : persistPosItemList) {
  1152.             List<Dto> persistPosAssemblyProductList = (List<Dto>) persistPosItemDto.remove("persistPosAssemblyProductList");
  1153.            
  1154.             PointOfSalesItem posItem = GsonUtil.fromDto(persistPosItemDto, PointOfSalesItem.class);
  1155.             posItem.setPosId(posId);
  1156.             posItemDao.persist(posItem);
  1157.  
  1158.             Dto posItemDto = new Dto(posItem);
  1159.  
  1160.             // Preparing Log for POS Item
  1161.             trxItemLog = new TrxItemLog();
  1162.             trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS_ITEM);
  1163.             trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1164.             trxItemLog.setDataLog(posItemDto.toString());
  1165.             trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1166.  
  1167.             persistTrxItemLogDto = new Dto(trxItemLog);
  1168.             prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1169.             prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1170.             persistTrxItemLogList.add(persistTrxItemLogDto);
  1171.            
  1172.             Long posItemId = posItem.getId();
  1173.             if (persistPosAssemblyProductList != null && !persistPosAssemblyProductList.isEmpty()) {
  1174.                 // Persist detail assembly product
  1175.                 for (Dto persistPosAssemblyProductDto : persistPosAssemblyProductList) {
  1176.                     PointOfSalesAssemblyProduct posAssemblyProduct = GsonUtil.fromDto(persistPosAssemblyProductDto, PointOfSalesAssemblyProduct.class);
  1177.                     posAssemblyProduct.setPosItemId(posItemId);
  1178.                     posAssemblyProductDao.persist(posAssemblyProduct);
  1179.  
  1180.                     Dto posAssemblyProductDto = new Dto(posAssemblyProduct);
  1181.                     posAssemblyProductDto.putLong("refLineNo", posItem.getLineNo()); // Set ref line no, that will be needed in upload data trx
  1182.                     posAssemblyProductDto.putLong("posId", posId); // Set posId, that will be needed in upload data trx
  1183.  
  1184.                     // Preparing Log for POS Item
  1185.                     trxItemLog = new TrxItemLog();
  1186.                     trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS_ASSEMBLY_PRODUCT);
  1187.                     trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1188.                     trxItemLog.setDataLog(posAssemblyProductDto.toString());
  1189.                     trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1190.  
  1191.                     persistTrxItemLogDto = new Dto(trxItemLog);
  1192.                     prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1193.                     prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1194.                     persistTrxItemLogList.add(persistTrxItemLogDto);
  1195.                 }
  1196.             }
  1197.         }
  1198.  
  1199.         // Persist tax list
  1200.         for (Dto persistPosTaxDto : persistPosTaxList) {
  1201.             PointOfSalesTax posTax = GsonUtil.fromDto(persistPosTaxDto, PointOfSalesTax.class);
  1202.             posTax.setPosId(posId);
  1203.             posTaxDao.persist(posTax);
  1204.  
  1205.             Dto posTaxDto = new Dto(posTax);
  1206.  
  1207.             // Preparing Log for POS Tax
  1208.             trxItemLog = new TrxItemLog();
  1209.             trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS_TAX);
  1210.             trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1211.             trxItemLog.setDataLog(posTaxDto.toString());
  1212.             trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1213.  
  1214.             persistTrxItemLogDto = new Dto(trxItemLog);
  1215.             prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1216.             prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1217.             persistTrxItemLogList.add(persistTrxItemLogDto);
  1218.  
  1219.         }
  1220.  
  1221.         // Persist cash list
  1222.         for (Dto persistCashDto : persistCashList) {
  1223.             PointOfSalesCashPayment posCashPayment = GsonUtil.fromDto(persistCashDto, PointOfSalesCashPayment.class);
  1224.             posCashPayment.setPosId(posId);
  1225.             posCashPaymentDao.persist(posCashPayment);
  1226.  
  1227.             Dto cashDto = new Dto(posCashPayment);
  1228.  
  1229.             // Preparing Log for POS Cash Payment
  1230.             trxItemLog = new TrxItemLog();
  1231.             trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS_CASH_PAYMENT);
  1232.             trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1233.             trxItemLog.setDataLog(cashDto.toString());
  1234.             trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1235.  
  1236.             persistTrxItemLogDto = new Dto(trxItemLog);
  1237.             prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1238.             prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1239.             persistTrxItemLogList.add(persistTrxItemLogDto);
  1240.         }
  1241.  
  1242.         // Persist non cash list
  1243.         for (Dto persistNonCashDto : persistNonCashList) {
  1244.             Dto posAddCostDto = (Dto) persistNonCashDto.remove("posAddCost");
  1245.            
  1246.             PointOfSalesNonCashPayment posNonCashPayment = GsonUtil.fromDto(persistNonCashDto, PointOfSalesNonCashPayment.class);
  1247.             posNonCashPayment.setPosId(posId);
  1248.             posNonCashPaymentDao.persist(posNonCashPayment);
  1249.            
  1250.             PointOfSalesAddCost posAddCost = GsonUtil.fromDto(posAddCostDto, PointOfSalesAddCost.class);
  1251.             posAddCost.setRefId(posNonCashPayment.getId());
  1252.             posAddCost.setPosId(posId);
  1253.             posAddCostDao.persist(posAddCost);
  1254.  
  1255.             Dto nonCashDto = new Dto(posNonCashPayment);
  1256.             Dto addCostDto = new Dto(posAddCost);
  1257.  
  1258.             // Preparing Log for POS Non Cash Payment
  1259.             trxItemLog = new TrxItemLog();
  1260.             trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS_NON_CASH_PAYMENT);
  1261.             trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1262.             trxItemLog.setDataLog(nonCashDto.toString());
  1263.             trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1264.  
  1265.             persistTrxItemLogDto = new Dto(trxItemLog);
  1266.             prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1267.             prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1268.             persistTrxItemLogList.add(persistTrxItemLogDto);
  1269.  
  1270.             // Preparing Log for POS Additional Cost
  1271.             trxItemLog = new TrxItemLog();
  1272.             trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS_ADD_COST);
  1273.             trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1274.             trxItemLog.setDataLog(addCostDto.toString());
  1275.             trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1276.  
  1277.             persistTrxItemLogDto = new Dto(trxItemLog);
  1278.             prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1279.             prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1280.             persistTrxItemLogList.add(persistTrxItemLogDto);
  1281.         }
  1282.  
  1283.         // Persist additional cost
  1284.         for (Dto persistAddCostDto : persistAddCostList) {
  1285.             PointOfSalesAddCost posAddCost = GsonUtil.fromDto(persistAddCostDto, PointOfSalesAddCost.class);
  1286.             posAddCost.setPosId(posId);
  1287.             posAddCost.setRefId(posId);
  1288.             posAddCostDao.persist(posAddCost);
  1289.            
  1290.             Dto addCostDto = new Dto(posAddCost);
  1291.  
  1292.             // Preparing Log for POS Additional Cost
  1293.             trxItemLog = new TrxItemLog();
  1294.             trxItemLog.setTypeData(PosBoConstants.TYPE_DATA_POS_ADD_COST);
  1295.             trxItemLog.setModeLog(PosBoConstants.LOG_MODE_ADD);
  1296.             trxItemLog.setDataLog(addCostDto.toString());
  1297.             trxItemLog.setRemark(GeneralConstants.EMPTY_VALUE);
  1298.  
  1299.             persistTrxItemLogDto = new Dto(trxItemLog);
  1300.             prepareInsertAudit(persistTrxItemLogDto, userLoginId, datetime);
  1301.             prepareUpdateAudit(persistTrxItemLogDto, userLoginId, datetime);
  1302.             persistTrxItemLogList.add(persistTrxItemLogDto);
  1303.         }
  1304.  
  1305.         QueryBuilder builder;
  1306.         Query q;
  1307.         // Merge Product Balance Stock
  1308.         for (Dto productBalanceStockDto : mergeProductBalanceStockList) {
  1309.             /*ProductBalanceStock productBalanceStock = GsonUtil.fromDto(productBalanceStockDto.getDto("productBalanceStock"), ProductBalanceStock.class);
  1310.             productBalanceStock = productBalanceStockDao.merge(productBalanceStock.getId(), productBalanceStock);*/
  1311.            
  1312.             //log.info(">>>>PRODUCTBALANCESTOCKDTO :"+productBalanceStockDto);
  1313.            
  1314.             builder = new QueryBuilder();
  1315.             builder.add("UPDATE ").add(ProductBalanceStock.TABLE_NAME)
  1316.                    .add(" SET qty = qty - :qtyRealization, ")
  1317.                    .add(" update_datetime = :updateDatetime, ")
  1318.                    .add(" update_user_id = :updateUserId, ")
  1319.                    .add(" version = version+1 ")
  1320.                    .add(" WHERE product_balance_stock_id = :id");
  1321.  
  1322.             q = productBalanceStockDao.createNativeQuery(builder.toString());
  1323.  
  1324.  
  1325.             q.setParameter("updateDatetime", productBalanceStockDto.getDto("productBalanceStock").getString("updateDateTime"));
  1326.             q.setParameter("updateUserId", productBalanceStockDto.getDto("productBalanceStock").getLong("updateUserId"));
  1327.             q.setParameter("qtyRealization", Double.valueOf(productBalanceStockDto.getDto("productBalanceStock").getString("qtyRealization")));
  1328.             q.setParameter("id", productBalanceStockDto.getDto("productBalanceStock").getLong("id"));
  1329.  
  1330.             //log.info("querynya:::"+ builder.toString());
  1331.  
  1332.             q.executeUpdate();
  1333.            
  1334.             // Persist Log Product Balance Stock
  1335.             LogProductBalanceStock logProductBalanceStock = GsonUtil.fromDto(productBalanceStockDto.getDto("logProductBalanceStock"),
  1336.                     LogProductBalanceStock.class);
  1337.             logProductBalanceStock.setRefId(posId);
  1338.             logProductBalanceStockDao.persist(logProductBalanceStock);
  1339.         }
  1340.  
  1341.         // Merge Cash Balance
  1342.         for (Dto cashBalanceDto : mergeCashBalanceList) {
  1343.             CashBalance cashBalance = GsonUtil.fromDto(cashBalanceDto, CashBalance.class);
  1344.             cashBalance = cashBalanceDao.merge(cashBalance.getId(), cashBalance);
  1345.         }
  1346.  
  1347.         // Persist Cash Balance
  1348.         for (Dto cashBalanceDto : persistCashBalanceList) {
  1349.             CashBalance cashBalance = GsonUtil.fromDto(cashBalanceDto, CashBalance.class);
  1350.             cashBalanceDao.persist(cashBalance);
  1351.         }
  1352.  
  1353.         // Merge Non Cash Balance
  1354.         for (Dto nonCashBalanceDto : mergeNonCashBalanceList) {
  1355.             NonCashBalance nonCashBalance = GsonUtil.fromDto(nonCashBalanceDto, NonCashBalance.class);
  1356.             nonCashBalance = nonCashBalanceDao.merge(nonCashBalance.getId(), nonCashBalance);
  1357.         }
  1358.  
  1359.         // Persist Non Cash Balance
  1360.         for (Dto nonCashBalanceDto : persistNonCashBalanceList) {
  1361.             NonCashBalance nonCashBalance = GsonUtil.fromDto(nonCashBalanceDto, NonCashBalance.class);
  1362.             nonCashBalanceDao.persist(nonCashBalance);
  1363.         }
  1364.  
  1365.         // Create Log Transaction for every list
  1366.  
  1367.         // Persist Trx Log Header
  1368.         TrxLog trxLog = GsonUtil.fromDto(persistTrxLogDto, TrxLog.class);
  1369.         trxLogDao.persist(trxLog);
  1370.         Long trxLogId = trxLog.getId();
  1371.  
  1372.         // Persist Trx Item Log
  1373.         for (Dto persisttrxLogItemDto : persistTrxItemLogList) {
  1374.             TrxItemLog persisttrxItemLog = GsonUtil.fromDto(persisttrxLogItemDto, TrxItemLog.class);
  1375.             persisttrxItemLog.setTrxLogId(trxLogId);
  1376.  
  1377.             trxItemLogDao.persist(persisttrxItemLog);
  1378.         }
  1379.  
  1380.         return posDto;
  1381.     }
  1382.  
  1383.     /**
  1384.      * Get cash bank code for flag cash bank = CASH from a currency
  1385.      *
  1386.      * @param tenantId
  1387.      * @param currencyCode
  1388.      * @return
  1389.      * @throws Exception
  1390.      */
  1391.     @SuppressWarnings("unchecked")
  1392.     private Dto getCashBankCodeForCash(Long tenantId, String currencyCode) throws Exception {
  1393.         Dto inputDtoForGetCashAccountList = new Dto();
  1394.         inputDtoForGetCashAccountList.put("tenantId", tenantId);
  1395.         inputDtoForGetCashAccountList.put("code", GeneralConstants.EMPTY_VALUE);
  1396.         inputDtoForGetCashAccountList.put("name", GeneralConstants.EMPTY_VALUE);
  1397.         inputDtoForGetCashAccountList.put("flagCashBank", MasterConstants.FLAG_CASH);
  1398.         inputDtoForGetCashAccountList.put("bankCode", CommonConstants.ID_NOT_USED.toString());
  1399.         inputDtoForGetCashAccountList.put("currencyCode", currencyCode);
  1400.         inputDtoForGetCashAccountList.put("active", GeneralConstants.YES);
  1401.  
  1402.         // Processing
  1403.         Dto outputDtoForGetCashAccountList = getCashBankListAdvance.execute(inputDtoForGetCashAccountList);
  1404.  
  1405.         List<Dto> cashBankList = outputDtoForGetCashAccountList.getList("cashBankList");
  1406.         if (cashBankList.size() != 0) {
  1407.             return new Dto().put("id", cashBankList.get(0).getLong("id")).put("code", cashBankList.get(0).getString("code"));
  1408.         } else {
  1409.             throw new CoreException(PosBoExceptionConstants.POS_CASH_NOT_FOUND);
  1410.         }
  1411.     }
  1412.  
  1413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement