"""Partial sale wizard (sell a portion of an asset). Splits the asset into a child (the sold portion) and disposes the child; parent retains remaining cost + salvage.""" from odoo import _, api, fields, models from odoo.exceptions import UserError class FusionPartialSaleWizard(models.TransientModel): _name = "fusion.partial.sale.wizard" _description = "Asset Partial Sale Wizard" asset_id = fields.Many2one( 'fusion.asset', required=True, default=lambda self: self._default_asset(), ) company_id = fields.Many2one(related='asset_id.company_id') currency_id = fields.Many2one(related='asset_id.currency_id') cost = fields.Monetary(related='asset_id.cost', readonly=True) book_value = fields.Monetary(related='asset_id.book_value', readonly=True) sold_pct = fields.Float( string='% of cost being sold', default=30.0, help="Percentage of original cost attributed to the sold portion.", ) sold_amount = fields.Monetary(string='Sale Amount', required=True) sale_date = fields.Date(required=True, default=fields.Date.today) sale_partner_id = fields.Many2one('res.partner') estimated_sold_cost = fields.Monetary(compute='_compute_sold_cost') estimated_gain_loss = fields.Monetary(compute='_compute_sold_cost') @api.model def _default_asset(self): ctx = self.env.context if ctx.get('active_model') == 'fusion.asset': return ctx.get('active_id') return False @api.depends('sold_pct', 'sold_amount', 'cost') def _compute_sold_cost(self): for w in self: w.estimated_sold_cost = round(w.cost * (w.sold_pct or 0) / 100, 2) w.estimated_gain_loss = w.sold_amount - w.estimated_sold_cost def action_partial_sell(self): self.ensure_one() if not (0 < self.sold_pct < 100): raise UserError(_("sold_pct must be strictly between 0 and 100.")) if self.asset_id.state == 'disposed': raise UserError(_("Asset already disposed.")) result = self.env['fusion.asset.engine'].partial_sale( self.asset_id, sold_amount=self.sold_amount, sold_qty=self.sold_pct / 100, sale_date=self.sale_date, sale_partner=self.sale_partner_id, ) return { 'type': 'ir.actions.act_window', 'res_model': 'fusion.asset', 'res_id': result['parent_asset_id'], 'view_mode': 'form', 'target': 'current', }