Price_list_rate wrong calculation & Pasted data trigger

Hi everybody,

We have Signage Company, we have to consider of 2 different rates for our products, one is finshed product rate(at factory) and other is their installations rates(service),

So we added custom fields to following doctypes:

  • Item Price (Price List) :

    • item_rate :currency
    • installation_rate: currency
  • Quotation Item:

    • item_rate :currency
    • installation_rate: currency
  • Quotation:

    • total_item_rate :currency (ready_only)
    • total_installation_rate: currency (read_only)
  • Sales Order Item:

    • item_rate :currency
    • installation_rate: currency
  • Sales Order:

    • total_item_rate :currency (read_only)
    • total_installation_rate: currency (read_only)

I write custom scripts to calculate them


  • Item Price Script:
frappe.ui.form.on('Item Price', {
  item_rate(frm) {
    frm.set_value('price_list_rate',frm.doc.item_rate + frm.doc.installation_rate);
    frm.refresh_field('price_list_rate');
  },
  installation_rate(frm) {
    frm.set_value('price_list_rate',frm.doc.item_rate + frm.doc.installation_rate);
    frm.refresh_field('price_list_rate');
  }
  });

Problems:

  • Item Price script
    • Working:

      • When i entered item_rate and installation_rate in Full page edit.
    • Not Working:

      • When i entered item_rate and installation_rate at Quick Enrty.
      • When i changed item_rate and/or installation_rate at Report View

Also i would like to mention there seems to be a bug which you enter rate with Quick Entry item default UOM aren’t fetched opened a bug report Bug:#20785 .


Quotation & Sales Order (Of course its start with frappe.ui.form.on('Sales Order Item') Script:

frappe.ui.form.on('Quotation Item', {
  item_code(frm, cdt, cdn) {  // When items code entered/pasted fetch item_rate & installation_rate from price list
    cur_frm.cscript.fetch_rate(frm, cdt, cdn);
  },
  items_add(frm, cdt, cdn) { // When a new row added calculate again
    cur_frm.cscript.calculate(frm, cdt, cdn);
  },
  items_remove(frm, cdt, cdn) { // When a row removed calculate again
    cur_frm.cscript.calculate(frm, cdt, cdn);
  },
  item_rate(frm, cdt, cdn) { // When a item_rate manually entered calculate again
    cur_frm.cscript.calculate(frm, cdt, cdn);
  },
  installation_rate(frm, cdt, cdn) { // When a installation_rate manually entered calculate again
    cur_frm.cscript.calculate(frm, cdt, cdn);
  },
  qty(frm, cdt, cdn) { // When quantity manually entered calculate again
    cur_frm.cscript.calculate(frm, cdt, cdn);
  }
});
cur_frm.cscript.fetch_rate = function(frm, cdt, cdn){ //fetch item_rate & installation_rate from 'item Price'
  var row = locals[cdt][cdn];
  frappe.call({
    method: 'frappe.client.get_value',
    args: {
      doctype: 'Item Price',
      filters: {
        'item_code': row.item_code,
        'price_list': frm.doc.selling_price_list,
      },
      fieldname: ['item_rate','installation_rate']
    },
    callback: function (data) {
      frappe.model.set_value(cdt, cdn, 'item_rate', data.message.item_rate);
      frappe.model.set_value(cdt, cdn, 'installation_rate', data.message.installation_rate);
      frm.refresh_field('item_rate');
      frm.refresh_field('installation_rate');
      frm.refresh_field('rate');
      cur_frm.cscript.calculate(frm, cdt, cdn);
    }
  });
};
cur_frm.cscript.calculate = function(frm, cdt, cdn){ // make calculation for row rate, total_item_rate and total_installation rate 
  var row = locals[cdt][cdn];
  var total_item = 0;
  var total_installation = 0;
  frm.doc.items.forEach(function(row) {
    frappe.model.set_value(row.doctype, row.name, 'rate', (row.item_rate + row.installation_rate)); //calculate rate for each row
    total_item += (row.item_rate * row.qty);
    total_installation += (row.installation_rate * row.qty);
  });
  frm.set_value('total_item_rate', total_item);
  frm.set_value('total_installation_rate', total_installation);
  frm.refresh_field('rate');
  frm.refresh_field('total_item_rate');
  frm.refresh_field('total_installation_rate');
};

2 situation here:

  • items with price list rates
  • items without price list rates

Problems:

Items With Price List Rate

  1. only paste item_code
Item_code qty item_rate installation_rate
3BTK.TTN01
6NİS.KHF39

Fetched item_rate and installation_rate right. but price_list_rate of last row is wrong. total_item_rate and total_installation_rate aren’t calculated at main doctype fields.

  1. paste item_code & qty
Item_code qty item_rate installation_rate
3BTK.TTN01 2
6NİS.KHF39 4

Fetched item_rate and installation_rate right. but price_list_rate of last row is wrong. total_item_rate and total_installation_rate are calculated right at main doctype fields.

  1. When Changed any value

    item_rate,installation_rate, qty, when changed one of those fields everything works well and last row rate recalculated right.

Items Without Price List Rate

Item_code qty item_rate installation_rate
3BTK.TTN01 2 12 24
6NİS.KHF39 4 34 54

Price_list_rate for each row aren’t calculated. total_item_rate and total_installation_rate are calculated right.
When i added or removed a row price_list_rate are calculated.


So my question are:

  • Whats wrong with my Item Price script.
  • Whats wrong with my Quotation & Sales Order script so its calculate last row wrong when i paste multi item with price list .
  • Is there any way to make Quotation & Sales Order script to calculate price_list_rate without add/remove row when pasted multi item from excel like table?

Solved! Make calculation manually via a button.