2018-11-12 10:45:54 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
2019-01-22 12:52:20 +00:00
|
|
|
from __future__ import unicode_literals
|
2018-11-12 10:45:54 +00:00
|
|
|
import frappe
|
|
|
|
import frappe.defaults
|
|
|
|
import unittest
|
|
|
|
from erpnext.selling.report.sales_analytics.sales_analytics import execute
|
|
|
|
from erpnext.selling.doctype.sales_order.test_sales_order import make_sales_order
|
|
|
|
|
|
|
|
class TestAnalytics(unittest.TestCase):
|
2018-11-23 10:45:32 +00:00
|
|
|
def test_sales_analytics(self):
|
|
|
|
frappe.db.sql("delete from `tabSales Order` where company='_Test Company 2'")
|
2018-11-12 10:45:54 +00:00
|
|
|
|
2018-11-23 10:45:32 +00:00
|
|
|
create_sales_orders()
|
2018-11-12 10:45:54 +00:00
|
|
|
|
2018-11-23 10:45:32 +00:00
|
|
|
self.compare_result_for_customer()
|
|
|
|
self.compare_result_for_customer_group()
|
|
|
|
self.compare_result_for_customer_based_on_quantity()
|
|
|
|
|
|
|
|
|
|
|
|
def compare_result_for_customer(self):
|
2018-11-12 10:45:54 +00:00
|
|
|
filters = {
|
|
|
|
'doc_type': 'Sales Order',
|
|
|
|
'range': 'Monthly',
|
|
|
|
'to_date': '2018-03-31',
|
|
|
|
'tree_type': 'Customer',
|
2018-11-15 10:54:34 +00:00
|
|
|
'company': '_Test Company 2',
|
2018-11-12 10:45:54 +00:00
|
|
|
'from_date': '2017-04-01',
|
|
|
|
'value_quantity': 'Value'
|
|
|
|
}
|
|
|
|
|
|
|
|
report = execute(filters)
|
|
|
|
|
|
|
|
expected_data = [
|
|
|
|
{
|
|
|
|
"entity": "_Test Customer 1",
|
|
|
|
"entity_name": "_Test Customer 1",
|
2018-11-29 08:58:01 +00:00
|
|
|
"apr_2017": 0.0,
|
|
|
|
"may_2017": 0.0,
|
|
|
|
"jun_2017": 0.0,
|
|
|
|
"jul_2017": 0.0,
|
|
|
|
"aug_2017": 0.0,
|
|
|
|
"oct_2017": 0.0,
|
|
|
|
"sep_2017": 0.0,
|
|
|
|
"nov_2017": 0.0,
|
|
|
|
"dec_2017": 0.0,
|
|
|
|
"jan_2018": 0.0,
|
|
|
|
"feb_2018": 2000.0,
|
|
|
|
"mar_2018": 0.0,
|
2018-11-12 10:45:54 +00:00
|
|
|
"total":2000.0
|
|
|
|
},
|
|
|
|
{
|
2018-11-23 10:45:32 +00:00
|
|
|
"entity": "_Test Customer 2",
|
|
|
|
"entity_name": "_Test Customer 2",
|
2018-11-29 08:58:01 +00:00
|
|
|
"apr_2017": 0.0,
|
|
|
|
"may_2017": 0.0,
|
|
|
|
"jun_2017": 0.0,
|
|
|
|
"jul_2017": 0.0,
|
|
|
|
"aug_2017": 0.0,
|
|
|
|
"sep_2017": 1500.0,
|
|
|
|
"oct_2017": 1000.0,
|
|
|
|
"nov_2017": 0.0,
|
|
|
|
"dec_2017": 0.0,
|
|
|
|
"jan_2018": 0.0,
|
|
|
|
"feb_2018": 0.0,
|
|
|
|
"mar_2018": 0.0,
|
2018-11-23 10:45:32 +00:00
|
|
|
"total":2500.0
|
2018-11-12 10:45:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-23 10:45:32 +00:00
|
|
|
"entity": "_Test Customer 3",
|
|
|
|
"entity_name": "_Test Customer 3",
|
2018-11-29 08:58:01 +00:00
|
|
|
"apr_2017": 0.0,
|
|
|
|
"may_2017": 0.0,
|
|
|
|
"jun_2017": 2000.0,
|
|
|
|
"jul_2017": 1000.0,
|
|
|
|
"aug_2017": 0.0,
|
|
|
|
"sep_2017": 0.0,
|
|
|
|
"oct_2017": 0.0,
|
|
|
|
"nov_2017": 0.0,
|
|
|
|
"dec_2017": 0.0,
|
|
|
|
"jan_2018": 0.0,
|
|
|
|
"feb_2018": 0.0,
|
|
|
|
"mar_2018": 0.0,
|
2018-11-23 10:45:32 +00:00
|
|
|
"total": 3000.0
|
2018-11-12 10:45:54 +00:00
|
|
|
}
|
|
|
|
]
|
2018-11-23 10:45:32 +00:00
|
|
|
result = sorted(report[1], key=lambda k: k['entity'])
|
|
|
|
self.assertEqual(expected_data, result)
|
2018-11-12 10:45:54 +00:00
|
|
|
|
2018-11-23 10:45:32 +00:00
|
|
|
def compare_result_for_customer_group(self):
|
2018-11-12 10:45:54 +00:00
|
|
|
filters = {
|
|
|
|
'doc_type': 'Sales Order',
|
|
|
|
'range': 'Monthly',
|
|
|
|
'to_date': '2018-03-31',
|
|
|
|
'tree_type': 'Customer Group',
|
2018-11-15 10:54:34 +00:00
|
|
|
'company': '_Test Company 2',
|
2018-11-12 10:45:54 +00:00
|
|
|
'from_date': '2017-04-01',
|
|
|
|
'value_quantity': 'Value'
|
|
|
|
}
|
|
|
|
|
|
|
|
report = execute(filters)
|
|
|
|
|
2018-11-23 10:45:32 +00:00
|
|
|
expected_first_row = {
|
|
|
|
"entity": "All Customer Groups",
|
|
|
|
"indent": 0,
|
2018-11-29 08:58:01 +00:00
|
|
|
"apr_2017": 0.0,
|
|
|
|
"may_2017": 0.0,
|
|
|
|
"jun_2017": 2000.0,
|
|
|
|
"jul_2017": 1000.0,
|
|
|
|
"aug_2017": 0.0,
|
|
|
|
"sep_2017": 1500.0,
|
|
|
|
"oct_2017": 1000.0,
|
|
|
|
"nov_2017": 0.0,
|
|
|
|
"dec_2017": 0.0,
|
|
|
|
"jan_2018": 0.0,
|
|
|
|
"feb_2018": 2000.0,
|
|
|
|
"mar_2018": 0.0,
|
2018-11-23 10:45:32 +00:00
|
|
|
"total":7500.0
|
|
|
|
}
|
|
|
|
self.assertEqual(expected_first_row, report[1][0])
|
2018-11-12 10:45:54 +00:00
|
|
|
|
2018-11-23 10:45:32 +00:00
|
|
|
def compare_result_for_customer_based_on_quantity(self):
|
2018-11-12 10:45:54 +00:00
|
|
|
filters = {
|
|
|
|
'doc_type': 'Sales Order',
|
|
|
|
'range': 'Monthly',
|
|
|
|
'to_date': '2018-03-31',
|
|
|
|
'tree_type': 'Customer',
|
2018-11-15 10:54:34 +00:00
|
|
|
'company': '_Test Company 2',
|
2018-11-12 10:45:54 +00:00
|
|
|
'from_date': '2017-04-01',
|
|
|
|
'value_quantity': 'Quantity'
|
|
|
|
}
|
|
|
|
|
|
|
|
report = execute(filters)
|
|
|
|
|
|
|
|
expected_data = [
|
|
|
|
{
|
|
|
|
"entity": "_Test Customer 1",
|
|
|
|
"entity_name": "_Test Customer 1",
|
2018-11-29 08:58:01 +00:00
|
|
|
"apr_2017": 0.0,
|
|
|
|
"may_2017": 0.0,
|
|
|
|
"jun_2017": 0.0,
|
|
|
|
"jul_2017": 0.0,
|
|
|
|
"aug_2017": 0.0,
|
|
|
|
"sep_2017": 0.0,
|
|
|
|
"oct_2017": 0.0,
|
|
|
|
"nov_2017": 0.0,
|
|
|
|
"dec_2017": 0.0,
|
|
|
|
"jan_2018": 0.0,
|
|
|
|
"feb_2018": 20.0,
|
|
|
|
"mar_2018": 0.0,
|
2018-11-12 10:45:54 +00:00
|
|
|
"total":20.0
|
|
|
|
},
|
|
|
|
{
|
2018-11-23 10:45:32 +00:00
|
|
|
"entity": "_Test Customer 2",
|
|
|
|
"entity_name": "_Test Customer 2",
|
2018-11-29 08:58:01 +00:00
|
|
|
"apr_2017": 0.0,
|
|
|
|
"may_2017": 0.0,
|
|
|
|
"jun_2017": 0.0,
|
|
|
|
"jul_2017": 0.0,
|
|
|
|
"aug_2017": 0.0,
|
|
|
|
"sep_2017": 15.0,
|
|
|
|
"oct_2017": 10.0,
|
|
|
|
"nov_2017": 0.0,
|
|
|
|
"dec_2017": 0.0,
|
|
|
|
"jan_2018": 0.0,
|
|
|
|
"feb_2018": 0.0,
|
|
|
|
"mar_2018": 0.0,
|
2018-11-23 10:45:32 +00:00
|
|
|
"total":25.0
|
2018-11-12 10:45:54 +00:00
|
|
|
},
|
|
|
|
{
|
2018-11-23 10:45:32 +00:00
|
|
|
"entity": "_Test Customer 3",
|
|
|
|
"entity_name": "_Test Customer 3",
|
2018-11-29 08:58:01 +00:00
|
|
|
"apr_2017": 0.0,
|
|
|
|
"may_2017": 0.0,
|
|
|
|
"jun_2017": 20.0,
|
|
|
|
"jul_2017": 10.0,
|
|
|
|
"aug_2017": 0.0,
|
|
|
|
"sep_2017": 0.0,
|
|
|
|
"oct_2017": 0.0,
|
|
|
|
"nov_2017": 0.0,
|
|
|
|
"dec_2017": 0.0,
|
|
|
|
"jan_2018": 0.0,
|
|
|
|
"feb_2018": 0.0,
|
|
|
|
"mar_2018": 0.0,
|
2018-11-23 10:45:32 +00:00
|
|
|
"total": 30.0
|
2018-11-12 10:45:54 +00:00
|
|
|
}
|
|
|
|
]
|
2018-11-23 10:45:32 +00:00
|
|
|
result = sorted(report[1], key=lambda k: k['entity'])
|
|
|
|
self.assertEqual(expected_data, result)
|
2018-11-12 10:45:54 +00:00
|
|
|
|
2018-11-23 10:45:32 +00:00
|
|
|
def create_sales_orders():
|
2018-11-12 10:45:54 +00:00
|
|
|
frappe.set_user("Administrator")
|
|
|
|
|
2018-11-15 10:54:34 +00:00
|
|
|
make_sales_order(company="_Test Company 2", qty=10,
|
|
|
|
customer = "_Test Customer 1",
|
|
|
|
transaction_date = '2018-02-10',
|
|
|
|
warehouse = 'Finished Goods - _TC2',
|
|
|
|
currency = 'EUR')
|
|
|
|
|
|
|
|
make_sales_order(company="_Test Company 2",
|
|
|
|
qty=10, customer = "_Test Customer 1",
|
|
|
|
transaction_date = '2018-02-15',
|
|
|
|
warehouse = 'Finished Goods - _TC2',
|
|
|
|
currency = 'EUR')
|
|
|
|
|
|
|
|
make_sales_order(company = "_Test Company 2",
|
|
|
|
qty=10, customer = "_Test Customer 2",
|
|
|
|
transaction_date = '2017-10-10',
|
|
|
|
warehouse='Finished Goods - _TC2',
|
|
|
|
currency = 'EUR')
|
|
|
|
|
|
|
|
make_sales_order(company="_Test Company 2",
|
|
|
|
qty=15, customer = "_Test Customer 2",
|
|
|
|
transaction_date='2017-09-23',
|
|
|
|
warehouse='Finished Goods - _TC2',
|
|
|
|
currency = 'EUR')
|
2018-11-23 10:45:32 +00:00
|
|
|
|
2018-11-15 10:54:34 +00:00
|
|
|
make_sales_order(company="_Test Company 2",
|
|
|
|
qty=20, customer = "_Test Customer 3",
|
|
|
|
transaction_date='2017-06-15',
|
|
|
|
warehouse='Finished Goods - _TC2',
|
|
|
|
currency = 'EUR')
|
2018-11-23 10:45:32 +00:00
|
|
|
|
2018-11-15 10:54:34 +00:00
|
|
|
make_sales_order(company="_Test Company 2",
|
|
|
|
qty=10, customer = "_Test Customer 3",
|
|
|
|
transaction_date='2017-07-10',
|
|
|
|
warehouse='Finished Goods - _TC2',
|
|
|
|
currency = 'EUR')
|