summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/sale_order.py
blob: 3382d95b8210d6a5f572220569ef72a7d571f8d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
from re import search

from odoo import fields, models, api, _
from odoo.exceptions import UserError, ValidationError
from datetime import datetime, timedelta, timezone, time
import logging, random, string, requests, math, json, re, qrcode, base64
import pytz
from io import BytesIO
from collections import defaultdict
import pytz
from lxml import etree

_logger = logging.getLogger(__name__)


class CancelReasonOrder(models.TransientModel):
    _name = 'cancel.reason.order'
    _description = 'Wizard for Cancel Reason order'

    request_id = fields.Many2one('sale.order', string='Request')
    reason_cancel = fields.Selection([
        ('harga_terlalu_mahal', 'Harga barang terlalu mahal'),
        ('harga_web_tidak_valid', 'Harga web tidak valid'),
        ('stok_kosong', 'Stock kosong'),
        ('tidak_mau_indent', 'Customer tidak mau indent'),
        ('batal_rencana_pembelian', 'Customer membatalkan rencana pembelian'),
        ('vendor_tidak_support_demo', 'Vendor tidak support demo/trial product'),
        ('product_knowledge_kurang', 'Product knowledge kurang baik'),
        ('barang_tidak_sesuai', 'Barang tidak sesuai/tepat'),
        ('tidak_sepakat_pembayaran', 'Tidak menemukan kesepakatan untuk pembayaran'),
        ('dokumen_tidak_support', 'Indoteknik tidak bisa support document yang dibutuhkan (Ex: TKDN, COO, SNI)'),
        ('ganti_quotation', 'Ganti Quotation'),
        ('testing_internal', 'Testing Internal'),
        ('revisi_data', 'Revisi Data'),
    ], string='Reason for Cancel', required=True, copy=False, index=True, tracking=3)
    attachment_bukti = fields.Many2many(
        'ir.attachment',
        string="Attachment Bukti", readonly=False,
        tracking=3, required=True
    )
    nomor_so_pengganti = fields.Char(string='Nomor SO Pengganti', copy=False, tracking=3)

    def confirm_reject(self):
        order = self.request_id
        if order:
            order.write({'reason_cancel': self.reason_cancel})
            if not self.attachment_bukti:
                raise UserError('Attachment bukti wajib disertakan')
            order.write({'attachment_bukti': self.attachment_bukti})
            order.message_post(body='Attachment Bukti Cancel',
                               attachment_ids=[self.attachment_bukti.id])
            if self.reason_cancel == 'ganti_quotation':
                if self.nomor_so_pengganti:
                    order.write({'nomor_so_pengganti': self.nomor_so_pengganti})
                else:
                    raise UserError('Nomor SO pengganti wajib disertakan')
            order.confirm_cancel_order()

        return {'type': 'ir.actions.act_window_close'}


class ShippingOption(models.Model):
    _name = "shipping.option"
    _description = "Shipping Option"

    name = fields.Char(string="Option Name", required=True)
    price = fields.Float(string="Price", required=True)
    provider = fields.Char(string="Provider")
    etd = fields.Char(string="Estimated Delivery Time")
    courier_service_code = fields.Char(string="Courier Service Code")
    sale_order_id = fields.Many2one('sale.order', string="Sale Order", ondelete="cascade")


class SaleOrderLine(models.Model):
    _inherit = 'sale.order.line'

    def unlink(self):
        
        lines_to_reject = []
        for line in self:
            if line.order_id:
                now = fields.Datetime.now()

                initial_reason = "Product Rejected"

                # Buat lognote untuk product yang di delete
                log_note = (f"<li>Product '{line.product_id.name}' rejected. </li>"
                            f"<li>Quantity: {line.product_uom_qty}, </li>"
                            f"<li>Date: {now.strftime('%d-%m-%Y')}, </li>"
                            f"<li>Time: {now.strftime('%H:%M:%S')} </li>"
                            f"<li>Reason reject: {initial_reason} </li>")

                lines_to_reject.append({
                    'sale_order_id': line.order_id.id,
                    'product_id': line.product_id.id,
                    'qty_reject': line.product_uom_qty,
                    'reason_reject': initial_reason,  # pesan reason reject
                    'message_body': log_note,
                    'order_id': line.order_id,
                })

        # Call the original unlink method
        result = super(SaleOrderLine, self).unlink()

        # After deletion, create reject lines and post messages
        SalesOrderReject = self.env['sales.order.reject']
        for reject_data in lines_to_reject:
            # Buat line baru di reject line
            SalesOrderReject.create({
                'sale_order_id': reject_data['sale_order_id'],
                'product_id': reject_data['product_id'],
                'qty_reject': reject_data['qty_reject'],
                'reason_reject': reject_data['reason_reject'],
            })

            # Post to chatter with a more prominent message
            reject_data['order_id'].message_post(
                body=reject_data['message_body'],
                author_id=self.env.user.partner_id.id,  # menampilkan pesan di lognote sebagai current user
            )

        return result


