ERPNext Production plan - script for removing available stock from Assembly items

In the ERPNext Production plan, companies often need to produce only items which is not in the stock already. Use these scritpt to achieve that.

 · 1 min read

A Complete Guide to Production Planning Strategy - Welp Magazine


In the ERPNext Production plan, companies often need to produce only items which is not in the stock already.


Example 1 -  You have a sales order for 10 QTY of  X, but 3 QTY already available in the stock. So you need to produce only 7.

Example 2 - You have a sales order for 10 QTY of Y, but all 10 QTY already available in the stock. So you need not produce anything.


Use the following client and server script to do these calculations.


  1. Create a client script for production plan doctype

 

 

frappe.ui.form.on('Production Plan', {

get_items:function(frm,cdt,cdn){

 

 let length=frm.doc.po_items.length

   for(let i=0;i<length;i++){

  var item_code=frm.doc.po_items[i].item_code

  if(item_code){

      frappe.call({

   method:"get_stock",

   args: {

       item_code:item_code},

   

callback: function(r) {

       var name=frm.doc.po_items[i].name

       var p_qty=frm.doc.po_items[i].planned_qty

       

        var stock=Math.abs((r.message[0][0])-(p_qty))

              if(stock == 0){

           cur_frm.get_field("po_items").grid.grid_rows[i].remove();

       }

       

        frappe.model.set_value("Production Plan Item", name, 'planned_qty', stock);

       

       }

   })

 

 

}

 

}

 

},

combine_items:function(frm,cdt,cdn){

   let length=frm.doc.po_items.length

  

    for(let i=0;i<length;i++){

  var item_code=frm.doc.po_items[i].item_code

 

  if(item_code){

      frappe.call({

   method:"get_stock",

   args: {

       item_code:item_code},

   callback: function(r) {

       

        

        var name=frm.doc.po_items[i].name

        var p_qty=frm.doc.po_items[i].planned_qty

      var stock=Math.abs((r.message[0][0])-(p_qty))

       frappe.model.set_value("Production Plan Item", name, 'planned_qty', stock);

       

               

            }

 

    })

 

 

 

}

 

}

 

},

 

})





--------------------------------


Create a server script with script type as ‘API’. Give API method as ‘get_stock’.

 

 

item_codee=frappe.form_dict.item_code

stock=frappe.db.sql(f"""SELECT qty_after_transaction FROM `tabStock Ledger Entry` WHERE item_code='{item_codee}' ORDER BY modified DESC LIMIT 1;""")

frappe.response["message"]=stock



Hiba Nazar

Hiba works on API and Integrations.

1 comment
Product Photography August 3, 2024

I enjoyed reading your post. The way you approached was enlightening. It made me reflect on a discussion I recently had on assembo.ai/blog. I'd love to hear your thoughts. Keep up the good work!

Add Comment