Pass Custom Value in Purchase Receipt Item to Custom Field in Batch DocType

I have custom fields in the Purchase Receipt Item that I want to be entered in to the associated Batch when it is created. I tried to do this by giving the DocTypes fields in each the same name and type but the information is not transferred from PR Item to Batch.

Does this need an additional step/script?

Thanks,
Paul

Can anyone help with this one?

I have two custom fields in the Purchase Receipt Item, not the Purchase Receipt DocType.
manufacturers_lot_no, a Data field
manufacturers_expiry_date, a Date field

When the Purchase Receipt is Submitted and new Batch No assigned to theitem, how can this accompanying information be added to corresponding fields in the Batch No DocType?

Any help on this would be great :smiley:

You can use The fetch from feature
In batch doctype add a new 2 field , and in fetch from add this { field contains purchase receipt id in batch doctype } . { Field name in purchase receipt }

1 Like
  1. batch no manually created via batch no link field, ensure the referenced (to-be copied field filled before create batch no), please create the following client script

frappe.ui.form.on('Purchase Receipt', {
	refresh(frm) {
	    let batch_field = frm.get_docfield("items", "batch_no");
		batch_field.get_route_options_for_new_doc = function(row) {
			//if (frm.is_new()) return;
			return {
				"manufacturer_part": row.doc.manufacturer_part,
				"item": row.doc.item_code
			}
		}
	}
})
  1. auto batch no case, please create the following server script

for item in doc.items:
    if item.batch_no and item.manufacturer_part:
        batch_doc = frappe.get_doc('Batch', item.batch_no)
        if batch_doc.reference_doctype == doc.doctype and batch_doc.reference_name == doc.name:
            batch_doc.manufacturer_part = item.manufacturer_part
            batch_doc.save()

for both scripts, please adapt the field name accordingly.

3 Likes

Thanks @szufisher , that script works brilliantly for v14.
Unfortunately I’m using the develop branch and it doesn’t work with the new Serial and Batch Bundle approach (batch_no is no longer used). However, it does give me a good direction to work on :smiley:

yes, from v15, the serial number and batch feature has been refactored, looking forward your sharing of the solution on v15.

1 Like

Been a while but I think I have working version to share that works on v15 with the Serial and Batch Bundle feature.

This is a server script for the Purchase Receipt, set to trigger on DocType Event After Submit

I have two custom fields setup in the Purchase Receipt Item:

  • custom_m_lot_number for the manufacturers’ lot/batch number
  • custom_m_expiry_date for the manufacturers’ expiry date

In turn I have two corresponding fields in the Batch DocType

  • custom_supplier_identifier to store the value for custom_m_lot_number
  • custom_supplier_expiry to store the value for custom_m_expiry_date

NB this assumes that you either completely accept the items on a line item OR completely reject them. It will not work, as is, for a Purchase Receipt Item that contains a quantity for both accepted and rejected fields. I use an additional Client Script to enforce this.

# Get Purchase Receipt Name
parent_receipt = doc.name
# Get the Purchase Receipt document
purchase_receipt = frappe.get_doc("Purchase Receipt", parent_receipt)

# Iterate through Purchase Receipt Item child table
for item in purchase_receipt.items:
# Get the custom_m, custom_d fields
    custom_m = item.get("custom_m_lot_number")
    custom_d = item.get("custom_m_expiry_date")
    serial_and_batch_bundle = item.get("serial_and_batch_bundle")
    # If serial_and_batch_bundle field is empty, find the SABB id from the rejected_serial_and_batch_bundle field instead
    if serial_and_batch_bundle is None:
        serial_and_batch_bundle = item.get("rejected_serial_and_batch_bundle")
    
        # Search for the batch number using the serial_and_batch_bundle field
    batch_number = frappe.db.get_value("Serial and Batch Entry", 
                                           {"parent": serial_and_batch_bundle}, 
                                           "batch_no")

    # Set custom field values in relevant Batch no
    frappe.db.set_value('Batch', batch_number, {
    'custom_supplier_identifier': custom_m,
    'custom_supplier_expiry': custom_d
})

Hope it helps,
Paul

1 Like