Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fetch party and advance accounts #39029

Closed
wants to merge 12 commits into from
6 changes: 2 additions & 4 deletions erpnext/accounts/doctype/payment_entry/payment_entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
make_reverse_gl_entries,
process_gl_map,
)
from erpnext.accounts.party import get_party_account
from erpnext.accounts.party import get_party_account, get_party_and_advance_accounts
from erpnext.accounts.utils import (
cancel_exchange_gain_loss_journal,
get_account_currency,
Expand Down Expand Up @@ -219,9 +219,7 @@ def set_liability_account(self):
self.book_advance_payments_in_separate_party_account = False
return

liability_account = get_party_account(
self.party_type, self.party, self.company, include_advance=True
)[1]
liability_account = get_party_and_advance_accounts(self.party_type, self.party, self.company)[1]

self.set(self.party_account_field, liability_account)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,11 @@ erpnext.accounts.PaymentReconciliationController = class PaymentReconciliationCo

if (!this.frm.doc.receivable_payable_account && this.frm.doc.party_type && this.frm.doc.party) {
frappe.call({
method: "erpnext.accounts.party.get_party_account",
method: "erpnext.accounts.party.get_party_and_advance_accounts",
args: {
company: this.frm.doc.company,
party_type: this.frm.doc.party_type,
party: this.frm.doc.party,
include_advance: 1
},
callback: (r) => {
if (!r.exc && r.message) {
Expand Down
67 changes: 38 additions & 29 deletions erpnext/accounts/party.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,39 +390,38 @@ def set_account_and_due_date(


@frappe.whitelist()
def get_party_account(party_type, party=None, company=None, include_advance=False):
def get_party_account(party_type, party=None, company=None):
"""Returns the account for the given `party`.
Will first search in party (Customer / Supplier) record, if not found,
will search in group (Customer Group / Supplier Group),
finally will return default."""
if not company:
frappe.throw(_("Please select a Company"))

if not party and party_type in ["Customer", "Supplier"]:
default_account_name = (
"default_receivable_account" if party_type == "Customer" else "default_payable_account"
)

return frappe.get_cached_value("Company", company, default_account_name)

account = frappe.db.get_value(
"Party Account", {"parenttype": party_type, "parent": party, "company": company}, "account"
)
account = None

if not account and party_type in ["Customer", "Supplier"]:
party_group_doctype = "Customer Group" if party_type == "Customer" else "Supplier Group"
group = frappe.get_cached_value(party_type, party, scrub(party_group_doctype))
if party:
account = frappe.db.get_value(
"Party Account",
{"parenttype": party_group_doctype, "parent": group, "company": company},
"account",
"Party Account", {"parenttype": party_type, "parent": party, "company": company}, "account"
)

if not account and party_type in ["Customer", "Supplier"]:
default_account_name = (
"default_receivable_account" if party_type == "Customer" else "default_payable_account"
)
account = frappe.get_cached_value("Company", company, default_account_name)
if party_type in ["Customer", "Supplier"]:
default_account = get_default_payable_receivable_account(company, party_type)

if party:
if not account:
party_group_doctype = "Customer Group" if party_type == "Customer" else "Supplier Group"
group = frappe.get_cached_value(party_type, party, scrub(party_group_doctype))
account = (
frappe.db.get_value(
"Party Account",
{"parenttype": party_group_doctype, "parent": group, "company": company},
"account",
)
or default_account
)
else:
return default_account

existing_gle_currency = get_party_gle_currency(party_type, party, company)
if existing_gle_currency:
Expand All @@ -431,16 +430,26 @@ def get_party_account(party_type, party=None, company=None, include_advance=Fals
if (account and account_currency != existing_gle_currency) or not account:
account = get_party_gle_account(party_type, party, company)

if include_advance and party_type in ["Customer", "Supplier", "Student"]:
advance_account = get_party_advance_account(party_type, party, company)
if advance_account:
return [account, advance_account]
else:
return [account]

return account


@frappe.whitelist()
def get_party_and_advance_accounts(party_type, party=None, company=None):
"""
Returns a list containing the party account and the advance account.
"""
party_accounts = [get_party_account(party_type, party, company)]
party_accounts.append(get_party_advance_account(party_type, party, company))
return party_accounts


def get_default_payable_receivable_account(company, party_type):
default_account_name = (
"default_receivable_account" if party_type == "Customer" else "default_payable_account"
)
return frappe.get_cached_value("Company", company, default_account_name)


def get_party_advance_account(party_type, party, company):
account = frappe.db.get_value(
"Party Account",
Expand Down
12 changes: 5 additions & 7 deletions erpnext/accounts/report/purchase_register/purchase_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from frappe.utils import flt, getdate
from pypika import Order

from erpnext.accounts.party import get_party_account
from erpnext.accounts.party import get_party_and_advance_accounts
from erpnext.accounts.report.utils import (
apply_common_conditions,
get_advance_taxes_and_charges,
Expand Down Expand Up @@ -419,10 +419,10 @@ def get_invoices(filters, additional_query_columns):
)

if filters.get("include_payments"):
party_account = get_party_account(
"Supplier", filters.get("supplier"), filters.get("company"), include_advance=True
party_accounts = get_party_and_advance_accounts(
"Supplier", filters.get("supplier"), filters.get("company")
)
query = query.where(pi.credit_to.isin(party_account))
query = query.where(pi.credit_to.isin(party_accounts))

invoices = query.run(as_dict=True)
return invoices
Expand All @@ -443,9 +443,7 @@ def get_payments(filters):
account_fieldname="paid_to",
party="supplier",
party_name="supplier_name",
party_account=get_party_account(
"Supplier", filters.supplier, filters.company, include_advance=True
),
party_accounts=get_party_and_advance_accounts("Supplier", filters.supplier, filters.company),
)
payment_entries = get_payment_entries(filters, args)
journal_entries = get_journal_entries(filters, args)
Expand Down
6 changes: 2 additions & 4 deletions erpnext/accounts/report/sales_register/sales_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from frappe.utils import flt, getdate
from pypika import Order

from erpnext.accounts.party import get_party_account
from erpnext.accounts.party import get_party_and_advance_accounts
from erpnext.accounts.report.utils import (
apply_common_conditions,
get_advance_taxes_and_charges,
Expand Down Expand Up @@ -480,9 +480,7 @@ def get_payments(filters):
account_fieldname="paid_from",
party="customer",
party_name="customer_name",
party_account=get_party_account(
"Customer", filters.customer, filters.company, include_advance=True
),
party_accounts=get_party_and_advance_accounts("Customer", filters.customer, filters.company),
)
payment_entries = get_payment_entries(filters, args)
journal_entries = get_journal_entries(filters, args)
Expand Down
10 changes: 5 additions & 5 deletions erpnext/accounts/report/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
get_dimension_with_children,
)
from erpnext.accounts.doctype.fiscal_year.fiscal_year import get_from_and_to_date
from erpnext.accounts.party import get_party_account
from erpnext.accounts.party import get_party_and_advance_accounts
from erpnext.setup.utils import get_exchange_rate

__exchange_rates = {}
Expand Down Expand Up @@ -253,7 +253,7 @@ def get_journal_entries(filters, args):
(je.voucher_type == "Journal Entry")
& (je.docstatus == 1)
& (journal_account.party == filters.get(args.party))
& (journal_account.account.isin(args.party_account))
& (journal_account.account.isin(args.party_accounts))
)
.orderby(je.posting_date, je.name, order=Order.desc)
)
Expand Down Expand Up @@ -284,7 +284,7 @@ def get_payment_entries(filters, args):
.where(
(pe.docstatus == 1)
& (pe.party == filters.get(args.party))
& (pe[args.account_fieldname].isin(args.party_account))
& (pe[args.account_fieldname].isin(args.party_accounts))
)
.orderby(pe.posting_date, pe.name, order=Order.desc)
)
Expand Down Expand Up @@ -373,7 +373,7 @@ def filter_invoices_based_on_dimensions(filters, query, parent_doc):


def get_opening_row(party_type, party, from_date, company):
party_account = get_party_account(party_type, party, company, include_advance=True)
party_accounts = get_party_and_advance_accounts(party_type, party, company)
gle = frappe.qb.DocType("GL Entry")
return (
frappe.qb.from_(gle)
Expand All @@ -384,7 +384,7 @@ def get_opening_row(party_type, party, from_date, company):
(Sum(gle.debit) - Sum(gle.credit)).as_("balance"),
)
.where(
(gle.account.isin(party_account))
(gle.account.isin(party_accounts))
& (gle.party == party)
& (gle.posting_date < from_date)
& (gle.is_cancelled == 0)
Expand Down
19 changes: 9 additions & 10 deletions erpnext/controllers/accounts_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from erpnext.accounts.party import (
get_party_account,
get_party_account_currency,
get_party_and_advance_accounts,
get_party_gle_currency,
validate_party_frozen_disabled,
)
Expand Down Expand Up @@ -1114,18 +1115,16 @@ def get_advance_entries(self, include_unallocated=True):
order_field = "purchase_order"
order_doctype = "Purchase Order"

party_account = get_party_account(
party_type, party=party, company=self.company, include_advance=True
)
party_accounts = get_party_and_advance_accounts(party_type, party=party, company=self.company)

order_list = list(set(d.get(order_field) for d in self.get("items") if d.get(order_field)))

journal_entries = get_advance_journal_entries(
party_type, party, party_account, amount_field, order_doctype, order_list, include_unallocated
party_type, party, party_accounts, amount_field, order_doctype, order_list, include_unallocated
)

payment_entries = get_advance_payment_entries_for_regional(
party_type, party, party_account, order_doctype, order_list, include_unallocated
party_type, party, party_accounts, order_doctype, order_list, include_unallocated
)

res = journal_entries + payment_entries
Expand Down Expand Up @@ -2616,7 +2615,7 @@ def set_balance_in_account_currency(
def get_advance_journal_entries(
party_type,
party,
party_account,
party_accounts,
amount_field,
order_doctype,
order_list,
Expand All @@ -2638,7 +2637,7 @@ def get_advance_journal_entries(
(journal_acc.exchange_rate),
)
.where(
journal_acc.account.isin(party_account)
journal_acc.account.isin(party_accounts)
& (journal_acc.party_type == party_type)
& (journal_acc.party == party)
& (journal_acc.is_advance == "Yes")
Expand Down Expand Up @@ -2727,7 +2726,7 @@ def get_advance_payment_entries(
def get_common_query(
party_type,
party,
party_account,
party_accounts,
limit,
condition,
):
Expand All @@ -2752,11 +2751,11 @@ def get_common_query(
if payment_type == "Receive":
q = q.select((payment_entry.paid_from_account_currency).as_("currency"))
q = q.select(payment_entry.paid_from)
q = q.where(payment_entry.paid_from.isin(party_account))
q = q.where(payment_entry.paid_from.isin(party_accounts))
else:
q = q.select((payment_entry.paid_to_account_currency).as_("currency"))
q = q.select(payment_entry.paid_to)
q = q.where(payment_entry.paid_to.isin(party_account))
q = q.where(payment_entry.paid_to.isin(party_accounts))

if payment_type == "Receive":
q = q.select((payment_entry.source_exchange_rate).as_("exchange_rate"))
Expand Down
6 changes: 3 additions & 3 deletions erpnext/setup/doctype/company/company.json
Original file line number Diff line number Diff line change
Expand Up @@ -694,15 +694,15 @@
"fieldname": "default_advance_received_account",
"fieldtype": "Link",
"label": "Default Advance Received Account",
"mandatory_depends_on": "book_advance_payments_as_liability",
"mandatory_depends_on": "book_advance_payments_in_separate_party_account",
"options": "Account"
},
{
"depends_on": "eval:doc.book_advance_payments_in_separate_party_account",
"fieldname": "default_advance_paid_account",
"fieldtype": "Link",
"label": "Default Advance Paid Account",
"mandatory_depends_on": "book_advance_payments_as_liability",
"mandatory_depends_on": "book_advance_payments_in_separate_party_account",
"options": "Account"
},
{
Expand Down Expand Up @@ -766,7 +766,7 @@
"image_field": "company_logo",
"is_tree": 1,
"links": [],
"modified": "2023-10-23 10:19:24.322898",
"modified": "2023-12-29 02:35:15.121778",
"modified_by": "Administrator",
"module": "Setup",
"name": "Company",
Expand Down
Loading