I am trying to add a link on sales order dashboard to Stock Entry(Material Transfer), I have customized to create Stock Entry(Material Transfer) from Sales order. The link works but i am having trouble getting the count of number of Stock entry links associated with a particular sales order.
here is the js & py code respectively
custom_app/custom_scripts/sales_order/sales_order.js
var dashboard_sales_order_doctype = function (frm, doctype) {
var sales_orders = ['in'];
var count = 0;
var items = [];
//console.log(sales_orders);
frappe.call({
'method': 'custom_app.custom_scripts.sales_order.sales_order.get_open_count',
'args': {
'docname': cur_frm.docname,
},
'callback': function(r){
$.each(r.message, function(i, d){
items.push(d.name);
})
//items.push(r.message);
}
});
items.forEach(function(item){
if( sales_orders.indexOf(item) == -1){
frappe.throw('Yess');
count++;
sales_orders.push(item);
}
});
var parent = $('.form-dashboard-wrapper [data-doctype="Purchase Order"]').closest('div').parent();
parent.find('[data-doctype="' + doctype + '"]').remove();
parent.append(frappe.render_template("dashboard_sales_order_doctype", {
doctype: doctype
}));
var self = parent.find('[data-doctype="' + doctype + '"]');
//set_open_count(frm, doctype);
// bind links
self.find(".badge-link").on('click', function () {
frappe.route_options = {
"sales_order_no": frm.doc.name
}
frappe.set_route("List", doctype);
});
console.log(count);
self.find('.count').html(count);
}
frappe.templates["dashboard_sales_order_doctype"] = ' \
<div class="document-link" data-doctype="{{ doctype }}"> \
<a class="badge-link small">{{ __(doctype) }}</a> \
<span class="text-muted small count"></span> \
<span class="open-notification hidden" title="{{ __("Open {0}", [__(doctype)])}}"></span> \
</div>';
custom_app/custom_scripts/sales_order/sales_order.py
from __future__ import unicode_literals
import frappe
@frappe.whitelist()
def get_open_count(**args):
args = frappe._dict(args)
return frappe.get_all("Stock Entry",
filters={
'sales_order_no': args.docname,
'purpose': 'Material Transfer',
},
fields=[
'name', 'sales_order_no',
])
Screenshot of Stock Entry link being successfully added (and it works if clicked) but with the wrong count
Any help would be highly appreciated