@ -301,11 +301,19 @@ async def get_master_by_assetnum(
None ,
None ,
)
)
# Historical data query
# Historical data query - filter to only one reference (prioritize oldest acquisition year)
oldest_ref_subquery = (
Select ( EquipmentHistoricalTransactionRecords . acquisition_year_ref )
. filter ( EquipmentHistoricalTransactionRecords . assetnum == assetnum )
. order_by ( EquipmentHistoricalTransactionRecords . acquisition_year_ref . asc ( ) )
. limit ( 1 )
. scalar_subquery ( )
)
historical_query = (
historical_query = (
Select ( EquipmentHistoricalTransactionRecords )
Select ( EquipmentHistoricalTransactionRecords )
. join ( EquipmentHistoricalTransactionRecords . equipment )
. filter( EquipmentHistoricalTransactionRecords . assetnum == assetnum )
. filter ( Equipment . assetnum == assetnum )
. filter ( Equipment HistoricalTransactionRecords. acquisition_year_ref == oldest_ref_subquery )
. order_by ( EquipmentHistoricalTransactionRecords . tahun . asc ( ) )
. order_by ( EquipmentHistoricalTransactionRecords . tahun . asc ( ) )
)
)
historical_result = await db_session . execute ( historical_query )
historical_result = await db_session . execute ( historical_query )
@ -672,7 +680,7 @@ async def check_and_update_acquisition_data(db_session: DbSession, assetnum: str
if conn :
if conn :
try :
try :
cursor = conn . cursor ( )
cursor = conn . cursor ( )
# Query the first year from wo_maximo
# Query the oldest year from wo_maximo to detect the original acquisition
query = """
query = """
select DATE_PART ( ' year ' , a . reportdate ) AS year , a . asset_replacecost AS cost
select DATE_PART ( ' year ' , a . reportdate ) AS year , a . asset_replacecost AS cost
from wo_maximo a
from wo_maximo a
@ -720,13 +728,20 @@ async def check_and_update_acquisition_data(db_session: DbSession, assetnum: str
change_year = ( eq . acquisition_year != first_year )
change_year = ( eq . acquisition_year != first_year )
change_cost = ( first_cost is not None and eq . acquisition_cost != first_cost )
change_cost = ( first_cost is not None and eq . acquisition_cost != first_cost )
if change_year or change_cost :
# We only archive transaction history if the acquisition year itself changed.
print ( f " Acquisition change detected for { assetnum } : Year { current_acq } -> { first_year } , Cost { current_acq_cost } -> { first_cost } . Archiving history. " )
# This prevents redundant history entries for cost-only updates.
if change_year :
print ( f " Acquisition year change detected for { assetnum } : { current_acq } -> { first_year } . Archiving history. " )
acq_year_ref = f " { current_acq } _ { current_target } "
acq_year_ref = f " { current_acq } _ { current_target } "
# --- ARCHIVE HISTORICAL DATA ---
# --- ARCHIVE HISTORICAL DATA ---
# Check for existing identical archive to prevent duplicates (after calculation failures/retries)
check_hist_query = text ( " SELECT 1 FROM lcc_ms_equipment_historical_data WHERE assetnum = :assetnum AND acquisition_year_ref = :acq_year_ref LIMIT 1 " )
hist_exists = ( await db_session . execute ( check_hist_query , { " assetnum " : assetnum , " acq_year_ref " : acq_year_ref } ) ) . fetchone ( )
if not hist_exists :
# 1. Copy old equipment master data to history
# 1. Copy old equipment master data to history
history_ms_query = text ( """
history_ms_query = text ( """
INSERT INTO lcc_ms_equipment_historical_data (
INSERT INTO lcc_ms_equipment_historical_data (
@ -750,8 +765,6 @@ async def check_and_update_acquisition_data(db_session: DbSession, assetnum: str
await db_session . execute ( history_ms_query , { " acq_year_ref " : acq_year_ref , " assetnum " : assetnum } )
await db_session . execute ( history_ms_query , { " acq_year_ref " : acq_year_ref , " assetnum " : assetnum } )
# 2. Copy old transaction data to lcc_equipment_historical_tr_data
# 2. Copy old transaction data to lcc_equipment_historical_tr_data
# Format: {acquisition_year}_{forecasting_target_year}
history_tr_query = text ( """
history_tr_query = text ( """
INSERT INTO lcc_equipment_historical_tr_data (
INSERT INTO lcc_equipment_historical_tr_data (
id , assetnum , tahun , seq , is_actual ,
id , assetnum , tahun , seq , is_actual ,
@ -797,12 +810,14 @@ async def check_and_update_acquisition_data(db_session: DbSession, assetnum: str
del_query = text ( " DELETE FROM lcc_equipment_tr_data WHERE assetnum = :assetnum " )
del_query = text ( " DELETE FROM lcc_equipment_tr_data WHERE assetnum = :assetnum " )
await db_session . execute ( del_query , { " assetnum " : assetnum } )
await db_session . execute ( del_query , { " assetnum " : assetnum } )
# Update Equipment Master
# Update Equipment Master regardless of if archive was needed/skipped
if change_year or change_cost :
if first_cost is not None and eq . acquisition_cost != first_cost :
if first_cost is not None and eq . acquisition_cost != first_cost :
eq . acquisition_cost = first_cost
eq . acquisition_cost = first_cost
if eq . acquisition_year != first_year :
if eq . acquisition_year != first_year :
eq . acquisition_year = first_year
eq . acquisition_year = first_year
eq . forecasting_start_year = first_year # Align start with acquisition
if is_valid_default and current_life :
if is_valid_default and current_life :
eq . forecasting_target_year = first_year + current_life
eq . forecasting_target_year = first_year + current_life