From 3b54b1e97588127d0a673f0a1cc769b78867c597 Mon Sep 17 00:00:00 2001 From: pateljannat Date: Mon, 19 Apr 2021 16:49:28 +0530 Subject: [PATCH] fix: report name and columns --- .../hr/doctype/hr_settings/hr_settings.json | 8 +- .../report/profitability/profitability.py | 162 ---------------- .../profitability/test_profitability.py | 51 ----- .../__init__.py | 0 .../project_profitability.js} | 8 +- .../project_profitability.json} | 25 ++- .../project_profitability.py | 174 ++++++++++++++++++ .../test_project_profitability.py | 51 +++++ .../projects/workspace/projects/projects.json | 6 +- erpnext/regional/india/utils.py | 2 +- 10 files changed, 255 insertions(+), 232 deletions(-) delete mode 100644 erpnext/projects/report/profitability/profitability.py delete mode 100644 erpnext/projects/report/profitability/test_profitability.py rename erpnext/projects/report/{profitability => project_profitability}/__init__.py (100%) rename erpnext/projects/report/{profitability/profitability.js => project_profitability/project_profitability.js} (84%) rename erpnext/projects/report/{profitability/profitability.json => project_profitability/project_profitability.json} (69%) create mode 100644 erpnext/projects/report/project_profitability/project_profitability.py create mode 100644 erpnext/projects/report/project_profitability/test_project_profitability.py diff --git a/erpnext/hr/doctype/hr_settings/hr_settings.json b/erpnext/hr/doctype/hr_settings/hr_settings.json index 4fa50c4852..35532291a5 100644 --- a/erpnext/hr/doctype/hr_settings/hr_settings.json +++ b/erpnext/hr/doctype/hr_settings/hr_settings.json @@ -10,7 +10,7 @@ "retirement_age", "emp_created_by", "column_break_4", - "default_working_hours", + "standard_working_hours", "stop_birthday_reminders", "expense_approver_mandatory_in_expense_claim", "leave_settings", @@ -147,16 +147,16 @@ }, { "default": "8", - "fieldname": "default_working_hours", + "fieldname": "standard_working_hours", "fieldtype": "Int", - "label": "Default Working Hours" + "label": "Standard Working Hours" } ], "icon": "fa fa-cog", "idx": 1, "issingle": 1, "links": [], - "modified": "2021-03-25 13:18:21.648077", + "modified": "2021-04-16 15:45:18.467699", "modified_by": "Administrator", "module": "HR", "name": "HR Settings", diff --git a/erpnext/projects/report/profitability/profitability.py b/erpnext/projects/report/profitability/profitability.py deleted file mode 100644 index 8ce2eb09ee..0000000000 --- a/erpnext/projects/report/profitability/profitability.py +++ /dev/null @@ -1,162 +0,0 @@ -# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors -# For license information, please see license.txt - -from __future__ import unicode_literals -import frappe -from frappe import _ - -def execute(filters=None): - columns, data = [], [] - data = get_data(filters) - columns = get_columns() - charts = get_chart_data(data) - return columns, data, None, charts - -def get_data(filters): - conditions = get_conditions(filters) - default_working_hours = frappe.db.get_single_value("HR Settings", "default_working_hours") - sql = """ - select - *, - t.gross_pay * t.utilization as fractional_cost, - t.grand_total - t.gross_pay * t.utilization as profit - from - (select - si.customer_name,tabTimesheet.title,tabTimesheet.employee,si.grand_total,si.name as voucher_no, - ss.gross_pay,ss.total_working_days,tabTimesheet.end_date,tabTimesheet.total_billed_hours,tabTimesheet.name as timesheet, - tabTimesheet.total_billed_hours/(ss.total_working_days * {0}) as utilization - from - `tabSalary Slip Timesheet` as sst join `tabTimesheet` on tabTimesheet.name = sst.time_sheet - join `tabSales Invoice Timesheet` as sit on sit.time_sheet = tabTimesheet.name - join `tabSales Invoice` as si on si.name = sit.parent and si.status != "Cancelled" - join `tabSalary Slip` as ss on ss.name = sst.parent and ss.status != "Cancelled" """.format(default_working_hours) - if conditions: - sql += """ - where - {0}) as t""".format(conditions) - data = frappe.db.sql(sql,filters, as_dict=True) - return data - -def get_conditions(filters): - conditions = [] - if filters.get("company"): - conditions.append('tabTimesheet.company="{0}"'.format(filters.get("company"))) - if filters.get("customer_name"): - conditions.append("si.customer_name='{0}'".format(filters.get("customer_name"))) - if filters.get("start_date"): - conditions.append("tabTimesheet.start_date>='{0}'".format(filters.get("start_date"))) - if filters.get("end_date"): - conditions.append("tabTimesheet.end_date<='{0}'".format(filters.get("end_date"))) - if filters.get("employee"): - conditions.append("tabTimesheet.employee='{0}'".format(filters.get("employee"))) - - conditions = " and ".join(conditions) - return conditions - -def get_chart_data(data): - if not data: - return None - - labels = [] - utilization = [] - - for entry in data: - labels.append(entry.get("title") + " - " + str(entry.get("end_date"))) - utilization.append(entry.get("utilization")) - charts = { - 'data': { - 'labels': labels, - 'datasets': [ - { - 'name': 'Utilization', - 'values': utilization - } - ] - }, - 'type': 'bar', - 'colors': ['#84BDD5'] - } - return charts - -def get_columns(): - return [ - { - "fieldname": "customer_name", - "label": _("Customer"), - "fieldtype": "Link", - "options": "Customer", - "width": 150 - }, - { - "fieldname": "title", - "label": _("Name"), - "fieldtype": "Data", - "width": 120 - }, - { - "fieldname": "employee", - "label": _("Employee"), - "fieldtype": "Link", - "options": "Employee", - "width": 150 - }, - { - "fieldname": "voucher_no", - "label": _("Sales Invoice"), - "fieldtype": "Link", - "options": "Sales Invoice", - "width": 200 - }, - { - "fieldname": "timesheet", - "label": _("Timesheet"), - "fieldtype": "Link", - "options": "Timesheet", - "width": 150 - }, - { - "fieldname": "grand_total", - "label": _("Bill Amount"), - "fieldtype": "Currency", - "options": "currency", - "width": 120 - }, - { - "fieldname": "gross_pay", - "label": _("Cost"), - "fieldtype": "Currency", - "options": "currency", - "width": 120 - }, - { - "fieldname": "profit", - "label": _("Profit"), - "fieldtype": "Currency", - "options": "currency", - "width": 120 - }, - { - "fieldname": "end_date", - "label": _("End Date"), - "fieldtype": "Date", - "width": 120 - }, - { - "fieldname": "total_billed_hours", - "label": _("Total Billed Hours"), - "fieldtype": "Int", - "width": 100 - }, - { - "fieldname": "utilization", - "label": _("Utilization"), - "fieldtype": "Percentage", - "width": 120 - }, - { - "fieldname": "fractional_cost", - "label": _("Fractional Cost"), - "fieldtype": "Int", - "width": 100 - } - ] \ No newline at end of file diff --git a/erpnext/projects/report/profitability/test_profitability.py b/erpnext/projects/report/profitability/test_profitability.py deleted file mode 100644 index 64ab6787eb..0000000000 --- a/erpnext/projects/report/profitability/test_profitability.py +++ /dev/null @@ -1,51 +0,0 @@ -from __future__ import unicode_literals -import unittest -import frappe -from frappe.utils import getdate, nowdate -from erpnext.hr.doctype.employee.test_employee import make_employee -from erpnext.projects.doctype.timesheet.test_timesheet import make_salary_structure_for_timesheet, make_timesheet -from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice -from erpnext.projects.report.profitability.profitability import execute - -class TestProfitability(unittest.TestCase): - @classmethod - def setUp(self): - emp = make_employee("test_employee_9@salary.com", company="_Test Company") - if not frappe.db.exists("Salary Component", "Timesheet Component"): - frappe.get_doc({"doctype": "Salary Component", "salary_component": "Timesheet Component"}).insert() - make_salary_structure_for_timesheet(emp, company="_Test Company") - self.timesheet = make_timesheet(emp, simulate = True, billable=1) - self.salary_slip = make_salary_slip(self.timesheet.name) - self.salary_slip.submit() - self.sales_invoice = make_sales_invoice(self.timesheet.name, '_Test Item', '_Test Customer') - self.sales_invoice.due_date = nowdate() - self.sales_invoice.submit() - - def test_profitability(self): - filters = { - 'company': '_Test Company', - 'start_date': getdate(), - 'end_date': getdate() - } - - report = execute(filters) - expected_data = [ - { - "customer_name": "_Test Customer", - "title": "test_employee_9@salary.com", - "grand_total": 100.0, - "gross_pay": 78100.0, - "profit": -19425.0, - "total_billed_hours": 2.0, - "utilization": 0.25, - "fractional_cost": 19525.0, - "total_working_days": 1.0 - } - ] - for key in ["customer_name","title","grand_total","gross_pay","profit","total_billed_hours","utilization","fractional_cost","total_working_days"]: - self.assertEqual(expected_data[0].get(key), report[1][0].get(key)) - - def tearDown(self): - frappe.get_doc("Sales Invoice", self.sales_invoice.name).cancel() - frappe.get_doc("Salary Slip", self.salary_slip.name).cancel() - frappe.get_doc("Timesheet", self.timesheet.name).cancel() \ No newline at end of file diff --git a/erpnext/projects/report/profitability/__init__.py b/erpnext/projects/report/project_profitability/__init__.py similarity index 100% rename from erpnext/projects/report/profitability/__init__.py rename to erpnext/projects/report/project_profitability/__init__.py diff --git a/erpnext/projects/report/profitability/profitability.js b/erpnext/projects/report/project_profitability/project_profitability.js similarity index 84% rename from erpnext/projects/report/profitability/profitability.js rename to erpnext/projects/report/project_profitability/project_profitability.js index 6cb6e39d34..cdf7bfdc9f 100644 --- a/erpnext/projects/report/profitability/profitability.js +++ b/erpnext/projects/report/project_profitability/project_profitability.js @@ -2,7 +2,7 @@ // For license information, please see license.txt /* eslint-disable */ -frappe.query_reports["Profitability"] = { +frappe.query_reports["Project Profitability"] = { "filters": [ { "fieldname": "company", @@ -26,6 +26,12 @@ frappe.query_reports["Profitability"] = { "reqd": 1, "default": frappe.datetime.now_date() }, + { + "fieldname": "project", + "label": __("Project"), + "fieldtype": "Link", + "options": "Project" + }, { "fieldname": "customer_name", "label": __("Customer"), diff --git a/erpnext/projects/report/profitability/profitability.json b/erpnext/projects/report/project_profitability/project_profitability.json similarity index 69% rename from erpnext/projects/report/profitability/profitability.json rename to erpnext/projects/report/project_profitability/project_profitability.json index 4f91accf58..0b092cd2c0 100644 --- a/erpnext/projects/report/profitability/profitability.json +++ b/erpnext/projects/report/project_profitability/project_profitability.json @@ -1,7 +1,7 @@ { "add_total_row": 0, "columns": [], - "creation": "2021-03-18 10:19:40.124932", + "creation": "2021-04-16 15:50:28.914872", "disable_prepared_report": 0, "disabled": 0, "docstatus": 0, @@ -9,31 +9,36 @@ "filters": [], "idx": 0, "is_standard": "Yes", - "json": "{}", - "modified": "2021-03-18 10:20:15.559305", + "modified": "2021-04-16 15:50:48.490866", "modified_by": "Administrator", "module": "Projects", - "name": "Profitability", + "name": "Project Profitability", "owner": "Administrator", "prepared_report": 0, "ref_doctype": "Timesheet", - "report_name": "Profitability", + "report_name": "Project Profitability", "report_type": "Script Report", "roles": [ - { - "role": "Projects User" - }, { "role": "HR User" }, { - "role": "Manufacturing User" + "role": "Accounts User" }, { "role": "Employee" }, { - "role": "Accounts User" + "role": "Projects User" + }, + { + "role": "Manufacturing User" + }, + { + "role": "Employee Self Service" + }, + { + "role": "HR Manager" } ] } \ No newline at end of file diff --git a/erpnext/projects/report/project_profitability/project_profitability.py b/erpnext/projects/report/project_profitability/project_profitability.py new file mode 100644 index 0000000000..7a76213994 --- /dev/null +++ b/erpnext/projects/report/project_profitability/project_profitability.py @@ -0,0 +1,174 @@ +# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals +# import frappe + +def execute(filters=None): + columns, data = [], [] + data = get_data(filters) + columns = get_columns() + charts = get_chart_data(data) + return columns, data, None, charts + +def get_data(filters): + conditions = get_conditions(filters) + standard_working_hours = frappe.db.get_single_value('HR Settings', 'standard_working_hours') + sql = ''' + SELECT + * + FROM + (SELECT + si.customer_name,tabTimesheet.title, + tabTimesheet.employee,si.base_grand_total + si.name as voucher_no,ss.base_gross_pay,ss.total_working_days, + tabTimesheet.end_date,tabTimesheet.total_billed_hours, + tabTimesheet.name as timesheet, + tabTimesheet.total_billed_hours/(ss.total_working_days * {0}) as utilization + FROM + `tabSalary Slip Timesheet` as sst join `tabTimesheet` on tabTimesheet.name = sst.time_sheet + join `tabSales Invoice Timesheet` as sit on sit.time_sheet = tabTimesheet.name + join `tabSales Invoice` as si on si.name = sit.parent and si.status != 'Cancelled' + join `tabSalary Slip` as ss on ss.name = sst.parent and ss.status != 'Cancelled' '''.format(standard_working_hours) + if conditions: + sql += ''' + where + {0}) as t'''.format(conditions) + data = frappe.db.sql(sql,filters, as_dict=True) + data = perform_calculations(data) + return data + +def perform_calculations(data): + data.fractional_cost = data.base_gross_pay * data.utilization + data.profit = data.base_grand_total - data.base_gross_pay + return data + +def get_conditions(filters): + conditions = [] + if filters.get('company'): + conditions.append('tabTimesheet.company="{0}"'.format(filters.get('company'))) + if filters.get('customer_name'): + conditions.append('si.customer_name="{0}"'.format(filters.get('customer_name'))) + if filters.get('start_date'): + conditions.append('tabTimesheet.start_date>="{0}"'.format(filters.get('start_date'))) + if filters.get('end_date'): + conditions.append('tabTimesheet.end_date<="{0}"'.format(filters.get('end_date'))) + if filters.get('employee'): + conditions.append('tabTimesheet.employee="{0}"'.format(filters.get('employee'))) + + conditions = ' and '.join(conditions) + return conditions + +def get_chart_data(data): + if not data: + return None + + labels = [] + utilization = [] + + for entry in data: + labels.append(entry.get('title') + ' - ' + str(entry.get('end_date'))) + utilization.append(entry.get('utilization')) + charts = { + 'data': { + 'labels': labels, + 'datasets': [ + { + 'name': 'Utilization', + 'values': utilization + } + ] + }, + 'type': 'bar', + 'colors': ['#84BDD5'] + } + return charts + +def get_columns(): + return [ + { + 'fieldname': 'customer_name', + 'label': _('Customer'), + 'fieldtype': 'Link', + 'options': 'Customer', + 'width': 150 + }, + { + 'fieldname': 'employee', + 'label': _('Employee'), + 'fieldtype': 'Link', + 'options': 'Employee', + 'width': 150 + }, + { + 'fieldname': 'employee_name', + 'label': _('Employee Name'), + 'fieldtype': 'Data', + 'width': 120 + }, + { + 'fieldname': 'voucher_no', + 'label': _('Sales Invoice'), + 'fieldtype': 'Link', + 'options': 'Sales Invoice', + 'width': 200 + }, + { + 'fieldname': 'timesheet', + 'label': _('Timesheet'), + 'fieldtype': 'Link', + 'options': 'Timesheet', + 'width': 150 + }, + { + 'fieldname': 'grand_total', + 'label': _('Bill Amount'), + 'fieldtype': 'Currency', + 'options': 'currency', + 'width': 120 + }, + { + 'fieldname': 'gross_pay', + 'label': _('Cost'), + 'fieldtype': 'Currency', + 'options': 'currency', + 'width': 120 + }, + { + 'fieldname': 'profit', + 'label': _('Profit'), + 'fieldtype': 'Currency', + 'options': 'currency', + 'width': 120 + }, + { + 'fieldname': 'utilization', + 'label': _('Utilization'), + 'fieldtype': 'Percentage', + 'width': 120 + }, + { + 'fieldname': 'fractional_cost', + 'label': _('Fractional Cost'), + 'fieldtype': 'Int', + 'width': 100 + }, + { + 'fieldname': 'total_billed_hours', + 'label': _('Total Billed Hours'), + 'fieldtype': 'Int', + 'width': 100 + }, + { + 'fieldname': 'start_date', + 'label': _('Start Date'), + 'fieldtype': 'Date', + 'width': 120 + }, + { + 'fieldname': 'end_date', + 'label': _('End Date'), + 'fieldtype': 'Date', + 'width': 120 + } + ] diff --git a/erpnext/projects/report/project_profitability/test_project_profitability.py b/erpnext/projects/report/project_profitability/test_project_profitability.py new file mode 100644 index 0000000000..7036547e40 --- /dev/null +++ b/erpnext/projects/report/project_profitability/test_project_profitability.py @@ -0,0 +1,51 @@ +from __future__ import unicode_literals +import unittest +import frappe +from frappe.utils import getdate, nowdate +from erpnext.hr.doctype.employee.test_employee import make_employee +from erpnext.projects.doctype.timesheet.test_timesheet import make_salary_structure_for_timesheet, make_timesheet +from erpnext.projects.doctype.timesheet.timesheet import make_salary_slip, make_sales_invoice +from erpnext.projects.report.project_profitability.project_profitability import execute + +class TestProjectProfitability(unittest.TestCase): + @classmethod + def setUp(self): + emp = make_employee('test_employee_9@salary.com', company='_Test Company') + if not frappe.db.exists('Salary Component', 'Timesheet Component'): + frappe.get_doc({'doctype': 'Salary Component', 'salary_component': 'Timesheet Component'}).insert() + make_salary_structure_for_timesheet(emp, company='_Test Company') + self.timesheet = make_timesheet(emp, simulate = True, billable=1) + self.salary_slip = make_salary_slip(self.timesheet.name) + self.salary_slip.submit() + self.sales_invoice = make_sales_invoice(self.timesheet.name, '_Test Item', '_Test Customer') + self.sales_invoice.due_date = nowdate() + self.sales_invoice.submit() + + def test_project_profitability(self): + filters = { + 'company': '_Test Company', + 'start_date': getdate(), + 'end_date': getdate() + } + + report = execute(filters) + expected_data = [ + { + 'customer_name': '_Test Customer', + 'title': 'test_employee_9@salary.com', + 'grand_total': 100.0, + 'gross_pay': 78100.0, + 'profit': -19425.0, + 'total_billed_hours': 2.0, + 'utilization': 0.25, + 'fractional_cost': 19525.0, + 'total_working_days': 1.0 + } + ] + for key in ['customer_name','title','grand_total','gross_pay','profit','total_billed_hours','utilization','fractional_cost','total_working_days']: + self.assertEqual(expected_data[0].get(key), report[1][0].get(key)) + + def tearDown(self): + frappe.get_doc('Sales Invoice', self.sales_invoice.name).cancel() + frappe.get_doc('Salary Slip', self.salary_slip.name).cancel() + frappe.get_doc('Timesheet', self.timesheet.name).cancel() \ No newline at end of file diff --git a/erpnext/projects/workspace/projects/projects.json b/erpnext/projects/workspace/projects/projects.json index 8703ffb756..621c4bb52f 100644 --- a/erpnext/projects/workspace/projects/projects.json +++ b/erpnext/projects/workspace/projects/projects.json @@ -134,8 +134,8 @@ "dependencies": "Timesheet, Sales Invoice, Salary Slip", "hidden": 0, "is_query_report": 1, - "label": "Profitability", - "link_to": "Profitability", + "label": "Project Profitability", + "link_to": "Project Profitability", "link_type": "Report", "onboard": 0, "type": "Link" @@ -161,7 +161,7 @@ "type": "Link" } ], - "modified": "2021-03-25 13:25:17.609608", + "modified": "2021-04-16 16:27:16.548780", "modified_by": "Administrator", "module": "Projects", "name": "Projects", diff --git a/erpnext/regional/india/utils.py b/erpnext/regional/india/utils.py index 3637de438c..ff4ac07041 100644 --- a/erpnext/regional/india/utils.py +++ b/erpnext/regional/india/utils.py @@ -159,7 +159,7 @@ def validate_document_name(doc, method=None): # Date was chosen as start of next FY to avoid irritating current users. if country != "India" or getdate(doc.posting_date) < getdate("2021-04-01"): return - + print(doc.name) if len(doc.name) > 16: frappe.throw(_("Maximum length of document number should be 16 characters as per GST rules. Please change the naming series."))