fix: multicurrency
This commit is contained in:
parent
73896ce94c
commit
cbdcfb4873
@ -262,7 +262,6 @@ doc_events = {
|
|||||||
],
|
],
|
||||||
"on_trash": "erpnext.regional.check_deletion_permission",
|
"on_trash": "erpnext.regional.check_deletion_permission",
|
||||||
"validate": [
|
"validate": [
|
||||||
"erpnext.regional.india.utils.validate_document_name",
|
|
||||||
"erpnext.regional.india.utils.update_taxable_values"
|
"erpnext.regional.india.utils.update_taxable_values"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
@ -26,12 +26,6 @@ frappe.query_reports["Project Profitability"] = {
|
|||||||
"reqd": 1,
|
"reqd": 1,
|
||||||
"default": frappe.datetime.now_date()
|
"default": frappe.datetime.now_date()
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"fieldname": "project",
|
|
||||||
"label": __("Project"),
|
|
||||||
"fieldtype": "Link",
|
|
||||||
"options": "Project"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"fieldname": "customer_name",
|
"fieldname": "customer_name",
|
||||||
"label": __("Customer"),
|
"label": __("Customer"),
|
||||||
@ -43,6 +37,12 @@ frappe.query_reports["Project Profitability"] = {
|
|||||||
"label": __("Employee"),
|
"label": __("Employee"),
|
||||||
"fieldtype": "Link",
|
"fieldtype": "Link",
|
||||||
"options": "Employee"
|
"options": "Employee"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldname": "project",
|
||||||
|
"label": __("Project"),
|
||||||
|
"fieldtype": "Link",
|
||||||
|
"options": "Project"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
@ -2,7 +2,12 @@
|
|||||||
# For license information, please see license.txt
|
# For license information, please see license.txt
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
# import frappe
|
import frappe
|
||||||
|
from frappe import _
|
||||||
|
from erpnext.payroll.doctype.salary_structure_assignment.salary_structure_assignment import get_employee_currency
|
||||||
|
from erpnext.accounts.utils import get_currency_precision
|
||||||
|
from frappe.utils import flt
|
||||||
|
from erpnext.setup.utils import get_exchange_rate
|
||||||
|
|
||||||
def execute(filters=None):
|
def execute(filters=None):
|
||||||
columns, data = [], []
|
columns, data = [], []
|
||||||
@ -12,51 +17,96 @@ def execute(filters=None):
|
|||||||
return columns, data, None, charts
|
return columns, data, None, charts
|
||||||
|
|
||||||
def get_data(filters):
|
def get_data(filters):
|
||||||
|
data = get_rows(filters)
|
||||||
|
data = handle_multi_currency(data, filters)
|
||||||
|
data = calculate_cost_and_profit(data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def get_rows(filters):
|
||||||
conditions = get_conditions(filters)
|
conditions = get_conditions(filters)
|
||||||
standard_working_hours = frappe.db.get_single_value('HR Settings', 'standard_working_hours')
|
standard_working_hours = frappe.db.get_single_value("HR Settings", "standard_working_hours")
|
||||||
sql = '''
|
sql = """
|
||||||
SELECT
|
SELECT
|
||||||
*
|
*
|
||||||
FROM
|
FROM
|
||||||
(SELECT
|
(SELECT
|
||||||
si.customer_name,tabTimesheet.title,
|
si.customer_name,si.base_grand_total,
|
||||||
tabTimesheet.employee,si.base_grand_total
|
si.name as voucher_no,tabTimesheet.employee,
|
||||||
si.name as voucher_no,ss.base_gross_pay,ss.total_working_days,
|
tabTimesheet.title as employee_name,tabTimesheet.parent_project as project,
|
||||||
tabTimesheet.end_date,tabTimesheet.total_billed_hours,
|
tabTimesheet.start_date,tabTimesheet.end_date,
|
||||||
tabTimesheet.name as timesheet,
|
tabTimesheet.total_billed_hours,tabTimesheet.name as timesheet,
|
||||||
|
ss.base_gross_pay,ss.total_working_days,
|
||||||
tabTimesheet.total_billed_hours/(ss.total_working_days * {0}) as utilization
|
tabTimesheet.total_billed_hours/(ss.total_working_days * {0}) as utilization
|
||||||
FROM
|
FROM
|
||||||
`tabSalary Slip Timesheet` as sst join `tabTimesheet` on tabTimesheet.name = sst.time_sheet
|
`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 Timesheet` as sit on sit.time_sheet = tabTimesheet.name
|
||||||
join `tabSales Invoice` as si on si.name = sit.parent and si.status != 'Cancelled'
|
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)
|
join `tabSalary Slip` as ss on ss.name = sst.parent and ss.status != "Cancelled" """.format(standard_working_hours)
|
||||||
if conditions:
|
if conditions:
|
||||||
sql += '''
|
sql += """
|
||||||
where
|
WHERE
|
||||||
{0}) as t'''.format(conditions)
|
{0}) as t""".format(conditions)
|
||||||
data = frappe.db.sql(sql,filters, as_dict=True)
|
return frappe.db.sql(sql,filters, as_dict=True)
|
||||||
data = perform_calculations(data)
|
|
||||||
|
def handle_multi_currency(data, filters):
|
||||||
|
currency_precision = get_currency_precision() or 2
|
||||||
|
company_currency = frappe.get_cached_value("Company", filters.get("company"), "default_currency")
|
||||||
|
|
||||||
|
for row in data:
|
||||||
|
row.currency = company_currency
|
||||||
|
|
||||||
|
if filters.get("employee"):
|
||||||
|
party_currency = get_employee_currency(row.employee)
|
||||||
|
|
||||||
|
if filters.get("customer_name"):
|
||||||
|
party_currency = frappe.db.get_value("Customer", row.customer_name, ["default_currency"])
|
||||||
|
|
||||||
|
if party_currency and party_currency != company_currency:
|
||||||
|
exchange_rate = get_exchange_rate(company_currency, party_currency)
|
||||||
|
row.currency = party_currency
|
||||||
|
|
||||||
|
row.base_grand_total = flt(flt(row.base_grand_total) *
|
||||||
|
flt(exchange_rate), currency_precision)
|
||||||
|
|
||||||
|
row.base_gross_pay = flt(flt(row.base_gross_pay) *
|
||||||
|
flt(exchange_rate), currency_precision)
|
||||||
|
|
||||||
|
row.profit = flt(flt(row.profit) *
|
||||||
|
flt(exchange_rate), currency_precision)
|
||||||
|
|
||||||
|
row.fractional_cost = flt(flt(row.fractional_cost) *
|
||||||
|
flt(exchange_rate), currency_precision)
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def perform_calculations(data):
|
def calculate_cost_and_profit(data):
|
||||||
data.fractional_cost = data.base_gross_pay * data.utilization
|
for row in data:
|
||||||
data.profit = data.base_grand_total - data.base_gross_pay
|
row.fractional_cost = row.base_gross_pay * row.utilization
|
||||||
|
row.profit = row.base_grand_total - row.base_gross_pay * row.utilization
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def get_conditions(filters):
|
def get_conditions(filters):
|
||||||
conditions = []
|
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)
|
if filters.get("company"):
|
||||||
|
conditions.append("tabTimesheet.company='{0}'".format(filters.get("company")))
|
||||||
|
|
||||||
|
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("customer_name"):
|
||||||
|
conditions.append("si.customer_name='{0}'".format(filters.get("customer_name")))
|
||||||
|
|
||||||
|
if filters.get("employee"):
|
||||||
|
conditions.append("tabTimesheet.employee='{0}'".format(filters.get("employee")))
|
||||||
|
|
||||||
|
if filters.get("project"):
|
||||||
|
conditions.append("tabTimesheet.parent_project='{0}'".format(filters.get("project")))
|
||||||
|
|
||||||
|
conditions = " and ".join(conditions)
|
||||||
return conditions
|
return conditions
|
||||||
|
|
||||||
def get_chart_data(data):
|
def get_chart_data(data):
|
||||||
@ -67,108 +117,123 @@ def get_chart_data(data):
|
|||||||
utilization = []
|
utilization = []
|
||||||
|
|
||||||
for entry in data:
|
for entry in data:
|
||||||
labels.append(entry.get('title') + ' - ' + str(entry.get('end_date')))
|
labels.append(entry.get("employee_name") + " - " + str(entry.get("end_date")))
|
||||||
utilization.append(entry.get('utilization'))
|
utilization.append(entry.get("utilization"))
|
||||||
|
|
||||||
charts = {
|
charts = {
|
||||||
'data': {
|
"data": {
|
||||||
'labels': labels,
|
"labels": labels,
|
||||||
'datasets': [
|
"datasets": [
|
||||||
{
|
{
|
||||||
'name': 'Utilization',
|
"name": "Utilization",
|
||||||
'values': utilization
|
"values": utilization
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
'type': 'bar',
|
"type": "bar",
|
||||||
'colors': ['#84BDD5']
|
"colors": ["#84BDD5"]
|
||||||
}
|
}
|
||||||
return charts
|
return charts
|
||||||
|
|
||||||
def get_columns():
|
def get_columns():
|
||||||
return [
|
return [
|
||||||
{
|
{
|
||||||
'fieldname': 'customer_name',
|
"fieldname": "customer_name",
|
||||||
'label': _('Customer'),
|
"label": _("Customer"),
|
||||||
'fieldtype': 'Link',
|
"fieldtype": "Link",
|
||||||
'options': 'Customer',
|
"options": "Customer",
|
||||||
'width': 150
|
"width": 150
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'employee',
|
"fieldname": "employee",
|
||||||
'label': _('Employee'),
|
"label": _("Employee"),
|
||||||
'fieldtype': 'Link',
|
"fieldtype": "Link",
|
||||||
'options': 'Employee',
|
"options": "Employee",
|
||||||
'width': 150
|
"width": 130
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'employee_name',
|
"fieldname": "employee_name",
|
||||||
'label': _('Employee Name'),
|
"label": _("Employee Name"),
|
||||||
'fieldtype': 'Data',
|
"fieldtype": "Data",
|
||||||
'width': 120
|
"width": 120
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'voucher_no',
|
"fieldname": "voucher_no",
|
||||||
'label': _('Sales Invoice'),
|
"label": _("Sales Invoice"),
|
||||||
'fieldtype': 'Link',
|
"fieldtype": "Link",
|
||||||
'options': 'Sales Invoice',
|
"options": "Sales Invoice",
|
||||||
'width': 200
|
"width": 180
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'timesheet',
|
"fieldname": "timesheet",
|
||||||
'label': _('Timesheet'),
|
"label": _("Timesheet"),
|
||||||
'fieldtype': 'Link',
|
"fieldtype": "Link",
|
||||||
'options': 'Timesheet',
|
"options": "Timesheet",
|
||||||
'width': 150
|
"width": 130
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'grand_total',
|
"fieldname": "project",
|
||||||
'label': _('Bill Amount'),
|
"label": _("Project"),
|
||||||
'fieldtype': 'Currency',
|
"fieldtype": "Link",
|
||||||
'options': 'currency',
|
"options": "Project",
|
||||||
'width': 120
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'gross_pay',
|
"fieldname": "base_grand_total",
|
||||||
'label': _('Cost'),
|
"label": _("Bill Amount"),
|
||||||
'fieldtype': 'Currency',
|
"fieldtype": "Currency",
|
||||||
'options': 'currency',
|
"options": "currency",
|
||||||
'width': 120
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'profit',
|
"fieldname": "base_gross_pay",
|
||||||
'label': _('Profit'),
|
"label": _("Cost"),
|
||||||
'fieldtype': 'Currency',
|
"fieldtype": "Currency",
|
||||||
'options': 'currency',
|
"options": "currency",
|
||||||
'width': 120
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'utilization',
|
"fieldname": "profit",
|
||||||
'label': _('Utilization'),
|
"label": _("Profit"),
|
||||||
'fieldtype': 'Percentage',
|
"fieldtype": "Currency",
|
||||||
'width': 120
|
"options": "currency",
|
||||||
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'fractional_cost',
|
"fieldname": "utilization",
|
||||||
'label': _('Fractional Cost'),
|
"label": _("Utilization"),
|
||||||
'fieldtype': 'Int',
|
"fieldtype": "Percentage",
|
||||||
'width': 100
|
"width": 120
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'total_billed_hours',
|
"fieldname": "fractional_cost",
|
||||||
'label': _('Total Billed Hours'),
|
"label": _("Fractional Cost"),
|
||||||
'fieldtype': 'Int',
|
"fieldtype": "Int",
|
||||||
'width': 100
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'start_date',
|
"fieldname": "total_billed_hours",
|
||||||
'label': _('Start Date'),
|
"label": _("Total Billed Hours"),
|
||||||
'fieldtype': 'Date',
|
"fieldtype": "Int",
|
||||||
'width': 120
|
"width": 100
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'fieldname': 'end_date',
|
"fieldname": "start_date",
|
||||||
'label': _('End Date'),
|
"label": _("Start Date"),
|
||||||
'fieldtype': 'Date',
|
"fieldtype": "Date",
|
||||||
'width': 120
|
"width": 120
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"fieldname": "end_date",
|
||||||
|
"label": _("End Date"),
|
||||||
|
"fieldtype": "Date",
|
||||||
|
"width": 120
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": _("Currency"),
|
||||||
|
"fieldname": "currency",
|
||||||
|
"fieldtype": "Link",
|
||||||
|
"options": "Currency",
|
||||||
|
"width": 100
|
||||||
}
|
}
|
||||||
]
|
]
|
@ -32,9 +32,9 @@ class TestProjectProfitability(unittest.TestCase):
|
|||||||
expected_data = [
|
expected_data = [
|
||||||
{
|
{
|
||||||
'customer_name': '_Test Customer',
|
'customer_name': '_Test Customer',
|
||||||
'title': 'test_employee_9@salary.com',
|
'employee_name': 'test_employee_9@salary.com',
|
||||||
'grand_total': 100.0,
|
'base_grand_total': 100.0,
|
||||||
'gross_pay': 78100.0,
|
'base_gross_pay': 78100.0,
|
||||||
'profit': -19425.0,
|
'profit': -19425.0,
|
||||||
'total_billed_hours': 2.0,
|
'total_billed_hours': 2.0,
|
||||||
'utilization': 0.25,
|
'utilization': 0.25,
|
||||||
@ -42,10 +42,10 @@ class TestProjectProfitability(unittest.TestCase):
|
|||||||
'total_working_days': 1.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']:
|
for key in ['customer_name','employee_name','base_grand_total','base_gross_pay','profit','total_billed_hours','utilization','fractional_cost','total_working_days']:
|
||||||
self.assertEqual(expected_data[0].get(key), report[1][0].get(key))
|
self.assertEqual(expected_data[0].get(key), report[1][0].get(key))
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
frappe.get_doc('Sales Invoice', self.sales_invoice.name).cancel()
|
frappe.get_doc("Sales Invoice", self.sales_invoice.name).cancel()
|
||||||
frappe.get_doc('Salary Slip', self.salary_slip.name).cancel()
|
frappe.get_doc("Salary Slip", self.salary_slip.name).cancel()
|
||||||
frappe.get_doc('Timesheet', self.timesheet.name).cancel()
|
frappe.get_doc("Timesheet", self.timesheet.name).cancel()
|
Loading…
x
Reference in New Issue
Block a user