I created a custom Doctype, Proposal from Quotation. The rate(Unit Price) wasn’t getting multiplied with quantity to give me Amount(Total Price). The Total and Grand Total weren’t working, hence, @ Balamurugan_Ravichan gave me a code which worked for me to get the above. The code is below:
frappe.ui.form.on("Proposal Item", {
item_code: function (frm, cdt, cdn) {
var child = locals[cdt][cdn];
if (child.item_code) {
calculateTotal(frm);
}
},
qty: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "total_price", d.qty * d.unit_price);
calculateTotal(frm);
},
unit_price: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "total_price", d.qty * d.unit_price);
calculateTotal(frm);
},
});
function calculateTotal(frm) {
var total = 0;
frm.doc.items.forEach(function (item) {
total += item.total_price;
});
frm.set_value("total", total);
}
frappe.ui.form.on("Proposal Item", {
item_code: function (frm, cdt, cdn) {
var child = locals[cdt][cdn];
if (child.item_code) {
calculateGrandTotal(frm);
}
},
qty: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "total_price", d.qty * d.unit_price);
calculateGrandTotal(frm);
},
unit_price: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "total_price", d.qty * d.unit_price);
calculateGrandTotal(frm);
},
});
function calculateGrandTotal(frm) {
var grand_total = 0;
frm.doc.items.forEach(function (item) {
grand_total += item.total_price;
});
frm.set_value("grand_total", grand_total);
}
frappe.ui.form.on("Proposal Item", {
item_code: function (frm, cdt, cdn) {
var child = locals[cdt][cdn];
if (child.item_code) {
calculateNetTotal(frm);
}
},
qty: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "total_price", d.qty * d.unit_price);
calculateNetTotal(frm);
},
unit_price: function (frm, cdt, cdn) {
var d = locals[cdt][cdn];
frappe.model.set_value(cdt, cdn, "total_price", d.qty * d.unit_price);
calculateNetTotal(frm);
},
});
function calculateNetTotal(frm) {
var net_total = 0;
frm.doc.items.forEach(function (item) {
net_total += item.total_price;
});
frm.set_value("net_total", net_total);
}
The next challenge now is that the Sales Tax is not getting pulled, although set up. How do I get the system to calculate VAT? I sincerely need help with this. There is this code I have been playing with for the VAT but it only get called after I have saved the form and it simply attempt to add the VAT on each item whether I want VAT or not which is not how I want things to run! Also, the code give error while calculating each Item’s VAT and the Total/Grand Total are not being updated. Here is the code for that below:
frappe.ui.form.on("Proposal", {
validate: function (frm) {
frm.clear_table("taxes");
var total_tax_amount = 0;
$.each(frm.doc.items || [], function (i, item) {
if (item.item_code) {
var tax_and_charge = {
charge_type: "On Net Total",
account_head: "VAT - SGSL",
description: "VAT",
rate: 7.5
};
var item_tax_amount = (item.amount * tax_and_charge.rate) / 100;
tax_and_charge.tax_amount = item_tax_amount;
total_tax_amount += item_tax_amount;
frm.add_child("taxes", tax_and_charge);
}
});
frm.set_value("total_taxes_and_charges", total_tax_amount);
var total_taxes = 0;
$.each(frm.doc.taxes || [], function (i, tax_row) {
total_taxes += tax_row.tax_amount || 0;
});
frm.doc.taxes.forEach(function (row) {
row.total = total_taxes;
});
var total = (frm.doc.total || 0) + total_tax_amount;
frm.set_value("total", total);
frm.refresh_field("taxes");
frm.refresh_field("total_taxes_and_charges");
frm.refresh_field("total");
}
});
Please can anyone help me solve this? Thank you all in advance.