class SaleOrder(models.Model):
    _inherit = "sale.order"

    ccm_id = fields.Many2one('tukar.guling', string='Doc. CCM', readonly=True, compute='_has_ccm', copy=False)
    ongkir_ke_xpdc = fields.Float(string='Ongkir ke Ekspedisi', help='Biaya ongkir ekspedisi', copy=False, index=True,
                                  tracking=3)

    metode_kirim_ke_xpdc = fields.Selection([
        ('indoteknik_deliv', 'Indoteknik Delivery'),
        ('lalamove', 'Lalamove'),
        ('grab', 'Grab'),
        ('gojek', 'Gojek'),
        ('deliveree', 'Deliveree'),
        ('other', 'Other'),
    ], string='Metode Kirim Ke Ekspedisi', copy=False, index=True, tracking=3)

    notes = fields.Text(string="Notes", tracking=3)
    koli_lines = fields.One2many('sales.order.koli', 'sale_order_id', string='Sales Order Koli', auto_join=True)
    fulfillment_line_v2 = fields.One2many('sales.order.fulfillment.v2', 'sale_order_id', string='Fullfillment2')
    fullfillment_line = fields.One2many('sales.order.fullfillment', 'sales_order_id', string='Fullfillment')
    reject_line = fields.One2many('sales.order.reject', 'sale_order_id', string='Reject Lines')
    order_sales_match_line = fields.One2many('sales.order.purchase.match', 'sales_order_id',
                                             string='Purchase Match Lines',
                                             states={'cancel': [('readonly', True)], 'done': [('readonly', True)]},
                                             copy=True)
    total_margin = fields.Float('Total Margin', compute='_compute_total_margin',
                                help="Total Margin in Sales Order Header")
    total_before_margin = fields.Float('Total Before Margin', compute='_compute_total_before_margin',
                                       help="Total Margin in Sales Order Header")
    total_percent_margin = fields.Float('Total Percent Margin', compute='_compute_total_percent_margin',
                                        help="Total % Margin in Sales Order Header")
    total_margin_excl_third_party = fields.Float('Before Margin', help="Before Margin in Sales Order Header")

    approval_status = fields.Selection([
        ('pengajuan1', 'Approval Manager'),
        ('pengajuan2', 'Approval Pimpinan'),
        ('approved', 'Approved'),
    ], string='Approval Status', readonly=True, copy=False, index=True, tracking=3)
    carrier_id = fields.Many2one('delivery.carrier', string='Shipping Method', tracking=3)
    have_visit_service = fields.Boolean(string='Have Visit Service', compute='_have_visit_service',
                                        help='To compute is customer get visit service')
    delivery_amt = fields.Float(string='Delivery Amt', copy=False, tracking=True)
    shipping_cost_covered = fields.Selection([
        ('indoteknik', 'Indoteknik'),
        ('customer', 'Customer')
    ], string='Shipping Covered by', help='Siapa yang menanggung biaya ekspedisi?', copy=False, tracking=3)
    shipping_paid_by = fields.Selection([
        ('indoteknik', 'Indoteknik'),
        ('customer', 'Customer')
    ], string='Shipping Paid by', help='Siapa yang talangin dulu Biaya ekspedisi-nya?', copy=False, tracking=3)
    sales_tax_id = fields.Many2one('account.tax', string='Tax',
                                   domain=['|', ('active', '=', False), ('active', '=', True)])
    have_outstanding_invoice = fields.Boolean('Have Outstanding Invoice', compute='_have_outstanding_invoice')
    have_outstanding_picking = fields.Boolean('Have Outstanding Picking', compute='_have_outstanding_picking')
    have_outstanding_po = fields.Boolean('Have Outstanding PO', compute='_have_outstanding_po')
    purchase_ids = fields.Many2many('purchase.order', string='Purchases', compute='_get_purchases')
    real_shipping_id = fields.Many2one(
        'res.partner', string='Real Delivery Address', readonly=True,
        states={'draft': [('readonly', False)], 'sent': [('readonly', False)], 'sale': [('readonly', False)]},
        domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
        help="Dipakai untuk alamat tempel", tracking=True)
    real_invoice_id = fields.Many2one(
        'res.partner', string='Delivery Invoice Address', required=True,
        domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
        help="Dipakai untuk alamat tempel", tracking=True)
    fee_third_party = fields.Float('Fee Pihak Ketiga')
    biaya_lain_lain = fields.Float('Biaya Lain Lain')
    so_status = fields.Selection([
        ('terproses', 'Terproses'),
        ('sebagian', 'Sebagian Diproses'),
        ('menunggu', 'Menunggu Diproses'),
    ], copy=False)
    partner_purchase_order_name = fields.Char(string='Nama PO Customer', copy=False,
                                              help="Nama purchase order customer, diisi oleh customer melalui website.",
                                              tracking=3)
    partner_purchase_order_description = fields.Text(string='Keterangan PO Customer', copy=False,
                                                     help="Keterangan purchase order customer, diisi oleh customer melalui website.",
                                                     tracking=3)
    partner_purchase_order_file = fields.Binary(string='File PO Customer', copy=False,
                                                help="File purchase order customer, diisi oleh customer melalui website.")
    payment_status = fields.Selection([
        ('pending', 'Pending'),
        ('capture', 'Capture'),
        ('settlement', 'Settlement'),
        ('deny', 'Deny'),
        ('cancel', 'Cancel'),
        ('expire', 'Expire'),
        ('failure', 'Failure'),
        ('refund', 'Refund'),
        ('chargeback', 'Chargeback'),
        ('partial_refund', 'Partial Refund'),
        ('partial_chargeback', 'Partial Chargeback'),
        ('authorize', 'Authorize'),
    ], tracking=True, string='Payment Status',
        help='Payment Gateway Status / Midtrans / Web, https://docs.midtrans.com/en/after-payment/status-cycle')
    date_doc_kirim = fields.Datetime(string='Tanggal Kirim di SJ',
                                     help="Tanggal Kirim di cetakan SJ yang terakhir, tidak berpengaruh ke Accounting")
    payment_type = fields.Char(string='Payment Type', help='Jenis pembayaran dengan Midtrans')
    gross_amount = fields.Float(string='Gross Amount', help='Jumlah pembayaran yang dilakukan dengan Midtrans')
    notification = fields.Char(string='Notification', help='Dapat membantu error dari approval')
    delivery_service_type = fields.Char(string='Delivery Service Type', help='data dari rajaongkir')
    grand_total = fields.Monetary(string='Grand Total', help='Amount total + amount delivery',
                                  compute='_compute_grand_total')
    payment_link_midtrans = fields.Char(string='Payment Link',
                                        help='Url payment yg digenerate oleh midtrans, harap diserahkan ke customer agar dapat dilakukan pembayaran secara mandiri')
    payment_qr_code = fields.Binary("Payment QR Code")
    due_id = fields.Many2one('due.extension', string="Due Extension", readonly=True, tracking=True)
    vendor_approval_id = fields.Many2many('vendor.approval', string="Vendor Approval", readonly=True, tracking=True,
                                          copy=False)
    customer_type = fields.Selection([
        ('pkp', 'PKP'),
        ('nonpkp', 'Non PKP')
    ], related="partner_id.customer_type", string="Customer Type", readonly=True)
    sppkp = fields.Char(string="SPPKP", related="partner_id.sppkp")
    npwp = fields.Char(string="NPWP", related="partner_id.npwp")
    purchase_total = fields.Monetary(string='Purchase Total', compute='_compute_purchase_total')
    voucher_id = fields.Many2one(comodel_name='voucher', string='Voucher', copy=False)
    applied_voucher_id = fields.Many2one(comodel_name='voucher', string='Applied Voucher', copy=False)
    amount_voucher_disc = fields.Float(string='Voucher Discount')
    applied_voucher_shipping_id = fields.Many2one(comodel_name='voucher', string='Applied Voucher', copy=False)
    amount_voucher_shipping_disc = fields.Float(string='Voucher Discount')
    source_id = fields.Many2one('utm.source', 'Source', domain="[('id', 'in', [32, 59, 60, 61])]", required=True)
    estimated_arrival_days = fields.Integer('Estimated Arrival To', default=0)
    estimated_arrival_days_start = fields.Integer('Estimated Arrival From', default=0)
    email = fields.Char(string='Email')
    picking_iu_id = fields.Many2one('stock.picking', 'Picking IU')
    helper_by_id = fields.Many2one('res.users', 'Helper By')
    eta_date_start = fields.Datetime(string='ETA Date start', copy=False, compute='_compute_eta_date')
    eta_date = fields.Datetime(string='ETA Date end', copy=False, compute='_compute_eta_date')
    flash_sale = fields.Boolean(string='Flash Sale', help='Data dari web')
    is_continue_transaction = fields.Boolean(string='Button Transaction', help='Data dari web')
    web_approval = fields.Selection([
        ('company', 'Company'),
        ('cust_manager', 'Customer Manager'),
        ('cust_director', 'Customer Director'),
        ('cust_procurement', 'Customer Procurement')
    ], string='Web Approval', copy=False)
    compute_fullfillment = fields.Boolean(string='Compute Fullfillment', compute="_compute_fullfillment")
    vendor_approval = fields.Boolean(string='Vendor Approval')
    note_ekspedisi = fields.Char(string="Note Ekspedisi")
    date_kirim_ril = fields.Datetime(string='Tanggal Kirim SJ', compute='_compute_date_kirim', copy=False)
    date_status_done = fields.Datetime(string='Date Done DO', compute='_compute_date_kirim', copy=False)
    date_driver_arrival = fields.Datetime(string='Arrival Date', compute='_compute_date_kirim', copy=False)
    date_driver_departure = fields.Datetime(string='Departure Date', compute='_compute_date_kirim', copy=False)
    note_website = fields.Char(string="Note Website")
    use_button = fields.Boolean(string='Using Calculate Selling Price', copy=False)
    unreserve_id = fields.Many2one('stock.picking', 'Unreserve Picking')
    voucher_shipping_id = fields.Many2one(comodel_name='voucher', string='Voucher Shipping', copy=False)
    margin_after_delivery_purchase = fields.Float(string='Margin After Delivery Purchase',
                                                  compute='_compute_margin_after_delivery_purchase')
    percent_margin_after_delivery_purchase = fields.Float(string='% Margin After Delivery Purchase',
                                                          compute='_compute_margin_after_delivery_purchase')
    purchase_delivery_amt = fields.Float(string='Purchase Delivery Amount', compute='_compute_purchase_delivery_amount')
    type_promotion = fields.Char(string='Type Promotion', compute='_compute_type_promotion')
    partner_invoice_id = fields.Many2one(
        'res.partner', string='Invoice Address',
        readonly=True, required=True,
        states={'draft': [('readonly', False)], 'sent': [('readonly', False)], 'sale': [('readonly', False)]},
        domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]",
        tracking=True,  # Menambahkan tracking=True
    )
    partner_shipping_id = fields.Many2one(
        'res.partner', string='Delivery Address', readonly=True, required=True,
        states={'draft': [('readonly', False)], 'sent': [('readonly', False)], 'sale': [('readonly', False)]},
        domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", tracking=True)

    payment_term_id = fields.Many2one(
        'account.payment.term', string='Payment Terms', check_company=True,  # Unrequired company
        domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]", tracking=True)

    total_weight = fields.Float(string='Total Weight', compute='_compute_total_weight')
    pareto_status = fields.Selection([
        ('PR', 'Pareto Repeating'),
        ('PPR', 'Potensi Pareto Repeating'),
        ('PNR', 'Pareto Non Repeating'),
        ('NP', 'Non Pareto')
    ])
    # estimated_ready_ship_date = fields.Datetime(
    #     string='ET Ready to Ship compute',
    #     compute='_compute_etrts_date'
    # )
    expected_ready_to_ship = fields.Datetime(
        string='ET Ready to Ship',
        copy=False
    )
    shipping_method_picking = fields.Char(string='Shipping Method Picking', compute='_compute_shipping_method_picking')

    reason_cancel = fields.Selection([
        ('harga_terlalu_mahal', 'Harga barang terlalu mahal'),
        ('harga_web_tidak_valid', 'Harga web tidak valid'),
        ('stok_kosong', 'Stock kosong'),
        ('tidak_mau_indent', 'Customer tidak mau indent'),
        ('batal_rencana_pembelian', 'Customer membatalkan rencana pembelian'),
        ('vendor_tidak_support_demo', 'Vendor tidak support demo/trial product'),
        ('product_knowledge_kurang', 'Product knowledge kurang baik'),
        ('barang_tidak_sesuai', 'Barang tidak sesuai/tepat'),
        ('tidak_sepakat_pembayaran', 'Tidak menemukan kesepakatan untuk pembayaran'),
        ('dokumen_tidak_support', 'Indoteknik tidak bisa support document yang dibutuhkan (Ex: TKDN, COO, SNI)'),
        ('ganti_quotation', 'Ganti Quotation'),
        ('testing_internal', 'Testing Internal'),
        ('revisi_data', 'Revisi Data'),
    ], string='Reason for Cancel', copy=False, index=True, tracking=3)
    attachment_bukti = fields.Many2one(
        'ir.attachment',
        string="Attachment Bukti Cancel", readonly=False,
    )
    nomor_so_pengganti = fields.Char(string='Nomor SO Pengganti', copy=False, tracking=3)
    
    shipping_option_id = fields.Many2one("shipping.option", string="Selected Shipping Option", domain="['|', ('sale_order_id', '=', False), ('sale_order_id', '=', id)]")
    select_shipping_option = fields.Selection([
        ('biteship', 'Biteship'),
        ('custom', 'Custom'),
    ], string='Shipping Option', help="Select shipping option for delivery", tracking=True, default='custom')
    
    hold_outgoing = fields.Boolean('Hold Outgoing SO', tracking=3)
    state_ask_cancel = fields.Selection([
        ('hold', 'Hold'),
        ('approve', 'Approve')
    ], tracking=True, string='State Cancel', copy=False)
    ready_to_ship_status_detail = fields.Char(
        string='Status Shipping Detail',
        compute='_compute_ready_to_ship_status_detail'
    )
    date_hold = fields.Datetime(string='Date Hold', tracking=True, readonly=True, help='Waktu ketika SO di Hold'
    )
    date_unhold = fields.Datetime(string='Date Unhold', tracking=True, readonly=True, help='Waktu ketika SO di Unhold'
    )

    et_products = fields.Datetime(string='ET Products', help="Leadtime produk berdasarkan SLA vendor, tanpa logistik.", tracking=True)

    eta_date_reserved = fields.Datetime(
        string="Date Reserved",
        compute="_compute_eta_date_reserved",
        help="Tanggal pertama kali barang berhasil di-reservasi pada DO (BU/PICK/) yang berstatus Siap Dikirim."
    )
    refund_ids = fields.Many2many('refund.sale.order', compute='_compute_refund_ids', string='Refunds')
    refund_count = fields.Integer(string='Refund Count', compute='_compute_refund_count')
    advance_payment_move_id = fields.Many2one(
        'account.move',
        compute='_compute_advance_payment_move',
        string='Advance Payment Move',
    )
    advance_payment_move_ids = fields.Many2many(
        'account.move',
        compute='_compute_advance_payment_moves',
        string='All Advance Payment Moves',
    )

    advance_payment_move_count = fields.Integer(
        string='Jumlah Jurnal Uang Muka',
        compute='_compute_advance_payment_moves',
        store=False
    )
    reserved_percent = fields.Float(
        string="Reserved %", digits=(16, 2),
        compute="_compute_reserved_delivered_pie", store=False
    )
    delivered_percent = fields.Float(
        string="Delivered %", digits=(16, 2),
        compute="_compute_reserved_delivered_pie", store=False
    )
    unreserved_percent = fields.Float(
        string="Unreserved %", digits=(16, 2),
        compute="_compute_reserved_delivered_pie", store=False
    )
    payment_state_custom = fields.Selection([
        ('unpaid', 'Unpaid'),
        ('partial', 'Partially Paid'),
        ('paid', 'Full Paid'),
        ('no_invoice', 'No Invoice'),
    ], string="Payment Status Invoice", compute="_compute_payment_state_custom", store=False)
    partner_is_cbd_locked = fields.Boolean(
        string="Partner Locked CBD",
        compute="_compute_partner_is_cbd_locked"
    )
    internal_notes_contact = fields.Text(related='partner_id.comment', string="Internal Notes", readonly=True, help="Internal Notes dari contact utama customer.")
    is_so_fiktif = fields.Boolean('SO Fiktif?', tracking=3)    
    team_id = fields.Many2one(tracking=True)

    client_order_ref = fields.Char(tracking=True)


    def action_set_shipping_id(self):
        for rec in self:
            bu_pick = self.env['stock.picking'].search([
                ('state', 'not in', ['cancel']),
                ('picking_type_id', '=', 30),
                ('sale_id', '=', rec.id),
                ('linked_manual_bu_out', '=', False)
            ])
            # bu_out = bu_pick_has_out.mapped('linked_manual_bu_out')
            bu_out = self.env['stock.picking'].search([
                ('sale_id', '=', rec.id),
                ('picking_type_id', '=', 29),
                ('state', 'not in', ['cancel', 'done'])
            ])
            bu_pick_has_out = self.env['stock.picking'].search([
                ('state', 'not in', ['cancel']),
                ('picking_type_id', '=', 30),
                ('sale_id', '=', rec.id),
                ('linked_manual_bu_out.id', '=', bu_out.id),
                ('linked_manual_bu_out.state', 'not in', ['done', 'cancel'])
            ])
            for pick in bu_pick_has_out:
                linked_out = pick.linked_manual_bu_out
                if pick.real_shipping_id != rec.real_shipping_id or linked_out.partner_id != rec.partner_shipping_id:
                    pick.real_shipping_id = rec.real_shipping_id
                    pick.partner_id = rec.partner_shipping_id
                    linked_out.partner_id = rec.partner_shipping_id
                    linked_out.real_shipping_id = rec.real_shipping_id
                    _logger.info('Updated bu_pick [%s] and bu_out [%s]', pick.name, linked_out.name)

            for pick in bu_pick:
                if pick.real_shipping_id != rec.real_shipping_id:
                    pick.real_shipping_id = rec.real_shipping_id
                    pick.partner_id = rec.partner_shipping_id
                    bu_out.partner_id = rec.partner_shipping_id
                    bu_out.real_shipping_id = rec.real_shipping_id
                    _logger.info('Updated bu_pick [%s] without bu_out', pick.name)

    def action_open_partial_delivery_wizard(self):
        # raise UserError("Fitur ini sedang dalam pengembangan")
        self.ensure_one()
        pickings = self.picking_ids.filtered(lambda p: p.state not in ['done', 'cancel'] and p.name and 'BU/PICK/' in p.name)
        return {
            'type': 'ir.actions.act_window',
            'name': 'Partial Delivery',
            'res_model': 'partial.delivery.wizard',
            'view_mode': 'form',
            'target': 'new',
            'context': {
                'default_sale_id': self.id,
                # kasih langsung list of int biar ga ribet di wizard
                'default_picking_ids': pickings.ids,
            }
        }


    @api.depends('partner_id.is_cbd_locked')
    def _compute_partner_is_cbd_locked(self):
        for order in self:
            order.partner_is_cbd_locked = order.partner_id.is_cbd_locked


    @api.constrains('payment_term_id', 'partner_id')
    def _check_cbd_lock_sale_order(self):
        cbd_term = self.env['account.payment.term'].browse(26)
        for rec in self:
            if rec.state == 'draft' and rec.partner_id.is_cbd_locked:
                if rec.payment_term_id and rec.payment_term_id != cbd_term:
                    raise ValidationError(
                        "Customer ini terkunci ke CBD, hanya boleh pakai Payment Term CBD."
                    )

    @api.depends('invoice_ids.payment_state', 'invoice_ids.amount_total', 'invoice_ids.amount_residual')
    def _compute_payment_state_custom(self):
        for order in self:
            invoices = order.invoice_ids.filtered(lambda inv: inv.state != 'cancel')
            total = sum(invoices.mapped('amount_total'))
            residual = sum(invoices.mapped('amount_residual'))

            if not invoices or total == 0:
                order.payment_state_custom = 'no_invoice'
                continue

            paid = total - residual
            percent_paid = (paid / total) * 100 if total > 0 else 0.0

            if percent_paid == 100:
                order.payment_state_custom = 'paid'
            elif percent_paid == 0:
                order.payment_state_custom = 'unpaid'
            else:
                order.payment_state_custom = 'partial'

    @api.depends(
        'order_line.move_ids.state',
        'order_line.move_ids.reserved_availability',
        'order_line.move_ids.quantity_done',
        'order_line.move_ids.picking_type_id'
    )
    def _compute_reserved_delivered_pie(self):
        for order in self:
            order_qty = sum(order.order_line.mapped('product_uom_qty')) or 0.0
            reserved_qty = delivered_qty = 0.0

            if order_qty > 0:
                # Kumpulin semua moves dari order
                all_moves = order.order_line.mapped('move_ids')

                for move in all_moves:
                    # --- CASE 1: Move belum selesai ---
                    if move.state not in ('done', 'cancel'):
                        # Reserved qty hanya dari move yang belum selesai
                        reserved_qty += move.reserved_availability or 0.0
                        continue

                    # --- CASE 2: Move sudah done ---
                    if move.location_dest_id.usage == 'customer':
                        # Barang dikirim ke customer
                        delivered_qty += move.quantity_done or 0.0
                    elif move.location_id.usage == 'customer':
                        # Barang balik dari customer (retur)
                        delivered_qty -= move.quantity_done or 0.0

                # Clamp supaya delivered gak minus
                delivered_qty = max(delivered_qty, 0)

            # Hitung persen
            order.reserved_percent = min((reserved_qty / order_qty) * 100, 100) if order_qty else 0
            order.delivered_percent = min((delivered_qty / order_qty) * 100, 100) if order_qty else 0
            order.unreserved_percent = max(100 - order.reserved_percent - order.delivered_percent, 0)

    def _has_ccm(self):
        if self.id:
            self.ccm_id = self.env['tukar.guling'].search([('origin', 'ilike', self.name)], limit=1)

    reason_change_date_planned = fields.Selection([
        ('delay', 'Delay By Vendor'),
        ('urgent', 'Urgent Delivery'),
    ], string='Reason Change Date Planned', tracking=True)

    @api.depends('order_line.product_id', 'date_order')
    def _compute_et_products(self):
        jakarta = pytz.timezone("Asia/Jakarta")
        for order in self:
            if not order.order_line or not order.date_order:
                order.et_products = False
                continue

            # Ambil tanggal order sebagai basis
            base_date = order.date_order
            if base_date.tzinfo is None:
                base_date = jakarta.localize(base_date)
            else:
                base_date = base_date.astimezone(jakarta)

            # Ambil nilai SLA vendor dalam hari
            sla_data = order.calculate_sla_by_vendor(order.order_line)
            sla_days = sla_data.get('slatime', 1)

            # Hitung ETA produk (tanpa logistik)
            eta_datetime = base_date + timedelta(days=sla_days)

            # Simpan ke field sebagai UTC-naive datetime (standar Odoo)
            order.et_products = eta_datetime.astimezone(pytz.utc).replace(tzinfo=None)

    @api.depends('picking_ids.state', 'picking_ids.date_done')
    def _compute_eta_date_reserved(self):
        for order in self:
            pickings = order.picking_ids.filtered(
                lambda p: p.state in ('assigned', 'done') and p.date_reserved and 'BU/PICK/' in (p.name or '')
            )
            done_dates = [d for d in pickings.mapped('date_done') if d]
            order.eta_date_reserved = min(done_dates) if done_dates else False
            # order.eta_date_reserved = min(pickings.mapped('date_done')) if pickings else False

    @api.onchange('shipping_cost_covered')
    def _onchange_shipping_cost_covered(self):
        if self.shipping_cost_covered == 'indoteknik' and self.select_shipping_option == 'biteship':
            self.shipping_cost_covered = 'customer' 
            return {
                'warning': {
                    'title': "Biteship Tidak Diizinkan",
                    'message': (
                        "Biaya pengiriman ditanggung Indoteknik, sehingga tidak diizinkan menggunakan metode Biteship. "
                        "Pilihan penanggung biaya akan dikembalikan sebelumnya"
                    )
                }
            }

    def get_biteship_carrier_ids(self):
        courier_codes = tuple(self._get_biteship_courier_codes() or [])
        if not courier_codes:
            return []

        self.env.cr.execute("""
            SELECT delivery_carrier_id 
            FROM rajaongkir_kurir 
            WHERE name IN %s AND delivery_carrier_id IS NOT NULL
        """, (courier_codes,))
        result = self.env.cr.fetchall()
        carrier_ids = [row[0] for row in result if row[0]]
        return carrier_ids

    # @api.model
    # def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
    #     res = super(SaleOrder, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar, submenu=submenu)

    #     if view_type == 'form':
    #         doc = etree.XML(res['arch'])

    #         # Ambil semua delivery_carrier_id dari mapping rajaongkir_kurir
    #         biteship_ids = self.env['rajaongkir.kurir'].search([]).mapped('delivery_carrier_id.id')
    #         biteship_ids = list(set(filter(None, biteship_ids)))  # pastikan unik dan bukan None

    #         all_ids = self.env['delivery.carrier'].search([]).ids
    #         custom_ids = list(set(all_ids) - set(biteship_ids))

    #         # Format sebagai string Python list
    #         biteship_ids_str = ','.join(str(i) for i in biteship_ids) or '-1'
    #         custom_ids_str = ','.join(str(i) for i in custom_ids) or '-1'

    #         # Terapkan domain ke field carrier_id
    #         for node in doc.xpath("//field[@name='carrier_id']"):
    #             # Domain tergantung select_shipping_option
    #             node.set(
    #                 'domain',
    #                 "[('id', 'in', [%s]) if select_shipping_option == 'biteship' else ('id', 'in', [%s])]" %
    #                 (biteship_ids_str, custom_ids_str)
    #             )

    #         # Simpan kembali hasil XML ke arsitektur form
    #         res['arch'] = etree.tostring(doc, encoding='unicode')

    #     return res

    # @api.onchange('shipping_option_id')
    # def _onchange_shipping_option_id(self):
    #     if self.shipping_option_id:
    #         self.delivery_amt = self.shipping_option_id.price
    #         self.delivery_service_type = self.shipping_option_id.courier_service_code    

    def _get_biteship_courier_codes(self):
        return [
            'gojek','grab','deliveree','lalamove','jne','ninja','lion','rara','sicepat','jnt','pos','idexpress','rpx','wahana','jdl','anteraja','sap','paxel','borzo'
        ]
        
    @api.onchange('carrier_id')
    def _onchange_carrier_id(self):
        if not self._origin or not self._origin.id:
            return

        sale_order_id = self._origin.id
        self.shipping_option_id = False

        if not self.carrier_id:
            return {'domain': {'shipping_option_id': [('id', '=', -1)]}}

        # Ambil provider dari mapping
        self.env.cr.execute("""
            SELECT name FROM rajaongkir_kurir
            WHERE delivery_carrier_id = %s LIMIT 1
        """, (self.carrier_id.id,))
        row = self.env.cr.fetchone()
        provider = row[0].lower() if row and row[0] else (
            self.carrier_id.name.lower().split()[0] if self.carrier_id.name else False
        )

        _logger.info(f"[Carrier Changed] {self.carrier_id.name}, Detected Provider: {provider}")

        # ───────────────────────────────────────────────────────────────
        # Validasi koordinat untuk kurir instan
        # ───────────────────────────────────────────────────────────────
        instan_kurir = ['gojek', 'grab', 'lalamove', 'borzo', 'rara', 'deliveree']
        if provider in instan_kurir:
            lat = self.real_shipping_id.latitude
            lng = self.real_shipping_id.longtitude
            def is_invalid(val):
                try:
                    return not val or float(val) == 0.0
                except (ValueError, TypeError):
                    return True

            if is_invalid(lat) or is_invalid(lng):
                self.carrier_id = self._origin.carrier_id
                self.shipping_option_id = self._origin.shipping_option_id or False
                return {
                    'warning': {
                        'title': "Alamat Belum Pin Point",
                        'message': (
                            "Kurir instan seperti Gojek, Grab, Lalamove, Borzo, Rara, dan Deliveree "
                            "membutuhkan alamat pengiriman yang sudah Pin Point.\n\n"
                            "Silakan tentukan lokasi dengan tepat pada Pin Point Location yang tersedia di kontak."
                        )
                    },
                    'domain': {'shipping_option_id': [('id', '=', -1)]}
                }

        # ───────────────────────────────────────────────────────────────
        # Baru cek apakah shipping option sudah ada
        # ───────────────────────────────────────────────────────────────
        total_so_options = self.env['shipping.option'].search_count([
            ('sale_order_id', '=', sale_order_id)
        ])

        if total_so_options == 0:
            return {'domain': {'shipping_option_id': [('id', '=', -1)]}}

        # Validasi: apakah shipping option ada untuk provider ini?
        matched = self.env['shipping.option'].search_count([
            ('sale_order_id', '=', sale_order_id),
            ('provider', 'ilike', provider),
        ])
        if self.select_shipping_option == 'biteship' and matched == 0:
            self.carrier_id = self._origin.carrier_id
            self.shipping_option_id = self._origin.shipping_option_id or False
            return {
                'warning': {
                    'title': "Shipping Option Tidak Ditemukan",
                    'message': (
                        "Layanan kurir ini tidak tersedia pada pengiriman ini. "
                        "Pilihan dikembalikan ke sebelumnya."
                    )
                },
                'domain': {'shipping_option_id': [('id', '=', -1)]}
            }

        # Kalau semua valid, kembalikan domain normal
        domain = [
            '|',
            '&', ('sale_order_id', '=', sale_order_id), ('provider', 'ilike', f'%{provider}%'),
            '&', ('sale_order_id', '=', False), ('provider', 'ilike', f'%{provider}%')
        ]
        return {'domain': {'shipping_option_id': domain}}

    @api.onchange('shipping_option_id')
    def _onchange_shipping_option_id(self):
        if not self.shipping_option_id:
            return

        if not self.carrier_id:
            # Jika belum pilih carrier, tetap update harga dan service type
            self.delivery_amt = self.shipping_option_id.price
            self.delivery_service_type = self.shipping_option_id.courier_service_code
            return

        # Ambil provider dari carrier
        self.env.cr.execute("""
            SELECT name FROM rajaongkir_kurir 
            WHERE delivery_carrier_id = %s LIMIT 1
        """, (self.carrier_id.id,))
        row = self.env.cr.fetchone()
        provider = row[0].lower() if row and row[0] else self.carrier_id.name.lower().split()[0]

        selected_provider = (self.shipping_option_id.provider or '').lower()

        if provider not in selected_provider:
            warning_msg = {
                'title': "Opsi Tidak Valid",
                'message': f"Opsi pengiriman '{self.shipping_option_id.name}' tidak cocok dengan metode '{self.carrier_id.name}'. Dikembalikan ke sebelumnya."
            }

            # Kembalikan ke nilai lama (jika record sudah disimpan)
            self.shipping_option_id = self._origin.shipping_option_id if self._origin else False
            return {'warning': warning_msg}

        # Jika valid
        self.delivery_amt = self.shipping_option_id.price
        self.delivery_service_type = self.shipping_option_id.courier_service_code

    def _update_delivery_service_type_from_shipping_option(self, vals):
        shipping_option_id = vals.get('shipping_option_id') or self.shipping_option_id.id
        if shipping_option_id:
            shipping_option = self.env['shipping.option'].browse(shipping_option_id)
            if shipping_option.exists():
                courier_service = shipping_option.courier_service_code
                vals['delivery_service_type'] = courier_service
                _logger.info("Set delivery_service_type: %s from shipping_option_id: %s", courier_service, shipping_option_id)
            else:
                _logger.warning("shipping_option_id %s not found or invalid.", shipping_option_id)
        else:
            _logger.info("shipping_option_id not found in vals or record.")

    # @api.model
    # def fields_get(self, allfields=None, attributes=None):
    #     res = super().fields_get(allfields=allfields, attributes=attributes)

    #     # Aktifkan hanya kalau sedang buka form Sales Order (safety check)
    #     if self.env.context.get('params', {}).get('model') == 'sale.order' and \
    #     self.env.context.get('params', {}).get('id'):

    #         sale_id = self.env.context['params']['id']

    #         # Ambil carrier_id dari SO yang sedang dibuka
    #         self.env.cr.execute("SELECT carrier_id FROM sale_order WHERE id = %s", (sale_id,))
    #         row = self.env.cr.fetchone()
    #         carrier_id = row[0] if row else None

    #         provider = None
    #         if carrier_id:
    #             self.env.cr.execute("""
    #                 SELECT name FROM rajaongkir_kurir WHERE delivery_carrier_id = %s LIMIT 1
    #             """, (carrier_id,))
    #             row = self.env.cr.fetchone()
    #             if row and row[0]:
    #                 provider = row[0].lower()
    #             else:
    #                 self.env.cr.execute("SELECT name FROM delivery_carrier WHERE id = %s", (carrier_id,))
    #                 row = self.env.cr.fetchone()
    #                 provider = row[0].lower().split()[0] if row and row[0] else ''

    #         if provider:
    #             domain = [
    #                 '|',
    #                 '&', ('sale_order_id', '=', sale_id), ('provider', 'ilike', f'%{provider}%'),
    #                 '&', ('sale_order_id', '=', False), ('provider', 'ilike', f'%{provider}%')
    #             ]

    #             if 'shipping_option_id' in res:
    #                 res['shipping_option_id']['domain'] = domain
    #                 _logger.info(f"fields_get - Injected domain for shipping_option_id: {domain}")
    #     return res


    @api.onchange('select_shipping_option')
    def _onchange_select_shipping_option(self):
        self.shipping_option_id = False
        self.delivery_service_type = False
        self.carrier_id = False
        self.delivery_amt = 0

        biteship_carrier_ids = []
        self.env.cr.execute("""
            SELECT delivery_carrier_id 
            FROM rajaongkir_kurir 
            WHERE name IN %s
        """, (tuple(self._get_biteship_courier_codes()),))
        biteship_carrier_ids = [row[0] for row in self.env.cr.fetchall() if row[0]]

        if self.select_shipping_option == 'biteship':
            if self.shipping_cost_covered == 'indoteknik':
                self.select_shipping_option = self._origin.select_shipping_option if self._origin else 'custom'
                return {
                    'warning': {
                        'title': "Biteship Tidak Diizinkan",
                        'message': (
                            "Biaya pengiriman ditanggung Indoteknik. Tidak diizinkan memilih metode Biteship. "
                            "Opsi pengiriman dikembalikan ke sebelumnya."
                        )
                    }
                }

            domain = [('id', 'in', biteship_carrier_ids)] if biteship_carrier_ids else [('id', '=', -1)]
        else:
            domain = []  # tampilkan semua

        return {'domain': {'carrier_id': domain}}

    # def _compute_total_margin_excl_third_party(self):
    #     for order in self:
    #         if order.amount_untaxed == 0:
    #             order.total_margin_excl_third_party = 0
    #             continue
    #
    #         # order.total_percent_margin = round((order.total_margin / (order.amount_untaxed-delivery_amt-order.fee_third_party)) * 100, 2)
    #         order.total_margin_excl_third_party = round((order.total_before_margin / (order.amount_untaxed)) * 100, 2)
    #         # order.total_percent_margin = round((order.total_margin / (order.amount_untaxed)) * 100, 2)
    #
    def ask_retur_cancel_purchasing(self):
        for rec in self:
            if self.env.user.has_group('indoteknik_custom.group_role_purchasing'):
                rec.state_ask_cancel = 'approve'
                return {
                    'type': 'ir.actions.client',
                    'tag': 'display_notification',
                    'params': {
                        'title': 'Persetujuan Diberikan',
                        'message': 'Proses cancel sudah disetujui',
                        'type': 'success',
                        'sticky': True
                    }
                }
            else:
                rec.state_ask_cancel = 'hold'
                return {
                    'type': 'ir.actions.client',
                    'tag': 'display_notification',
                    'params': {
                        'title': 'Menunggu Persetujuan',
                        'message': 'Tim Purchasing akan memproses permintaan Anda',
                        'type': 'warning',
                        'sticky': False
                    }
                }

    def hold_unhold_qty_outgoing_so(self):  
        if self.hold_outgoing == True:
            self.hold_outgoing = False
            self.date_unhold = fields.Datetime.now()
        else:
            pick = self.env['stock.picking'].search([
                ('sale_id', '=', self.id),
                ('state', 'not in', ['cancel', 'done']),
                ('name', 'ilike', 'BU/PICK/%')
            ])
            for picking in pick:
                picking.do_unreserve()
            self.hold_outgoing = True
            self.date_hold = fields.Datetime.now()
        

    def _validate_uniform_taxes(self):
        for order in self:
            tax_sets = set()
            for line in order.order_line:
                tax_ids = tuple(sorted(line.tax_id.ids))
                if tax_ids:
                    tax_sets.add(tax_ids)
            if len(tax_sets) > 1:
                raise ValidationError("Semua produk dalam Sales Order harus memiliki kombinasi pajak yang sama.")

    # @api.constrains('fee_third_party', 'delivery_amt', 'biaya_lain_lain', 'ongkir_ke_xpdc')
    # def _check_total_margin_excl_third_party(self):
    #     for rec in self:
    #         if rec.fee_third_party == 0 and rec.total_margin_excl_third_party != rec.total_percent_margin:
    #             # Gunakan direct SQL atau flag context untuk menghindari rekursi
    #             self.env.cr.execute("""
    #                 UPDATE sale_order
    #                 SET total_margin_excl_third_party = %s
    #                 WHERE id = %s
    #             """, (rec.total_percent_margin, rec.id))
    #             self.invalidate_cache()

    def _compute_shipping_method_picking(self):
        for order in self:
            if order.picking_ids:
                carrier_names = order.picking_ids.mapped('carrier_id.name')
                order.shipping_method_picking = ', '.join(filter(None, carrier_names))
            else:
                order.shipping_method_picking = False

    @api.onchange('payment_status')
    def _is_continue_transaction(self):
        if not self.is_continue_transaction:
            if self.payment_status == 'settlement':
                self.is_continue_transaction = True
            else:
                self.is_continue_transaction = False

    def _compute_total_weight(self):
        total_weight = 0
        missing_weight_products = []

        for line in self.order_line:
            if line.weight > 0:
                total_weight += line.weight * line.product_uom_qty

        self.total_weight = total_weight

    def action_indoteknik_estimate_shipping(self):
        if not self.real_shipping_id.kota_id.is_jabodetabek:
            raise UserError('Estimasi ongkir hanya bisa dilakukan di kota Jabodetabek')

        total_weight = 0
        missing_weight_products = []

        for line in self.order_line:
            if line.weight > 0:
                total_weight += line.weight * line.product_uom_qty
                line.product_id.weight = line.weight
            else:
                missing_weight_products.append(line.product_id.name)

        if missing_weight_products:
            product_names = '<br/>'.join(missing_weight_products)
            self.message_post(body=f"Produk berikut tidak memiliki berat:<br/>{product_names}")

        if total_weight == 0:
            raise UserError("Tidak dapat mengestimasi ongkir tanpa berat yang valid.")

        if total_weight < 10:
            total_weight = 10

        self.delivery_amt = total_weight * 3000

        shipping_option = self.env["shipping.option"].create({
            "name": "Indoteknik Delivery",
            "price": self.delivery_amt,
            "provider": "Indoteknik",
            "etd": "1-2 Hari",
            "sale_order_id": self.id,
        })
        self.shipping_option_id = shipping_option.id
        self.message_post(
            body=(
                f"<b>Estimasi pengiriman Indoteknik berhasil:</b><br/>"
                f"Layanan: {shipping_option.name}<br/>"
                f"ETD: {shipping_option.etd}<br/>"
                f"Biaya: Rp {shipping_option.price:,}<br/>"
                f"Provider: {shipping_option.provider}"
            ),
            message_type="comment",
        )

    def action_estimate_shipping(self):
        # if self.carrier_id.id in [1, 151]:
        #     self.action_indoteknik_estimate_shipping()
        #     return

        if self.select_shipping_option == 'biteship':
            return self.action_estimate_shipping_biteship()
        elif self.carrier_id.id in [1, 151]:  # ID untuk Indoteknik Delivery
            return self.action_indoteknik_estimate_shipping()
        else:
            total_weight = 0
            missing_weight_products = []

            for line in self.order_line:
                if line.weight > 0:
                    total_weight += line.weight * line.product_uom_qty
                    line.product_id.weight = line.weight    
                else:
                    missing_weight_products.append(line.product_id.name)

            if missing_weight_products:
                product_names = '<br/>'.join(missing_weight_products)
                self.message_post(body=f"Produk berikut tidak memiliki berat:<br/>{product_names}")

            if total_weight == 0:
                raise UserError("Tidak dapat mengestimasi ongkir tanpa berat yang valid.")

            kecamatan_name = self.real_shipping_id.kecamatan_id.name
            kota_name = self.real_shipping_id.kota_id.name
            kelurahan_name = self.real_shipping_id.kelurahan_id.name

            destination_subsdistrict_id = self._get_subdistrict_id_from_komerce(kecamatan_name, kota_name, kelurahan_name)

            # destination_subsdistrict_id = self.real_shipping_id.kecamatan_id.rajaongkir_id
            if not destination_subsdistrict_id:
                raise UserError("Gagal mendapatkan ID kota tujuan.")

            result = self._call_rajaongkir_api(total_weight, destination_subsdistrict_id)
            if not result or not result.get('data'):
                raise UserError(_("Kurir %s tidak tersedia untuk tujuan ini. Silakan pilih kurir lain.") % self.carrier_id.name)

            if result:
                shipping_options = []

                for cost in result.get('data', []):
                    service = cost.get('service')
                    description = cost.get('description')
                    etd = cost.get('etd', '')
                    value = cost.get('cost', 0)
                    provider = cost.get('code')

                    shipping_options.append((service, description, etd, value, provider))

                self.env["shipping.option"].search([('sale_order_id', '=', self.id)]).unlink()

                _logger.info(f"Shipping options: {shipping_options}")

                for service, description, etd, value, provider in shipping_options:
                    self.env["shipping.option"].create({
                        "name": service,
                        "price": value,
                        "provider": provider,
                        "etd": etd,
                        "sale_order_id": self.id,
                    })

                self.shipping_option_id = self.env["shipping.option"].search([('sale_order_id', '=', self.id)], limit=1).id

                _logger.info(f"Shipping option SO ID: {self.shipping_option_id}")

                self.message_post(
                    body=f"Estimasi Ongkos Kirim: Rp{self.delivery_amt}<br/>Detail Lain:<br/>"
                        f"{'<br/>'.join([f'Service: {s[0]}, Description: {s[1]}, ETD: {s[2]}, Cost: Rp {s[3]}' for s in shipping_options])}",
                    message_type="comment"
                )
            else:
                raise UserError("Gagal mendapatkan estimasi ongkir.")

    def _validate_for_shipping_estimate(self):
        # Cek berat produk
        total_weight = 0
        missing_weight_products = []

        for line in self.order_line:
            product_weight = line.product_id.weight or 0
            if product_weight > 0:
                total_weight += product_weight * line.product_uom_qty
                line.weight = product_weight  
            else:
                missing_weight_products.append(line.product_id.name)

        if missing_weight_products:
            product_names = '<br/>'.join(missing_weight_products)
            self.message_post(body=f"Produk berikut tidak memiliki berat:<br/>{product_names}")

        if total_weight == 0:
            raise UserError("Tidak dapat mengestimasi ongkir tanpa karena berat produk = 0 kg.")
        
        # Validasi alamat pengiriman
        if not self.real_shipping_id:
            raise UserError("Alamat pengiriman (Real Delivery Address) harus diisi.")
        
        if not self.real_shipping_id.kota_id:
            raise UserError("Kota pada alamat pengiriman harus diisi.")
        
        if not self.real_shipping_id.zip:
            raise UserError("Kode pos pada alamat pengiriman harus diisi.")
        
        if not self.real_shipping_id.state_id:
            raise UserError("Provinsi pada alamat pengiriman harus diisi.")
        
        return total_weight

    def action_estimate_shipping_biteship(self):
        total_weight = self._validate_for_shipping_estimate()
    
        weight_gram = int(total_weight * 1000)
        if weight_gram < 100:
            weight_gram = 100

        value = int(self.amount_untaxed or sum(line.price_subtotal for line in self.order_line))

        items = [{
            "name": "Paket Pesanan",
            "description": f"Sale Order {self.name}",
            "value": value,
            "weight": weight_gram,
            "quantity": 1,
        }]

        shipping_address = self.real_shipping_id
        _logger.info(f"Shipping Address: {shipping_address}")

        origin_data = {
            "origin_latitude": -6.3031123,
            "origin_longitude": 106.7794934,
        }

        destination_data = {}
        use_coordinate = False

        if hasattr(shipping_address, 'latitude') and hasattr(shipping_address, 'longtitude'):
            if shipping_address.latitude and shipping_address.longtitude:
                try:
                    lat = float(shipping_address.latitude)
                    lng = float(shipping_address.longtitude)
                    destination_data = {
                        "destination_latitude": lat,
                        "destination_longitude": lng
                    }
                    use_coordinate = True
                    _logger.info(f"Using coordinates: lat={lat}, lng={lng}")
                except (ValueError, TypeError):
                    _logger.warning(f"Invalid coordinates, falling back to postal code")
                    use_coordinate = False

        if not use_coordinate:
            if shipping_address.zip:
                origin_data = {"origin_postal_code": 14440}
                destination_data = {
                    "destination_postal_code": shipping_address.zip
                }
                _logger.info(f"Using postal code: {shipping_address.zip}")
            else:
                raise UserError("Tidak dapat mengestimasikan ongkir: Kode pos tujuan tidak tersedia.")

        couriers = ','.join(self._get_biteship_courier_codes())

        api_mode = "koordinat" if use_coordinate else "kode_pos"
        _logger.info(f"Calling Biteship API with mode: {api_mode}")

        result = self._call_biteship_api(origin_data, destination_data, items, couriers)

        if not result:
            raise UserError("Gagal mendapatkan estimasi ongkir dari Biteship.")

        self.env["shipping.option"].search([('sale_order_id', '=', self.id)]).unlink()

        shipping_options = []
        courier_options = {}
        shipping_services = result.get('pricing', [])

        _logger.info(f"Ditemukan {len(shipping_services)} layanan pengiriman")

        for service in shipping_services:
            courier_code = service.get('courier_code', '').lower()
            courier_name = service.get('courier_name', '')
            service_name = service.get('courier_service_name', '')
            raw_price = service.get('price', 0)
            markup_price = int(raw_price * 1.1)
            price = round(markup_price / 1000) * 1000

            _logger.info(f"Layanan: {courier_name} - {service_name}, Harga: {price}")

            if not price:
                _logger.warning(f"Melewati layanan dengan harga 0: {courier_name} - {service_name}")
                continue

            duration = service.get('duration', '')
            shipment_range = service.get('shipment_duration_range', '')
            shipment_unit = service.get('shipment_duration_unit', 'days')

            if duration:
                etd = duration
            elif shipment_range:
                etd = f"{shipment_range} {shipment_unit}"
            else:
                etd = "1-3 days"

            try:
                shipping_option = self.env["shipping.option"].create({
                    "name": f"{courier_name} - {service_name}",
                    "price": price,
                    "provider": courier_code,
                    "etd": etd,
                    "courier_service_code": service.get('courier_service_code'),
                    "sale_order_id": self.id,
                })

                shipping_options.append(shipping_option)

                courier_upper = courier_code.upper()
                if courier_upper not in courier_options:
                    courier_options[courier_upper] = []
                courier_options[courier_upper].append({
                    "name": service_name,
                    "etd": etd,
                    "price": price
                })

                _logger.info(f"Berhasil membuat opsi pengiriman: {courier_name} - {service_name}")
            except Exception as e:
                _logger.error(f"Gagal membuat opsi pengiriman: {str(e)}")

        if not shipping_options:
            raise UserError(f"Tidak ada layanan pengiriman ditemukan untuk kode pos {destination_data.get('destination_postal_code', '')}. Mohon periksa kembali kode pos atau gunakan metode pengiriman lain.")

        # Temukan shipping option yang cocok berdasarkan carrier_id
        selected_option = None

        if self.carrier_id:
            rajaongkir_kurir = self.env['rajaongkir.kurir'].search([
                ('delivery_carrier_id', '=', self.carrier_id.id)
            ], limit=1)

            if rajaongkir_kurir:
                courier_code = rajaongkir_kurir.name.lower()
                carrier_name = self.carrier_id.name.lower()

                possible_codes = list({
                    courier_code,
                    carrier_name,
                    carrier_name.split()[0] if ' ' in carrier_name else carrier_name
                })

                _logger.info(f"[MATCHING] Mencari shipping option untuk kurir: {possible_codes}")

                for option in shipping_options:
                    option_provider = (option.provider or '').lower()
                    option_name = (option.name or '').lower()

                    if any(code in option_provider or code in option_name for code in possible_codes):
                        selected_option = option
                        _logger.info(f"[MATCHED] Shipping option cocok: {option.name}")
                        break

        if not selected_option and shipping_options:
            if not self.env.context.get('from_website_checkout'):
                _logger.info(f"[DEFAULT] Tidak ada yang cocok, pakai opsi pertama: {shipping_options[0].name}")
            selected_option = shipping_options[0]

            # ❗ Ganti carrier_id hanya jika BELUM terisi sama sekali (contoh: user dari backend)
            if not self.carrier_id:
                provider = selected_option.provider.lower()
                self.env.cr.execute("""
                    SELECT delivery_carrier_id FROM rajaongkir_kurir
                    WHERE LOWER(name) = %s AND delivery_carrier_id IS NOT NULL
                    LIMIT 1
                """, (provider,))
                row = self.env.cr.fetchone()
                matched_carrier_id = row[0] if row else False
                if matched_carrier_id:
                    self.carrier_id = matched_carrier_id
                    _logger.info(f"[AUTO-SET] Carrier diisi otomatis ke ID {matched_carrier_id} (provider: {provider})")
                else:
                    _logger.warning(f"[WARNING] Provider {provider} tidak ditemukan di rajaongkir_kurir")

        # Set shipping option dan nilai ongkir
        if selected_option:
            if self.env.context.get('from_website_checkout'):
                # Simpan di context sebagai nilai sementara
                self = self.with_context(
                    _temp_delivery_amt=selected_option.price,
                    _temp_delivery_service=selected_option.courier_service_code,
                    _temp_shipping_option=selected_option.id
                )
            else:
                self.shipping_option_id = selected_option.id
                self.delivery_amt = selected_option.price
                self.delivery_service_type = selected_option.courier_service_code
                
        message_lines = [f"<b>Estimasi Ongkos Kirim Biteship:</b><br/>"]

        for courier, options in courier_options.items():
            message_lines.append(f"<b>{courier}:</b><br/>")
            for opt in options:
                message_lines.append(f"Service: {opt['name']}, ETD: {opt['etd']}, Cost: Rp {int(opt['price']):,}<br/>")
            if courier != list(courier_options.keys())[-1]:
                message_lines.append("<br/>")

        origin_address = "Jl. Bandengan Utara Komp A & BRT. Penjaringan, Kec. Penjaringan, Jakarta (BELAKANG INDOMARET) KOTA JAKARTA UTARA PENJARINGAN"
        destination_address = ', '.join(filter(None, [
            shipping_address.street,
            shipping_address.kelurahan_id.name if shipping_address.kelurahan_id else None,
            shipping_address.kecamatan_id.name if shipping_address.kecamatan_id else None,
            shipping_address.kota_id.name if shipping_address.kota_id else None,
            shipping_address.state_id.name if shipping_address.state_id else None
        ]))
        if use_coordinate:
            origin_suffix = f"(Koordinat: {origin_data.get('origin_latitude')}, {origin_data.get('origin_longitude')})"
            destination_suffix = f"(Koordinat: {destination_data.get('destination_latitude')}, {destination_data.get('destination_longitude')})"
        else:
            origin_suffix = f"(Kode Pos: {origin_data.get('origin_postal_code')})"
            destination_suffix = f"(Kode Pos: {destination_data.get('destination_postal_code')})"

        message_lines.append("<br/><br/><br><b>Info Lokasi:</b><br/>")
        message_lines.append(f"<b>Asal</b>: {origin_address} {origin_suffix}<br/>")
        message_lines.append(f"<b>Tujuan</b>: {destination_address} {destination_suffix}<br/>")

        message_body = "".join(message_lines)

        self.message_post(
            body=message_body,
            message_type="comment"
        )
        
        # Simpan informasi untuk note ekspedisi
        # selected_option = shipping_options[0]  # Opsi pertama dipilih sebagai default
        # self.note_ekspedisi = f"Pengiriman: {selected_option.name} - Rp {selected_option.price:,.0f} ({selected_option.etd}) [via {api_mode}]"
 
    
    def _call_biteship_api(self, origin_data, destination_data, items, couriers=None):

        url = "https://api.biteship.com/v1/rates/couriers"
        api_key = "biteship_live.eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiaW5kb3Rla25payIsInVzZXJJZCI6IjY3MTViYTJkYzVkMjdkMDAxMjRjODk2MiIsImlhdCI6MTc0MTE1NTU4M30.pbFCai9QJv8iWhgdosf8ScVmEeP3e5blrn33CHe7Hgo"
        # api_key = "biteship_test.eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSW5kb3Rla25payIsInVzZXJJZCI6IjY3MTViYTJkYzVkMjdkMDAxMjRjODk2MiIsImlhdCI6MTcyOTQ5ODAwMX0.L6C73couP4-cgVEfhKI2g7eMCMo3YOFSRZhS-KSuHNA"
        # api_key = self.env['ir.config_parameter'].sudo().get_param('biteship.api_key_live')
        # api_key = self.env['ir.config_parameter'].sudo().get_param('biteship.api_key_test')
        headers = {
            'Authorization': api_key,
            'Content-Type': 'application/json'
        }
        
        if not couriers:
            couriers = ','.join(self._get_biteship_courier_codes())

        # Persiapkan payload dengan menggabungkan origin, destination, dan items
        payload = {
            **origin_data,
            **destination_data,
            "couriers": couriers,
            "items": items
        }
        
        api_mode = "koordinat" if "destination_latitude" in destination_data else "kode_pos"
        
        try:
            _logger.info(f"Calling Biteship API with mode: {api_mode}")
            _logger.info(f"Payload: {payload}")
            
            response = requests.post(url, headers=headers, json=payload, timeout=30)
            
            _logger.info(f"Biteship API Status Code: {response.status_code}")
            if response.status_code != 200:
                _logger.error(f"Biteship API Error Response: {response.text}")
            
            if response.status_code == 200:
                result = response.json()
                result['api_mode'] = api_mode  # Tambahkan info mode API
                return result
            else:
                error_msg = response.text
                _logger.error(f"Error calling Biteship API: {response.status_code} - {error_msg}")
                return False
        except Exception as e:
            _logger.error(f"Exception calling Biteship API: {str(e)}")
            return False


    def _call_rajaongkir_api(self, total_weight, destination_subsdistrict_id):
        url = 'https://rajaongkir.komerce.id/api/v1/calculate/domestic-cost'
        headers = {
            'key': '9b1310f644056d84d60b0af6bb21611a',
        }
        courier = self.carrier_id.name.lower()

        data = {
            'origin': 17656,
            # 'originType': 'subdistrict',
            'destination': int(destination_subsdistrict_id),
            # 'destinationType': 'subdistrict',
            'weight': int(total_weight * 1000),
            'courier': courier,
        }

        try:
            _logger.info(f"Calling RajaOngkir API with data: {data}")
            response = requests.post(url, headers=headers, data=data)
            _logger.info(f"RajaOngkir response: {response.status_code} - {response.text}")

            if response.status_code == 200:
                return response.json()
        except Exception as e:
            _logger.error(f"Exception while calling RajaOngkir: {str(e)}")

    def _normalize_city_name(self, city_name):
        city_name = city_name.lower()

        if city_name.startswith('kabupaten'):
            city_name = city_name.replace('kabupaten', '').strip()
        elif city_name.startswith('kota'):
            city_name = city_name.replace('kota', '').strip()

        city_name = " ".join(city_name.split())

        return city_name

    # def _get_city_id_by_name(self, city_name):
    #     url = 'https://pro.rajaongkir.com/api/city'
    #     headers = {
    #         'key': '9b1310f644056d84d60b0af6bb21611a',
    #     }

    #     normalized_city_name = self._normalize_city_name(city_name)

    #     response = requests.get(url, headers=headers)
    #     if response.status_code == 200:
    #         city_data = response.json()
    #         for city in city_data['rajaongkir']['results']:
    #             if city['city_name'].lower() == normalized_city_name:
    #                 return city['city_id']
    #     return None

    # def _get_subdistrict_id_by_name(self, city_id, subdistrict_name):
    #     url = f'https://pro.rajaongkir.com/api/subdistrict?city={city_id}'
    #     headers = {
    #         'key': '9b1310f644056d84d60b0af6bb21611a',
    #     }

    #     response = requests.get(url, headers=headers)
    #     if response.status_code == 200:
    #         subdistrict_data = response.json()
    #         for subdistrict in subdistrict_data['rajaongkir']['results']:
    #             subsdistrict_1 = subdistrict['subdistrict_name'].lower()
    #             subsdistrict_2 = subdistrict_name.lower()

    #             if subsdistrict_1 == subsdistrict_2:
    #                 return subdistrict['subdistrict_id']
    #     return None

    def _get_subdistrict_id_from_komerce(self, kecamatan_name, kota_name, kelurahan_name=None):
        url = 'https://rajaongkir.komerce.id/api/v1/destination/domestic-destination'
        headers = {
            'key': '9b1310f644056d84d60b0af6bb21611a',
        }

        if kelurahan_name:
            search = f"{kelurahan_name} {kecamatan_name} {kota_name}"
        else:
            search = f"{kecamatan_name} {kota_name}"

        params = {
            'search': search,
            'limit': 5
        }

        try:
            response = requests.get(url, headers=headers, params=params, timeout=10)
            if response.status_code == 200:
                data = response.json().get('data', [])
                _logger.info(f"[Komerce] Fetched {len(data)} subdistricts for search '{search}'")
                _logger.info(f"[Komerce] Response: {data}")

                normalized_kota = self._normalize_city_name(kota_name)

                for item in data:
                    match_kelurahan = (
                        not kelurahan_name or 
                        item.get('subdistrict_name', '').lower() == kelurahan_name.lower()
                    )
                    if (
                        match_kelurahan and
                        item.get('district_name', '').lower() == kecamatan_name.lower() and
                        item.get('city_name', '').lower() == normalized_kota
                    ):
                        return item.get('id')

                _logger.warning(f"[Komerce] No match for '{kecamatan_name}' in city '{kota_name}' with kelurahan '{kelurahan_name}'")
            else:
                _logger.error(f"[Komerce] HTTP Error {response.status_code}: {response.text}")
        except Exception as e:
            _logger.error(f"[Komerce] Exception: {e}")

        return None

    def _compute_type_promotion(self):
        for rec in self:
            promotion_types = []
            for promotion in rec.order_promotion_ids:
                for line_program in promotion.program_line_id:
                    if line_program.promotion_type:
                        promotion_types.append(
                            dict(line_program._fields['promotion_type'].selection).get(line_program.promotion_type))

            rec.type_promotion = ', '.join(sorted(set(promotion_types)))

    def _compute_purchase_delivery_amount(self):
        for order in self:
            match = self.env['purchase.order.sales.match']
            result2 = match.search([
                ('sale_id.id', '=', order.id)
            ])
            delivery_amt = 0
            for res in result2:
                delivery_amt = res.delivery_amt
            order.purchase_delivery_amt = delivery_amt

    def _compute_margin_after_delivery_purchase(self):
        for order in self:
            order.margin_after_delivery_purchase = order.total_margin - order.purchase_delivery_amt
            if order.amount_untaxed == 0:
                order.percent_margin_after_delivery_purchase = 0
                continue
            if order.shipping_cost_covered == 'indoteknik':
                delivery_amt = order.delivery_amt
            else:
                delivery_amt = 0
            order.percent_margin_after_delivery_purchase = round((order.margin_after_delivery_purchase / (
                    order.amount_untaxed - delivery_amt - order.fee_third_party - order.biaya_lain_lain)) * 100, 2)

    def _compute_date_kirim(self):
        for rec in self:
            picking = self.env['stock.picking'].search(
                [('sale_id', '=', rec.id), ('state', 'not in', ['cancel']), ('name', 'not ilike', 'BU/PICK/%')],
                order='date_doc_kirim desc', limit=1)
            rec.date_kirim_ril = picking.date_doc_kirim
            rec.date_status_done = picking.date_done
            rec.date_driver_arrival = picking.driver_arrival_date
            rec.date_driver_departure = picking.driver_departure_date

    def open_form_multi_create_uang_muka(self):
        action = self.env['ir.actions.act_window']._for_xml_id('indoteknik_custom.action_sale_order_multi_uangmuka')
        action['context'] = {
            'so_ids': [x.id for x in self]
        }
        return action

    def _compute_fullfillment(self):
        for rec in self:
            #     rec.fullfillment_line.unlink()
            #
            #     for line in rec.order_line:
            #         line._compute_reserved_from()

            rec.compute_fullfillment = True

    @api.depends('expected_ready_to_ship', 'shipping_option_id.etd', 'state')
    def _compute_eta_date(self):
        for rec in self:
            if rec.expected_ready_to_ship and rec.shipping_option_id and rec.shipping_option_id.etd and rec.state not in ['cancel']:
                etd_text = rec.shipping_option_id.etd.strip().lower()
                match = re.match(r"(\d+)\s*-\s*(\d+)\s*(days?|hours?)", etd_text)
                single_match = re.match(r"(\d+)\s*(days?|hours?)", etd_text)

                if match:
                    start_val = int(match.group(1))
                    end_val = int(match.group(2))
                    unit = match.group(3)

                    if 'hour' in unit:
                        rec.eta_date_start = rec.expected_ready_to_ship + timedelta(hours=start_val)
                        rec.eta_date = rec.expected_ready_to_ship + timedelta(hours=end_val)
                    else:
                        rec.eta_date_start = rec.expected_ready_to_ship + timedelta(days=start_val)
                        rec.eta_date = rec.expected_ready_to_ship + timedelta(days=end_val)

                elif single_match:
                    val = int(single_match.group(1))
                    unit = single_match.group(2)

                    if 'hour' in unit:
                        rec.eta_date_start = rec.expected_ready_to_ship + timedelta(hours=val)
                        rec.eta_date = rec.expected_ready_to_ship + timedelta(hours=val)
                    else:
                        rec.eta_date_start = rec.expected_ready_to_ship + timedelta(days=val)
                        rec.eta_date = rec.expected_ready_to_ship + timedelta(days=val)

                else:
                    rec.eta_date_start = False
                    rec.eta_date = False
            else:
                rec.eta_date_start = False
                rec.eta_date = False
                
                
    def get_days_until_next_business_day(self, start_date=None, *args, **kwargs):
        jakarta = pytz.timezone("Asia/Jakarta")
        now = datetime.now(jakarta)

        if start_date is None:
            start_date = now

        if start_date.tzinfo is None:
            start_date = jakarta.localize(start_date)

        holiday = self.env['hr.public.holiday']
        batas_waktu = datetime.strptime("15:00", "%H:%M").time()
        current_day = start_date.date()
        offset = 0
        is3pm = False

        # Step 1: Lewat jam 15 → Tambah 1 hari
        if start_date.time() > batas_waktu:
            is3pm = True
            offset += 1

        # Step 2: Hitung hari libur selama offset itu
        i = 0
        total_days = 0
        while i < offset:
            current_day += timedelta(days=1)
            total_days += 1
            is_weekend = current_day.weekday() >= 5
            is_holiday = holiday.search([("start_date", "=", current_day)])
            if not is_weekend and not is_holiday:
                i += 1  # hanya hitung hari kerja

        # Step 3: Tambah 1 hari masa persiapan gudang
        i = 0
        while i < 1:
            current_day += timedelta(days=1)
            total_days += 1
            is_weekend = current_day.weekday() >= 5
            is_holiday = holiday.search([("start_date", "=", current_day)])
            if not is_weekend and not is_holiday:
                i += 1

        # Step 4: Kalau current_day ternyata weekend/libur, cari hari kerja berikutnya
        while True:
            is_weekend = current_day.weekday() >= 5
            is_holiday = holiday.search([("start_date", "=", current_day)])
            if is_weekend or is_holiday:
                current_day += timedelta(days=1)
                total_days += 1
            else:
                break

        offset = (current_day - start_date.date()).days
        return offset, is3pm


               
    def calculate_sla_by_vendor(self, products):
        product_ids = products.mapped('product_id.id')  # Kumpulkan semua ID produk
        include_instant = True  # Default True, tetapi bisa menjadi False

        # Cek apakah SEMUA produk memiliki qty_free_bandengan >= qty_needed
        all_fast_products = all(
            product.product_id.qty_free_bandengan >= product.product_uom_qty for product in products)
        if all_fast_products:
            return {'slatime': 0, 'include_instant': include_instant}

        # Cari semua vendor pemenang untuk produk yang diberikan
        vendors = self.env['purchase.pricelist'].search([
            ('product_id', 'in', product_ids),
            ('is_winner', '=', True)
        ])

        max_slatime = 1

        for vendor in vendors:
            vendor_sla = self.env['vendor.sla'].search([('id_vendor', '=', vendor.vendor_id.id)], limit=1)
            slatime = 15
            if vendor_sla:
                if vendor_sla.unit == 'hari':
                    vendor_duration = vendor_sla.duration * 24 * 60
                    include_instant = False
                else:
                    vendor_duration = vendor_sla.duration * 60
                    include_instant = True

                estimation_sla = (1 * 24 * 60) + vendor_duration
                estimation_sla_days = estimation_sla / (24 * 60)
                slatime = math.ceil(estimation_sla_days)

            max_slatime = max(max_slatime, slatime)

        return {'slatime': max_slatime, 'include_instant': include_instant}

    def _calculate_etrts_date(self):
        for rec in self:
            if not rec.date_order:
                rec.expected_ready_to_ship = False
                return
            
            jakarta = pytz.timezone("Asia/Jakarta")
            current_date = datetime.now(jakarta)
            
            max_slatime = 1  # Default SLA jika tidak ada
            slatime = self.calculate_sla_by_vendor(rec.order_line)
            max_slatime = max(max_slatime, slatime['slatime'])
            
            offset , is3pm = self.get_days_until_next_business_day(current_date)
            sum_days = max_slatime + offset
            sum_days -= 1 
            if not rec.estimated_arrival_days:
                rec.estimated_arrival_days = sum_days

            eta_date = current_date + timedelta(days=sum_days)
            if is3pm:
                eta_date = datetime.combine(eta_date, time(10, 0))  # jam 10:00
                eta_date = jakarta.localize(eta_date).astimezone(timezone.utc)  # ubah ke UTC
                
                
            eta_date = eta_date.astimezone(timezone.utc).replace(tzinfo=None)
            rec.commitment_date = eta_date
            rec.expected_ready_to_ship = eta_date

    @api.depends("order_line.product_id", "date_order")
    def _compute_etrts_date(self):
        self._calculate_etrts_date()
        
                
    # def _validate_expected_ready_ship_date(self):
    #     for rec in self:
    #         if not rec.order_line:
    #             _logger.info("⏩ Lewati validasi ERTS karena belum ada produk.")
    #             return  # Lewati validasi jika belum ada produk

    #         now = fields.Datetime.now()
    #         expected_date = rec.expected_ready_to_ship and rec.expected_ready_to_ship.date() or None
    #         if not expected_date:
    #             return  # Tidak validasi jika tidak ada input sama sekali

    #         sla = rec.calculate_sla_by_vendor()
    #         offset_day, lewat_jam_3 = rec.get_days_until_next_business_day()
    #         eta_minimum = now + timedelta(days=sla + offset_day)

    #         if expected_date < eta_minimum.date():
    #             rec.expected_ready_to_ship = eta_minimum
    #             raise ValidationError(
    #                 "Tanggal 'Expected Ready to Ship' tidak boleh lebih kecil dari {}. Mohon pilih tanggal minimal {}."
    #                 .format(eta_minimum.strftime('%d-%m-%Y'), eta_minimum.strftime('%d-%m-%Y'))
    #             )

    def _validate_expected_ready_ship_date(self):
        """
        Pastikan expected_ready_to_ship tidak lebih awal dari SLA minimum.
        Dipanggil setiap onchange / simpan SO.
        """
        for rec in self:
            # ─────────────────────────────────────────────────────
            # 1. Hanya validasi kalau field sudah terisi
            #    (quotation baru / belum ada tanggal → abaikan)
            # ─────────────────────────────────────────────────────
            if not rec.expected_ready_to_ship:
                continue

            current_date = datetime.now()

            # ─────────────────────────────────────────────────────
            # 2. Hitung SLA berdasarkan product lines (jika ada)
            # ─────────────────────────────────────────────────────
            products = rec.order_line
            if products:
                sla_data = rec.calculate_sla_by_vendor(products)
                max_sla_time = sla_data.get('slatime', 1)
            else:
                # belum ada item → gunakan default 1 hari
                max_sla_time = 1

            # offset hari libur / weekend
            offset, is3pm = rec.get_days_until_next_business_day(current_date)
            min_days = max_sla_time + offset - 1
            eta_minimum = current_date + timedelta(days=min_days)

            # ─────────────────────────────────────────────────────
            # 3. Validasi - raise error bila terlalu cepat
            # ─────────────────────────────────────────────────────
            if rec.expected_ready_to_ship.date() < eta_minimum.date():
                # set otomatis ke tanggal minimum supaya user tidak perlu
                # menekan Save dua kali
                rec.expected_ready_to_ship = eta_minimum

                raise ValidationError(
                    _("Tanggal 'Expected Ready to Ship' tidak boleh "
                    "lebih kecil dari %(tgl)s. Mohon pilih minimal %(tgl)s.")
                    % {'tgl': eta_minimum.strftime('%d-%m-%Y')}
                )
            else:
                # sinkronkan ke field commitment_date
                rec.commitment_date = rec.expected_ready_to_ship

    # def _validate_expected_ready_ship_date(self):
    #     """
    #     Pastikan expected_ready_to_ship tidak lebih awal dari SLA minimum.
    #     Dipanggil setiap onchange / simpan SO.
    #     """
    #     for rec in self:
    #         if not rec.expected_ready_to_ship:
    #             continue
    #
    #         # ADDED: gunakan "sekarang" lokal user, bukan datetime.now() server
    #         current_date = fields.Datetime.context_timestamp(rec, fields.Datetime.now())
    #
    #         # Hitung SLA
    #         products = rec.order_line
    #         if products:
    #             sla_data = rec.calculate_sla_by_vendor(products)
    #             max_sla_time = sla_data.get('slatime', 1)
    #         else:
    #             max_sla_time = 1
    #
    #         # offset hari libur/weekend
    #         offset, is3pm = rec.get_days_until_next_business_day(current_date)
    #         min_days = max_sla_time + offset - 1
    #         eta_minimum = current_date + timedelta(days=min_days)
    #
    #         if rec._fields['expected_ready_to_ship'].type == 'date':
    #             exp_date_local = rec.expected_ready_to_ship
    #         else:
    #             exp_date_local = fields.Datetime.context_timestamp(
    #                 rec, rec.expected_ready_to_ship
    #             ).date()
    #
    #         if exp_date_local < eta_minimum.date():
    #             # (opsional) auto-set ke minimum → konversi balik ke UTC naive bila field Datetime
    #             if rec._fields['expected_ready_to_ship'].type == 'date':
    #                 rec.expected_ready_to_ship = eta_minimum.date()
    #             else:
    #                 rec.expected_ready_to_ship = eta_minimum.astimezone(pytz.UTC).replace(tzinfo=None)
    #
    #             raise ValidationError(
    #                 _("Tanggal 'Expected Ready to Ship' tidak boleh "
    #                   "lebih kecil dari %(tgl)s. Mohon pilih minimal %(tgl)s.")
    #                 % {'tgl': eta_minimum.strftime('%d-%m-%Y')}
    #             )
    #         else:
    #             rec.commitment_date = rec.expected_ready_to_ship

                
    @api.onchange('expected_ready_to_ship')
    def _onchange_expected_ready_ship_date(self):
        self._validate_expected_ready_ship_date()
    
    def _set_etrts_date(self):
        for order in self:
            if order.state in ('done', 'cancel', 'sale'):
                raise UserError(
                    _("You cannot change the Estimated Ready To Ship Date on a done, sale or cancelled order."))
            # order.move_lines.write({'estimated_ready_ship_date': order.estimated_ready_ship_date})

    def _prepare_invoice(self):
        """
        Prepare the dict of values to create the new invoice for a sales order. This method may be
        overridden to implement custom invoice generation (making sure to call super() to establish
        a clean extension chain).
        """
        self.ensure_one()
        journal = self.env['account.move'].with_context(default_move_type='out_invoice')._get_default_journal()
        if not journal:
            raise UserError(
                _('Please define an accounting sales journal for the company %s (%s).') % (self.company_id.name,
                                                                                           self.company_id.id))

        parent_id = self.partner_id.parent_id
        parent_id = parent_id if parent_id else self.partner_id

        invoice_vals = {
            'ref': self.client_order_ref or '',
            'move_type': 'out_invoice',
            'narration': self.note,
            'currency_id': self.pricelist_id.currency_id.id,
            'campaign_id': self.campaign_id.id,
            'medium_id': self.medium_id.id,
            'source_id': self.source_id.id,
            'down_payment': 229625 in [line.product_id.id for line in self.order_line],
            'user_id': self.user_id.id,
            'sale_id': self.id,
            'invoice_user_id': self.user_id.id,
            'team_id': self.team_id.id,
            'partner_id': parent_id.id,
            'partner_shipping_id': parent_id.id,
            'real_invoice_id': self.real_invoice_id.id,
            'fiscal_position_id': (self.fiscal_position_id or self.fiscal_position_id.get_fiscal_position(
                self.partner_invoice_id.id)).id,
            'partner_bank_id': self.company_id.partner_id.bank_ids[:1].id,
            'journal_id': journal.id,  # company comes from the journal
            'invoice_origin': self.name,
            'invoice_payment_term_id': self.payment_term_id.id,
            'payment_reference': self.reference,
            'transaction_ids': [(6, 0, self.transaction_ids.ids)],
            'invoice_line_ids': [],
            'company_id': self.company_id.id,
        }
        return invoice_vals

    @api.constrains('email')
    def _validate_email(self):
        rule_regex = self.env['ir.config_parameter'].sudo().get_param('sale.order.validate_email') or ''
        pattern = rf'^{rule_regex}$'

        if self.email and not re.match(pattern, self.email):
            raise UserError('Email yang anda input kurang valid')

    # @api.constrains('delivery_amt', 'carrier_id', 'shipping_cost_covered')
    def _validate_delivery_amt(self):
        is_indoteknik = self.carrier_id.id == 1 or self.shipping_cost_covered == 'indoteknik'
        is_active_id = not self.env.context.get('active_id', [])
        if is_indoteknik and is_active_id:
            if self.delivery_amt == 0:
                if self.carrier_id.id == 1:
                    raise UserError('Untuk Kurir Indoteknik Delivery, estimasi ongkos kirim belum diisi.')
                else:
                    raise UserError('Untuk Shipping Covered Indoteknik, estimasi ongkos kirim belum diisi.')

            if self.delivery_amt < 100:
                if self.carrier_id.id == 1:
                    raise UserError(
                        'Untuk Kurir Indoteknik Delivery, estimasi ongkos kirim belum memenuhi tarif minimum.')
                else:
                    raise UserError(
                        'Untuk Shipping Covered Indoteknik, estimasi ongkos kirim belum memenuhi tarif minimum.')

        # if self.delivery_amt < 5000:
        #     if (self.carrier_id.id == 1 or self.shipping_cost_covered == 'indoteknik') and not self.env.context.get('active_id', []):
        #         if self.carrier_id.id == 1:
        #             raise UserError('Untuk Kurir Indoteknik Delivery, estimasi ongkos kirim belum memenuhi jumlah minimum.')
        #         else:
        #             raise UserError('Untuk Shipping Covered Indoteknik, estimasi ongkos kirim belum memenuhi jumlah minimum.')

    def override_allow_create_invoice(self):
        if not self.env.user.is_accounting:
            raise UserError('Hanya Finance Accounting yang dapat klik tombol ini')
        # for term in self.payment_term_id.line_ids:
        #     if term.days > 0:
        #         raise UserError('Hanya dapat digunakan pada Cash Before Delivery')
        for line in self.order_line:
            if line.product_id.type == 'product':
                line.qty_to_invoice = line.product_uom_qty

    # def _get_pickings(self):
    #     state = ['assigned']
    #     for order in self:
    #         pickings = self.env['stock.picking'].search([
    #             ('sale_id.id', '=', order.id),
    #             ('state', 'in', state)
    #         ])
    #         order.picking_ids = pickings

    @api.model
    def action_multi_update_state(self):
        for sale in self:
            for picking_ids in sale.picking_ids:
                if not picking_ids.state == 'cancel':
                    raise UserError('DO harus cancel terlebih dahulu')

            sale.update({
                'state': 'cancel',
            })

            if sale.state == 'cancel':
                sale.update({
                    'approval_status': False,
                })

    def open_form_multi_update_status(self):
        if self.env.user.id != 688 or self.env.user.has_group('indoteknik_custom.group_role_it'):
            raise UserError("Hanya Finance nya yang bisa approve.")
        action = self.env['ir.actions.act_window']._for_xml_id('indoteknik_custom.action_sale_orders_multi_update')
        action['context'] = {
            'sale_ids': [x.id for x in self]
        }
        return action

    def open_form_multi_update_state(self):
        action = self.env['ir.actions.act_window']._for_xml_id('indoteknik_custom.action_quotation_so_multi_update')
        action['context'] = {
            'quotation_ids': [x.id for x in self]
        }
        return action

    def action_multi_update_invoice_status(self):
        for sale in self:
            sale.update({
                'invoice_status': 'invoiced',
            })

    def _compute_purchase_total(self):
        for order in self:
            total = 0
            for line in order.order_line:
                total += line.vendor_subtotal
            order.purchase_total = total

    def check_data_real_delivery_address(self):
        real_delivery_address = self.real_shipping_id

        if not real_delivery_address.state_id:
            raise UserError('State Real Delivery Address harus diisi')
        if not real_delivery_address.zip:
            raise UserError('Zip code Real Delivery Address harus diisi')
        if not real_delivery_address.mobile:
            raise UserError('Mobile Real Delivery Address harus diisi')
        if not real_delivery_address.phone:
            raise UserError('Phone Real Delivery Address harus diisi')
        if not real_delivery_address.kecamatan_id:
            raise UserError('Kecamatan Real Delivery Address harus diisi')
        # if not real_delivery_address.kelurahan_id:
        #     raise UserError('Kelurahan Real Delivery Address harus diisi')

    def generate_payment_link_midtrans_sales_order(self):
        # midtrans_url = 'https://app.sandbox.midtrans.com/snap/v1/transactions'  # dev - sandbox
        # midtrans_auth = 'Basic U0ItTWlkLXNlcnZlci1uLVY3ZDJjMlpCMFNWRUQyOU95Q1dWWXA6'  # dev - sandbox
        midtrans_url = 'https://app.midtrans.com/snap/v1/transactions'          # production
        midtrans_auth = 'Basic TWlkLXNlcnZlci1SbGMxZ2gzWGpSVW5scl9JblZzTV9OTnU6' # production

        so_number = self.name
        so_number = so_number.replace('/', '-')
        so_grandtotal = math.floor(self.grand_total)

        headers = {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': midtrans_auth,
        }

        # ==== ENV ====
        # check_url = f'https://api.sandbox.midtrans.com/v2/{so_number}/status'  # dev - sandbox
        check_url = f'https://api.midtrans.com/v2/{so_number}/status'        # production
        # =============================================

        check_response = requests.get(check_url, headers=headers)
        if check_response.status_code == 200:
            status_response = check_response.json()
            if status_response.get('transaction_status') in ('expire', 'cancel'):
                so_number = so_number + '-cpl'

        json_data = {
            'transaction_details': {
                'order_id': so_number,
                'gross_amount': so_grandtotal,
            },
            'credit_card': {
                'secure': True,
            },
        }

        response = requests.post(midtrans_url, headers=headers, json=json_data).json()
        lookup_json = json.dumps(response, indent=4, sort_keys=True)
        redirect_url = json.loads(lookup_json)['redirect_url']
        self.payment_link_midtrans = str(redirect_url)

        if 'redirect_url' in response:
            qr = qrcode.QRCode(
                version=1,
                error_correction=qrcode.constants.ERROR_CORRECT_L,
                box_size=10,
                border=4,
            )
            qr.add_data(redirect_url)
            qr.make(fit=True)
            img = qr.make_image(fill_color="black", back_color="white")

            buffer = BytesIO()
            img.save(buffer, format="PNG")
            qr_code_img = base64.b64encode(buffer.getvalue()).decode()
            self.payment_qr_code = qr_code_img

    @api.model
    def _generate_so_access_token(self, limit=50):
        orders = self.search([('access_token', '=', False)], limit=limit)
        for order in orders:
            token_source = string.ascii_letters + string.digits
            order.access_token = ''.join(random.choice(token_source) for i in range(20))

    def calculate_line_no(self):
        line_no = 0
        for line in self.order_line:
            if line.product_id.type == 'product':
                line_no += 1
                line.line_no = line_no

    # def write(self, vals):
    #     if 'carrier_id' in vals:
    #         for picking in self.picking_ids:
    #             if picking.state == 'assigned':
    #                 picking.carrier_id = self.carrier_id

    def calculate_so_status(self):
        so_state = ['sale']
        sales = self.search([
            ('state', 'in', so_state),
            ('so_status', '!=', 'terproses'),
        ])

        for sale in sales:
            picking_states = ['draft', 'assigned', 'confirmed', 'waiting']
            have_outstanding_pick = any(x.state in picking_states for x in sale.picking_ids)

            sum_qty_so = sum(so_line.product_uom_qty for so_line in sale.order_line)
            sum_qty_ship = sum(so_line.qty_delivered for so_line in sale.order_line)

            if sum_qty_so > sum_qty_ship > 0:
                sale.so_status = 'sebagian'
            elif not have_outstanding_pick:
                sale.so_status = 'terproses'
            else:
                sale.so_status = 'menunggu'

            for picking in sale.picking_ids:
                sum_qty_pick = sum(move_line.product_uom_qty for move_line in picking.move_ids_without_package)
                sum_qty_reserved = sum(move_line.product_uom_qty for move_line in picking.move_line_ids_without_package)
                if picking.state == 'done':
                    continue
                elif sum_qty_pick == sum_qty_reserved and not picking.date_reserved:  # baru ke reserved
                    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    picking.date_reserved = current_time
                elif sum_qty_pick == sum_qty_reserved:  # sudah ada data reserved
                    picking.date_reserved = picking.date_reserved
                else:
                    picking.date_reserved = ''

            _logger.info('Calculate SO Status %s' % sale.id)

    # def _search_picking_ids(self, operator, value):
    #     if operator == 'in' and value:
    #         self.env.cr.execute("""
    #             SELECT array_agg(so.sale_id)
    #                 FROM stock_picking so
    #             WHERE
    #                 so.sale_id is not null and so.id = ANY(%s)
    #         """, (list(value),))
    #         so_ids = self.env.cr.fetchone()[0] or []
    #         return [('id', 'in', so_ids)]
    #     elif operator == '=' and not value:
    #         order_ids = self._search([
    #             ('order_line.invoice_lines.move_id.move_type', 'in', ('out_invoice', 'out_refund'))
    #         ])
    #         return [('id', 'not in', order_ids)]
    #     return ['&', ('order_line.invoice_lines.move_id.move_type', 'in', ('out_invoice', 'out_refund')), ('order_line.invoice_lines.move_id', operator, value)]

    # @api.depends('partner_id')
    # def _compute_partner_field(self):
    #     for order in self:
    #         partner = order.partner_id.parent_id or order.partner_id
    #         order.npwp = partner.npwp
    #         order.sppkp = partner.sppkp
    #         order.customer_type = partner.customer_type

    @api.onchange('partner_id')
    def onchange_partner_contact(self):
        parent_id = self.partner_id.parent_id
        parent_id = parent_id if parent_id else self.partner_id

        # self.npwp = parent_id.npwp
        # self.sppkp = parent_id.sppkp
        # self.customer_type = parent_id.customer_type
        self.email = parent_id.email
        self.pareto_status = parent_id.pareto_status
        self.user_id = parent_id.user_id

    @api.onchange('partner_id')
    def onchange_partner_id(self):
        # INHERIT
        result = super(SaleOrder, self).onchange_partner_id()
        parent_id = self.partner_id.parent_id
        parent_id = parent_id if parent_id else self.partner_id

        self.partner_invoice_id = parent_id
        return result

    def _get_purchases(self):
        po_state = ['done', 'draft', 'purchase']
        for order in self:
            purchases = self.env['purchase.order'].search([
                ('sale_order_id', '=', order.id),
                ('state', 'in', po_state)
            ])
            order.purchase_ids = purchases

    def _have_outstanding_invoice(self):
        invoice_state = ['posted', 'draft']
        for order in self:
            order.have_outstanding_invoice = any(inv.state in invoice_state for inv in order.invoice_ids)

    def _have_outstanding_picking(self):
        picking_state = ['done', 'confirmed', 'draft']
        for order in self:
            order.have_outstanding_picking = any(pick.state in picking_state for pick in order.picking_ids)

    def _have_outstanding_po(self):
        po_state = ['done', 'draft', 'purchase']
        for order in self:
            order.have_outstanding_po = any(po.state in po_state for po in order.purchase_ids)

    def _have_visit_service(self):
        minimum_amount = 20000000
        for order in self:
            order.have_visit_service = self.amount_total > minimum_amount

    def _get_helper_ids(self):
        helper_ids_str = self.env['ir.config_parameter'].sudo().get_param('sale.order.user_helper_ids')
        return helper_ids_str.split(', ')

    # def write(self, values):
    #     helper_ids = self._get_helper_ids()
    #     if str(self.env.user.id) in helper_ids:
    #         values['helper_by_id'] = self.env.user.id
    #
    #     return super(SaleOrder, self).write(values)

    def check_due(self):
        """To show the due amount and warning stage"""
        for order in self:
            partner = order.partner_id.parent_id or order.partner_id
            if partner and partner.active_limit and partner.enable_credit_limit:
                order.has_due = partner.due_amount > 0
                if order.outstanding_amount >= partner.warning_stage and partner.warning_stage != 0:
                    order.is_warning = True
            else:
                order.has_due = False
                order.is_warning = False

    def _validate_order(self):
        if self.payment_term_id.id == 31 and self.total_percent_margin < 25:
            raise UserError("Jika ingin menggunakan Tempo 90 Hari maka margin harus di atas 25%")

        if self.warehouse_id.id != 8 and self.warehouse_id.id != 10 and self.warehouse_id.id != 12:  # GD Bandengan / Pameran
            raise UserError('Gudang harus Bandengan atau Pameran')

        if self.state not in ['draft', 'sent']:
            raise UserError("Status harus draft atau sent")

    def _validate_npwp(self):
        if not self.npwp:
            raise UserError("NPWP partner kosong, silahkan isi terlebih dahulu npwp nya di contact partner")
        
        if not self.customer_type:
            raise UserError("Customer Type partner kosong, silahkan isi terlebih dahulu Customer Type nya di contact partner")
        
        if not self.sppkp:
            raise UserError("SPPKP partner kosong, silahkan isi terlebih dahulu SPPKP nya di contact partner")
        num_digits = sum(c.isdigit() for c in self.npwp)

        if num_digits < 10:
            raise UserError("NPWP harus memiliki minimal 10 digit")

        # pattern = r'^\d{10,}$'
        # return re.match(pattern, self.npwp) is not None

    def sale_order_check_approve(self):
        self.check_due()

        self._validate_order()
        for order in self:
            order._validate_npwp()
            order._validate_uniform_taxes()
            order.order_line.validate_line()

            term_days = 0
            for term_line in order.payment_term_id.line_ids:
                term_days += term_line.days

            partner = order.partner_id.parent_id or order.partner_id
            if not partner.property_payment_term_id:
                raise UserError("Payment Term pada Master Data Customer harus diisi")
            if not partner.active_limit and term_days > 0:
                raise UserError("Credit Limit pada Master Data Customer harus diisi")
            if order.payment_term_id != partner.property_payment_term_id and not order.partner_id.id == 29179:
                raise UserError("Payment Term berbeda pada Master Data Customer")
            if (partner.customer_type == 'pkp' or order.customer_type == 'pkp') and order.npwp != partner.npwp:
                raise UserError("NPWP berbeda pada Master Data Customer")
            if (partner.customer_type == 'pkp' or order.customer_type == 'pkp') and order.sppkp != partner.sppkp:
                raise UserError("SPPKP berbeda pada Master Data Customer")
            if not order.client_order_ref and order.create_date > datetime(2024, 6, 27):
                raise UserError(
                    "Customer Reference kosong, di isi dengan NO PO jika PO tidak ada mohon ditulis Tanpa PO")
            if not order.user_id.active:
                raise UserError("Salesperson sudah tidak aktif, mohon diisi yang benar pada data SO dan Contact")

    def check_product_bom(self):
        for order in self:
            for line in order.order_line:
                if 'bom-it' in line.name.lower() or 'bom' in line.product_id.default_code.lower() if line.product_id.default_code else False:
                    search_bom = self.env['mrp.production'].search([('product_id', '=', line.product_id.id), ('sale_order', '=', order.id), ('state', '!=', 'cancel')],
                    order='name desc')
                    if search_bom:
                        confirmed_bom = search_bom.filtered(lambda x: x.state in ['confirmed', 'to_close', 'progress', 'done'])
                        if not confirmed_bom:
                            raise UserError(
                                "Product BOM belum dikonfirmasi di Manufacturing Orders. Silakan hubungi Purchasing.")
                    else:
                        raise UserError("Product BOM tidak di temukan di manufacturing orders, silahkan hubungi Purchasing")

    def check_duplicate_product(self):
        for order in self:
            for line in order.order_line:
                search_product = self.env['sale.order.line'].search(
                    [('product_id', '=', line.product_id.id), ('order_id', '=', order.id), ('display_type', '=', False)])
                if len(search_product) > 1:
                    raise UserError("Terdapat DUPLIKASI data pada Product {}".format(line.product_id.display_name))

    def sale_order_approve(self):
        self.check_duplicate_product()
        self.check_product_bom()
        self.check_credit_limit()
        self.check_limit_so_to_invoice()
        if self.validate_different_vendor() and not self.vendor_approval:
            return self._create_notification_action('Notification', 'Terdapat Vendor yang berbeda dengan MD Vendor')
        self.check_due()
        for order in self:
            order._validate_npwp()
            order._validate_delivery_amt()
            order._validate_uniform_taxes()
            order.order_line.validate_line()
            order.check_data_real_delivery_address()
            order._validate_order()
            order.sale_order_check_approve()

            main_parent = order.partner_id.get_main_parent()
            SYSTEM_UID = 25
            FROM_WEBSITE = order.create_uid.id == SYSTEM_UID

            if FROM_WEBSITE and main_parent.use_so_approval and order.web_approval not in ['cust_procurement',
                                                                                           'cust_director']:
                raise UserError("This order not yet approved by customer procurement or director")

            if not order.client_order_ref and order.create_date > datetime(2024, 6, 27):
                raise UserError(
                    "Customer Reference kosong, di isi dengan NO PO jika PO tidak ada mohon ditulis Tanpa PO")

            if not order.commitment_date and order.create_date > datetime(2024, 9, 12):
                raise UserError("Expected Delivery Date kosong, wajib diisi")

            if not order.real_shipping_id:
                UserError('Real Delivery Address harus di isi')

            if not self.env.context.get('due_approve', []):
                if order.validate_partner_invoice_due():
                    return self._create_notification_action('Notification',
                                                            'Terdapat invoice yang telah melewati batas waktu, mohon perbarui pada dokumen Due Extension')

            term_days = 0
            for term_line in order.payment_term_id.line_ids:
                term_days += term_line.days

            partner = order.partner_id.parent_id or order.partner_id
            if not partner.property_payment_term_id:
                raise UserError("Payment Term pada Master Data Customer harus diisi")
            if not partner.active_limit and term_days > 0:
                raise UserError("Credit Limit pada Master Data Customer harus diisi")
            if order.payment_term_id != partner.property_payment_term_id and not order.partner_id.id == 29179:
                raise UserError("Payment Term berbeda pada Master Data Customer")
            if (partner.customer_type == 'pkp' or order.customer_type == 'pkp') and order.npwp != partner.npwp:
                raise UserError("NPWP berbeda pada Master Data Customer")
            if (partner.customer_type == 'pkp' or order.customer_type == 'pkp') and order.sppkp != partner.sppkp:
                raise UserError("SPPKP berbeda pada Master Data Customer")
            if not order.client_order_ref and order.create_date > datetime(2024, 6, 27):
                raise UserError(
                    "Customer Reference kosong, di isi dengan NO PO jika PO tidak ada mohon ditulis Tanpa PO")
            if not order.user_id.active:
                raise UserError("Salesperson sudah tidak aktif, mohon diisi yang benar pada data SO dan Contact")

            # if order.validate_partner_invoice_due():
            #     return self._create_notification_action('Notification',
            #                                             'Terdapat invoice yang telah melewati batas waktu, mohon perbarui pada dokumen Due Extension')
            
            if order._requires_approval_margin_leader():
                order.approval_status = 'pengajuan2'
                order.message_post(body="Mengajukan approval ke Pimpinan")
                return self._create_approval_notification('Pimpinan')
            elif order._requires_approval_margin_manager():
                self.check_product_bom()
                self.check_credit_limit()
                self.check_limit_so_to_invoice()
                order.approval_status = 'pengajuan1'
                order.message_post(body="Mengajukan approval ke Sales Manager")
                return self._create_approval_notification('Sales Manager')
            elif order._requires_approval_team_sales():
                self.check_product_bom()
                self.check_credit_limit()
                self.check_limit_so_to_invoice()
                order.approval_status = 'pengajuan1'
                order.message_post(body="Mengajukan approval ke Team Sales")
                return self._create_approval_notification('Team Sales')
            
            if not order.with_context(ask_approval=True)._is_request_to_own_team_leader():
                return self._create_notification_action(
                    'Peringatan',
                    'Hanya bisa konfirmasi SO tim Anda.'
                )

            user = self.env.user
            is_sales_admin = user.id in (3401, 20, 3988, 17340)
            if is_sales_admin:
                order.approval_status = 'pengajuan1'
                order.message_post(body="Mengajukan approval ke Sales")
                return self._create_approval_notification('Sales')
            
            raise UserError("Bisa langsung Confirm")

    def send_notif_to_salesperson(self, cancel=False):

        if not cancel:
            grouping_so = self.search([
                ('partner_id.parent_id.id', '=', self.partner_id.parent_id.id),
                ('partner_id.site_id.id', '=', self.partner_id.site_id.id),
            ])
        else:
            grouping_so = self.search([
                ('partner_id.parent_id.id', '=', self.partner_id.parent_id.id),
                ('partner_id.site_id.id', '=', self.partner_id.site_id.id),
                ('id', '!=', self.id),
            ])
        # Kelompokkan data berdasarkan salesperson
        salesperson_data = {}
        for rec in grouping_so:
            if rec.user_id.id not in salesperson_data:
                salesperson_data[rec.user_id.id] = {'name': rec.user_id.name, 'orders': [], 'total_amount': 0,
                                                    'sum_total_amount': 0, 'business_partner': '',
                                                    'site': ''}  # Menetapkan nilai awal untuk 'site'
            if rec.picking_ids:
                if not any(picking.state in ['assigned', 'confirmed', 'waiting'] for picking in rec.picking_ids):
                    continue
                if all(picking.state == 'done' for picking in rec.picking_ids):
                    continue
                if all(picking.state == 'cancel' for picking in rec.picking_ids):
                    continue
            if not rec.partner_id.main_parent_id.use_so_approval:
                continue
            order_total_amount = rec.amount_total  # Mengakses langsung rec.amount_total
            salesperson_data[rec.user_id.id]['orders'].append({
                'order_name': rec.name,
                'parent_name': rec.partner_id.name,
                'site_name': rec.partner_id.site_id.name,
                'total_amount': rec.amount_total,
            })
            salesperson_data[rec.user_id.id]['sum_total_amount'] += order_total_amount
            salesperson_data[rec.user_id.id]['business_partner'] = grouping_so[0].partner_id.main_parent_id.name
            salesperson_data[rec.user_id.id]['site'] = grouping_so[
                0].partner_id.site_id.name  # Menambahkan nilai hanya jika ada

        # Kirim email untuk setiap salesperson
        for salesperson_id, data in salesperson_data.items():
            if data['orders']:
                # Buat isi tabel untuk email
                table_content = ''
                for order_data in data['orders']:
                    table_content += f"""
                        <tr>
                            <td>{order_data['order_name']}</td>
                            <td>{order_data['parent_name']}</td>
                            <td>{order_data['site_name']}</td>
                            <td>{order_data['total_amount']}</td>
                        </tr>
                    """

                # Dapatkan email salesperson
                salesperson_email = self.env['res.users'].browse(salesperson_id).partner_id.email

                # Kirim email hanya jika ada data yang dikumpulkan
                template = self.env.ref('indoteknik_custom.mail_template_sale_order_notification_to_salesperson')
                email_body = template.body_html.replace('${table_content}', table_content)
                email_body = email_body.replace('${salesperson_name}', data['name'])
                email_body = email_body.replace('${sum_total_amount}', str(data['sum_total_amount']))
                email_body = email_body.replace('${site}', str(data['site']))
                email_body = email_body.replace('${business_partner}', str(data['business_partner']))
                # Kirim email
                self.env['mail.mail'].create({
                    'subject': 'Notification: Sale Orders',
                    'body_html': email_body,
                    'email_to': salesperson_email,
                }).send()

    def check_credit_limit(self):
        for rec in self:
            outstanding_amount = rec.outstanding_amount
            check_credit_limit = False
            ######
            block_stage = 0
            if rec.partner_id.parent_id:
                if rec.partner_id.parent_id.active_limit and rec.partner_id.parent_id.enable_credit_limit:
                    check_credit_limit = True
            else:
                if rec.partner_id.active_limit and rec.partner_id.enable_credit_limit:
                    check_credit_limit = True

            term_days = 0
            for term_line in rec.payment_term_id.line_ids:
                term_days += term_line.days
            if term_days == 0:
                check_credit_limit = False

            if check_credit_limit:
                if rec.partner_id.parent_id:
                    block_stage = rec.partner_id.parent_id.blocking_stage or 0
                else:
                    block_stage = rec.partner_id.blocking_stage or 0

                if (outstanding_amount + rec.amount_total) >= block_stage:
                    if block_stage != 0:
                        remaining_credit_limit = block_stage - outstanding_amount
                        raise UserError(
                            _("%s is in  Blocking Stage, Remaining credit limit is %s, from %s and outstanding %s")
                            % (rec.partner_id.name, remaining_credit_limit, block_stage, outstanding_amount))

    def check_limit_so_to_invoice(self):
        for rec in self:
            # Ambil jumlah outstanding_amount dan rec.amount_total sebagai current_total
            outstanding_amount = rec.outstanding_amount
            current_total = rec.amount_total + outstanding_amount

            # Ambil blocking stage dari partner
            block_stage = rec.partner_id.parent_id.blocking_stage if rec.partner_id.parent_id else rec.partner_id.blocking_stage or 0
            is_cbd = rec.partner_id.parent_id.property_payment_term_id.id == 26 if rec.partner_id.parent_id else rec.partner_id.property_payment_term_id.id == 26 or False
            partner_term = rec.partner_id.property_payment_term_id
            partner_term_days_total = 0
            if partner_term:
                partner_term_days_total = sum((line.days or 0) for line in partner_term.line_ids)
            is_partner_cbd = (partner_term_days_total == 0)
            is_so_cbd = bool(rec.payment_term_id.id == 26)

            so_to_invoice = 0
            for sale in rec.partner_id.sale_order_ids:
                if sale.invoice_status == 'to invoice':
                    so_to_invoice = so_to_invoice + sale.amount_total

            remaining_credit_limit = block_stage - current_total - so_to_invoice if not is_cbd and not is_partner_cbd else 0

            # Validasi limit
            if remaining_credit_limit <= 0 and block_stage > 0 and not is_cbd and not is_so_cbd and not is_partner_cbd:
                raise UserError(
                    _("The credit limit for %s will exceed the Blocking Stage if the Sale Order is confirmed. The remaining credit limit is %s, from %s and the outstanding amount is %s.")
                    % (rec.partner_id.name, block_stage - current_total, block_stage, outstanding_amount))

    def validate_different_vendor(self):
        if self.vendor_approval_id.filtered(lambda v: v.state == 'draft'):
            draft_names = ", ".join(self.vendor_approval_id.filtered(lambda v: v.state == 'draft').mapped('number'))
            raise UserError(f"SO ini sedang dalam review Vendor Approval: {draft_names}")

        if self.vendor_approval_id and all(v.state != 'draft' for v in self.vendor_approval_id):
            return False

        different_vendor = self.order_line.filtered(
            lambda l: l.vendor_id and l.vendor_md_id and l.vendor_id.id != l.vendor_md_id.id
        )

        if different_vendor:
            vendor_approvals = []
            for line in different_vendor:
                vendor_approval = self.env['vendor.approval'].create({
                    'order_id': self.id,
                    'order_line_id': line.id,
                    'create_date_so': self.create_date,
                    'partner_id': self.partner_id.id,
                    'state': 'draft',
                    'product_id': line.product_id.id,
                    'product_uom_qty': line.product_uom_qty,
                    'vendor_id': line.vendor_id.id,
                    'vendor_md_id': line.vendor_md_id.id,
                    'purchase_price': line.purchase_price,
                    'purchase_price_md': line.purchase_price_md,
                    'sales_price': line.price_unit,
                    'margin_before': line.margin_md,
                    'margin_after': line.item_percent_margin,
                    'purchase_tax_id': line.purchase_tax_id.id,
                    'sales_tax_id': line.tax_id[0].id if line.tax_id else False,
                    'percent_margin_difference': (
                        (line.price_unit - line.purchase_price_md) / line.purchase_price_md
                        if line.purchase_price_md else False
                    ),
                })

                vendor_approvals.append(vendor_approval.id)

            self.vendor_approval_id = [(4, vid) for vid in vendor_approvals]
            return True
        else:
            return False

    def check_archived_product(self):
        for order in self:
            for line in order.order_line:
                if line.product_id.active == False:
                    raise UserError("Terdapat Product yang sudah di Archive pada Product: {}".format(line.product_id.display_name))

    def check_archived_uom(self):
        for order in self:
            for line in order.order_line:
                if line.product_uom.active == False:
                    raise UserError("Terdapat UoM yang sudah di Archive pada UoM {} di Product {}".format(line.product_uom.name, line.product_id.display_name))

    def action_confirm(self):
        for order in self:
            order._validate_delivery_amt()
            order._validate_uniform_taxes()
            order.check_duplicate_product()
            order.check_product_bom()
            order.check_credit_limit()
            order.check_limit_so_to_invoice()
            if self.validate_different_vendor() and not self.vendor_approval:
                return self._create_notification_action('Notification', 'Terdapat Vendor yang berbeda dengan MD Vendor')

            order.check_data_real_delivery_address()
            order.sale_order_check_approve()
            order._validate_order()
            order._validate_npwp()
            order.order_line.validate_line()
            order.check_archived_product()
            order.check_archived_uom()

            main_parent = order.partner_id.get_main_parent()
            SYSTEM_UID = 25
            FROM_WEBSITE = order.create_uid.id == SYSTEM_UID

            if FROM_WEBSITE and main_parent.use_so_approval and order.web_approval not in ['cust_procurement',
                                                                                           'cust_director']:
                raise UserError("This order not yet approved by customer procurement or director")

            if not order.client_order_ref and order.create_date > datetime(2024, 6, 27):
                raise UserError(
                    "Customer Reference kosong, di isi dengan NO PO jika PO tidak ada mohon ditulis Tanpa PO")

            if not order.commitment_date and order.create_date > datetime(2024, 9, 12):
                raise UserError("Expected Delivery Date kosong, wajib diisi")

            if not order.real_shipping_id:
                UserError('Real Delivery Address harus di isi')
                
            if not self.env.context.get('due_approve', []):
                if order.validate_partner_invoice_due():
                    return self._create_notification_action('Notification',
                                                            'Terdapat invoice yang telah melewati batas waktu, mohon perbarui pada dokumen Due Extension')

            if not order._is_request_to_own_team_leader():
                return self._create_notification_action(
                    'Warning', 
                    'Hanya bisa konfirmasi SO tim Anda.'
                )
            if order._requires_approval_margin_leader():
                order.approval_status = 'pengajuan2'
                return self._create_approval_notification('Pimpinan')
            elif order._requires_approval_margin_manager():
                order.approval_status = 'pengajuan1'
                return self._create_approval_notification('Sales Manager')
            elif order._requires_approval_team_sales():
                order.approval_status = 'pengajuan1'
                order.message_post(body="Mengajukan approval ke Team Sales")
                return self._create_approval_notification('Team Sales')

            order.approval_status = 'approved'
            order._set_sppkp_npwp_contact()
            order.calculate_line_no()
            order.send_notif_to_salesperson()
            # order._compute_etrts_date()
            # order.order_line.get_reserved_from()

        res = super(SaleOrder, self).action_confirm()
        for order in self:
            note = []
            for line in order.order_line:
                if line.display_type == 'line_note':
                    note.append(line.name)

            if order.picking_ids:
                # Sort picking_ids by creation date to get the most recent one
                latest_picking = order.picking_ids.sorted(key=lambda p: p.create_date, reverse=True)[0]
                latest_picking.notee = '\n'.join(note)
        return res

    def action_cancel(self):
        # TODO stephan prevent cancel if have invoice, do, and po
        if self.state_ask_cancel != 'approve' and self.state not in ['draft', 'sent']:
            raise UserError("Anda harus approval purchasing terlebih dahulu")
        main_parent = self.partner_id.get_main_parent()
        if self._name != 'sale.order':
            return super(SaleOrder, self).action_cancel()

        if self.have_outstanding_invoice:
            raise UserError("Invoice harus di Cancel dahulu")

        disallow_states = ['draft', 'waiting', 'confirmed', 'assigned']
        for picking in self.picking_ids:
            if picking.state in disallow_states:
                raise UserError("DO yang draft, waiting, confirmed, atau assigned harus di-cancel oleh Logistik")
        for line in self.order_line:
            if line.qty_delivered > 0:
                raise UserError("DO yang done harus di-Return oleh Logistik")

        if not self.web_approval:
            self.web_approval = 'company'
        # elif self.have_outstanding_po:
        #     raise UserError("PO harus di Cancel dahulu")

        self.approval_status = False
        self.due_id = False
        if main_parent.use_so_approval:
            self.send_notif_to_salesperson(cancel=True)
        for order in self:
            if order.amount_total > 30000000:
                return {
                    'type': 'ir.actions.act_window',
                    'name': _('Cancel Reason'),
                    'res_model': 'cancel.reason.order',
                    'view_mode': 'form',
                    'target': 'new',
                    'context': {'default_request_id': self.id},
                }
        return super(SaleOrder, self).action_cancel()

    def confirm_cancel_order(self):
        """Fungsi ini akan dipanggil oleh wizard setelah alasan pembatalan dipilih"""
        if self.state != 'cancel':
            self.state = 'cancel'
        return super(SaleOrder, self).action_cancel()

    def validate_partner_invoice_due(self):
        parent_id = self.partner_id.parent_id.id
        parent_id = parent_id if parent_id else self.partner_id.id

        if self.due_id and self.due_id.is_approve == False:
            raise UserError('Document Over Due Yang Anda Buat Belum Di Approve')

        query = [
            ('partner_id', '=', parent_id),
            ('state', '=', 'posted'),
            ('move_type', '=', 'out_invoice'),
            ('amount_residual_signed', '>', 0)
        ]
        invoices = self.env['account.move'].search(query, order='invoice_date')

        if invoices:
            due_extension = self.env['due.extension'].create([{
                'partner_id': parent_id,
                'day_extension': '3',
                'order_id': self.id,
            }])
            due_extension.generate_due_line()
            self.due_id = due_extension.id
            if len(self.due_id.due_line) > 0:
                return True
            else:
                due_extension.unlink()
                return False

    def _requires_approval_margin_leader(self):
        return self.total_percent_margin <= 15 and not self.env.user.is_leader

    def _requires_approval_margin_manager(self):
        return 15 < self.total_percent_margin < 18 and not self.env.user.is_sales_manager and not self.env.user.id == 375 and not self.env.user.is_leader
    
    def _requires_approval_team_sales(self):
        return (
            18 <= self.total_percent_margin <= 24
            # self.total_percent_margin >= 18
            and self.env.user.id not in [11, 9, 375]  # Eko, Ade, Putra
            and not self.env.user.is_sales_manager
            and not self.env.user.is_leader
        )


    def _is_request_to_own_team_leader(self):
        user = self.env.user

        # Pengecualian Pak Akbar & Darren
        if user.is_leader or user.is_sales_manager:
            return True

        if self.env.context.get("ask_approval") and user.id in (3401, 20, 3988, 17340):
            return True
        
        if not self.env.context.get("ask_approval") and user.id in (3401, 20, 3988, 17340): # admin (fida, nabila, ninda, feby)
            raise UserError("Sales Admin tidak bisa confirm SO, silahkan hubungi Salesperson yang bersangkutan.")
        
        salesperson_id = self.user_id.id
        approver_id = user.id
        team_leader_id = self.team_id.user_id.id
        team = self.env['crm.team'].search([('user_id', '=', approver_id)], limit=1)

        return salesperson_id == approver_id or bool(team)


    def _create_approval_notification(self, approval_role):
        title = 'Warning'
        message = f'SO butuh approval {approval_role}'
        return self._create_notification_action(title, message)

    def _create_notification_action(self, title, message):
        return {
            'type': 'ir.actions.client',
            'tag': 'display_notification',
            'params': {'title': title, 'message': message, 'next': {'type': 'ir.actions.act_window_close'}},
        }

    def _set_sppkp_npwp_contact(self):
        partner = self.partner_id.parent_id or self.partner_id

        # if not partner.sppkp:
        #     partner.sppkp = self.sppkp
        # if not partner.npwp:
        #     partner.npwp = self.npwp
        if not partner.email:
            partner.email = self.email
        # if not partner.customer_type:
        #     partner.customer_type = self.customer_type
        if not partner.user_id:
            partner.user_id = self.user_id.id

    def _compute_total_margin(self):
        for order in self:
            total_margin = sum(line.item_margin for line in order.order_line if line.product_id)
            if order.ongkir_ke_xpdc:
                total_margin -= order.ongkir_ke_xpdc

            order.total_margin = total_margin

    def _compute_total_before_margin(self):
        for order in self:
            total_before_margin = sum(line.item_before_margin for line in order.order_line if line.product_id)
            order.total_before_margin = total_before_margin

    # Perhitungan Lama
    # def _compute_total_percent_margin(self):
    #     for order in self:
    #         if order.amount_untaxed == 0:
    #             order.total_percent_margin = 0
    #             continue
    #         if order.shipping_cost_covered == 'indoteknik':
    #             delivery_amt = order.delivery_amt
    #         else:
    #             delivery_amt = 0
    #
    #         net_margin = order.total_margin - order.biaya_lain_lain
    #
    #         order.total_percent_margin = round(
    #             (net_margin / (order.amount_untaxed - order.fee_third_party)) * 100, 2)

            # order.total_percent_margin = round((order.total_margin / (order.amount_untaxed-delivery_amt-order.fee_third_party)) * 100, 2)
            # order.total_percent_margin = round(
            #     (order.total_margin / (order.amount_untaxed - order.fee_third_party - order.biaya_lain_lain)) * 100, 2)
            # order.total_percent_margin = round((order.total_margin / (order.amount_untaxed)) * 100, 2)

    def _compute_total_percent_margin(self):
        for order in self:
            if order.amount_untaxed == 0:
                order.total_percent_margin = 0
                continue

            if order.shipping_cost_covered == 'indoteknik':
                delivery_amt = order.delivery_amt
            else:
                delivery_amt = 0

            net_margin = order.total_margin - order.fee_third_party - order.biaya_lain_lain


            if order.amount_untaxed > 0:
                order.total_percent_margin = round((net_margin / order.amount_untaxed) * 100, 2)
            else:
                order.total_percent_margin = 0

    # @api.onchange('biaya_lain_lain')
    # def _onchange_biaya_lain_lain(self):
    #     """Ketika biaya_lain_lain berubah, simpan nilai margin sebelumnya"""
    #     if hasattr(self, '_origin') and self._origin.id:
    #         # Hitung margin sebelum biaya_lain_lain ditambahkan
    #         if self.amount_untaxed > 0:
    #             original_net_margin = self.total_margin  # tanpa biaya_lain_lain
    #             self.total_margin_excl_third_party = round(
    #                 (original_net_margin / (self.amount_untaxed - self.fee_third_party)) * 100, 2)

    def _prepare_before_margin_values(self, vals):
        margin_sebelumnya = {}

        margin_affecting_fields = [
            'biaya_lain_lain', 'fee_third_party', 'delivery_amt',
            'ongkir_ke_xpdc', 'shipping_cost_covered', 'order_line'
        ]

        if not any(field in vals for field in margin_affecting_fields):
            return {}

        for order in self:
            if order.amount_untaxed <= 0:
                continue

            current_before = order.total_margin_excl_third_party or 0

            # CASE 1: Before margin masih kosong → ambil dari item_percent_margin
            if current_before == 0:
                line_margin = 0
                for line in order.order_line:
                    if line.item_percent_margin is not None:
                        line_margin = line.item_percent_margin
                        break
                margin_sebelumnya[order.id] = line_margin
                _logger.info(f"[BEFORE] SO {order.name}: Before margin kosong, ambil dari order line: {line_margin}%")
            else:
                # CASE 2: Ada perubahan field yang mempengaruhi margin
                for field in margin_affecting_fields:
                    if field in vals:
                        old_val = getattr(order, field, 0) or 0
                        new_val = vals[field] or 0
                        if old_val != new_val:
                            margin_sebelumnya[order.id] = order.total_percent_margin
                            _logger.info(
                                f"[BEFORE] SO {order.name}: {field} berubah dari {old_val} ke {new_val}, simpan {order.total_percent_margin}%")
                            break

        return margin_sebelumnya

    @api.onchange('sales_tax_id')
    def onchange_sales_tax_id(self):
        for line in self.order_line:
            line.product_id_change()

    def _compute_grand_total(self):
        for order in self:
            if order.shipping_cost_covered == 'customer':
                order.grand_total = order.delivery_amt + order.amount_total
            else:
                order.grand_total = order.amount_total

    def action_apply_voucher(self):
        for line in self.order_line:
            if line.order_promotion_id:
                _logger.warning(f"[CHECKOUT FAILED] Produk promo ditemukan: {line.product_id.display_name}")
                raise UserError('Voucher tidak dapat digabung dengan promotion program')

        voucher = self.voucher_id
        if voucher.limit > 0 and voucher.count_order >= voucher.limit:
            raise UserError('Voucher tidak dapat digunakan karena sudah habis digunakan')

        partner_voucher_orders = []
        for order in voucher.order_ids:
            if order.partner_id.id == self.partner_id.id:
                partner_voucher_orders.append(order)

        if voucher.limit_user > 0 and len(partner_voucher_orders) >= voucher.limit_user:
            raise UserError('Voucher tidak dapat digunakan karena Customer ini sudah menghabiskan kuota voucher')

        if self.pricelist_id.id in [x.id for x in voucher.excl_pricelist_ids]:
            raise UserError('Voucher tidak dapat digunakan karena pricelist ini tidak berlaku pada voucher')

        self.apply_voucher()

    def action_apply_voucher_shipping(self):
        for line in self.order_line:
            if line.order_promotion_id:
                raise UserError('Voucher tidak dapat digabung dengan promotion program')

        voucher = self.voucher_shipping_id
        if voucher.limit > 0 and voucher.count_order >= voucher.limit:
            raise UserError('Voucher tidak dapat digunakan karena sudah habis digunakan')

        partner_voucher_orders = []
        for order in voucher.order_ids:
            if order.partner_id.id == self.partner_id.id:
                partner_voucher_orders.append(order)

        if voucher.limit_user > 0 and len(partner_voucher_orders) >= voucher.limit_user:
            raise UserError('Voucher tidak dapat digunakan karena Customer ini sudah menghabiskan kuota voucher')

        if self.pricelist_id.id in [x.id for x in voucher.excl_pricelist_ids]:
            raise UserError('Voucher tidak dapat digunakan karena pricelist ini tidak berlaku pada voucher')

        self.apply_voucher_shipping()

    def apply_voucher(self):
        def _is_promo_line(line):
            # TRUE jika baris tidak boleh kena voucher
            if getattr(line, 'order_promotion_id', False):
                return True  # baris dari program promo
            if (line.price_unit or 0.0) == 0.0:
                return True  # free item
            if getattr(line, 'is_has_disc', False):
                return True  # sudah promo/flashsale/berdiskon
            if (line.discount or 0.0) >= 100.0:
                return True  # safety
            return False

        # --- LOOP 1: susun input untuk voucher.apply() ---
        order_line = []
        for line in self.order_line:
            if _is_promo_line(line):
                continue
            order_line.append({
                'product_id': line.product_id,
                'price': line.price_unit,
                'discount': line.discount,
                'qty': line.product_uom_qty,
                'subtotal': line.price_subtotal,
            })

        if not order_line:
            return

        voucher = self.voucher_id.apply(order_line)

        # --- LOOP 2: tulis hasilnya HANYA ke non-promo ---
        for line in self.order_line:
            if _is_promo_line(line):
                continue

            line.initial_discount = line.discount

            voucher_type = voucher['type']
            total_map = voucher['total'][voucher_type]
            discount_map = voucher['discount'][voucher_type]

            if voucher_type == 'brand':
                m_id = line.product_id.x_manufacture.id
                used_total = (total_map or {}).get(m_id)
                used_discount = (discount_map or {}).get(m_id)
            else:
                used_total = total_map
                used_discount = discount_map

            if not used_total or not used_discount or (line.product_uom_qty or 0.0) == 0.0:
                continue

            line_contribution = line.price_subtotal / used_total
            line_voucher = used_discount * line_contribution
            per_item_voucher = line_voucher / line.product_uom_qty

            has_ppn_11 = any(tax.id == 23 for tax in line.tax_id)
            base_unit = line.price_unit / 1.11 if has_ppn_11 else line.price_unit

            new_disc_value = base_unit * line.discount / 100 + per_item_voucher
            new_disc_pct = (new_disc_value / base_unit) * 100

            line.amount_voucher_disc = line_voucher
            line.discount = new_disc_pct

            _logger.info(
                "[VOUCHER_APPLIED] SO=%s voucher=%s type=%s line_id=%s product=%s qty=%s discount_pct=%.2f amount_voucher=%s",
                self.name,
                getattr(self.voucher_id, "code", None),
                voucher.get("type"),
                line.id,
                line.product_id.display_name,
                line.product_uom_qty,
                line.discount,
                line.amount_voucher_disc,
            )

        self.amount_voucher_disc = voucher['discount']['all']
        self.applied_voucher_id = self.voucher_id

    def apply_voucher_shipping(self):
        for order in self:
            delivery_amt = order.delivery_amt
            voucher = order.voucher_shipping_id

            if voucher:
                max_discount_amount = voucher.discount_amount
                voucher_type = voucher.discount_type

                if voucher_type == 'fixed_price':
                    discount = max_discount_amount
                elif voucher_type == 'percentage':
                    discount = delivery_amt * (max_discount_amount / 100)

                delivery_amt -= discount

                delivery_amt = max(delivery_amt, 0)

                order.delivery_amt = delivery_amt

            order.amount_voucher_shipping_disc = discount
            order.applied_voucher_shipping_id = order.voucher_id.id

    def cancel_voucher(self):
        self.applied_voucher_id = False
        self.amount_voucher_disc = 0
        for line in self.order_line:
            line.amount_voucher_disc = 0
            line.discount = line.initial_discount
            line.initial_discount = False

    def cancel_voucher_shipping(self):
        self.delivery_amt + self.amount_voucher_shipping_disc
        self.applied_voucher_shipping_id = False
        self.amount_voucher_shipping_disc = 0

    def action_web_approve(self):
        if self.env.uid != self.partner_id.user_id.id:
            raise UserError(
                'You are not authorized to approve this order. Only %s can approve this order.' % self.partner_id.user_id.name)

        self.web_approval = 'company'
        template = self.env.ref('indoteknik_custom.mail_template_sale_order_web_approve_notification')
        template.send_mail(self.id, force_send=True)

        return {
            'type': 'ir.actions.client',
            'tag': 'display_notification',
            'params': {
                'title': 'Notification',
                'message': 'Berhasil approve web order',
                'next': {'type': 'ir.actions.act_window_close'},
            }
        }

    def calculate_selling_price(self):
        # ongkos kirim, biaya pihak ketiga calculate @stephan
        # TODO voucher @stephan
        # vendor hilangin child di field SO Line @stephan
        # button pindahin @stephan
        # last so 1 tahun ke belakang @stephan
        # pastikan harga beli 1 tahun ke belakang jg
        # harga yg didapat dari semua kumpulan parent parner dan child nya
        # counter di klik berapa banyak @stephan
        for order_line in self.order_line:
            if not order_line.product_id:
                continue
            current_time = datetime.now()
            delta_time = current_time - timedelta(days=365)
            delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S')

            # Initialize partners list with parent_id or partner_id
            partners = []
            parent_id = self.partner_id.parent_id or self.partner_id

            # Add all child_ids and the parent itself to partners as IDs
            partners.extend(parent_id.child_ids.ids)
            partners.append(parent_id.id)

            rec_purchase_price, rec_taxes_id, rec_vendor_id = order_line._get_purchase_price(order_line.product_id)
            state = ['sale', 'done']
            last_so = self.env['sale.order.line'].search([
                # ('order_id.partner_id.id', '=', order_line.order_id.partner_id.id),
                ('order_id.partner_id', 'in', partners),
                ('product_id.id', '=', order_line.product_id.id),
                ('order_id.state', 'in', state),
                ('id', '!=', order_line.id),
                ('order_id.date_order', '>=', delta_time)
            ], limit=1, order='create_date desc')

            if last_so and rec_vendor_id != last_so.vendor_id.id:
                last_so = self.env['sale.order.line'].search([
                    # ('order_id.partner_id.id', '=', order_line.order_id.partner_id.id),
                    ('order_id.partner_id', 'in', partners),
                    ('product_id.id', '=', order_line.product_id.id),
                    ('order_id.state', 'in', state),
                    ('vendor_id', '=', rec_vendor_id),
                    ('id', '!=', order_line.id),
                    ('order_id.date_order', '>=', delta_time)
                ], limit=1, order='create_date desc')

                if last_so and rec_purchase_price != last_so.purchase_price:
                    rec_taxes = self.env['account.tax'].search([('id', '=', rec_taxes_id)], limit=1)
                    if rec_taxes.price_include:
                        selling_price = (rec_purchase_price / 1.11) / (
                                1 - (last_so.item_percent_margin_without_deduction / 100))
                    else:
                        selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin_without_deduction / 100))
                    tax_id = last_so.tax_id
                    for tax in tax_id:
                        if tax.price_include:
                            selling_price = selling_price + (selling_price * 11 / 100)
                        else:
                            selling_price = selling_price
                    discount = 0
                elif last_so:
                    selling_price = last_so.price_unit
                    tax_id = last_so.tax_id
                    discount = last_so.discount
                else:
                    selling_price = order_line.price_unit
                    tax_id = order_line.tax_id
                    discount = order_line.discount

            elif last_so and rec_vendor_id == order_line.vendor_id.id and rec_purchase_price != last_so.purchase_price:
                rec_taxes = self.env['account.tax'].search([('id', '=', rec_taxes_id)], limit=1)
                if rec_taxes.price_include:
                    selling_price = (rec_purchase_price / 1.11) / (
                            1 - (last_so.item_percent_margin_without_deduction / 100))
                else:
                    selling_price = rec_purchase_price / (1 - (last_so.item_percent_margin_without_deduction / 100))
                tax_id = last_so.tax_id
                for tax in tax_id:
                    if tax.price_include:
                        selling_price = selling_price + (selling_price * 11 / 100)
                    else:
                        selling_price = selling_price
                discount = 0

            elif last_so:
                selling_price = last_so.price_unit
                tax_id = last_so.tax_id
                discount = last_so.discount

            else:
                selling_price = order_line.price_unit
                tax_id = order_line.tax_id
                discount = order_line.discount
            order_line.price_unit = selling_price
            order_line.tax_id = tax_id
            order_line.discount = discount
            order_line.order_id.use_button = True

    def _auto_set_shipping_from_website(self):
        if not self.env.context.get('from_website_checkout'):
            return

        for order in self:
            # Validasi source website
            if not order.source_id or order.source_id.id != 59:
                continue

            # Skip jika Self Pick Up
            if int(order.carrier_id.id or 0) == 32:
                _logger.info(f"[Checkout] Skip estimasi: Self Pickup untuk SO {order.name}")
                order.select_shipping_option = 'custom'
                continue

            # Simpan pilihan user sebelum estimasi
            user_carrier_id = order.carrier_id.id if order.carrier_id else None
            user_service = order.delivery_service_type
            user_amount = order.delivery_amt
            
            # Jalankan estimasi untuk refresh data
            order.select_shipping_option = 'biteship'
            order.action_estimate_shipping()

            temp_price = self.env.context.get('_temp_delivery_amt')
            temp_service = self.env.context.get('_temp_delivery_service')
            temp_option_id = self.env.context.get('_temp_shipping_option')
            if temp_price and temp_option_id:
                order.shipping_option_id = temp_option_id
                order.delivery_amt = temp_price
                order.delivery_service_type = temp_service

            # Restore pilihan user setelah estimasi
            if user_carrier_id and user_service:
                # Dapatkan provider
                self.env.cr.execute("SELECT name FROM rajaongkir_kurir WHERE delivery_carrier_id = %s LIMIT 1", (user_carrier_id,))
                result = self.env.cr.fetchone()
                provider = result[0].lower() if result else order.env['delivery.carrier'].browse(user_carrier_id).name.lower().split()[0]

                # Cari opsi yang cocok (prioritas: service code > nama > harga > fallback)
                domain_options = [
                    [('courier_service_code', '=', user_service), ('provider', 'ilike', provider)],  # exact service
                    [('name', 'ilike', user_service), ('provider', 'ilike', provider)],              # nama service
                    [('price', '=', user_amount), ('provider', 'ilike', provider)] if user_amount > 0 else None,  # harga sama
                    [('provider', 'ilike', provider)]  # fallback
                ]
                
                matched_option = None
                for domain in domain_options:
                    if domain:
                        matched_option = self.env['shipping.option'].search([('sale_order_id', '=', order.id)] + domain, limit=1)
                        if matched_option:
                            break
                
                # Set opsi yang cocok atau buat manual
                if matched_option:
                    order.shipping_option_id = matched_option.id
                    order.delivery_amt = matched_option.price
                    order.delivery_service_type = matched_option.courier_service_code
                    
                    # Notif jika harga berubah
                    if user_amount > 0 and abs(matched_option.price - user_amount) > 1000:
                        order.message_post(body=f"Harga shipping berubah dari Rp {user_amount:,} ke Rp {matched_option.price:,}")
                
                elif user_amount > 0:
                    # Buat opsi manual jika tidak ada yang cocok
                    manual_option = self.env['shipping.option'].create({
                        'name': f"{provider.upper()} - {user_service}",
                        'price': user_amount,
                        'provider': provider,
                        'courier_service_code': user_service,
                        'sale_order_id': order.id,
                    })
                    order.shipping_option_id = manual_option.id

    @api.model
    def create(self, vals):
        # Ensure partner details are updated when a sale order is created
        order = super(SaleOrder, self).create(vals)
        # _logger.info(f"[CREATE CONTEXT] {self.env.context}")
        # order._auto_set_shipping_from_website()
        order._compute_etrts_date()
        order._validate_expected_ready_ship_date()
        # for line in order.order_line:
        #     updated_vals = line._update_purchase_info()
        #     if updated_vals:
        #         line.write(updated_vals)
        # order._validate_delivery_amt()
        # order._check_total_margin_excl_third_party()
        # order._update_partner_details()
        return order

    # @api.depends('commitment_date')
    def _compute_ready_to_ship_status_detail(self):
        def is_empty(val):
            """Helper untuk cek data kosong yang umum di Odoo."""
            return val is None or val == "" or val == [] or val == {}

        for order in self:
            order.ready_to_ship_status_detail = 'On Track'  # Default value

            # Skip if no commitment date
            if is_empty(order.commitment_date):
                continue

            eta = order.commitment_date
            match_lines = self.env['purchase.order.sales.match'].search([
                ('sale_id', '=', order.id)
            ])

            if match_lines:
                for match in match_lines:
                    po = match.purchase_order_id
                    product = match.product_id
                    po_line = self.env['purchase.order.line'].search([
                        ('order_id', '=', po.id),
                        ('product_id', '=', product.id)
                    ], limit=1)

                    if is_empty(po_line):
                        continue

                    stock_move = self.env['stock.move'].search([
                        ('purchase_line_id', '=', po_line.id)
                    ], limit=1)

                    if is_empty(stock_move) or is_empty(stock_move.picking_id):
                        continue

                    picking_in = stock_move.picking_id
                    result_date = picking_in.date_done

                    if is_empty(result_date):
                        continue

                    try:
                        if result_date < eta:
                            order.ready_to_ship_status_detail = f"Early (Actual: {result_date.strftime('%m/%d/%Y')})"
                        else:
                            order.ready_to_ship_status_detail = f"Delay (Actual: {result_date.strftime('%m/%d/%Y')})"
                    except Exception as e:
                        _logger.error(f"Error computing ready to ship status: {str(e)}")
                        continue

    def write(self, vals):

        margin_sebelumnya = self._prepare_before_margin_values(vals)

        for order in self:
            if order.state in ['sale', 'cancel']:
                if 'order_line' in vals:
                    for command in vals.get('order_line', []):
                        if command[0] == 0:
                            raise UserError(
                                "SO tidak dapat ditambahkan produk baru karena SO sudah menjadi sale order.")

            order._update_delivery_service_type_from_shipping_option(vals)

        if 'carrier_id' in vals:
            for order in self:
                for picking in order.picking_ids:
                    if picking.state == 'assigned':
                        picking.carrier_id = vals['carrier_id']

                for picking in order.picking_ids:
                    if picking.state not in ['done', 'cancel', 'assigned']:
                        picking.write({'carrier_id': vals['carrier_id']})

        try:
            helper_ids = self._get_helper_ids()
            if str(self.env.user.id) in helper_ids:
                vals['helper_by_id'] = self.env.user.id
        except:
            pass

        #payment term vals
        if 'payment_term_id' in vals and any(
                order.approval_status in ['pengajuan1', 'pengajuan2', 'approved'] for order in self):
            raise UserError(
                "Payment Term tidak dapat diubah karena Sales Order sedang dalam proses approval atau sudah diapprove.")

        if 'payment_term_id' in vals:
            for order in self:
                partner = order.partner_id.parent_id or order.partner_id
                customer_payment_term = partner.property_payment_term_id
                if vals['payment_term_id'] != customer_payment_term.id and not order.partner_id.id == 29179:
                    raise UserError(
                        f"Payment Term berbeda pada Master Data Customer. "
                        f"Harap ganti ke '{customer_payment_term.name}' "
                        f"sesuai dengan payment term yang terdaftar pada customer."
                    )
                
                if order.partner_id.id == 29179 and vals['payment_term_id'] not in [25,28]:
                    raise UserError(_("Pilih payment term 60 hari atau 30 hari."))


        res = super(SaleOrder, self).write(vals)

        # Update before margin setelah write
        if margin_sebelumnya:
            for order_id, margin_value in margin_sebelumnya.items():
                _logger.info(f"[UPDATE] SO ID {order_id}: Set before margin ke {margin_value}%")
                self.env.cr.execute("""
                    UPDATE sale_order 
                    SET total_margin_excl_third_party = %s 
                    WHERE id = %s
                """, (margin_value, order_id))

            self.env.cr.commit()
            self.invalidate_cache(['total_margin_excl_third_party'])

        # Validasi setelah write
        if any(field in vals for field in ['delivery_amt', 'carrier_id', 'shipping_cost_covered']):
            self._validate_delivery_amt()

        if any(field in vals for field in ["order_line", "client_order_ref"]):
            self._calculate_etrts_date()

        # for order in self:
        #     for line in order.order_line:
        #         updated_vals = line._update_purchase_info()
        #         if updated_vals:
        #             line.write(updated_vals)


        if 'real_shipping_id' in vals:
            self.action_set_shipping_id()
        return res
    
    def button_refund(self):
        self.ensure_one()
        
        invoice_ids = self.invoice_ids.filtered(lambda inv: inv.state == 'posted')

        moves = self.env['account.move'].search([
            ('sale_id', '=', self.id),
            ('journal_id', '=', 11),
            ('state', '=', 'posted'),
        ])
        piutangbca = self.env['account.move']
        piutangmdr = self.env['account.move']
        cabinvoice = self.env['account.move']

        for inv_name in invoice_ids.mapped('name'):
            piutangbca |= self.env['account.move'].search([
                ('ref', 'ilike', inv_name),
                ('journal_id', '=', 4),
                ('state', '=', 'posted'),
            ])
            piutangmdr |= self.env['account.move'].search([
                ('ref', 'ilike', inv_name),
                ('journal_id', '=', 7),
                ('state', '=', 'posted'),
            ])
            cabinvoice |= self.env['account.move'].search([
                ('ref', 'ilike', inv_name),
                ('journal_id', '=', 11),
                ('state', '=', 'posted'),
            ])

        moves2 = self.env['account.move'].search([
            ('ref', 'ilike', self.name),
            ('journal_id', '=', 11),
            ('state', '=', 'posted'),
        ])

        # Default 0
        total_uang_muka = 0.0  

        has_moves = bool(moves)
        has_moves2 = bool(moves2)
        has_piutangmdr = bool(piutangmdr)
        has_piutangbca = bool(piutangbca)
        has_cabinvoice = bool(cabinvoice)
        has_settlement = self.payment_status == 'settlement'

        if has_moves and has_settlement:
            total_uang_muka = sum(moves.mapped('amount_total_signed')) + self.gross_amount
        elif has_moves:
            total_uang_muka = sum(moves.mapped('amount_total_signed'))
        elif has_moves2:
            total_uang_muka = sum(moves2.mapped('amount_total_signed'))
        elif has_settlement:
            total_uang_muka = self.gross_amount
        elif has_cabinvoice:
            total_uang_muka = sum(cabinvoice.mapped('amount_total_signed'))
        elif has_piutangbca:
            total_uang_muka = sum(piutangbca.mapped('amount_total_signed'))
        elif has_piutangmdr:
            total_uang_muka = sum(piutangmdr.mapped('amount_total_signed'))
        else:
            raise UserError(
                "Tidak bisa melakukan refund karena SO tidak memiliki Record Uang Masuk "
                "(Journal Uang Muka/Payment Invoices/Midtrans Payment)."
            )
        total_refunded = sum(self.refund_ids.mapped('amount_refund'))
        sisa_uang_muka = total_uang_muka - total_refunded

        if sisa_uang_muka <= 0:
            raise UserError("❌ Tidak ada sisa transaksi untuk di-refund. Semua dana sudah dikembalikan.")

        return {
            'name': 'Refund Sale Order',
            'type': 'ir.actions.act_window',
            'res_model': 'refund.sale.order',
            'view_mode': 'form',
            'target':'new',
            'target': 'current',
            'context': {
                'default_sale_order_ids': [(6, 0, [self.id])],
                'default_invoice_ids': [(6, 0, invoice_ids.ids)],
                'default_uang_masuk': sisa_uang_muka,
                'default_ongkir': self.delivery_amt or 0.0,
                'default_bank': '',  
                'default_account_name': '',
                'default_account_no': '',
                'default_refund_type': '',
            },
        }
    
    def open_form_multi_create_refund(self):
        if not self:
            raise UserError("Tidak ada Sale Order yang dipilih.")
        
        if len(self) > 1:
            not_cancel_orders = self.filtered(lambda so: so.state != 'cancel')
            if not_cancel_orders:
                raise ValidationError(
                    f"❌ Refund Multi SO hanya bisa dibuat untuk SO dengan status Cancel. "
                    f"SO berikut tidak Cancel: {', '.join(not_cancel_orders.mapped('name'))}"
                )

        
        invalid_status_orders = []
        for order in self:
            if order.state not in ['cancel', 'sale']:
                invalid_status_orders.append(order.name)
            elif order.state == 'sale':
                not_done_pickings = order.picking_ids.filtered(lambda p: p.state != 'done')
                if not_done_pickings:
                    invalid_status_orders.append(order.name)

        if invalid_status_orders:
            raise ValidationError(
                f"❌ Refund tidak bisa dibuat untuk SO {', '.join(invalid_status_orders)}. "
                f"SO harus Cancel atau Sale dengan semua Pengiriman sudah selesai."
            )
        
        partner_set = set(self.mapped('partner_id.id'))
        if len(partner_set) > 1:
            raise UserError("Tidak dapat membuat refund untuk Multi SO dengan Customer berbeda. Harus memiliki Customer yang sama.")

        invoice_status_set = set(self.mapped('invoice_status'))
        if len(invoice_status_set) > 1:
            raise UserError("Tidak dapat membuat refund untuk SO dengan status invoice berbeda. Harus memiliki status invoice yang sama.")
        
        refunded_orders = self.filtered(lambda so: self.env['refund.sale.order'].search([('sale_order_ids', 'in', so.id)], limit=1))
        if refunded_orders:
            raise ValidationError(
                f"SO {', '.join(refunded_orders.mapped('name'))} sudah pernah di-refund dan tidak bisa ikut dalam refund Multi SO."
            )
        
        total_uang_masuk = 0.0
        invalid_orders=[]
        for order in self:
            moves = self.env['account.move'].search([
                ('sale_id', '=', order.id),
                ('journal_id', '=', 11),
                ('state', '=', 'posted'),
            ])

            moves2 = self.env['account.move'].search([
                ('ref', 'ilike', order.name),
                ('journal_id', '=', 11),
                ('state', '=', 'posted'),
            ])

            total_uang_muka = 0.0  

            if moves and order.payment_status == 'settlement':
                total_uang_muka = order.gross_amount + sum(moves.mapped('amount_total_signed')) or 0.0
            elif moves:
                total_uang_muka = sum(moves.mapped('amount_total_signed')) or 0.0
            elif moves2:
                total_uang_muka = sum(moves2.mapped('amount_total_signed')) or 0.0
            elif order.payment_status == 'settlement':
                total_uang_muka = order.gross_amount
            else:
                invalid_orders.append(order.name)

            total_uang_masuk += total_uang_muka

        if invalid_orders:
            raise ValidationError(
                f"Tidak dapat membuat refund untuk SO {', '.join(invalid_orders)} karena tidak memiliki Record Uang Masuk (Journal Uang Muka/Midtrans).\n"
                "Pastikan semua SO yang dipilih sudah memiliki Record pembayaran yang valid."
            )
        

        invoice_ids = self.mapped('invoice_ids').filtered(lambda inv: inv.state != 'cancel')
        delivery_total = sum(self.mapped('delivery_amt'))

        return {
            'type': 'ir.actions.act_window',
            'name': 'Create Refund',
            'res_model': 'refund.sale.order',
            'view_mode': 'form',
            'target': 'current',  
            'context': {
                'default_sale_order_ids': [(6, 0, self.ids)],
                'default_invoice_ids': [(6, 0, invoice_ids.ids)],
                'default_uang_masuk': total_uang_masuk,
                'default_ongkir': delivery_total,
                'default_bank': '',
                'default_account_name': '',
                'default_account_no': '',
                'default_refund_type': '',
            }
        }
    
    def action_view_related_refunds(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_window',
            'name': 'Refunds',
            'res_model': 'refund.sale.order',
            'view_mode': 'tree,form',
            'domain': [('sale_order_ids', 'in', [self.id])],
            'context': {'default_sale_order_ids': [self.id]},
        }
    
    def _compute_refund_ids(self):
        for order in self:
            refunds = self.env['refund.sale.order'].search([
                ('sale_order_ids', 'in', [order.id])
            ])
            order.refund_ids = refunds

    def _compute_refund_count(self):
        for order in self:
            order.refund_count = self.env['refund.sale.order'].search_count([
                ('sale_order_ids', 'in', order.id)
            ])

    @api.depends('invoice_ids')
    def _compute_advance_payment_move(self):
        for order in self:
            move = self.env['account.move'].search([
                ('sale_id', '=', order.id),
                ('journal_id', '=', 11),
                ('state', '=', 'posted'),
            ], limit=1, order="id desc")
            order.advance_payment_move_id = move
        
    @api.depends('invoice_ids')
    def _compute_advance_payment_moves(self):
        for order in self:
            moves = self.env['account.move'].search([
                ('sale_id', '=', order.id),
                ('journal_id', '=', 11),
                ('state', '=', 'posted'),
            ])
            order.advance_payment_move_ids = moves

    @api.depends('invoice_ids')
    def _compute_advance_payment_moves(self):
        for order in self:
            moves = self.env['account.move'].search([
                ('sale_id', '=', order.id),
                ('journal_id', '=', 11),
                ('state', '=', 'posted'),
            ])
            order.advance_payment_move_ids = moves
            order.advance_payment_move_count = len(moves)

    def action_open_advance_payment_moves(self):
        self.ensure_one()
        moves = self.advance_payment_move_ids
        if not moves:
            return
        return {
            'type': 'ir.actions.act_window',
            'name': 'Journals Sales Order',
            'res_model': 'account.move',
            'view_mode': 'tree,form',
            'domain': [('id', 'in', moves.ids)],
            'target': 'current',
        }

    def action_open_sjo(self):
        return {
        'name': 'SJO',
        'type': 'ir.actions.act_window',
        'res_model': 'sourcing.job.order', 
        'view_mode': 'form',
        'target': 'current',
        'context': {
                'default_so_id': self.id,
            }
        }