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