Anand Doshi | 885e074 | 2015-03-03 14:55:30 +0530 | [diff] [blame] | 1 | # Copyright (c) 2015, Frappe 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 _ |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 8 | from frappe.model.meta import get_field_precision |
Rushabh Mehta | 1f84799 | 2013-12-12 19:12:19 +0530 | [diff] [blame] | 9 | from erpnext.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 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 12 | class StockAccountInvalidTransaction(frappe.ValidationError): pass |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 13 | |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 14 | 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] | 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) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 19 | if gl_map and len(gl_map) > 1: |
nabinhait | 0ad6770 | 2014-07-29 17:47:02 +0530 | [diff] [blame] | 20 | save_entries(gl_map, adv_adj, update_outstanding) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 21 | else: |
nabinhait | 4859b48 | 2014-07-30 14:28:24 +0530 | [diff] [blame] | 22 | 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] | 23 | else: |
| 24 | 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] | 25 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 26 | def process_gl_map(gl_map, merge_entries=True): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 27 | if merge_entries: |
| 28 | gl_map = merge_similar_entries(gl_map) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 29 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 30 | for entry in gl_map: |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 31 | # toggle debit, credit if negative entry |
Nabin Hait | 88f3cd5 | 2013-10-23 16:29:19 +0530 | [diff] [blame] | 32 | if flt(entry.debit) < 0: |
| 33 | entry.credit = flt(entry.credit) - flt(entry.debit) |
| 34 | entry.debit = 0.0 |
| 35 | if flt(entry.credit) < 0: |
| 36 | entry.debit = flt(entry.debit) - flt(entry.credit) |
| 37 | entry.credit = 0.0 |
Anand Doshi | e74a1f2 | 2013-02-14 13:38:12 +0530 | [diff] [blame] | 38 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 39 | return gl_map |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 40 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 41 | def merge_similar_entries(gl_map): |
| 42 | merged_gl_map = [] |
| 43 | for entry in gl_map: |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 44 | # 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] | 45 | # to that entry |
| 46 | same_head = check_if_in_list(entry, merged_gl_map) |
| 47 | if same_head: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 48 | same_head.debit = flt(same_head.debit) + flt(entry.debit) |
| 49 | same_head.credit = flt(same_head.credit) + flt(entry.credit) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 50 | else: |
| 51 | merged_gl_map.append(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 52 | |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 53 | # filter zero debit and credit entries |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 54 | 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] | 55 | return merged_gl_map |
| 56 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 57 | def check_if_in_list(gle, gl_map): |
| 58 | for e in gl_map: |
Nabin Hait | 1119026 | 2015-06-01 16:21:25 +0530 | [diff] [blame] | 59 | if e.account == gle.account \ |
| 60 | and cstr(e.get('party_type'))==cstr(gle.get('party_type')) \ |
| 61 | and cstr(e.get('party'))==cstr(gle.get('party')) \ |
| 62 | and cstr(e.get('against_voucher'))==cstr(gle.get('against_voucher')) \ |
| 63 | and cstr(e.get('against_voucher_type')) == cstr(gle.get('against_voucher_type')) \ |
| 64 | and cstr(e.get('cost_center')) == cstr(gle.get('cost_center')): |
| 65 | return e |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 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) |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 69 | round_off_debit_credit(gl_map) |
| 70 | |
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) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 75 | |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 76 | def make_entry(args, adv_adj, update_outstanding): |
| 77 | args.update({"doctype": "GL Entry"}) |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 78 | gle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 79 | gle.flags.ignore_permissions = 1 |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 80 | gle.insert() |
| 81 | gle.run_method("on_update_with_args", adv_adj, update_outstanding) |
| 82 | gle.submit() |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 83 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 84 | def validate_account_for_auto_accounting_for_stock(gl_map): |
Nabin Hait | e7f479b | 2015-06-22 07:31:49 +0530 | [diff] [blame] | 85 | if cint(frappe.db.get_single_value("Accounts Settings", "auto_accounting_for_stock")) \ |
| 86 | and gl_map[0].voucher_type=="Journal Entry": |
| 87 | aii_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount |
| 88 | where account_type = 'Warehouse' and ifnull(warehouse, '')!=''""")] |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 89 | |
Nabin Hait | e7f479b | 2015-06-22 07:31:49 +0530 | [diff] [blame] | 90 | for entry in gl_map: |
| 91 | if entry.account in aii_accounts: |
| 92 | frappe.throw(_("Account: {0} can only be updated via Stock Transactions") |
| 93 | .format(entry.account), StockAccountInvalidTransaction) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 94 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 95 | def round_off_debit_credit(gl_map): |
| 96 | precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"), |
| 97 | currency=frappe.db.get_value("Company", gl_map[0].company, "default_currency", cache=True)) |
| 98 | |
| 99 | debit_credit_diff = 0.0 |
| 100 | for entry in gl_map: |
| 101 | entry.debit = flt(entry.debit, precision) |
| 102 | entry.credit = flt(entry.credit, precision) |
| 103 | debit_credit_diff += entry.debit - entry.credit |
| 104 | |
| 105 | debit_credit_diff = flt(debit_credit_diff, precision) |
Nabin Hait | b63ad44 | 2015-06-08 14:15:02 +0530 | [diff] [blame] | 106 | if abs(debit_credit_diff) >= (5.0 / (10**precision)): |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 107 | frappe.throw(_("Debit and Credit not equal for {0} #{1}. Difference is {2}.") |
| 108 | .format(gl_map[0].voucher_type, gl_map[0].voucher_no, debit_credit_diff)) |
| 109 | |
| 110 | elif abs(debit_credit_diff) >= (1.0 / (10**precision)): |
| 111 | make_round_off_gle(gl_map, debit_credit_diff) |
| 112 | |
| 113 | def make_round_off_gle(gl_map, debit_credit_diff): |
| 114 | round_off_account, round_off_cost_center = frappe.db.get_value("Company", gl_map[0].company, |
| 115 | ["round_off_account", "round_off_cost_center"]) or [None, None] |
| 116 | if not round_off_account: |
| 117 | frappe.throw(_("Please mention Round Off Account in Company")) |
| 118 | |
| 119 | if not round_off_cost_center: |
| 120 | frappe.throw(_("Please mention Round Off Cost Center in Company")) |
| 121 | |
Nabin Hait | 80069a6 | 2015-05-28 19:19:59 +0530 | [diff] [blame] | 122 | |
| 123 | round_off_gle = frappe._dict() |
| 124 | for k in ["voucher_type", "voucher_no", "company", |
| 125 | "posting_date", "remarks", "fiscal_year", "is_opening"]: |
| 126 | round_off_gle[k] = gl_map[0][k] |
| 127 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 128 | round_off_gle.update({ |
| 129 | "account": round_off_account, |
| 130 | "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, |
| 131 | "credit": debit_credit_diff if debit_credit_diff > 0 else 0, |
Nabin Hait | 3c67146 | 2015-05-28 13:19:01 +0530 | [diff] [blame] | 132 | "cost_center": round_off_cost_center, |
| 133 | "party_type": None, |
| 134 | "party": None, |
| 135 | "against_voucher_type": None, |
| 136 | "against_voucher": None |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 137 | }) |
Nabin Hait | 80069a6 | 2015-05-28 19:19:59 +0530 | [diff] [blame] | 138 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 139 | gl_map.append(round_off_gle) |
| 140 | |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 141 | |
| 142 | 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] | 143 | adv_adj=False, update_outstanding="Yes"): |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 144 | |
Nabin Hait | 53822ae | 2014-03-03 19:18:17 +0530 | [diff] [blame] | 145 | from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \ |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 146 | check_freezing_date, update_outstanding_amt, validate_frozen_account |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 147 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 148 | if not gl_entries: |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 149 | gl_entries = frappe.db.sql("""select * from `tabGL Entry` |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 150 | 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] | 151 | if gl_entries: |
| 152 | check_freezing_date(gl_entries[0]["posting_date"], adv_adj) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 153 | |
| 154 | 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] | 155 | (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] | 156 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 157 | for entry in gl_entries: |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 158 | validate_frozen_account(entry["account"], adv_adj) |
Nabin Hait | 53822ae | 2014-03-03 19:18:17 +0530 | [diff] [blame] | 159 | validate_balance_type(entry["account"], adv_adj) |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 160 | validate_expense_against_budget(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 161 | |
| 162 | if entry.get("against_voucher") and update_outstanding == 'Yes': |
Nabin Hait | 3225102 | 2014-08-29 11:18:32 +0530 | [diff] [blame] | 163 | update_outstanding_amt(entry["account"], entry.get("party_type"), entry.get("party"), entry.get("against_voucher_type"), |
Rushabh Mehta | c38fc71 | 2014-04-16 17:21:25 +0530 | [diff] [blame] | 164 | entry.get("against_voucher"), on_cancel=True) |