hr_departure_wizard.py 1.4 KB

12345678910111213141516171819202122232425262728
  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 HrDepartureWizard(models.TransientModel):
  5. _inherit = 'hr.departure.wizard'
  6. release_campany_car = fields.Boolean("Release Company Car", default=lambda self: self.env.user.user_has_groups('fleet.fleet_group_user'))
  7. def action_register_departure(self):
  8. super(HrDepartureWizard, self).action_register_departure()
  9. if self.release_campany_car:
  10. self._free_campany_car()
  11. def _free_campany_car(self):
  12. """Find all fleet.vehichle.assignation.log records that link to the employee, if there is no
  13. end date or end date > departure date, update the date. Also check fleet.vehicle to see if
  14. there is any record with its dirver_id to be the employee, set them to False."""
  15. drivers = self.employee_id.user_id.partner_id | self.employee_id.sudo().address_home_id
  16. assignations = self.env['fleet.vehicle.assignation.log'].search([('driver_id', 'in', drivers.ids)])
  17. for assignation in assignations:
  18. if self.departure_date and (not assignation.date_end or assignation.date_end > self.departure_date):
  19. assignation.write({'date_end': self.departure_date})
  20. cars = self.env['fleet.vehicle'].search([('driver_id', 'in', drivers.ids)])
  21. cars.write({'driver_id': False, 'driver_employee_id': False})