ruthra kumar | 25fe752 | 2023-08-29 13:46:29 +0530 | [diff] [blame] | 1 | frappe.provide('erpnext.accounts'); |
| 2 | |
| 3 | erpnext.accounts.unreconcile_payments = { |
| 4 | add_unreconcile_btn(frm) { |
| 5 | if (frm.doc.docstatus == 1) { |
ruthra kumar | 1d93d66 | 2023-09-05 09:59:34 +0530 | [diff] [blame^] | 6 | if(((frm.doc.doctype == "Journal Entry") && (frm.doc.voucher_type != "Journal Entry")) |
| 7 | || !["Purchase Invoice", "Sales Invoice", "Journal Entry", "Payment Entry"].includes(frm.doc.doctype) |
| 8 | ) { |
| 9 | return; |
| 10 | } |
| 11 | |
ruthra kumar | 25fe752 | 2023-08-29 13:46:29 +0530 | [diff] [blame] | 12 | frappe.call({ |
ruthra kumar | 0ccb6d8 | 2023-08-29 21:49:21 +0530 | [diff] [blame] | 13 | "method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.doc_has_references", |
ruthra kumar | 25fe752 | 2023-08-29 13:46:29 +0530 | [diff] [blame] | 14 | "args": { |
| 15 | "doctype": frm.doc.doctype, |
| 16 | "docname": frm.doc.name |
| 17 | }, |
| 18 | callback: function(r) { |
| 19 | if (r.message) { |
| 20 | frm.add_custom_button(__("Un-Reconcile"), function() { |
| 21 | erpnext.accounts.unreconcile_payments.build_unreconcile_dialog(frm); |
| 22 | }); |
| 23 | } |
| 24 | } |
| 25 | }); |
| 26 | } |
| 27 | }, |
| 28 | |
ruthra kumar | 1981f38 | 2023-08-29 15:15:14 +0530 | [diff] [blame] | 29 | build_selection_map(frm, selections) { |
| 30 | // assuming each row is an individual voucher |
| 31 | // pass this to server side method that creates unreconcile doc for each row |
| 32 | let selection_map = []; |
| 33 | if (['Sales Invoice', 'Purchase Invoice'].includes(frm.doc.doctype)) { |
| 34 | selection_map = selections.map(function(elem) { |
| 35 | return { |
| 36 | company: elem.company, |
| 37 | voucher_type: elem.voucher_type, |
| 38 | voucher_no: elem.voucher_no, |
| 39 | against_voucher_type: frm.doc.doctype, |
| 40 | against_voucher_no: frm.doc.name |
| 41 | }; |
| 42 | }); |
| 43 | } else if (['Payment Entry', 'Journal Entry'].includes(frm.doc.doctype)) { |
| 44 | selection_map = selections.map(function(elem) { |
| 45 | return { |
| 46 | company: elem.company, |
| 47 | voucher_type: frm.doc.doctype, |
| 48 | voucher_no: frm.doc.name, |
| 49 | against_voucher_type: elem.voucher_type, |
| 50 | against_voucher_no: elem.voucher_no, |
| 51 | }; |
| 52 | }); |
| 53 | } |
| 54 | return selection_map; |
| 55 | }, |
| 56 | |
ruthra kumar | 25fe752 | 2023-08-29 13:46:29 +0530 | [diff] [blame] | 57 | build_unreconcile_dialog(frm) { |
| 58 | if (['Sales Invoice', 'Purchase Invoice', 'Payment Entry', 'Journal Entry'].includes(frm.doc.doctype)) { |
| 59 | let child_table_fields = [ |
| 60 | { label: __("Voucher Type"), fieldname: "voucher_type", fieldtype: "Dynamic Link", options: "DocType", in_list_view: 1, read_only: 1}, |
| 61 | { label: __("Voucher No"), fieldname: "voucher_no", fieldtype: "Link", options: "voucher_type", in_list_view: 1, read_only: 1 }, |
ruthra kumar | 6fd1c1b | 2023-09-05 08:53:10 +0530 | [diff] [blame] | 62 | { label: __("Allocated Amount"), fieldname: "allocated_amount", fieldtype: "Currency", in_list_view: 1, read_only: 1 , options: "account_currency"}, |
| 63 | { label: __("Currency"), fieldname: "account_currency", fieldtype: "Currency", read_only: 1}, |
ruthra kumar | 25fe752 | 2023-08-29 13:46:29 +0530 | [diff] [blame] | 64 | ] |
| 65 | let unreconcile_dialog_fields = [ |
| 66 | { |
| 67 | label: __('Allocations'), |
| 68 | fieldname: 'allocations', |
| 69 | fieldtype: 'Table', |
| 70 | read_only: 1, |
| 71 | fields: child_table_fields, |
| 72 | }, |
| 73 | ]; |
| 74 | |
| 75 | // get linked payments |
| 76 | frappe.call({ |
| 77 | "method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.get_linked_payments_for_doc", |
| 78 | "args": { |
| 79 | "company": frm.doc.company, |
| 80 | "doctype": frm.doc.doctype, |
| 81 | "docname": frm.doc.name |
| 82 | }, |
| 83 | callback: function(r) { |
| 84 | if (r.message) { |
| 85 | // populate child table with allocations |
| 86 | unreconcile_dialog_fields[0].data = r.message; |
| 87 | unreconcile_dialog_fields[0].get_data = function(){ return r.message}; |
| 88 | |
| 89 | let d = new frappe.ui.Dialog({ |
| 90 | title: 'Un-Reconcile Allocations', |
| 91 | fields: unreconcile_dialog_fields, |
| 92 | size: 'large', |
ruthra kumar | 6fd1c1b | 2023-09-05 08:53:10 +0530 | [diff] [blame] | 93 | cannot_add_rows: true, |
ruthra kumar | 25fe752 | 2023-08-29 13:46:29 +0530 | [diff] [blame] | 94 | primary_action_label: 'Un-Reconcile', |
| 95 | primary_action(values) { |
| 96 | |
| 97 | let selected_allocations = values.allocations.filter(x=>x.__checked); |
| 98 | if (selected_allocations.length > 0) { |
ruthra kumar | 1981f38 | 2023-08-29 15:15:14 +0530 | [diff] [blame] | 99 | let selection_map = erpnext.accounts.unreconcile_payments.build_selection_map(frm, selected_allocations); |
| 100 | erpnext.accounts.unreconcile_payments.create_unreconcile_docs(selection_map); |
| 101 | d.hide(); |
ruthra kumar | 25fe752 | 2023-08-29 13:46:29 +0530 | [diff] [blame] | 102 | |
| 103 | } else { |
| 104 | frappe.msgprint("No Selection"); |
| 105 | } |
| 106 | } |
| 107 | }); |
| 108 | |
| 109 | d.show(); |
| 110 | } |
| 111 | } |
| 112 | }); |
| 113 | } |
| 114 | }, |
| 115 | |
| 116 | create_unreconcile_docs(selection_map) { |
| 117 | frappe.call({ |
| 118 | "method": "erpnext.accounts.doctype.unreconcile_payments.unreconcile_payments.create_unreconcile_doc_for_selection", |
| 119 | "args": { |
| 120 | "selections": selection_map |
| 121 | }, |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | } |