Rushabh Mehta | e67d1fb | 2013-08-05 14:59:54 +0530 | [diff] [blame] | 1 | # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. |
| 2 | # License: GNU General Public License v3. See license.txt |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 3 | |
| 4 | from __future__ import unicode_literals |
| 5 | import webnotes |
Anand Doshi | b96fef9 | 2013-03-07 15:35:36 +0530 | [diff] [blame] | 6 | from webnotes.utils import flt, cstr, now |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 7 | from webnotes.model.doc import Document |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 8 | from accounts.utils import validate_expense_against_budget |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 9 | |
| 10 | def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True, |
| 11 | update_outstanding='Yes'): |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 12 | if not cancel: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 13 | gl_map = process_gl_map(gl_map, merge_entries) |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 14 | save_entries(gl_map, adv_adj, update_outstanding) |
| 15 | else: |
| 16 | delete_gl_entries(gl_map, adv_adj, update_outstanding) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 17 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 18 | def 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 Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 32 | def 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 Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 43 | |
| 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 Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 46 | return merged_gl_map |
| 47 | |
| 48 | def 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 Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 57 | def save_entries(gl_map, adv_adj, update_outstanding): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 58 | total_debit = total_credit = 0.0 |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 59 | for entry in gl_map: |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 60 | make_entry(entry, adv_adj, update_outstanding) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 61 | # check against budget |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 62 | validate_expense_against_budget(entry) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 63 | |
| 64 | # update total debit / credit |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 65 | total_debit += flt(entry["debit"]) |
| 66 | total_credit += flt(entry["credit"]) |
| 67 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 68 | validate_total_debit_credit(total_debit, total_credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 69 | |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 70 | def 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 Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 78 | def validate_total_debit_credit(total_debit, total_credit): |
| 79 | if abs(total_debit - total_credit) > 0.005: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 80 | webnotes.throw(webnotes._("Debit and Credit not equal for this voucher: Diff (Debit) is ") + |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 81 | cstr(total_debit - total_credit)) |
| 82 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 83 | def delete_gl_entries(gl_entries=None, adv_adj=False, update_outstanding="Yes"): |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 84 | from accounts.doctype.gl_entry.gl_entry import check_negative_balance, \ |
| 85 | check_freezing_date, update_outstanding_amt, validate_freezed_account |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 86 | if gl_entries: |
| 87 | check_freezing_date(gl_entries[0]["posting_date"], adv_adj) |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 88 | |
| 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 Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 95 | validate_expense_against_budget(entry) |
| 96 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 97 | 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 Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 100 | entry.get("against_voucher"), on_cancel=True) |