2016-10-17 11:32:12 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
import frappe
|
|
|
|
from frappe import _
|
2021-05-31 03:41:42 +00:00
|
|
|
from frappe.utils import flt
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
from erpnext.accounts.report.financial_statements import get_period_list
|
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
def execute(filters=None):
|
2021-05-31 03:41:42 +00:00
|
|
|
filters = frappe._dict(filters or {})
|
|
|
|
|
|
|
|
columns = get_columns()
|
|
|
|
data = get_vehicle_log_data(filters)
|
|
|
|
chart = get_chart_data(data, filters)
|
|
|
|
|
2016-12-30 10:51:35 +00:00
|
|
|
return columns, data, None, chart
|
2017-04-10 06:39:18 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
def get_columns():
|
2021-05-31 03:41:42 +00:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
'fieldname': 'vehicle',
|
|
|
|
'fieldtype': 'Link',
|
|
|
|
'label': _('Vehicle'),
|
|
|
|
'options': 'Vehicle',
|
|
|
|
'width': 150
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'make',
|
|
|
|
'fieldtype': 'Data',
|
|
|
|
'label': _('Make'),
|
|
|
|
'width': 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'model',
|
|
|
|
'fieldtype': 'Data',
|
|
|
|
'label': _('Model'),
|
|
|
|
'width': 80
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'location',
|
|
|
|
'fieldtype': 'Data',
|
|
|
|
'label': _('Location'),
|
|
|
|
'width': 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'log_name',
|
|
|
|
'fieldtype': 'Link',
|
|
|
|
'label': _('Vehicle Log'),
|
|
|
|
'options': 'Vehicle Log',
|
|
|
|
'width': 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'odometer',
|
|
|
|
'fieldtype': 'Int',
|
|
|
|
'label': _('Odometer Value'),
|
|
|
|
'width': 120
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'date',
|
|
|
|
'fieldtype': 'Date',
|
|
|
|
'label': _('Date'),
|
|
|
|
'width': 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'fuel_qty',
|
|
|
|
'fieldtype': 'Float',
|
|
|
|
'label': _('Fuel Qty'),
|
|
|
|
'width': 80
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'fuel_price',
|
|
|
|
'fieldtype': 'Float',
|
|
|
|
'label': _('Fuel Price'),
|
|
|
|
'width': 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'fuel_expense',
|
|
|
|
'fieldtype': 'Currency',
|
|
|
|
'label': _('Fuel Expense'),
|
|
|
|
'width': 150
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'service_expense',
|
|
|
|
'fieldtype': 'Currency',
|
|
|
|
'label': _('Service Expense'),
|
|
|
|
'width': 150
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'fieldname': 'employee',
|
|
|
|
'fieldtype': 'Link',
|
|
|
|
'label': _('Employee'),
|
|
|
|
'options': 'Employee',
|
|
|
|
'width': 150
|
|
|
|
}
|
2016-10-17 11:32:12 +00:00
|
|
|
]
|
2021-05-31 03:41:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_vehicle_log_data(filters):
|
|
|
|
start_date, end_date = get_period_dates(filters)
|
|
|
|
conditions, values = get_conditions(filters)
|
|
|
|
|
|
|
|
data = frappe.db.sql("""
|
|
|
|
SELECT
|
|
|
|
vhcl.license_plate as vehicle, vhcl.make, vhcl.model,
|
|
|
|
vhcl.location, log.name as log_name, log.odometer,
|
|
|
|
log.date, log.employee, log.fuel_qty,
|
|
|
|
log.price as fuel_price,
|
|
|
|
log.fuel_qty * log.price as fuel_expense
|
|
|
|
FROM
|
2017-04-10 06:39:18 +00:00
|
|
|
`tabVehicle` vhcl,`tabVehicle Log` log
|
2021-05-31 03:41:42 +00:00
|
|
|
WHERE
|
|
|
|
vhcl.license_plate = log.license_plate
|
|
|
|
and log.docstatus = 1
|
|
|
|
and date between %(start_date)s and %(end_date)s
|
|
|
|
{0}
|
|
|
|
ORDER BY date""".format(conditions), values, as_dict=1)
|
|
|
|
|
|
|
|
for row in data:
|
|
|
|
row['service_expense'] = get_service_expense(row.log_name)
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def get_conditions(filters):
|
|
|
|
conditions = ''
|
|
|
|
|
|
|
|
start_date, end_date = get_period_dates(filters)
|
|
|
|
values = {
|
|
|
|
'start_date': start_date,
|
|
|
|
'end_date': end_date
|
|
|
|
}
|
|
|
|
|
|
|
|
if filters.employee:
|
|
|
|
conditions += ' and log.employee = %(employee)s'
|
|
|
|
values['employee'] = filters.employee
|
|
|
|
|
|
|
|
if filters.vehicle:
|
|
|
|
conditions += ' and vhcl.license_plate = %(vehicle)s'
|
|
|
|
values['vehicle'] = filters.vehicle
|
|
|
|
|
|
|
|
return conditions, values
|
|
|
|
|
|
|
|
|
|
|
|
def get_period_dates(filters):
|
|
|
|
if filters.filter_based_on == 'Fiscal Year' and filters.fiscal_year:
|
|
|
|
fy = frappe.db.get_value('Fiscal Year', filters.fiscal_year,
|
|
|
|
['year_start_date', 'year_end_date'], as_dict=True)
|
|
|
|
return fy.year_start_date, fy.year_end_date
|
|
|
|
else:
|
|
|
|
return filters.from_date, filters.to_date
|
|
|
|
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
def get_service_expense(logname):
|
2021-05-31 03:41:42 +00:00
|
|
|
expense_amount = frappe.db.sql("""
|
|
|
|
SELECT sum(expense_amount)
|
|
|
|
FROM
|
|
|
|
`tabVehicle Log` log, `tabVehicle Service` service
|
|
|
|
WHERE
|
|
|
|
service.parent=log.name and log.name=%s
|
|
|
|
""", logname)
|
|
|
|
|
|
|
|
return flt(expense_amount[0][0]) if expense_amount else 0.0
|
|
|
|
|
|
|
|
|
|
|
|
def get_chart_data(data, filters):
|
|
|
|
period_list = get_period_list(filters.fiscal_year, filters.fiscal_year,
|
|
|
|
filters.from_date, filters.to_date, filters.filter_based_on, 'Monthly')
|
|
|
|
|
|
|
|
fuel_data, service_data = [], []
|
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
for period in period_list:
|
2021-05-31 03:41:42 +00:00
|
|
|
total_fuel_exp = 0
|
|
|
|
total_service_exp = 0
|
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
for row in data:
|
2021-05-31 03:41:42 +00:00
|
|
|
if row.date <= period.to_date and row.date >= period.from_date:
|
|
|
|
total_fuel_exp += flt(row.fuel_expense)
|
|
|
|
total_service_exp += flt(row.service_expense)
|
|
|
|
|
|
|
|
fuel_data.append([period.key, total_fuel_exp])
|
|
|
|
service_data.append([period.key, total_service_exp])
|
|
|
|
|
|
|
|
labels = [period.label for period in period_list]
|
|
|
|
fuel_exp_data= [row[1] for row in fuel_data]
|
|
|
|
service_exp_data= [row[1] for row in service_data]
|
|
|
|
|
2017-10-17 06:33:02 +00:00
|
|
|
datasets = []
|
2016-10-17 11:32:12 +00:00
|
|
|
if fuel_exp_data:
|
2017-10-17 06:33:02 +00:00
|
|
|
datasets.append({
|
2021-05-31 03:41:42 +00:00
|
|
|
'name': _('Fuel Expenses'),
|
2017-10-17 06:33:02 +00:00
|
|
|
'values': fuel_exp_data
|
|
|
|
})
|
2021-05-31 03:41:42 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
if service_exp_data:
|
2017-10-17 06:33:02 +00:00
|
|
|
datasets.append({
|
2021-05-31 03:41:42 +00:00
|
|
|
'name': _('Service Expenses'),
|
2017-10-17 06:33:02 +00:00
|
|
|
'values': service_exp_data
|
|
|
|
})
|
2021-05-31 03:41:42 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
chart = {
|
2021-05-31 03:41:42 +00:00
|
|
|
'data': {
|
2017-10-17 06:33:02 +00:00
|
|
|
'labels': labels,
|
|
|
|
'datasets': datasets
|
2021-05-31 03:41:42 +00:00
|
|
|
},
|
|
|
|
'type': 'line',
|
|
|
|
'fieldtype': 'Currency'
|
2016-10-17 11:32:12 +00:00
|
|
|
}
|
2021-05-31 03:41:42 +00:00
|
|
|
|
2019-08-23 07:20:52 +00:00
|
|
|
return chart
|