Dynamic Mass-to-Volume Conversion for Oil and Gas Products with Varying Density in ERPNext

Override Delivery Note validate event : import frappe
from erpnext.stock.doctype.delivery_note.delivery_note import DeliveryNote
from frappe import _

class CustomDeliveryNote(DeliveryNote):
def validate(self):
print(‘delivery note is called’)
self.validate_posting_time()

    super(DeliveryNote, self).validate()
    self.validate_references()
    self.set_status()
    self.so_required()
    self.validate_proj_cust()
    self.check_sales_order_on_hold_or_close("against_sales_order")
    self.validate_warehouse()
    self.validate_uom_is_integer("stock_uom", "stock_qty")
    self.validate_uom_is_integer("uom", "qty")
    self.set_serial_and_batch_bundle_from_pick_list()

    from erpnext.stock.doctype.packed_item.packed_item import make_packing_list

    make_packing_list(self)
    self.update_current_stock()

    if not self.installation_status:
        self.installation_status = "Not Installed"

    self.validate_against_stock_reservation_entries()
    self.reset_default_field_value("set_warehouse", "items", "warehouse");; it ignore the uom and conversion factor between both Sales Order and Delivery Note and add field of density in delivery note items after that write client script for calculation of density (kg/m^3) : 

frappe.ui.form.on(‘Delivery Note Item’, {
uom: function(frm, cdt, cdn) {
update_conversion_factor(cdt, cdn);
},

qty: function(frm, cdt, cdn) {
    update_conversion_factor(cdt, cdn);
},

custom_density: function(frm, cdt, cdn) {
    update_conversion_factor(cdt, cdn);
}

});

// Function to calculate and set the conversion factor
function update_conversion_factor(cdt, cdn) {
let item = locals[cdt][cdn];
let uom = item.uom;
let density = item.custom_density; // Ensure this field exists

let conversion_factor = 0;

// Perform conversion logic based on UOM and density
if (uom === "Tonne") {
    conversion_factor = 1000 / density;
} else if (uom === "Litre") {
    conversion_factor = 1; // Assuming 1 for Litres
} else if (uom === "Kg") {
    conversion_factor = 1 / density;
}



// Set the calculated conversion factor to the field
frappe.model.set_value(cdt, cdn, 'conversion_factor', conversion_factor);

}
;;; this will caclulate qty out from stock it convert Mass(Tonne) to Volume (Litre)

1 Like