Advertisement
aadddrr

CalculateAmountFromGrossPriceForXcom

Sep 26th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.78 KB | None | 0 0
  1. package org.jleaf.erp.master.bo.common;
  2.  
  3. import java.math.BigDecimal;
  4.  
  5. import org.jleaf.core.AbstractBusinessFunction;
  6. import org.jleaf.core.BusinessFunction;
  7. import org.jleaf.core.CoreException;
  8. import org.jleaf.core.CoreExceptionConstants;
  9. import org.jleaf.core.Dto;
  10. import org.jleaf.core.GeneralConstants;
  11. import org.jleaf.core.annotation.Info;
  12. import org.jleaf.core.annotation.InfoIn;
  13. import org.jleaf.core.annotation.InfoOut;
  14. import org.jleaf.erp.master.MasterConstantsForXcom;
  15. import org.jleaf.erp.master.MasterExceptionConstants;
  16. import org.jleaf.erp.master.dao.TaxDao;
  17. import org.jleaf.erp.master.entity.Tax;
  18. import org.jleaf.util.Calc;
  19. import org.jleaf.util.ValidationUtil;
  20. import org.jleaf.validator.CommonBusinessValidator;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.stereotype.Service;
  23.  
  24. //@formatter:off
  25. @Service
  26. @InfoIn(value = {
  27.         @Info(name = "grossPrice", description = "gross price", type = Double.class),
  28.         @Info(name = "qty", description = "qty", type = Double.class),
  29.         @Info(name = "discountPercentage", description = "discount percentage", type = Double.class),
  30.         @Info(name = "discountAmount", description = "discount amount", type = Double.class),
  31.         @Info(name = "decimalForRounding", description = "decimal for rounding", type = Integer.class),
  32.         @Info(name = "roundingMode", description = "decimal for rounding", type = Integer.class),
  33.         @Info(name = "flgTaxAmount", description = "flgTaxAmount", type = String.class),
  34.         @Info(name = "taxId", description = "tax id", type = Long.class),
  35.         @Info(name = "flgDisc", description = "Flag tipe discount: [P]ercentage / [A]mount", type = String.class)
  36. })
  37. @InfoOut(value = {
  38.         @Info(name = "grossPrice", description = "gross price", type = Double.class),
  39.         @Info(name = "taxPrice", description = "tax price", type = Double.class),
  40.         @Info(name = "nettPrice", description = "nett price", type = Double.class),
  41.         @Info(name = "grossAmount", description = "gross amount", type = Double.class),
  42.         @Info(name = "taxAmount", description = "tax amount", type = Double.class),
  43.         @Info(name = "nettAmount", description = "nett amount", type = Double.class),
  44.         @Info(name = "taxPercentage", description = "tax percentage", type = Double.class),
  45.         @Info(name = "discountPercentage", description = "discount percentage", type = Double.class),
  46.         @Info(name = "discountAmount", description = "discount amount", type = Double.class)
  47. })
  48. //@formatter:on
  49. public class CalculateAmountFromGrossPriceForXcom extends AbstractBusinessFunction implements BusinessFunction {
  50.    
  51.     @Autowired
  52.     TaxDao taxDao;
  53.  
  54.     @Override
  55.     public Dto execute(Dto inputDto) throws Exception {
  56.         if (inputDto == null) {
  57.             throw new CoreException(CoreExceptionConstants.DTO_CANNOT_NULL);
  58.         }
  59.        
  60.         ValidationUtil.valDtoContainsKey(inputDto, "grossPrice");
  61.         ValidationUtil.valDtoContainsKey(inputDto, "qty");
  62.         ValidationUtil.valDtoContainsKey(inputDto, "discountPercentage");
  63.         ValidationUtil.valDtoContainsKey(inputDto, "discountAmount");
  64.         ValidationUtil.valDtoContainsKey(inputDto, "flgTaxAmount");
  65.         ValidationUtil.valDtoContainsKey(inputDto, "decimalForRounding");
  66.         ValidationUtil.valDtoContainsKey(inputDto, "roundingMode");
  67.         ValidationUtil.valDtoContainsKey(inputDto, "taxId");
  68.         ValidationUtil.valBlankOrNull(inputDto, "flgDisc");
  69.  
  70.         Double grossPrice = inputDto.getDouble("grossPrice");
  71.         Double qty = inputDto.getDouble("qty");
  72.         Double discountPercentage = inputDto.getDouble("discountPercentage");
  73.         Double discountAmount = inputDto.getDouble("discountAmount");
  74.         Integer decimalForRounding = inputDto.getInteger("decimalForRounding");
  75.         Integer roundingMode = inputDto.getInteger("roundingMode");
  76.         String flgTaxAmount = inputDto.getString("flgTaxAmount");
  77.         Long taxId = inputDto.getLong("taxId");
  78.         String flgDisc = inputDto.getString("flgDisc");
  79.        
  80.         Double taxPrice = new Double(0);
  81.         Double nettPrice = new Double(0);
  82.         Double nettAmount = new Double(0);
  83.         Double taxAmount = new Double(0);
  84.         Double taxPercentage = new Double(0);
  85.        
  86.         // OBSOLETE:
  87.         //  Untuk flag tax amount (flag mempengaruhi pajak) : Y / YES  
  88.         //      harus memilih tax nya, jika tidak memilih tax maka error
  89.         //      tax price po = (gross price po – discount) * 100 / (100 + tax percentage value) * (tax percentage value / 100) rounding ke atas sampai 1 rupiah
  90.         //      Nett price po = (gross price po – discount) – tax price
  91.         //  Untuk flag tax amount (flag mempengaruhi pajak) : N / NO   
  92.         //      nett price po = (gross price po – discount)
  93.         //      jika memilih tax, maka tax price = (gross price po – discount) * (tax percentage value / 100) rounding ke atas sampai 1 rupiah
  94.         //      tidak memilih tax maka tax percentage dan tax price = 0
  95.         //  Gross Item Amount = gross price po * qty po
  96.         //  Nett item amount = nett price po * qty po  
  97.         //  Tax Amount = tax price * qty po
  98.         //
  99.         //
  100.         //  Cara hitung yang baru:
  101.         //  1. Hitung gross item amount dan gross item amount after disc:
  102.         //     a. gross item amount = qty * gross price
  103.         //     b. gross item amount after disc:
  104.         //        1. Jika flgDisc = A:
  105.         //           a. grossAmountAfterDisc = qty * (gross price - discount amount)
  106.         //        2. Selain itu:
  107.         //           a. grossAmountAfterDisc = gross item amount * (100 - disc percentage ) / 100 --> (rounding)
  108.         //           b. discount amount = (gross amount - grossAmountAfterDisc) / qty --> (no rounding)
  109.         //  2. Hitung tax item amount, dan nett item amount berdasarkan gross item amount after disc
  110.         //     a. Jika flag tax amount = Y
  111.         //        1. harus memilih tax nya, jika tidak memilih tax maka error
  112.         //        2. tax amount = gross amount after disc * tax percentage / ( 100 + tax percentage) --> rounding
  113.         //        3. nett item amount = gross after disc - tax amount
  114.         //     b. Jika flag tax amount = N
  115.         //        1. nett item amount = gross price after disc
  116.         //        2. Jika memilih tax (tax id <> -99):
  117.         //           a. tax amount = gross price after disc * tax pct / 100 --> rounding
  118.         //        3. Jika tidak memilih tax: tax amount = 0
  119.         //  3. Hitung nett sell price, tax price dari amount yg diperoleh:
  120.         //     a. nett sell price = nett item amount / qty --> no rounding
  121.         //     b. tax price = tax amount / qty --> no rounding
  122.                
  123.        
  124.         //  1. Hitung gross item amount dan gross item amount after disc:
  125.         //     a. gross item amount = qty * gross price
  126.         //     b. gross item amount after disc:
  127.         //        1. Jika flgDisc = A:
  128.         //           a. grossAmountAfterDisc = qty * (gross price - discount amount)
  129.         //        2. Selain itu:
  130.         //           a. grossAmountAfterDisc = gross item amount * (100 - disc percentage ) / 100 --> (rounding)
  131.         //           b. discount amount = (gross amount - grossAmountAfterDisc) / qty --> (no rounding)
  132.         BigDecimal grossAmountValue = new Calc(grossPrice).multiply(qty).getValue();
  133.        
  134.         BigDecimal grossAmountAfterDiscount = new BigDecimal(0);
  135.         if (MasterConstantsForXcom.DISC_TYPE_AMOUNT.equals(flgDisc)) {
  136.            
  137.             Calc grossPriceAfterDisc = new Calc(grossPrice).subtract(discountAmount);
  138.             grossAmountAfterDiscount = new Calc(qty).multiply(grossPriceAfterDisc).divide(1, decimalForRounding, roundingMode).getValue();
  139.            
  140.         } else if (MasterConstantsForXcom.DISC_TYPE_PERCENTAGE.equals(flgDisc) &&
  141.                 !CommonBusinessValidator.compareNumber(discountPercentage.toString(), "0", CommonBusinessValidator.COMPARE_EQUAL, "Discount Percentage", "0")) {
  142.            
  143.             grossAmountAfterDiscount = new Calc(grossAmountValue).multiply(new Calc(100).subtract(discountPercentage).doubleValue())
  144.                                         .divide(100, decimalForRounding, roundingMode).getValue();
  145.             BigDecimal totalDiscAmount = new Calc(grossAmountValue).subtract(new Calc(grossAmountAfterDiscount)).getValue();
  146.             discountAmount = new Calc(totalDiscAmount)
  147.                                 .divide(qty, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_SCALE, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_MODE).doubleValue();
  148.         } else {
  149.             grossAmountAfterDiscount = new Calc(qty).multiply(grossPrice).getValue();
  150.         }
  151.        
  152.         //  2. Hitung tax item amount, dan nett item amount berdasarkan gross item amount after disc
  153.         //     a. Jika flag tax amount = Y
  154.         //        1. harus memilih tax nya, jika tidak memilih tax maka error
  155.         //        2. tax amount = gross amount after disc * tax percentage / ( 100 + tax percentage) --> rounding
  156.         //        3. nett item amount = gross amount after disc - tax amount
  157.         //     b. Jika flag tax amount = N
  158.         //        1. nett item amount = gross amount after disc
  159.         //        2. Jika memilih tax (tax id <> -99):
  160.         //           a. tax amount = gross amount after disc * tax pct / 100 --> rounding
  161.         //        3. Jika tidak memilih tax: tax amount = 0
  162.         Tax tax = null;
  163.        
  164.         if (GeneralConstants.YES.equals(flgTaxAmount)) {
  165.             if (!GeneralConstants.NULL_REF_VALUE_LONG.equals(taxId)) {
  166.                 tax = taxDao.findByPk(taxId);
  167.                 taxPercentage = tax.getPercentage();
  168.                 Double taxPercentageDivisor = new Calc(100).add(taxPercentage).doubleValue();
  169.                 taxAmount = new Calc(grossAmountAfterDiscount).multiply(taxPercentage).divide(taxPercentageDivisor, decimalForRounding, roundingMode).doubleValue();
  170.                 nettAmount = new Calc(grossAmountAfterDiscount).subtract(taxAmount).doubleValue();
  171.             } else {
  172.                 Double zero = new Double(0);
  173.                 // Jika price nya 0 tidak kena validasi
  174.                 if(CommonBusinessValidator.compareNumber(grossPrice.toString(), zero.toString(), CommonBusinessValidator.COMPARE_EQUAL, "Gross Price", "0")){
  175.                     // do nothing
  176.                 } else {
  177.                     throw new CoreException(MasterExceptionConstants.PRICE_INCLUDE_TAX);
  178.                 }
  179.             }
  180.         } else {
  181.             nettAmount = grossAmountAfterDiscount.doubleValue();
  182.             if (!GeneralConstants.NULL_REF_VALUE_LONG.equals(taxId)) {
  183.                 tax = taxDao.findByPk(taxId);
  184.                 taxPercentage = tax.getPercentage();
  185.                 taxAmount = new Calc(grossAmountAfterDiscount).multiply(taxPercentage).divide(100, decimalForRounding, roundingMode).doubleValue();
  186.             }
  187.         }
  188.        
  189.         nettPrice = new Calc(nettAmount).divide(qty, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_SCALE,
  190.                             MasterConstantsForXcom.DEFAULT_NO_ROUNDING_MODE).doubleValue();
  191.         taxPrice = new Calc(taxAmount).divide(qty, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_SCALE,
  192.                             MasterConstantsForXcom.DEFAULT_NO_ROUNDING_MODE).doubleValue();
  193.        
  194.         Dto outputDto = new Dto();
  195.         outputDto.put("grossPrice", grossPrice);
  196.         outputDto.put("taxPrice", taxPrice);
  197.         outputDto.put("nettPrice", nettPrice);
  198.         outputDto.put("grossAmount", grossAmountValue.doubleValue());
  199.         outputDto.put("taxAmount", taxAmount);
  200.         outputDto.put("nettAmount", nettAmount);
  201.         outputDto.put("taxPercentage", taxPercentage);
  202.         outputDto.put("discountPercentage", discountPercentage);
  203.         outputDto.put("discountAmount", discountAmount);
  204.        
  205.         return outputDto;
  206.     }
  207.  
  208.     @Override
  209.     public String getDescription() {
  210.         return "Calculate nett price and amount, tax price and amount, gross amount by gross price and tax";
  211.     }
  212.    
  213.    
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement