Hi,
I am trying to make a custom app that updates values on delivery note and delivery note item on the bases of item attributes.
I am able to update the values field following the tutorial:
Frappe - Creating a Custom Application & Integrating it with ERPNext
What I want to do is fetch item attribute vales, make calculations based on the attribute values and update the corresponding fields.
Example:
Item attributes = Height,Length and Width
Field on delivery note item: Item_volume = item.Height x Item.Length x Item.Width
Field on delivery note: total_volume= sum of all item_volume
Regards.
From where you want to fetch the item attribute?
can you Please provide the example for that or screenshot.
My App path:
ahmad@HP-Pavilion:~/master-frappe-bench/apps/steelpipes/steelpipes/sp_delivery_note$
In the below particular scenario:
delivery note item: item_weight = (1.2 x item.Pipe Thinkness x item.Strip.Width x item.Pipe Length) / 508
Devlivery note: total_weight = sum of all the item_weights
To fetch Item Attribute
frappe.call({
method:"frappe.client.get_list",
args:{
doctype:"Item Variant Attribute",
filters:{
parent:"Item Code",
parentfield:'attributes',
parenttype:'Item',
attribute:'Attribute Name (Length,width)'
},
fields:['from_range','to_range','attribute_value']
},
callback:function(r){
console.log(r.message)
}
})
Thanks I get the Idea from .js point of view. But how to do that in .py file.
i am thinking this approach
item_obj = frappe.get_doc("Item", itemcode)
But here I wonder do I need to import a specific module in my .py file to perform that ?
for Example:
from frappe.modules.utils import get_doc
In Python File same way as we are doing in js file.
frappe.get_list('DocType Name',filters={
parent:"Item Code",
parentfield:'attributes',
parenttype:'Item',
attribute:'Attribute Name (Length,width)'
},
fields:['from_range','to_range','attribute_value'])
I will try and then update. Thanks Rohan
@ROHAN_JAIN1 Thank you for your contribution. How ever I tried some other technique. Below is my python code and then the javascript code
Python Code
@frappe.whitelist()
def calculate_pipe_weight_um(itemcode, um):
# Calculating Pipe Weight using Attributes
delivery_note_item_um = frappe.get_doc("Item", itemcode)
item_thickness_um = item_width_um = item_length_um = 0
if str(delivery_note_item_um.variant_of) in ['Pipe']:
for item_attribute_um in delivery_note_item_um.attributes:
attribute_name = str(item_attribute_um.attribute)
if 'Thickness' in attribute_name:
item_thickness_um = float(item_attribute_um.attribute_value)
if 'Width' in attribute_name:
item_width_um = float(item_attribute_um.attribute_value)
if 'Length' in attribute_name:
item_length_um = float(item_attribute_um.attribute_value)
item_weight_um = (1.2 * item_thickness_um * item_width_um * item_length_um) / 508
if um == 'Meter':
item_length_um = item_length_um * 0.3048
return {"item_weight_um": round(item_weight_um, 2), "item_length_um": round(item_length_um, 2)}
Javascript code
frappe.ui.form.on("Delivery Note Item", {
item_code: function(frm,cdt,cdn){
var item_code = frappe.model.get_doc(cdt, cdn);
if (item_code.item_code){
if(item_code.item_code.includes("Pipe-MS",0)){
frm.call({
method: "steelpipes.sp_delivery_note.sp_delivery_note_item.calculate_pipe_weight_um",
args: {itemcode: item_code.item_code, um: item_code.um},
callback:function(r){
var weight_um_temp = r.message.item_weight_um;
var length_um_temp = r.message.item_length_um;
if (item_code.qty == undefined || item_code.qty == 0 ){
var total_weight_um_temp = weight_um_temp * 1;
var total_length_um_temp = length_um_temp * 1;
}
else{
var total_weight_um_temp = weight_um_temp * item_code.qty;
var total_length_um_temp = length_um_temp * item_code.qty;
}
frappe.model.set_value(cdt, cdn, "weight_um", weight_um_temp);
frappe.model.set_value(cdt, cdn, "total_weight_um", total_weight_um_temp);
frappe.model.set_value(cdt, cdn, "length_um", length_um_temp);
frappe.model.set_value(cdt, cdn, "total_length_um", total_length_um_temp);
}
})
}
}
else{
frappe.model.set_value(cdt,cdn, "item_name", null);
}
}
2 Likes