# -*- coding: utf-8 -*- # Copyright 2024-2025 Nexa Systems Inc. # License OPL-1 (Odoo Proprietary License v1.0) # Part of the Fusion Claim Assistant product family. from odoo import models, fields, api, _ from odoo.exceptions import UserError import logging _logger = logging.getLogger(__name__) class DeviceApprovalWizard(models.TransientModel): """Wizard to confirm which device types were approved by ADP and set deductions. This is Stage 2 of the two-stage verification system: - Stage 1 (Submission): What device types were submitted (stored in x_fc_submitted_device_types) - Stage 2 (Approval): What device types were approved by ADP (this wizard) """ _name = 'fusion_claims.device.approval.wizard' _description = 'ADP Device Approval Wizard' # ========================================================================== # MAIN FIELDS # ========================================================================== sale_order_id = fields.Many2one( 'sale.order', string='Sale Order', required=True, readonly=True, ) line_ids = fields.One2many( 'fusion_claims.device.approval.wizard.line', 'wizard_id', string='Device Lines', ) all_approved = fields.Boolean( string='All Approved', default=True, help='Check this to approve all devices at once', ) has_unapproved = fields.Boolean( string='Has Unapproved Devices', compute='_compute_has_unapproved', ) has_deductions = fields.Boolean( string='Has Deductions', compute='_compute_has_deductions', help='True if any device has a deduction applied', ) has_invoices = fields.Boolean( string='Has Invoices', compute='_compute_has_invoices', help='True if invoices already exist for this order', ) # Stage 1 comparison fields submitted_device_types = fields.Text( string='Submitted Device Types', compute='_compute_submitted_device_types', help='Device types that were verified during submission (Stage 1)', ) has_submission_data = fields.Boolean( string='Has Submission Data', compute='_compute_submitted_device_types', ) # Claim Number - required for Mark as Approved claim_number = fields.Char( string='Claim Number', help='ADP Claim Number from the approval letter', ) # Approval Documents - for Mark as Approved mode is_mark_approved_mode = fields.Boolean( string='Mark Approved Mode', compute='_compute_is_mark_approved_mode', ) approval_letter = fields.Binary( string='ADP Approval Letter', help='Upload the ADP approval letter PDF', ) approval_letter_filename = fields.Char( string='Approval Letter Filename', ) # For multiple approval photos, we'll use a Many2many to ir.attachment approval_photo_ids = fields.Many2many( 'ir.attachment', 'device_approval_wizard_attachment_rel', 'wizard_id', 'attachment_id', string='Approval Screenshots', help='Upload screenshots from the ADP approval document', ) @api.depends_context('mark_as_approved') def _compute_is_mark_approved_mode(self): for wizard in self: wizard.is_mark_approved_mode = self.env.context.get('mark_as_approved', False) # ========================================================================== # COMPUTED FIELDS # ========================================================================== @api.depends('line_ids.approved') def _compute_has_unapproved(self): for wizard in self: wizard.has_unapproved = any(not line.approved for line in wizard.line_ids) @api.depends('line_ids.deduction_type') def _compute_has_deductions(self): for wizard in self: wizard.has_deductions = any( line.deduction_type and line.deduction_type != 'none' for line in wizard.line_ids ) @api.depends('sale_order_id', 'sale_order_id.invoice_ids') def _compute_has_invoices(self): for wizard in self: if wizard.sale_order_id: wizard.has_invoices = bool(wizard.sale_order_id.invoice_ids.filtered( lambda inv: inv.state != 'cancel' )) else: wizard.has_invoices = False @api.depends('sale_order_id', 'sale_order_id.x_fc_submitted_device_types') def _compute_submitted_device_types(self): """Compute the submitted device types from Stage 1 for comparison display.""" import json for wizard in self: if wizard.sale_order_id and wizard.sale_order_id.x_fc_submitted_device_types: try: data = json.loads(wizard.sale_order_id.x_fc_submitted_device_types) # Format as readable list submitted = [dt for dt, selected in data.items() if selected] wizard.submitted_device_types = '\n'.join([f'• {dt}' for dt in sorted(submitted)]) wizard.has_submission_data = bool(submitted) except (json.JSONDecodeError, TypeError): wizard.submitted_device_types = '' wizard.has_submission_data = False else: wizard.submitted_device_types = '' wizard.has_submission_data = False @api.onchange('all_approved') def _onchange_all_approved(self): """Toggle all lines when 'All Approved' is changed. Only triggers when toggling ON - sets all devices to approved. When toggling OFF, users manually uncheck individual items. """ if self.all_approved: # Iterate and set approved flag - avoid replacing the entire line for line in self.line_ids: if not line.approved: line.approved = True # ========================================================================== # DEFAULT GET - Populate with order lines # ========================================================================== @api.model def default_get(self, fields_list): res = super().default_get(fields_list) active_id = self._context.get('active_id') if not active_id: return res order = self.env['sale.order'].browse(active_id) res['sale_order_id'] = order.id # Build line data from order lines that have ADP device codes ONLY # Non-ADP items are excluded from the verification list ADPDevice = self.env['fusion.adp.device.code'].sudo() lines_data = [] for so_line in order.order_line: # Skip non-product lines if so_line.display_type in ('line_section', 'line_note'): continue if not so_line.product_id or so_line.product_uom_qty <= 0: continue # Get device code and look up device type device_code = so_line._get_adp_device_code() # SKIP items without a valid ADP device code in the database # These are non-ADP items and don't need verification if not device_code: continue adp_device = ADPDevice.search([('device_code', '=', device_code), ('active', '=', True)], limit=1) # Skip if device code not found in ADP database (non-ADP item) if not adp_device: continue device_type = adp_device.device_type or '' device_description = adp_device.device_description or '' # Default to NOT approved - user must actively check each item # Unless it was already approved in a previous verification is_approved = so_line.x_fc_adp_approved if so_line.x_fc_adp_approved else False lines_data.append((0, 0, { 'sale_line_id': so_line.id, 'product_name': so_line.product_id.display_name, 'device_code': device_code, 'device_type': device_type or so_line.x_fc_adp_device_type or '', 'device_description': device_description, 'serial_number': so_line.x_fc_serial_number or '', 'quantity': so_line.product_uom_qty, 'unit_price': so_line.price_unit, 'adp_portion': so_line.x_fc_adp_portion, 'client_portion': so_line.x_fc_client_portion, 'approved': is_approved, 'deduction_type': so_line.x_fc_deduction_type or 'none', 'deduction_value': so_line.x_fc_deduction_value or 0, })) res['line_ids'] = lines_data # All Approved checkbox - only true if ALL lines are already approved if lines_data: all_approved = all(line[2].get('approved', False) for line in lines_data) res['all_approved'] = all_approved else: res['all_approved'] = True # No ADP items = nothing to verify return res # ========================================================================== # ACTION METHODS # ========================================================================== def action_confirm_approval(self): """Confirm the approval status and deductions, update order lines and invoices.""" self.ensure_one() approved_count = 0 unapproved_count = 0 deduction_count = 0 updated_lines = self.env['sale.order.line'] for wiz_line in self.line_ids: if wiz_line.sale_line_id: # NOTE: Do NOT write x_fc_adp_device_type here - it's a computed field # that should be computed from the product's device code vals = { 'x_fc_adp_approved': wiz_line.approved, 'x_fc_deduction_type': wiz_line.deduction_type or 'none', 'x_fc_deduction_value': wiz_line.deduction_value or 0, } wiz_line.sale_line_id.write(vals) updated_lines |= wiz_line.sale_line_id if wiz_line.approved: approved_count += 1 else: unapproved_count += 1 if wiz_line.deduction_type and wiz_line.deduction_type != 'none': deduction_count += 1 # MARK VERIFICATION AS COMPLETE on the sale order # This allows invoice creation even if some items are unapproved if self.sale_order_id: self.sale_order_id.write({'x_fc_device_verification_complete': True}) # FORCE RECALCULATION of ADP/Client portions on all order lines # This ensures unapproved items get 100% assigned to client portion for line in self.sale_order_id.order_line: line._compute_adp_portions() # Recalculate order totals self.sale_order_id._compute_adp_totals() # If we're in "mark_as_approved" mode, also update the status and save approval documents if self.env.context.get('mark_as_approved') and self.sale_order_id: # Determine status based on whether there are deductions new_status = 'approved_deduction' if deduction_count > 0 else 'approved' update_vals = { 'x_fc_adp_application_status': new_status, } # Save claim number if provided if self.claim_number: update_vals['x_fc_claim_number'] = self.claim_number # Save approval letter if uploaded if self.approval_letter: update_vals['x_fc_approval_letter'] = self.approval_letter update_vals['x_fc_approval_letter_filename'] = self.approval_letter_filename self.sale_order_id.with_context(skip_status_validation=True).write(update_vals) # Collect attachment IDs for chatter post chatter_attachment_ids = [] # IMPORTANT: When files are uploaded via many2many_binary to a transient model, # they are linked to the wizard and may be garbage collected when the wizard closes. # We need to COPY the attachment data to create persistent attachments linked to the sale order. photos_attached = 0 if self.approval_photo_ids: photo_ids_to_link = [] IrAttachment = self.env['ir.attachment'].sudo() for attachment in self.approval_photo_ids: # Create a NEW attachment linked to the sale order (copy the data) # This ensures the attachment persists after the wizard is deleted new_attachment = IrAttachment.create({ 'name': attachment.name or f'approval_screenshot_{photos_attached + 1}', 'datas': attachment.datas, 'mimetype': attachment.mimetype, 'res_model': 'sale.order', 'res_id': self.sale_order_id.id, 'type': 'binary', }) chatter_attachment_ids.append(new_attachment.id) photo_ids_to_link.append(new_attachment.id) photos_attached += 1 # Link photos to the Many2many field for easy access in ADP Documents tab if photo_ids_to_link: existing_photo_ids = self.sale_order_id.x_fc_approval_photo_ids.ids self.sale_order_id.write({ 'x_fc_approval_photo_ids': [(6, 0, existing_photo_ids + photo_ids_to_link)] }) # Create attachment for approval letter if uploaded if self.approval_letter: letter_attachment = self.env['ir.attachment'].create({ 'name': self.approval_letter_filename or 'ADP_Approval_Letter.pdf', 'datas': self.approval_letter, 'res_model': 'sale.order', 'res_id': self.sale_order_id.id, }) chatter_attachment_ids.append(letter_attachment.id) # Post approval to chatter with all documents in ONE message from markupsafe import Markup from datetime import date device_details = f'{approved_count} approved' if unapproved_count > 0: device_details += f', {unapproved_count} not approved' if deduction_count > 0: device_details += f', {deduction_count} with deductions' # Build documents list - show individual file names for screenshots docs_items = '' if self.approval_letter: docs_items += f'
Documents:
Date: {date.today().strftime("%B %d, %Y")}
' f'Devices: {device_details}
' f'{docs_html}' 'Date: {date.today().strftime("%B %d, %Y")}
' f'Devices: {device_details}
' '