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