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 | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 3 | |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 4 | |
Saqib | 9051735 | 2021-10-04 11:44:46 +0530 | [diff] [blame] | 5 | from json import loads |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 6 | from typing import List, Tuple |
Saqib | 9051735 | 2021-10-04 11:44:46 +0530 | [diff] [blame] | 7 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 8 | import frappe |
Nabin Hait | 9b20e07 | 2017-04-25 12:10:24 +0530 | [diff] [blame] | 9 | import frappe.defaults |
ruthra kumar | 451cf3a | 2022-05-16 14:29:58 +0530 | [diff] [blame] | 10 | from frappe import _, qb, throw |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 11 | from frappe.model.meta import get_field_precision |
Gavin D'souza | 0727d1d | 2022-06-13 12:39:28 +0530 | [diff] [blame] | 12 | from frappe.query_builder.utils import DocType |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 13 | from frappe.utils import cint, cstr, flt, formatdate, get_number_format_info, getdate, now, nowdate |
Gavin D'souza | 0727d1d | 2022-06-13 12:39:28 +0530 | [diff] [blame] | 14 | from pypika import Order |
| 15 | from pypika.terms import ExistsCriterion |
Anand Doshi | cd0989e | 2015-09-28 13:31:17 +0530 | [diff] [blame] | 16 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 17 | import erpnext |
| 18 | |
| 19 | # imported to enable erpnext.accounts.utils.get_account_currency |
| 20 | from erpnext.accounts.doctype.account.account import get_account_currency # noqa |
ruthra kumar | 451cf3a | 2022-05-16 14:29:58 +0530 | [diff] [blame] | 21 | from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 22 | from erpnext.stock import get_warehouse_account_map |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 23 | from erpnext.stock.utils import get_stock_value_on |
| 24 | |
Anurag Mishra | a11e738 | 2019-10-31 15:55:03 +0530 | [diff] [blame] | 25 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 26 | class FiscalYearError(frappe.ValidationError): |
| 27 | pass |
| 28 | |
| 29 | |
| 30 | class PaymentEntryUnlinkError(frappe.ValidationError): |
| 31 | pass |
| 32 | |
Nabin Hait | 2b06aaa | 2013-08-22 18:25:43 +0530 | [diff] [blame] | 33 | |
Neil Trini Lasrado | 78fa695 | 2014-10-03 17:43:02 +0530 | [diff] [blame] | 34 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 35 | def get_fiscal_year( |
| 36 | date=None, fiscal_year=None, label="Date", verbose=1, company=None, as_dict=False |
| 37 | ): |
Anand Doshi | c75c1d7 | 2016-03-11 14:56:19 +0530 | [diff] [blame] | 38 | return get_fiscal_years(date, fiscal_year, label, verbose, company, as_dict=as_dict)[0] |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 39 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 40 | |
| 41 | def get_fiscal_years( |
| 42 | transaction_date=None, fiscal_year=None, label="Date", verbose=1, company=None, as_dict=False |
| 43 | ): |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 44 | fiscal_years = frappe.cache().hget("fiscal_years", company) or [] |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 45 | |
| 46 | if not fiscal_years: |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 47 | # if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate) |
Gavin D'souza | 0727d1d | 2022-06-13 12:39:28 +0530 | [diff] [blame] | 48 | FY = DocType("Fiscal Year") |
Nabin Hait | fff3ab7 | 2015-01-14 16:27:13 +0530 | [diff] [blame] | 49 | |
Gavin D'souza | 0727d1d | 2022-06-13 12:39:28 +0530 | [diff] [blame] | 50 | query = ( |
| 51 | frappe.qb.from_(FY) |
| 52 | .select(FY.name, FY.year_start_date, FY.year_end_date) |
| 53 | .where(FY.disabled == 0) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 54 | ) |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 55 | |
Gavin D'souza | 0727d1d | 2022-06-13 12:39:28 +0530 | [diff] [blame] | 56 | if fiscal_year: |
| 57 | query = query.where(FY.name == fiscal_year) |
| 58 | |
| 59 | if company: |
| 60 | FYC = DocType("Fiscal Year Company") |
| 61 | query = query.where( |
| 62 | ExistsCriterion(frappe.qb.from_(FYC).select(FYC.name).where(FYC.parent == FY.name)).negate() |
| 63 | | ExistsCriterion( |
| 64 | frappe.qb.from_(FYC) |
| 65 | .select(FYC.company) |
| 66 | .where(FYC.parent == FY.name) |
| 67 | .where(FYC.company == company) |
| 68 | ) |
| 69 | ) |
| 70 | |
| 71 | query = query.orderby(FY.year_start_date, Order.desc) |
| 72 | fiscal_years = query.run(as_dict=True) |
| 73 | |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 74 | frappe.cache().hset("fiscal_years", company, fiscal_years) |
Nabin Hait | fff3ab7 | 2015-01-14 16:27:13 +0530 | [diff] [blame] | 75 | |
Prssanna Desai | 82ddef5 | 2020-06-18 18:18:41 +0530 | [diff] [blame] | 76 | if not transaction_date and not fiscal_year: |
| 77 | return fiscal_years |
| 78 | |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 79 | if transaction_date: |
| 80 | transaction_date = getdate(transaction_date) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 81 | |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 82 | for fy in fiscal_years: |
| 83 | matched = False |
| 84 | if fiscal_year and fy.name == fiscal_year: |
| 85 | matched = True |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 86 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 87 | if ( |
| 88 | transaction_date |
| 89 | and getdate(fy.year_start_date) <= transaction_date |
| 90 | and getdate(fy.year_end_date) >= transaction_date |
| 91 | ): |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 92 | matched = True |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 93 | |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 94 | if matched: |
| 95 | if as_dict: |
| 96 | return (fy,) |
| 97 | else: |
| 98 | return ((fy.name, fy.year_start_date, fy.year_end_date),) |
| 99 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 100 | error_msg = _("""{0} {1} is not in any active Fiscal Year""").format( |
| 101 | label, formatdate(transaction_date) |
| 102 | ) |
Anuja P | 2e4faf9 | 2020-12-05 13:36:43 +0530 | [diff] [blame] | 103 | if company: |
Anuja P | 550cb9c | 2020-12-07 11:11:00 +0530 | [diff] [blame] | 104 | error_msg = _("""{0} for {1}""").format(error_msg, frappe.bold(company)) |
rohitwaghchaure | d60ff83 | 2021-02-16 09:12:27 +0530 | [diff] [blame] | 105 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 106 | if verbose == 1: |
| 107 | frappe.msgprint(error_msg) |
cclauss | 6848708 | 2017-07-27 07:08:35 +0200 | [diff] [blame] | 108 | raise FiscalYearError(error_msg) |
Nabin Hait | 9784d27 | 2016-12-30 16:21:35 +0530 | [diff] [blame] | 109 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 110 | |
Prssanna Desai | 82ddef5 | 2020-06-18 18:18:41 +0530 | [diff] [blame] | 111 | @frappe.whitelist() |
| 112 | def get_fiscal_year_filter_field(company=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 113 | field = {"fieldtype": "Select", "options": [], "operator": "Between", "query_value": True} |
Prssanna Desai | 82ddef5 | 2020-06-18 18:18:41 +0530 | [diff] [blame] | 114 | fiscal_years = get_fiscal_years(company=company) |
| 115 | for fiscal_year in fiscal_years: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 116 | field["options"].append( |
| 117 | { |
| 118 | "label": fiscal_year.name, |
| 119 | "value": fiscal_year.name, |
| 120 | "query_value": [ |
| 121 | fiscal_year.year_start_date.strftime("%Y-%m-%d"), |
| 122 | fiscal_year.year_end_date.strftime("%Y-%m-%d"), |
| 123 | ], |
| 124 | } |
| 125 | ) |
Prssanna Desai | 82ddef5 | 2020-06-18 18:18:41 +0530 | [diff] [blame] | 126 | return field |
| 127 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 128 | |
Nabin Hait | 8bf5836 | 2017-03-27 12:30:01 +0530 | [diff] [blame] | 129 | def validate_fiscal_year(date, fiscal_year, company, label="Date", doc=None): |
| 130 | years = [f[0] for f in get_fiscal_years(date, label=_(label), company=company)] |
Rushabh Mehta | c2563ef | 2013-02-05 23:25:37 +0530 | [diff] [blame] | 131 | if fiscal_year not in years: |
Rushabh Mehta | d60acb9 | 2015-02-19 14:51:58 +0530 | [diff] [blame] | 132 | if doc: |
| 133 | doc.fiscal_year = years[0] |
| 134 | else: |
| 135 | throw(_("{0} '{1}' not in Fiscal Year {2}").format(label, formatdate(date), fiscal_year)) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 136 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 137 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 138 | @frappe.whitelist() |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 139 | def get_balance_on( |
| 140 | account=None, |
| 141 | date=None, |
| 142 | party_type=None, |
| 143 | party=None, |
| 144 | company=None, |
| 145 | in_account_currency=True, |
| 146 | cost_center=None, |
| 147 | ignore_account_permission=False, |
| 148 | ): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 149 | if not account and frappe.form_dict.get("account"): |
| 150 | account = frappe.form_dict.get("account") |
Nabin Hait | 6f17cf9 | 2014-09-17 14:11:22 +0530 | [diff] [blame] | 151 | if not date and frappe.form_dict.get("date"): |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 152 | date = frappe.form_dict.get("date") |
Nabin Hait | 6f17cf9 | 2014-09-17 14:11:22 +0530 | [diff] [blame] | 153 | if not party_type and frappe.form_dict.get("party_type"): |
| 154 | party_type = frappe.form_dict.get("party_type") |
| 155 | if not party and frappe.form_dict.get("party"): |
| 156 | party = frappe.form_dict.get("party") |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 157 | if not cost_center and frappe.form_dict.get("cost_center"): |
| 158 | cost_center = frappe.form_dict.get("cost_center") |
| 159 | |
Nabin Hait | 9837285 | 2020-07-30 20:52:20 +0530 | [diff] [blame] | 160 | cond = ["is_cancelled=0"] |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 161 | if date: |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 162 | cond.append("posting_date <= %s" % frappe.db.escape(cstr(date))) |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 163 | else: |
| 164 | # get balance of all entries that exist |
| 165 | date = nowdate() |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 166 | |
Deepesh Garg | 8c21703 | 2019-07-16 09:41:01 +0530 | [diff] [blame] | 167 | if account: |
| 168 | acc = frappe.get_doc("Account", account) |
| 169 | |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 170 | try: |
Deepesh Garg | 96d40ec | 2020-07-03 22:59:00 +0530 | [diff] [blame] | 171 | year_start_date = get_fiscal_year(date, company=company, verbose=0)[1] |
Rushabh Mehta | 9f0d625 | 2014-04-14 19:20:45 +0530 | [diff] [blame] | 172 | except FiscalYearError: |
Nabin Hait | 23941aa | 2013-01-29 11:32:38 +0530 | [diff] [blame] | 173 | if getdate(date) > getdate(nowdate()): |
| 174 | # if fiscal year not found and the date is greater than today |
| 175 | # get fiscal year for today's date and its corresponding year start date |
| 176 | year_start_date = get_fiscal_year(nowdate(), verbose=1)[1] |
| 177 | else: |
| 178 | # this indicates that it is a date older than any existing fiscal year. |
| 179 | # hence, assuming balance as 0.0 |
| 180 | return 0.0 |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 181 | |
deepeshgarg007 | 2ddbebf | 2019-07-20 14:52:59 +0530 | [diff] [blame] | 182 | if account: |
deepeshgarg007 | e097d4f | 2019-07-20 14:49:32 +0530 | [diff] [blame] | 183 | report_type = acc.report_type |
deepeshgarg007 | 1ee3d04 | 2019-07-20 14:46:59 +0530 | [diff] [blame] | 184 | else: |
| 185 | report_type = "" |
| 186 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 187 | if cost_center and report_type == "Profit and Loss": |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 188 | cc = frappe.get_doc("Cost Center", cost_center) |
| 189 | if cc.is_group: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 190 | cond.append( |
| 191 | """ exists ( |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 192 | select 1 from `tabCost Center` cc where cc.name = gle.cost_center |
| 193 | and cc.lft >= %s and cc.rgt <= %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 194 | )""" |
| 195 | % (cc.lft, cc.rgt) |
| 196 | ) |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 197 | |
| 198 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 199 | cond.append("""gle.cost_center = %s """ % (frappe.db.escape(cost_center, percent=False),)) |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 200 | |
Nabin Hait | 6f17cf9 | 2014-09-17 14:11:22 +0530 | [diff] [blame] | 201 | if account: |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 202 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 203 | if not (frappe.flags.ignore_account_permission or ignore_account_permission): |
Rushabh Mehta | 2e7f9d2 | 2015-10-15 16:31:16 +0530 | [diff] [blame] | 204 | acc.check_permission("read") |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 205 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 206 | if report_type == "Profit and Loss": |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 207 | # for pl accounts, get balance within a fiscal year |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 208 | cond.append( |
| 209 | "posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % year_start_date |
| 210 | ) |
Nabin Hait | 6f17cf9 | 2014-09-17 14:11:22 +0530 | [diff] [blame] | 211 | # different filter for group and ledger - improved performance |
Rushabh Mehta | 38c6b52 | 2015-04-23 13:14:17 +0530 | [diff] [blame] | 212 | if acc.is_group: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 213 | cond.append( |
| 214 | """exists ( |
Nabin Hait | 6b01abe | 2015-06-14 17:49:29 +0530 | [diff] [blame] | 215 | select name from `tabAccount` ac where ac.name = gle.account |
Nabin Hait | 6f17cf9 | 2014-09-17 14:11:22 +0530 | [diff] [blame] | 216 | and ac.lft >= %s and ac.rgt <= %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 217 | )""" |
| 218 | % (acc.lft, acc.rgt) |
| 219 | ) |
Anand Doshi | cd0989e | 2015-09-28 13:31:17 +0530 | [diff] [blame] | 220 | |
| 221 | # If group and currency same as company, |
Nabin Hait | 59f4fa9 | 2015-09-17 13:57:42 +0530 | [diff] [blame] | 222 | # always return balance based on debit and credit in company currency |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 223 | if acc.account_currency == frappe.get_cached_value("Company", acc.company, "default_currency"): |
Nabin Hait | 59f4fa9 | 2015-09-17 13:57:42 +0530 | [diff] [blame] | 224 | in_account_currency = False |
Nabin Hait | 6f17cf9 | 2014-09-17 14:11:22 +0530 | [diff] [blame] | 225 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 226 | cond.append("""gle.account = %s """ % (frappe.db.escape(account, percent=False),)) |
Anand Doshi | c75c1d7 | 2016-03-11 14:56:19 +0530 | [diff] [blame] | 227 | |
Nabin Hait | 6f17cf9 | 2014-09-17 14:11:22 +0530 | [diff] [blame] | 228 | if party_type and party: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 229 | cond.append( |
| 230 | """gle.party_type = %s and gle.party = %s """ |
| 231 | % (frappe.db.escape(party_type), frappe.db.escape(party, percent=False)) |
| 232 | ) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 233 | |
Nabin Hait | 85648d9 | 2016-07-20 11:26:45 +0530 | [diff] [blame] | 234 | if company: |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 235 | cond.append("""gle.company = %s """ % (frappe.db.escape(company, percent=False))) |
Anand Doshi | 0b031cd | 2015-09-16 12:46:54 +0530 | [diff] [blame] | 236 | |
Nabin Hait | dc76823 | 2015-08-17 11:27:00 +0530 | [diff] [blame] | 237 | if account or (party_type and party): |
Nabin Hait | c0e3b1a | 2015-09-04 13:26:23 +0530 | [diff] [blame] | 238 | if in_account_currency: |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 239 | select_field = "sum(debit_in_account_currency) - sum(credit_in_account_currency)" |
Nabin Hait | c0e3b1a | 2015-09-04 13:26:23 +0530 | [diff] [blame] | 240 | else: |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 241 | select_field = "sum(debit) - sum(credit)" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 242 | bal = frappe.db.sql( |
| 243 | """ |
Nabin Hait | c0e3b1a | 2015-09-04 13:26:23 +0530 | [diff] [blame] | 244 | SELECT {0} |
Nabin Hait | dc76823 | 2015-08-17 11:27:00 +0530 | [diff] [blame] | 245 | FROM `tabGL Entry` gle |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 246 | WHERE {1}""".format( |
| 247 | select_field, " and ".join(cond) |
| 248 | ) |
| 249 | )[0][0] |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 250 | |
Nabin Hait | dc76823 | 2015-08-17 11:27:00 +0530 | [diff] [blame] | 251 | # if bal is None, return 0 |
| 252 | return flt(bal) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 253 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 254 | |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 255 | def get_count_on(account, fieldname, date): |
Nabin Hait | 9837285 | 2020-07-30 20:52:20 +0530 | [diff] [blame] | 256 | cond = ["is_cancelled=0"] |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 257 | if date: |
Suraj Shetty | bfc195d | 2018-09-21 10:20:52 +0530 | [diff] [blame] | 258 | cond.append("posting_date <= %s" % frappe.db.escape(cstr(date))) |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 259 | else: |
| 260 | # get balance of all entries that exist |
| 261 | date = nowdate() |
| 262 | |
| 263 | try: |
| 264 | year_start_date = get_fiscal_year(date, verbose=0)[1] |
| 265 | except FiscalYearError: |
| 266 | if getdate(date) > getdate(nowdate()): |
| 267 | # if fiscal year not found and the date is greater than today |
| 268 | # get fiscal year for today's date and its corresponding year start date |
| 269 | year_start_date = get_fiscal_year(nowdate(), verbose=1)[1] |
| 270 | else: |
| 271 | # this indicates that it is a date older than any existing fiscal year. |
| 272 | # hence, assuming balance as 0.0 |
| 273 | return 0.0 |
| 274 | |
| 275 | if account: |
| 276 | acc = frappe.get_doc("Account", account) |
| 277 | |
| 278 | if not frappe.flags.ignore_account_permission: |
| 279 | acc.check_permission("read") |
| 280 | |
| 281 | # for pl accounts, get balance within a fiscal year |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 282 | if acc.report_type == "Profit and Loss": |
| 283 | cond.append( |
| 284 | "posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % year_start_date |
| 285 | ) |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 286 | |
| 287 | # different filter for group and ledger - improved performance |
| 288 | if acc.is_group: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 289 | cond.append( |
| 290 | """exists ( |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 291 | select name from `tabAccount` ac where ac.name = gle.account |
| 292 | and ac.lft >= %s and ac.rgt <= %s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 293 | )""" |
| 294 | % (acc.lft, acc.rgt) |
| 295 | ) |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 296 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 297 | cond.append("""gle.account = %s """ % (frappe.db.escape(account, percent=False),)) |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 298 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 299 | entries = frappe.db.sql( |
| 300 | """ |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 301 | SELECT name, posting_date, account, party_type, party,debit,credit, |
| 302 | voucher_type, voucher_no, against_voucher_type, against_voucher |
| 303 | FROM `tabGL Entry` gle |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 304 | WHERE {0}""".format( |
| 305 | " and ".join(cond) |
| 306 | ), |
| 307 | as_dict=True, |
| 308 | ) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 309 | |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 310 | count = 0 |
| 311 | for gle in entries: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 312 | if fieldname not in ("invoiced_amount", "payables"): |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 313 | count += 1 |
| 314 | else: |
| 315 | dr_or_cr = "debit" if fieldname == "invoiced_amount" else "credit" |
| 316 | cr_or_dr = "credit" if fieldname == "invoiced_amount" else "debit" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 317 | select_fields = ( |
| 318 | "ifnull(sum(credit-debit),0)" |
| 319 | if fieldname == "invoiced_amount" |
| 320 | else "ifnull(sum(debit-credit),0)" |
| 321 | ) |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 322 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 323 | if ( |
| 324 | (not gle.against_voucher) |
| 325 | or (gle.against_voucher_type in ["Sales Order", "Purchase Order"]) |
| 326 | or (gle.against_voucher == gle.voucher_no and gle.get(dr_or_cr) > 0) |
| 327 | ): |
| 328 | payment_amount = frappe.db.sql( |
| 329 | """ |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 330 | SELECT {0} |
| 331 | FROM `tabGL Entry` gle |
| 332 | WHERE docstatus < 2 and posting_date <= %(date)s and against_voucher = %(voucher_no)s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 333 | and party = %(party)s and name != %(name)s""".format( |
| 334 | select_fields |
| 335 | ), |
| 336 | {"date": date, "voucher_no": gle.voucher_no, "party": gle.party, "name": gle.name}, |
| 337 | )[0][0] |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 338 | |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 339 | outstanding_amount = flt(gle.get(dr_or_cr)) - flt(gle.get(cr_or_dr)) - payment_amount |
| 340 | currency_precision = get_currency_precision() or 2 |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 341 | if abs(flt(outstanding_amount)) > 0.1 / 10**currency_precision: |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 342 | count += 1 |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 343 | |
RobertSchouten | 8d43b32 | 2016-09-20 13:41:39 +0800 | [diff] [blame] | 344 | return count |
| 345 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 346 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 347 | @frappe.whitelist() |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 348 | def add_ac(args=None): |
Saurabh | 2f02101 | 2017-01-11 12:41:01 +0530 | [diff] [blame] | 349 | from frappe.desk.treeview import make_tree_args |
| 350 | |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 351 | if not args: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 352 | args = frappe.local.form_dict |
Saurabh | 2f02101 | 2017-01-11 12:41:01 +0530 | [diff] [blame] | 353 | |
Nabin Hait | 02d987e | 2017-02-17 15:50:26 +0530 | [diff] [blame] | 354 | args.doctype = "Account" |
Saurabh | 2f02101 | 2017-01-11 12:41:01 +0530 | [diff] [blame] | 355 | args = make_tree_args(**args) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 356 | |
Anand Doshi | 3e41fd1 | 2014-04-22 18:54:54 +0530 | [diff] [blame] | 357 | ac = frappe.new_doc("Account") |
Rushabh Mehta | 0dcb861 | 2016-05-31 07:22:37 +0530 | [diff] [blame] | 358 | |
Nabin Hait | 665568d | 2016-05-13 15:56:00 +0530 | [diff] [blame] | 359 | if args.get("ignore_permissions"): |
| 360 | ac.flags.ignore_permissions = True |
| 361 | args.pop("ignore_permissions") |
Rushabh Mehta | 0dcb861 | 2016-05-31 07:22:37 +0530 | [diff] [blame] | 362 | |
Anand Doshi | 3e41fd1 | 2014-04-22 18:54:54 +0530 | [diff] [blame] | 363 | ac.update(args) |
Saurabh | 0e47bfe | 2016-05-30 17:54:16 +0530 | [diff] [blame] | 364 | |
| 365 | if not ac.parent_account: |
| 366 | ac.parent_account = args.get("parent") |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 367 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 368 | ac.old_parent = "" |
| 369 | ac.freeze_account = "No" |
Nabin Hait | a1ec7f1 | 2016-05-11 12:37:22 +0530 | [diff] [blame] | 370 | if cint(ac.get("is_root")): |
| 371 | ac.parent_account = None |
Rushabh Mehta | 0dcb861 | 2016-05-31 07:22:37 +0530 | [diff] [blame] | 372 | ac.flags.ignore_mandatory = True |
| 373 | |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 374 | ac.insert() |
Anand Doshi | 3e41fd1 | 2014-04-22 18:54:54 +0530 | [diff] [blame] | 375 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 376 | return ac.name |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 377 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 378 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 379 | @frappe.whitelist() |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 380 | def add_cc(args=None): |
Saurabh | 2f02101 | 2017-01-11 12:41:01 +0530 | [diff] [blame] | 381 | from frappe.desk.treeview import make_tree_args |
| 382 | |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 383 | if not args: |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 384 | args = frappe.local.form_dict |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 385 | |
Nabin Hait | 02d987e | 2017-02-17 15:50:26 +0530 | [diff] [blame] | 386 | args.doctype = "Cost Center" |
Saurabh | 2f02101 | 2017-01-11 12:41:01 +0530 | [diff] [blame] | 387 | args = make_tree_args(**args) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 388 | |
Zarrar | 7ab70ca | 2018-06-08 14:33:15 +0530 | [diff] [blame] | 389 | if args.parent_cost_center == args.company: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 390 | args.parent_cost_center = "{0} - {1}".format( |
| 391 | args.parent_cost_center, frappe.get_cached_value("Company", args.company, "abbr") |
| 392 | ) |
Zarrar | 7ab70ca | 2018-06-08 14:33:15 +0530 | [diff] [blame] | 393 | |
Anand Doshi | 3e41fd1 | 2014-04-22 18:54:54 +0530 | [diff] [blame] | 394 | cc = frappe.new_doc("Cost Center") |
| 395 | cc.update(args) |
Saurabh | 0e47bfe | 2016-05-30 17:54:16 +0530 | [diff] [blame] | 396 | |
| 397 | if not cc.parent_cost_center: |
| 398 | cc.parent_cost_center = args.get("parent") |
| 399 | |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 400 | cc.old_parent = "" |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 401 | cc.insert() |
Anand Doshi | f78d1ae | 2014-03-28 13:55:00 +0530 | [diff] [blame] | 402 | return cc.name |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 403 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 404 | |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 405 | def reconcile_against_document(args): |
| 406 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 407 | Cancel PE or JV, Update against document, split if required and resubmit |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 408 | """ |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 409 | # To optimize making GL Entry for PE or JV with multiple references |
| 410 | reconciled_entries = {} |
| 411 | for row in args: |
| 412 | if not reconciled_entries.get((row.voucher_type, row.voucher_no)): |
| 413 | reconciled_entries[(row.voucher_type, row.voucher_no)] = [] |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 414 | |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 415 | reconciled_entries[(row.voucher_type, row.voucher_no)].append(row) |
| 416 | |
| 417 | for key, entries in reconciled_entries.items(): |
| 418 | voucher_type = key[0] |
| 419 | voucher_no = key[1] |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 420 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 421 | # cancel advance entry |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 422 | doc = frappe.get_doc(voucher_type, voucher_no) |
Subin Tom | b8845b9 | 2021-07-30 16:30:18 +0530 | [diff] [blame] | 423 | frappe.flags.ignore_party_validation = True |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 424 | doc.make_gl_entries(cancel=1, adv_adj=1) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 425 | |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 426 | for entry in entries: |
| 427 | check_if_advance_entry_modified(entry) |
| 428 | validate_allocated_amount(entry) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 429 | |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 430 | # update ref in advance entry |
| 431 | if voucher_type == "Journal Entry": |
| 432 | update_reference_in_journal_entry(entry, doc, do_not_save=True) |
| 433 | else: |
| 434 | update_reference_in_payment_entry(entry, doc, do_not_save=True) |
| 435 | |
| 436 | doc.save(ignore_permissions=True) |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 437 | # re-submit advance entry |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 438 | doc = frappe.get_doc(entry.voucher_type, entry.voucher_no) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 439 | doc.make_gl_entries(cancel=0, adv_adj=1) |
Subin Tom | b8845b9 | 2021-07-30 16:30:18 +0530 | [diff] [blame] | 440 | frappe.flags.ignore_party_validation = False |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 441 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 442 | if entry.voucher_type in ("Payment Entry", "Journal Entry"): |
Nabin Hait | 5cd04a6 | 2019-05-27 11:44:06 +0530 | [diff] [blame] | 443 | doc.update_expense_claim() |
| 444 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 445 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 446 | def check_if_advance_entry_modified(args): |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 447 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 448 | check if there is already a voucher reference |
| 449 | check if amount is same |
| 450 | check if jv is submitted |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 451 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 452 | if not args.get("unreconciled_amount"): |
| 453 | args.update({"unreconciled_amount": args.get("unadjusted_amount")}) |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 454 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 455 | ret = None |
| 456 | if args.voucher_type == "Journal Entry": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 457 | ret = frappe.db.sql( |
| 458 | """ |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 459 | select t2.{dr_or_cr} from `tabJournal Entry` t1, `tabJournal Entry Account` t2 |
| 460 | where t1.name = t2.parent and t2.account = %(account)s |
| 461 | and t2.party_type = %(party_type)s and t2.party = %(party)s |
| 462 | and (t2.reference_type is null or t2.reference_type in ("", "Sales Order", "Purchase Order")) |
| 463 | and t1.name = %(voucher_no)s and t2.name = %(voucher_detail_no)s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 464 | and t1.docstatus=1 """.format( |
| 465 | dr_or_cr=args.get("dr_or_cr") |
| 466 | ), |
| 467 | args, |
| 468 | ) |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 469 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 470 | party_account_field = ( |
| 471 | "paid_from" if erpnext.get_party_account_type(args.party_type) == "Receivable" else "paid_to" |
| 472 | ) |
rohitwaghchaure | e8358f3 | 2018-05-16 11:02:26 +0530 | [diff] [blame] | 473 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 474 | if args.voucher_detail_no: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 475 | ret = frappe.db.sql( |
| 476 | """select t1.name |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 477 | from `tabPayment Entry` t1, `tabPayment Entry Reference` t2 |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 478 | where |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 479 | t1.name = t2.parent and t1.docstatus = 1 |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 480 | and t1.name = %(voucher_no)s and t2.name = %(voucher_detail_no)s |
| 481 | and t1.party_type = %(party_type)s and t1.party = %(party)s and t1.{0} = %(account)s |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 482 | and t2.reference_doctype in ("", "Sales Order", "Purchase Order") |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 483 | and t2.allocated_amount = %(unreconciled_amount)s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 484 | """.format( |
| 485 | party_account_field |
| 486 | ), |
| 487 | args, |
| 488 | ) |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 489 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 490 | ret = frappe.db.sql( |
| 491 | """select name from `tabPayment Entry` |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 492 | where |
| 493 | name = %(voucher_no)s and docstatus = 1 |
| 494 | and party_type = %(party_type)s and party = %(party)s and {0} = %(account)s |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 495 | and unallocated_amount = %(unreconciled_amount)s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 496 | """.format( |
| 497 | party_account_field |
| 498 | ), |
| 499 | args, |
| 500 | ) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 501 | |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 502 | if not ret: |
Akhilesh Darjee | 4f72156 | 2014-01-29 16:31:38 +0530 | [diff] [blame] | 503 | throw(_("""Payment Entry has been modified after you pulled it. Please pull it again.""")) |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 504 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 505 | |
Nabin Hait | 576f025 | 2014-04-30 19:30:50 +0530 | [diff] [blame] | 506 | def validate_allocated_amount(args): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 507 | precision = args.get("precision") or frappe.db.get_single_value( |
| 508 | "System Settings", "currency_precision" |
| 509 | ) |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 510 | if args.get("allocated_amount") < 0: |
Kenneth Sequeira | a29ed40 | 2019-05-27 11:47:07 +0530 | [diff] [blame] | 511 | throw(_("Allocated amount cannot be negative")) |
Deepesh Garg | f54d596 | 2021-03-31 14:06:02 +0530 | [diff] [blame] | 512 | elif flt(args.get("allocated_amount"), precision) > flt(args.get("unadjusted_amount"), precision): |
Kenneth Sequeira | a29ed40 | 2019-05-27 11:47:07 +0530 | [diff] [blame] | 513 | throw(_("Allocated amount cannot be greater than unadjusted amount")) |
Nabin Hait | 576f025 | 2014-04-30 19:30:50 +0530 | [diff] [blame] | 514 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 515 | |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 516 | def update_reference_in_journal_entry(d, journal_entry, do_not_save=False): |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 517 | """ |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 518 | Updates against document, if partial amount splits into rows |
Nabin Hait | fb3fd6e | 2013-01-30 19:16:13 +0530 | [diff] [blame] | 519 | """ |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 520 | jv_detail = journal_entry.get("accounts", {"name": d["voucher_detail_no"]})[0] |
Rushabh Mehta | 1828c12 | 2015-08-10 17:04:07 +0530 | [diff] [blame] | 521 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 522 | if flt(d["unadjusted_amount"]) - flt(d["allocated_amount"]) != 0: |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 523 | # adjust the unreconciled balance |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 524 | amount_in_account_currency = flt(d["unadjusted_amount"]) - flt(d["allocated_amount"]) |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 525 | amount_in_company_currency = amount_in_account_currency * flt(jv_detail.exchange_rate) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 526 | jv_detail.set(d["dr_or_cr"], amount_in_account_currency) |
| 527 | jv_detail.set( |
| 528 | "debit" if d["dr_or_cr"] == "debit_in_account_currency" else "credit", |
| 529 | amount_in_company_currency, |
| 530 | ) |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 531 | else: |
| 532 | journal_entry.remove(jv_detail) |
Anand Doshi | 0b031cd | 2015-09-16 12:46:54 +0530 | [diff] [blame] | 533 | |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 534 | # new row with references |
| 535 | new_row = journal_entry.append("accounts") |
Anuja Pawar | 1a6e98e | 2021-10-29 20:52:47 +0530 | [diff] [blame] | 536 | |
| 537 | new_row.update((frappe.copy_doc(jv_detail)).as_dict()) |
Rushabh Mehta | 2e7f9d2 | 2015-10-15 16:31:16 +0530 | [diff] [blame] | 538 | |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 539 | new_row.set(d["dr_or_cr"], d["allocated_amount"]) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 540 | new_row.set( |
| 541 | "debit" if d["dr_or_cr"] == "debit_in_account_currency" else "credit", |
| 542 | d["allocated_amount"] * flt(jv_detail.exchange_rate), |
| 543 | ) |
Rushabh Mehta | 2e7f9d2 | 2015-10-15 16:31:16 +0530 | [diff] [blame] | 544 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 545 | new_row.set( |
| 546 | "credit_in_account_currency" |
| 547 | if d["dr_or_cr"] == "debit_in_account_currency" |
| 548 | else "debit_in_account_currency", |
| 549 | 0, |
| 550 | ) |
| 551 | new_row.set("credit" if d["dr_or_cr"] == "debit_in_account_currency" else "debit", 0) |
Rushabh Mehta | 2e7f9d2 | 2015-10-15 16:31:16 +0530 | [diff] [blame] | 552 | |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 553 | new_row.set("reference_type", d["against_voucher_type"]) |
| 554 | new_row.set("reference_name", d["against_voucher"]) |
| 555 | |
| 556 | new_row.against_account = cstr(jv_detail.against_account) |
| 557 | new_row.is_advance = cstr(jv_detail.is_advance) |
| 558 | new_row.docstatus = 1 |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 559 | |
| 560 | # will work as update after submit |
Anuja Pawar | 3e404f1 | 2021-08-31 18:59:29 +0530 | [diff] [blame] | 561 | journal_entry.flags.ignore_validate_update_after_submit = True |
| 562 | if not do_not_save: |
| 563 | journal_entry.save(ignore_permissions=True) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 564 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 565 | |
Rohit Waghchaure | a2408a6 | 2019-06-24 01:52:48 +0530 | [diff] [blame] | 566 | def update_reference_in_payment_entry(d, payment_entry, do_not_save=False): |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 567 | reference_details = { |
| 568 | "reference_doctype": d.against_voucher_type, |
| 569 | "reference_name": d.against_voucher, |
| 570 | "total_amount": d.grand_total, |
| 571 | "outstanding_amount": d.outstanding_amount, |
| 572 | "allocated_amount": d.allocated_amount, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 573 | "exchange_rate": d.exchange_rate |
| 574 | if not d.exchange_gain_loss |
| 575 | else payment_entry.get_exchange_rate(), |
| 576 | "exchange_gain_loss": d.exchange_gain_loss, # only populated from invoice in case of advance allocation |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 577 | } |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 578 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 579 | if d.voucher_detail_no: |
| 580 | existing_row = payment_entry.get("references", {"name": d["voucher_detail_no"]})[0] |
| 581 | original_row = existing_row.as_dict().copy() |
| 582 | existing_row.update(reference_details) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 583 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 584 | if d.allocated_amount < original_row.allocated_amount: |
| 585 | new_row = payment_entry.append("references") |
| 586 | new_row.docstatus = 1 |
Achilles Rasquinha | efb7319 | 2018-05-23 01:01:24 -0500 | [diff] [blame] | 587 | for field in list(reference_details): |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 588 | new_row.set(field, original_row[field]) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 589 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 590 | new_row.allocated_amount = original_row.allocated_amount - d.allocated_amount |
| 591 | else: |
| 592 | new_row = payment_entry.append("references") |
| 593 | new_row.docstatus = 1 |
| 594 | new_row.update(reference_details) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 595 | |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 596 | payment_entry.flags.ignore_validate_update_after_submit = True |
Nabin Hait | 05aefbb | 2016-06-28 19:42:19 +0530 | [diff] [blame] | 597 | payment_entry.setup_party_account_field() |
Nabin Hait | 1991c7b | 2016-06-27 20:09:05 +0530 | [diff] [blame] | 598 | payment_entry.set_missing_values() |
Nabin Hait | 28a0528 | 2016-06-27 17:41:39 +0530 | [diff] [blame] | 599 | payment_entry.set_amounts() |
Rohit Waghchaure | a2408a6 | 2019-06-24 01:52:48 +0530 | [diff] [blame] | 600 | |
| 601 | if d.difference_amount and d.difference_account: |
Saqib | a20999c | 2021-07-12 14:33:23 +0530 | [diff] [blame] | 602 | account_details = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 603 | "account": d.difference_account, |
| 604 | "cost_center": payment_entry.cost_center |
| 605 | or frappe.get_cached_value("Company", payment_entry.company, "cost_center"), |
Saqib | a20999c | 2021-07-12 14:33:23 +0530 | [diff] [blame] | 606 | } |
| 607 | if d.difference_amount: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 608 | account_details["amount"] = d.difference_amount |
Saqib | a20999c | 2021-07-12 14:33:23 +0530 | [diff] [blame] | 609 | |
| 610 | payment_entry.set_gain_or_loss(account_details=account_details) |
Rohit Waghchaure | a2408a6 | 2019-06-24 01:52:48 +0530 | [diff] [blame] | 611 | |
| 612 | if not do_not_save: |
| 613 | payment_entry.save(ignore_permissions=True) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 614 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 615 | |
Nabin Hait | 297d74a | 2016-11-23 15:58:51 +0530 | [diff] [blame] | 616 | def unlink_ref_doc_from_payment_entries(ref_doc): |
| 617 | remove_ref_doc_link_from_jv(ref_doc.doctype, ref_doc.name) |
| 618 | remove_ref_doc_link_from_pe(ref_doc.doctype, ref_doc.name) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 619 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 620 | frappe.db.sql( |
| 621 | """update `tabGL Entry` |
Nabin Hait | e0cc87d | 2016-07-21 18:30:03 +0530 | [diff] [blame] | 622 | set against_voucher_type=null, against_voucher=null, |
| 623 | modified=%s, modified_by=%s |
| 624 | where against_voucher_type=%s and against_voucher=%s |
| 625 | and voucher_no != ifnull(against_voucher, '')""", |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 626 | (now(), frappe.session.user, ref_doc.doctype, ref_doc.name), |
| 627 | ) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 628 | |
Nabin Hait | 297d74a | 2016-11-23 15:58:51 +0530 | [diff] [blame] | 629 | if ref_doc.doctype in ("Sales Invoice", "Purchase Invoice"): |
| 630 | ref_doc.set("advances", []) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 631 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 632 | frappe.db.sql( |
| 633 | """delete from `tab{0} Advance` where parent = %s""".format(ref_doc.doctype), ref_doc.name |
| 634 | ) |
| 635 | |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 636 | |
Nabin Hait | e0cc87d | 2016-07-21 18:30:03 +0530 | [diff] [blame] | 637 | def remove_ref_doc_link_from_jv(ref_type, ref_no): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 638 | linked_jv = frappe.db.sql_list( |
| 639 | """select parent from `tabJournal Entry Account` |
| 640 | where reference_type=%s and reference_name=%s and docstatus < 2""", |
| 641 | (ref_type, ref_no), |
| 642 | ) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 643 | |
| 644 | if linked_jv: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 645 | frappe.db.sql( |
| 646 | """update `tabJournal Entry Account` |
Rushabh Mehta | 1828c12 | 2015-08-10 17:04:07 +0530 | [diff] [blame] | 647 | set reference_type=null, reference_name = null, |
Nabin Hait | dc15b4f | 2014-01-20 16:48:49 +0530 | [diff] [blame] | 648 | modified=%s, modified_by=%s |
Rushabh Mehta | 1828c12 | 2015-08-10 17:04:07 +0530 | [diff] [blame] | 649 | where reference_type=%s and reference_name=%s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 650 | and docstatus < 2""", |
| 651 | (now(), frappe.session.user, ref_type, ref_no), |
| 652 | ) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 653 | |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 654 | frappe.msgprint(_("Journal Entries {0} are un-linked").format("\n".join(linked_jv))) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 655 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 656 | |
Nabin Hait | e0cc87d | 2016-07-21 18:30:03 +0530 | [diff] [blame] | 657 | def remove_ref_doc_link_from_pe(ref_type, ref_no): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 658 | linked_pe = frappe.db.sql_list( |
| 659 | """select parent from `tabPayment Entry Reference` |
| 660 | where reference_doctype=%s and reference_name=%s and docstatus < 2""", |
| 661 | (ref_type, ref_no), |
| 662 | ) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 663 | |
Nabin Hait | e0cc87d | 2016-07-21 18:30:03 +0530 | [diff] [blame] | 664 | if linked_pe: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 665 | frappe.db.sql( |
| 666 | """update `tabPayment Entry Reference` |
Nabin Hait | e0cc87d | 2016-07-21 18:30:03 +0530 | [diff] [blame] | 667 | set allocated_amount=0, modified=%s, modified_by=%s |
| 668 | where reference_doctype=%s and reference_name=%s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 669 | and docstatus < 2""", |
| 670 | (now(), frappe.session.user, ref_type, ref_no), |
| 671 | ) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 672 | |
Nabin Hait | e0cc87d | 2016-07-21 18:30:03 +0530 | [diff] [blame] | 673 | for pe in linked_pe: |
Deepesh Garg | c5276f3 | 2021-08-01 17:48:50 +0530 | [diff] [blame] | 674 | try: |
| 675 | pe_doc = frappe.get_doc("Payment Entry", pe) |
Deepesh Garg | 7141860 | 2021-08-10 22:21:28 +0530 | [diff] [blame] | 676 | pe_doc.set_amounts() |
Deepesh Garg | b516239 | 2021-08-10 14:52:24 +0530 | [diff] [blame] | 677 | pe_doc.clear_unallocated_reference_document_rows() |
| 678 | pe_doc.validate_payment_type_with_outstanding() |
Deepesh Garg | c5276f3 | 2021-08-01 17:48:50 +0530 | [diff] [blame] | 679 | except Exception as e: |
| 680 | msg = _("There were issues unlinking payment entry {0}.").format(pe_doc.name) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 681 | msg += "<br>" |
Deepesh Garg | 188bba8 | 2021-08-10 14:04:31 +0530 | [diff] [blame] | 682 | msg += _("Please cancel payment entry manually first") |
Deepesh Garg | 7141860 | 2021-08-10 22:21:28 +0530 | [diff] [blame] | 683 | frappe.throw(msg, exc=PaymentEntryUnlinkError, title=_("Payment Unlink Error")) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 684 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 685 | frappe.db.sql( |
| 686 | """update `tabPayment Entry` set total_allocated_amount=%s, |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 687 | base_total_allocated_amount=%s, unallocated_amount=%s, modified=%s, modified_by=%s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 688 | where name=%s""", |
| 689 | ( |
| 690 | pe_doc.total_allocated_amount, |
| 691 | pe_doc.base_total_allocated_amount, |
| 692 | pe_doc.unallocated_amount, |
| 693 | now(), |
| 694 | frappe.session.user, |
| 695 | pe, |
| 696 | ), |
| 697 | ) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 698 | |
Suraj Shetty | 48e9bc3 | 2020-01-29 15:06:18 +0530 | [diff] [blame] | 699 | frappe.msgprint(_("Payment Entries {0} are un-linked").format("\n".join(linked_pe))) |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 700 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 701 | |
Rushabh Mehta | 793ba6b | 2014-02-14 15:47:51 +0530 | [diff] [blame] | 702 | @frappe.whitelist() |
Rohit Waghchaure | 2a14f25 | 2021-07-30 12:36:35 +0530 | [diff] [blame] | 703 | def get_company_default(company, fieldname, ignore_validation=False): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 704 | value = frappe.get_cached_value("Company", company, fieldname) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 705 | |
Rohit Waghchaure | 2a14f25 | 2021-07-30 12:36:35 +0530 | [diff] [blame] | 706 | if not ignore_validation and not value: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 707 | throw( |
| 708 | _("Please set default {0} in Company {1}").format( |
| 709 | frappe.get_meta("Company").get_label(fieldname), company |
| 710 | ) |
| 711 | ) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 712 | |
Nabin Hait | 0fc2454 | 2013-03-25 11:06:00 +0530 | [diff] [blame] | 713 | return value |
Nabin Hait | 8b509f5 | 2013-05-28 16:52:30 +0530 | [diff] [blame] | 714 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 715 | |
Nabin Hait | 8b509f5 | 2013-05-28 16:52:30 +0530 | [diff] [blame] | 716 | def fix_total_debit_credit(): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 717 | vouchers = frappe.db.sql( |
| 718 | """select voucher_type, voucher_no, |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 719 | sum(debit) - sum(credit) as diff |
| 720 | from `tabGL Entry` |
Nabin Hait | 8b509f5 | 2013-05-28 16:52:30 +0530 | [diff] [blame] | 721 | group by voucher_type, voucher_no |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 722 | having sum(debit) != sum(credit)""", |
| 723 | as_dict=1, |
| 724 | ) |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 725 | |
Nabin Hait | 8b509f5 | 2013-05-28 16:52:30 +0530 | [diff] [blame] | 726 | for d in vouchers: |
| 727 | if abs(d.diff) > 0: |
| 728 | dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit" |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 729 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 730 | frappe.db.sql( |
| 731 | """update `tabGL Entry` set %s = %s + %s |
| 732 | where voucher_type = %s and voucher_no = %s and %s > 0 limit 1""" |
| 733 | % (dr_or_cr, dr_or_cr, "%s", "%s", "%s", dr_or_cr), |
| 734 | (d.diff, d.voucher_type, d.voucher_no), |
| 735 | ) |
| 736 | |
Anand Doshi | cd71e1d | 2014-04-08 13:53:35 +0530 | [diff] [blame] | 737 | |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 738 | def get_currency_precision(): |
Nabin Hait | 9b20e07 | 2017-04-25 12:10:24 +0530 | [diff] [blame] | 739 | precision = cint(frappe.db.get_default("currency_precision")) |
| 740 | if not precision: |
| 741 | number_format = frappe.db.get_default("number_format") or "#,###.##" |
| 742 | precision = get_number_format_info(number_format)[2] |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 743 | |
Nabin Hait | 9b20e07 | 2017-04-25 12:10:24 +0530 | [diff] [blame] | 744 | return precision |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 745 | |
Nabin Hait | 9a0c46f | 2014-08-07 15:10:05 +0530 | [diff] [blame] | 746 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 747 | def get_stock_rbnb_difference(posting_date, company): |
| 748 | stock_items = frappe.db.sql_list( |
| 749 | """select distinct item_code |
| 750 | from `tabStock Ledger Entry` where company=%s""", |
| 751 | company, |
| 752 | ) |
| 753 | |
| 754 | pr_valuation_amount = frappe.db.sql( |
| 755 | """ |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 756 | select sum(pr_item.valuation_rate * pr_item.qty * pr_item.conversion_factor) |
Nabin Hait | 9a0c46f | 2014-08-07 15:10:05 +0530 | [diff] [blame] | 757 | from `tabPurchase Receipt Item` pr_item, `tabPurchase Receipt` pr |
Suraj Shetty | 084b0b3 | 2018-05-26 09:12:59 +0530 | [diff] [blame] | 758 | where pr.name = pr_item.parent and pr.docstatus=1 and pr.company=%s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 759 | and pr.posting_date <= %s and pr_item.item_code in (%s)""" |
| 760 | % ("%s", "%s", ", ".join(["%s"] * len(stock_items))), |
| 761 | tuple([company, posting_date] + stock_items), |
| 762 | )[0][0] |
Nabin Hait | 9a0c46f | 2014-08-07 15:10:05 +0530 | [diff] [blame] | 763 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 764 | pi_valuation_amount = frappe.db.sql( |
| 765 | """ |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 766 | select sum(pi_item.valuation_rate * pi_item.qty * pi_item.conversion_factor) |
Nabin Hait | 9a0c46f | 2014-08-07 15:10:05 +0530 | [diff] [blame] | 767 | from `tabPurchase Invoice Item` pi_item, `tabPurchase Invoice` pi |
Suraj Shetty | 084b0b3 | 2018-05-26 09:12:59 +0530 | [diff] [blame] | 768 | where pi.name = pi_item.parent and pi.docstatus=1 and pi.company=%s |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 769 | and pi.posting_date <= %s and pi_item.item_code in (%s)""" |
| 770 | % ("%s", "%s", ", ".join(["%s"] * len(stock_items))), |
| 771 | tuple([company, posting_date] + stock_items), |
| 772 | )[0][0] |
Nabin Hait | 9a0c46f | 2014-08-07 15:10:05 +0530 | [diff] [blame] | 773 | |
| 774 | # Balance should be |
| 775 | stock_rbnb = flt(pr_valuation_amount, 2) - flt(pi_valuation_amount, 2) |
| 776 | |
| 777 | # Balance as per system |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 778 | stock_rbnb_account = "Stock Received But Not Billed - " + frappe.get_cached_value( |
| 779 | "Company", company, "abbr" |
| 780 | ) |
Nabin Hait | c0e3b1a | 2015-09-04 13:26:23 +0530 | [diff] [blame] | 781 | sys_bal = get_balance_on(stock_rbnb_account, posting_date, in_account_currency=False) |
Nabin Hait | 9a0c46f | 2014-08-07 15:10:05 +0530 | [diff] [blame] | 782 | |
| 783 | # Amount should be credited |
| 784 | return flt(stock_rbnb) + flt(sys_bal) |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 785 | |
tunde | c4b0d17 | 2017-09-26 00:48:30 +0100 | [diff] [blame] | 786 | |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 787 | def get_held_invoices(party_type, party): |
| 788 | """ |
| 789 | Returns a list of names Purchase Invoices for the given party that are on hold |
| 790 | """ |
| 791 | held_invoices = None |
| 792 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 793 | if party_type == "Supplier": |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 794 | held_invoices = frappe.db.sql( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 795 | "select name from `tabPurchase Invoice` where release_date IS NOT NULL and release_date > CURDATE()", |
| 796 | as_dict=1, |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 797 | ) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 798 | held_invoices = set(d["name"] for d in held_invoices) |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 799 | |
| 800 | return held_invoices |
| 801 | |
| 802 | |
Rohit Waghchaure | 0f065d5 | 2019-05-02 00:06:04 +0530 | [diff] [blame] | 803 | def get_outstanding_invoices(party_type, party, account, condition=None, filters=None): |
Anand Doshi | d40d1e9 | 2015-11-12 17:05:29 +0530 | [diff] [blame] | 804 | outstanding_invoices = [] |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 805 | precision = frappe.get_precision("Sales Invoice", "outstanding_amount") or 2 |
Anand Doshi | d40d1e9 | 2015-11-12 17:05:29 +0530 | [diff] [blame] | 806 | |
Nabin Hait | 9db9edc | 2019-11-19 18:44:32 +0530 | [diff] [blame] | 807 | if account: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 808 | root_type, account_type = frappe.get_cached_value( |
| 809 | "Account", account, ["root_type", "account_type"] |
| 810 | ) |
Nabin Hait | 9db9edc | 2019-11-19 18:44:32 +0530 | [diff] [blame] | 811 | party_account_type = "Receivable" if root_type == "Asset" else "Payable" |
Saqib | 9291df4 | 2020-01-24 16:22:49 +0530 | [diff] [blame] | 812 | party_account_type = account_type or party_account_type |
Nabin Hait | 9db9edc | 2019-11-19 18:44:32 +0530 | [diff] [blame] | 813 | else: |
| 814 | party_account_type = erpnext.get_party_account_type(party_type) |
| 815 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 816 | if party_account_type == "Receivable": |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 817 | dr_or_cr = "debit_in_account_currency - credit_in_account_currency" |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 818 | payment_dr_or_cr = "credit_in_account_currency - debit_in_account_currency" |
Anand Doshi | d40d1e9 | 2015-11-12 17:05:29 +0530 | [diff] [blame] | 819 | else: |
Anand Doshi | 602e825 | 2015-11-16 19:05:46 +0530 | [diff] [blame] | 820 | dr_or_cr = "credit_in_account_currency - debit_in_account_currency" |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 821 | payment_dr_or_cr = "debit_in_account_currency - credit_in_account_currency" |
Anand Doshi | d40d1e9 | 2015-11-12 17:05:29 +0530 | [diff] [blame] | 822 | |
tundebabzy | ad08d4c | 2018-05-16 07:01:41 +0100 | [diff] [blame] | 823 | held_invoices = get_held_invoices(party_type, party) |
| 824 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 825 | invoice_list = frappe.db.sql( |
| 826 | """ |
Nabin Hait | e4bbb69 | 2016-07-04 16:16:24 +0530 | [diff] [blame] | 827 | select |
Rohit Waghchaure | 0f065d5 | 2019-05-02 00:06:04 +0530 | [diff] [blame] | 828 | voucher_no, voucher_type, posting_date, due_date, |
Deepesh Garg | c7eadfc | 2020-07-22 17:59:37 +0530 | [diff] [blame] | 829 | ifnull(sum({dr_or_cr}), 0) as invoice_amount, |
| 830 | account_currency as currency |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 831 | from |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 832 | `tabGL Entry` |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 833 | where |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 834 | party_type = %(party_type)s and party = %(party)s |
Nabin Hait | e4bbb69 | 2016-07-04 16:16:24 +0530 | [diff] [blame] | 835 | and account = %(account)s and {dr_or_cr} > 0 |
aakvatech | 38ccf82 | 2020-10-03 19:41:38 +0300 | [diff] [blame] | 836 | and is_cancelled=0 |
Anand Doshi | d40d1e9 | 2015-11-12 17:05:29 +0530 | [diff] [blame] | 837 | {condition} |
| 838 | and ((voucher_type = 'Journal Entry' |
Nabin Hait | 56548cb | 2016-07-01 15:58:39 +0530 | [diff] [blame] | 839 | and (against_voucher = '' or against_voucher is null)) |
| 840 | or (voucher_type not in ('Journal Entry', 'Payment Entry'))) |
Nabin Hait | a5cc84f | 2017-12-21 11:37:18 +0530 | [diff] [blame] | 841 | group by voucher_type, voucher_no |
Nabin Hait | 34c551d | 2019-07-03 10:34:31 +0530 | [diff] [blame] | 842 | order by posting_date, name""".format( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 843 | dr_or_cr=dr_or_cr, condition=condition or "" |
| 844 | ), |
| 845 | { |
Anand Doshi | d40d1e9 | 2015-11-12 17:05:29 +0530 | [diff] [blame] | 846 | "party_type": party_type, |
| 847 | "party": party, |
| 848 | "account": account, |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 849 | }, |
| 850 | as_dict=True, |
| 851 | ) |
Ankit Javalkar | 8e7ca41 | 2014-09-12 15:18:53 +0530 | [diff] [blame] | 852 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 853 | payment_entries = frappe.db.sql( |
| 854 | """ |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 855 | select against_voucher_type, against_voucher, |
| 856 | ifnull(sum({payment_dr_or_cr}), 0) as payment_amount |
| 857 | from `tabGL Entry` |
| 858 | where party_type = %(party_type)s and party = %(party)s |
| 859 | and account = %(account)s |
| 860 | and {payment_dr_or_cr} > 0 |
| 861 | and against_voucher is not null and against_voucher != '' |
aakvatech | 38ccf82 | 2020-10-03 19:41:38 +0300 | [diff] [blame] | 862 | and is_cancelled=0 |
Rohit Waghchaure | 0f065d5 | 2019-05-02 00:06:04 +0530 | [diff] [blame] | 863 | group by against_voucher_type, against_voucher |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 864 | """.format( |
| 865 | payment_dr_or_cr=payment_dr_or_cr |
| 866 | ), |
| 867 | {"party_type": party_type, "party": party, "account": account}, |
| 868 | as_dict=True, |
| 869 | ) |
tunde | c4b0d17 | 2017-09-26 00:48:30 +0100 | [diff] [blame] | 870 | |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 871 | pe_map = frappe._dict() |
| 872 | for d in payment_entries: |
| 873 | pe_map.setdefault((d.against_voucher_type, d.against_voucher), d.payment_amount) |
| 874 | |
| 875 | for d in invoice_list: |
| 876 | payment_amount = pe_map.get((d.voucher_type, d.voucher_no), 0) |
| 877 | outstanding_amount = flt(d.invoice_amount - payment_amount, precision) |
| 878 | if outstanding_amount > 0.5 / (10**precision): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 879 | if ( |
| 880 | filters |
| 881 | and filters.get("outstanding_amt_greater_than") |
| 882 | and not ( |
| 883 | outstanding_amount >= filters.get("outstanding_amt_greater_than") |
| 884 | and outstanding_amount <= filters.get("outstanding_amt_less_than") |
| 885 | ) |
| 886 | ): |
Rohit Waghchaure | 0f065d5 | 2019-05-02 00:06:04 +0530 | [diff] [blame] | 887 | continue |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 888 | |
Rohit Waghchaure | 0f065d5 | 2019-05-02 00:06:04 +0530 | [diff] [blame] | 889 | if not d.voucher_type == "Purchase Invoice" or d.voucher_no not in held_invoices: |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 890 | outstanding_invoices.append( |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 891 | frappe._dict( |
| 892 | { |
| 893 | "voucher_no": d.voucher_no, |
| 894 | "voucher_type": d.voucher_type, |
| 895 | "posting_date": d.posting_date, |
| 896 | "invoice_amount": flt(d.invoice_amount), |
| 897 | "payment_amount": payment_amount, |
| 898 | "outstanding_amount": outstanding_amount, |
| 899 | "due_date": d.due_date, |
| 900 | "currency": d.currency, |
| 901 | } |
| 902 | ) |
Nabin Hait | ac18498 | 2019-02-04 21:13:43 +0530 | [diff] [blame] | 903 | ) |
Rushabh Mehta | e9d9b8e | 2016-12-15 11:27:35 +0530 | [diff] [blame] | 904 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 905 | outstanding_invoices = sorted( |
| 906 | outstanding_invoices, key=lambda k: k["due_date"] or getdate(nowdate()) |
| 907 | ) |
Anand Doshi | d40d1e9 | 2015-11-12 17:05:29 +0530 | [diff] [blame] | 908 | return outstanding_invoices |
Saurabh | 3a26829 | 2016-02-22 15:18:40 +0530 | [diff] [blame] | 909 | |
| 910 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 911 | def get_account_name( |
| 912 | account_type=None, root_type=None, is_group=None, account_currency=None, company=None |
| 913 | ): |
Saurabh | 3a26829 | 2016-02-22 15:18:40 +0530 | [diff] [blame] | 914 | """return account based on matching conditions""" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 915 | return frappe.db.get_value( |
| 916 | "Account", |
| 917 | { |
| 918 | "account_type": account_type or "", |
| 919 | "root_type": root_type or "", |
| 920 | "is_group": is_group or 0, |
| 921 | "account_currency": account_currency or frappe.defaults.get_defaults().currency, |
| 922 | "company": company or frappe.defaults.get_defaults().company, |
| 923 | }, |
| 924 | "name", |
| 925 | ) |
| 926 | |
Saurabh | a9ba734 | 2016-06-21 12:33:12 +0530 | [diff] [blame] | 927 | |
| 928 | @frappe.whitelist() |
| 929 | def get_companies(): |
| 930 | """get a list of companies based on permission""" |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 931 | return [d.name for d in frappe.get_list("Company", fields=["name"], order_by="name")] |
| 932 | |
Saurabh | a9ba734 | 2016-06-21 12:33:12 +0530 | [diff] [blame] | 933 | |
| 934 | @frappe.whitelist() |
Rushabh Mehta | 4313326 | 2017-11-10 18:52:21 +0530 | [diff] [blame] | 935 | def get_children(doctype, parent, company, is_root=False): |
Nabin Hait | af98f5d | 2018-04-02 10:14:32 +0530 | [diff] [blame] | 936 | from erpnext.accounts.report.financial_statements import sort_accounts |
Rushabh Mehta | d50da78 | 2017-07-28 11:39:01 +0530 | [diff] [blame] | 937 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 938 | parent_fieldname = "parent_" + doctype.lower().replace(" ", "_") |
| 939 | fields = ["name as value", "is_group as expandable"] |
| 940 | filters = [["docstatus", "<", 2]] |
Suraj Shetty | fbb6b3d | 2018-05-16 13:53:31 +0530 | [diff] [blame] | 941 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 942 | filters.append(['ifnull(`{0}`,"")'.format(parent_fieldname), "=", "" if is_root else parent]) |
Suraj Shetty | 084b0b3 | 2018-05-26 09:12:59 +0530 | [diff] [blame] | 943 | |
Rushabh Mehta | 4313326 | 2017-11-10 18:52:21 +0530 | [diff] [blame] | 944 | if is_root: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 945 | fields += ["root_type", "report_type", "account_currency"] if doctype == "Account" else [] |
| 946 | filters.append(["company", "=", company]) |
Suraj Shetty | 084b0b3 | 2018-05-26 09:12:59 +0530 | [diff] [blame] | 947 | |
Saurabh | a9ba734 | 2016-06-21 12:33:12 +0530 | [diff] [blame] | 948 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 949 | fields += ["root_type", "account_currency"] if doctype == "Account" else [] |
| 950 | fields += [parent_fieldname + " as parent"] |
Suraj Shetty | 084b0b3 | 2018-05-26 09:12:59 +0530 | [diff] [blame] | 951 | |
| 952 | acc = frappe.get_list(doctype, fields=fields, filters=filters) |
Saurabh | a9ba734 | 2016-06-21 12:33:12 +0530 | [diff] [blame] | 953 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 954 | if doctype == "Account": |
Nabin Hait | af98f5d | 2018-04-02 10:14:32 +0530 | [diff] [blame] | 955 | sort_accounts(acc, is_root, key="value") |
Saurabh | a9ba734 | 2016-06-21 12:33:12 +0530 | [diff] [blame] | 956 | |
| 957 | return acc |
Saurabh | b835fef | 2016-09-09 11:19:22 +0530 | [diff] [blame] | 958 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 959 | |
Saqib | 9051735 | 2021-10-04 11:44:46 +0530 | [diff] [blame] | 960 | @frappe.whitelist() |
| 961 | def get_account_balances(accounts, company): |
| 962 | |
Ankush Menat | 8fe5feb | 2021-11-04 19:48:32 +0530 | [diff] [blame] | 963 | if isinstance(accounts, str): |
Saqib | 9051735 | 2021-10-04 11:44:46 +0530 | [diff] [blame] | 964 | accounts = loads(accounts) |
| 965 | |
| 966 | if not accounts: |
| 967 | return [] |
| 968 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 969 | company_currency = frappe.get_cached_value("Company", company, "default_currency") |
Saqib | 9051735 | 2021-10-04 11:44:46 +0530 | [diff] [blame] | 970 | |
| 971 | for account in accounts: |
| 972 | account["company_currency"] = company_currency |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 973 | account["balance"] = flt( |
| 974 | get_balance_on(account["value"], in_account_currency=False, company=company) |
| 975 | ) |
Saqib | 9051735 | 2021-10-04 11:44:46 +0530 | [diff] [blame] | 976 | if account["account_currency"] and account["account_currency"] != company_currency: |
| 977 | account["balance_in_account_currency"] = flt(get_balance_on(account["value"], company=company)) |
| 978 | |
| 979 | return accounts |
| 980 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 981 | |
Mangesh-Khairnar | 97ab96c | 2020-09-22 12:58:32 +0530 | [diff] [blame] | 982 | def create_payment_gateway_account(gateway, payment_channel="Email"): |
Deepesh Garg | a72589c | 2021-05-29 23:54:51 +0530 | [diff] [blame] | 983 | from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account |
Saurabh | b835fef | 2016-09-09 11:19:22 +0530 | [diff] [blame] | 984 | |
| 985 | company = frappe.db.get_value("Global Defaults", None, "default_company") |
| 986 | if not company: |
| 987 | return |
| 988 | |
| 989 | # NOTE: we translate Payment Gateway account name because that is going to be used by the end user |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 990 | bank_account = frappe.db.get_value( |
| 991 | "Account", |
| 992 | {"account_name": _(gateway), "company": company}, |
| 993 | ["name", "account_currency"], |
| 994 | as_dict=1, |
| 995 | ) |
Saurabh | b835fef | 2016-09-09 11:19:22 +0530 | [diff] [blame] | 996 | |
| 997 | if not bank_account: |
| 998 | # check for untranslated one |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 999 | bank_account = frappe.db.get_value( |
| 1000 | "Account", |
| 1001 | {"account_name": gateway, "company": company}, |
| 1002 | ["name", "account_currency"], |
| 1003 | as_dict=1, |
| 1004 | ) |
Saurabh | b835fef | 2016-09-09 11:19:22 +0530 | [diff] [blame] | 1005 | |
| 1006 | if not bank_account: |
| 1007 | # try creating one |
| 1008 | bank_account = create_bank_account({"company_name": company, "bank_account": _(gateway)}) |
| 1009 | |
| 1010 | if not bank_account: |
| 1011 | frappe.msgprint(_("Payment Gateway Account not created, please create one manually.")) |
| 1012 | return |
| 1013 | |
| 1014 | # if payment gateway account exists, return |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1015 | if frappe.db.exists( |
| 1016 | "Payment Gateway Account", |
| 1017 | {"payment_gateway": gateway, "currency": bank_account.account_currency}, |
| 1018 | ): |
Saurabh | b835fef | 2016-09-09 11:19:22 +0530 | [diff] [blame] | 1019 | return |
| 1020 | |
| 1021 | try: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1022 | frappe.get_doc( |
| 1023 | { |
| 1024 | "doctype": "Payment Gateway Account", |
| 1025 | "is_default": 1, |
| 1026 | "payment_gateway": gateway, |
| 1027 | "payment_account": bank_account.name, |
| 1028 | "currency": bank_account.account_currency, |
| 1029 | "payment_channel": payment_channel, |
| 1030 | } |
| 1031 | ).insert(ignore_permissions=True, ignore_if_duplicate=True) |
Saurabh | b835fef | 2016-09-09 11:19:22 +0530 | [diff] [blame] | 1032 | |
| 1033 | except frappe.DuplicateEntryError: |
| 1034 | # already exists, due to a reinstall? |
cclauss | 6848708 | 2017-07-27 07:08:35 +0200 | [diff] [blame] | 1035 | pass |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1036 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1037 | |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1038 | @frappe.whitelist() |
Deepesh Garg | 817cbc4 | 2020-06-15 12:07:04 +0530 | [diff] [blame] | 1039 | def update_cost_center(docname, cost_center_name, cost_center_number, company, merge): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1040 | """ |
| 1041 | Renames the document by adding the number as a prefix to the current name and updates |
| 1042 | all transaction where it was present. |
| 1043 | """ |
Saqib | a877987 | 2020-04-30 11:28:43 +0530 | [diff] [blame] | 1044 | validate_field_number("Cost Center", docname, cost_center_number, company, "cost_center_number") |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1045 | |
Saqib | a877987 | 2020-04-30 11:28:43 +0530 | [diff] [blame] | 1046 | if cost_center_number: |
| 1047 | frappe.db.set_value("Cost Center", docname, "cost_center_number", cost_center_number.strip()) |
| 1048 | else: |
| 1049 | frappe.db.set_value("Cost Center", docname, "cost_center_number", "") |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1050 | |
Saqib | a877987 | 2020-04-30 11:28:43 +0530 | [diff] [blame] | 1051 | frappe.db.set_value("Cost Center", docname, "cost_center_name", cost_center_name.strip()) |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1052 | |
Saqib | a877987 | 2020-04-30 11:28:43 +0530 | [diff] [blame] | 1053 | new_name = get_autoname_with_number(cost_center_number, cost_center_name, docname, company) |
| 1054 | if docname != new_name: |
Deepesh Garg | 817cbc4 | 2020-06-15 12:07:04 +0530 | [diff] [blame] | 1055 | frappe.rename_doc("Cost Center", docname, new_name, force=1, merge=merge) |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1056 | return new_name |
| 1057 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1058 | |
Saqib | a877987 | 2020-04-30 11:28:43 +0530 | [diff] [blame] | 1059 | def validate_field_number(doctype_name, docname, number_value, company, field_name): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1060 | """Validate if the number entered isn't already assigned to some other document.""" |
Zlash65 | 1aeca1d | 2018-07-06 17:15:33 +0530 | [diff] [blame] | 1061 | if number_value: |
Saqib | a877987 | 2020-04-30 11:28:43 +0530 | [diff] [blame] | 1062 | filters = {field_name: number_value, "name": ["!=", docname]} |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1063 | if company: |
Saqib | a877987 | 2020-04-30 11:28:43 +0530 | [diff] [blame] | 1064 | filters["company"] = company |
| 1065 | |
| 1066 | doctype_with_same_number = frappe.db.get_value(doctype_name, filters) |
| 1067 | |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1068 | if doctype_with_same_number: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1069 | frappe.throw( |
| 1070 | _("{0} Number {1} is already used in {2} {3}").format( |
| 1071 | doctype_name, number_value, doctype_name.lower(), doctype_with_same_number |
| 1072 | ) |
| 1073 | ) |
| 1074 | |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1075 | |
Zarrar | 17705f9 | 2018-07-06 14:44:44 +0530 | [diff] [blame] | 1076 | def get_autoname_with_number(number_value, doc_title, name, company): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1077 | """append title with prefix as number and suffix as company's abbreviation separated by '-'""" |
Zarrar | 88a90ff | 2018-06-11 11:21:17 +0530 | [diff] [blame] | 1078 | if name: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1079 | name_split = name.split("-") |
| 1080 | parts = [doc_title.strip(), name_split[len(name_split) - 1].strip()] |
Zarrar | 4417590 | 2018-06-08 16:35:21 +0530 | [diff] [blame] | 1081 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1082 | abbr = frappe.get_cached_value("Company", company, ["abbr"], as_dict=True) |
Zarrar | 88a90ff | 2018-06-11 11:21:17 +0530 | [diff] [blame] | 1083 | parts = [doc_title.strip(), abbr.abbr] |
Zlash65 | 1aeca1d | 2018-07-06 17:15:33 +0530 | [diff] [blame] | 1084 | if cstr(number_value).strip(): |
| 1085 | parts.insert(0, cstr(number_value).strip()) |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1086 | return " - ".join(parts) |
| 1087 | |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 1088 | |
| 1089 | @frappe.whitelist() |
| 1090 | def get_coa(doctype, parent, is_root, chart=None): |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1091 | from erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts import ( |
| 1092 | build_tree_from_json, |
| 1093 | ) |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 1094 | |
| 1095 | # add chart to flags to retrieve when called from expand all function |
| 1096 | chart = chart if chart else frappe.flags.chart |
| 1097 | frappe.flags.chart = chart |
| 1098 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1099 | parent = None if parent == _("All Accounts") else parent |
| 1100 | accounts = build_tree_from_json(chart) # returns alist of dict in a tree render-able form |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 1101 | |
| 1102 | # filter out to show data for the selected node only |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1103 | accounts = [d for d in accounts if d["parent_account"] == parent] |
Zarrar | 254ce64 | 2018-06-28 14:15:34 +0530 | [diff] [blame] | 1104 | |
| 1105 | return accounts |
Sanjay Kumar | 1b49f3a | 2018-09-06 13:09:35 +0400 | [diff] [blame] | 1106 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1107 | |
| 1108 | def update_gl_entries_after( |
| 1109 | posting_date, |
| 1110 | posting_time, |
| 1111 | for_warehouses=None, |
| 1112 | for_items=None, |
| 1113 | warehouse_account=None, |
| 1114 | company=None, |
| 1115 | ): |
| 1116 | stock_vouchers = get_future_stock_vouchers( |
| 1117 | posting_date, posting_time, for_warehouses, for_items, company |
| 1118 | ) |
Nabin Hait | 9b178bc | 2021-02-16 14:57:00 +0530 | [diff] [blame] | 1119 | repost_gle_for_stock_vouchers(stock_vouchers, posting_date, company, warehouse_account) |
| 1120 | |
| 1121 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1122 | def repost_gle_for_stock_vouchers( |
| 1123 | stock_vouchers, posting_date, company=None, warehouse_account=None |
| 1124 | ): |
Ankush Menat | 67c2632 | 2022-06-04 14:46:35 +0530 | [diff] [blame] | 1125 | |
| 1126 | from erpnext.accounts.general_ledger import toggle_debit_credit_if_negative |
| 1127 | |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 1128 | if not stock_vouchers: |
| 1129 | return |
| 1130 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1131 | def _delete_gl_entries(voucher_type, voucher_no): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1132 | frappe.db.sql( |
| 1133 | """delete from `tabGL Entry` |
| 1134 | where voucher_type=%s and voucher_no=%s""", |
| 1135 | (voucher_type, voucher_no), |
| 1136 | ) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1137 | |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 1138 | stock_vouchers = sort_stock_vouchers_by_posting_date(stock_vouchers) |
| 1139 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1140 | if not warehouse_account: |
| 1141 | warehouse_account = get_warehouse_account_map(company) |
| 1142 | |
Rohit Waghchaure | bc8c9de | 2021-02-23 17:50:49 +0530 | [diff] [blame] | 1143 | precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit")) or 2 |
| 1144 | |
Nabin Hait | 9b178bc | 2021-02-16 14:57:00 +0530 | [diff] [blame] | 1145 | gle = get_voucherwise_gl_entries(stock_vouchers, posting_date) |
Ankush Menat | eb53a97 | 2022-06-04 18:19:44 +0530 | [diff] [blame] | 1146 | for idx, (voucher_type, voucher_no) in enumerate(stock_vouchers): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1147 | existing_gle = gle.get((voucher_type, voucher_no), []) |
Ankush Menat | 67c2632 | 2022-06-04 14:46:35 +0530 | [diff] [blame] | 1148 | voucher_obj = frappe.get_doc(voucher_type, voucher_no) |
| 1149 | # Some transactions post credit as negative debit, this is handled while posting GLE |
| 1150 | # but while comparing we need to make sure it's flipped so comparisons are accurate |
| 1151 | expected_gle = toggle_debit_credit_if_negative(voucher_obj.get_gl_entries(warehouse_account)) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1152 | if expected_gle: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1153 | if not existing_gle or not compare_existing_and_expected_gle( |
| 1154 | existing_gle, expected_gle, precision |
| 1155 | ): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1156 | _delete_gl_entries(voucher_type, voucher_no) |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1157 | voucher_obj.make_gl_entries(gl_entries=expected_gle, from_repost=True) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1158 | else: |
| 1159 | _delete_gl_entries(voucher_type, voucher_no) |
| 1160 | |
Ankush Menat | eb53a97 | 2022-06-04 18:19:44 +0530 | [diff] [blame] | 1161 | if idx % 20 == 0: |
| 1162 | # Commit every 20 documents to avoid losing progress |
| 1163 | # and reducing memory usage |
| 1164 | frappe.db.commit() |
| 1165 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1166 | |
Ankush Menat | 700e864 | 2022-04-19 13:24:29 +0530 | [diff] [blame] | 1167 | def sort_stock_vouchers_by_posting_date( |
| 1168 | stock_vouchers: List[Tuple[str, str]] |
| 1169 | ) -> List[Tuple[str, str]]: |
| 1170 | sle = frappe.qb.DocType("Stock Ledger Entry") |
| 1171 | voucher_nos = [v[1] for v in stock_vouchers] |
| 1172 | |
| 1173 | sles = ( |
| 1174 | frappe.qb.from_(sle) |
| 1175 | .select(sle.voucher_type, sle.voucher_no, sle.posting_date, sle.posting_time, sle.creation) |
| 1176 | .where((sle.is_cancelled == 0) & (sle.voucher_no.isin(voucher_nos))) |
| 1177 | .groupby(sle.voucher_type, sle.voucher_no) |
| 1178 | ).run(as_dict=True) |
| 1179 | sorted_vouchers = [(sle.voucher_type, sle.voucher_no) for sle in sles] |
| 1180 | |
| 1181 | unknown_vouchers = set(stock_vouchers) - set(sorted_vouchers) |
| 1182 | if unknown_vouchers: |
| 1183 | sorted_vouchers.extend(unknown_vouchers) |
| 1184 | |
| 1185 | return sorted_vouchers |
| 1186 | |
| 1187 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1188 | def get_future_stock_vouchers( |
| 1189 | posting_date, posting_time, for_warehouses=None, for_items=None, company=None |
| 1190 | ): |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1191 | |
| 1192 | values = [] |
| 1193 | condition = "" |
| 1194 | if for_items: |
| 1195 | condition += " and item_code in ({})".format(", ".join(["%s"] * len(for_items))) |
| 1196 | values += for_items |
| 1197 | |
| 1198 | if for_warehouses: |
| 1199 | condition += " and warehouse in ({})".format(", ".join(["%s"] * len(for_warehouses))) |
| 1200 | values += for_warehouses |
| 1201 | |
rohitwaghchaure | d60ff83 | 2021-02-16 09:12:27 +0530 | [diff] [blame] | 1202 | if company: |
Nabin Hait | 9b178bc | 2021-02-16 14:57:00 +0530 | [diff] [blame] | 1203 | condition += " and company = %s" |
rohitwaghchaure | d60ff83 | 2021-02-16 09:12:27 +0530 | [diff] [blame] | 1204 | values.append(company) |
| 1205 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1206 | future_stock_vouchers = frappe.db.sql( |
| 1207 | """select distinct sle.voucher_type, sle.voucher_no |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1208 | from `tabStock Ledger Entry` sle |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1209 | where |
| 1210 | timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s) |
| 1211 | and is_cancelled = 0 |
| 1212 | {condition} |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1213 | order by timestamp(sle.posting_date, sle.posting_time) asc, creation asc for update""".format( |
| 1214 | condition=condition |
| 1215 | ), |
| 1216 | tuple([posting_date, posting_time] + values), |
| 1217 | as_dict=True, |
| 1218 | ) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1219 | |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 1220 | return [(d.voucher_type, d.voucher_no) for d in future_stock_vouchers] |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1221 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1222 | |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1223 | def get_voucherwise_gl_entries(future_stock_vouchers, posting_date): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1224 | """Get voucherwise list of GL entries. |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 1225 | |
| 1226 | Only fetches GLE fields required for comparing with new GLE. |
| 1227 | Check compare_existing_and_expected_gle function below. |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 1228 | |
| 1229 | returns: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1230 | Dict[Tuple[voucher_type, voucher_no], List[GL Entries]] |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 1231 | """ |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1232 | gl_entries = {} |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 1233 | if not future_stock_vouchers: |
| 1234 | return gl_entries |
| 1235 | |
| 1236 | voucher_nos = [d[1] for d in future_stock_vouchers] |
| 1237 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1238 | gles = frappe.db.sql( |
| 1239 | """ |
rohitwaghchaure | 058d983 | 2021-09-07 12:14:40 +0530 | [diff] [blame] | 1240 | select name, account, credit, debit, cost_center, project, voucher_type, voucher_no |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 1241 | from `tabGL Entry` |
| 1242 | where |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1243 | posting_date >= %s and voucher_no in (%s)""" |
| 1244 | % ("%s", ", ".join(["%s"] * len(voucher_nos))), |
| 1245 | tuple([posting_date] + voucher_nos), |
| 1246 | as_dict=1, |
| 1247 | ) |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 1248 | |
| 1249 | for d in gles: |
| 1250 | gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d) |
Deepesh Garg | 2a9c5ba | 2020-04-30 10:38:58 +0530 | [diff] [blame] | 1251 | |
Prssanna Desai | 82ddef5 | 2020-06-18 18:18:41 +0530 | [diff] [blame] | 1252 | return gl_entries |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1253 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1254 | |
Rohit Waghchaure | bc8c9de | 2021-02-23 17:50:49 +0530 | [diff] [blame] | 1255 | def compare_existing_and_expected_gle(existing_gle, expected_gle, precision): |
Ankush | 9152715 | 2021-08-11 11:17:50 +0530 | [diff] [blame] | 1256 | if len(existing_gle) != len(expected_gle): |
| 1257 | return False |
| 1258 | |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1259 | matched = True |
| 1260 | for entry in expected_gle: |
| 1261 | account_existed = False |
| 1262 | for e in existing_gle: |
| 1263 | if entry.account == e.account: |
| 1264 | account_existed = True |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1265 | if ( |
| 1266 | entry.account == e.account |
| 1267 | and (not entry.cost_center or not e.cost_center or entry.cost_center == e.cost_center) |
| 1268 | and ( |
| 1269 | flt(entry.debit, precision) != flt(e.debit, precision) |
| 1270 | or flt(entry.credit, precision) != flt(e.credit, precision) |
| 1271 | ) |
| 1272 | ): |
Nabin Hait | a77b8c9 | 2020-12-21 14:45:50 +0530 | [diff] [blame] | 1273 | matched = False |
| 1274 | break |
| 1275 | if not account_existed: |
| 1276 | matched = False |
| 1277 | break |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1278 | return matched |
| 1279 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1280 | |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1281 | def get_stock_accounts(company, voucher_type=None, voucher_no=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1282 | stock_accounts = [ |
| 1283 | d.name |
| 1284 | for d in frappe.db.get_all( |
| 1285 | "Account", {"account_type": "Stock", "company": company, "is_group": 0} |
| 1286 | ) |
| 1287 | ] |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1288 | if voucher_type and voucher_no: |
| 1289 | if voucher_type == "Journal Entry": |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1290 | stock_accounts = [ |
| 1291 | d.account |
| 1292 | for d in frappe.db.get_all( |
| 1293 | "Journal Entry Account", {"parent": voucher_no, "account": ["in", stock_accounts]}, "account" |
| 1294 | ) |
| 1295 | ] |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1296 | |
| 1297 | else: |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1298 | stock_accounts = [ |
| 1299 | d.account |
| 1300 | for d in frappe.db.get_all( |
| 1301 | "GL Entry", |
| 1302 | {"voucher_type": voucher_type, "voucher_no": voucher_no, "account": ["in", stock_accounts]}, |
| 1303 | "account", |
| 1304 | ) |
| 1305 | ] |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1306 | |
| 1307 | return stock_accounts |
| 1308 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1309 | |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1310 | def get_stock_and_account_balance(account=None, posting_date=None, company=None): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1311 | if not posting_date: |
| 1312 | posting_date = nowdate() |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1313 | |
| 1314 | warehouse_account = get_warehouse_account_map(company) |
| 1315 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1316 | account_balance = get_balance_on( |
| 1317 | account, posting_date, in_account_currency=False, ignore_account_permission=True |
| 1318 | ) |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1319 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1320 | related_warehouses = [ |
| 1321 | wh |
| 1322 | for wh, wh_details in warehouse_account.items() |
| 1323 | if wh_details.account == account and not wh_details.is_group |
| 1324 | ] |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1325 | |
| 1326 | total_stock_value = 0.0 |
| 1327 | for warehouse in related_warehouses: |
| 1328 | value = get_stock_value_on(warehouse, posting_date) |
| 1329 | total_stock_value += value |
| 1330 | |
| 1331 | precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency") |
| 1332 | return flt(account_balance, precision), flt(total_stock_value, precision), related_warehouses |
| 1333 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1334 | |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1335 | def get_journal_entry(account, stock_adjustment_account, amount): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1336 | db_or_cr_warehouse_account = ( |
| 1337 | "credit_in_account_currency" if amount < 0 else "debit_in_account_currency" |
| 1338 | ) |
| 1339 | db_or_cr_stock_adjustment_account = ( |
| 1340 | "debit_in_account_currency" if amount < 0 else "credit_in_account_currency" |
| 1341 | ) |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1342 | |
| 1343 | return { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1344 | "accounts": [ |
| 1345 | {"account": account, db_or_cr_warehouse_account: abs(amount)}, |
| 1346 | {"account": stock_adjustment_account, db_or_cr_stock_adjustment_account: abs(amount)}, |
| 1347 | ] |
Nabin Hait | b99c77b | 2020-12-25 18:12:35 +0530 | [diff] [blame] | 1348 | } |
Shadrak Gurupnor | 7433757 | 2021-08-27 18:00:16 +0530 | [diff] [blame] | 1349 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1350 | |
Shadrak Gurupnor | 7433757 | 2021-08-27 18:00:16 +0530 | [diff] [blame] | 1351 | def check_and_delete_linked_reports(report): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 1352 | """Check if reports are referenced in Desktop Icon""" |
| 1353 | icons = frappe.get_all("Desktop Icon", fields=["name"], filters={"_report": report}) |
Shadrak Gurupnor | 7433757 | 2021-08-27 18:00:16 +0530 | [diff] [blame] | 1354 | if icons: |
| 1355 | for icon in icons: |
| 1356 | frappe.delete_doc("Desktop Icon", icon) |
ruthra kumar | 451cf3a | 2022-05-16 14:29:58 +0530 | [diff] [blame] | 1357 | |
| 1358 | |
| 1359 | def create_payment_ledger_entry(gl_entries, cancel=0): |
| 1360 | if gl_entries: |
| 1361 | ple = None |
| 1362 | |
| 1363 | # companies |
| 1364 | account = qb.DocType("Account") |
| 1365 | companies = list(set([x.company for x in gl_entries])) |
| 1366 | |
| 1367 | # receivable/payable account |
| 1368 | accounts_with_types = ( |
| 1369 | qb.from_(account) |
| 1370 | .select(account.name, account.account_type) |
| 1371 | .where( |
| 1372 | (account.account_type.isin(["Receivable", "Payable"]) & (account.company.isin(companies))) |
| 1373 | ) |
| 1374 | .run(as_dict=True) |
| 1375 | ) |
| 1376 | receivable_or_payable_accounts = [y.name for y in accounts_with_types] |
| 1377 | |
| 1378 | def get_account_type(account): |
| 1379 | for entry in accounts_with_types: |
| 1380 | if entry.name == account: |
| 1381 | return entry.account_type |
| 1382 | |
| 1383 | dr_or_cr = 0 |
| 1384 | account_type = None |
| 1385 | for gle in gl_entries: |
| 1386 | if gle.account in receivable_or_payable_accounts: |
| 1387 | account_type = get_account_type(gle.account) |
| 1388 | if account_type == "Receivable": |
| 1389 | dr_or_cr = gle.debit - gle.credit |
| 1390 | dr_or_cr_account_currency = gle.debit_in_account_currency - gle.credit_in_account_currency |
| 1391 | elif account_type == "Payable": |
| 1392 | dr_or_cr = gle.credit - gle.debit |
| 1393 | dr_or_cr_account_currency = gle.credit_in_account_currency - gle.debit_in_account_currency |
| 1394 | |
| 1395 | if cancel: |
| 1396 | dr_or_cr *= -1 |
| 1397 | dr_or_cr_account_currency *= -1 |
| 1398 | |
| 1399 | ple = frappe.get_doc( |
| 1400 | { |
| 1401 | "doctype": "Payment Ledger Entry", |
| 1402 | "posting_date": gle.posting_date, |
| 1403 | "company": gle.company, |
| 1404 | "account_type": account_type, |
| 1405 | "account": gle.account, |
| 1406 | "party_type": gle.party_type, |
| 1407 | "party": gle.party, |
| 1408 | "cost_center": gle.cost_center, |
| 1409 | "finance_book": gle.finance_book, |
| 1410 | "due_date": gle.due_date, |
| 1411 | "voucher_type": gle.voucher_type, |
| 1412 | "voucher_no": gle.voucher_no, |
| 1413 | "against_voucher_type": gle.against_voucher_type |
| 1414 | if gle.against_voucher_type |
| 1415 | else gle.voucher_type, |
| 1416 | "against_voucher_no": gle.against_voucher if gle.against_voucher else gle.voucher_no, |
| 1417 | "currency": gle.currency, |
| 1418 | "amount": dr_or_cr, |
| 1419 | "amount_in_account_currency": dr_or_cr_account_currency, |
| 1420 | "delinked": True if cancel else False, |
| 1421 | } |
| 1422 | ) |
| 1423 | |
| 1424 | dimensions_and_defaults = get_dimensions() |
| 1425 | if dimensions_and_defaults: |
| 1426 | for dimension in dimensions_and_defaults[0]: |
| 1427 | ple.set(dimension.fieldname, gle.get(dimension.fieldname)) |
| 1428 | |
| 1429 | if cancel: |
| 1430 | delink_original_entry(ple) |
| 1431 | ple.flags.ignore_permissions = 1 |
| 1432 | ple.submit() |
| 1433 | |
| 1434 | |
| 1435 | def delink_original_entry(pl_entry): |
| 1436 | if pl_entry: |
| 1437 | ple = qb.DocType("Payment Ledger Entry") |
| 1438 | query = ( |
| 1439 | qb.update(ple) |
| 1440 | .set(ple.delinked, True) |
| 1441 | .set(ple.modified, now()) |
| 1442 | .set(ple.modified_by, frappe.session.user) |
| 1443 | .where( |
| 1444 | (ple.company == pl_entry.company) |
| 1445 | & (ple.account_type == pl_entry.account_type) |
| 1446 | & (ple.account == pl_entry.account) |
| 1447 | & (ple.party_type == pl_entry.party_type) |
| 1448 | & (ple.party == pl_entry.party) |
| 1449 | & (ple.voucher_type == pl_entry.voucher_type) |
| 1450 | & (ple.voucher_no == pl_entry.voucher_no) |
| 1451 | & (ple.against_voucher_type == pl_entry.against_voucher_type) |
| 1452 | & (ple.against_voucher_no == pl_entry.against_voucher_no) |
| 1453 | ) |
| 1454 | ) |
| 1455 | query.run() |