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