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