[flat discount] implemented flat discount on client side
diff --git a/public/js/transaction.js b/public/js/transaction.js
index f06fc6b..542a1c1 100644
--- a/public/js/transaction.js
+++ b/public/js/transaction.js
@@ -516,7 +516,7 @@
this.validate_conversion_rate();
this.frm.item_doclist = this.get_item_doclist();
this.frm.tax_doclist = this.get_tax_doclist();
-
+
this.calculate_item_values();
this.initialize_taxes();
this.determine_exclusive_rate && this.determine_exclusive_rate();
@@ -533,12 +533,11 @@
$.each(this.frm.tax_doclist, function(i, tax) {
tax.item_wise_tax_detail = {};
- tax_fields = ["total", "tax_amount_for_current_item", "grand_total_for_current_item",
+ tax_fields = ["total", "tax_amount_after_flat_discount",
+ "tax_amount_for_current_item", "grand_total_for_current_item",
"tax_fraction_for_current_item", "grand_total_fraction_for_current_item"]
- if (me.flat_discount_applied)
- tax_fields.push("tax_amount_after_flat_discount");
- else
+ if (!me.flat_discount_applied)
tax_fields.push("tax_amount");
$.each(tax_fields, function(i, fieldname) { tax[fieldname] = 0.0 });
@@ -551,34 +550,41 @@
calculate_taxes: function() {
var me = this;
+ var actual_tax_dict = {};
+ // maintain actual tax rate based on idx
+ $.each(this.frm.tax_doclist, function(i, tax) {
+ if (tax.charge_type == "Actual") {
+ actual_tax_dict[tax.idx] = flt(tax.rate);
+ }
+ });
+
$.each(this.frm.item_doclist, function(n, item) {
var item_tax_map = me._load_item_tax_rate(item.item_tax_rate);
-
+
$.each(me.frm.tax_doclist, function(i, tax) {
// tax_amount represents the amount of tax for the current step
var current_tax_amount = me.get_current_tax_amount(item, tax, item_tax_map);
- me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
-
- // case when net total is 0 but there is an actual type charge
- // in this case add the actual amount to tax.tax_amount
- // and tax.grand_total_for_current_item for the first such iteration
- if(tax.charge_type == "Actual" &&
- !(current_tax_amount || me.frm.doc.net_total || tax.tax_amount)) {
- var zero_net_total_adjustment = flt(tax.rate, precision("tax_amount", tax));
- current_tax_amount += zero_net_total_adjustment;
+ // Adjust divisional loss to the last item
+ if (tax.charge_type == "Actual") {
+ actual_tax_dict[tax.idx] -= current_tax_amount;
+ if (n == me.frm.item_doclist.length - 1) {
+ current_tax_amount += actual_tax_dict[tax.idx]
}
-
+ }
+
+ me.set_item_tax_amount && me.set_item_tax_amount(item, tax, current_tax_amount);
+
// store tax_amount for current item as it will be used for
// charge type = 'On Previous Row Amount'
tax.tax_amount_for_current_item = current_tax_amount;
// accumulate tax amount into tax.tax_amount
- if (me.flat_discount_applied)
- tax.tax_amount_after_flat_discount += current_tax_amount;
- else
+ if (!me.flat_discount_applied)
tax.tax_amount += current_tax_amount;
+
+ tax.tax_amount_after_flat_discount += current_tax_amount;
// for buying
if(tax.category) {
@@ -603,9 +609,32 @@
// in tax.total, accumulate grand total for each item
tax.total += tax.grand_total_for_current_item;
+
+ // set precision in the last item iteration
+ if (n == me.frm.item_doclist.length - 1) {
+ me.round_off_totals(tax);
+
+ // adjust flat discount loss in last tax iteration
+ if ((i == me.frm.tax_doclist.length - 1) && me.flat_discount_applied)
+ me.adjust_flat_discount_loss(tax);
+ }
});
});
},
+
+ round_off_totals: function(tax) {
+ tax.total = flt(tax.total, precision("total", tax));
+ tax.tax_amount = flt(tax.tax_amount, precision("tax_amount", tax));
+ tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount,
+ precision("tax_amount", tax));
+ },
+
+ adjust_flat_discount_loss: function(tax) {
+ var flat_discount_loss = this.frm.doc.grand_total - this.frm.doc.flat_discount - tax.total;
+ tax.tax_amount_after_flat_discount = flt(tax.tax_amount_after_flat_discount +
+ flat_discount_loss, precision("tax_amount", tax));
+ tax.total = flt(tax.total + flat_discount_loss, precision("total", tax));
+ },
get_current_tax_amount: function(item, tax, item_tax_map) {
var tax_rate = this._get_tax_rate(tax, item_tax_map);