blob: 5c9e93d019ebbb56e81548603dd83b52a41e7e42 [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
Nabin Hait818d9672015-06-22 18:21:38 +05306from frappe.utils import flt, cstr, cint
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
Nabin Haitbb777562013-08-29 18:19:37 +053012
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053013class ClosedAccountingPeriod(frappe.ValidationError): pass
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053014class StockAccountInvalidTransaction(frappe.ValidationError): pass
Nabin Haitbf495c92013-01-30 12:49:08 +053015
Nabin Hait9784d272016-12-30 16:21:35 +053016def 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 +053017 if gl_map:
18 if not cancel:
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053019 validate_accounting_period(gl_map)
Nabin Hait2e296fa2013-08-28 18:53:11 +053020 gl_map = process_gl_map(gl_map, merge_entries)
nabinhaitc3432922014-07-29 18:06:18 +053021 if gl_map and len(gl_map) > 1:
Nabin Hait9784d272016-12-30 16:21:35 +053022 save_entries(gl_map, adv_adj, update_outstanding, from_repost)
nabinhaitc3432922014-07-29 18:06:18 +053023 else:
nabinhait4859b482014-07-30 14:28:24 +053024 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 +053025 else:
26 delete_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding)
Anand Doshi652bc072014-04-16 15:21:46 +053027
Mangesh-Khairnare5c733b2019-08-08 15:44:11 +053028def validate_accounting_period(gl_map):
29 accounting_periods = frappe.db.sql(""" SELECT
30 ap.name as name
31 FROM
32 `tabAccounting Period` ap, `tabClosed Document` cd
33 WHERE
34 ap.name = cd.parent
35 AND ap.company = %(company)s
36 AND cd.closed = 1
37 AND cd.document_type = %(voucher_type)s
38 AND %(date)s between ap.start_date and ap.end_date
39 """, {
40 'date': gl_map[0].posting_date,
41 'company': gl_map[0].company,
42 'voucher_type': gl_map[0].voucher_type
43 }, as_dict=1)
44
45 if accounting_periods:
46 frappe.throw(_("You can't create accounting entries in the closed accounting period {0}")
47 .format(accounting_periods[0].name), ClosedAccountingPeriod)
48
Nabin Hait27994c22013-08-26 16:53:30 +053049def process_gl_map(gl_map, merge_entries=True):
Nabin Haitbf495c92013-01-30 12:49:08 +053050 if merge_entries:
51 gl_map = merge_similar_entries(gl_map)
Nabin Hait27994c22013-08-26 16:53:30 +053052 for entry in gl_map:
Anand Doshi602e8252015-11-16 19:05:46 +053053 # toggle debit, credit if negative entry
Nabin Hait88f3cd52013-10-23 16:29:19 +053054 if flt(entry.debit) < 0:
55 entry.credit = flt(entry.credit) - flt(entry.debit)
56 entry.debit = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053057
Nabin Haitc561a492015-08-19 19:22:34 +053058 if flt(entry.debit_in_account_currency) < 0:
59 entry.credit_in_account_currency = \
60 flt(entry.credit_in_account_currency) - flt(entry.debit_in_account_currency)
61 entry.debit_in_account_currency = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053062
Nabin Hait88f3cd52013-10-23 16:29:19 +053063 if flt(entry.credit) < 0:
64 entry.debit = flt(entry.debit) - flt(entry.credit)
65 entry.credit = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053066
Nabin Haitc561a492015-08-19 19:22:34 +053067 if flt(entry.credit_in_account_currency) < 0:
68 entry.debit_in_account_currency = \
69 flt(entry.debit_in_account_currency) - flt(entry.credit_in_account_currency)
70 entry.credit_in_account_currency = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053071
Nabin Hait27994c22013-08-26 16:53:30 +053072 return gl_map
Anand Doshi652bc072014-04-16 15:21:46 +053073
Nabin Haitbf495c92013-01-30 12:49:08 +053074def merge_similar_entries(gl_map):
75 merged_gl_map = []
deepeshgarg0073d11ac02019-05-19 00:02:01 +053076 accounting_dimensions = get_accounting_dimensions()
Nabin Haitbf495c92013-01-30 12:49:08 +053077 for entry in gl_map:
Anand Doshi652bc072014-04-16 15:21:46 +053078 # if there is already an entry in this account then just add it
Nabin Haitbf495c92013-01-30 12:49:08 +053079 # to that entry
deepeshgarg0073d11ac02019-05-19 00:02:01 +053080 same_head = check_if_in_list(entry, merged_gl_map, accounting_dimensions)
Nabin Haitbf495c92013-01-30 12:49:08 +053081 if same_head:
Nabin Hait2e296fa2013-08-28 18:53:11 +053082 same_head.debit = flt(same_head.debit) + flt(entry.debit)
Nabin Haitc561a492015-08-19 19:22:34 +053083 same_head.debit_in_account_currency = \
84 flt(same_head.debit_in_account_currency) + flt(entry.debit_in_account_currency)
Nabin Hait2e296fa2013-08-28 18:53:11 +053085 same_head.credit = flt(same_head.credit) + flt(entry.credit)
Nabin Haitc561a492015-08-19 19:22:34 +053086 same_head.credit_in_account_currency = \
87 flt(same_head.credit_in_account_currency) + flt(entry.credit_in_account_currency)
Nabin Haitbf495c92013-01-30 12:49:08 +053088 else:
89 merged_gl_map.append(entry)
Anand Doshi652bc072014-04-16 15:21:46 +053090
Nabin Hait815a49e2013-08-07 17:00:01 +053091 # filter zero debit and credit entries
Nabin Hait2e54da22015-08-13 12:19:20 +053092 merged_gl_map = filter(lambda x: flt(x.debit, 9)!=0 or flt(x.credit, 9)!=0, merged_gl_map)
Achilles Rasquinha908289d2018-03-08 13:10:51 +053093 merged_gl_map = list(merged_gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +053094
Nabin Haitbf495c92013-01-30 12:49:08 +053095 return merged_gl_map
96
deepeshgarg0073d11ac02019-05-19 00:02:01 +053097def check_if_in_list(gle, gl_map, dimensions=None):
98 account_head_fieldnames = ['party_type', 'party', 'against_voucher', 'against_voucher_type',
99 'cost_center', 'project']
100
101 if dimensions:
102 account_head_fieldnames = account_head_fieldnames + dimensions
103
Nabin Haitbb777562013-08-29 18:19:37 +0530104 for e in gl_map:
deepeshgarg0073d11ac02019-05-19 00:02:01 +0530105 same_head = True
106 if e.account != gle.account:
107 same_head = False
108
109 for fieldname in account_head_fieldnames:
110 if cstr(e.get(fieldname)) != cstr(gle.get(fieldname)):
111 same_head = False
112
113 if same_head:
114 return e
Nabin Haitbf495c92013-01-30 12:49:08 +0530115
Nabin Hait9784d272016-12-30 16:21:35 +0530116def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False):
117 if not from_repost:
Rohit Waghchaurea5f40942017-06-16 15:21:36 +0530118 validate_account_for_perpetual_inventory(gl_map)
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530119 validate_cwip_accounts(gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530120
Nabin Haite2c200a2015-05-28 13:00:37 +0530121 round_off_debit_credit(gl_map)
Anand Doshi602e8252015-11-16 19:05:46 +0530122
Nabin Haitbf495c92013-01-30 12:49:08 +0530123 for entry in gl_map:
Nabin Hait9784d272016-12-30 16:21:35 +0530124 make_entry(entry, adv_adj, update_outstanding, from_repost)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530125
Nabin Hait27994c22013-08-26 16:53:30 +0530126 # check against budget
Nabin Hait9784d272016-12-30 16:21:35 +0530127 if not from_repost:
128 validate_expense_against_budget(entry)
Anand Doshi652bc072014-04-16 15:21:46 +0530129
Nabin Hait9784d272016-12-30 16:21:35 +0530130def make_entry(args, adv_adj, update_outstanding, from_repost=False):
Nabin Haitaeba24e2013-08-23 15:17:36 +0530131 args.update({"doctype": "GL Entry"})
Rushabh Mehtaa504f062014-04-04 12:16:26 +0530132 gle = frappe.get_doc(args)
Anand Doshi6dfd4302015-02-10 14:41:27 +0530133 gle.flags.ignore_permissions = 1
Nabin Hait9784d272016-12-30 16:21:35 +0530134 gle.flags.from_repost = from_repost
Nabin Haitaeba24e2013-08-23 15:17:36 +0530135 gle.insert()
Nabin Hait9784d272016-12-30 16:21:35 +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
Rohit Waghchaurea5f40942017-06-16 15:21:36 +0530139def validate_account_for_perpetual_inventory(gl_map):
Rohit Waghchauree9ff1912017-06-19 12:54:59 +0530140 if cint(erpnext.is_perpetual_inventory_enabled(gl_map[0].company)) \
Nabin Haite7f479b2015-06-22 07:31:49 +0530141 and gl_map[0].voucher_type=="Journal Entry":
142 aii_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount
Nabin Hait6d7b0ce2017-06-15 11:09:27 +0530143 where account_type = 'Stock' and is_group=0""")]
Anand Doshi652bc072014-04-16 15:21:46 +0530144
Nabin Haite7f479b2015-06-22 07:31:49 +0530145 for entry in gl_map:
146 if entry.account in aii_accounts:
147 frappe.throw(_("Account: {0} can only be updated via Stock Transactions")
148 .format(entry.account), StockAccountInvalidTransaction)
Anand Doshi652bc072014-04-16 15:21:46 +0530149
Anurag Mishra7e1987e2019-08-05 10:18:57 +0530150def validate_cwip_accounts(gl_map):
151 if not cint(frappe.db.get_value("Asset Settings", None, "disable_cwip_accounting")) \
152 and gl_map[0].voucher_type == "Journal Entry":
153 cwip_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount
154 where account_type = 'Capital Work in Progress' and is_group=0""")]
155
156 for entry in gl_map:
157 if entry.account in cwip_accounts:
158 frappe.throw(_("Account: <b>{0}</b> is capital Work in progress and can not be updated by Journal Entry").format(entry.account))
159
Nabin Haite2c200a2015-05-28 13:00:37 +0530160def round_off_debit_credit(gl_map):
161 precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"),
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530162 currency=frappe.get_cached_value('Company', gl_map[0].company, "default_currency"))
Anand Doshi602e8252015-11-16 19:05:46 +0530163
Nabin Haite2c200a2015-05-28 13:00:37 +0530164 debit_credit_diff = 0.0
165 for entry in gl_map:
166 entry.debit = flt(entry.debit, precision)
167 entry.credit = flt(entry.credit, precision)
168 debit_credit_diff += entry.debit - entry.credit
Anand Doshi602e8252015-11-16 19:05:46 +0530169
Nabin Haite2c200a2015-05-28 13:00:37 +0530170 debit_credit_diff = flt(debit_credit_diff, precision)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530171
Nabin Haitd2a966e2017-05-05 10:41:16 +0530172 if gl_map[0]["voucher_type"] in ("Journal Entry", "Payment Entry"):
Nabin Haitfb24a272016-02-18 19:18:07 +0530173 allowance = 5.0 / (10**precision)
174 else:
Nabin Haitd2a966e2017-05-05 10:41:16 +0530175 allowance = .5
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530176
Nabin Haitfb24a272016-02-18 19:18:07 +0530177 if abs(debit_credit_diff) >= allowance:
Nabin Haite2c200a2015-05-28 13:00:37 +0530178 frappe.throw(_("Debit and Credit not equal for {0} #{1}. Difference is {2}.")
179 .format(gl_map[0].voucher_type, gl_map[0].voucher_no, debit_credit_diff))
Anand Doshi602e8252015-11-16 19:05:46 +0530180
Nabin Haite2c200a2015-05-28 13:00:37 +0530181 elif abs(debit_credit_diff) >= (1.0 / (10**precision)):
Nabin Hait34c551d2019-07-03 10:34:31 +0530182 make_round_off_gle(gl_map, debit_credit_diff, precision)
Anand Doshi602e8252015-11-16 19:05:46 +0530183
Nabin Hait34c551d2019-07-03 10:34:31 +0530184def make_round_off_gle(gl_map, debit_credit_diff, precision):
Nabin Hait2e4de832017-09-19 14:53:16 +0530185 round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(gl_map[0].company)
Zarrar3523b772018-08-14 16:28:14 +0530186 round_off_account_exists = False
Nabin Hait80069a62015-05-28 19:19:59 +0530187 round_off_gle = frappe._dict()
Zarrar3523b772018-08-14 16:28:14 +0530188 for d in gl_map:
189 if d.account == round_off_account:
190 round_off_gle = d
191 if d.debit_in_account_currency:
192 debit_credit_diff -= flt(d.debit_in_account_currency)
193 else:
194 debit_credit_diff += flt(d.credit_in_account_currency)
195 round_off_account_exists = True
196
Nabin Hait34c551d2019-07-03 10:34:31 +0530197 if round_off_account_exists and abs(debit_credit_diff) <= (1.0 / (10**precision)):
198 gl_map.remove(round_off_gle)
199 return
200
Zarrar3523b772018-08-14 16:28:14 +0530201 if not round_off_gle:
202 for k in ["voucher_type", "voucher_no", "company",
203 "posting_date", "remarks", "is_opening"]:
204 round_off_gle[k] = gl_map[0][k]
Anand Doshi602e8252015-11-16 19:05:46 +0530205
Nabin Haite2c200a2015-05-28 13:00:37 +0530206 round_off_gle.update({
207 "account": round_off_account,
Nabin Haitd4507ac2016-01-20 16:14:27 +0530208 "debit_in_account_currency": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
209 "credit_in_account_currency": debit_credit_diff if debit_credit_diff > 0 else 0,
Nabin Haite2c200a2015-05-28 13:00:37 +0530210 "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
211 "credit": debit_credit_diff if debit_credit_diff > 0 else 0,
Nabin Hait3c671462015-05-28 13:19:01 +0530212 "cost_center": round_off_cost_center,
213 "party_type": None,
214 "party": None,
215 "against_voucher_type": None,
216 "against_voucher": None
Nabin Haite2c200a2015-05-28 13:00:37 +0530217 })
Anand Doshi602e8252015-11-16 19:05:46 +0530218
Zarrar3523b772018-08-14 16:28:14 +0530219 if not round_off_account_exists:
220 gl_map.append(round_off_gle)
Nabin Haite2c200a2015-05-28 13:00:37 +0530221
Nabin Hait2e4de832017-09-19 14:53:16 +0530222def get_round_off_account_and_cost_center(company):
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530223 round_off_account, round_off_cost_center = frappe.get_cached_value('Company', company,
Nabin Hait2e4de832017-09-19 14:53:16 +0530224 ["round_off_account", "round_off_cost_center"]) or [None, None]
225 if not round_off_account:
226 frappe.throw(_("Please mention Round Off Account in Company"))
227
228 if not round_off_cost_center:
229 frappe.throw(_("Please mention Round Off Cost Center in Company"))
230
231 return round_off_account, round_off_cost_center
232
Anand Doshi652bc072014-04-16 15:21:46 +0530233def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,
Nabin Hait2e296fa2013-08-28 18:53:11 +0530234 adv_adj=False, update_outstanding="Yes"):
Anand Doshi652bc072014-04-16 15:21:46 +0530235
Nabin Hait53822ae2014-03-03 19:18:17 +0530236 from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \
Nabin Hait11f41952013-09-24 14:36:55 +0530237 check_freezing_date, update_outstanding_amt, validate_frozen_account
Anand Doshi652bc072014-04-16 15:21:46 +0530238
Nabin Hait2e296fa2013-08-28 18:53:11 +0530239 if not gl_entries:
Nabin Hait56548cb2016-07-01 15:58:39 +0530240 gl_entries = frappe.db.sql("""
Rohit Waghchaureb1391ca2016-10-03 12:25:04 +0530241 select account, posting_date, party_type, party, cost_center, fiscal_year,voucher_type,
242 voucher_no, against_voucher_type, against_voucher, cost_center, company
Nabin Hait56548cb2016-07-01 15:58:39 +0530243 from `tabGL Entry`
Nabin Hait2e296fa2013-08-28 18:53:11 +0530244 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True)
Nabin Hait56548cb2016-07-01 15:58:39 +0530245
Nabin Hait27994c22013-08-26 16:53:30 +0530246 if gl_entries:
247 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Anand Doshi652bc072014-04-16 15:21:46 +0530248
249 frappe.db.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""",
Nabin Hait2e296fa2013-08-28 18:53:11 +0530250 (voucher_type or gl_entries[0]["voucher_type"], voucher_no or gl_entries[0]["voucher_no"]))
Anand Doshi652bc072014-04-16 15:21:46 +0530251
Nabin Hait9b09c952013-08-21 17:47:11 +0530252 for entry in gl_entries:
Nabin Hait11f41952013-09-24 14:36:55 +0530253 validate_frozen_account(entry["account"], adv_adj)
Nabin Hait53822ae2014-03-03 19:18:17 +0530254 validate_balance_type(entry["account"], adv_adj)
Nabin Hait1e1b2362018-01-29 16:45:43 +0530255 if not adv_adj:
256 validate_expense_against_budget(entry)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530257
Nabin Hait1e1b2362018-01-29 16:45:43 +0530258 if entry.get("against_voucher") and update_outstanding == 'Yes' and not adv_adj:
Nabin Hait32251022014-08-29 11:18:32 +0530259 update_outstanding_amt(entry["account"], entry.get("party_type"), entry.get("party"), entry.get("against_voucher_type"),
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530260 entry.get("against_voucher"), on_cancel=True)