ga_asset_management/wizard/asset_transfer_wizard.py
2026-02-04 11:37:09 +07:00

98 lines
5.0 KiB
Python

from odoo import models, fields, api
class GaAssetTransferWizard(models.TransientModel):
_name = 'ga.asset.transfer.wizard'
_description = 'Asset Transfer Wizard'
asset_id = fields.Many2one('account.asset', string='Asset', required=True, readonly=True)
current_company_id = fields.Many2one('res.company', string='Current Company', required=True, readonly=True)
target_company_id = fields.Many2one('res.company', string='Target Company', required=True)
note = fields.Text(string='Transfer Note')
def action_transfer(self):
self.ensure_one()
if self.target_company_id and self.target_company_id != self.current_company_id:
old_asset = self.asset_id
# CASE 1: Asset is Draft or Model -> Simple Move
# No depreciation history to preserve, so we just move it.
if old_asset.state in ('draft', 'model'):
old_asset.sudo().write({'company_id': self.target_company_id.id})
if self.note:
old_asset.sudo().message_post(body=f"Asset transferred to {self.target_company_id.name}. Note: {self.note}")
return {
'name': 'Transferred Asset',
'type': 'ir.actions.act_window',
'res_model': 'account.asset',
'view_mode': 'form',
'res_id': old_asset.id,
'target': 'current',
}
# CASE 2: Asset is Running (Open/Parsed) -> Close and Clone
# We want to stop the old one and start fresh in new company
vals = {
'name': old_asset.name,
'product_id': old_asset.product_id.id,
'asset_code': old_asset.asset_code,
'original_value': old_asset.original_value,
'already_depreciated_amount_import': old_asset.original_value - old_asset.value_residual,
'acquisition_date': old_asset.acquisition_date,
'prorata_date': old_asset.prorata_date,
'method': old_asset.method,
'method_number': old_asset.method_number,
'method_period': old_asset.method_period,
'method_progress_factor': old_asset.method_progress_factor,
'prorata_computation_type': old_asset.prorata_computation_type,
'company_id': self.target_company_id.id,
'state': 'draft',
}
# Find Asset Model in Target Company to apply Accounts
if old_asset.product_id:
# Get Product with company context
product = old_asset.product_id.with_company(self.target_company_id)
account = product.property_account_expense_id or product.categ_id.property_account_expense_categ_id
if account and account.asset_model:
model = account.asset_model
vals['model_id'] = model.id
# Auto-populate Accounts from Model
vals['account_asset_id'] = model.account_asset_id.id
vals['account_depreciation_id'] = model.account_depreciation_id.id
vals['account_depreciation_expense_id'] = model.account_depreciation_expense_id.id
vals['journal_id'] = model.journal_id.id
# 2. Close the old asset WITHOUT generating Disposal Moves
# User request: "no need to use dispose method (gain/loss account)"
# We simply stop future depreciation and archive the old asset.
# NOTE: The Asset Value remains on the old company's Balance Sheet until manually adjusted/transferred.
# Cancel future draft moves (using sudo to bypass company rules if needed)
old_asset.depreciation_move_ids.filtered(lambda m: m.state == 'draft').with_context(force_delete=True).sudo().unlink()
# Close and Archive (using sudo to ensure we can modify the record even if we are switching context)
old_asset.sudo().write({
'state': 'close',
'active': False,
})
old_asset.sudo().message_post(body=f"Asset transferred to {self.target_company_id.name}. Auto-disposal skipped.")
# 3. Create the new asset in target company (using sudo to create in a company we might not be active in)
new_asset = self.env['account.asset'].sudo().create(vals)
new_asset.sudo().message_post(body=f"Asset transferred from {self.current_company_id.name}. Note: {self.note}")
# 4. Open the new asset
return {
'name': 'Transferred Asset',
'type': 'ir.actions.act_window',
'res_model': 'account.asset',
'view_mode': 'form',
'res_id': new_asset.id,
'target': 'current',
}
return {'type': 'ir.actions.act_window_close'}