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 |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | from frappe.utils import cint, cstr, flt, formatdate, getdate, now |
| 11 | |
| 12 | import erpnext |
| 13 | from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import ( |
| 14 | get_accounting_dimensions, |
| 15 | ) |
Nabin Hait | b9bc7d6 | 2016-05-16 14:38:47 +0530 | [diff] [blame] | 16 | from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 17 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 18 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 19 | class ClosedAccountingPeriod(frappe.ValidationError): |
| 20 | pass |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 21 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 22 | |
| 23 | def make_gl_entries( |
| 24 | gl_map, |
| 25 | cancel=False, |
| 26 | adv_adj=False, |
| 27 | merge_entries=True, |
| 28 | update_outstanding="Yes", |
| 29 | from_repost=False, |
| 30 | ): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 31 | if gl_map: |
| 32 | if not cancel: |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 33 | validate_accounting_period(gl_map) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 34 | gl_map = process_gl_map(gl_map, merge_entries) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 35 | if gl_map and len(gl_map) > 1: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 36 | save_entries(gl_map, adv_adj, update_outstanding, from_repost) |
Deepesh Garg | 4c8d15b | 2021-04-26 15:24:34 +0530 | [diff] [blame] | 37 | # Post GL Map proccess there may no be any GL Entries |
| 38 | elif gl_map: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 39 | frappe.throw( |
| 40 | _( |
| 41 | "Incorrect number of General Ledger Entries found. You might have selected a wrong Account in the transaction." |
| 42 | ) |
| 43 | ) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 44 | else: |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 45 | 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] | 46 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 47 | |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 48 | def validate_accounting_period(gl_map): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 49 | accounting_periods = frappe.db.sql( |
| 50 | """ SELECT |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 51 | ap.name as name |
| 52 | FROM |
| 53 | `tabAccounting Period` ap, `tabClosed Document` cd |
| 54 | WHERE |
| 55 | ap.name = cd.parent |
| 56 | AND ap.company = %(company)s |
| 57 | AND cd.closed = 1 |
| 58 | AND cd.document_type = %(voucher_type)s |
| 59 | AND %(date)s between ap.start_date and ap.end_date |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 60 | """, |
| 61 | { |
| 62 | "date": gl_map[0].posting_date, |
| 63 | "company": gl_map[0].company, |
| 64 | "voucher_type": gl_map[0].voucher_type, |
| 65 | }, |
| 66 | as_dict=1, |
| 67 | ) |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 68 | |
| 69 | if accounting_periods: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 70 | frappe.throw( |
| 71 | _( |
| 72 | "You cannot create or cancel any accounting entries with in the closed Accounting Period {0}" |
| 73 | ).format(frappe.bold(accounting_periods[0].name)), |
| 74 | ClosedAccountingPeriod, |
| 75 | ) |
| 76 | |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 77 | |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 78 | def process_gl_map(gl_map, merge_entries=True, precision=None): |
Nabin Hait | 6099af5 | 2022-01-31 17:24:50 +0530 | [diff] [blame] | 79 | if not gl_map: |
| 80 | return [] |
| 81 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 82 | gl_map = distribute_gl_based_on_cost_center_allocation(gl_map, precision) |
| 83 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 84 | if merge_entries: |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 85 | gl_map = merge_similar_entries(gl_map, precision) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 86 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 87 | gl_map = toggle_debit_credit_if_negative(gl_map) |
Deepesh Garg | 5ba3b28 | 2021-11-25 15:42:30 +0530 | [diff] [blame] | 88 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 89 | return gl_map |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 90 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 91 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 92 | def distribute_gl_based_on_cost_center_allocation(gl_map, precision=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 93 | cost_center_allocation = get_cost_center_allocation_data( |
| 94 | gl_map[0]["company"], gl_map[0]["posting_date"] |
| 95 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 96 | if not cost_center_allocation: |
| 97 | return gl_map |
Deepesh Garg | 5ba3b28 | 2021-11-25 15:42:30 +0530 | [diff] [blame] | 98 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 99 | new_gl_map = [] |
| 100 | for d in gl_map: |
| 101 | cost_center = d.get("cost_center") |
| 102 | if cost_center and cost_center_allocation.get(cost_center): |
| 103 | for sub_cost_center, percentage in cost_center_allocation.get(cost_center, {}).items(): |
| 104 | gle = copy.deepcopy(d) |
| 105 | gle.cost_center = sub_cost_center |
| 106 | for field in ("debit", "credit", "debit_in_account_currency", "credit_in_company_currency"): |
| 107 | gle[field] = flt(flt(d.get(field)) * percentage / 100, precision) |
| 108 | new_gl_map.append(gle) |
| 109 | else: |
| 110 | new_gl_map.append(d) |
| 111 | |
| 112 | return new_gl_map |
| 113 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 114 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 115 | def get_cost_center_allocation_data(company, posting_date): |
| 116 | par = frappe.qb.DocType("Cost Center Allocation") |
| 117 | child = frappe.qb.DocType("Cost Center Allocation Percentage") |
| 118 | |
| 119 | records = ( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 120 | frappe.qb.from_(par) |
| 121 | .inner_join(child) |
| 122 | .on(par.name == child.parent) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 123 | .select(par.main_cost_center, child.cost_center, child.percentage) |
| 124 | .where(par.docstatus == 1) |
| 125 | .where(par.company == company) |
| 126 | .where(par.valid_from <= posting_date) |
| 127 | .orderby(par.valid_from, order=frappe.qb.desc) |
| 128 | ).run(as_dict=True) |
| 129 | |
| 130 | cc_allocation = frappe._dict() |
| 131 | for d in records: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 132 | cc_allocation.setdefault(d.main_cost_center, frappe._dict()).setdefault( |
| 133 | d.cost_center, d.percentage |
| 134 | ) |
Nabin Hait | 5b0ec64 | 2022-01-31 18:07:04 +0530 | [diff] [blame] | 135 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 136 | return cc_allocation |
Deepesh Garg | 5ba3b28 | 2021-11-25 15:42:30 +0530 | [diff] [blame] | 137 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 138 | |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 139 | def merge_similar_entries(gl_map, precision=None): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 140 | merged_gl_map = [] |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 141 | accounting_dimensions = get_accounting_dimensions() |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 142 | for entry in gl_map: |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 143 | # 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] | 144 | # to that entry |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 145 | same_head = check_if_in_list(entry, merged_gl_map, accounting_dimensions) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 146 | if same_head: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 147 | same_head.debit = flt(same_head.debit) + flt(entry.debit) |
| 148 | same_head.debit_in_account_currency = flt(same_head.debit_in_account_currency) + flt( |
| 149 | entry.debit_in_account_currency |
| 150 | ) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 151 | same_head.credit = flt(same_head.credit) + flt(entry.credit) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 152 | same_head.credit_in_account_currency = flt(same_head.credit_in_account_currency) + flt( |
| 153 | entry.credit_in_account_currency |
| 154 | ) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 155 | else: |
| 156 | merged_gl_map.append(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 157 | |
thefalconx33 | bfc43d3 | 2019-12-13 15:09:51 +0530 | [diff] [blame] | 158 | company = gl_map[0].company if gl_map else erpnext.get_default_company() |
| 159 | company_currency = erpnext.get_company_currency(company) |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 160 | |
| 161 | if not precision: |
| 162 | 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] | 163 | |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 164 | # filter zero debit and credit entries |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 165 | merged_gl_map = filter( |
| 166 | lambda x: flt(x.debit, precision) != 0 or flt(x.credit, precision) != 0, merged_gl_map |
| 167 | ) |
Achilles Rasquinha | 908289d | 2018-03-08 13:10:51 +0530 | [diff] [blame] | 168 | merged_gl_map = list(merged_gl_map) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 169 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 170 | return merged_gl_map |
| 171 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 172 | |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 173 | def check_if_in_list(gle, gl_map, dimensions=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 174 | account_head_fieldnames = [ |
| 175 | "voucher_detail_no", |
| 176 | "party", |
| 177 | "against_voucher", |
| 178 | "cost_center", |
| 179 | "against_voucher_type", |
| 180 | "party_type", |
| 181 | "project", |
| 182 | "finance_book", |
| 183 | ] |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 184 | |
| 185 | if dimensions: |
| 186 | account_head_fieldnames = account_head_fieldnames + dimensions |
| 187 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 188 | for e in gl_map: |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 189 | same_head = True |
| 190 | if e.account != gle.account: |
| 191 | same_head = False |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 192 | continue |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 193 | |
| 194 | for fieldname in account_head_fieldnames: |
| 195 | if cstr(e.get(fieldname)) != cstr(gle.get(fieldname)): |
| 196 | same_head = False |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 197 | break |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 198 | |
| 199 | if same_head: |
| 200 | return e |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 201 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 202 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 203 | def toggle_debit_credit_if_negative(gl_map): |
| 204 | for entry in gl_map: |
| 205 | # toggle debit, credit if negative entry |
| 206 | if flt(entry.debit) < 0: |
| 207 | entry.credit = flt(entry.credit) - flt(entry.debit) |
| 208 | entry.debit = 0.0 |
| 209 | |
| 210 | if flt(entry.debit_in_account_currency) < 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 211 | entry.credit_in_account_currency = flt(entry.credit_in_account_currency) - flt( |
| 212 | entry.debit_in_account_currency |
| 213 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 214 | entry.debit_in_account_currency = 0.0 |
| 215 | |
| 216 | if flt(entry.credit) < 0: |
| 217 | entry.debit = flt(entry.debit) - flt(entry.credit) |
| 218 | entry.credit = 0.0 |
| 219 | |
| 220 | if flt(entry.credit_in_account_currency) < 0: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 221 | entry.debit_in_account_currency = flt(entry.debit_in_account_currency) - flt( |
| 222 | entry.credit_in_account_currency |
| 223 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 224 | entry.credit_in_account_currency = 0.0 |
| 225 | |
| 226 | update_net_values(entry) |
| 227 | |
| 228 | return gl_map |
| 229 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 230 | |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 231 | def update_net_values(entry): |
| 232 | # In some scenarios net value needs to be shown in the ledger |
| 233 | # This method updates net values as debit or credit |
| 234 | if entry.post_net_value and entry.debit and entry.credit: |
| 235 | if entry.debit > entry.credit: |
| 236 | entry.debit = entry.debit - entry.credit |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 237 | entry.debit_in_account_currency = ( |
| 238 | entry.debit_in_account_currency - entry.credit_in_account_currency |
| 239 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 240 | entry.credit = 0 |
| 241 | entry.credit_in_account_currency = 0 |
| 242 | else: |
| 243 | entry.credit = entry.credit - entry.debit |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 244 | entry.credit_in_account_currency = ( |
| 245 | entry.credit_in_account_currency - entry.debit_in_account_currency |
| 246 | ) |
Nabin Hait | 004c1ed | 2022-01-31 13:20:18 +0530 | [diff] [blame] | 247 | |
| 248 | entry.debit = 0 |
| 249 | entry.debit_in_account_currency = 0 |
| 250 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 251 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 252 | def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False): |
| 253 | if not from_repost: |
| 254 | validate_cwip_accounts(gl_map) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 255 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 256 | process_debit_credit_difference(gl_map) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 257 | |
| 258 | if gl_map: |
| 259 | check_freezing_date(gl_map[0]["posting_date"], adv_adj) |
| 260 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 261 | for entry in gl_map: |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 262 | make_entry(entry, adv_adj, update_outstanding, from_repost) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 263 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 264 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 265 | def make_entry(args, adv_adj, update_outstanding, from_repost=False): |
Nabin Hait | ddc3bb2 | 2020-03-17 17:04:18 +0530 | [diff] [blame] | 266 | gle = frappe.new_doc("GL Entry") |
| 267 | gle.update(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 268 | gle.flags.ignore_permissions = 1 |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 269 | gle.flags.from_repost = from_repost |
Nabin Hait | 19f8fa5 | 2021-02-22 22:27:22 +0530 | [diff] [blame] | 270 | gle.flags.adv_adj = adv_adj |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 271 | gle.flags.update_outstanding = update_outstanding or "Yes" |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 272 | gle.submit() |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 273 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 274 | if not from_repost: |
| 275 | validate_expense_against_budget(args) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 276 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 277 | |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 278 | def validate_cwip_accounts(gl_map): |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 279 | """Validate that CWIP account are not used in Journal Entry""" |
| 280 | if gl_map and gl_map[0].voucher_type != "Journal Entry": |
| 281 | return |
Marica | d00c598 | 2019-11-12 19:17:43 +0530 | [diff] [blame] | 282 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 283 | cwip_enabled = any( |
| 284 | cint(ac.enable_cwip_accounting) |
| 285 | for ac in frappe.db.get_all("Asset Category", "enable_cwip_accounting") |
| 286 | ) |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 287 | if cwip_enabled: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 288 | cwip_accounts = [ |
| 289 | d[0] |
| 290 | for d in frappe.db.sql( |
| 291 | """select name from tabAccount |
| 292 | where account_type = 'Capital Work in Progress' and is_group=0""" |
| 293 | ) |
| 294 | ] |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 295 | |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 296 | for entry in gl_map: |
| 297 | if entry.account in cwip_accounts: |
| 298 | frappe.throw( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 299 | _( |
| 300 | "Account: <b>{0}</b> is capital Work in progress and can not be updated by Journal Entry" |
| 301 | ).format(entry.account) |
| 302 | ) |
| 303 | |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 304 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 305 | def process_debit_credit_difference(gl_map): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 306 | precision = get_field_precision( |
| 307 | frappe.get_meta("GL Entry").get_field("debit"), |
| 308 | currency=frappe.get_cached_value("Company", gl_map[0].company, "default_currency"), |
| 309 | ) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 310 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 311 | voucher_type = gl_map[0].voucher_type |
| 312 | voucher_no = gl_map[0].voucher_no |
| 313 | allowance = get_debit_credit_allowance(voucher_type, precision) |
| 314 | |
| 315 | debit_credit_diff = get_debit_credit_difference(gl_map, precision) |
| 316 | if abs(debit_credit_diff) > allowance: |
| 317 | raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no) |
| 318 | |
| 319 | elif abs(debit_credit_diff) >= (1.0 / (10**precision)): |
| 320 | make_round_off_gle(gl_map, debit_credit_diff, precision) |
| 321 | |
| 322 | debit_credit_diff = get_debit_credit_difference(gl_map, precision) |
| 323 | if abs(debit_credit_diff) > allowance: |
| 324 | raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no) |
| 325 | |
| 326 | |
| 327 | def get_debit_credit_difference(gl_map, precision): |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 328 | debit_credit_diff = 0.0 |
| 329 | for entry in gl_map: |
| 330 | entry.debit = flt(entry.debit, precision) |
| 331 | entry.credit = flt(entry.credit, precision) |
| 332 | debit_credit_diff += entry.debit - entry.credit |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 333 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 334 | debit_credit_diff = flt(debit_credit_diff, precision) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 335 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 336 | return debit_credit_diff |
| 337 | |
| 338 | |
| 339 | def get_debit_credit_allowance(voucher_type, precision): |
| 340 | if voucher_type in ("Journal Entry", "Payment Entry"): |
Nabin Hait | fb24a27 | 2016-02-18 19:18:07 +0530 | [diff] [blame] | 341 | allowance = 5.0 / (10**precision) |
| 342 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 343 | allowance = 0.5 |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 344 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 345 | return allowance |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 346 | |
Saqib Ansari | bfc34e1 | 2022-04-01 11:03:16 +0530 | [diff] [blame] | 347 | |
| 348 | def raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no): |
| 349 | frappe.throw( |
| 350 | _("Debit and Credit not equal for {0} #{1}. Difference is {2}.").format( |
| 351 | voucher_type, voucher_no, debit_credit_diff |
| 352 | ) |
| 353 | ) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 354 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 355 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 356 | def make_round_off_gle(gl_map, debit_credit_diff, precision): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 357 | round_off_account, round_off_cost_center = get_round_off_account_and_cost_center( |
| 358 | gl_map[0].company |
| 359 | ) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 360 | round_off_account_exists = False |
Nabin Hait | 80069a6 | 2015-05-28 19:19:59 +0530 | [diff] [blame] | 361 | round_off_gle = frappe._dict() |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 362 | for d in gl_map: |
| 363 | if d.account == round_off_account: |
| 364 | round_off_gle = d |
Saqib | c6e016e | 2021-06-04 10:08:22 +0530 | [diff] [blame] | 365 | if d.debit: |
| 366 | debit_credit_diff -= flt(d.debit) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 367 | else: |
Saqib | c6e016e | 2021-06-04 10:08:22 +0530 | [diff] [blame] | 368 | debit_credit_diff += flt(d.credit) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 369 | round_off_account_exists = True |
| 370 | |
Saqib Ansari | ad2c64f | 2022-03-01 13:32:34 +0530 | [diff] [blame] | 371 | if round_off_account_exists and abs(debit_credit_diff) < (1.0 / (10**precision)): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 372 | gl_map.remove(round_off_gle) |
| 373 | return |
| 374 | |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 375 | if not round_off_gle: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 376 | for k in ["voucher_type", "voucher_no", "company", "posting_date", "remarks"]: |
| 377 | round_off_gle[k] = gl_map[0][k] |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 378 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 379 | round_off_gle.update( |
| 380 | { |
| 381 | "account": round_off_account, |
| 382 | "debit_in_account_currency": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, |
| 383 | "credit_in_account_currency": debit_credit_diff if debit_credit_diff > 0 else 0, |
| 384 | "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, |
| 385 | "credit": debit_credit_diff if debit_credit_diff > 0 else 0, |
| 386 | "cost_center": round_off_cost_center, |
| 387 | "party_type": None, |
| 388 | "party": None, |
| 389 | "is_opening": "No", |
| 390 | "against_voucher_type": None, |
| 391 | "against_voucher": None, |
| 392 | } |
| 393 | ) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 394 | |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 395 | if not round_off_account_exists: |
| 396 | gl_map.append(round_off_gle) |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 397 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 398 | |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 399 | def get_round_off_account_and_cost_center(company): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 400 | round_off_account, round_off_cost_center = frappe.get_cached_value( |
| 401 | "Company", company, ["round_off_account", "round_off_cost_center"] |
| 402 | ) or [None, None] |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 403 | if not round_off_account: |
| 404 | frappe.throw(_("Please mention Round Off Account in Company")) |
| 405 | |
| 406 | if not round_off_cost_center: |
| 407 | frappe.throw(_("Please mention Round Off Cost Center in Company")) |
| 408 | |
| 409 | return round_off_account, round_off_cost_center |
| 410 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 411 | |
| 412 | def make_reverse_gl_entries( |
| 413 | gl_entries=None, voucher_type=None, voucher_no=None, adv_adj=False, update_outstanding="Yes" |
| 414 | ): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 415 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 416 | Get original gl entries of the voucher |
| 417 | and make reverse gl entries by swapping debit and credit |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 418 | """ |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 419 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 420 | if not gl_entries: |
Deepesh Garg | ff57450 | 2022-02-06 22:56:12 +0530 | [diff] [blame] | 421 | gl_entry = frappe.qb.DocType("GL Entry") |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 422 | gl_entries = ( |
| 423 | frappe.qb.from_(gl_entry) |
| 424 | .select("*") |
| 425 | .where(gl_entry.voucher_type == voucher_type) |
| 426 | .where(gl_entry.voucher_no == voucher_no) |
| 427 | .where(gl_entry.is_cancelled == 0) |
| 428 | .for_update() |
| 429 | ).run(as_dict=1) |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 430 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 431 | if gl_entries: |
Deepesh Garg | 069a54e | 2020-08-10 16:01:01 +0530 | [diff] [blame] | 432 | validate_accounting_period(gl_entries) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 433 | check_freezing_date(gl_entries[0]["posting_date"], adv_adj) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 434 | 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] | 435 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 436 | for entry in gl_entries: |
Deepesh Garg | ffec865 | 2022-02-02 17:14:42 +0530 | [diff] [blame] | 437 | new_gle = copy.deepcopy(entry) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 438 | new_gle["name"] = None |
| 439 | debit = new_gle.get("debit", 0) |
| 440 | credit = new_gle.get("credit", 0) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 441 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 442 | debit_in_account_currency = new_gle.get("debit_in_account_currency", 0) |
| 443 | credit_in_account_currency = new_gle.get("credit_in_account_currency", 0) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 444 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 445 | new_gle["debit"] = credit |
| 446 | new_gle["credit"] = debit |
| 447 | new_gle["debit_in_account_currency"] = credit_in_account_currency |
| 448 | new_gle["credit_in_account_currency"] = debit_in_account_currency |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 449 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 450 | new_gle["remarks"] = "On cancellation of " + new_gle["voucher_no"] |
| 451 | new_gle["is_cancelled"] = 1 |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 452 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 453 | if new_gle["debit"] or new_gle["credit"]: |
Deepesh Garg | ffec865 | 2022-02-02 17:14:42 +0530 | [diff] [blame] | 454 | make_entry(new_gle, adv_adj, "Yes") |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 455 | |
| 456 | |
| 457 | def check_freezing_date(posting_date, adv_adj=False): |
| 458 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 459 | Nobody can do GL Entries where posting date is before freezing date |
| 460 | except authorized person |
Deepesh Garg | 13d2e7b | 2021-09-16 18:54:57 +0530 | [diff] [blame] | 461 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 462 | Administrator has all the roles so this check will be bypassed if any role is allowed to post |
| 463 | Hence stop admin to bypass if accounts are freezed |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 464 | """ |
| 465 | if not adv_adj: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 466 | acc_frozen_upto = frappe.db.get_value("Accounts Settings", None, "acc_frozen_upto") |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 467 | if acc_frozen_upto: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 468 | frozen_accounts_modifier = frappe.db.get_value( |
| 469 | "Accounts Settings", None, "frozen_accounts_modifier" |
| 470 | ) |
| 471 | if getdate(posting_date) <= getdate(acc_frozen_upto) and ( |
| 472 | frozen_accounts_modifier not in frappe.get_roles() or frappe.session.user == "Administrator" |
| 473 | ): |
| 474 | frappe.throw( |
| 475 | _("You are not authorized to add or update entries before {0}").format( |
| 476 | formatdate(acc_frozen_upto) |
| 477 | ) |
| 478 | ) |
| 479 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 480 | |
| 481 | def set_as_cancel(voucher_type, voucher_no): |
| 482 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 483 | Set is_cancelled=1 in all original gl entries for the voucher |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 484 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 485 | frappe.db.sql( |
| 486 | """UPDATE `tabGL Entry` SET is_cancelled = 1, |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 487 | modified=%s, modified_by=%s |
| 488 | where voucher_type=%s and voucher_no=%s and is_cancelled = 0""", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 489 | (now(), frappe.session.user, voucher_type, voucher_no), |
| 490 | ) |