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 | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 8 | from webnotes import msgprint, _ |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 9 | from accounts.utils import validate_expense_against_budget |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 10 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 11 | |
| 12 | class StockAccountInvalidTransaction(webnotes.ValidationError): pass |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 13 | |
| 14 | def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True, |
| 15 | update_outstanding='Yes'): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 16 | if gl_map: |
| 17 | if not cancel: |
| 18 | gl_map = process_gl_map(gl_map, merge_entries) |
| 19 | save_entries(gl_map, adv_adj, update_outstanding) |
| 20 | else: |
| 21 | 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] | 22 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 23 | def process_gl_map(gl_map, merge_entries=True): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 24 | if merge_entries: |
| 25 | gl_map = merge_similar_entries(gl_map) |
| 26 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 27 | for entry in gl_map: |
| 28 | # round off upto 2 decimal |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 29 | entry.debit = flt(entry.debit, 2) |
| 30 | entry.credit = flt(entry.credit, 2) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 31 | |
| 32 | # toggle debit, credit if negative entry |
Nabin Hait | 88f3cd5 | 2013-10-23 16:29:19 +0530 | [diff] [blame] | 33 | if flt(entry.debit) < 0: |
| 34 | entry.credit = flt(entry.credit) - flt(entry.debit) |
| 35 | entry.debit = 0.0 |
| 36 | if flt(entry.credit) < 0: |
| 37 | entry.debit = flt(entry.debit) - flt(entry.credit) |
| 38 | entry.credit = 0.0 |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 39 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 40 | return gl_map |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 41 | |
| 42 | def merge_similar_entries(gl_map): |
| 43 | merged_gl_map = [] |
| 44 | for entry in gl_map: |
| 45 | # if there is already an entry in this account then just add it |
| 46 | # to that entry |
| 47 | same_head = check_if_in_list(entry, merged_gl_map) |
| 48 | if same_head: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 49 | same_head.debit = flt(same_head.debit) + flt(entry.debit) |
| 50 | same_head.credit = flt(same_head.credit) + flt(entry.credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 51 | else: |
| 52 | merged_gl_map.append(entry) |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 53 | |
| 54 | # filter zero debit and credit entries |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 55 | 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] | 56 | return merged_gl_map |
| 57 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 58 | def check_if_in_list(gle, gl_map): |
| 59 | for e in gl_map: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 60 | if e.account == gle.account and \ |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 61 | cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \ |
| 62 | and cstr(e.get('against_voucher_type')) == \ |
| 63 | cstr(gle.get('against_voucher_type')) \ |
| 64 | and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')): |
| 65 | return e |
| 66 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 67 | def save_entries(gl_map, adv_adj, update_outstanding): |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 68 | validate_account_for_auto_accounting_for_stock(gl_map) |
| 69 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 70 | total_debit = total_credit = 0.0 |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 71 | for entry in gl_map: |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 72 | make_entry(entry, adv_adj, update_outstanding) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 73 | # check against budget |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 74 | validate_expense_against_budget(entry) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 75 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 76 | |
| 77 | # update total debit / credit |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 78 | total_debit += flt(entry.debit) |
| 79 | total_credit += flt(entry.credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 80 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 81 | validate_total_debit_credit(total_debit, total_credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 82 | |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 83 | def make_entry(args, adv_adj, update_outstanding): |
| 84 | args.update({"doctype": "GL Entry"}) |
| 85 | gle = webnotes.bean([args]) |
| 86 | gle.ignore_permissions = 1 |
| 87 | gle.insert() |
| 88 | gle.run_method("on_update_with_args", adv_adj, update_outstanding) |
| 89 | gle.submit() |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 90 | |
| 91 | def validate_total_debit_credit(total_debit, total_credit): |
| 92 | if abs(total_debit - total_credit) > 0.005: |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 93 | webnotes.throw(_("Debit and Credit not equal for this voucher: Diff (Debit) is ") + |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 94 | cstr(total_debit - total_credit)) |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 95 | |
| 96 | def validate_account_for_auto_accounting_for_stock(gl_map): |
| 97 | if gl_map[0].voucher_type=="Journal Voucher": |
Nabin Hait | 142007a | 2013-09-17 15:15:16 +0530 | [diff] [blame] | 98 | aii_accounts = [d[0] for d in webnotes.conn.sql("""select name from tabAccount |
| 99 | where account_type = 'Warehouse' and ifnull(master_name, '')!=''""")] |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 100 | |
| 101 | for entry in gl_map: |
| 102 | if entry.account in aii_accounts: |
| 103 | webnotes.throw(_("Account") + ": " + entry.account + |
| 104 | _(" can only be debited/credited through Stock transactions"), |
| 105 | StockAccountInvalidTransaction) |
| 106 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 107 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 108 | def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None, |
| 109 | adv_adj=False, update_outstanding="Yes"): |
| 110 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 111 | from accounts.doctype.gl_entry.gl_entry import check_negative_balance, \ |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 112 | check_freezing_date, update_outstanding_amt, validate_frozen_account |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 113 | |
| 114 | if not gl_entries: |
| 115 | gl_entries = webnotes.conn.sql("""select * from `tabGL Entry` |
| 116 | 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] | 117 | if gl_entries: |
| 118 | check_freezing_date(gl_entries[0]["posting_date"], adv_adj) |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 119 | |
| 120 | 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] | 121 | (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] | 122 | |
| 123 | for entry in gl_entries: |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 124 | validate_frozen_account(entry["account"], adv_adj) |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 125 | check_negative_balance(entry["account"], adv_adj) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 126 | validate_expense_against_budget(entry) |
| 127 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 128 | if entry.get("against_voucher") and entry.get("against_voucher_type") != "POS" \ |
| 129 | and update_outstanding == 'Yes': |
| 130 | update_outstanding_amt(entry["account"], entry.get("against_voucher_type"), |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 131 | entry.get("against_voucher"), on_cancel=True) |