class SaleOrder(models.Model): _inherit = 'sale.order' state = fields.Selection(selection_add=[ ('so_hold', 'SO Hold') ])
def _action_confirm(self): """Check if the customer's or their parent's due amount exceeds the available blocking limit."""
unpaid_invoices = self.env['account.move'].search([ ('partner_id.parent_id', '=', self.partner_id.parent_id.id), ('state', '=', 'posted'), ('amount_residual', '>', 0), ('move_type', 'in', ['out_invoice', 'out_refund']) ])
total_unpaid = sum(unpaid_invoices.mapped('amount_residual')) blocking_limit = self.partner_id.parent_id.blocking_stage available_credit = blocking_limit - total_unpaid
_logger.info("blocking_limit: %.2f", blocking_limit) _logger.info("total_unpaid: %.2f", total_unpaid) _logger.info("available_credit: %.2f", available_credit)
if self.partner_id.active_limit and self.partner_id.enable_credit_limit: to_invoice = self._get_sale_invoice_parent() required_credit = self.amount_total + to_invoice
_logger.info("required_credit: %.2f", required_credit)
if required_credit > available_credit and not self.approval_credit_limit: _logger.warning("Order exceeds available credit: blocking") self.over_credit_limit_block = True self.state = 'so_hold' # Change state to "so hold" raise UserError(_( "The customer %s is in Blocking Stage. The available credit (%.2f) is not enough to cover the total due " "(%.2f).") % (self.partner_id.name, available_credit, required_credit))
return super(SaleOrder, self)._action_confirm()
I want to change the status, if I click the confirm button then a warning appears then the status changes to SO Hold di odoo 15. I've tried but it doesn't work. can anyone help me...