blob: 3fbbe39e2cafb6e30be32fadc8c81179f4860c39 [file] [log] [blame]
Rushabh Mehtaad45e312013-11-20 12:59:58 +05301# Copyright (c) 2013, Web Notes 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
Rushabh Mehta793ba6b2014-02-14 15:47:51 +05305import frappe
6from frappe.utils import flt, cstr
7from frappe import _
Rushabh Mehta1f847992013-12-12 19:12:19 +05308from erpnext.accounts.utils import validate_expense_against_budget
Nabin Haitbf495c92013-01-30 12:49:08 +05309
Nabin Haitbb777562013-08-29 18:19:37 +053010
Rushabh Mehta793ba6b2014-02-14 15:47:51 +053011class StockAccountInvalidTransaction(frappe.ValidationError): pass
Nabin Haitbf495c92013-01-30 12:49:08 +053012
Anand Doshi652bc072014-04-16 15:21:46 +053013def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True,
Nabin Haitbf495c92013-01-30 12:49:08 +053014 update_outstanding='Yes'):
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)
18 save_entries(gl_map, adv_adj, update_outstanding)
19 else:
20 delete_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding)
Anand Doshi652bc072014-04-16 15:21:46 +053021
Nabin Hait27994c22013-08-26 16:53:30 +053022def process_gl_map(gl_map, merge_entries=True):
Nabin Haitbf495c92013-01-30 12:49:08 +053023 if merge_entries:
24 gl_map = merge_similar_entries(gl_map)
Anand Doshi652bc072014-04-16 15:21:46 +053025
Nabin Hait27994c22013-08-26 16:53:30 +053026 for entry in gl_map:
Nabin Hait27994c22013-08-26 16:53:30 +053027 # toggle debit, credit if negative entry
Nabin Hait88f3cd52013-10-23 16:29:19 +053028 if flt(entry.debit) < 0:
29 entry.credit = flt(entry.credit) - flt(entry.debit)
30 entry.debit = 0.0
31 if flt(entry.credit) < 0:
32 entry.debit = flt(entry.debit) - flt(entry.credit)
33 entry.credit = 0.0
Anand Doshie74a1f22013-02-14 13:38:12 +053034
Nabin Hait27994c22013-08-26 16:53:30 +053035 return gl_map
Anand Doshi652bc072014-04-16 15:21:46 +053036
Nabin Haitbf495c92013-01-30 12:49:08 +053037def merge_similar_entries(gl_map):
38 merged_gl_map = []
39 for entry in gl_map:
Anand Doshi652bc072014-04-16 15:21:46 +053040 # if there is already an entry in this account then just add it
Nabin Haitbf495c92013-01-30 12:49:08 +053041 # to that entry
42 same_head = check_if_in_list(entry, merged_gl_map)
43 if same_head:
Nabin Hait2e296fa2013-08-28 18:53:11 +053044 same_head.debit = flt(same_head.debit) + flt(entry.debit)
45 same_head.credit = flt(same_head.credit) + flt(entry.credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053046 else:
47 merged_gl_map.append(entry)
Anand Doshi652bc072014-04-16 15:21:46 +053048
Nabin Hait815a49e2013-08-07 17:00:01 +053049 # filter zero debit and credit entries
Nabin Hait2e296fa2013-08-28 18:53:11 +053050 merged_gl_map = filter(lambda x: flt(x.debit)!=0 or flt(x.credit)!=0, merged_gl_map)
Nabin Haitbf495c92013-01-30 12:49:08 +053051 return merged_gl_map
52
Nabin Haitbb777562013-08-29 18:19:37 +053053def check_if_in_list(gle, gl_map):
54 for e in gl_map:
Nabin Hait2e296fa2013-08-28 18:53:11 +053055 if e.account == gle.account and \
Nabin Haitbf495c92013-01-30 12:49:08 +053056 cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \
57 and cstr(e.get('against_voucher_type')) == \
58 cstr(gle.get('against_voucher_type')) \
59 and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')):
60 return e
61
Nabin Hait9b09c952013-08-21 17:47:11 +053062def save_entries(gl_map, adv_adj, update_outstanding):
Nabin Haitbb777562013-08-29 18:19:37 +053063 validate_account_for_auto_accounting_for_stock(gl_map)
Anand Doshi652bc072014-04-16 15:21:46 +053064
Nabin Haitbf495c92013-01-30 12:49:08 +053065 total_debit = total_credit = 0.0
Nabin Haitbf495c92013-01-30 12:49:08 +053066 for entry in gl_map:
Nabin Haitaeba24e2013-08-23 15:17:36 +053067 make_entry(entry, adv_adj, update_outstanding)
Nabin Hait27994c22013-08-26 16:53:30 +053068 # check against budget
Nabin Haita3916062013-08-23 10:46:41 +053069 validate_expense_against_budget(entry)
Anand Doshi652bc072014-04-16 15:21:46 +053070
Nabin Haitbf495c92013-01-30 12:49:08 +053071
72 # update total debit / credit
Nabin Hait2e296fa2013-08-28 18:53:11 +053073 total_debit += flt(entry.debit)
74 total_credit += flt(entry.credit)
Anand Doshi652bc072014-04-16 15:21:46 +053075
Nabin Hait9b09c952013-08-21 17:47:11 +053076 validate_total_debit_credit(total_debit, total_credit)
Anand Doshi652bc072014-04-16 15:21:46 +053077
Nabin Haitaeba24e2013-08-23 15:17:36 +053078def make_entry(args, adv_adj, update_outstanding):
79 args.update({"doctype": "GL Entry"})
Rushabh Mehtaa504f062014-04-04 12:16:26 +053080 gle = frappe.get_doc(args)
Nabin Haitaeba24e2013-08-23 15:17:36 +053081 gle.ignore_permissions = 1
82 gle.insert()
83 gle.run_method("on_update_with_args", adv_adj, update_outstanding)
84 gle.submit()
Anand Doshi652bc072014-04-16 15:21:46 +053085
Nabin Haitbf495c92013-01-30 12:49:08 +053086def validate_total_debit_credit(total_debit, total_credit):
87 if abs(total_debit - total_credit) > 0.005:
Rushabh Mehtac38fc712014-04-16 17:21:25 +053088 frappe.throw(_("Debit and Credit not equal for this voucher. Difference is {0}.").format(total_debit - total_credit))
Anand Doshi652bc072014-04-16 15:21:46 +053089
Nabin Haitbb777562013-08-29 18:19:37 +053090def validate_account_for_auto_accounting_for_stock(gl_map):
91 if gl_map[0].voucher_type=="Journal Voucher":
Anand Doshi652bc072014-04-16 15:21:46 +053092 aii_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount
Nabin Hait142007a2013-09-17 15:15:16 +053093 where account_type = 'Warehouse' and ifnull(master_name, '')!=''""")]
Anand Doshi652bc072014-04-16 15:21:46 +053094
Nabin Haitbb777562013-08-29 18:19:37 +053095 for entry in gl_map:
96 if entry.account in aii_accounts:
Nabin Haitc49b7f82014-05-20 15:07:18 +053097 frappe.throw(_("Account: {0} can only be updated via \
98 Stock Transactions").format(entry.account), StockAccountInvalidTransaction)
Anand Doshi652bc072014-04-16 15:21:46 +053099
100
101def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,
Nabin Hait2e296fa2013-08-28 18:53:11 +0530102 adv_adj=False, update_outstanding="Yes"):
Anand Doshi652bc072014-04-16 15:21:46 +0530103
Nabin Hait53822ae2014-03-03 19:18:17 +0530104 from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \
Nabin Hait11f41952013-09-24 14:36:55 +0530105 check_freezing_date, update_outstanding_amt, validate_frozen_account
Anand Doshi652bc072014-04-16 15:21:46 +0530106
Nabin Hait2e296fa2013-08-28 18:53:11 +0530107 if not gl_entries:
Anand Doshi652bc072014-04-16 15:21:46 +0530108 gl_entries = frappe.db.sql("""select * from `tabGL Entry`
Nabin Hait2e296fa2013-08-28 18:53:11 +0530109 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True)
Nabin Hait27994c22013-08-26 16:53:30 +0530110 if gl_entries:
111 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Anand Doshi652bc072014-04-16 15:21:46 +0530112
113 frappe.db.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""",
Nabin Hait2e296fa2013-08-28 18:53:11 +0530114 (voucher_type or gl_entries[0]["voucher_type"], voucher_no or gl_entries[0]["voucher_no"]))
Anand Doshi652bc072014-04-16 15:21:46 +0530115
Nabin Hait9b09c952013-08-21 17:47:11 +0530116 for entry in gl_entries:
Nabin Hait11f41952013-09-24 14:36:55 +0530117 validate_frozen_account(entry["account"], adv_adj)
Nabin Hait53822ae2014-03-03 19:18:17 +0530118 validate_balance_type(entry["account"], adv_adj)
Nabin Hait2b06aaa2013-08-22 18:25:43 +0530119 validate_expense_against_budget(entry)
Anand Doshi652bc072014-04-16 15:21:46 +0530120
121 if entry.get("against_voucher") and update_outstanding == 'Yes':
Rushabh Mehtac38fc712014-04-16 17:21:25 +0530122 update_outstanding_amt(entry["account"], entry.get("against_voucher_type"),
123 entry.get("against_voucher"), on_cancel=True)