Update BOM to latest version automatically

If you find a mistake with your BOM after submitting Sales Orders and Production Plans you have a problem. You also have to remember to use the BOM Update Tool to make sure the latest BOM is referenced by other BOMs.

Not anymore!
(Note this does not update Work Orders or Job cards…)

  • Script Type: DocType Event
  • Reference Document Type: BOM
  • DocType Event: After Submit

Code:

if doc.is_active and doc.is_default:
    # Make all BOM replacements
    boms = frappe.get_all('BOM',
        filters={'is_default': 0, 'item': doc.item}
    )
    but = frappe.get_doc("BOM Update Tool")
    for bom in boms:
        but.current_bom = bom['name']
        but.new_bom = doc.name
        but.replace_bom()

    # Update open Sales Order Items
    sos = frappe.get_all('Sales Order',
        filters={'docstatus': ['<=', 1], 'status': ['!=', 'Completed']}
    )
    for so in sos:
        so_doc = frappe.get_cached_doc('Sales Order', so['name'])
        dirty = False
        for soi_doc in so_doc.items:
            if soi_doc.item_code == doc.item:
                dirty = True
                soi_doc.bom_no = doc.name
        if dirty:
            so_doc.flags.ignore_validate_update_after_submit = True
            so_doc.save()

    # Update open Production Plans
    pps = frappe.get_all('Production Plan',
        filters={'docstatus': ['<=', 1], 'status': ['!=', 'Completed']}
    )
    for pp in pps:
        pp_doc = frappe.get_cached_doc('Production Plan', pp['name'])
        dirty = False
        for ppi_doc in pp_doc.po_items:
            if ppi_doc.item_code == doc.item:
                dirty = True
                ppi_doc.bom_no = doc.name
        if dirty:
            pp_doc.flags.ignore_validate_update_after_submit = True
            pp_doc.save()
1 Like