fleet_vehicle_assignation_log.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class FleetVehicleAssignationLog(models.Model):
  5. _inherit = 'fleet.vehicle.assignation.log'
  6. driver_employee_id = fields.Many2one('hr.employee', string='Driver (Employee)', compute='_compute_driver_employee_id', store=True, readonly=False)
  7. attachment_number = fields.Integer('Number of Attachments', compute='_compute_attachment_number')
  8. @api.depends('driver_id')
  9. def _compute_driver_employee_id(self):
  10. employees = self.env['hr.employee'].search([('address_home_id', 'in', self.driver_id.ids)])
  11. for log in self:
  12. employee = employees.filtered(lambda e: e.address_home_id.id == log.driver_id.id)
  13. log.driver_employee_id = employee and employee[0] or False
  14. def _compute_attachment_number(self):
  15. attachment_data = self.env['ir.attachment']._read_group([
  16. ('res_model', '=', 'fleet.vehicle.assignation.log'),
  17. ('res_id', 'in', self.ids)], ['res_id'], ['res_id'])
  18. attachment = dict((data['res_id'], data['res_id_count']) for data in attachment_data)
  19. for doc in self:
  20. doc.attachment_number = attachment.get(doc.id, 0)
  21. def action_get_attachment_view(self):
  22. self.ensure_one()
  23. res = self.env['ir.actions.act_window']._for_xml_id('base.action_attachment')
  24. res['views'] = [[self.env.ref('hr_fleet.view_attachment_kanban_inherit_hr').id, 'kanban']]
  25. res['domain'] = [('res_model', '=', 'fleet.vehicle.assignation.log'), ('res_id', 'in', self.ids)]
  26. res['context'] = {'default_res_model': 'fleet.vehicle.assignation.log', 'default_res_id': self.id}
  27. return res