-- Kurva depresiasi khusus flock 18 (house_type=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.

INSERT INTO house_depreciation_standards
    (project_flock_ids, house_type, day, effective_date,
     multiplication_percentage, depreciation_percent, standard_week, name)
SELECT
    ARRAY[18,14,12,15,9,19]::bigint[], g.house_type, g.day, DATE '2026-06-03',
    v.mult, (1 - v.mult) * 100, g.standard_week,
    'Custom flocks 18,14,12,15,9,19 (eff 2026-06-03)'
FROM (VALUES
    (1, 0.9976::numeric), (2, 0.9976), (3, 0.9976), (4, 0.9976), (5, 0.9976),
    (6, 0.9976), (7, 0.9976), (8, 0.9976), (9, 0.9976), (10, 0.9976),
    (11, 0.9976), (12, 0.9976), (13, 0.9976), (14, 0.9975), (15, 0.9979),
    (16, 0.9979), (17, 0.9979), (18, 0.9979), (19, 0.9979), (20, 0.9979),
    (21, 0.9979), (22, 0.9975), (23, 0.9975), (24, 0.9975), (25, 0.9975),
    (26, 0.9975), (27, 0.9975), (28, 0.9975), (29, 0.9975), (30, 0.9975),
    (31, 0.9974), (32, 0.9974), (33, 0.9974), (34, 0.9974), (35, 0.9974),
    (36, 0.9974), (37, 0.9974), (38, 0.9974), (39, 0.9974), (40, 0.9974),
    (41, 0.9974), (42, 0.9974), (43, 0.9977), (44, 0.9977), (45, 0.9977),
    (46, 0.9977), (47, 0.9977), (48, 0.9977), (49, 0.9977), (50, 0.9973),
    (51, 0.9973), (52, 0.9973), (53, 0.9973), (54, 0.9973), (55, 0.9973),
    (56, 0.9973), (57, 0.9973), (58, 0.9973), (59, 0.9973), (60, 0.9973),
    (61, 0.9972), (62, 0.9972), (63, 0.9972), (64, 0.9976), (65, 0.9976),
    (66, 0.9976), (67, 0.9976), (68, 0.9976), (69, 0.9976), (70, 0.9976),
    (71, 0.9972), (72, 0.9972), (73, 0.9972), (74, 0.9972), (75, 0.9971),
    (76, 0.9971), (77, 0.9971), (78, 0.9975), (79, 0.9975), (80, 0.9975),
    (81, 0.9975), (82, 0.9975), (83, 0.9975), (84, 0.9975), (85, 0.9971),
    (86, 0.9971), (87, 0.9971), (88, 0.997), (89, 0.997), (90, 0.997),
    (91, 0.997), (92, 0.997), (93, 0.997), (94, 0.997), (95, 0.997),
    (96, 0.997), (97, 0.997), (98, 0.997), (99, 0.9974), (100, 0.9974),
    (101, 0.9974), (102, 0.9974), (103, 0.9974), (104, 0.9973), (105, 0.9973),
    (106, 0.9969), (107, 0.9969), (108, 0.9969), (109, 0.9969), (110, 0.9968),
    (111, 0.9968), (112, 0.9968), (113, 0.9973), (114, 0.9973), (115, 0.9973),
    (116, 0.9972), (117, 0.9972), (118, 0.9972), (119, 0.9972), (120, 0.9968),
    (121, 0.9967), (122, 0.9967), (123, 0.9967), (124, 0.9967), (125, 0.9967),
    (126, 0.9967), (127, 0.9972), (128, 0.9971), (129, 0.9971), (130, 0.9971),
    (131, 0.9971), (132, 0.9971), (133, 0.9971), (134, 0.9966), (135, 0.9966),
    (136, 0.9966), (137, 0.9966), (138, 0.9966), (139, 0.9966), (140, 0.9965),
    (141, 0.997), (142, 0.997), (143, 0.997), (144, 0.997), (145, 0.997),
    (146, 0.997), (147, 0.997), (148, 0.9965), (149, 0.9964), (150, 0.9964),
    (151, 0.9964), (152, 0.9964), (153, 0.9964), (154, 0.9964), (155, 0.9969),
    (156, 0.9969), (157, 0.9969), (158, 0.9969), (159, 0.9968), (160, 0.9968),
    (161, 0.9968), (162, 0.9968), (163, 0.9968), (164, 0.9968), (165, 0.9968),
    (166, 0.9968), (167, 0.9968), (168, 0.9968), (169, 0.9962), (170, 0.9962),
    (171, 0.9962), (172, 0.9962), (173, 0.9961), (174, 0.9961), (175, 0.9961),
    (176, 0.9967), (177, 0.9966), (178, 0.9966), (179, 0.9966), (180, 0.9966),
    (181, 0.9966), (182, 0.9966), (183, 0.9966), (184, 0.9966), (185, 0.9965),
    (186, 0.9965), (187, 0.9965), (188, 0.9965), (189, 0.9965), (190, 0.9959),
    (191, 0.9959), (192, 0.9959), (193, 0.9959), (194, 0.9958), (195, 0.9958),
    (196, 0.9958), (197, 0.9964), (198, 0.9964), (199, 0.9964), (200, 0.9963),
    (201, 0.9963), (202, 0.9963), (203, 0.9963), (204, 0.9963), (205, 0.9963),
    (206, 0.9963), (207, 0.9962), (208, 0.9962), (209, 0.9962), (210, 0.9962),
    (211, 0.9962), (212, 0.9962), (213, 0.9962), (214, 0.9961), (215, 0.9961),
    (216, 0.9961), (217, 0.9961), (218, 0.9961), (219, 0.9961), (220, 0.9961),
    (221, 0.996), (222, 0.996), (223, 0.996), (224, 0.996), (225, 0.9953),
    (226, 0.9953), (227, 0.9953), (228, 0.9952), (229, 0.9952), (230, 0.9952),
    (231, 0.9952), (232, 0.9958), (233, 0.9958), (234, 0.9958), (235, 0.9958),
    (236, 0.9958), (237, 0.9958), (238, 0.9957), (239, 0.9957), (240, 0.9957),
    (241, 0.9957), (242, 0.9957), (243, 0.9956), (244, 0.9956), (245, 0.9956),
    (246, 0.9956), (247, 0.9956), (248, 0.9955), (249, 0.9955), (250, 0.9955),
    (251, 0.9955), (252, 0.9955), (253, 0.9954), (254, 0.9954), (255, 0.9954),
    (256, 0.9954), (257, 0.9954), (258, 0.9953), (259, 0.9953), (260, 0.9953),
    (261, 0.9953), (262, 0.9952), (263, 0.9952), (264, 0.9952), (265, 0.9952),
    (266, 0.9952), (267, 0.9951), (268, 0.9951), (269, 0.9951), (270, 0.9951),
    (271, 0.995), (272, 0.995), (273, 0.995), (274, 0.995), (275, 0.9949),
    (276, 0.9949), (277, 0.9949), (278, 0.9949), (279, 0.9948), (280, 0.9948),
    (281, 0.9948), (282, 0.9947), (283, 0.9947), (284, 0.9947), (285, 0.9947),
    (286, 0.9946), (287, 0.9946), (288, 0.9955), (289, 0.9955), (290, 0.9954),
    (291, 0.9954), (292, 0.9954), (293, 0.9954), (294, 0.9954), (295, 0.9944),
    (296, 0.9944), (297, 0.9943), (298, 0.9943), (299, 0.9943), (300, 0.9942),
    (301, 0.9942), (302, 0.9942), (303, 0.9941), (304, 0.9941), (305, 0.9941),
    (306, 0.994), (307, 0.994), (308, 0.994), (309, 0.9939), (310, 0.9939),
    (311, 0.9938), (312, 0.9938), (313, 0.9938), (314, 0.9937), (315, 0.9937),
    (316, 0.9937), (317, 0.9936), (318, 0.9936), (319, 0.9935), (320, 0.9935),
    (321, 0.9934), (322, 0.9934), (323, 0.9945), (324, 0.9944), (325, 0.9944),
    (326, 0.9944), (327, 0.9943), (328, 0.9943), (329, 0.9943), (330, 0.9931),
    (331, 0.993), (332, 0.993), (333, 0.9929), (334, 0.9929), (335, 0.9928),
    (336, 0.9928), (337, 0.9939), (338, 0.9939), (339, 0.9939), (340, 0.9938),
    (341, 0.9938), (342, 0.9938), (343, 0.9937), (344, 0.9924), (345, 0.9924),
    (346, 0.9923), (347, 0.9922), (348, 0.9922), (349, 0.9921), (350, 0.9921),
    (351, 0.992), (352, 0.9919), (353, 0.9919), (354, 0.9918), (355, 0.9917),
    (356, 0.9917), (357, 0.9916), (358, 0.9929), (359, 0.9929), (360, 0.9928),
    (361, 0.9928), (362, 0.9927), (363, 0.9927), (364, 0.9926), (365, 0.9911),
    (366, 0.991), (367, 0.9909), (368, 0.9908), (369, 0.9907), (370, 0.9907),
    (371, 0.9906), (372, 0.9921), (373, 0.992), (374, 0.9919), (375, 0.9919),
    (376, 0.9918), (377, 0.9917), (378, 0.9917), (379, 0.9916), (380, 0.9915),
    (381, 0.9915), (382, 0.9914), (383, 0.9913), (384, 0.9912), (385, 0.9912),
    (386, 0.9893), (387, 0.9892), (388, 0.9891), (389, 0.9889), (390, 0.9888),
    (391, 0.9887), (392, 0.9885), (393, 0.9903), (394, 0.9903), (395, 0.9902),
    (396, 0.9901), (397, 0.99), (398, 0.9899), (399, 0.9898), (400, 0.9896),
    (401, 0.9895), (402, 0.9894), (403, 0.9893), (404, 0.9892), (405, 0.9891),
    (406, 0.989), (407, 0.9888), (408, 0.9887), (409, 0.9886), (410, 0.9885),
    (411, 0.9883), (412, 0.9882), (413, 0.988), (414, 0.9855), (415, 0.9853),
    (416, 0.985), (417, 0.9848), (418, 0.9846), (419, 0.9843), (420, 0.9841),
    (421, 0.9865), (422, 0.9863), (423, 0.9861), (424, 0.986), (425, 0.9858),
    (426, 0.9855), (427, 0.9853), (428, 0.9851), (429, 0.9849), (430, 0.9847),
    (431, 0.9844), (432, 0.9842), (433, 0.9839), (434, 0.9837), (435, 0.9834),
    (436, 0.9831), (437, 0.9828), (438, 0.9825), (439, 0.9822), (440, 0.9819),
    (441, 0.9815), (442, 0.9812), (443, 0.9808), (444, 0.9805), (445, 0.9801),
    (446, 0.9797), (447, 0.9793), (448, 0.9788), (449, 0.9784), (450, 0.9779),
    (451, 0.9774), (452, 0.9769), (453, 0.9763), (454, 0.9757), (455, 0.9751),
    (456, 0.9745), (457, 0.9738), (458, 0.9731), (459, 0.9724), (460, 0.9716),
    (461, 0.9708), (462, 0.9699), (463, 0.9752), (464, 0.9745), (465, 0.9739),
    (466, 0.9732), (467, 0.9724), (468, 0.9716), (469, 0.9708), (470, 0.9624),
    (471, 0.9609), (472, 0.9593), (473, 0.9576), (474, 0.9558), (475, 0.9537),
    (476, 0.9515), (477, 0.949), (478, 0.9462), (479, 0.9432), (480, 0.9398),
    (481, 0.9359), (482, 0.9315), (483, 0.9265), (484, 0.9206), (485, 0.9138),
    (486, 0.9057), (487, 0.8958), (488, 0.8837), (489, 0.8684), (490, 0.8485),
    (491, 0.8571), (492, 0.8333), (493, 0.8), (494, 0.75), (495, 0.6667),
    (496, 0.5), (497, 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 (18, 14, 12, 15, 9, 19);
