[IMP] odex_benefit: IMP benefit

This commit is contained in:
younes 2026-01-08 11:39:16 +01:00
parent 85a135c1e5
commit c00f5b584c
2 changed files with 37 additions and 18 deletions

View File

@ -1,4 +1,3 @@
from odoo import fields, models, api, _
from odoo.exceptions import UserError, ValidationError
from datetime import date, datetime, timedelta
@ -42,10 +41,11 @@ class ServiceRequest(models.Model):
'attachment_id', string='Service Attachment')
requested_service_amount = fields.Float(string="Requested Service Amount", copy=False)
# yearly Estimated Rent Amount
estimated_rent_amount = fields.Float(string="Estimated Rent Amount", compute="_get_estimated_rent_amount",store=True)
estimated_rent_amount = fields.Float(string="Estimated Rent Amount", compute="_get_estimated_rent_amount",
store=True)
# The value of payment by payment method(yearly-half-quartarly)
estimated_rent_amount_payment = fields.Float(string="Estimated Rent Amount Payment",
compute="_get_estimated_rent_amount_payment",store=True)
compute="_get_estimated_rent_amount_payment", store=True)
service_type = fields.Selection([('rent', 'Rent')], string='Service Type', related='service_cat.service_type')
max_limit_period = fields.Selection(string='Maximum Limit Period', related='service_cat.max_limit_period')
payment_method = fields.Selection([
@ -58,7 +58,7 @@ class ServiceRequest(models.Model):
rent_start_date = fields.Date(string='Rent Start Date', compute='_compute_rent_details', store=True)
rent_end_date = fields.Date(string='Rent End Date', compute='_compute_rent_details', store=True)
rent_amount = fields.Float(string='Rent Amount', compute='_compute_rent_details', store=True)
rent_amount_payment = fields.Float(string='Rent Amount Payment', compute='_get_rent_amount_payment')
rent_amount_payment = fields.Float(string='Rent Amount Payment', compute='_get_rent_amount_payment',store=True)
payment_type = fields.Selection(
[
('1', 'Yearly'),
@ -213,7 +213,7 @@ class ServiceRequest(models.Model):
required_docs = self.attachment_lines.filtered(lambda l: l.state == current_state)
missing_docs = required_docs.filtered(lambda l: not l.service_attach)
if missing_docs:
#missing_names = ", ".join(missing_docs.mapped('name'))
# missing_names = ", ".join(missing_docs.mapped('name'))
missing_names = ", ".join([doc.name for doc in missing_docs if doc.name])
raise UserError(
_("Cannot proceed with approval. The following required documents for status '%(status)s' are missing: %(missing)s") % {
@ -979,7 +979,13 @@ class ServiceRequest(models.Model):
else:
if service_type in special_services:
if service_type == 'electrical_devices':
rec.service_max_amount = rec.device_id.price_unit if rec.device_id else 0.0
#rec.service_max_amount = rec.device_id.price_unit if rec.device_id else 0.0
if rec.device_id:
rec.service_max_amount = rec.device_id.price_unit * rec.requested_quantity
rec.requested_service_amount = rec.service_max_amount
else:
rec.service_max_amount = 0.0
rec.requested_service_amount = 0.0
if rec.device_id:
delta_kwargs = {period: interval}
date_before = rec.date - relativedelta(**delta_kwargs)
@ -1052,13 +1058,19 @@ class ServiceRequest(models.Model):
if rec.start and rec.end:
start_date = rec.start.date() if isinstance(rec.start, datetime) else rec.start
end_date = rec.end.date() if isinstance(rec.end, datetime) else rec.end
num_months = (end_date.year - start_date.year) * 12 + (
end_date.month - start_date.month) + 1
if num_months > rec.service_cat.max_months_limit:
diff = relativedelta(end_date, start_date)
num_months = diff.years * 12 + diff.months
if end_date > (start_date + relativedelta(months=rec.service_cat.max_months_limit)):
raise ValidationError(
_("You cannot request this service for more than %s months.") % rec.service_cat.max_months_limit
)
if diff.days > 0:
num_months += 1
rec.service_max_amount *= num_months
if service_type == 'rent':
rec.requested_service_amount = min(rec.rent_amount_payment,
rec.estimated_rent_amount_payment)
elif rec.max_limit_period == "calendar_year":
current_date = rec.date.date() if isinstance(rec.date, datetime) else rec.date
allowed_years = rec.service_cat.allowed_period or 1
@ -1076,7 +1088,7 @@ class ServiceRequest(models.Model):
existing_requests = Service.search(period_domain)
total_spent = sum(existing_requests.mapped('requested_service_amount'))
rec.service_max_amount = rec.service_cat.max_amount - total_spent
rec.service_max_amount -= total_spent
elif rec.max_limit_period == "year_from_request":
current_date = rec.date.date() if isinstance(rec.date, datetime) else rec.date
allowed_years = rec.service_cat.allowed_period or 1
@ -1090,7 +1102,7 @@ class ServiceRequest(models.Model):
existing_requests = Service.search(period_domain)
total_spent = sum(existing_requests.mapped('requested_service_amount'))
rec.service_max_amount = rec.service_cat.max_amount - total_spent
rec.service_max_amount -= total_spent
elif rec.max_limit_period == "individual":
rec.service_max_amount *= rec.service_benefit_count
if rec.service_cat.service_type == 'marriage':
@ -1333,6 +1345,7 @@ class ServiceRequest(models.Model):
if rec.estimated_rent_amount and rec.payment_type:
rec.estimated_rent_amount_payment = rec.estimated_rent_amount / int(rec.payment_type)
@api.depends('rent_amount', 'payment_type')
def _get_rent_amount_payment(self):
for rec in self:
if rec.rent_amount and rec.payment_type:

View File

@ -268,13 +268,19 @@
<field name="show_car_remaining_amount" invisible="1"/>
<field name="car_price"
attrs="{'readonly':[('state','not in',['draft','researcher','waiting_approve'])],'invisible': [('service_type', '!=', 'buy_car')]}"/>
<field name="requested_service_amount" attrs="{'readonly': [
'|',
'&amp;',
('state', 'not in', ['draft', 'researcher', 'waiting_approve', 'first_approve', 'gm_assistant', 'accounting_approve']),
('service_type', 'in', ['marriage', 'eid_gift', 'winter_clothing', 'ramadan_basket']),
('service_type', 'in', ['electrical_devices', 'home_furnishing'])
]}" force_save="1" required="1"/>
<field name="requested_service_amount"
attrs="{
'readonly': [
'&amp;',
('exception', '=', False),
'|',
'&amp;',
('state', 'not in', ['draft', 'researcher', 'waiting_approve', 'first_approve', 'gm_assistant', 'accounting_approve']),
('service_type', 'in', ['marriage', 'eid_gift', 'winter_clothing', 'ramadan_basket']),
('service_type', 'in', ['electrical_devices', 'home_furnishing', 'rent'])
]
}"
force_save="1" required="1"/>
<field name="car_remaining_amount" readonly="1"
attrs="{'invisible': [('show_car_remaining_amount', '=', False)]}"/>
<field name="service_max_amount" force_save="1" readonly="1"/>