# -*- coding: utf-8 -*- from odoo import models, fields, api from odoo.exceptions import UserError class SupplierRequest(models.Model): _name = 'supplier.request' _description = 'Richiesta fornitore' _inherit = ['mail.thread', 'mail.activity.mixin'] _order = 'create_date desc' name = fields.Char( string="Numero richiesta", required=True, copy=False, readonly=True, index=True, default=lambda self: self.env['ir.sequence'].next_by_code('supplier.request') ) # Campo 1: Fornitore (selezionabile dai contatti) partner_id = fields.Many2one( 'res.partner', string="Fornitore", required=True, help="Seleziona il fornitore dalla lista dei contatti" ) # Campo 2: Descrizione description = fields.Text( string="Descrizione", required=True, help="Descrivi dettagliatamente la richiesta" ) # Campo 3: Allegati attachment_ids = fields.Many2many( 'ir.attachment', 'supplier_request_attachment_rel', 'request_id', 'attachment_id', string="Allegati" ) # Stage configurabile (simile al CRM) stage_id = fields.Many2one( 'supplier.request.stage', string='Stato', index=True, tracking=True, group_expand='_read_group_stage_ids', ondelete='restrict', default=lambda self: self.env['supplier.request.stage'].search([('sequence', '=', 1)], limit=1), help="Stato attuale della richiesta fornitore" ) # Campi informativi create_uid = fields.Many2one('res.users', string='Creato da', readonly=True) create_date = fields.Datetime(string='Data Creazione', readonly=True) @api.model def create(self, vals): if vals.get('name', '/') == '/': vals['name'] = self.env['ir.sequence'].next_by_code('supplier.request') or '/' return super(SupplierRequest, self).create(vals) @api.model def _read_group_stage_ids(self, stages, domain): """Read group customization for stage_id field to always show all stages""" # Mostra sempre tutti gli stage attivi search_domain = ['|', ('id', 'in', stages.ids), ('active', '=', True)] stage_ids = stages.sudo()._search(search_domain, order=stages._order) return stages.browse(stage_ids) def action_move_to_stage(self, stage_name): """Metodo generico per spostare a uno stage specifico""" self.ensure_one() stage = self.env['supplier.request.stage'].search([('name', '=', stage_name)], limit=1) if stage: old_stage_name = self.stage_id.name if self.stage_id else 'Nessuno' self.stage_id = stage self.message_post(body=f"Richiesta spostata da '{old_stage_name}' a '{stage_name}'.") else: raise UserError(f"Stage '{stage_name}' non trovato.") def action_submit(self): """Invia la richiesta""" self.action_move_to_stage('Inviata') def action_approve(self): """Approva la richiesta""" self.action_move_to_stage('Approvata') def action_reject(self): """Rifiuta la richiesta""" self.action_move_to_stage('Rifiutata') def action_done(self): """Completa la richiesta""" self.action_move_to_stage('Completata') def action_cancel(self): """Annulla la richiesta""" self.action_move_to_stage('Annullata') def action_reset_to_draft(self): """Ripristina la richiesta in bozza""" self.action_move_to_stage('Bozza') # Metodi di utilità per verificare lo stato @api.depends('stage_id') def _compute_is_closed(self): for record in self: record.is_closed = record.stage_id.is_closed if record.stage_id else False @api.depends('stage_id') def _compute_is_cancelled(self): for record in self: record.is_cancelled = record.stage_id.is_cancelled if record.stage_id else False is_closed = fields.Boolean('È chiusa', compute='_compute_is_closed', store=True) is_cancelled = fields.Boolean('È annullata', compute='_compute_is_cancelled', store=True)