Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jleaf.erp.master.bo.common;
- import java.math.BigDecimal;
- import org.jleaf.core.AbstractBusinessFunction;
- import org.jleaf.core.BusinessFunction;
- import org.jleaf.core.CoreException;
- import org.jleaf.core.CoreExceptionConstants;
- import org.jleaf.core.Dto;
- import org.jleaf.core.GeneralConstants;
- import org.jleaf.core.annotation.Info;
- import org.jleaf.core.annotation.InfoIn;
- import org.jleaf.core.annotation.InfoOut;
- import org.jleaf.erp.master.MasterConstantsForXcom;
- import org.jleaf.erp.master.MasterExceptionConstants;
- import org.jleaf.erp.master.dao.TaxDao;
- import org.jleaf.erp.master.entity.Tax;
- import org.jleaf.util.Calc;
- import org.jleaf.util.ValidationUtil;
- import org.jleaf.validator.CommonBusinessValidator;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- //@formatter:off
- @Service
- @InfoIn(value = {
- @Info(name = "grossPrice", description = "gross price", type = Double.class),
- @Info(name = "qty", description = "qty", type = Double.class),
- @Info(name = "discountPercentage", description = "discount percentage", type = Double.class),
- @Info(name = "discountAmount", description = "discount amount", type = Double.class),
- @Info(name = "decimalForRounding", description = "decimal for rounding", type = Integer.class),
- @Info(name = "roundingMode", description = "decimal for rounding", type = Integer.class),
- @Info(name = "flgTaxAmount", description = "flgTaxAmount", type = String.class),
- @Info(name = "taxId", description = "tax id", type = Long.class),
- @Info(name = "flgDisc", description = "Flag tipe discount: [P]ercentage / [A]mount", type = String.class)
- })
- @InfoOut(value = {
- @Info(name = "grossPrice", description = "gross price", type = Double.class),
- @Info(name = "taxPrice", description = "tax price", type = Double.class),
- @Info(name = "nettPrice", description = "nett price", type = Double.class),
- @Info(name = "grossAmount", description = "gross amount", type = Double.class),
- @Info(name = "taxAmount", description = "tax amount", type = Double.class),
- @Info(name = "nettAmount", description = "nett amount", type = Double.class),
- @Info(name = "taxPercentage", description = "tax percentage", type = Double.class),
- @Info(name = "discountPercentage", description = "discount percentage", type = Double.class),
- @Info(name = "discountAmount", description = "discount amount", type = Double.class)
- })
- //@formatter:on
- public class CalculateAmountFromGrossPriceForXcom extends AbstractBusinessFunction implements BusinessFunction {
- @Autowired
- TaxDao taxDao;
- @Override
- public Dto execute(Dto inputDto) throws Exception {
- if (inputDto == null) {
- throw new CoreException(CoreExceptionConstants.DTO_CANNOT_NULL);
- }
- ValidationUtil.valDtoContainsKey(inputDto, "grossPrice");
- ValidationUtil.valDtoContainsKey(inputDto, "qty");
- ValidationUtil.valDtoContainsKey(inputDto, "discountPercentage");
- ValidationUtil.valDtoContainsKey(inputDto, "discountAmount");
- ValidationUtil.valDtoContainsKey(inputDto, "flgTaxAmount");
- ValidationUtil.valDtoContainsKey(inputDto, "decimalForRounding");
- ValidationUtil.valDtoContainsKey(inputDto, "roundingMode");
- ValidationUtil.valDtoContainsKey(inputDto, "taxId");
- ValidationUtil.valBlankOrNull(inputDto, "flgDisc");
- Double grossPrice = inputDto.getDouble("grossPrice");
- Double qty = inputDto.getDouble("qty");
- Double discountPercentage = inputDto.getDouble("discountPercentage");
- Double discountAmount = inputDto.getDouble("discountAmount");
- Integer decimalForRounding = inputDto.getInteger("decimalForRounding");
- Integer roundingMode = inputDto.getInteger("roundingMode");
- String flgTaxAmount = inputDto.getString("flgTaxAmount");
- Long taxId = inputDto.getLong("taxId");
- String flgDisc = inputDto.getString("flgDisc");
- Double taxPrice = new Double(0);
- Double nettPrice = new Double(0);
- Double nettAmount = new Double(0);
- Double taxAmount = new Double(0);
- Double taxPercentage = new Double(0);
- // OBSOLETE:
- // Untuk flag tax amount (flag mempengaruhi pajak) : Y / YES
- // harus memilih tax nya, jika tidak memilih tax maka error
- // tax price po = (gross price po – discount) * 100 / (100 + tax percentage value) * (tax percentage value / 100) rounding ke atas sampai 1 rupiah
- // Nett price po = (gross price po – discount) – tax price
- // Untuk flag tax amount (flag mempengaruhi pajak) : N / NO
- // nett price po = (gross price po – discount)
- // jika memilih tax, maka tax price = (gross price po – discount) * (tax percentage value / 100) rounding ke atas sampai 1 rupiah
- // tidak memilih tax maka tax percentage dan tax price = 0
- // Gross Item Amount = gross price po * qty po
- // Nett item amount = nett price po * qty po
- // Tax Amount = tax price * qty po
- //
- //
- // Cara hitung yang baru:
- // 1. Hitung gross item amount dan gross item amount after disc:
- // a. gross item amount = qty * gross price
- // b. gross item amount after disc:
- // 1. Jika flgDisc = A:
- // a. grossAmountAfterDisc = qty * (gross price - discount amount)
- // 2. Selain itu:
- // a. grossAmountAfterDisc = gross item amount * (100 - disc percentage ) / 100 --> (rounding)
- // b. discount amount = (gross amount - grossAmountAfterDisc) / qty --> (no rounding)
- // 2. Hitung tax item amount, dan nett item amount berdasarkan gross item amount after disc
- // a. Jika flag tax amount = Y
- // 1. harus memilih tax nya, jika tidak memilih tax maka error
- // 2. tax amount = gross amount after disc * tax percentage / ( 100 + tax percentage) --> rounding
- // 3. nett item amount = gross after disc - tax amount
- // b. Jika flag tax amount = N
- // 1. nett item amount = gross price after disc
- // 2. Jika memilih tax (tax id <> -99):
- // a. tax amount = gross price after disc * tax pct / 100 --> rounding
- // 3. Jika tidak memilih tax: tax amount = 0
- // 3. Hitung nett sell price, tax price dari amount yg diperoleh:
- // a. nett sell price = nett item amount / qty --> no rounding
- // b. tax price = tax amount / qty --> no rounding
- // 1. Hitung gross item amount dan gross item amount after disc:
- // a. gross item amount = qty * gross price
- // b. gross item amount after disc:
- // 1. Jika flgDisc = A:
- // a. grossAmountAfterDisc = qty * (gross price - discount amount)
- // 2. Selain itu:
- // a. grossAmountAfterDisc = gross item amount * (100 - disc percentage ) / 100 --> (rounding)
- // b. discount amount = (gross amount - grossAmountAfterDisc) / qty --> (no rounding)
- BigDecimal grossAmountValue = new Calc(grossPrice).multiply(qty).getValue();
- BigDecimal grossAmountAfterDiscount = new BigDecimal(0);
- if (MasterConstantsForXcom.DISC_TYPE_AMOUNT.equals(flgDisc)) {
- Calc grossPriceAfterDisc = new Calc(grossPrice).subtract(discountAmount);
- grossAmountAfterDiscount = new Calc(qty).multiply(grossPriceAfterDisc).divide(1, decimalForRounding, roundingMode).getValue();
- } else if (MasterConstantsForXcom.DISC_TYPE_PERCENTAGE.equals(flgDisc) &&
- !CommonBusinessValidator.compareNumber(discountPercentage.toString(), "0", CommonBusinessValidator.COMPARE_EQUAL, "Discount Percentage", "0")) {
- grossAmountAfterDiscount = new Calc(grossAmountValue).multiply(new Calc(100).subtract(discountPercentage).doubleValue())
- .divide(100, decimalForRounding, roundingMode).getValue();
- BigDecimal totalDiscAmount = new Calc(grossAmountValue).subtract(new Calc(grossAmountAfterDiscount)).getValue();
- discountAmount = new Calc(totalDiscAmount)
- .divide(qty, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_SCALE, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_MODE).doubleValue();
- } else {
- grossAmountAfterDiscount = new Calc(qty).multiply(grossPrice).getValue();
- }
- // 2. Hitung tax item amount, dan nett item amount berdasarkan gross item amount after disc
- // a. Jika flag tax amount = Y
- // 1. harus memilih tax nya, jika tidak memilih tax maka error
- // 2. tax amount = gross amount after disc * tax percentage / ( 100 + tax percentage) --> rounding
- // 3. nett item amount = gross amount after disc - tax amount
- // b. Jika flag tax amount = N
- // 1. nett item amount = gross amount after disc
- // 2. Jika memilih tax (tax id <> -99):
- // a. tax amount = gross amount after disc * tax pct / 100 --> rounding
- // 3. Jika tidak memilih tax: tax amount = 0
- Tax tax = null;
- if (GeneralConstants.YES.equals(flgTaxAmount)) {
- if (!GeneralConstants.NULL_REF_VALUE_LONG.equals(taxId)) {
- tax = taxDao.findByPk(taxId);
- taxPercentage = tax.getPercentage();
- Double taxPercentageDivisor = new Calc(100).add(taxPercentage).doubleValue();
- taxAmount = new Calc(grossAmountAfterDiscount).multiply(taxPercentage).divide(taxPercentageDivisor, decimalForRounding, roundingMode).doubleValue();
- nettAmount = new Calc(grossAmountAfterDiscount).subtract(taxAmount).doubleValue();
- } else {
- Double zero = new Double(0);
- // Jika price nya 0 tidak kena validasi
- if(CommonBusinessValidator.compareNumber(grossPrice.toString(), zero.toString(), CommonBusinessValidator.COMPARE_EQUAL, "Gross Price", "0")){
- // do nothing
- } else {
- throw new CoreException(MasterExceptionConstants.PRICE_INCLUDE_TAX);
- }
- }
- } else {
- nettAmount = grossAmountAfterDiscount.doubleValue();
- if (!GeneralConstants.NULL_REF_VALUE_LONG.equals(taxId)) {
- tax = taxDao.findByPk(taxId);
- taxPercentage = tax.getPercentage();
- taxAmount = new Calc(grossAmountAfterDiscount).multiply(taxPercentage).divide(100, decimalForRounding, roundingMode).doubleValue();
- }
- }
- nettPrice = new Calc(nettAmount).divide(qty, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_SCALE,
- MasterConstantsForXcom.DEFAULT_NO_ROUNDING_MODE).doubleValue();
- taxPrice = new Calc(taxAmount).divide(qty, MasterConstantsForXcom.DEFAULT_NO_ROUNDING_SCALE,
- MasterConstantsForXcom.DEFAULT_NO_ROUNDING_MODE).doubleValue();
- Dto outputDto = new Dto();
- outputDto.put("grossPrice", grossPrice);
- outputDto.put("taxPrice", taxPrice);
- outputDto.put("nettPrice", nettPrice);
- outputDto.put("grossAmount", grossAmountValue.doubleValue());
- outputDto.put("taxAmount", taxAmount);
- outputDto.put("nettAmount", nettAmount);
- outputDto.put("taxPercentage", taxPercentage);
- outputDto.put("discountPercentage", discountPercentage);
- outputDto.put("discountAmount", discountAmount);
- return outputDto;
- }
- @Override
- public String getDescription() {
- return "Calculate nett price and amount, tax price and amount, gross amount by gross price and tax";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement