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 |
Rohit Waghchaure | e9ff191 | 2017-06-19 12:54:59 +0530 | [diff] [blame] | 5 | import frappe, erpnext |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 6 | from frappe.utils import flt, cstr, cint, comma_and |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 7 | from frappe import _ |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 8 | from erpnext.accounts.utils import get_stock_and_account_balance |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 9 | from frappe.model.meta import get_field_precision |
Nabin Hait | b9bc7d6 | 2016-05-16 14:38:47 +0530 | [diff] [blame] | 10 | from erpnext.accounts.doctype.budget.budget import validate_expense_against_budget |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 11 | from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_accounting_dimensions |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 12 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 13 | |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 14 | class ClosedAccountingPeriod(frappe.ValidationError): pass |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 15 | class StockAccountInvalidTransaction(frappe.ValidationError): pass |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 16 | class StockValueAndAccountBalanceOutOfSync(frappe.ValidationError): pass |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 17 | |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 18 | def make_gl_entries(gl_map, cancel=False, adv_adj=False, merge_entries=True, update_outstanding='Yes', from_repost=False): |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 19 | if gl_map: |
| 20 | if not cancel: |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 21 | validate_accounting_period(gl_map) |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 22 | gl_map = process_gl_map(gl_map, merge_entries) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 23 | if gl_map and len(gl_map) > 1: |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 24 | save_entries(gl_map, adv_adj, update_outstanding, from_repost) |
nabinhait | c343292 | 2014-07-29 18:06:18 +0530 | [diff] [blame] | 25 | else: |
nabinhait | 4859b48 | 2014-07-30 14:28:24 +0530 | [diff] [blame] | 26 | 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] | 27 | else: |
| 28 | 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] | 29 | |
Mangesh-Khairnar | e5c733b | 2019-08-08 15:44:11 +0530 | [diff] [blame] | 30 | def validate_accounting_period(gl_map): |
| 31 | accounting_periods = frappe.db.sql(""" SELECT |
| 32 | ap.name as name |
| 33 | FROM |
| 34 | `tabAccounting Period` ap, `tabClosed Document` cd |
| 35 | WHERE |
| 36 | ap.name = cd.parent |
| 37 | AND ap.company = %(company)s |
| 38 | AND cd.closed = 1 |
| 39 | AND cd.document_type = %(voucher_type)s |
| 40 | AND %(date)s between ap.start_date and ap.end_date |
| 41 | """, { |
| 42 | 'date': gl_map[0].posting_date, |
| 43 | 'company': gl_map[0].company, |
| 44 | 'voucher_type': gl_map[0].voucher_type |
| 45 | }, as_dict=1) |
| 46 | |
| 47 | if accounting_periods: |
| 48 | frappe.throw(_("You can't create accounting entries in the closed accounting period {0}") |
| 49 | .format(accounting_periods[0].name), ClosedAccountingPeriod) |
| 50 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 51 | def process_gl_map(gl_map, merge_entries=True): |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 52 | if merge_entries: |
| 53 | gl_map = merge_similar_entries(gl_map) |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 54 | for entry in gl_map: |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 55 | # toggle debit, credit if negative entry |
Nabin Hait | 88f3cd5 | 2013-10-23 16:29:19 +0530 | [diff] [blame] | 56 | if flt(entry.debit) < 0: |
| 57 | entry.credit = flt(entry.credit) - flt(entry.debit) |
| 58 | entry.debit = 0.0 |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 59 | |
Nabin Hait | c561a49 | 2015-08-19 19:22:34 +0530 | [diff] [blame] | 60 | if flt(entry.debit_in_account_currency) < 0: |
| 61 | entry.credit_in_account_currency = \ |
| 62 | flt(entry.credit_in_account_currency) - flt(entry.debit_in_account_currency) |
| 63 | entry.debit_in_account_currency = 0.0 |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 64 | |
Nabin Hait | 88f3cd5 | 2013-10-23 16:29:19 +0530 | [diff] [blame] | 65 | if flt(entry.credit) < 0: |
| 66 | entry.debit = flt(entry.debit) - flt(entry.credit) |
| 67 | entry.credit = 0.0 |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 68 | |
Nabin Hait | c561a49 | 2015-08-19 19:22:34 +0530 | [diff] [blame] | 69 | if flt(entry.credit_in_account_currency) < 0: |
| 70 | entry.debit_in_account_currency = \ |
| 71 | flt(entry.debit_in_account_currency) - flt(entry.credit_in_account_currency) |
| 72 | entry.credit_in_account_currency = 0.0 |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 73 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 74 | return gl_map |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 75 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 76 | def merge_similar_entries(gl_map): |
| 77 | merged_gl_map = [] |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 78 | accounting_dimensions = get_accounting_dimensions() |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 79 | for entry in gl_map: |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 80 | # 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] | 81 | # to that entry |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 82 | same_head = check_if_in_list(entry, merged_gl_map, accounting_dimensions) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 83 | if same_head: |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 84 | same_head.debit = flt(same_head.debit) + flt(entry.debit) |
Nabin Hait | c561a49 | 2015-08-19 19:22:34 +0530 | [diff] [blame] | 85 | same_head.debit_in_account_currency = \ |
| 86 | 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] | 87 | same_head.credit = flt(same_head.credit) + flt(entry.credit) |
Nabin Hait | c561a49 | 2015-08-19 19:22:34 +0530 | [diff] [blame] | 88 | same_head.credit_in_account_currency = \ |
| 89 | 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] | 90 | else: |
| 91 | merged_gl_map.append(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 92 | |
Nabin Hait | 815a49e | 2013-08-07 17:00:01 +0530 | [diff] [blame] | 93 | # filter zero debit and credit entries |
Nabin Hait | 2e54da2 | 2015-08-13 12:19:20 +0530 | [diff] [blame] | 94 | merged_gl_map = filter(lambda x: flt(x.debit, 9)!=0 or flt(x.credit, 9)!=0, merged_gl_map) |
Achilles Rasquinha | 908289d | 2018-03-08 13:10:51 +0530 | [diff] [blame] | 95 | merged_gl_map = list(merged_gl_map) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 96 | |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 97 | return merged_gl_map |
| 98 | |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 99 | def check_if_in_list(gle, gl_map, dimensions=None): |
| 100 | account_head_fieldnames = ['party_type', 'party', 'against_voucher', 'against_voucher_type', |
| 101 | 'cost_center', 'project'] |
| 102 | |
| 103 | if dimensions: |
| 104 | account_head_fieldnames = account_head_fieldnames + dimensions |
| 105 | |
Nabin Hait | bb77756 | 2013-08-29 18:19:37 +0530 | [diff] [blame] | 106 | for e in gl_map: |
deepeshgarg007 | 3d11ac0 | 2019-05-19 00:02:01 +0530 | [diff] [blame] | 107 | same_head = True |
| 108 | if e.account != gle.account: |
| 109 | same_head = False |
| 110 | |
| 111 | for fieldname in account_head_fieldnames: |
| 112 | if cstr(e.get(fieldname)) != cstr(gle.get(fieldname)): |
| 113 | same_head = False |
| 114 | |
| 115 | if same_head: |
| 116 | return e |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 117 | |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 118 | def save_entries(gl_map, adv_adj, update_outstanding, from_repost=False): |
| 119 | if not from_repost: |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 120 | validate_cwip_accounts(gl_map) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 121 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 122 | round_off_debit_credit(gl_map) |
Nabin Hait | bf495c9 | 2013-01-30 12:49:08 +0530 | [diff] [blame] | 123 | for entry in gl_map: |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 124 | make_entry(entry, adv_adj, update_outstanding, from_repost) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 125 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 126 | # check against budget |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 127 | if not from_repost: |
| 128 | validate_expense_against_budget(entry) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 129 | |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 130 | if not from_repost: |
| 131 | validate_account_for_perpetual_inventory(gl_map) |
| 132 | |
| 133 | |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 134 | def make_entry(args, adv_adj, update_outstanding, from_repost=False): |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 135 | args.update({"doctype": "GL Entry"}) |
Rushabh Mehta | a504f06 | 2014-04-04 12:16:26 +0530 | [diff] [blame] | 136 | gle = frappe.get_doc(args) |
Anand Doshi | 6dfd430 | 2015-02-10 14:41:27 +0530 | [diff] [blame] | 137 | gle.flags.ignore_permissions = 1 |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 138 | gle.flags.from_repost = from_repost |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 139 | gle.insert() |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 140 | gle.run_method("on_update_with_args", adv_adj, update_outstanding, from_repost) |
Nabin Hait | aeba24e | 2013-08-23 15:17:36 +0530 | [diff] [blame] | 141 | gle.submit() |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 142 | |
Rohit Waghchaure | a5f4094 | 2017-06-16 15:21:36 +0530 | [diff] [blame] | 143 | def validate_account_for_perpetual_inventory(gl_map): |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 144 | if cint(erpnext.is_perpetual_inventory_enabled(gl_map[0].company)): |
| 145 | account_list = [gl_entries.account for gl_entries in gl_map] |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 146 | |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 147 | aii_accounts = [d.name for d in frappe.get_all("Account", |
| 148 | filters={'account_type': 'Stock', 'is_group': 0, 'company': gl_map[0].company})] |
| 149 | |
| 150 | for account in account_list: |
| 151 | if account not in aii_accounts: |
| 152 | continue |
| 153 | |
| 154 | account_bal, stock_bal, warehouse_list = get_stock_and_account_balance(account, |
| 155 | gl_map[0].posting_date, gl_map[0].company) |
| 156 | |
| 157 | if gl_map[0].voucher_type=="Journal Entry": |
| 158 | # In case of Journal Entry, there are no corresponding SL entries, |
| 159 | # hence deducting currency amount |
| 160 | account_bal -= flt(gl_map[0].debit) - flt(gl_map[0].credit) |
| 161 | if account_bal == stock_bal: |
Nabin Hait | e7f479b | 2015-06-22 07:31:49 +0530 | [diff] [blame] | 162 | frappe.throw(_("Account: {0} can only be updated via Stock Transactions") |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 163 | .format(account), StockAccountInvalidTransaction) |
| 164 | |
| 165 | elif account_bal != stock_bal: |
Marica | c9e8a1b | 2019-11-14 16:13:43 +0530 | [diff] [blame] | 166 | error_reason = _("Account Balance ({0}) and Stock Value ({1}) is out of sync for account {2} and it's linked warehouses.").format( |
| 167 | account_bal, stock_bal, frappe.bold(account)) |
| 168 | error_resolution = _("Please create adjustment Journal Entry for amount {0} ").format(frappe.bold(stock_bal - account_bal)) |
| 169 | button_text = _("Make Adjustment Entry") |
| 170 | |
| 171 | frappe.throw("""{0}<br></br>{1}<br></br> |
| 172 | <div style="text-align:right;"> |
| 173 | <button class="btn btn-primary" onclick="frappe.new_doc('Journal Entry')">{2}</button> |
| 174 | </div>""".format(error_reason, error_resolution, button_text), |
| 175 | StockValueAndAccountBalanceOutOfSync, title=_('Account Balance Out Of Sync')) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 176 | |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 177 | def validate_cwip_accounts(gl_map): |
Marica | d00c598 | 2019-11-12 19:17:43 +0530 | [diff] [blame] | 178 | cwip_enabled = cint(frappe.get_cached_value("Company", |
| 179 | gl_map[0].company, "enable_cwip_accounting")) |
| 180 | |
| 181 | if not cwip_enabled: |
| 182 | cwip_enabled = any([cint(ac.enable_cwip_accounting) for ac in frappe.db.get_all("Asset Category","enable_cwip_accounting")]) |
| 183 | |
| 184 | if cwip_enabled and gl_map[0].voucher_type == "Journal Entry": |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 185 | cwip_accounts = [d[0] for d in frappe.db.sql("""select name from tabAccount |
| 186 | where account_type = 'Capital Work in Progress' and is_group=0""")] |
| 187 | |
| 188 | for entry in gl_map: |
| 189 | if entry.account in cwip_accounts: |
Marica | d00c598 | 2019-11-12 19:17:43 +0530 | [diff] [blame] | 190 | frappe.throw( |
| 191 | _("Account: <b>{0}</b> is capital Work in progress and can not be updated by Journal Entry").format(entry.account)) |
Anurag Mishra | 7e1987e | 2019-08-05 10:18:57 +0530 | [diff] [blame] | 192 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 193 | def round_off_debit_credit(gl_map): |
| 194 | precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit"), |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 195 | currency=frappe.get_cached_value('Company', gl_map[0].company, "default_currency")) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 196 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 197 | debit_credit_diff = 0.0 |
| 198 | for entry in gl_map: |
| 199 | entry.debit = flt(entry.debit, precision) |
| 200 | entry.credit = flt(entry.credit, precision) |
| 201 | debit_credit_diff += entry.debit - entry.credit |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 202 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 203 | debit_credit_diff = flt(debit_credit_diff, precision) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 204 | |
Nabin Hait | d2a966e | 2017-05-05 10:41:16 +0530 | [diff] [blame] | 205 | if gl_map[0]["voucher_type"] in ("Journal Entry", "Payment Entry"): |
Nabin Hait | fb24a27 | 2016-02-18 19:18:07 +0530 | [diff] [blame] | 206 | allowance = 5.0 / (10**precision) |
| 207 | else: |
Nabin Hait | d2a966e | 2017-05-05 10:41:16 +0530 | [diff] [blame] | 208 | allowance = .5 |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 209 | |
Nabin Hait | fb24a27 | 2016-02-18 19:18:07 +0530 | [diff] [blame] | 210 | if abs(debit_credit_diff) >= allowance: |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 211 | frappe.throw(_("Debit and Credit not equal for {0} #{1}. Difference is {2}.") |
| 212 | .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] | 213 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 214 | elif abs(debit_credit_diff) >= (1.0 / (10**precision)): |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 215 | make_round_off_gle(gl_map, debit_credit_diff, precision) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 216 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 217 | def make_round_off_gle(gl_map, debit_credit_diff, precision): |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 218 | round_off_account, round_off_cost_center = get_round_off_account_and_cost_center(gl_map[0].company) |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 219 | round_off_account_exists = False |
Nabin Hait | 80069a6 | 2015-05-28 19:19:59 +0530 | [diff] [blame] | 220 | round_off_gle = frappe._dict() |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 221 | for d in gl_map: |
| 222 | if d.account == round_off_account: |
| 223 | round_off_gle = d |
| 224 | if d.debit_in_account_currency: |
| 225 | debit_credit_diff -= flt(d.debit_in_account_currency) |
| 226 | else: |
| 227 | debit_credit_diff += flt(d.credit_in_account_currency) |
| 228 | round_off_account_exists = True |
| 229 | |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 230 | if round_off_account_exists and abs(debit_credit_diff) <= (1.0 / (10**precision)): |
| 231 | gl_map.remove(round_off_gle) |
| 232 | return |
| 233 | |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 234 | if not round_off_gle: |
| 235 | for k in ["voucher_type", "voucher_no", "company", |
| 236 | "posting_date", "remarks", "is_opening"]: |
| 237 | round_off_gle[k] = gl_map[0][k] |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 238 | |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 239 | round_off_gle.update({ |
| 240 | "account": round_off_account, |
Nabin Hait | d4507ac | 2016-01-20 16:14:27 +0530 | [diff] [blame] | 241 | "debit_in_account_currency": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, |
| 242 | "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] | 243 | "debit": abs(debit_credit_diff) if debit_credit_diff < 0 else 0, |
| 244 | "credit": debit_credit_diff if debit_credit_diff > 0 else 0, |
Nabin Hait | 3c67146 | 2015-05-28 13:19:01 +0530 | [diff] [blame] | 245 | "cost_center": round_off_cost_center, |
| 246 | "party_type": None, |
| 247 | "party": None, |
| 248 | "against_voucher_type": None, |
| 249 | "against_voucher": None |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 250 | }) |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 251 | |
Zarrar | 3523b77 | 2018-08-14 16:28:14 +0530 | [diff] [blame] | 252 | if not round_off_account_exists: |
| 253 | gl_map.append(round_off_gle) |
Nabin Hait | e2c200a | 2015-05-28 13:00:37 +0530 | [diff] [blame] | 254 | |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 255 | def get_round_off_account_and_cost_center(company): |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 256 | round_off_account, round_off_cost_center = frappe.get_cached_value('Company', company, |
Nabin Hait | 2e4de83 | 2017-09-19 14:53:16 +0530 | [diff] [blame] | 257 | ["round_off_account", "round_off_cost_center"]) or [None, None] |
| 258 | if not round_off_account: |
| 259 | frappe.throw(_("Please mention Round Off Account in Company")) |
| 260 | |
| 261 | if not round_off_cost_center: |
| 262 | frappe.throw(_("Please mention Round Off Cost Center in Company")) |
| 263 | |
| 264 | return round_off_account, round_off_cost_center |
| 265 | |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 266 | 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] | 267 | adv_adj=False, update_outstanding="Yes"): |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 268 | |
Nabin Hait | 53822ae | 2014-03-03 19:18:17 +0530 | [diff] [blame] | 269 | from erpnext.accounts.doctype.gl_entry.gl_entry import validate_balance_type, \ |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 270 | check_freezing_date, update_outstanding_amt, validate_frozen_account |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 271 | |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 272 | if not gl_entries: |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 273 | gl_entries = frappe.db.sql(""" |
Rohit Waghchaure | b1391ca | 2016-10-03 12:25:04 +0530 | [diff] [blame] | 274 | select account, posting_date, party_type, party, cost_center, fiscal_year,voucher_type, |
| 275 | voucher_no, against_voucher_type, against_voucher, cost_center, company |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 276 | from `tabGL Entry` |
Nabin Hait | 2e296fa | 2013-08-28 18:53:11 +0530 | [diff] [blame] | 277 | where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no), as_dict=True) |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 278 | |
Nabin Hait | 27994c2 | 2013-08-26 16:53:30 +0530 | [diff] [blame] | 279 | if gl_entries: |
| 280 | check_freezing_date(gl_entries[0]["posting_date"], adv_adj) |
Anand Doshi | 652bc07 | 2014-04-16 15:21:46 +0530 | [diff] [blame] | 281 | |
| 282 | 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] | 283 | (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] | 284 | |
Nabin Hait | 9b09c95 | 2013-08-21 17:47:11 +0530 | [diff] [blame] | 285 | for entry in gl_entries: |
Nabin Hait | 11f4195 | 2013-09-24 14:36:55 +0530 | [diff] [blame] | 286 | validate_frozen_account(entry["account"], adv_adj) |
Nabin Hait | 53822ae | 2014-03-03 19:18:17 +0530 | [diff] [blame] | 287 | validate_balance_type(entry["account"], adv_adj) |
Nabin Hait | 1e1b236 | 2018-01-29 16:45:43 +0530 | [diff] [blame] | 288 | if not adv_adj: |
| 289 | validate_expense_against_budget(entry) |
Rushabh Mehta | 708e47a | 2018-08-08 16:37:31 +0530 | [diff] [blame] | 290 | |
Nabin Hait | 1e1b236 | 2018-01-29 16:45:43 +0530 | [diff] [blame] | 291 | if entry.get("against_voucher") and update_outstanding == 'Yes' and not adv_adj: |
Nabin Hait | 3225102 | 2014-08-29 11:18:32 +0530 | [diff] [blame] | 292 | 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] | 293 | entry.get("against_voucher"), on_cancel=True) |