Correct tax report

This commit is contained in:
Hillel Coren 2021-09-14 09:24:42 +03:00
parent dcd9f1e0cf
commit abc08eeafd
2 changed files with 68 additions and 12 deletions

View File

@ -988,11 +988,18 @@ abstract class InvoiceEntity extends Object
/// Gets taxes in the form { taxName1: { amount: 0, paid: 0} , ... }
Map<String, Map<String, dynamic>> getTaxes(int precision) {
final taxes = <String, Map<String, dynamic>>{};
final taxable = calculateTaxes(
useInclusiveTaxes: usesInclusiveTaxes, precision: precision);
final taxable = getTaxable();
double calculateAmount(double taxable, double rate) {
if (usesInclusiveTaxes) {
return round(taxable - (taxable / (1 + (rate / 100))), 2);
} else {
return round(taxable * (rate / 100), 2);
}
}
if (taxRate1 != 0) {
final invoiceTaxAmount = taxable[taxName1];
final invoiceTaxAmount = calculateAmount(taxable, taxRate1);
final invoicePaidAmount = (amount * invoiceTaxAmount != 0)
? (paidToDate / amount * invoiceTaxAmount)
: 0.0;
@ -1001,7 +1008,7 @@ abstract class InvoiceEntity extends Object
}
if (taxRate2 != 0) {
final invoiceTaxAmount = taxable[taxName2];
final invoiceTaxAmount = calculateAmount(taxable, taxRate2);
final invoicePaidAmount = (amount * invoiceTaxAmount != 0)
? (paidToDate / amount * invoiceTaxAmount)
: 0.0;
@ -1010,7 +1017,7 @@ abstract class InvoiceEntity extends Object
}
if (taxRate3 != 0) {
final invoiceTaxAmount = taxable[taxName3];
final invoiceTaxAmount = calculateAmount(taxable, taxRate3);
final invoicePaidAmount = (amount * invoiceTaxAmount != 0)
? (paidToDate / amount * invoiceTaxAmount)
: 0.0;
@ -1019,19 +1026,22 @@ abstract class InvoiceEntity extends Object
}
for (final item in lineItems) {
final itemTaxable = getItemTaxable(item, amount, precision);
if (item.taxRate1 != 0) {
final itemTaxAmount = taxable[item.taxName1];
final itemTaxAmount = calculateAmount(itemTaxable, item.taxRate1);
final itemPaidAmount = amount != null &&
itemTaxAmount != null &&
amount * itemTaxAmount != 0
? (paidToDate / amount * itemTaxAmount)
: 0.0;
_calculateTax(
taxes, item.taxName1, item.taxRate1, itemTaxAmount, itemPaidAmount);
}
if (item.taxRate2 != 0) {
final itemTaxAmount = taxable[item.taxName2];
final itemTaxAmount = calculateAmount(itemTaxable, item.taxRate2);
final itemPaidAmount = amount != null &&
itemTaxAmount != null &&
amount * itemTaxAmount != 0
@ -1042,7 +1052,7 @@ abstract class InvoiceEntity extends Object
}
if (item.taxRate3 != 0) {
final itemTaxAmount = taxable[item.taxName3];
final itemTaxAmount = calculateAmount(itemTaxable, item.taxRate3);
final itemPaidAmount = amount != null &&
itemTaxAmount != null &&
amount * itemTaxAmount != 0

View File

@ -124,18 +124,64 @@ abstract class CalculateInvoiceTotal {
return map;
}
double getTaxable() {
double total = 0;
lineItems.forEach((invoiceItem) {
double lineTotal = invoiceItem.quantity * invoiceItem.cost;
if (invoiceItem.discount != 0) {
if (isAmountDiscount) {
lineTotal -= invoiceItem.discount;
} else {
lineTotal -= lineTotal * invoiceItem.discount / 100;
}
}
total += lineTotal;
});
if (discount > 0) {
if (isAmountDiscount) {
total -= discount;
} else {
total *= (100 - discount) / 100;
total = round(total, 2);
}
}
if (customTaxes1) {
total += customSurcharge1;
}
if (customTaxes2) {
total += customSurcharge2;
}
if (customTaxes3) {
total += customSurcharge3;
}
if (customTaxes4) {
total += customSurcharge4;
}
return total;
}
double getItemTaxable(
InvoiceItemEntity item, double invoiceTotal, int precision) {
final double qty = round(item.quantity, 4);
final double qty = round(item.quantity, 5);
final double cost = round(item.cost, 4);
final double itemDiscount = round(item.discount, precision);
double lineTotal = qty * cost;
if (discount != 0) {
if (isAmountDiscount) {
if (invoiceTotal != 0) {
lineTotal -= round(lineTotal / invoiceTotal * discount, 4);
if (invoiceTotal + discount != 0) {
lineTotal -= invoiceTotal != 0
? (lineTotal / (invoiceTotal + discount) * discount)
: 0;
}
} else {
lineTotal *= (100 - discount) / 100;
}
}
@ -143,7 +189,7 @@ abstract class CalculateInvoiceTotal {
if (isAmountDiscount) {
lineTotal -= itemDiscount;
} else {
lineTotal -= round(lineTotal * itemDiscount / 100, 4);
lineTotal -= lineTotal * itemDiscount / 100;
}
}