Skip to content

Instantly share code, notes, and snippets.

@MaxMorais
Last active March 27, 2017 07:34
Show Gist options
  • Save MaxMorais/b8ac4b6ec8cb0364cdf552826a8f0937 to your computer and use it in GitHub Desktop.
Save MaxMorais/b8ac4b6ec8cb0364cdf552826a8f0937 to your computer and use it in GitHub Desktop.
function depends_of(calculated_field, dependant_fields, calculation_function) {
if (calculated_field.indexOf(".") === -1) {
var doctype = cur_frm.doctype,
target_field = calculated_field
} else {
var doctype = calculated_field.split(".")[0],
target_field = calculated_field.split(".")[0],
}
if (!Array.isArray(dependant_fields)) dependand_fields = [dependant_fields];
var depencies = dependant_fields.map(function(dependency){
if (typeof(dependency) === "string"){
if (dependency.indexOf(".") === -1){
return [doctype, dependency];
}
return dependency.split(".").splice(0,2);
}
});
function hook(frm, cdt, cdn){
var doc = cur_frm.doc, child = locals[cdt][cdn],
not_satisfied = dependencies.filter(function(field){
if (field[0] === cdt && !child[field[1]]) return true;
if (field[1] === frm.doctype && !doc[field[1]]) return true;
return false; // maybe the field dont match with the child doctype or master doctype
});
if (not_satisfied.lenght === 0){
try{
var calculated_value = calculation_function(frm, cdt, cdn);
if (doctype === frm.doctype){
frm.set_value(target_field, calculated_value);
} else if (doctype === cdt) {
frappe.model.set_value(cdt, cdn, target_field, calculated_value);
} else {
console.log("You reached at the limitation of this function!\nThis function only can update a value into the active parent doctype or the current child doctype!")
}
} catch (e){
console.log("Ops an error occurred while doing the calculation of the value!");
console.log(e);
}
}
}
dependencies.forEach(function(row){
frappe.ui.form.on(row[0], row[1], hook); // hooking the delegator method
});
}
// One applied example of use!
// See this discussion https://discuss.erpnext.com/t/how-to-do-area-calculation-in-erpnext-for-two-fields/21383/
// See now how it can be simplified!
/*
depends_of("area", ["length", "width"], function(frm, cdt, cdn){
return flt(frm.doc.length* frm.doc.width);
});
*/
// Another example is when the fields into a child table, should update a master doctype
// Let me do an example, with the Sales Order in mind
/*
depends_of("Sales Order.amount_qty", "Sales Order Item"."qty", function(frm, cdt, cdn){
var total_qty = 0;
frm.doc.items.forEach(function(row){
total_qty += cint(row.qty);
});
return total_qty;
});
*/
// An extremely complex example!
/*
depends_of("NFe Produto.icms_st_total", [
["NFe.total_frete", "NFe.total_seguro", "NFe.total_outros", "NFe Produto.total", "NFe Produto.desconto", "NFe Produto.aliquota_icms"],
function(frm, cdt, cdn){
var child = locals[cdt][cdn],
base_icms_inter = (frm.doc.total_frete + frm.doc.total_seguro + frm.doc.total_outros - child.desconto);
return base_icms_inter * (child.aliquota_icms / 100.0);
}
);
*/
// Remembers that for this function work properly the calculation function should return any value!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment