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: Payment Terms not fetched in Purchase Invoice from Purchase Receipt #23735

Merged
6 changes: 3 additions & 3 deletions erpnext/accounts/party.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _get_party_details(party=None, account=None, party_type="Customer", company=
billing_address=party_address, shipping_address=shipping_address)

if fetch_payment_terms_template:
party_details["payment_terms_template"] = get_pyt_term_template(party.name, party_type, company)
party_details["payment_terms_template"] = get_payment_terms_template(party.name, party_type, company)

if not party_details.get("currency"):
party_details["currency"] = currency
Expand Down Expand Up @@ -315,7 +315,7 @@ def get_due_date(posting_date, party_type, party, company=None, bill_date=None):
due_date = None
if (bill_date or posting_date) and party:
due_date = bill_date or posting_date
template_name = get_pyt_term_template(party, party_type, company)
template_name = get_payment_terms_template(party, party_type, company)

if template_name:
due_date = get_due_date_from_template(template_name, posting_date, bill_date).strftime("%Y-%m-%d")
Expand Down Expand Up @@ -422,7 +422,7 @@ def set_taxes(party, party_type, posting_date, company, customer_group=None, sup


@frappe.whitelist()
def get_pyt_term_template(party_name, party_type, company=None):
def get_payment_terms_template(party_name, party_type, company=None):
if party_type not in ("Customer", "Supplier"):
return
template = None
Expand Down
3 changes: 3 additions & 0 deletions erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,8 @@ def update_billed_amount_based_on_po(po_detail, update_modified=True):
@frappe.whitelist()
def make_purchase_invoice(source_name, target_doc=None):
from frappe.model.mapper import get_mapped_doc
from erpnext.accounts.party import get_payment_terms_template

doc = frappe.get_doc('Purchase Receipt', source_name)
returned_qty_map = get_returned_qty_map(source_name)
invoiced_qty_map = get_invoiced_qty_map(source_name)
Expand All @@ -543,6 +545,7 @@ def set_missing_values(source, target):

doc = frappe.get_doc(target)
doc.ignore_pricing_rule = 1
doc.payment_terms_template = get_payment_terms_template(source.supplier, "Supplier", source.company)
doc.run_method("onload")
doc.run_method("set_missing_values")
doc.run_method("calculate_taxes_and_totals")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ def test_reverse_purchase_receipt_sle(self):
frappe.db.set_value('UOM', '_Test UOM', 'must_be_whole_number', 1)

def test_make_purchase_invoice(self):
if not frappe.db.exists('Payment Terms Template', '_Test Payment Terms Template For Purchase Invoice'):
frappe.get_doc({
'doctype': 'Payment Terms Template',
'template_name': '_Test Payment Terms Template For Purchase Invoice',
'allocate_payment_based_on_payment_terms': 1,
'terms': [
{
'doctype': 'Payment Terms Template Detail',
'invoice_portion': 50.00,
'credit_days_based_on': 'Day(s) after invoice date',
'credit_days': 00
},
{
'doctype': 'Payment Terms Template Detail',
'invoice_portion': 50.00,
'credit_days_based_on': 'Day(s) after invoice date',
'credit_days': 30
}]
}).insert()

template = frappe.db.get_value('Payment Terms Template', '_Test Payment Terms Template For Purchase Invoice')
old_template_in_supplier = frappe.db.get_value("Supplier", "_Test Supplier", "payment_terms")
frappe.db.set_value("Supplier", "_Test Supplier", "payment_terms", template)

pr = make_purchase_receipt(do_not_save=True)
self.assertRaises(frappe.ValidationError, make_purchase_invoice, pr.name)
pr.submit()
Expand All @@ -51,10 +75,23 @@ def test_make_purchase_invoice(self):
self.assertEqual(pi.doctype, "Purchase Invoice")
self.assertEqual(len(pi.get("items")), len(pr.get("items")))

# modify rate
# test maintaining same rate throughout purchade cycle
pi.get("items")[0].rate = 200
self.assertRaises(frappe.ValidationError, frappe.get_doc(pi).submit)

# test if payment terms are fetched and set in PI
self.assertEqual(pi.payment_terms_template, template)
self.assertEqual(pi.payment_schedule[0].payment_amount, flt(pi.grand_total)/2)
self.assertEqual(pi.payment_schedule[0].invoice_portion, 50)
self.assertEqual(pi.payment_schedule[1].payment_amount, flt(pi.grand_total)/2)
self.assertEqual(pi.payment_schedule[1].invoice_portion, 50)

# teardown
pi.delete() # draft PI
pr.cancel()
frappe.db.set_value("Supplier", "_Test Supplier", "payment_terms", old_template_in_supplier)
frappe.get_doc('Payment Terms Template', '_Test Payment Terms Template For Purchase Invoice').delete()

def test_purchase_receipt_no_gl_entry(self):
company = frappe.db.get_value('Warehouse', '_Test Warehouse - _TC', 'company')

Expand Down