Merge pull request #16277 from deepeshgarg007/loyalty-program-fix

fix: Loyalty program issue fix in customer dashboard
This commit is contained in:
Nabin Hait 2019-01-08 12:15:57 +05:30 committed by GitHub
commit c190192d5f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 36 deletions

View File

@ -7,6 +7,7 @@ import frappe
import unittest
from frappe.utils import today, cint, flt, getdate
from erpnext.accounts.doctype.loyalty_program.loyalty_program import get_loyalty_program_details_with_points
from erpnext.accounts.party import get_dashboard_info
class TestLoyaltyProgram(unittest.TestCase):
@classmethod
@ -144,6 +145,13 @@ class TestLoyaltyProgram(unittest.TestCase):
frappe.get_doc('Sales Invoice', d.name).cancel()
frappe.delete_doc('Sales Invoice', d.name)
def test_loyalty_points_for_dashboard(self):
doc = frappe.get_doc('Customer', 'Test Loyalty Customer')
company_wise_info = get_dashboard_info("Customer", doc.name, doc.loyalty_program)
for d in company_wise_info:
self.assertTrue(d.loyalty_points)
def get_points_earned(self):
def get_returned_amount():
returned_amount = frappe.db.sql("""

View File

@ -454,7 +454,7 @@ def get_timeline_data(doctype, name):
return out
def get_dashboard_info(party_type, party):
def get_dashboard_info(party_type, party, loyalty_program=None):
current_fiscal_year = get_fiscal_year(nowdate(), as_dict=True)
doctype = "Sales Invoice" if party_type=="Customer" else "Purchase Invoice"
@ -476,6 +476,19 @@ def get_dashboard_info(party_type, party):
fields=["company", "sum(grand_total) as grand_total", "sum(base_grand_total) as base_grand_total"]
)
loyalty_point_details = []
if party_type == "Customer":
loyalty_point_details = frappe._dict(frappe.get_all("Loyalty Point Entry",
filters={
'customer': party,
'expiry_date': ('>=', getdate()),
},
group_by="company",
fields=["company", "sum(loyalty_points) as loyalty_points"],
as_list =1
))
company_wise_billing_this_year = frappe._dict()
for d in company_wise_grand_total:
@ -503,12 +516,18 @@ def get_dashboard_info(party_type, party):
total_unpaid = flt(company_wise_total_unpaid.get(d.company))
if loyalty_point_details:
loyalty_points = loyalty_point_details.get(d.company)
info = {}
info["billing_this_year"] = flt(billing_this_year) if billing_this_year else 0
info["currency"] = party_account_currency
info["total_unpaid"] = flt(total_unpaid) if total_unpaid else 0
info["company"] = d.company
if party_type == "Customer" and loyalty_point_details:
info["loyalty_points"] = loyalty_points
if party_type == "Supplier":
info["total_unpaid"] = -1 * info["total_unpaid"]

View File

@ -105,33 +105,52 @@ $.extend(erpnext.utils, {
if(frm.doc.__onload && frm.doc.__onload.dashboard_info) {
var company_wise_info = frm.doc.__onload.dashboard_info;
if(company_wise_info.length > 1) {
frm.dashboard.stats_area.removeClass('hidden');
frm.dashboard.stats_area_row.addClass('flex');
frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
company_wise_info.forEach(function(info) {
frm.dashboard.stats_area_row.append(
'<div class="flex-column col-xs-6">'+
'<div style="margin-bottom:20px"><h6>'+info.company+'</h6></div>'+
'<div class="badge-link small" style="margin-bottom:10px">Annual Billing: '
+format_currency(info.billing_this_year, info.currency)+'</div>'+
'<div class="badge-link small" style="margin-bottom:20px">Total Unpaid: '
+format_currency(info.total_unpaid, info.currency)+'</div>'+
'</div>'
);
erpnext.utils.add_indicator_for_multicompany(frm, info);
});
}
else if (company_wise_info.length === 1) {
frm.dashboard.stats_area.removeClass('hidden');
frm.dashboard.stats_area_row.append(
'</div><div class="col-xs-6 small" style="margin-bottom:10px">Annual Billing: <b>'
+format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)+'</b></div>' +
'<div class="col-xs-6 small" style="margin-bottom:10px">Total Unpaid: <b>'
+format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)+'</b></div>'
);
} else if (company_wise_info.length === 1) {
frm.dashboard.add_indicator(__('Annual Billing: {0}',
[format_currency(company_wise_info[0].billing_this_year, company_wise_info[0].currency)]), 'blue');
frm.dashboard.add_indicator(__('Total Unpaid: {0}',
[format_currency(company_wise_info[0].total_unpaid, company_wise_info[0].currency)]),
company_wise_info[0].total_unpaid ? 'orange' : 'green');
if(company_wise_info[0].loyalty_points) {
frm.dashboard.add_indicator(__('Loyalty Points: {0}',
[company_wise_info[0].loyalty_points]), 'blue');
}
}
}
},
add_indicator_for_multicompany: function(frm, info) {
frm.dashboard.stats_area.removeClass('hidden');
frm.dashboard.stats_area_row.addClass('flex');
frm.dashboard.stats_area_row.css('flex-wrap', 'wrap');
var color = info.total_unpaid ? 'orange' : 'green';
var indicator = $('<div class="flex-column col-xs-6">'+
'<div style="margin-top:10px"><h6>'+info.company+'</h6></div>'+
'<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
'Annual Billing: '+format_currency(info.billing_this_year, info.currency)+'</span></div>'+
'<div class="badge-link small" style="margin-bottom:10px">'+
'<span class="indicator '+color+'">Total Unpaid: '
+format_currency(info.total_unpaid, info.currency)+'</span></div>'+
'</div>').appendTo(frm.dashboard.stats_area_row);
if(info.loyalty_points){
$('<div class="badge-link small" style="margin-bottom:10px"><span class="indicator blue">'+
'Loyalty Points: '+info.loyalty_points+'</span></div>').appendTo(indicator);
}
return indicator;
},
get_party_name: function(party_type) {
var dict = {'Customer': 'customer_name', 'Supplier': 'supplier_name', 'Employee': 'employee_name',
'Member': 'member_name'};

View File

@ -113,11 +113,6 @@ frappe.ui.form.on("Customer", {
// indicator
erpnext.utils.set_party_dashboard_indicators(frm);
//
if (frm.doc.__onload.dashboard_info.loyalty_point) {
frm.dashboard.add_indicator(__('Loyalty Point: {0}', [frm.doc.__onload.dashboard_info.loyalty_point]), 'blue');
}
} else {
frappe.contacts.clear_address_and_contact(frm);
}

View File

@ -23,10 +23,7 @@ class Customer(TransactionBase):
self.load_dashboard_info()
def load_dashboard_info(self):
info = get_dashboard_info(self.doctype, self.name)
loyalty_point_details = self.get_loyalty_points()
if loyalty_point_details and loyalty_point_details.get("loyalty_points"):
info["loyalty_point"] = loyalty_point_details.loyalty_points
info = get_dashboard_info(self.doctype, self.name, self.loyalty_program)
self.set_onload('dashboard_info', info)
def autoname(self):
@ -36,11 +33,6 @@ class Customer(TransactionBase):
else:
set_name_by_naming_series(self)
def get_loyalty_points(self):
if self.loyalty_program:
from erpnext.accounts.doctype.loyalty_program.loyalty_program import get_loyalty_details
return get_loyalty_details(self.name, self.loyalty_program)
def get_customer_name(self):
if frappe.db.get_value("Customer", self.customer_name):
count = frappe.db.sql("""select ifnull(MAX(CAST(SUBSTRING_INDEX(name, ' ', -1) AS UNSIGNED)), 0) from tabCustomer