Skip to content

Commit

Permalink
fix: common taxes for BOE and subcontracting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninad1306 committed Aug 13, 2024
1 parent a0108de commit d95a4e2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 419 deletions.
239 changes: 3 additions & 236 deletions india_compliance/gst_india/doctype/bill_of_entry/bill_of_entry.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2023, Resilient Tech and contributors
// For license information, please see license.txt
frappe.provide("india_compliance");

frappe.ui.form.on("Bill of Entry", {
onload(frm) {
Expand Down Expand Up @@ -68,11 +69,7 @@ frappe.ui.form.on("Bill of Entry", {
},

total_customs_duty(frm) {
frm.bill_of_entry_controller.update_total_amount_payable();
},

total_taxes(frm) {
frm.bill_of_entry_controller.update_total_amount_payable();
frm.taxes_controller.update_total_amount_payable();
},
});

Expand All @@ -99,41 +96,10 @@ frappe.ui.form.on("Bill of Entry Item", {
},
});

frappe.ui.form.on("Bill of Entry Taxes", {
rate(frm, cdt, cdn) {
frm.taxes_controller.update_tax_rate(cdt, cdn);
},

tax_amount(frm, cdt, cdn) {
frm.taxes_controller.update_tax_amount(cdt, cdn);
},

async account_head(frm, cdt, cdn) {
await frm.taxes_controller.set_item_wise_tax_rates(null, cdn);
frm.taxes_controller.update_tax_amount(cdt, cdn);
},

async charge_type(frm, cdt, cdn) {
const row = locals[cdt][cdn];
if (!row.charge_type || row.charge_type === "Actual") {
row.rate = 0;
row.item_wise_tax_rates = "{}";
frm.refresh_field("taxes");
} else {
await frm.taxes_controller.set_item_wise_tax_rates(null, cdn);
frm.taxes_controller.update_tax_amount(cdt, cdn);
}
},

taxes_remove(frm) {
frm.bill_of_entry_controller.update_total_taxes();
},
});

class BillOfEntryController {
constructor(frm) {
this.frm = frm;
this.frm.taxes_controller = new TaxesController(frm);
this.frm.taxes_controller = new india_compliance.taxes_controller(frm);
this.setup();
}

Expand Down Expand Up @@ -190,203 +156,4 @@ class BillOfEntryController {
}, 0)
);
}

update_total_taxes() {
const total_taxes = this.frm.doc.taxes.reduce(
(total, row) => total + row.tax_amount,
0
);
this.frm.set_value("total_taxes", total_taxes);
}

update_total_amount_payable() {
this.frm.set_value(
"total_amount_payable",
this.frm.doc.total_customs_duty + this.frm.doc.total_taxes
);
}
}

