Skip to content

Commit

Permalink
refactor(taxes): refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
vishdha committed Oct 30, 2020
1 parent 827ea57 commit c316acc
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions erpnext/public/js/controllers/taxes_and_totals.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,40 +765,43 @@ erpnext.taxes_and_totals = erpnext.payments.extend({
if (["Sales Order", "Sales Invoice", "Delivery Note", "Stock Entry"].includes(me.frm.doctype) && (!r || r.license_for !== "Distributor")) {
return;
}
});

frappe.call({
method: "erpnext.compliance.taxes.set_cultivation_tax",
args: {
doc: me.frm.doc
},
callback: (r) => {
if (r.message) {
let taxes = me.frm.doc.taxes;
// get tax account head from company master to avoid disorder behaviour and remove it from taxes table.
frappe.db.get_value('Company', { "name": me.frm.doc.company }, ["default_cultivation_tax_account_flower", "default_cultivation_tax_account_leaf", "default_cultivation_tax_account_plant"], (r) => {
let tax_account_list = [r.default_cultivation_tax_account_flower, r.default_cultivation_tax_account_leaf, r.default_cultivation_tax_account_plant];
if (taxes && taxes.length > 0) {
$.each(taxes, function (i, tax) {
if (tax_account_list.indexOf(tax.account_head) !== -1) {
me.frm.get_field("taxes").grid.grid_rows[i].remove();
}
});
}
});
let cultivation_tax_row_list = r.message;
for (let cultivation_tax_row of cultivation_tax_row_list) {
if (cultivation_tax_row.tax_amount > 0) {
me.frm.add_child('taxes', cultivation_tax_row);
me.frm.refresh_field('taxes');
me.calculate_taxes_and_totals();
}
}
if (me.frm.doc.items.length === 0 && (taxes && taxes.length > 0)) {
me.frm.get_field("taxes").grid.remove_all();
let tax_account_list = []
// get tax account head from company master to avoid disorder behaviour and remove it from taxes table.
frappe.db.get_value('Company', { "name": me.frm.doc.company }, ["default_cultivation_tax_account_flower", "default_cultivation_tax_account_leaf", "default_cultivation_tax_account_plant"], (r) => {
let taxes = me.frm.doc.taxes;
tax_account_list = [r.default_cultivation_tax_account_flower, r.default_cultivation_tax_account_leaf, r.default_cultivation_tax_account_plant];
if (taxes && taxes.length > 0) {
$.each(taxes, function (i, tax) {
if (tax_account_list.indexOf(tax.account_head) !== -1) {
me.frm.get_field("taxes").grid.grid_rows[i].remove();
}
});
}
});

frappe.call({
method: "erpnext.compliance.taxes.set_cultivation_tax",
args: {
doc: me.frm.doc
},
callback: (r) => {
if (r.message) {
let taxes = me.frm.doc.taxes;
let cultivation_tax_row_list = r.message;
for (let cultivation_tax_row of cultivation_tax_row_list) {
if (cultivation_tax_row.tax_amount > 0) {
me.frm.add_child('taxes', cultivation_tax_row);
me.frm.refresh_field('taxes');
me.calculate_taxes_and_totals();
}
}
if (me.frm.doc.items.length === 0 && (taxes && taxes.length > 0)) {
me.frm.get_field("taxes").grid.remove_all();
}
}
});
})
}
});
}
});

0 comments on commit c316acc

Please sign in to comment.