blob: a929ff17b09836f2b8133d4124356eaac2240169 [file] [log] [blame]
Anand Doshi885e0742015-03-03 14:55:30 +05301# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05302# License: GNU General Public License v3. See license.txt
Nabin Haitbf495c92013-01-30 12:49:08 +05303
Chillar Anand915b3432021-09-02 16:44:59 +05304
Nabin Hait004c1ed2022-01-31 13:20:18 +05305import copy
Nabin Hait5b0ec642022-01-31 18:07:04 +05306
7import frappe
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05308from frappe import _
Nabin Haite2c200a2015-05-28 13:00:37 +05309from frappe.model.meta import get_field_precision
Chillar Anand915b3432021-09-02 16:44:59 +053010from frappe.utils import cint, cstr, flt, formatdate, getdate, now
11
12import erpnext
13from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
14 get_accounting_dimensions,
15)
Nabin Haitb9bc7d62016-05-16 14:38:47 +053016from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget
ruthra kumare8889752022-05-16 14:28:25 +053017from erpnext.accounts.utils import create_payment_ledger_entry
Chillar Anand915b3432021-09-02 16:44:59 +053018
Nabin Haitbf495c92013-01-30 12:49:08 +053019
Ankush Menat494bd9e2022-03-28 18:52:46 +053020class ClosedAccountingPeriod(frappe.ValidationError):
21 pass
Nabin Haitbf495c92013-01-30 12:49:08 +053022
Ankush Menat494bd9e2022-03-28 18:52:46 +053023
24def make_gl_entries(
25 gl_map,
26 cancel=False,
27 adv_adj=False,
28 merge_entries=True,
29 update_outstanding="Yes",
30 from_repost=False,
31):
Nabin Hait2e296fa2013-08-28 18:53:11 +053032 if gl_map:
33 if not cancel:
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053034 validate_accounting_period(gl_map)
Saqib Ansari95b059a2022-05-11 13:26:15 +053035 validate_disabled_accounts(gl_map)
Nabin Hait2e296fa2013-08-28 18:53:11 +053036 gl_map = process_gl_map(gl_map, merge_entries)
nabinhaitc3432922014-07-29 18:06:18 +053037 if gl_map and len(gl_map) > 1:
ruthra kumar7312f222022-05-29 21:33:08 +053038 create_payment_ledger_entry(
39 gl_map,
40 cancel=0,
41 adv_adj=adv_adj,
42 update_outstanding=update_outstanding,
43 from_repost=from_repost,
44 )
Nabin Haita77b8c92020-12-21 14:45:50 +053045 save_entries(gl_map, adv_adj, update_outstanding, from_repost)
Deepesh Garg4c8d15b2021-04-26 15:24:34 +053046 # Post GL Map proccess there may no be any GL Entries
47 elif gl_map:
Ankush Menat494bd9e2022-03-28 18:52:46 +053048 frappe.throw(
49 _(
50 "Incorrect number of General Ledger Entries found. You might have selected a wrong Account in the transaction."
51 )
52 )
Nabin Hait2e296fa2013-08-28 18:53:11 +053053 else:
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +053054 make_reverse_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding)
Anand Doshi652bc072014-04-16 15:21:46 +053055
Ankush Menat494bd9e2022-03-28 18:52:46 +053056
Saqib Ansari95b059a2022-05-11 13:26:15 +053057def validate_disabled_accounts(gl_map):
58 accounts = [d.account for d in gl_map if d.account]
59
60 Account = frappe.qb.DocType("Account")
61
62 disabled_accounts = (
63 frappe.qb.from_(Account)
64 .where(Account.name.isin(accounts) & Account.disabled == 1)
65 .select(Account.name, Account.disabled)
66 ).run(as_dict=True)
67
68 if disabled_accounts:
69 account_list = "<br>"
70 account_list += ", ".join([frappe.bold(d.name) for d in disabled_accounts])
71 frappe.throw(
72 _("Cannot create accounting entries against disabled accounts: {0}").format(account_list),
73 title=_("Disabled Account Selected"),
74 )
75
76
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053077def validate_accounting_period(gl_map):
Ankush Menat494bd9e2022-03-28 18:52:46 +053078 accounting_periods = frappe.db.sql(
79 """ SELECT
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053080 ap.name as name
81 FROM
82 `tabAccounting Period` ap, `tabClosed Document` cd
83 WHERE
84 ap.name = cd.parent
85 AND ap.company = %(company)s
86 AND cd.closed = 1
87 AND cd.document_type = %(voucher_type)s
88 AND %(date)s between ap.start_date and ap.end_date
Ankush Menat494bd9e2022-03-28 18:52:46 +053089 """,
90 {
91 "date": gl_map[0].posting_date,
92 "company": gl_map[0].company,
93 "voucher_type": gl_map[0].voucher_type,
94 },
95 as_dict=1,
96 )
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053097
98 if accounting_periods:
Ankush Menat494bd9e2022-03-28 18:52:46 +053099 frappe.throw(
100 _(
101 "You cannot create or cancel any accounting entries with in the closed Accounting Period {0}"
102 ).format(frappe.bold(accounting_periods[0].name)),
103 ClosedAccountingPeriod,
104 )
105
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +0530106
Nabin Hait19f8fa52021-02-22 22:27:22 +0530107def process_gl_map(gl_map, merge_entries=True, precision=None):
Nabin Hait6099af52022-01-31 17:24:50 +0530108 if not gl_map:
109 return []
110
Nabin Hait004c1ed2022-01-31 13:20:18 +0530111 gl_map = distribute_gl_based_on_cost_center_allocation(gl_map, precision)
112
Nabin Haitbf495c92013-01-30 12:49:08 +0530113 if merge_entries:
Nabin Hait19f8fa52021-02-22 22:27:22 +0530114 gl_map = merge_similar_entries(gl_map, precision)
Anand Doshi602e8252015-11-16 19:05:46 +0530115
Nabin Hait004c1ed2022-01-31 13:20:18 +0530116 gl_map = toggle_debit_credit_if_negative(gl_map)
Deepesh Garg5ba3b282021-11-25 15:42:30 +0530117
Nabin Hait27994c22013-08-26 16:53:30 +0530118 return gl_map
Anand Doshi652bc072014-04-16 15:21:46 +0530119
Ankush Menat494bd9e2022-03-28 18:52:46 +0530120
Nabin Hait004c1ed2022-01-31 13:20:18 +0530121def distribute_gl_based_on_cost_center_allocation(gl_map, precision=None):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530122 cost_center_allocation = get_cost_center_allocation_data(
123 gl_map[0]["company"], gl_map[0]["posting_date"]
124 )
Nabin Hait004c1ed2022-01-31 13:20:18 +0530125 if not cost_center_allocation:
126 return gl_map
Deepesh Garg5ba3b282021-11-25 15:42:30 +0530127
Nabin Hait004c1ed2022-01-31 13:20:18 +0530128 new_gl_map = []
129 for d in gl_map:
130 cost_center = d.get("cost_center")
Deepesh Garg4e26d422022-10-30 19:33:27 +0530131
132 # Validate budget against main cost center
133 validate_expense_against_budget(
134 d, expense_amount=flt(d.debit, precision) - flt(d.credit, precision)
135 )
136
Nabin Hait004c1ed2022-01-31 13:20:18 +0530137 if cost_center and cost_center_allocation.get(cost_center):
138 for sub_cost_center, percentage in cost_center_allocation.get(cost_center, {}).items():
139 gle = copy.deepcopy(d)
140 gle.cost_center = sub_cost_center
ruthra kumar71f6f782022-06-24 13:36:15 +0530141 for field in ("debit", "credit", "debit_in_account_currency", "credit_in_account_currency"):
Nabin Hait004c1ed2022-01-31 13:20:18 +0530142 gle[field] = flt(flt(d.get(field)) * percentage / 100, precision)
143 new_gl_map.append(gle)
144 else:
145 new_gl_map.append(d)
146
147 return new_gl_map
148
Ankush Menat494bd9e2022-03-28 18:52:46 +0530149
Nabin Hait004c1ed2022-01-31 13:20:18 +0530150def get_cost_center_allocation_data(company, posting_date):
151 par = frappe.qb.DocType("Cost Center Allocation")
152 child = frappe.qb.DocType("Cost Center Allocation Percentage")
153
154 records = (
Ankush Menat494bd9e2022-03-28 18:52:46 +0530155 frappe.qb.from_(par)
156 .inner_join(child)
157 .on(par.name == child.parent)
Nabin Hait004c1ed2022-01-31 13:20:18 +0530158 .select(par.main_cost_center, child.cost_center, child.percentage)
159 .where(par.docstatus == 1)
160 .where(par.company == company)
161 .where(par.valid_from <= posting_date)
162 .orderby(par.valid_from, order=frappe.qb.desc)
163 ).run(as_dict=True)
164
165 cc_allocation = frappe._dict()
166 for d in records:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530167 cc_allocation.setdefault(d.main_cost_center, frappe._dict()).setdefault(
168 d.cost_center, d.percentage
169 )
Nabin Hait5b0ec642022-01-31 18:07:04 +0530170
Nabin Hait004c1ed2022-01-31 13:20:18 +0530171 return cc_allocation
Deepesh Garg5ba3b282021-11-25 15:42:30 +0530172
Ankush Menat494bd9e2022-03-28 18:52:46 +0530173
Nabin Hait19f8fa52021-02-22 22:27:22 +0530174def merge_similar_entries(gl_map, precision=None):
Nabin Haitbf495c92013-01-30 12:49:08 +0530175 merged_gl_map = []
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530176 accounting_dimensions = get_accounting_dimensions()
Nabin Hait914a3882022-07-19 12:32:54 +0530177
Nabin Haitbf495c92013-01-30 12:49:08 +0530178 for entry in gl_map:
Anand Doshi652bc072014-04-16 15:21:46 +0530179 # if there is already an entry in this account then just add it
Nabin Haitbf495c92013-01-30 12:49:08 +0530180 # to that entry
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530181 same_head = check_if_in_list(entry, merged_gl_map, accounting_dimensions)
Nabin Haitbf495c92013-01-30 12:49:08 +0530182 if same_head:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530183 same_head.debit = flt(same_head.debit) + flt(entry.debit)
184 same_head.debit_in_account_currency = flt(same_head.debit_in_account_currency) + flt(
185 entry.debit_in_account_currency
186 )
Nabin Hait2e296fa2013-08-28 18:53:11 +0530187 same_head.credit = flt(same_head.credit) + flt(entry.credit)
Ankush Menat494bd9e2022-03-28 18:52:46 +0530188 same_head.credit_in_account_currency = flt(same_head.credit_in_account_currency) + flt(
189 entry.credit_in_account_currency
190 )
Nabin Haitbf495c92013-01-30 12:49:08 +0530191 else:
192 merged_gl_map.append(entry)
Anand Doshi652bc072014-04-16 15:21:46 +0530193
thefalconx33bfc43d32019-12-13 15:09:51 +0530194 company = gl_map[0].company if gl_map else erpnext.get_default_company()
195 company_currency = erpnext.get_company_currency(company)
Nabin Hait19f8fa52021-02-22 22:27:22 +0530196
197 if not precision:
198 precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"), company_currency)
thefalconx33bfc43d32019-12-13 15:09:51 +0530199
Nabin Hait815a49e2013-08-07 17:00:01 +0530200 # filter zero debit and credit entries
Ankush Menat494bd9e2022-03-28 18:52:46 +0530201 merged_gl_map = filter(
ruthra kumar914b2302023-01-02 14:33:14 +0530202 lambda x: flt(x.debit, precision) != 0
203 or flt(x.credit, precision) != 0
204 or (
205 x.voucher_type == "Journal Entry"
206 and frappe.get_cached_value("Journal Entry", x.voucher_no, "voucher_type")
207 == "Exchange Gain Or Loss"
208 ),
209 merged_gl_map,
Ankush Menat494bd9e2022-03-28 18:52:46 +0530210 )
Achilles Rasquinha908289d2018-03-08 13:10:51 +0530211 merged_gl_map = list(merged_gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530212
Nabin Haitbf495c92013-01-30 12:49:08 +0530213 return merged_gl_map
214
Ankush Menat494bd9e2022-03-28 18:52:46 +0530215
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530216def check_if_in_list(gle, gl_map, dimensions=None):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530217 account_head_fieldnames = [
218 "voucher_detail_no",
219 "party",
220 "against_voucher",
221 "cost_center",
222 "against_voucher_type",
223 "party_type",
224 "project",
225 "finance_book",
226 ]
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530227
228 if dimensions:
229 account_head_fieldnames = account_head_fieldnames + dimensions
230
Nabin Haitbb777562013-08-29 18:19:37 +0530231 for e in gl_map:
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530232 same_head = True
233 if e.account != gle.account:
234 same_head = False
Ankush91527152021-08-11 11:17:50 +0530235 continue
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530236
237 for fieldname in account_head_fieldnames:
238 if cstr(e.get(fieldname)) != cstr(gle.get(fieldname)):
239 same_head = False
Ankush91527152021-08-11 11:17:50 +0530240 break
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530241
242 if same_head:
243 return e
Nabin Haitbf495c92013-01-30 12:49:08 +0530244
Ankush Menat494bd9e2022-03-28 18:52:46 +0530245
Nabin Hait004c1ed2022-01-31 13:20:18 +0530246def toggle_debit_credit_if_negative(gl_map):
247 for entry in gl_map:
248 # toggle debit, credit if negative entry
249 if flt(entry.debit) < 0:
250 entry.credit = flt(entry.credit) - flt(entry.debit)
251 entry.debit = 0.0
252
253 if flt(entry.debit_in_account_currency) < 0:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530254 entry.credit_in_account_currency = flt(entry.credit_in_account_currency) - flt(
255 entry.debit_in_account_currency
256 )
Nabin Hait004c1ed2022-01-31 13:20:18 +0530257 entry.debit_in_account_currency = 0.0
258
259 if flt(entry.credit) < 0:
260 entry.debit = flt(entry.debit) - flt(entry.credit)
261 entry.credit = 0.0
262
263 if flt(entry.credit_in_account_currency) < 0:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530264 entry.debit_in_account_currency = flt(entry.debit_in_account_currency) - flt(
265 entry.credit_in_account_currency
266 )
Nabin Hait004c1ed2022-01-31 13:20:18 +0530267 entry.credit_in_account_currency = 0.0
268
269 update_net_values(entry)
270
271 return gl_map
272
Ankush Menat494bd9e2022-03-28 18:52:46 +0530273
Nabin Hait004c1ed2022-01-31 13:20:18 +0530274def update_net_values(entry):
275 # In some scenarios net value needs to be shown in the ledger
276 # This method updates net values as debit or credit
277 if entry.post_net_value and entry.debit and entry.credit:
278 if entry.debit > entry.credit:
279 entry.debit = entry.debit - entry.credit
Ankush Menat494bd9e2022-03-28 18:52:46 +0530280 entry.debit_in_account_currency = (
281 entry.debit_in_account_currency - entry.credit_in_account_currency
282 )
Nabin Hait004c1ed2022-01-31 13:20:18 +0530283 entry.credit = 0
284 entry.credit_in_account_currency = 0
285 else:
286 entry.credit = entry.credit - entry.debit
Ankush Menat494bd9e2022-03-28 18:52:46 +0530287 entry.credit_in_account_currency = (
288 entry.credit_in_account_currency - entry.debit_in_account_currency
289 )
Nabin Hait004c1ed2022-01-31 13:20:18 +0530290
291 entry.debit = 0
292 entry.debit_in_account_currency = 0
293
Ankush Menat494bd9e2022-03-28 18:52:46 +0530294
Nabin Haita77b8c92020-12-21 14:45:50 +0530295def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False):
296 if not from_repost:
297 validate_cwip_accounts(gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530298
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530299 process_debit_credit_difference(gl_map)
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530300
301 if gl_map:
302 check_freezing_date(gl_map[0]["posting_date"], adv_adj)
Deepesh Gargf92c63f2023-02-26 15:53:33 +0530303 is_opening = any(d.get("is_opening") == "Yes" for d in gl_map)
304 if gl_map[0]["voucher_type"] != "Period Closing Voucher":
305 validate_against_pcv(is_opening, gl_map[0]["posting_date"], gl_map[0]["company"])
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530306
Nabin Haitbf495c92013-01-30 12:49:08 +0530307 for entry in gl_map:
Nabin Haita77b8c92020-12-21 14:45:50 +0530308 make_entry(entry, adv_adj, update_outstanding, from_repost)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530309
Nabin Hait914a3882022-07-19 12:32:54 +0530310
Nabin Haita77b8c92020-12-21 14:45:50 +0530311def make_entry(args, adv_adj, update_outstanding, from_repost=False):
Nabin Haitddc3bb22020-03-17 17:04:18 +0530312 gle = frappe.new_doc("GL Entry")
313 gle.update(args)
Anand Doshi6dfd4302015-02-10 14:41:27 +0530314 gle.flags.ignore_permissions = 1
Nabin Haita77b8c92020-12-21 14:45:50 +0530315 gle.flags.from_repost = from_repost
Nabin Hait19f8fa52021-02-22 22:27:22 +0530316 gle.flags.adv_adj = adv_adj
Ankush Menat494bd9e2022-03-28 18:52:46 +0530317 gle.flags.update_outstanding = update_outstanding or "Yes"
Nabin Hait4caaab32022-07-18 17:59:42 +0530318 gle.flags.notify_update = False
Nabin Haitaeba24e2013-08-23 15:17:36 +0530319 gle.submit()
Anand Doshi652bc072014-04-16 15:21:46 +0530320
Nabin Hait4caaab32022-07-18 17:59:42 +0530321 if not from_repost and gle.voucher_type != "Period Closing Voucher":
Nabin Haita77b8c92020-12-21 14:45:50 +0530322 validate_expense_against_budget(args)
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530323
Ankush Menat494bd9e2022-03-28 18:52:46 +0530324
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530325def validate_cwip_accounts(gl_map):
Ankush91527152021-08-11 11:17:50 +0530326 """Validate that CWIP account are not used in Journal Entry"""
327 if gl_map and gl_map[0].voucher_type != "Journal Entry":
328 return
Maricad00c5982019-11-12 19:17:43 +0530329
Ankush Menat494bd9e2022-03-28 18:52:46 +0530330 cwip_enabled = any(
331 cint(ac.enable_cwip_accounting)
332 for ac in frappe.db.get_all("Asset Category", "enable_cwip_accounting")
333 )
Ankush91527152021-08-11 11:17:50 +0530334 if cwip_enabled:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530335 cwip_accounts = [
336 d[0]
337 for d in frappe.db.sql(
338 """select name from tabAccount
339 where account_type = 'Capital Work in Progress' and is_group=0"""
340 )
341 ]
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530342
Ankush91527152021-08-11 11:17:50 +0530343 for entry in gl_map:
344 if entry.account in cwip_accounts:
345 frappe.throw(
Ankush Menat494bd9e2022-03-28 18:52:46 +0530346 _(
347 "Account: <b>{0}</b> is capital Work in progress and can not be updated by Journal Entry"
348 ).format(entry.account)
349 )
350
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530351
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530352def process_debit_credit_difference(gl_map):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530353 precision = get_field_precision(
354 frappe.get_meta("GL Entry").get_field("debit"),
355 currency=frappe.get_cached_value("Company", gl_map[0].company, "default_currency"),
356 )
Anand Doshi602e8252015-11-16 19:05:46 +0530357
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530358 voucher_type = gl_map[0].voucher_type
359 voucher_no = gl_map[0].voucher_no
360 allowance = get_debit_credit_allowance(voucher_type, precision)
361
362 debit_credit_diff = get_debit_credit_difference(gl_map, precision)
ruthra kumar914b2302023-01-02 14:33:14 +0530363
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530364 if abs(debit_credit_diff) > allowance:
ruthra kumar914b2302023-01-02 14:33:14 +0530365 if not (
366 voucher_type == "Journal Entry"
367 and frappe.get_cached_value("Journal Entry", voucher_no, "voucher_type")
368 == "Exchange Gain Or Loss"
369 ):
370 raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no)
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530371
372 elif abs(debit_credit_diff) >= (1.0 / (10**precision)):
373 make_round_off_gle(gl_map, debit_credit_diff, precision)
374
375 debit_credit_diff = get_debit_credit_difference(gl_map, precision)
376 if abs(debit_credit_diff) > allowance:
ruthra kumar914b2302023-01-02 14:33:14 +0530377 if not (
378 voucher_type == "Journal Entry"
379 and frappe.get_cached_value("Journal Entry", voucher_no, "voucher_type")
380 == "Exchange Gain Or Loss"
381 ):
382 raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no)
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530383
384
385def get_debit_credit_difference(gl_map, precision):
Nabin Haite2c200a2015-05-28 13:00:37 +0530386 debit_credit_diff = 0.0
387 for entry in gl_map:
388 entry.debit = flt(entry.debit, precision)
389 entry.credit = flt(entry.credit, precision)
390 debit_credit_diff += entry.debit - entry.credit
Anand Doshi602e8252015-11-16 19:05:46 +0530391
Nabin Haite2c200a2015-05-28 13:00:37 +0530392 debit_credit_diff = flt(debit_credit_diff, precision)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530393
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530394 return debit_credit_diff
395
396
397def get_debit_credit_allowance(voucher_type, precision):
398 if voucher_type in ("Journal Entry", "Payment Entry"):
Nabin Haitfb24a272016-02-18 19:18:07 +0530399 allowance = 5.0 / (10**precision)
400 else:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530401 allowance = 0.5
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530402
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530403 return allowance
Anand Doshi602e8252015-11-16 19:05:46 +0530404
Saqib Ansaribfc34e12022-04-01 11:03:16 +0530405
406def raise_debit_credit_not_equal_error(debit_credit_diff, voucher_type, voucher_no):
407 frappe.throw(
408 _("Debit and Credit not equal for {0} #{1}. Difference is {2}.").format(
409 voucher_type, voucher_no, debit_credit_diff
410 )
411 )
Anand Doshi602e8252015-11-16 19:05:46 +0530412
Ankush Menat494bd9e2022-03-28 18:52:46 +0530413
Nabin Hait34c551d2019-07-03 10:34:31 +0530414def make_round_off_gle(gl_map, debit_credit_diff, precision):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530415 round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(
Deepesh Garg0ac11a52022-04-20 12:18:11 +0530416 gl_map[0].company, gl_map[0].voucher_type, gl_map[0].voucher_no
Ankush Menat494bd9e2022-03-28 18:52:46 +0530417 )
Nabin Hait80069a62015-05-28 19:19:59 +0530418 round_off_gle = frappe._dict()
Nabin Hait022d8d52022-11-21 15:16:53 +0530419 round_off_account_exists = False
Zarrar3523b772018-08-14 16:28:14 +0530420
Nabin Hait022d8d52022-11-21 15:16:53 +0530421 if gl_map[0].voucher_type != "Period Closing Voucher":
422 for d in gl_map:
423 if d.account == round_off_account:
424 round_off_gle = d
425 if d.debit:
426 debit_credit_diff -= flt(d.debit) - flt(d.credit)
427 else:
428 debit_credit_diff += flt(d.credit)
429 round_off_account_exists = True
430
431 if round_off_account_exists and abs(debit_credit_diff) < (1.0 / (10**precision)):
432 gl_map.remove(round_off_gle)
433 return
Nabin Hait34c551d2019-07-03 10:34:31 +0530434
Zarrar3523b772018-08-14 16:28:14 +0530435 if not round_off_gle:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530436 for k in ["voucher_type", "voucher_no", "company", "posting_date", "remarks"]:
437 round_off_gle[k] = gl_map[0][k]
Anand Doshi602e8252015-11-16 19:05:46 +0530438
Ankush Menat494bd9e2022-03-28 18:52:46 +0530439 round_off_gle.update(
440 {
441 "account": round_off_account,
442 "debit_in_account_currency": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
443 "credit_in_account_currency": debit_credit_diff if debit_credit_diff > 0 else 0,
444 "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
445 "credit": debit_credit_diff if debit_credit_diff > 0 else 0,
446 "cost_center": round_off_cost_center,
447 "party_type": None,
448 "party": None,
449 "is_opening": "No",
450 "against_voucher_type": None,
451 "against_voucher": None,
452 }
453 )
Anand Doshi602e8252015-11-16 19:05:46 +0530454
Deepesh Garg015812b2022-04-23 21:40:08 +0530455 update_accounting_dimensions(round_off_gle)
Zarrar3523b772018-08-14 16:28:14 +0530456 if not round_off_account_exists:
457 gl_map.append(round_off_gle)
Nabin Haite2c200a2015-05-28 13:00:37 +0530458
Ankush Menat494bd9e2022-03-28 18:52:46 +0530459
Deepesh Garg015812b2022-04-23 21:40:08 +0530460def update_accounting_dimensions(round_off_gle):
461 dimensions = get_accounting_dimensions()
Deepesh Gargc312cd32022-04-24 18:11:32 +0530462 meta = frappe.get_meta(round_off_gle["voucher_type"])
463 has_all_dimensions = True
Deepesh Garg015812b2022-04-23 21:40:08 +0530464
465 for dimension in dimensions:
Deepesh Gargc312cd32022-04-24 18:11:32 +0530466 if not meta.has_field(dimension):
467 has_all_dimensions = False
468
469 if dimensions and has_all_dimensions:
470 dimension_values = frappe.db.get_value(
Deepesh Garg3fa1c632022-04-25 16:29:26 +0530471 round_off_gle["voucher_type"], round_off_gle["voucher_no"], dimensions, as_dict=1
Deepesh Gargc312cd32022-04-24 18:11:32 +0530472 )
473
474 for dimension in dimensions:
475 round_off_gle[dimension] = dimension_values.get(dimension)
Deepesh Garg015812b2022-04-23 21:40:08 +0530476
477
ruthra kumarebe67872023-04-28 14:07:28 +0530478def get_round_off_account_and_cost_center(
479 company, voucher_type, voucher_no, use_company_default=False
480):
Ankush Menat494bd9e2022-03-28 18:52:46 +0530481 round_off_account, round_off_cost_center = frappe.get_cached_value(
482 "Company", company, ["round_off_account", "round_off_cost_center"]
483 ) or [None, None]
Deepesh Garg0ac11a52022-04-20 12:18:11 +0530484
Deepesh Gargc312cd32022-04-24 18:11:32 +0530485 meta = frappe.get_meta(voucher_type)
486
Deepesh Garg0ac11a52022-04-20 12:18:11 +0530487 # Give first preference to parent cost center for round off GLE
ruthra kumarebe67872023-04-28 14:07:28 +0530488 if not use_company_default and meta.has_field("cost_center"):
Deepesh Garg0ac11a52022-04-20 12:18:11 +0530489 parent_cost_center = frappe.db.get_value(voucher_type, voucher_no, "cost_center")
490 if parent_cost_center:
491 round_off_cost_center = parent_cost_center
492
Nabin Hait2e4de832017-09-19 14:53:16 +0530493 if not round_off_account:
494 frappe.throw(_("Please mention Round Off Account in Company"))
495
496 if not round_off_cost_center:
497 frappe.throw(_("Please mention Round Off Cost Center in Company"))
498
499 return round_off_account, round_off_cost_center
500
Ankush Menat494bd9e2022-03-28 18:52:46 +0530501
502def make_reverse_gl_entries(
503 gl_entries=None, voucher_type=None, voucher_no=None, adv_adj=False, update_outstanding="Yes"
504):
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530505 """
Ankush Menat494bd9e2022-03-28 18:52:46 +0530506 Get original gl entries of the voucher
507 and make reverse gl entries by swapping debit and credit
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530508 """
Anand Doshi652bc072014-04-16 15:21:46 +0530509
Nabin Hait2e296fa2013-08-28 18:53:11 +0530510 if not gl_entries:
Deepesh Gargff574502022-02-06 22:56:12 +0530511 gl_entry = frappe.qb.DocType("GL Entry")
Ankush Menat494bd9e2022-03-28 18:52:46 +0530512 gl_entries = (
513 frappe.qb.from_(gl_entry)
514 .select("*")
515 .where(gl_entry.voucher_type == voucher_type)
516 .where(gl_entry.voucher_no == voucher_no)
517 .where(gl_entry.is_cancelled == 0)
518 .for_update()
519 ).run(as_dict=1)
Nabin Hait56548cb2016-07-01 15:58:39 +0530520
Nabin Hait27994c22013-08-26 16:53:30 +0530521 if gl_entries:
ruthra kumar7312f222022-05-29 21:33:08 +0530522 create_payment_ledger_entry(
523 gl_entries, cancel=1, adv_adj=adv_adj, update_outstanding=update_outstanding
524 )
Deepesh Garg069a54e2020-08-10 16:01:01 +0530525 validate_accounting_period(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530526 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Deepesh Gargf92c63f2023-02-26 15:53:33 +0530527
528 is_opening = any(d.get("is_opening") == "Yes" for d in gl_entries)
529 validate_against_pcv(is_opening, gl_entries[0]["posting_date"], gl_entries[0]["company"])
Ankush Menat494bd9e2022-03-28 18:52:46 +0530530 set_as_cancel(gl_entries[0]["voucher_type"], gl_entries[0]["voucher_no"])
Anand Doshi652bc072014-04-16 15:21:46 +0530531
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530532 for entry in gl_entries:
Deepesh Gargffec8652022-02-02 17:14:42 +0530533 new_gle = copy.deepcopy(entry)
Ankush Menat494bd9e2022-03-28 18:52:46 +0530534 new_gle["name"] = None
535 debit = new_gle.get("debit", 0)
536 credit = new_gle.get("credit", 0)
Anand Doshi652bc072014-04-16 15:21:46 +0530537
Ankush Menat494bd9e2022-03-28 18:52:46 +0530538 debit_in_account_currency = new_gle.get("debit_in_account_currency", 0)
539 credit_in_account_currency = new_gle.get("credit_in_account_currency", 0)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530540
Ankush Menat494bd9e2022-03-28 18:52:46 +0530541 new_gle["debit"] = credit
542 new_gle["credit"] = debit
543 new_gle["debit_in_account_currency"] = credit_in_account_currency
544 new_gle["credit_in_account_currency"] = debit_in_account_currency
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530545
Ankush Menat494bd9e2022-03-28 18:52:46 +0530546 new_gle["remarks"] = "On cancellation of " + new_gle["voucher_no"]
547 new_gle["is_cancelled"] = 1
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530548
Ankush Menat494bd9e2022-03-28 18:52:46 +0530549 if new_gle["debit"] or new_gle["credit"]:
Deepesh Gargffec8652022-02-02 17:14:42 +0530550 make_entry(new_gle, adv_adj, "Yes")
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530551
552
553def check_freezing_date(posting_date, adv_adj=False):
554 """
Ankush Menat494bd9e2022-03-28 18:52:46 +0530555 Nobody can do GL Entries where posting date is before freezing date
556 except authorized person
Deepesh Garg13d2e7b2021-09-16 18:54:57 +0530557
Ankush Menat494bd9e2022-03-28 18:52:46 +0530558 Administrator has all the roles so this check will be bypassed if any role is allowed to post
559 Hence stop admin to bypass if accounts are freezed
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530560 """
561 if not adv_adj:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530562 acc_frozen_upto = frappe.db.get_value("Accounts Settings", None, "acc_frozen_upto")
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530563 if acc_frozen_upto:
Ankush Menat494bd9e2022-03-28 18:52:46 +0530564 frozen_accounts_modifier = frappe.db.get_value(
565 "Accounts Settings", None, "frozen_accounts_modifier"
566 )
567 if getdate(posting_date) <= getdate(acc_frozen_upto) and (
568 frozen_accounts_modifier not in frappe.get_roles() or frappe.session.user == "Administrator"
569 ):
570 frappe.throw(
571 _("You are not authorized to add or update entries before {0}").format(
572 formatdate(acc_frozen_upto)
573 )
574 )
575
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530576
Deepesh Gargf92c63f2023-02-26 15:53:33 +0530577def validate_against_pcv(is_opening, posting_date, company):
578 if is_opening and frappe.db.exists(
579 "Period Closing Voucher", {"docstatus": 1, "company": company}
580 ):
581 frappe.throw(
582 _("Opening Entry can not be created after Period Closing Voucher is created."),
583 title=_("Invalid Opening Entry"),
584 )
585
586 last_pcv_date = frappe.db.get_value(
587 "Period Closing Voucher", {"docstatus": 1, "company": company}, "max(posting_date)"
588 )
589
590 if last_pcv_date and getdate(posting_date) <= getdate(last_pcv_date):
591 message = _("Books have been closed till the period ending on {0}").format(
592 formatdate(last_pcv_date)
593 )
594 message += "</br >"
Deepesh Garg8ce1da12023-03-23 19:14:12 +0530595 message += _("You cannot create/amend any accounting entries till this date.")
Deepesh Gargf92c63f2023-02-26 15:53:33 +0530596 frappe.throw(message, title=_("Period Closed"))
597
598
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530599def set_as_cancel(voucher_type, voucher_no):
600 """
Ankush Menat494bd9e2022-03-28 18:52:46 +0530601 Set is_cancelled=1 in all original gl entries for the voucher
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530602 """
Ankush Menat494bd9e2022-03-28 18:52:46 +0530603 frappe.db.sql(
604 """UPDATE `tabGL Entry` SET is_cancelled = 1,
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530605 modified=%s, modified_by=%s
606 where voucher_type=%s and voucher_no=%s and is_cancelled = 0""",
Ankush Menat494bd9e2022-03-28 18:52:46 +0530607 (now(), frappe.session.user, voucher_type, voucher_no),
608 )