Advertisement
widana

AddPosTransactionItem

Oct 26th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.92 KB | None | 0 0
  1. package org.jleaf.pos2.bo.postransaction;
  2.  
  3. import java.math.BigDecimal;
  4.  
  5. import org.jleaf.core.BusinessFunction;
  6. import org.jleaf.core.BusinessTransaction;
  7. import org.jleaf.core.CoreException;
  8. import org.jleaf.core.DefaultBusinessTransaction;
  9. import org.jleaf.core.Dto;
  10. import org.jleaf.core.GeneralConstants;
  11. import org.jleaf.core.annotation.ErrorList;
  12. import org.jleaf.core.annotation.Info;
  13. import org.jleaf.core.annotation.InfoIn;
  14. import org.jleaf.core.annotation.InfoOut;
  15. import org.jleaf.pos2.PosConstants;
  16. import org.jleaf.pos2.PosExceptionConstants;
  17. import org.jleaf.pos2.entity.ProductSellPrice;
  18. import org.jleaf.pos2.dao.SellPriceProductDao;
  19. import org.jleaf.pos2.entity.Product;
  20. import org.jleaf.pos2.dao.ProductDao;
  21. import org.jleaf.pos2.entity.Pos;
  22. import org.jleaf.pos2.dao.PosDao;
  23. import org.jleaf.pos2.entity.PosCashier;
  24. import org.jleaf.pos2.entity.PosCashier;
  25. import org.jleaf.pos2.entity.PosItem;
  26. import org.jleaf.pos2.dao.PosItemDao;
  27. import org.jleaf.util.Calc;
  28. import org.jleaf.util.GsonUtil;
  29. import org.jleaf.util.ValidationUtil;
  30. import org.springframework.beans.factory.annotation.Autowired;
  31. import org.springframework.beans.factory.annotation.Qualifier;
  32. import org.springframework.stereotype.Service;
  33.  
  34. /**
  35.  * Business Transactions: AddPosTransaction
  36.  *
  37.  * @author Widana Nur Azis , 26 oct 2016
  38.  */
  39.  
  40. // @formatter:off
  41. @Service
  42. @InfoIn(value = {
  43.         @Info(name = "userLoginId", description = "user login id", type = Long.class),
  44.         @Info(name = "userLoginName", description = "user login name", type = String.class),
  45.         @Info(name = "roleLoginId", description = "role login id", type = Long.class),
  46.         @Info(name = "roleLoginName", description = "role login name", type = String.class),
  47.         @Info(name = "tenantLoginId", description = "tenantLoginId", type = Long.class),
  48.         @Info(name = "datetime", description = "date time", type = String.class),
  49.  
  50.         @Info(name = "posId", description = "docNo", type = String.class),
  51.         @Info(name = "posCashierId", description = "posCashierId", type = Long.class),
  52.         @Info(name = "productCode", description = "productCode", type = String.class),
  53.         @Info(name = "qty", description = "qty", type = Long.class),
  54.         @Info(name = "flgDiscount", description = "flgDiscount", type = String.class),
  55.         @Info(name = "discount", description = "discount", type = Double.class),
  56.         @Info(name = "active", description = "active", type = String.class)
  57.        
  58. })
  59.  
  60. @InfoOut(value = {
  61.         @Info(name = "posItemId", description = "posItemId", type = Long.class),
  62.         @Info(name = "posId", description = "posId", type = Long.class),
  63.         @Info(name = "lineNo,", description = "lineNo", type = Long.class),
  64.         @Info(name = "productCode", description = "productCode", type = String.class),
  65.         @Info(name = "productSerialNumber", description = "productSerialNumber", type = String.class),
  66.         @Info(name = "uomCode", description = "uomCode", type = String.class),
  67.         @Info(name = "qty", description = "qty", type = Long.class),
  68.         @Info(name = "currCode", description = "currCode", type = String.class),
  69.         @Info(name = "grossSellPrice", description = "grossSellPrice", type = Double.class),
  70.         @Info(name = "taxId", description = "taxId", type = Long.class),
  71.         @Info(name = "taxPercentage", description = "taxPercentage", type = Long.class),
  72.         @Info(name = "taxPrice", description = "taxPrice", type = Double.class),
  73.         @Info(name = "promoCode", description = "promoCode", type = String.class),
  74.         @Info(name = "discountPercentage", description = "discountPercentage", type = Long.class),
  75.         @Info(name = "discountAmount", description = "discountAmount", type = Double.class),
  76.         @Info(name = "nettSellPrice", description = "nettSellPrice", type = Double.class),
  77.         @Info(name = "taxAmount", description = "taxAmount", type = Double.class),
  78.         @Info(name = "nettAmountItem", description = "nettAmountItem", type = Double.class),
  79.         @Info(name = "remark", description = "remark", type = String.class),
  80.         @Info(name = "flgDiscount", description = "flgDiscount", type = String.class),
  81.         @Info(name = "flgTaxAmount", description = "flgTaxAmount", type = String.class),
  82.  
  83.         @Info(name = "createUsername", description = "create username", type = String.class),
  84.         @Info(name = "updateUsername", description = "update username", type = String.class),
  85.         @Info(name = "active", description = "active", type = String.class, required = true),
  86.         @Info(name = "activeDateTime", description = "active date time", type = String.class),
  87.         @Info(name = "nonActiveDateTime", description = "non active date time", type = String.class),
  88.         @Info(name = "createDateTime", description = "create date time", type = String.class),
  89.         @Info(name = "updateDateTime", description = "update date time", type = String.class),
  90.         @Info(name = "version", description = "version", type = Long.class)
  91. })
  92. // @formatter:on
  93. @ErrorList(errorKeys = {
  94.         PosExceptionConstants.DISCOUNT_MUST_BE_GREATER_THAN_ZERO
  95. })
  96.  
  97. public class AddPosTransactionItem extends DefaultBusinessTransaction implements BusinessTransaction {
  98.  
  99.     @Autowired
  100.     PosItemDao posItemDao;
  101.  
  102.     @Autowired
  103.     @Qualifier("findPosById")
  104.     BusinessFunction findPosById;
  105.    
  106.     @Autowired
  107.     @Qualifier("findPosCashierById")
  108.     BusinessFunction findPosCashierById;
  109.    
  110.     @Autowired
  111.     @Qualifier("findProductById")
  112.     BusinessFunction findProductById;
  113.  
  114.     @Autowired
  115.     @Qualifier("findProductSellPriceById")
  116.     BusinessFunction findProductSellPriceById;
  117.  
  118.     @Override
  119.     public String getDescription() {
  120.         // TODO Auto-generated method stub
  121.         return "AddPosTransactionItem";
  122.     }
  123.  
  124.     @SuppressWarnings("unchecked")
  125.     @Override
  126.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  127.         // TODO Auto-generated method stub
  128.        
  129.         ValidationUtil.valBlankOrNull(inputDto, "posId");
  130.         ValidationUtil.valBlankOrNull(inputDto, "posCashierId");
  131.         ValidationUtil.valBlankOrNull(inputDto, "productCode");
  132.         ValidationUtil.valBlankOrNull(inputDto, "qty");
  133.  
  134.         ValidationUtil.valDtoContainsKey(inputDto, "posId");
  135.         ValidationUtil.valDtoContainsKey(inputDto, "posCashierId");
  136.         ValidationUtil.valDtoContainsKey(inputDto, "flgDiscount");
  137.         ValidationUtil.valDtoContainsKey(inputDto, "qty");
  138.         ValidationUtil.valDtoContainsKey(inputDto, "qty");
  139.        
  140.         Long posId = inputDto.getLong("posId");
  141.         Long posCashierId = inputDto.getLong("posCashierId");
  142.         String productCode = inputDto.getString("productCode");
  143.         Long qty = inputDto.getLong("qty");
  144.         String flgDiscount = inputDto.getString("flgDiscount");
  145.         Double discountAmoutOrPercentage = inputDto.getDouble("discount");
  146.         String flgTaxAmount = PosConstants.YES; // ambil dari system config
  147.         Long taxPercentage = 10L; // ambil dari system config
  148.         Double nettSellPrice = 0D;
  149.        
  150.         findPosById.execute(new Dto().put("id", posId));
  151.        
  152.         findPosCashierById.execute(new Dto().put("id", posCashierId));
  153.        
  154.         Dto findProductByIdDto =  findProductById.execute(new Dto().put("productCode", productCode));
  155.        
  156.         Dto findProductSellPriceByIdDto = findProductSellPriceById.execute(new Dto().put("productCode", productCode));
  157.        
  158.         Double grossSellPrice = findProductSellPriceByIdDto.getDouble("grossSellPrice");
  159.  
  160.         Dto addPosTransactionItemDto = new Dto();
  161.  
  162.         BigDecimal sellPriceIncludeTaxBigDecimal = new BigDecimal((100 / (100 + taxPercentage) * grossSellPrice));
  163.         BigDecimal sellPriceIncludeTaxBigDecimalAfterScale = sellPriceIncludeTaxBigDecimal.setScale(0, BigDecimal.ROUND_UP);
  164.         Double sellPriceIncludeTax = sellPriceIncludeTaxBigDecimalAfterScale.doubleValue();
  165.         Double taxPrice = grossSellPrice - sellPriceIncludeTax;
  166.  
  167.         if(flgDiscount.equals("PCT")){
  168.             // nett sell price if flg discount PCT (grossSellPrice * ((100 - discountPercentage) / 100))
  169.             ValidationUtil.valBlankOrNull(inputDto, "discount");
  170.             ValidationUtil.valDtoContainsKey(inputDto, "discount");
  171.            
  172.             if(inputDto.getDouble("discount") <= PosConstants.ZERO_VALUE_DOUBEL){
  173.                 throw new CoreException(PosExceptionConstants.DISCOUNT_MUST_BE_GREATER_THAN_ZERO , PosConstants.ZERO_VALUE_DOUBEL);
  174.             }
  175.            
  176.             nettSellPrice = new Calc(grossSellPrice)
  177.                     .multiply(new Calc(100).subtract(discountAmoutOrPercentage))
  178.                     .divide(100, 0, BigDecimal.ROUND_DOWN).doubleValue();
  179.  
  180.             addPosTransactionItemDto.put("discountPercentage", discountAmoutOrPercentage);
  181.             addPosTransactionItemDto.put("discountAmount", nettSellPrice);
  182.  
  183.         } else if(flgDiscount.equals("AMT")) {
  184.             // nett sell price if flg discount AMT (grossSellPrice - discountAmoutOrPercentage)
  185.             ValidationUtil.valBlankOrNull(inputDto, "discount");
  186.             ValidationUtil.valDtoContainsKey(inputDto, "discount");
  187.  
  188.             if(inputDto.getDouble("discount") <= PosConstants.ZERO_VALUE_DOUBEL){
  189.                 throw new CoreException(PosExceptionConstants.DISCOUNT_MUST_BE_GREATER_THAN_ZERO , PosConstants.ZERO_VALUE_DOUBEL);
  190.             }
  191.            
  192.             nettSellPrice = new Calc(grossSellPrice).subtract(discountAmoutOrPercentage).doubleValue();
  193.  
  194.             addPosTransactionItemDto.put("discountPercentage", PosConstants.ZERO_VALUE_DOUBEL);
  195.             addPosTransactionItemDto.put("discountAmount", discountAmoutOrPercentage);
  196.  
  197.         } else  {
  198.             // nett sell price if there no discount amount or percentage else nettSellPrice = taxPrice
  199.             nettSellPrice = taxPrice;
  200.            
  201.             addPosTransactionItemDto.put("discountPercentage", PosConstants.ZERO_VALUE_DOUBEL);
  202.             addPosTransactionItemDto.put("discountAmount", PosConstants.ZERO_VALUE_DOUBEL);
  203.  
  204.         }
  205.  
  206.         Double taxAmount = new Calc(qty).multiply(taxPrice).doubleValue();
  207.         Double nettAmountItem = new Calc(qty).multiply(nettSellPrice).doubleValue();
  208.        
  209.         addPosTransactionItemDto.put("posId", posId);
  210.         addPosTransactionItemDto.put("lineNo", 1);
  211.         addPosTransactionItemDto.put("productCode", productCode);
  212.         addPosTransactionItemDto.put("productSerailNumber", PosConstants.EMPTY_STRING);
  213.         addPosTransactionItemDto.put("uomCode", findProductByIdDto.getString("baseUomCode"));
  214.         addPosTransactionItemDto.put("qty", qty);
  215.         addPosTransactionItemDto.put("currCode", "IDR");
  216.         addPosTransactionItemDto.put("grossSellPrice", grossSellPrice);
  217.         addPosTransactionItemDto.put("flgTaxAmount", flgTaxAmount);
  218.         addPosTransactionItemDto.put("taxId", PosConstants.VALUE_DEFAULT);
  219.         addPosTransactionItemDto.put("taxPercentage", taxPercentage);
  220.         addPosTransactionItemDto.put("taxPrice", taxPrice);
  221.         addPosTransactionItemDto.put("flgDiscount", flgDiscount);
  222.         addPosTransactionItemDto.put("promoCode", "");
  223.         addPosTransactionItemDto.put("nettSellPrice", nettSellPrice);
  224.         addPosTransactionItemDto.put("taxAmount", taxAmount);
  225.         addPosTransactionItemDto.put("nettAmountItem", nettAmountItem);
  226.         addPosTransactionItemDto.put("remark", "");
  227.        
  228.         inputDto.put("addPosTransactionItemDto", addPosTransactionItemDto);
  229.        
  230.         return null;
  231.     }
  232.  
  233.     @Override
  234.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  235.         // TODO Auto-generated method stub
  236.        
  237.         Dto getAddPosTransactionItemDto = inputDto.getDto("addPosTransactionItemDto");
  238.        
  239.         PosItem entityPosItem = GsonUtil.fromDto(getAddPosTransactionItemDto, PosItem.class);
  240.        
  241.         posItemDao.persist(entityPosItem);
  242.        
  243.         return new Dto(entityPosItem);
  244.     }
  245.  
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement