from odoo import _, api, fields, models from odoo.exceptions import UserError class DepositDeductionWizard(models.TransientModel): _name = 'deposit.deduction.wizard' _description = 'Security Deposit Deduction' order_id = fields.Many2one( 'sale.order', string="Rental Order", required=True, readonly=True, ) deposit_total = fields.Float( string="Deposit Amount", readonly=True, ) deduction_amount = fields.Float( string="Deduction Amount", required=True, help="Amount to deduct from the security deposit for damages.", ) reason = fields.Text( string="Reason for Deduction", required=True, ) remaining_preview = fields.Float( string="Remaining to Refund", compute='_compute_remaining_preview', ) overage_preview = fields.Float( string="Additional Invoice Amount", compute='_compute_remaining_preview', help="Amount exceeding the deposit that will be invoiced to the customer.", ) @api.depends('deposit_total', 'deduction_amount') def _compute_remaining_preview(self): for wizard in self: diff = wizard.deposit_total - wizard.deduction_amount if diff >= 0: wizard.remaining_preview = diff wizard.overage_preview = 0.0 else: wizard.remaining_preview = 0.0 wizard.overage_preview = abs(diff) def action_confirm_deduction(self): self.ensure_one() if self.deduction_amount <= 0: raise UserError(_("Deduction amount must be greater than zero.")) order = self.order_id order._deduct_security_deposit(self.deduction_amount) order.message_post(body=_( "Security deposit deduction of %s processed.\nReason: %s", self.env['ir.qweb.field.monetary'].value_to_html( self.deduction_amount, {'display_currency': order.currency_id}, ), self.reason, )) return {'type': 'ir.actions.act_window_close'}