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