Add doctype link to dashboard using custom app

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

@s_Mafutta did you get the solution for this?

@Rakesh_dev Yes let me share with you my final codes
sales_order.js

//Add Stock Entry in dashboard
var dashboard_sales_order_doctype = function (frm, doctype) {
		frappe.call({
				'method': 'metactical.custom_scripts.sales_order.sales_order.get_open_count',
				'args': {
					'docname': cur_frm.docname,
				},
				'callback': function(r){
					var items = [];
					$.each((r.message), function(i, d){
						items.push(d.name);		
					})
					load_template_links(frm, doctype, items);
				}
		});
}

var load_template_links = function(frm, doctype, items){
	var sales_orders = ['in'];
	var count_links = 0;
	items.forEach(function(item){
		console.log("in loop");		
		if( sales_orders.indexOf(item) == -1){
			count_links++;
			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 + '"]');
	

	// bind links
	self.find(".badge-link").on('click', function () {
		frappe.route_options = {
			"sales_order_no": frm.doc.name
		}
		frappe.set_route("List", doctype);
	});

	self.find('.count').html(count_links);
}

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>';

sales_order.py

@frappe.whitelist()
def get_open_count(**args):
	args = frappe._dict(args)

	doc = frappe.get_all("Stock Entry", 
		filters={
			'sales_order_no': args.docname,
			'purpose': 'Material Transfer',
		},
		fields=[
			'name', 'sales_order_no',
		])
	return doc

Since it’s been a while i don’t remember exactly what made it to work but hopefully going through the code can help you solve your problem.