ga_asset_management/tests/test_ga_asset.py
2026-02-04 11:37:09 +07:00

132 lines
5.0 KiB
Python

from odoo.tests.common import TransactionCase
from datetime import datetime
from odoo.tests import tagged
@tagged('post_install', '-at_install')
class TestGAAssetManagement(TransactionCase):
def setUp(self):
super(TestGAAssetManagement, self).setUp()
self.Asset = self.env['account.asset']
self.Product = self.env['product.product']
self.Wizard = self.env['ga.asset.transfer.wizard']
self.Company = self.env['res.company']
self.StockPicking = self.env['stock.picking']
self.PurchaseOrder = self.env['purchase.order']
self.AccountMove = self.env['account.move']
# Create a product with barcode
self.product_with_barcode = self.Product.create({
'name': 'Test Product Barcode',
'barcode': '123456789',
'type': 'product', # Storable generic
})
# Create a product without barcode
self.product_no_barcode = self.Product.create({
'name': 'Test Product No Barcode',
'type': 'product',
})
self.company_a = self.env.user.company_id
self.company_b = self.Company.create({'name': 'Company B'})
self.partner = self.env['res.partner'].create({'name': 'Vendor A'})
def test_asset_creation_with_barcode(self):
"""Test asset code generation with product barcode"""
asset = self.Asset.create({
'name': 'Asset Barcode',
'product_id': self.product_with_barcode.id,
'original_value': 1000,
'acquisition_date': '2025-01-01',
'company_id': self.company_a.id,
})
year = datetime.now().year
self.assertTrue(asset.asset_code.startswith(f"123456789/{year}"))
def test_asset_creation_no_barcode(self):
"""Test asset code generation without product barcode (fallback to AST)"""
asset = self.Asset.create({
'name': 'Asset No Barcode',
'product_id': self.product_no_barcode.id,
'original_value': 1000,
'acquisition_date': '2025-01-01',
'company_id': self.company_a.id,
})
year = datetime.now().year
self.assertTrue(asset.asset_code.startswith(f"AST/{year}"))
def test_asset_transfer(self):
"""Test asset transfer wizard"""
asset = self.Asset.create({
'name': 'Asset Transfer',
'original_value': 1000,
'acquisition_date': '2025-01-01',
'company_id': self.company_a.id,
})
wizard = self.Wizard.create({
'asset_id': asset.id,
'current_company_id': self.company_a.id,
'target_company_id': self.company_b.id,
'note': 'Moving to Company B'
})
wizard.action_transfer()
self.assertEqual(asset.company_id, self.company_b)
def test_receipt_to_asset_flow(self):
"""Test PO -> Receipt -> Asset Gen -> Validation -> Asset Created -> Bill -> Link"""
# 1. Create PO
po = self.PurchaseOrder.create({
'partner_id': self.partner.id,
'order_line': [(0, 0, {
'product_id': self.product_with_barcode.id,
'name': 'Laptop',
'product_qty': 1.0,
'price_unit': 1500.0,
})]
})
po.button_confirm()
# 2. Get Receipt
picking = po.picking_ids[0]
self.assertEqual(picking.state, 'assigned')
# 3. Generate Asset Codes
picking.action_generate_asset_codes()
move = picking.move_ids_without_package[0]
year = datetime.now().year
self.assertTrue(move.asset_code.startswith(f"123456789/{year}"))
# 4. Validate Receipt
picking.button_validate()
self.assertEqual(picking.state, 'done')
# 5. Check Asset Creation
self.assertTrue(move.asset_id, "Asset should be linked to Stock Move")
asset = move.asset_id
self.assertEqual(asset.asset_code, move.asset_code)
self.assertEqual(asset.state, 'draft')
# 6. Create Vendor Bill from PO
action = po.action_create_invoice()
invoice = self.AccountMove.browse(action['res_id'])
# 7. Post Bill (Triggers _auto_create_asset which we overrode)
invoice.action_post()
# 8. Verify Link (Invoice Line should be linked to the SAME asset)
inv_line = invoice.invoice_line_ids[0]
# inv_line.asset_ids is Many2many
self.assertIn(asset, inv_line.asset_ids, "Bill line should be linked to the existing asset from Receipt")
# Ensure no duplicate asset created
assets_for_this_product = self.Asset.search([('product_id', '=', self.product_with_barcode.id)])
# We might have other tests creating assets, so filter by code
assets_for_this_code = self.Asset.search([('asset_code', '=', move.asset_code)])
self.assertEqual(len(assets_for_this_code), 1, "Should be exactly one asset for this code")