I are trying to group the items in the Sales Invoice based on the Item Template (variant_of) and the unit_rate.
I have created a field ‘Grouped Items’ in Sales Invoice DocType as follows:
Next we created the Custom Script as follows:
frappe.ui.form.on('Sales Invoice', {
validate: function(frm) {
var grouped_list = []
var already_filtered = [];
// Loops through all items of array
for(var i = 0, len1=frm.doc.items.length; i < len1; i++) {
// Skip rest of the loop if the item has been grouped with some previous item
if(already_filtered.indexOf(i) != -1) {
continue;
}
// Object to hold item temporary before grouping
var grouped_item = {
item_code : frm.doc.items[i].item_variant_of,
item_name : [frm.doc.items[i].item_name],
gst_hsn_code: frm.doc.items[i].gst_hsn_code,
qty : frm.doc.items[i].qty,
net_amount: frm.doc.items[i].net_amount
};
// Loop to check the current item with rest of the items
for(var j = i+1; j < len1; j++) {
// Skip this loop if the current item has been grouped with some previous item.
if(already_filtered.indexOf(j) != -1) {
continue;
}
// Check if item_variant_of and net_rate is equal of 2 items.
if(frm.doc.items[i].item_variant_of === frm.doc.items[j].item_variant_of && frm.doc.items[i].net_rate === frm.doc.items[j].net_rate) {
// combine item_name
grouped_item.item_name.push(frm.doc.items[j].item_name);
// Add item quantity
grouped_item.qty += frm.doc.items[j].qty;
// Add net amount
grouped_item.net_amount += frm.doc.items[j].net_amount;
// Push the item to already_filtered array so it can be skipped later while comparing with rest of the items
already_filtered.push(j);
}
}
// Join item names array to show as string separated with ,
grouped_item.item_name = grouped_item.item_name.join(', ');
// Add the grouped item to the final output list
grouped_list.push(grouped_item);
}
frm.set_value('grouped_items', grouped_list);
}
})
The error log is as follows, please share what may be the issue:
Traceback (most recent call last):
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/desk/form/save.py", line 12, in savedocs
doc = frappe.get_doc(json.loads(doc))
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/__init__.py", line 638, in get_doc
return frappe.model.document.get_doc(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/document.py", line 68, in get_doc
return controller(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/erpnext/erpnext/accounts/doctype/sales_invoice/sales_invoice.py", line 31, in __init__
super(SalesInvoice, self).__init__(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/erpnext/erpnext/controllers/accounts_controller.py", line 19, in __init__
super(AccountsController, self).__init__(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/document.py", line 112, in __init__
super(Document, self).__init__(kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 54, in __init__
self.update(d)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 77, in update
self.set(key, value)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 124, in set
self.extend(key, value)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 152, in extend
self.append(key, v)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 138, in append
value = self._init_child(value, key)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 166, in _init_child
raise AttributeError(key)
AttributeError: grouped_items
Traceback (most recent call last):
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/app.py", line 62, in application
response = frappe.handler.handle()
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/handler.py", line 22, in handle
data = execute_cmd(cmd)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/handler.py", line 53, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/__init__.py", line 939, in call
return fn(*args, **newargs)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/desk/form/save.py", line 12, in savedocs
doc = frappe.get_doc(json.loads(doc))
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/__init__.py", line 638, in get_doc
return frappe.model.document.get_doc(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/document.py", line 68, in get_doc
return controller(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/erpnext/erpnext/accounts/doctype/sales_invoice/sales_invoice.py", line 31, in __init__
super(SalesInvoice, self).__init__(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/erpnext/erpnext/controllers/accounts_controller.py", line 19, in __init__
super(AccountsController, self).__init__(*args, **kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/document.py", line 112, in __init__
super(Document, self).__init__(kwargs)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 54, in __init__
self.update(d)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 77, in update
self.set(key, value)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 124, in set
self.extend(key, value)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 152, in extend
self.append(key, v)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 138, in append
value = self._init_child(value, key)
File "/home/frappe/benches/bench-2018-05-15/apps/frappe/frappe/model/base_document.py", line 166, in _init_child
raise AttributeError(key)
AttributeError: grouped_items