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