120 lines
5.9 KiB
Python
120 lines
5.9 KiB
Python
from odoo import models, fields, api, _
|
|
from odoo.exceptions import UserError
|
|
|
|
class GaAssetTransferLog(models.Model):
|
|
_name = 'ga.asset.transfer.log'
|
|
_description = 'Asset Transfer Log'
|
|
_order = 'create_date desc'
|
|
_inherit = ['mail.thread', 'mail.activity.mixin']
|
|
|
|
name = fields.Char(string='Transfer Reference', required=True, copy=False, readonly=True, default=lambda self: _('New'))
|
|
asset_id = fields.Many2one('account.asset', string='Asset', required=True, readonly=True, help="Asset being transferred.")
|
|
source_company_id = fields.Many2one('res.company', string='Source Company', required=True, readonly=True)
|
|
target_company_id = fields.Many2one('res.company', string='Target Company', required=True, readonly=True)
|
|
user_id = fields.Many2one('res.users', string='Requested By', required=True, readonly=True, default=lambda self: self.env.user)
|
|
transfer_date = fields.Date(string='Request Date', default=fields.Date.context_today, readonly=True)
|
|
|
|
note = fields.Text(string='Transfer Note', readonly=True)
|
|
|
|
state = fields.Selection([
|
|
('draft', 'Draft'),
|
|
('transit', 'In Transit'),
|
|
('done', 'Done'),
|
|
('cancel', 'Cancelled')
|
|
], string='Status', default='draft', tracking=True, copy=False)
|
|
|
|
@api.model
|
|
def create(self, vals):
|
|
if vals.get('name', _('New')) == _('New'):
|
|
vals['name'] = self.env['ir.sequence'].next_by_code('ga.asset.transfer.log') or _('New')
|
|
return super(GaAssetTransferLog, self).create(vals)
|
|
|
|
def action_validate(self):
|
|
self.ensure_one()
|
|
if self.state != 'transit':
|
|
raise UserError(_("You can only validate transfers that are currently In Transit."))
|
|
|
|
# Check if user is allowed to validate (must be in target company or have access)
|
|
# Assuming if they can see the button and access the record rules allow it, but let's double check company context
|
|
if self.env.company != self.target_company_id:
|
|
raise UserError(_("You must be logged into the Target Company (%s) to validate this transfer.", self.target_company_id.name))
|
|
|
|
# Perform the logic that was previously in the wizard
|
|
old_asset = self.asset_id
|
|
target_company = self.target_company_id
|
|
|
|
# Logic adapted from original wizard
|
|
if old_asset.state in ('draft', 'model'):
|
|
old_asset.sudo().write({'company_id': target_company.id})
|
|
old_asset.sudo().message_post(body=f"Asset transfer validated by {self.env.user.name}. Received in {target_company.name}.")
|
|
else:
|
|
# Running Asset -> Close and Clone
|
|
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': target_company.id,
|
|
'location_id': old_asset.location_id.id, # Copy location? Maybe not if it's new company. But user surely wants to update it later.
|
|
'employee_id': old_asset.employee_id.id, # Keep employee if moving with them?
|
|
'state': 'draft',
|
|
}
|
|
|
|
# Find Asset Model in Target Company
|
|
if old_asset.product_id:
|
|
product = old_asset.product_id.with_company(target_company)
|
|
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
|
|
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
|
|
|
|
# Close Old Asset
|
|
old_asset.depreciation_move_ids.filtered(lambda m: m.state == 'draft').with_context(force_delete=True).sudo().unlink()
|
|
old_asset.sudo().write({
|
|
'state': 'close',
|
|
'active': False,
|
|
'in_transit': False # No longer in transit
|
|
})
|
|
old_asset.sudo().message_post(body=f"Asset transfer validated. Closed and moved to {target_company.name}.")
|
|
|
|
# Create New Asset
|
|
new_asset = self.env['account.asset'].sudo().create(vals)
|
|
new_asset.sudo().message_post(body=f"Asset received from {self.source_company_id.name}. Transfer Ref: {self.name}")
|
|
|
|
self.write({'state': 'done'})
|
|
|
|
# Unlock old asset just in case (if it was simple move)
|
|
if old_asset.id: # If simple move, old_asset is still the active one
|
|
old_asset.sudo().write({'in_transit': False})
|
|
|
|
return {
|
|
'type': 'ir.actions.client',
|
|
'tag': 'display_notification',
|
|
'params': {
|
|
'title': _('Success'),
|
|
'message': _('Asset has been successfully transferred.'),
|
|
'sticky': False,
|
|
}
|
|
}
|
|
|
|
def action_cancel(self):
|
|
self.ensure_one()
|
|
if self.state == 'done':
|
|
raise UserError(_("You cannot cancel a completed transfer."))
|
|
|
|
self.asset_id.sudo().write({'in_transit': False})
|
|
self.write({'state': 'cancel'})
|