Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors |
Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 3 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 4 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 5 | import copy |
Nabin Hait | 5b0ec64 | 2022-01-31 18:07:04 +0530 | [diff] [blame] | 6 | |
| 7 | import frappe |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 8 | from frappe import _ |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 9 | from frappe.model.meta import get_field_precision |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 10 | from frappe.utils import cint, flt, formatdate, getdate, now |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 11 | |
| 12 | import erpnext |
| 13 | from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( |
| 14 | get_accounting_dimensions, |
| 15 | ) |
Deepesh Garg | b834ed1 | 2024-02-05 14:05:01 +0530 | [diff] [blame] | 16 | from erpnext.accounts.doctype.accounting_dimension_filter.accounting_dimension_filter import ( |
| 17 | get_dimension_filter_map, |
| 18 | ) |
mergify[bot] | b4db5e9 | 2023-07-18 17:40:49 +0530 | [diff] [blame] | 19 | from erpnext.accounts.doctype.accounting_period.accounting_period import ClosedAccountingPeriod |
Nabin Hait | b9bc7d6 | 2016-05-16 14:38:47 +0530 | [diff] [blame] | 20 | from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget |
ruthra kumar | e888975 | 2022-05-16 14:28:25 +0530 | [diff] [blame] | 21 | from erpnext.accounts.utils import create_payment_ledger_entry |
Deepesh Garg | b834ed1 | 2024-02-05 14:05:01 +0530 | [diff] [blame] | 22 | from erpnext.exceptions import InvalidAccountDimensionError, MandatoryAccountDimensionError |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 23 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 24 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 25 | def make_gl_entries( |
| 26 | gl_map, |
| 27 | cancel=False, |
| 28 | adv_adj=False, |
| 29 | merge_entries=True, |
| 30 | update_outstanding="Yes", |
| 31 | from_repost=False, |
| 32 | ): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 33 | if gl_map: |
| 34 | if not cancel: |
Deepesh Garg | ecca9cb | 2023-07-29 11:52:54 +0530 | [diff] [blame] | 35 | make_acc_dimensions_offsetting_entry(gl_map) |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 36 | validate_accounting_period(gl_map) |
Saqib Ansari | 95b059a | 2022-05-11 13:26:15 +0530 | [diff] [blame] | 37 | validate_disabled_accounts(gl_map) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 38 | gl_map = process_gl_map(gl_map, merge_entries) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 39 | if gl_map and len(gl_map) > 1: |
ruthra kumar | 7312f22 | 2022-05-29 21:33:08 +0530 | [diff] [blame] | 40 | create_payment_ledger_entry( |
| 41 | gl_map, |
| 42 | cancel=0, |
| 43 | adv_adj=adv_adj, |
| 44 | update_outstanding=update_outstanding, |
| 45 | from_repost=from_repost, |
| 46 | ) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 47 | save_entries(gl_map, adv_adj, update_outstanding, from_repost) |
Deepesh Garg | 9474908 | 2023-10-20 19:49:41 +0530 | [diff] [blame] | 48 | # Post GL Map process there may no be any GL Entries |
Deepesh Garg | 4c8d15b | 2021-04-26 15:24:34 +0530 | [diff] [blame] | 49 | elif gl_map: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 50 | frappe.throw( |
| 51 | _( |
| 52 | "Incorrect number of General Ledger Entries found. You might have selected a wrong Account in the transaction." |
| 53 | ) |
| 54 | ) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 55 | else: |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 56 | make_reverse_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 57 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 58 | |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 59 | def make_acc_dimensions_offsetting_entry(gl_map): |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 60 | accounting_dimensions_to_offset = get_accounting_dimensions_for_offsetting_entry( |
| 61 | gl_map, gl_map[0].company |
| 62 | ) |
Gursheen Anand | 1e1e4b9 | 2023-07-18 12:12:24 +0530 | [diff] [blame] | 63 | no_of_dimensions = len(accounting_dimensions_to_offset) |
| 64 | if no_of_dimensions == 0: |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 65 | return |
| 66 | |
| 67 | offsetting_entries = [] |
Deepesh Garg | ecca9cb | 2023-07-29 11:52:54 +0530 | [diff] [blame] | 68 | |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 69 | for gle in gl_map: |
| 70 | for dimension in accounting_dimensions_to_offset: |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 71 | offsetting_entry = gle.copy() |
Gursheen Anand | 4f9242d | 2023-07-27 15:45:48 +0530 | [diff] [blame] | 72 | debit = flt(gle.credit) / no_of_dimensions if gle.credit != 0 else 0 |
| 73 | credit = flt(gle.debit) / no_of_dimensions if gle.debit != 0 else 0 |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 74 | offsetting_entry.update( |
| 75 | { |
Gursheen Anand | 4f9242d | 2023-07-27 15:45:48 +0530 | [diff] [blame] | 76 | "account": dimension.offsetting_account, |
| 77 | "debit": debit, |
| 78 | "credit": credit, |
| 79 | "debit_in_account_currency": debit, |
| 80 | "credit_in_account_currency": credit, |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 81 | "remarks": _("Offsetting for Accounting Dimension") + " - {0}".format(dimension.name), |
| 82 | "against_voucher": None, |
| 83 | } |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 84 | ) |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 85 | offsetting_entry["against_voucher_type"] = None |
| 86 | offsetting_entries.append(offsetting_entry) |
Deepesh Garg | ecca9cb | 2023-07-29 11:52:54 +0530 | [diff] [blame] | 87 | |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 88 | gl_map += offsetting_entries |
| 89 | |
| 90 | |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 91 | def get_accounting_dimensions_for_offsetting_entry(gl_map, company): |
| 92 | acc_dimension = frappe.qb.DocType("Accounting Dimension") |
| 93 | dimension_detail = frappe.qb.DocType("Accounting Dimension Detail") |
Deepesh Garg | ecca9cb | 2023-07-29 11:52:54 +0530 | [diff] [blame] | 94 | |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 95 | acc_dimensions = ( |
| 96 | frappe.qb.from_(acc_dimension) |
| 97 | .inner_join(dimension_detail) |
| 98 | .on(acc_dimension.name == dimension_detail.parent) |
Gursheen Anand | 4f9242d | 2023-07-27 15:45:48 +0530 | [diff] [blame] | 99 | .select(acc_dimension.fieldname, acc_dimension.name, dimension_detail.offsetting_account) |
Gursheen Anand | 3a3ffa2 | 2023-07-18 12:51:09 +0530 | [diff] [blame] | 100 | .where( |
| 101 | (acc_dimension.disabled == 0) |
| 102 | & (dimension_detail.company == company) |
| 103 | & (dimension_detail.automatically_post_balancing_accounting_entry == 1) |
| 104 | ) |
| 105 | ).run(as_dict=True) |
Deepesh Garg | ecca9cb | 2023-07-29 11:52:54 +0530 | [diff] [blame] | 106 | |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 107 | accounting_dimensions_to_offset = [] |
| 108 | for acc_dimension in acc_dimensions: |
Gursheen Anand | e19a6f5 | 2023-07-19 12:26:57 +0530 | [diff] [blame] | 109 | values = set([entry.get(acc_dimension.fieldname) for entry in gl_map]) |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 110 | if len(values) > 1: |
| 111 | accounting_dimensions_to_offset.append(acc_dimension) |
Deepesh Garg | ecca9cb | 2023-07-29 11:52:54 +0530 | [diff] [blame] | 112 | |
Gursheen Anand | 22ba121 | 2023-07-17 15:17:53 +0530 | [diff] [blame] | 113 | return accounting_dimensions_to_offset |
| 114 | |
| 115 | |
Saqib Ansari | 95b059a | 2022-05-11 13:26:15 +0530 | [diff] [blame] | 116 | def validate_disabled_accounts(gl_map): |
| 117 | accounts = [d.account for d in gl_map if d.account] |
| 118 | |
| 119 | Account = frappe.qb.DocType("Account") |
| 120 | |
| 121 | disabled_accounts = ( |
| 122 | frappe.qb.from_(Account) |
| 123 | .where(Account.name.isin(accounts) & Account.disabled == 1) |
| 124 | .select(Account.name, Account.disabled) |
| 125 | ).run(as_dict=True) |
| 126 | |
| 127 | if disabled_accounts: |
| 128 | account_list = "<br>" |
| 129 | account_list += ", ".join([frappe.bold(d.name) for d in disabled_accounts]) |
| 130 | frappe.throw( |
| 131 | _("Cannot create accounting entries against disabled accounts: {0}").format(account_list), |
| 132 | title=_("Disabled Account Selected"), |
| 133 | ) |
| 134 | |
| 135 | |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 136 | def validate_accounting_period(gl_map): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 137 | accounting_periods = frappe.db.sql( |
| 138 | """ SELECT |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 139 | ap.name as name |
| 140 | FROM |
| 141 | `tabAccounting Period` ap, `tabClosed Document` cd |
| 142 | WHERE |
| 143 | ap.name = cd.parent |
| 144 | AND ap.company = %(company)s |
| 145 | AND cd.closed = 1 |
| 146 | AND cd.document_type = %(voucher_type)s |
| 147 | AND %(date)s between ap.start_date and ap.end_date |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 148 | """, |
| 149 | { |
| 150 | "date": gl_map[0].posting_date, |
| 151 | "company": gl_map[0].company, |
| 152 | "voucher_type": gl_map[0].voucher_type, |
| 153 | }, |
| 154 | as_dict=1, |
| 155 | ) |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 156 | |
| 157 | if accounting_periods: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 158 | frappe.throw( |
| 159 | _( |
| 160 | "You cannot create or cancel any accounting entries with in the closed Accounting Period {0}" |
| 161 | ).format(frappe.bold(accounting_periods[0].name)), |
| 162 | ClosedAccountingPeriod, |
| 163 | ) |
| 164 | |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 165 | |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 166 | def process_gl_map(gl_map, merge_entries=True, precision=None): |
Nabin Hait | 6099af5 | 2022-01-31 17:24:50 +0530 | [diff] [blame] | 167 | if not gl_map: |
| 168 | return [] |
| 169 | |
Nabin Hait | 666d961 | 2023-07-26 13:03:29 +0530 | [diff] [blame] | 170 | if gl_map[0].voucher_type != "Period Closing Voucher": |
| 171 | gl_map = distribute_gl_based_on_cost_center_allocation(gl_map, precision) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 172 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 173 | if merge_entries: |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 174 | gl_map = merge_similar_entries(gl_map, precision) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 175 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 176 | gl_map = toggle_debit_credit_if_negative(gl_map) |
Deepesh Garg | 5ba3b28 | 2021-11-25 15:42:30 +0530 | [diff] [blame] | 177 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 178 | return gl_map |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 179 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 180 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 181 | def distribute_gl_based_on_cost_center_allocation(gl_map, precision=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 182 | cost_center_allocation = get_cost_center_allocation_data( |
| 183 | gl_map[0]["company"], gl_map[0]["posting_date"] |
| 184 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 185 | if not cost_center_allocation: |
| 186 | return gl_map |
Deepesh Garg | 5ba3b28 | 2021-11-25 15:42:30 +0530 | [diff] [blame] | 187 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 188 | new_gl_map = [] |
| 189 | for d in gl_map: |
| 190 | cost_center = d.get("cost_center") |
Deepesh Garg | 4e26d42 | 2022-10-30 19:33:27 +0530 | [diff] [blame] | 191 | |
| 192 | # Validate budget against main cost center |
| 193 | validate_expense_against_budget( |
| 194 | d, expense_amount=flt(d.debit, precision) - flt(d.credit, precision) |
| 195 | ) |
| 196 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 197 | if cost_center and cost_center_allocation.get(cost_center): |
| 198 | for sub_cost_center, percentage in cost_center_allocation.get(cost_center, {}).items(): |
| 199 | gle = copy.deepcopy(d) |
| 200 | gle.cost_center = sub_cost_center |
ruthra kumar | 71f6f78 | 2022-06-24 13:36:15 +0530 | [diff] [blame] | 201 | for field in ("debit", "credit", "debit_in_account_currency", "credit_in_account_currency"): |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 202 | gle[field] = flt(flt(d.get(field)) * percentage / 100, precision) |
| 203 | new_gl_map.append(gle) |
| 204 | else: |
| 205 | new_gl_map.append(d) |
| 206 | |
| 207 | return new_gl_map |
| 208 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 209 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 210 | def get_cost_center_allocation_data(company, posting_date): |
| 211 | par = frappe.qb.DocType("Cost Center Allocation") |
| 212 | child = frappe.qb.DocType("Cost Center Allocation Percentage") |
| 213 | |
| 214 | records = ( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 215 | frappe.qb.from_(par) |
| 216 | .inner_join(child) |
| 217 | .on(par.name == child.parent) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 218 | .select(par.main_cost_center, child.cost_center, child.percentage) |
| 219 | .where(par.docstatus == 1) |
| 220 | .where(par.company == company) |
| 221 | .where(par.valid_from <= posting_date) |
| 222 | .orderby(par.valid_from, order=frappe.qb.desc) |
| 223 | ).run(as_dict=True) |
| 224 | |
| 225 | cc_allocation = frappe._dict() |
| 226 | for d in records: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 227 | cc_allocation.setdefault(d.main_cost_center, frappe._dict()).setdefault( |
| 228 | d.cost_center, d.percentage |
| 229 | ) |
Nabin Hait | 5b0ec64 | 2022-01-31 18:07:04 +0530 | [diff] [blame] | 230 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 231 | return cc_allocation |
Deepesh Garg | 5ba3b28 | 2021-11-25 15:42:30 +0530 | [diff] [blame] | 232 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 233 | |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 234 | def merge_similar_entries(gl_map, precision=None): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 235 | merged_gl_map = [] |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 236 | accounting_dimensions = get_accounting_dimensions() |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 237 | merge_properties = get_merge_properties(accounting_dimensions) |
Nabin Hait | 914a388 | 2022-07-19 12:32:54 +0530 | [diff] [blame] | 238 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 239 | for entry in gl_map: |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 240 | entry.merge_key = get_merge_key(entry, merge_properties) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 241 | # if there is already an entry in this account then just add it |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 242 | # to that entry |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 243 | same_head = check_if_in_list(entry, merged_gl_map) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 244 | if same_head: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 245 | same_head.debit = flt(same_head.debit) + flt(entry.debit) |
| 246 | same_head.debit_in_account_currency = flt(same_head.debit_in_account_currency) + flt( |
| 247 | entry.debit_in_account_currency |
| 248 | ) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 249 | same_head.credit = flt(same_head.credit) + flt(entry.credit) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 250 | same_head.credit_in_account_currency = flt(same_head.credit_in_account_currency) + flt( |
| 251 | entry.credit_in_account_currency |
| 252 | ) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 253 | else: |
| 254 | merged_gl_map.append(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 255 | |
thefalconx33 | bfc43d3 | 2019-12-13 15:09:51 +0530 | [diff] [blame] | 256 | company = gl_map[0].company if gl_map else erpnext.get_default_company() |
| 257 | company_currency = erpnext.get_company_currency(company) |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 258 | |
| 259 | if not precision: |
| 260 | precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"), company_currency) |
thefalconx33 | bfc43d3 | 2019-12-13 15:09:51 +0530 | [diff] [blame] | 261 | |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 262 | # filter zero debit and credit entries |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 263 | merged_gl_map = filter( |
ruthra kumar | 914b230 | 2023-01-02 14:33:14 +0530 | [diff] [blame] | 264 | lambda x: flt(x.debit, precision) != 0 |
| 265 | or flt(x.credit, precision) != 0 |
| 266 | or ( |
| 267 | x.voucher_type == "Journal Entry" |
| 268 | and frappe.get_cached_value("Journal Entry", x.voucher_no, "voucher_type") |
| 269 | == "Exchange Gain Or Loss" |
| 270 | ), |
| 271 | merged_gl_map, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 272 | ) |
Achilles Rasquinha | 908289d | 2018-03-08 13:10:51 +0530 | [diff] [blame] | 273 | merged_gl_map = list(merged_gl_map) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 274 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 275 | return merged_gl_map |
| 276 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 277 | |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 278 | def get_merge_properties(dimensions=None): |
| 279 | merge_properties = [ |
| 280 | "account", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 281 | "cost_center", |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 282 | "party", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 283 | "party_type", |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 284 | "voucher_detail_no", |
| 285 | "against_voucher", |
| 286 | "against_voucher_type", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 287 | "project", |
| 288 | "finance_book", |
Gursheen Anand | 442e3f2 | 2023-06-16 13:38:47 +0530 | [diff] [blame] | 289 | "voucher_no", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 290 | ] |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 291 | if dimensions: |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 292 | merge_properties.extend(dimensions) |
| 293 | return merge_properties |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 294 | |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 295 | |
| 296 | def get_merge_key(entry, merge_properties): |
| 297 | merge_key = [] |
| 298 | for fieldname in merge_properties: |
| 299 | merge_key.append(entry.get(fieldname, "")) |
| 300 | |
| 301 | return tuple(merge_key) |
| 302 | |
| 303 | |
| 304 | def check_if_in_list(gle, gl_map): |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 305 | for e in gl_map: |
Nabin Hait | 26202d9 | 2024-03-13 18:17:41 +0530 | [diff] [blame^] | 306 | if e.merge_key == gle.merge_key: |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 307 | return e |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 308 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 309 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 310 | def toggle_debit_credit_if_negative(gl_map): |
| 311 | for entry in gl_map: |
| 312 | # toggle debit, credit if negative entry |
| 313 | if flt(entry.debit) < 0: |
| 314 | entry.credit = flt(entry.credit) - flt(entry.debit) |
| 315 | entry.debit = 0.0 |
| 316 | |
| 317 | if flt(entry.debit_in_account_currency) < 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 318 | entry.credit_in_account_currency = flt(entry.credit_in_account_currency) - flt( |
| 319 | entry.debit_in_account_currency |
| 320 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 321 | entry.debit_in_account_currency = 0.0 |
| 322 | |
| 323 | if flt(entry.credit) < 0: |
| 324 | entry.debit = flt(entry.debit) - flt(entry.credit) |
| 325 | entry.credit = 0.0 |
| 326 | |
| 327 | if flt(entry.credit_in_account_currency) < 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 328 | entry.debit_in_account_currency = flt(entry.debit_in_account_currency) - flt( |
| 329 | entry.credit_in_account_currency |
| 330 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 331 | entry.credit_in_account_currency = 0.0 |
| 332 | |
| 333 | update_net_values(entry) |
| 334 | |
| 335 | return gl_map |
| 336 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 337 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 338 | def update_net_values(entry): |
| 339 | # In some scenarios net value needs to be shown in the ledger |
| 340 | # This method updates net values as debit or credit |
| 341 | if entry.post_net_value and entry.debit and entry.credit: |
| 342 | if entry.debit > entry.credit: |
| 343 | entry.debit = entry.debit - entry.credit |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 344 | entry.debit_in_account_currency = ( |
| 345 | entry.debit_in_account_currency - entry.credit_in_account_currency |
| 346 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 347 | entry.credit = 0 |
| 348 | entry.credit_in_account_currency = 0 |
| 349 | else: |
| 350 | entry.credit = entry.credit - entry.debit |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 351 | entry.credit_in_account_currency = ( |
| 352 | entry.credit_in_account_currency - entry.debit_in_account_currency |
| 353 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 354 | |
| 355 | entry.debit = 0 |
| 356 | entry.debit_in_account_currency = 0 |
| 357 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 358 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 359 | def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False): |
| 360 | if not from_repost: |
| 361 | validate_cwip_accounts(gl_map) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 362 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 363 | process_debit_credit_difference(gl_map) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 364 | |
Deepesh Garg | b834ed1 | 2024-02-05 14:05:01 +0530 | [diff] [blame] | 365 | dimension_filter_map = get_dimension_filter_map() |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 366 | if gl_map: |
Deepesh Garg | 3aa17fa | 2024-01-17 12:47:27 +0530 | [diff] [blame] | 367 | check_freezing_date(gl_map[0]["posting_date"], adv_adj) |
Deepesh Garg | f92c63f | 2023-02-26 15:53:33 +0530 | [diff] [blame] | 368 | is_opening = any(d.get("is_opening") == "Yes" for d in gl_map) |
| 369 | if gl_map[0]["voucher_type"] != "Period Closing Voucher": |
| 370 | validate_against_pcv(is_opening, gl_map[0]["posting_date"], gl_map[0]["company"]) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 371 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 372 | for entry in gl_map: |
Deepesh Garg | b834ed1 | 2024-02-05 14:05:01 +0530 | [diff] [blame] | 373 | validate_allowed_dimensions(entry, dimension_filter_map) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 374 | make_entry(entry, adv_adj, update_outstanding, from_repost) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 375 | |
Nabin Hait | 914a388 | 2022-07-19 12:32:54 +0530 | [diff] [blame] | 376 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 377 | def make_entry(args, adv_adj, update_outstanding, from_repost=False): |
Nabin Hait | ddc3bb2 | 2020-03-17 17:04:18 +0530 | [diff] [blame] | 378 | gle = frappe.new_doc("GL Entry") |
| 379 | gle.update(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 380 | gle.flags.ignore_permissions = 1 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 381 | gle.flags.from_repost = from_repost |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 382 | gle.flags.adv_adj = adv_adj |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 383 | gle.flags.update_outstanding = update_outstanding or "Yes" |
Nabin Hait | 4caaab3 | 2022-07-18 17:59:42 +0530 | [diff] [blame] | 384 | gle.flags.notify_update = False |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 385 | gle.submit() |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 386 | |
Nabin Hait | 4caaab3 | 2022-07-18 17:59:42 +0530 | [diff] [blame] | 387 | if not from_repost and gle.voucher_type != "Period Closing Voucher": |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 388 | validate_expense_against_budget(args) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 389 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 390 | |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 391 | def validate_cwip_accounts(gl_map): |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 392 | """Validate that CWIP account are not used in Journal Entry""" |
| 393 | if gl_map and gl_map[0].voucher_type != "Journal Entry": |
| 394 | return |
Marica | d00c598 | 2019-11-12 19:17:43 +0530 | [diff] [blame] | 395 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 396 | cwip_enabled = any( |
| 397 | cint(ac.enable_cwip_accounting) |
| 398 | for ac in frappe.db.get_all("Asset Category", "enable_cwip_accounting") |
| 399 | ) |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 400 | if cwip_enabled: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 401 | cwip_accounts = [ |
| 402 | d[0] |
| 403 | for d in frappe.db.sql( |
| 404 | """select name from tabAccount |
| 405 | where account_type = 'Capital Work in Progress' and is_group=0""" |
| 406 | ) |
| 407 | ] |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 408 | |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 409 | for entry in gl_map: |
| 410 | if entry.account in cwip_accounts: |
| 411 | frappe.throw( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 412 | _( |
| 413 | "Account: <b>{0}</b> is capital Work in progress and can not be updated by Journal Entry" |
| 414 | ).format(entry.account) |
| 415 | ) |
| 416 | |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 417 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 418 | def process_debit_credit_difference(gl_map): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 419 | precision = get_field_precision( |
| 420 | frappe.get_meta("GL Entry").get_field("debit"), |
| 421 | currency=frappe.get_cached_value("Company", gl_map[0].company, "default_currency"), |
| 422 | ) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 423 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 424 | voucher_type = gl_map[0].voucher_type |
| 425 | voucher_no = gl_map[0].voucher_no |
| 426 | allowance = get_debit_credit_allowance(voucher_type, precision) |
| 427 | |
| 428 | debit_credit_diff = get_debit_credit_difference(gl_map, precision) |
ruthra kumar | 914b230 | 2023-01-02 14:33:14 +0530 | [diff] [blame] | 429 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 430 | if abs(debit_credit_diff) > allowance: |
ruthra kumar | 914b230 | 2023-01-02 14:33:14 +0530 | [diff] [blame] | 431 | if not ( |
| 432 | voucher_type == "Journal Entry" |
| 433 | and frappe.get_cached_value("Journal Entry", voucher_no, "voucher_type") |
| 434 | == "Exchange Gain Or Loss" |
| 435 | ): |
| 436 | raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no) |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 437 | |
| 438 | elif abs(debit_credit_diff) >= (1.0 / (10**precision)): |
| 439 | make_round_off_gle(gl_map, debit_credit_diff, precision) |
| 440 | |
| 441 | debit_credit_diff = get_debit_credit_difference(gl_map, precision) |
| 442 | if abs(debit_credit_diff) > allowance: |
ruthra kumar | 914b230 | 2023-01-02 14:33:14 +0530 | [diff] [blame] | 443 | if not ( |
| 444 | voucher_type == "Journal Entry" |
| 445 | and frappe.get_cached_value("Journal Entry", voucher_no, "voucher_type") |
| 446 | == "Exchange Gain Or Loss" |
| 447 | ): |
| 448 | raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no) |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 449 | |
| 450 | |
| 451 | def get_debit_credit_difference(gl_map, precision): |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 452 | debit_credit_diff = 0.0 |
| 453 | for entry in gl_map: |
| 454 | entry.debit = flt(entry.debit, precision) |
| 455 | entry.credit = flt(entry.credit, precision) |
| 456 | debit_credit_diff += entry.debit - entry.credit |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 457 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 458 | debit_credit_diff = flt(debit_credit_diff, precision) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 459 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 460 | return debit_credit_diff |
| 461 | |
| 462 | |
| 463 | def get_debit_credit_allowance(voucher_type, precision): |
| 464 | if voucher_type in ("Journal Entry", "Payment Entry"): |
Nabin Hait | fb24a27 | 2016-02-18 19:18:07 +0530 | [diff] [blame] | 465 | allowance = 5.0 / (10**precision) |
| 466 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 467 | allowance = 0.5 |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 468 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 469 | return allowance |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 470 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 471 | |
| 472 | def raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no): |
| 473 | frappe.throw( |
| 474 | _("Debit and Credit not equal for {0} #{1}. Difference is {2}.").format( |
| 475 | voucher_type, voucher_no, debit_credit_diff |
| 476 | ) |
| 477 | ) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 478 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 479 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 480 | def make_round_off_gle(gl_map, debit_credit_diff, precision): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 481 | round_off_account, round_off_cost_center = get_round_off_account_and_cost_center( |
Deepesh Garg | 0ac11a5 | 2022-04-20 12:18:11 +0530 | [diff] [blame] | 482 | gl_map[0].company, gl_map[0].voucher_type, gl_map[0].voucher_no |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 483 | ) |
Nabin Hait | 80069a6 | 2015-05-28 19:19:59 +0530 | [diff] [blame] | 484 | round_off_gle = frappe._dict() |
Nabin Hait | 022d8d5 | 2022-11-21 15:16:53 +0530 | [diff] [blame] | 485 | round_off_account_exists = False |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 486 | |
Nabin Hait | 022d8d5 | 2022-11-21 15:16:53 +0530 | [diff] [blame] | 487 | if gl_map[0].voucher_type != "Period Closing Voucher": |
| 488 | for d in gl_map: |
| 489 | if d.account == round_off_account: |
| 490 | round_off_gle = d |
| 491 | if d.debit: |
| 492 | debit_credit_diff -= flt(d.debit) - flt(d.credit) |
| 493 | else: |
| 494 | debit_credit_diff += flt(d.credit) |
| 495 | round_off_account_exists = True |
| 496 | |
| 497 | if round_off_account_exists and abs(debit_credit_diff) < (1.0 / (10**precision)): |
| 498 | gl_map.remove(round_off_gle) |
| 499 | return |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 500 | |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 501 | if not round_off_gle: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 502 | for k in ["voucher_type", "voucher_no", "company", "posting_date", "remarks"]: |
| 503 | round_off_gle[k] = gl_map[0][k] |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 504 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 505 | round_off_gle.update( |
| 506 | { |
| 507 | "account": round_off_account, |
| 508 | "debit_in_account_currency": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, |
| 509 | "credit_in_account_currency": debit_credit_diff if debit_credit_diff > 0 else 0, |
| 510 | "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, |
| 511 | "credit": debit_credit_diff if debit_credit_diff > 0 else 0, |
| 512 | "cost_center": round_off_cost_center, |
| 513 | "party_type": None, |
| 514 | "party": None, |
| 515 | "is_opening": "No", |
| 516 | "against_voucher_type": None, |
| 517 | "against_voucher": None, |
| 518 | } |
| 519 | ) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 520 | |
Deepesh Garg | 015812b | 2022-04-23 21:40:08 +0530 | [diff] [blame] | 521 | update_accounting_dimensions(round_off_gle) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 522 | if not round_off_account_exists: |
| 523 | gl_map.append(round_off_gle) |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 524 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 525 | |
Deepesh Garg | 015812b | 2022-04-23 21:40:08 +0530 | [diff] [blame] | 526 | def update_accounting_dimensions(round_off_gle): |
| 527 | dimensions = get_accounting_dimensions() |
Deepesh Garg | c312cd3 | 2022-04-24 18:11:32 +0530 | [diff] [blame] | 528 | meta = frappe.get_meta(round_off_gle["voucher_type"]) |
| 529 | has_all_dimensions = True |
Deepesh Garg | 015812b | 2022-04-23 21:40:08 +0530 | [diff] [blame] | 530 | |
| 531 | for dimension in dimensions: |
Deepesh Garg | c312cd3 | 2022-04-24 18:11:32 +0530 | [diff] [blame] | 532 | if not meta.has_field(dimension): |
| 533 | has_all_dimensions = False |
| 534 | |
| 535 | if dimensions and has_all_dimensions: |
| 536 | dimension_values = frappe.db.get_value( |
Deepesh Garg | 3fa1c63 | 2022-04-25 16:29:26 +0530 | [diff] [blame] | 537 | round_off_gle["voucher_type"], round_off_gle["voucher_no"], dimensions, as_dict=1 |
Deepesh Garg | c312cd3 | 2022-04-24 18:11:32 +0530 | [diff] [blame] | 538 | ) |
| 539 | |
| 540 | for dimension in dimensions: |
| 541 | round_off_gle[dimension] = dimension_values.get(dimension) |
Deepesh Garg | 015812b | 2022-04-23 21:40:08 +0530 | [diff] [blame] | 542 | |
| 543 | |
ruthra kumar | ebe6787 | 2023-04-28 14:07:28 +0530 | [diff] [blame] | 544 | def get_round_off_account_and_cost_center( |
| 545 | company, voucher_type, voucher_no, use_company_default=False |
| 546 | ): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 547 | round_off_account, round_off_cost_center = frappe.get_cached_value( |
| 548 | "Company", company, ["round_off_account", "round_off_cost_center"] |
| 549 | ) or [None, None] |
Deepesh Garg | 0ac11a5 | 2022-04-20 12:18:11 +0530 | [diff] [blame] | 550 | |
Deepesh Garg | 5f75aea | 2023-08-24 17:58:51 +0530 | [diff] [blame] | 551 | # Use expense account as fallback |
| 552 | if not round_off_account: |
| 553 | round_off_account = frappe.get_cached_value("Company", company, "default_expense_account") |
| 554 | |
Deepesh Garg | c312cd3 | 2022-04-24 18:11:32 +0530 | [diff] [blame] | 555 | meta = frappe.get_meta(voucher_type) |
| 556 | |
Deepesh Garg | 0ac11a5 | 2022-04-20 12:18:11 +0530 | [diff] [blame] | 557 | # Give first preference to parent cost center for round off GLE |
ruthra kumar | ebe6787 | 2023-04-28 14:07:28 +0530 | [diff] [blame] | 558 | if not use_company_default and meta.has_field("cost_center"): |
Deepesh Garg | 0ac11a5 | 2022-04-20 12:18:11 +0530 | [diff] [blame] | 559 | parent_cost_center = frappe.db.get_value(voucher_type, voucher_no, "cost_center") |
| 560 | if parent_cost_center: |
| 561 | round_off_cost_center = parent_cost_center |
| 562 | |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 563 | if not round_off_account: |
| 564 | frappe.throw(_("Please mention Round Off Account in Company")) |
| 565 | |
| 566 | if not round_off_cost_center: |
| 567 | frappe.throw(_("Please mention Round Off Cost Center in Company")) |
| 568 | |
| 569 | return round_off_account, round_off_cost_center |
| 570 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 571 | |
| 572 | def make_reverse_gl_entries( |
Deepesh Garg | da6bc1a | 2023-06-23 20:57:51 +0530 | [diff] [blame] | 573 | gl_entries=None, |
| 574 | voucher_type=None, |
| 575 | voucher_no=None, |
| 576 | adv_adj=False, |
| 577 | update_outstanding="Yes", |
| 578 | partial_cancel=False, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 579 | ): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 580 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 581 | Get original gl entries of the voucher |
| 582 | and make reverse gl entries by swapping debit and credit |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 583 | """ |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 584 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 585 | if not gl_entries: |
Deepesh Garg | ff57450 | 2022-02-06 22:56:12 +0530 | [diff] [blame] | 586 | gl_entry = frappe.qb.DocType("GL Entry") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 587 | gl_entries = ( |
| 588 | frappe.qb.from_(gl_entry) |
| 589 | .select("*") |
| 590 | .where(gl_entry.voucher_type == voucher_type) |
| 591 | .where(gl_entry.voucher_no == voucher_no) |
| 592 | .where(gl_entry.is_cancelled == 0) |
| 593 | .for_update() |
| 594 | ).run(as_dict=1) |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 595 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 596 | if gl_entries: |
ruthra kumar | 7312f22 | 2022-05-29 21:33:08 +0530 | [diff] [blame] | 597 | create_payment_ledger_entry( |
Deepesh Garg | 1e078d0 | 2023-06-29 12:18:25 +0530 | [diff] [blame] | 598 | gl_entries, |
| 599 | cancel=1, |
| 600 | adv_adj=adv_adj, |
| 601 | update_outstanding=update_outstanding, |
| 602 | partial_cancel=partial_cancel, |
ruthra kumar | 7312f22 | 2022-05-29 21:33:08 +0530 | [diff] [blame] | 603 | ) |
Deepesh Garg | 069a54e | 2020-08-10 16:01:01 +0530 | [diff] [blame] | 604 | validate_accounting_period(gl_entries) |
Deepesh Garg | 3aa17fa | 2024-01-17 12:47:27 +0530 | [diff] [blame] | 605 | check_freezing_date(gl_entries[0]["posting_date"], adv_adj) |
Deepesh Garg | f92c63f | 2023-02-26 15:53:33 +0530 | [diff] [blame] | 606 | |
| 607 | is_opening = any(d.get("is_opening") == "Yes" for d in gl_entries) |
| 608 | validate_against_pcv(is_opening, gl_entries[0]["posting_date"], gl_entries[0]["company"]) |
ruthra kumar | 2633d7d | 2023-11-28 17:05:29 +0530 | [diff] [blame] | 609 | if partial_cancel: |
| 610 | # Partial cancel is only used by `Advance` in separate account feature. |
| 611 | # Only cancel GL entries for unlinked reference using `voucher_detail_no` |
| 612 | gle = frappe.qb.DocType("GL Entry") |
| 613 | for x in gl_entries: |
| 614 | query = ( |
| 615 | frappe.qb.update(gle) |
| 616 | .set(gle.is_cancelled, True) |
| 617 | .set(gle.modified, now()) |
| 618 | .set(gle.modified_by, frappe.session.user) |
| 619 | .where( |
| 620 | (gle.company == x.company) |
| 621 | & (gle.account == x.account) |
| 622 | & (gle.party_type == x.party_type) |
| 623 | & (gle.party == x.party) |
| 624 | & (gle.voucher_type == x.voucher_type) |
| 625 | & (gle.voucher_no == x.voucher_no) |
| 626 | & (gle.against_voucher_type == x.against_voucher_type) |
| 627 | & (gle.against_voucher == x.against_voucher) |
| 628 | & (gle.voucher_detail_no == x.voucher_detail_no) |
| 629 | ) |
| 630 | ) |
| 631 | query.run() |
| 632 | else: |
Deepesh Garg | da6bc1a | 2023-06-23 20:57:51 +0530 | [diff] [blame] | 633 | set_as_cancel(gl_entries[0]["voucher_type"], gl_entries[0]["voucher_no"]) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 634 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 635 | for entry in gl_entries: |
Deepesh Garg | ffec865 | 2022-02-02 17:14:42 +0530 | [diff] [blame] | 636 | new_gle = copy.deepcopy(entry) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 637 | new_gle["name"] = None |
| 638 | debit = new_gle.get("debit", 0) |
| 639 | credit = new_gle.get("credit", 0) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 640 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 641 | debit_in_account_currency = new_gle.get("debit_in_account_currency", 0) |
| 642 | credit_in_account_currency = new_gle.get("credit_in_account_currency", 0) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 643 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 644 | new_gle["debit"] = credit |
| 645 | new_gle["credit"] = debit |
| 646 | new_gle["debit_in_account_currency"] = credit_in_account_currency |
| 647 | new_gle["credit_in_account_currency"] = debit_in_account_currency |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 648 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 649 | new_gle["remarks"] = "On cancellation of " + new_gle["voucher_no"] |
| 650 | new_gle["is_cancelled"] = 1 |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 651 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 652 | if new_gle["debit"] or new_gle["credit"]: |
Deepesh Garg | ffec865 | 2022-02-02 17:14:42 +0530 | [diff] [blame] | 653 | make_entry(new_gle, adv_adj, "Yes") |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 654 | |
| 655 | |
| 656 | def check_freezing_date(posting_date, adv_adj=False): |
| 657 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 658 | Nobody can do GL Entries where posting date is before freezing date |
| 659 | except authorized person |
Deepesh Garg | 13d2e7b | 2021-09-16 18:54:57 +0530 | [diff] [blame] | 660 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 661 | Administrator has all the roles so this check will be bypassed if any role is allowed to post |
| 662 | Hence stop admin to bypass if accounts are freezed |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 663 | """ |
| 664 | if not adv_adj: |
Ankush Menat | bb18ae8 | 2024-01-09 21:56:47 +0530 | [diff] [blame] | 665 | acc_frozen_upto = frappe.db.get_single_value("Accounts Settings", "acc_frozen_upto") |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 666 | if acc_frozen_upto: |
Ankush Menat | bb18ae8 | 2024-01-09 21:56:47 +0530 | [diff] [blame] | 667 | frozen_accounts_modifier = frappe.db.get_single_value( |
| 668 | "Accounts Settings", "frozen_accounts_modifier" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 669 | ) |
| 670 | if getdate(posting_date) <= getdate(acc_frozen_upto) and ( |
| 671 | frozen_accounts_modifier not in frappe.get_roles() or frappe.session.user == "Administrator" |
| 672 | ): |
| 673 | frappe.throw( |
| 674 | _("You are not authorized to add or update entries before {0}").format( |
| 675 | formatdate(acc_frozen_upto) |
| 676 | ) |
| 677 | ) |
| 678 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 679 | |
Deepesh Garg | f92c63f | 2023-02-26 15:53:33 +0530 | [diff] [blame] | 680 | def validate_against_pcv(is_opening, posting_date, company): |
| 681 | if is_opening and frappe.db.exists( |
| 682 | "Period Closing Voucher", {"docstatus": 1, "company": company} |
| 683 | ): |
| 684 | frappe.throw( |
| 685 | _("Opening Entry can not be created after Period Closing Voucher is created."), |
| 686 | title=_("Invalid Opening Entry"), |
| 687 | ) |
| 688 | |
| 689 | last_pcv_date = frappe.db.get_value( |
| 690 | "Period Closing Voucher", {"docstatus": 1, "company": company}, "max(posting_date)" |
| 691 | ) |
| 692 | |
| 693 | if last_pcv_date and getdate(posting_date) <= getdate(last_pcv_date): |
| 694 | message = _("Books have been closed till the period ending on {0}").format( |
| 695 | formatdate(last_pcv_date) |
| 696 | ) |
| 697 | message += "</br >" |
Deepesh Garg | 8ce1da1 | 2023-03-23 19:14:12 +0530 | [diff] [blame] | 698 | message += _("You cannot create/amend any accounting entries till this date.") |
Deepesh Garg | f92c63f | 2023-02-26 15:53:33 +0530 | [diff] [blame] | 699 | frappe.throw(message, title=_("Period Closed")) |
| 700 | |
| 701 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 702 | def set_as_cancel(voucher_type, voucher_no): |
| 703 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 704 | Set is_cancelled=1 in all original gl entries for the voucher |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 705 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 706 | frappe.db.sql( |
| 707 | """UPDATE `tabGL Entry` SET is_cancelled = 1, |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 708 | modified=%s, modified_by=%s |
| 709 | where voucher_type=%s and voucher_no=%s and is_cancelled = 0""", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 710 | (now(), frappe.session.user, voucher_type, voucher_no), |
| 711 | ) |
Deepesh Garg | b834ed1 | 2024-02-05 14:05:01 +0530 | [diff] [blame] | 712 | |
| 713 | |
| 714 | def validate_allowed_dimensions(gl_entry, dimension_filter_map): |
| 715 | for key, value in dimension_filter_map.items(): |
| 716 | dimension = key[0] |
| 717 | account = key[1] |
| 718 | |
| 719 | if gl_entry.account == account: |
| 720 | if value["is_mandatory"] and not gl_entry.get(dimension): |
| 721 | frappe.throw( |
| 722 | _("{0} is mandatory for account {1}").format( |
| 723 | frappe.bold(frappe.unscrub(dimension)), frappe.bold(gl_entry.account) |
| 724 | ), |
| 725 | MandatoryAccountDimensionError, |
| 726 | ) |
| 727 | |
| 728 | if value["allow_or_restrict"] == "Allow": |
| 729 | if gl_entry.get(dimension) and gl_entry.get(dimension) not in value["allowed_dimensions"]: |
| 730 | frappe.throw( |
| 731 | _("Invalid value {0} for {1} against account {2}").format( |
| 732 | frappe.bold(gl_entry.get(dimension)), |
| 733 | frappe.bold(frappe.unscrub(dimension)), |
| 734 | frappe.bold(gl_entry.account), |
| 735 | ), |
| 736 | InvalidAccountDimensionError, |
| 737 | ) |
| 738 | else: |
| 739 | if gl_entry.get(dimension) and gl_entry.get(dimension) in value["allowed_dimensions"]: |
| 740 | frappe.throw( |
| 741 | _("Invalid value {0} for {1} against account {2}").format( |
| 742 | frappe.bold(gl_entry.get(dimension)), |
| 743 | frappe.bold(frappe.unscrub(dimension)), |
| 744 | frappe.bold(gl_entry.account), |
| 745 | ), |
| 746 | InvalidAccountDimensionError, |
| 747 | ) |