2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
2013-01-29 06:02:38 +00:00
|
|
|
|
|
|
|
|
2021-10-04 06:14:46 +00:00
|
|
|
from json import loads
|
2022-06-14 12:50:33 +00:00
|
|
|
from typing import TYPE_CHECKING, List, Optional, Tuple
|
2021-10-04 06:14:46 +00:00
|
|
|
|
2018-05-16 05:32:26 +00:00
|
|
|
import frappe
|
2017-04-25 06:40:24 +00:00
|
|
|
import frappe.defaults
|
2022-05-16 08:59:58 +00:00
|
|
|
from frappe import _, qb, throw
|
2020-12-25 12:42:35 +00:00
|
|
|
from frappe.model.meta import get_field_precision
|
2022-06-14 12:16:04 +00:00
|
|
|
from frappe.query_builder import AliasedQuery, Criterion, Table
|
|
|
|
from frappe.query_builder.functions import Sum
|
2022-06-13 07:09:28 +00:00
|
|
|
from frappe.query_builder.utils import DocType
|
2022-06-15 14:00:26 +00:00
|
|
|
from frappe.utils import (
|
|
|
|
cint,
|
|
|
|
create_batch,
|
|
|
|
cstr,
|
|
|
|
flt,
|
|
|
|
formatdate,
|
|
|
|
get_number_format_info,
|
|
|
|
getdate,
|
|
|
|
now,
|
|
|
|
nowdate,
|
|
|
|
)
|
2022-06-13 07:09:28 +00:00
|
|
|
from pypika import Order
|
|
|
|
from pypika.terms import ExistsCriterion
|
2015-09-28 08:01:17 +00:00
|
|
|
|
2016-06-23 07:14:06 +00:00
|
|
|
import erpnext
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2019-10-31 10:25:03 +00:00
|
|
|
# imported to enable erpnext.accounts.utils.get_account_currency
|
|
|
|
from erpnext.accounts.doctype.account.account import get_account_currency # noqa
|
2022-05-16 08:59:58 +00:00
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import get_dimensions
|
2019-10-31 10:25:03 +00:00
|
|
|
from erpnext.stock import get_warehouse_account_map
|
|
|
|
from erpnext.stock.utils import get_stock_value_on
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2022-06-14 12:50:33 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from erpnext.stock.doctype.repost_item_valuation.repost_item_valuation import RepostItemValuation
|
|
|
|
|
2019-10-31 10:25:03 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
class FiscalYearError(frappe.ValidationError):
|
|
|
|
pass
|
2022-03-28 13:22:46 +00:00
|
|
|
|
|
|
|
|
2021-08-10 16:51:28 +00:00
|
|
|
class PaymentEntryUnlinkError(frappe.ValidationError):
|
|
|
|
pass
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-08-22 12:55:43 +00:00
|
|
|
|
2022-06-14 12:50:33 +00:00
|
|
|
GL_REPOSTING_CHUNK = 100
|
|
|
|
|
|
|
|
|
2014-10-03 12:13:02 +00:00
|
|
|
@frappe.whitelist()
|
2016-03-11 09:26:19 +00:00
|
|
|
def get_fiscal_year(
|
2023-03-19 07:16:42 +00:00
|
|
|
date=None, fiscal_year=None, label="Date", verbose=1, company=None, as_dict=False, boolean=False
|
2016-03-11 09:26:19 +00:00
|
|
|
):
|
2023-03-19 07:16:42 +00:00
|
|
|
fiscal_years = get_fiscal_years(
|
|
|
|
date, fiscal_year, label, verbose, company, as_dict=as_dict, boolean=boolean
|
|
|
|
)
|
|
|
|
if boolean:
|
|
|
|
return fiscal_years
|
|
|
|
else:
|
|
|
|
return fiscal_years[0]
|
2013-01-29 06:02:38 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-03-11 09:26:19 +00:00
|
|
|
def get_fiscal_years(
|
2023-03-19 07:16:42 +00:00
|
|
|
transaction_date=None,
|
|
|
|
fiscal_year=None,
|
|
|
|
label="Date",
|
|
|
|
verbose=1,
|
|
|
|
company=None,
|
|
|
|
as_dict=False,
|
|
|
|
boolean=False,
|
2016-03-11 09:26:19 +00:00
|
|
|
):
|
2016-12-30 10:51:35 +00:00
|
|
|
fiscal_years = frappe.cache().hget("fiscal_years", company) or []
|
2017-07-28 06:09:01 +00:00
|
|
|
|
|
|
|
if not fiscal_years:
|
2016-12-30 10:51:35 +00:00
|
|
|
# if year start date is 2012-04-01, year end date should be 2013-03-31 (hence subdate)
|
2022-06-13 07:09:28 +00:00
|
|
|
FY = DocType("Fiscal Year")
|
|
|
|
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(FY)
|
|
|
|
.select(FY.name, FY.year_start_date, FY.year_end_date)
|
|
|
|
.where(FY.disabled == 0)
|
|
|
|
)
|
|
|
|
|
2016-12-30 10:51:35 +00:00
|
|
|
if fiscal_year:
|
2022-06-13 07:09:28 +00:00
|
|
|
query = query.where(FY.name == fiscal_year)
|
|
|
|
|
2016-12-30 10:51:35 +00:00
|
|
|
if company:
|
2022-06-13 07:09:28 +00:00
|
|
|
FYC = DocType("Fiscal Year Company")
|
|
|
|
query = query.where(
|
|
|
|
ExistsCriterion(frappe.qb.from_(FYC).select(FYC.name).where(FYC.parent == FY.name)).negate()
|
|
|
|
| ExistsCriterion(
|
|
|
|
frappe.qb.from_(FYC)
|
|
|
|
.select(FYC.company)
|
|
|
|
.where(FYC.parent == FY.name)
|
|
|
|
.where(FYC.company == company)
|
2016-12-30 10:51:35 +00:00
|
|
|
)
|
2022-06-13 07:09:28 +00:00
|
|
|
)
|
|
|
|
|
2022-10-03 05:37:24 +00:00
|
|
|
query = query.orderby(FY.year_start_date, order=Order.desc)
|
2022-06-13 07:09:28 +00:00
|
|
|
fiscal_years = query.run(as_dict=True)
|
2017-07-28 06:09:01 +00:00
|
|
|
|
2016-12-30 10:51:35 +00:00
|
|
|
frappe.cache().hset("fiscal_years", company, fiscal_years)
|
|
|
|
|
2020-06-18 12:48:41 +00:00
|
|
|
if not transaction_date and not fiscal_year:
|
|
|
|
return fiscal_years
|
|
|
|
|
2016-12-30 10:51:35 +00:00
|
|
|
if transaction_date:
|
|
|
|
transaction_date = getdate(transaction_date)
|
|
|
|
|
|
|
|
for fy in fiscal_years:
|
|
|
|
matched = False
|
|
|
|
if fiscal_year and fy.name == fiscal_year:
|
|
|
|
matched = True
|
|
|
|
|
2017-07-28 06:09:01 +00:00
|
|
|
if (
|
|
|
|
transaction_date
|
|
|
|
and getdate(fy.year_start_date) <= transaction_date
|
2016-12-30 10:51:35 +00:00
|
|
|
and getdate(fy.year_end_date) >= transaction_date
|
|
|
|
):
|
|
|
|
matched = True
|
2017-07-28 06:09:01 +00:00
|
|
|
|
2016-12-30 10:51:35 +00:00
|
|
|
if matched:
|
|
|
|
if as_dict:
|
|
|
|
return (fy,)
|
|
|
|
else:
|
|
|
|
return ((fy.name, fy.year_start_date, fy.year_end_date),)
|
|
|
|
|
2020-12-07 05:41:00 +00:00
|
|
|
error_msg = _("""{0} {1} is not in any active Fiscal Year""").format(
|
|
|
|
label, formatdate(transaction_date)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-12-05 08:06:43 +00:00
|
|
|
if company:
|
2020-12-07 05:41:00 +00:00
|
|
|
error_msg = _("""{0} for {1}""").format(error_msg, frappe.bold(company))
|
2021-02-16 03:42:27 +00:00
|
|
|
|
2023-03-19 07:16:42 +00:00
|
|
|
if boolean:
|
|
|
|
return False
|
|
|
|
|
2016-12-30 10:51:35 +00:00
|
|
|
if verbose == 1:
|
|
|
|
frappe.msgprint(error_msg)
|
2023-03-19 07:16:42 +00:00
|
|
|
|
2017-07-27 05:08:35 +00:00
|
|
|
raise FiscalYearError(error_msg)
|
2016-12-30 10:51:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-06-18 12:48:41 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_fiscal_year_filter_field(company=None):
|
|
|
|
field = {"fieldtype": "Select", "options": [], "operator": "Between", "query_value": True}
|
|
|
|
fiscal_years = get_fiscal_years(company=company)
|
|
|
|
for fiscal_year in fiscal_years:
|
|
|
|
field["options"].append(
|
|
|
|
{
|
|
|
|
"label": fiscal_year.name,
|
|
|
|
"value": fiscal_year.name,
|
|
|
|
"query_value": [
|
|
|
|
fiscal_year.year_start_date.strftime("%Y-%m-%d"),
|
|
|
|
fiscal_year.year_end_date.strftime("%Y-%m-%d"),
|
2022-03-28 13:22:46 +00:00
|
|
|
],
|
2020-06-18 12:48:41 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return field
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-03-27 07:00:01 +00:00
|
|
|
def validate_fiscal_year(date, fiscal_year, company, label="Date", doc=None):
|
|
|
|
years = [f[0] for f in get_fiscal_years(date, label=_(label), company=company)]
|
2013-02-05 17:55:37 +00:00
|
|
|
if fiscal_year not in years:
|
2015-02-19 09:21:58 +00:00
|
|
|
if doc:
|
|
|
|
doc.fiscal_year = years[0]
|
|
|
|
else:
|
|
|
|
throw(_("{0} '{1}' not in Fiscal Year {2}").format(label, formatdate(date), fiscal_year))
|
2013-01-29 06:02:38 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2019-08-13 06:35:22 +00:00
|
|
|
def get_balance_on(
|
|
|
|
account=None,
|
|
|
|
date=None,
|
|
|
|
party_type=None,
|
|
|
|
party=None,
|
|
|
|
company=None,
|
|
|
|
in_account_currency=True,
|
|
|
|
cost_center=None,
|
|
|
|
ignore_account_permission=False,
|
|
|
|
):
|
2014-02-14 10:17:51 +00:00
|
|
|
if not account and frappe.form_dict.get("account"):
|
|
|
|
account = frappe.form_dict.get("account")
|
2014-09-17 08:41:22 +00:00
|
|
|
if not date and frappe.form_dict.get("date"):
|
2014-02-14 10:17:51 +00:00
|
|
|
date = frappe.form_dict.get("date")
|
2014-09-17 08:41:22 +00:00
|
|
|
if not party_type and frappe.form_dict.get("party_type"):
|
|
|
|
party_type = frappe.form_dict.get("party_type")
|
|
|
|
if not party and frappe.form_dict.get("party"):
|
|
|
|
party = frappe.form_dict.get("party")
|
2018-09-06 09:09:35 +00:00
|
|
|
if not cost_center and frappe.form_dict.get("cost_center"):
|
|
|
|
cost_center = frappe.form_dict.get("cost_center")
|
|
|
|
|
2020-07-30 15:22:20 +00:00
|
|
|
cond = ["is_cancelled=0"]
|
2013-01-29 06:02:38 +00:00
|
|
|
if date:
|
2018-09-21 04:50:52 +00:00
|
|
|
cond.append("posting_date <= %s" % frappe.db.escape(cstr(date)))
|
2013-01-29 06:02:38 +00:00
|
|
|
else:
|
|
|
|
# get balance of all entries that exist
|
|
|
|
date = nowdate()
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2019-07-16 04:11:01 +00:00
|
|
|
if account:
|
|
|
|
acc = frappe.get_doc("Account", account)
|
|
|
|
|
2013-01-29 06:02:38 +00:00
|
|
|
try:
|
2020-07-03 17:29:00 +00:00
|
|
|
year_start_date = get_fiscal_year(date, company=company, verbose=0)[1]
|
2014-04-14 13:50:45 +00:00
|
|
|
except FiscalYearError:
|
2013-01-29 06:02:38 +00:00
|
|
|
if getdate(date) > getdate(nowdate()):
|
|
|
|
# if fiscal year not found and the date is greater than today
|
|
|
|
# get fiscal year for today's date and its corresponding year start date
|
|
|
|
year_start_date = get_fiscal_year(nowdate(), verbose=1)[1]
|
|
|
|
else:
|
|
|
|
# this indicates that it is a date older than any existing fiscal year.
|
|
|
|
# hence, assuming balance as 0.0
|
|
|
|
return 0.0
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2019-07-20 09:22:59 +00:00
|
|
|
if account:
|
2019-07-20 09:19:32 +00:00
|
|
|
report_type = acc.report_type
|
2019-07-20 09:16:59 +00:00
|
|
|
else:
|
|
|
|
report_type = ""
|
|
|
|
|
2020-03-11 08:29:15 +00:00
|
|
|
if cost_center and report_type == "Profit and Loss":
|
2018-09-06 09:09:35 +00:00
|
|
|
cc = frappe.get_doc("Cost Center", cost_center)
|
|
|
|
if cc.is_group:
|
|
|
|
cond.append(
|
|
|
|
""" exists (
|
|
|
|
select 1 from `tabCost Center` cc where cc.name = gle.cost_center
|
|
|
|
and cc.lft >= %s and cc.rgt <= %s
|
|
|
|
)"""
|
|
|
|
% (cc.lft, cc.rgt)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2018-09-06 09:09:35 +00:00
|
|
|
|
|
|
|
else:
|
2018-09-21 10:16:44 +00:00
|
|
|
cond.append("""gle.cost_center = %s """ % (frappe.db.escape(cost_center, percent=False),))
|
2018-09-06 09:09:35 +00:00
|
|
|
|
2014-09-17 08:41:22 +00:00
|
|
|
if account:
|
2018-09-06 09:09:35 +00:00
|
|
|
|
2019-08-13 06:35:22 +00:00
|
|
|
if not (frappe.flags.ignore_account_permission or ignore_account_permission):
|
2015-10-15 11:01:16 +00:00
|
|
|
acc.check_permission("read")
|
2014-09-17 08:41:22 +00:00
|
|
|
|
|
|
|
# different filter for group and ledger - improved performance
|
2015-04-23 07:44:17 +00:00
|
|
|
if acc.is_group:
|
2014-09-17 08:41:22 +00:00
|
|
|
cond.append(
|
|
|
|
"""exists (
|
2015-06-14 12:19:29 +00:00
|
|
|
select name from `tabAccount` ac where ac.name = gle.account
|
2014-09-17 08:41:22 +00:00
|
|
|
and ac.lft >= %s and ac.rgt <= %s
|
|
|
|
)"""
|
|
|
|
% (acc.lft, acc.rgt)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-09-28 08:01:17 +00:00
|
|
|
|
|
|
|
# If group and currency same as company,
|
2015-09-17 08:27:42 +00:00
|
|
|
# always return balance based on debit and credit in company currency
|
2018-08-08 11:07:31 +00:00
|
|
|
if acc.account_currency == frappe.get_cached_value("Company", acc.company, "default_currency"):
|
2015-09-17 08:27:42 +00:00
|
|
|
in_account_currency = False
|
2014-09-17 08:41:22 +00:00
|
|
|
else:
|
2018-09-21 04:50:52 +00:00
|
|
|
cond.append("""gle.account = %s """ % (frappe.db.escape(account, percent=False),))
|
2016-03-11 09:26:19 +00:00
|
|
|
|
2014-09-17 08:41:22 +00:00
|
|
|
if party_type and party:
|
2018-09-21 04:50:52 +00:00
|
|
|
cond.append(
|
|
|
|
"""gle.party_type = %s and gle.party = %s """
|
2016-02-19 07:15:57 +00:00
|
|
|
% (frappe.db.escape(party_type), frappe.db.escape(party, percent=False))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-07-20 05:56:45 +00:00
|
|
|
if company:
|
2018-09-21 04:50:52 +00:00
|
|
|
cond.append("""gle.company = %s """ % (frappe.db.escape(company, percent=False)))
|
2015-09-16 07:16:54 +00:00
|
|
|
|
2015-08-17 05:57:00 +00:00
|
|
|
if account or (party_type and party):
|
2015-09-04 07:56:23 +00:00
|
|
|
if in_account_currency:
|
2015-11-16 13:35:46 +00:00
|
|
|
select_field = "sum(debit_in_account_currency) - sum(credit_in_account_currency)"
|
2015-09-04 07:56:23 +00:00
|
|
|
else:
|
2015-11-16 13:35:46 +00:00
|
|
|
select_field = "sum(debit) - sum(credit)"
|
2015-08-17 05:57:00 +00:00
|
|
|
bal = frappe.db.sql(
|
|
|
|
"""
|
2015-09-04 07:56:23 +00:00
|
|
|
SELECT {0}
|
2015-08-17 05:57:00 +00:00
|
|
|
FROM `tabGL Entry` gle
|
2015-09-04 07:56:23 +00:00
|
|
|
WHERE {1}""".format(
|
|
|
|
select_field, " and ".join(cond)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-09-04 07:56:23 +00:00
|
|
|
)[0][0]
|
2015-08-17 05:57:00 +00:00
|
|
|
|
|
|
|
# if bal is None, return 0
|
|
|
|
return flt(bal)
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-09-20 05:41:39 +00:00
|
|
|
def get_count_on(account, fieldname, date):
|
2020-07-30 15:22:20 +00:00
|
|
|
cond = ["is_cancelled=0"]
|
2016-09-20 05:41:39 +00:00
|
|
|
if date:
|
2018-09-21 04:50:52 +00:00
|
|
|
cond.append("posting_date <= %s" % frappe.db.escape(cstr(date)))
|
2016-09-20 05:41:39 +00:00
|
|
|
else:
|
|
|
|
# get balance of all entries that exist
|
|
|
|
date = nowdate()
|
|
|
|
|
|
|
|
try:
|
|
|
|
year_start_date = get_fiscal_year(date, verbose=0)[1]
|
|
|
|
except FiscalYearError:
|
|
|
|
if getdate(date) > getdate(nowdate()):
|
|
|
|
# if fiscal year not found and the date is greater than today
|
|
|
|
# get fiscal year for today's date and its corresponding year start date
|
|
|
|
year_start_date = get_fiscal_year(nowdate(), verbose=1)[1]
|
|
|
|
else:
|
|
|
|
# this indicates that it is a date older than any existing fiscal year.
|
|
|
|
# hence, assuming balance as 0.0
|
|
|
|
return 0.0
|
|
|
|
|
|
|
|
if account:
|
|
|
|
acc = frappe.get_doc("Account", account)
|
|
|
|
|
|
|
|
if not frappe.flags.ignore_account_permission:
|
|
|
|
acc.check_permission("read")
|
|
|
|
|
|
|
|
# for pl accounts, get balance within a fiscal year
|
|
|
|
if acc.report_type == "Profit and Loss":
|
|
|
|
cond.append(
|
|
|
|
"posting_date >= '%s' and voucher_type != 'Period Closing Voucher'" % year_start_date
|
|
|
|
)
|
|
|
|
|
|
|
|
# different filter for group and ledger - improved performance
|
|
|
|
if acc.is_group:
|
|
|
|
cond.append(
|
|
|
|
"""exists (
|
|
|
|
select name from `tabAccount` ac where ac.name = gle.account
|
|
|
|
and ac.lft >= %s and ac.rgt <= %s
|
|
|
|
)"""
|
|
|
|
% (acc.lft, acc.rgt)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2016-09-20 05:41:39 +00:00
|
|
|
else:
|
2018-09-21 04:50:52 +00:00
|
|
|
cond.append("""gle.account = %s """ % (frappe.db.escape(account, percent=False),))
|
2016-09-20 05:41:39 +00:00
|
|
|
|
|
|
|
entries = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT name, posting_date, account, party_type, party,debit,credit,
|
|
|
|
voucher_type, voucher_no, against_voucher_type, against_voucher
|
|
|
|
FROM `tabGL Entry` gle
|
|
|
|
WHERE {0}""".format(
|
|
|
|
" and ".join(cond)
|
|
|
|
),
|
|
|
|
as_dict=True,
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-09-20 05:41:39 +00:00
|
|
|
count = 0
|
|
|
|
for gle in entries:
|
|
|
|
if fieldname not in ("invoiced_amount", "payables"):
|
|
|
|
count += 1
|
|
|
|
else:
|
|
|
|
dr_or_cr = "debit" if fieldname == "invoiced_amount" else "credit"
|
|
|
|
cr_or_dr = "credit" if fieldname == "invoiced_amount" else "debit"
|
2017-06-15 05:39:27 +00:00
|
|
|
select_fields = (
|
|
|
|
"ifnull(sum(credit-debit),0)"
|
|
|
|
if fieldname == "invoiced_amount"
|
|
|
|
else "ifnull(sum(debit-credit),0)"
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2016-09-20 05:41:39 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
(not gle.against_voucher)
|
|
|
|
or (gle.against_voucher_type in ["Sales Order", "Purchase Order"])
|
|
|
|
or (gle.against_voucher == gle.voucher_no and gle.get(dr_or_cr) > 0)
|
|
|
|
):
|
|
|
|
payment_amount = frappe.db.sql(
|
|
|
|
"""
|
|
|
|
SELECT {0}
|
|
|
|
FROM `tabGL Entry` gle
|
|
|
|
WHERE docstatus < 2 and posting_date <= %(date)s and against_voucher = %(voucher_no)s
|
2017-06-15 05:39:27 +00:00
|
|
|
and party = %(party)s and name != %(name)s""".format(
|
|
|
|
select_fields
|
|
|
|
),
|
2017-07-28 06:09:01 +00:00
|
|
|
{"date": date, "voucher_no": gle.voucher_no, "party": gle.party, "name": gle.name},
|
2017-06-15 05:39:27 +00:00
|
|
|
)[0][0]
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-09-20 05:41:39 +00:00
|
|
|
outstanding_amount = flt(gle.get(dr_or_cr)) - flt(gle.get(cr_or_dr)) - payment_amount
|
|
|
|
currency_precision = get_currency_precision() or 2
|
|
|
|
if abs(flt(outstanding_amount)) > 0.1 / 10**currency_precision:
|
|
|
|
count += 1
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-09-20 05:41:39 +00:00
|
|
|
return count
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2013-01-30 13:46:13 +00:00
|
|
|
def add_ac(args=None):
|
2017-01-11 07:11:01 +00:00
|
|
|
from frappe.desk.treeview import make_tree_args
|
|
|
|
|
2013-01-30 13:46:13 +00:00
|
|
|
if not args:
|
2014-02-14 10:17:51 +00:00
|
|
|
args = frappe.local.form_dict
|
2017-01-11 07:11:01 +00:00
|
|
|
|
2017-02-17 10:20:26 +00:00
|
|
|
args.doctype = "Account"
|
2017-01-11 07:11:01 +00:00
|
|
|
args = make_tree_args(**args)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2014-04-22 13:24:54 +00:00
|
|
|
ac = frappe.new_doc("Account")
|
2016-05-31 01:52:37 +00:00
|
|
|
|
2016-05-13 10:26:00 +00:00
|
|
|
if args.get("ignore_permissions"):
|
|
|
|
ac.flags.ignore_permissions = True
|
|
|
|
args.pop("ignore_permissions")
|
2016-05-31 01:52:37 +00:00
|
|
|
|
2014-04-22 13:24:54 +00:00
|
|
|
ac.update(args)
|
2016-05-30 12:24:16 +00:00
|
|
|
|
|
|
|
if not ac.parent_account:
|
|
|
|
ac.parent_account = args.get("parent")
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
ac.old_parent = ""
|
|
|
|
ac.freeze_account = "No"
|
2016-05-11 07:07:22 +00:00
|
|
|
if cint(ac.get("is_root")):
|
|
|
|
ac.parent_account = None
|
2016-05-31 01:52:37 +00:00
|
|
|
ac.flags.ignore_mandatory = True
|
|
|
|
|
2013-01-30 13:46:13 +00:00
|
|
|
ac.insert()
|
2014-04-22 13:24:54 +00:00
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
return ac.name
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2013-01-30 13:46:13 +00:00
|
|
|
def add_cc(args=None):
|
2017-01-11 07:11:01 +00:00
|
|
|
from frappe.desk.treeview import make_tree_args
|
|
|
|
|
2013-01-30 13:46:13 +00:00
|
|
|
if not args:
|
2014-02-14 10:17:51 +00:00
|
|
|
args = frappe.local.form_dict
|
2017-07-28 06:09:01 +00:00
|
|
|
|
2017-02-17 10:20:26 +00:00
|
|
|
args.doctype = "Cost Center"
|
2017-01-11 07:11:01 +00:00
|
|
|
args = make_tree_args(**args)
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2018-06-08 09:03:15 +00:00
|
|
|
if args.parent_cost_center == args.company:
|
|
|
|
args.parent_cost_center = "{0} - {1}".format(
|
2018-08-08 11:07:31 +00:00
|
|
|
args.parent_cost_center, frappe.get_cached_value("Company", args.company, "abbr")
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2018-06-08 09:03:15 +00:00
|
|
|
|
2014-04-22 13:24:54 +00:00
|
|
|
cc = frappe.new_doc("Cost Center")
|
|
|
|
cc.update(args)
|
2016-05-30 12:24:16 +00:00
|
|
|
|
|
|
|
if not cc.parent_cost_center:
|
|
|
|
cc.parent_cost_center = args.get("parent")
|
|
|
|
|
2014-03-28 08:25:00 +00:00
|
|
|
cc.old_parent = ""
|
2013-01-30 13:46:13 +00:00
|
|
|
cc.insert()
|
2014-03-28 08:25:00 +00:00
|
|
|
return cc.name
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2023-06-20 07:52:32 +00:00
|
|
|
def reconcile_against_document(args, skip_ref_details_update_for_pe=False): # nosemgrep
|
2013-01-30 13:46:13 +00:00
|
|
|
"""
|
2021-08-31 13:29:29 +00:00
|
|
|
Cancel PE or JV, Update against document, split if required and resubmit
|
2013-01-30 13:46:13 +00:00
|
|
|
"""
|
2021-08-31 13:29:29 +00:00
|
|
|
# To optimize making GL Entry for PE or JV with multiple references
|
|
|
|
reconciled_entries = {}
|
|
|
|
for row in args:
|
|
|
|
if not reconciled_entries.get((row.voucher_type, row.voucher_no)):
|
|
|
|
reconciled_entries[(row.voucher_type, row.voucher_no)] = []
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
reconciled_entries[(row.voucher_type, row.voucher_no)].append(row)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
for key, entries in reconciled_entries.items():
|
|
|
|
voucher_type = key[0]
|
|
|
|
voucher_no = key[1]
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
# cancel advance entry
|
|
|
|
doc = frappe.get_doc(voucher_type, voucher_no)
|
2021-07-30 11:00:18 +00:00
|
|
|
frappe.flags.ignore_party_validation = True
|
2023-01-14 06:52:22 +00:00
|
|
|
_delete_pl_entries(voucher_type, voucher_no)
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
for entry in entries:
|
|
|
|
check_if_advance_entry_modified(entry)
|
|
|
|
validate_allocated_amount(entry)
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
# update ref in advance entry
|
|
|
|
if voucher_type == "Journal Entry":
|
|
|
|
update_reference_in_journal_entry(entry, doc, do_not_save=True)
|
|
|
|
else:
|
2023-04-22 11:54:35 +00:00
|
|
|
update_reference_in_payment_entry(
|
|
|
|
entry, doc, do_not_save=True, skip_ref_details_update_for_pe=skip_ref_details_update_for_pe
|
|
|
|
)
|
2021-08-31 13:29:29 +00:00
|
|
|
|
|
|
|
doc.save(ignore_permissions=True)
|
2016-06-27 12:11:39 +00:00
|
|
|
# re-submit advance entry
|
2021-08-31 13:29:29 +00:00
|
|
|
doc = frappe.get_doc(entry.voucher_type, entry.voucher_no)
|
2023-06-20 07:52:32 +00:00
|
|
|
gl_map = doc.build_gl_map()
|
2023-01-14 06:52:22 +00:00
|
|
|
create_payment_ledger_entry(gl_map, update_outstanding="No", cancel=0, adv_adj=1)
|
2023-06-23 16:23:34 +00:00
|
|
|
|
|
|
|
if voucher_type == "Payment Entry":
|
2023-06-27 12:41:47 +00:00
|
|
|
doc.make_advance_gl_entries()
|
2023-01-14 06:52:22 +00:00
|
|
|
|
|
|
|
# Only update outstanding for newly linked vouchers
|
|
|
|
for entry in entries:
|
|
|
|
update_voucher_outstanding(
|
|
|
|
entry.against_voucher_type, entry.against_voucher, entry.account, entry.party_type, entry.party
|
|
|
|
)
|
2022-05-26 10:30:40 +00:00
|
|
|
|
2021-07-30 11:00:18 +00:00
|
|
|
frappe.flags.ignore_party_validation = False
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
def check_if_advance_entry_modified(args):
|
2013-01-30 13:46:13 +00:00
|
|
|
"""
|
|
|
|
check if there is already a voucher reference
|
|
|
|
check if amount is same
|
|
|
|
check if jv is submitted
|
|
|
|
"""
|
2021-08-31 13:29:29 +00:00
|
|
|
if not args.get("unreconciled_amount"):
|
|
|
|
args.update({"unreconciled_amount": args.get("unadjusted_amount")})
|
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
ret = None
|
|
|
|
if args.voucher_type == "Journal Entry":
|
2023-06-02 11:43:51 +00:00
|
|
|
journal_entry = frappe.qb.DocType("Journal Entry")
|
|
|
|
journal_acc = frappe.qb.DocType("Journal Entry Account")
|
|
|
|
|
2023-06-08 07:45:23 +00:00
|
|
|
q = (
|
|
|
|
frappe.qb.from_(journal_entry)
|
2023-06-22 06:11:43 +00:00
|
|
|
.inner_join(journal_acc)
|
2023-06-02 11:43:51 +00:00
|
|
|
.on(journal_entry.name == journal_acc.parent)
|
2023-06-22 12:58:16 +00:00
|
|
|
.select(journal_acc[args.get("dr_or_cr")])
|
|
|
|
.where(
|
|
|
|
(journal_acc.account == args.get("account"))
|
|
|
|
& ((journal_acc.party_type == args.get("party_type")))
|
|
|
|
& ((journal_acc.party == args.get("party")))
|
|
|
|
& (
|
|
|
|
(journal_acc.reference_type.isnull())
|
|
|
|
| (journal_acc.reference_type.isin(["", "Sales Order", "Purchase Order"]))
|
|
|
|
)
|
|
|
|
& ((journal_entry.name == args.get("voucher_no")))
|
|
|
|
& ((journal_acc.name == args.get("voucher_detail_no")))
|
|
|
|
& ((journal_entry.docstatus == 1))
|
2023-06-08 07:45:23 +00:00
|
|
|
)
|
2023-06-02 11:43:51 +00:00
|
|
|
)
|
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
else:
|
2023-06-02 11:43:51 +00:00
|
|
|
payment_entry = frappe.qb.DocType("Payment Entry")
|
|
|
|
payment_ref = frappe.qb.DocType("Payment Entry Reference")
|
|
|
|
|
2023-06-08 07:45:23 +00:00
|
|
|
q = (
|
|
|
|
frappe.qb.from_(payment_entry)
|
2023-06-02 11:43:51 +00:00
|
|
|
.select(payment_entry.name)
|
|
|
|
.where(payment_entry.name == args.get("voucher_no"))
|
|
|
|
.where(payment_entry.docstatus == 1)
|
|
|
|
.where(payment_entry.party_type == args.get("party_type"))
|
|
|
|
.where(payment_entry.party == args.get("party"))
|
2018-05-16 05:32:26 +00:00
|
|
|
)
|
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
if args.voucher_detail_no:
|
2023-06-08 07:45:23 +00:00
|
|
|
q = (
|
|
|
|
q.inner_join(payment_ref)
|
2023-06-02 11:43:51 +00:00
|
|
|
.on(payment_entry.name == payment_ref.parent)
|
|
|
|
.where(payment_ref.name == args.get("voucher_detail_no"))
|
2023-06-08 07:45:23 +00:00
|
|
|
.where(payment_ref.reference_doctype.isin(("", "Sales Order", "Purchase Order")))
|
2023-06-02 11:43:51 +00:00
|
|
|
.where(payment_ref.allocated_amount == args.get("unreconciled_amount"))
|
2023-06-08 07:45:23 +00:00
|
|
|
)
|
2016-06-27 12:11:39 +00:00
|
|
|
else:
|
2023-06-02 11:43:51 +00:00
|
|
|
q = q.where(payment_entry.unallocated_amount == args.get("unreconciled_amount"))
|
2023-06-08 07:45:23 +00:00
|
|
|
|
2023-06-02 11:43:51 +00:00
|
|
|
ret = q.run(as_dict=True)
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2013-01-30 13:46:13 +00:00
|
|
|
if not ret:
|
2014-01-29 11:01:38 +00:00
|
|
|
throw(_("""Payment Entry has been modified after you pulled it. Please pull it again."""))
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-04-30 14:00:50 +00:00
|
|
|
def validate_allocated_amount(args):
|
2021-03-31 08:36:02 +00:00
|
|
|
precision = args.get("precision") or frappe.db.get_single_value(
|
|
|
|
"System Settings", "currency_precision"
|
|
|
|
)
|
2016-06-27 12:11:39 +00:00
|
|
|
if args.get("allocated_amount") < 0:
|
2019-05-27 06:17:07 +00:00
|
|
|
throw(_("Allocated amount cannot be negative"))
|
2021-03-31 08:36:02 +00:00
|
|
|
elif flt(args.get("allocated_amount"), precision) > flt(args.get("unadjusted_amount"), precision):
|
2019-05-27 06:17:07 +00:00
|
|
|
throw(_("Allocated amount cannot be greater than unadjusted amount"))
|
2014-04-30 14:00:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
def update_reference_in_journal_entry(d, journal_entry, do_not_save=False):
|
2013-01-30 13:46:13 +00:00
|
|
|
"""
|
|
|
|
Updates against document, if partial amount splits into rows
|
|
|
|
"""
|
2021-08-31 13:29:29 +00:00
|
|
|
jv_detail = journal_entry.get("accounts", {"name": d["voucher_detail_no"]})[0]
|
2015-08-10 11:34:07 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
if flt(d["unadjusted_amount"]) - flt(d["allocated_amount"]) != 0:
|
|
|
|
# adjust the unreconciled balance
|
|
|
|
amount_in_account_currency = flt(d["unadjusted_amount"]) - flt(d["allocated_amount"])
|
|
|
|
amount_in_company_currency = amount_in_account_currency * flt(jv_detail.exchange_rate)
|
|
|
|
jv_detail.set(d["dr_or_cr"], amount_in_account_currency)
|
|
|
|
jv_detail.set(
|
|
|
|
"debit" if d["dr_or_cr"] == "debit_in_account_currency" else "credit",
|
|
|
|
amount_in_company_currency,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
journal_entry.remove(jv_detail)
|
2015-08-10 11:34:07 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
# new row with references
|
|
|
|
new_row = journal_entry.append("accounts")
|
2021-10-29 15:22:47 +00:00
|
|
|
|
|
|
|
new_row.update((frappe.copy_doc(jv_detail)).as_dict())
|
2013-01-30 13:46:13 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
new_row.set(d["dr_or_cr"], d["allocated_amount"])
|
|
|
|
new_row.set(
|
|
|
|
"debit" if d["dr_or_cr"] == "debit_in_account_currency" else "credit",
|
|
|
|
d["allocated_amount"] * flt(jv_detail.exchange_rate),
|
|
|
|
)
|
2015-10-15 11:01:16 +00:00
|
|
|
|
2021-08-31 13:29:29 +00:00
|
|
|
new_row.set(
|
|
|
|
"credit_in_account_currency"
|
|
|
|
if d["dr_or_cr"] == "debit_in_account_currency"
|
|
|
|
else "debit_in_account_currency",
|
|
|
|
0,
|
|
|
|
)
|
|
|
|
new_row.set("credit" if d["dr_or_cr"] == "debit_in_account_currency" else "debit", 0)
|
|
|
|
|
|
|
|
new_row.set("reference_type", d["against_voucher_type"])
|
|
|
|
new_row.set("reference_name", d["against_voucher"])
|
|
|
|
|
|
|
|
new_row.against_account = cstr(jv_detail.against_account)
|
|
|
|
new_row.is_advance = cstr(jv_detail.is_advance)
|
|
|
|
new_row.docstatus = 1
|
2014-04-08 08:23:35 +00:00
|
|
|
|
|
|
|
# will work as update after submit
|
2021-08-31 13:29:29 +00:00
|
|
|
journal_entry.flags.ignore_validate_update_after_submit = True
|
|
|
|
if not do_not_save:
|
|
|
|
journal_entry.save(ignore_permissions=True)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2023-04-22 11:54:35 +00:00
|
|
|
def update_reference_in_payment_entry(
|
|
|
|
d, payment_entry, do_not_save=False, skip_ref_details_update_for_pe=False
|
|
|
|
):
|
2016-06-27 12:11:39 +00:00
|
|
|
reference_details = {
|
|
|
|
"reference_doctype": d.against_voucher_type,
|
|
|
|
"reference_name": d.against_voucher,
|
|
|
|
"total_amount": d.grand_total,
|
|
|
|
"outstanding_amount": d.outstanding_amount,
|
|
|
|
"allocated_amount": d.allocated_amount,
|
2021-07-12 09:03:23 +00:00
|
|
|
"exchange_rate": d.exchange_rate
|
|
|
|
if not d.exchange_gain_loss
|
|
|
|
else payment_entry.get_exchange_rate(),
|
|
|
|
"exchange_gain_loss": d.exchange_gain_loss, # only populated from invoice in case of advance allocation
|
2023-06-08 12:45:37 +00:00
|
|
|
"account": d.account,
|
2016-06-27 12:11:39 +00:00
|
|
|
}
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
if d.voucher_detail_no:
|
|
|
|
existing_row = payment_entry.get("references", {"name": d["voucher_detail_no"]})[0]
|
|
|
|
original_row = existing_row.as_dict().copy()
|
|
|
|
existing_row.update(reference_details)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
if d.allocated_amount < original_row.allocated_amount:
|
|
|
|
new_row = payment_entry.append("references")
|
|
|
|
new_row.docstatus = 1
|
2018-05-23 06:01:24 +00:00
|
|
|
for field in list(reference_details):
|
2016-06-27 12:11:39 +00:00
|
|
|
new_row.set(field, original_row[field])
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
new_row.allocated_amount = original_row.allocated_amount - d.allocated_amount
|
|
|
|
else:
|
|
|
|
new_row = payment_entry.append("references")
|
|
|
|
new_row.docstatus = 1
|
|
|
|
new_row.update(reference_details)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2019-06-23 20:22:48 +00:00
|
|
|
if d.difference_amount and d.difference_account:
|
2021-07-12 09:03:23 +00:00
|
|
|
account_details = {
|
2019-06-23 20:22:48 +00:00
|
|
|
"account": d.difference_account,
|
|
|
|
"cost_center": payment_entry.cost_center
|
|
|
|
or frappe.get_cached_value("Company", payment_entry.company, "cost_center"),
|
2021-07-12 09:03:23 +00:00
|
|
|
}
|
|
|
|
if d.difference_amount:
|
|
|
|
account_details["amount"] = d.difference_amount
|
|
|
|
|
|
|
|
payment_entry.set_gain_or_loss(account_details=account_details)
|
2019-06-23 20:22:48 +00:00
|
|
|
|
2023-01-02 03:25:12 +00:00
|
|
|
payment_entry.flags.ignore_validate_update_after_submit = True
|
|
|
|
payment_entry.setup_party_account_field()
|
|
|
|
payment_entry.set_missing_values()
|
2023-04-22 11:54:35 +00:00
|
|
|
if not skip_ref_details_update_for_pe:
|
|
|
|
payment_entry.set_missing_ref_details()
|
2023-01-02 03:25:12 +00:00
|
|
|
payment_entry.set_amounts()
|
|
|
|
|
2019-06-23 20:22:48 +00:00
|
|
|
if not do_not_save:
|
|
|
|
payment_entry.save(ignore_permissions=True)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-11-23 10:28:51 +00:00
|
|
|
def unlink_ref_doc_from_payment_entries(ref_doc):
|
|
|
|
remove_ref_doc_link_from_jv(ref_doc.doctype, ref_doc.name)
|
|
|
|
remove_ref_doc_link_from_pe(ref_doc.doctype, ref_doc.name)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-07-21 13:00:03 +00:00
|
|
|
frappe.db.sql(
|
|
|
|
"""update `tabGL Entry`
|
|
|
|
set against_voucher_type=null, against_voucher=null,
|
|
|
|
modified=%s, modified_by=%s
|
|
|
|
where against_voucher_type=%s and against_voucher=%s
|
|
|
|
and voucher_no != ifnull(against_voucher, '')""",
|
2016-11-23 10:28:51 +00:00
|
|
|
(now(), frappe.session.user, ref_doc.doctype, ref_doc.name),
|
|
|
|
)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2022-10-10 04:47:19 +00:00
|
|
|
ple = qb.DocType("Payment Ledger Entry")
|
|
|
|
|
|
|
|
qb.update(ple).set(ple.against_voucher_type, ple.voucher_type).set(
|
|
|
|
ple.against_voucher_no, ple.voucher_no
|
|
|
|
).set(ple.modified, now()).set(ple.modified_by, frappe.session.user).where(
|
|
|
|
(ple.against_voucher_type == ref_doc.doctype)
|
|
|
|
& (ple.against_voucher_no == ref_doc.name)
|
|
|
|
& (ple.delinked == 0)
|
|
|
|
).run()
|
|
|
|
|
2016-11-23 10:28:51 +00:00
|
|
|
if ref_doc.doctype in ("Sales Invoice", "Purchase Invoice"):
|
|
|
|
ref_doc.set("advances", [])
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-11-23 10:28:51 +00:00
|
|
|
frappe.db.sql(
|
|
|
|
"""delete from `tab{0} Advance` where parent = %s""".format(ref_doc.doctype), ref_doc.name
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-07-21 13:00:03 +00:00
|
|
|
|
|
|
|
def remove_ref_doc_link_from_jv(ref_type, ref_no):
|
2014-12-25 10:31:55 +00:00
|
|
|
linked_jv = frappe.db.sql_list(
|
|
|
|
"""select parent from `tabJournal Entry Account`
|
2015-08-10 11:34:07 +00:00
|
|
|
where reference_type=%s and reference_name=%s and docstatus < 2""",
|
|
|
|
(ref_type, ref_no),
|
|
|
|
)
|
2014-04-08 08:23:35 +00:00
|
|
|
|
|
|
|
if linked_jv:
|
2015-08-10 11:34:07 +00:00
|
|
|
frappe.db.sql(
|
|
|
|
"""update `tabJournal Entry Account`
|
|
|
|
set reference_type=null, reference_name = null,
|
2014-01-03 09:42:16 +00:00
|
|
|
modified=%s, modified_by=%s
|
2015-08-10 11:34:07 +00:00
|
|
|
where reference_type=%s and reference_name=%s
|
|
|
|
and docstatus < 2""",
|
|
|
|
(now(), frappe.session.user, ref_type, ref_no),
|
|
|
|
)
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2020-01-29 09:36:18 +00:00
|
|
|
frappe.msgprint(_("Journal Entries {0} are un-linked").format("\n".join(linked_jv)))
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-07-21 13:00:03 +00:00
|
|
|
def remove_ref_doc_link_from_pe(ref_type, ref_no):
|
|
|
|
linked_pe = frappe.db.sql_list(
|
|
|
|
"""select parent from `tabPayment Entry Reference`
|
|
|
|
where reference_doctype=%s and reference_name=%s and docstatus < 2""",
|
|
|
|
(ref_type, ref_no),
|
|
|
|
)
|
|
|
|
|
|
|
|
if linked_pe:
|
|
|
|
frappe.db.sql(
|
|
|
|
"""update `tabPayment Entry Reference`
|
|
|
|
set allocated_amount=0, modified=%s, modified_by=%s
|
|
|
|
where reference_doctype=%s and reference_name=%s
|
|
|
|
and docstatus < 2""",
|
|
|
|
(now(), frappe.session.user, ref_type, ref_no),
|
|
|
|
)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-07-21 13:00:03 +00:00
|
|
|
for pe in linked_pe:
|
2021-08-01 12:18:50 +00:00
|
|
|
try:
|
2023-06-23 16:23:34 +00:00
|
|
|
pe_doc = frappe.get_doc("Payment Entry", pe)
|
2021-08-10 16:51:28 +00:00
|
|
|
pe_doc.set_amounts()
|
2023-06-23 15:27:51 +00:00
|
|
|
pe_doc.make_advance_gl_entries(against_voucher_type=ref_type, against_voucher=ref_no, cancel=1)
|
2021-08-10 09:22:24 +00:00
|
|
|
pe_doc.clear_unallocated_reference_document_rows()
|
|
|
|
pe_doc.validate_payment_type_with_outstanding()
|
2021-08-01 12:18:50 +00:00
|
|
|
except Exception as e:
|
|
|
|
msg = _("There were issues unlinking payment entry {0}.").format(pe_doc.name)
|
|
|
|
msg += "<br>"
|
2021-08-10 08:34:31 +00:00
|
|
|
msg += _("Please cancel payment entry manually first")
|
2021-08-10 16:51:28 +00:00
|
|
|
frappe.throw(msg, exc=PaymentEntryUnlinkError, title=_("Payment Unlink Error"))
|
2016-12-15 05:57:35 +00:00
|
|
|
|
|
|
|
frappe.db.sql(
|
|
|
|
"""update `tabPayment Entry` set total_allocated_amount=%s,
|
|
|
|
base_total_allocated_amount=%s, unallocated_amount=%s, modified=%s, modified_by=%s
|
|
|
|
where name=%s""",
|
|
|
|
(
|
|
|
|
pe_doc.total_allocated_amount,
|
|
|
|
pe_doc.base_total_allocated_amount,
|
2016-07-21 13:00:03 +00:00
|
|
|
pe_doc.unallocated_amount,
|
|
|
|
now(),
|
|
|
|
frappe.session.user,
|
|
|
|
pe,
|
2022-03-28 13:22:46 +00:00
|
|
|
),
|
2016-07-21 13:00:03 +00:00
|
|
|
)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2020-01-29 09:36:18 +00:00
|
|
|
frappe.msgprint(_("Payment Entries {0} are un-linked").format("\n".join(linked_pe)))
|
2013-03-25 05:36:00 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
@frappe.whitelist()
|
2021-07-30 07:06:35 +00:00
|
|
|
def get_company_default(company, fieldname, ignore_validation=False):
|
2021-08-31 13:29:29 +00:00
|
|
|
value = frappe.get_cached_value("Company", company, fieldname)
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2021-07-30 07:06:35 +00:00
|
|
|
if not ignore_validation and not value:
|
2017-08-01 11:13:11 +00:00
|
|
|
throw(
|
|
|
|
_("Please set default {0} in Company {1}").format(
|
2018-05-26 03:42:59 +00:00
|
|
|
frappe.get_meta("Company").get_label(fieldname), company
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2013-03-25 05:36:00 +00:00
|
|
|
return value
|
2013-05-28 11:22:30 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-05-28 11:22:30 +00:00
|
|
|
def fix_total_debit_credit():
|
2014-04-08 08:23:35 +00:00
|
|
|
vouchers = frappe.db.sql(
|
|
|
|
"""select voucher_type, voucher_no,
|
|
|
|
sum(debit) - sum(credit) as diff
|
|
|
|
from `tabGL Entry`
|
2013-05-28 11:22:30 +00:00
|
|
|
group by voucher_type, voucher_no
|
2015-11-16 13:35:46 +00:00
|
|
|
having sum(debit) != sum(credit)""",
|
|
|
|
as_dict=1,
|
|
|
|
)
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2013-05-28 11:22:30 +00:00
|
|
|
for d in vouchers:
|
|
|
|
if abs(d.diff) > 0:
|
|
|
|
dr_or_cr = d.voucher_type == "Sales Invoice" and "credit" or "debit"
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
frappe.db.sql(
|
|
|
|
"""update `tabGL Entry` set %s = %s + %s
|
2013-05-28 11:22:30 +00:00
|
|
|
where voucher_type = %s and voucher_no = %s and %s > 0 limit 1"""
|
2014-04-08 08:23:35 +00:00
|
|
|
% (dr_or_cr, dr_or_cr, "%s", "%s", "%s", dr_or_cr),
|
2013-08-02 06:14:29 +00:00
|
|
|
(d.diff, d.voucher_type, d.voucher_no),
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-04-08 08:23:35 +00:00
|
|
|
|
2017-07-28 06:09:01 +00:00
|
|
|
def get_currency_precision():
|
2017-04-25 06:40:24 +00:00
|
|
|
precision = cint(frappe.db.get_default("currency_precision"))
|
|
|
|
if not precision:
|
|
|
|
number_format = frappe.db.get_default("number_format") or "#,###.##"
|
|
|
|
precision = get_number_format_info(number_format)[2]
|
2017-07-28 06:09:01 +00:00
|
|
|
|
2017-04-25 06:40:24 +00:00
|
|
|
return precision
|
2017-07-28 06:09:01 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2014-08-07 09:40:05 +00:00
|
|
|
def get_stock_rbnb_difference(posting_date, company):
|
|
|
|
stock_items = frappe.db.sql_list(
|
|
|
|
"""select distinct item_code
|
2014-08-07 09:43:52 +00:00
|
|
|
from `tabStock Ledger Entry` where company=%s""",
|
|
|
|
company,
|
|
|
|
)
|
2014-08-07 09:40:05 +00:00
|
|
|
|
|
|
|
pr_valuation_amount = frappe.db.sql(
|
|
|
|
"""
|
2015-11-16 13:35:46 +00:00
|
|
|
select sum(pr_item.valuation_rate * pr_item.qty * pr_item.conversion_factor)
|
2014-08-07 09:40:05 +00:00
|
|
|
from `tabPurchase Receipt Item` pr_item, `tabPurchase Receipt` pr
|
2018-05-26 03:42:59 +00:00
|
|
|
where pr.name = pr_item.parent and pr.docstatus=1 and pr.company=%s
|
2014-08-07 09:40:05 +00:00
|
|
|
and pr.posting_date <= %s and pr_item.item_code in (%s)"""
|
2018-05-26 03:42:59 +00:00
|
|
|
% ("%s", "%s", ", ".join(["%s"] * len(stock_items))),
|
|
|
|
tuple([company, posting_date] + stock_items),
|
|
|
|
)[0][0]
|
2014-08-07 09:40:05 +00:00
|
|
|
|
|
|
|
pi_valuation_amount = frappe.db.sql(
|
|
|
|
"""
|
2015-11-16 13:35:46 +00:00
|
|
|
select sum(pi_item.valuation_rate * pi_item.qty * pi_item.conversion_factor)
|
2014-08-07 09:40:05 +00:00
|
|
|
from `tabPurchase Invoice Item` pi_item, `tabPurchase Invoice` pi
|
2018-05-26 03:42:59 +00:00
|
|
|
where pi.name = pi_item.parent and pi.docstatus=1 and pi.company=%s
|
2014-08-07 09:40:05 +00:00
|
|
|
and pi.posting_date <= %s and pi_item.item_code in (%s)"""
|
2018-05-26 03:42:59 +00:00
|
|
|
% ("%s", "%s", ", ".join(["%s"] * len(stock_items))),
|
|
|
|
tuple([company, posting_date] + stock_items),
|
|
|
|
)[0][0]
|
2014-08-07 09:40:05 +00:00
|
|
|
|
|
|
|
# Balance should be
|
|
|
|
stock_rbnb = flt(pr_valuation_amount, 2) - flt(pi_valuation_amount, 2)
|
|
|
|
|
|
|
|
# Balance as per system
|
2018-08-08 11:07:31 +00:00
|
|
|
stock_rbnb_account = "Stock Received But Not Billed - " + frappe.get_cached_value(
|
|
|
|
"Company", company, "abbr"
|
|
|
|
)
|
2015-09-04 07:56:23 +00:00
|
|
|
sys_bal = get_balance_on(stock_rbnb_account, posting_date, in_account_currency=False)
|
2014-08-07 09:40:05 +00:00
|
|
|
|
|
|
|
# Amount should be credited
|
|
|
|
return flt(stock_rbnb) + flt(sys_bal)
|
2014-09-12 09:48:53 +00:00
|
|
|
|
2017-09-25 23:48:30 +00:00
|
|
|
|
2018-05-16 06:01:41 +00:00
|
|
|
def get_held_invoices(party_type, party):
|
|
|
|
"""
|
|
|
|
Returns a list of names Purchase Invoices for the given party that are on hold
|
|
|
|
"""
|
|
|
|
held_invoices = None
|
|
|
|
|
|
|
|
if party_type == "Supplier":
|
|
|
|
held_invoices = frappe.db.sql(
|
|
|
|
"select name from `tabPurchase Invoice` where release_date IS NOT NULL and release_date > CURDATE()",
|
|
|
|
as_dict=1,
|
|
|
|
)
|
2021-06-11 13:10:22 +00:00
|
|
|
held_invoices = set(d["name"] for d in held_invoices)
|
2018-05-16 06:01:41 +00:00
|
|
|
|
|
|
|
return held_invoices
|
|
|
|
|
|
|
|
|
2022-06-14 12:16:04 +00:00
|
|
|
def get_outstanding_invoices(
|
2022-07-28 06:27:27 +00:00
|
|
|
party_type,
|
|
|
|
party,
|
|
|
|
account,
|
|
|
|
common_filter=None,
|
|
|
|
posting_date=None,
|
|
|
|
min_outstanding=None,
|
|
|
|
max_outstanding=None,
|
2022-12-14 10:35:15 +00:00
|
|
|
accounting_dimensions=None,
|
2022-06-14 12:16:04 +00:00
|
|
|
):
|
|
|
|
|
|
|
|
ple = qb.DocType("Payment Ledger Entry")
|
2015-11-12 11:35:29 +00:00
|
|
|
outstanding_invoices = []
|
2019-02-04 15:43:43 +00:00
|
|
|
precision = frappe.get_precision("Sales Invoice", "outstanding_amount") or 2
|
2015-11-12 11:35:29 +00:00
|
|
|
|
2019-11-19 13:14:32 +00:00
|
|
|
if account:
|
2020-01-24 10:52:49 +00:00
|
|
|
root_type, account_type = frappe.get_cached_value(
|
|
|
|
"Account", account, ["root_type", "account_type"]
|
|
|
|
)
|
2019-11-19 13:14:32 +00:00
|
|
|
party_account_type = "Receivable" if root_type == "Asset" else "Payable"
|
2020-01-24 10:52:49 +00:00
|
|
|
party_account_type = account_type or party_account_type
|
2019-11-19 13:14:32 +00:00
|
|
|
else:
|
|
|
|
party_account_type = erpnext.get_party_account_type(party_type)
|
|
|
|
|
2018-05-16 06:01:41 +00:00
|
|
|
held_invoices = get_held_invoices(party_type, party)
|
|
|
|
|
2022-06-14 12:16:04 +00:00
|
|
|
common_filter = common_filter or []
|
|
|
|
common_filter.append(ple.account_type == party_account_type)
|
|
|
|
common_filter.append(ple.account == account)
|
|
|
|
common_filter.append(ple.party_type == party_type)
|
|
|
|
common_filter.append(ple.party == party)
|
|
|
|
|
|
|
|
ple_query = QueryPaymentLedger()
|
|
|
|
invoice_list = ple_query.get_voucher_outstandings(
|
|
|
|
common_filter=common_filter,
|
2022-07-28 06:27:27 +00:00
|
|
|
posting_date=posting_date,
|
2022-06-14 12:16:04 +00:00
|
|
|
min_outstanding=min_outstanding,
|
|
|
|
max_outstanding=max_outstanding,
|
|
|
|
get_invoices=True,
|
2022-12-14 10:35:15 +00:00
|
|
|
accounting_dimensions=accounting_dimensions or [],
|
2019-02-04 15:43:43 +00:00
|
|
|
)
|
|
|
|
|
2015-11-12 11:35:29 +00:00
|
|
|
for d in invoice_list:
|
2022-06-30 09:44:38 +00:00
|
|
|
payment_amount = d.invoice_amount_in_account_currency - d.outstanding_in_account_currency
|
|
|
|
outstanding_amount = d.outstanding_in_account_currency
|
2019-02-04 15:43:43 +00:00
|
|
|
if outstanding_amount > 0.5 / (10**precision):
|
2019-07-22 05:43:27 +00:00
|
|
|
if (
|
2022-06-14 12:16:04 +00:00
|
|
|
min_outstanding
|
|
|
|
and max_outstanding
|
|
|
|
and not (outstanding_amount >= min_outstanding and outstanding_amount <= max_outstanding)
|
2019-05-01 18:36:04 +00:00
|
|
|
):
|
|
|
|
continue
|
2019-02-04 15:43:43 +00:00
|
|
|
|
2019-05-01 18:36:04 +00:00
|
|
|
if not d.voucher_type == "Purchase Invoice" or d.voucher_no not in held_invoices:
|
2019-02-04 15:43:43 +00:00
|
|
|
outstanding_invoices.append(
|
|
|
|
frappe._dict(
|
|
|
|
{
|
|
|
|
"voucher_no": d.voucher_no,
|
|
|
|
"voucher_type": d.voucher_type,
|
|
|
|
"posting_date": d.posting_date,
|
2022-06-30 09:44:38 +00:00
|
|
|
"invoice_amount": flt(d.invoice_amount_in_account_currency),
|
2019-02-04 15:43:43 +00:00
|
|
|
"payment_amount": payment_amount,
|
|
|
|
"outstanding_amount": outstanding_amount,
|
2020-07-22 12:29:37 +00:00
|
|
|
"due_date": d.due_date,
|
|
|
|
"currency": d.currency,
|
2023-06-08 07:45:23 +00:00
|
|
|
"account": d.account,
|
2019-02-04 15:43:43 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2016-12-15 05:57:35 +00:00
|
|
|
|
2016-06-27 12:11:39 +00:00
|
|
|
outstanding_invoices = sorted(
|
|
|
|
outstanding_invoices, key=lambda k: k["due_date"] or getdate(nowdate())
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2015-11-12 11:35:29 +00:00
|
|
|
return outstanding_invoices
|
2016-02-22 09:48:40 +00:00
|
|
|
|
|
|
|
|
2016-03-18 09:35:50 +00:00
|
|
|
def get_account_name(
|
|
|
|
account_type=None, root_type=None, is_group=None, account_currency=None, company=None
|
|
|
|
):
|
2016-02-22 09:48:40 +00:00
|
|
|
"""return account based on matching conditions"""
|
2016-02-23 14:31:04 +00:00
|
|
|
return frappe.db.get_value(
|
|
|
|
"Account",
|
|
|
|
{
|
|
|
|
"account_type": account_type or "",
|
|
|
|
"root_type": root_type or "",
|
|
|
|
"is_group": is_group or 0,
|
|
|
|
"account_currency": account_currency or frappe.defaults.get_defaults().currency,
|
|
|
|
"company": company or frappe.defaults.get_defaults().company,
|
|
|
|
},
|
|
|
|
"name",
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-06-21 07:03:12 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_companies():
|
|
|
|
"""get a list of companies based on permission"""
|
|
|
|
return [d.name for d in frappe.get_list("Company", fields=["name"], order_by="name")]
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2016-06-21 07:03:12 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
2017-11-10 13:22:21 +00:00
|
|
|
def get_children(doctype, parent, company, is_root=False):
|
2018-04-02 04:44:32 +00:00
|
|
|
from erpnext.accounts.report.financial_statements import sort_accounts
|
2017-07-28 06:09:01 +00:00
|
|
|
|
2018-05-26 03:42:59 +00:00
|
|
|
parent_fieldname = "parent_" + doctype.lower().replace(" ", "_")
|
|
|
|
fields = ["name as value", "is_group as expandable"]
|
|
|
|
filters = [["docstatus", "<", 2]]
|
|
|
|
|
|
|
|
filters.append(['ifnull(`{0}`,"")'.format(parent_fieldname), "=", "" if is_root else parent])
|
2018-05-16 05:14:38 +00:00
|
|
|
|
2018-05-16 08:23:31 +00:00
|
|
|
if is_root:
|
2019-07-17 15:50:05 +00:00
|
|
|
fields += ["root_type", "report_type", "account_currency"] if doctype == "Account" else []
|
2018-05-26 03:42:59 +00:00
|
|
|
filters.append(["company", "=", company])
|
|
|
|
|
2016-06-21 07:03:12 +00:00
|
|
|
else:
|
2019-07-17 15:50:05 +00:00
|
|
|
fields += ["root_type", "account_currency"] if doctype == "Account" else []
|
2018-05-26 03:42:59 +00:00
|
|
|
fields += [parent_fieldname + " as parent"]
|
|
|
|
|
|
|
|
acc = frappe.get_list(doctype, fields=fields, filters=filters)
|
2016-06-21 07:03:12 +00:00
|
|
|
|
2016-06-24 06:06:31 +00:00
|
|
|
if doctype == "Account":
|
2018-04-02 04:44:32 +00:00
|
|
|
sort_accounts(acc, is_root, key="value")
|
2016-06-21 07:03:12 +00:00
|
|
|
|
|
|
|
return acc
|
2016-09-09 05:49:22 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-10-04 06:14:46 +00:00
|
|
|
@frappe.whitelist()
|
|
|
|
def get_account_balances(accounts, company):
|
|
|
|
|
|
|
|
if isinstance(accounts, str):
|
|
|
|
accounts = loads(accounts)
|
|
|
|
|
|
|
|
if not accounts:
|
|
|
|
return []
|
|
|
|
|
|
|
|
company_currency = frappe.get_cached_value("Company", company, "default_currency")
|
|
|
|
|
|
|
|
for account in accounts:
|
|
|
|
account["company_currency"] = company_currency
|
|
|
|
account["balance"] = flt(
|
|
|
|
get_balance_on(account["value"], in_account_currency=False, company=company)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2021-10-04 06:14:46 +00:00
|
|
|
if account["account_currency"] and account["account_currency"] != company_currency:
|
|
|
|
account["balance_in_account_currency"] = flt(get_balance_on(account["value"], company=company))
|
|
|
|
|
|
|
|
return accounts
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-09-22 07:28:32 +00:00
|
|
|
def create_payment_gateway_account(gateway, payment_channel="Email"):
|
2021-05-29 18:24:51 +00:00
|
|
|
from erpnext.setup.setup_wizard.operations.install_fixtures import create_bank_account
|
2016-09-09 05:49:22 +00:00
|
|
|
|
2022-11-17 13:44:10 +00:00
|
|
|
company = frappe.get_cached_value("Global Defaults", "Global Defaults", "default_company")
|
2016-09-09 05:49:22 +00:00
|
|
|
if not company:
|
|
|
|
return
|
|
|
|
|
|
|
|
# NOTE: we translate Payment Gateway account name because that is going to be used by the end user
|
|
|
|
bank_account = frappe.db.get_value(
|
|
|
|
"Account",
|
|
|
|
{"account_name": _(gateway), "company": company},
|
|
|
|
["name", "account_currency"],
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not bank_account:
|
|
|
|
# check for untranslated one
|
|
|
|
bank_account = frappe.db.get_value(
|
|
|
|
"Account",
|
|
|
|
{"account_name": gateway, "company": company},
|
|
|
|
["name", "account_currency"],
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
if not bank_account:
|
|
|
|
# try creating one
|
|
|
|
bank_account = create_bank_account({"company_name": company, "bank_account": _(gateway)})
|
|
|
|
|
|
|
|
if not bank_account:
|
|
|
|
frappe.msgprint(_("Payment Gateway Account not created, please create one manually."))
|
|
|
|
return
|
|
|
|
|
|
|
|
# if payment gateway account exists, return
|
|
|
|
if frappe.db.exists(
|
|
|
|
"Payment Gateway Account",
|
|
|
|
{"payment_gateway": gateway, "currency": bank_account.account_currency},
|
|
|
|
):
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
frappe.get_doc(
|
|
|
|
{
|
|
|
|
"doctype": "Payment Gateway Account",
|
|
|
|
"is_default": 1,
|
|
|
|
"payment_gateway": gateway,
|
|
|
|
"payment_account": bank_account.name,
|
2020-09-22 07:28:32 +00:00
|
|
|
"currency": bank_account.account_currency,
|
|
|
|
"payment_channel": payment_channel,
|
2022-02-22 15:23:19 +00:00
|
|
|
}
|
|
|
|
).insert(ignore_permissions=True, ignore_if_duplicate=True)
|
2016-09-09 05:49:22 +00:00
|
|
|
|
|
|
|
except frappe.DuplicateEntryError:
|
|
|
|
# already exists, due to a reinstall?
|
2017-07-27 05:08:35 +00:00
|
|
|
pass
|
2018-06-08 11:05:21 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-08 11:05:21 +00:00
|
|
|
@frappe.whitelist()
|
2020-06-15 06:37:04 +00:00
|
|
|
def update_cost_center(docname, cost_center_name, cost_center_number, company, merge):
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
2018-06-08 11:05:21 +00:00
|
|
|
Renames the document by adding the number as a prefix to the current name and updates
|
|
|
|
all transaction where it was present.
|
2022-03-28 13:22:46 +00:00
|
|
|
"""
|
2020-04-30 05:58:43 +00:00
|
|
|
validate_field_number("Cost Center", docname, cost_center_number, company, "cost_center_number")
|
2018-06-08 11:05:21 +00:00
|
|
|
|
2020-04-30 05:58:43 +00:00
|
|
|
if cost_center_number:
|
|
|
|
frappe.db.set_value("Cost Center", docname, "cost_center_number", cost_center_number.strip())
|
|
|
|
else:
|
|
|
|
frappe.db.set_value("Cost Center", docname, "cost_center_number", "")
|
2018-06-08 11:05:21 +00:00
|
|
|
|
2020-04-30 05:58:43 +00:00
|
|
|
frappe.db.set_value("Cost Center", docname, "cost_center_name", cost_center_name.strip())
|
2018-06-08 11:05:21 +00:00
|
|
|
|
2022-09-15 06:39:18 +00:00
|
|
|
new_name = get_autoname_with_number(cost_center_number, cost_center_name, company)
|
2020-04-30 05:58:43 +00:00
|
|
|
if docname != new_name:
|
2020-06-15 06:37:04 +00:00
|
|
|
frappe.rename_doc("Cost Center", docname, new_name, force=1, merge=merge)
|
2018-06-08 11:05:21 +00:00
|
|
|
return new_name
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-04-30 05:58:43 +00:00
|
|
|
def validate_field_number(doctype_name, docname, number_value, company, field_name):
|
2018-06-08 11:05:21 +00:00
|
|
|
"""Validate if the number entered isn't already assigned to some other document."""
|
2018-07-06 11:45:33 +00:00
|
|
|
if number_value:
|
2020-04-30 05:58:43 +00:00
|
|
|
filters = {field_name: number_value, "name": ["!=", docname]}
|
2018-06-08 11:05:21 +00:00
|
|
|
if company:
|
2020-04-30 05:58:43 +00:00
|
|
|
filters["company"] = company
|
|
|
|
|
|
|
|
doctype_with_same_number = frappe.db.get_value(doctype_name, filters)
|
|
|
|
|
2018-06-08 11:05:21 +00:00
|
|
|
if doctype_with_same_number:
|
2020-04-30 05:58:43 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("{0} Number {1} is already used in {2} {3}").format(
|
|
|
|
doctype_name, number_value, doctype_name.lower(), doctype_with_same_number
|
|
|
|
)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
|
|
|
|
2018-06-08 11:05:21 +00:00
|
|
|
|
2022-09-15 06:39:18 +00:00
|
|
|
def get_autoname_with_number(number_value, doc_title, company):
|
2018-06-08 11:05:21 +00:00
|
|
|
"""append title with prefix as number and suffix as company's abbreviation separated by '-'"""
|
2022-09-15 06:39:18 +00:00
|
|
|
company_abbr = frappe.get_cached_value("Company", company, "abbr")
|
|
|
|
parts = [doc_title.strip(), company_abbr]
|
|
|
|
|
2018-07-06 11:45:33 +00:00
|
|
|
if cstr(number_value).strip():
|
|
|
|
parts.insert(0, cstr(number_value).strip())
|
2022-09-15 06:39:18 +00:00
|
|
|
|
2018-06-08 11:05:21 +00:00
|
|
|
return " - ".join(parts)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-06-28 08:45:34 +00:00
|
|
|
|
|
|
|
@frappe.whitelist()
|
|
|
|
def get_coa(doctype, parent, is_root, chart=None):
|
|
|
|
from erpnext.accounts.doctype.account.chart_of_accounts.chart_of_accounts import (
|
|
|
|
build_tree_from_json,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
2018-06-28 08:45:34 +00:00
|
|
|
|
|
|
|
# add chart to flags to retrieve when called from expand all function
|
|
|
|
chart = chart if chart else frappe.flags.chart
|
|
|
|
frappe.flags.chart = chart
|
|
|
|
|
|
|
|
parent = None if parent == _("All Accounts") else parent
|
|
|
|
accounts = build_tree_from_json(chart) # returns alist of dict in a tree render-able form
|
|
|
|
|
|
|
|
# filter out to show data for the selected node only
|
|
|
|
accounts = [d for d in accounts if d["parent_account"] == parent]
|
|
|
|
|
|
|
|
return accounts
|
2018-09-06 09:09:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-04-30 05:08:58 +00:00
|
|
|
def update_gl_entries_after(
|
|
|
|
posting_date,
|
|
|
|
posting_time,
|
|
|
|
for_warehouses=None,
|
|
|
|
for_items=None,
|
|
|
|
warehouse_account=None,
|
|
|
|
company=None,
|
|
|
|
):
|
2021-02-16 09:27:00 +00:00
|
|
|
stock_vouchers = get_future_stock_vouchers(
|
|
|
|
posting_date, posting_time, for_warehouses, for_items, company
|
|
|
|
)
|
|
|
|
repost_gle_for_stock_vouchers(stock_vouchers, posting_date, company, warehouse_account)
|
|
|
|
|
|
|
|
|
|
|
|
def repost_gle_for_stock_vouchers(
|
2022-06-14 12:50:33 +00:00
|
|
|
stock_vouchers: List[Tuple[str, str]],
|
|
|
|
posting_date: str,
|
|
|
|
company: Optional[str] = None,
|
|
|
|
warehouse_account=None,
|
|
|
|
repost_doc: Optional["RepostItemValuation"] = None,
|
2021-02-16 09:27:00 +00:00
|
|
|
):
|
2022-06-04 09:16:35 +00:00
|
|
|
|
|
|
|
from erpnext.accounts.general_ledger import toggle_debit_credit_if_negative
|
|
|
|
|
2022-04-19 07:54:29 +00:00
|
|
|
if not stock_vouchers:
|
|
|
|
return
|
|
|
|
|
2020-04-30 05:08:58 +00:00
|
|
|
if not warehouse_account:
|
|
|
|
warehouse_account = get_warehouse_account_map(company)
|
|
|
|
|
2022-06-14 12:50:33 +00:00
|
|
|
stock_vouchers = sort_stock_vouchers_by_posting_date(stock_vouchers)
|
|
|
|
if repost_doc and repost_doc.gl_reposting_index:
|
|
|
|
# Restore progress
|
|
|
|
stock_vouchers = stock_vouchers[cint(repost_doc.gl_reposting_index) :]
|
|
|
|
|
2021-02-23 12:20:49 +00:00
|
|
|
precision = get_field_precision(frappe.get_meta("GL Entry").get_field("debit")) or 2
|
|
|
|
|
2022-06-15 14:00:26 +00:00
|
|
|
for stock_vouchers_chunk in create_batch(stock_vouchers, GL_REPOSTING_CHUNK):
|
2022-06-14 12:50:33 +00:00
|
|
|
gle = get_voucherwise_gl_entries(stock_vouchers_chunk, posting_date)
|
|
|
|
|
|
|
|
for voucher_type, voucher_no in stock_vouchers_chunk:
|
|
|
|
existing_gle = gle.get((voucher_type, voucher_no), [])
|
|
|
|
voucher_obj = frappe.get_doc(voucher_type, voucher_no)
|
|
|
|
# Some transactions post credit as negative debit, this is handled while posting GLE
|
|
|
|
# but while comparing we need to make sure it's flipped so comparisons are accurate
|
|
|
|
expected_gle = toggle_debit_credit_if_negative(voucher_obj.get_gl_entries(warehouse_account))
|
|
|
|
if expected_gle:
|
|
|
|
if not existing_gle or not compare_existing_and_expected_gle(
|
|
|
|
existing_gle, expected_gle, precision
|
|
|
|
):
|
2022-10-21 09:48:40 +00:00
|
|
|
_delete_accounting_ledger_entries(voucher_type, voucher_no)
|
2022-06-14 12:50:33 +00:00
|
|
|
voucher_obj.make_gl_entries(gl_entries=expected_gle, from_repost=True)
|
|
|
|
else:
|
2022-10-21 09:48:40 +00:00
|
|
|
_delete_accounting_ledger_entries(voucher_type, voucher_no)
|
2022-06-15 15:49:09 +00:00
|
|
|
|
|
|
|
if not frappe.flags.in_test:
|
|
|
|
frappe.db.commit()
|
2020-04-30 05:08:58 +00:00
|
|
|
|
2022-06-14 12:50:33 +00:00
|
|
|
if repost_doc:
|
|
|
|
repost_doc.db_set(
|
|
|
|
"gl_reposting_index",
|
2022-06-15 14:00:26 +00:00
|
|
|
cint(repost_doc.gl_reposting_index) + len(stock_vouchers_chunk),
|
2022-06-14 12:50:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-10-21 09:48:40 +00:00
|
|
|
def _delete_pl_entries(voucher_type, voucher_no):
|
2022-10-04 10:47:56 +00:00
|
|
|
ple = qb.DocType("Payment Ledger Entry")
|
|
|
|
qb.from_(ple).delete().where(
|
|
|
|
(ple.voucher_type == voucher_type) & (ple.voucher_no == voucher_no)
|
|
|
|
).run()
|
2022-06-04 12:49:44 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-10-21 09:48:40 +00:00
|
|
|
def _delete_gl_entries(voucher_type, voucher_no):
|
|
|
|
gle = qb.DocType("GL Entry")
|
|
|
|
qb.from_(gle).delete().where(
|
|
|
|
(gle.voucher_type == voucher_type) & (gle.voucher_no == voucher_no)
|
|
|
|
).run()
|
|
|
|
|
|
|
|
|
|
|
|
def _delete_accounting_ledger_entries(voucher_type, voucher_no):
|
|
|
|
"""
|
|
|
|
Remove entries from both General and Payment Ledger for specified Voucher
|
|
|
|
"""
|
|
|
|
_delete_gl_entries(voucher_type, voucher_no)
|
|
|
|
_delete_pl_entries(voucher_type, voucher_no)
|
|
|
|
|
|
|
|
|
2022-04-19 07:54:29 +00:00
|
|
|
def sort_stock_vouchers_by_posting_date(
|
|
|
|
stock_vouchers: List[Tuple[str, str]]
|
|
|
|
) -> List[Tuple[str, str]]:
|
|
|
|
sle = frappe.qb.DocType("Stock Ledger Entry")
|
|
|
|
voucher_nos = [v[1] for v in stock_vouchers]
|
|
|
|
|
|
|
|
sles = (
|
|
|
|
frappe.qb.from_(sle)
|
|
|
|
.select(sle.voucher_type, sle.voucher_no, sle.posting_date, sle.posting_time, sle.creation)
|
|
|
|
.where((sle.is_cancelled == 0) & (sle.voucher_no.isin(voucher_nos)))
|
|
|
|
.groupby(sle.voucher_type, sle.voucher_no)
|
2022-06-14 12:50:33 +00:00
|
|
|
.orderby(sle.posting_date)
|
|
|
|
.orderby(sle.posting_time)
|
|
|
|
.orderby(sle.creation)
|
2022-04-19 07:54:29 +00:00
|
|
|
).run(as_dict=True)
|
|
|
|
sorted_vouchers = [(sle.voucher_type, sle.voucher_no) for sle in sles]
|
|
|
|
|
|
|
|
unknown_vouchers = set(stock_vouchers) - set(sorted_vouchers)
|
|
|
|
if unknown_vouchers:
|
|
|
|
sorted_vouchers.extend(unknown_vouchers)
|
|
|
|
|
|
|
|
return sorted_vouchers
|
|
|
|
|
|
|
|
|
2021-02-16 03:42:27 +00:00
|
|
|
def get_future_stock_vouchers(
|
|
|
|
posting_date, posting_time, for_warehouses=None, for_items=None, company=None
|
|
|
|
):
|
2020-04-30 05:08:58 +00:00
|
|
|
|
|
|
|
values = []
|
|
|
|
condition = ""
|
|
|
|
if for_items:
|
|
|
|
condition += " and item_code in ({})".format(", ".join(["%s"] * len(for_items)))
|
|
|
|
values += for_items
|
|
|
|
|
|
|
|
if for_warehouses:
|
|
|
|
condition += " and warehouse in ({})".format(", ".join(["%s"] * len(for_warehouses)))
|
|
|
|
values += for_warehouses
|
|
|
|
|
2021-02-16 03:42:27 +00:00
|
|
|
if company:
|
2021-02-16 09:27:00 +00:00
|
|
|
condition += " and company = %s"
|
2021-02-16 03:42:27 +00:00
|
|
|
values.append(company)
|
|
|
|
|
2021-08-11 05:47:50 +00:00
|
|
|
future_stock_vouchers = frappe.db.sql(
|
|
|
|
"""select distinct sle.voucher_type, sle.voucher_no
|
2020-04-30 05:08:58 +00:00
|
|
|
from `tabStock Ledger Entry` sle
|
2020-12-21 09:15:50 +00:00
|
|
|
where
|
|
|
|
timestamp(sle.posting_date, sle.posting_time) >= timestamp(%s, %s)
|
|
|
|
and is_cancelled = 0
|
|
|
|
{condition}
|
2020-04-30 05:08:58 +00:00
|
|
|
order by timestamp(sle.posting_date, sle.posting_time) asc, creation asc for update""".format(
|
|
|
|
condition=condition
|
|
|
|
),
|
2021-08-11 05:47:50 +00:00
|
|
|
tuple([posting_date, posting_time] + values),
|
|
|
|
as_dict=True,
|
|
|
|
)
|
2020-04-30 05:08:58 +00:00
|
|
|
|
2021-08-11 05:47:50 +00:00
|
|
|
return [(d.voucher_type, d.voucher_no) for d in future_stock_vouchers]
|
2020-04-30 05:08:58 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-04-30 05:08:58 +00:00
|
|
|
def get_voucherwise_gl_entries(future_stock_vouchers, posting_date):
|
2021-08-11 05:47:50 +00:00
|
|
|
"""Get voucherwise list of GL entries.
|
|
|
|
|
|
|
|
Only fetches GLE fields required for comparing with new GLE.
|
|
|
|
Check compare_existing_and_expected_gle function below.
|
2021-09-07 06:44:40 +00:00
|
|
|
|
|
|
|
returns:
|
|
|
|
Dict[Tuple[voucher_type, voucher_no], List[GL Entries]]
|
2021-08-11 05:47:50 +00:00
|
|
|
"""
|
2020-04-30 05:08:58 +00:00
|
|
|
gl_entries = {}
|
2021-08-11 05:47:50 +00:00
|
|
|
if not future_stock_vouchers:
|
|
|
|
return gl_entries
|
|
|
|
|
|
|
|
voucher_nos = [d[1] for d in future_stock_vouchers]
|
|
|
|
|
|
|
|
gles = frappe.db.sql(
|
|
|
|
"""
|
2021-09-07 06:44:40 +00:00
|
|
|
select name, account, credit, debit, cost_center, project, voucher_type, voucher_no
|
2021-08-11 05:47:50 +00:00
|
|
|
from `tabGL Entry`
|
|
|
|
where
|
|
|
|
posting_date >= %s and voucher_no in (%s)"""
|
|
|
|
% ("%s", ", ".join(["%s"] * len(voucher_nos))),
|
|
|
|
tuple([posting_date] + voucher_nos),
|
|
|
|
as_dict=1,
|
|
|
|
)
|
|
|
|
|
|
|
|
for d in gles:
|
|
|
|
gl_entries.setdefault((d.voucher_type, d.voucher_no), []).append(d)
|
2020-04-30 05:08:58 +00:00
|
|
|
|
2020-06-18 12:48:41 +00:00
|
|
|
return gl_entries
|
2020-12-21 09:15:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-02-23 12:20:49 +00:00
|
|
|
def compare_existing_and_expected_gle(existing_gle, expected_gle, precision):
|
2021-08-11 05:47:50 +00:00
|
|
|
if len(existing_gle) != len(expected_gle):
|
|
|
|
return False
|
|
|
|
|
2020-12-21 09:15:50 +00:00
|
|
|
matched = True
|
|
|
|
for entry in expected_gle:
|
|
|
|
account_existed = False
|
|
|
|
for e in existing_gle:
|
|
|
|
if entry.account == e.account:
|
|
|
|
account_existed = True
|
2021-07-29 10:11:22 +00:00
|
|
|
if (
|
|
|
|
entry.account == e.account
|
2021-02-23 12:20:49 +00:00
|
|
|
and (not entry.cost_center or not e.cost_center or entry.cost_center == e.cost_center)
|
|
|
|
and (
|
|
|
|
flt(entry.debit, precision) != flt(e.debit, precision)
|
|
|
|
or flt(entry.credit, precision) != flt(e.credit, precision)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2021-02-23 12:20:49 +00:00
|
|
|
):
|
2020-12-21 09:15:50 +00:00
|
|
|
matched = False
|
|
|
|
break
|
|
|
|
if not account_existed:
|
|
|
|
matched = False
|
|
|
|
break
|
2020-12-25 12:42:35 +00:00
|
|
|
return matched
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-12-25 12:42:35 +00:00
|
|
|
def get_stock_accounts(company, voucher_type=None, voucher_no=None):
|
|
|
|
stock_accounts = [
|
|
|
|
d.name
|
|
|
|
for d in frappe.db.get_all(
|
|
|
|
"Account", {"account_type": "Stock", "company": company, "is_group": 0}
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-12-25 12:42:35 +00:00
|
|
|
]
|
|
|
|
if voucher_type and voucher_no:
|
|
|
|
if voucher_type == "Journal Entry":
|
|
|
|
stock_accounts = [
|
|
|
|
d.account
|
|
|
|
for d in frappe.db.get_all(
|
|
|
|
"Journal Entry Account", {"parent": voucher_no, "account": ["in", stock_accounts]}, "account"
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-12-25 12:42:35 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
else:
|
|
|
|
stock_accounts = [
|
|
|
|
d.account
|
|
|
|
for d in frappe.db.get_all(
|
|
|
|
"GL Entry",
|
|
|
|
{"voucher_type": voucher_type, "voucher_no": voucher_no, "account": ["in", stock_accounts]},
|
|
|
|
"account",
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2020-12-25 12:42:35 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
return stock_accounts
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-12-25 12:42:35 +00:00
|
|
|
def get_stock_and_account_balance(account=None, posting_date=None, company=None):
|
|
|
|
if not posting_date:
|
|
|
|
posting_date = nowdate()
|
|
|
|
|
|
|
|
warehouse_account = get_warehouse_account_map(company)
|
|
|
|
|
|
|
|
account_balance = get_balance_on(
|
|
|
|
account, posting_date, in_account_currency=False, ignore_account_permission=True
|
|
|
|
)
|
|
|
|
|
|
|
|
related_warehouses = [
|
|
|
|
wh
|
|
|
|
for wh, wh_details in warehouse_account.items()
|
|
|
|
if wh_details.account == account and not wh_details.is_group
|
|
|
|
]
|
|
|
|
|
2023-04-25 08:24:36 +00:00
|
|
|
total_stock_value = get_stock_value_on(related_warehouses, posting_date)
|
2020-12-25 12:42:35 +00:00
|
|
|
|
|
|
|
precision = frappe.get_precision("Journal Entry Account", "debit_in_account_currency")
|
|
|
|
return flt(account_balance, precision), flt(total_stock_value, precision), related_warehouses
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2020-12-25 12:42:35 +00:00
|
|
|
def get_journal_entry(account, stock_adjustment_account, amount):
|
|
|
|
db_or_cr_warehouse_account = (
|
|
|
|
"credit_in_account_currency" if amount < 0 else "debit_in_account_currency"
|
|
|
|
)
|
|
|
|
db_or_cr_stock_adjustment_account = (
|
|
|
|
"debit_in_account_currency" if amount < 0 else "credit_in_account_currency"
|
|
|
|
)
|
|
|
|
|
|
|
|
return {
|
|
|
|
"accounts": [
|
|
|
|
{"account": account, db_or_cr_warehouse_account: abs(amount)},
|
|
|
|
{"account": stock_adjustment_account, db_or_cr_stock_adjustment_account: abs(amount)},
|
|
|
|
]
|
|
|
|
}
|
2021-08-27 12:30:16 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2021-08-27 12:30:16 +00:00
|
|
|
def check_and_delete_linked_reports(report):
|
|
|
|
"""Check if reports are referenced in Desktop Icon"""
|
|
|
|
icons = frappe.get_all("Desktop Icon", fields=["name"], filters={"_report": report})
|
|
|
|
if icons:
|
|
|
|
for icon in icons:
|
|
|
|
frappe.delete_doc("Desktop Icon", icon)
|
2022-05-16 08:59:58 +00:00
|
|
|
|
|
|
|
|
2022-10-13 08:43:48 +00:00
|
|
|
def get_payment_ledger_entries(gl_entries, cancel=0):
|
|
|
|
ple_map = []
|
2022-05-16 08:59:58 +00:00
|
|
|
if gl_entries:
|
|
|
|
ple = None
|
|
|
|
|
|
|
|
# companies
|
|
|
|
account = qb.DocType("Account")
|
|
|
|
companies = list(set([x.company for x in gl_entries]))
|
|
|
|
|
|
|
|
# receivable/payable account
|
|
|
|
accounts_with_types = (
|
|
|
|
qb.from_(account)
|
|
|
|
.select(account.name, account.account_type)
|
|
|
|
.where(
|
|
|
|
(account.account_type.isin(["Receivable", "Payable"]) & (account.company.isin(companies)))
|
|
|
|
)
|
|
|
|
.run(as_dict=True)
|
|
|
|
)
|
|
|
|
receivable_or_payable_accounts = [y.name for y in accounts_with_types]
|
|
|
|
|
|
|
|
def get_account_type(account):
|
|
|
|
for entry in accounts_with_types:
|
|
|
|
if entry.name == account:
|
|
|
|
return entry.account_type
|
|
|
|
|
|
|
|
dr_or_cr = 0
|
|
|
|
account_type = None
|
|
|
|
for gle in gl_entries:
|
|
|
|
if gle.account in receivable_or_payable_accounts:
|
|
|
|
account_type = get_account_type(gle.account)
|
|
|
|
if account_type == "Receivable":
|
|
|
|
dr_or_cr = gle.debit - gle.credit
|
|
|
|
dr_or_cr_account_currency = gle.debit_in_account_currency - gle.credit_in_account_currency
|
|
|
|
elif account_type == "Payable":
|
|
|
|
dr_or_cr = gle.credit - gle.debit
|
|
|
|
dr_or_cr_account_currency = gle.credit_in_account_currency - gle.debit_in_account_currency
|
|
|
|
|
|
|
|
if cancel:
|
|
|
|
dr_or_cr *= -1
|
|
|
|
dr_or_cr_account_currency *= -1
|
|
|
|
|
2022-10-13 08:43:48 +00:00
|
|
|
ple = frappe._dict(
|
|
|
|
doctype="Payment Ledger Entry",
|
|
|
|
posting_date=gle.posting_date,
|
|
|
|
company=gle.company,
|
|
|
|
account_type=account_type,
|
|
|
|
account=gle.account,
|
|
|
|
party_type=gle.party_type,
|
|
|
|
party=gle.party,
|
|
|
|
cost_center=gle.cost_center,
|
|
|
|
finance_book=gle.finance_book,
|
|
|
|
due_date=gle.due_date,
|
|
|
|
voucher_type=gle.voucher_type,
|
|
|
|
voucher_no=gle.voucher_no,
|
2023-06-29 06:48:25 +00:00
|
|
|
voucher_detail_no=gle.voucher_detail_no,
|
2022-10-13 08:43:48 +00:00
|
|
|
against_voucher_type=gle.against_voucher_type
|
|
|
|
if gle.against_voucher_type
|
|
|
|
else gle.voucher_type,
|
|
|
|
against_voucher_no=gle.against_voucher if gle.against_voucher else gle.voucher_no,
|
|
|
|
account_currency=gle.account_currency,
|
|
|
|
amount=dr_or_cr,
|
|
|
|
amount_in_account_currency=dr_or_cr_account_currency,
|
|
|
|
delinked=True if cancel else False,
|
|
|
|
remarks=gle.remarks,
|
2022-05-16 08:59:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
dimensions_and_defaults = get_dimensions()
|
|
|
|
if dimensions_and_defaults:
|
|
|
|
for dimension in dimensions_and_defaults[0]:
|
2022-10-13 08:43:48 +00:00
|
|
|
ple[dimension.fieldname] = gle.get(dimension.fieldname)
|
2022-05-16 08:59:58 +00:00
|
|
|
|
2022-10-13 08:43:48 +00:00
|
|
|
ple_map.append(ple)
|
|
|
|
return ple_map
|
|
|
|
|
|
|
|
|
|
|
|
def create_payment_ledger_entry(
|
2023-06-29 06:48:25 +00:00
|
|
|
gl_entries, cancel=0, adv_adj=0, update_outstanding="Yes", from_repost=0, partial_cancel=False
|
2022-10-13 08:43:48 +00:00
|
|
|
):
|
|
|
|
if gl_entries:
|
|
|
|
ple_map = get_payment_ledger_entries(gl_entries, cancel=cancel)
|
|
|
|
|
|
|
|
for entry in ple_map:
|
|
|
|
|
|
|
|
ple = frappe.get_doc(entry)
|
|
|
|
|
|
|
|
if cancel:
|
2023-06-29 06:48:25 +00:00
|
|
|
delink_original_entry(ple, partial_cancel=partial_cancel)
|
2022-10-13 08:43:48 +00:00
|
|
|
|
|
|
|
ple.flags.ignore_permissions = 1
|
|
|
|
ple.flags.adv_adj = adv_adj
|
|
|
|
ple.flags.from_repost = from_repost
|
|
|
|
ple.flags.update_outstanding = update_outstanding
|
|
|
|
ple.submit()
|
2022-05-16 08:59:58 +00:00
|
|
|
|
|
|
|
|
2022-05-29 16:03:08 +00:00
|
|
|
def update_voucher_outstanding(voucher_type, voucher_no, account, party_type, party):
|
|
|
|
ple = frappe.qb.DocType("Payment Ledger Entry")
|
|
|
|
vouchers = [frappe._dict({"voucher_type": voucher_type, "voucher_no": voucher_no})]
|
|
|
|
common_filter = []
|
|
|
|
if account:
|
|
|
|
common_filter.append(ple.account == account)
|
|
|
|
|
|
|
|
if party_type:
|
|
|
|
common_filter.append(ple.party_type == party_type)
|
|
|
|
|
|
|
|
if party:
|
|
|
|
common_filter.append(ple.party == party)
|
|
|
|
|
|
|
|
ple_query = QueryPaymentLedger()
|
|
|
|
|
|
|
|
# on cancellation outstanding can be an empty list
|
|
|
|
voucher_outstanding = ple_query.get_voucher_outstandings(vouchers, common_filter=common_filter)
|
2022-10-18 04:03:37 +00:00
|
|
|
if (
|
|
|
|
voucher_type in ["Sales Invoice", "Purchase Invoice", "Fees"]
|
|
|
|
and party_type
|
|
|
|
and party
|
|
|
|
and voucher_outstanding
|
|
|
|
):
|
2022-05-29 16:03:08 +00:00
|
|
|
outstanding = voucher_outstanding[0]
|
|
|
|
ref_doc = frappe.get_doc(voucher_type, voucher_no)
|
|
|
|
|
|
|
|
# Didn't use db_set for optimisation purpose
|
2023-02-12 08:36:40 +00:00
|
|
|
ref_doc.outstanding_amount = outstanding["outstanding_in_account_currency"] or 0.0
|
2022-05-29 16:03:08 +00:00
|
|
|
frappe.db.set_value(
|
2023-02-12 08:36:40 +00:00
|
|
|
voucher_type,
|
|
|
|
voucher_no,
|
|
|
|
"outstanding_amount",
|
|
|
|
outstanding["outstanding_in_account_currency"] or 0.0,
|
2022-05-29 16:03:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
ref_doc.set_status(update=True)
|
|
|
|
|
|
|
|
|
2023-06-29 06:48:25 +00:00
|
|
|
def delink_original_entry(pl_entry, partial_cancel=False):
|
2022-05-16 08:59:58 +00:00
|
|
|
if pl_entry:
|
|
|
|
ple = qb.DocType("Payment Ledger Entry")
|
|
|
|
query = (
|
|
|
|
qb.update(ple)
|
|
|
|
.set(ple.delinked, True)
|
|
|
|
.set(ple.modified, now())
|
|
|
|
.set(ple.modified_by, frappe.session.user)
|
|
|
|
.where(
|
|
|
|
(ple.company == pl_entry.company)
|
|
|
|
& (ple.account_type == pl_entry.account_type)
|
|
|
|
& (ple.account == pl_entry.account)
|
|
|
|
& (ple.party_type == pl_entry.party_type)
|
|
|
|
& (ple.party == pl_entry.party)
|
|
|
|
& (ple.voucher_type == pl_entry.voucher_type)
|
|
|
|
& (ple.voucher_no == pl_entry.voucher_no)
|
|
|
|
& (ple.against_voucher_type == pl_entry.against_voucher_type)
|
|
|
|
& (ple.against_voucher_no == pl_entry.against_voucher_no)
|
|
|
|
)
|
|
|
|
)
|
2023-06-29 06:48:25 +00:00
|
|
|
|
|
|
|
if partial_cancel:
|
|
|
|
query = query.where(ple.voucher_detail_no == pl_entry.voucher_detail_no)
|
|
|
|
|
2022-05-16 08:59:58 +00:00
|
|
|
query.run()
|
2022-05-25 10:21:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class QueryPaymentLedger(object):
|
|
|
|
"""
|
|
|
|
Helper Class for Querying Payment Ledger Entry
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.ple = qb.DocType("Payment Ledger Entry")
|
|
|
|
|
|
|
|
# query result
|
|
|
|
self.voucher_outstandings = []
|
|
|
|
|
|
|
|
# query filters
|
|
|
|
self.vouchers = []
|
|
|
|
self.common_filter = []
|
2022-07-28 06:27:27 +00:00
|
|
|
self.voucher_posting_date = []
|
2022-05-25 10:21:16 +00:00
|
|
|
self.min_outstanding = None
|
|
|
|
self.max_outstanding = None
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
# clear filters
|
|
|
|
self.vouchers.clear()
|
|
|
|
self.common_filter.clear()
|
|
|
|
self.min_outstanding = self.max_outstanding = None
|
|
|
|
|
|
|
|
# clear result
|
|
|
|
self.voucher_outstandings.clear()
|
|
|
|
|
|
|
|
def query_for_outstanding(self):
|
|
|
|
"""
|
|
|
|
Database query to fetch voucher amount and voucher outstanding using Common Table Expression
|
|
|
|
"""
|
|
|
|
|
|
|
|
ple = self.ple
|
|
|
|
|
|
|
|
filter_on_voucher_no = []
|
|
|
|
filter_on_against_voucher_no = []
|
|
|
|
if self.vouchers:
|
|
|
|
voucher_types = set([x.voucher_type for x in self.vouchers])
|
|
|
|
voucher_nos = set([x.voucher_no for x in self.vouchers])
|
|
|
|
|
|
|
|
filter_on_voucher_no.append(ple.voucher_type.isin(voucher_types))
|
|
|
|
filter_on_voucher_no.append(ple.voucher_no.isin(voucher_nos))
|
|
|
|
|
|
|
|
filter_on_against_voucher_no.append(ple.against_voucher_type.isin(voucher_types))
|
|
|
|
filter_on_against_voucher_no.append(ple.against_voucher_no.isin(voucher_nos))
|
|
|
|
|
|
|
|
# build outstanding amount filter
|
|
|
|
filter_on_outstanding_amount = []
|
|
|
|
if self.min_outstanding:
|
|
|
|
if self.min_outstanding > 0:
|
|
|
|
filter_on_outstanding_amount.append(
|
|
|
|
Table("outstanding").amount_in_account_currency >= self.min_outstanding
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
filter_on_outstanding_amount.append(
|
|
|
|
Table("outstanding").amount_in_account_currency <= self.min_outstanding
|
|
|
|
)
|
|
|
|
if self.max_outstanding:
|
|
|
|
if self.max_outstanding > 0:
|
|
|
|
filter_on_outstanding_amount.append(
|
|
|
|
Table("outstanding").amount_in_account_currency <= self.max_outstanding
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
filter_on_outstanding_amount.append(
|
|
|
|
Table("outstanding").amount_in_account_currency >= self.max_outstanding
|
|
|
|
)
|
|
|
|
|
|
|
|
# build query for voucher amount
|
|
|
|
query_voucher_amount = (
|
|
|
|
qb.from_(ple)
|
|
|
|
.select(
|
|
|
|
ple.account,
|
|
|
|
ple.voucher_type,
|
|
|
|
ple.voucher_no,
|
|
|
|
ple.party_type,
|
|
|
|
ple.party,
|
|
|
|
ple.posting_date,
|
|
|
|
ple.due_date,
|
|
|
|
ple.account_currency.as_("currency"),
|
|
|
|
Sum(ple.amount).as_("amount"),
|
|
|
|
Sum(ple.amount_in_account_currency).as_("amount_in_account_currency"),
|
|
|
|
)
|
|
|
|
.where(ple.delinked == 0)
|
|
|
|
.where(Criterion.all(filter_on_voucher_no))
|
|
|
|
.where(Criterion.all(self.common_filter))
|
2022-12-14 10:35:15 +00:00
|
|
|
.where(Criterion.all(self.dimensions_filter))
|
2022-07-28 06:27:27 +00:00
|
|
|
.where(Criterion.all(self.voucher_posting_date))
|
2022-05-25 10:21:16 +00:00
|
|
|
.groupby(ple.voucher_type, ple.voucher_no, ple.party_type, ple.party)
|
|
|
|
)
|
|
|
|
|
|
|
|
# build query for voucher outstanding
|
|
|
|
query_voucher_outstanding = (
|
|
|
|
qb.from_(ple)
|
|
|
|
.select(
|
|
|
|
ple.account,
|
|
|
|
ple.against_voucher_type.as_("voucher_type"),
|
|
|
|
ple.against_voucher_no.as_("voucher_no"),
|
|
|
|
ple.party_type,
|
|
|
|
ple.party,
|
|
|
|
ple.posting_date,
|
|
|
|
ple.due_date,
|
|
|
|
ple.account_currency.as_("currency"),
|
|
|
|
Sum(ple.amount).as_("amount"),
|
|
|
|
Sum(ple.amount_in_account_currency).as_("amount_in_account_currency"),
|
|
|
|
)
|
|
|
|
.where(ple.delinked == 0)
|
|
|
|
.where(Criterion.all(filter_on_against_voucher_no))
|
|
|
|
.where(Criterion.all(self.common_filter))
|
|
|
|
.groupby(ple.against_voucher_type, ple.against_voucher_no, ple.party_type, ple.party)
|
|
|
|
)
|
|
|
|
|
|
|
|
# build CTE for combining voucher amount and outstanding
|
|
|
|
self.cte_query_voucher_amount_and_outstanding = (
|
|
|
|
qb.with_(query_voucher_amount, "vouchers")
|
|
|
|
.with_(query_voucher_outstanding, "outstanding")
|
|
|
|
.from_(AliasedQuery("vouchers"))
|
|
|
|
.left_join(AliasedQuery("outstanding"))
|
|
|
|
.on(
|
|
|
|
(AliasedQuery("vouchers").account == AliasedQuery("outstanding").account)
|
|
|
|
& (AliasedQuery("vouchers").voucher_type == AliasedQuery("outstanding").voucher_type)
|
|
|
|
& (AliasedQuery("vouchers").voucher_no == AliasedQuery("outstanding").voucher_no)
|
|
|
|
& (AliasedQuery("vouchers").party_type == AliasedQuery("outstanding").party_type)
|
|
|
|
& (AliasedQuery("vouchers").party == AliasedQuery("outstanding").party)
|
|
|
|
)
|
|
|
|
.select(
|
|
|
|
Table("vouchers").account,
|
|
|
|
Table("vouchers").voucher_type,
|
|
|
|
Table("vouchers").voucher_no,
|
|
|
|
Table("vouchers").party_type,
|
|
|
|
Table("vouchers").party,
|
|
|
|
Table("vouchers").posting_date,
|
|
|
|
Table("vouchers").amount.as_("invoice_amount"),
|
|
|
|
Table("vouchers").amount_in_account_currency.as_("invoice_amount_in_account_currency"),
|
|
|
|
Table("outstanding").amount.as_("outstanding"),
|
|
|
|
Table("outstanding").amount_in_account_currency.as_("outstanding_in_account_currency"),
|
|
|
|
(Table("vouchers").amount - Table("outstanding").amount).as_("paid_amount"),
|
|
|
|
(
|
|
|
|
Table("vouchers").amount_in_account_currency - Table("outstanding").amount_in_account_currency
|
|
|
|
).as_("paid_amount_in_account_currency"),
|
|
|
|
Table("vouchers").due_date,
|
|
|
|
Table("vouchers").currency,
|
|
|
|
)
|
|
|
|
.where(Criterion.all(filter_on_outstanding_amount))
|
|
|
|
)
|
|
|
|
|
|
|
|
# build CTE filter
|
|
|
|
# only fetch invoices
|
|
|
|
if self.get_invoices:
|
|
|
|
self.cte_query_voucher_amount_and_outstanding = (
|
|
|
|
self.cte_query_voucher_amount_and_outstanding.having(
|
|
|
|
qb.Field("outstanding_in_account_currency") > 0
|
|
|
|
)
|
|
|
|
)
|
|
|
|
# only fetch payments
|
|
|
|
elif self.get_payments:
|
|
|
|
self.cte_query_voucher_amount_and_outstanding = (
|
|
|
|
self.cte_query_voucher_amount_and_outstanding.having(
|
|
|
|
qb.Field("outstanding_in_account_currency") < 0
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# execute SQL
|
|
|
|
self.voucher_outstandings = self.cte_query_voucher_amount_and_outstanding.run(as_dict=True)
|
|
|
|
|
|
|
|
def get_voucher_outstandings(
|
|
|
|
self,
|
|
|
|
vouchers=None,
|
|
|
|
common_filter=None,
|
2022-07-28 06:27:27 +00:00
|
|
|
posting_date=None,
|
2022-05-25 10:21:16 +00:00
|
|
|
min_outstanding=None,
|
|
|
|
max_outstanding=None,
|
|
|
|
get_payments=False,
|
|
|
|
get_invoices=False,
|
2022-12-14 10:35:15 +00:00
|
|
|
accounting_dimensions=None,
|
2022-05-25 10:21:16 +00:00
|
|
|
):
|
|
|
|
"""
|
|
|
|
Fetch voucher amount and outstanding amount from Payment Ledger using Database CTE
|
|
|
|
|
|
|
|
vouchers - dict of vouchers to get
|
|
|
|
common_filter - array of criterions
|
|
|
|
min_outstanding - filter on minimum total outstanding amount
|
|
|
|
max_outstanding - filter on maximum total outstanding amount
|
|
|
|
get_invoices - only fetch vouchers(ledger entries with +ve outstanding)
|
|
|
|
get_payments - only fetch payments(ledger entries with -ve outstanding)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.reset()
|
|
|
|
self.vouchers = vouchers
|
|
|
|
self.common_filter = common_filter or []
|
2022-12-14 10:35:15 +00:00
|
|
|
self.dimensions_filter = accounting_dimensions or []
|
2022-07-28 06:27:27 +00:00
|
|
|
self.voucher_posting_date = posting_date or []
|
2022-05-25 10:21:16 +00:00
|
|
|
self.min_outstanding = min_outstanding
|
|
|
|
self.max_outstanding = max_outstanding
|
|
|
|
self.get_payments = get_payments
|
|
|
|
self.get_invoices = get_invoices
|
|
|
|
self.query_for_outstanding()
|
|
|
|
|
|
|
|
return self.voucher_outstandings
|