# -*- 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 class SaleAdvancePaymentInv(models.TransientModel): _inherit = 'sale.advance.payment.inv' # Add new invoice type options for ADP advance_payment_method = fields.Selection( selection_add=[ ('adp_client', 'ADP Client Invoice (25%)'), ('adp_portion', 'ADP Invoice (75%/100%)'), ], ondelete={ 'adp_client': 'set default', 'adp_portion': 'set default', } ) def _is_adp_sale_order(self): """Check if the current sale order(s) are ADP sales.""" sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', [])) for order in sale_orders: if hasattr(order, '_is_adp_sale') and order._is_adp_sale(): return True return False def _get_adp_client_type(self): """Get client type from the first ADP sale order.""" sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', [])) for order in sale_orders: if hasattr(order, '_get_client_type'): return order._get_client_type() return 'REG' def create_invoices(self): """Override to handle ADP split invoices.""" if self.advance_payment_method == 'adp_client': return self._create_adp_client_invoice() elif self.advance_payment_method == 'adp_portion': return self._create_adp_portion_invoice() return super().create_invoices() def _create_adp_client_invoice(self): """Create 25% client invoice for REG clients.""" sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', [])) invoices = self.env['account.move'] for order in sale_orders: if not hasattr(order, '_is_adp_sale') or not order._is_adp_sale(): raise UserError(_("Order %s is not an ADP sale. Cannot create ADP client invoice.") % order.name) client_type = order._get_client_type() if hasattr(order, '_get_client_type') else 'REG' if client_type != 'REG': raise UserError(_( "Order %s has client type '%s'. Only REG clients have a 25%% client portion. " "Use 'ADP Invoice (75%%/100%%)' for this order." ) % (order.name, client_type)) invoice = order._create_adp_split_invoice(invoice_type='client') if invoice: invoices |= invoice if not invoices: raise UserError(_("No invoices were created. Check if the orders have a client portion.")) # Return action to view created invoices if len(invoices) == 1: return { 'name': _('Client Invoice (25%)'), 'type': 'ir.actions.act_window', 'res_model': 'account.move', 'res_id': invoices.id, 'view_mode': 'form', 'target': 'current', } return { 'name': _('Client Invoices (25%)'), 'type': 'ir.actions.act_window', 'res_model': 'account.move', 'domain': [('id', 'in', invoices.ids)], 'view_mode': 'list,form', 'target': 'current', } def _create_adp_portion_invoice(self): """Create 75%/100% ADP invoice.""" sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', [])) invoices = self.env['account.move'] for order in sale_orders: if not hasattr(order, '_is_adp_sale') or not order._is_adp_sale(): raise UserError(_("Order %s is not an ADP sale. Cannot create ADP invoice.") % order.name) invoice = order._create_adp_split_invoice(invoice_type='adp') if invoice: invoices |= invoice if not invoices: raise UserError(_("No invoices were created.")) # Return action to view created invoices if len(invoices) == 1: return { 'name': _('ADP Invoice'), 'type': 'ir.actions.act_window', 'res_model': 'account.move', 'res_id': invoices.id, 'view_mode': 'form', 'target': 'current', } return { 'name': _('ADP Invoices'), 'type': 'ir.actions.act_window', 'res_model': 'account.move', 'domain': [('id', 'in', invoices.ids)], 'view_mode': 'list,form', 'target': 'current', }