[fix] Shipping charges change on qty, item_code and conversion_factor trigger (#12001)
* [wip] shipping rule
* [wip] Shipping Rule tax auto calculate
* [wip] Shipping Rule
* [new] Shipping charges change on different triggers
* [minor] Removed unnecessary comments
* [fix] Minor Changes in function calls
diff --git a/erpnext/public/js/controllers/transaction.js b/erpnext/public/js/controllers/transaction.js
index e86435c..5cb1973 100644
--- a/erpnext/public/js/controllers/transaction.js
+++ b/erpnext/public/js/controllers/transaction.js
@@ -114,7 +114,7 @@
}
if(this.frm.fields_dict["items"]) {
- this["items_remove"] = this.calculate_taxes_and_totals;
+ this["items_remove"] = this.calculate_net_weight;
}
if(this.frm.fields_dict["recurring_print_format"]) {
@@ -336,18 +336,23 @@
callback: function(r) {
if(!r.exc) {
- me.frm.script_manager.trigger("price_list_rate", cdt, cdn);
- me.toggle_conversion_factor(item);
- if(show_batch_dialog && !frappe.flags.hide_serial_batch_dialog) {
- var d = locals[cdt][cdn];
- $.each(r.message, function(k, v) {
- if(!d[k]) d[k] = v;
- });
+ frappe.run_serially([
+ () => me.frm.script_manager.trigger("price_list_rate", cdt, cdn),
+ () => me.toggle_conversion_factor(item),
+ () => {
+ if(show_batch_dialog && !frappe.flags.hide_serial_batch_dialog) {
+ var d = locals[cdt][cdn];
+ $.each(r.message, function(k, v) {
+ if(!d[k]) d[k] = v;
+ });
- erpnext.show_serial_batch_selector(me.frm, d, (item) => {
- me.frm.script_manager.trigger('qty', item.doctype, item.name);
- });
- }
+ erpnext.show_serial_batch_selector(me.frm, d, (item) => {
+ me.frm.script_manager.trigger('qty', item.doctype, item.name);
+ });
+ }
+ },
+ () => me.conversion_factor(doc, cdt, cdn, true)
+ ]);
}
}
});
@@ -663,8 +668,11 @@
var item = frappe.get_doc(cdt, cdn);
frappe.model.round_floats_in(item, ["qty", "conversion_factor"]);
item.stock_qty = flt(item.qty * item.conversion_factor, precision("stock_qty", item));
+ item.total_weight = flt(item.stock_qty * item.weight_per_unit);
refresh_field("stock_qty", item.name, item.parentfield);
+ refresh_field("total_weight", item.name, item.parentfield);
this.toggle_conversion_factor(item);
+ this.calculate_net_weight();
if (!dont_fetch_price_list_rate &&
frappe.meta.has_field(doc.doctype, "price_list_currency")) {
this.apply_price_list(item, true);
@@ -678,20 +686,24 @@
this.frm.fields_dict.items.grid.toggle_enable("conversion_factor",
(item.uom != item.stock_uom)? true: false);
}
+
},
qty: function(doc, cdt, cdn) {
this.conversion_factor(doc, cdt, cdn, true);
this.apply_pricing_rule(frappe.get_doc(cdt, cdn), true);
- this.calculate_total_weight(doc, cdt, cdn, true);
},
- calculate_total_weight: function(doc, cdt, cdn){
- if(frappe.meta.get_docfield(cdt, "weight_per_unit", cdn)) {
- var item = frappe.get_doc(cdt, cdn);
- item.total_weight = flt(item.qty * item.weight_per_unit);
- refresh_field("total_weight", item.name, item.parentfield);
- }
+ calculate_net_weight: function(){
+ /* Calculate Total Net Weight then further applied shipping rule to calculate shipping charges.*/
+ var me = this;
+ this.frm.doc.total_net_weight= 0.0;
+
+ $.each(this.frm.doc["items"] || [], function(i, item) {
+ me.frm.doc.total_net_weight += flt(item.total_weight);
+ });
+ refresh_field("total_net_weight");
+ this.shipping_rule();
},
set_dynamic_labels: function() {