feat: Trigger rule application from client side

- Table is reset and overwritten with applied rules on checkbox trigger
- Sider fixes
This commit is contained in:
marination
2020-12-08 19:11:51 +05:30
parent 0b68243979
commit 0f3cfc502b
9 changed files with 77 additions and 64 deletions

View File

@@ -517,26 +517,3 @@ erpnext.buying.get_items_from_product_bundle = function(frm) {
dialog.show();
}
erpnext.apply_putaway_rule = (frm) => {
if (!frm.doc.company) {
frappe.throw({message:__("Please select a Company first."), title: __("Mandatory")})
}
if (!frm.doc.items.length) return;
frappe.call({
method: "erpnext.stock.doctype.putaway_rule.putaway_rule.apply_putaway_rule",
args: {
items: frm.doc.items,
company: frm.doc.company
},
callback: (result) => {
if(!result.exc) {
if(result.message) {
frm.doc.items = result.message;
frm.get_field("items").refresh();
}
}
}
});
}

View File

@@ -2029,3 +2029,33 @@ erpnext.show_serial_batch_selector = function (frm, d, callback, on_close, show_
}, show_dialog);
});
}
erpnext.apply_putaway_rule = (frm) => {
if (!frm.doc.company) {
frappe.throw({message: __("Please select a Company first."), title: __("Mandatory")});
}
if (!frm.doc.items.length) return;
frappe.call({
method: "erpnext.stock.doctype.putaway_rule.putaway_rule.apply_putaway_rule",
args: {
doctype: frm.doctype,
items: frm.doc.items,
company: frm.doc.company,
sync: true
},
callback: (result) => {
if (!result.exc && result.message) {
frm.clear_table("items");
let items = result.message;
items.forEach((row) => {
delete row["name"];
let child = frm.add_child("items");
Object.assign(child, row);
});
frm.get_field("items").grid.refresh();
}
}
});
};

View File

@@ -88,7 +88,7 @@ erpnext.stock.ItemDashboard = Class.extend({
start: this.start,
sort_by: this.sort_by,
sort_order: this.sort_order
}
};
var me = this;
frappe.call({
@@ -104,10 +104,12 @@ erpnext.stock.ItemDashboard = Class.extend({
this.max_count = 0;
this.result.empty();
}
let context = "";
if (this.page_name === "warehouse-capacity-summary") {
var context = this.get_capacity_dashboard_data(data);
context = this.get_capacity_dashboard_data(data);
} else {
var context = this.get_item_dashboard_data(data, this.max_count, true);
context = this.get_item_dashboard_data(data, this.max_count, true);
}
this.max_count = this.max_count;
@@ -179,9 +181,9 @@ erpnext.stock.ItemDashboard = Class.extend({
return {
data: data,
can_write: can_write,
};
}
}
})
});
erpnext.stock.move_item = function(item, source, target, actual_qty, rate, callback) {
var dialog = new frappe.ui.Dialog({

View File

@@ -214,7 +214,7 @@ erpnext.stock.PurchaseReceiptController = erpnext.buying.BuyingController.extend
},
apply_putaway_rule: function() {
// if (this.frm.doc.apply_putaway_rule) erpnext.apply_putaway_rule(this.frm);
if (this.frm.doc.apply_putaway_rule) erpnext.apply_putaway_rule(this.frm);
}
});

View File

@@ -87,7 +87,7 @@ class PurchaseReceipt(BuyingController):
from erpnext.stock.doctype.putaway_rule.putaway_rule import apply_putaway_rule
if self.get("items") and self.apply_putaway_rule:
self.items = apply_putaway_rule(self.doctype, self.get("items"), self.company)
apply_putaway_rule(self.doctype, self.get("items"), self.company)
def validate(self):
self.validate_posting_time()

View File

