Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 1 | # 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 | |
| 17 | from __future__ import unicode_literals |
| 18 | import webnotes |
Anand Doshi | b96fef9 | 2013-03-07 15:35:36 +0530 | [diff] [blame] | 19 | from webnotes.utils import flt, cstr, now |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 20 | from webnotes.model.doc import Document |
| 21 | |
| 22 | def 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 Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 27 | if cancel: |
| 28 | set_as_cancel(gl_map[0]["voucher_type"], gl_map[0]["voucher_no"]) |
Anand Doshi | e74a1f2 | 2013-02-14 13:38:12 +0530 | [diff] [blame] | 29 | |
| 30 | check_budget(gl_map, cancel) |
| 31 | save_entries(gl_map, cancel, adv_adj, update_outstanding) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 32 | |
| 33 | def 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 | |
| 47 | def 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 | |
| 56 | def 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 | |
| 65 | def 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 Hait | 362455e | 2013-02-13 15:50:39 +0530 | [diff] [blame] | 73 | # round off upto 2 decimal |
| 74 | gle.debit = flt(gle.debit, 2) |
| 75 | gle.credit = flt(gle.credit, 2) |
| 76 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 77 | # 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 | |
| 105 | def 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 | |
| 111 | def set_as_cancel(voucher_type, voucher_no): |
Anand Doshi | b96fef9 | 2013-03-07 15:35:36 +0530 | [diff] [blame] | 112 | 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)) |