blob: f7932bf60cd9e5d998ad2146744483eb9280f68c [file] [log] [blame]
Nabin Haitbf495c92013-01-30 12:49:08 +05301# ERPNext - web based ERP (http://erpnext.com)
2# Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17from __future__ import unicode_literals
18import webnotes
19from webnotes.utils import flt, cstr
20from webnotes.model.doc import Document
21
22def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True,
23 update_outstanding='Yes'):
24 if merge_entries:
25 gl_map = merge_similar_entries(gl_map)
26
27 check_budget(gl_map, cancel)
28 save_entries(gl_map, cancel, adv_adj, update_outstanding)
29
30 if cancel:
31 set_as_cancel(gl_map[0]["voucher_type"], gl_map[0]["voucher_no"])
32
33def merge_similar_entries(gl_map):
34 merged_gl_map = []
35 for entry in gl_map:
36 # if there is already an entry in this account then just add it
37 # to that entry
38 same_head = check_if_in_list(entry, merged_gl_map)
39 if same_head:
40 same_head['debit'] = flt(same_head['debit']) + flt(entry['debit'])
41 same_head['credit'] = flt(same_head['credit']) + flt(entry['credit'])
42 else:
43 merged_gl_map.append(entry)
44
45 return merged_gl_map
46
47def check_if_in_list(gle, gl_mqp):
48 for e in gl_mqp:
49 if e['account'] == gle['account'] and \
50 cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \
51 and cstr(e.get('against_voucher_type')) == \
52 cstr(gle.get('against_voucher_type')) \
53 and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')):
54 return e
55
56def check_budget(gl_map, cancel):
57 for gle in gl_map:
58 if gle.get('cost_center'):
59 #check budget only if account is expense account
60 acc_details = webnotes.conn.get_value("Account", gle['account'],
61 ['is_pl_account', 'debit_or_credit'])
62 if acc_details[0]=="Yes" and acc_details[1]=="Debit":
63 webnotes.get_obj('Budget Control').check_budget(gle, cancel)
64
65def save_entries(gl_map, cancel, adv_adj, update_outstanding):
66 total_debit = total_credit = 0.0
67 def _swap(gle):
68 gle.debit, gle.credit = abs(flt(gle.credit)), abs(flt(gle.debit))
69
70 for entry in gl_map:
71 gle = Document('GL Entry', fielddata=entry)
72
73 # toggle debit, credit if negative entry
74 if flt(gle.debit) < 0 or flt(gle.credit) < 0:
75 _swap(gle)
76
77 # toggled debit/credit in two separate condition because
78 # both should be executed at the
79 # time of cancellation when there is negative amount (tax discount)
80 if cancel:
81 _swap(gle)
82
83 gle_obj = webnotes.get_obj(doc=gle)
84 # validate except on_cancel
85 if not cancel:
86 gle_obj.validate()
87
88 # save
89 gle.save(1)
90 gle_obj.on_update(adv_adj, cancel, update_outstanding)
91
92 # update total debit / credit
93 total_debit += flt(gle.debit)
94 total_credit += flt(gle.credit)
95
96 # print gle.account, gle.debit, gle.credit, total_debit, total_credit
97
98 if not cancel:
99 validate_total_debit_credit(total_debit, total_credit)
100
101def validate_total_debit_credit(total_debit, total_credit):
102 if abs(total_debit - total_credit) > 0.005:
103 webnotes.msgprint("""Debit and Credit not equal for
104 this voucher: Diff (Debit) is %s""" %
105 (total_debit - total_credit), raise_exception=1)
106
107def set_as_cancel(voucher_type, voucher_no):
108 webnotes.conn.sql("""update `tabGL Entry` set is_cancelled='Yes'
109 where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))