feat: Implement historical data archiving for equipment when acquisition year or cost is updated.

rest-api
MrWaradana 1 month ago
parent 151489a642
commit 8f3a74c597

@ -694,30 +694,104 @@ async def update_initial_simulation_data(db_session: DbSession, assetnum: str):
current_acq = eq.acquisition_year current_acq = eq.acquisition_year
current_life = eq.design_life current_life = eq.design_life
current_target = eq.forecasting_target_year current_target = eq.forecasting_target_year
current_acq_cost = eq.acquisition_cost
# If current_target is logically "default", we update it. # If current_target is logically "default", we update it.
# If user changed it to something else, we might want to preserve it? # If user changed it to something else, we might want to preserve it
# User request: "must be default acquisition_year+design_life if not changes" # if it currently holds the default value (based on old acq year).
# We interpret "if not changes" as: if it currently holds the default value (based on old acq year).
is_valid_default = False is_valid_default = False
if current_acq and current_life and current_target: if current_acq and current_life and current_target:
is_valid_default = current_target == (current_acq + current_life) is_valid_default = current_target == (current_acq + current_life)
updates_needed = False # Check for changes
change_year = (eq.acquisition_year != first_year)
# Update acquisition_cost change_cost = (first_cost is not None and eq.acquisition_cost != first_cost)
if change_year or change_cost:
print(f"Acquisition change detected for {assetnum}: Year {current_acq}->{first_year}, Cost {current_acq_cost}->{first_cost}. Archiving history.")
acq_year_ref = f"{current_acq}_{current_target}"
# --- ARCHIVE HISTORICAL DATA ---
# 1. Copy old equipment master data to history
history_ms_query = text("""
INSERT INTO lcc_ms_equipment_historical_data (
id, assetnum, acquisition_year, acquisition_cost, capital_cost_record_time, design_life,
forecasting_start_year, forecasting_target_year, manhours_rate, created_at, created_by,
updated_at, updated_by, min_eac_info, harga_saat_ini, minimum_eac_seq, minimum_eac_year,
minimum_eac, minimum_npv, minimum_pmt, minimum_pmt_aq_cost, minimum_is_actual,
efdh_equivalent_forced_derated_hours, foh_forced_outage_hours, category_no, proportion,
acquisition_year_ref
)
SELECT
uuid_generate_v4(), assetnum, acquisition_year, acquisition_cost, capital_cost_record_time, design_life,
forecasting_start_year, forecasting_target_year, manhours_rate, created_at, created_by,
updated_at, updated_by, min_eac_info, harga_saat_ini, minimum_eac_seq, minimum_eac_year,
minimum_eac, minimum_npv, minimum_pmt, minimum_pmt_aq_cost, minimum_is_actual,
efdh_equivalent_forced_derated_hours, foh_forced_outage_hours, category_no, proportion,
:acq_year_ref
FROM lcc_ms_equipment_data
WHERE 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
# Format: {acquisition_year}_{forecasting_target_year}
history_tr_query = text("""
INSERT INTO lcc_equipment_historical_tr_data (
id, assetnum, tahun, seq, is_actual,
raw_cm_interval, raw_cm_material_cost, raw_cm_labor_time, raw_cm_labor_human,
raw_pm_interval, raw_pm_material_cost, raw_pm_labor_time, raw_pm_labor_human,
raw_oh_interval, raw_oh_material_cost, raw_oh_labor_time, raw_oh_labor_human,
raw_predictive_interval, raw_predictive_material_cost, raw_predictive_labor_time, raw_predictive_labor_human,
raw_project_task_material_cost, raw_loss_output_MW, raw_loss_output_price,
raw_operational_cost, raw_maintenance_cost,
rc_cm_material_cost, rc_cm_labor_cost,
rc_pm_material_cost, rc_pm_labor_cost,
rc_oh_material_cost, rc_oh_labor_cost,
rc_predictive_labor_cost,
rc_project_material_cost, rc_lost_cost, rc_operation_cost, rc_maintenance_cost,
rc_total_cost,
eac_npv, eac_annual_mnt_cost, eac_annual_acq_cost, eac_disposal_cost, eac_eac,
efdh_equivalent_forced_derated_hours, foh_forced_outage_hours,
created_by, created_at, acquisition_year_ref
)
SELECT
uuid_generate_v4(), assetnum, tahun, seq, is_actual,
raw_cm_interval, raw_cm_material_cost, raw_cm_labor_time, raw_cm_labor_human,
raw_pm_interval, raw_pm_material_cost, raw_pm_labor_time, raw_pm_labor_human,
raw_oh_interval, raw_oh_material_cost, raw_oh_labor_time, raw_oh_labor_human,
raw_predictive_interval, raw_predictive_material_cost, raw_predictive_labor_time, raw_predictive_labor_human,
raw_project_task_material_cost, raw_loss_output_MW, raw_loss_output_price,
raw_operational_cost, raw_maintenance_cost,
rc_cm_material_cost, rc_cm_labor_cost,
rc_pm_material_cost, rc_pm_labor_cost,
rc_oh_material_cost, rc_oh_labor_cost,
rc_predictive_labor_cost,
rc_project_material_cost, rc_lost_cost, rc_operation_cost, rc_maintenance_cost,
rc_total_cost,
eac_npv, eac_annual_mnt_cost, eac_annual_acq_cost, eac_disposal_cost, eac_eac,
efdh_equivalent_forced_derated_hours, foh_forced_outage_hours,
created_by, NOW(), :acq_year_ref
FROM lcc_equipment_tr_data
WHERE assetnum = :assetnum
""")
await db_session.execute(history_tr_query, {"acq_year_ref": acq_year_ref, "assetnum": assetnum})
# 3. Delete old data
del_query = text("DELETE FROM lcc_equipment_tr_data WHERE assetnum = :assetnum")
await db_session.execute(del_query, {"assetnum": assetnum})
# Update Equipment Master
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
updates_needed = True
# Update acquisition_year
if eq.acquisition_year != first_year: if eq.acquisition_year != first_year:
eq.acquisition_year = first_year eq.acquisition_year = first_year
updates_needed = True
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
if updates_needed:
await db_session.commit() await db_session.commit()
# await db_session.refresh(eq) # await db_session.refresh(eq)

Loading…
Cancel
Save