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