Skip to content

Commit 7f3f5d1

Browse files
committed
refactor: total amount calculation on the client-side
1 parent 2b38f05 commit 7f3f5d1

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

hrms/hr/doctype/full_and_final_statement/full_and_final_statement.js

+54-11
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ frappe.ui.form.on("Full and Final Statement", {
5454
frm.events.get_outstanding_statements(frm);
5555
},
5656

57+
total_asset_recovery_cost: function (frm) {
58+
frm.trigger("calculate_total_receivable_amt");
59+
},
60+
5761
get_outstanding_statements: function (frm) {
5862
if (frm.doc.employee) {
5963
frappe.call({
@@ -66,6 +70,45 @@ frappe.ui.form.on("Full and Final Statement", {
6670
}
6771
},
6872

73+
calculate_total_payable_amt: function (frm) {
74+
let total_payable_amount = 0;
75+
76+
frm.doc.payables?.forEach(
77+
(row) => (total_payable_amount += flt(row.amount, precision("amount", row))),
78+
);
79+
frm.set_value(
80+
"total_payable_amount",
81+
flt(total_payable_amount, precision("total_payable_amount")),
82+
);
83+
},
84+
85+
calculate_total_receivable_amt: function (frm) {
86+
let total_asset_recovery_cost = 0;
87+
let total_receivable_amount = 0;
88+
89+
frm.doc.assets_allocated?.forEach((row) => {
90+
if (row.action === "Recover Cost") {
91+
total_asset_recovery_cost += flt(row.cost, precision("cost", row));
92+
}
93+
});
94+
95+
frm.doc.receivables?.forEach(
96+
(row) => (total_receivable_amount += flt(row.amount, precision("amount", row))),
97+
);
98+
99+
frm.set_value(
100+
"total_asset_recovery_cost",
101+
flt(total_asset_recovery_cost, precision("total_asset_recovery_cost")),
102+
);
103+
frm.set_value(
104+
"total_receivable_amount",
105+
flt(
106+
total_asset_recovery_cost + total_receivable_amount,
107+
precision("total_receivable_amount"),
108+
),
109+
);
110+
},
111+
69112
create_journal_entry: function (frm) {
70113
frappe.call({
71114
method: "create_journal_entry",
@@ -80,7 +123,7 @@ frappe.ui.form.on("Full and Final Statement", {
80123

81124
frappe.ui.form.on("Full and Final Outstanding Statement", {
82125
reference_document: function (frm, cdt, cdn) {
83-
var child = locals[cdt][cdn];
126+
const child = locals[cdt][cdn];
84127
if (child.reference_document_type && child.reference_document) {
85128
frappe.call({
86129
method: "hrms.hr.doctype.full_and_final_statement.full_and_final_statement.get_account_and_amount",
@@ -98,20 +141,20 @@ frappe.ui.form.on("Full and Final Outstanding Statement", {
98141
}
99142
},
100143

101-
amount: function(frm, cdt, cdn) {
102-
let amount = 0;
144+
amount: function (frm, cdt, cdn) {
103145
const child_row = locals[cdt][cdn];
104146
const table = child_row.parentfield;
105147

106-
frm.doc[table]?.forEach(row => amount += row.amount);
107-
108148
if (table === "payables") {
109-
frm.set_value("total_payable_amount", flt(amount));
149+
frm.trigger("calculate_total_payable_amt");
110150
} else {
111-
frm.set_value(
112-
"total_receivable_amount",
113-
flt(amount) + flt(frm.doc.total_asset_recovery_cost)
114-
);
151+
frm.trigger("calculate_total_receivable_amt");
115152
}
116-
}
153+
},
154+
});
155+
156+
frappe.ui.form.on("Full and Final Asset", {
157+
cost: function (frm, _cdt, _cdn) {
158+
frm.trigger("calculate_total_receivable_amt");
159+
},
117160
});

0 commit comments

Comments
 (0)