Purchase Order Status not updated after Purchase Receipt completed via REST API

Hey everyone :wave:

Again, I am hoping for some feedback and help because I find it hard to understand how ERPNext actually works :see_no_evil:

In this current scenario, I want to work with ERPNext only by using the REST API. I want to create Purchase Orders and Purchase Receipts based on those orders. My issue is, that the status of my Purchase Orders stays To Receive and Bill even though the corresponding Purchase Receipts are in status Completed.

This is how I create my Purchase Orders:

po_endpoint = f'{erpnext_url}/api/resource/Purchase Order'
po_data = {
    "supplier": "Primary Processor ABC",
    "docstatus": 1,
    "items": [
        {
            "item_code": "BB",
            "custom_content": "MAT-ABS-5mm",
            "schedule_date": schedule_date,
            "qty": 1.0,
        },
    ]
}

All good, I get HTTP 200 and a Purchase Order id: po_id :white_check_mark:

This is how I create my Purchase Receipt:

pr_endpoint = f'{erpnext_url}/api/resource/Purchase Receipt'
pr_data = {
    "purchase_order": po_id,
    "supplier": "Primary Processor ABC",
    "docstatus": 1,
    "items": [
        {
            "item_code": "BB",
            "purchase_order": po_id,
            "custom_content": "MAT-ABS-5mm",
            "schedule_date": schedule_date,
            "qty": 1.0,
        },
    ]
}

All good, I get HTTP 200 and a Purchase Receipt :white_check_mark:

When I look at the ERPNext UI, my Purchase Receipt is in status Completed and has a Connection to the correct Purchase Order.

However, when I click on the connected PO, I see that the status is still To Receive and Bill:


It is even possible to created another Purchase Receipt from the same Purchase Order. ERPNext apparently has no problem creating multiple Purchase Receipts from the same Purchase Order exceeding the quantity of items. :exploding_head:

I would be very grateful for some help because I don’t know where to look for clues. I cannot explain this behaviour or ERPNext. And, please don’t mind the Item field Content. This is a Custom Field we had to add to the DocTypes Item, Purchase Order Item and Purchase Receipt Item.

All the best :slightly_smiling_face:

In another attempt, I tried to use the “whitelisted” function make_purchase_receipt of Purchase Order.

So, I created a PO as described before. But, instead of POST’ing a Purchase Receipt, I did the following:

pr_endpoint = f'{erpnext_url}/api/method/erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt'
pr_data = {
    "source_name": po_id
}

response = login_session.post(pr_endpoint, json=pr_data)

This also resulted in a HTTP 200 and the response body contained a Purchase Order object.

However, the response body did not contain an id, or a “document name”. I could see, that the status of the PR was draft. But, neither the UI nor the API showed me this draft PR. Moreover, the corresponding PO also did not have a PR in the Connections tab.

Hi @tgraupne,

You have to link the Purchase Order Item ‘name’ to the Purchase Receipt Item ‘purchase_order_item’. Please check it.

Purchase Order: PUR-ORD-2024-00001

cur_frm.doc.items[0].name = ‘5ecf4f2aef’

Purchase Receipt: MAT-PRE-2024-00001

cur_frm.doc.items[0].purchase_order_item = ‘5ecf4f2aef’

Then after, your purchase order status will be changed.

Thank You!

Hey @NCP , thank you so much for your feedback.

With your feedback, I was able to fix my issue. Using the following as my Purchase Order data worked and the corresponding Purchase Order changed status:

pr_data = {
        "purchase_order": po_id,
        "supplier": "Primary Processor ABC",
        "docstatus": 1,
        "items": [
            {
                "item_code": "BB",
                "purchase_order_item": po_item_name,
                "purchase_order": po_id,
                "custom_content": "MAT-ABS-5mm",
                "schedule_date": schedule_date,
                "qty": 1.0,
            },
        ]
    }

While trying different things out, I though, “even though it feels so wrong, why not put the response I get from /api/method/erpnext.buying.doctype.purchase_order.purchase_order.make_purchase_receipt straight into a new POST request to /api/resource/Purchase Receipt”

I don’t know if this is the intended use, but it worked :woman_shrugging:

Instead of manually creating the Purchase Order as an object, it seems possible to call this make_purchase_receipt function and use the response body.