2016-10-17 11:32:12 +00:00
|
|
|
# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors
|
|
|
|
# For license information, please see license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import frappe
|
2017-04-10 06:39:18 +00:00
|
|
|
import erpnext
|
2016-10-17 11:32:12 +00:00
|
|
|
from frappe import _
|
|
|
|
from frappe.utils import flt,cstr
|
|
|
|
from erpnext.accounts.report.financial_statements import get_period_list
|
|
|
|
|
|
|
|
def execute(filters=None):
|
|
|
|
columns, data = [], []
|
2017-04-10 06:39:18 +00:00
|
|
|
if filters.get('fiscal_year'):
|
|
|
|
company = erpnext.get_default_company()
|
|
|
|
period_list = get_period_list(filters.get('fiscal_year'), filters.get('fiscal_year'),"Monthly", company)
|
|
|
|
columns=get_columns()
|
|
|
|
data=get_log_data(filters)
|
|
|
|
chart=get_chart_data(data,period_list)
|
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():
|
|
|
|
columns = [_("License") + ":Link/Vehicle:100", _("Make") + ":data:50",
|
2017-04-10 06:39:18 +00:00
|
|
|
_("Model") + ":data:50", _("Location") + ":data:100",
|
|
|
|
_("Log") + ":Link/Vehicle Log:100", _("Odometer") + ":Int:80",
|
|
|
|
_("Date") + ":Date:100", _("Fuel Qty") + ":Float:80",
|
|
|
|
_("Fuel Price") + ":Float:100",_("Service Expense") + ":Float:100"
|
2016-10-17 11:32:12 +00:00
|
|
|
]
|
|
|
|
return columns
|
|
|
|
|
|
|
|
def get_log_data(filters):
|
2017-04-10 06:39:18 +00:00
|
|
|
fy = frappe.db.get_value('Fiscal Year', filters.get('fiscal_year'), ['year_start_date', 'year_end_date'], as_dict=True)
|
2017-10-17 06:33:02 +00:00
|
|
|
data = frappe.db.sql("""select
|
2017-04-10 06:39:18 +00:00
|
|
|
vhcl.license_plate as "License", vhcl.make as "Make", vhcl.model as "Model",
|
2017-10-17 06:33:02 +00:00
|
|
|
vhcl.location as "Location", log.name as "Log", log.odometer as "Odometer",
|
2017-04-10 06:39:18 +00:00
|
|
|
log.date as "Date", log.fuel_qty as "Fuel Qty", log.price as "Fuel Price"
|
2017-10-17 06:33:02 +00:00
|
|
|
from
|
2017-04-10 06:39:18 +00:00
|
|
|
`tabVehicle` vhcl,`tabVehicle Log` log
|
2017-10-17 06:33:02 +00:00
|
|
|
where
|
2017-04-10 06:39:18 +00:00
|
|
|
vhcl.license_plate = log.license_plate and log.docstatus = 1 and date between %s and %s
|
|
|
|
order by date""" ,(fy.year_start_date, fy.year_end_date), as_dict=1)
|
2016-10-17 11:32:12 +00:00
|
|
|
dl=list(data)
|
|
|
|
for row in dl:
|
|
|
|
row["Service Expense"]= get_service_expense(row["Log"])
|
|
|
|
return dl
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
def get_service_expense(logname):
|
2017-10-17 06:33:02 +00:00
|
|
|
expense_amount = frappe.db.sql("""select sum(expense_amount)
|
|
|
|
from `tabVehicle Log` log,`tabVehicle Service` ser
|
2017-04-10 06:39:18 +00:00
|
|
|
where ser.parent=log.name and log.name=%s""",logname)
|
2016-10-17 11:32:12 +00:00
|
|
|
return flt(expense_amount[0][0]) if expense_amount else 0
|
2017-10-17 06:33:02 +00:00
|
|
|
|
2016-10-17 11:32:12 +00:00
|
|
|
def get_chart_data(data,period_list):
|
|
|
|
fuel_exp_data,service_exp_data,fueldata,servicedata = [],[],[],[]
|
|
|
|
service_exp_data = []
|
|
|
|
fueldata = []
|
|
|
|
for period in period_list:
|
|
|
|
total_fuel_exp=0
|
|
|
|
total_ser_exp=0
|
|
|
|
for row in data:
|
|
|
|
if row["Date"] <= period.to_date and row["Date"] >= period.from_date:
|
|
|
|
total_fuel_exp+=flt(row["Fuel Price"])
|
|
|
|
total_ser_exp+=flt(row["Service Expense"])
|
|
|
|
fueldata.append([period.key,total_fuel_exp])
|
|
|
|
servicedata.append([period.key,total_ser_exp])
|
|
|
|
|
2017-10-17 06:33:02 +00:00
|
|
|
labels = [period.key for period in period_list]
|
2016-10-17 11:32:12 +00:00
|
|
|
fuel_exp_data= [row[1] for row in fueldata]
|
|
|
|
service_exp_data= [row[1] for row in servicedata]
|
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({
|
|
|
|
'title': 'Fuel Expenses',
|
|
|
|
'values': fuel_exp_data
|
|
|
|
})
|
2016-10-17 11:32:12 +00:00
|
|
|
if service_exp_data:
|
2017-10-17 06:33:02 +00:00
|
|
|
datasets.append({
|
|
|
|
'title': 'Service Expenses',
|
|
|
|
'values': service_exp_data
|
|
|
|
})
|
2016-10-17 11:32:12 +00:00
|
|
|
chart = {
|
|
|
|
"data": {
|
2017-10-17 06:33:02 +00:00
|
|
|
'labels': labels,
|
|
|
|
'datasets': datasets
|
2016-10-17 11:32:12 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-17 06:33:02 +00:00
|
|
|
chart["type"] = "line"
|
2016-10-17 11:32:12 +00:00
|
|
|
return chart
|