blob: 87814b128f1c6cccd03919358f1cb9392ef6dcd9 [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
5import webnotes
Nabin Haite83069c2013-11-14 18:40:08 +05306from webnotes.utils import flt, cstr
7from webnotes import _
Nabin Hait2b06aaa2013-08-22 18:25:43 +05308from accounts.utils import validate_expense_against_budget
Nabin Haitbf495c92013-01-30 12:49:08 +05309
Nabin Haitbb777562013-08-29 18:19:37 +053010
11class StockAccountInvalidTransaction(webnotes.ValidationError): pass
Nabin Haitbf495c92013-01-30 12:49:08 +053012
13def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True,
14 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)
Nabin Haitbf495c92013-01-30 12:49:08 +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)
25
Nabin Hait27994c22013-08-26 16:53:30 +053026 for entry in gl_map:
27 # round off upto 2 decimal
Nabin Hait2e296fa2013-08-28 18:53:11 +053028 entry.debit = flt(entry.debit, 2)
29 entry.credit = flt(entry.credit, 2)
Nabin Hait27994c22013-08-26 16:53:30 +053030
31 # toggle debit, credit if negative entry
Nabin Hait88f3cd52013-10-23 16:29:19 +053032 if flt(entry.debit) < 0:
33 entry.credit = flt(entry.credit) - flt(entry.debit)
34 entry.debit = 0.0
35 if flt(entry.credit) < 0:
36 entry.debit = flt(entry.debit) - flt(entry.credit)
37 entry.credit = 0.0
Nabin Haitbf495c92013-01-30 12:49:08 +053038
Nabin Hait27994c22013-08-26 16:53:30 +053039 return gl_map
Nabin Haitbf495c92013-01-30 12:49:08 +053040
41def merge_similar_entries(gl_map):
42 merged_gl_map = []
43 for entry in gl_map:
44 # if there is already an entry in this account then just add it
45 # to that entry
46 same_head = check_if_in_list(entry, merged_gl_map)
47 if same_head:
Nabin Hait2e296fa2013-08-28 18:53:11 +053048 same_head.debit = flt(same_head.debit) + flt(entry.debit)
49 same_head.credit = flt(same_head.credit) + flt(entry.credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053050 else:
51 merged_gl_map.append(entry)
Nabin Hait815a49e2013-08-07 17:00:01 +053052
53 # filter zero debit and credit entries
Nabin Hait2e296fa2013-08-28 18:53:11 +053054 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 +053055 return merged_gl_map
56
Nabin Haitbb777562013-08-29 18:19:37 +053057def check_if_in_list(gle, gl_map):
58 for e in gl_map:
Nabin Hait2e296fa2013-08-28 18:53:11 +053059 if e.account == gle.account and \
Nabin Haitbf495c92013-01-30 12:49:08 +053060 cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \
61 and cstr(e.get('against_voucher_type')) == \
62 cstr(gle.get('against_voucher_type')) \
63 and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')):
64 return e
65
Nabin Hait9b09c952013-08-21 17:47:11 +053066def save_entries(gl_map, adv_adj, update_outstanding):
Nabin Haitbb777562013-08-29 18:19:37 +053067 validate_account_for_auto_accounting_for_stock(gl_map)
68
Nabin Haitbf495c92013-01-30 12:49:08 +053069 total_debit = total_credit = 0.0
Nabin Haitbf495c92013-01-30 12:49:08 +053070 for entry in gl_map:
Nabin Haitaeba24e2013-08-23 15:17:36 +053071 make_entry(entry, adv_adj, update_outstanding)
Nabin Hait27994c22013-08-26 16:53:30 +053072 # check against budget
Nabin Haita3916062013-08-23 10:46:41 +053073 validate_expense_against_budget(entry)
Nabin Haitbf495c92013-01-30 12:49:08 +053074
Nabin Haitbf495c92013-01-30 12:49:08 +053075
76 # update total debit / credit
Nabin Hait2e296fa2013-08-28 18:53:11 +053077 total_debit += flt(entry.debit)
78 total_credit += flt(entry.credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053079
Nabin Hait9b09c952013-08-21 17:47:11 +053080 validate_total_debit_credit(total_debit, total_credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053081
Nabin Haitaeba24e2013-08-23 15:17:36 +053082def make_entry(args, adv_adj, update_outstanding):
83 args.update({"doctype": "GL Entry"})
84 gle = webnotes.bean([args])
85 gle.ignore_permissions = 1
86 gle.insert()
87 gle.run_method("on_update_with_args", adv_adj, update_outstanding)
88 gle.submit()
Nabin Haitbf495c92013-01-30 12:49:08 +053089
90def validate_total_debit_credit(total_debit, total_credit):
91 if abs(total_debit - total_credit) > 0.005:
Nabin Haitbb777562013-08-29 18:19:37 +053092 webnotes.throw(_("Debit and Credit not equal for this voucher: Diff (Debit) is ") +
Nabin Hait9b09c952013-08-21 17:47:11 +053093 cstr(total_debit - total_credit))
Nabin Haitbb777562013-08-29 18:19:37 +053094
95def validate_account_for_auto_accounting_for_stock(gl_map):
96 if gl_map[0].voucher_type=="Journal Voucher":
Nabin Hait142007a2013-09-17 15:15:16 +053097 aii_accounts = [d[0] for d in webnotes.conn.sql("""select name from tabAccount
98 where account_type = 'Warehouse' and ifnull(master_name, '')!=''""")]
Nabin Haitbb777562013-08-29 18:19:37 +053099
100 for entry in gl_map:
101 if entry.account in aii_accounts:
102 webnotes.throw(_("Account") + ": " + entry.account +
103 _(" can only be debited/credited through Stock transactions"),
104 StockAccountInvalidTransaction)
105
Nabin Hait9b09c952013-08-21 17:47:11 +0530106
Nabin Hait2e296fa2013-08-28 18:53:11 +0530107def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,
108 adv_adj=False, update_outstanding="Yes"):
109
Nabin Hait9b09c952013-08-21 17:47:11 +0530110 from accounts.doctype.gl_entry.gl_entry import check_negative_balance, \
Nabin Hait11f41952013-09-24 14:36:55 +0530111 check_freezing_date, update_outstanding_amt, validate_frozen_account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530112
113 if not gl_entries:
114 gl_entries = webnotes.conn.sql("""select * from `tabGL Entry`
115 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True)
Nabin Hait27994c22013-08-26 16:53:30 +0530116 if gl_entries:
117 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Nabin Hait9b09c952013-08-21 17:47:11 +0530118
119 webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""",
Nabin Hait2e296fa2013-08-28 18:53:11 +0530120 (voucher_type or gl_entries[0]["voucher_type"], voucher_no or gl_entries[0]["voucher_no"]))
Nabin Hait9b09c952013-08-21 17:47:11 +0530121
122 for entry in gl_entries:
Nabin Hait11f41952013-09-24 14:36:55 +0530123 validate_frozen_account(entry["account"], adv_adj)
Nabin Hait9b09c952013-08-21 17:47:11 +0530124 check_negative_balance(entry["account"], adv_adj)
Nabin Hait2b06aaa2013-08-22 18:25:43 +0530125 validate_expense_against_budget(entry)
126
Nabin Hait9b09c952013-08-21 17:47:11 +0530127 if entry.get("against_voucher") and entry.get("against_voucher_type") != "POS" \
128 and update_outstanding == 'Yes':
129 update_outstanding_amt(entry["account"], entry.get("against_voucher_type"),
Nabin Haitaeba24e2013-08-23 15:17:36 +0530130 entry.get("against_voucher"), on_cancel=True)