erp_be/scripts/patch-assets-salvage-percentage.sql

28 lines
869 B
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Rename depreciation method OTHER → CUSTOM; add salvage_percentage
-- Run on each environment after deploy.
ALTER TABLE assets
ADD COLUMN IF NOT EXISTS salvage_percentage DECIMAL(8, 4) DEFAULT 0;
COMMENT ON COLUMN assets.salvage_percentage IS 'Salvage as % of purchase_cost (0100). Synced with salvage_value.';
COMMENT ON COLUMN assets.depreciation_method IS 'SLM, WDV, CUSTOM';
UPDATE assets
SET depreciation_method = 'CUSTOM'
WHERE depreciation_method = 'OTHER'
AND deleted_at IS NULL;
UPDATE assets
SET salvage_percentage = ROUND(
(COALESCE(salvage_value, 0) / purchase_cost) * 100,
4
)
WHERE purchase_cost > 0
AND (salvage_percentage IS NULL OR salvage_percentage = 0)
AND COALESCE(salvage_value, 0) > 0
AND deleted_at IS NULL;
UPDATE item_categories
SET default_depreciation_method = 'CUSTOM'
WHERE default_depreciation_method = 'OTHER';