[FIX] hr_expense_custom: make Show Expense Module group visible without debug mode && fix approve expense sheet method that could the selected manager approve it

This commit is contained in:
Samir Ladoui 2025-02-06 12:52:11 +01:00
parent d26cacde55
commit 60d6c51195
3 changed files with 49 additions and 2 deletions

View File

@ -1 +1,2 @@
from . import hr_employee
from . import hr_employee
from . import hr_expense_sheet

View File

@ -0,0 +1,47 @@
from odoo import models, _
from odoo.exceptions import UserError
class HrExpenseSheet(models.Model):
_inherit = 'hr.expense.sheet'
def approve_expense_sheets(self):
# This function is same as super function if the super added something you should add the modification here
if not self.user_has_groups('hr_expense.group_hr_expense_team_approver'):
raise UserError(_("Only Managers and HR Officers can approve expenses"))
elif not self.user_has_groups('hr_expense.group_hr_expense_manager'):
# The line below is the only line is different of the super
current_managers = self.employee_id.expense_manager_id |\
self.employee_id.parent_id.user_id |\
self.employee_id.department_id.manager_id.user_id |\
self.user_id
if self.employee_id.user_id == self.env.user:
raise UserError(_("You cannot approve your own expenses"))
if not self.env.user in current_managers and not self.user_has_groups('hr_expense.group_hr_expense_user') and self.employee_id.expense_manager_id != self.env.user:
raise UserError(_("You can only approve your department expenses"))
notification = {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _('There are no expense reports to approve.'),
'type': 'warning',
'sticky': False, #True/False will display for few seconds if false
},
}
filtered_sheet = self.filtered(lambda s: s.state in ['submit', 'draft'])
if not filtered_sheet:
return notification
for sheet in filtered_sheet:
sheet.write({'state': 'approve', 'user_id': sheet.user_id.id or self.env.user.id})
notification['params'].update({
'title': _('The expense reports were successfully approved.'),
'type': 'success',
'next': {'type': 'ir.actions.act_window_close'},
})
self.activity_update()
return notification

View File

@ -3,7 +3,6 @@
<record id="show_hr_expense_module" model="res.groups">
<field name="name">Show Expenses Module</field>
<field name="category_id" ref="base.module_category_hidden"/>
</record>
</odoo>