diff --git a/odex25_benefit/odex_benefit/models/service_request.py b/odex25_benefit/odex_benefit/models/service_request.py index b8d6e79e7..f6ba2683e 100644 --- a/odex25_benefit/odex_benefit/models/service_request.py +++ b/odex25_benefit/odex_benefit/models/service_request.py @@ -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: diff --git a/odex25_benefit/odex_benefit/views/service_request.xml b/odex25_benefit/odex_benefit/views/service_request.xml index 56b3eff22..7bfdbcec6 100644 --- a/odex25_benefit/odex_benefit/views/service_request.xml +++ b/odex25_benefit/odex_benefit/views/service_request.xml @@ -268,13 +268,19 @@ - +