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 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import calendar
from collections import defaultdict
from contextlib import ExitStack, contextmanager
from datetime import date, timedelta
from dateutil.relativedelta import relativedelta
from hashlib import sha256
from json import dumps
import logging
from markupsafe import Markup
import math
import re
from textwrap import shorten
from odoo import api, fields, models, _, Command, SUPERUSER_ID, modules, tools
from odoo.tools.sql import column_exists, create_column
from odoo.addons.account.tools import format_structured_reference_iso
from odoo.exceptions import UserError, ValidationError, AccessError, RedirectWarning
from odoo.osv import expression
from odoo.tools import (
create_index,
date_utils,
float_compare,
float_is_zero,
float_repr,
format_amount,
format_date,
formatLang,
frozendict,
get_lang,
groupby,
index_exists,
OrderedSet,
SQL,
)
from odoo.tools.mail import email_re, email_split, is_html_empty
_logger = logging.getLogger(__name__)
MAX_HASH_VERSION = 4
PAYMENT_STATE_SELECTION = [
('not_paid', 'Not Paid'),
('in_payment', 'In Payment'),
('paid', 'Paid'),
('partial', 'Partially Paid'),
('reversed', 'Reversed'),
('blocked', 'Blocked'),
('invoicing_legacy', 'Invoicing App Legacy'),
]
TYPE_REVERSE_MAP = {
'entry': 'entry',
'out_invoice': 'out_refund',
'out_refund': 'entry',
'in_invoice': 'in_refund',
'in_refund': 'entry',
'out_receipt': 'out_refund',
'in_receipt': 'in_refund',
}
ALLOWED_MIMETYPES = {
'text/plain',
'text/csv',
'application/pdf',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.oasis.opendocument.spreadsheet',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
}
EMPTY = object()
class AccountMove(models.Model):
_name = "account.move"
_inherit = ['portal.mixin', 'mail.thread.main.attachment', 'mail.activity.mixin', 'sequence.mixin', 'product.catalog.mixin']
_description = "Journal Entry"
_order = 'date desc, name desc, invoice_date desc, id desc'
_mail_post_access = 'read'
_check_company_auto = True
_sequence_index = "journal_id"
_rec_names_search = ['name', 'partner_id.name', 'ref']
_systray_view = 'activity'
_mailing_enabled = True
@property
def _sequence_monthly_regex(self):
return self.journal_id.sequence_override_regex or super()._sequence_monthly_regex
@property
def _sequence_yearly_regex(self):
return self.journal_id.sequence_override_regex or super()._sequence_yearly_regex
@property
def _sequence_fixed_regex(self):
return self.journal_id.sequence_override_regex or super()._sequence_fixed_regex
@property
def _sequence_year_range_monthly_regex(self):
return self.journal_id.sequence_override_regex or super()._sequence_year_range_monthly_regex
# ==============================================================================================
# JOURNAL ENTRY
# ==============================================================================================
# === Accounting fields === #
name = fields.Char(
string='Number',
compute='_compute_name', inverse='_inverse_name', readonly=False, store=True,
copy=False,
tracking=True,
index='trigram',
)
ref = fields.Char(
string='Reference',
copy=False,
tracking=True,
index='trigram',
)
date = fields.Date(
string='Date',
index=True,
compute='_compute_date', store=True, required=True, readonly=False, precompute=True,
copy=False,
tracking=True,
)
state = fields.Selection(
selection=[
('draft', 'Draft'),
('posted', 'Posted'),
('cancel', 'Cancelled'),
],
string='Status',
required=True,
readonly=True,
copy=False,
tracking=True,
default='draft',
)
move_type = fields.Selection(
selection=[
('entry', 'Journal Entry'),
('out_invoice', 'Customer Invoice'),
('out_refund', 'Customer Credit Note'),
('in_invoice', 'Vendor Bill'),
('in_refund', 'Vendor Credit Note'),
('out_receipt', 'Sales Receipt'),
('in_receipt', 'Purchase Receipt'),
],
string='Type',
required=True,
readonly=True,
tracking=True,
change_default=True,
index=True,
default="entry",
)
is_storno = fields.Boolean(
compute='_compute_is_storno', store=True, readonly=False,
copy=False,
)
journal_id = fields.Many2one(
'account.journal',
string='Journal',
compute='_compute_journal_id', inverse='_inverse_journal_id', store=True, readonly=False, precompute=True,
required=True,
check_company=True,
domain="[('id', 'in', suitable_journal_ids)]",
)
journal_group_id = fields.Many2one(
'account.journal.group',
string='Ledger',
store=False,
search='_search_journal_group_id',
)
company_id = fields.Many2one(
comodel_name='res.company',
string='Company',
compute='_compute_company_id', inverse='_inverse_company_id', store=True, readonly=False, precompute=True,
index=True,
)
line_ids = fields.One2many(
'account.move.line',
'move_id',
string='Journal Items',
copy=True,
)
# === Payment fields === #
origin_payment_id = fields.Many2one( # the payment this is the journal entry of
comodel_name='account.payment',
string="Payment",
index='btree_not_null',
copy=False,
check_company=True,
)
matched_payment_ids = fields.Many2many( # the payments linked to this invoice
string="Matched Payments",
comodel_name='account.payment',
relation='account_move__account_payment',
column1='invoice_id',
column2='payment_id',
copy=False,
)
payment_count = fields.Integer(compute='_compute_payment_count')
# === Statement fields === #
statement_line_id = fields.Many2one(
comodel_name='account.bank.statement.line',
string="Statement Line",
copy=False,
check_company=True,
index='btree_not_null',
)
statement_id = fields.Many2one(
related="statement_line_id.statement_id"
)
# === Cash basis feature fields === #
# used to keep track of the tax cash basis reconciliation. This is needed
# when cancelling the source: it will post the inverse journal entry to
# cancel that part too.
tax_cash_basis_rec_id = fields.Many2one(
comodel_name='account.partial.reconcile',
index='btree_not_null',
string='Tax Cash Basis Entry of',
)
tax_cash_basis_origin_move_id = fields.Many2one(
comodel_name='account.move',
index='btree_not_null',
string="Cash Basis Origin",
readonly=True,
help="The journal entry from which this tax cash basis journal entry has been created.",
)
tax_cash_basis_created_move_ids = fields.One2many(
string="Cash Basis Entries",
comodel_name='account.move',
inverse_name='tax_cash_basis_origin_move_id',
help="The cash basis entries created from the taxes on this entry, when reconciling its lines.",
)
# used by cash basis taxes, telling the lines of the move are always
# exigible. This happens if the move contains no payable or receivable line.
always_tax_exigible = fields.Boolean(compute='_compute_always_tax_exigible', store=True, readonly=False)
# === Misc fields === #
auto_post = fields.Selection(
string='Auto-post',
selection=[
('no', 'No'),
('at_date', 'At Date'),
('monthly', 'Monthly'),
('quarterly', 'Quarterly'),
('yearly', 'Yearly'),
],
default='no', required=True, copy=False,
help='Specify whether this entry is posted automatically on its accounting date, and any similar recurring invoices.')
auto_post_until = fields.Date(
string='Auto-post until',
copy=False,
compute='_compute_auto_post_until', store=True, readonly=False,
help='This recurring move will be posted up to and including this date.')
auto_post_origin_id = fields.Many2one(
comodel_name='account.move',
string='First recurring entry',
readonly=True, copy=False,
index='btree_not_null',
)
hide_post_button = fields.Boolean(compute='_compute_hide_post_button', readonly=True)
checked = fields.Boolean(
string='Checked',
tracking=True,
help="If this checkbox is not ticked, it means that the user was not sure of all the related "
"information at the time of the creation of the move and that the move needs to be "
"checked again.",
)
posted_before = fields.Boolean(copy=False)
suitable_journal_ids = fields.Many2many(
'account.journal',
compute='_compute_suitable_journal_ids',
)
highest_name = fields.Char(compute='_compute_highest_name')
made_sequence_gap = fields.Boolean(compute='_compute_made_sequence_gap', store=True) # store wether this is the first move breaking the natural sequencing
show_name_warning = fields.Boolean(store=False)
type_name = fields.Char('Type Name', compute='_compute_type_name')
country_code = fields.Char(related='company_id.account_fiscal_country_id.code', readonly=True)
company_price_include = fields.Selection(related='company_id.account_price_include', readonly=True)
attachment_ids = fields.One2many('ir.attachment', 'res_id', domain=[('res_model', '=', 'account.move')], string='Attachments')
audit_trail_message_ids = fields.One2many(
'mail.message',
'res_id',
domain=[
('model', '=', 'account.move'),
('message_type', '=', 'notification'),
],
string='Audit Trail Messages',
)
# === Hash Fields === #
restrict_mode_hash_table = fields.Boolean(related='journal_id.restrict_mode_hash_table')
secure_sequence_number = fields.Integer(string="Inalterability No Gap Sequence #", readonly=True, copy=False, index=True)
inalterable_hash = fields.Char(string="Inalterability Hash", readonly=True, copy=False, index='btree_not_null')
secured = fields.Boolean(
compute="_compute_secured",
search='_search_secured',
help="The entry is secured with an inalterable hash."
)
# ==============================================================================================
# INVOICE
# ==============================================================================================
invoice_line_ids = fields.One2many( # /!\ invoice_line_ids is just a subset of line_ids.
'account.move.line',
'move_id',
string='Invoice lines',
copy=False,
domain=[('display_type', 'in', ('product', 'line_section', 'line_note'))],
)
# === Date fields === #
invoice_date = fields.Date(
string='Invoice/Bill Date',
index=True,
copy=False,
)
invoice_date_due = fields.Date(
string='Due Date',
compute='_compute_invoice_date_due', store=True, readonly=False,
index=True,
copy=False,
)
delivery_date = fields.Date(
string='Delivery Date',
copy=False,
store=True,
compute='_compute_delivery_date',
)
show_delivery_date = fields.Boolean(compute='_compute_show_delivery_date')
invoice_payment_term_id = fields.Many2one(
comodel_name='account.payment.term',
string='Payment Terms',
compute='_compute_invoice_payment_term_id', store=True, readonly=False, precompute=True,
inverse='_inverse_invoice_payment_term_id',
check_company=True,
)
needed_terms = fields.Binary(compute='_compute_needed_terms', exportable=False)
needed_terms_dirty = fields.Boolean(compute='_compute_needed_terms')
tax_calculation_rounding_method = fields.Selection(
related='company_id.tax_calculation_rounding_method',
string='Tax calculation rounding method', readonly=True)
# === Partner fields === #
partner_id = fields.Many2one(
'res.partner',
string='Partner',
readonly=False,
tracking=True,
inverse='_inverse_partner_id',
check_company=True,
change_default=True,
index=True,
ondelete='restrict',
)
commercial_partner_id = fields.Many2one(
'res.partner',
string='Commercial Entity',
compute='_compute_commercial_partner_id', store=True, readonly=True,
ondelete='restrict',
check_company=True,
)
partner_shipping_id = fields.Many2one(
comodel_name='res.partner',
string='Delivery Address',
compute='_compute_partner_shipping_id', store=True, readonly=False, precompute=True,
check_company=True,
help="The delivery address will be used in the computation of the fiscal position.",
)
partner_bank_id = fields.Many2one(
'res.partner.bank',
string='Recipient Bank',
compute='_compute_partner_bank_id', store=True, readonly=False,
help="Bank Account Number to which the invoice will be paid. "
"A Company bank account if this is a Customer Invoice or Vendor Credit Note, "
"otherwise a Partner bank account number.",
check_company=True,
tracking=True,
ondelete='restrict',
)
fiscal_position_id = fields.Many2one(
'account.fiscal.position',
string='Fiscal Position',
check_company=True,
compute='_compute_fiscal_position_id', store=True, readonly=False, precompute=True,
ondelete="restrict",
help="Fiscal positions are used to adapt taxes and accounts for particular "
"customers or sales orders/invoices. The default value comes from the customer.",
)
# === Payment fields === #
payment_reference = fields.Char(
string='Payment Reference',
index='trigram',
copy=False,
help="The payment reference to set on journal items.",
tracking=True,
compute='_compute_payment_reference', inverse='_inverse_payment_reference', store=True, readonly=False,
)
display_qr_code = fields.Boolean(
string="Display QR-code",
compute='_compute_display_qr_code',
)
qr_code_method = fields.Selection(
string="Payment QR-code", copy=False,
selection=lambda self: self.env['res.partner.bank'].get_available_qr_methods_in_sequence(),
help="Type of QR-code to be generated for the payment of this invoice, "
"when printing it. If left blank, the first available and usable method "
"will be used.",
)
# === Payment widget fields === #
invoice_outstanding_credits_debits_widget = fields.Binary(
groups="account.group_account_invoice,account.group_account_readonly",
compute='_compute_payments_widget_to_reconcile_info',
exportable=False,
)
invoice_has_outstanding = fields.Boolean(
groups="account.group_account_invoice,account.group_account_readonly",
compute='_compute_payments_widget_to_reconcile_info',
)
invoice_payments_widget = fields.Binary(
groups="account.group_account_invoice,account.group_account_readonly",
compute='_compute_payments_widget_reconciled_info',
exportable=False,
)
preferred_payment_method_line_id = fields.Many2one(
string="Preferred Payment Method Line",
comodel_name='account.payment.method.line',
compute='_compute_preferred_payment_method_line_id',
store=True,
readonly=False,
)
# === Currency fields === #
company_currency_id = fields.Many2one(
string='Company Currency',
related='company_id.currency_id', readonly=True,
)
currency_id = fields.Many2one(
'res.currency',
string='Currency',
tracking=True,
required=True,
compute='_compute_currency_id', inverse='_inverse_currency_id', store=True, readonly=False, precompute=True,
)
invoice_currency_rate = fields.Float(
string='Currency Rate',
compute='_compute_invoice_currency_rate', store=True, precompute=True,
copy=False,
digits=0,
help="Currency rate from company currency to document currency.",
)
# === Amount fields === #
direction_sign = fields.Integer(
compute='_compute_direction_sign',
help="Multiplicator depending on the document type, to convert a price into a balance",
)
amount_untaxed = fields.Monetary(
string='Untaxed Amount',
compute='_compute_amount', store=True, readonly=True,
tracking=True,
)
amount_tax = fields.Monetary(
string='Tax',
compute='_compute_amount', store=True, readonly=True,
)
amount_total = fields.Monetary(
string='Total',
compute='_compute_amount', store=True, readonly=True,
inverse='_inverse_amount_total',
)
amount_residual = fields.Monetary(
string='Amount Due',
compute='_compute_amount', store=True,
)
amount_untaxed_signed = fields.Monetary(
string='Untaxed Amount Signed',
compute='_compute_amount', store=True, readonly=True,
currency_field='company_currency_id',
)
amount_untaxed_in_currency_signed = fields.Monetary(
string="Untaxed Amount Signed Currency",
compute='_compute_amount', store=True, readonly=True,
currency_field='currency_id',
)
amount_tax_signed = fields.Monetary(
string='Tax Signed',
compute='_compute_amount', store=True, readonly=True,
currency_field='company_currency_id',
)
amount_total_signed = fields.Monetary(
string='Total Signed',
compute='_compute_amount', store=True, readonly=True,
currency_field='company_currency_id',
)
amount_total_in_currency_signed = fields.Monetary(
string='Total in Currency Signed',
compute='_compute_amount', store=True, readonly=True,
currency_field='currency_id',
)
amount_residual_signed = fields.Monetary(
string='Amount Due Signed',
compute='_compute_amount', store=True,
currency_field='company_currency_id',
)
tax_totals = fields.Binary(
string="Invoice Totals",
compute='_compute_tax_totals',
inverse='_inverse_tax_totals',
help='Edit Tax amounts if you encounter rounding issues.',
exportable=False,
)
payment_state = fields.Selection(
selection=PAYMENT_STATE_SELECTION,
string="Payment Status",
compute='_compute_payment_state', store=True, readonly=True,
copy=False,
tracking=True,
)
status_in_payment = fields.Selection(
selection=PAYMENT_STATE_SELECTION + [
('draft', "Draft"),
('cancel', "Cancelled"),
],
compute='_compute_status_in_payment',
copy=False,
)
amount_total_words = fields.Char(
string="Amount total in words",
compute="_compute_amount_total_words",
)
# === Reverse feature fields === #
reversed_entry_id = fields.Many2one(
comodel_name='account.move',
string="Reversal of",
index='btree_not_null',
readonly=True,
copy=False,
check_company=True,
)
reversal_move_ids = fields.One2many('account.move', 'reversed_entry_id')
# === Vendor bill fields === #
invoice_vendor_bill_id = fields.Many2one(
'account.move',
store=False,
check_company=True,
string='Vendor Bill',
help="Auto-complete from a past bill.",
)
invoice_source_email = fields.Char(string='Source Email', tracking=True)
invoice_partner_display_name = fields.Char(compute='_compute_invoice_partner_display_info', store=True)
is_manually_modified = fields.Boolean()
# === Fiduciary mode fields === #
quick_edit_mode = fields.Boolean(compute='_compute_quick_edit_mode')
quick_edit_total_amount = fields.Monetary(
string='Total (Tax inc.)',
help='Use this field to encode the total amount of the invoice.\n'
'Odoo will automatically create one invoice line with default values to match it.',
)
quick_encoding_vals = fields.Binary(compute='_compute_quick_encoding_vals', exportable=False)
# === Misc Information === #
narration = fields.Html(
string='Terms and Conditions',
compute='_compute_narration', store=True, readonly=False,
)
is_move_sent = fields.Boolean(
readonly=True,
copy=False,
tracking=True,
help="It indicates that the invoice/payment has been sent or the PDF has been generated.",
)
is_being_sent = fields.Boolean(
help="Is the move being sent asynchronously",
compute='_compute_is_being_sent'
)
move_sent_values = fields.Selection(
selection=[
('sent', 'Sent'),
('not_sent', 'Not Sent'),
],
string='Sent',
compute='compute_move_sent_values',
)
invoice_user_id = fields.Many2one(
string='Salesperson',
comodel_name='res.users',
copy=False,
tracking=True,
compute='_compute_invoice_default_sale_person',
store=True,
readonly=False,
)
# Technical field used to fit the generic behavior in mail templates.
user_id = fields.Many2one(string='User', related='invoice_user_id')
invoice_origin = fields.Char(
string='Origin',
readonly=True,
tracking=True,
help="The document(s) that generated the invoice.",
)
invoice_incoterm_id = fields.Many2one(
comodel_name='account.incoterms',
string='Incoterm',
default=lambda self: self.env.company.incoterm_id,
help='International Commercial Terms are a series of predefined commercial '
'terms used in international transactions.',
)
incoterm_location = fields.Char(
string='Incoterm Location',
compute='_compute_incoterm_location',
readonly=False,
store=True,
)
invoice_cash_rounding_id = fields.Many2one(
comodel_name='account.cash.rounding',
string='Cash Rounding Method',
help='Defines the smallest coinage of the currency that can be used to pay by cash.',
)
sending_data = fields.Json(copy=False)
invoice_pdf_report_id = fields.Many2one(
comodel_name='ir.attachment',
string="PDF Attachment",
compute=lambda self: self._compute_linked_attachment_id('invoice_pdf_report_id', 'invoice_pdf_report_file'),
depends=['invoice_pdf_report_file']
)
invoice_pdf_report_file = fields.Binary(
attachment=True,
string="PDF File",
copy=False,
)
# === Display purpose fields === #
# used to have a dynamic domain on journal / taxes in the form view.
invoice_filter_type_domain = fields.Char(compute='_compute_invoice_filter_type_domain')
bank_partner_id = fields.Many2one(
comodel_name='res.partner',
compute='_compute_bank_partner_id',
help='Technical field to get the domain on the bank',
)
# used to display a message when the invoice's accounting date is prior of the tax lock date
tax_lock_date_message = fields.Char(compute='_compute_tax_lock_date_message')
# used for tracking the status of the currency
display_inactive_currency_warning = fields.Boolean(compute="_compute_display_inactive_currency_warning")
tax_country_id = fields.Many2one( # used to filter the available taxes depending on the fiscal country and fiscal position.
comodel_name='res.country',
compute='_compute_tax_country_id',
)
tax_country_code = fields.Char(compute="_compute_tax_country_code")
has_reconciled_entries = fields.Boolean(compute="_compute_has_reconciled_entries")
show_reset_to_draft_button = fields.Boolean(compute='_compute_show_reset_to_draft_button')
partner_credit_warning = fields.Text(
compute='_compute_partner_credit_warning',
groups="account.group_account_invoice,account.group_account_readonly",
)
partner_credit = fields.Monetary(compute='_compute_partner_credit')
duplicated_ref_ids = fields.Many2many(comodel_name='account.move', compute='_compute_duplicated_ref_ids')
need_cancel_request = fields.Boolean(compute='_compute_need_cancel_request')
show_update_fpos = fields.Boolean(string="Has Fiscal Position Changed", store=False) # True if the fiscal position was changed
# used to display the various dates and amount dues on the invoice's PDF
payment_term_details = fields.Binary(compute="_compute_payment_term_details", exportable=False)
show_payment_term_details = fields.Boolean(compute="_compute_show_payment_term_details")
show_discount_details = fields.Boolean(compute="_compute_show_payment_term_details")
abnormal_amount_warning = fields.Text(compute='_compute_abnormal_warnings')
abnormal_date_warning = fields.Text(compute='_compute_abnormal_warnings')
taxes_legal_notes = fields.Html(string='Taxes Legal Notes', compute='_compute_taxes_legal_notes')
# payment_date is the minimum payment_date of the unpaid lines of the move.
next_payment_date = fields.Date(
string='Next Payment Date',
compute='_compute_next_payment_date',
search='_search_next_payment_date',
)
_sql_constraints = [(
'unique_name', "", "Another entry with the same name already exists.",
)]
def _auto_init(self):
super()._auto_init()
if not index_exists(self.env.cr, 'account_move_checked_idx'):
self.env.cr.execute("""
CREATE INDEX account_move_checked_idx
ON account_move(journal_id)
WHERE checked = false
""")
if not index_exists(self.env.cr, 'account_move_payment_idx'):
self.env.cr.execute("""
CREATE INDEX account_move_payment_idx
ON account_move(journal_id, state, payment_state, move_type, date)
""")
if not index_exists(self.env.cr, 'account_move_unique_name'):
self.env.cr.execute("""
CREATE UNIQUE INDEX account_move_unique_name
ON account_move(name, journal_id)
WHERE (state = 'posted' AND name != '/')
""")
if not column_exists(self.env.cr, "account_move", "preferred_payment_method_line_id"):
create_column(self.env.cr, "account_move", "preferred_payment_method_line_id", "int4")
def init(self):
super().init()
create_index(self.env.cr,
indexname='account_move_journal_id_company_id_idx',
tablename='account_move',
expressions=['journal_id', 'company_id', 'date'])
create_index(
self.env.cr,
indexname='account_move_made_gaps',
tablename='account_move',
expressions=['journal_id', 'company_id', 'date'],
where="made_sequence_gap = TRUE",
) # used in <account.journal>._query_has_sequence_holes
# -------------------------------------------------------------------------
# COMPUTE METHODS
# -------------------------------------------------------------------------
@api.depends('move_type')
def _compute_invoice_default_sale_person(self):
# We want to modify the sale person only when we don't have one and if the move type corresponds to this condition
# If the move doesn't correspond, we remove the sale person
for move in self:
if move.is_sale_document(include_receipts=True):
move.invoice_user_id = move.invoice_user_id or self.env.user
else:
move.invoice_user_id = False
@api.depends('sending_data')
def _compute_is_being_sent(self):
for move in self:
move.is_being_sent = bool(move.sending_data)
@api.depends('is_move_sent')
def compute_move_sent_values(self):
for move in self:
move.move_sent_values = 'sent' if move.is_move_sent else 'not_sent'
def _compute_payment_reference(self):
for move in self.filtered(lambda m: (
m.state == 'posted'
and m.move_type == 'out_invoice'
and not m.payment_reference
)):
move.payment_reference = move._get_invoice_computed_reference()
self._inverse_payment_reference()
@api.depends('invoice_date', 'company_id')
def _compute_date(self):
for move in self:
if not move.invoice_date:
if not move.date:
move.date = fields.Date.context_today(self)
continue
accounting_date = move.invoice_date
if not move.is_sale_document(include_receipts=True):
accounting_date = move._get_accounting_date(move.invoice_date, move._affect_tax_report())
if accounting_date and accounting_date != move.date:
move.date = accounting_date
# _affect_tax_report may trigger premature recompute of line_ids.date
self.env.add_to_compute(move.line_ids._fields['date'], move.line_ids)
# might be protected because `_get_accounting_date` requires the `name`
self.env.add_to_compute(self._fields['name'], move)
@api.depends('auto_post')
def _compute_auto_post_until(self):
for record in self:
if record.auto_post in ('no', 'at_date'):
record.auto_post_until = False
@api.depends('date', 'auto_post')
def _compute_hide_post_button(self):
for record in self:
record.hide_post_button = record.state != 'draft' \
or record.auto_post != 'no' and record.date > fields.Date.context_today(record)
@api.depends('journal_id')
def _compute_company_id(self):
for move in self:
if move.journal_id.company_id not in move.company_id.parent_ids:
move.company_id = (move.journal_id.company_id or self.env.company)._accessible_branches()[:1]
@api.depends('move_type', 'origin_payment_id', 'statement_line_id')
def _compute_journal_id(self):
for move in self.filtered(lambda r: r.journal_id.type not in r._get_valid_journal_types()):
move.journal_id = move._search_default_journal()
def _get_valid_journal_types(self):
if self.is_sale_document(include_receipts=True):
return ['sale']
elif self.is_purchase_document(include_receipts=True):
return ['purchase']
elif self.origin_payment_id or self.statement_line_id or self.env.context.get('is_payment') or self.env.context.get('is_statement_line'):
return ['bank', 'cash', 'credit']
return ['general']
def _search_default_journal(self):
if self.statement_line_ids.statement_id.journal_id:
return self.statement_line_ids.statement_id.journal_id[:1]
journal_types = self._get_valid_journal_types()
company = self.company_id or self.env.company
domain = [
*self.env['account.journal']._check_company_domain(company),
('type', 'in', journal_types),
]
journal = None
# the currency is not a hard dependence, it triggers via manual add_to_compute
# avoid computing the currency before all it's dependences are set (like the journal...)
if self.env.cache.contains(self, self._fields['currency_id']):
currency_id = self.currency_id.id or self._context.get('default_currency_id')
if currency_id and currency_id != company.currency_id.id:
currency_domain = domain + [('currency_id', '=', currency_id)]
journal = self.env['account.journal'].search(currency_domain, limit=1)
if not journal:
journal = self.env['account.journal'].search(domain, limit=1)
if not journal:
error_msg = self.env['account.journal']._build_no_journal_error_msg(company.display_name, journal_types)
raise UserError(error_msg)
return journal
@api.depends('move_type')
def _compute_is_storno(self):
for move in self:
move.is_storno = move.is_storno or (move.move_type in ('out_refund', 'in_refund') and move.company_id.account_storno)
@api.depends('company_id', 'invoice_filter_type_domain')
def _compute_suitable_journal_ids(self):
for m in self:
journal_type = m.invoice_filter_type_domain or 'general'
company = m.company_id or self.env.company
m.suitable_journal_ids = self.env['account.journal'].search([
*self.env['account.journal']._check_company_domain(company),
('type', '=', journal_type),
])
@api.depends('posted_before', 'state', 'journal_id', 'date', 'move_type', 'origin_payment_id')
def _compute_name(self):
self = self.sorted(lambda m: (m.date, m.ref or '', m._origin.id))
for move in self:
if move.state == 'cancel':
continue
move_has_name = move.name and move.name != '/'
if move_has_name or move.state != 'posted':
if not move.posted_before and not move._sequence_matches_date():
if move._get_last_sequence():
# The name does not match the date and the move is not the first in the period:
# Reset to draft
move.name = False
continue
else:
if move_has_name and move.posted_before or not move_has_name and move._get_last_sequence():
# The move either
# - has a name and was posted before, or
# - doesn't have a name, but is not the first in the period
# so we don't recompute the name
continue
if move.date and (not move_has_name or not move._sequence_matches_date()):
move._set_next_sequence()
self.filtered(lambda m: not m.name and not move.quick_edit_mode).name = '/'
self._inverse_name()
@api.depends('journal_id', 'date')
def _compute_highest_name(self):
for record in self:
record.highest_name = record._get_last_sequence()
@api.depends('journal_id', 'sequence_number', 'sequence_prefix', 'state')
def _compute_made_sequence_gap(self):
unposted = self.filtered(lambda move: move.sequence_number != 0 and move.state != 'posted')
unposted.made_sequence_gap = True
for (journal, prefix), moves in (self - unposted).grouped(lambda m: (m.journal_id, m.sequence_prefix)).items():
previous_numbers = set(self.env['account.move'].sudo().search([
('journal_id', '=', journal.id),
('sequence_prefix', '=', prefix),
('sequence_number', '>=', min(moves.mapped('sequence_number')) - 1),
('sequence_number', '<=', max(moves.mapped('sequence_number')) - 1),
]).mapped('sequence_number'))
for move in moves:
move.made_sequence_gap = move.sequence_number > 1 and (move.sequence_number - 1) not in previous_numbers
@api.depends('move_type')
def _compute_type_name(self):
type_name_mapping = dict(
self._fields['move_type']._description_selection(self.env),
out_invoice=_('Invoice'),
out_refund=_('Credit Note'),
)
for record in self:
record.type_name = type_name_mapping[record.move_type]
@api.depends('inalterable_hash')
def _compute_secured(self):
for move in self:
move.secured = bool(move.inalterable_hash)
def _search_secured(self, operator, value):
if operator not in ['=', '!='] or value not in [True, False]:
raise UserError(_('Operation not supported'))
want_secured = (operator == '=') == value
return [('inalterable_hash', '!=' if want_secured else '=', False)]
@api.depends('line_ids.account_id.account_type')
def _compute_always_tax_exigible(self):
for record in self.with_context(prefetch_fields=False):
# We need to check is_invoice as well because always_tax_exigible is used to
# set the tags as well, during the encoding. So, if no receivable/payable
# line has been created yet, the invoice would be detected as always exigible,
# and set the tags on some lines ; which would be wrong.
record.always_tax_exigible = not record.is_invoice(True) \
and not record._collect_tax_cash_basis_values()
@api.depends('partner_id')
def _compute_commercial_partner_id(self):
for move in self:
move.commercial_partner_id = move.partner_id.commercial_partner_id
@api.depends('partner_id')
def _compute_partner_shipping_id(self):
for move in self:
if move.is_invoice(include_receipts=True):
addr = move.partner_id.address_get(['delivery'])
move.partner_shipping_id = addr and addr.get('delivery')
else:
move.partner_shipping_id = False
@api.depends('partner_id', 'partner_shipping_id', 'company_id')
def _compute_fiscal_position_id(self):
for move in self:
delivery_partner = self.env['res.partner'].browse(
move.partner_shipping_id.id
or move.partner_id.address_get(['delivery'])['delivery']
)
move.fiscal_position_id = self.env['account.fiscal.position'].with_company(move.company_id)._get_fiscal_position(
move.partner_id, delivery=delivery_partner)
@api.depends('bank_partner_id')
def _compute_partner_bank_id(self):
for move in self:
# This will get the bank account from the partner in an order with the trusted first
bank_ids = move.bank_partner_id.bank_ids.filtered(
lambda bank: not bank.company_id or bank.company_id == move.company_id
).sorted(lambda bank: not bank.allow_out_payment)
move.partner_bank_id = bank_ids[:1]
@api.depends('partner_id')
def _compute_invoice_payment_term_id(self):
for move in self:
if move.is_sale_document(include_receipts=True) and move.partner_id.property_payment_term_id:
move.invoice_payment_term_id = move.partner_id.property_payment_term_id
elif move.is_purchase_document(include_receipts=True) and move.partner_id.property_supplier_payment_term_id:
move.invoice_payment_term_id = move.partner_id.property_supplier_payment_term_id
else:
move.invoice_payment_term_id = False
@api.depends('needed_terms')
def _compute_invoice_date_due(self):
today = fields.Date.context_today(self)
for move in self:
move.invoice_date_due = move.needed_terms and max(
(k['date_maturity'] for k in move.needed_terms.keys() if k),
default=False,
) or move.invoice_date_due or today
def _compute_delivery_date(self):
pass
@api.depends('delivery_date')
def _compute_show_delivery_date(self):
for move in self:
move.show_delivery_date = move.delivery_date and move.is_sale_document()
@api.depends('journal_id', 'statement_line_id')
def _compute_currency_id(self):
for invoice in self:
currency = (
invoice.statement_line_id.foreign_currency_id
or invoice.journal_id.currency_id
or invoice.currency_id
or invoice.journal_id.company_id.currency_id
)
invoice.currency_id = currency
@api.depends('currency_id', 'company_currency_id', 'company_id', 'invoice_date')
def _compute_invoice_currency_rate(self):
for move in self:
if move.is_invoice(include_receipts=True):
if move.currency_id:
move.invoice_currency_rate = self.env['res.currency']._get_conversion_rate(
from_currency=move.company_currency_id,
to_currency=move.currency_id,
company=move.company_id,
date=move.invoice_date or fields.Date.context_today(move),
)
else:
move.invoice_currency_rate = 1
@api.depends('move_type')
def _compute_direction_sign(self):
for invoice in self:
if invoice.move_type == 'entry' or invoice.is_outbound():
invoice.direction_sign = 1
else:
invoice.direction_sign = -1
@api.depends(
'line_ids.matched_debit_ids.debit_move_id.move_id.origin_payment_id.is_matched',
'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual',
'line_ids.matched_debit_ids.debit_move_id.move_id.line_ids.amount_residual_currency',
'line_ids.matched_credit_ids.credit_move_id.move_id.origin_payment_id.is_matched',
'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual',
'line_ids.matched_credit_ids.credit_move_id.move_id.line_ids.amount_residual_currency',
'line_ids.balance',
'line_ids.currency_id',
'line_ids.amount_currency',
'line_ids.amount_residual',
'line_ids.amount_residual_currency',
'line_ids.payment_id.state',
'line_ids.full_reconcile_id',
'state')
def _compute_amount(self):
for move in self:
total_untaxed, total_untaxed_currency = 0.0, 0.0
total_tax, total_tax_currency = 0.0, 0.0
total_residual, total_residual_currency = 0.0, 0.0
total, total_currency = 0.0, 0.0
for line in move.line_ids:
if move.is_invoice(True):
# === Invoices ===
if line.display_type == 'tax' or (line.display_type == 'rounding' and line.tax_repartition_line_id):
# Tax amount.
total_tax += line.balance
total_tax_currency += line.amount_currency
total += line.balance
total_currency += line.amount_currency
elif line.display_type in ('product', 'rounding'):
# Untaxed amount.
total_untaxed += line.balance
total_untaxed_currency += line.amount_currency
total += line.balance
total_currency += line.amount_currency
elif line.display_type == 'payment_term':
# Residual amount.
total_residual += line.amount_residual
total_residual_currency += line.amount_residual_currency
else:
# === Miscellaneous journal entry ===
if line.debit:
total += line.balance
total_currency += line.amount_currency
sign = move.direction_sign
move.amount_untaxed = sign * total_untaxed_currency
move.amount_tax = sign * total_tax_currency
move.amount_total = sign * total_currency
move.amount_residual = -sign * total_residual_currency
move.amount_untaxed_signed = -total_untaxed
move.amount_untaxed_in_currency_signed = -total_untaxed_currency
move.amount_tax_signed = -total_tax
move.amount_total_signed = abs(total) if move.move_type == 'entry' else -total
move.amount_residual_signed = total_residual
move.amount_total_in_currency_signed = abs(move.amount_total) if move.move_type == 'entry' else -(sign * move.amount_total)
@api.depends('amount_residual', 'move_type', 'state', 'company_id', 'matched_payment_ids.state')
def _compute_payment_state(self):
stored_ids = tuple(self.ids)
if stored_ids:
self.env['account.partial.reconcile'].flush_model()
self.env['account.payment'].flush_model(['is_matched'])
queries = []
for source_field, counterpart_field in (
('debit_move_id', 'credit_move_id'),
('credit_move_id', 'debit_move_id'),
):
queries.append(SQL('''
SELECT
source_line.id AS source_line_id,
source_line.move_id AS source_move_id,
account.account_type AS source_line_account_type,
ARRAY_AGG(counterpart_move.move_type) AS counterpart_move_types,
COALESCE(BOOL_AND(COALESCE(pay.is_matched, FALSE))
FILTER (WHERE counterpart_move.origin_payment_id IS NOT NULL), TRUE) AS all_payments_matched,
BOOL_OR(COALESCE(BOOL(pay.id), FALSE)) as has_payment,
BOOL_OR(COALESCE(BOOL(counterpart_move.statement_line_id), FALSE)) as has_st_line
FROM account_partial_reconcile part
JOIN account_move_line source_line ON source_line.id = part.%s
JOIN account_account account ON account.id = source_line.account_id
JOIN account_move_line counterpart_line ON counterpart_line.id = part.%s
JOIN account_move counterpart_move ON counterpart_move.id = counterpart_line.move_id
LEFT JOIN account_payment pay ON pay.id = counterpart_move.origin_payment_id
WHERE source_line.move_id IN %s AND counterpart_line.move_id != source_line.move_id
GROUP BY source_line.id, source_line.move_id, account.account_type
''', SQL.identifier(source_field), SQL.identifier(counterpart_field), stored_ids))
payment_data = defaultdict(list)
for row in self.env.execute_query_dict(SQL(" UNION ALL ").join(queries)):
payment_data[row['source_move_id']].append(row)
else:
payment_data = {}
for invoice in self:
if invoice.payment_state == 'invoicing_legacy':
# invoicing_legacy state is set via SQL when setting setting field
# invoicing_switch_threshold (defined in account_accountant).
# The only way of going out of this state is through this setting,
# so we don't recompute it here.
continue
currencies = invoice._get_lines_onchange_currency().currency_id
currency = currencies if len(currencies) == 1 else invoice.company_id.currency_id
reconciliation_vals = payment_data.get(invoice.id, [])
payment_state_matters = invoice.is_invoice(True)
# Restrict on 'receivable'/'payable' lines for invoices/expense entries.
if payment_state_matters:
reconciliation_vals = [x for x in reconciliation_vals if x['source_line_account_type'] in ('asset_receivable', 'liability_payable')]
new_pmt_state = 'not_paid' if invoice.payment_state != 'blocked' else 'blocked'
if invoice.state == 'posted':
# Posted invoice/expense entry.
if payment_state_matters:
if currency.is_zero(invoice.amount_residual):
if any(x['has_payment'] or x['has_st_line'] for x in reconciliation_vals):
# Check if the invoice/expense entry is fully paid or 'in_payment'.
if all(x['all_payments_matched'] for x in reconciliation_vals):
new_pmt_state = 'paid'
else:
new_pmt_state = invoice._get_invoice_in_payment_state()
else:
new_pmt_state = 'paid'
reverse_move_types = set()
for x in reconciliation_vals:
for move_type in x['counterpart_move_types']:
reverse_move_types.add(move_type)
in_reverse = (invoice.move_type in ('in_invoice', 'in_receipt')
and (reverse_move_types == {'in_refund'} or reverse_move_types == {'in_refund', 'entry'}))
out_reverse = (invoice.move_type in ('out_invoice', 'out_receipt')
and (reverse_move_types == {'out_refund'} or reverse_move_types == {'out_refund', 'entry'}))
misc_reverse = (invoice.move_type in ('entry', 'out_refund', 'in_refund')
and reverse_move_types == {'entry'})
if in_reverse or out_reverse or misc_reverse:
new_pmt_state = 'reversed'
elif invoice.matched_payment_ids.filtered(lambda p: not p.move_id and p.state == 'in_process'):
new_pmt_state = invoice._get_invoice_in_payment_state()
elif reconciliation_vals:
new_pmt_state = 'partial'
elif invoice.matched_payment_ids.filtered(lambda p: not p.move_id and p.state == 'paid'):
new_pmt_state = invoice._get_invoice_in_payment_state()
invoice.payment_state = new_pmt_state
@api.depends('payment_state', 'state')
def _compute_status_in_payment(self):
for move in self:
move.status_in_payment = move.state if move.state in ('draft', 'cancel') else move.payment_state
@api.depends('matched_payment_ids')
def _compute_payment_count(self):
for invoice in self:
invoice.payment_count = len(invoice.matched_payment_ids)
@api.depends('invoice_payment_term_id', 'invoice_date', 'currency_id', 'amount_total_in_currency_signed', 'invoice_date_due')
def _compute_needed_terms(self):
AccountTax = self.env['account.tax']
for invoice in self.with_context(bin_size=False):
is_draft = invoice.id != invoice._origin.id
invoice.needed_terms = {}
invoice.needed_terms_dirty = True
sign = 1 if invoice.is_inbound(include_receipts=True) else -1
if invoice.is_invoice(True) and invoice.invoice_line_ids:
if invoice.invoice_payment_term_id:
if is_draft:
tax_amount_currency = 0.0
tax_amount = tax_amount_currency
untaxed_amount_currency = 0.0
untaxed_amount = untaxed_amount_currency
sign = invoice.direction_sign
base_lines, _tax_lines = invoice._get_rounded_base_and_tax_lines(round_from_tax_lines=False)
AccountTax._add_accounting_data_in_base_lines_tax_details(base_lines, invoice.company_id, include_caba_tags=invoice.always_tax_exigible)
tax_results = AccountTax._prepare_tax_lines(base_lines, invoice.company_id)
for base_line, to_update in tax_results['base_lines_to_update']:
untaxed_amount_currency += sign * to_update['amount_currency']
untaxed_amount += sign * to_update['balance']
for tax_line_vals in tax_results['tax_lines_to_add']:
tax_amount_currency += sign * tax_line_vals['amount_currency']
tax_amount += sign * tax_line_vals['balance']
else:
tax_amount_currency = invoice.amount_tax * sign
tax_amount = invoice.amount_tax_signed
untaxed_amount_currency = invoice.amount_untaxed * sign
untaxed_amount = invoice.amount_untaxed_signed
invoice_payment_terms = invoice.invoice_payment_term_id._compute_terms(
date_ref=invoice.invoice_date or invoice.date or fields.Date.context_today(invoice),
currency=invoice.currency_id,
tax_amount_currency=tax_amount_currency,
tax_amount=tax_amount,
untaxed_amount_currency=untaxed_amount_currency,
untaxed_amount=untaxed_amount,
company=invoice.company_id,
cash_rounding=invoice.invoice_cash_rounding_id,
sign=sign
)
for term_line in invoice_payment_terms['line_ids']:
key = frozendict({
'move_id': invoice.id,
'date_maturity': fields.Date.to_date(term_line.get('date')),
'discount_date': invoice_payment_terms.get('discount_date'),
})
values = {
'balance': term_line['company_amount'],
'amount_currency': term_line['foreign_amount'],
'discount_date': invoice_payment_terms.get('discount_date'),
'discount_balance': invoice_payment_terms.get('discount_balance') or 0.0,
'discount_amount_currency': invoice_payment_terms.get('discount_amount_currency') or 0.0,
}
if key not in invoice.needed_terms:
invoice.needed_terms[key] = values
else:
invoice.needed_terms[key]['balance'] += values['balance']
invoice.needed_terms[key]['amount_currency'] += values['amount_currency']
else:
invoice.needed_terms[frozendict({
'move_id': invoice.id,
'date_maturity': fields.Date.to_date(invoice.invoice_date_due),
'discount_date': False,
'discount_balance': 0.0,
'discount_amount_currency': 0.0
})] = {
'balance': invoice.amount_total_signed,
'amount_currency': invoice.amount_total_in_currency_signed,
}
def _compute_payments_widget_to_reconcile_info(self):
for move in self:
move.invoice_outstanding_credits_debits_widget = False
move.invoice_has_outstanding = False
if move.state != 'posted' \
or move.payment_state not in ('not_paid', 'partial') \
or not move.is_invoice(include_receipts=True):
continue
pay_term_lines = move.line_ids\
.filtered(lambda line: line.account_id.account_type in ('asset_receivable', 'liability_payable'))
domain = [
('account_id', 'in', pay_term_lines.account_id.ids),
('parent_state', '=', 'posted'),
('partner_id', '=', move.commercial_partner_id.id),
('reconciled', '=', False),
'|', ('amount_residual', '!=', 0.0), ('amount_residual_currency', '!=', 0.0),
]
payments_widget_vals = {'outstanding': True, 'content': [], 'move_id': move.id}
if move.is_inbound():
domain.append(('balance', '<', 0.0))
payments_widget_vals['title'] = _('Outstanding credits')
else:
domain.append(('balance', '>', 0.0))
payments_widget_vals['title'] = _('Outstanding debits')
for line in self.env['account.move.line'].search(domain):
if line.currency_id == move.currency_id:
# Same foreign currency.
amount = abs(line.amount_residual_currency)
else:
# Different foreign currencies.
amount = line.company_currency_id._convert(
abs(line.amount_residual),
move.currency_id,
move.company_id,
line.date,
)
if move.currency_id.is_zero(amount):
continue
payments_widget_vals['content'].append({
'journal_name': line.ref or line.move_id.name,
'amount': amount,
'currency_id': move.currency_id.id,
'id': line.id,
'move_id': line.move_id.id,
'date': fields.Date.to_string(line.date),
'account_payment_id': line.payment_id.id,
})
if not payments_widget_vals['content']:
continue
move.invoice_outstanding_credits_debits_widget = payments_widget_vals
move.invoice_has_outstanding = True
@api.depends('partner_id', 'company_id')
def _compute_preferred_payment_method_line_id(self):
for move in self:
partner = move.partner_id.with_company(move.company_id)
if move.is_sale_document():
move.preferred_payment_method_line_id = partner.property_inbound_payment_method_line_id
else:
move.preferred_payment_method_line_id = partner.property_outbound_payment_method_line_id
@api.depends('move_type', 'line_ids.amount_residual')
def _compute_payments_widget_reconciled_info(self):
for move in self:
payments_widget_vals = {'title': _('Less Payment'), 'outstanding': False, 'content': []}
if move.state == 'posted' and move.is_invoice(include_receipts=True):
reconciled_vals = []
reconciled_partials = move.sudo()._get_all_reconciled_invoice_partials()
for reconciled_partial in reconciled_partials:
counterpart_line = reconciled_partial['aml']
if counterpart_line.move_id.ref:
reconciliation_ref = '%s (%s)' % (counterpart_line.move_id.name, counterpart_line.move_id.ref)
else:
reconciliation_ref = counterpart_line.move_id.name
if counterpart_line.amount_currency and counterpart_line.currency_id != counterpart_line.company_id.currency_id:
foreign_currency = counterpart_line.currency_id
else:
foreign_currency = False
reconciled_vals.append({
'name': counterpart_line.name,
'journal_name': counterpart_line.journal_id.name,
'company_name': counterpart_line.journal_id.company_id.name if counterpart_line.journal_id.company_id != move.company_id else False,
'amount': reconciled_partial['amount'],
'currency_id': move.company_id.currency_id.id if reconciled_partial['is_exchange'] else reconciled_partial['currency'].id,
'date': counterpart_line.date,
'partial_id': reconciled_partial['partial_id'],
'account_payment_id': counterpart_line.payment_id.id,
'payment_method_name': counterpart_line.payment_id.payment_method_line_id.name,
'move_id': counterpart_line.move_id.id,
'ref': reconciliation_ref,
# these are necessary for the views to change depending on the values
'is_exchange': reconciled_partial['is_exchange'],
'amount_company_currency': formatLang(self.env, abs(counterpart_line.balance), currency_obj=counterpart_line.company_id.currency_id),
'amount_foreign_currency': foreign_currency and formatLang(self.env, abs(counterpart_line.amount_currency), currency_obj=foreign_currency)
})
payments_widget_vals['content'] = reconciled_vals
if payments_widget_vals['content']:
move.invoice_payments_widget = payments_widget_vals
else:
move.invoice_payments_widget = False
def _prepare_product_base_line_for_taxes_computation(self, product_line):
""" Convert an account.move.line having display_type='product' into a base line for the taxes computation.
:param product_line: An account.move.line.
:return: A base line returned by '_prepare_base_line_for_taxes_computation'.
"""
self.ensure_one()
is_invoice = self.is_invoice(include_receipts=True)
sign = self.direction_sign if is_invoice else 1
if is_invoice:
rate = self.invoice_currency_rate
else:
rate = (abs(product_line.amount_currency) / abs(product_line.balance)) if product_line.balance else 0.0
return self.env['account.tax']._prepare_base_line_for_taxes_computation(
product_line,
price_unit=product_line.price_unit if is_invoice else product_line.amount_currency,
quantity=product_line.quantity if is_invoice else 1.0,
discount=product_line.discount if is_invoice else 0.0,
rate=rate,
sign=sign,
special_mode=False if is_invoice else 'total_excluded',
)
def _prepare_epd_base_line_for_taxes_computation(self, epd_line):
""" Convert an account.move.line having display_type='epd' into a base line for the taxes computation.
:param epd_line: An account.move.line.
:return: A base line returned by '_prepare_base_line_for_taxes_computation'.
"""
self.ensure_one()
sign = self.direction_sign
rate = self.invoice_currency_rate
return self.env['account.tax']._prepare_base_line_for_taxes_computation(
epd_line,
price_unit=sign * epd_line.amount_currency,
quantity=1.0,
sign=sign,
special_mode='total_excluded',
special_type='early_payment',
is_refund=self.move_type in ('out_refund', 'in_refund'),
rate=rate,
)
def _prepare_epd_base_lines_for_taxes_computation_from_base_lines(self, base_lines):
""" Anticipate the epd lines to be generated from the base lines passed as parameter.
When the record is in draft (not saved), the accounting items are not there so we can't
call '_prepare_epd_base_line_for_taxes_computation'.
:param base_lines: The base lines generated by '_prepare_product_base_line_for_taxes_computation'.
:return: A list of base lines representing the epd lines.
"""
self.ensure_one()
aggregated_results = self._sync_dynamic_line_needed_values(base_lines.mapped('epd_needed'))
sign = self.direction_sign
rate = self.invoice_currency_rate
epd_lines = []
for grouping_key, values in aggregated_results.items():
all_values = {**grouping_key, **values}
epd_lines.append(self.env['account.tax']._prepare_base_line_for_taxes_computation(
all_values,
id=grouping_key,
tax_ids=self.env['account.tax'].browse(all_values['tax_ids'][0][2]),
price_unit=sign * values['amount_currency'],
quantity=1.0,
currency_id=self.currency_id,
sign=1,
special_mode='total_excluded',
special_type='early_payment',
partner_id=self.commercial_partner_id,
account_id=self.env['account.account'].browse(all_values['account_id']),
is_refund=self.move_type in ('out_refund', 'in_refund'),
rate=rate,
))
return epd_lines
def _prepare_cash_rounding_base_line_for_taxes_computation(self, cash_rounding_line):
""" Convert an account.move.line having display_type='rounding' into a base line for the taxes computation.
:param cash_rounding_line: An account.move.line.
:return: A base line returned by '_prepare_base_line_for_taxes_computation'.
"""
self.ensure_one()
sign = self.direction_sign
rate = self.invoice_currency_rate
return self.env['account.tax']._prepare_base_line_for_taxes_computation(
cash_rounding_line,
price_unit=sign * cash_rounding_line.amount_currency,
quantity=1.0,
sign=sign,
special_mode='total_excluded',
special_type='cash_rounding',
is_refund=self.move_type in ('out_refund', 'in_refund'),
rate=rate,
)
def _prepare_tax_line_for_taxes_computation(self, tax_line):
""" Convert an account.move.line having display_type='tax' into a tax line for the taxes computation.
:param tax_line: An account.move.line.
:return: A tax line returned by '_prepare_tax_line_for_taxes_computation'.
"""
self.ensure_one()
return self.env['account.tax']._prepare_tax_line_for_taxes_computation(
tax_line,
sign=self.direction_sign,
)
def _get_rounded_base_and_tax_lines(self, round_from_tax_lines=True):
""" Small helper to extract the base and tax lines for the taxes computation from the current move.
The move could be stored or not and could have some features generating extra journal items acting as
base lines for the taxes computation (e.g. epd, rounding lines).
:param round_from_tax_lines: Indicate if the manual tax amounts of tax journal items should be kept or not.
It only works when the move is stored.
:return: A tuple <base_lines, tax_lines> for the taxes computation.
"""
self.ensure_one()
AccountTax = self.env['account.tax']
is_invoice = self.is_invoice(include_receipts=True)
if self.id or not is_invoice:
base_amls = self.line_ids.filtered(lambda line: line.display_type == 'product')
else:
base_amls = self.invoice_line_ids.filtered(lambda line: line.display_type == 'product')
base_lines = [self._prepare_product_base_line_for_taxes_computation(line) for line in base_amls]
tax_lines = []
if self.id:
# The move is stored so we can add the early payment discount lines directly to reduce the
# tax amount without touching the untaxed amount.
epd_amls = self.line_ids.filtered(lambda line: line.display_type == 'epd')
base_lines += [self._prepare_epd_base_line_for_taxes_computation(line) for line in epd_amls]
cash_rounding_amls = self.line_ids \
.filtered(lambda line: line.display_type == 'rounding' and not line.tax_repartition_line_id)
base_lines += [self._prepare_cash_rounding_base_line_for_taxes_computation(line) for line in cash_rounding_amls]
AccountTax._add_tax_details_in_base_lines(base_lines, self.company_id)
tax_amls = self.line_ids.filtered('tax_repartition_line_id')
tax_lines = [self._prepare_tax_line_for_taxes_computation(tax_line) for tax_line in tax_amls]
AccountTax._round_base_lines_tax_details(base_lines, self.company_id, tax_lines=tax_lines if round_from_tax_lines else [])
else:
# The move is not stored yet so the only thing we have is the invoice lines.
base_lines += self._prepare_epd_base_lines_for_taxes_computation_from_base_lines(base_amls)
AccountTax._add_tax_details_in_base_lines(base_lines, self.company_id)
AccountTax._round_base_lines_tax_details(base_lines, self.company_id)
return base_lines, tax_lines
@api.depends_context('lang')
@api.depends(
'invoice_line_ids.currency_rate',
'invoice_line_ids.tax_base_amount',
'invoice_line_ids.tax_line_id',
'invoice_line_ids.price_total',
'invoice_line_ids.price_subtotal',
'invoice_payment_term_id',
'partner_id',
'currency_id',
)
def _compute_tax_totals(self):
""" Computed field used for custom widget's rendering.
Only set on invoices.
"""
for move in self:
if move.is_invoice(include_receipts=True):
base_lines, _tax_lines = move._get_rounded_base_and_tax_lines()
move.tax_totals = self.env['account.tax']._get_tax_totals_summary(
base_lines=base_lines,
currency=move.currency_id,
company=move.company_id,
cash_rounding=move.invoice_cash_rounding_id,
)
move.tax_totals['display_in_company_currency'] = (
move.company_id.display_invoice_tax_company_currency
and move.company_currency_id != move.currency_id
and move.tax_totals['has_tax_groups']
and move.is_sale_document(include_receipts=True)
)
else:
# Non-invoice moves don't support that field (because of multicurrency: all lines of the invoice share the same currency)
move.tax_totals = None
@api.depends('show_payment_term_details')
def _compute_payment_term_details(self):
'''
Returns an [] containing the payment term's information to be displayed on the invoice's PDF.
'''
for invoice in self:
invoice.payment_term_details = False
if invoice.show_payment_term_details:
sign = 1 if invoice.is_inbound(include_receipts=True) else -1
payment_term_details = []
for line in invoice.line_ids.filtered(lambda l: l.display_type == 'payment_term').sorted('date_maturity'):
payment_term_details.append({
'date': format_date(self.env, line.date_maturity),
'amount': sign * line.amount_currency,
})
invoice.payment_term_details = payment_term_details
@api.depends('move_type', 'payment_state', 'invoice_payment_term_id')
def _compute_show_payment_term_details(self):
'''
Determines :
- whether or not an additional table should be added at the end of the invoice to display the various
- whether or not there is an early pay discount in this invoice that should be displayed
'''
for invoice in self:
if invoice.move_type in ('out_invoice', 'out_receipt', 'in_invoice', 'in_receipt') and invoice.payment_state in ('not_paid', 'partial'):
payment_term_lines = invoice.line_ids.filtered(lambda l: l.display_type == 'payment_term')
invoice.show_discount_details = invoice.invoice_payment_term_id.early_discount
invoice.show_payment_term_details = len(payment_term_lines) > 1 or invoice.show_discount_details
else:
invoice.show_discount_details = False
invoice.show_payment_term_details = False
def _need_cancel_request(self):
""" Hook allowing a localization to prevent the user to reset draft an invoice that has been already sent
to the government and thus, must remain untouched except if its cancellation is approved.
:return: True if the cancel button is displayed instead of draft button, False otherwise.
"""
self.ensure_one()
return False
@api.depends('country_code')
def _compute_need_cancel_request(self):
for move in self:
move.need_cancel_request = move._need_cancel_request()
@api.depends('partner_id', 'invoice_source_email', 'partner_id.display_name')
def _compute_invoice_partner_display_info(self):
for move in self:
vendor_display_name = move.partner_id.display_name
if not vendor_display_name:
if move.invoice_source_email:
vendor_display_name = _('@From: %(email)s', email=move.invoice_source_email)
else:
vendor_display_name = _('#Created by: %s', move.sudo().create_uid.name or self.env.user.name)
move.invoice_partner_display_name = vendor_display_name
@api.depends('move_type')
def _compute_invoice_filter_type_domain(self):
for move in self:
if move.is_sale_document(include_receipts=True):
move.invoice_filter_type_domain = 'sale'
elif move.is_purchase_document(include_receipts=True):
move.invoice_filter_type_domain = 'purchase'
else:
move.invoice_filter_type_domain = False
@api.depends('commercial_partner_id')
def _compute_bank_partner_id(self):
for move in self:
if move.is_inbound():
move.bank_partner_id = move.company_id.partner_id
else:
move.bank_partner_id = move.commercial_partner_id
@api.depends('date', 'line_ids.debit', 'line_ids.credit', 'line_ids.tax_line_id', 'line_ids.tax_ids', 'line_ids.tax_tag_ids',
'invoice_line_ids.debit', 'invoice_line_ids.credit', 'invoice_line_ids.tax_line_id', 'invoice_line_ids.tax_ids', 'invoice_line_ids.tax_tag_ids')
def _compute_tax_lock_date_message(self):
for move in self:
accounting_date = move.date or fields.Date.context_today(move)
affects_tax_report = move._affect_tax_report()
move.tax_lock_date_message = move._get_lock_date_message(accounting_date, affects_tax_report)
@api.depends('currency_id')
def _compute_display_inactive_currency_warning(self):
for move in self.with_context(active_test=False):
move.display_inactive_currency_warning = move.state == 'draft' and move.currency_id and not move.currency_id.active
@api.depends('company_id.account_fiscal_country_id', 'fiscal_position_id', 'fiscal_position_id.country_id', 'fiscal_position_id.foreign_vat')
def _compute_tax_country_id(self):
foreign_vat_records = self.filtered(lambda r: r.fiscal_position_id.foreign_vat)
for fiscal_position_id, record_group in groupby(foreign_vat_records, key=lambda r: r.fiscal_position_id):
self.env['account.move'].concat(*record_group).tax_country_id = fiscal_position_id.country_id
for company_id, record_group in groupby((self-foreign_vat_records), key=lambda r: r.company_id):
self.env['account.move'].concat(*record_group).tax_country_id = company_id.account_fiscal_country_id
@api.depends('tax_country_id')
def _compute_tax_country_code(self):
for record in self:
record.tax_country_code = record.tax_country_id.code
@api.depends('line_ids')
def _compute_has_reconciled_entries(self):
for move in self:
move.has_reconciled_entries = len(move.line_ids._reconciled_lines()) > 1
@api.depends('restrict_mode_hash_table', 'state', 'inalterable_hash')
def _compute_show_reset_to_draft_button(self):
for move in self:
move.show_reset_to_draft_button = (
not self._is_move_restricted(move) \
and not move.inalterable_hash
and (move.state == 'cancel' or (move.state == 'posted' and not move.need_cancel_request))
)
# EXTENDS portal portal.mixin
def _compute_access_url(self):
super()._compute_access_url()
for move in self.filtered(lambda move: move.is_invoice()):
move.access_url = '/my/invoices/%s' % (move.id)
@api.depends('move_type', 'partner_id', 'company_id')
def _compute_narration(self):
use_invoice_terms = self.env['ir.config_parameter'].sudo().get_param('account.use_invoice_terms')
for move in self:
if not move.is_sale_document(include_receipts=True):
continue
if not use_invoice_terms:
move.narration = False
else:
lang = move.partner_id.lang or self.env.user.lang
if not move.company_id.terms_type == 'html':
narration = move.company_id.with_context(lang=lang).invoice_terms if not is_html_empty(move.company_id.invoice_terms) else ''
else:
baseurl = self.env.company.get_base_url() + '/terms'
context = {'lang': lang}
narration = _('Terms & Conditions: %s', baseurl)
del context
move.narration = narration or False
def _get_partner_credit_warning_exclude_amount(self):
# to extend in module 'sale'; see there for details
self.ensure_one()
return 0
@api.depends('company_id', 'partner_id', 'tax_totals', 'currency_id')
def _compute_partner_credit_warning(self):
for move in self:
move.with_company(move.company_id)
move.partner_credit_warning = ''
show_warning = move.state == 'draft' and \
move.move_type == 'out_invoice' and \
move.company_id.account_use_credit_limit
if show_warning:
total_field = 'total_amount_currency' if move.currency_id == move.company_currency_id else 'total_amount'
current_amount = move.tax_totals[total_field]
move.partner_credit_warning = self._build_credit_warning_message(
move,
current_amount=current_amount,
exclude_amount=move._get_partner_credit_warning_exclude_amount(),
)
@api.depends('partner_id')
def _compute_partner_credit(self):
for move in self:
move.partner_credit = move.partner_id.commercial_partner_id.credit
def _build_credit_warning_message(self, record, current_amount=0.0, exclude_current=False, exclude_amount=0.0):
""" Build the warning message that will be displayed in a yellow banner on top of the current record
if the partner exceeds a credit limit (set on the company or the partner itself).
:param record: The record where the warning will appear (Invoice, Sales Order...).
:param current_amount (float): The partner's outstanding credit amount from the current document.
:param exclude_current (bool): DEPRECATED in favor of parameter `exclude_amount`:
Whether to exclude `current_amount` from the credit to invoice.
:param exclude_amount (float): The amount to subtract from the partner's `credit_to_invoice`.
Consider the warning on a draft invoice created from a sales order.
After confirming the invoice the (partial) amount (on the invoice)
stemming from sales orders will be substracted from the `credit_to_invoice`.
This will reduce the total credit of the partner.
This parameter is used to reflect this amount.
:return (str): The warning message to be showed.
"""
partner_id = record.partner_id.commercial_partner_id
credit_to_invoice = partner_id.credit_to_invoice - exclude_amount
total_credit = partner_id.credit + credit_to_invoice + current_amount
if not partner_id.credit_limit or total_credit <= partner_id.credit_limit:
return ''
msg = _(
'%(partner_name)s has reached its credit limit of: %(credit_limit)s',
partner_name=partner_id.name,
credit_limit=formatLang(self.env, partner_id.credit_limit, currency_obj=record.company_id.currency_id)
)
total_credit_formatted = formatLang(self.env, total_credit, currency_obj=record.company_id.currency_id)
if credit_to_invoice > 0 and current_amount > 0:
return msg + '\n' + _(
'Total amount due (including sales orders and this document): %(total_credit)s',
total_credit=total_credit_formatted
)
elif credit_to_invoice > 0:
return msg + '\n' + _(
'Total amount due (including sales orders): %(total_credit)s',
total_credit=total_credit_formatted
)
elif current_amount > 0:
return msg + '\n' + _(
'Total amount due (including this document): %(total_credit)s',
total_credit=total_credit_formatted
)
else:
return msg + '\n' + _(
'Total amount due: %(total_credit)s',
total_credit=total_credit_formatted
)
@api.depends('journal_id.type', 'company_id')
def _compute_quick_edit_mode(self):
for move in self:
quick_edit_mode = move.company_id.quick_edit_mode
if move.journal_id.type == 'sale':
move.quick_edit_mode = quick_edit_mode in ('out_invoices', 'out_and_in_invoices')
elif move.journal_id.type == 'purchase':
move.quick_edit_mode = quick_edit_mode in ('in_invoices', 'out_and_in_invoices')
else:
move.quick_edit_mode = False
@api.depends('quick_edit_total_amount', 'invoice_line_ids.price_total', 'tax_totals')
def _compute_quick_encoding_vals(self):
for move in self:
move.quick_encoding_vals = move._get_quick_edit_suggestions()
@api.depends('ref', 'move_type', 'partner_id', 'invoice_date', 'tax_totals')
def _compute_duplicated_ref_ids(self):
move_to_duplicate_move = self._fetch_duplicate_reference()
for move in self:
# Uses move._origin.id to handle records in edition/existing records and 0 for new records
move.duplicated_ref_ids = move_to_duplicate_move.get(move._origin, self.env['account.move'])
def _fetch_duplicate_reference(self, matching_states=('draft', 'posted')):
moves = self.filtered(lambda m: m.is_sale_document() or m.is_purchase_document() and m.ref)
if not moves:
return {}
used_fields = ("company_id", "partner_id", "commercial_partner_id", "ref", "move_type", "invoice_date", "state", "amount_total")
self.env["account.move"].flush_model(used_fields)
move_table_and_alias = SQL("account_move AS move")
if not moves[0].id: # check if record is under creation/edition in UI
# New record aren't searchable in the DB and record in edition aren't up to date yet
# Replace the table by safely injecting the values in the query
values = {
field_name: moves._fields[field_name].convert_to_write(moves[field_name], moves) or None
for field_name in used_fields
}
values["id"] = moves._origin.id or 0
# The amount total depends on the field line_ids and is calculated upon saving, we needed a way to get it even when the
# invoices has not been saved yet.
values['amount_total'] = self.tax_totals.get('total_amount_currency', 0)
casted_values = SQL(', ').join(
SQL("%s::%s", value, SQL.identifier(moves._fields[field_name].column_type[0]))
for field_name, value in values.items()
)
column_names = SQL(', ').join(SQL.identifier(field_name) for field_name in values)
move_table_and_alias = SQL("(VALUES (%s)) AS move(%s)", casted_values, column_names)
result = self.env.execute_query(SQL("""
SELECT
move.id AS move_id,
array_agg(duplicate_move.id) AS duplicate_ids
FROM %(move_table_and_alias)s
JOIN account_move AS duplicate_move ON
move.company_id = duplicate_move.company_id
AND move.id != duplicate_move.id
AND duplicate_move.state IN %(matching_states)s
AND move.move_type = duplicate_move.move_type
AND (
move.commercial_partner_id = duplicate_move.commercial_partner_id
OR (move.commercial_partner_id IS NULL AND duplicate_move.state = 'draft')
)
AND (
-- For out moves
move.move_type in ('out_invoice', 'out_refund')
AND (
move.amount_total = duplicate_move.amount_total
AND move.invoice_date = duplicate_move.invoice_date
)
OR
-- For in moves
move.move_type in ('in_invoice', 'in_refund')
AND (
move.ref = duplicate_move.ref
AND (move.invoice_date = duplicate_move.invoice_date OR move.state = 'draft')
)
)
WHERE move.id IN %(moves)s
GROUP BY move.id
""",
matching_states=tuple(matching_states),
moves=tuple(moves.ids or [0]),
move_table_and_alias=move_table_and_alias,
))
return {
self.env['account.move'].browse(move_id): self.env['account.move'].browse(duplicate_ids)
for move_id, duplicate_ids in result
}
@api.depends('company_id')
def _compute_display_qr_code(self):
for move in self:
move.display_qr_code = (
move.move_type in ('out_invoice', 'out_receipt', 'in_invoice', 'in_receipt')
and move.company_id.qr_code
)
@api.depends('amount_total', 'currency_id')
def _compute_amount_total_words(self):
for move in self:
move.amount_total_words = move.currency_id.amount_to_text(move.amount_total).replace(',', '')
def _compute_linked_attachment_id(self, attachment_field, binary_field):
"""Helper to retreive Attachment from Binary fields
This is needed because fields.Many2one('ir.attachment') makes all
attachments available to the user.
"""
attachments = self.env['ir.attachment'].search([
('res_model', '=', self._name),
('res_id', 'in', self.ids),
('res_field', '=', binary_field)
])
move_vals = {att.res_id: att for att in attachments}
for move in self:
move[attachment_field] = move_vals.get(move._origin.id, False)
def _compute_incoterm_location(self):
pass
@api.depends('partner_id', 'invoice_date', 'amount_total')
def _compute_abnormal_warnings(self):
"""Assign warning fields based on historical data.
The last invoices (between 10 and 30) are used to compute the normal distribution.
If the amount or days between invoices of the current invoice falls outside of the boundaries
of the Bell curve, we warn the user.
"""
if self.env.context.get('disable_abnormal_invoice_detection'):
draft_invoices = self.browse()
else:
draft_invoices = self.filtered(lambda m:
m.is_purchase_document()
and m.state == 'draft'
and m.amount_total
and not (m.partner_id.ignore_abnormal_invoice_date and m.partner_id.ignore_abnormal_invoice_amount)
)
other_moves = self - draft_invoices
other_moves.abnormal_amount_warning = False
other_moves.abnormal_date_warning = False
if not draft_invoices:
return
draft_invoices.flush_recordset(['invoice_date', 'date', 'amount_total', 'partner_id', 'move_type', 'company_id'])
today = fields.Date.context_today(self)
self.env.cr.execute("""
WITH previous_invoices AS (
SELECT this.id,
other.invoice_date,
other.amount_total,
LAG(other.invoice_date) OVER invoice - other.invoice_date AS date_diff
FROM account_move this
JOIN account_move other USING (partner_id, move_type, company_id, currency_id)
WHERE other.state = 'posted'
AND other.invoice_date <= COALESCE(this.invoice_date, this.date, %(today)s)
AND this.id = ANY(%(move_ids)s)
AND this.id != other.id
WINDOW invoice AS (PARTITION BY this.id ORDER BY other.invoice_date DESC)
), stats AS (
SELECT id,
MAX(invoice_date) OVER invoice AS last_invoice_date,
AVG(date_diff) OVER invoice AS date_diff_mean,
STDDEV_SAMP(date_diff) OVER invoice AS date_diff_deviation,
AVG(amount_total) OVER invoice AS amount_mean,
STDDEV_SAMP(amount_total) OVER invoice AS amount_deviation,
ROW_NUMBER() OVER invoice AS row_number
FROM previous_invoices
WINDOW invoice AS (PARTITION BY id ORDER BY invoice_date DESC)
)
SELECT id, last_invoice_date, date_diff_mean, date_diff_deviation, amount_mean, amount_deviation
FROM stats
WHERE row_number BETWEEN 10 AND 30
ORDER BY row_number ASC
""", {
'today': today,
'move_ids': draft_invoices.ids,
})
result = {invoice: vals for invoice, *vals in self.env.cr.fetchall()}
for move in draft_invoices:
invoice_date = move.invoice_date or today
(
last_invoice_date, date_diff_mean, date_diff_deviation,
amount_mean, amount_deviation,
) = result.get(move._origin.id, (invoice_date, 0, 10000000000, 0, 10000000000))
if date_diff_mean > 25:
# Correct for varying days per month and leap years
# If we have a recurring invoice every month, the mean will be ~30.5 days, and the deviation ~1 day.
# We need to add some wiggle room for the month of February otherwise it will trigger because 28 days is outside of the range
date_diff_deviation += 1
wiggle_room_date = 2 * date_diff_deviation
move.abnormal_date_warning = (
not move.partner_id.ignore_abnormal_invoice_date
and (invoice_date - last_invoice_date).days < int(date_diff_mean - wiggle_room_date)
) and _(
"The billing frequency for %(partner_name)s appears unusual. Based on your historical data, "
"the expected next invoice date is not before %(expected_date)s (every %(mean)s (± %(wiggle)s) days).\n"
"Please verify if this date is accurate.",
partner_name=move.partner_id.display_name,
expected_date=format_date(self.env, fields.Date.add(last_invoice_date, days=int(date_diff_mean - wiggle_room_date))),
mean=int(date_diff_mean),
wiggle=int(wiggle_room_date),
)
wiggle_room_amount = 2 * amount_deviation
move.abnormal_amount_warning = (
not move.partner_id.ignore_abnormal_invoice_amount
and not (amount_mean - wiggle_room_amount <= move.amount_total <= amount_mean + wiggle_room_amount)
) and _(
"The amount for %(partner_name)s appears unusual. Based on your historical data, the expected amount is %(mean)s (± %(wiggle)s).\n"
"Please verify if this amount is accurate.",
partner_name=move.partner_id.display_name,
mean=move.currency_id.format(amount_mean),
wiggle=move.currency_id.format(wiggle_room_amount),
)
@api.depends('line_ids.tax_ids')
def _compute_taxes_legal_notes(self):
for move in self:
move.taxes_legal_notes = ''.join(
tax.invoice_legal_notes
for tax in OrderedSet(move.line_ids.tax_ids)
if not is_html_empty(tax.invoice_legal_notes)
)
@api.depends('line_ids.payment_date', 'line_ids.reconciled')
def _compute_next_payment_date(self):
for move in self:
move.next_payment_date = min([line.payment_date for line in move.line_ids.filtered(lambda l: l.payment_date and not l.reconciled)], default=False)
def _search_next_payment_date(self, operator, value):
if operator not in ('=', '<', '<='):
raise UserError(self.env._('Operation not supported'))
return [('line_ids', 'any', [('reconciled', '=', False), ('payment_date', operator, value)])]
# -------------------------------------------------------------------------
# SEARCH METHODS
# -------------------------------------------------------------------------
def _search_journal_group_id(self, operator, value):
field = 'name' if 'like' in operator else 'id'
journal_groups = self.env['account.journal.group'].search([(field, operator, value)])
return [('journal_id', 'not in', journal_groups.excluded_journal_ids.ids)]
# -------------------------------------------------------------------------
# INVERSE METHODS
# -------------------------------------------------------------------------
def _inverse_tax_totals(self):
if self.env.context.get('skip_invoice_sync'):
return
with self._sync_dynamic_line(
existing_key_fname='term_key',
needed_vals_fname='needed_terms',
needed_dirty_fname='needed_terms_dirty',
line_type='payment_term',
container={'records': self},
):
for move in self:
if not move.is_invoice(include_receipts=True):
continue
invoice_totals = move.tax_totals
for subtotal in invoice_totals['subtotals']:
for tax_group in subtotal['tax_groups']:
tax_lines = move.line_ids.filtered(lambda line: line.tax_group_id.id == tax_group['id'])
if tax_lines:
first_tax_line = tax_lines[0]
tax_group_old_amount = sum(tax_lines.mapped('amount_currency'))
sign = -1 if move.is_inbound() else 1
delta_amount = tax_group_old_amount * sign - tax_group['tax_amount_currency']
if not move.currency_id.is_zero(delta_amount):
first_tax_line.amount_currency -= delta_amount * sign
self._compute_amount()
def _inverse_amount_total(self):
for move in self:
if len(move.line_ids) != 2 or move.is_invoice(include_receipts=True):
continue
to_write = []
amount_currency = abs(move.amount_total)
balance = move.currency_id._convert(amount_currency, move.company_currency_id, move.company_id, move.invoice_date or move.date)
for line in move.line_ids:
if not line.currency_id.is_zero(balance - abs(line.balance)):
to_write.append((1, line.id, {
'debit': line.balance > 0.0 and balance or 0.0,
'credit': line.balance < 0.0 and balance or 0.0,
'amount_currency': line.balance > 0.0 and amount_currency or -amount_currency,
}))
move.write({'line_ids': to_write})
@api.onchange('partner_id')
def _inverse_partner_id(self):
for invoice in self:
if invoice.is_invoice(True):
for line in invoice.line_ids + invoice.invoice_line_ids:
if line.partner_id != invoice.commercial_partner_id:
line.partner_id = invoice.commercial_partner_id
line._inverse_partner_id()
@api.onchange('company_id')
def _inverse_company_id(self):
for move in self:
# This can't be caught by a python constraint as it is only triggered at save and the compute method that
# needs this data to be set correctly before saving
if not move.company_id:
raise ValidationError(_("We can't leave this document without any company. Please select a company for this document."))
self._conditional_add_to_compute('journal_id', lambda m: (
not m.journal_id.filtered_domain(self.env['account.journal']._check_company_domain(m.company_id))
))
@api.onchange('currency_id')
def _inverse_currency_id(self):
(self.line_ids | self.invoice_line_ids)._conditional_add_to_compute('currency_id', lambda l: (
l.move_id.is_invoice(True)
and l.move_id.currency_id != l.currency_id
))
@api.onchange('journal_id')
def _inverse_journal_id(self):
self._conditional_add_to_compute('company_id', lambda m: (
not m.company_id
or m.company_id != m.journal_id.company_id
))
self._conditional_add_to_compute('currency_id', lambda m: (
not m.currency_id
or m.journal_id.currency_id and m.currency_id != m.journal_id.currency_id
))
@api.onchange('payment_reference')
def _inverse_payment_reference(self):
self.line_ids._conditional_add_to_compute('name', lambda line: (
line.display_type == 'payment_term'
))
@api.onchange('invoice_payment_term_id')
def _inverse_invoice_payment_term_id(self):
self.line_ids._conditional_add_to_compute('name', lambda l: (
l.display_type == 'payment_term'
))
def _inverse_name(self):
self._conditional_add_to_compute('payment_reference', lambda move: (
move.name and move.name != '/'
))
self._set_next_made_sequence_gap(False)
# -------------------------------------------------------------------------
# ONCHANGE METHODS
# -------------------------------------------------------------------------
@api.onchange('date')
def _onchange_date(self):
if not self.is_invoice(True):
self.line_ids._inverse_amount_currency()
@api.onchange('invoice_vendor_bill_id')
def _onchange_invoice_vendor_bill(self):
if self.invoice_vendor_bill_id:
# Copy invoice lines.
for line in self.invoice_vendor_bill_id.invoice_line_ids:
copied_vals = line.copy_data()[0]
self.invoice_line_ids += self.env['account.move.line'].new(copied_vals)
self.currency_id = self.invoice_vendor_bill_id.currency_id
self.fiscal_position_id = self.invoice_vendor_bill_id.fiscal_position_id
# Reset
self.invoice_vendor_bill_id = False
@api.onchange('fiscal_position_id')
def _onchange_fpos_id_show_update_fpos(self):
self.show_update_fpos = self.line_ids and self._origin.fiscal_position_id != self.fiscal_position_id
@api.onchange('partner_id')
def _onchange_partner_id(self):
self = self.with_company((self.journal_id.company_id or self.env.company)._accessible_branches()[:1])
warning = {}
if self.partner_id:
rec_account = self.partner_id.property_account_receivable_id
pay_account = self.partner_id.property_account_payable_id
if not rec_account and not pay_account:
action = self.env.ref('account.action_account_config')
msg = _('Cannot find a chart of accounts for this company, You should configure it. \nPlease go to Account Configuration.')
raise RedirectWarning(msg, action.id, _('Go to the configuration panel'))
p = self.partner_id
if p.invoice_warn == 'no-message' and p.parent_id:
p = p.parent_id
if p.invoice_warn and p.invoice_warn != 'no-message':
# Block if partner only has warning but parent company is blocked
if p.invoice_warn != 'block' and p.parent_id and p.parent_id.invoice_warn == 'block':
p = p.parent_id
warning = {
'title': _("Warning for %s", p.name),
'message': p.invoice_warn_msg
}
if p.invoice_warn == 'block':
self.partner_id = False
return {'warning': warning}
@api.onchange('name', 'highest_name')
def _onchange_name_warning(self):
if self.name and self.name != '/' and self.name <= (self.highest_name or '') and not self.quick_edit_mode:
self.show_name_warning = True
else:
self.show_name_warning = False
origin_name = self._origin.name
if not origin_name or origin_name == '/':
origin_name = self.highest_name
if (
self.name and self.name != '/'
and origin_name and origin_name != '/'
and self.date == self._origin.date
and self.journal_id == self._origin.journal_id
):
new_format, new_format_values = self._get_sequence_format_param(self.name)
origin_format, origin_format_values = self._get_sequence_format_param(origin_name)
if (
new_format != origin_format
or dict(new_format_values, year=0, month=0, seq=0) != dict(origin_format_values, year=0, month=0, seq=0)
):
changed = _(
"It was previously '%(previous)s' and it is now '%(current)s'.",
previous=origin_name,
current=self.name,
)
reset = self._deduce_sequence_number_reset(self.name)
if reset == 'month':
detected = _(
"The sequence will restart at 1 at the start of every month.\n"
"The year detected here is '%(year)s' and the month is '%(month)s'.\n"
"The incrementing number in this case is '%(formatted_seq)s'."
)
elif reset == 'year':
detected = _(
"The sequence will restart at 1 at the start of every year.\n"
"The year detected here is '%(year)s'.\n"
"The incrementing number in this case is '%(formatted_seq)s'."
)
elif reset == 'year_range':
detected = _(
"The sequence will restart at 1 at the start of every financial year.\n"
"The financial start year detected here is '%(year)s'.\n"
"The financial end year detected here is '%(year_end)s'.\n"
"The incrementing number in this case is '%(formatted_seq)s'."
)
elif reset == 'year_range_month':
detected = _(
"The sequence will restart at 1 at the start of every month.\n"
"The financial start year detected here is '%(year)s'.\n"
"The financial end year detected here is '%(year_end)s'.\n"
"The month detected here is '%(month)s'.\n"
"The incrementing number in this case is '%(formatted_seq)s'."
)
else:
detected = _(
"The sequence will never restart.\n"
"The incrementing number in this case is '%(formatted_seq)s'."
)
new_format_values['formatted_seq'] = "{seq:0{seq_length}d}".format(**new_format_values)
detected = detected % new_format_values
return {'warning': {
'title': _("The sequence format has changed."),
'message': "%s\n\n%s" % (changed, detected)
}}
@api.onchange('journal_id')
def _onchange_journal_id(self):
if not self.quick_edit_mode:
self.name = '/'
self._compute_name()
@api.onchange('invoice_cash_rounding_id')
def _onchange_invoice_cash_rounding_id(self):
for move in self:
if move.invoice_cash_rounding_id.strategy == 'add_invoice_line' and not move.invoice_cash_rounding_id.profit_account_id:
return {'warning': {
'title': _("Warning for Cash Rounding Method: %s", move.invoice_cash_rounding_id.name),
'message': _("You must specify the Profit Account (company dependent)")
}}
# -------------------------------------------------------------------------
# CONSTRAINT METHODS
# -------------------------------------------------------------------------
@contextmanager
def _check_balanced(self, container):
''' Assert the move is fully balanced debit = credit.
An error is raised if it's not the case.
'''
with self._disable_recursion(container, 'check_move_validity', default=True, target=False) as disabled:
yield
if disabled:
return
unbalanced_moves = self._get_unbalanced_moves(container)
if unbalanced_moves:
error_msg = _("An error has occurred.")
for move_id, sum_debit, sum_credit in unbalanced_moves:
move = self.browse(move_id)
error_msg += _(
"\n\n"
"The move (%(move)s) is not balanced.\n"
"The total of debits equals %(debit_total)s and the total of credits equals %(credit_total)s.\n"
"You might want to specify a default account on journal \"%(journal)s\" to automatically balance each move.",
move=move.display_name,
debit_total=format_amount(self.env, sum_debit, move.company_id.currency_id),
credit_total=format_amount(self.env, sum_credit, move.company_id.currency_id),
journal=move.journal_id.name)
raise UserError(error_msg)
def _get_unbalanced_moves(self, container):
moves = container['records'].filtered(lambda move: move.line_ids)
if not moves:
return
# /!\ As this method is called in create / write, we can't make the assumption the computed stored fields
# are already done. Then, this query MUST NOT depend on computed stored fields.
# It happens as the ORM calls create() with the 'no_recompute' statement.
self.env['account.move.line'].flush_model(['debit', 'credit', 'balance', 'currency_id', 'move_id'])
return self.env.execute_query(SQL('''
SELECT line.move_id,
ROUND(SUM(line.debit), currency.decimal_places) debit,
ROUND(SUM(line.credit), currency.decimal_places) credit
FROM account_move_line line
JOIN account_move move ON move.id = line.move_id
JOIN res_company company ON company.id = move.company_id
JOIN res_currency currency ON currency.id = company.currency_id
WHERE line.move_id IN %s
GROUP BY line.move_id, currency.decimal_places
HAVING ROUND(SUM(line.balance), currency.decimal_places) != 0
''', tuple(moves.ids)))
def _check_fiscal_lock_dates(self):
for move in self:
journal = move.journal_id
violated_lock_dates = move.company_id._get_lock_date_violations(
move.date,
fiscalyear=True,
sale=journal and journal.type == 'sale',
purchase=journal and journal.type == 'purchase',
tax=False,
hard=True,
)
if violated_lock_dates:
message = _("You cannot add/modify entries prior to and inclusive of: %(lock_date_info)s.",
lock_date_info=self.env['res.company']._format_lock_dates(violated_lock_dates))
raise UserError(message)
return True
@api.constrains('auto_post', 'invoice_date')
def _require_bill_date_for_autopost(self):
"""Vendor bills must have an invoice date set to be posted. Require it for auto-posted bills."""
for record in self:
if record.auto_post != 'no' and record.is_purchase_document() and not record.invoice_date:
raise ValidationError(_("For this entry to be automatically posted, it required a bill date."))
@api.constrains('journal_id', 'move_type')
def _check_journal_move_type(self):
for move in self:
if move.is_purchase_document(include_receipts=True) and move.journal_id.type != 'purchase':
raise ValidationError(_("Cannot create a purchase document in a non purchase journal"))
if move.is_sale_document(include_receipts=True) and move.journal_id.type != 'sale':
raise ValidationError(_("Cannot create a sale document in a non sale journal"))
@api.constrains('line_ids', 'fiscal_position_id', 'company_id')
def _validate_taxes_country(self):
""" By playing with the fiscal position in the form view, it is possible to keep taxes on the invoices from
a different country than the one allowed by the fiscal country or the fiscal position.
This contrains ensure such account.move cannot be kept, as they could generate inconsistencies in the reports.
"""
self._compute_tax_country_id() # We need to ensure this field has been computed, as we use it in our check
for record in self:
amls = record.line_ids
impacted_countries = amls.tax_ids.country_id | amls.tax_line_id.country_id
if impacted_countries and impacted_countries != record.tax_country_id:
if record.fiscal_position_id and impacted_countries != record.fiscal_position_id.country_id:
raise ValidationError(_("This entry contains taxes that are not compatible with your fiscal position. Check the country set in fiscal position and in your tax configuration."))
raise ValidationError(_("This entry contains one or more taxes that are incompatible with your fiscal country. Check company fiscal country in the settings and tax country in taxes configuration."))
# -------------------------------------------------------------------------
# CATALOG
# -------------------------------------------------------------------------
def action_add_from_catalog(self):
res = super().action_add_from_catalog()
if res['context'].get('product_catalog_order_model') == 'account.move':
res['search_view_id'] = [self.env.ref('account.product_view_search_catalog').id, 'search']
return res
def _get_action_add_from_catalog_extra_context(self):
res = super()._get_action_add_from_catalog_extra_context()
if self.is_purchase_document() and self.partner_id:
res['search_default_seller_ids'] = self.partner_id.name
res['product_catalog_currency_id'] = self.currency_id.id
res['product_catalog_digits'] = self.line_ids._fields['price_unit'].get_digits(self.env)
return res
def _get_product_catalog_domain(self):
if self.is_sale_document():
return expression.AND([super()._get_product_catalog_domain(), [('sale_ok', '=', True)]])
elif self.is_purchase_document():
return expression.AND([super()._get_product_catalog_domain(), [('purchase_ok', '=', True)]])
else: # In case of an entry
return super()._get_product_catalog_domain()
def _default_order_line_values(self, child_field=False):
default_data = super()._default_order_line_values(child_field)
new_default_data = self.env['account.move.line']._get_product_catalog_lines_data()
return {**default_data, **new_default_data}
def _get_product_catalog_order_data(self, products, **kwargs):
product_catalog = super()._get_product_catalog_order_data(products, **kwargs)
for product in products:
product_catalog[product.id] |= self._get_product_price_and_data(product)
return product_catalog
def _get_product_price_and_data(self, product):
"""
This function will return a dict containing the price of the product. If the product is a sale document then
we return the list price (which is the "Sales Price" in a product) otherwise we return the standard_price
(which is the "Cost" in a product).
In case of a purchase document, it's possible that we have special price for certain partner.
We will check the sellers set on the product and update the price and min_qty for it if needed.
"""
self.ensure_one()
product_infos = {'price': product.list_price if self.is_sale_document() else product.standard_price}
# Check if there is a price and a minimum quantity for the order's vendor.
if self.is_purchase_document() and self.partner_id:
seller = product._select_seller(
partner_id=self.partner_id,
quantity=None,
date=self.invoice_date,
uom_id=product.uom_id,
ordered_by='min_qty',
params={'order_id': self}
)
if seller:
product_infos.update(
price=seller.price,
min_qty=seller.min_qty,
)
return product_infos
def _get_product_catalog_record_lines(self, product_ids, child_field=False):
grouped_lines = defaultdict(lambda: self.env['account.move.line'])
for line in self.line_ids:
if line.display_type == 'product' and line.product_id.id in product_ids:
grouped_lines[line.product_id] |= line
return grouped_lines
def _update_order_line_info(self, product_id, quantity, **kwargs):
""" Update account_move_line information for a given product or create a
new one if none exists yet.
:param int product_id: The product, as a `product.product` id.
:param int quantity: The quantity selected in the catalog
:return: The unit price of the product, based on the pricelist of the
sale order and the quantity selected.
:rtype: float
"""
move_line = self.line_ids.filtered(lambda line: line.product_id.id == product_id)
if move_line:
if quantity != 0:
move_line.quantity = quantity
elif self.state in {'draft', 'sent'}:
price_unit = self._get_product_price_and_data(move_line.product_id)['price']
# The catalog is designed to allow the user to select products quickly.
# Therefore, sometimes they may select the wrong product or decide to remove
# some of them from the quotation. The unlink is there for that reason.
move_line.unlink()
return price_unit
else:
move_line.quantity = 0
elif quantity > 0:
move_line = self.env['account.move.line'].create({
'move_id': self.id,
'quantity': quantity,
'product_id': product_id,
})
return move_line.price_unit
def _is_readonly(self):
"""
Check if the move has been canceled
"""
self.ensure_one()
return self.state == 'cancel'
# -------------------------------------------------------------------------
# EARLY PAYMENT DISCOUNT
# -------------------------------------------------------------------------
def _is_eligible_for_early_payment_discount(self, currency, reference_date):
self.ensure_one()
payment_terms = self.line_ids.filtered(lambda line: line.display_type == 'payment_term')
return self.currency_id == currency \
and self.move_type in ('out_invoice', 'out_receipt', 'in_invoice', 'in_receipt') \
and self.invoice_payment_term_id.early_discount \
and (not reference_date or reference_date <= self.invoice_payment_term_id._get_last_discount_date(self.invoice_date)) \
and not (payment_terms.matched_debit_ids + payment_terms.matched_credit_ids)
# -------------------------------------------------------------------------
# BUSINESS MODELS SYNCHRONIZATION
# -------------------------------------------------------------------------
def _synchronize_business_models(self, changed_fields):
''' Ensure the consistency between:
account.payment & account.move
account.bank.statement.line & account.move
The idea is to call the method performing the synchronization of the business
models regarding their related journal entries. To avoid cycling, the
'skip_account_move_synchronization' key is used through the context.
:param changed_fields: A set containing all modified fields on account.move.
'''
if self._context.get('skip_account_move_synchronization'):
return
self_sudo = self.sudo()
self_sudo.statement_line_id._synchronize_from_moves(changed_fields)
# -------------------------------------------------------------------------
# DYNAMIC LINES
# -------------------------------------------------------------------------
def _recompute_cash_rounding_lines(self):
''' Handle the cash rounding feature on invoices.
In some countries, the smallest coins do not exist. For example, in Switzerland, there is no coin for 0.01 CHF.
For this reason, if invoices are paid in cash, you have to round their total amount to the smallest coin that
exists in the currency. For the CHF, the smallest coin is 0.05 CHF.
There are two strategies for the rounding:
1) Add a line on the invoice for the rounding: The cash rounding line is added as a new invoice line.
2) Add the rounding in the biggest tax amount: The cash rounding line is added as a new tax line on the tax
having the biggest balance.
'''
self.ensure_one()
def _compute_cash_rounding(self, total_amount_currency):
''' Compute the amount differences due to the cash rounding.
:param self: The current account.move record.
:param total_amount_currency: The invoice's total in invoice's currency.
:return: The amount differences both in company's currency & invoice's currency.
'''
difference = self.invoice_cash_rounding_id.compute_difference(self.currency_id, total_amount_currency)
if self.currency_id == self.company_id.currency_id:
diff_amount_currency = diff_balance = difference
else:
diff_amount_currency = difference
diff_balance = self.currency_id._convert(diff_amount_currency, self.company_id.currency_id, self.company_id, self.invoice_date or self.date)
return diff_balance, diff_amount_currency
def _apply_cash_rounding(self, diff_balance, diff_amount_currency, cash_rounding_line):
''' Apply the cash rounding.
:param self: The current account.move record.
:param diff_balance: The computed balance to set on the new rounding line.
:param diff_amount_currency: The computed amount in invoice's currency to set on the new rounding line.
:param cash_rounding_line: The existing cash rounding line.
:return: The newly created rounding line.
'''
rounding_line_vals = {
'balance': diff_balance,
'amount_currency': diff_amount_currency,
'partner_id': self.partner_id.id,
'move_id': self.id,
'currency_id': self.currency_id.id,
'company_id': self.company_id.id,
'company_currency_id': self.company_id.currency_id.id,
'display_type': 'rounding',
}
if self.invoice_cash_rounding_id.strategy == 'biggest_tax':
biggest_tax_line = None
for tax_line in self.line_ids.filtered('tax_repartition_line_id'):
if not biggest_tax_line or abs(tax_line.balance) > abs(biggest_tax_line.balance):
biggest_tax_line = tax_line
# No tax found.
if not biggest_tax_line:
return
rounding_line_vals.update({
'name': _("%(tax_name)s (rounding)", tax_name=biggest_tax_line.name),
'account_id': biggest_tax_line.account_id.id,
'tax_repartition_line_id': biggest_tax_line.tax_repartition_line_id.id,
'tax_tag_ids': [(6, 0, biggest_tax_line.tax_tag_ids.ids)],
'tax_ids': [Command.set(biggest_tax_line.tax_ids.ids)]
})
elif self.invoice_cash_rounding_id.strategy == 'add_invoice_line':
if diff_balance > 0.0 and self.invoice_cash_rounding_id.loss_account_id:
account_id = self.invoice_cash_rounding_id.loss_account_id.id
else:
account_id = self.invoice_cash_rounding_id.profit_account_id.id
rounding_line_vals.update({
'name': self.invoice_cash_rounding_id.name,
'account_id': account_id,
'tax_ids': [Command.clear()]
})
# Create or update the cash rounding line.
if cash_rounding_line:
cash_rounding_line.write(rounding_line_vals)
else:
cash_rounding_line = self.env['account.move.line'].create(rounding_line_vals)
existing_cash_rounding_line = self.line_ids.filtered(lambda line: line.display_type == 'rounding')
# The cash rounding has been removed.
if not self.invoice_cash_rounding_id:
existing_cash_rounding_line.unlink()
# self.line_ids -= existing_cash_rounding_line
return
# The cash rounding strategy has changed.
if self.invoice_cash_rounding_id and existing_cash_rounding_line:
strategy = self.invoice_cash_rounding_id.strategy
old_strategy = 'biggest_tax' if existing_cash_rounding_line.tax_line_id else 'add_invoice_line'
if strategy != old_strategy:
# self.line_ids -= existing_cash_rounding_line
existing_cash_rounding_line.unlink()
existing_cash_rounding_line = self.env['account.move.line']
others_lines = self.line_ids.filtered(lambda line: line.account_id.account_type not in ('asset_receivable', 'liability_payable'))
others_lines -= existing_cash_rounding_line
total_amount_currency = sum(others_lines.mapped('amount_currency'))
diff_balance, diff_amount_currency = _compute_cash_rounding(self, total_amount_currency)
# The invoice is already rounded.
if self.currency_id.is_zero(diff_balance) and self.currency_id.is_zero(diff_amount_currency):
existing_cash_rounding_line.unlink()
# self.line_ids -= existing_cash_rounding_line
return
# No update needed
if existing_cash_rounding_line \
and float_compare(existing_cash_rounding_line.balance, diff_balance, precision_rounding=self.currency_id.rounding) == 0 \
and float_compare(existing_cash_rounding_line.amount_currency, diff_amount_currency, precision_rounding=self.currency_id.rounding) == 0:
return
_apply_cash_rounding(self, diff_balance, diff_amount_currency, existing_cash_rounding_line)
def _get_automatic_balancing_account(self):
""" Small helper for special cases where we want to auto balance a move with a specific account. """
self.ensure_one()
return self.company_id.account_journal_suspense_account_id.id
@contextmanager
def _sync_unbalanced_lines(self, container):
def has_tax(move):
return bool(move.line_ids.tax_ids)
move_had_tax = {move: has_tax(move) for move in container['records']}
yield
# Skip posted moves.
for move in (x for x in container['records'] if x.state != 'posted'):
if not has_tax(move) and not move_had_tax.get(move):
continue # only manage automatically unbalanced when taxes are involved
if move_had_tax.get(move) and not has_tax(move):
# taxes have been removed, the tax sync is deactivated so we need to clear everything here
move.line_ids.filtered('tax_line_id').unlink()
move.line_ids.tax_tag_ids = [Command.set([])]
# Set the balancing line's balance and amount_currency to zero,
# so that it does not interfere with _get_unbalanced_moves() below.
balance_name = _('Automatic Balancing Line')
existing_balancing_line = move.line_ids.filtered(lambda line: line.name == balance_name)
if existing_balancing_line:
existing_balancing_line.balance = existing_balancing_line.amount_currency = 0.0
# Create an automatic balancing line to make sure the entry can be saved/posted.
# If such a line already exists, we simply update its amounts.
unbalanced_moves = self._get_unbalanced_moves({'records': move})
if isinstance(unbalanced_moves, list) and len(unbalanced_moves) == 1:
dummy, debit, credit = unbalanced_moves[0]
vals = {'balance': credit - debit}
if existing_balancing_line:
existing_balancing_line.write(vals)
else:
vals.update({
'name': balance_name,
'move_id': move.id,
'account_id': move._get_automatic_balancing_account(),
'currency_id': move.currency_id.id,
# A balancing line should never have default taxes applied to it, it doesn't work well and wouldn't make much sense.
'tax_ids': False,
})
self.env['account.move.line'].create(vals)
@contextmanager
def _sync_rounding_lines(self, container):
yield
for invoice in container['records']:
if invoice.state != 'posted':
invoice._recompute_cash_rounding_lines()
@api.model
def _sync_dynamic_line_needed_values(self, values_list):
res = {}
for computed_needed in values_list:
if computed_needed is False:
continue # there was an invalidation, let's hope nothing needed to be changed...
for key, values in computed_needed.items():
if key not in res:
res[key] = dict(values)
else:
ignore = True
for fname in res[key]:
if self.env['account.move.line']._fields[fname].type == 'monetary':
res[key][fname] += values[fname]
if res[key][fname]:
ignore = False
if ignore:
del res[key]
# Convert float values to their "ORM cache" one to prevent different rounding calculations
for key, values in res.items():
move_id = key.get('move_id')
if not move_id:
continue
record = self.env['account.move'].browse(move_id)
for fname, current_value in values.items():
field = self.env['account.move.line']._fields[fname]
if isinstance(current_value, float):
values[fname] = field.convert_to_cache(current_value, record)
return res
@contextmanager
def _sync_tax_lines(self, container):
AccountTax = self.env['account.tax']
fake_base_line = AccountTax._prepare_base_line_for_taxes_computation(None)
def get_base_lines(move):
return move.line_ids.filtered(lambda line: line.display_type in ('product', 'epd', 'rounding', 'cogs'))
def get_tax_lines(move):
return move.line_ids.filtered('tax_repartition_line_id')
def get_value(record, field):
return self.env['account.move.line']._fields[field].convert_to_write(record[field], record)
def get_tax_line_tracked_fields(line):
return ('amount_currency', 'balance')
def get_base_line_tracked_fields(line):
grouping_key = AccountTax._prepare_base_line_grouping_key(fake_base_line)
if line.move_id.is_invoice(include_receipts=True):
extra_fields = ['price_unit', 'quantity', 'discount']
else:
extra_fields = ['amount_currency']
return list(grouping_key.keys()) + extra_fields
def field_has_changed(values, record, field):
return get_value(record, field) != values.get(record, {}).get(field)
def get_changed_lines(values, records, fields=None):
return (
record
for record in records
if record not in values
or any(field_has_changed(values, record, field) for field in values[record] if not fields or field in fields)
)
def any_field_has_changed(values, records, fields=None):
return any(record for record in get_changed_lines(values, records, fields))
def is_write_needed(line, values):
return any(
self.env['account.move.line']._fields[fname].convert_to_write(line[fname], self) != values[fname]
for fname in values
)
moves_values_before = {
move: {
field: get_value(move, field)
for field in ('currency_id', 'partner_id', 'move_type')
}
for move in container['records']
if move.state == 'draft'
}
base_lines_values_before = {
move: {
line: {
field: get_value(line, field)
for field in get_base_line_tracked_fields(line)
}
for line in get_base_lines(move)
}
for move in container['records']
}
tax_lines_values_before = {
move: {
line: {
field: get_value(line, field)
for field in get_tax_line_tracked_fields(line)
}
for line in get_tax_lines(move)
}
for move in container['records']
}
yield
to_delete = []
to_create = []
for move in container['records']:
if move.state != 'draft':
continue
tax_lines = get_tax_lines(move)
base_lines = get_base_lines(move)
move_tax_lines_values_before = tax_lines_values_before.get(move, {})
move_base_lines_values_before = base_lines_values_before.get(move, {})
if (
move.is_invoice(include_receipts=True)
and (
field_has_changed(moves_values_before, move, 'currency_id')
or field_has_changed(moves_values_before, move, 'move_type')
)
):
# Changing the type of an invoice using 'switch to refund' feature or just changing the currency.
round_from_tax_lines = False
elif changed_lines := list(get_changed_lines(move_base_lines_values_before, base_lines)):
# A base line has been modified.
round_from_tax_lines = (
# The changed lines don't affect the taxes.
all(not line.tax_ids and not move_base_lines_values_before.get(line, {}).get('tax_ids') for line in changed_lines)
# Keep the tax lines amounts if an amount has been manually computed.
or any_field_has_changed(move_tax_lines_values_before, tax_lines)
)
# If the move has been created with all lines including the tax ones and the balance/amount_currency are provided on
# base lines, we don't need to recompute anything.
if (
round_from_tax_lines
and any(line[field] for line in changed_lines for field in ('amount_currency', 'balance'))
):
continue
elif any(line not in base_lines for line, values in move_base_lines_values_before.items() if values['tax_ids']):
# Removed a base line affecting the taxes.
round_from_tax_lines = any_field_has_changed(move_tax_lines_values_before, tax_lines)
else:
continue
base_lines_values, tax_lines_values = move._get_rounded_base_and_tax_lines(round_from_tax_lines=round_from_tax_lines)
AccountTax._add_accounting_data_in_base_lines_tax_details(base_lines_values, move.company_id, include_caba_tags=move.always_tax_exigible)
tax_results = AccountTax._prepare_tax_lines(base_lines_values, move.company_id, tax_lines=tax_lines_values)
for base_line, to_update in tax_results['base_lines_to_update']:
line = base_line['record']
if is_write_needed(line, to_update):
line.write(to_update)
for tax_line_vals in tax_results['tax_lines_to_delete']:
to_delete.append(tax_line_vals['record'].id)
for tax_line_vals in tax_results['tax_lines_to_add']:
to_create.append({
**tax_line_vals,
'display_type': 'tax',
'move_id': move.id,
})
for tax_line_vals, grouping_key, to_update in tax_results['tax_lines_to_update']:
line = tax_line_vals['record']
if is_write_needed(line, to_update):
line.write(to_update)
if to_delete:
self.env['account.move.line'].browse(to_delete).with_context(dynamic_unlink=True).unlink()
if to_create:
self.env['account.move.line'].create(to_create)
@contextmanager
def _sync_dynamic_line(self, existing_key_fname, needed_vals_fname, needed_dirty_fname, line_type, container):
def existing():
return {
line: line[existing_key_fname]
for line in container['records'].line_ids
if line[existing_key_fname]
}
def needed():
return self._sync_dynamic_line_needed_values(container['records'].mapped(needed_vals_fname))
def dirty():
*path, dirty_fname = needed_dirty_fname.split('.')
eligible_recs = container['records'].mapped('.'.join(path))
if eligible_recs._name == 'account.move.line':
eligible_recs = eligible_recs.filtered(lambda l: l.display_type != 'cogs')
dirty_recs = eligible_recs.filtered(dirty_fname)
return dirty_recs, dirty_fname
def filter_trivial(mapping):
return {k: v for k, v in mapping.items() if 'id' not in v}
inv_existing_before = existing()
needed_before = needed()
dirty_recs_before, dirty_fname = dirty()
dirty_recs_before[dirty_fname] = False
yield
dirty_recs_after, dirty_fname = dirty()
if not dirty_recs_after: # TODO improve filter
return
inv_existing_after = existing()
needed_after = needed()
# Filter out deleted lines from `needed_before` to not recompute lines if not necessary or wanted
line_ids = set(self.env['account.move.line'].browse(k['id'] for k in needed_before if 'id' in k).exists().ids)
needed_before = {k: v for k, v in needed_before.items() if 'id' not in k or k['id'] in line_ids}
# old key to new key for the same line
before2after = {
before: inv_existing_after[bline]
for bline, before in inv_existing_before.items()
if bline in inv_existing_after
}
if needed_after == needed_before:
return # do not modify user input if nothing changed in the needs
if not needed_before and (filter_trivial(inv_existing_after) != filter_trivial(inv_existing_before)):
return # do not modify user input if already created manually
existing_after = defaultdict(list)
for k, v in inv_existing_after.items():
existing_after[v].append(k)
to_delete = [
line.id
for line, key in inv_existing_before.items()
if key not in needed_after
and key in existing_after
and before2after[key] not in needed_after
]
to_delete_set = set(to_delete)
to_delete.extend(line.id
for line, key in inv_existing_after.items()
if key not in needed_after and line.id not in to_delete_set
)
to_create = {
key: values
for key, values in needed_after.items()
if key not in existing_after
}
to_write = {
line: values
for key, values in needed_after.items()
for line in existing_after[key]
if any(
self.env['account.move.line']._fields[fname].convert_to_write(line[fname], self)
!= values[fname]
for fname in values
)
}
while to_delete and to_create:
key, values = to_create.popitem()
line_id = to_delete.pop()
self.env['account.move.line'].browse(line_id).write(
{**key, **values, 'display_type': line_type}
)
if to_delete:
self.env['account.move.line'].browse(to_delete).with_context(dynamic_unlink=True).unlink()
if to_create:
self.env['account.move.line'].create([
{**key, **values, 'display_type': line_type}
for key, values in to_create.items()
])
if to_write:
for line, values in to_write.items():
line.write(values)
@contextmanager
def _sync_invoice(self, container):
def existing():
return {
move: {
'commercial_partner_id': move.commercial_partner_id,
}
for move in container['records'].filtered(lambda m: m.is_invoice(True))
}
def changed(fname):
return move not in before or before[move][fname] != after[move][fname]
before = existing()
yield
after = existing()
for move in after:
if changed('commercial_partner_id'):
move.line_ids.partner_id = after[move]['commercial_partner_id']
@contextmanager
def _sync_dynamic_lines(self, container):
with self._disable_recursion(container, 'skip_invoice_sync') as disabled:
if disabled:
yield
return
def update_containers():
# Only invoice-like and journal entries in "auto tax mode" are synced
tax_container['records'] = container['records'].filtered(lambda m: m.is_invoice(True) or m.line_ids.tax_ids or m.line_ids.tax_repartition_line_id)
invoice_container['records'] = container['records'].filtered(lambda m: m.is_invoice(True))
misc_container['records'] = container['records'].filtered(lambda m: m.is_entry() and not m.tax_cash_basis_origin_move_id)
tax_container, invoice_container, misc_container = ({} for __ in range(3))
update_containers()
with ExitStack() as stack:
stack.enter_context(self._sync_dynamic_line(
existing_key_fname='term_key',
needed_vals_fname='needed_terms',
needed_dirty_fname='needed_terms_dirty',
line_type='payment_term',
container=invoice_container,
))
stack.enter_context(self._sync_unbalanced_lines(misc_container))
stack.enter_context(self._sync_rounding_lines(invoice_container))
stack.enter_context(self._sync_dynamic_line(
existing_key_fname='discount_allocation_key',
needed_vals_fname='line_ids.discount_allocation_needed',
needed_dirty_fname='line_ids.discount_allocation_dirty',
line_type='discount',
container=invoice_container,
))
stack.enter_context(self._sync_tax_lines(tax_container))
stack.enter_context(self._sync_dynamic_line(
existing_key_fname='epd_key',
needed_vals_fname='line_ids.epd_needed',
needed_dirty_fname='line_ids.epd_dirty',
line_type='epd',
container=invoice_container,
))
stack.enter_context(self._sync_invoice(invoice_container))
line_container = {'records': self.line_ids}
with self.line_ids._sync_invoice(line_container):
yield
line_container['records'] = self.line_ids
update_containers()
# -------------------------------------------------------------------------
# LOW-LEVEL METHODS
# -------------------------------------------------------------------------
def check_field_access_rights(self, operation, field_names):
result = super().check_field_access_rights(operation, field_names)
if not field_names:
weirdos = ['needed_terms', 'quick_encoding_vals', 'payment_term_details']
result = [fname for fname in result if fname not in weirdos]
return result
def copy_data(self, default=None):
default = dict(default or {})
vals_list = super().copy_data(default)
default_date = fields.Date.to_date(default.get('date'))
for move, vals in zip(self, vals_list):
if move.move_type in ('out_invoice', 'in_invoice'):
vals['line_ids'] = [
(command, _id, line_vals)
for command, _id, line_vals in vals['line_ids']
if command == Command.CREATE
]
elif move.move_type == 'entry':
if 'partner_id' not in vals:
vals['partner_id'] = False
user_fiscal_lock_date = move.company_id._get_user_fiscal_lock_date(move.journal_id)
if (default_date or move.date) <= user_fiscal_lock_date:
vals['date'] = user_fiscal_lock_date + timedelta(days=1)
if not move.journal_id.active and 'journal_id' in vals:
del vals['journal_id']
return vals_list
def copy(self, default=None):
default = dict(default or {})
new_moves = super().copy(default)
bodies = {}
for old_move, new_move in zip(self, new_moves):
message_origin = '' if not new_move.auto_post_origin_id else \
(Markup('<br/>') + _('This recurring entry originated from %s', new_move.auto_post_origin_id._get_html_link()))
message_content = _('This entry has been reversed from %s', old_move._get_html_link()) if default.get('reversed_entry_id') else _('This entry has been duplicated from %s', old_move._get_html_link())
bodies[new_move.id] = message_content + message_origin
new_moves._message_log_batch(bodies=bodies)
return new_moves
def _sanitize_vals(self, vals):
if vals.get('invoice_line_ids') and vals.get('line_ids'):
# values can sometimes be in only one of the two fields, sometimes in
# both fields, sometimes one field can be explicitely empty while the other
# one is not, sometimes not...
update_vals = {
line_id: line_vals[0]
for command, line_id, *line_vals in vals['invoice_line_ids']
if command == Command.UPDATE
}
for command, line_id, *line_vals in vals['line_ids']:
if command == Command.UPDATE and line_id in update_vals:
line_vals[0].update(update_vals.pop(line_id))
for line_id, line_vals in update_vals.items():
vals['line_ids'] += [Command.update(line_id, line_vals)]
for command, line_id, *line_vals in vals['invoice_line_ids']:
assert command not in (Command.SET, Command.CLEAR)
if [command, line_id, *line_vals] not in vals['line_ids']:
vals['line_ids'] += [(command, line_id, *line_vals)]
del vals['invoice_line_ids']
return vals
def _stolen_move(self, vals):
for command in vals.get('line_ids', ()):
if command[0] == Command.LINK:
yield self.env['account.move.line'].browse(command[1]).move_id.id
if command[0] == Command.SET:
yield from self.env['account.move.line'].browse(command[2]).move_id.ids
def _get_protected_vals(self, vals, records):
protected = set()
for fname in vals:
field = records._fields.get(fname)
if field.inverse or (field.compute and not field.readonly):
protected.update(self.pool.field_computed.get(field, [field]))
return [(protected, rec) for rec in records]
@api.model_create_multi
def create(self, vals_list):
if any('state' in vals and vals.get('state') == 'posted' for vals in vals_list):
raise UserError(_('You cannot create a move already in the posted state. Please create a draft move and post it after.'))
container = {'records': self}
with self._check_balanced(container):
with self._sync_dynamic_lines(container):
for vals in vals_list:
self._sanitize_vals(vals)
stolen_moves = self.browse(set(move for vals in vals_list for move in self._stolen_move(vals)))
moves = super().create(vals_list)
container['records'] = moves | stolen_moves
for move, vals in zip(moves, vals_list):
if 'tax_totals' in vals:
move.tax_totals = vals['tax_totals']
moves.is_manually_modified = False
return moves
def write(self, vals):
if not vals:
return True
self._sanitize_vals(vals)
for move in self:
violated_fields = set(vals).intersection(move._get_integrity_hash_fields() + ['inalterable_hash'])
if move.inalterable_hash and violated_fields:
raise UserError(_(
"This document is protected by a hash. "
"Therefore, you cannot edit the following fields: %s.",
', '.join(f['string'] for f in self.fields_get(violated_fields).values())
))
if (
move.posted_before
and 'journal_id' in vals and move.journal_id.id != vals['journal_id']
and not (move.name == '/' or not move.name or ('name' in vals and (vals['name'] == '/' or not vals['name'])))
):
raise UserError(_('You cannot edit the journal of an account move if it has been posted once, unless the name is removed or set to "/". This might create a gap in the sequence.'))
if (
move.name and move.name != '/'
and move.sequence_number not in (0, 1)
and 'journal_id' in vals and move.journal_id.id != vals['journal_id']
and not move.quick_edit_mode
and not ('name' in vals and (vals['name'] == '/' or not vals['name']))
):
raise UserError(_('You cannot edit the journal of an account move with a sequence number assigned, unless the name is removed or set to "/". This might create a gap in the sequence.'))
# You can't change the date or name of a move being inside a locked period.
if move.state == "posted" and (
('name' in vals and move.name != vals['name'])
or ('date' in vals and move.date != vals['date'])
):
move._check_fiscal_lock_dates()
move.line_ids._check_tax_lock_date()
# You can't post subtract a move to a locked period.
if 'state' in vals and move.state == 'posted' and vals['state'] != 'posted':
move._check_fiscal_lock_dates()
move.line_ids._check_tax_lock_date()
# Disallow modifying readonly fields on a posted move
move_state = vals.get('state', move.state)
unmodifiable_fields = (
'invoice_line_ids', 'line_ids', 'invoice_date', 'date', 'partner_id', 'partner_bank_id',
'invoice_payment_term_id', 'currency_id', 'fiscal_position_id', 'invoice_cash_rounding_id')
readonly_fields = [val for val in vals if val in unmodifiable_fields]
if not self._context.get('skip_readonly_check') and move_state == "posted" and readonly_fields:
raise UserError(_("You cannot modify the following readonly fields on a posted move: %s", ', '.join(readonly_fields)))
if move.journal_id.sequence_override_regex and vals.get('name') and vals['name'] != '/' and not re.match(move.journal_id.sequence_override_regex, vals['name']):
if not self.env.user.has_group('account.group_account_manager'):
raise UserError(_('The Journal Entry sequence is not conform to the current format. Only the Accountant can change it.'))
move.journal_id.sequence_override_regex = False
if {'sequence_prefix', 'sequence_number', 'journal_id', 'name'} & vals.keys():
self._set_next_made_sequence_gap(True)
stolen_moves = self.browse(set(move for move in self._stolen_move(vals)))
container = {'records': self | stolen_moves}
with self.env.protecting(self._get_protected_vals(vals, self)), self._check_balanced(container):
with self._sync_dynamic_lines(container):
if 'is_manually_modified' not in vals and not self.env.context.get('skip_is_manually_modified'):
vals['is_manually_modified'] = True
res = super(AccountMove, self.with_context(
skip_account_move_synchronization=True,
)).write(vals)
# Reset the name of draft moves when changing the journal.
# Protected against holes in the pre-validation checks.
if 'journal_id' in vals and 'name' not in vals:
self.name = False
self._compute_name()
# You can't change the date of a not-locked move to a locked period.
# You can't post a new journal entry inside a locked period.
if 'date' in vals or 'state' in vals:
posted_move = self.filtered(lambda m: m.state == 'posted')
posted_move._check_fiscal_lock_dates()
posted_move.line_ids._check_tax_lock_date()
if vals.get('state') == 'posted':
self.flush_recordset() # Ensure that the name is correctly computed
self._hash_moves()
self._synchronize_business_models(set(vals.keys()))
# Apply the rounding on the Quick Edit mode only when adding a new line
for move in self:
if 'tax_totals' in vals:
super(AccountMove, move).write({'tax_totals': vals['tax_totals']})
if 'journal_id' in vals:
self.line_ids._check_constrains_account_id_journal_id()
return res
def check_move_sequence_chain(self):
return self.filtered(lambda move: move.name != '/')._is_end_of_seq_chain()
def _get_unlink_logger_message(self):
""" Before unlink, get a log message for audit trail if it's enabled.
Logger is added here because in api ondelete, account.move.line is deleted, and we can't get total amount """
if not self._context.get('force_delete'):
pass
moves_details = []
for move in self.filtered(lambda m: m.posted_before and m.company_id.check_account_audit_trail):
entry_details = f"{move.name} ({move.id}) amount {move.amount_total} {move.currency_id.name} and partner {move.partner_id.display_name}"
account_balances_per_account = defaultdict(float)
for line in move.line_ids:
account_balances_per_account[line.account_id] += line.balance
account_details = "\n".join(
f"- {account.name} ({account.id}) with balance {balance} {move.currency_id.name}"
for account, balance in account_balances_per_account.items()
)
moves_details.append(f"{entry_details}\n{account_details}")
if moves_details:
return "\nForce deleted Journal Entries by {user_name} ({user_id})\nEntries\n{moves_details}".format(
user_name=self.env.user.name,
user_id=self.env.user.id,
moves_details="\n".join(moves_details),
)
@api.ondelete(at_uninstall=False)
def _unlink_forbid_parts_of_chain(self):
""" For a user with Billing/Bookkeeper rights, when the fidu mode is deactivated,
moves with a sequence number can only be deleted if they are the last element of a chain of sequence.
If they are not, deleting them would create a gap. If the user really wants to do this, he still can
explicitly empty the 'name' field of the move; but we discourage that practice.
If a user is a Billing Administrator/Accountant or if fidu mode is activated, we show a warning,
but they can delete the moves even if it creates a sequence gap.
"""
if not (
self.env.user.has_group('account.group_account_manager')
or any(self.company_id.mapped('quick_edit_mode'))
or self._context.get('force_delete')
or self.check_move_sequence_chain()
):
raise UserError(_(
"You cannot delete this entry, as it has already consumed a sequence number and is not the last one in the chain. "
"You should probably revert it instead."
))
@api.ondelete(at_uninstall=False)
def _unlink_account_audit_trail_except_once_post(self):
if not self._context.get('force_delete') and any(
move.posted_before and move.company_id.check_account_audit_trail
for move in self
):
raise UserError(_(
"To keep the audit trail, you can not delete journal entries once they have been posted.\n"
"Instead, you can cancel the journal entry."
))
def unlink(self):
self._set_next_made_sequence_gap(True)
self = self.with_context(skip_invoice_sync=True, dynamic_unlink=True) # no need to sync to delete everything
logger_message = self._get_unlink_logger_message()
self.line_ids.unlink()
res = super().unlink()
if logger_message:
_logger.info(logger_message)
return res
@api.depends('partner_id', 'date', 'state', 'move_type')
@api.depends_context('input_full_display_name')
def _compute_display_name(self):
for move in self:
move.display_name = move._get_move_display_name(show_ref=True)
def onchange(self, values, field_names, fields_spec):
# Since only one field can be changed at the same time (the record is
# saved when changing tabs) we can avoid building the snapshots for the
# other field
if 'line_ids' in field_names:
values = {key: val for key, val in values.items() if key != 'invoice_line_ids'}
fields_spec = {key: val for key, val in fields_spec.items() if key != 'invoice_line_ids'}
elif 'invoice_line_ids' in field_names:
values = {key: val for key, val in values.items() if key != 'line_ids'}
fields_spec = {key: val for key, val in fields_spec.items() if key != 'line_ids'}
return super().onchange(values, field_names, fields_spec)
# -------------------------------------------------------------------------
# RECONCILIATION METHODS
# -------------------------------------------------------------------------
def _collect_tax_cash_basis_values(self):
''' Collect all information needed to create the tax cash basis journal entries:
- Determine if a tax cash basis journal entry is needed.
- Compute the lines to be processed and the amounts needed to compute a percentage.
:return: A dictionary:
* move: The current account.move record passed as parameter.
* to_process_lines: A tuple (caba_treatment, line) where:
- caba_treatment is either 'tax' or 'base', depending on what should
be considered on the line when generating the caba entry.
For example, a line with tax_ids=caba and tax_line_id=non_caba
will have a 'base' caba treatment, as we only want to treat its base
part in the caba entry (the tax part is already exigible on the invoice)
- line is an account.move.line record being not exigible on the tax report.
* currency: The currency on which the percentage has been computed.
* total_balance: sum(payment_term_lines.mapped('balance').
* total_residual: sum(payment_term_lines.mapped('amount_residual').
* total_amount_currency: sum(payment_term_lines.mapped('amount_currency').
* total_residual_currency: sum(payment_term_lines.mapped('amount_residual_currency').
* is_fully_paid: A flag indicating the current move is now fully paid.
'''
self.ensure_one()
values = {
'move': self,
'to_process_lines': [],
'total_balance': 0.0,
'total_residual': 0.0,
'total_amount_currency': 0.0,
'total_residual_currency': 0.0,
}
currencies = set()
has_term_lines = False
for line in self.line_ids:
if line.account_type in ('asset_receivable', 'liability_payable'):
sign = 1 if line.balance > 0.0 else -1
currencies.add(line.currency_id)
has_term_lines = True
values['total_balance'] += sign * line.balance
values['total_residual'] += sign * line.amount_residual
values['total_amount_currency'] += sign * line.amount_currency
values['total_residual_currency'] += sign * line.amount_residual_currency
elif line.tax_line_id.tax_exigibility == 'on_payment':
values['to_process_lines'].append(('tax', line))
currencies.add(line.currency_id)
elif 'on_payment' in line.tax_ids.flatten_taxes_hierarchy().mapped('tax_exigibility'):
values['to_process_lines'].append(('base', line))
currencies.add(line.currency_id)
if not values['to_process_lines'] or not has_term_lines:
return None
# Compute the currency on which made the percentage.
if len(currencies) == 1:
values['currency'] = list(currencies)[0]
else:
# Don't support the case where there is multiple involved currencies.
return None
# Determine whether the move is now fully paid.
values['is_fully_paid'] = self.company_id.currency_id.is_zero(values['total_residual']) \
or values['currency'].is_zero(values['total_residual_currency'])
return values
# -------------------------------------------------------------------------
# SEQUENCE MIXIN
# -------------------------------------------------------------------------
def _must_check_constrains_date_sequence(self):
# OVERRIDES sequence.mixin
return self.state == 'posted' and not self.quick_edit_mode
def _get_last_sequence_domain(self, relaxed=False):
#pylint: disable=sql-injection
# EXTENDS account sequence.mixin
self.ensure_one()
if not self.date or not self.journal_id:
return "WHERE FALSE", {}
where_string = "WHERE journal_id = %(journal_id)s AND name != '/'"
param = {'journal_id': self.journal_id.id}
is_payment = self.origin_payment_id or self.env.context.get('is_payment')
if not relaxed:
domain = [('journal_id', '=', self.journal_id.id), ('id', '!=', self.id or self._origin.id), ('name', 'not in', ('/', '', False))]
if self.journal_id.refund_sequence:
refund_types = ('out_refund', 'in_refund')
domain += [('move_type', 'in' if self.move_type in refund_types else 'not in', refund_types)]
if self.journal_id.payment_sequence:
domain += [('origin_payment_id', '!=' if is_payment else '=', False)]
reference_move_name = self.sudo().search(domain + [('date', '<=', self.date)], order='date desc', limit=1).name
if not reference_move_name:
reference_move_name = self.sudo().search(domain, order='date asc', limit=1).name
sequence_number_reset = self._deduce_sequence_number_reset(reference_move_name)
date_start, date_end, *_ = self._get_sequence_date_range(sequence_number_reset)
where_string += """ AND date BETWEEN %(date_start)s AND %(date_end)s"""
param['date_start'] = date_start
param['date_end'] = date_end
# Some regex are catching more sequence formats than we want, so we
# need to exclude them:
#
# | Regex type |
# Move Name Format | Fixed | Yearly | Monthly | Year Range | Year range Monthly |
# ------------------ | ----- | ------ | ------- | ---------- | ------------------ |
# Fixed | X | | | | |
# Yearly | X | X | | | |
# Monthly | X | X | X | X | |
# Year Range | X | X | | X | |
# Year range Monthly | X | X | X | X | X |
if sequence_number_reset in ('year', 'year_range'):
param['anti_regex'] = self._make_regex_non_capturing(self._sequence_monthly_regex.split('(?P<seq>')[0]) + '$'
elif sequence_number_reset == 'never':
# Excluding yearly will also exclude "monthly", "year range" and
# "year range monthly"
param['anti_regex'] = self._make_regex_non_capturing(self._sequence_yearly_regex.split('(?P<seq>')[0]) + '$'
if param.get('anti_regex') and not self.journal_id.sequence_override_regex:
where_string += " AND sequence_prefix !~ %(anti_regex)s "
if self.journal_id.refund_sequence:
if self.move_type in ('out_refund', 'in_refund'):
where_string += " AND move_type IN ('out_refund', 'in_refund') "
else:
where_string += " AND move_type NOT IN ('out_refund', 'in_refund') "
elif self.journal_id.payment_sequence:
if is_payment:
where_string += " AND origin_payment_id IS NOT NULL "
else:
where_string += " AND origin_payment_id IS NULL "
return where_string, param
def _get_starting_sequence(self):
# EXTENDS account sequence.mixin
self.ensure_one()
year_part = "%04d" % self.date.year
last_day = int(self.company_id.fiscalyear_last_day)
last_month = int(self.company_id.fiscalyear_last_month)
is_staggered_year = last_month != 12 or last_day != 31
if is_staggered_year:
if self.date > date(self.date.year, last_month, last_day):
year_part = "%s-%s" % (self.date.strftime('%y'), (self.date + relativedelta(years=1)).strftime('%y'))
else:
year_part = "%s-%s" % ((self.date + relativedelta(years=-1)).strftime('%y'), self.date.strftime('%y'))
# Arbitrarily use annual sequence for sales documents, but monthly
# sequence for other documents
if self.journal_id.type in ['sale', 'bank', 'cash', 'credit']:
# We reduce short code to 4 characters (0000) in case of staggered
# year to avoid too long sequences (see Indian GST rule 46(b) for
# example). Note that it's already the case for monthly sequences.
starting_sequence = "%s/%s/%s" % (self.journal_id.code, year_part, '0000' if is_staggered_year else '00000')
else:
starting_sequence = "%s/%s/%02d/0000" % (self.journal_id.code, year_part, self.date.month)
if self.journal_id.refund_sequence and self.move_type in ('out_refund', 'in_refund'):
starting_sequence = "R" + starting_sequence
if self.journal_id.payment_sequence and self.origin_payment_id or self.env.context.get('is_payment'):
starting_sequence = "P" + starting_sequence
return starting_sequence
def _get_sequence_date_range(self, reset):
if reset not in ('year_range', 'year_range_month'):
return super()._get_sequence_date_range(reset)
fiscalyear_last_day = self.company_id.fiscalyear_last_day
fiscalyear_last_month = int(self.company_id.fiscalyear_last_month)
date_start, date_end = date_utils.get_fiscal_year(self.date, day=fiscalyear_last_day, month=fiscalyear_last_month)
if reset == 'year_range':
return (date_start, date_end) + (None, None)
forced_year_range = (date_start.year, date_end.year)
month_range = date_utils.get_month(self.date)
fiscalyear_last_month_max_day = calendar.monthrange(self.date.year, fiscalyear_last_month)[1]
# We need to truncate the month if:
# - the fiscal year does not end on the last day of the month
# - and the move date is part of that month
# The sequence date range will be something like 2020-11-01 to
# 2020-11-30. But the sequence should be 2019-2020/11/0001 (or
# 2020-2021/11/0001), not 2020-2020/11/0001.
if fiscalyear_last_day < fiscalyear_last_month_max_day and fiscalyear_last_month == self.date.month:
if self.date.day <= fiscalyear_last_day:
return (month_range[0], month_range[1].replace(day=fiscalyear_last_day)) + forced_year_range
else:
return (month_range[0].replace(day=fiscalyear_last_day + 1), month_range[1]) + forced_year_range
else:
return month_range + forced_year_range
# -------------------------------------------------------------------------
# PAYMENT REFERENCE
# -------------------------------------------------------------------------
def _get_invoice_reference_euro_invoice(self):
""" This computes the reference based on the RF Creditor Reference.
The data of the reference is the database id number of the invoice.
For instance, if an invoice is issued with id 43, the check number
is 07 so the reference will be 'RF07 43'.
"""
self.ensure_one()
return format_structured_reference_iso(self.id)
def _get_invoice_reference_euro_partner(self):
""" This computes the reference based on the RF Creditor Reference.
The data of the reference is the user defined reference of the
partner or the database id number of the parter.
For instance, if an invoice is issued for the partner with internal
reference 'food buyer 654', the digits will be extracted and used as
the data. This will lead to a check number equal to 00 and the
reference will be 'RF00 654'.
If no reference is set for the partner, its id in the database will
be used.
"""
self.ensure_one()
partner_ref = self.partner_id.ref
partner_ref_nr = re.sub(r'\D', '', partner_ref or '')[-21:] or str(self.partner_id.id)[-21:]
partner_ref_nr = partner_ref_nr[-21:]
return format_structured_reference_iso(partner_ref_nr)
def _get_invoice_reference_odoo_invoice(self):
""" This computes the reference based on the Odoo format.
We simply return the number of the invoice, defined on the journal
sequence.
"""
self.ensure_one()
return self.name
def _get_invoice_reference_odoo_partner(self):
""" This computes the reference based on the Odoo format.
The data used is the reference set on the partner or its database
id otherwise. For instance if the reference of the customer is
'dumb customer 97', the reference will be 'CUST/dumb customer 97'.
"""
ref = self.partner_id.ref or str(self.partner_id.id)
prefix = _('CUST')
return '%s/%s' % (prefix, ref)
def _get_invoice_computed_reference(self):
self.ensure_one()
if self.journal_id.invoice_reference_type == 'none':
return ''
ref_function = getattr(self, f'_get_invoice_reference_{self.journal_id.invoice_reference_model}_{self.journal_id.invoice_reference_type}', None)
if ref_function is None:
raise UserError(_("The combination of reference model and reference type on the journal is not implemented"))
return ref_function()
# -------------------------------------------------------------------------
# QUICK ENCODING
# -------------------------------------------------------------------------
@api.model
def _get_frequent_account_and_taxes(self, company_id, partner_id, move_type):
"""
Returns the most used accounts and taxes for a given partner and company,
eventually filtered according to the move type.
"""
if not partner_id:
return 0, False, False
domain = [
*self.env['account.move.line']._check_company_domain(company_id),
('partner_id', '=', partner_id),
('account_id.deprecated', '=', False),
('date', '>=', date.today() - timedelta(days=365 * 2)),
]
if move_type in self.env['account.move'].get_inbound_types(include_receipts=True):
domain.append(('account_id.internal_group', '=', 'income'))
elif move_type in self.env['account.move'].get_outbound_types(include_receipts=True):
domain.append(('account_id.internal_group', '=', 'expense'))
query = self.env['account.move.line']._where_calc(domain)
account_code = self.env['account.account']._field_to_sql('account_move_line__account_id', 'code', query)
rows = self.env.execute_query(SQL("""
SELECT COUNT(foo.id), foo.account_id, foo.taxes
FROM (
SELECT account_move_line__account_id.id AS account_id,
%(account_code)s AS code,
account_move_line.id,
ARRAY_AGG(tax_rel.account_tax_id) FILTER (WHERE tax_rel.account_tax_id IS NOT NULL) AS taxes
FROM %(from_clause)s
LEFT JOIN account_move_line_account_tax_rel tax_rel ON account_move_line.id = tax_rel.account_move_line_id
WHERE %(where_clause)s
GROUP BY account_move_line__account_id.id,
%(account_code)s,
account_move_line.id
) AS foo
GROUP BY foo.account_id, foo.taxes
ORDER BY COUNT(foo.id) DESC, taxes ASC NULLS LAST
LIMIT 1
""",
account_code=account_code,
from_clause=query.from_clause,
where_clause=query.where_clause or SQL("TRUE"),
))
return rows[0] if rows else (0, False, False)
def _get_quick_edit_suggestions(self):
"""
Returns a dictionnary containing the suggested values when creating a new
line with the quick_edit_total_amount set. We will compute the price_unit
that has to be set with the correct that in order to match this total amount.
If the vendor/customer is set, we will suggest the most frequently used account
for that partner as the default one, otherwise the default of the journal.
"""
self.ensure_one()
if not self.quick_edit_mode or not self.quick_edit_total_amount:
return False
count, account_id, tax_ids = self._get_frequent_account_and_taxes(
self.company_id.id,
self.partner_id.id,
self.move_type,
)
if count:
taxes = self.env['account.tax'].browse(tax_ids)
else:
account_id = self.journal_id.default_account_id.id
if self.is_sale_document(include_receipts=True):
taxes = self.journal_id.default_account_id.tax_ids.filtered(lambda tax: tax.type_tax_use == 'sale')
else:
taxes = self.journal_id.default_account_id.tax_ids.filtered(lambda tax: tax.type_tax_use == 'purchase')
if not taxes:
taxes = (
self.journal_id.company_id.account_sale_tax_id
if self.journal_id.type == 'sale' else
self.journal_id.company_id.account_purchase_tax_id
)
taxes = self.fiscal_position_id.map_tax(taxes)
# When a payment term has an early payment discount with the epd computation set to 'mixed', recomputing
# the untaxed amount should take in consideration the discount percentage otherwise we'd get a wrong value.
# We check that we have only one percentage tax as computing from multiple taxes with different types can get complicated.
# In one example: let's say: base = 100, discount = 2%, tax = 21%
# the total will be calculated as: total = base + (base * (1 - discount)) * tax
# If we manipulate the equation to get the base from the total, we'll have base = total / ((1 - discount) * tax + 1)
term = self.invoice_payment_term_id
discount_percentage = term.discount_percentage if term.early_discount else 0
remaining_amount = self.quick_edit_total_amount - self.tax_totals['total_amount_currency']
if (
discount_percentage
and term.early_pay_discount_computation == 'mixed'
and len(taxes) == 1
and taxes.amount_type == 'percent'
):
price_untaxed = self.currency_id.round(
remaining_amount / (((1.0 - discount_percentage / 100.0) * (taxes.amount / 100.0)) + 1.0))
else:
price_untaxed = taxes.with_context(force_price_include=True).compute_all(remaining_amount)['total_excluded']
return {'account_id': account_id, 'tax_ids': taxes.ids, 'price_unit': price_untaxed}
@api.onchange('quick_edit_mode', 'journal_id', 'company_id')
def _quick_edit_mode_suggest_invoice_date(self):
"""Suggest the Customer Invoice/Vendor Bill date based on previous invoice and lock dates"""
for record in self:
if record.quick_edit_mode and not record.invoice_date:
invoice_date = fields.Date.context_today(self)
prev_move = self.search([('state', '=', 'posted'),
('journal_id', '=', record.journal_id.id),
('company_id', '=', record.company_id.id),
('invoice_date', '!=', False)],
limit=1)
if prev_move:
invoice_date = self._get_accounting_date(prev_move.invoice_date, False)
record.invoice_date = invoice_date
@api.onchange('quick_edit_total_amount', 'partner_id')
def _onchange_quick_edit_total_amount(self):
"""
Creates a new line with the suggested values (for the account, the price_unit,
and the tax) such that the total amount matches the quick total amount.
"""
if (
not self.quick_edit_total_amount
or not self.quick_edit_mode
or len(self.invoice_line_ids) > 0
):
return
suggestions = self.quick_encoding_vals
self.invoice_line_ids = [Command.clear()]
self.invoice_line_ids += self.env['account.move.line'].new({
'partner_id': self.partner_id,
'account_id': suggestions['account_id'],
'currency_id': self.currency_id.id,
'price_unit': suggestions['price_unit'],
'tax_ids': [Command.set(suggestions['tax_ids'])],
})
self._check_total_amount(self.quick_edit_total_amount)
@api.onchange('invoice_line_ids')
def _onchange_quick_edit_line_ids(self):
quick_encode_suggestion = self.env.context.get('quick_encoding_vals')
if (
not self.quick_edit_total_amount
or not self.quick_edit_mode
or not self.invoice_line_ids
or not quick_encode_suggestion
or not quick_encode_suggestion['price_unit'] == self.invoice_line_ids[-1].price_unit
):
return
self._check_total_amount(self.quick_edit_total_amount)
def _check_total_amount(self, amount_total):
"""
Verifies that the total amount corresponds to the quick total amount chosen as some
rounding errors may appear. In such a case, we round up the tax such that the total
is equal to the quick total amount set
E.g.: 100€ including 21% tax: base = 82.64, tax = 17.35, total = 99.99
The tax will be set to 17.36 in order to have a total of 100.00
"""
if not self.tax_totals or not amount_total:
return
totals = self.tax_totals
tax_amount_rounding_error = amount_total - totals['total_amount_currency']
if not float_is_zero(tax_amount_rounding_error, precision_rounding=self.currency_id.rounding):
for subtotal in totals['subtotals']:
if _('Untaxed Amount') == subtotal['name']:
if subtotal['tax_groups']:
subtotal['tax_groups'][0]['tax_amount_currency'] += tax_amount_rounding_error
totals['total_amount_currency'] = amount_total
self.tax_totals = totals
break
# -------------------------------------------------------------------------
# HASH
# -------------------------------------------------------------------------
def _get_integrity_hash_fields(self):
# Use the latest hash version by default, but keep the old one for backward compatibility when generating the integrity report.
hash_version = self._context.get('hash_version', MAX_HASH_VERSION)
if hash_version == 1:
return ['date', 'journal_id', 'company_id']
elif hash_version in (2, 3, 4):
return ['name', 'date', 'journal_id', 'company_id']
raise NotImplementedError(f"hash_version={hash_version} doesn't exist")
def _get_integrity_hash_fields_and_subfields(self):
return self._get_integrity_hash_fields() + [f'line_ids.{subfield}' for subfield in self.line_ids._get_integrity_hash_fields()]
@api.model
def _get_move_hash_domain(self, common_domain=False, force_hash=False):
"""
Returns a search domain on model account.move checking whether they should be hashed.
:param common_domain: a search domain that will be included in the returned domain in any case
:param force_hash: if True, we'll check all moves posted, independently of journal settings
"""
common_domain = expression.AND([
common_domain or [],
[('state', '=', 'posted')],
])
if force_hash:
return common_domain
return expression.AND([
common_domain,
[('restrict_mode_hash_table', '=', True)],
])
@api.model
def _is_move_restricted(self, move, force_hash=False):
"""
Returns whether a move should be hashed (depending on journal settings)
:param move: the account.move we check
:param force_hash: if True, we'll check all moves posted, independently of journal settings
"""
return move.filtered_domain(self._get_move_hash_domain(force_hash=force_hash))
def _hash_moves(self, **kwargs):
chains_to_hash = self._get_chains_to_hash(**kwargs)
for chain in chains_to_hash:
move_hashes = chain['moves']._calculate_hashes(chain['previous_hash'])
for move, move_hash in move_hashes.items():
move.inalterable_hash = move_hash
chain['moves']._message_log_batch(bodies={m.id: self.env._("This journal entry has been secured.") for m in chain['moves']})
def _get_chain_info(self, force_hash=False, include_pre_last_hash=False, early_stop=False):
"""All records in `self` must belong to the same journal and sequence_prefix
"""
if not self:
return False
last_move_in_chain = max(self, key=lambda m: m.sequence_number)
journal = last_move_in_chain.journal_id
if not self._is_move_restricted(last_move_in_chain, force_hash=force_hash):
return False
common_domain = [
('journal_id', '=', journal.id),
('sequence_prefix', '=', last_move_in_chain.sequence_prefix),
]
last_move_hashed = self.env['account.move'].search([
*common_domain,
('inalterable_hash', '!=', False),
], order='sequence_number desc', limit=1)
domain = self.env['account.move']._get_move_hash_domain([
*common_domain,
('sequence_number', '<=', last_move_in_chain.sequence_number),
('inalterable_hash', '=', False),
], force_hash=True)
if last_move_hashed and not include_pre_last_hash:
# Hash moves only after the last hashed move, not the ones that may have been posted before the journal was set on restrict mode
domain.extend([('sequence_number', '>', last_move_hashed.sequence_number)])
# On the accounting dashboard, we are only interested on whether there are documents to hash or not
# so we can stop the computation early if we find at least one document to hash
if early_stop:
return self.env['account.move'].sudo().search_count(domain, limit=1)
moves_to_hash = self.env['account.move'].sudo().search(domain, order='sequence_number')
warnings = set()
if moves_to_hash:
# gap warning
if last_move_hashed:
first = last_move_hashed.sequence_number
difference = len(moves_to_hash)
else:
first = moves_to_hash[0].sequence_number
difference = len(moves_to_hash) - 1
last = moves_to_hash[-1].sequence_number
if first + difference != last:
warnings.add('gap')
# unreconciled warning
unreconciled = False in moves_to_hash.statement_line_ids.mapped('is_reconciled')
if unreconciled:
warnings.add('unreconciled')
else:
warnings.add('no_document')
moves = moves_to_hash.sudo(False)
return {
'previous_hash': last_move_hashed.inalterable_hash,
'last_move_hashed': last_move_hashed,
'moves': moves,
'remaining_moves': self - moves,
'warnings': warnings,
}
def _get_chains_to_hash(self, force_hash=False, raise_if_gap=True, raise_if_no_document=True, include_pre_last_hash=False, early_stop=False):
"""
From a recordset of moves, retrieve the chains of moves that need to be hashed by taking
into account the last move of each chain of the recordset.
So if we have INV/1, INV/2, INV/3, INV4 that are not hashed yet in the database
but self contains INV/2, INV/3, we will return INV/1, INV/2 and INV/3. Not INV/4.
:param force_hash: if True, we'll check all moves posted, independently of journal settings
:param raise_if_gap: if True, we'll raise an error if a gap is detected in the sequence
:param raise_if_no_document: if True, we'll raise an error if no document needs to be hashed
:param include_pre_last_hash: if True, we'll include the moves not hashed that are previous to the last hashed move
:param early_stop: if True, we'll stop the computation as soon as we find at least one document to hash
:return bool when early_stop else a list of dictionaries (each dict generated by `_get_chain_info`)
"""
res = []
for journal, journal_moves in self.grouped('journal_id').items():
for chain_moves in journal_moves.grouped('sequence_prefix').values():
chain_info = chain_moves._get_chain_info(
force_hash=force_hash, include_pre_last_hash=include_pre_last_hash, early_stop=early_stop
)
if chain_info is False:
continue
if early_stop and chain_info:
return True
if 'unreconciled' in chain_info['warnings']:
raise UserError(_("An error occurred when computing the inalterability. All entries have to be reconciled."))
if raise_if_no_document and 'no_document' in chain_info['warnings']:
raise UserError(_(
"This move could not be locked either because "
"some move with the same sequence prefix has a higher number. You may need to resequence it."
))
if raise_if_gap and 'gap' in chain_info['warnings']:
raise UserError(_(
"An error occurred when computing the inalterability. A gap has been detected in the sequence."
))
res.append(chain_info)
if early_stop:
return False
return res
def _calculate_hashes(self, previous_hash):
"""
:return: dict of move_id: hash
"""
hash_version = self._context.get('hash_version', MAX_HASH_VERSION)
def _getattrstring(obj, field_name):
field_value = obj[field_name]
if obj._fields[field_name].type == 'many2one':
field_value = field_value.id
if obj._fields[field_name].type == 'monetary' and hash_version >= 3:
return float_repr(field_value, obj.currency_id.decimal_places)
return str(field_value)
move2hash = {}
previous_hash = previous_hash or ''
for move in self:
if previous_hash and previous_hash.startswith("$"):
previous_hash = previous_hash.split("$")[2] # The hash version is not used for the computation of the next hash
values = {}
for fname in move._get_integrity_hash_fields():
values[fname] = _getattrstring(move, fname)
for line in move.line_ids:
for fname in line._get_integrity_hash_fields():
k = 'line_%d_%s' % (line.id, fname)
values[k] = _getattrstring(line, fname)
current_record = dumps(values, sort_keys=True, ensure_ascii=True, indent=None, separators=(',', ':'))
hash_string = sha256((previous_hash + current_record).encode('utf-8')).hexdigest()
move2hash[move] = f"${hash_version}${hash_string}" if hash_version >= 4 else hash_string
previous_hash = move2hash[move]
return move2hash
# -------------------------------------------------------------------------
# RECURRING ENTRIES
# -------------------------------------------------------------------------
@api.model
def _apply_delta_recurring_entries(self, date, date_origin, period):
'''Advances date by `period` months, maintaining original day of the month if possible.'''
deltas = {'monthly': 1, 'quarterly': 3, 'yearly': 12}
prev_months = (date.year - date_origin.year) * 12 + date.month - date_origin.month
return date_origin + relativedelta(months=deltas[period] + prev_months)
def _copy_recurring_entries(self):
''' Creates a copy of a recurring (periodic) entry and adjusts its dates for the next period.
Meant to be called right after posting a periodic entry.
Copies extra fields as defined by _get_fields_to_copy_recurring_entries().
'''
for record in self:
record.auto_post_origin_id = record.auto_post_origin_id or record # original entry references itself
next_date = self._apply_delta_recurring_entries(record.date, record.auto_post_origin_id.date, record.auto_post)
if not record.auto_post_until or next_date <= record.auto_post_until: # recurrence continues
record.copy(default=record._get_fields_to_copy_recurring_entries({'date': next_date}))
def _get_fields_to_copy_recurring_entries(self, values):
''' Determines which extra fields to copy when copying a recurring entry.
To be extended by modules that add fields with copy=False (implicit or explicit)
whenever the opposite behavior is expected for recurring invoices.
'''
values.update({
'auto_post': self.auto_post, # copy=False to avoid mistakes but should be the same in recurring copies
'auto_post_until': self.auto_post_until, # same as above
'auto_post_origin_id': self.auto_post_origin_id.id, # same as above
'invoice_user_id': self.invoice_user_id.id, # otherwise user would be OdooBot
})
if self.invoice_date:
values.update({'invoice_date': self._apply_delta_recurring_entries(self.invoice_date, self.auto_post_origin_id.invoice_date, self.auto_post)})
if not self.invoice_payment_term_id and self.invoice_date_due:
# no payment terms: maintain timedelta between due date and accounting date
values.update({'invoice_date_due': values['date'] + (self.invoice_date_due - self.date)})
return values
# -------------------------------------------------------------------------
# EDI
# -------------------------------------------------------------------------
@contextmanager
def _get_edi_creation(self):
"""Get an environment to import documents from other sources.
Allow to edit the current move or create a new one.
This will prevent computing the dynamic lines at each invoice line added and only
compute everything at the end.
"""
container = {'records': self}
with self._check_balanced(container),\
self._disable_discount_precision(),\
self._sync_dynamic_lines(container):
move = self or self.create({})
yield move
container['records'] = move
@contextmanager
def _disable_discount_precision(self):
"""Disable the user defined precision for discounts.
This is useful for importing documents coming from other softwares and providers.
The reasonning is that if the document that we are importing has a discount, it
shouldn't be rounded to the local settings.
"""
with self._disable_recursion({'records': self}, 'ignore_discount_precision'):
yield
def _get_edi_decoder(self, file_data, new=False):
"""To be extended with decoding capabilities.
:returns: Function to be later used to import the file.
Function' args:
- invoice: account.move
- file_data: attachemnt information / value
- new: whether the invoice is newly created
returns True if was able to process the invoice
"""
return None
def _extend_with_attachments(self, attachments, new=False):
"""Main entry point to extend/enhance invoices with attachments.
Either coming from:
- The chatter when the user drops an attachment on an existing invoice.
- The journal when the user drops one or multiple attachments from the dashboard.
- The server mail alias when an alias is configured on the journal.
It will unwrap all attachments by priority then try to decode until it succeed.
:param attachments: A recordset of ir.attachment.
:param new: Indicate if the current invoice is a fresh one or an existing one.
:returns: True if at least one document is successfully imported
"""
def close_file(file_data):
if file_data.get('on_close'):
file_data['on_close']()
def add_file_data_results(file_data, invoice):
passed_file_data_list.append(file_data)
attachment = file_data.get('attachment') or file_data.get('originator_pdf')
if attachment:
if attachments_by_invoice.get(attachment):
attachments_by_invoice[attachment] |= invoice
else:
attachments_by_invoice[attachment] = invoice
file_data_list = attachments._unwrap_edi_attachments()
attachments_by_invoice = {}
invoices = self
current_invoice = self
passed_file_data_list = []
for file_data in file_data_list:
# Rogue binaries from mail alias are skipped and unlinked.
if (
file_data['type'] == 'binary'
and self._context.get('from_alias')
and not attachments_by_invoice.get(file_data['attachment'])
and file_data['attachment'].mimetype not in ALLOWED_MIMETYPES
):
close_file(file_data)
continue
# The invoice has already been decoded by an embedded file.
if attachments_by_invoice.get(file_data['attachment']):
add_file_data_results(file_data, attachments_by_invoice[file_data['attachment']])
close_file(file_data)
continue
# When receiving multiple files, if they have a different type, we supposed they are all linked
# to the same invoice.
if (
passed_file_data_list
and passed_file_data_list[-1]['filename'] != file_data['filename']
and passed_file_data_list[-1]['sort_weight'] != file_data['sort_weight']
):
add_file_data_results(file_data, invoices[-1])
close_file(file_data)
continue
if passed_file_data_list and not new:
add_file_data_results(file_data, invoices[-1])
close_file(file_data)
continue
extend_with_existing_lines = file_data.get('process_if_existing_lines', False)
if current_invoice.invoice_line_ids and not extend_with_existing_lines:
continue
decoder = (current_invoice or current_invoice.new(self.default_get(['move_type', 'journal_id'])))._get_edi_decoder(file_data, new=new)
if decoder or file_data['type'] in ('pdf', 'binary'):
try:
with self.env.cr.savepoint():
invoice = current_invoice or self.create({})
existing_lines = invoice.invoice_line_ids
if not decoder and file_data['type'] in ('pdf', 'binary'):
success = False
else:
success = decoder(invoice, file_data, new)
if success or file_data['type'] == 'pdf' or file_data['attachment'].mimetype in ALLOWED_MIMETYPES:
(invoice.invoice_line_ids - existing_lines).is_imported = True
invoice._link_bill_origin_to_purchase_orders(timeout=4)
invoices |= invoice
current_invoice = self.env['account.move']
add_file_data_results(file_data, invoice)
except RedirectWarning:
raise
except Exception:
message = _(
"Error importing attachment '%(file_name)s' as invoice (decoder=%(decoder)s)",
file_name=file_data['filename'],
decoder=decoder.__name__,
)
current_invoice.sudo().message_post(body=message)
_logger.exception(message)
passed_file_data_list.append(file_data)
close_file(file_data)
return attachments_by_invoice
# -------------------------------------------------------------------------
# BUSINESS METHODS
# -------------------------------------------------------------------------
def _prepare_invoice_aggregated_taxes(
self,
filter_invl_to_apply=None,
filter_tax_values_to_apply=None,
grouping_key_generator=None,
round_from_tax_lines=None,
postfix_function=None,
):
""" This method is deprecated and will be removed in the next version.
Use the following pattern instead:
base_amls = self.line_ids.filtered(lambda x: x.display_type == 'product')
base_lines = [self._prepare_product_base_line_for_taxes_computation(x) for x in base_amls]
tax_amls = self.line_ids.filtered(lambda x: x.display_type == 'tax')
tax_lines = [self._prepare_tax_line_for_taxes_computation(x) for x in tax_amls]
AccountTax._add_tax_details_in_base_lines(base_lines, self.company_id)
AccountTax._round_base_lines_tax_details(base_lines, self.company_id, tax_lines=tax_lines)
def grouping_function(base_line, tax_data):
...
base_lines_aggregated_values = self._aggregate_base_lines_tax_details(base_lines, grouping_function)
values_per_grouping_key = self._aggregate_base_lines_aggregated_values(base_lines_aggregated_values)
"""
self.ensure_one()
AccountTax = self.env['account.tax']
if round_from_tax_lines is None:
round_from_tax_lines = filter_tax_values_to_apply or filter_invl_to_apply
base_amls = self.line_ids.filtered(lambda x: x.display_type == 'product' and (not filter_invl_to_apply or filter_invl_to_apply(x)))
base_lines = [self._prepare_product_base_line_for_taxes_computation(x) for x in base_amls]
tax_amls = self.line_ids.filtered(lambda x: x.display_type == 'tax')
if round_from_tax_lines:
tax_lines = [self._prepare_tax_line_for_taxes_computation(x) for x in tax_amls]
else:
tax_lines = []
AccountTax._add_tax_details_in_base_lines(base_lines, self.company_id)
if postfix_function:
postfix_function(base_lines)
AccountTax._round_base_lines_tax_details(base_lines, self.company_id, tax_lines=tax_lines)
# Retro-compatibility with previous aggregator.
results = {
'base_amount_currency': 0.0,
'base_amount': 0.0,
'tax_amount_currency': 0.0,
'tax_amount': 0.0,
'tax_details_per_record': defaultdict(lambda: {
'base_amount_currency': 0.0,
'base_amount': 0.0,
'tax_amount_currency': 0.0,
'tax_amount': 0.0,
}),
'base_lines': base_lines,
}
def total_grouping_function(base_line, tax_data):
return not filter_tax_values_to_apply or filter_tax_values_to_apply(base_line, tax_data)
# Report the total amounts.
base_lines_aggregated_values = AccountTax._aggregate_base_lines_tax_details(base_lines, total_grouping_function)
for base_line, aggregated_values in base_lines_aggregated_values:
record = base_line['record']
base_line_results = results['tax_details_per_record'][record]
base_line_results['base_line'] = base_line
for grouping_key, values in aggregated_values.items():
if grouping_key:
for key in ('base_amount', 'base_amount_currency', 'tax_amount', 'tax_amount_currency'):
base_line_results[key] += values[key]
values_per_grouping_key = AccountTax._aggregate_base_lines_aggregated_values(base_lines_aggregated_values)
for grouping_key, values in values_per_grouping_key.items():
if grouping_key:
for key in ('base_amount', 'base_amount_currency', 'tax_amount', 'tax_amount_currency'):
results[key] += values[key]
# Same with the custom grouping_key passed as parameter.
def tax_details_grouping_function(base_line, tax_data):
if not total_grouping_function(base_line, tax_data):
return None
if grouping_key_generator:
grouping_key = grouping_key_generator(base_line, tax_data)
assert grouping_key is not None # None must be kept for inner-grouping.
return grouping_key
return tax_data['tax']
base_lines_aggregated_values = AccountTax._aggregate_base_lines_tax_details(base_lines, tax_details_grouping_function)
for base_line, aggregated_values in base_lines_aggregated_values:
record = base_line['record']
base_line_results = results['tax_details_per_record'][record]
base_line_results['tax_details'] = tax_details = {}
for grouping_key, values in aggregated_values.items():
if not grouping_key:
continue
if isinstance(grouping_key, dict):
values.update(grouping_key)
tax_details[grouping_key] = values
values_per_grouping_key = AccountTax._aggregate_base_lines_aggregated_values(base_lines_aggregated_values)
results['tax_details'] = tax_details = {}
for grouping_key, values in values_per_grouping_key.items():
if not grouping_key:
continue
if isinstance(grouping_key, dict):
values.update(grouping_key)
tax_details[grouping_key] = values
return results
def _get_invoice_counterpart_amls_for_early_payment_discount_per_payment_term_line(self):
""" Helper to get the values to create the counterpart journal items on the register payment wizard and the
bank reconciliation widget in case of an early payment discount. When the early payment discount computation
is included, we need to compute the base amounts / tax amounts for each receivable / payable but we need to
take care about the rounding issues. For others computations, we need to balance the discount you get.
:return: A list of values to create the counterpart journal items split in 3 categories:
* term_lines: The journal items containing the discount amounts for each receivable line when the
discount computation is excluded / mixed.
* tax_lines: The journal items acting as tax lines when the discount computation is included.
* base_lines: The journal items acting as base for tax lines when the discount computation is included.
"""
self.ensure_one()
def inverse_tax_rep(tax_rep):
tax = tax_rep.tax_id
index = list(tax.invoice_repartition_line_ids).index(tax_rep)
return tax.refund_repartition_line_ids[index]
company = self.company_id
payment_term_line = self.line_ids.filtered(lambda x: x.display_type == 'payment_term')
tax_lines = self.line_ids.filtered(lambda x: x.display_type == 'tax')
invoice_lines = self.line_ids.filtered(lambda x: x.display_type == 'product')
payment_term = self.invoice_payment_term_id
early_pay_discount_computation = payment_term.early_pay_discount_computation
discount_percentage = payment_term.discount_percentage
res = {
'term_lines': defaultdict(lambda: {}),
'tax_lines': defaultdict(lambda: {}),
'base_lines': defaultdict(lambda: {}),
}
if not discount_percentage:
return res
# Get the current tax amounts in the current invoice.
tax_amounts = {
inverse_tax_rep(line.tax_repartition_line_id).id: {
'amount_currency': line.amount_currency,
'balance': line.balance,
}
for line in tax_lines
}
base_lines = [
{
**self._prepare_product_base_line_for_taxes_computation(line),
'is_refund': True,
}
for line in invoice_lines
]
for base_line in base_lines:
base_line['tax_ids'] = base_line['tax_ids'].filtered(lambda t: t.amount_type != 'fixed')
if early_pay_discount_computation == 'included':
remaining_part_to_consider = (100 - discount_percentage) / 100.0
base_line['price_unit'] *= remaining_part_to_consider
AccountTax = self.env['account.tax']
AccountTax._add_tax_details_in_base_lines(base_lines, self.company_id)
AccountTax._round_base_lines_tax_details(base_lines, self.company_id)
AccountTax._add_accounting_data_in_base_lines_tax_details(base_lines, self.company_id)
if self.is_inbound(include_receipts=True):
cash_discount_account = company.account_journal_early_pay_discount_loss_account_id
else:
cash_discount_account = company.account_journal_early_pay_discount_gain_account_id
bases_details = {}
term_amount_currency = payment_term_line.amount_currency - payment_term_line.discount_amount_currency
term_balance = payment_term_line.balance - payment_term_line.discount_balance
if early_pay_discount_computation == 'included' and invoice_lines.tax_ids:
# Compute the base amounts.
resulting_delta_base_details = {}
resulting_delta_tax_details = {}
for base_line in base_lines:
tax_details = base_line['tax_details']
invoice_line = base_line['record']
grouping_dict = {
'tax_ids': [Command.set(base_line['tax_ids'].ids)],
'tax_tag_ids': [Command.set(base_line['tax_tag_ids'].ids)],
'partner_id': base_line['partner_id'].id,
'currency_id': base_line['currency_id'].id,
'account_id': cash_discount_account.id,
'analytic_distribution': base_line['analytic_distribution'],
}
base_detail = resulting_delta_base_details.setdefault(frozendict(grouping_dict), {
'balance': 0.0,
'amount_currency': 0.0,
})
amount_currency = self.currency_id\
.round(self.direction_sign * tax_details['total_excluded_currency'] - invoice_line.amount_currency)
balance = self.company_currency_id\
.round(self.direction_sign * tax_details['total_excluded'] - invoice_line.balance)
base_detail['balance'] += balance
base_detail['amount_currency'] += amount_currency
bases_details[frozendict(grouping_dict)] = base_detail
# Compute the tax amounts.
tax_results = AccountTax._prepare_tax_lines(base_lines, self.company_id)
for tax_line_vals in tax_results['tax_lines_to_add']:
tax_amount_without_epd = tax_amounts.get(tax_line_vals['tax_repartition_line_id'])
if tax_amount_without_epd:
resulting_delta_tax_details[tax_line_vals['tax_repartition_line_id']] = {
**tax_line_vals,
'amount_currency': tax_line_vals['amount_currency'] - tax_amount_without_epd['amount_currency'],
'balance': tax_line_vals['balance'] - tax_amount_without_epd['balance'],
}
# Multiply the amount by the percentage
percentage_paid = abs(payment_term_line.amount_residual_currency / self.amount_total)
for tax_line_vals in resulting_delta_tax_details.values():
tax_rep = self.env['account.tax.repartition.line'].browse(tax_line_vals['tax_repartition_line_id'])
tax = tax_rep.tax_id
grouping_dict = {
'account_id': tax_line_vals['account_id'],
'partner_id': tax_line_vals['partner_id'],
'currency_id': tax_line_vals['currency_id'],
'analytic_distribution': tax_line_vals['analytic_distribution'],
'tax_repartition_line_id': tax_rep.id,
'tax_ids': tax_line_vals['tax_ids'],
'tax_tag_ids': tax_line_vals['tax_tag_ids'],
'group_tax_id': tax_line_vals['group_tax_id'],
}
res['tax_lines'][payment_term_line][frozendict(grouping_dict)] = {
'name': _("Early Payment Discount (%s)", tax.name),
'amount_currency': payment_term_line.currency_id.round(tax_line_vals['amount_currency'] * percentage_paid),
'balance': payment_term_line.company_currency_id.round(tax_line_vals['balance'] * percentage_paid),
}
for grouping_dict, base_detail in bases_details.items():
res['base_lines'][payment_term_line][grouping_dict] = {
'name': _("Early Payment Discount"),
'amount_currency': payment_term_line.currency_id.round(base_detail['amount_currency'] * percentage_paid),
'balance': payment_term_line.company_currency_id.round(base_detail['balance'] * percentage_paid),
}
# Fix the rounding issue if any.
delta_amount_currency = term_amount_currency \
- sum(x['amount_currency'] for x in res['base_lines'][payment_term_line].values()) \
- sum(x['amount_currency'] for x in res['tax_lines'][payment_term_line].values())
delta_balance = term_balance \
- sum(x['balance'] for x in res['base_lines'][payment_term_line].values()) \
- sum(x['balance'] for x in res['tax_lines'][payment_term_line].values())
biggest_base_line = max(list(res['base_lines'][payment_term_line].values()), key=lambda x: x['amount_currency'])
biggest_base_line['amount_currency'] += delta_amount_currency
biggest_base_line['balance'] += delta_balance
else:
grouping_dict = {'account_id': cash_discount_account.id}
res['term_lines'][payment_term_line][frozendict(grouping_dict)] = {
'name': _("Early Payment Discount"),
'partner_id': payment_term_line.partner_id.id,
'currency_id': payment_term_line.currency_id.id,
'amount_currency': term_amount_currency,
'balance': term_balance,
}
return res
@api.model
def _get_invoice_counterpart_amls_for_early_payment_discount(self, aml_values_list, open_balance):
""" Helper to get the values to create the counterpart journal items on the register payment wizard and the
bank reconciliation widget in case of an early payment discount by taking care of the payment term lines we
are matching and the exchange difference in case of multi-currencies.
:param aml_values_list: A list of dictionaries containing:
* aml: The payment term line we match.
* amount_currency: The matched amount_currency for this line.
* balance: The matched balance for this line (could be different in case of multi-currencies).
:param open_balance: The current open balance to be covered by the early payment discount.
:return: A list of values to create the counterpart journal items split in 3 categories:
* term_lines: The journal items containing the discount amounts for each receivable line when the
discount computation is excluded / mixed.
* tax_lines: The journal items acting as tax lines when the discount computation is included.
* base_lines: The journal items acting as base for tax lines when the discount computation is included.
* exchange_lines: The journal items representing the exchange differences in case of multi-currencies.
"""
res = {
'base_lines': {},
'tax_lines': {},
'term_lines': {},
'exchange_lines': {},
}
res_per_invoice = {}
for aml_values in aml_values_list:
aml = aml_values['aml']
invoice = aml.move_id
if invoice not in res_per_invoice:
res_per_invoice[invoice] = invoice._get_invoice_counterpart_amls_for_early_payment_discount_per_payment_term_line()
for key in ('base_lines', 'tax_lines', 'term_lines'):
for grouping_dict, vals in res_per_invoice[invoice][key][aml].items():
line_vals = res[key].setdefault(grouping_dict, {
**vals,
'amount_currency': 0.0,
'balance': 0.0,
'display_type': 'epd', # Used to compute tax_tag_invert for early payment discount lines
})
line_vals['amount_currency'] += vals['amount_currency']
line_vals['balance'] += vals['balance']
# Track the balance to handle the exchange difference.
open_balance -= vals['balance']
exchange_diff_sign = aml.company_currency_id.compare_amounts(open_balance, 0.0)
if exchange_diff_sign != 0.0:
if exchange_diff_sign > 0.0:
exchange_line_account = aml.company_id.expense_currency_exchange_account_id
else:
exchange_line_account = aml.company_id.income_currency_exchange_account_id
grouping_dict = {
'account_id': exchange_line_account.id,
'currency_id': aml.currency_id.id,
'partner_id': aml.partner_id.id,
}
line_vals = res['exchange_lines'].setdefault(frozendict(grouping_dict), {
**grouping_dict,
'name': _("Early Payment Discount (Exchange Difference)"),
'amount_currency': 0.0,
'balance': 0.0,
})
line_vals['balance'] += open_balance
return {
key: [
{
**grouping_dict,
**vals,
}
for grouping_dict, vals in mapping.items()
]
for key, mapping in res.items()
}
def _affect_tax_report(self):
return any(line._affect_tax_report() for line in (self.line_ids | self.invoice_line_ids))
def _get_move_display_name(self, show_ref=False):
''' Helper to get the display name of an invoice depending of its type.
:param show_ref: A flag indicating of the display name must include or not the journal entry reference.
:return: A string representing the invoice.
'''
self.ensure_one()
if self.env.context.get('name_as_amount_total'):
currency_amount = self.currency_id.format(self.amount_total)
if self.state == 'posted':
return _("%(ref)s (%(currency_amount)s)", ref=(self.ref or self.name), currency_amount=currency_amount)
else:
return _("Draft (%(currency_amount)s)", currency_amount=currency_amount)
name = ''
if self.state == 'draft':
name += {
'out_invoice': _('Draft Invoice'),
'out_refund': _('Draft Credit Note'),
'in_invoice': _('Draft Bill'),
'in_refund': _('Draft Vendor Credit Note'),
'out_receipt': _('Draft Sales Receipt'),
'in_receipt': _('Draft Purchase Receipt'),
'entry': _('Draft Entry'),
}[self.move_type]
name += ' '
if self.name and self.name != '/':
name += self.name
if self.env.context.get('input_full_display_name'):
if self.partner_id:
name += f', {self.partner_id.name}'
if self.date:
name += f', {format_date(self.env, self.date)}'
return name + (f" ({shorten(self.ref, width=50)})" if show_ref and self.ref else '')
def _get_reconciled_amls(self):
"""Helper used to retrieve the reconciled move lines on this journal entry"""
reconciled_lines = self.line_ids.filtered(lambda line: line.account_id.account_type in ('asset_receivable', 'liability_payable'))
return reconciled_lines.mapped('matched_debit_ids.debit_move_id') + reconciled_lines.mapped('matched_credit_ids.credit_move_id')
def _get_reconciled_payments(self):
"""Helper used to retrieve the reconciled payments on this journal entry"""
return self._get_reconciled_amls().move_id.origin_payment_id
def _get_reconciled_statement_lines(self):
"""Helper used to retrieve the reconciled statement lines on this journal entry"""
return self._get_reconciled_amls().move_id.statement_line_id
def _get_reconciled_invoices(self):
"""Helper used to retrieve the reconciled invoices on this journal entry"""
return self._get_reconciled_amls().move_id.filtered(lambda move: move.is_invoice(include_receipts=True))
def _get_all_reconciled_invoice_partials(self):
self.ensure_one()
reconciled_lines = self.line_ids.filtered(lambda line: line.account_id.account_type in ('asset_receivable', 'liability_payable'))
if not reconciled_lines:
return {}
self.env['account.partial.reconcile'].flush_model([
'credit_amount_currency', 'credit_move_id', 'debit_amount_currency',
'debit_move_id', 'exchange_move_id',
])
sql = SQL('''
SELECT
part.id,
part.exchange_move_id,
part.debit_amount_currency AS amount,
part.credit_move_id AS counterpart_line_id
FROM account_partial_reconcile part
WHERE part.debit_move_id IN %(line_ids)s
UNION ALL
SELECT
part.id,
part.exchange_move_id,
part.credit_amount_currency AS amount,
part.debit_move_id AS counterpart_line_id
FROM account_partial_reconcile part
WHERE part.credit_move_id IN %(line_ids)s
''', line_ids=tuple(reconciled_lines.ids))
partial_values_list = []
counterpart_line_ids = set()
exchange_move_ids = set()
for values in self.env.execute_query_dict(sql):
partial_values_list.append({
'aml_id': values['counterpart_line_id'],
'partial_id': values['id'],
'amount': values['amount'],
'currency': self.currency_id,
})
counterpart_line_ids.add(values['counterpart_line_id'])
if values['exchange_move_id']:
exchange_move_ids.add(values['exchange_move_id'])
if exchange_move_ids:
self.env['account.move.line'].flush_model(['move_id'])
sql = SQL('''
SELECT
part.id,
part.credit_move_id AS counterpart_line_id
FROM account_partial_reconcile part
JOIN account_move_line credit_line ON credit_line.id = part.credit_move_id
WHERE credit_line.move_id IN %(exchange_move_ids)s AND part.debit_move_id IN %(counterpart_line_ids)s
UNION ALL
SELECT
part.id,
part.debit_move_id AS counterpart_line_id
FROM account_partial_reconcile part
JOIN account_move_line debit_line ON debit_line.id = part.debit_move_id
WHERE debit_line.move_id IN %(exchange_move_ids)s AND part.credit_move_id IN %(counterpart_line_ids)s
''', exchange_move_ids=tuple(exchange_move_ids), counterpart_line_ids=tuple(counterpart_line_ids))
for part_id, line_ids in self.env.execute_query(sql):
counterpart_line_ids.add(line_ids)
partial_values_list.append({
'aml_id': line_ids,
'partial_id': part_id,
'currency': self.company_id.currency_id,
})
counterpart_lines = {x.id: x for x in self.env['account.move.line'].browse(counterpart_line_ids)}
for partial_values in partial_values_list:
partial_values['aml'] = counterpart_lines[partial_values['aml_id']]
partial_values['is_exchange'] = partial_values['aml'].move_id.id in exchange_move_ids
if partial_values['is_exchange']:
partial_values['amount'] = abs(partial_values['aml'].balance)
return partial_values_list
def _get_reconciled_invoices_partials(self):
''' Helper to retrieve the details about reconciled invoices.
:return A list of tuple (partial, amount, invoice_line).
'''
self.ensure_one()
pay_term_lines = self.line_ids\
.filtered(lambda line: line.account_type in ('asset_receivable', 'liability_payable'))
invoice_partials = []
exchange_diff_moves = []
for partial in pay_term_lines.matched_debit_ids:
invoice_partials.append((partial, partial.credit_amount_currency, partial.debit_move_id))
if partial.exchange_move_id:
exchange_diff_moves.append(partial.exchange_move_id.id)
for partial in pay_term_lines.matched_credit_ids:
invoice_partials.append((partial, partial.debit_amount_currency, partial.credit_move_id))
if partial.exchange_move_id:
exchange_diff_moves.append(partial.exchange_move_id.id)
return invoice_partials, exchange_diff_moves
def _reconcile_reversed_moves(self, reverse_moves, move_reverse_cancel):
''' Reconciles moves in self and reverse moves
:param move_reverse_cancel: parameter used when lines are reconciled
will determine whether the tax cash basis journal entries should be created
:param reverse_moves: An account.move recordset, reverse of the current self.
:return: An account.move recordset, reverse of the current self.
'''
for move, reverse_move in zip(self, reverse_moves):
group = (move.line_ids + reverse_move.line_ids) \
.filtered(lambda l: not l.reconciled) \
.grouped(lambda l: (l.account_id, l.currency_id))
for (account, _currency), lines in group.items():
if account.reconcile or account.account_type in ('asset_cash', 'liability_credit_card'):
lines.with_context(move_reverse_cancel=move_reverse_cancel).reconcile()
return reverse_moves
def _reverse_moves(self, default_values_list=None, cancel=False):
''' Reverse a recordset of account.move.
If cancel parameter is true, the reconcilable or liquidity lines
of each original move will be reconciled with its reverse's.
:param default_values_list: A list of default values to consider per move.
('type' & 'reversed_entry_id' are computed in the method).
:return: An account.move recordset, reverse of the current self.
'''
if not default_values_list:
default_values_list = [{} for move in self]
if cancel:
lines = self.mapped('line_ids')
# Avoid maximum recursion depth.
if lines:
lines.remove_move_reconcile()
reverse_moves = self.env['account.move']
for move, default_values in zip(self, default_values_list):
default_values.update({
'move_type': TYPE_REVERSE_MAP[move.move_type],
'reversed_entry_id': move.id,
'partner_id': move.partner_id.id,
})
reverse_moves += move.with_context(
move_reverse_cancel=cancel,
include_business_fields=True,
skip_invoice_sync=move.move_type == 'entry',
).copy(default_values)
reverse_moves.with_context(skip_invoice_sync=cancel).write({'line_ids': [
Command.update(line.id, {
'balance': -line.balance,
'amount_currency': -line.amount_currency,
})
for line in reverse_moves.line_ids
if line.move_id.move_type == 'entry' or line.display_type == 'cogs'
]})
# Reconcile moves together to cancel the previous one.
if cancel:
reverse_moves.with_context(move_reverse_cancel=cancel)._post(soft=False)
return reverse_moves
def _can_be_unlinked(self):
self.ensure_one()
lock_date = self.company_id._get_user_fiscal_lock_date(self.journal_id)
is_part_of_audit_trail = self.posted_before and self.company_id.check_account_audit_trail
return not self.inalterable_hash and self.date > lock_date and not is_part_of_audit_trail
def _unlink_or_reverse(self):
if not self:
return
to_reverse = self.env['account.move']
to_unlink = self.env['account.move']
for move in self:
if move._can_be_unlinked():
to_unlink += move
else:
to_reverse += move
to_unlink.filtered(lambda m: m.state in ('posted', 'cancel')).button_draft()
to_unlink.filtered(lambda m: m.state == 'draft').unlink()
return to_reverse._reverse_moves(cancel=True)
def _post(self, soft=True):
"""Post/Validate the documents.
Posting the documents will give it a number, and check that the document is
complete (some fields might not be required if not posted but are required
otherwise).
If the journal is locked with a hash table, it will be impossible to change
some fields afterwards.
:param soft (bool): if True, future documents are not immediately posted,
but are set to be auto posted automatically at the set accounting date.
Nothing will be performed on those documents before the accounting date.
:return Model<account.move>: the documents that have been posted
"""
if not self.env.su and not self.env.user.has_group('account.group_account_invoice'):
raise AccessError(_("You don't have the access rights to post an invoice."))
# Avoid marking is_manually_modified as True when posting an invoice
self = self.with_context(skip_is_manually_modified=True) # noqa: PLW0642
validation_msgs = set()
for invoice in self.filtered(lambda move: move.is_invoice(include_receipts=True)):
if (
invoice.quick_edit_mode
and invoice.quick_edit_total_amount
and invoice.currency_id.compare_amounts(invoice.quick_edit_total_amount, invoice.amount_total) != 0
):
validation_msgs.add(_(
"The current total is %(current_total)s but the expected total is %(expected_total)s. In order to post the invoice/bill, "
"you can adjust its lines or the expected Total (tax inc.).",
current_total=formatLang(self.env, invoice.amount_total, currency_obj=invoice.currency_id),
expected_total=formatLang(self.env, invoice.quick_edit_total_amount, currency_obj=invoice.currency_id),
))
if invoice.partner_bank_id and not invoice.partner_bank_id.active:
validation_msgs.add(_(
"The recipient bank account linked to this invoice is archived.\n"
"So you cannot confirm the invoice."
))
if float_compare(invoice.amount_total, 0.0, precision_rounding=invoice.currency_id.rounding) < 0:
validation_msgs.add(_(
"You cannot validate an invoice with a negative total amount. "
"You should create a credit note instead. "
"Use the action menu to transform it into a credit note or refund."
))
if not invoice.partner_id:
if invoice.is_sale_document():
validation_msgs.add(_("The field 'Customer' is required, please complete it to validate the Customer Invoice."))
elif invoice.is_purchase_document():
validation_msgs.add(_("The field 'Vendor' is required, please complete it to validate the Vendor Bill."))
# Handle case when the invoice_date is not set. In that case, the invoice_date is set at today and then,
# lines are recomputed accordingly.
if not invoice.invoice_date:
if invoice.is_sale_document(include_receipts=True):
invoice.invoice_date = fields.Date.context_today(self)
elif invoice.is_purchase_document(include_receipts=True):
validation_msgs.add(_("The Bill/Refund date is required to validate this document."))
for move in self:
if move.state in ['posted', 'cancel']:
validation_msgs.add(_('The entry %(name)s (id %(id)s) must be in draft.', name=move.name, id=move.id))
if not move.line_ids.filtered(lambda line: line.display_type not in ('line_section', 'line_note')):
validation_msgs.add(_('You need to add a line before posting.'))
if not soft and move.auto_post != 'no' and move.date > fields.Date.context_today(self):
date_msg = move.date.strftime(get_lang(self.env).date_format)
validation_msgs.add(_("This move is configured to be auto-posted on %(date)s", date=date_msg))
if not move.journal_id.active:
validation_msgs.add(_(
"You cannot post an entry in an archived journal (%(journal)s)",
journal=move.journal_id.display_name,
))
if move.display_inactive_currency_warning:
validation_msgs.add(_(
"You cannot validate a document with an inactive currency: %s",
move.currency_id.name
))
if move.line_ids.account_id.filtered(lambda account: account.deprecated) and not self._context.get('skip_account_deprecation_check'):
validation_msgs.add(_("A line of this move is using a deprecated account, you cannot post it."))
# If the field autocheck_on_post is set, we want the checked field on the move to be checked
move.checked = move.journal_id.autocheck_on_post
if validation_msgs:
msg = "\n".join([line for line in validation_msgs])
raise UserError(msg)
if soft:
future_moves = self.filtered(lambda move: move.date > fields.Date.context_today(self))
for move in future_moves:
if move.auto_post == 'no':
move.auto_post = 'at_date'
msg = _('This move will be posted at the accounting date: %(date)s', date=format_date(self.env, move.date))
move.message_post(body=msg)
to_post = self - future_moves
else:
to_post = self
for move in to_post:
affects_tax_report = move._affect_tax_report()
lock_dates = move._get_violated_lock_dates(move.date, affects_tax_report)
if lock_dates:
move.date = move._get_accounting_date(move.invoice_date or move.date, affects_tax_report, lock_dates=lock_dates)
# Create the analytic lines in batch is faster as it leads to less cache invalidation.
to_post.line_ids._create_analytic_lines()
# Trigger copying for recurring invoices
to_post.filtered(lambda m: m.auto_post not in ('no', 'at_date'))._copy_recurring_entries()
for invoice in to_post:
# Fix inconsistencies that may occure if the OCR has been editing the invoice at the same time of a user. We force the
# partner on the lines to be the same as the one on the move, because that's the only one the user can see/edit.
wrong_lines = invoice.is_invoice() and invoice.line_ids.filtered(lambda aml:
aml.partner_id != invoice.commercial_partner_id
and aml.display_type not in ('line_note', 'line_section')
)
if wrong_lines:
wrong_lines.write({'partner_id': invoice.commercial_partner_id.id})
# reconcile if state is in draft and move has reversal_entry_id set
draft_reverse_moves = to_post.filtered(lambda move: move.reversed_entry_id and move.reversed_entry_id.state == 'posted')
to_post.write({
'state': 'posted',
'posted_before': True,
})
draft_reverse_moves.reversed_entry_id._reconcile_reversed_moves(draft_reverse_moves, self._context.get('move_reverse_cancel', False))
to_post.line_ids._reconcile_marked()
for invoice in to_post:
invoice.message_subscribe([
p.id
for p in [invoice.partner_id]
if p not in invoice.sudo().message_partner_ids
])
customer_count, supplier_count = defaultdict(int), defaultdict(int)
for invoice in to_post:
if invoice.is_sale_document():
customer_count[invoice.partner_id] += 1
elif invoice.is_purchase_document():
supplier_count[invoice.partner_id] += 1
elif invoice.move_type == 'entry':
sale_amls = invoice.line_ids.filtered(lambda line: line.partner_id and line.account_id.account_type == 'asset_receivable')
for partner in sale_amls.mapped('partner_id'):
customer_count[partner] += 1
purchase_amls = invoice.line_ids.filtered(lambda line: line.partner_id and line.account_id.account_type == 'liability_payable')
for partner in purchase_amls.mapped('partner_id'):
supplier_count[partner] += 1
for partner, count in customer_count.items():
(partner | partner.commercial_partner_id)._increase_rank('customer_rank', count)
for partner, count in supplier_count.items():
(partner | partner.commercial_partner_id)._increase_rank('supplier_rank', count)
# Trigger action for paid invoices if amount is zero
to_post.filtered(
lambda m: m.is_invoice(include_receipts=True) and m.currency_id.is_zero(m.amount_total)
)._invoice_paid_hook()
return to_post
def _set_next_made_sequence_gap(self, made_gap: bool):
"""Update the field made_sequence_gap on the next moves of the current ones.
Either:
- we changed something related to the sequence on the current moves, so we need to set the
sequence as broken on the next moves before updating (made_gap=True)
- we are filling a gap, so we need to update the next move to remove the flag (made_gap=False)
"""
next_moves = self.browse()
named = self.filtered(lambda m: m.name and m.name != '/')
for (journal, prefix), moves in named.grouped(lambda move: (move.journal_id, move.sequence_prefix)).items():
next_moves += self.env['account.move'].sudo().search([
('journal_id', '=', journal.id),
('sequence_prefix', '=', prefix),
('sequence_number', 'in', [move.sequence_number + 1 for move in moves]),
])
next_moves.made_sequence_gap = made_gap
def _find_and_set_purchase_orders(self, po_references, partner_id, amount_total, from_ocr=False, timeout=10):
# hook to be used with purchase, so that vendor bills are sync/autocompleted with purchase orders
self.ensure_one()
def _link_bill_origin_to_purchase_orders(self, timeout=10):
for move in self.filtered(lambda m: m.move_type in self.get_purchase_types()):
references = [move.invoice_origin] if move.invoice_origin else []
move._find_and_set_purchase_orders(references, move.partner_id.id, move.amount_total, timeout=timeout)
return self
def _autopost_bill(self):
# Verify if the bill should be autoposted, if so, post it
self.ensure_one()
if (
self.company_id.autopost_bills
and self.partner_id
and self.is_purchase_document(include_receipts=True)
and self.partner_id.autopost_bills == 'always'
and not self.abnormal_amount_warning
and not self.restrict_mode_hash_table
):
self.action_post()
def _show_autopost_bills_wizard(self):
if (
len(self) != 1
or self.state != "posted"
or not self.is_purchase_document(include_receipts=True)
or self.restrict_mode_hash_table
or all(not l.is_imported for l in self.line_ids)
or not self.partner_id
or self.partner_id.autopost_bills != "ask"
or not self.company_id.autopost_bills
or self.is_manually_modified
):
return False
prev_bills_same_partner = self.search([
('id', '!=', self.id),
('partner_id', '=', self.partner_id.id),
('state', '=', 'posted'),
('move_type', 'in', self.get_purchase_types(include_receipts=True)),
], order="create_date DESC", limit=10)
nb_unmodified_bills = 1 # +1 for current bill that hasn't been modified either
for move in prev_bills_same_partner:
if move.is_manually_modified:
break
nb_unmodified_bills += 1
if nb_unmodified_bills < 3:
return False
wizard = self.env['account.autopost.bills.wizard'].create({
'partner_id': self.partner_id.id,
'nb_unmodified_bills': nb_unmodified_bills,
})
return {
'name': _("Autopost Bills"),
'type': 'ir.actions.act_window',
'res_model': 'account.autopost.bills.wizard',
'res_id': wizard.id,
'views': [(False, 'form')],
'target': 'new',
}
# -------------------------------------------------------------------------
# PUBLIC ACTIONS
# -------------------------------------------------------------------------
def open_payments(self):
return self.matched_payment_ids._get_records_action(name=_("Payments"))
def open_reconcile_view(self):
return self.line_ids.open_reconcile_view()
def action_open_business_doc(self):
self.ensure_one()
if self.origin_payment_id:
name = _("Payment")
res_model = 'account.payment'
res_id = self.origin_payment_id.id
elif self.statement_line_id:
name = _("Bank Transaction")
res_model = 'account.bank.statement.line'
res_id = self.statement_line_id.id
else:
name = _("Journal Entry")
res_model = 'account.move'
res_id = self.id
return {
'name': name,
'type': 'ir.actions.act_window',
'view_mode': 'form',
'views': [(False, 'form')],
'res_model': res_model,
'res_id': res_id,
'target': 'current',
}
def action_update_fpos_values(self):
self.invoice_line_ids._compute_tax_ids()
self.line_ids._compute_account_id()
def open_created_caba_entries(self):
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'name': _("Cash Basis Entries"),
'res_model': 'account.move',
'view_mode': 'form',
'domain': [('id', 'in', self.tax_cash_basis_created_move_ids.ids)],
'views': [(self.env.ref('account.view_move_tree').id, 'list'), (False, 'form')],
}
def action_switch_move_type(self):
if any(move.posted_before for move in self):
raise ValidationError(_("You cannot switch the type of a posted document."))
if any(move.move_type == "entry" for move in self):
raise ValidationError(_("This action isn't available for this document."))
for move in self:
in_out, old_move_type = move.move_type.split('_')
new_move_type = f"{in_out}_{'invoice' if old_move_type == 'refund' else 'refund'}"
move.name = False
move.write({
'move_type': new_move_type,
'partner_bank_id': False,
'currency_id': move.currency_id.id,
})
if move.amount_total < 0:
move.write({
'line_ids': [
Command.update(line.id, {'quantity': -line.quantity})
for line in move.line_ids
if line.display_type == 'product'
]
})
def action_register_payment(self):
if any(m.state != 'posted' for m in self):
raise UserError(_("You can only register payment for posted journal entries."))
return self.action_force_register_payment()
def action_force_register_payment(self):
if any(m.payment_state not in ('not_paid', 'partial', 'in_payment') for m in self):
raise UserError(_("You can only register payments for (partially) unpaid documents."))
if any(m.move_type == 'entry' for m in self):
raise UserError(_("You cannot register payments for miscellaneous entries."))
return self.line_ids.action_register_payment()
def action_duplicate(self):
# offer the possibility to duplicate thanks to a button instead of a hidden menu, which is more visible
self.ensure_one()
action = self.env["ir.actions.actions"]._for_xml_id("account.action_move_journal_line")
action['context'] = dict(self.env.context)
action['context']['view_no_maturity'] = False
action['views'] = [(self.env.ref('account.view_move_form').id, 'form')]
action['res_id'] = self.copy().id
return action
def action_send_and_print(self):
return {
'name': _("Print & Send"),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'account.move.send.wizard' if len(self) == 1 else 'account.move.send.batch.wizard',
'target': 'new',
'context': {
'active_model': 'account.move',
'active_ids': self.ids,
},
}
def action_invoice_sent(self):
""" Open a window to compose an email, with the edi invoice template
message loaded by default
"""
self.ensure_one()
report_action = self.action_send_and_print()
if self.env.is_admin() and not self.env.company.external_report_layout_id and not self.env.context.get('discard_logo_check'):
report_action = self.env['ir.actions.report']._action_configure_external_report_layout(report_action, "account.action_base_document_layout_configurator")
report_action['context']['default_from_invoice'] = self.move_type == 'out_invoice'
return report_action
def action_invoice_download_pdf(self):
return {
'type': 'ir.actions.act_url',
'url': f'/account/download_invoice_documents/{",".join(map(str, self.ids))}/pdf',
'target': 'download',
}
def preview_invoice(self):
self.ensure_one()
return {
'type': 'ir.actions.act_url',
'target': 'self',
'url': self.get_portal_url(),
}
def action_reverse(self):
action = self.env["ir.actions.actions"]._for_xml_id("account.action_view_account_move_reversal")
if self.is_invoice():
action['name'] = _('Credit Note')
return action
def action_post(self):
# Disabled by default to avoid breaking automated action flow
if (
not self.env.context.get('disable_abnormal_invoice_detection', True)
and self.filtered(lambda m: m.abnormal_amount_warning or m.abnormal_date_warning)
):
wizard = self.env['validate.account.move'].create({
'move_ids': [Command.set(self.ids)],
})
return {
'name': _("Confirm Entries"),
'type': 'ir.actions.act_window',
'res_model': 'validate.account.move',
'res_id': wizard.id,
'view_mode': 'form',
'target': 'new',
}
if self:
self._post(soft=False)
if autopost_bills_wizard := self._show_autopost_bills_wizard():
return autopost_bills_wizard
return False
def js_assign_outstanding_line(self, line_id):
''' Called by the 'payment' widget to reconcile a suggested journal item to the present
invoice.
:param line_id: The id of the line to reconcile with the current invoice.
'''
self.ensure_one()
lines = self.env['account.move.line'].browse(line_id)
lines += self.line_ids.filtered(lambda line: line.account_id == lines[0].account_id and not line.reconciled)
return lines.reconcile()
def js_remove_outstanding_partial(self, partial_id):
''' Called by the 'payment' widget to remove a reconciled entry to the present invoice.
:param partial_id: The id of an existing partial reconciled with the current invoice.
'''
self.ensure_one()
partial = self.env['account.partial.reconcile'].browse(partial_id)
return partial.unlink()
def button_set_checked(self):
for move in self:
move.checked = True
def button_draft(self):
if any(move.state not in ('cancel', 'posted') for move in self):
raise UserError(_("Only posted/cancelled journal entries can be reset to draft."))
if any(move.need_cancel_request for move in self):
raise UserError(_("You can't reset to draft those journal entries. You need to request a cancellation instead."))
self._check_draftable()
# We remove all the analytics entries for this journal
self.mapped('line_ids.analytic_line_ids').with_context(force_analytic_line_delete=True).unlink()
self.mapped('line_ids').remove_move_reconcile()
self.state = 'draft'
def _check_draftable(self):
exchange_move_ids = set()
if self:
self.env['account.full.reconcile'].flush_model(['exchange_move_id'])
self.env['account.partial.reconcile'].flush_model(['exchange_move_id'])
sql = SQL(
"""
SELECT DISTINCT sub.exchange_move_id
FROM (
SELECT exchange_move_id
FROM account_full_reconcile
WHERE exchange_move_id IN %s
UNION ALL
SELECT exchange_move_id
FROM account_partial_reconcile
WHERE exchange_move_id IN %s
) AS sub
""",
tuple(self.ids), tuple(self.ids),
)
exchange_move_ids = {id_ for id_, in self.env.execute_query(sql)}
for move in self:
if move.id in exchange_move_ids:
raise UserError(_('You cannot reset to draft an exchange difference journal entry.'))
if move.tax_cash_basis_rec_id or move.tax_cash_basis_origin_move_id:
# If the reconciliation was undone, move.tax_cash_basis_rec_id will be empty;
# but we still don't want to allow setting the caba entry to draft
# (it'll have been reversed automatically, so no manual intervention is required),
# so we also check tax_cash_basis_origin_move_id, which stays unchanged
# (we need both, as tax_cash_basis_origin_move_id did not exist in older versions).
raise UserError(_('You cannot reset to draft a tax cash basis journal entry.'))
if move.inalterable_hash:
raise UserError(_('You cannot reset to draft a locked journal entry.'))
def button_hash(self):
self._hash_moves(force_hash=True)
def button_request_cancel(self):
""" Hook allowing the localizations to request a cancellation from the government before cancelling the invoice. """
self.ensure_one()
if not self.need_cancel_request:
raise UserError(_("You can only request a cancellation for invoice sent to the government."))
def button_cancel(self):
# Shortcut to move from posted to cancelled directly. This is useful for E-invoices that must not be changed
# when sent to the government.
moves_to_reset_draft = self.filtered(lambda x: x.state == 'posted')
if moves_to_reset_draft:
moves_to_reset_draft.button_draft()
if any(move.state != 'draft' for move in self):
raise UserError(_("Only draft journal entries can be cancelled."))
self.write({'auto_post': 'no', 'state': 'cancel'})
def action_toggle_block_payment(self):
self.ensure_one()
if self.payment_state == 'blocked':
self.payment_state = 'not_paid'
self.env.add_to_compute(self._fields['payment_state'], self)
else:
if self.payment_state in ('paid', 'in_payment'):
raise UserError(_("You can't block a paid invoice."))
self.payment_state = 'blocked'
def action_activate_currency(self):
self.currency_id.filtered(lambda currency: not currency.active).write({'active': True})
def _get_mail_template(self):
"""
:return: the correct mail template based on the current move type
"""
return self.env.ref(
'account.email_template_edi_credit_note'
if all(move.move_type == 'out_refund' for move in self)
else 'account.email_template_edi_invoice'
)
def _notify_get_recipients_groups(self, message, model_description, msg_vals=None):
groups = super()._notify_get_recipients_groups(message, model_description, msg_vals=msg_vals)
self.ensure_one()
if self.move_type != 'entry':
local_msg_vals = dict(msg_vals or {})
self._portal_ensure_token()
access_link = self._notify_get_action_link('view', **local_msg_vals, access_token=self.access_token)
# Create a new group for partners that have been manually added as recipients.
# Those partners should have access to the invoice.
button_access = {'url': access_link} if access_link else {}
recipient_group = (
'additional_intended_recipient',
lambda pdata: pdata['id'] in local_msg_vals.get('partner_ids', []) and pdata['id'] != self.partner_id.id and pdata['type'] != 'user',
{
'has_button_access': True,
'button_access': button_access,
}
)
groups.insert(0, recipient_group)
return groups
def _get_report_base_filename(self):
return self._get_move_display_name()
# -------------------------------------------------------------------------
# CRON
# -------------------------------------------------------------------------
def _autopost_draft_entries(self):
''' This method is called from a cron job.
It is used to post entries such as those created by the module
account_asset and recurring entries created in _post().
'''
moves = self.search([
('state', '=', 'draft'),
('date', '<=', fields.Date.context_today(self)),
('auto_post', '!=', 'no'),
'|', ('checked', '=', True), ('journal_id.autocheck_on_post', '=', True)
], limit=100)
try: # try posting in batch
with self.env.cr.savepoint():
moves._post()
except UserError: # if at least one move cannot be posted, handle moves one by one
for move in moves:
try:
with self.env.cr.savepoint():
move._post()
except UserError as e:
move.checked = False
msg = _('The move could not be posted for the following reason: %(error_message)s', error_message=e)
move.message_post(body=msg, message_type='comment')
if len(moves) == 100: # assumes there are more whenever search hits limit
self.env.ref('account.ir_cron_auto_post_draft_entry')._trigger()
@api.model
def _cron_account_move_send(self, job_count=10):
""" Process invoices generation and sending asynchronously.
:param job_count: maximum number of jobs to process if specified.
"""
def get_account_notification(moves, is_success: bool):
_ = self.env._
return [
'account_notification',
{
'type': 'success' if is_success else 'warning',
'title': _('Invoices sent') if is_success else _('Invoices in error'),
'message': _('Invoices sent successfully.') if is_success else _(
"One or more invoices couldn't be processed."),
'action_button': {
'name': _('Open'),
'action_name': _('Sent invoices') if is_success else _('Invoices in error'),
'model': 'account.move',
'res_ids': moves.ids,
},
},
]
limit = job_count + 1
to_process = self.env['account.move'].search(
[('sending_data', '!=', False)],
limit=limit,
)
need_retrigger = len(to_process) > job_count
if not to_process:
return
to_process = to_process[:job_count]
if not self.env['res.company']._with_locked_records(to_process, allow_raising=False):
return
# Collect moves by res.partner that executed the Send & Print wizard, must be done before the _process
# that modify sending_data.
moves_by_partner = to_process.grouped(lambda m: m.sending_data['author_partner_id'])
self.env['account.move.send']._generate_and_send_invoices(
to_process,
from_cron=True,
)
for partner_id, partner_moves in moves_by_partner.items():
partner = self.env['res.partner'].browse(partner_id)
partner_moves_error = partner_moves.filtered(lambda m: m.sending_data and m.sending_data.get('error'))
if partner_moves_error:
partner._bus_send(*get_account_notification(partner_moves_error, False))
partner_moves_success = partner_moves - partner_moves_error
if partner_moves_success:
partner._bus_send(*get_account_notification(partner_moves_success, True))
partner_moves_error.sending_data = False
if need_retrigger:
self.env.ref('account.ir_cron_account_move_send')._trigger()
# -------------------------------------------------------------------------
# HELPER METHODS
# -------------------------------------------------------------------------
@api.model
def get_invoice_types(self, include_receipts=False):
return self.get_sale_types(include_receipts) + self.get_purchase_types(include_receipts)
def is_invoice(self, include_receipts=False):
return self.is_sale_document(include_receipts) or self.is_purchase_document(include_receipts)
def is_entry(self):
return self.move_type == 'entry'
@api.model
def get_sale_types(self, include_receipts=False):
return ['out_invoice', 'out_refund'] + (include_receipts and ['out_receipt'] or [])
def is_sale_document(self, include_receipts=False):
return self.move_type in self.get_sale_types(include_receipts)
@api.model
def get_purchase_types(self, include_receipts=False):
return ['in_invoice', 'in_refund'] + (include_receipts and ['in_receipt'] or [])
def is_purchase_document(self, include_receipts=False):
return self.move_type in self.get_purchase_types(include_receipts)
@api.model
def get_inbound_types(self, include_receipts=True):
return ['out_invoice', 'in_refund'] + (include_receipts and ['out_receipt'] or [])
def is_inbound(self, include_receipts=True):
return self.move_type in self.get_inbound_types(include_receipts)
@api.model
def get_outbound_types(self, include_receipts=True):
return ['in_invoice', 'out_refund'] + (include_receipts and ['in_receipt'] or [])
def is_outbound(self, include_receipts=True):
return self.move_type in self.get_outbound_types(include_receipts)
def _get_installments_data(self):
self.ensure_one()
term_lines = self.line_ids.filtered(lambda l: l.display_type == 'payment_term')
return term_lines._get_installments_data()
def _get_invoice_next_payment_values(self, custom_amount=None):
self.ensure_one()
term_lines = self.line_ids.filtered(lambda line: line.display_type == 'payment_term')
if not term_lines:
return {}
installments = term_lines._get_installments_data()
not_reconciled_installments = [x for x in installments if not x['reconciled']]
overdue_installments = [x for x in not_reconciled_installments if x['type'] == 'overdue']
# Early payment discounts can only have one installment at most
epd_installment = next((installment for installment in installments if installment['type'] == 'early_payment_discount'), {})
show_installments = len(installments) > 1
additional_info = {}
if show_installments and overdue_installments:
installment_state = 'overdue'
amount_due = self.amount_residual
next_amount_to_pay = sum(x['amount_residual_currency_unsigned'] for x in overdue_installments)
next_payment_reference = f"{self.name}-{overdue_installments[0]['number']}"
next_due_date = overdue_installments[0]['date_maturity']
elif show_installments and not_reconciled_installments:
installment_state = 'next'
amount_due = self.amount_residual
next_amount_to_pay = not_reconciled_installments[0]['amount_residual_currency_unsigned']
next_payment_reference = f"{self.name}-{not_reconciled_installments[0]['number']}"
next_due_date = not_reconciled_installments[0]['date_maturity']
elif epd_installment:
installment_state = 'epd'
amount_due = epd_installment['amount_residual_currency_unsigned']
next_amount_to_pay = self.amount_residual
next_payment_reference = self.name
next_due_date = epd_installment['date_maturity']
discount_date = epd_installment['line'].discount_date
discount_amount_currency = epd_installment['discount_amount_currency']
days_left = (discount_date - fields.Date.context_today(self)).days # should never be lower than 0 since epd is valid
if days_left > 0:
discount_msg = _(
"Discount of %(amount)s if paid within %(days)s days",
amount=self.currency_id.format(discount_amount_currency),
days=days_left,
)
else:
discount_msg = _(
"Discount of %(amount)s if paid today",
amount=self.currency_id.format(discount_amount_currency),
)
additional_info.update({
'epd_discount_amount_currency': discount_amount_currency,
'epd_discount_amount': epd_installment['discount_amount'],
'discount_date': fields.Date.to_string(discount_date),
'epd_days_left': days_left,
'epd_line': epd_installment['line'],
'epd_discount_msg': discount_msg,
})
else:
installment_state = None
amount_due = self.amount_residual
next_amount_to_pay = self.amount_residual
next_payment_reference = self.name
next_due_date = self.invoice_date_due
if custom_amount is not None:
is_custom_amount_same_as_next_amount = self.currency_id.is_zero(custom_amount - next_amount_to_pay)
is_custom_amount_same_as_epd_discounted_amount = installment_state == 'epd' and self.currency_id.is_zero(custom_amount - amount_due)
if not is_custom_amount_same_as_next_amount and not is_custom_amount_same_as_epd_discounted_amount:
installment_state = 'next'
next_amount_to_pay = custom_amount
next_payment_reference = self.name
next_due_date = installments[0]['date_maturity']
return {
'payment_state': self.payment_state,
'installment_state': installment_state,
'next_amount_to_pay': next_amount_to_pay,
'next_payment_reference': next_payment_reference,
'amount_paid': self.amount_total - self.amount_residual,
'amount_due': amount_due,
'next_due_date': next_due_date,
'due_date': self.invoice_date_due,
'not_reconciled_installments': not_reconciled_installments,
'is_last_installment': len(not_reconciled_installments) == 1,
**additional_info,
}
def _get_invoice_portal_extra_values(self, custom_amount=None):
self.ensure_one()
return {
'invoice': self,
'currency': self.currency_id,
**self._get_invoice_next_payment_values(custom_amount=custom_amount),
}
def _get_accounting_date(self, invoice_date, has_tax, lock_dates=None):
"""Get correct accounting date for previous periods, taking tax lock date and affected journal into account.
When registering an invoice in the past, we still want the sequence to be increasing.
We then take the last day of the period, depending on the sequence format.
If there is a tax lock date and there are taxes involved, we register the invoice at the
last date of the first open period.
:param invoice_date (datetime.date): The invoice date
:param has_tax (bool): Iff any taxes are involved in the lines of the invoice
:param lock_dates: Like result from `_get_violated_lock_dates`;
Can be used to avoid recomputing them in case they are already known.
:return (datetime.date):
"""
self.ensure_one()
lock_dates = lock_dates or self._get_violated_lock_dates(invoice_date, has_tax)
today = fields.Date.context_today(self)
highest_name = self.highest_name or self._get_last_sequence(relaxed=True)
number_reset = self._deduce_sequence_number_reset(highest_name)
if lock_dates:
invoice_date = lock_dates[-1][0] + timedelta(days=1)
if self.is_sale_document(include_receipts=True):
if lock_dates:
if not highest_name or number_reset == 'month':
return min(today, date_utils.get_month(invoice_date)[1])
elif number_reset == 'year':
return min(today, date_utils.end_of(invoice_date, 'year'))
else:
if not highest_name or number_reset in ('month', 'year_range_month'):
if (today.year, today.month) > (invoice_date.year, invoice_date.month):
return date_utils.get_month(invoice_date)[1]
else:
return max(invoice_date, today)
elif number_reset == 'year':
if today.year > invoice_date.year:
return date(invoice_date.year, 12, 31)
else:
return max(invoice_date, today)
return invoice_date
def _get_violated_lock_dates(self, invoice_date, has_tax):
"""Get all the lock dates affecting the current invoice_date.
:param invoice_date: The invoice date
:param has_tax: If any taxes are involved in the lines of the invoice
:return: a list of tuples containing the lock dates affecting this move, ordered chronologically.
"""
self.ensure_one()
return self.company_id._get_violated_lock_dates(invoice_date, has_tax, self.journal_id)
def _get_lock_date_message(self, invoice_date, has_tax):
"""Get a message describing the latest lock date affecting the specified date.
:param invoice_date: The date to be checked
:param has_tax: If any taxes are involved in the lines of the invoice
:return: a message describing the latest lock date affecting this move and the date it will be
accounted on if posted, or False if no lock dates affect this move.
"""
lock_dates = self._get_violated_lock_dates(invoice_date, has_tax)
if lock_dates:
invoice_date = self._get_accounting_date(invoice_date, has_tax, lock_dates=lock_dates)
tax_lock_date_message = _(
"The date is being set prior to: %(lock_date_info)s. "
"The Journal Entry will be accounted on %(invoice_date)s upon posting.",
lock_date_info=self.env['res.company']._format_lock_dates(lock_dates),
invoice_date=format_date(self.env, invoice_date))
return tax_lock_date_message
return False
@api.model
def _move_dict_to_preview_vals(self, move_vals, currency_id=None):
preview_vals = {
'group_name': "%s, %s" % (format_date(self.env, move_vals['date']) or _('[Not set]'), move_vals['ref']),
'items_vals': move_vals['line_ids'],
}
for line in preview_vals['items_vals']:
if 'partner_id' in line[2]:
# sudo is needed to compute display_name in a multi companies environment
line[2]['partner_id'] = self.env['res.partner'].browse(line[2]['partner_id']).sudo().display_name
line[2]['account_id'] = self.env['account.account'].browse(line[2]['account_id']).display_name or _('Destination Account')
line[2]['debit'] = currency_id and formatLang(self.env, line[2]['debit'], currency_obj=currency_id) or line[2]['debit']
line[2]['credit'] = currency_id and formatLang(self.env, line[2]['credit'], currency_obj=currency_id) or line[2]['debit']
return preview_vals
def _generate_qr_code(self, silent_errors=False):
""" Generates and returns a QR-code generation URL for this invoice,
raising an error message if something is misconfigured.
The chosen QR generation method is the one set in qr_method field if there is one,
or the first eligible one found. If this search had to be performed and
and eligible method was found, qr_method field is set to this method before
returning the URL. If no eligible QR method could be found, we return None.
"""
self.ensure_one()
if not self.display_qr_code:
return None
qr_code_method = self.qr_code_method
if qr_code_method:
# If the user set a qr code generator manually, we check that we can use it
error_msg = self.partner_bank_id._get_error_messages_for_qr(self.qr_code_method, self.partner_id, self.currency_id)
if error_msg:
raise UserError(error_msg)
else:
# Else we find one that's eligible and assign it to the invoice
for candidate_method, _candidate_name in self.env['res.partner.bank'].get_available_qr_methods_in_sequence():
error_msg = self.partner_bank_id._get_error_messages_for_qr(candidate_method, self.partner_id, self.currency_id)
if not error_msg:
qr_code_method = candidate_method
break
if not qr_code_method:
# No eligible method could be found; we can't generate the QR-code
return None
unstruct_ref = self.ref if self.ref else self.name
rslt = self.partner_bank_id.build_qr_code_base64(self.amount_residual, unstruct_ref, self.payment_reference, self.currency_id, self.partner_id, qr_code_method, silent_errors=silent_errors)
# We only set qr_code_method after generating the url; otherwise, it
# could be set even in case of a failure in the QR code generation
# (which would change the field, but not refresh UI, making the displayed data inconsistent with db)
self.qr_code_method = qr_code_method
return rslt
def _generate_and_send(self, force_synchronous=True, allow_fallback_pdf=True, **custom_settings):
""" Generate the pdf and electronic format(s) for the current invoices and send them given default settings
(on partner or company) or given provided custom_settings.
:param force_synchronous: whether to process (as)synchronously (! only relevant for batch sending (multiple invoices))
:param allow_fallback_pdf: In case of error when generating the documents for invoices, generate a
proforma PDF report instead.
:param custom_settings: custom settings to create the wizard (! only relevant for single sending (one invoice))
(Since default settings are use for batch sending.
If you are looking for something more flexible, directly call env[account.move.send]._generate_and_send_invoices method.)
"""
if not self:
return
if len(self) == 1:
wizard = self.env['account.move.send.wizard'].with_context(
active_model='account.move',
active_ids=self.ids,
).create(custom_settings)
wizard.action_send_and_print(allow_fallback_pdf=allow_fallback_pdf)
else:
wizard = self.env['account.move.send.batch.wizard'].with_context(
active_model='account.move',
active_ids=self.ids,
).create({})
wizard.action_send_and_print(force_synchronous=force_synchronous)
return wizard
def _get_invoice_pdf_proforma(self):
""" Generate the Proforma of the invoice.
:return dict: the Proforma's data such as
{'filename': 'INV_2024_0001_proforma.pdf', 'filetype': 'pdf', 'content': ...}
"""
self.ensure_one()
filename = self._get_invoice_proforma_pdf_report_filename()
content, report_type = self.env['ir.actions.report']._pre_render_qweb_pdf('account.account_invoices', self.ids, data={'proforma': True})
content_by_id = self.env['ir.actions.report']._get_splitted_report('account.account_invoices', content, report_type)
return {
'filename': filename,
'filetype': 'pdf',
'content': content_by_id[self.id],
}
def _get_invoice_legal_documents(self, filetype, allow_fallback=False):
""" Retrieve the invoice legal document of type filetype.
:param filetype: the type of legal document to retrieve. Example: 'pdf', 'all'.
:param bool allow_fallback: if True, returns a Proforma if the PDF invoice doesn't exist.
:return dict: the invoice PDF data such as
{'filename': 'INV_2024_0001.pdf', 'filetype': 'pdf', 'content':...}
To extend to add more supported filetypes.
"""
self.ensure_one()
if filetype == 'pdf':
if invoice_pdf := self.invoice_pdf_report_id:
return {
'filename': invoice_pdf.name,
'filetype': invoice_pdf.mimetype,
'content': invoice_pdf.raw,
}
elif allow_fallback:
return self._get_invoice_pdf_proforma()
elif filetype == 'all':
return self._get_invoice_legal_documents_all(allow_fallback=allow_fallback)
def _get_invoice_legal_documents_all(self, allow_fallback=False):
""" Retrieve the invoice legal attachments: PDF, XML, ...
:param bool allow_fallback: if True, returns a Proforma if the PDF invoice doesn't exist.
:return list: a list of the attachments data such as
[{'filename': 'INV_2024_0001.pdf', 'filetype': 'pdf', 'content': ...}, ...]
"""
self.ensure_one()
if self.invoice_pdf_report_id:
attachments = self.env['account.move.send']._get_invoice_extra_attachments(self)
return [
{
'filename': attachment.name,
'filetype': attachment.mimetype,
'content': attachment.raw,
}
for attachment in attachments
]
elif allow_fallback:
return [self._get_invoice_pdf_proforma()]
def _get_invoice_report_filename(self, extension='pdf'):
""" Get the filename of the generated invoice report with extension file. """
self.ensure_one()
return f"{self.name.replace('/', '_')}.{extension}"
def _get_invoice_proforma_pdf_report_filename(self):
""" Get the filename of the generated proforma PDF invoice report. """
self.ensure_one()
return f"{self.name.replace('/', '_')}_proforma.pdf"
def _prepare_edi_vals_to_export(self):
''' The purpose of this helper is to prepare values in order to export an invoice through the EDI system.
This includes the computation of the tax details for each invoice line that could be very difficult to
handle regarding the computation of the base amount.
:return: A python dict containing default pre-processed values.
'''
self.ensure_one()
res = {
'record': self,
'balance_multiplicator': -1 if self.is_inbound() else 1,
'invoice_line_vals_list': [],
}
# Invoice lines details.
for index, line in enumerate(self.invoice_line_ids.filtered(lambda line: line.display_type == 'product'), start=1):
line_vals = line._prepare_edi_vals_to_export()
line_vals['index'] = index
res['invoice_line_vals_list'].append(line_vals)
# Totals.
res.update({
'total_price_subtotal_before_discount': sum(x['price_subtotal_before_discount'] for x in res['invoice_line_vals_list']),
'total_price_discount': sum(x['price_discount'] for x in res['invoice_line_vals_list']),
})
return res
def _get_discount_allocation_account(self):
if self.is_sale_document(include_receipts=True) and self.company_id.account_discount_expense_allocation_id:
return self.company_id.account_discount_expense_allocation_id
if self.is_purchase_document(include_receipts=True) and self.company_id.account_discount_income_allocation_id:
return self.company_id.account_discount_income_allocation_id
return None
# -------------------------------------------------------------------------
# TOOLING
# -------------------------------------------------------------------------
@api.model
def _field_will_change(self, record, vals, field_name):
if field_name not in vals:
return False
field = record._fields[field_name]
if field.type == 'many2one':
return record[field_name].id != vals[field_name]
if field.type == 'many2many':
current_ids = set(record[field_name].ids)
after_write_ids = set(record.new({field_name: vals[field_name]})[field_name].ids)
return current_ids != after_write_ids
if field.type == 'one2many':
return True
if field.type == 'monetary' and record[field.get_currency_field(record)]:
return not record[field.get_currency_field(record)].is_zero(record[field_name] - vals[field_name])
if field.type == 'float':
record_value = field.convert_to_cache(record[field_name], record)
to_write_value = field.convert_to_cache(vals[field_name], record)
return record_value != to_write_value
return record[field_name] != vals[field_name]
@api.model
def _cleanup_write_orm_values(self, record, vals):
cleaned_vals = dict(vals)
for field_name in vals.keys():
if not self._field_will_change(record, vals, field_name):
del cleaned_vals[field_name]
return cleaned_vals
@contextmanager
def _disable_recursion(self, container, key, default=None, target=True):
"""Apply the context key to all environments inside this context manager.
If this context key is already set on the recordsets, yield `True`.
The recordsets modified are the one in the container, as well as all the
`self` recordsets of the calling stack.
This more or less gives the wanted context to all records inside of the
context manager.
:param container: A mutable dict that needs to at least contain the key
`records`. Can contain other items if changing the env
is needed.
:param key: The context key to apply to the recordsets.
:param default: the default value of the context key, if it isn't defined
yet in the context
:param target: the value of the context key meaning that we shouldn't
recurse
:return: True iff we should just exit the context manager
"""
disabled = container['records'].env.context.get(key, default) == target
previous_values = {}
previous_envs = set(self.env.transaction.envs)
if not disabled: # it wasn't disabled yet, disable it now
for env in self.env.transaction.envs:
previous_values[env] = env.context.get(key, EMPTY)
env.context = frozendict({**env.context, key: target})
try:
yield disabled
finally:
for env, val in previous_values.items():
if val != EMPTY:
env.context = frozendict({**env.context, key: val})
else:
env.context = frozendict({k: v for k, v in env.context.items() if k != key})
for env in (self.env.transaction.envs - previous_envs):
if key in env.context:
env.context = frozendict({k: v for k, v in env.context.items() if k != key})
# ------------------------------------------------------------
# MAIL.THREAD
# ------------------------------------------------------------
def _mailing_get_default_domain(self, mailing):
return ['&', ('move_type', '=', 'out_invoice'), ('state', '=', 'posted')]
@api.model
def _routing_check_route(self, message, message_dict, route, raise_exception=True):
if route[0] == 'account.move' and len(message_dict['attachments']) < 1:
# Don't create the move if no attachment.
body = self.env['ir.qweb']._render('account.email_template_mail_gateway_failed', {
'company_email': self.env.company.email,
'company_name': self.env.company.name,
})
self._routing_create_bounce_email(message_dict['from'], body, message)
return ()
return super()._routing_check_route(message, message_dict, route, raise_exception=raise_exception)
@api.model
def message_new(self, msg_dict, custom_values=None):
# EXTENDS mail mail.thread
# Add custom behavior when receiving a new invoice through the mail's gateway.
if (custom_values or {}).get('move_type', 'entry') not in ('out_invoice', 'in_invoice', 'entry'):
return super().message_new(msg_dict, custom_values=custom_values)
self = self.with_context(skip_is_manually_modified=True) # noqa: PLW0642
company = self.env['res.company'].browse(custom_values['company_id']) if custom_values.get('company_id') else self.env.company
def is_internal_partner(partner):
# Helper to know if the partner is an internal one.
return partner == company.partner_id or (partner.user_ids and all(user._is_internal() for user in partner.user_ids))
extra_domain = False
if custom_values.get('company_id'):
extra_domain = ['|', ('company_id', '=', custom_values['company_id']), ('company_id', '=', False)]
# Search for partners in copy.
cc_mail_addresses = email_split(msg_dict.get('cc', ''))
followers = [partner for partner in self._mail_find_partner_from_emails(cc_mail_addresses, extra_domain=extra_domain) if partner]
# Search for partner that sent the mail.
from_mail_addresses = email_split(msg_dict.get('from', ''))
senders = partners = [partner for partner in self._mail_find_partner_from_emails(from_mail_addresses, extra_domain=extra_domain) if partner]
# Search for partners using the user.
if not senders:
senders = partners = list(self._mail_search_on_user(from_mail_addresses))
if partners:
# Check we are not in the case when an internal user forwarded the mail manually.
if is_internal_partner(partners[0]):
# Search for partners in the mail's body.
body_mail_addresses = set(email_re.findall(msg_dict.get('body')))
partners = [
partner
for partner in self._mail_find_partner_from_emails(body_mail_addresses, extra_domain=extra_domain)
if not is_internal_partner(partner) and partner.company_id.id in (False, company.id)
]
# Little hack: Inject the mail's subject in the body.
if msg_dict.get('subject') and msg_dict.get('body'):
msg_dict['body'] = Markup('<div><div><h3>%s</h3></div>%s</div>') % (msg_dict['subject'], msg_dict['body'])
# Create the invoice.
values = {
'name': '/', # we have to give the name otherwise it will be set to the mail's subject
'invoice_source_email': from_mail_addresses[0],
'partner_id': partners and partners[0].id or False,
}
move_ctx = self.with_context(default_move_type=custom_values['move_type'], default_journal_id=custom_values['journal_id'])
move = super(AccountMove, move_ctx).message_new(msg_dict, custom_values=values)
move._compute_name() # because the name is given, we need to recompute in case it is the first invoice of the journal
# Assign followers.
all_followers_ids = set(partner.id for partner in followers + senders + partners if is_internal_partner(partner))
move.message_subscribe(list(all_followers_ids))
return move
def _message_post_after_hook(self, new_message, message_values):
# EXTENDS mail mail.thread
# When posting a message, check the attachment to see if it's an invoice and update with the imported data.
res = super()._message_post_after_hook(new_message, message_values)
if not self.env.user._is_internal():
return res
attachments = new_message.attachment_ids
attachments_per_invoice = defaultdict(lambda: self.env['ir.attachment'])
checked_attachment = self._check_and_decode_attachment(attachments)
if not checked_attachment:
return res
for attachment_in_res, invoices in checked_attachment.items():
invoices = invoices or self
for invoice in invoices:
attachments_per_invoice[invoice] |= attachment_in_res
for invoice, attachments in attachments_per_invoice.items():
if invoice == self:
invoice.attachment_ids |= attachments
new_message.attachment_ids = attachments.ids
message_values.update({'res_id': self.id, 'attachment_ids': [Command.link(attachment.id) for attachment in attachments]})
super(AccountMove, invoice)._message_post_after_hook(new_message, message_values)
else:
sub_new_message = new_message.copy({'attachment_ids': attachments.ids})
sub_message_values = {
**message_values,
'res_id': invoice.id,
'attachment_ids': [Command.link(attachment.id) for attachment in attachments],
}
invoice.attachment_ids |= attachments
invoice.message_ids = [Command.set(sub_new_message.id)]
super(AccountMove, invoice)._message_post_after_hook(sub_new_message, sub_message_values)
return res
def _check_and_decode_attachment(self, attachments):
if not attachments or self.env.context.get('no_new_invoice'):
return False
if self.state != 'draft':
self.with_user(SUPERUSER_ID).message_post(
body=_('The invoice is not a draft, it was not updated from the attachment.'),
message_type='comment',
)
return False
# As we are coming from the mail, we assume that ONE of the attachments
# will enhance the invoice thanks to EDI / OCR / .. capabilities
move_per_decodable_attachment = self._extend_with_attachments(attachments, new=bool(self._context.get('from_alias')))
if self.invoice_line_ids and not move_per_decodable_attachment:
self.with_user(SUPERUSER_ID).message_post(
body=_('The invoice already contains lines, it was not updated from the attachment.'),
message_type='comment',
)
return False
attachments_in_invoices = self.env['ir.attachment']
for attachment in move_per_decodable_attachment:
attachments_in_invoices += attachment
# Unlink the unused attachments
(attachments - attachments_in_invoices).unlink()
return move_per_decodable_attachment
def _creation_subtype(self):
# EXTENDS mail mail.thread
if self.move_type in ('out_invoice', 'out_receipt'):
return self.env.ref('account.mt_invoice_created')
else:
return super()._creation_subtype()
def _track_subtype(self, init_values):
# EXTENDS mail mail.thread
# add custom subtype depending of the state.
self.ensure_one()
if not self.is_invoice(include_receipts=True):
if self.origin_payment_id and 'state' in init_values:
self.origin_payment_id._message_track(['state'], {self.origin_payment_id.id: init_values})
return super()._track_subtype(init_values)
if 'payment_state' in init_values and self.payment_state == 'paid':
return self.env.ref('account.mt_invoice_paid')
elif 'state' in init_values and self.state == 'posted' and self.is_sale_document(include_receipts=True):
return self.env.ref('account.mt_invoice_validated')
return super()._track_subtype(init_values)
def _creation_message(self):
# EXTENDS mail mail.thread
if not self.is_invoice(include_receipts=True):
return super()._creation_message()
return {
'out_invoice': _('Invoice Created'),
'out_refund': _('Credit Note Created'),
'in_invoice': _('Vendor Bill Created'),
'in_refund': _('Refund Created'),
'out_receipt': _('Sales Receipt Created'),
'in_receipt': _('Purchase Receipt Created'),
}[self.move_type]
def _notify_by_email_prepare_rendering_context(self, message, msg_vals=False, model_description=False,
force_email_company=False, force_email_lang=False):
# EXTENDS mail mail.thread
render_context = super()._notify_by_email_prepare_rendering_context(
message, msg_vals, model_description=model_description,
force_email_company=force_email_company, force_email_lang=force_email_lang
)
record = render_context['record']
subtitles = [f"{record.name} - {record.partner_id.name}" if record.partner_id else record.name]
if (
self.invoice_date_due
and self.is_invoice(include_receipts=True)
and self.payment_state not in ('in_payment', 'paid')
):
subtitles.append(_('%(amount)s due\N{NO-BREAK SPACE}%(date)s',
amount=format_amount(self.env, self.amount_total, self.currency_id, lang_code=render_context.get('lang')),
date=format_date(self.env, self.invoice_date_due, lang_code=render_context.get('lang'))
))
else:
subtitles.append(format_amount(self.env, self.amount_total, self.currency_id, lang_code=render_context.get('lang')))
render_context['subtitles'] = subtitles
return render_context
def _get_mail_thread_data_attachments(self):
res = super()._get_mail_thread_data_attachments()
# else, attachments with 'res_field' get excluded
return res | self.env['account.move.send']._get_invoice_extra_attachments(self)
# -------------------------------------------------------------------------
# TOOLING
# -------------------------------------------------------------------------
def _conditional_add_to_compute(self, fname, condition):
field = self._fields[fname]
to_reset = self.filtered(lambda move:
condition(move)
and not self.env.is_protected(field, move._origin)
and (move._origin or not move[fname])
)
to_reset.invalidate_recordset([fname])
self.env.add_to_compute(field, to_reset)
# -------------------------------------------------------------------------
# HOOKS
# -------------------------------------------------------------------------
def _action_invoice_ready_to_be_sent(self):
""" Hook allowing custom code when an invoice becomes ready to be sent by mail to the customer.
For example, when an EDI document must be sent to the government and be signed by it.
"""
def _is_ready_to_be_sent(self):
""" Helper telling if a journal entry is ready to be sent by mail to the customer.
:return: True if the invoice is ready, False otherwise.
"""
self.ensure_one()
return True
def _can_force_cancel(self):
""" Hook to indicate whether it should be possible to force-cancel this invoice,
that is, cancel it without waiting for the cancellation request to succeed.
"""
self.ensure_one()
return False
@contextmanager
def _send_only_when_ready(self):
moves_not_ready = self.filtered(lambda x: not x._is_ready_to_be_sent())
try:
yield
finally:
moves_now_ready = moves_not_ready.filtered(lambda x: x._is_ready_to_be_sent())
if moves_now_ready:
moves_now_ready._action_invoice_ready_to_be_sent()
def _invoice_paid_hook(self):
''' Hook to be overrided called when the invoice moves to the paid state. '''
def _get_lines_onchange_currency(self):
# Override needed for COGS
return self.line_ids
@api.model
def _get_invoice_in_payment_state(self):
''' Hook to give the state when the invoice becomes fully paid. This is necessary because the users working
with only invoicing don't want to see the 'in_payment' state. Then, this method will be overridden in the
accountant module to enable the 'in_payment' state. '''
return 'paid'
def _get_name_invoice_report(self):
""" This method need to be inherit by the localizations if they want to print a custom invoice report instead of
the default one. For example please review the l10n_ar module """
self.ensure_one()
return 'account.report_invoice_document'
def _is_downpayment(self):
''' Return true if the invoice is a downpayment.
Down-payments can be created from a sale order. This method is overridden in the sale order module.
'''
return False
@api.model
def get_invoice_localisation_fields_required_to_invoice(self, country_id):
""" Returns the list of fields that needs to be filled when creating an invoice for the selected country.
This is required for some flows that would allow a user to request an invoice from the portal.
Using these, we can get their information and dynamically create form inputs based for the fields required legally for the company country_id.
The returned fields must be of type ir.model.fields in order to handle translations
:param country_id: The country for which we want the fields.
:return: an array of ir.model.fields for which the user should provide values.
"""
return []
def get_extra_print_items(self):
""" Helper to dynamically add items in the 'Print' menu of list and form of account.move.
This is necessary to avoid the re-generation of the PDF through the action_report.
Indeed, once a legal PDF is generated, it should be used and not re-generated.
"""
return [{
'key': 'download_pdf',
'description': _('PDF'),
**self.action_invoice_download_pdf()
}]
@staticmethod
def _can_commit():
""" Helper to know if we can commit the current transaction or not.
:returns: True if commit is acceptable, False otherwise.
"""
return not tools.config['test_enable'] and not modules.module.current_test
|