blob: 6864e2865d3a00ac7f3df0ae4aaa55d7afe6459b [file] [log] [blame]
Ankush Menatec74a5e2024-03-10 19:45:40 +05301frappe.provide("erpnext.accounts");
ruthra kumar25fe7522023-08-29 13:46:29 +05302
ruthra kumar74f9e342023-11-21 16:51:06 +05303erpnext.accounts.unreconcile_payment = {
ruthra kumar25fe7522023-08-29 13:46:29 +05304 add_unreconcile_btn(frm) {
5 if (frm.doc.docstatus == 1) {
Ankush Menatec74a5e2024-03-10 19:45:40 +05306 if (
7 (frm.doc.doctype == "Journal Entry" && frm.doc.voucher_type != "Journal Entry") ||
8 !["Purchase Invoice", "Sales Invoice", "Journal Entry", "Payment Entry"].includes(
9 frm.doc.doctype
10 )
11 ) {
ruthra kumar1d93d662023-09-05 09:59:34 +053012 return;
13 }
14
ruthra kumar25fe7522023-08-29 13:46:29 +053015 frappe.call({
Ankush Menatec74a5e2024-03-10 19:45:40 +053016 method: "erpnext.accounts.doctype.unreconcile_payment.unreconcile_payment.doc_has_references",
17 args: {
18 doctype: frm.doc.doctype,
19 docname: frm.doc.name,
ruthra kumar25fe7522023-08-29 13:46:29 +053020 },
Ankush Menatec74a5e2024-03-10 19:45:40 +053021 callback: function (r) {
ruthra kumar25fe7522023-08-29 13:46:29 +053022 if (r.message) {
Ankush Menatec74a5e2024-03-10 19:45:40 +053023 frm.add_custom_button(
24 __("UnReconcile"),
25 function () {
26 erpnext.accounts.unreconcile_payment.build_unreconcile_dialog(frm);
27 },
28 __("Actions")
29 );
ruthra kumar25fe7522023-08-29 13:46:29 +053030 }
Ankush Menatec74a5e2024-03-10 19:45:40 +053031 },
ruthra kumar25fe7522023-08-29 13:46:29 +053032 });
33 }
34 },
35
ruthra kumar1981f382023-08-29 15:15:14 +053036 build_selection_map(frm, selections) {
37 // assuming each row is an individual voucher
38 // pass this to server side method that creates unreconcile doc for each row
39 let selection_map = [];
Ankush Menatec74a5e2024-03-10 19:45:40 +053040 if (["Sales Invoice", "Purchase Invoice"].includes(frm.doc.doctype)) {
41 selection_map = selections.map(function (elem) {
ruthra kumar1981f382023-08-29 15:15:14 +053042 return {
43 company: elem.company,
44 voucher_type: elem.voucher_type,
45 voucher_no: elem.voucher_no,
46 against_voucher_type: frm.doc.doctype,
Ankush Menatec74a5e2024-03-10 19:45:40 +053047 against_voucher_no: frm.doc.name,
ruthra kumar1981f382023-08-29 15:15:14 +053048 };
49 });
Ankush Menatec74a5e2024-03-10 19:45:40 +053050 } else if (["Payment Entry", "Journal Entry"].includes(frm.doc.doctype)) {
51 selection_map = selections.map(function (elem) {
ruthra kumar1981f382023-08-29 15:15:14 +053052 return {
53 company: elem.company,
54 voucher_type: frm.doc.doctype,
55 voucher_no: frm.doc.name,
56 against_voucher_type: elem.voucher_type,
57 against_voucher_no: elem.voucher_no,
58 };
59 });
60 }
61 return selection_map;
62 },
63
ruthra kumar25fe7522023-08-29 13:46:29 +053064 build_unreconcile_dialog(frm) {
Ankush Menatec74a5e2024-03-10 19:45:40 +053065 if (
66 ["Sales Invoice", "Purchase Invoice", "Payment Entry", "Journal Entry"].includes(frm.doc.doctype)
67 ) {
ruthra kumar25fe7522023-08-29 13:46:29 +053068 let child_table_fields = [
Ankush Menatec74a5e2024-03-10 19:45:40 +053069 {
70 label: __("Voucher Type"),
71 fieldname: "voucher_type",
72 fieldtype: "Dynamic Link",
73 options: "DocType",
74 in_list_view: 1,
75 read_only: 1,
76 },
77 {
78 label: __("Voucher No"),
79 fieldname: "voucher_no",
80 fieldtype: "Link",
81 options: "voucher_type",
82 in_list_view: 1,
83 read_only: 1,
84 },
85 {
86 label: __("Allocated Amount"),
87 fieldname: "allocated_amount",
88 fieldtype: "Currency",
89 in_list_view: 1,
90 read_only: 1,
91 options: "account_currency",
92 },
93 { label: __("Currency"), fieldname: "account_currency", fieldtype: "Currency", read_only: 1 },
94 ];
ruthra kumar25fe7522023-08-29 13:46:29 +053095 let unreconcile_dialog_fields = [
96 {
Ankush Menatec74a5e2024-03-10 19:45:40 +053097 label: __("Allocations"),
98 fieldname: "allocations",
99 fieldtype: "Table",
ruthra kumar25fe7522023-08-29 13:46:29 +0530100 read_only: 1,
101 fields: child_table_fields,
102 },
103 ];
104
105 // get linked payments
106 frappe.call({
Ankush Menatec74a5e2024-03-10 19:45:40 +0530107 method: "erpnext.accounts.doctype.unreconcile_payment.unreconcile_payment.get_linked_payments_for_doc",
108 args: {
109 company: frm.doc.company,
110 doctype: frm.doc.doctype,
111 docname: frm.doc.name,
ruthra kumar25fe7522023-08-29 13:46:29 +0530112 },
Ankush Menatec74a5e2024-03-10 19:45:40 +0530113 callback: function (r) {
ruthra kumar25fe7522023-08-29 13:46:29 +0530114 if (r.message) {
115 // populate child table with allocations
116 unreconcile_dialog_fields[0].data = r.message;
Ankush Menatec74a5e2024-03-10 19:45:40 +0530117 unreconcile_dialog_fields[0].get_data = function () {
118 return r.message;
119 };
ruthra kumar25fe7522023-08-29 13:46:29 +0530120
121 let d = new frappe.ui.Dialog({
Ankush Menatec74a5e2024-03-10 19:45:40 +0530122 title: "UnReconcile Allocations",
ruthra kumar25fe7522023-08-29 13:46:29 +0530123 fields: unreconcile_dialog_fields,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530124 size: "large",
ruthra kumar6fd1c1b2023-09-05 08:53:10 +0530125 cannot_add_rows: true,
Ankush Menatec74a5e2024-03-10 19:45:40 +0530126 primary_action_label: "UnReconcile",
ruthra kumar25fe7522023-08-29 13:46:29 +0530127 primary_action(values) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530128 let selected_allocations = values.allocations.filter((x) => x.__checked);
ruthra kumar25fe7522023-08-29 13:46:29 +0530129 if (selected_allocations.length > 0) {
Ankush Menatec74a5e2024-03-10 19:45:40 +0530130 let selection_map =
131 erpnext.accounts.unreconcile_payment.build_selection_map(
132 frm,
133 selected_allocations
134 );
135 erpnext.accounts.unreconcile_payment.create_unreconcile_docs(
136 selection_map
137 );
ruthra kumar1981f382023-08-29 15:15:14 +0530138 d.hide();
ruthra kumar25fe7522023-08-29 13:46:29 +0530139 } else {
140 frappe.msgprint("No Selection");
141 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530142 },
ruthra kumar25fe7522023-08-29 13:46:29 +0530143 });
144
145 d.show();
146 }
Ankush Menatec74a5e2024-03-10 19:45:40 +0530147 },
ruthra kumar25fe7522023-08-29 13:46:29 +0530148 });
149 }
150 },
151
152 create_unreconcile_docs(selection_map) {
153 frappe.call({
Ankush Menatec74a5e2024-03-10 19:45:40 +0530154 method: "erpnext.accounts.doctype.unreconcile_payment.unreconcile_payment.create_unreconcile_doc_for_selection",
155 args: {
156 selections: selection_map,
ruthra kumar25fe7522023-08-29 13:46:29 +0530157 },
158 });
Ankush Menatec74a5e2024-03-10 19:45:40 +0530159 },
160};