blob: b0c585a3a38383de808be7ce0fe4c6a9110537c3 [file] [log] [blame]
Rushabh Mehtae67d1fb2013-08-05 14:59:54 +05301# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd.
2# License: GNU General Public License v3. See license.txt
Nabin Haitbf495c92013-01-30 12:49:08 +05303
4from __future__ import unicode_literals
5import webnotes
Anand Doshib96fef92013-03-07 15:35:36 +05306from webnotes.utils import flt, cstr, now
Nabin Haitbf495c92013-01-30 12:49:08 +05307from webnotes.model.doc import Document
Nabin Haitbb777562013-08-29 18:19:37 +05308from webnotes import msgprint, _
Nabin Hait2b06aaa2013-08-22 18:25:43 +05309from accounts.utils import validate_expense_against_budget
Nabin Haitbf495c92013-01-30 12:49:08 +053010
Nabin Haitbb777562013-08-29 18:19:37 +053011
12class StockAccountInvalidTransaction(webnotes.ValidationError): pass
Nabin Haitbf495c92013-01-30 12:49:08 +053013
14def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True,
15 update_outstanding='Yes'):
Nabin Hait2e296fa2013-08-28 18:53:11 +053016 if gl_map:
17 if not cancel:
18 gl_map = process_gl_map(gl_map, merge_entries)
19 save_entries(gl_map, adv_adj, update_outstanding)
20 else:
21 delete_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding)
Nabin Haitbf495c92013-01-30 12:49:08 +053022
Nabin Hait27994c22013-08-26 16:53:30 +053023def process_gl_map(gl_map, merge_entries=True):
Nabin Haitbf495c92013-01-30 12:49:08 +053024 if merge_entries:
25 gl_map = merge_similar_entries(gl_map)
26
Nabin Hait27994c22013-08-26 16:53:30 +053027 for entry in gl_map:
28 # round off upto 2 decimal
Nabin Hait2e296fa2013-08-28 18:53:11 +053029 entry.debit = flt(entry.debit, 2)
30 entry.credit = flt(entry.credit, 2)
Nabin Hait27994c22013-08-26 16:53:30 +053031
32 # toggle debit, credit if negative entry
Nabin Hait88f3cd52013-10-23 16:29:19 +053033 if flt(entry.debit) < 0:
34 entry.credit = flt(entry.credit) - flt(entry.debit)
35 entry.debit = 0.0
36 if flt(entry.credit) < 0:
37 entry.debit = flt(entry.debit) - flt(entry.credit)
38 entry.credit = 0.0
Nabin Haitbf495c92013-01-30 12:49:08 +053039
Nabin Hait27994c22013-08-26 16:53:30 +053040 return gl_map
Nabin Haitbf495c92013-01-30 12:49:08 +053041
42def merge_similar_entries(gl_map):
43 merged_gl_map = []
44 for entry in gl_map:
45 # if there is already an entry in this account then just add it
46 # to that entry
47 same_head = check_if_in_list(entry, merged_gl_map)
48 if same_head:
Nabin Hait2e296fa2013-08-28 18:53:11 +053049 same_head.debit = flt(same_head.debit) + flt(entry.debit)
50 same_head.credit = flt(same_head.credit) + flt(entry.credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053051 else:
52 merged_gl_map.append(entry)
Nabin Hait815a49e2013-08-07 17:00:01 +053053
54 # filter zero debit and credit entries
Nabin Hait2e296fa2013-08-28 18:53:11 +053055 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 +053056 return merged_gl_map
57
Nabin Haitbb777562013-08-29 18:19:37 +053058def check_if_in_list(gle, gl_map):
59 for e in gl_map:
Nabin Hait2e296fa2013-08-28 18:53:11 +053060 if e.account == gle.account and \
Nabin Haitbf495c92013-01-30 12:49:08 +053061 cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \
62 and cstr(e.get('against_voucher_type')) == \
63 cstr(gle.get('against_voucher_type')) \
64 and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')):
65 return e
66
Nabin Hait9b09c952013-08-21 17:47:11 +053067def save_entries(gl_map, adv_adj, update_outstanding):
Nabin Haitbb777562013-08-29 18:19:37 +053068 validate_account_for_auto_accounting_for_stock(gl_map)
69
Nabin Haitbf495c92013-01-30 12:49:08 +053070 total_debit = total_credit = 0.0
Nabin Haitbf495c92013-01-30 12:49:08 +053071 for entry in gl_map:
Nabin Haitaeba24e2013-08-23 15:17:36 +053072 make_entry(entry, adv_adj, update_outstanding)
Nabin Hait27994c22013-08-26 16:53:30 +053073 # check against budget
Nabin Haita3916062013-08-23 10:46:41 +053074 validate_expense_against_budget(entry)
Nabin Haitbf495c92013-01-30 12:49:08 +053075
Nabin Haitbf495c92013-01-30 12:49:08 +053076
77 # update total debit / credit
Nabin Hait2e296fa2013-08-28 18:53:11 +053078 total_debit += flt(entry.debit)
79 total_credit += flt(entry.credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053080
Nabin Hait9b09c952013-08-21 17:47:11 +053081 validate_total_debit_credit(total_debit, total_credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053082
Nabin Haitaeba24e2013-08-23 15:17:36 +053083def make_entry(args, adv_adj, update_outstanding):
84 args.update({"doctype": "GL Entry"})
85 gle = webnotes.bean([args])
86 gle.ignore_permissions = 1
87 gle.insert()
88 gle.run_method("on_update_with_args", adv_adj, update_outstanding)
89 gle.submit()
Nabin Haitbf495c92013-01-30 12:49:08 +053090
91def validate_total_debit_credit(total_debit, total_credit):
92 if abs(total_debit - total_credit) > 0.005:
Nabin Haitbb777562013-08-29 18:19:37 +053093 webnotes.throw(_("Debit and Credit not equal for this voucher: Diff (Debit) is ") +
Nabin Hait9b09c952013-08-21 17:47:11 +053094 cstr(total_debit - total_credit))
Nabin Haitbb777562013-08-29 18:19:37 +053095
96def validate_account_for_auto_accounting_for_stock(gl_map):
97 if gl_map[0].voucher_type=="Journal Voucher":
Nabin Hait142007a2013-09-17 15:15:16 +053098 aii_accounts = [d[0] for d in webnotes.conn.sql("""select name from tabAccount
99 where account_type = 'Warehouse' and ifnull(master_name, '')!=''""")]
Nabin Haitbb777562013-08-29 18:19:37 +0530100
101 for entry in gl_map:
102 if entry.account in aii_accounts:
103 webnotes.throw(_("Account") + ": " + entry.account +
104 _(" can only be debited/credited through Stock transactions"),
105 StockAccountInvalidTransaction)
106
Nabin Hait9b09c952013-08-21 17:47:11 +0530107
Nabin Hait2e296fa2013-08-28 18:53:11 +0530108def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None,
109 adv_adj=False, update_outstanding="Yes"):
110
Nabin Hait9b09c952013-08-21 17:47:11 +0530111 from accounts.doctype.gl_entry.gl_entry import check_negative_balance, \
Nabin Hait11f41952013-09-24 14:36:55 +0530112 check_freezing_date, update_outstanding_amt, validate_frozen_account
Nabin Hait2e296fa2013-08-28 18:53:11 +0530113
114 if not gl_entries:
115 gl_entries = webnotes.conn.sql("""select * from `tabGL Entry`
116 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True)
Nabin Hait27994c22013-08-26 16:53:30 +0530117 if gl_entries:
118 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Nabin Hait9b09c952013-08-21 17:47:11 +0530119
120 webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""",
Nabin Hait2e296fa2013-08-28 18:53:11 +0530121 (voucher_type or gl_entries[0]["voucher_type"], voucher_no or gl_entries[0]["voucher_no"]))
Nabin Hait9b09c952013-08-21 17:47:11 +0530122
123 for entry in gl_entries:
Nabin Hait11f41952013-09-24 14:36:55 +0530124 validate_frozen_account(entry["account"], adv_adj)
Nabin Hait9b09c952013-08-21 17:47:11 +0530125 check_negative_balance(entry["account"], adv_adj)
Nabin Hait2b06aaa2013-08-22 18:25:43 +0530126 validate_expense_against_budget(entry)
127
Nabin Hait9b09c952013-08-21 17:47:11 +0530128 if entry.get("against_voucher") and entry.get("against_voucher_type") != "POS" \
129 and update_outstanding == 'Yes':
130 update_outstanding_amt(entry["account"], entry.get("against_voucher_type"),
Nabin Haitaeba24e2013-08-23 15:17:36 +0530131 entry.get("against_voucher"), on_cancel=True)