-
-
Save ismdcf/5c013f20d4df258eb196f0e5ae3ecb13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.clover.core.internal.calc.Calc; | |
import com.clover.core.internal.calc.Decimal; | |
import com.clover.core.internal.calc.Price; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.logging.Logger; | |
import java.util.stream.Collectors; | |
public class CloverOrder implements Calc.Order { | |
private final List<OrderLineItem> lineItems; | |
private final long tip; | |
private final long discount; | |
private final long serviceChargePercentage; | |
public static final int TAX_RATE_SCALE = 5; | |
public static final int SERVICE_CHARGE_SCALE = 4; | |
private static final Decimal TAX_RATE_DIVISOR = new Decimal(100000); | |
private static final Decimal SERVICE_CHARGE_DIVISOR = new Decimal(10000); | |
private static final Decimal UNIT_QUANTITY_DIVISOR = new Decimal(1000); | |
private static final Decimal HUNDRED = new Decimal(100); | |
private final boolean isVat; | |
CalcOrder calcOrder; | |
private static final String TAG = CloverOrder.class.getSimpleName(); | |
private final Calc.Logger logger = new Calc.Logger() { | |
@Override | |
public void warn(String s) { | |
System.out.println(s); | |
} | |
}; | |
public CloverOrder(List<OrderLineItem> lineItems, | |
long tip, | |
long discount, | |
long serviceChargePercentage, | |
boolean isVat) { | |
this.lineItems = lineItems; | |
this.tip = tip; | |
this.discount = discount; | |
this.serviceChargePercentage = serviceChargePercentage; | |
this.isVat = isVat; | |
} | |
private Calc getCalc() { | |
return new Calc(calcOrder,logger); | |
} | |
public long getTax() { | |
return getCalc().getTax().getCents(); | |
} | |
@Override | |
public boolean isVat() { | |
return isVat; | |
} | |
@Override | |
public boolean isTaxRemoved() { | |
return false; | |
} | |
@Override | |
public Collection<? extends Calc.LineItem> getLineItems() { | |
return lineItems.stream().map(CloverOrder::lineItem).collect(Collectors.toList()); | |
} | |
@Override | |
public Price getComboDiscount() { | |
return Price.ZERO; | |
} | |
@Override | |
public Price getAmountDiscount() { | |
return new Price(discount); | |
} | |
@Override | |
public Collection<Decimal> getPercentDiscounts() { | |
return Collections.emptyList(); | |
} | |
@Override | |
public Price getTip() { | |
return new Price(tip); | |
} | |
@Override | |
public Decimal getPercentServiceCharge() { | |
return new Decimal(serviceChargePercentage, SERVICE_CHARGE_SCALE); | |
} | |
public static Calc.TaxRate convertTaxRate(TaxRates.Element element) { | |
return new Calc.TaxRate() { | |
@Override | |
public String getId() { | |
return element.getId(); | |
} | |
@Override | |
public Decimal getRate() { | |
return new Decimal(element.getRate()).divide(TAX_RATE_DIVISOR); | |
} | |
public Long getRateAsLong() { | |
return element.getRate(); | |
} | |
@Override | |
public Price getFlatTaxAmount() { | |
return null; | |
} | |
@Override | |
public String getTaxType() { | |
return null; | |
} | |
public String getName() { | |
return element.getName(); | |
} | |
public int hashCode() { | |
return (getId() != null ? getId().hashCode() : super.hashCode()); | |
} | |
public boolean equals(Object o) { | |
if (o instanceof Calc.TaxRate) { | |
return getId() != null ? getId().equals(((Calc.TaxRate) o).getId()) : super.equals(o); | |
} | |
return false; | |
} | |
}; | |
} | |
public static Calc.LineItem lineItem(OrderLineItem item) { | |
return new Calc.LineItem() { | |
@Override | |
public Price getPrice() { | |
return new Price(item.getPrice()); | |
} | |
@Override | |
public boolean allowNegativePrice() { | |
return false; | |
} | |
@Override | |
public Decimal getUnitQuantity() { | |
return null; | |
} | |
@Override | |
public boolean isRefunded() { | |
return false; | |
} | |
@Override | |
public boolean isExchanged() { | |
return false; | |
} | |
@Override | |
public Collection<Calc.TaxRate> getTaxRates() { | |
if(item.getTaxRates() != null && item.getTaxRates().getElements() != null) { | |
final List<TaxRates.Element> elements = item.getTaxRates().getElements(); | |
return elements.stream().map(CloverOrder::convertTaxRate).collect(Collectors.toList()); | |
} | |
return Collections.emptyList(); | |
} | |
@Override | |
public Price getModification() { | |
return Price.ZERO; | |
} | |
@Override | |
public Price getAmountDiscount() { | |
return Price.ZERO; | |
} | |
@Override | |
public Collection<Decimal> getPercentDiscounts() { | |
return Collections.emptyList(); | |
} | |
@Override | |
public Decimal getSplitPercent() { | |
return new Decimal(100, 0); | |
} | |
}; | |
} | |
private class TaxRateCalc implements Calc.TaxRate { | |
private Calc.TaxRate taxRate; | |
TaxRateCalc(Calc.TaxRate taxRate) { | |
if (taxRate == null) { | |
throw new NullPointerException("taxRate cannot be null"); | |
} | |
this.taxRate = taxRate; | |
} | |
@Override | |
public String getId() { | |
return taxRate.getId(); | |
} | |
@Override | |
public Decimal getRate() { | |
return new Decimal(taxRate.getRate()).divide(TAX_RATE_DIVISOR); | |
} | |
public Long getRateAsLong() { | |
return taxRate.getRateAsLong(); | |
} | |
@Override | |
public Price getFlatTaxAmount() { | |
return null; | |
} | |
@Override | |
public String getTaxType() { | |
return null; | |
} | |
public String getName() { | |
return taxRate.getName(); | |
} | |
public int hashCode() { | |
return (getId() != null ? getId().hashCode() : super.hashCode()); | |
} | |
public boolean equals(Object o) { | |
if (o instanceof Calc.TaxRate) { | |
return getId() != null ? getId().equals(((Calc.TaxRate) o).getId()) : super.equals(o); | |
} | |
return false; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.clover.core.internal.calc.Calc; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class CloverTaxCalculationTest { | |
@Test | |
public void taxCalculation() { | |
List<OrderLineItem> orderLineItemList = new ArrayList<>(); | |
addLineItem(orderLineItemList); | |
addLineItem(orderLineItemList); | |
addLineItem(orderLineItemList); | |
addLineItem(orderLineItemList); | |
addLineItem(orderLineItemList); | |
final Calc calc = new Calc(new CloverOrder(orderLineItemList, 0, 0, 0, false)); | |
Assert.assertNotNull(calc.getTax()); | |
} | |
private void addLineItem(List<OrderLineItem> orderLineItemList) { | |
final OrderLineItem lineItem = new OrderLineItem(); | |
final TaxRates taxRates = new TaxRates(); | |
final TaxRates.Element tax1 = new TaxRates.Element(); | |
tax1.setRate(700000); | |
taxRates.setElements(Collections.singletonList(tax1)); | |
lineItem.setTaxRates(taxRates); | |
lineItem.setPrice(645); | |
lineItem.setQuantity(1); | |
orderLineItemList.add(lineItem); | |
} | |
private void addLineItem2(List<OrderLineItem> orderLineItemList) { | |
final OrderLineItem lineItem = new OrderLineItem(); | |
final TaxRates taxRates = new TaxRates(); | |
final TaxRates.Element tax1 = new TaxRates.Element(); | |
tax1.setRate(700000); | |
taxRates.setElements(Collections.singletonList(tax1)); | |
lineItem.setTaxRates(taxRates); | |
lineItem.setPrice(9450); | |
lineItem.setQuantity(1); | |
orderLineItemList.add(lineItem); | |
} | |
private void addLineItem3(List<OrderLineItem> orderLineItemList) { | |
final OrderLineItem lineItem = new OrderLineItem(); | |
final TaxRates taxRates = new TaxRates(); | |
final TaxRates.Element tax1 = new TaxRates.Element(); | |
tax1.setRate(700000); | |
taxRates.setElements(Collections.singletonList(tax1)); | |
lineItem.setTaxRates(taxRates); | |
lineItem.setPrice(1950); | |
lineItem.setQuantity(1); | |
orderLineItemList.add(lineItem); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment