How to make a quotation record from material receipt entry

We want to make a quotation from a stock entry. The transaction is such that we receive material from our customer on which we do some jobwork for which we send them a quotation.

First we prepare a material receipt with Customer Name, Items description etc.
Now we prepare a quotation in which we have mentioned a custom field Service_material which is link to the Stock Entry. We have also made one custom button get_item_details to read data from the linked Stock entry record. I am able to read the Parent table without problem but I have no clue how to copy values from child table ie. Item table.

I am using following Code in the custom script of doctype Quotation:

frappe.ui.form.on("Quotation", "get_item_details", function(frm) {
     frappe.model.with_doc("Stock Entry", frm.doc.service_material, function() { 
         var stock_doc = frappe.model.get_doc("Stock Entry", frm.doc.service_material);
         frm.set_value("customer", stock_doc.customer); 
         frm.set_value("party_gp_no", stock_doc.party_gp_no)
         frm.set_value("party_gp_date", stock_doc.party_gp_date)
         for row in stock_doc.mtn_details
        {
           frm.set_value("item_code", row.item_code); 

        }
});
 
});

Please inform how to populate data in the child table.
Thanks in advance.

In version 5:

$.each(stock_doc.items, function(s) {
  d = frm.add_child("items");
  d.item_code = s.item_code;
})

Thanks, but can this be done in version 4 as well and if so what is the syntax. Thanks again.

I have tried this script but it only fetches the master data.

frappe.ui.form.on("Quotation", "get_item_details", function(frm) {
   frappe.model.with_doc("Stock Entry", frm.doc.service_material, function() { 
 var stock_doc = frappe.model.get_doc("Stock Entry", frm.doc.service_material);
      frm.set_value("customer", stock_doc.customer); 
      frm.set_value("party_gp_no", stock_doc.party_gp_no)
      frm.set_value("party_gp_date", stock_doc.party_gp_date)  
            $.each(stock_doc.mtn_details, function(s) {
            d = frm.add_child("Quotation Item");
            d.item_code = s.item_code;
            d.party_gp_no=stock_doc.party_gp_no;
            })   
 })
})

Please inform what should be done to make it work in V4.
Thanks.

Is there a way in Version 4 to populate child table using data from parent or other tables. I am stuck with this. I want to make it in custom script as it is version update compatible. Any custom changes in .py and .js files get removed on erp version update.
Thanks

@arpanzen

You can maintain your code/function in custom app and call custom function using custom script or hook.

for populating data in child table:
reference I
reference II

Now i am migrating from V4 to V6 but iam still unable to transfer data from child records. Please check the script and please suggest changes. Here service_material is a variable which works as the link between Stock_entry and Quotation.

frappe.ui.form.on(“Quotation”, “service_material”, function(frm) {
frappe.model.with_doc(“Stock Entry”, frm.doc.service_material, function() {
var stock_doc = frappe.model.get_doc(“Stock Entry”, frm.doc.service_material);
frm.set_value(“customer”, stock_doc.customer);
frm.set_value(“party_gp_no”, stock_doc.party_gp_no)
frm.set_value(“party_gp_date”, stock_doc.party_gp_date)
$.each(stock_doc.items, function(s) {
d = frm.add_child(“Quotation Item”);
d.item_code = s.item_code;
d.party_gp_no=stock_doc.party_gp_no;
})
cur_frm.refresh()
})
})

On running the script the child data is not copied only the parent data is copied. Also the quotation form becomes disabled and we need to exit the form without saving.

On the form above, “Quotation Item” should be the name of the table field in Quotation, which is “items” in the standard document.

cur_frm.refresh() refreshes the entire document. try this instead:

cur_frm.refresh_field("items");

This may work for you:

frappe.ui.form.on("Quotation", "service_material", function(frm) {
    frappe.model.with_doc("Stock Entry", frm.doc.service_material, function() { 
    var stock_doc = frappe.model.get_doc("Stock Entry", frm.doc.service_material);
    frappe.model.set_value("customer", stock_doc.customer); 
    frappe.model.set_value("party_gp_no", stock_doc.party_gp_no)
    frappe.model.set_value("party_gp_date", stock_doc.party_gp_date)
        $.each(stock_doc.items, function(s) {
        d = frm.add_child("items");
        d.item_code = s.item_code;
        d.party_gp_no=stock_doc.party_gp_no;
        }) 

cur_frm.refresh_field("items");

})
})

Thanks cpurbaugh,
your pointers have moved me forward, now the form is not disabled while entering so the script is fine but it is not able to fetch the child record data. Child record data like item_code etc remains blank only the party_gp_no gets transfered. Can you think of some more tweaks so that the values also get transferred.

Thanks again for your timely reply.

This script worked for me. Thanks for the pointers.

frappe.ui.form.on(“Quotation”, “service_material”, function(frm) {
frappe.model.with_doc(“Stock Entry”, frm.doc.service_material, function() {
var stock_doc = frappe.model.get_doc(“Stock Entry”, frm.doc.service_material);
frm.set_value(“customer”, stock_doc.customer);
frm.set_value(“party_gp_no”, stock_doc.party_gp_no);
frm.set_value(“party_gp_date”, stock_doc.party_gp_date);
$.each(stock_doc.items, function(index,row) {
d = frm.add_child(“items”);
d.item_code = **row.**item_code;
d.party_gp_no=stock_doc.party_gp_no;
})
cur_frm.refresh_field(“items”);
})
})

1 Like