@@ -64,7 +64,7 @@ def get_putaway_capacity(rule):
return free_space if free_space > 0 else 0
@frappe.whitelist()
def apply_putaway_rule(doctype, items, company):
def apply_putaway_rule(doctype, items, company, sync=None):
""" Applies Putaway Rule on line items.
items: List of Purchase Receipt Item objects
@@ -82,7 +82,7 @@ def apply_putaway_rule(doctype, items, company):
source_warehouse = item.get("s_warehouse")
serial_nos = get_serial_nos(item.get("serial_no"))
conversion = flt(item.conversion_factor) or 1
item.conversion_factor = flt(item.conversion_factor) or 1
pending_qty, item_code = flt(item.qty), item.item_code
pending_stock_qty = flt(item.transfer_qty) if doctype == "Stock Entry" else flt(item.stock_qty)
if not pending_qty or not item_code:
@@ -109,11 +109,11 @@ def apply_putaway_rule(doctype, items, company):
for rule in item_wise_rules[item_code]:
if pending_stock_qty > 0 and rule.free_space:
stock_qty_to_allocate = flt(rule.free_space) if pending_stock_qty >= flt(rule.free_space) else pending_stock_qty
qty_to_allocate = stock_qty_to_allocate / (conversion)
qty_to_allocate = stock_qty_to_allocate / item.conversion_factor
if uom_must_be_whole_number:
qty_to_allocate = floor(qty_to_allocate)
stock_qty_to_allocate = qty_to_allocate * conversion
stock_qty_to_allocate = qty_to_allocate * item.conversion_factor
if not qty_to_allocate: break
@@ -124,17 +124,19 @@ def apply_putaway_rule(doctype, items, company):
pending_qty -= qty_to_allocate
rule["free_space"] -= stock_qty_to_allocate
if not pending_stock_qty: break
if not pending_stock_qty > 0: break
# if pending qty after applying all rules, add row without warehouse
if pending_stock_qty > 0:
# updated_table = add_row(item, pending_qty, '', updated_table, serial_nos=serial_nos)
items_not_accomodated.append([item.item_code, pending_qty])
if items_not_accomodated:
show_unassigned_items_message(items_not_accomodated)
return updated_table if updated_table else items
items[:] = updated_table if updated_table else items # modify items table
if sync and json.loads(sync): # sync with client side
return items
def get_ordered_putaway_rules(item_code, company, source_warehouse=None):
"""Returns an ordered list of putaway rules to apply on an item."""
@@ -174,12 +176,14 @@ def get_ordered_putaway_rules(item_code, company, source_warehouse=None):
def add_row(item, to_allocate, warehouse, updated_table, rule=None, serial_nos=None):
new_updated_table_row = copy.deepcopy(item)
new_updated_table_row.idx = 1 if not updated_table else cint(updated_table[-1].idx) + 1
new_updated_table_row.name = "New " + str(item.doctype) + " " + str(new_updated_table_row.idx)
new_updated_table_row.name = None
new_updated_table_row.qty = to_allocate
new_updated_table_row.stock_qty = flt(to_allocate) * flt(new_updated_table_row.conversion_factor)
if item.doctype == "Stock Entry Detail":
new_updated_table_row.t_warehouse = warehouse
new_updated_table_row.transfer_qty = flt(to_allocate) * flt(new_updated_table_row.conversion_factor)
else:
new_updated_table_row.stock_qty = flt(to_allocate) * flt(new_updated_table_row.conversion_factor)
new_updated_table_row.warehouse = warehouse
new_updated_table_row.rejected_qty = 0
new_updated_table_row.received_qty = to_allocate

View File

@@ -574,9 +574,9 @@ frappe.ui.form.on('Stock Entry', {
},
apply_putaway_rule: function (frm) {
// if (frm.doc.apply_putaway_rule) erpnext.apply_putaway_rule(frm);
if (frm.doc.apply_putaway_rule) erpnext.apply_putaway_rule(frm);
}
})
});
frappe.ui.form.on('Stock Entry Detail', {
qty: function(frm, cdt, cdn) {

View File

@@ -47,7 +47,7 @@ class StockEntry(StockController):
apply_rule = self.apply_putaway_rule and (self.purpose in ["Material Transfer", "Material Receipt"])
if self.get("items") and apply_rule:
self.items = apply_putaway_rule(self.doctype, self.get("items"), self.company)
apply_putaway_rule(self.doctype, self.get("items"), self.company)
def validate(self):
self.pro_doc = frappe._dict()

View File

@@ -90,14 +90,14 @@ frappe.pages['warehouse-capacity-summary'].on_page_load = function(wrapper) {
sort_order: 'desc',
method: 'erpnext.stock.dashboard.warehouse_capacity_dashboard.get_data',
template: 'warehouse_capacity_summary'
})
});
page.capacity_dashboard.before_refresh = function() {
this.item_code = page.item_field.get_value();
this.warehouse = page.warehouse_field.get_value();
this.parent_warehouse = page.parent_warehouse_field.get_value();
this.company = page.company_field.get_value();
}
};
page.capacity_dashboard.refresh();
@@ -106,15 +106,15 @@ frappe.pages['warehouse-capacity-summary'].on_page_load = function(wrapper) {
var name = $(this).attr('data-name');
var field = page[doctype.toLowerCase() + '_field'];
if (field.get_value()===name) {
frappe.set_route('Form', doctype, name)
frappe.set_route('Form', doctype, name);
} else {
field.set_input(name);
page.capacity_dashboard.refresh();
}
});
}
};
setup_click('Item');
setup_click('Warehouse');
});
}
};