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 | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 12 | if gl_map: |
| 13 | if not cancel: |
| 14 | gl_map = process_gl_map(gl_map, merge_entries) |
| 15 | save_entries(gl_map, adv_adj, update_outstanding) |
| 16 | else: |
| 17 | delete_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 18 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 19 | def process_gl_map(gl_map, merge_entries=True): |
| 20 | if merge_entries: |
| 21 | gl_map = merge_similar_entries(gl_map) |
| 22 | |
| 23 | for entry in gl_map: |
| 24 | # round off upto 2 decimal |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 25 | entry.debit = flt(entry.debit, 2) |
| 26 | entry.credit = flt(entry.credit, 2) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 27 | |
| 28 | # toggle debit, credit if negative entry |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 29 | if flt(entry.debit) < 0 or flt(entry.credit) < 0: |
| 30 | entry.debit, entry.credit = abs(flt(entry.credit)), abs(flt(entry.debit)) |
| 31 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 32 | return gl_map |
| 33 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 34 | def merge_similar_entries(gl_map): |
| 35 | merged_gl_map = [] |
| 36 | for entry in gl_map: |
| 37 | # if there is already an entry in this account then just add it |
| 38 | # to that entry |
| 39 | same_head = check_if_in_list(entry, merged_gl_map) |
| 40 | if same_head: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 41 | same_head.debit = flt(same_head.debit) + flt(entry.debit) |
| 42 | same_head.credit = flt(same_head.credit) + flt(entry.credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 43 | else: |
| 44 | merged_gl_map.append(entry) |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 45 | |
| 46 | # filter zero debit and credit entries |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 47 | 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] | 48 | return merged_gl_map |
| 49 | |
| 50 | def check_if_in_list(gle, gl_mqp): |
| 51 | for e in gl_mqp: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 52 | if e.account == gle.account and \ |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 53 | cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \ |
| 54 | and cstr(e.get('against_voucher_type')) == \ |
| 55 | cstr(gle.get('against_voucher_type')) \ |
| 56 | and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')): |
| 57 | return e |
| 58 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 59 | def save_entries(gl_map, adv_adj, update_outstanding): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 60 | total_debit = total_credit = 0.0 |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 61 | for entry in gl_map: |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 62 | make_entry(entry, adv_adj, update_outstanding) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 63 | # check against budget |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 64 | validate_expense_against_budget(entry) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 65 | |
| 66 | # update total debit / credit |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 67 | total_debit += flt(entry.debit) |
| 68 | total_credit += flt(entry.credit) |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 69 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 70 | validate_total_debit_credit(total_debit, total_credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 71 | |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 72 | def make_entry(args, adv_adj, update_outstanding): |
| 73 | args.update({"doctype": "GL Entry"}) |
| 74 | gle = webnotes.bean([args]) |
| 75 | gle.ignore_permissions = 1 |
| 76 | gle.insert() |
| 77 | gle.run_method("on_update_with_args", adv_adj, update_outstanding) |
| 78 | gle.submit() |
| 79 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 80 | def validate_total_debit_credit(total_debit, total_credit): |
| 81 | if abs(total_debit - total_credit) > 0.005: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 82 | 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] | 83 | cstr(total_debit - total_credit)) |
| 84 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 85 | def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None, |
| 86 | adv_adj=False, update_outstanding="Yes"): |
| 87 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 88 | from accounts.doctype.gl_entry.gl_entry import check_negative_balance, \ |
| 89 | check_freezing_date, update_outstanding_amt, validate_freezed_account |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 90 | |
| 91 | if not gl_entries: |
| 92 | gl_entries = webnotes.conn.sql("""select * from `tabGL Entry` |
| 93 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 94 | if gl_entries: |
| 95 | check_freezing_date(gl_entries[0]["posting_date"], adv_adj) |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 96 | |
| 97 | webnotes.conn.sql("""delete from `tabGL Entry` where voucher_type=%s and voucher_no=%s""", |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame^] | 98 | (voucher_type or gl_entries[0]["voucher_type"], voucher_no or gl_entries[0]["voucher_no"])) |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 99 | |
| 100 | for entry in gl_entries: |
| 101 | validate_freezed_account(entry["account"], adv_adj) |
| 102 | check_negative_balance(entry["account"], adv_adj) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 103 | validate_expense_against_budget(entry) |
| 104 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 105 | if entry.get("against_voucher") and entry.get("against_voucher_type") != "POS" \ |
| 106 | and update_outstanding == 'Yes': |
| 107 | update_outstanding_amt(entry["account"], entry.get("against_voucher_type"), |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 108 | entry.get("against_voucher"), on_cancel=True) |