chore: Advance fetching order

This commit is contained in:
Deepesh Garg 2023-06-21 12:21:19 +05:30
parent 98cfea6f63
commit 92f845c0e1
2 changed files with 91 additions and 79 deletions

View File

@ -410,7 +410,11 @@ def get_party_account(party_type, party=None, company=None, include_advance=Fals
if include_advance and party_type in ["Customer", "Supplier"]:
advance_account = get_party_advance_account(party_type, party, company)
if advance_account:
return [account, advance_account]
else:
return [account]
return account

View File

@ -892,9 +892,10 @@ class AccountsController(TransactionBase):
amount_field = "debit_in_account_currency"
order_field = "purchase_order"
order_doctype = "Purchase Order"
party_account = [
get_party_account(party_type, party=party, company=self.company, include_advance=True)[1]
]
party_account = get_party_account(
party_type, party=party, company=self.company, include_advance=True
)
order_list = list(set(d.get(order_field) for d in self.get("items") if d.get(order_field)))
@ -2203,37 +2204,59 @@ def get_advance_payment_entries(
condition=None,
):
q = build_query(
payment_entries = []
payment_entry = frappe.qb.DocType("Payment Entry")
if order_list or against_all_orders:
q = get_common_query(
party_type,
party,
party_account,
order_doctype,
order_list,
include_unallocated,
against_all_orders,
limit,
condition,
)
payment_ref = frappe.qb.DocType("Payment Entry Reference")
payment_entries = q.run(as_dict=True)
q = q.inner_join(payment_ref).on(payment_entry.name == payment_ref.parent)
q = q.select(
(payment_ref.allocated_amount).as_("amount"),
(payment_ref.name).as_("reference_row"),
(payment_ref.reference_name).as_("against_order"),
payment_ref.reference_doctype == order_doctype,
)
return list(payment_entries)
if order_list:
q = q.where(payment_ref.reference_name.isin(order_list))
allocated = list(q.run(as_dict=True))
payment_entries += allocated
def build_query(
if include_unallocated:
q = get_common_query(
party_type,
party,
party_account,
limit,
condition,
)
q = q.select((payment_entry.unallocated_amount).as_("amount"))
q = q.where(payment_entry.unallocated_amount > 0)
unallocated = list(q.run(as_dict=True))
payment_entries += unallocated
return payment_entries
def get_common_query(
party_type,
party,
party_account,
order_doctype,
order_list,
include_unallocated,
against_all_orders,
limit,
condition,
):
payment_type = "Receive" if party_type == "Customer" else "Pay"
payment_entry = frappe.qb.DocType("Payment Entry")
payment_ref = frappe.qb.DocType("Payment Entry Reference")
q = (
frappe.qb.from_(payment_entry)
@ -2259,13 +2282,9 @@ def build_query(
q = q.where(payment_entry.paid_to.isin(party_account))
if payment_type == "Receive":
q = q.select(payment_entry.source_exchange_rate)
q = q.select((payment_entry.source_exchange_rate).as_("exchange_rate"))
else:
q.select(payment_entry.target_exchange_rate)
if include_unallocated:
q = q.select((payment_entry.unallocated_amount).as_("amount"))
q = q.where(payment_entry.unallocated_amount > 0)
q = q.select((payment_entry.target_exchange_rate).as_("exchange_rate"))
if condition:
q = q.where(payment_entry.company == condition["company"])
@ -2307,20 +2326,9 @@ def build_query(
else q
)
elif order_list or against_all_orders:
q = q.inner_join(payment_ref).on(payment_entry.name == payment_ref.parent)
q = q.select(
(payment_ref.allocated_amount).as_("amount"),
(payment_ref.name).as_("reference_row"),
(payment_ref.reference_name).as_("against_order"),
payment_ref.reference_doctype == order_doctype,
)
if order_list:
q = q.where(payment_ref.reference_name.isin(order_list))
q = q.orderby(payment_entry.posting_date)
q = q.limit(limit) if limit else q
return q