blob: 287c79f13fe28ab9d115200bd1caa8ae58050f10 [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
4from __future__ import unicode_literals
Rohit Waghchauree9ff1912017-06-19 12:54:59 +05305import frappe, erpnext
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +05306from frappe.utils import flt, cstr, cint, comma_and, today, getdate, formatdate, now
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05307from frappe import _
Nabin Haite2c200a2015-05-28 13:00:37 +05308from frappe.model.meta import get_field_precision
Nabin Haitb9bc7d62016-05-16 14:38:47 +05309from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget
deepeshgarg0073d11ac02019-05-19 00:02:01 +053010from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions
Nabin Haitbf495c92013-01-30 12:49:08 +053011
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053012class ClosedAccountingPeriod(frappe.ValidationError): pass
Nabin Haitbf495c92013-01-30 12:49:08 +053013
Nabin Haitbd4bdca2020-12-21 20:03:59 +053014def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True, update_outstanding='Yes', from_repost=False):
Nabin Hait2e296fa2013-08-28 18:53:11 +053015 if gl_map:
16 if not cancel:
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053017 validate_accounting_period(gl_map)
Nabin Hait2e296fa2013-08-28 18:53:11 +053018 gl_map = process_gl_map(gl_map, merge_entries)
nabinhaitc3432922014-07-29 18:06:18 +053019 if gl_map and len(gl_map) > 1:
Nabin Haitbd4bdca2020-12-21 20:03:59 +053020 save_entries(gl_map, adv_adj, update_outstanding, from_repost)
nabinhaitc3432922014-07-29 18:06:18 +053021 else:
nabinhait4859b482014-07-30 14:28:24 +053022 frappe.throw(_("Incorrect number of General Ledger Entries found. You might have selected a wrong Account in the transaction."))
Nabin Hait2e296fa2013-08-28 18:53:11 +053023 else:
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +053024 make_reverse_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding)
Anand Doshi652bc072014-04-16 15:21:46 +053025
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053026def validate_accounting_period(gl_map):
27 accounting_periods = frappe.db.sql(""" SELECT
28 ap.name as name
29 FROM
30 `tabAccounting Period` ap, `tabClosed Document` cd
31 WHERE
32 ap.name = cd.parent
33 AND ap.company = %(company)s
34 AND cd.closed = 1
35 AND cd.document_type = %(voucher_type)s
36 AND %(date)s between ap.start_date and ap.end_date
37 """, {
38 'date': gl_map[0].posting_date,
39 'company': gl_map[0].company,
40 'voucher_type': gl_map[0].voucher_type
41 }, as_dict=1)
42
43 if accounting_periods:
Deepesh Garg069a54e2020-08-10 16:01:01 +053044 frappe.throw(_("You cannot create or cancel any accounting entries with in the closed Accounting Period {0}")
45 .format(frappe.bold(accounting_periods[0].name)), ClosedAccountingPeriod)
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053046
Nabin Hait27994c22013-08-26 16:53:30 +053047def process_gl_map(gl_map, merge_entries=True):
Nabin Haitbf495c92013-01-30 12:49:08 +053048 if merge_entries:
49 gl_map = merge_similar_entries(gl_map)
Nabin Hait27994c22013-08-26 16:53:30 +053050 for entry in gl_map:
Anand Doshi602e8252015-11-16 19:05:46 +053051 # toggle debit, credit if negative entry
Nabin Hait88f3cd52013-10-23 16:29:19 +053052 if flt(entry.debit) < 0:
53 entry.credit = flt(entry.credit) - flt(entry.debit)
54 entry.debit = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053055
Nabin Haitc561a492015-08-19 19:22:34 +053056 if flt(entry.debit_in_account_currency) < 0:
57 entry.credit_in_account_currency = \
58 flt(entry.credit_in_account_currency) - flt(entry.debit_in_account_currency)
59 entry.debit_in_account_currency = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053060
Nabin Hait88f3cd52013-10-23 16:29:19 +053061 if flt(entry.credit) < 0:
62 entry.debit = flt(entry.debit) - flt(entry.credit)
63 entry.credit = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053064
Nabin Haitc561a492015-08-19 19:22:34 +053065 if flt(entry.credit_in_account_currency) < 0:
66 entry.debit_in_account_currency = \
67 flt(entry.debit_in_account_currency) - flt(entry.credit_in_account_currency)
68 entry.credit_in_account_currency = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053069
Nabin Hait27994c22013-08-26 16:53:30 +053070 return gl_map
Anand Doshi652bc072014-04-16 15:21:46 +053071
Nabin Haitbf495c92013-01-30 12:49:08 +053072def merge_similar_entries(gl_map):
73 merged_gl_map = []
deepeshgarg0073d11ac02019-05-19 00:02:01 +053074 accounting_dimensions = get_accounting_dimensions()
Nabin Haitbf495c92013-01-30 12:49:08 +053075 for entry in gl_map:
Anand Doshi652bc072014-04-16 15:21:46 +053076 # if there is already an entry in this account then just add it
Nabin Haitbf495c92013-01-30 12:49:08 +053077 # to that entry
deepeshgarg0073d11ac02019-05-19 00:02:01 +053078 same_head = check_if_in_list(entry, merged_gl_map, accounting_dimensions)
Nabin Haitbf495c92013-01-30 12:49:08 +053079 if same_head:
Nabin Hait2e296fa2013-08-28 18:53:11 +053080 same_head.debit = flt(same_head.debit) + flt(entry.debit)
Nabin Haitc561a492015-08-19 19:22:34 +053081 same_head.debit_in_account_currency = \
82 flt(same_head.debit_in_account_currency) + flt(entry.debit_in_account_currency)
Nabin Hait2e296fa2013-08-28 18:53:11 +053083 same_head.credit = flt(same_head.credit) + flt(entry.credit)
Nabin Haitc561a492015-08-19 19:22:34 +053084 same_head.credit_in_account_currency = \
85 flt(same_head.credit_in_account_currency) + flt(entry.credit_in_account_currency)
Nabin Haitbf495c92013-01-30 12:49:08 +053086 else:
87 merged_gl_map.append(entry)
Anand Doshi652bc072014-04-16 15:21:46 +053088
thefalconx33bfc43d32019-12-13 15:09:51 +053089 company = gl_map[0].company if gl_map else erpnext.get_default_company()
90 company_currency = erpnext.get_company_currency(company)
91 precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"), company_currency)
92
Nabin Hait815a49e2013-08-07 17:00:01 +053093 # filter zero debit and credit entries
thefalconx33bfc43d32019-12-13 15:09:51 +053094 merged_gl_map = filter(lambda x: flt(x.debit, precision)!=0 or flt(x.credit, precision)!=0, merged_gl_map)
Achilles Rasquinha908289d2018-03-08 13:10:51 +053095 merged_gl_map = list(merged_gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +053096
Nabin Haitbf495c92013-01-30 12:49:08 +053097 return merged_gl_map
98
deepeshgarg0073d11ac02019-05-19 00:02:01 +053099def check_if_in_list(gle, gl_map, dimensions=None):
100 account_head_fieldnames = ['party_type', 'party', 'against_voucher', 'against_voucher_type',
101 'cost_center', 'project']
102
103 if dimensions:
104 account_head_fieldnames = account_head_fieldnames + dimensions
105
Nabin Haitbb777562013-08-29 18:19:37 +0530106 for e in gl_map:
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530107 same_head = True
108 if e.account != gle.account:
109 same_head = False
110
111 for fieldname in account_head_fieldnames:
112 if cstr(e.get(fieldname)) != cstr(gle.get(fieldname)):
113 same_head = False
114
115 if same_head:
116 return e
Nabin Haitbf495c92013-01-30 12:49:08 +0530117
Nabin Haitbd4bdca2020-12-21 20:03:59 +0530118def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False):
119 if not from_repost:
120 validate_cwip_accounts(gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530121
Nabin Haite2c200a2015-05-28 13:00:37 +0530122 round_off_debit_credit(gl_map)
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530123
124 if gl_map:
125 check_freezing_date(gl_map[0]["posting_date"], adv_adj)
126
Nabin Haitbf495c92013-01-30 12:49:08 +0530127 for entry in gl_map:
Nabin Haitbd4bdca2020-12-21 20:03:59 +0530128 make_entry(entry, adv_adj, update_outstanding, from_repost)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530129
Nabin Haitbd4bdca2020-12-21 20:03:59 +0530130def make_entry(args, adv_adj, update_outstanding, from_repost=False):
Nabin Haitddc3bb22020-03-17 17:04:18 +0530131 gle = frappe.new_doc("GL Entry")
132 gle.update(args)
Anand Doshi6dfd4302015-02-10 14:41:27 +0530133 gle.flags.ignore_permissions = 1
Nabin Haitbd4bdca2020-12-21 20:03:59 +0530134 gle.flags.from_repost = from_repost
Deepesh Garg8230e412020-06-29 21:20:05 +0530135 gle.insert()
Nabin Haitbd4bdca2020-12-21 20:03:59 +0530136 gle.run_method("on_update_with_args", adv_adj, update_outstanding, from_repost)
Nabin Haitaeba24e2013-08-23 15:17:36 +0530137 gle.submit()
Anand Doshi652bc072014-04-16 15:21:46 +0530138
Nabin Haitbd4bdca2020-12-21 20:03:59 +0530139 if not from_repost:
140 validate_expense_against_budget(args)
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530141
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530142def validate_cwip_accounts(gl_map):
Saqibf37a46e2019-11-22 16:32:50 +0530143 cwip_enabled = any([cint(ac.enable_cwip_accounting) for ac in frappe.db.get_all("Asset Category","enable_cwip_accounting")])
Maricad00c5982019-11-12 19:17:43 +0530144
145 if cwip_enabled and gl_map[0].voucher_type == "Journal Entry":
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530146 cwip_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount
147 where account_type = 'Capital Work in Progress' and is_group=0""")]
148
149 for entry in gl_map:
150 if entry.account in cwip_accounts:
Maricad00c5982019-11-12 19:17:43 +0530151 frappe.throw(
152 _("Account: <b>{0}</b> is capital Work in progress and can not be updated by Journal Entry").format(entry.account))
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530153
Nabin Haite2c200a2015-05-28 13:00:37 +0530154def round_off_debit_credit(gl_map):
155 precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"),
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530156 currency=frappe.get_cached_value('Company', gl_map[0].company, "default_currency"))
Anand Doshi602e8252015-11-16 19:05:46 +0530157
Nabin Haite2c200a2015-05-28 13:00:37 +0530158 debit_credit_diff = 0.0
159 for entry in gl_map:
160 entry.debit = flt(entry.debit, precision)
161 entry.credit = flt(entry.credit, precision)
162 debit_credit_diff += entry.debit - entry.credit
Anand Doshi602e8252015-11-16 19:05:46 +0530163
Nabin Haite2c200a2015-05-28 13:00:37 +0530164 debit_credit_diff = flt(debit_credit_diff, precision)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530165
Nabin Haitd2a966e2017-05-05 10:41:16 +0530166 if gl_map[0]["voucher_type"] in ("Journal Entry", "Payment Entry"):
Nabin Haitfb24a272016-02-18 19:18:07 +0530167 allowance = 5.0 / (10**precision)
168 else:
Nabin Haitd2a966e2017-05-05 10:41:16 +0530169 allowance = .5
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530170
Nabin Haitfb24a272016-02-18 19:18:07 +0530171 if abs(debit_credit_diff) >= allowance:
Nabin Haite2c200a2015-05-28 13:00:37 +0530172 frappe.throw(_("Debit and Credit not equal for {0} #{1}. Difference is {2}.")
173 .format(gl_map[0].voucher_type, gl_map[0].voucher_no, debit_credit_diff))
Anand Doshi602e8252015-11-16 19:05:46 +0530174
Nabin Haite2c200a2015-05-28 13:00:37 +0530175 elif abs(debit_credit_diff) >= (1.0 / (10**precision)):
Nabin Hait34c551d2019-07-03 10:34:31 +0530176 make_round_off_gle(gl_map, debit_credit_diff, precision)
Anand Doshi602e8252015-11-16 19:05:46 +0530177
Nabin Hait34c551d2019-07-03 10:34:31 +0530178def make_round_off_gle(gl_map, debit_credit_diff, precision):
Nabin Hait2e4de832017-09-19 14:53:16 +0530179 round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(gl_map[0].company)
Zarrar3523b772018-08-14 16:28:14 +0530180 round_off_account_exists = False
Nabin Hait80069a62015-05-28 19:19:59 +0530181 round_off_gle = frappe._dict()
Zarrar3523b772018-08-14 16:28:14 +0530182 for d in gl_map:
183 if d.account == round_off_account:
184 round_off_gle = d
185 if d.debit_in_account_currency:
186 debit_credit_diff -= flt(d.debit_in_account_currency)
187 else:
188 debit_credit_diff += flt(d.credit_in_account_currency)
189 round_off_account_exists = True
190
Nabin Hait34c551d2019-07-03 10:34:31 +0530191 if round_off_account_exists and abs(debit_credit_diff) <= (1.0 / (10**precision)):
192 gl_map.remove(round_off_gle)
193 return
194
Zarrar3523b772018-08-14 16:28:14 +0530195 if not round_off_gle:
196 for k in ["voucher_type", "voucher_no", "company",
197 "posting_date", "remarks", "is_opening"]:
198 round_off_gle[k] = gl_map[0][k]
Anand Doshi602e8252015-11-16 19:05:46 +0530199
Nabin Haite2c200a2015-05-28 13:00:37 +0530200 round_off_gle.update({
201 "account": round_off_account,
Nabin Haitd4507ac2016-01-20 16:14:27 +0530202 "debit_in_account_currency": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
203 "credit_in_account_currency": debit_credit_diff if debit_credit_diff > 0 else 0,
Nabin Haite2c200a2015-05-28 13:00:37 +0530204 "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
205 "credit": debit_credit_diff if debit_credit_diff > 0 else 0,
Nabin Hait3c671462015-05-28 13:19:01 +0530206 "cost_center": round_off_cost_center,
207 "party_type": None,
208 "party": None,
209 "against_voucher_type": None,
210 "against_voucher": None
Nabin Haite2c200a2015-05-28 13:00:37 +0530211 })
Anand Doshi602e8252015-11-16 19:05:46 +0530212
Zarrar3523b772018-08-14 16:28:14 +0530213 if not round_off_account_exists:
214 gl_map.append(round_off_gle)
Nabin Haite2c200a2015-05-28 13:00:37 +0530215
Nabin Hait2e4de832017-09-19 14:53:16 +0530216def get_round_off_account_and_cost_center(company):
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530217 round_off_account, round_off_cost_center = frappe.get_cached_value('Company', company,
Nabin Hait2e4de832017-09-19 14:53:16 +0530218 ["round_off_account", "round_off_cost_center"]) or [None, None]
219 if not round_off_account:
220 frappe.throw(_("Please mention Round Off Account in Company"))
221
222 if not round_off_cost_center:
223 frappe.throw(_("Please mention Round Off Cost Center in Company"))
224
225 return round_off_account, round_off_cost_center
226
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530227def make_reverse_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,
228 adv_adj=False, update_outstanding="Yes"):
229 """
230 Get original gl entries of the voucher
231 and make reverse gl entries by swapping debit and credit
232 """
Anand Doshi652bc072014-04-16 15:21:46 +0530233
Nabin Hait2e296fa2013-08-28 18:53:11 +0530234 if not gl_entries:
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530235 gl_entries = frappe.get_all("GL Entry",
236 fields = ["*"],
237 filters = {
238 "voucher_type": voucher_type,
Deepesh Gargb821f222020-05-06 18:12:23 +0530239 "voucher_no": voucher_no,
240 "is_cancelled": 0
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530241 })
Nabin Hait56548cb2016-07-01 15:58:39 +0530242
Nabin Hait27994c22013-08-26 16:53:30 +0530243 if gl_entries:
Deepesh Garg069a54e2020-08-10 16:01:01 +0530244 validate_accounting_period(gl_entries)
Nabin Hait27994c22013-08-26 16:53:30 +0530245 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Deepesh Garg069a54e2020-08-10 16:01:01 +0530246 set_as_cancel(gl_entries[0]['voucher_type'], gl_entries[0]['voucher_no'])
Anand Doshi652bc072014-04-16 15:21:46 +0530247
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530248 for entry in gl_entries:
249 entry['name'] = None
250 debit = entry.get('debit', 0)
251 credit = entry.get('credit', 0)
Anand Doshi652bc072014-04-16 15:21:46 +0530252
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530253 debit_in_account_currency = entry.get('debit_in_account_currency', 0)
254 credit_in_account_currency = entry.get('credit_in_account_currency', 0)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530255
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530256 entry['debit'] = credit
257 entry['credit'] = debit
258 entry['debit_in_account_currency'] = credit_in_account_currency
259 entry['credit_in_account_currency'] = debit_in_account_currency
260
261 entry['remarks'] = "On cancellation of " + entry['voucher_no']
262 entry['is_cancelled'] = 1
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530263
264 if entry['debit'] or entry['credit']:
265 make_entry(entry, adv_adj, "Yes")
266
267
268def check_freezing_date(posting_date, adv_adj=False):
269 """
270 Nobody can do GL Entries where posting date is before freezing date
271 except authorized person
272 """
273 if not adv_adj:
274 acc_frozen_upto = frappe.db.get_value('Accounts Settings', None, 'acc_frozen_upto')
275 if acc_frozen_upto:
276 frozen_accounts_modifier = frappe.db.get_value( 'Accounts Settings', None,'frozen_accounts_modifier')
277 if getdate(posting_date) <= getdate(acc_frozen_upto) \
278 and not frozen_accounts_modifier in frappe.get_roles():
279 frappe.throw(_("You are not authorized to add or update entries before {0}").format(formatdate(acc_frozen_upto)))
280
281def set_as_cancel(voucher_type, voucher_no):
282 """
283 Set is_cancelled=1 in all original gl entries for the voucher
284 """
Deepesh Garg069a54e2020-08-10 16:01:01 +0530285 frappe.db.sql("""UPDATE `tabGL Entry` SET is_cancelled = 1,
Deepesh Garg2a9c5ba2020-04-30 10:38:58 +0530286 modified=%s, modified_by=%s
287 where voucher_type=%s and voucher_no=%s and is_cancelled = 0""",
288 (now(), frappe.session.user, voucher_type, voucher_no))