Calculate a value for custom field

Hi

I have a custom field deposit_required on DocType Quotation which should be 60% of the grand total

deposit_required = grand_total*0.6

The following script has updated all existing quotes but when I start a new quote I can’t get the value for deposit_required

cur_frm.cscript.deposit_required = function(doc) {
rate_calc = (doc.grand_total / 100 * 60);
doc.deposit_required = rate_calc;
refresh_field(“deposit_required”);
}

Appreciate any help.

Should work. Try the new API:

try:

frappe.ui.form.on("Quotation", "deposit_required", function(frm) {
  frm.set_value("deposit_required", frm.doc.grand_total * 0.6);
});

Also use console.log to debug.

2 Likes

Thank you! This works now but I do have to type something into the field and click away for it to refresh.

Would this work the same on a child table within a form? I want to do calculations on Quotation Item and I’ve used the code you suggested above but it’s not calculating.

frappe.ui.form.on(“Quotation Item”, “full_price”, function(frm) {
frm.set_value(“full_price”, frm.doc.qty * frm.doc.price_list_rate);
});

Its a bit different for child items.

frappe.ui.form.on("Quotation Item", "full_price", function(frm, doctype, name) {
  var row = locals[doctype][name];
  row.full_price = row.qty * row.price_list_rate;
  refresh_field("items");
});
7 Likes

@rmehta using your sample, I’d like to calculate net rate in Purchase Receipt Item based on multilevel discounts. For testing, I created new field : discount_percentage2

Then add below codes in Purchase Receipt Item doctype:

frappe.ui.form.on("Purchase Receipt Item", "discount_percentage2", function(frm, doctype, name) {
  var row = locals[doctype][name];
  row.rate = row.discount_percentage2/100 * flt(row.price_list_rate);
  refresh_field("items");
});

When I change discount_percentage2, nothing’s happened…what wrong? No clues in console…

frappe.ui.form.on("Purchase Receipt", "discount_percentage2", function(frm, doctype, name) {

Write Purchase Receipt instead of Purchase Receipt Item

frappe.ui.form.on("Purchase Receipt", "discount_percentage2", function(frm, doctype, name) {
  var row = locals[doctype][name];
  row.rate = row.discount_percentage2/100 * flt(row.price_list_rate);
  refresh_field("items");
});

@kolate_sambhaji still not working. I put the custom script in Purchase Receipt Item.

@jof2jc test this script and then modify according to yours, also Put this script on Purchase Receipt.
In this script, I am changing warehouse on change of item_code.

frappe.ui.form.on("Purchase Receipt Item", "item_code", function(frm, cdt, cdn) {
    var d = locals[cdt][cdn];
    d.warehouse="Finished Goods - IP";
    refresh_field('warehouse', d.name, 'items');
    msgprint(d.item_code);
})

@kolate_sambhaji @rmehta, Hi guys, I have items that are sold by piece units, but we need to input weight * price per weight = total price per weight, just to print in the sales order with no extra function in the system, so I have made 3 custom fields:

  1. item_weight (manual input)
  2. item_weight_price (manual input)
  3. item_weight_price_total (auto input “item_weight * item_weight_price = item_weight_price_total”)

I use this script that I took from above:

frappe.ui.form.on(“Sales Order Item”, “item_weight_price_total”, function(frm, doctype, name) {
var row = locals[doctype][name];
row.item_weight_price_total = row.item_weight * row.item_weight_price;
refresh_field(“item_weight_price_total”);
});

Hope you can help me out, thank you so much!

Mo

write script on item_weight and item_weight_price,
So on changing item_weight/item_weight_price, item_weight_price_total will be calculated

   frappe.ui.form.on("Sales Order Item", "item_weight", function(frm, doctype, name) {
          var row = locals[doctype][name];
          row.item_weight_price_total = row.item_weight * row.item_weight_price;
          refresh_field("item_weight_price_total");
        });
    frappe.ui.form.on("Sales Order Item", "item_weight_price", function(frm, doctype, name) {
      var row = locals[doctype][name];
      row.item_weight_price_total = row.item_weight * row.item_weight_price;
      refresh_field("item_weight_price_total");
    });
1 Like

@kolate_sambhaji thank you for your help.
Your code works only if I change

refresh_field(“item_weight_price_total”);

into the following:

refresh_field(“items”);

But then I have to enter something in field “item_weight_price_total” manually to refresh. I appreciate your help, thanks.

Hi @kolate_sambhaji I have got it set with the following script

frappe.ui.form.on(“Sales Order Item”, “item_weight”, function(frm, doctype, name) {
var row = locals[doctype][name];
row.item_weight_price_total = row.item_weight * row.item_weight_price;
refresh_field(“items”);
});
frappe.ui.form.on(“Sales Order Item”, “item_weight_price”, function(frm, doctype, name) {
var row = locals[doctype][name];
row.item_weight_price_total = row.item_weight * row.item_weight_price;
refresh_field(“items”);
});

Now it refreshes automatically, strange but it works. Thanks for your kindness.

@mrmo
if you use

frappe.model.set_value(cdt, cdn, "field_name", value);

then no need to refresh_field

1 Like

@kolate_sambhaji Could you please give a hint where to insert this

frappe.model.set_value(cdt, cdn, “field_name”, value);

Thanks

frappe.ui.form.on("Sales Order Item", "item_weight", function(frm, cdt, cdn) {
       var d = locals[cdt][cdn];
       frappe.model.set_value(cdt, cdn, "item_weight_price_total", d.item_weight * d.item_weight_price);
});
3 Likes

@jof2jc Thank you!

I’d like to do something extremely similar to this with Material Request Items, but I can’t find the file to insert this code.

Do you just insert the python into the Material Request doctype, or where?

@JamesE You insert it into the custom script which you can find in the Setup module under customize

Hello @Roy_Stephen, how did you sort our the refresh issue you have? I have the same problem.

Hi Ahmed

I never got it working perfectly and it is long but this was my last effort

I’m sure there’s a better way but I haven’t worked on it for months now.

Hope it helps

1 Like