import logging from datetime import timedelta from odoo import _, api, fields, models from odoo.exceptions import UserError _logger = logging.getLogger(__name__) class ManualRenewalWizard(models.TransientModel): _name = 'manual.renewal.wizard' _description = 'Manual Rental Renewal Wizard' order_id = fields.Many2one( 'sale.order', string="Rental Order", required=True, readonly=True, ) partner_id = fields.Many2one( related='order_id.partner_id', string="Customer", ) current_start_date = fields.Datetime( string="Current Start Date", readonly=True, ) current_return_date = fields.Datetime( string="Current Return Date", readonly=True, ) new_start_date = fields.Datetime( string="New Start Date", required=True, ) new_return_date = fields.Datetime( string="New Return Date", required=True, ) amount_preview = fields.Float( string="Estimated Renewal Amount", compute='_compute_amount_preview', ) @api.depends('order_id', 'new_start_date', 'new_return_date') def _compute_amount_preview(self): for wizard in self: if wizard.order_id: wizard.amount_preview = wizard.order_id._get_renewal_amount() else: wizard.amount_preview = 0.0 def action_confirm_renewal(self): """Confirm the manual renewal: extend dates, invoice, and collect payment.""" self.ensure_one() order = self.order_id if not order.is_rental_order: raise UserError(_("This is not a rental order.")) if self.new_return_date <= self.new_start_date: raise UserError(_("New return date must be after the new start date.")) old_start = order.rental_start_date old_return = order.rental_return_date order.write({ 'rental_start_date': self.new_start_date, 'rental_return_date': self.new_return_date, }) order._recompute_rental_prices() invoice = order._create_renewal_invoice() if invoice: invoice.action_post() renewal_log = self.env['rental.renewal.log'].create({ 'order_id': order.id, 'renewal_number': order.rental_renewal_count + 1, 'previous_start_date': old_start, 'previous_return_date': old_return, 'new_start_date': self.new_start_date, 'new_return_date': self.new_return_date, 'invoice_id': invoice.id if invoice else False, 'renewal_type': 'manual', 'state': 'done', 'payment_status': 'pending', }) order.write({ 'rental_renewal_count': order.rental_renewal_count + 1, 'rental_reminder_sent': False, }) order._send_renewal_confirmation_email(renewal_log, False) if invoice: inv = invoice.with_user(self.env.uid) return inv.action_open_poynt_payment_wizard() return {'type': 'ir.actions.act_window_close'}