blob: 1a8965e2c66fa529df440810295e608f3eaed0a0 [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
Nabin Haitbf495c92013-01-30 12:49:08 +053010
Nabin Haitbb777562013-08-29 18:19:37 +053011
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053012class StockAccountInvalidTransaction(frappe.ValidationError): pass
Nabin Haitbf495c92013-01-30 12:49:08 +053013
Nabin Hait9784d272016-12-30 16:21:35 +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:
17 gl_map = process_gl_map(gl_map, merge_entries)
nabinhaitc3432922014-07-29 18:06:18 +053018 if gl_map and len(gl_map) > 1:
Nabin Hait9784d272016-12-30 16:21:35 +053019 save_entries(gl_map, adv_adj, update_outstanding, from_repost)
nabinhaitc3432922014-07-29 18:06:18 +053020 else:
nabinhait4859b482014-07-30 14:28:24 +053021 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 +053022 else:
23 delete_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding)
Anand Doshi652bc072014-04-16 15:21:46 +053024
Nabin Hait27994c22013-08-26 16:53:30 +053025def process_gl_map(gl_map, merge_entries=True):
Nabin Haitbf495c92013-01-30 12:49:08 +053026 if merge_entries:
27 gl_map = merge_similar_entries(gl_map)
Nabin Hait27994c22013-08-26 16:53:30 +053028 for entry in gl_map:
Anand Doshi602e8252015-11-16 19:05:46 +053029 # toggle debit, credit if negative entry
Nabin Hait88f3cd52013-10-23 16:29:19 +053030 if flt(entry.debit) < 0:
31 entry.credit = flt(entry.credit) - flt(entry.debit)
32 entry.debit = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053033
Nabin Haitc561a492015-08-19 19:22:34 +053034 if flt(entry.debit_in_account_currency) < 0:
35 entry.credit_in_account_currency = \
36 flt(entry.credit_in_account_currency) - flt(entry.debit_in_account_currency)
37 entry.debit_in_account_currency = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053038
Nabin Hait88f3cd52013-10-23 16:29:19 +053039 if flt(entry.credit) < 0:
40 entry.debit = flt(entry.debit) - flt(entry.credit)
41 entry.credit = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053042
Nabin Haitc561a492015-08-19 19:22:34 +053043 if flt(entry.credit_in_account_currency) < 0:
44 entry.debit_in_account_currency = \
45 flt(entry.debit_in_account_currency) - flt(entry.credit_in_account_currency)
46 entry.credit_in_account_currency = 0.0
Anand Doshi602e8252015-11-16 19:05:46 +053047
Nabin Hait27994c22013-08-26 16:53:30 +053048 return gl_map
Anand Doshi652bc072014-04-16 15:21:46 +053049
Nabin Haitbf495c92013-01-30 12:49:08 +053050def merge_similar_entries(gl_map):
51 merged_gl_map = []
52 for entry in gl_map:
Anand Doshi652bc072014-04-16 15:21:46 +053053 # if there is already an entry in this account then just add it
Nabin Haitbf495c92013-01-30 12:49:08 +053054 # to that entry
55 same_head = check_if_in_list(entry, merged_gl_map)
56 if same_head:
Nabin Hait2e296fa2013-08-28 18:53:11 +053057 same_head.debit = flt(same_head.debit) + flt(entry.debit)
Nabin Haitc561a492015-08-19 19:22:34 +053058 same_head.debit_in_account_currency = \
59 flt(same_head.debit_in_account_currency) + flt(entry.debit_in_account_currency)
Nabin Hait2e296fa2013-08-28 18:53:11 +053060 same_head.credit = flt(same_head.credit) + flt(entry.credit)
Nabin Haitc561a492015-08-19 19:22:34 +053061 same_head.credit_in_account_currency = \
62 flt(same_head.credit_in_account_currency) + flt(entry.credit_in_account_currency)
Nabin Haitbf495c92013-01-30 12:49:08 +053063 else:
64 merged_gl_map.append(entry)
Anand Doshi652bc072014-04-16 15:21:46 +053065
Nabin Hait815a49e2013-08-07 17:00:01 +053066 # filter zero debit and credit entries
Nabin Hait2e54da22015-08-13 12:19:20 +053067 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 +053068 merged_gl_map = list(merged_gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +053069
Nabin Haitbf495c92013-01-30 12:49:08 +053070 return merged_gl_map
71
Nabin Haitbb777562013-08-29 18:19:37 +053072def check_if_in_list(gle, gl_map):
73 for e in gl_map:
Nabin Hait11190262015-06-01 16:21:25 +053074 if e.account == gle.account \
75 and cstr(e.get('party_type'))==cstr(gle.get('party_type')) \
76 and cstr(e.get('party'))==cstr(gle.get('party')) \
77 and cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \
78 and cstr(e.get('against_voucher_type')) == cstr(gle.get('against_voucher_type')) \
Nabin Hait591a5ab2016-05-26 17:41:39 +053079 and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')) \
80 and cstr(e.get('project')) == cstr(gle.get('project')):
Nabin Hait11190262015-06-01 16:21:25 +053081 return e
Nabin Haitbf495c92013-01-30 12:49:08 +053082
Nabin Hait9784d272016-12-30 16:21:35 +053083def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False):
84 if not from_repost:
Rohit Waghchaurea5f40942017-06-16 15:21:36 +053085 validate_account_for_perpetual_inventory(gl_map)
Rushabh Mehta708e47a2018-08-08 16:37:31 +053086
Nabin Haite2c200a2015-05-28 13:00:37 +053087 round_off_debit_credit(gl_map)
Anand Doshi602e8252015-11-16 19:05:46 +053088
Nabin Haitbf495c92013-01-30 12:49:08 +053089 for entry in gl_map:
Nabin Hait9784d272016-12-30 16:21:35 +053090 make_entry(entry, adv_adj, update_outstanding, from_repost)
Rushabh Mehta708e47a2018-08-08 16:37:31 +053091
Nabin Hait27994c22013-08-26 16:53:30 +053092 # check against budget
Nabin Hait9784d272016-12-30 16:21:35 +053093 if not from_repost:
94 validate_expense_against_budget(entry)
Anand Doshi652bc072014-04-16 15:21:46 +053095
Nabin Hait9784d272016-12-30 16:21:35 +053096def make_entry(args, adv_adj, update_outstanding, from_repost=False):
Nabin Haitaeba24e2013-08-23 15:17:36 +053097 args.update({"doctype": "GL Entry"})
Rushabh Mehtaa504f062014-04-04 12:16:26 +053098 gle = frappe.get_doc(args)
Anand Doshi6dfd4302015-02-10 14:41:27 +053099 gle.flags.ignore_permissions = 1
Nabin Hait9784d272016-12-30 16:21:35 +0530100 gle.flags.from_repost = from_repost
Nabin Haitaeba24e2013-08-23 15:17:36 +0530101 gle.insert()
Nabin Hait9784d272016-12-30 16:21:35 +0530102 gle.run_method("on_update_with_args", adv_adj, update_outstanding, from_repost)
Nabin Haitaeba24e2013-08-23 15:17:36 +0530103 gle.submit()
Anand Doshi652bc072014-04-16 15:21:46 +0530104
Rohit Waghchaurea5f40942017-06-16 15:21:36 +0530105def validate_account_for_perpetual_inventory(gl_map):
Rohit Waghchauree9ff1912017-06-19 12:54:59 +0530106 if cint(erpnext.is_perpetual_inventory_enabled(gl_map[0].company)) \
Nabin Haite7f479b2015-06-22 07:31:49 +0530107 and gl_map[0].voucher_type=="Journal Entry":
108 aii_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount
Nabin Hait6d7b0ce2017-06-15 11:09:27 +0530109 where account_type = 'Stock' and is_group=0""")]
Anand Doshi652bc072014-04-16 15:21:46 +0530110
Nabin Haite7f479b2015-06-22 07:31:49 +0530111 for entry in gl_map:
112 if entry.account in aii_accounts:
113 frappe.throw(_("Account: {0} can only be updated via Stock Transactions")
114 .format(entry.account), StockAccountInvalidTransaction)
Anand Doshi652bc072014-04-16 15:21:46 +0530115
Nabin Haite2c200a2015-05-28 13:00:37 +0530116def round_off_debit_credit(gl_map):
117 precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"),
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530118 currency=frappe.get_cached_value('Company', gl_map[0].company, "default_currency"))
Anand Doshi602e8252015-11-16 19:05:46 +0530119
Nabin Haite2c200a2015-05-28 13:00:37 +0530120 debit_credit_diff = 0.0
121 for entry in gl_map:
122 entry.debit = flt(entry.debit, precision)
123 entry.credit = flt(entry.credit, precision)
124 debit_credit_diff += entry.debit - entry.credit
Anand Doshi602e8252015-11-16 19:05:46 +0530125
Nabin Haite2c200a2015-05-28 13:00:37 +0530126 debit_credit_diff = flt(debit_credit_diff, precision)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530127
Nabin Haitd2a966e2017-05-05 10:41:16 +0530128 if gl_map[0]["voucher_type"] in ("Journal Entry", "Payment Entry"):
Nabin Haitfb24a272016-02-18 19:18:07 +0530129 allowance = 5.0 / (10**precision)
130 else:
Nabin Haitd2a966e2017-05-05 10:41:16 +0530131 allowance = .5
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530132
Nabin Haitfb24a272016-02-18 19:18:07 +0530133 if abs(debit_credit_diff) >= allowance:
Nabin Haite2c200a2015-05-28 13:00:37 +0530134 frappe.throw(_("Debit and Credit not equal for {0} #{1}. Difference is {2}.")
135 .format(gl_map[0].voucher_type, gl_map[0].voucher_no, debit_credit_diff))
Anand Doshi602e8252015-11-16 19:05:46 +0530136
Nabin Haite2c200a2015-05-28 13:00:37 +0530137 elif abs(debit_credit_diff) >= (1.0 / (10**precision)):
138 make_round_off_gle(gl_map, debit_credit_diff)
Anand Doshi602e8252015-11-16 19:05:46 +0530139
Nabin Haite2c200a2015-05-28 13:00:37 +0530140def make_round_off_gle(gl_map, debit_credit_diff):
Nabin Hait2e4de832017-09-19 14:53:16 +0530141 round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(gl_map[0].company)
Anand Doshi602e8252015-11-16 19:05:46 +0530142
Nabin Hait80069a62015-05-28 19:19:59 +0530143 round_off_gle = frappe._dict()
Anand Doshi602e8252015-11-16 19:05:46 +0530144 for k in ["voucher_type", "voucher_no", "company",
Valmik Janglabc609702016-03-03 16:37:35 +0530145 "posting_date", "remarks", "is_opening"]:
Nabin Hait80069a62015-05-28 19:19:59 +0530146 round_off_gle[k] = gl_map[0][k]
Anand Doshi602e8252015-11-16 19:05:46 +0530147
Nabin Haite2c200a2015-05-28 13:00:37 +0530148 round_off_gle.update({
149 "account": round_off_account,
Nabin Haitd4507ac2016-01-20 16:14:27 +0530150 "debit_in_account_currency": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
151 "credit_in_account_currency": debit_credit_diff if debit_credit_diff > 0 else 0,
Nabin Haite2c200a2015-05-28 13:00:37 +0530152 "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0,
153 "credit": debit_credit_diff if debit_credit_diff > 0 else 0,
Nabin Hait3c671462015-05-28 13:19:01 +0530154 "cost_center": round_off_cost_center,
155 "party_type": None,
156 "party": None,
157 "against_voucher_type": None,
158 "against_voucher": None
Nabin Haite2c200a2015-05-28 13:00:37 +0530159 })
Anand Doshi602e8252015-11-16 19:05:46 +0530160
Nabin Haite2c200a2015-05-28 13:00:37 +0530161 gl_map.append(round_off_gle)
162
Nabin Hait2e4de832017-09-19 14:53:16 +0530163def get_round_off_account_and_cost_center(company):
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530164 round_off_account, round_off_cost_center = frappe.get_cached_value('Company', company,
Nabin Hait2e4de832017-09-19 14:53:16 +0530165 ["round_off_account", "round_off_cost_center"]) or [None, None]
166 if not round_off_account:
167 frappe.throw(_("Please mention Round Off Account in Company"))
168
169 if not round_off_cost_center:
170 frappe.throw(_("Please mention Round Off Cost Center in Company"))
171
172 return round_off_account, round_off_cost_center
173
Anand Doshi652bc072014-04-16 15:21:46 +0530174def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,
Nabin Hait2e296fa2013-08-28 18:53:11 +0530175 adv_adj=False, update_outstanding="Yes"):
Anand Doshi652bc072014-04-16 15:21:46 +0530176
Nabin Hait53822ae2014-03-03 19:18:17 +0530177 from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \
Nabin Hait11f41952013-09-24 14:36:55 +0530178 check_freezing_date, update_outstanding_amt, validate_frozen_account
Anand Doshi652bc072014-04-16 15:21:46 +0530179
Nabin Hait2e296fa2013-08-28 18:53:11 +0530180 if not gl_entries:
Nabin Hait56548cb2016-07-01 15:58:39 +0530181 gl_entries = frappe.db.sql("""
Rohit Waghchaureb1391ca2016-10-03 12:25:04 +0530182 select account, posting_date, party_type, party, cost_center, fiscal_year,voucher_type,
183 voucher_no, against_voucher_type, against_voucher, cost_center, company
Nabin Hait56548cb2016-07-01 15:58:39 +0530184 from `tabGL Entry`
Nabin Hait2e296fa2013-08-28 18:53:11 +0530185 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True)
Nabin Hait56548cb2016-07-01 15:58:39 +0530186
Nabin Hait27994c22013-08-26 16:53:30 +0530187 if gl_entries:
188 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Anand Doshi652bc072014-04-16 15:21:46 +0530189
190 frappe.db.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""",
Nabin Hait2e296fa2013-08-28 18:53:11 +0530191 (voucher_type or gl_entries[0]["voucher_type"], voucher_no or gl_entries[0]["voucher_no"]))
Anand Doshi652bc072014-04-16 15:21:46 +0530192
Nabin Hait9b09c952013-08-21 17:47:11 +0530193 for entry in gl_entries:
Nabin Hait11f41952013-09-24 14:36:55 +0530194 validate_frozen_account(entry["account"], adv_adj)
Nabin Hait53822ae2014-03-03 19:18:17 +0530195 validate_balance_type(entry["account"], adv_adj)
Nabin Hait1e1b2362018-01-29 16:45:43 +0530196 if not adv_adj:
197 validate_expense_against_budget(entry)
Rushabh Mehta708e47a2018-08-08 16:37:31 +0530198
Nabin Hait1e1b2362018-01-29 16:45:43 +0530199 if entry.get("against_voucher") and update_outstanding == 'Yes' and not adv_adj:
Nabin Hait32251022014-08-29 11:18:32 +0530200 update_outstanding_amt(entry["account"], entry.get("party_type"), entry.get("party"), entry.get("against_voucher_type"),
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530201 entry.get("against_voucher"), on_cancel=True)