-- Kurva depresiasi khusus flock 4 (house_types=open_house, effective_date=2026-06-03).
-- Override hanya multiplication_percentage; house_type & standard_week diwarisi dari baris global.
-- depreciation_percent diturunkan = (1 - multiplication_percentage) * 100.
-- Lookup engine: ? = ANY(project_flock_ids) — satu baris dipakai semua flock.

-- house_type: open_house
INSERT INTO house_depreciation_standards
    (project_flock_ids, house_type, day, effective_date,
     multiplication_percentage, depreciation_percent, standard_week, name)
SELECT
    ARRAY[4]::bigint[], g.house_type, g.day, DATE '2026-06-03',
    v.mult, (1 - v.mult) * 100, g.standard_week,
    'Custom flocks 4 (eff 2026-06-03)'
FROM (VALUES
    (1, 0.9958::numeric), (2, 0.9958), (3, 0.9958), (4, 0.9958), (5, 0.9958),
    (6, 0.9957), (7, 0.9957), (8, 0.9957), (9, 0.9957), (10, 0.9957),
    (11, 0.9956), (12, 0.9956), (13, 0.9956), (14, 0.9956), (15, 0.9956),
    (16, 0.9955), (17, 0.9955), (18, 0.9955), (19, 0.9955), (20, 0.9955),
    (21, 0.9954), (22, 0.9954), (23, 0.9954), (24, 0.9954), (25, 0.9954),
    (26, 0.9953), (27, 0.9953), (28, 0.9953), (29, 0.9953), (30, 0.9952),
    (31, 0.9952), (32, 0.9952), (33, 0.9952), (34, 0.9952), (35, 0.9951),
    (36, 0.9951), (37, 0.9951), (38, 0.9951), (39, 0.995), (40, 0.995),
    (41, 0.995), (42, 0.995), (43, 0.9949), (44, 0.9949), (45, 0.9949),
    (46, 0.9949), (47, 0.9948), (48, 0.9948), (49, 0.9948), (50, 0.9947),
    (51, 0.9947), (52, 0.9947), (53, 0.9947), (54, 0.9946), (55, 0.9946),
    (56, 0.9955), (57, 0.9955), (58, 0.9954), (59, 0.9954), (60, 0.9954),
    (61, 0.9954), (62, 0.9954), (63, 0.9944), (64, 0.9944), (65, 0.9943),
    (66, 0.9943), (67, 0.9943), (68, 0.9942), (69, 0.9942), (70, 0.9942),
    (71, 0.9941), (72, 0.9941), (73, 0.9941), (74, 0.994), (75, 0.994),
    (76, 0.994), (77, 0.9939), (78, 0.9939), (79, 0.9938), (80, 0.9938),
    (81, 0.9938), (82, 0.9937), (83, 0.9937), (84, 0.9937), (85, 0.9936),
    (86, 0.9936), (87, 0.9935), (88, 0.9935), (89, 0.9934), (90, 0.9934),
    (91, 0.9945), (92, 0.9944), (93, 0.9944), (94, 0.9944), (95, 0.9943),
    (96, 0.9943), (97, 0.9943), (98, 0.9931), (99, 0.993), (100, 0.993),
    (101, 0.9929), (102, 0.9929), (103, 0.9928), (104, 0.9928), (105, 0.9939),
    (106, 0.9939), (107, 0.9939), (108, 0.9938), (109, 0.9938), (110, 0.9938),
    (111, 0.9937), (112, 0.9924), (113, 0.9924), (114, 0.9923), (115, 0.9922),
    (116, 0.9922), (117, 0.9921), (118, 0.9921), (119, 0.992), (120, 0.9919),
    (121, 0.9919), (122, 0.9918), (123, 0.9917), (124, 0.9917), (125, 0.9916),
    (126, 0.9929), (127, 0.9929), (128, 0.9928), (129, 0.9928), (130, 0.9927),
    (131, 0.9927), (132, 0.9926), (133, 0.9911), (134, 0.991), (135, 0.9909),
    (136, 0.9908), (137, 0.9907), (138, 0.9907), (139, 0.9906), (140, 0.9921),
    (141, 0.992), (142, 0.9919), (143, 0.9919), (144, 0.9918), (145, 0.9917),
    (146, 0.9917), (147, 0.9916), (148, 0.9915), (149, 0.9915), (150, 0.9914),
    (151, 0.9913), (152, 0.9912), (153, 0.9912), (154, 0.9893), (155, 0.9892),
    (156, 0.9891), (157, 0.9889), (158, 0.9888), (159, 0.9887), (160, 0.9885),
    (161, 0.9903), (162, 0.9903), (163, 0.9902), (164, 0.9901), (165, 0.99),
    (166, 0.9899), (167, 0.9898), (168, 0.9896), (169, 0.9895), (170, 0.9894),
    (171, 0.9893), (172, 0.9892), (173, 0.9891), (174, 0.989), (175, 0.9888),
    (176, 0.9887), (177, 0.9886), (178, 0.9885), (179, 0.9883), (180, 0.9882),
    (181, 0.988), (182, 0.9855), (183, 0.9853), (184, 0.985), (185, 0.9848),
    (186, 0.9846), (187, 0.9843), (188, 0.9841), (189, 0.9865), (190, 0.9863),
    (191, 0.9861), (192, 0.986), (193, 0.9858), (194, 0.9855), (195, 0.9853),
    (196, 0.9851), (197, 0.9849), (198, 0.9847), (199, 0.9844), (200, 0.9842),
    (201, 0.9839), (202, 0.9837), (203, 0.9834), (204, 0.9831), (205, 0.9828),
    (206, 0.9825), (207, 0.9822), (208, 0.9819), (209, 0.9815), (210, 0.9812),
    (211, 0.9808), (212, 0.9805), (213, 0.9801), (214, 0.9797), (215, 0.9793),
    (216, 0.9788), (217, 0.9784), (218, 0.9779), (219, 0.9774), (220, 0.9769),
    (221, 0.9763), (222, 0.9757), (223, 0.9751), (224, 0.9745), (225, 0.9738),
    (226, 0.9731), (227, 0.9724), (228, 0.9716), (229, 0.9708), (230, 0.9699),
    (231, 0.9752), (232, 0.9745), (233, 0.9739), (234, 0.9732), (235, 0.9724),
    (236, 0.9716), (237, 0.9708), (238, 0.9624), (239, 0.9609), (240, 0.9593),
    (241, 0.9576), (242, 0.9558), (243, 0.9537), (244, 0.9515), (245, 0.949),
    (246, 0.9462), (247, 0.9432), (248, 0.9398), (249, 0.9359), (250, 0.9315),
    (251, 0.9265), (252, 0.9206), (253, 0.9138), (254, 0.9057), (255, 0.8958),
    (256, 0.8837), (257, 0.8684), (258, 0.8485), (259, 0.8571), (260, 0.8333),
    (261, 0.8), (262, 0.75), (263, 0.6667), (264, 0.5), (265, 0)
) AS v(day, mult)
JOIN LATERAL (
    SELECT DISTINCT ON (day) house_type, day, standard_week
    FROM house_depreciation_standards
    WHERE project_flock_ids IS NULL
      AND house_type = 'open_house'::house_type_enum
      AND day = v.day
    ORDER BY day, effective_date DESC NULLS LAST
) g ON TRUE;

-- Recompute snapshot depresiasi untuk semua flock yang dipetakan.
DELETE FROM farm_depreciation_snapshots WHERE project_flock_id IN (4);
