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