Skip to content

Commit

Permalink
[RFC] egd_cnh_date_expiry: refactor module
Browse files Browse the repository at this point in the history
  • Loading branch information
WesleyOliveira98 committed Oct 8, 2024
1 parent 3c829bb commit 17482ce
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 68 deletions.
2 changes: 1 addition & 1 deletion egd_cnh_date_expiry/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"author": "Escodoo",
"website": "https://github.com/Escodoo/egd-addons",
"depends": ["l10n_br_hr", "fleet"],
"data": ["views/hr_employee.xml", "views/fleet_vehicle_menu.xml"],
"data": ["views/hr_employee.xml"],
}
79 changes: 44 additions & 35 deletions egd_cnh_date_expiry/models/hr_employee.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
# Copyright 2024 - TODAY, Matheus Marques <[email protected]>
# Copyright 2024 - TODAY, Wesley Oliveira <[email protected]>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from datetime import datetime, timedelta
from datetime import date

from odoo import models
from odoo import api, fields, models


class HrEmployee(models.Model):

_inherit = "hr.employee"

def _get_employee_expirations(self):
expiration_date_threshold = datetime.now() + timedelta(days=30)
employees = self.env["hr.employee"].search(
[("expiration_date", "<=", expiration_date_threshold)]
)
expirations = []
for employee in employees:
days_until_expiration = (
employee.expiration_date - datetime.now().date()
).days
expirations.append(
{"name": employee.name, "days_until_expiration": days_until_expiration}
)
return expirations

def action_view_employee_expirations(self):
expirations = self._get_employee_expirations()
return {
"name": "Employees with Expiration Date Approaching",
"view_mode": "tree",
"res_model": "employee.expiration",
"type": "ir.actions.act_window",
"context": {"create": False},
"domain": [],
"view_id": False,
"views": [(False, "tree")],
"target": "current",
"readonly": True,
"limit": 80,
"auto_search": False,
"auto_refresh": 1,
"expirations": expirations,
}
cnh_days_to_expire = fields.Integer(
string="CNH Days to Expire",
compute="_compute_cnh_days_to_expire",
readonly=True,
)
cnh_expiry_state = fields.Selection(
selection=[
("valid", "Valid"),
("expiring_soon", "Expiring Soon"),
("expired", "Expired"),
("no_cnh", "No CNH"),
],
string="CNH Expiry State",
compute="_compute_cnh_expiry_state",
store=True,
readonly=True,
)

@api.depends("expiration_date")
def _compute_cnh_days_to_expire(self):
today = date.today()
for employee in self:
if employee.expiration_date:
days = (employee.expiration_date - today).days
employee.cnh_days_to_expire = days
else:
employee.cnh_days_to_expire = 0
employee._compute_cnh_expiry_state()

@api.depends("driver_license", "cnh_days_to_expire")
def _compute_cnh_expiry_state(self):
for employee in self:
if not employee.driver_license:
employee.cnh_expiry_state = "no_cnh"
else:
if employee.cnh_days_to_expire < 0:
employee.cnh_expiry_state = "expired"
elif employee.cnh_days_to_expire <= 30:
employee.cnh_expiry_state = "expiring_soon"
else:
employee.cnh_expiry_state = "valid"
7 changes: 5 additions & 2 deletions egd_cnh_date_expiry/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
Marcel Savegnago <[email protected]>
Matheus Marques <[email protected]>
* `Escodoo <https://escodoo.com.br>`_:

* Marcel Savegnago <[email protected]>
* Matheus Marques <[email protected]>
* Wesley Oliveira <[email protected]>
6 changes: 3 additions & 3 deletions egd_cnh_date_expiry/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
To use this module you need:

1- Access the employee module and set an expiration date for the driver's license
2 - When the validity is close to one month,
it will appear in a view in fleet/vehicles as cnh expiry
1 - Access the employee module and set an expiration date for the driver's license
2 - In employee tree view, you will have the fields related to driver license expiration
and filters to search driver license expired or expiring soon.
13 changes: 0 additions & 13 deletions egd_cnh_date_expiry/views/fleet_vehicle_menu.xml

This file was deleted.

57 changes: 43 additions & 14 deletions egd_cnh_date_expiry/views/hr_employee.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2024 - TODAY, Matheus Marques <[email protected]>
Copyright 2024 - TODAY, Wesley Oliveira <[email protected]>
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
-->
<odoo>
<record id="view_employee_expiration_tree" model="ir.ui.view">
<field name="name">hr.employee.expiration.tree (in egd_CNH_expiry)</field>

<record id="view_employee_tree" model="ir.ui.view">
<field name="name">hr.employee.tree (in egd_cnh_date_expiry)</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_tree" />
<field name="arch" type="xml">
<tree delete="false" create="false" string="CNH Expiry">
<field name="name" />
<field name="expiration_date" />
</tree>
<field name="work_location" position="after">
<field name="driver_license" optional="hide" />
<field name="driver_categ" optional="hide" />
<field name="expiration_date" optional="hide" />
<field name="cnh_days_to_expire" optional="hide" />
<field
name="cnh_expiry_state"
optional="hide"
widget="badge"
decoration-success="cnh_expiry_state == 'valid'"
decoration-warning="cnh_expiry_state == 'expiring_soon'"
decoration-danger="cnh_expiry_state == 'expired'"
/>
</field>
</field>
</record>

<record id="action_employee_expiration" model="ir.actions.act_window">
<field name="name">CNH Expiry</field>
<field name="res_model">hr.employee</field>
<field name="view_mode">tree</field>
<field name="domain">[('expiration_date', '&lt;=', (context_today() +
relativedelta(days=30)).strftime('%Y-%m-%d')), ('expiration_date', '>=',
context_today().strftime('%Y-%m-%d'))]</field>
<record id="view_employee_filter" model="ir.ui.view">
<field name="name">hr.employee.search (in egd_cnh_date_expiry)</field>
<field name="model">hr.employee</field>
<field name="inherit_id" ref="hr.view_employee_filter" />
<field name="arch" type="xml">
<xpath expr="//filter[@name='inactive']" position="before">
<filter
string="CNH Expired"
name="cnh_expired"
domain="[('cnh_expiry_state', '=', 'expired')]"
/>
<filter
string="CNH Expiring Soon"
name="cnh_expiring_soon"
domain="[('cnh_expiry_state', '=', 'expiring_soon')]"
/>
<separator />
</xpath>
</field>
</record>


</odoo>

0 comments on commit 17482ce

Please sign in to comment.