I have added a table of items bought by customer to customer doctype with two fields item_code and serial_no, my problem is how can i feel the table with data of the items that customer has bought.
I have tried pulling the information from the Serial No doctype which has a field of the item_code and customer who bought the item without much success.
Check my code below, any help will be much appreciated.
frappe.model.with_doc("Serial No", frm.doc.customer_name, function(){
var ref_doc = frappe.get_doc("Serial No", frm.doc.customer_name);
//console.log(ref_doc);
if (ref_doc.item_code && ref_doc.item_code !='') {
var new_row = frm.add_child("items");
new_row.item_code = ref_doc.item_code;
new_row.serial_no = ref_doc.serial_no;
}
});
You can do it better with a Python function being called from a Custom Script.
You need to decide where this python function will be. I will recommend your own doctype and not make changes to the default code. This will create problems in ERPNext updates.
The custom script will call this Python function, and the Python function will add details as wanted. Also, you will be getting returning customers and also they may buy the same item, so it will be beneficial if you will check for items already bought and simply increment the number bought rather than make a separate entry.
Thanks for getting back to me @root13F actually had the same thought but didn’t know how to go about it.
this is what i have tried so far
@frappe.whitelist()
def get_item_bought_details(doctype, filters):
customer_name = frappe.db.escape(filters.get(‘customer’))
return frappe.db.sql(“”"
select tabSerial No
.item_code, tabSerial No
.serial_no
from tabSerial No
where tabSerial No
.customer = tabCustomer
.customer
“”"
)
and
frappe.ui.form.on("Customer Item", {
items: function(frm) {
if (frm.doc.items) {
frappe.call({
method: "erpnext.selling.doctype.customer.customer.get_item_bought_details"
args: {
fieldname: ["serial_no", 'item_code'],
filters: {
customer: frm.doc.customer
}
},
callback: function(r) {
frm.set_value("items.item_code", r.message);
frm.set_value("items.serial_no", r.message);
}
})
}
/*$.each(frm.doc.items || [], function(i, d) {
if(!d.delivery_date) d.delivery_date = frm.doc.delivery_date;
});*/
refresh_field("items");
},
}