blob: 2dcbe57b08a87c698f19bf077bce8d8dc338545b [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 Hait2b06aaa2013-08-22 18:25:43 +05308from accounts.utils import validate_expense_against_budget
Nabin Haitbf495c92013-01-30 12:49:08 +05309
10def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True,
11 update_outstanding='Yes'):
Nabin Hait9b09c952013-08-21 17:47:11 +053012 if not cancel:
Nabin Hait27994c22013-08-26 16:53:30 +053013 gl_map = process_gl_map(gl_map, merge_entries)
Nabin Hait9b09c952013-08-21 17:47:11 +053014 save_entries(gl_map, adv_adj, update_outstanding)
15 else:
16 delete_gl_entries(gl_map, adv_adj, update_outstanding)
Nabin Haitbf495c92013-01-30 12:49:08 +053017
Nabin Hait27994c22013-08-26 16:53:30 +053018def process_gl_map(gl_map, merge_entries=True):
19 if merge_entries:
20 gl_map = merge_similar_entries(gl_map)
21
22 for entry in gl_map:
23 # round off upto 2 decimal
24 entry["debit"] = flt(entry["debit"], 2)
25 entry["credit"] = flt(entry["credit"], 2)
26
27 # toggle debit, credit if negative entry
28 if flt(entry["debit"]) < 0 or flt(entry["credit"]) < 0:
29 entry["debit"], entry["credit"] = abs(flt(entry["credit"])), abs(flt(entry["debit"]))
30 return gl_map
31
Nabin Haitbf495c92013-01-30 12:49:08 +053032def merge_similar_entries(gl_map):
33 merged_gl_map = []
34 for entry in gl_map:
35 # if there is already an entry in this account then just add it
36 # to that entry
37 same_head = check_if_in_list(entry, merged_gl_map)
38 if same_head:
39 same_head['debit'] = flt(same_head['debit']) + flt(entry['debit'])
40 same_head['credit'] = flt(same_head['credit']) + flt(entry['credit'])
41 else:
42 merged_gl_map.append(entry)
Nabin Hait815a49e2013-08-07 17:00:01 +053043
44 # filter zero debit and credit entries
45 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 +053046 return merged_gl_map
47
48def check_if_in_list(gle, gl_mqp):
49 for e in gl_mqp:
50 if e['account'] == gle['account'] and \
51 cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \
52 and cstr(e.get('against_voucher_type')) == \
53 cstr(gle.get('against_voucher_type')) \
54 and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')):
55 return e
56
Nabin Hait9b09c952013-08-21 17:47:11 +053057def save_entries(gl_map, adv_adj, update_outstanding):
Nabin Haitbf495c92013-01-30 12:49:08 +053058 total_debit = total_credit = 0.0
Nabin Haitbf495c92013-01-30 12:49:08 +053059 for entry in gl_map:
Nabin Haitaeba24e2013-08-23 15:17:36 +053060 make_entry(entry, adv_adj, update_outstanding)
Nabin Hait27994c22013-08-26 16:53:30 +053061 # check against budget
Nabin Haita3916062013-08-23 10:46:41 +053062 validate_expense_against_budget(entry)
Nabin Haitbf495c92013-01-30 12:49:08 +053063
64 # update total debit / credit
Nabin Haitaeba24e2013-08-23 15:17:36 +053065 total_debit += flt(entry["debit"])
66 total_credit += flt(entry["credit"])
67
Nabin Hait9b09c952013-08-21 17:47:11 +053068 validate_total_debit_credit(total_debit, total_credit)
Nabin Haitbf495c92013-01-30 12:49:08 +053069
Nabin Haitaeba24e2013-08-23 15:17:36 +053070def make_entry(args, adv_adj, update_outstanding):
71 args.update({"doctype": "GL Entry"})
72 gle = webnotes.bean([args])
73 gle.ignore_permissions = 1
74 gle.insert()
75 gle.run_method("on_update_with_args", adv_adj, update_outstanding)
76 gle.submit()
77
Nabin Haitbf495c92013-01-30 12:49:08 +053078def validate_total_debit_credit(total_debit, total_credit):
79 if abs(total_debit - total_credit) > 0.005:
Nabin Hait27994c22013-08-26 16:53:30 +053080 webnotes.throw(webnotes._("Debit and Credit not equal for this voucher: Diff (Debit) is ") +
Nabin Hait9b09c952013-08-21 17:47:11 +053081 cstr(total_debit - total_credit))
82
Nabin Hait27994c22013-08-26 16:53:30 +053083def delete_gl_entries(gl_entries=None, adv_adj=False, update_outstanding="Yes"):
Nabin Hait9b09c952013-08-21 17:47:11 +053084 from accounts.doctype.gl_entry.gl_entry import check_negative_balance, \
85 check_freezing_date, update_outstanding_amt, validate_freezed_account
Nabin Hait27994c22013-08-26 16:53:30 +053086 if gl_entries:
87 check_freezing_date(gl_entries[0]["posting_date"], adv_adj)
Nabin Hait9b09c952013-08-21 17:47:11 +053088
89 webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""",
90 (gl_entries[0]["voucher_type"], gl_entries[0]["voucher_no"]))
91
92 for entry in gl_entries:
93 validate_freezed_account(entry["account"], adv_adj)
94 check_negative_balance(entry["account"], adv_adj)
Nabin Hait2b06aaa2013-08-22 18:25:43 +053095 validate_expense_against_budget(entry)
96
Nabin Hait9b09c952013-08-21 17:47:11 +053097 if entry.get("against_voucher") and entry.get("against_voucher_type") != "POS" \
98 and update_outstanding == 'Yes':
99 update_outstanding_amt(entry["account"], entry.get("against_voucher_type"),
Nabin Haitaeba24e2013-08-23 15:17:36 +0530100 entry.get("against_voucher"), on_cancel=True)