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