Files
odoo_source/addons/hr_fleet/controllers/main.py
T
jbm-odoo 22ee6ebbfc [ADD] hr_fleet: Add claim report and cars history per employee
Purpose
=======

When an employee leaves the company, the fleet manager must ask to the insurance company
a claim report for this employee. But in oddo, there is no easy way to have an history
of the drivers.

Specification
=============

1/ On the employee form, add a stat button: "x cars". The button shouldn't be displayed if the count = 0.
   Make a bridge (auto_install: True in the manifest) depending on hr (hr.employee) and fleet (fleet.vehicle.assignation.log)
   - Add a field employee_cars_count (computed) to count the amount of cars the employee drove.
   - Add an method action_open_employee_cars, that return an action (ir.actions.act_window)
     that open on the fleet.vehicle kanban filtered on cars the employee had driven.
2/ In this bridge module, add on the fleet.vehicle.assignation.logs a button (like in expense) to upload
   documents about the car for a particular employee (PV, rapport d'assurance, blabla). On the employee
   form view, add an action button (with groups fleet_manager) to print all the related documents
   (all the attachements on the fleet.vehicle.assignation.logs for the employee).

closes odoo/odoo#28077
2018-11-06 13:01:57 +00:00

73 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import base64
import io
from odoo import _
from odoo.http import request, route, Controller
from PyPDF2 import PdfFileReader, PdfFileWriter
from reportlab.pdfgen import canvas
class HrFleet(Controller):
@route(["/fleet/print_claim_report/<int:employee_id>"], type='http', auth='user')
def get_claim_report_user(self, employee_id, **post):
if not request.env.user.has_group('fleet.fleet_group_manager'):
return request.not_found()
employee = request.env['hr.employee'].search([('id', '=', employee_id)], limit=1)
partner_id = employee.user_id.partner_id.id
if not employee or not partner_id:
return request.not_found()
car_assignation_logs = request.env['fleet.vehicle.assignation.log'].search([('driver_id', '=', partner_id)])
doc_list = request.env['ir.attachment'].search([
('res_model', '=', 'fleet.vehicle.assignation.log'),
('res_id', 'in', car_assignation_logs.ids)], order='create_date')
writer = PdfFileWriter()
font = "Helvetica"
normal_font_size = 14
for document in doc_list:
car_line_doc = request.env['fleet.vehicle.assignation.log'].browse(document.res_id)
try:
reader = PdfFileReader(io.BytesIO(base64.b64decode(document.datas)), strict=False, overwriteWarnings=False)
except Exception:
continue
width = float(reader.getPage(0).mediaBox.getUpperRight_x())
height = float(reader.getPage(0).mediaBox.getUpperRight_y())
header = io.BytesIO()
can = canvas.Canvas(header)
can.setFont(font, normal_font_size)
can.setFillColorRGB(1, 0, 0)
car_name = car_line_doc.vehicle_id.display_name
date_start = car_line_doc.date_start
date_end = car_line_doc.date_end or '...'
text_to_print = _("%s (driven from: %s to %s)") % (car_name, date_start, date_end)
can.drawCentredString(width / 2, height - normal_font_size, text_to_print)
can.save()
header_pdf = PdfFileReader(header, overwriteWarnings=False)
for page_number in range(0, reader.getNumPages()):
page = reader.getPage(page_number)
page.mergePage(header_pdf.getPage(0))
writer.addPage(page)
_buffer = io.BytesIO()
writer.write(_buffer)
merged_pdf = _buffer.getvalue()
_buffer.close()
pdfhttpheaders = [('Content-Type', 'application/pdf'), ('Content-Length', len(merged_pdf))]
return request.make_response(merged_pdf, headers=pdfhttpheaders)