87 lines
3.9 KiB
Python
87 lines
3.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright 2026 Nexa Systems Inc.
|
|
# License OPL-1 (Odoo Proprietary License v1.0)
|
|
from odoo import api, fields, models
|
|
|
|
|
|
class FpPermit(models.Model):
|
|
_name = 'fusion.plating.permit'
|
|
_description = 'Fusion Plating - Regulatory Permit'
|
|
_inherit = ['mail.thread', 'mail.activity.mixin']
|
|
_order = 'expiry_date, name'
|
|
|
|
name = fields.Char(string='Title', required=True, tracking=True)
|
|
permit_type = fields.Selection(
|
|
[('eca', 'Environmental Compliance Approval'),
|
|
('sewer', 'Sewer Use Permit'),
|
|
('waste_generator', 'Waste Generator Registration'),
|
|
('air', 'Air Permit'),
|
|
('water', 'Water Taking / Discharge'),
|
|
('fire', 'Fire / Hazmat'),
|
|
('other', 'Other')],
|
|
string='Type', required=True, default='eca', tracking=True,
|
|
)
|
|
number = fields.Char(string='Permit Number', tracking=True)
|
|
facility_id = fields.Many2one('fusion.plating.facility', string='Facility', required=True, ondelete='cascade', tracking=True)
|
|
company_id = fields.Many2one('res.company', related='facility_id.company_id', store=True, readonly=True)
|
|
regulator_id = fields.Many2one('fusion.plating.regulator', string='Regulator', ondelete='restrict', tracking=True)
|
|
jurisdiction_id = fields.Many2one('fusion.plating.jurisdiction', string='Jurisdiction', ondelete='restrict')
|
|
issue_date = fields.Date(string='Issued', tracking=True)
|
|
expiry_date = fields.Date(string='Expires', tracking=True)
|
|
state = fields.Selection(
|
|
[('draft', 'Draft'), ('pending', 'Pending'), ('active', 'Active'),
|
|
('expired', 'Expired'), ('revoked', 'Revoked')],
|
|
string='Status', default='draft', required=True, tracking=True,
|
|
)
|
|
renewal_reminder_days = fields.Integer(string='Reminder (days before expiry)', default=60)
|
|
days_until_expiry = fields.Integer(string='Days Until Expiry', compute='_compute_days_until_expiry')
|
|
is_expiring_soon = fields.Boolean(string='Expiring Soon', compute='_compute_days_until_expiry', search='_search_is_expiring_soon')
|
|
condition_ids = fields.One2many('fusion.plating.permit.condition', 'permit_id', string='Conditions', copy=True)
|
|
condition_count = fields.Integer(compute='_compute_condition_count')
|
|
attachment_ids = fields.Many2many(
|
|
'ir.attachment', 'fp_permit_attachment_rel', 'permit_id', 'attachment_id', string='Attachments',
|
|
)
|
|
notes = fields.Html(string='Notes')
|
|
active = fields.Boolean(default=True)
|
|
|
|
@api.depends('expiry_date')
|
|
def _compute_days_until_expiry(self):
|
|
today = fields.Date.context_today(self)
|
|
for rec in self:
|
|
if rec.expiry_date:
|
|
delta = (rec.expiry_date - today).days
|
|
rec.days_until_expiry = delta
|
|
rec.is_expiring_soon = 0 <= delta <= (rec.renewal_reminder_days or 60)
|
|
else:
|
|
rec.days_until_expiry = 0
|
|
rec.is_expiring_soon = False
|
|
|
|
def _search_is_expiring_soon(self, operator, value):
|
|
today = fields.Date.context_today(self)
|
|
permits = self.search([('expiry_date', '!=', False)])
|
|
soon_ids = []
|
|
for p in permits:
|
|
delta = (p.expiry_date - today).days
|
|
if 0 <= delta <= (p.renewal_reminder_days or 60):
|
|
soon_ids.append(p.id)
|
|
if (operator == '=' and value) or (operator == '!=' and not value):
|
|
return [('id', 'in', soon_ids)]
|
|
return [('id', 'not in', soon_ids)]
|
|
|
|
@api.depends('condition_ids')
|
|
def _compute_condition_count(self):
|
|
for rec in self:
|
|
rec.condition_count = len(rec.condition_ids)
|
|
|
|
def action_activate(self):
|
|
self.write({'state': 'active'})
|
|
|
|
def action_set_pending(self):
|
|
self.write({'state': 'pending'})
|
|
|
|
def action_revoke(self):
|
|
self.write({'state': 'revoked'})
|
|
|
|
def action_mark_expired(self):
|
|
self.write({'state': 'expired'})
|