fix: incorrect status creating PR from PO after creating PI (#25203)

* test: add tests for PO->PI, PO->PR workflow

Add failing test cases for PR status and PR percent billed.

* fix: update PR status in database

* fix: do not copy percent billed from PO to PR

* fix: patch for purchase receipt status

* fix: remove logging in patch for v12 compatibility
This commit is contained in:
Ankush Menat
2021-04-13 18:57:03 +05:30
committed by GitHub
parent 16c3a76c49
commit e6eab3b5f8
5 changed files with 76 additions and 2 deletions

View File

@@ -366,7 +366,6 @@ def make_purchase_receipt(source_name, target_doc=None):
"Purchase Order": {
"doctype": "Purchase Receipt",
"field_map": {
"per_billed": "per_billed",
"supplier_warehouse":"supplier_warehouse"
},
"validation": {

View File

@@ -681,3 +681,4 @@ erpnext.patches.v12_0.update_payment_entry_status
erpnext.patches.v12_0.add_transporter_address_field #2020-10-27
erpnext.patches.v12_0.setup_einvoice_fields #2020-12-02
erpnext.patches.v12_0.add_state_code_for_ladakh
erpnext.patches.v12_0.purchase_receipt_status

View File

@@ -0,0 +1,24 @@
""" This patch fixes old purchase receipts (PR) where even after submitting
the PR, the `status` remains "Draft". `per_billed` field was copied over from previous
doc (PO), hence it is recalculated for setting new correct status of PR.
"""
import frappe
def execute():
affected_purchase_receipts = frappe.db.sql(
"""select name from `tabPurchase Receipt`
where status = 'Draft' and per_billed = 100 and docstatus = 1"""
)
if not affected_purchase_receipts:
return
for pr in affected_purchase_receipts:
pr_name = pr[0]
pr_doc = frappe.get_doc("Purchase Receipt", pr_name)
pr_doc.update_billing_status(update_modified=False)
pr_doc.set_status(update=True, update_modified=False)

View File

@@ -155,7 +155,7 @@ class PurchaseReceipt(BuyingController):
if flt(self.per_billed) < 100:
self.update_billing_status()
else:
self.status = "Completed"
self.db_set("status", "Completed")
# Updating stock ledger should always be called after updating prevdoc status,

View File

@@ -676,6 +676,56 @@ class TestPurchaseReceipt(unittest.TestCase):
update_backflush_based_on("BOM")
def test_po_to_pi_and_po_to_pr_worflow_full(self):
"""Test following behaviour:
- Create PO
- Create PI from PO and submit
- Create PR from PO and submit
"""
from erpnext.buying.doctype.purchase_order import test_purchase_order
from erpnext.buying.doctype.purchase_order import purchase_order
po = test_purchase_order.create_purchase_order()
pi = purchase_order.make_purchase_invoice(po.name)
pi.submit()
pr = purchase_order.make_purchase_receipt(po.name)
pr.submit()
pr.load_from_db()
self.assertEqual(pr.status, "Completed")
self.assertEqual(pr.per_billed, 100)
def test_po_to_pi_and_po_to_pr_worflow_partial(self):
"""Test following behaviour:
- Create PO
- Create partial PI from PO and submit
- Create PR from PO and submit
"""
from erpnext.buying.doctype.purchase_order import test_purchase_order
from erpnext.buying.doctype.purchase_order import purchase_order
po = test_purchase_order.create_purchase_order()
pi = purchase_order.make_purchase_invoice(po.name)
pi.items[0].qty /= 2 # roughly 50%, ^ this function only creates PI with 1 item.
pi.submit()
pr = purchase_order.make_purchase_receipt(po.name)
pr.save()
# per_billed is only updated after submission.
self.assertEqual(flt(pr.per_billed), 0)
pr.submit()
pi.load_from_db()
pr.load_from_db()
self.assertEqual(pr.status, "To Bill")
self.assertAlmostEqual(pr.per_billed, 50.0, places=2)
def get_gl_entries(voucher_type, voucher_no):
return frappe.db.sql("""select account, debit, credit, cost_center
from `tabGL Entry` where voucher_type=%s and voucher_no=%s