class TaxesController {
constructor(frm) {
this.frm = frm;
this.setup();
}

setup() {
this.fetch_round_off_accounts();
this.set_item_tax_template_query();
this.set_account_head_query();
}

fetch_round_off_accounts() {
if (this.frm.doc.docstatus !== 0 || !this.frm.doc.company) return;

frappe.call({
method: "erpnext.controllers.taxes_and_totals.get_round_off_applicable_accounts",
args: {
company: this.frm.doc.company,
account_list: [],
},
callback(r) {
if (r.message) {
frappe.flags.round_off_applicable_accounts = r.message;
}
},
});
}

set_item_tax_template_query() {
this.frm.set_query("item_tax_template", "items", () => {
return {
filters: {
company: this.frm.doc.company,
},
};
});
}

set_account_head_query() {
this.frm.set_query("account_head", "taxes", () => {
return {
filters: {
company: this.frm.doc.company,
is_group: 0,
},
};
});
}

async set_item_wise_tax_rates(item_name, tax_name) {
/**
* This method is used to set item wise tax rates from the server
* and update the item_wise_tax_rates field in the taxes table.
*
* @param {string} item_name - Item row name for which the tax rates are to be fetched.
* @param {string} tax_name - Tax row name for which the tax rates are to be fetched.
*/

if (!this.frm.taxes || !this.frm.taxes.length) return;

await this.frm.call("set_item_wise_tax_rates", {
item_name: item_name,
tax_name: tax_name,
});
}

update_item_wise_tax_rates(tax_row) {
/**
* This method is used to update the item_wise_tax_rates field in the taxes table when
* - Item tax template is removed from the item row.
* - Tax rate is changed in the tax row.
*
* It will update item rate with default tax rate.
*
* @param {object} tax_row - Tax row object.
*/

let taxes;
if (tax_row) taxes = [tax_row];
else taxes = this.frm.doc.taxes;

taxes.forEach(tax => {
const item_wise_tax_rates = JSON.parse(tax.item_wise_tax_rates || "{}");
this.frm.doc.items.forEach(item => {
if (item.item_tax_template) return;
item_wise_tax_rates[item.name] = tax.rate;
});
tax.item_wise_tax_rates = JSON.stringify(item_wise_tax_rates);
});
}

async update_tax_rate(cdt, cdn) {
const row = locals[cdt][cdn];
if (!row.charge_type || row.charge_type === "Actual") row.rate = 0;
else {
this.update_item_wise_tax_rates(row);
await this.update_tax_amount(cdt, cdn);
}
}

async update_tax_amount(cdt, cdn) {
/**
* This method is used to update the tax amount in the tax row
* - Update for all tax rows when cdt is null.
* - Update for a single tax row when cdt and cdn are passed.
*
* @param {string} cdt - DocType of the tax row.
* @param {string} cdn - Name of the tax row.
*/

let taxes;
if (cdt) taxes = [locals[cdt][cdn]];
else taxes = this.frm.doc.taxes;

taxes.forEach(async row => {
if (!row.charge_type || row.charge_type === "Actual") return;

let tax_amount = 0;

if (row.charge_type === "On Net Total") {
tax_amount = this.get_tax_on_net_total(row);
}

if (row.charge_type == "On Item Quantity") {
tax_amount = this.get_tax_on_item_quantity(row);
}

// update if tax amount is changed manually
if (tax_amount !== row.tax_amount) {
row.tax_amount = tax_amount;
}

if (
frappe.flags.round_off_applicable_accounts?.includes(row.account_head)
) {
row.tax_amount = Math.round(row.tax_amount);
}
});

this.update_total_amount();
this.frm.bill_of_entry_controller.update_total_taxes();
}

update_total_amount() {
this.frm.doc.taxes.reduce((total, row) => {
const total_amount = total + row.tax_amount;
row.total = total_amount;

return total_amount;
}, this.frm.doc.total_taxable_value);

this.frm.refresh_field("taxes");
}

get_tax_on_net_total(tax_row) {
/**
* This method is used to calculate the tax amount on net total
* based on the item wise tax rates.
*
* @param {object} tax_row - Tax row object.
*/

const item_wise_tax_rates = JSON.parse(tax_row.item_wise_tax_rates || "{}");
return this.frm.doc.items.reduce((total, item) => {
return total + (item.taxable_value * item_wise_tax_rates[item.name]) / 100;
}, 0);
}

get_tax_on_item_quantity(tax_row) {
/**
* This method is used to calculate the tax amount on item quntity (cess non advol)
* based on the item wise tax rates and item quantity.
*
* @param {object} tax_row - Tax row object.
*/

const item_wise_tax_rates = JSON.parse(tax_row.item_wise_tax_rates || "{}");
return this.frm.doc.items.reduce((total, item) => {
return total + (item.qty * item_wise_tax_rates[item.name]);
}, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
{
"fieldname": "taxes",
"fieldtype": "Table",
"options": "Bill of Entry Taxes",
"options": "India Compliance Taxes and Charges",
"reqd": 1
},
{
Expand Down Expand Up @@ -297,7 +297,7 @@
"link_fieldname": "link_name"
}
],
"modified": "2024-03-29 11:36:17.991037",
"modified": "2024-08-12 15:48:43.769450",
"modified_by": "Administrator",
"module": "GST India",
"name": "Bill of Entry",
Expand Down
Loading

0 comments on commit d95a4e2

Please sign in to comment.