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