blob: 8e0f4082bc98187382e4f1c4ace5ef82ae16ea55 [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
Anand Doshib96fef92013-03-07 15:35:36 +053019from webnotes.utils import flt, cstr, now
Nabin Haitbf495c92013-01-30 12:49:08 +053020from 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
Nabin Haitbf495c92013-01-30 12:49:08 +053027 if cancel:
28 set_as_cancel(gl_map[0]["voucher_type"], gl_map[0]["voucher_no"])
Anand Doshie74a1f22013-02-14 13:38:12 +053029
30 check_budget(gl_map, cancel)
31 save_entries(gl_map, cancel, adv_adj, update_outstanding)
Nabin Haitbf495c92013-01-30 12:49:08 +053032
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
Nabin Hait362455e2013-02-13 15:50:39 +053073 # round off upto 2 decimal
74 gle.debit = flt(gle.debit, 2)
75 gle.credit = flt(gle.credit, 2)
76
Nabin Haitbf495c92013-01-30 12:49:08 +053077 # toggle debit, credit if negative entry
78 if flt(gle.debit) < 0 or flt(gle.credit) < 0:
79 _swap(gle)
80
81 # toggled debit/credit in two separate condition because
82 # both should be executed at the
83 # time of cancellation when there is negative amount (tax discount)
84 if cancel:
85 _swap(gle)
86
87 gle_obj = webnotes.get_obj(doc=gle)
88 # validate except on_cancel
89 if not cancel:
90 gle_obj.validate()
91
92 # save
93 gle.save(1)
94 gle_obj.on_update(adv_adj, cancel, update_outstanding)
95
96 # update total debit / credit
97 total_debit += flt(gle.debit)
98 total_credit += flt(gle.credit)
99
100 # print gle.account, gle.debit, gle.credit, total_debit, total_credit
101
102 if not cancel:
103 validate_total_debit_credit(total_debit, total_credit)
104
105def validate_total_debit_credit(total_debit, total_credit):
106 if abs(total_debit - total_credit) > 0.005:
107 webnotes.msgprint("""Debit and Credit not equal for
108 this voucher: Diff (Debit) is %s""" %
109 (total_debit - total_credit), raise_exception=1)
110
111def set_as_cancel(voucher_type, voucher_no):
Anand Doshib96fef92013-03-07 15:35:36 +0530112 webnotes.conn.sql("""update `tabGL Entry` set is_cancelled='Yes',
113 modified=%s, modified_by=%s
114 where voucher_type=%s and voucher_no=%s""",
115 (now(), webnotes.session.user, voucher_type, voucher_no))