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 |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 5 | import frappe |
| 6 | from frappe.utils import flt, cstr |
| 7 | from frappe import _ |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 8 | from erpnext.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 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 11 | class StockAccountInvalidTransaction(frappe.ValidationError): pass |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 12 | |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 13 | def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True, |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 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) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 18 | if gl_map and len(gl_map) > 1: |
nabinhait | 0ad6770 | 2014-07-29 17:47:02 +0530 | [diff] [blame] | 19 | save_entries(gl_map, adv_adj, update_outstanding) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 20 | else: |
nabinhait | 4859b48 | 2014-07-30 14:28:24 +0530 | [diff] [blame] | 21 | frappe.throw(_("Incorrect number of General Ledger Entries found. You might have selected a wrong Account in the transaction.")) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 22 | else: |
| 23 | delete_gl_entries(gl_map, adv_adj=adv_adj, update_outstanding=update_outstanding) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 24 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 25 | def process_gl_map(gl_map, merge_entries=True): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 26 | if merge_entries: |
| 27 | gl_map = merge_similar_entries(gl_map) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 28 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 29 | for entry in gl_map: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 30 | # toggle debit, credit if negative entry |
Nabin Hait | 88f3cd5 | 2013-10-23 16:29:19 +0530 | [diff] [blame] | 31 | if flt(entry.debit) < 0: |
| 32 | entry.credit = flt(entry.credit) - flt(entry.debit) |
| 33 | entry.debit = 0.0 |
| 34 | if flt(entry.credit) < 0: |
| 35 | entry.debit = flt(entry.debit) - flt(entry.credit) |
| 36 | entry.credit = 0.0 |
Anand Doshi | e74a1f2 | 2013-02-14 13:38:12 +0530 | [diff] [blame] | 37 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 38 | return gl_map |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 39 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 40 | def merge_similar_entries(gl_map): |
| 41 | merged_gl_map = [] |
| 42 | for entry in gl_map: |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 43 | # if there is already an entry in this account then just add it |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 44 | # to that entry |
| 45 | same_head = check_if_in_list(entry, merged_gl_map) |
| 46 | if same_head: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 47 | same_head.debit = flt(same_head.debit) + flt(entry.debit) |
| 48 | same_head.credit = flt(same_head.credit) + flt(entry.credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 49 | else: |
| 50 | merged_gl_map.append(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 51 | |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 52 | # filter zero debit and credit entries |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 53 | 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] | 54 | return merged_gl_map |
| 55 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 56 | def check_if_in_list(gle, gl_map): |
| 57 | for e in gl_map: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 58 | if e.account == gle.account and \ |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 59 | cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \ |
| 60 | and cstr(e.get('against_voucher_type')) == \ |
| 61 | cstr(gle.get('against_voucher_type')) \ |
| 62 | and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')): |
| 63 | return e |
| 64 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 65 | def save_entries(gl_map, adv_adj, update_outstanding): |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 66 | validate_account_for_auto_accounting_for_stock(gl_map) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 67 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 68 | total_debit = total_credit = 0.0 |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 69 | for entry in gl_map: |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 70 | make_entry(entry, adv_adj, update_outstanding) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 71 | # check against budget |
Nabin Hait | a391606 | 2013-08-23 10:46:41 +0530 | [diff] [blame] | 72 | validate_expense_against_budget(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 73 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 74 | |
| 75 | # update total debit / credit |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 76 | total_debit += flt(entry.debit) |
| 77 | total_credit += flt(entry.credit) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 78 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 79 | validate_total_debit_credit(total_debit, total_credit) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 80 | |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 81 | def make_entry(args, adv_adj, update_outstanding): |
| 82 | args.update({"doctype": "GL Entry"}) |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 83 | gle = frappe.get_doc(args) |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 84 | gle.ignore_permissions = 1 |
| 85 | gle.insert() |
| 86 | gle.run_method("on_update_with_args", adv_adj, update_outstanding) |
| 87 | gle.submit() |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 88 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 89 | def validate_total_debit_credit(total_debit, total_credit): |
| 90 | if abs(total_debit - total_credit) > 0.005: |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 91 | frappe.throw(_("Debit and Credit not equal for this voucher. Difference is {0}.").format(total_debit - total_credit)) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 92 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 93 | def validate_account_for_auto_accounting_for_stock(gl_map): |
| 94 | if gl_map[0].voucher_type=="Journal Voucher": |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 95 | aii_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount |
Nabin Hait | 142007a | 2013-09-17 15:15:16 +0530 | [diff] [blame] | 96 | where account_type = 'Warehouse' and ifnull(master_name, '')!=''""")] |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 97 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 98 | for entry in gl_map: |
| 99 | if entry.account in aii_accounts: |
Nabin Hait | adeb976 | 2014-10-06 11:53:52 +0530 | [diff] [blame] | 100 | frappe.throw(_("Account: {0} can only be updated via Stock Transactions").format(entry.account), StockAccountInvalidTransaction) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 101 | |
| 102 | |
| 103 | def delete_gl_entries(gl_entries=None, voucher_type=None, voucher_no=None, |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 104 | adv_adj=False, update_outstanding="Yes"): |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 105 | |
Nabin Hait | 53822ae | 2014-03-03 19:18:17 +0530 | [diff] [blame] | 106 | from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \ |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 107 | check_freezing_date, update_outstanding_amt, validate_frozen_account |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 108 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 109 | if not gl_entries: |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 110 | gl_entries = frappe.db.sql("""select * from `tabGL Entry` |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 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) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 114 | |
| 115 | frappe.db.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"])) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 117 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 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 | 53822ae | 2014-03-03 19:18:17 +0530 | [diff] [blame] | 120 | validate_balance_type(entry["account"], adv_adj) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 121 | validate_expense_against_budget(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 122 | |
| 123 | if entry.get("against_voucher") and update_outstanding == 'Yes': |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 124 | update_outstanding_amt(entry["account"], entry.get("against_voucher_type"), |
| 125 | entry.get("against_voucher"), on_cancel=True) |