How to create and submit a purchase receipt when a purchase order is submitted

I am trying to customise ERPnext to replace our accounts software, Microsoft Dynamics GP. Our process is to only import goods to GP as they are received through our custom warehouse management software, which works with GP as it has no concept of a purchase receipt (it goes straight from Purchase Order to Invoice).

ERPnext has the unnecessary (to us) step of creating a purchase receipt after a PO has been raised, and I would like to eliminate this by automatically creating and submitting a PR whenever a PO is submitted. I have looked through the forum for existing tasks, and found How to trigger something when save or submit is clicked in a doctype but can’t see how to adapt this for my needs. I tried making a custom function following the suggestion and adding it to hooks.py, and it was picked up but didn’t do anything.

Any advice on this would be greatly appreciated.

This is configurable. Have you checked your buying settings (Purchase Receipt Required)?

I am aware of this setting but it doesn’t do what I’m looking for. The process I would like is as follows:

  1. Using data import tool, import previous day’s goods in receipts to ERPnext as draft Purchase Orders
  2. After confirming the import balance, submit the Purchase Orders within ERPnext. This should, behind the scenes, create and submit 100% Purchase Receipts for the relevant Purchase Order.
  3. With no further user interaction after pressing “Submit” on the Purchase Order, the user should then be able to invoice off the goods or check the Goods Received Not Invoiced balance.

Might it be easier the completely ignore the Purchase Receipt step and have the Purchase Order submission create the relevant journal entries for GRNI?

If you are trying to automate this, it would be better to create purchase receipts for all the POs submitted by data import tool or by custom code.

Doing receipt accounting entries, without a verified document (like Purchase Receipt) in the system is not a correct accounting practice.

For us the Purchase Order in ERPnext is the verified document. The confirmation of receipt happens on a different system so we will only ever be importing confirmed purchase receipts. It is literally impossible for the accounting system to even see anything that is not a confirmed purchase receipt. This is how our current accounting system works.

That said, I would rather go through the existing ERPnext process ( PO → PR → PI) than have to circumvent anything if possible by using automation.

I have got this working with the following server script:

def create_purchase_receipt(doc, model):
    po = frappe.get_doc({
            "doctype": "Purchase Order",
            "name": doc.name,
            "transaction_date": doc.transaction_date
    })

    pr = make_purchase_receipt(po.name)

    pr.posting_date = po.transaction_date
    pr.posting_time = "00:00:00"

    pr.insert()
    pr.submit()
    pr.save()

    frappe.db.commit()

However it’s not updating the posting_date or posting_time of the Purchase Receipt which will cause problems with the Goods Received Not Billed report. Any thoughts on why this isn’t working and how to fix it?

did you try:

    from frappe.utils.data import get_time
    pr = make_purchase_receipt(po.name)
    pr.posting_date = po.transaction_date
    pr.posting_time = get_time("00:00:00")
    pr.save()
    pr.submit()

No difference I’m afraid. The time isn’t actually important so I’ve tried removing that part and still nothing.

If I print pr.posting_date I get the correct date so I’m not sure why it’s not submitting the document with the current date.

Probably there are some mandatory fields not filled …double check

Figured it out, had to change “set_posting_time” to 1

2 Likes