2018-02-12 09:34:50 +00:00
|
|
|
import frappe
|
2023-07-12 09:13:18 +00:00
|
|
|
from frappe.query_builder.custom import ConstantColumn
|
2023-07-12 11:12:58 +00:00
|
|
|
from frappe.query_builder.functions import Sum
|
2023-07-04 12:11:30 +00:00
|
|
|
from frappe.utils import flt, formatdate, get_datetime_str, get_table_name
|
2023-07-12 09:13:18 +00:00
|
|
|
from pypika import Order
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
from erpnext import get_company_currency, get_default_company
|
2023-07-12 09:13:18 +00:00
|
|
|
from erpnext.accounts.doctype.accounting_dimension.accounting_dimension import (
|
2023-07-14 05:35:50 +00:00
|
|
|
get_accounting_dimensions,
|
2023-07-12 09:13:18 +00:00
|
|
|
get_dimension_with_children,
|
|
|
|
)
|
2018-07-18 12:33:27 +00:00
|
|
|
from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
|
2023-07-14 05:20:12 +00:00
|
|
|
from erpnext.accounts.party import get_party_account
|
2021-09-02 11:14:59 +00:00
|
|
|
from erpnext.setup.utils import get_exchange_rate
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
__exchange_rates = {}
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
def get_currency(filters):
|
|
|
|
"""
|
|
|
|
Returns a dictionary containing currency information. The keys of the dict are
|
|
|
|
- company: The company for which we are fetching currency information. if no
|
|
|
|
company is specified, it will fallback to the default company.
|
|
|
|
- company currency: The functional currency of the said company.
|
|
|
|
- presentation currency: The presentation currency to use. Only currencies that
|
|
|
|
have been used for transactions will be allowed.
|
|
|
|
- report date: The report date.
|
|
|
|
:param filters: Report filters
|
|
|
|
:type filters: dict
|
|
|
|
|
|
|
|
:return: str - Currency
|
|
|
|
"""
|
|
|
|
company = get_appropriate_company(filters)
|
|
|
|
company_currency = get_company_currency(company)
|
2022-03-28 13:22:46 +00:00
|
|
|
presentation_currency = (
|
|
|
|
filters["presentation_currency"] if filters.get("presentation_currency") else company_currency
|
|
|
|
)
|
2018-07-18 12:33:27 +00:00
|
|
|
|
2022-12-06 08:14:54 +00:00
|
|
|
report_date = filters.get("to_date") or filters.get("period_end_date")
|
2018-07-18 12:33:27 +00:00
|
|
|
|
|
|
|
if not report_date:
|
2022-03-28 13:22:46 +00:00
|
|
|
fiscal_year_to_date = get_from_and_to_date(filters.get("to_fiscal_year"))["to_date"]
|
2018-07-18 12:33:27 +00:00
|
|
|
report_date = formatdate(get_datetime_str(fiscal_year_to_date), "dd-MM-yyyy")
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
currency_map = dict(
|
|
|
|
company=company,
|
|
|
|
company_currency=company_currency,
|
|
|
|
presentation_currency=presentation_currency,
|
|
|
|
report_date=report_date,
|
|
|
|
)
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
return currency_map
|
|
|
|
|
|
|
|
|
|
|
|
def convert(value, from_, to, date):
|
|
|
|
"""
|
|
|
|
convert `value` from `from_` to `to` on `date`
|
|
|
|
:param value: Amount to be converted
|
|
|
|
:param from_: Currency of `value`
|
|
|
|
:param to: Currency to convert to
|
|
|
|
:param date: exchange rate as at this date
|
|
|
|
:return: Result of converting `value`
|
|
|
|
"""
|
|
|
|
rate = get_rate_as_at(date, from_, to)
|
2018-08-14 10:58:14 +00:00
|
|
|
converted_value = flt(value) / (rate or 1)
|
2018-02-12 09:34:50 +00:00
|
|
|
return converted_value
|
|
|
|
|
|
|
|
|
|
|
|
def get_rate_as_at(date, from_currency, to_currency):
|
|
|
|
"""
|
|
|
|
Gets exchange rate as at `date` for `from_currency` - `to_currency` exchange rate.
|
|
|
|
This calls `get_exchange_rate` so that we can get the correct exchange rate as per
|
|
|
|
the user's Accounts Settings.
|
|
|
|
It is made efficient by memoising results to `__exchange_rates`
|
|
|
|
:param date: exchange rate as at this date
|
|
|
|
:param from_currency: Base currency
|
|
|
|
:param to_currency: Quote currency
|
|
|
|
:return: Retrieved exchange rate
|
|
|
|
"""
|
2018-07-18 12:33:27 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
rate = __exchange_rates.get("{0}-{1}@{2}".format(from_currency, to_currency, date))
|
2018-02-12 09:34:50 +00:00
|
|
|
if not rate:
|
|
|
|
rate = get_exchange_rate(from_currency, to_currency, date) or 1
|
2022-03-28 13:22:46 +00:00
|
|
|
__exchange_rates["{0}-{1}@{2}".format(from_currency, to_currency, date)] = rate
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
return rate
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2023-07-09 14:46:12 +00:00
|
|
|
def convert_to_presentation_currency(gl_entries, currency_info):
|
2018-02-12 09:34:50 +00:00
|
|
|
"""
|
|
|
|
Take a list of GL Entries and change the 'debit' and 'credit' values to currencies
|
|
|
|
in `currency_info`.
|
|
|
|
:param gl_entries:
|
|
|
|
:param currency_info:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
converted_gl_list = []
|
2022-03-28 13:22:46 +00:00
|
|
|
presentation_currency = currency_info["presentation_currency"]
|
|
|
|
company_currency = currency_info["company_currency"]
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
account_currencies = list(set(entry["account_currency"] for entry in gl_entries))
|
2020-08-22 07:01:06 +00:00
|
|
|
|
2018-02-12 09:34:50 +00:00
|
|
|
for entry in gl_entries:
|
2022-03-28 13:22:46 +00:00
|
|
|
debit = flt(entry["debit"])
|
|
|
|
credit = flt(entry["credit"])
|
|
|
|
debit_in_account_currency = flt(entry["debit_in_account_currency"])
|
|
|
|
credit_in_account_currency = flt(entry["credit_in_account_currency"])
|
|
|
|
account_currency = entry["account_currency"]
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2021-05-25 13:46:02 +00:00
|
|
|
if len(account_currencies) == 1 and account_currency == presentation_currency:
|
2023-01-02 09:03:14 +00:00
|
|
|
entry["debit"] = debit_in_account_currency
|
|
|
|
entry["credit"] = credit_in_account_currency
|
2021-05-25 13:46:02 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
date = currency_info["report_date"]
|
2021-09-05 11:51:29 +00:00
|
|
|
converted_debit_value = convert(debit, presentation_currency, company_currency, date)
|
|
|
|
converted_credit_value = convert(credit, presentation_currency, company_currency, date)
|
2018-02-12 09:34:50 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if entry.get("debit"):
|
|
|
|
entry["debit"] = converted_debit_value
|
2019-04-18 16:31:33 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
if entry.get("credit"):
|
|
|
|
entry["credit"] = converted_credit_value
|
2018-02-12 09:34:50 +00:00
|
|
|
|
|
|
|
converted_gl_list.append(entry)
|
|
|
|
|
|
|
|
return converted_gl_list
|
|
|
|
|
|
|
|
|
|
|
|
def get_appropriate_company(filters):
|
2022-03-28 13:22:46 +00:00
|
|
|
if filters.get("company"):
|
|
|
|
company = filters["company"]
|
2018-02-12 09:34:50 +00:00
|
|
|
else:
|
|
|
|
company = get_default_company()
|
|
|
|
|
|
|
|
return company
|
2019-04-23 13:03:01 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-04-23 13:03:01 +00:00
|
|
|
@frappe.whitelist()
|
2022-03-28 13:22:46 +00:00
|
|
|
def get_invoiced_item_gross_margin(
|
|
|
|
sales_invoice=None, item_code=None, company=None, with_item_data=False
|
|
|
|
):
|
2019-04-23 13:03:01 +00:00
|
|
|
from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
sales_invoice = sales_invoice or frappe.form_dict.get("sales_invoice")
|
|
|
|
item_code = item_code or frappe.form_dict.get("item_code")
|
|
|
|
company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, "company")
|
2019-04-23 13:03:01 +00:00
|
|
|
|
|
|
|
filters = {
|
2022-03-28 13:22:46 +00:00
|
|
|
"sales_invoice": sales_invoice,
|
|
|
|
"item_code": item_code,
|
|
|
|
"company": company,
|
|
|
|
"group_by": "Invoice",
|
2019-04-23 13:03:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gross_profit_data = GrossProfitGenerator(filters)
|
2019-05-08 10:01:29 +00:00
|
|
|
result = gross_profit_data.grouped_data
|
|
|
|
if not with_item_data:
|
2021-06-11 13:10:22 +00:00
|
|
|
result = sum(d.gross_profit for d in result)
|
2019-04-23 13:03:01 +00:00
|
|
|
|
2019-05-08 10:01:29 +00:00
|
|
|
return result
|
2023-07-10 13:09:35 +00:00
|
|
|
|
|
|
|
|
2023-07-12 09:13:18 +00:00
|
|
|
def get_query_columns(report_columns):
|
|
|
|
if not report_columns:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
columns = []
|
|
|
|
for column in report_columns:
|
|
|
|
fieldname = column["fieldname"]
|
|
|
|
|
|
|
|
if doctype := column.get("_doctype"):
|
|
|
|
columns.append(f"`{get_table_name(doctype)}`.`{fieldname}`")
|
|
|
|
else:
|
|
|
|
columns.append(fieldname)
|
|
|
|
|
2023-07-14 07:33:22 +00:00
|
|
|
return columns
|
2023-07-12 09:13:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_values_for_columns(report_columns, report_row):
|
|
|
|
values = {}
|
|
|
|
|
|
|
|
if not report_columns:
|
|
|
|
return values
|
|
|
|
|
|
|
|
for column in report_columns:
|
|
|
|
fieldname = column["fieldname"]
|
|
|
|
values[fieldname] = report_row.get(fieldname)
|
|
|
|
|
|
|
|
return values
|
|
|
|
|
|
|
|
|
2023-07-10 13:09:35 +00:00
|
|
|
def get_party_details(party_type, party_list):
|
|
|
|
party_details = {}
|
|
|
|
party = frappe.qb.DocType(party_type)
|
|
|
|
query = frappe.qb.from_(party).select(party.name, party.tax_id).where(party.name.isin(party_list))
|
|
|
|
if party_type == "Supplier":
|
|
|
|
query = query.select(party.supplier_group)
|
|
|
|
else:
|
|
|
|
query = query.select(party.customer_group, party.territory)
|
|
|
|
|
|
|
|
party_detail_list = query.run(as_dict=True)
|
|
|
|
for party_dict in party_detail_list:
|
|
|
|
party_details[party_dict.name] = party_dict
|
|
|
|
return party_details
|
|
|
|
|
|
|
|
|
|
|
|
def get_taxes_query(invoice_list, doctype, parenttype):
|
|
|
|
taxes = frappe.qb.DocType(doctype)
|
|
|
|
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(taxes)
|
|
|
|
.select(taxes.account_head)
|
|
|
|
.distinct()
|
|
|
|
.where(
|
|
|
|
(taxes.parenttype == parenttype)
|
|
|
|
& (taxes.docstatus == 1)
|
|
|
|
& (taxes.account_head.isnotnull())
|
|
|
|
& (taxes.parent.isin([inv.name for inv in invoice_list]))
|
|
|
|
)
|
|
|
|
.orderby(taxes.account_head)
|
|
|
|
)
|
|
|
|
|
|
|
|
if doctype == "Purchase Taxes and Charges":
|
|
|
|
return query.where(taxes.category.isin(["Total", "Valuation and Total"]))
|
|
|
|
elif doctype == "Sales Taxes and Charges":
|
2023-07-12 11:12:58 +00:00
|
|
|
return query
|
2023-07-10 13:09:35 +00:00
|
|
|
return query.where(taxes.charge_type.isin(["On Paid Amount", "Actual"]))
|
2023-07-11 09:17:23 +00:00
|
|
|
|
|
|
|
|
2023-07-12 11:47:58 +00:00
|
|
|
def get_journal_entries(filters, args):
|
2023-07-12 09:13:18 +00:00
|
|
|
je = frappe.qb.DocType("Journal Entry")
|
|
|
|
journal_account = frappe.qb.DocType("Journal Entry Account")
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(je)
|
|
|
|
.inner_join(journal_account)
|
|
|
|
.on(je.name == journal_account.parent)
|
|
|
|
.select(
|
|
|
|
je.voucher_type.as_("doctype"),
|
|
|
|
je.name,
|
|
|
|
je.posting_date,
|
|
|
|
journal_account.account.as_(args.account),
|
|
|
|
journal_account.party.as_(args.party),
|
|
|
|
journal_account.party.as_(args.party_name),
|
|
|
|
je.bill_no,
|
|
|
|
je.bill_date,
|
|
|
|
je.remark.as_("remarks"),
|
|
|
|
je.total_amount.as_("base_net_total"),
|
|
|
|
je.total_amount.as_("base_grand_total"),
|
|
|
|
je.mode_of_payment,
|
|
|
|
journal_account.project,
|
|
|
|
)
|
2023-07-14 05:20:12 +00:00
|
|
|
.where(
|
|
|
|
(je.voucher_type == "Journal Entry")
|
|
|
|
& (journal_account.party == filters.get(args.party))
|
|
|
|
& (journal_account.account.isin(args.party_account))
|
|
|
|
)
|
2023-07-12 09:13:18 +00:00
|
|
|
.orderby(je.posting_date, je.name, order=Order.desc)
|
2023-07-11 09:17:23 +00:00
|
|
|
)
|
2023-07-21 05:27:55 +00:00
|
|
|
query = get_conditions(filters, query, doctype="Journal Entry", payments=True)
|
2023-07-12 11:12:58 +00:00
|
|
|
journal_entries = query.run(as_dict=True)
|
2023-07-12 09:13:18 +00:00
|
|
|
return journal_entries
|
2023-07-11 09:17:23 +00:00
|
|
|
|
|
|
|
|
2023-07-12 11:47:58 +00:00
|
|
|
def get_payment_entries(filters, args):
|
2023-07-12 09:13:18 +00:00
|
|
|
pe = frappe.qb.DocType("Payment Entry")
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(pe)
|
|
|
|
.select(
|
|
|
|
ConstantColumn("Payment Entry").as_("doctype"),
|
|
|
|
pe.name,
|
|
|
|
pe.posting_date,
|
2023-07-27 04:38:26 +00:00
|
|
|
pe[args.account_fieldname].as_(args.account),
|
2023-07-12 09:13:18 +00:00
|
|
|
pe.party.as_(args.party),
|
|
|
|
pe.party_name.as_(args.party_name),
|
|
|
|
pe.remarks,
|
|
|
|
pe.paid_amount.as_("base_net_total"),
|
|
|
|
pe.paid_amount_after_tax.as_("base_grand_total"),
|
|
|
|
pe.mode_of_payment,
|
|
|
|
pe.project,
|
|
|
|
pe.cost_center,
|
|
|
|
)
|
2023-07-27 04:38:26 +00:00
|
|
|
.where(
|
|
|
|
(pe.party == filters.get(args.party)) & (pe[args.account_fieldname].isin(args.party_account))
|
|
|
|
)
|
2023-07-12 09:13:18 +00:00
|
|
|
.orderby(pe.posting_date, pe.name, order=Order.desc)
|
2023-07-11 09:17:23 +00:00
|
|
|
)
|
2023-07-21 05:27:55 +00:00
|
|
|
query = get_conditions(filters, query, doctype="Payment Entry", payments=True)
|
2023-07-12 11:12:58 +00:00
|
|
|
payment_entries = query.run(as_dict=True)
|
2023-07-12 09:13:18 +00:00
|
|
|
return payment_entries
|
|
|
|
|
|
|
|
|
2023-07-21 05:27:55 +00:00
|
|
|
def get_conditions(filters, query, doctype, child_doctype=None, payments=False):
|
|
|
|
parent_doc = frappe.qb.DocType(doctype)
|
|
|
|
if child_doctype:
|
|
|
|
child_doc = frappe.qb.DocType(child_doctype)
|
2023-07-12 09:13:18 +00:00
|
|
|
|
|
|
|
if parent_doc.get_table_name() == "tabSales Invoice":
|
|
|
|
if filters.get("owner"):
|
|
|
|
query = query.where(parent_doc.owner == filters.owner)
|
|
|
|
if filters.get("mode_of_payment"):
|
2023-07-21 05:27:55 +00:00
|
|
|
payment_doc = frappe.qb.DocType("Sales Invoice Payment")
|
2023-07-12 09:13:18 +00:00
|
|
|
query = query.where(payment_doc.mode_of_payment == filters.mode_of_payment)
|
|
|
|
if not payments:
|
|
|
|
if filters.get("brand"):
|
|
|
|
query = query.where(child_doc.brand == filters.brand)
|
|
|
|
else:
|
|
|
|
if filters.get("mode_of_payment"):
|
|
|
|
query = query.where(parent_doc.mode_of_payment == filters.mode_of_payment)
|
2023-07-12 05:30:35 +00:00
|
|
|
|
2023-07-12 09:13:18 +00:00
|
|
|
if filters.get("company"):
|
|
|
|
query = query.where(parent_doc.company == filters.company)
|
|
|
|
if filters.get("from_date"):
|
|
|
|
query = query.where(parent_doc.posting_date >= filters.from_date)
|
|
|
|
if filters.get("to_date"):
|
|
|
|
query = query.where(parent_doc.posting_date <= filters.to_date)
|
|
|
|
|
|
|
|
if payments:
|
|
|
|
if filters.get("cost_center"):
|
|
|
|
query = query.where(parent_doc.cost_center == filters.cost_center)
|
|
|
|
else:
|
|
|
|
if filters.get("cost_center"):
|
|
|
|
query = query.where(child_doc.cost_center == filters.cost_center)
|
|
|
|
if filters.get("warehouse"):
|
|
|
|
query = query.where(child_doc.warehouse == filters.warehouse)
|
|
|
|
if filters.get("item_group"):
|
|
|
|
query = query.where(child_doc.item_group == filters.item_group)
|
2023-07-14 05:35:50 +00:00
|
|
|
|
|
|
|
if parent_doc.get_table_name() != "tabJournal Entry":
|
|
|
|
query = filter_invoices_based_on_dimensions(filters, query, parent_doc)
|
2023-07-12 09:13:18 +00:00
|
|
|
return query
|
2023-07-12 11:12:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_advance_taxes_and_charges(invoice_list):
|
|
|
|
adv_taxes = frappe.qb.DocType("Advance Taxes and Charges")
|
|
|
|
return (
|
|
|
|
frappe.qb.from_(adv_taxes)
|
|
|
|
.select(
|
|
|
|
adv_taxes.parent,
|
|
|
|
adv_taxes.account_head,
|
|
|
|
(
|
|
|
|
frappe.qb.terms.Case()
|
|
|
|
.when(adv_taxes.add_deduct_tax == "Add", Sum(adv_taxes.base_tax_amount))
|
|
|
|
.else_(Sum(adv_taxes.base_tax_amount) * -1)
|
|
|
|
).as_("tax_amount"),
|
|
|
|
)
|
|
|
|
.where(
|
|
|
|
(adv_taxes.parent.isin([inv.name for inv in invoice_list]))
|
|
|
|
& (adv_taxes.charge_type.isin(["On Paid Amount", "Actual"]))
|
|
|
|
& (adv_taxes.base_tax_amount != 0)
|
|
|
|
)
|
|
|
|
.groupby(adv_taxes.parent, adv_taxes.account_head, adv_taxes.add_deduct_tax)
|
2023-07-12 11:47:58 +00:00
|
|
|
).run(as_dict=True)
|
|
|
|
|
|
|
|
|
2023-07-14 05:35:50 +00:00
|
|
|
def filter_invoices_based_on_dimensions(filters, query, parent_doc):
|
|
|
|
accounting_dimensions = get_accounting_dimensions(as_list=False)
|
|
|
|
if accounting_dimensions:
|
2023-07-12 11:47:58 +00:00
|
|
|
for dimension in accounting_dimensions:
|
|
|
|
if filters.get(dimension.fieldname):
|
|
|
|
if frappe.get_cached_value("DocType", dimension.document_type, "is_tree"):
|
|
|
|
filters[dimension.fieldname] = get_dimension_with_children(
|
|
|
|
dimension.document_type, filters.get(dimension.fieldname)
|
|
|
|
)
|
|
|
|
fieldname = dimension.fieldname
|
2023-07-14 05:35:50 +00:00
|
|
|
query = query.where(parent_doc[fieldname] == filters.fieldname)
|
|
|
|
return query
|
2023-07-14 05:20:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_opening_row(party_type, party, from_date, company):
|
|
|
|
party_account = get_party_account(party_type, party, company, include_advance=True)
|
|
|
|
gle = frappe.qb.DocType("GL Entry")
|
|
|
|
return (
|
|
|
|
frappe.qb.from_(gle)
|
|
|
|
.select(
|
|
|
|
ConstantColumn("Opening").as_("account"),
|
|
|
|
Sum(gle.debit).as_("debit"),
|
|
|
|
Sum(gle.credit).as_("credit"),
|
|
|
|
(Sum(gle.debit) - Sum(gle.credit)).as_("balance"),
|
|
|
|
)
|
2023-07-21 05:27:55 +00:00
|
|
|
.where(
|
|
|
|
(gle.account.isin(party_account))
|
|
|
|
& (gle.party == party)
|
|
|
|
& (gle.posting_date < from_date)
|
2023-07-21 07:52:01 +00:00
|
|
|
& (gle.is_cancelled == 0)
|
2023-07-21 05:27:55 +00:00
|
|
|
)
|
2023-07-14 05:20:12 +00:00
|
|
|
).run(as_dict=True)
|