con_contract_pkg.pck 308 KB
Newer Older
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 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165
create or replace package con_contract_pkg is

  -- Author  : ZHANGLEI
  -- Created : 2013/6/6 10:25:44
  -- Purpose :
  -- Version : 1.74

  -- 0426 添加 v_through_flag 字段
  v_incept_tolerance_days constant number := 3;
  function get_contract_rec(p_contract_id number, p_user_id number)
    return con_contract%rowtype;

  function get_cashflow_rec(p_cashflow_id number, p_user_id number)
    return con_contract_cashflow%rowtype;
  procedure update_contract_cashflow(p_contract_cashflow_rec con_contract_cashflow%rowtype);
  procedure insert_contract_cashflow(p_contract_cashflow_rec con_contract_cashflow%rowtype);
  procedure save_contract_cashflow(p_column_cf_item        hls_fin_calculator_itfc_pkg.column_cf_item_t,
                                   p_fin_calculator_hd_rec hls_fin_calculator_hd%rowtype,
                                   p_fin_calculator_ln_rec hls_fin_calculator_ln%rowtype,
                                   p_contract_id           number,
                                   p_user_id               number);

  procedure save_contract_lease_num(p_contract_id number);

  procedure save_contract_from_project(p_project_id        number,
                                       p_contract_seq      number,
                                       p_bp_contract_seq   number,
                                       p_con_document_type varchar2,
                                       p_con_contract_name varchar2,
                                       p_billing_method    varchar2,
                                       p_contract_id       out number,
                                       p_contract_number   out varchar2,
                                       p_user_id           number);

  procedure save_contract_from_calculator(p_contract_id      in number,
                                          p_calc_session_id  in number,
                                          p_save_contract_hd in varchar2 default 'Y',
                                          p_save_contract_ln in varchar2 default 'Y',
                                          p_user_id          in number);
  --add by jack wu 2014-10-23
  procedure insert_contract_item_detail(p_contract_id number,
                                        p_user_id     number);
  procedure recreate_cf_duedate(p_company_id      number default null,
                                p_contract_id     number default null,
                                p_contract_status varchar2 default null,
                                p_start_date      date,
                                p_end_date        date,
                                p_user_id         number);

  procedure contract_update_check(p_contract_id     number,
                                  p_user_id         number,
                                  p_contract_number out varchar2);

  procedure contract_print(p_contract_id number,
                           p_content_id  number,
                           p_user_id     number);

  procedure contract_sign(p_contract_id             number,
                          p_exchange_rate_quotation varchar2,
                          p_exchange_rate_type      varchar2,
                          p_exchange_rate           number,
                          p_signing_date            date,
                          p_user_id                 number);

  procedure contract_incept(p_contract_id                number,
                            p_exchange_rate_quotation    varchar2,
                            p_exchange_rate_type         varchar2,
                            p_exchange_rate              number,
                            p_inception_of_lease         date,
                            p_base_rate_new              number default null,
                            p_Refresh_cashflow_only_flag varchar2 default null,
                            p_user_id                    number);

  procedure contract_incept_rd_wf(p_contract_id number, p_user_id number);

  --合同取消
  procedure contract_cancel(p_contract_id number, p_user_id number);

  --更新现金流含税金额
  procedure update_cashflow_net_amount(p_contract_id number,
                                       p_user_id     number);

  procedure update_contract_file_date(p_file_date   date,
                                      p_contract_id number,
                                      p_user_id     number);

  function get_tax_rate(p_contract_id number, p_cf_item number) return number;
  --hongquan.dai 20180104 取tax_rate_id 
  function get_tax_rate_id(p_contract_id number, p_cf_item number)
    return number;

  function get_tax_type_rate(p_tax_type_id number default null) return number;

  procedure create_cf_date(p_contract_rec       con_contract%rowtype,
                           p_inception_of_lease date,
                           p_inception_50_flag  varchar2 default null);

  procedure save_contract_cf_item(p_contract_id number,
                                  
                                  p_company_id number,
                                  p_user_id    number);
  procedure save_contract_billing_method(p_contract_id    number,
                                         p_billing_method varchar2,
                                         p_user_id        number);

  procedure contract_print(p_contract_id number,
                           p_content_id  number,
                           p_file_path   varchar2,
                           p_file_name   varchar2,
                           p_type        varchar2,
                           p_user_id     number);

  function get_contract_number(p_document_type varchar2,
                               p_document_date date,
                               p_company_id    number,
                               p_user_id       number,
                               p_contract_id   number default null)
    return varchar2;
  -- 合同维护判断是否为NP
  procedure get_bp_class(p_bp_id_tenant  number,
                         p_bp_class_flag in out varchar2);

  /*add by xuls 2016-12-24 更改合同商业伙伴序号*/
  procedure update_contract_bp_seq(p_contract_id number,
                                   p_bp_category varchar2);

  procedure income_stat(p_date_from date, p_date_to date, p_user_id number);

  function get_fin_amount(p_bp_id number) return number;

  function check_grt_amount(p_user_id number) return varchar2;

  procedure ACCOUNT_QUERY(p_account_type varchar2,
                          p_date_from    date,
                          p_date_to      date,
                          p_user_id      number);

  procedure save_cdd_item_doc_ref(p_source_document_table varchar2,
                                  p_source_document_id    number,
                                  p_to_document_table     varchar2,
                                  p_to_document_id        number,
                                  p_user_id               number);
  --FUNCTION get_inception_of_lease(p_inception_of_lease DATE) RETURN DATE;
  --是否逾期  违约金状态
  function get_if_overdue(p_cashflow_id number) return varchar2;
  --获取担保人
  function get_guarantor(p_contract_id number) return varchar2;

  procedure default_idtype_idcode_mobile(p_bp_id_tenant number,
                                         p_user_id      number,
                                         p_idtype       out varchar2,
                                         p_idtype_n     out varchar2,
                                         p_idcode       out varchar2,
                                         p_mobile       out varchar2);
  --得到产品是否自定义
  procedure get_if_self_definition(p_product_id         number,
                                   p_user_id            number,
                                   p_if_self_definition out varchar2);
  function get_first_contract_day(p_contract_id number) return varchar2;

  procedure update_send_info(p_contract_id      number,
                             p_cert_recipient01 varchar2,
                             p_cert_tracknum01  varchar2,
                             p_cert_send_date01 date,
                             
                             p_cert_recipient02 varchar2,
                             p_cert_tracknum02  varchar2,
                             p_cert_send_date02 date,
                             
                             p_trans_recipient01 varchar2,
                             p_trans_tracknum01  varchar2,
                             p_trans_send_date01 date,
                             
                             p_trans_recipient02 varchar2,
                             p_trans_tracknum02  varchar2,
                             p_trans_send_date02 date,
                             p_note              varchar2,
                             p_user_id           number);
  function get_inception_of_lease(p_date date) return date;
  function get_inception_of_lease_8(p_date date) return date;
  procedure delete_con_contract_bp(p_record_id number, p_user_id number);
  procedure update_contract_info_sign(p_contract_id number);
end con_contract_pkg;
/
create or replace package body con_contract_pkg is
  g_document_category constant varchar2(100) := 'CONTRACT';
  e_get_contract_number_err    exception;
  e_bp_id_tenant_err           exception;
  e_quotation_created_err      exception;
  e_quotation_status_err       exception;
  e_calculator_not_success_err exception;
  e_calc_method_notfound_err   exception;

  e_lock_table exception;
  pragma exception_init(e_lock_table, -54);

  function get_contract_id return number is
    v_id number;
  begin
    select con_contract_s.nextval into v_id from dual;
    --dbms_output.put_line(v_id);
    return v_id;
  
  end;

  function get_quotation_id return number is
    v_id number;
  begin
    select prj_quotation_s.nextval into v_id from dual;
    return v_id;
  end;

  function get_contract_rec(p_contract_id number, p_user_id number)
    return con_contract%rowtype is
    v_con_contract_rec con_contract%rowtype;
  begin
    select *
      into v_con_contract_rec
      from con_contract t
     where t.contract_id = p_contract_id
       for update nowait;
    return v_con_contract_rec;
  exception
    when e_lock_table then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_LOCK_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'con_contract_pkg',
                                                      p_procedure_function_name => 'get_contract_rec');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  procedure update_contract_bp_seq(p_contract_id number,
                                   p_bp_category varchar2) is
    v_num number := 1;
  begin
    for c_bps in (select *
                    from con_contract_bp cb
                   where cb.contract_id = p_contract_id
                     and cb.bp_category = p_bp_category
                     and cb.bp_type not in ('GUTA_SP') --排除配偶担保
                   order by cb.record_id) loop
      update con_contract_bp b
         set b.bp_seq = v_num
       where b.record_id = c_bps.record_id;
      v_num := v_num + 1;
    end loop;
  end update_contract_bp_seq;

  function get_cashflow_rec(p_cashflow_id number, p_user_id number)
    return con_contract_cashflow%rowtype is
    v_con_cashflow_rec con_contract_cashflow%rowtype;
  begin
    select *
      into v_con_cashflow_rec
      from con_contract_cashflow t
     where t.cashflow_id = p_cashflow_id
       for update nowait;
    return v_con_cashflow_rec;
  exception
    when e_lock_table then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_LOCK_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'con_contract_pkg',
                                                      p_procedure_function_name => 'get_contract_rec');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  function get_contract_number(p_document_type varchar2,
                               p_document_date date,
                               p_company_id    number,
                               p_user_id       number,
                               p_contract_id   number default null)
    return varchar2 is
    v_no            con_contract.contract_number%type;
    v_agent_bp_code varchar2(30);
    v_price_ref_v05 varchar2(200);
    v_product_code  varchar2(2000);
    product_code_err exception;
  begin
    begin
      select hbm.bp_code
        into v_agent_bp_code
        from con_contract_bp t, hls_bp_master hbm
       where t.contract_id = p_contract_id
         and t.bp_category = 'AGENT'
         and t.enabled_flag = 'Y'
         and t.bp_id = hbm.bp_id
         and rownum = 1;
    exception
      when no_data_found then
        null;
    end;
  
    begin
      select l.ref_v05
        into v_price_ref_v05
        from con_contract t, hls_price_list l
       where t.contract_id = p_contract_id
         and t.price_list = l.price_list;
    exception
      when no_data_found then
        null;
    end;
  
    begin
      select hp.product_code
        into v_product_code
        from con_contract_lease_item_lv ccl, hls_product_plan_definition hp
       where ccl.contract_id = p_contract_id
         and ccl.product_plan_id = hp.product_plan_id;
    exception
      when no_data_found then
        raise product_code_err;
      when product_code_err then
        sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.GET_CONTRACT_NUMBER',
                                                        p_created_by              => p_user_id,
                                                        p_package_name            => 'CON_CONTRACT_PKG',
                                                        p_procedure_function_name => 'get_contract_number');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      
    end;
  
    v_no := fnd_code_rule_pkg.get_rule_next_auto_num(p_document_category => 'CONTRACT',
                                                     p_document_type     => p_document_type,
                                                     p_company_id        => p_company_id,
                                                     p_operation_unit_id => null,
                                                     p_operation_date    => p_document_date,
                                                     p_agent_code        => v_agent_bp_code,
                                                     p_price_ref_v05     => v_price_ref_v05,
                                                     p_product_code      => v_product_code,
                                                     p_created_by        => p_user_id);
  
    if v_no = fnd_code_rule_pkg.c_error then
      raise e_get_contract_number_err;
    end if;
  
    return v_no;
  
  exception
    when e_get_contract_number_err then
    
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '获取编码失败,请联系系统管理员!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'get_contract_number');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  procedure insert_contract_cashflow(p_contract_cashflow_rec con_contract_cashflow%rowtype) is
  begin
  
    insert into con_contract_cashflow values p_contract_cashflow_rec;
    /*exception when dup_val_on_index then
      haha_test_pkg.log(p_contract_cashflow_rec.times || '-' ||p_contract_cashflow_rec.cf_item);
    */
  end;

  procedure update_contract_cashflow(p_contract_cashflow_rec con_contract_cashflow%rowtype) is
  begin
    update con_contract_cashflow
       set row = p_contract_cashflow_rec
     where cashflow_id = p_contract_cashflow_rec.cashflow_id;
  end;

  procedure insert_rental_cashflow(p_calculator_ln_rec hls_fin_calculator_ln%rowtype,
                                   p_price_list        varchar2,
                                   p_contract_id       number,
                                   p_company_id        number,
                                   p_user_id           number) is
    v_contract_cashflow_rec con_contract_cashflow%rowtype;
  
    v_cf_item                  hls_cashflow_item.cf_item%type;
    v_cf_type                  hls_cashflow_item.cf_type%type;
    v_cf_direction             hls_cashflow_item.cf_direction%type;
    v_cf_status                con_contract_cashflow.cf_status%type;
    v_con_contract_cashflow_id number;
  begin
    begin
      select b.cf_item,
             b.cf_type,
             b.cf_direction,
             nvl(c.default_cf_status, 'RELEASE')
        into v_cf_item, v_cf_type, v_cf_direction, v_cf_status
        from hls_fin_calc_config       a,
             hls_cashflow_item         b,
             hls_cashflow_item_company c
       where a.price_list = p_price_list
         and a.column_name = 'RENTAL'
         and a.cf_item = b.cf_item
         and c.company_id(+) = p_company_id
         and b.cf_item = c.cf_item(+);
    exception
      when no_data_found then
        return;
    end;
  
    --cf_direction
  
    begin
      select *
        into v_contract_cashflow_rec
        from con_contract_cashflow
       where contract_id = p_contract_id
         and times = p_calculator_ln_rec.times
         and cf_item = v_cf_item
      --and generated_source = 'CALCULATOR'
      --and calc_line_id = p_calculator_ln_rec.calc_line_id
      ;
    
      v_contract_cashflow_rec.times                       := p_calculator_ln_rec.times; --期数
      v_contract_cashflow_rec.calc_date                   := to_date(p_calculator_ln_rec.calc_date,
                                                                     'YYYY-MM-DD'); --计算日
      v_contract_cashflow_rec.due_date                    := to_date(p_calculator_ln_rec.due_date,
                                                                     'YYYY-MM-DD'); --到期日
      v_contract_cashflow_rec.fin_income_date             := to_date(p_calculator_ln_rec.fin_income_date,
                                                                     'YYYY-MM-DD'); --收入计算日
      v_contract_cashflow_rec.due_amount                  := nvl(p_calculator_ln_rec.rental,
                                                                 0); --租金
      v_contract_cashflow_rec.principal                   := nvl(p_calculator_ln_rec.principal,
                                                                 0); --本金
      v_contract_cashflow_rec.interest                    := nvl(p_calculator_ln_rec.interest,
                                                                 0); --利息
      v_contract_cashflow_rec.outstanding_rental          := p_calculator_ln_rec.outstanding_rental; --剩余租金
      v_contract_cashflow_rec.outstanding_principal       := p_calculator_ln_rec.outstanding_principal; --剩余本金
      v_contract_cashflow_rec.outstanding_interest        := p_calculator_ln_rec.outstanding_interest; --剩余利息
      v_contract_cashflow_rec.interest_accrual_balance    := p_calculator_ln_rec.interest_accrual_balance; --计息余额
      v_contract_cashflow_rec.principal_implicit_rate     := p_calculator_ln_rec.principal_implicit_rate; --实际利率法本金
      v_contract_cashflow_rec.interest_implicit_rate      := p_calculator_ln_rec.interest_implicit_rate; --实际利率法利息
      v_contract_cashflow_rec.accumulated_unpaid_interest := p_calculator_ln_rec.accumulated_unpaid_interest; --累计未偿还利息
      v_contract_cashflow_rec.interest_period_days        := p_calculator_ln_rec.interest_period_days; --计息天数(利息期天数)
      v_contract_cashflow_rec.discounting_days            := p_calculator_ln_rec.discounting_days; --贴现天数
      v_contract_cashflow_rec.vat_due_amount              := nvl(p_calculator_ln_rec.vat_rental,
                                                                 0);
      v_contract_cashflow_rec.vat_principal               := nvl(p_calculator_ln_rec.vat_principal,
                                                                 0);
      v_contract_cashflow_rec.vat_interest                := nvl(p_calculator_ln_rec.vat_interest,
                                                                 0);
      v_contract_cashflow_rec.vat_principal_implicit      := p_calculator_ln_rec.vat_principal_implicit; --实际利率法本金增值税额
      v_contract_cashflow_rec.vat_interest_implicit       := p_calculator_ln_rec.vat_interest_implicit; --实际利率法利息增值税额
      v_contract_cashflow_rec.net_due_amount              := nvl(p_calculator_ln_rec.net_rental,
                                                                 0);
      v_contract_cashflow_rec.net_principal               := nvl(p_calculator_ln_rec.net_principal,
                                                                 0);
      v_contract_cashflow_rec.net_interest                := nvl(p_calculator_ln_rec.net_interest,
                                                                 0);
      v_contract_cashflow_rec.net_principal_implicit      := p_calculator_ln_rec.net_principal_implicit; --实际利率法税后本金
      v_contract_cashflow_rec.net_interest_implicit       := p_calculator_ln_rec.net_interest_implicit; --实际利率法税后利息
      v_contract_cashflow_rec.fix_principal_flag          := p_calculator_ln_rec.fix_principal_flag; --固定本金标志
      v_contract_cashflow_rec.fix_rental_flag             := p_calculator_ln_rec.fix_rental_flag; --固定租金标志
      v_contract_cashflow_rec.interest_only_flag          := p_calculator_ln_rec.interest_only_flag; --仅支付利息
      v_contract_cashflow_rec.equal_flag                  := p_calculator_ln_rec.equal_flag;
      v_contract_cashflow_rec.manual_flag                 := p_calculator_ln_rec.manual_flag;
      v_contract_cashflow_rec.beginning_of_lease_year     := p_calculator_ln_rec.beginning_of_lease_year; --每个租赁年度的开始
      v_contract_cashflow_rec.salestax                    := p_calculator_ln_rec.salestax; --营业税
    
      --用户字段日期1
      if p_calculator_ln_rec.ln_user_col_d01 is not null then
        v_contract_cashflow_rec.ln_user_col_d01 := to_date(p_calculator_ln_rec.ln_user_col_d01,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d01 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d02 is not null then
        v_contract_cashflow_rec.ln_user_col_d02 := to_date(p_calculator_ln_rec.ln_user_col_d02,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d02 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d03 is not null then
        v_contract_cashflow_rec.ln_user_col_d03 := to_date(p_calculator_ln_rec.ln_user_col_d03,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d03 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d04 is not null then
        v_contract_cashflow_rec.ln_user_col_d04 := to_date(p_calculator_ln_rec.ln_user_col_d04,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d04 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d05 is not null then
        v_contract_cashflow_rec.ln_user_col_d05 := to_date(p_calculator_ln_rec.ln_user_col_d05,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d05 := null;
      end if;
      v_contract_cashflow_rec.ln_user_col_v01       := p_calculator_ln_rec.ln_user_col_v01; --用户字段字符1
      v_contract_cashflow_rec.ln_user_col_v02       := p_calculator_ln_rec.ln_user_col_v02;
      v_contract_cashflow_rec.ln_user_col_v03       := p_calculator_ln_rec.ln_user_col_v03;
      v_contract_cashflow_rec.ln_user_col_v04       := p_calculator_ln_rec.ln_user_col_v04;
      v_contract_cashflow_rec.ln_user_col_v05       := p_calculator_ln_rec.ln_user_col_v05;
      v_contract_cashflow_rec.ln_user_col_n01       := p_calculator_ln_rec.ln_user_col_n01; --用户字段数字1
      v_contract_cashflow_rec.ln_user_col_n02       := p_calculator_ln_rec.ln_user_col_n02;
      v_contract_cashflow_rec.ln_user_col_n03       := p_calculator_ln_rec.ln_user_col_n03;
      v_contract_cashflow_rec.ln_user_col_n04       := p_calculator_ln_rec.ln_user_col_n04;
      v_contract_cashflow_rec.ln_user_col_n05       := p_calculator_ln_rec.ln_user_col_n05;
      v_contract_cashflow_rec.ln_user_col_n06       := p_calculator_ln_rec.ln_user_col_n06;
      v_contract_cashflow_rec.ln_user_col_n07       := p_calculator_ln_rec.ln_user_col_n07;
      v_contract_cashflow_rec.ln_user_col_n08       := p_calculator_ln_rec.ln_user_col_n08;
      v_contract_cashflow_rec.ln_user_col_n09       := p_calculator_ln_rec.ln_user_col_n09;
      v_contract_cashflow_rec.ln_user_col_n10       := p_calculator_ln_rec.ln_user_col_n10;
      v_contract_cashflow_rec.ln_user_col_n11       := p_calculator_ln_rec.ln_user_col_n11;
      v_contract_cashflow_rec.ln_user_col_n12       := p_calculator_ln_rec.ln_user_col_n12;
      v_contract_cashflow_rec.ln_user_col_n13       := p_calculator_ln_rec.ln_user_col_n13;
      v_contract_cashflow_rec.ln_user_col_n14       := p_calculator_ln_rec.ln_user_col_n14;
      v_contract_cashflow_rec.ln_user_col_n15       := p_calculator_ln_rec.ln_user_col_n15;
      v_contract_cashflow_rec.ln_user_col_n16       := p_calculator_ln_rec.ln_user_col_n16;
      v_contract_cashflow_rec.ln_user_col_n17       := p_calculator_ln_rec.ln_user_col_n17;
      v_contract_cashflow_rec.ln_user_col_n18       := p_calculator_ln_rec.ln_user_col_n18;
      v_contract_cashflow_rec.ln_user_col_n19       := p_calculator_ln_rec.ln_user_col_n19;
      v_contract_cashflow_rec.ln_user_col_n20       := p_calculator_ln_rec.ln_user_col_n20;
      v_contract_cashflow_rec.rental_eq_pymt_raw    := p_calculator_ln_rec.rental_eq_pymt_raw; --初始租金(等额)
      v_contract_cashflow_rec.rental_eq_pymt_adj    := p_calculator_ln_rec.rental_eq_pymt_adj; --调整租金(等额)
      v_contract_cashflow_rec.interest_eq_pymt_raw  := p_calculator_ln_rec.interest_eq_pymt_raw; --初始利息(等额)
      v_contract_cashflow_rec.interest_eq_pymt_adj  := p_calculator_ln_rec.interest_eq_pymt_adj; --调整利息(等额)
      v_contract_cashflow_rec.principal_eq_pymt_raw := p_calculator_ln_rec.principal_eq_pymt_raw; --初始本金(等额)
      v_contract_cashflow_rec.principal_eq_pymt_adj := p_calculator_ln_rec.principal_eq_pymt_adj; --调整本金(等额)
      v_contract_cashflow_rec.rental_eq_prin_raw    := p_calculator_ln_rec.rental_eq_prin_raw; --初始租金(等本)
      v_contract_cashflow_rec.rental_eq_prin_adj    := p_calculator_ln_rec.rental_eq_prin_adj; --调整租金(等本)
      v_contract_cashflow_rec.interest_eq_prin_raw  := p_calculator_ln_rec.interest_eq_prin_raw; --初始利息(等本)
      v_contract_cashflow_rec.interest_eq_prin_adj  := p_calculator_ln_rec.interest_eq_prin_adj; --调整利息(等本)
      v_contract_cashflow_rec.principal_eq_prin_raw := p_calculator_ln_rec.principal_eq_prin_raw; --初始本金(等本)
      v_contract_cashflow_rec.principal_eq_prin_adj := p_calculator_ln_rec.principal_eq_prin_adj; --调整本金(等本)
    
      v_contract_cashflow_rec.outstanding_rental_tax_incld  := p_calculator_ln_rec.outstanding_rental_tax_incld;
      v_contract_cashflow_rec.outstanding_prin_tax_incld    := p_calculator_ln_rec.outstanding_prin_tax_incld;
      v_contract_cashflow_rec.outstanding_int_tax_incld     := p_calculator_ln_rec.outstanding_int_tax_incld;
      v_contract_cashflow_rec.interest_accrual_bal_tax_incl := p_calculator_ln_rec.interest_accrual_bal_tax_incl;
      v_contract_cashflow_rec.accumulated_unpd_int_tax_incl := p_calculator_ln_rec.accumulated_unpd_int_tax_incl;
      v_contract_cashflow_rec.exchange_rate_type            := p_calculator_ln_rec.exchange_rate_type;
      v_contract_cashflow_rec.exchange_rate_quotation       := p_calculator_ln_rec.exchange_rate_quotation;
      v_contract_cashflow_rec.exchange_rate                 := p_calculator_ln_rec.exchange_rate;
      v_contract_cashflow_rec.due_amount_cny                := p_calculator_ln_rec.due_amount_cny;
      v_contract_cashflow_rec.main_business_income          := p_calculator_ln_rec.main_business_income;
      v_contract_cashflow_rec.main_business_cost            := p_calculator_ln_rec.main_business_cost;
      v_contract_cashflow_rec.financing_cost                := p_calculator_ln_rec.financing_cost;
    
      v_contract_cashflow_rec.last_updated_by  := p_user_id; --当前用户
      v_contract_cashflow_rec.last_update_date := sysdate; --系统时间
    
      -- liukang
      v_contract_cashflow_rec.payment_deduction_flag   := 'Y';
      v_contract_cashflow_rec.payment_deduction_amount := v_contract_cashflow_rec.due_amount;
      --add by chenlingfeng 2019年4月9日
      v_contract_cashflow_rec.tax_type_id   := p_calculator_ln_rec.tax_type_id;
      v_contract_cashflow_rec.tax_type_rate := p_calculator_ln_rec.tax_type_rate;
      update_contract_cashflow(p_contract_cashflow_rec => v_contract_cashflow_rec);
    
    exception
      when no_data_found then
        select con_contract_cashflow_s.nextval
          into v_con_contract_cashflow_id
          from dual;
        v_contract_cashflow_rec.cashflow_id                 := v_con_contract_cashflow_id;
        v_contract_cashflow_rec.contract_id                 := p_contract_id; --当前合同ID
        v_contract_cashflow_rec.cf_item                     := v_cf_item; --计算器行表的列,所配置的现金流项目
        v_contract_cashflow_rec.cf_type                     := v_cf_type; -- 根据现金流项目取得现金流类型
        v_contract_cashflow_rec.cf_direction                := v_cf_direction;
        v_contract_cashflow_rec.cf_status                   := v_cf_status;
        v_contract_cashflow_rec.times                       := p_calculator_ln_rec.times; --期数
        v_contract_cashflow_rec.calc_date                   := to_date(p_calculator_ln_rec.calc_date,
                                                                       'YYYY-MM-DD'); --计算日
        v_contract_cashflow_rec.due_date                    := to_date(p_calculator_ln_rec.due_date,
                                                                       'YYYY-MM-DD'); --到期日
        v_contract_cashflow_rec.fin_income_date             := to_date(p_calculator_ln_rec.fin_income_date,
                                                                       'YYYY-MM-DD'); --收入计算日
        v_contract_cashflow_rec.due_amount                  := p_calculator_ln_rec.rental; --租金
        v_contract_cashflow_rec.principal                   := p_calculator_ln_rec.principal; --本金
        v_contract_cashflow_rec.interest                    := p_calculator_ln_rec.interest; --利息
        v_contract_cashflow_rec.outstanding_rental          := p_calculator_ln_rec.outstanding_rental; --剩余租金
        v_contract_cashflow_rec.outstanding_principal       := p_calculator_ln_rec.outstanding_principal; --剩余本金
        v_contract_cashflow_rec.outstanding_interest        := p_calculator_ln_rec.outstanding_interest; --剩余利息
        v_contract_cashflow_rec.interest_accrual_balance    := p_calculator_ln_rec.interest_accrual_balance; --计息余额
        v_contract_cashflow_rec.principal_implicit_rate     := p_calculator_ln_rec.principal_implicit_rate; --实际利率法本金
        v_contract_cashflow_rec.interest_implicit_rate      := p_calculator_ln_rec.interest_implicit_rate; --实际利率法利息
        v_contract_cashflow_rec.accumulated_unpaid_interest := p_calculator_ln_rec.accumulated_unpaid_interest; --累计未偿还利息
        v_contract_cashflow_rec.interest_period_days        := p_calculator_ln_rec.interest_period_days; --计息天数(利息期天数)
        v_contract_cashflow_rec.discounting_days            := p_calculator_ln_rec.discounting_days; --贴现天数
        v_contract_cashflow_rec.vat_due_amount              := p_calculator_ln_rec.vat_rental;
        v_contract_cashflow_rec.vat_principal               := p_calculator_ln_rec.vat_principal;
        v_contract_cashflow_rec.vat_interest                := p_calculator_ln_rec.vat_interest;
        v_contract_cashflow_rec.vat_principal_implicit      := p_calculator_ln_rec.vat_principal_implicit; --实际利率法本金增值税额
        v_contract_cashflow_rec.vat_interest_implicit       := p_calculator_ln_rec.vat_interest_implicit; --实际利率法利息增值税额
        v_contract_cashflow_rec.net_due_amount              := p_calculator_ln_rec.net_rental;
        v_contract_cashflow_rec.net_principal               := p_calculator_ln_rec.net_principal;
        v_contract_cashflow_rec.net_interest                := p_calculator_ln_rec.net_interest;
        v_contract_cashflow_rec.net_principal_implicit      := p_calculator_ln_rec.net_principal_implicit; --实际利率法税后本金
        v_contract_cashflow_rec.net_interest_implicit       := p_calculator_ln_rec.net_interest_implicit; --实际利率法税后利息
        v_contract_cashflow_rec.fix_principal_flag          := p_calculator_ln_rec.fix_principal_flag; --固定本金标志
        v_contract_cashflow_rec.fix_rental_flag             := p_calculator_ln_rec.fix_rental_flag; --固定租金标志
        v_contract_cashflow_rec.interest_only_flag          := p_calculator_ln_rec.interest_only_flag; --仅支付利息
        v_contract_cashflow_rec.equal_flag                  := p_calculator_ln_rec.equal_flag;
        v_contract_cashflow_rec.manual_flag                 := p_calculator_ln_rec.manual_flag;
        v_contract_cashflow_rec.beginning_of_lease_year     := p_calculator_ln_rec.beginning_of_lease_year; --每个租赁年度的开始
        v_contract_cashflow_rec.salestax                    := p_calculator_ln_rec.salestax; --营业税
        v_contract_cashflow_rec.generated_source            := 'CALCULATOR'; --现金流由计算器创建
        v_contract_cashflow_rec.calc_line_id                := p_calculator_ln_rec.calc_line_id;
        v_contract_cashflow_rec.overdue_status              := 'N';
        v_contract_cashflow_rec.overdue_book_date           := null;
        v_contract_cashflow_rec.overdue_amount              := null;
        v_contract_cashflow_rec.overdue_principal           := null;
        v_contract_cashflow_rec.overdue_interest            := null;
        v_contract_cashflow_rec.overdue_remark              := null;
        v_contract_cashflow_rec.received_amount             := null;
        v_contract_cashflow_rec.received_principal          := null;
        v_contract_cashflow_rec.received_interest           := null;
        v_contract_cashflow_rec.write_off_flag              := 'NOT';
        v_contract_cashflow_rec.last_received_date          := null;
        v_contract_cashflow_rec.full_write_off_date         := null;
        v_contract_cashflow_rec.penalty_process_status      := 'NORMAL';
        v_contract_cashflow_rec.billing_status              := 'NOT';
        --用户字段日期1
        if p_calculator_ln_rec.ln_user_col_d01 is not null then
          v_contract_cashflow_rec.ln_user_col_d01 := to_date(p_calculator_ln_rec.ln_user_col_d01,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d01 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d02 is not null then
          v_contract_cashflow_rec.ln_user_col_d02 := to_date(p_calculator_ln_rec.ln_user_col_d02,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d02 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d03 is not null then
          v_contract_cashflow_rec.ln_user_col_d03 := to_date(p_calculator_ln_rec.ln_user_col_d03,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d03 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d04 is not null then
          v_contract_cashflow_rec.ln_user_col_d04 := to_date(p_calculator_ln_rec.ln_user_col_d04,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d04 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d05 is not null then
          v_contract_cashflow_rec.ln_user_col_d05 := to_date(p_calculator_ln_rec.ln_user_col_d05,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d05 := null;
        end if;
        v_contract_cashflow_rec.ln_user_col_v01               := p_calculator_ln_rec.ln_user_col_v01; --用户字段字符1
        v_contract_cashflow_rec.ln_user_col_v02               := p_calculator_ln_rec.ln_user_col_v02;
        v_contract_cashflow_rec.ln_user_col_v03               := p_calculator_ln_rec.ln_user_col_v03;
        v_contract_cashflow_rec.ln_user_col_v04               := p_calculator_ln_rec.ln_user_col_v04;
        v_contract_cashflow_rec.ln_user_col_v05               := p_calculator_ln_rec.ln_user_col_v05;
        v_contract_cashflow_rec.ln_user_col_n01               := p_calculator_ln_rec.ln_user_col_n01; --用户字段数字1
        v_contract_cashflow_rec.ln_user_col_n02               := p_calculator_ln_rec.ln_user_col_n02;
        v_contract_cashflow_rec.ln_user_col_n03               := p_calculator_ln_rec.ln_user_col_n03;
        v_contract_cashflow_rec.ln_user_col_n04               := p_calculator_ln_rec.ln_user_col_n04;
        v_contract_cashflow_rec.ln_user_col_n05               := p_calculator_ln_rec.ln_user_col_n05;
        v_contract_cashflow_rec.ln_user_col_n06               := p_calculator_ln_rec.ln_user_col_n06;
        v_contract_cashflow_rec.ln_user_col_n07               := p_calculator_ln_rec.ln_user_col_n07;
        v_contract_cashflow_rec.ln_user_col_n08               := p_calculator_ln_rec.ln_user_col_n08;
        v_contract_cashflow_rec.ln_user_col_n09               := p_calculator_ln_rec.ln_user_col_n09;
        v_contract_cashflow_rec.ln_user_col_n10               := p_calculator_ln_rec.ln_user_col_n10;
        v_contract_cashflow_rec.ln_user_col_n11               := p_calculator_ln_rec.ln_user_col_n11;
        v_contract_cashflow_rec.ln_user_col_n12               := p_calculator_ln_rec.ln_user_col_n12;
        v_contract_cashflow_rec.ln_user_col_n13               := p_calculator_ln_rec.ln_user_col_n13;
        v_contract_cashflow_rec.ln_user_col_n14               := p_calculator_ln_rec.ln_user_col_n14;
        v_contract_cashflow_rec.ln_user_col_n15               := p_calculator_ln_rec.ln_user_col_n15;
        v_contract_cashflow_rec.ln_user_col_n16               := p_calculator_ln_rec.ln_user_col_n16;
        v_contract_cashflow_rec.ln_user_col_n17               := p_calculator_ln_rec.ln_user_col_n17;
        v_contract_cashflow_rec.ln_user_col_n18               := p_calculator_ln_rec.ln_user_col_n18;
        v_contract_cashflow_rec.ln_user_col_n19               := p_calculator_ln_rec.ln_user_col_n19;
        v_contract_cashflow_rec.ln_user_col_n20               := p_calculator_ln_rec.ln_user_col_n20;
        v_contract_cashflow_rec.rental_eq_pymt_raw            := p_calculator_ln_rec.rental_eq_pymt_raw; --初始租金(等额)
        v_contract_cashflow_rec.rental_eq_pymt_adj            := p_calculator_ln_rec.rental_eq_pymt_adj; --调整租金(等额)
        v_contract_cashflow_rec.interest_eq_pymt_raw          := p_calculator_ln_rec.interest_eq_pymt_raw; --初始利息(等额)
        v_contract_cashflow_rec.interest_eq_pymt_adj          := p_calculator_ln_rec.interest_eq_pymt_adj; --调整利息(等额)
        v_contract_cashflow_rec.principal_eq_pymt_raw         := p_calculator_ln_rec.principal_eq_pymt_raw; --初始本金(等额)
        v_contract_cashflow_rec.principal_eq_pymt_adj         := p_calculator_ln_rec.principal_eq_pymt_adj; --调整本金(等额)
        v_contract_cashflow_rec.rental_eq_prin_raw            := p_calculator_ln_rec.rental_eq_prin_raw; --初始租金(等本)
        v_contract_cashflow_rec.rental_eq_prin_adj            := p_calculator_ln_rec.rental_eq_prin_adj; --调整租金(等本)
        v_contract_cashflow_rec.interest_eq_prin_raw          := p_calculator_ln_rec.interest_eq_prin_raw; --初始利息(等本)
        v_contract_cashflow_rec.interest_eq_prin_adj          := p_calculator_ln_rec.interest_eq_prin_adj; --调整利息(等本)
        v_contract_cashflow_rec.principal_eq_prin_raw         := p_calculator_ln_rec.principal_eq_prin_raw; --初始本金(等本)
        v_contract_cashflow_rec.principal_eq_prin_adj         := p_calculator_ln_rec.principal_eq_prin_adj; --调整本金(等本)
        v_contract_cashflow_rec.outstanding_rental_tax_incld  := p_calculator_ln_rec.outstanding_rental_tax_incld;
        v_contract_cashflow_rec.outstanding_prin_tax_incld    := p_calculator_ln_rec.outstanding_prin_tax_incld;
        v_contract_cashflow_rec.outstanding_int_tax_incld     := p_calculator_ln_rec.outstanding_int_tax_incld;
        v_contract_cashflow_rec.interest_accrual_bal_tax_incl := p_calculator_ln_rec.interest_accrual_bal_tax_incl;
        v_contract_cashflow_rec.accumulated_unpd_int_tax_incl := p_calculator_ln_rec.accumulated_unpd_int_tax_incl;
        v_contract_cashflow_rec.exchange_rate_type            := p_calculator_ln_rec.exchange_rate_type;
        v_contract_cashflow_rec.exchange_rate_quotation       := p_calculator_ln_rec.exchange_rate_quotation;
        v_contract_cashflow_rec.exchange_rate                 := p_calculator_ln_rec.exchange_rate;
        v_contract_cashflow_rec.due_amount_cny                := p_calculator_ln_rec.due_amount_cny;
        v_contract_cashflow_rec.main_business_income          := p_calculator_ln_rec.main_business_income;
        v_contract_cashflow_rec.main_business_cost            := p_calculator_ln_rec.main_business_cost;
        v_contract_cashflow_rec.financing_cost                := p_calculator_ln_rec.financing_cost;
      
        v_contract_cashflow_rec.created_by       := p_user_id; --当前用户
        v_contract_cashflow_rec.creation_date    := sysdate; --系统时间
        v_contract_cashflow_rec.last_updated_by  := p_user_id; --当前用户
        v_contract_cashflow_rec.last_update_date := sysdate; --系统时间
      
        -- liukang
        v_contract_cashflow_rec.payment_deduction_flag   := 'Y';
        v_contract_cashflow_rec.payment_deduction_amount := v_contract_cashflow_rec.due_amount;
        --add by chenlingfeng 2019年4月9日
        v_contract_cashflow_rec.tax_type_id   := p_calculator_ln_rec.tax_type_id;
        v_contract_cashflow_rec.tax_type_rate := p_calculator_ln_rec.tax_type_rate;
        insert_contract_cashflow(p_contract_cashflow_rec => v_contract_cashflow_rec);
    end;
  
  end;

  procedure insert_btb_cashflow(p_calculator_ln_rec hls_fin_calculator_ln%rowtype,
                                p_price_list        varchar2,
                                p_contract_id       number,
                                p_company_id        number,
                                p_user_id           number) is
    v_contract_cashflow_rec con_contract_cashflow%rowtype;
  
    v_cf_item                  hls_cashflow_item.cf_item%type;
    v_cf_type                  hls_cashflow_item.cf_type%type;
    v_cf_direction             hls_cashflow_item.cf_direction%type;
    v_cf_status                con_contract_cashflow.cf_status%type;
    v_con_contract_cashflow_id number;
  begin
    begin
      select b.cf_item,
             b.cf_type,
             b.cf_direction,
             nvl(c.default_cf_status, 'RELEASE')
        into v_cf_item, v_cf_type, v_cf_direction, v_cf_status
        from hls_fin_calc_config       a,
             hls_cashflow_item         b,
             hls_cashflow_item_company c
       where a.price_list = p_price_list
         and a.column_name = 'BTB_REPAYMENT'
         and a.cf_item = b.cf_item
         and c.company_id(+) = p_company_id
         and b.cf_item = c.cf_item(+);
    exception
      when no_data_found then
        return;
    end;
  
    begin
      select *
        into v_contract_cashflow_rec
        from con_contract_cashflow
       where contract_id = p_contract_id
         and times = p_calculator_ln_rec.times
         and cf_item = v_cf_item
      --and generated_source = 'CALCULATOR'
      --and calc_line_id = p_calculator_ln_rec.calc_line_id
      ;
    
      v_contract_cashflow_rec.times           := p_calculator_ln_rec.times; --期数
      v_contract_cashflow_rec.calc_date       := to_date(p_calculator_ln_rec.calc_date,
                                                         'YYYY-MM-DD'); --计算日
      v_contract_cashflow_rec.due_date        := to_date(p_calculator_ln_rec.due_date,
                                                         'YYYY-MM-DD'); --到期日
      v_contract_cashflow_rec.fin_income_date := to_date(p_calculator_ln_rec.fin_income_date,
                                                         'YYYY-MM-DD'); --收入计算日
      --btb fin_income_date 待考虑
      v_contract_cashflow_rec.due_amount                  := p_calculator_ln_rec.btb_repayment; -- (背靠背交易) 还款额
      v_contract_cashflow_rec.principal                   := p_calculator_ln_rec.btb_principal; -- (背靠背交易) 本金
      v_contract_cashflow_rec.interest                    := p_calculator_ln_rec.btb_interest; --(背靠背交易)利息
      v_contract_cashflow_rec.outstanding_rental          := p_calculator_ln_rec.outstanding_rental; --剩余租金
      v_contract_cashflow_rec.outstanding_principal       := p_calculator_ln_rec.outstanding_principal; --剩余本金
      v_contract_cashflow_rec.outstanding_interest        := p_calculator_ln_rec.outstanding_interest; --剩余利息
      v_contract_cashflow_rec.interest_accrual_balance    := p_calculator_ln_rec.interest_accrual_balance; --计息余额
      v_contract_cashflow_rec.principal_implicit_rate     := null; --实际利率法本金
      v_contract_cashflow_rec.interest_implicit_rate      := null; --实际利率法利息
      v_contract_cashflow_rec.accumulated_unpaid_interest := null; --累计未偿还利息
      v_contract_cashflow_rec.interest_period_days        := null; --计息天数(利息期天数)
      v_contract_cashflow_rec.discounting_days            := p_calculator_ln_rec.btb_discounting_days; -- (背靠背交易) 贴现天数
      v_contract_cashflow_rec.vat_due_amount              := p_calculator_ln_rec.btb_vat_repayment; -- (背靠背交易)
      v_contract_cashflow_rec.vat_principal               := p_calculator_ln_rec.btb_vat_principal;
      v_contract_cashflow_rec.vat_interest                := p_calculator_ln_rec.btb_vat_interest;
      v_contract_cashflow_rec.vat_principal_implicit      := null; --实际利率法本金增值税额
      v_contract_cashflow_rec.vat_interest_implicit       := null; --实际利率法利息增值税额
      v_contract_cashflow_rec.net_due_amount              := null;
      v_contract_cashflow_rec.net_principal               := p_calculator_ln_rec.btb_principal_after_tax;
      v_contract_cashflow_rec.net_interest                := p_calculator_ln_rec.btb_interest_after_tax;
      v_contract_cashflow_rec.net_principal_implicit      := null; --实际利率法税后本金
      v_contract_cashflow_rec.net_interest_implicit       := null; --实际利率法税后利息
      v_contract_cashflow_rec.fix_principal_flag          := p_calculator_ln_rec.fix_principal_flag; --固定本金标志
      v_contract_cashflow_rec.fix_rental_flag             := p_calculator_ln_rec.fix_rental_flag; --固定租金标志
      v_contract_cashflow_rec.interest_only_flag          := p_calculator_ln_rec.interest_only_flag; --仅支付利息
      v_contract_cashflow_rec.equal_flag                  := p_calculator_ln_rec.equal_flag;
      v_contract_cashflow_rec.manual_flag                 := p_calculator_ln_rec.manual_flag;
      v_contract_cashflow_rec.beginning_of_lease_year     := p_calculator_ln_rec.beginning_of_lease_year; --每个租赁年度的开始
      v_contract_cashflow_rec.salestax                    := p_calculator_ln_rec.salestax; --营业税
    
      --用户字段日期1
      if p_calculator_ln_rec.ln_user_col_d01 is not null then
        v_contract_cashflow_rec.ln_user_col_d01 := to_date(p_calculator_ln_rec.ln_user_col_d01,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d01 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d02 is not null then
        v_contract_cashflow_rec.ln_user_col_d02 := to_date(p_calculator_ln_rec.ln_user_col_d02,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d02 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d03 is not null then
        v_contract_cashflow_rec.ln_user_col_d03 := to_date(p_calculator_ln_rec.ln_user_col_d03,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d03 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d04 is not null then
        v_contract_cashflow_rec.ln_user_col_d04 := to_date(p_calculator_ln_rec.ln_user_col_d04,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d04 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d05 is not null then
        v_contract_cashflow_rec.ln_user_col_d05 := to_date(p_calculator_ln_rec.ln_user_col_d05,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d05 := null;
      end if;
      v_contract_cashflow_rec.ln_user_col_v01 := p_calculator_ln_rec.ln_user_col_v01; --用户字段字符1
      v_contract_cashflow_rec.ln_user_col_v02 := p_calculator_ln_rec.ln_user_col_v02;
      v_contract_cashflow_rec.ln_user_col_v03 := p_calculator_ln_rec.ln_user_col_v03;
      v_contract_cashflow_rec.ln_user_col_v04 := p_calculator_ln_rec.ln_user_col_v04;
      v_contract_cashflow_rec.ln_user_col_v05 := p_calculator_ln_rec.ln_user_col_v05;
      v_contract_cashflow_rec.ln_user_col_n01 := p_calculator_ln_rec.ln_user_col_n01; --用户字段数字1
      v_contract_cashflow_rec.ln_user_col_n02 := p_calculator_ln_rec.ln_user_col_n02;
      v_contract_cashflow_rec.ln_user_col_n03 := p_calculator_ln_rec.ln_user_col_n03;
      v_contract_cashflow_rec.ln_user_col_n04 := p_calculator_ln_rec.ln_user_col_n04;
      v_contract_cashflow_rec.ln_user_col_n05 := p_calculator_ln_rec.ln_user_col_n05;
      v_contract_cashflow_rec.ln_user_col_n06 := p_calculator_ln_rec.ln_user_col_n06;
      v_contract_cashflow_rec.ln_user_col_n07 := p_calculator_ln_rec.ln_user_col_n07;
      v_contract_cashflow_rec.ln_user_col_n08 := p_calculator_ln_rec.ln_user_col_n08;
      v_contract_cashflow_rec.ln_user_col_n09 := p_calculator_ln_rec.ln_user_col_n09;
      v_contract_cashflow_rec.ln_user_col_n10 := p_calculator_ln_rec.ln_user_col_n10;
      v_contract_cashflow_rec.ln_user_col_n11 := p_calculator_ln_rec.ln_user_col_n11;
      v_contract_cashflow_rec.ln_user_col_n12 := p_calculator_ln_rec.ln_user_col_n12;
      v_contract_cashflow_rec.ln_user_col_n13 := p_calculator_ln_rec.ln_user_col_n13;
      v_contract_cashflow_rec.ln_user_col_n14 := p_calculator_ln_rec.ln_user_col_n14;
      v_contract_cashflow_rec.ln_user_col_n15 := p_calculator_ln_rec.ln_user_col_n15;
      v_contract_cashflow_rec.ln_user_col_n16 := p_calculator_ln_rec.ln_user_col_n16;
      v_contract_cashflow_rec.ln_user_col_n17 := p_calculator_ln_rec.ln_user_col_n17;
      v_contract_cashflow_rec.ln_user_col_n18 := p_calculator_ln_rec.ln_user_col_n18;
      v_contract_cashflow_rec.ln_user_col_n19 := p_calculator_ln_rec.ln_user_col_n19;
      v_contract_cashflow_rec.ln_user_col_n20 := p_calculator_ln_rec.ln_user_col_n20;
    
      v_contract_cashflow_rec.outstanding_rental_tax_incld  := p_calculator_ln_rec.outstanding_rental_tax_incld;
      v_contract_cashflow_rec.outstanding_prin_tax_incld    := p_calculator_ln_rec.outstanding_prin_tax_incld;
      v_contract_cashflow_rec.outstanding_int_tax_incld     := p_calculator_ln_rec.outstanding_int_tax_incld;
      v_contract_cashflow_rec.interest_accrual_bal_tax_incl := p_calculator_ln_rec.interest_accrual_bal_tax_incl;
      v_contract_cashflow_rec.accumulated_unpd_int_tax_incl := p_calculator_ln_rec.accumulated_unpd_int_tax_incl;
      v_contract_cashflow_rec.exchange_rate_type            := p_calculator_ln_rec.exchange_rate_type;
      v_contract_cashflow_rec.exchange_rate_quotation       := p_calculator_ln_rec.exchange_rate_quotation;
      v_contract_cashflow_rec.exchange_rate                 := p_calculator_ln_rec.exchange_rate;
      v_contract_cashflow_rec.due_amount_cny                := p_calculator_ln_rec.due_amount_cny;
      v_contract_cashflow_rec.main_business_income          := p_calculator_ln_rec.main_business_income;
      v_contract_cashflow_rec.main_business_cost            := p_calculator_ln_rec.main_business_cost;
      v_contract_cashflow_rec.financing_cost                := p_calculator_ln_rec.financing_cost;
    
      v_contract_cashflow_rec.last_updated_by  := p_user_id; --当前用户
      v_contract_cashflow_rec.last_update_date := sysdate; --系统时间
    
      --liukang
      v_contract_cashflow_rec.payment_deduction_flag   := 'Y';
      v_contract_cashflow_rec.payment_deduction_amount := v_contract_cashflow_rec.due_amount;
    
      update_contract_cashflow(p_contract_cashflow_rec => v_contract_cashflow_rec);
    
    exception
      when no_data_found then
        select con_contract_cashflow_s.nextval
          into v_con_contract_cashflow_id
          from dual;
        v_contract_cashflow_rec.cashflow_id     := v_con_contract_cashflow_id;
        v_contract_cashflow_rec.contract_id     := p_contract_id; --当前合同ID
        v_contract_cashflow_rec.cf_item         := v_cf_item; --计算器行表的列,所配置的现金流项目
        v_contract_cashflow_rec.cf_type         := v_cf_type; -- 根据现金流项目取得现金流类型
        v_contract_cashflow_rec.cf_direction    := v_cf_direction;
        v_contract_cashflow_rec.cf_status       := v_cf_status;
        v_contract_cashflow_rec.times           := p_calculator_ln_rec.times; --期数
        v_contract_cashflow_rec.calc_date       := to_date(p_calculator_ln_rec.calc_date,
                                                           'YYYY-MM-DD'); --计算日
        v_contract_cashflow_rec.due_date        := to_date(p_calculator_ln_rec.due_date,
                                                           'YYYY-MM-DD'); --到期日
        v_contract_cashflow_rec.fin_income_date := to_date(p_calculator_ln_rec.fin_income_date,
                                                           'YYYY-MM-DD'); --收入计算日
        --btb fin_income_date 待考虑
      
        v_contract_cashflow_rec.due_amount                  := p_calculator_ln_rec.btb_repayment; -- (背靠背交易) 还款额
        v_contract_cashflow_rec.principal                   := p_calculator_ln_rec.btb_principal; -- (背靠背交易) 本金
        v_contract_cashflow_rec.interest                    := p_calculator_ln_rec.btb_interest; --(背靠背交易)利息
        v_contract_cashflow_rec.outstanding_rental          := p_calculator_ln_rec.outstanding_rental; --剩余租金
        v_contract_cashflow_rec.outstanding_principal       := p_calculator_ln_rec.outstanding_principal; --剩余本金
        v_contract_cashflow_rec.outstanding_interest        := p_calculator_ln_rec.outstanding_interest; --剩余利息
        v_contract_cashflow_rec.interest_accrual_balance    := p_calculator_ln_rec.interest_accrual_balance; --计息余额
        v_contract_cashflow_rec.principal_implicit_rate     := null; --实际利率法本金
        v_contract_cashflow_rec.interest_implicit_rate      := null; --实际利率法利息
        v_contract_cashflow_rec.accumulated_unpaid_interest := null; --累计未偿还利息
        v_contract_cashflow_rec.interest_period_days        := null; --计息天数(利息期天数)
        v_contract_cashflow_rec.discounting_days            := p_calculator_ln_rec.btb_discounting_days; -- (背靠背交易) 贴现天数
        v_contract_cashflow_rec.vat_due_amount              := p_calculator_ln_rec.btb_vat_repayment; -- (背靠背交易)
        v_contract_cashflow_rec.vat_principal               := p_calculator_ln_rec.btb_vat_principal;
        v_contract_cashflow_rec.vat_interest                := p_calculator_ln_rec.btb_vat_interest;
        v_contract_cashflow_rec.vat_principal_implicit      := null; --实际利率法本金增值税额
        v_contract_cashflow_rec.vat_interest_implicit       := null; --实际利率法利息增值税额
        v_contract_cashflow_rec.net_due_amount              := null;
        v_contract_cashflow_rec.net_principal               := p_calculator_ln_rec.btb_principal_after_tax;
        v_contract_cashflow_rec.net_interest                := p_calculator_ln_rec.btb_interest_after_tax;
        v_contract_cashflow_rec.net_principal_implicit      := null; --实际利率法税后本金
        v_contract_cashflow_rec.net_interest_implicit       := null; --实际利率法税后利息
        v_contract_cashflow_rec.fix_principal_flag          := p_calculator_ln_rec.fix_principal_flag; --固定本金标志
        v_contract_cashflow_rec.fix_rental_flag             := p_calculator_ln_rec.fix_rental_flag; --固定租金标志
        v_contract_cashflow_rec.interest_only_flag          := p_calculator_ln_rec.interest_only_flag; --仅支付利息
        v_contract_cashflow_rec.equal_flag                  := p_calculator_ln_rec.equal_flag;
        v_contract_cashflow_rec.manual_flag                 := p_calculator_ln_rec.manual_flag;
        v_contract_cashflow_rec.beginning_of_lease_year     := p_calculator_ln_rec.beginning_of_lease_year; --每个租赁年度的开始
        v_contract_cashflow_rec.salestax                    := p_calculator_ln_rec.salestax; --营业税
        v_contract_cashflow_rec.generated_source            := 'CALCULATOR'; --现金流由计算器创建
        v_contract_cashflow_rec.calc_line_id                := p_calculator_ln_rec.calc_line_id;
        v_contract_cashflow_rec.overdue_status              := 'N';
        v_contract_cashflow_rec.overdue_book_date           := null;
        v_contract_cashflow_rec.overdue_amount              := null;
        v_contract_cashflow_rec.overdue_principal           := null;
        v_contract_cashflow_rec.overdue_interest            := null;
        v_contract_cashflow_rec.overdue_remark              := null;
        v_contract_cashflow_rec.received_amount             := null;
        v_contract_cashflow_rec.received_principal          := null;
        v_contract_cashflow_rec.received_interest           := null;
        v_contract_cashflow_rec.write_off_flag              := 'NOT';
        v_contract_cashflow_rec.last_received_date          := null;
        v_contract_cashflow_rec.full_write_off_date         := null;
        v_contract_cashflow_rec.penalty_process_status      := 'NORMAL';
        v_contract_cashflow_rec.billing_status              := 'NOT';
        --用户字段日期1
        if p_calculator_ln_rec.ln_user_col_d01 is not null then
          v_contract_cashflow_rec.ln_user_col_d01 := to_date(p_calculator_ln_rec.ln_user_col_d01,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d01 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d02 is not null then
          v_contract_cashflow_rec.ln_user_col_d02 := to_date(p_calculator_ln_rec.ln_user_col_d02,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d02 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d03 is not null then
          v_contract_cashflow_rec.ln_user_col_d03 := to_date(p_calculator_ln_rec.ln_user_col_d03,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d03 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d04 is not null then
          v_contract_cashflow_rec.ln_user_col_d04 := to_date(p_calculator_ln_rec.ln_user_col_d04,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d04 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d05 is not null then
          v_contract_cashflow_rec.ln_user_col_d05 := to_date(p_calculator_ln_rec.ln_user_col_d05,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d05 := null;
        end if;
        v_contract_cashflow_rec.ln_user_col_v01       := p_calculator_ln_rec.ln_user_col_v01; --用户字段字符1
        v_contract_cashflow_rec.ln_user_col_v02       := p_calculator_ln_rec.ln_user_col_v02;
        v_contract_cashflow_rec.ln_user_col_v03       := p_calculator_ln_rec.ln_user_col_v03;
        v_contract_cashflow_rec.ln_user_col_v04       := p_calculator_ln_rec.ln_user_col_v04;
        v_contract_cashflow_rec.ln_user_col_v05       := p_calculator_ln_rec.ln_user_col_v05;
        v_contract_cashflow_rec.ln_user_col_n01       := p_calculator_ln_rec.ln_user_col_n01; --用户字段数字1
        v_contract_cashflow_rec.ln_user_col_n02       := p_calculator_ln_rec.ln_user_col_n02;
        v_contract_cashflow_rec.ln_user_col_n03       := p_calculator_ln_rec.ln_user_col_n03;
        v_contract_cashflow_rec.ln_user_col_n04       := p_calculator_ln_rec.ln_user_col_n04;
        v_contract_cashflow_rec.ln_user_col_n05       := p_calculator_ln_rec.ln_user_col_n05;
        v_contract_cashflow_rec.ln_user_col_n06       := p_calculator_ln_rec.ln_user_col_n06;
        v_contract_cashflow_rec.ln_user_col_n07       := p_calculator_ln_rec.ln_user_col_n07;
        v_contract_cashflow_rec.ln_user_col_n08       := p_calculator_ln_rec.ln_user_col_n08;
        v_contract_cashflow_rec.ln_user_col_n09       := p_calculator_ln_rec.ln_user_col_n09;
        v_contract_cashflow_rec.ln_user_col_n10       := p_calculator_ln_rec.ln_user_col_n10;
        v_contract_cashflow_rec.ln_user_col_n11       := p_calculator_ln_rec.ln_user_col_n11;
        v_contract_cashflow_rec.ln_user_col_n12       := p_calculator_ln_rec.ln_user_col_n12;
        v_contract_cashflow_rec.ln_user_col_n13       := p_calculator_ln_rec.ln_user_col_n13;
        v_contract_cashflow_rec.ln_user_col_n14       := p_calculator_ln_rec.ln_user_col_n14;
        v_contract_cashflow_rec.ln_user_col_n15       := p_calculator_ln_rec.ln_user_col_n15;
        v_contract_cashflow_rec.ln_user_col_n16       := p_calculator_ln_rec.ln_user_col_n16;
        v_contract_cashflow_rec.ln_user_col_n17       := p_calculator_ln_rec.ln_user_col_n17;
        v_contract_cashflow_rec.ln_user_col_n18       := p_calculator_ln_rec.ln_user_col_n18;
        v_contract_cashflow_rec.ln_user_col_n19       := p_calculator_ln_rec.ln_user_col_n19;
        v_contract_cashflow_rec.ln_user_col_n20       := p_calculator_ln_rec.ln_user_col_n20;
        v_contract_cashflow_rec.rental_eq_pymt_raw    := null; --初始租金(等额)
        v_contract_cashflow_rec.rental_eq_pymt_adj    := null; --调整租金(等额)
        v_contract_cashflow_rec.interest_eq_pymt_raw  := null; --初始利息(等额)
        v_contract_cashflow_rec.interest_eq_pymt_adj  := null; --调整利息(等额)
        v_contract_cashflow_rec.principal_eq_pymt_raw := null; --初始本金(等额)
        v_contract_cashflow_rec.principal_eq_pymt_adj := null; --调整本金(等额)
        v_contract_cashflow_rec.rental_eq_prin_raw    := null; --初始租金(等本)
        v_contract_cashflow_rec.rental_eq_prin_adj    := null; --调整租金(等本)
        v_contract_cashflow_rec.interest_eq_prin_raw  := null; --初始利息(等本)
        v_contract_cashflow_rec.interest_eq_prin_adj  := null; --调整利息(等本)
        v_contract_cashflow_rec.principal_eq_prin_raw := null; --初始本金(等本)
        v_contract_cashflow_rec.principal_eq_prin_adj := null; --调整本金(等本)
      
        v_contract_cashflow_rec.outstanding_rental_tax_incld  := p_calculator_ln_rec.outstanding_rental_tax_incld;
        v_contract_cashflow_rec.outstanding_prin_tax_incld    := p_calculator_ln_rec.outstanding_prin_tax_incld;
        v_contract_cashflow_rec.outstanding_int_tax_incld     := p_calculator_ln_rec.outstanding_int_tax_incld;
        v_contract_cashflow_rec.interest_accrual_bal_tax_incl := p_calculator_ln_rec.interest_accrual_bal_tax_incl;
        v_contract_cashflow_rec.accumulated_unpd_int_tax_incl := p_calculator_ln_rec.accumulated_unpd_int_tax_incl;
        v_contract_cashflow_rec.exchange_rate_type            := p_calculator_ln_rec.exchange_rate_type;
        v_contract_cashflow_rec.exchange_rate_quotation       := p_calculator_ln_rec.exchange_rate_quotation;
        v_contract_cashflow_rec.exchange_rate                 := p_calculator_ln_rec.exchange_rate;
        v_contract_cashflow_rec.due_amount_cny                := p_calculator_ln_rec.due_amount_cny;
        v_contract_cashflow_rec.main_business_income          := p_calculator_ln_rec.main_business_income;
        v_contract_cashflow_rec.main_business_cost            := p_calculator_ln_rec.main_business_cost;
        v_contract_cashflow_rec.financing_cost                := p_calculator_ln_rec.financing_cost;
      
        v_contract_cashflow_rec.created_by       := p_user_id; --当前用户
        v_contract_cashflow_rec.creation_date    := sysdate; --系统时间
        v_contract_cashflow_rec.last_updated_by  := p_user_id; --当前用户
        v_contract_cashflow_rec.last_update_date := sysdate; --系统时间
      
        --liukang
        v_contract_cashflow_rec.payment_deduction_flag   := 'Y';
        v_contract_cashflow_rec.payment_deduction_amount := v_contract_cashflow_rec.due_amount;
      
        insert_contract_cashflow(p_contract_cashflow_rec => v_contract_cashflow_rec);
      
    end;
  
  end;

  procedure insert_others_cashflow(p_calculator_ln_rec hls_fin_calculator_ln%rowtype,
                                   p_price_list        varchar2,
                                   p_contract_id       number,
                                   p_company_id        number,
                                   p_column_name       varchar2,
                                   p_due_amount        number,
                                   p_vat_due_amount    number,
                                   p_net_due_amount    number,
                                   p_user_id           number) is
    v_contract_cashflow_rec con_contract_cashflow%rowtype;
  
    v_cf_item                  hls_cashflow_item.cf_item%type;
    v_cf_type                  hls_cashflow_item.cf_type%type;
    v_cf_direction             hls_cashflow_item.cf_direction%type;
    v_cf_status                con_contract_cashflow.cf_status%type;
    v_con_contract_cashflow_id number;
  begin
    begin
      select b.cf_item,
             b.cf_type,
             b.cf_direction,
             nvl(c.default_cf_status, 'RELEASE')
        into v_cf_item, v_cf_type, v_cf_direction, v_cf_status
        from hls_fin_calc_config       a,
             hls_cashflow_item         b,
             hls_cashflow_item_company c
       where a.price_list = p_price_list
         and a.column_name = p_column_name
         and a.cf_item = b.cf_item
         and c.company_id(+) = p_company_id
         and b.cf_item = c.cf_item(+);
    exception
      when no_data_found then
        return;
    end;
    if v_cf_item = 2 then
      null;
    end if;
    begin
      select *
        into v_contract_cashflow_rec
        from con_contract_cashflow
       where contract_id = p_contract_id
         and times = p_calculator_ln_rec.times
         and cf_item = v_cf_item
      --and generated_source = 'CALCULATOR'
      --and calc_line_id = p_calculator_ln_rec.calc_line_id
      ;
    
      v_contract_cashflow_rec.times           := p_calculator_ln_rec.times; --期数
      v_contract_cashflow_rec.calc_date       := to_date(p_calculator_ln_rec.calc_date,
                                                         'YYYY-MM-DD'); --计算日
      v_contract_cashflow_rec.due_date        := to_date(p_calculator_ln_rec.due_date,
                                                         'YYYY-MM-DD'); --到期日
      v_contract_cashflow_rec.fin_income_date := to_date(p_calculator_ln_rec.fin_income_date,
                                                         'YYYY-MM-DD'); --收入计算日
      v_contract_cashflow_rec.due_amount      := p_due_amount; --租金
      v_contract_cashflow_rec.principal       := null; --本金
      v_contract_cashflow_rec.interest        := null; --利息
    
      if p_column_name = 'LEASE_ITEM_AMOUNT' then
      
        v_contract_cashflow_rec.outstanding_rental_tax_incld  := p_calculator_ln_rec.outstanding_rental_tax_incld;
        v_contract_cashflow_rec.outstanding_prin_tax_incld    := p_calculator_ln_rec.outstanding_prin_tax_incld;
        v_contract_cashflow_rec.outstanding_int_tax_incld     := p_calculator_ln_rec.outstanding_int_tax_incld;
        v_contract_cashflow_rec.interest_accrual_bal_tax_incl := p_calculator_ln_rec.interest_accrual_bal_tax_incl;
        v_contract_cashflow_rec.accumulated_unpd_int_tax_incl := p_calculator_ln_rec.accumulated_unpd_int_tax_incl;
      
        v_contract_cashflow_rec.outstanding_rental    := p_calculator_ln_rec.outstanding_rental; --剩余租金
        v_contract_cashflow_rec.outstanding_principal := p_calculator_ln_rec.outstanding_principal; --剩余本金
        v_contract_cashflow_rec.outstanding_interest  := p_calculator_ln_rec.outstanding_interest; --剩余利息
      
        v_contract_cashflow_rec.interest_accrual_balance := null; --计息余额
      
        v_contract_cashflow_rec.accumulated_unpaid_interest := null; --累计未偿还利息
      end if;
    
      v_contract_cashflow_rec.principal_implicit_rate := null; --实际利率法本金
      v_contract_cashflow_rec.interest_implicit_rate  := null; --实际利率法利息
    
      v_contract_cashflow_rec.interest_period_days    := p_calculator_ln_rec.interest_period_days; --计息天数(利息期天数)
      v_contract_cashflow_rec.discounting_days        := p_calculator_ln_rec.discounting_days; --贴现天数
      v_contract_cashflow_rec.vat_due_amount          := p_vat_due_amount;
      v_contract_cashflow_rec.vat_principal           := null;
      v_contract_cashflow_rec.vat_interest            := null;
      v_contract_cashflow_rec.vat_principal_implicit  := null; --实际利率法本金增值税额
      v_contract_cashflow_rec.vat_interest_implicit   := null; --实际利率法利息增值税额
      v_contract_cashflow_rec.net_due_amount          := p_net_due_amount;
      v_contract_cashflow_rec.net_principal           := null;
      v_contract_cashflow_rec.net_interest            := null;
      v_contract_cashflow_rec.net_principal_implicit  := null; --实际利率法税后本金
      v_contract_cashflow_rec.net_interest_implicit   := null; --实际利率法税后利息
      v_contract_cashflow_rec.fix_principal_flag      := p_calculator_ln_rec.fix_principal_flag; --固定本金标志
      v_contract_cashflow_rec.fix_rental_flag         := p_calculator_ln_rec.fix_rental_flag; --固定租金标志
      v_contract_cashflow_rec.interest_only_flag      := p_calculator_ln_rec.interest_only_flag; --仅支付利息
      v_contract_cashflow_rec.equal_flag              := p_calculator_ln_rec.equal_flag;
      v_contract_cashflow_rec.manual_flag             := p_calculator_ln_rec.manual_flag;
      v_contract_cashflow_rec.beginning_of_lease_year := p_calculator_ln_rec.beginning_of_lease_year; --每个租赁年度的开始
      v_contract_cashflow_rec.salestax                := null; --营业税
    
      --用户字段日期1
      if p_calculator_ln_rec.ln_user_col_d01 is not null then
        v_contract_cashflow_rec.ln_user_col_d01 := to_date(p_calculator_ln_rec.ln_user_col_d01,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d01 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d02 is not null then
        v_contract_cashflow_rec.ln_user_col_d02 := to_date(p_calculator_ln_rec.ln_user_col_d02,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d02 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d03 is not null then
        v_contract_cashflow_rec.ln_user_col_d03 := to_date(p_calculator_ln_rec.ln_user_col_d03,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d03 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d04 is not null then
        v_contract_cashflow_rec.ln_user_col_d04 := to_date(p_calculator_ln_rec.ln_user_col_d04,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d04 := null;
      end if;
      if p_calculator_ln_rec.ln_user_col_d05 is not null then
        v_contract_cashflow_rec.ln_user_col_d05 := to_date(p_calculator_ln_rec.ln_user_col_d05,
                                                           'YYYY-MM-DD');
      else
        v_contract_cashflow_rec.ln_user_col_d05 := null;
      end if;
      v_contract_cashflow_rec.ln_user_col_v01 := p_calculator_ln_rec.ln_user_col_v01; --用户字段字符1
      v_contract_cashflow_rec.ln_user_col_v02 := p_calculator_ln_rec.ln_user_col_v02;
      v_contract_cashflow_rec.ln_user_col_v03 := p_calculator_ln_rec.ln_user_col_v03;
      v_contract_cashflow_rec.ln_user_col_v04 := p_calculator_ln_rec.ln_user_col_v04;
      v_contract_cashflow_rec.ln_user_col_v05 := p_calculator_ln_rec.ln_user_col_v05;
      v_contract_cashflow_rec.ln_user_col_n01 := p_calculator_ln_rec.ln_user_col_n01; --用户字段数字1
      v_contract_cashflow_rec.ln_user_col_n02 := p_calculator_ln_rec.ln_user_col_n02;
      v_contract_cashflow_rec.ln_user_col_n03 := p_calculator_ln_rec.ln_user_col_n03;
      v_contract_cashflow_rec.ln_user_col_n04 := p_calculator_ln_rec.ln_user_col_n04;
      v_contract_cashflow_rec.ln_user_col_n05 := p_calculator_ln_rec.ln_user_col_n05;
      v_contract_cashflow_rec.ln_user_col_n06 := p_calculator_ln_rec.ln_user_col_n06;
      v_contract_cashflow_rec.ln_user_col_n07 := p_calculator_ln_rec.ln_user_col_n07;
      v_contract_cashflow_rec.ln_user_col_n08 := p_calculator_ln_rec.ln_user_col_n08;
      v_contract_cashflow_rec.ln_user_col_n09 := p_calculator_ln_rec.ln_user_col_n09;
      v_contract_cashflow_rec.ln_user_col_n10 := p_calculator_ln_rec.ln_user_col_n10;
      v_contract_cashflow_rec.ln_user_col_n11 := p_calculator_ln_rec.ln_user_col_n11;
      v_contract_cashflow_rec.ln_user_col_n12 := p_calculator_ln_rec.ln_user_col_n12;
      v_contract_cashflow_rec.ln_user_col_n13 := p_calculator_ln_rec.ln_user_col_n13;
      v_contract_cashflow_rec.ln_user_col_n14 := p_calculator_ln_rec.ln_user_col_n14;
      v_contract_cashflow_rec.ln_user_col_n15 := p_calculator_ln_rec.ln_user_col_n15;
      v_contract_cashflow_rec.ln_user_col_n16 := p_calculator_ln_rec.ln_user_col_n16;
      v_contract_cashflow_rec.ln_user_col_n17 := p_calculator_ln_rec.ln_user_col_n17;
      v_contract_cashflow_rec.ln_user_col_n18 := p_calculator_ln_rec.ln_user_col_n18;
      v_contract_cashflow_rec.ln_user_col_n19 := p_calculator_ln_rec.ln_user_col_n19;
      v_contract_cashflow_rec.ln_user_col_n20 := p_calculator_ln_rec.ln_user_col_n20;
    
      v_contract_cashflow_rec.exchange_rate_type      := p_calculator_ln_rec.exchange_rate_type;
      v_contract_cashflow_rec.exchange_rate_quotation := p_calculator_ln_rec.exchange_rate_quotation;
      v_contract_cashflow_rec.exchange_rate           := p_calculator_ln_rec.exchange_rate;
      v_contract_cashflow_rec.due_amount_cny          := p_calculator_ln_rec.due_amount_cny;
      v_contract_cashflow_rec.main_business_income    := p_calculator_ln_rec.main_business_income;
      v_contract_cashflow_rec.main_business_cost      := p_calculator_ln_rec.main_business_cost;
      v_contract_cashflow_rec.financing_cost          := p_calculator_ln_rec.financing_cost;
    
      v_contract_cashflow_rec.last_updated_by  := p_user_id; --当前用户
      v_contract_cashflow_rec.last_update_date := sysdate; --系统时间
    
      --liukang
      v_contract_cashflow_rec.payment_deduction_flag   := 'Y';
      v_contract_cashflow_rec.payment_deduction_amount := v_contract_cashflow_rec.due_amount;
    
      if v_contract_cashflow_rec.cf_type = 1 then
        --add by chenlingfeng 2019年4月9日
        v_contract_cashflow_rec.tax_type_id   := p_calculator_ln_rec.tax_type_id;
        v_contract_cashflow_rec.tax_type_rate := p_calculator_ln_rec.tax_type_rate;
      end if;
      update_contract_cashflow(p_contract_cashflow_rec => v_contract_cashflow_rec);
    
    exception
      when no_data_found then
        select con_contract_cashflow_s.nextval
          into v_con_contract_cashflow_id
          from dual;
        v_contract_cashflow_rec.cashflow_id     := v_con_contract_cashflow_id;
        v_contract_cashflow_rec.contract_id     := p_contract_id; --当前合同ID
        v_contract_cashflow_rec.cf_item         := v_cf_item; --计算器行表的列,所配置的现金流项目
        v_contract_cashflow_rec.cf_type         := v_cf_type; -- 根据现金流项目取得现金流类型
        v_contract_cashflow_rec.cf_direction    := v_cf_direction;
        v_contract_cashflow_rec.cf_status       := v_cf_status;
        v_contract_cashflow_rec.times           := p_calculator_ln_rec.times; --期数
        v_contract_cashflow_rec.calc_date       := to_date(p_calculator_ln_rec.calc_date,
                                                           'YYYY-MM-DD'); --计算日
        v_contract_cashflow_rec.due_date        := to_date(p_calculator_ln_rec.due_date,
                                                           'YYYY-MM-DD'); --到期日
        v_contract_cashflow_rec.fin_income_date := to_date(p_calculator_ln_rec.fin_income_date,
                                                           'YYYY-MM-DD'); --收入计算日
        v_contract_cashflow_rec.due_amount      := p_due_amount; --租金
        v_contract_cashflow_rec.principal       := null; --本金
        v_contract_cashflow_rec.interest        := null; --利息
      
        if p_column_name = 'LEASE_ITEM_AMOUNT' then
        
          v_contract_cashflow_rec.outstanding_rental_tax_incld  := p_calculator_ln_rec.outstanding_rental_tax_incld;
          v_contract_cashflow_rec.outstanding_prin_tax_incld    := p_calculator_ln_rec.outstanding_prin_tax_incld;
          v_contract_cashflow_rec.outstanding_int_tax_incld     := p_calculator_ln_rec.outstanding_int_tax_incld;
          v_contract_cashflow_rec.interest_accrual_bal_tax_incl := p_calculator_ln_rec.interest_accrual_bal_tax_incl;
          v_contract_cashflow_rec.accumulated_unpd_int_tax_incl := p_calculator_ln_rec.accumulated_unpd_int_tax_incl;
        
          v_contract_cashflow_rec.outstanding_rental    := p_calculator_ln_rec.outstanding_rental; --剩余租金
          v_contract_cashflow_rec.outstanding_principal := p_calculator_ln_rec.outstanding_principal; --剩余本金
          v_contract_cashflow_rec.outstanding_interest  := p_calculator_ln_rec.outstanding_interest; --剩余利息
        
          v_contract_cashflow_rec.interest_accrual_balance := null; --计息余额
        
          v_contract_cashflow_rec.accumulated_unpaid_interest := null; --累计未偿还利息
        end if;
      
        v_contract_cashflow_rec.principal_implicit_rate := null; --实际利率法本金
        v_contract_cashflow_rec.interest_implicit_rate  := null; --实际利率法利息
      
        v_contract_cashflow_rec.interest_period_days    := p_calculator_ln_rec.interest_period_days; --计息天数(利息期天数)
        v_contract_cashflow_rec.discounting_days        := p_calculator_ln_rec.discounting_days; --贴现天数
        v_contract_cashflow_rec.vat_due_amount          := p_vat_due_amount;
        v_contract_cashflow_rec.vat_principal           := null;
        v_contract_cashflow_rec.vat_interest            := null;
        v_contract_cashflow_rec.vat_principal_implicit  := null; --实际利率法本金增值税额
        v_contract_cashflow_rec.vat_interest_implicit   := null; --实际利率法利息增值税额
        v_contract_cashflow_rec.net_due_amount          := p_net_due_amount;
        v_contract_cashflow_rec.net_principal           := null;
        v_contract_cashflow_rec.net_interest            := null;
        v_contract_cashflow_rec.net_principal_implicit  := null; --实际利率法税后本金
        v_contract_cashflow_rec.net_interest_implicit   := null; --实际利率法税后利息
        v_contract_cashflow_rec.fix_principal_flag      := p_calculator_ln_rec.fix_principal_flag; --固定本金标志
        v_contract_cashflow_rec.fix_rental_flag         := p_calculator_ln_rec.fix_rental_flag; --固定租金标志
        v_contract_cashflow_rec.interest_only_flag      := p_calculator_ln_rec.interest_only_flag; --仅支付利息
        v_contract_cashflow_rec.equal_flag              := p_calculator_ln_rec.equal_flag;
        v_contract_cashflow_rec.manual_flag             := p_calculator_ln_rec.manual_flag;
        v_contract_cashflow_rec.beginning_of_lease_year := p_calculator_ln_rec.beginning_of_lease_year; --每个租赁年度的开始
        v_contract_cashflow_rec.salestax                := null; --营业税
        v_contract_cashflow_rec.generated_source        := 'CALCULATOR'; --现金流由计算器创建
        v_contract_cashflow_rec.calc_line_id            := p_calculator_ln_rec.calc_line_id;
        v_contract_cashflow_rec.overdue_status          := 'N';
        v_contract_cashflow_rec.overdue_book_date       := null;
        v_contract_cashflow_rec.overdue_amount          := null;
        v_contract_cashflow_rec.overdue_principal       := null;
        v_contract_cashflow_rec.overdue_interest        := null;
        v_contract_cashflow_rec.overdue_remark          := null;
        v_contract_cashflow_rec.received_amount         := null;
        v_contract_cashflow_rec.received_principal      := null;
        v_contract_cashflow_rec.received_interest       := null;
        v_contract_cashflow_rec.write_off_flag          := 'NOT';
        v_contract_cashflow_rec.last_received_date      := null;
        v_contract_cashflow_rec.full_write_off_date     := null;
        v_contract_cashflow_rec.penalty_process_status  := 'NORMAL';
        v_contract_cashflow_rec.billing_status          := 'NOT';
        --用户字段日期1
        if p_calculator_ln_rec.ln_user_col_d01 is not null then
          v_contract_cashflow_rec.ln_user_col_d01 := to_date(p_calculator_ln_rec.ln_user_col_d01,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d01 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d02 is not null then
          v_contract_cashflow_rec.ln_user_col_d02 := to_date(p_calculator_ln_rec.ln_user_col_d02,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d02 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d03 is not null then
          v_contract_cashflow_rec.ln_user_col_d03 := to_date(p_calculator_ln_rec.ln_user_col_d03,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d03 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d04 is not null then
          v_contract_cashflow_rec.ln_user_col_d04 := to_date(p_calculator_ln_rec.ln_user_col_d04,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d04 := null;
        end if;
        if p_calculator_ln_rec.ln_user_col_d05 is not null then
          v_contract_cashflow_rec.ln_user_col_d05 := to_date(p_calculator_ln_rec.ln_user_col_d05,
                                                             'YYYY-MM-DD');
        else
          v_contract_cashflow_rec.ln_user_col_d05 := null;
        end if;
        v_contract_cashflow_rec.ln_user_col_v01       := p_calculator_ln_rec.ln_user_col_v01; --用户字段字符1
        v_contract_cashflow_rec.ln_user_col_v02       := p_calculator_ln_rec.ln_user_col_v02;
        v_contract_cashflow_rec.ln_user_col_v03       := p_calculator_ln_rec.ln_user_col_v03;
        v_contract_cashflow_rec.ln_user_col_v04       := p_calculator_ln_rec.ln_user_col_v04;
        v_contract_cashflow_rec.ln_user_col_v05       := p_calculator_ln_rec.ln_user_col_v05;
        v_contract_cashflow_rec.ln_user_col_n01       := p_calculator_ln_rec.ln_user_col_n01; --用户字段数字1
        v_contract_cashflow_rec.ln_user_col_n02       := p_calculator_ln_rec.ln_user_col_n02;
        v_contract_cashflow_rec.ln_user_col_n03       := p_calculator_ln_rec.ln_user_col_n03;
        v_contract_cashflow_rec.ln_user_col_n04       := p_calculator_ln_rec.ln_user_col_n04;
        v_contract_cashflow_rec.ln_user_col_n05       := p_calculator_ln_rec.ln_user_col_n05;
        v_contract_cashflow_rec.ln_user_col_n06       := p_calculator_ln_rec.ln_user_col_n06;
        v_contract_cashflow_rec.ln_user_col_n07       := p_calculator_ln_rec.ln_user_col_n07;
        v_contract_cashflow_rec.ln_user_col_n08       := p_calculator_ln_rec.ln_user_col_n08;
        v_contract_cashflow_rec.ln_user_col_n09       := p_calculator_ln_rec.ln_user_col_n09;
        v_contract_cashflow_rec.ln_user_col_n10       := p_calculator_ln_rec.ln_user_col_n10;
        v_contract_cashflow_rec.ln_user_col_n11       := p_calculator_ln_rec.ln_user_col_n11;
        v_contract_cashflow_rec.ln_user_col_n12       := p_calculator_ln_rec.ln_user_col_n12;
        v_contract_cashflow_rec.ln_user_col_n13       := p_calculator_ln_rec.ln_user_col_n13;
        v_contract_cashflow_rec.ln_user_col_n14       := p_calculator_ln_rec.ln_user_col_n14;
        v_contract_cashflow_rec.ln_user_col_n15       := p_calculator_ln_rec.ln_user_col_n15;
        v_contract_cashflow_rec.ln_user_col_n16       := p_calculator_ln_rec.ln_user_col_n16;
        v_contract_cashflow_rec.ln_user_col_n17       := p_calculator_ln_rec.ln_user_col_n17;
        v_contract_cashflow_rec.ln_user_col_n18       := p_calculator_ln_rec.ln_user_col_n18;
        v_contract_cashflow_rec.ln_user_col_n19       := p_calculator_ln_rec.ln_user_col_n19;
        v_contract_cashflow_rec.ln_user_col_n20       := p_calculator_ln_rec.ln_user_col_n20;
        v_contract_cashflow_rec.rental_eq_pymt_raw    := null; --初始租金(等额)
        v_contract_cashflow_rec.rental_eq_pymt_adj    := null; --调整租金(等额)
        v_contract_cashflow_rec.interest_eq_pymt_raw  := null; --初始利息(等额)
        v_contract_cashflow_rec.interest_eq_pymt_adj  := null; --调整利息(等额)
        v_contract_cashflow_rec.principal_eq_pymt_raw := null; --初始本金(等额)
        v_contract_cashflow_rec.principal_eq_pymt_adj := null; --调整本金(等额)
        v_contract_cashflow_rec.rental_eq_prin_raw    := null; --初始租金(等本)
        v_contract_cashflow_rec.rental_eq_prin_adj    := null; --调整租金(等本)
        v_contract_cashflow_rec.interest_eq_prin_raw  := null; --初始利息(等本)
        v_contract_cashflow_rec.interest_eq_prin_adj  := null; --调整利息(等本)
        v_contract_cashflow_rec.principal_eq_prin_raw := null; --初始本金(等本)
        v_contract_cashflow_rec.principal_eq_prin_adj := null; --调整本金(等本)
      
        v_contract_cashflow_rec.exchange_rate_type      := p_calculator_ln_rec.exchange_rate_type;
        v_contract_cashflow_rec.exchange_rate_quotation := p_calculator_ln_rec.exchange_rate_quotation;
        v_contract_cashflow_rec.exchange_rate           := p_calculator_ln_rec.exchange_rate;
        v_contract_cashflow_rec.due_amount_cny          := p_calculator_ln_rec.due_amount_cny;
        v_contract_cashflow_rec.main_business_income    := p_calculator_ln_rec.main_business_income;
        v_contract_cashflow_rec.main_business_cost      := p_calculator_ln_rec.main_business_cost;
        v_contract_cashflow_rec.financing_cost          := p_calculator_ln_rec.financing_cost;
      
        v_contract_cashflow_rec.created_by       := p_user_id; --当前用户
        v_contract_cashflow_rec.creation_date    := sysdate; --系统时间
        v_contract_cashflow_rec.last_updated_by  := p_user_id; --当前用户
        v_contract_cashflow_rec.last_update_date := sysdate; --系统时间
      
        --liukang
        v_contract_cashflow_rec.payment_deduction_flag   := 'Y';
        v_contract_cashflow_rec.payment_deduction_amount := v_contract_cashflow_rec.due_amount;
        if v_contract_cashflow_rec.cf_type = 1 then
          --add by chenlingfeng 2019年4月9日
          v_contract_cashflow_rec.tax_type_id   := p_calculator_ln_rec.tax_type_id;
          v_contract_cashflow_rec.tax_type_rate := p_calculator_ln_rec.tax_type_rate;
        end if;
        insert_contract_cashflow(p_contract_cashflow_rec => v_contract_cashflow_rec);
    end;
  
  end;

  procedure insert_quotation(p_quotation_rec prj_quotation%rowtype) is
  begin
    insert into prj_quotation values p_quotation_rec;
  end;

  procedure insert_contract(p_contract_rec con_contract%rowtype) is
  begin
    insert into con_contract values p_contract_rec;
  end;

  procedure insert_contract_bp(p_contract_bp_rec con_contract_bp%rowtype) is
  begin
    insert into con_contract_bp values p_contract_bp_rec;
  end;

  procedure insert_contract_lease_item(p_contract_lease_item_rec con_contract_lease_item%rowtype) is
  begin
    insert into con_contract_lease_item values p_contract_lease_item_rec;
  end;

  procedure recreate_cf_duedate(p_company_id      number default null,
                                p_contract_id     number default null,
                                p_contract_status varchar2 default null,
                                p_start_date      date,
                                p_end_date        date,
                                p_user_id         number) is
  begin
  
    for c_con in (select contract_id, biz_day_convention
                    from con_contract
                   where company_id = nvl(p_company_id, company_id)
                     and contract_id = nvl(p_contract_id, contract_id)
                     and contract_status =
                         nvl(p_contract_status, contract_status)
                     and data_class = 'NORMAL'
                     and biz_day_convention is not null) loop
      for c_cf in (select a.cashflow_id, a.due_date
                     from con_contract_cashflow a
                    where a.contract_id = c_con.contract_id
                      and due_date is not null
                      and due_date between p_start_date and
                          nvl(p_end_date, due_date)) loop
        update con_contract_cashflow
           set due_date = nextduedate(c_cf.due_date,
                                      0,
                                      c_con.biz_day_convention)
         where cashflow_id = c_cf.cashflow_id;
      
      end loop;
    
    end loop;
  
  end;
  --add by wcs 2014-10-30
  --purpose : calculate the due date for rental
  function calculate_rental_due_date(p_date             date,
                                     p_annual_pay_times number) return date is
    v_date date;
    v_days varchar2(2);
  begin
    v_date := add_months(p_date, p_annual_pay_times);
    v_days := to_char(v_date, 'dd');
    if p_annual_pay_times = 0 then
      return p_date;
    end if;
    if (to_number(v_days) >= 28) then
      v_date := to_date(to_char(v_date, 'yyyy-mm') || '28', 'yyyy-mm-dd');
    end if;
    return v_date;
  end;

  procedure create_cf_date(p_contract_rec       con_contract%rowtype,
                           p_inception_of_lease date,
                           p_inception_50_flag  varchar2 default null) is
    v_calc_date date;
    v_due_date  date;
    v_fin_date  date;
  
    i number;
  
    v_exists           number;
    v_annual_pay_times number;
    v_times            number;
    v_min_times        number;
  
    v_current_times number;
    v_last_times    number;
    v_month_num     number;
  begin
  
    --add by wangwei
    if p_contract_rec.annual_pay_times is not null then
      v_annual_pay_times := p_contract_rec.annual_pay_times;
    else
      select max(ccc.times)
        into v_times
        from con_contract_cashflow ccc
       where ccc.contract_id = p_contract_rec.contract_id
         and ccc.cf_item = 1;
      v_annual_pay_times := v_times / p_contract_rec.lease_term;
    end if;
    begin
      select 1
        into v_exists
        from dual
       where exists (select 1
                from con_contract_cashflow
               where contract_id = p_contract_rec.contract_id
                 and cf_item = 1
                 and cf_direction = 'INFLOW'
                 and (calc_date is null or due_date is null or
                     fin_income_date is null));
    exception
      when no_data_found then
      
        if p_contract_rec.inception_of_lease = p_inception_of_lease then
          return; --不计算
        end if;
      
    end;
    --modify by wcs 2014-10-30
    --purpose: individual designted for YONDA, the pay type is prepayment model forever
    if p_contract_rec.pay_type = 0 then
      --后付
      i := 12 / p_contract_rec.annual_pay_times;
    elsif p_contract_rec.pay_type = 1 then
      --先付
      i := 0;
    end if;
    --add by zyx 2017-03-17
    select min(ccc.times)
      into v_min_times
      from con_contract_cashflow ccc
     where ccc.contract_id = p_contract_rec.contract_id
          -- AND ccc.cf_item = 1
       and times > 0
       and ccc.cf_direction = 'INFLOW';
    i            := i + (12 / v_annual_pay_times) * (v_min_times - 1);
    v_last_times := v_min_times - 1;
    for cur_cf in (select --cashflow_id,
                    times
                     from con_contract_cashflow
                    where contract_id = p_contract_rec.contract_id
                         --  AND cf_item = 1
                      and cf_direction = 'INFLOW'
                      and times > 0
                    group by times
                    order by times
                   --modify by chenlingfeng 同一期可能没有cf_item=1
                   /*SELECT cashflow_id,
                         times
                    FROM con_contract_cashflow
                   WHERE contract_id = p_contract_rec.contract_id
                     AND cf_item = 1
                     AND cf_direction = 'INFLOW'
                   ORDER BY times*/
                   ) loop
      v_current_times := cur_cf.times;
      --第一期租金取付款申请创建时录入的首期还款日
      --隔期支付租金
      v_month_num := (12 / v_annual_pay_times) *
                     (v_current_times - v_last_times - 1);
      i           := i + v_month_num;
      v_calc_date := add_months(p_inception_of_lease, (i - 1));
    
      v_due_date := nextduedate(v_calc_date,
                                0,
                                p_contract_rec.biz_day_convention);
      v_fin_date := v_calc_date;
    
      /*UPDATE con_contract_cashflow
        SET calc_date       = v_calc_date,
            due_date        = v_due_date,
            fin_income_date = v_fin_date,
            ln_user_col_d01 = v_calc_date
      WHERE cashflow_id = cur_cf.cashflow_id;*/
    
      --更新同期现金流
      update con_contract_cashflow
         set calc_date       = v_calc_date,
             due_date        = v_due_date,
             fin_income_date = v_fin_date,
             ln_user_col_d01 = v_calc_date
       where contract_id = p_contract_rec.contract_id
         and times = cur_cf.times
      --   AND cf_item <> 1
      /*and (due_date is null or calc_date is null or
      fin_income_date is null)*/
      
      ;
    
      i            := i + (12 / v_annual_pay_times);
      v_last_times := cur_cf.times;
    end loop;
  
  end;

  procedure save_cdd_item_doc_ref(p_source_document_table varchar2,
                                  p_source_document_id    number,
                                  p_to_document_table     varchar2,
                                  p_to_document_id        number,
                                  p_user_id               number) is
  begin
  
    --save prj_cdd_item_doc_ref
    insert into prj_cdd_item_doc_ref
      (doc_ref_id,
       document_table,
       document_id,
       check_id,
       created_by,
       creation_date,
       last_updated_by,
       last_update_date)
      (select prj_cdd_item_doc_ref_s.nextval,
              p_to_document_table,
              p_to_document_id,
              check_id,
              p_user_id,
              sysdate,
              p_user_id,
              sysdate
         from prj_cdd_item_doc_ref
        where document_table = p_source_document_table
          and document_id = p_source_document_id);
  
  end;

  --更新项目后督表数据到合同后督表 modify by lpc 9874 2017/12/6
  procedure save_file_info(p_source_document_id number,
                           p_to_document_id     number,
                           p_user_id            number) is
  begin
  
    --save prj_cdd_item_doc_ref
    insert into con_file_info
      (file_info_id,
       contract_id,
       condition_req,
       department_id,
       req_status,
       expiration_date,
       req_description,
       req_flag,
       req_date,
       created_by,
       creation_date,
       last_updated_by,
       last_update_date,
       raise_status)
      (select con_file_info_s.nextval,
              p_to_document_id,
              condition_req,
              department_id,
              req_status,
              expiration_date,
              req_description,
              req_flag,
              req_date,
              p_user_id,
              sysdate,
              p_user_id,
              sysdate,
              raise_status
         from prj_file_info
        where project_id = p_source_document_id);
  
  end;

  procedure save_contract(p_project_rec         prj_project%rowtype,
                          p_calculator_hd_rec   hls_fin_calculator_hd%rowtype,
                          p_contract_id         number,
                          p_contract_seq        number,
                          p_con_document_type   varchar2,
                          p_con_contract_name   varchar2,
                          p_billing_method      varchar2,
                          p_con_contract_number out varchar2,
                          p_user_id             number) is
    v_contract_rec      con_contract%rowtype;
    v_con_document_type varchar2(30);
    v_bp_id_tenant      number;
    v_contract_number   varchar2(30);
    v_file_count        number;
    v_count             number;
    v_bp_class          varchar2(50);
  
    v_num_year varchar2(10);
    v_num_seq  varchar2(10);
    v_num_div  varchar2(10);
  
    r_quotation_rec prj_quotation%rowtype;
  begin
  
    begin
      select bp_id
        into v_bp_id_tenant
        from prj_project_bp
       where project_id = p_project_rec.project_id
         and bp_category = 'TENANT'
         and bp_id is not null;
      --  and contract_seq = p_contract_seq;
    
    exception
      when no_data_found then
        begin
          select bp_id_tenant
            into v_bp_id_tenant
            from prj_project
           where project_id = p_project_rec.project_id; --2017年12月05日14:40:53 for Deron
        exception
          when no_data_found then
            raise e_bp_id_tenant_err;
        end;
    end;
  
    select hbm.bp_class
      into v_bp_class
      from hls_bp_master hbm
     where hbm.bp_id = v_bp_id_tenant;
    --缺经销商类别的商业伙伴
  
    v_contract_rec.contract_id       := p_contract_id; -- 合同ID / PK
    v_contract_rec.project_id        := p_project_rec.project_id;
    v_contract_rec.document_type     := p_con_document_type; -- 合同类型
    v_contract_rec.document_category := g_document_category; -- 合同类别
  
    --直接从项目上取相应的商业模式
    --update by ww
    select business_type
      into v_contract_rec.business_type
      from prj_project a
     where a.project_id = p_project_rec.project_id;
    v_contract_rec.data_class := 'NORMAL';
    --hongquan.dai 20180108 取项目编号
    /* select replace(pp.project_number, 'SQ', 'ZL')
     into v_contract_number
     from prj_project pp
    where pp.project_id = p_project_rec.project_id;*/
  
    /* v_contract_number := 'CON' || substr(p_project_rec.project_number,
                                          4);
    --hongquan.dai 20180105
     SELECT COUNT(1) + 1
       INTO v_count
       FROM con_contract c
      WHERE c.project_id = p_project_rec.project_id
        AND c.data_class = 'NORMAL';
     v_contract_number              := v_contract_number || '-' || v_count;                                    
      */
    /*年度(218) + 挖掘机(EX)/装载机(WL)/二手机(IE)+
    营业税(1)/17%增值税(2)/16%增值税(3) + 5位序列号
    第6位新系统取值为3
    */
    hls_document_save_pkg.get_doc_field(p_document_category => 'CONTRACT',
                                        p_document_type     => 'CARCON',
                                        p_company_id        => 1,
                                        p_function_code     => null,
                                        p_function_usage    => null,
                                        p_user_id           => 1,
                                        p_document_number   => v_contract_number); --2018CT00001
  
   /* select replace(substr(v_contract_number, 1, 4), '0', '')
      into v_num_year
      from dual;
  
    select substr(v_contract_number, 7, length(v_contract_number))
      into v_num_seq
      from dual;
  
    select t.ref_v05
      into v_num_div
      from hls_division t
     where t.division = p_project_rec.division;
  
    v_contract_number              := v_num_year || v_num_div || '3' ||
                                      v_num_seq;
    v_contract_rec.contract_number := v_contract_number;*/
    /*    v_contract_rec.contract_number        := get_contract_number(p_document_type => p_con_document_type,
    p_document_date => SYSDATE,
    p_company_id    => p_project_rec.company_id,
    p_user_id       => p_user_id); -- 合同编号*/
    v_contract_rec.contract_name          := p_con_contract_name; -- 合同名称
    v_contract_rec.contract_number        :=v_contract_number;
    v_contract_rec.company_id             := p_project_rec.company_id; -- 公司ID
    v_contract_rec.lease_organization     := p_project_rec.lease_organization; -- 租赁组织
    v_contract_rec.lease_channel          := p_project_rec.lease_channel; -- 渠道
    v_contract_rec.district               := p_project_rec.district;
    v_contract_rec.division               := p_project_rec.division; -- 产品线
    v_contract_rec.bp_id_tenant           := v_bp_id_tenant; -- 主承租人ID
    v_contract_rec.bp_id_agent_level1     := ''; -- 经销商ID(第1层)
    v_contract_rec.bp_id_agent_level2     := ''; -- 经销商ID(第2层)
    v_contract_rec.bp_id_agent_level3     := ''; -- 经销商ID(第3层)
    v_contract_rec.owner_user_id          := p_project_rec.owner_user_id; -- 单据所有者(用户ID)
    v_contract_rec.employee_id            := p_project_rec.employee_id; -- 业务员(用户ID)
    v_contract_rec.unit_id                := p_project_rec.unit_id; -- 部门ID
    v_contract_rec.employee_id_of_manager := p_project_rec.employee_id_of_manager; -- 部门经理(用户ID)
    v_contract_rec.description            := p_project_rec.description; -- 说明
    v_contract_rec.contract_status        := 'NEW'; -- 合同状态(新建 NEW、签约 SIGN、起租 INCEPT、正常结清 TERMINATE、提前结清 ET、回购 REPUR、取消 CANCEL)
    v_contract_rec.user_status_1          := 'UNDEFINE'; -- 合同状态(预留1)
    v_contract_rec.user_status_2          := 'UNDEFINE'; -- 合同状态(预留2)
    v_contract_rec.user_status_3          := 'UNDEFINE'; -- 合同状态(预留3)
    v_contract_rec.overdue_status         := 'N';
    v_contract_rec.print_status           := 'NOT'; -- 打印状态
    v_contract_rec.print_times            := ''; -- 打印次数
    v_contract_rec.first_print_date       := ''; -- 打印日期
    v_contract_rec.first_print_by         := ''; -- 打印者
    v_contract_rec.delivery_status        := 'NOT'; -- 租赁物交付状态(已交付、已验收)
    v_contract_rec.delivery_date          := ''; -- 交付日期
    v_contract_rec.billing_method         := p_billing_method; -- 开票方式(设备票、分期、本金收据利息票)
    v_contract_rec.billing_status         := 'NOT'; -- 开票状态(NOT/PARTIAL/FULL)
    v_contract_rec.calc_session_id        := p_calculator_hd_rec.calc_session_id; -- 租金计算ID
    v_contract_rec.price_list             := p_calculator_hd_rec.price_list; -- 价目表
    v_contract_rec.calc_method            := ''; -- 计算方式:等本/等额
    v_contract_rec.invoice_price          := p_project_rec.invoice_price;
  
    ---add by zlf
    v_contract_rec.owner_user_tel := p_project_rec.owner_user_tel;
  
    v_contract_rec.version         := 1;
    v_contract_rec.version_date    := trunc(sysdate);
    v_contract_rec.version_display := 'v1';
    v_contract_rec.version_reason  := 'CONTRACT_CREATION';
  
    v_contract_rec.created_by       := p_user_id;
    v_contract_rec.creation_date    := sysdate;
    v_contract_rec.last_updated_by  := p_user_id;
    v_contract_rec.last_update_date := sysdate;
    v_contract_rec.reg_status       := '10'; --合同归档状态
    v_contract_rec.archive_status   := '10'; --登记证归档状态
  
   -- v_contract_rec.wfl_instance_id := p_project_rec.wfl_instance_id; -- 插实例ID
    --使用经销商1字段 
    v_contract_rec.bp_id_agent_level1 := p_project_rec.invoice_agent_id;
    v_contract_rec.fin_manager        := p_project_rec.fin_manager;
    --modify by lpc for BISINESS_DIVISION
    v_contract_rec.bisiness_division := p_project_rec.bisiness_division;
   -- v_contract_rec.bank_account_id   := 25678; --默认收款银行   A_德融融资租赁_建行_0466
    --add  by  lijingjing  2019-09-24
    v_contract_rec.lease_item_location :=p_project_rec.lease_item_location;
    v_contract_rec.delivery_address :=p_project_rec.delivery_address;
    v_contract_rec.payment_deduction :=p_project_rec.payment_deduction;
    v_contract_rec.secondary_lease :=p_project_rec.secondary_lease;
    v_contract_rec.pay_method :=p_project_rec.pay_method;
    v_contract_rec.bank_branch_name :=p_project_rec.bank_branch_name;
    v_contract_rec.bank_branch_num :=p_project_rec.bank_branch_num;
    v_contract_rec.bank_account_name :=p_project_rec.bank_account_name;
	v_contract_rec.score_level :=p_project_rec.score_level;
    v_contract_rec.score_result :=p_project_rec.score_result;
     v_contract_rec.wfl_contract_status :='NEW';
  
    insert_contract(p_contract_rec => v_contract_rec);
  
    --单据复制
    hls_document_transfer_pkg.doc_to_doc(p_from_doc_table => 'PRJ_PROJECT',
                                         p_from_doc_pk    => p_project_rec.project_id,
                                         p_to_doc_table   => 'CON_CONTRACT',
                                         p_to_doc_pk      => p_contract_id,
                                         p_function_code  => null,
                                         p_function_usage => null,
                                         p_user_id        => p_user_id);
    --add by chenlingfeng 2018年8月6日
  
    select *
      into r_quotation_rec
      from prj_quotation
     where document_category = 'PROJECT'
       and document_id = p_project_rec.project_id
          /*and enabled_flag = 'Y'*/
          --and contract_seq = p_contract_seq
       and calc_session_id is not null
    /*and internal_confirm = 'Y'
    and external_confirm = 'Y'*/
    ;
  
    hls_document_transfer_pkg.doc_to_doc(p_from_doc_table => 'PRJ_QUOTATION',
                                         p_from_doc_pk    => r_quotation_rec.quotation_id,
                                         p_to_doc_table   => 'CON_CONTRACT',
                                         p_to_doc_pk      => p_contract_id,
                                         p_function_code  => null,
                                         p_function_usage => null,
                                         p_user_id        => p_user_id);
    --end
  
    --单据复制
    hls_document_transfer_pkg.doc_to_doc(p_from_doc_table => 'HLS_FIN_CALCULATOR_HD',
                                         p_from_doc_pk    => p_calculator_hd_rec.calc_session_id,
                                         p_to_doc_table   => 'CON_CONTRACT',
                                         p_to_doc_pk      => p_contract_id,
                                         p_function_code  => null,
                                         p_function_usage => null,
                                         p_user_id        => p_user_id);
  
    update con_contract
       set billing_method = p_billing_method,
           billing_way    = decode(v_bp_class, 'NP', '10', 'ORG', '20', '10')
     where contract_id = p_contract_id;
  
    --在 prj_project 表中新增一个字段 create_con_date
    update prj_project pp
       set pp.create_con_date = sysdate
     where pp.project_id = p_project_rec.project_id;
  
    --save penalty profile
    update con_contract a
       set (penalty_profile,
            grace_period,
            penalty_rate,
            penalty_calc_base,
            penalty_total_base_ratio) =
           (select penalty_profile,
                   grace_period,
                   penalty_rate,
                   penalty_calc_base,
                   penalty_total_base_ratio
              from con_penalty_profile b
             where b.penalty_profile = nvl(a.penalty_profile, 'PENALTY_STD'))
     where a.contract_id = p_contract_id;
    --add by wcs 2015-04-28
    --SOLVE THE BUG about special contract than annual pay times filed is null
    select *
      into v_contract_rec
      from con_contract t
     where t.contract_id = p_contract_id;
    if v_contract_rec.annual_pay_times is null then
      select count(*)
        into v_contract_rec.lease_times
        from hls_fin_calculator_ln t
       where t.times != '0'
         and t.calc_session_id = v_contract_rec.calc_session_id;
      v_contract_rec.annual_pay_times := v_contract_rec.lease_times /
                                         v_contract_rec.lease_term;
      update con_contract t
         set t.annual_pay_times = v_contract_rec.annual_pay_times,
             t.lease_times      = v_contract_rec.lease_times
       where t.contract_id = p_contract_id;
    end if;
    save_cdd_item_doc_ref(p_source_document_table => 'PRJ_PROJECT',
                          p_source_document_id    => p_project_rec.project_id,
                          p_to_document_table     => 'CON_CONTRACT',
                          p_to_document_id        => p_contract_id,
                          p_user_id               => p_user_id);
    --更新项目后督表数据到合同后督表 modify by lpc 9874 2017/12/6
    begin
      select count(1)
        into v_file_count
        from prj_file_info pf
       where pf.project_id = p_project_rec.project_id;
      if v_file_count > 0 then
        save_file_info(p_source_document_id => p_project_rec.project_id,
                       p_to_document_id     => p_contract_id,
                       p_user_id            => p_user_id);
      end if;
    end;
    aut_document_authority_pkg.copy_trx_user_authority(p_from_doc_category => 'PROJECT',
                                                       p_from_doc_id       => p_project_rec.project_id,
                                                       p_to_doc_category   => 'CONTRACT',
                                                       p_to_doc_id         => p_contract_id,
                                                       p_user_id           => p_user_id);
  
    con_contract_custom_pkg.after_save_contract(p_contract_id => p_contract_id,
                                                p_user_id     => p_user_id);
  
    p_con_contract_number := v_contract_rec.contract_number;
  end;

  procedure save_contract_lease_num(p_contract_id number) is
    r_contract_rec con_contract%rowtype;
    v_lease_year   number;
    v_quarter_num  number;
    v_period_num   number;
  
    v_quarter_step number;
    v_period_step  number;
  
    j number := 0;
  begin
    select *
      into r_contract_rec
      from con_contract
     where contract_id = p_contract_id;
  
    if r_contract_rec.annual_pay_times = 1 then
      --年付
      v_quarter_step := 0.25;
      v_period_step  := 12;
    elsif r_contract_rec.annual_pay_times = 2 then
      --半年付
      v_quarter_step := 0.5;
      v_period_step  := 6;
    elsif r_contract_rec.annual_pay_times = 4 then
      --季付
      v_quarter_step := 1;
      v_period_step  := 3;
    elsif r_contract_rec.annual_pay_times = 12 then
      --月付
      v_quarter_step := 3;
      v_period_step  := 1;
    end if;
  
    for i in 0 .. r_contract_rec.lease_times loop
    
      if i = 0 then
        update con_contract_cashflow
           set lease_year = 0, quarter_num = 0, period_num = 0
         where contract_id = p_contract_id
           and times = i;
      else
      
        j := j + 1; --j是每个年度内的 支付期数顺序号
      
        v_lease_year  := ceil(i / r_contract_rec.annual_pay_times);
        v_quarter_num := ceil(j / v_quarter_step);
        v_period_num  := j * v_period_step;
      
        update con_contract_cashflow
           set lease_year  = v_lease_year,
               quarter_num = v_quarter_num,
               period_num  = v_period_num
         where contract_id = p_contract_id
           and times = i;
      
      end if;
    
      if j = r_contract_rec.annual_pay_times then
        --重置
        j := 0;
      end if;
    
    end loop;
  
  end;

  procedure save_contract_cashflow(p_column_cf_item        hls_fin_calculator_itfc_pkg.column_cf_item_t,
                                   p_fin_calculator_hd_rec hls_fin_calculator_hd%rowtype,
                                   p_fin_calculator_ln_rec hls_fin_calculator_ln%rowtype,
                                   p_contract_id           number,
                                   p_user_id               number) is
    t_column_cf_item hls_fin_calculator_itfc_pkg.column_cf_item_t;
  
    i         number;
    v_cf_item varchar2(100);
  
    v_sql varchar2(32767);
  
    v_column_name     varchar2(100);
    v_column_name_vat varchar2(100);
    v_column_name_net varchar2(100);
    v_due_amount      number;
    v_vat_due_amount  number;
    v_net_due_amount  number;
  begin
    if p_column_cf_item.count = 0 then
      t_column_cf_item := hls_fin_calculator_itfc_pkg.get_column_cf_item(p_price_list => p_fin_calculator_hd_rec.price_list);
    else
      t_column_cf_item := p_column_cf_item;
    end if;
  
    i := t_column_cf_item.first;
  
    while t_column_cf_item.exists(i) loop
      v_cf_item := t_column_cf_item(i).cf_item;
    
      if v_cf_item = '1' then
        --租金
        if to_number(p_fin_calculator_ln_rec.times) >= 1 then
          if nvl(p_fin_calculator_ln_rec.rental, 0) <> 0 then
            insert_rental_cashflow(p_calculator_ln_rec => p_fin_calculator_ln_rec,
                                   p_price_list        => p_fin_calculator_hd_rec.price_list,
                                   p_contract_id       => p_contract_id,
                                   p_company_id        => p_fin_calculator_hd_rec.company_id,
                                   p_user_id           => p_user_id);
          end if;
        end if;
      
      else
        --其他现金流
        v_column_name     := t_column_cf_item(i).column_name;
        v_column_name_net := t_column_cf_item(i).net_column_name;
        v_column_name_vat := t_column_cf_item(i).vat_column_name;
        --添加对手续费和贴息的处理 add by Spencer 3893 20160818
        --remove by chenlingfeng for ds
        /* --modify by fengyong for 德融,不需要对保证金和贴息特殊处理。贴息正常按期收取
        IF v_cf_item = '3'
           AND p_fin_calculator_ln_rec.times = '0' THEN
          v_sql := 'begin select lease_charge,net_lease_charge,vat_lease_charge into :1,:2,:3 from hls_fin_calculator_hd where calc_session_id = ' ||
                   p_fin_calculator_hd_rec.calc_session_id || ';end;';
          \*  ELSIF v_cf_item = '912'
              AND p_fin_calculator_ln_rec.times = '0' THEN
          v_sql := 'begin select notarial_fee_payable,net_notarial_fee,vat_notarial_fee into :1,:2,:3 from hls_fin_calculator_hd where calc_session_id = ' ||
                   p_fin_calculator_hd_rec.calc_session_id || ';end;';*\
        ELSE*/
        v_sql := 'begin select ' || v_column_name || ',' ||
                 nvl(v_column_name_net, 'null') || ',' ||
                 nvl(v_column_name_vat, 'null') ||
                 ' into :1,:2,:3 from hls_fin_calculator_ln where calc_line_id = ' ||
                 p_fin_calculator_ln_rec.calc_line_id || ';end;';
        /*  END IF;*/
      
        execute immediate v_sql
          using out v_due_amount, out v_net_due_amount, out v_vat_due_amount;
      
        if nvl(v_due_amount, 0) <> 0 then
          --remove by chenlingfeng for ds
          /* modify by fengyong for 德融不对贴息处理,正常按照每期收取。       
          IF v_cf_item IN ('3',
                                   '912')*/
          /* IF v_cf_item IN ('3')
             AND p_fin_calculator_ln_rec.times = '0' THEN
            insert_others_cashflow(p_calculator_ln_rec => p_fin_calculator_ln_rec,
                                   p_price_list        => p_fin_calculator_hd_rec.price_list,
                                   p_contract_id       => p_contract_id,
                                   p_company_id        => p_fin_calculator_hd_rec.company_id,
                                   p_column_name       => v_column_name,
                                   p_due_amount        => round(v_due_amount,
                                                                2),
                                   p_vat_due_amount    => round(v_vat_due_amount,
                                                                2),
                                   p_net_due_amount    => round(v_net_due_amount,
                                                                2),
                                   p_user_id           => p_user_id);
          ELSE*/
          insert_others_cashflow(p_calculator_ln_rec => p_fin_calculator_ln_rec,
                                 p_price_list        => p_fin_calculator_hd_rec.price_list,
                                 p_contract_id       => p_contract_id,
                                 p_company_id        => p_fin_calculator_hd_rec.company_id,
                                 p_column_name       => v_column_name,
                                 p_due_amount        => v_due_amount,
                                 p_vat_due_amount    => v_vat_due_amount,
                                 p_net_due_amount    => v_net_due_amount,
                                 p_user_id           => p_user_id);
          /* END IF;*/
        end if;
      end if;
    
      i := t_column_cf_item.next(i);
    end loop;
  
  end;

  procedure save_contract_cashflow(p_contract_id           number,
                                   p_fin_calculator_hd_rec hls_fin_calculator_hd%rowtype,
                                   p_user_id               number) is
  
    t_column_cf_item hls_fin_calculator_itfc_pkg.column_cf_item_t;
  
  begin
  
    t_column_cf_item := hls_fin_calculator_itfc_pkg.get_column_cf_item(p_price_list => p_fin_calculator_hd_rec.price_list);
  
    for c_hls_fin_calculator_ln in (select *
                                      from hls_fin_calculator_ln
                                     where calc_session_id =
                                           p_fin_calculator_hd_rec.calc_session_id
                                     order by times) loop
    
      save_contract_cashflow(p_column_cf_item        => t_column_cf_item,
                             p_fin_calculator_hd_rec => p_fin_calculator_hd_rec,
                             p_fin_calculator_ln_rec => c_hls_fin_calculator_ln,
                             p_contract_id           => p_contract_id,
                             p_user_id               => p_user_id);
    
    end loop;
  
  end;

  procedure save_contract_cashflow_bak(p_contract_id           number,
                                       p_fin_calculator_hd_rec hls_fin_calculator_hd%rowtype,
                                       p_user_id               number) is
  
  begin
  
    --插入合同行表
    for c_hls_fin_calculator_ln in (select *
                                      from hls_fin_calculator_ln
                                     where calc_session_id =
                                           p_fin_calculator_hd_rec.calc_session_id
                                     order by times) loop
      --租金
      --if nvl(c_hls_fin_calculator_ln.rental, 0) <> 0 then
      if to_number(c_hls_fin_calculator_ln.times) >= 1 then
        insert_rental_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_user_id           => p_user_id);
      end if;
    
      --其他现金流金额
      if nvl(c_hls_fin_calculator_ln.lease_item_amount, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'LEASE_ITEM_AMOUNT',
                               p_due_amount        => c_hls_fin_calculator_ln.lease_item_amount,
                               p_vat_due_amount    => null,
                               --c_hls_fin_calculator_ln.vat_input,
                               p_net_due_amount => null,
                               --c_hls_fin_calculator_ln.net_lease_item_amount,
                               p_user_id => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.down_payment, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'DOWN_PAYMENT',
                               p_due_amount        => c_hls_fin_calculator_ln.down_payment,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_down_payment,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_down_payment,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.finance_amount, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'FINANCE_AMOUNT',
                               p_due_amount        => c_hls_fin_calculator_ln.finance_amount,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_finance_amount,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_finance_amount,
                               p_user_id           => p_user_id);
      end if;
    
      --
      /*if nvl(c_hls_fin_calculator_ln.net_finance_amount, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'NET_FINANCE_AMOUNT',
                               p_due_amount        => c_hls_fin_calculator_ln.net_finance_amount,
                               p_vat_due_amount    => round(c_hls_fin_calculator_ln.net_finance_amount *
                                                            p_fin_calculator_hd_rec.vat_rate / (1 +
                                                            p_fin_calculator_hd_rec.vat_rate),
                                                            p_fin_calculator_hd_rec.currency_precision),
                               p_user_id           => p_user_id);
      end if;  */
    
      --
      if nvl(c_hls_fin_calculator_ln.lease_charge, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'LEASE_CHARGE',
                               p_due_amount        => c_hls_fin_calculator_ln.lease_charge,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_lease_charge,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_lease_charge,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.lease_mgt_fee, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'LEASE_MGT_FEE',
                               p_due_amount        => c_hls_fin_calculator_ln.lease_mgt_fee,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_lease_mgt_fee,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_lease_mgt_fee,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.deposit, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'DEPOSIT',
                               p_due_amount        => c_hls_fin_calculator_ln.deposit,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.residual_value, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'RESIDUAL_VALUE',
                               p_due_amount        => c_hls_fin_calculator_ln.residual_value,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_residual_value,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_residual_value,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.insurance_fee, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'INSURANCE_FEE',
                               p_due_amount        => c_hls_fin_calculator_ln.insurance_fee,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_insurance_fee,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_insurance_fee,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.commission_payable, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'COMMISSION_PAYABLE',
                               p_due_amount        => c_hls_fin_calculator_ln.commission_payable,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.commission_receivable, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'COMMISSION_RECEIVABLE',
                               p_due_amount        => c_hls_fin_calculator_ln.commission_receivable,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_commission_receivable,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_commission_receivable,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.third_party_deposit, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'THIRD_PARTY_DEPOSIT',
                               p_due_amount        => c_hls_fin_calculator_ln.third_party_deposit,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.deposit_refund, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'DEPOSIT_REFUND',
                               p_due_amount        => c_hls_fin_calculator_ln.deposit_refund,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.third_party_deposit_refund, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'THIRD_PARTY_DEPOSIT_REFUND',
                               p_due_amount        => c_hls_fin_calculator_ln.third_party_deposit_refund,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.promise_to_pay, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'PROMISE_TO_PAY',
                               p_due_amount        => c_hls_fin_calculator_ln.promise_to_pay,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.other_fee, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'OTHER_FEE',
                               p_due_amount        => c_hls_fin_calculator_ln.other_fee,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_other_fee,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.other_payment, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'OTHER_PAYMENT',
                               p_due_amount        => c_hls_fin_calculator_ln.other_payment,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
    
      --
      if nvl(c_hls_fin_calculator_ln.cashflow_total, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'CASHFLOW_TOTAL',
                               p_due_amount        => c_hls_fin_calculator_ln.cashflow_total,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      if nvl(c_hls_fin_calculator_ln.notarial_fee, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'NOTARIAL_FEE',
                               p_due_amount        => c_hls_fin_calculator_ln.notarial_fee,
                               p_vat_due_amount    => c_hls_fin_calculator_ln.vat_notarial_fee,
                               p_net_due_amount    => c_hls_fin_calculator_ln.net_notarial_fee,
                               p_user_id           => p_user_id);
      end if;
    
      if nvl(c_hls_fin_calculator_ln.notarial_fee_payable, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'NOTARIAL_FEE_PAYABLE',
                               p_due_amount        => c_hls_fin_calculator_ln.notarial_fee_payable,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --
      if nvl(c_hls_fin_calculator_ln.btb_fee, 0) <> 0 then
        insert_others_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                               p_price_list        => p_fin_calculator_hd_rec.price_list,
                               p_contract_id       => p_contract_id,
                               p_company_id        => p_fin_calculator_hd_rec.company_id,
                               p_column_name       => 'BTB_FEE',
                               p_due_amount        => c_hls_fin_calculator_ln.btb_fee,
                               p_vat_due_amount    => null,
                               p_net_due_amount    => null,
                               p_user_id           => p_user_id);
      end if;
    
      --BTB
      if nvl(c_hls_fin_calculator_ln.btb_repayment, 0) <> 0 then
        insert_btb_cashflow(p_calculator_ln_rec => c_hls_fin_calculator_ln,
                            p_price_list        => p_fin_calculator_hd_rec.price_list,
                            p_contract_id       => p_contract_id,
                            p_company_id        => p_fin_calculator_hd_rec.company_id,
                            p_user_id           => p_user_id);
      end if;
    
    end loop;
  
    save_contract_lease_num(p_contract_id => p_contract_id);
  
    /*自定义出口程序 */
    con_contract_custom_pkg.save_contract_cashflow(p_fin_calculator_hd_rec => p_fin_calculator_hd_rec,
                                                   p_contract_id           => p_contract_id,
                                                   p_user_id               => p_user_id);
  
  end;

  procedure save_contract_bp(p_project_id   number,
                             p_contract_id  number,
                             p_contract_seq number,
                             p_user_id      number) is
    r_contract_bp_rec    con_contract_bp%rowtype;
    v_con_contract_bp_id number;
    r_doc_pk_list        hls_document_transfer_pkg.r_doc_pk_list;
    v_doc_pk_list        hls_document_transfer_pkg.t_doc_pk_list;
    i                    number := 0;
    v_sec_seq            number := 1;
    v_np_seq             number := 1;
    v_org_seq            number := 1;
  begin
    --update by : zhangxing5129
    --update date:2014-7-18
    --update msg: 去掉合同号关联条件 。将项目上的主承租人、次承租人都生成至项目中
  
    for c_project_bp in (select *
                           from prj_project_bp b
                          where b.project_id = p_project_id
                            and b.bp_id is not null) loop
      /*IF c_project_bp.bp_type = 'TENANT_SEC' THEN
        SELECT COUNT(1)
          INTO v_count
          FROM prj_project_bp t
         WHERE t.project_id = p_project_id
           AND t.bp_type = c_project_bp.bp_type;
        IF v_count > 0 THEN
          r_contract_bp_rec.seq := v_sec_seq;
          v_sec_seq             := v_sec_seq + 1;
        END IF;
      END IF;
      IF c_project_bp.bp_type = 'GUTA_NP' THEN
        SELECT COUNT(1)
          INTO v_count
          FROM prj_project_bp t
         WHERE t.project_id = p_project_id
           AND t.bp_type = c_project_bp.bp_type;
        IF v_count > 0 THEN
          r_contract_bp_rec.seq := v_np_seq;
          v_sec_seq             := v_np_seq + 1;
        END IF;
      END IF;
      IF c_project_bp.bp_type = 'GUTA_ORG' THEN
        SELECT COUNT(1)
          INTO v_count
          FROM prj_project_bp t
         WHERE t.project_id = p_project_id
           AND t.bp_type = c_project_bp.bp_type;
        IF v_count > 0 THEN
          r_contract_bp_rec.seq := v_org_seq;
          v_sec_seq             := v_org_seq + 1;
        END IF;
      END IF;*/
      i := i + 1;
      select con_contract_bp_s.nextval into v_con_contract_bp_id from dual;
      r_contract_bp_rec.record_id   := v_con_contract_bp_id;
      r_contract_bp_rec.contract_id := p_contract_id;
      r_contract_bp_rec.bp_type     := c_project_bp.bp_type;
      r_contract_bp_rec.bp_class    := c_project_bp.bp_class;
      r_contract_bp_rec.bp_name     := c_project_bp.bp_name;
      r_contract_bp_rec.bp_code     := c_project_bp.bp_code;
      r_contract_bp_rec.bp_category := c_project_bp.bp_category;
      r_contract_bp_rec.bp_id       := c_project_bp.bp_id;
      --  r_contract_bp_rec.app_level  := c_project_bp.app_level;
      --  r_contract_bp_rec.as_level  := c_project_bp.as_level;
      --  r_contract_bp_rec.manage_level  := c_project_bp.manage_level;
      r_contract_bp_rec.pay_method   := c_project_bp.pay_method;
      r_contract_bp_rec.score_level  := c_project_bp.score_level;
      r_contract_bp_rec.score_result := c_project_bp.score_result;
      r_contract_bp_rec.funnel_score := c_project_bp.funnel_score;
      r_contract_bp_rec.bs_level     := c_project_bp.bs_level;
      r_contract_bp_rec.enabled_flag := 'Y';
      --add by liukang 20160427 start
      --将prj_project_bp表中的字段赋值给con_contract_bp表
    
      --申请人信息
    
      insert_contract_bp(p_contract_bp_rec => r_contract_bp_rec);
      r_doc_pk_list.from_doc_pk := c_project_bp.prj_bp_id;
      r_doc_pk_list.to_doc_pk := v_con_contract_bp_id;
      v_doc_pk_list(i) := r_doc_pk_list;
      save_cdd_item_doc_ref(p_source_document_table => 'PRJ_PROJECT_BP',
                            p_source_document_id    => c_project_bp.prj_bp_id,
                            p_to_document_table     => 'CON_CONTRACT',
                            p_to_document_id        => p_contract_id,
                            p_user_id               => p_user_id);
    
    end loop;
    if i > 0 then
      hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'PRJ_PROJECT_BP',
                                           p_to_doc_table          => 'CON_CONTRACT_BP',
                                           p_doc_pk_list           => v_doc_pk_list,
                                           p_to_doc_column_1       => 'contract_id',
                                           p_to_doc_column_1_value => p_contract_id,
                                           p_copy_method           => hls_document_transfer_pkg.c_doc_to_history,
                                           p_user_id               => p_user_id);
    end if;
  
    --更新bp 带出默认值
    update con_contract_bp t
       set (t.bp_code,
            t.bp_class,
            t.bp_name,
            t.id_type,
            t.id_card_no,
            t.organization_code,
            t.tax_registry_num,
            t.cell_phone,
            t.id_card_address,
            t.SEND_ADDRESS,
            t.phone,
            t.phone_2,
            t.bp_name_sp,
            t.id_no_sp,
            t.id_card_address_sp,
            t.cell_phone_sp,
            t.bp_name_leg,
            t.id_card_no_leg) =
           (select t1.bp_code,
                   t1.bp_class,
                   t1.bp_name,
                   t1.id_type,
                   t1.id_card_no,
                   t1.organization_code,
                   t1.tax_registry_num,
                   nvl(t.cell_phone, t1.cell_phone),
                   nvl(t.id_card_address, t1.id_card_address),
                   nvl(t.SEND_ADDRESS, t1.SEND_ADDRESS),
                   nvl(t.phone, t1.phone),
                   nvl(t.phone_2, t1.phone_2),
                   t1.bp_name_sp,
                   nvl(t.id_no_sp, t1.id_no_sp),
                   nvl(t.id_card_address_sp, t1.id_card_address_sp),
                   nvl(t.cell_phone_sp, t1.cell_phone_sp),
                   t1.bp_name_leg,
                   nvl(t.id_card_no_leg, t1.id_card_no_leg)
              from hls_bp_master t1
             where t1.bp_id = t.bp_id)
     where t.contract_id = p_contract_id;
    --
  
  end;

  procedure save_ast_from_prj(p_project_id   number,
                              p_contract_id  number,
                              p_contract_seq number,
                              p_user_id      number) is
    r_ast_con                  ast_con_insurance%rowtype;
    v_ast_con_car_insurance_id number;
  begin
    for r_ast_prj in (select *
                        from ast_car_insurance t
                       where t.project_id = p_project_id) loop
    
      select ast_con_insurance_s.nextval
        into v_ast_con_car_insurance_id
        from dual;
    
      hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'AST_CAR_INSURANCE',
                                           p_from_doc_pk           => r_ast_prj.ast_car_insurance_id,
                                           p_to_doc_table          => 'AST_CON_INSURANCE',
                                           p_to_doc_pk             => v_ast_con_car_insurance_id,
                                           p_copy_method           => 'DOC_TO_HISTORY',
                                           p_user_id               => p_user_id,
                                           p_to_doc_column_1       => 'CONTRACT_ID',
                                           p_to_doc_column_1_value => p_contract_id);
    
    end loop;
  end;

  procedure save_con_bp_from_agent(p_project_id   number,
                                   p_contract_id  number,
                                   p_contract_seq number,
                                   p_user_id      number) is
    r_contract_bp_rec    con_contract_bp%rowtype;
    v_con_contract_bp_id number;
    r_prj_project        prj_project%rowtype;
  begin
  
    --通过项目invoice_agent_id插入合同BP表
    select *
      into r_prj_project
      from prj_project t
     where t.project_id = p_project_id;
  
    if r_prj_project.invoice_agent_id is not null then
      select con_contract_bp_s.nextval into v_con_contract_bp_id from dual;
    
      /* BEGIN
        SELECT bm.through_flag
          INTO v_through_flag
          FROM hls_bp_master bm
         WHERE bm.bp_id = r_prj_project.invoice_agent_id;
      EXCEPTION
        WHEN no_data_found THEN
          v_through_flag := '';
          NULL;
      END;*/
      r_contract_bp_rec.record_id   := v_con_contract_bp_id;
      r_contract_bp_rec.contract_id := p_contract_id;
      r_contract_bp_rec.bp_category := 'AGENT';
      r_contract_bp_rec.bp_class    := 'ORG';
      r_contract_bp_rec.bp_id       := r_prj_project.invoice_agent_id;
    
      r_contract_bp_rec.enabled_flag     := 'Y';
      r_contract_bp_rec.created_by       := p_user_id;
      r_contract_bp_rec.creation_date    := sysdate;
      r_contract_bp_rec.last_updated_by  := p_user_id;
      r_contract_bp_rec.last_update_date := sysdate;
    
      insert_contract_bp(p_contract_bp_rec => r_contract_bp_rec);
    end if;
  
  end;

  procedure save_contract_lease_item(p_project_id   number,
                                     p_contract_id  number,
                                     p_contract_seq number,
                                     p_user_id      number) is
    r_contract_lease_item_rec    con_contract_lease_item%rowtype;
    v_con_contract_lease_item_id number;
    v_division                   varchar2(10);
    i                            number := 0;
    e_lease_item_number_error exception;
  
  begin
    --库存融资业务和保理业务全部复制租赁物--其他产品线则复制所填写租赁物 2017年12月05日14:00:05 for Deron
    select cc.division
      into v_division
      from con_contract cc
     where cc.contract_id = p_contract_id;
  
    for c_project_lease_item in (select *
                                   from prj_project_lease_item
                                  where project_id = p_project_id) loop
      --  i := 0;
      --检查数据
      --已创建
      /*    SELECT COUNT(1)
          INTO v_exist_count
          FROM con_contract_lease_item ci
         WHERE ci.project_lease_item_id = c_project_lease_item.project_lease_item_id;
      */
      --remove by chenlingfeng 2018-7-19 13:23:57
      --去除数量确认
      /*if nvl(c_project_lease_item.vehicles_number, 0) + v_exist_count >
         c_project_lease_item.quantity then
        raise e_lease_item_number_error;
      end if;*/
    
      --本次确认数量                         
      -- if nvl(c_project_lease_item.vehicles_number, 0) > 0 then
      /*   LOOP
      i := i + 1;*/
    
    /*  select v.code_value
        into r_contract_lease_item_rec.manufacturer
        from sys_code_values_v v
       where v.code = 'MANUFACTURER_CON'
         and v.ref_v01 like '%' || v_division || '%';*/
      select con_contract_lease_item_s.nextval
        into v_con_contract_lease_item_id
        from dual;
      r_contract_lease_item_rec.contract_lease_item_id := v_con_contract_lease_item_id;
      hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'PRJ_PROJECT_LEASE_ITEM',
                                           p_from_doc_pk           => c_project_lease_item.project_lease_item_id,
                                           p_to_doc_table          => 'CON_CONTRACT_LEASE_ITEM',
                                           p_to_doc_pk             => v_con_contract_lease_item_id,
                                           p_copy_method           => 'DOC_TO_HISTORY',
                                           p_user_id               => p_user_id,
                                           p_to_doc_column_1       => 'CONTRACT_ID',
                                           p_to_doc_column_1_value => p_contract_id,
                                           p_to_doc_column_2       => 'QUANTITY', --modify by lpc 拆分时数量为1
                                           p_to_doc_column_2_value => 1,
                                           p_to_doc_column_3       => 'ENABLED_FLAG',
                                           p_to_doc_column_3_value => 'Y');
    
      update con_contract_lease_item t
         set MANUFACTURER = r_contract_lease_item_rec.manufacturer
       where t.contract_lease_item_id = v_con_contract_lease_item_id;
    
      save_cdd_item_doc_ref(p_source_document_table => 'PRJ_PROJECT_LEASE_ITEM',
                            p_source_document_id    => c_project_lease_item.project_lease_item_id,
                            p_to_document_table     => 'CON_CONTRACT',
                            p_to_document_id        => p_contract_id,
                            p_user_id               => p_user_id);
    
      /* EXIT WHEN i >= c_project_lease_item.vehicles_number;
      END LOOP;*/
      -- end if;
    
      --
      update prj_project_lease_item pi
         set pi.vehicles_number = 0
       where pi.project_lease_item_id =
             c_project_lease_item.project_lease_item_id;
    
    end loop;
  
  exception
    when e_lease_item_number_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.LEASE_ITEM_NUMBER_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'save_contract_lease_item');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  --从合同lease_item到合同bp
  procedure save_con_bp_from_lease_item(p_contract_id number,
                                        p_user_id     number) is
    r_contract_bp_rec    con_contract_bp%rowtype;
    v_con_contract_bp_id number;
  begin
    for c_con_bp_rec in (select l.bp_id_vender
                           from con_contract_lease_item l
                          where l.contract_id = p_contract_id
                            and l.bp_id_vender is not null
                            and not exists
                          (select 1
                                   from con_contract_bp b
                                  where b.contract_id = p_contract_id
                                    and b.bp_category = 'VENDER'
                                    and b.bp_id = l.bp_id_vender)
                          group by l.bp_id_vender) loop
      select con_contract_bp_s.nextval into v_con_contract_bp_id from dual;
      r_contract_bp_rec.record_id        := v_con_contract_bp_id;
      r_contract_bp_rec.contract_id      := p_contract_id;
      r_contract_bp_rec.bp_category      := 'VENDER';
      r_contract_bp_rec.bp_id            := c_con_bp_rec.bp_id_vender;
      r_contract_bp_rec.created_by       := p_user_id;
      r_contract_bp_rec.creation_date    := sysdate;
      r_contract_bp_rec.last_updated_by  := p_user_id;
      r_contract_bp_rec.last_update_date := sysdate;
    
      insert_contract_bp(p_contract_bp_rec => r_contract_bp_rec);
    end loop;
  end;

  --从项目PRJ_PROJECT_ACT_CTRLER_HD到合同bp
  procedure save_con_bp_from_act_ctrler(p_project_id   number,
                                        p_contract_id  number,
                                        p_contract_seq number,
                                        p_user_id      number) is
    r_contract_bp_rec    con_contract_bp%rowtype;
    v_con_contract_bp_id number;
  begin
    for c_con_bp_rec in (select *
                           from prj_project_act_ctrler_hd h
                          where h.project_id = p_project_id
                            and h.contract_seq = p_contract_seq
                            and h.bp_id is not null
                            and not exists
                          (select 1
                                   from con_contract_bp b
                                  where b.contract_id = p_contract_id
                                    and b.bp_category = h.bp_category
                                    and b.bp_id = h.bp_id)) loop
      select con_contract_bp_s.nextval into v_con_contract_bp_id from dual;
      r_contract_bp_rec.record_id        := v_con_contract_bp_id;
      r_contract_bp_rec.contract_id      := p_contract_id;
      r_contract_bp_rec.bp_category      := c_con_bp_rec.bp_category;
      r_contract_bp_rec.bp_id            := c_con_bp_rec.bp_id;
      r_contract_bp_rec.created_by       := p_user_id;
      r_contract_bp_rec.creation_date    := sysdate;
      r_contract_bp_rec.last_updated_by  := p_user_id;
      r_contract_bp_rec.last_update_date := sysdate;
    
      insert_contract_bp(p_contract_bp_rec => r_contract_bp_rec);
    end loop;
  end;

  --从项目PRJ_PROJECT_MORTGAGE到合同bp
  procedure save_con_bp_from_mortgage(p_project_id   number,
                                      p_contract_id  number,
                                      p_contract_seq number,
                                      p_user_id      number) is
    r_contract_bp_rec    con_contract_bp%rowtype;
    v_con_contract_bp_id number;
  begin
    for c_con_bp_rec in (select h.bp_id_mortgagor
                           from prj_project_mortgage h
                          where h.project_id = p_project_id
                            and h.contract_seq = p_contract_seq
                            and h.bp_id_mortgagor is not null
                            and not exists
                          (select 1
                                   from con_contract_bp b
                                  where b.contract_id = p_contract_id
                                    and b.bp_category = 'PLEDGER'
                                    and b.bp_id = h.bp_id_mortgagor)
                          group by h.bp_id_mortgagor) loop
      select con_contract_bp_s.nextval into v_con_contract_bp_id from dual;
      r_contract_bp_rec.record_id        := v_con_contract_bp_id;
      r_contract_bp_rec.contract_id      := p_contract_id;
      r_contract_bp_rec.bp_category      := 'PLEDGER';
      r_contract_bp_rec.bp_id            := c_con_bp_rec.bp_id_mortgagor;
      r_contract_bp_rec.created_by       := p_user_id;
      r_contract_bp_rec.creation_date    := sysdate;
      r_contract_bp_rec.last_updated_by  := p_user_id;
      r_contract_bp_rec.last_update_date := sysdate;
    
      insert_contract_bp(p_contract_bp_rec => r_contract_bp_rec);
    end loop;
  end;

  --插入hls_lease_item并更新PRJ_PROJECT_LEASE_ITEM
  procedure save_hls_lease_item(p_project_id   number,
                                p_contract_seq number,
                                p_user_id      number) is
    v_lease_item_id   number;
    v_lease_item_code hls_lease_item.lease_item_code%type;
  begin
    for c_hls_lease_item_rec in (select i.*,
                                        p.company_id,
                                        p.owner_user_id,
                                        p.division project_division --添加别名project_division,因为租赁物表有同样列名,在游标中编译错误
                                   from prj_project_lease_item i,
                                        prj_project            p
                                  where i.project_id = p_project_id
                                    and i.project_id = p.project_id
                                    and i.lease_item_id is null
                                    and i.contract_seq =
                                        nvl(p_contract_seq, i.contract_seq)) loop
      hls_lease_item_pkg.ins_hls_lease_item(p_lease_item_id            => v_lease_item_id,
                                            p_owner_user_id            => c_hls_lease_item_rec.owner_user_id,
                                            p_lease_item_code          => v_lease_item_code,
                                            p_short_name               => c_hls_lease_item_rec.short_name,
                                            p_full_name                => c_hls_lease_item_rec.full_name,
                                            p_lease_item_type          => '',
                                            p_enabled_flag             => 'Y',
                                            p_search_term              => '',
                                            p_serial_number            => c_hls_lease_item_rec.serial_number,
                                            p_pattern                  => c_hls_lease_item_rec.pattern,
                                            p_specification            => c_hls_lease_item_rec.specification,
                                            p_uom                      => c_hls_lease_item_rec.uom,
                                            p_quantity                 => c_hls_lease_item_rec.quantity,
                                            p_currency                 => c_hls_lease_item_rec.currency,
                                            p_price                    => c_hls_lease_item_rec.price,
                                            p_original_asset_value     => c_hls_lease_item_rec.original_asset_value,
                                            p_net_asset_value          => c_hls_lease_item_rec.net_asset_value,
                                            p_accumulated_depreciation => c_hls_lease_item_rec.accumulated_depreciation,
                                            p_detention                => '',
                                            p_manufacturer_id          => c_hls_lease_item_rec.manufacturer_id,
                                            p_manufacturer_name        => c_hls_lease_item_rec.manufacturer_name,
                                            p_manufacturing_date       => c_hls_lease_item_rec.manufacturing_date,
                                            p_vender_id                => c_hls_lease_item_rec.vender_id,
                                            p_vender_name              => c_hls_lease_item_rec.vender_name,
                                            p_installation_site        => c_hls_lease_item_rec.installation_site,
                                            p_fixed_assets_site        => c_hls_lease_item_rec.fixed_assets_site,
                                            p_description              => c_hls_lease_item_rec.description,
                                            p_division                 => c_hls_lease_item_rec.project_division,
                                            p_abc_class                => '',
                                            p_lease_item_group         => '',
                                            p_authority_group          => '',
                                            p_company_enabled_flag     => 'Y',
                                            p_company_id               => c_hls_lease_item_rec.company_id,
                                            p_user_id                  => p_user_id,
                                            p_invoice_amt              => c_hls_lease_item_rec.invoice_amt,
                                            p_invoice_num              => c_hls_lease_item_rec.invoice_num,
                                            p_invoice_date             => c_hls_lease_item_rec.invoice_date);
      update prj_project_lease_item t
         set t.lease_item_id    = v_lease_item_id,
             t.last_update_date = sysdate,
             t.last_updated_by  = p_user_id
       where t.project_lease_item_id =
             c_hls_lease_item_rec.project_lease_item_id;
    
      update prj_project_lease_item_list t1
         set t1.lease_item_id    = v_lease_item_id,
             t1.last_update_date = sysdate,
             t1.last_updated_by  = p_user_id
       where t1.project_lease_item_id =
             c_hls_lease_item_rec.project_lease_item_id;
    
      for c_hls_lease_item_list_rec in (select *
                                          from prj_project_lease_item_list lil
                                         where lil.project_lease_item_id =
                                               c_hls_lease_item_rec.project_lease_item_id) loop
      
        hls_lease_item_pkg.ins_lease_item_list(p_lease_item_id            => v_lease_item_id,
                                               p_seq                      => c_hls_lease_item_list_rec.seq,
                                               p_asset_num                => c_hls_lease_item_list_rec.asset_num,
                                               p_asset_type               => c_hls_lease_item_list_rec.asset_type,
                                               p_asset_name               => c_hls_lease_item_list_rec.asset_name,
                                               p_posted_date              => c_hls_lease_item_list_rec.posted_date,
                                               p_specification            => c_hls_lease_item_list_rec.specification,
                                               p_vender                   => c_hls_lease_item_list_rec.vender,
                                               p_manufacturer             => c_hls_lease_item_list_rec.manufacturer,
                                               p_quantity                 => c_hls_lease_item_list_rec.quantity,
                                               p_uom                      => c_hls_lease_item_list_rec.uom,
                                               p_original_asset_value     => c_hls_lease_item_list_rec.original_asset_value,
                                               p_net_asset_value          => c_hls_lease_item_list_rec.net_asset_value,
                                               p_price                    => c_hls_lease_item_list_rec.price,
                                               p_total_amount             => c_hls_lease_item_list_rec.total_amount,
                                               p_accumulated_depreciation => c_hls_lease_item_list_rec.accumulated_depreciation,
                                               p_currency                 => c_hls_lease_item_list_rec.currency,
                                               p_installation_site        => c_hls_lease_item_list_rec.installation_site,
                                               p_invoice_amt              => c_hls_lease_item_list_rec.invoice_amt,
                                               p_invoice_amt_after_tax    => c_hls_lease_item_list_rec.invoice_amt_after_tax,
                                               p_invoice_num              => c_hls_lease_item_list_rec.invoice_num,
                                               p_invoice_date             => c_hls_lease_item_list_rec.invoice_date,
                                               p_user_id                  => p_user_id,
                                               p_ref_v01                  => c_hls_lease_item_list_rec.ref_v01,
                                               p_ref_v02                  => c_hls_lease_item_list_rec.ref_v02,
                                               p_ref_v03                  => c_hls_lease_item_list_rec.ref_v03,
                                               p_ref_v04                  => c_hls_lease_item_list_rec.ref_v04,
                                               p_ref_v05                  => c_hls_lease_item_list_rec.ref_v05,
                                               p_ref_n01                  => c_hls_lease_item_list_rec.ref_n01,
                                               p_ref_n02                  => c_hls_lease_item_list_rec.ref_n02,
                                               p_ref_n03                  => c_hls_lease_item_list_rec.ref_n03,
                                               p_ref_n04                  => c_hls_lease_item_list_rec.ref_n04,
                                               p_ref_n05                  => c_hls_lease_item_list_rec.ref_n05,
                                               p_ref_d01                  => c_hls_lease_item_list_rec.ref_d01,
                                               p_ref_d02                  => c_hls_lease_item_list_rec.ref_d02,
                                               p_ref_d03                  => c_hls_lease_item_list_rec.ref_d03,
                                               p_ref_d04                  => c_hls_lease_item_list_rec.ref_d04,
                                               p_ref_d05                  => c_hls_lease_item_list_rec.ref_d05);
      
      end loop;
    end loop;
  end;

  --插入hls_mortgage并更新PPRJ_PROJECT_MORTGAGE
  procedure save_hls_mortgage(p_project_id   number,
                              p_contract_seq number,
                              p_user_id      number) is
    v_mortgage_id hls_mortgage.mortgage_id%type;
  begin
    for c_hls_mortgage in (select i.*, p.company_id, p.owner_user_id
                             from prj_project_mortgage i, prj_project p
                            where i.project_id = p_project_id
                              and i.project_id = p.project_id
                              and i.mortgage_id is null
                              and i.contract_seq = p_contract_seq) loop
      hls_mortgage_pkg.insert_mortgage(p_mortgage_id                => v_mortgage_id,
                                       p_owner_user_id              => c_hls_mortgage.owner_user_id,
                                       p_mortgage_code              => '',
                                       p_mortgage_name              => c_hls_mortgage.mortgage_name,
                                       p_mortgage_type              => c_hls_mortgage.mortgage_type,
                                       p_mortgage_ast_classfication => c_hls_mortgage.mortgage_ast_classfication,
                                       p_mortgage_asset_detail      => c_hls_mortgage.mortgage_asset_detail,
                                       p_mortgage_contract_no       => c_hls_mortgage.mortgage_contract_no,
                                       p_project_id                 => c_hls_mortgage.project_id,
                                       p_mortgage_register_no       => c_hls_mortgage.mortgage_register_no,
                                       p_mortgage_registed_dept     => c_hls_mortgage.mortgage_registed_dept,
                                       p_bp_id_mortgagor            => c_hls_mortgage.bp_id_mortgagor,
                                       p_mortgagor_name             => c_hls_mortgage.mortgage_name,
                                       p_value                      => c_hls_mortgage.value,
                                       p_currency                   => c_hls_mortgage.currency,
                                       p_uom                        => c_hls_mortgage.uom,
                                       p_quantity                   => c_hls_mortgage.quantity,
                                       p_mortgage_status            => '',
                                       p_start_date                 => c_hls_mortgage.start_date,
                                       p_end_date                   => c_hls_mortgage.end_date,
                                       p_bp_id_evaluator            => c_hls_mortgage.bp_id_evaluator,
                                       p_evaluator_name             => c_hls_mortgage.evaluator_name,
                                       p_ownership                  => c_hls_mortgage.ownership,
                                       p_occupied                   => c_hls_mortgage.occupied,
                                       p_location                   => c_hls_mortgage.location,
                                       p_estate_license_code        => c_hls_mortgage.estate_license_code,
                                       p_housing_area               => c_hls_mortgage.housing_area,
                                       p_use                        => c_hls_mortgage.use,
                                       p_construction_date          => c_hls_mortgage.construction_date,
                                       p_building_structure         => c_hls_mortgage.building_structure,
                                       p_land_certificate_code      => c_hls_mortgage.land_certificate_code,
                                       p_land_area                  => c_hls_mortgage.land_area,
                                       p_land_use_rights_type       => c_hls_mortgage.land_use_rights_type,
                                       p_land_use_rights_start_date => c_hls_mortgage.land_use_rights_start_date,
                                       p_usable_period              => c_hls_mortgage.usable_period,
                                       p_construction_area          => c_hls_mortgage.construction_area,
                                       p_status_of_land_use         => c_hls_mortgage.status_of_land_use,
                                       p_company_name_stock         => c_hls_mortgage.company_name_stock,
                                       p_stock_name                 => c_hls_mortgage.stock_name,
                                       p_stock_code                 => c_hls_mortgage.stock_code,
                                       p_stock_amount               => c_hls_mortgage.stock_amount,
                                       p_stock_ratio                => c_hls_mortgage.stock_ratio,
                                       p_fair_value                 => c_hls_mortgage.fair_value,
                                       p_appraise_date              => c_hls_mortgage.appraise_date,
                                       p_total_value                => c_hls_mortgage.total_value,
                                       p_bvps                       => c_hls_mortgage.bvps,
                                       p_equipment_type             => c_hls_mortgage.equipment_type,
                                       p_pattern                    => c_hls_mortgage.pattern,
                                       p_specification              => c_hls_mortgage.specification,
                                       p_enabled_flag               => 'Y',
                                       p_note                       => c_hls_mortgage.note,
                                       p_user_id                    => p_user_id,
                                       p_company_id                 => c_hls_mortgage.company_id,
                                       p_ref_v01                    => c_hls_mortgage.ref_v01,
                                       p_ref_v02                    => c_hls_mortgage.ref_v02,
                                       p_ref_v03                    => c_hls_mortgage.ref_v03,
                                       p_ref_v04                    => c_hls_mortgage.ref_v04,
                                       p_ref_v05                    => c_hls_mortgage.ref_v05,
                                       p_ref_n01                    => c_hls_mortgage.ref_n01,
                                       p_ref_n02                    => c_hls_mortgage.ref_n02,
                                       p_ref_n03                    => c_hls_mortgage.ref_n03,
                                       p_ref_n04                    => c_hls_mortgage.ref_n04,
                                       p_ref_n05                    => c_hls_mortgage.ref_n05,
                                       p_ref_d01                    => c_hls_mortgage.ref_d01,
                                       p_ref_d02                    => c_hls_mortgage.ref_d02,
                                       p_ref_d03                    => c_hls_mortgage.ref_d03,
                                       p_ref_d04                    => c_hls_mortgage.ref_d04,
                                       p_ref_d05                    => c_hls_mortgage.ref_d05);
      update prj_project_mortgage t
         set t.mortgage_id      = v_mortgage_id,
             t.last_update_date = sysdate,
             t.last_updated_by  = p_user_id
       where t.prj_mortgage_id = c_hls_mortgage.prj_mortgage_id;
    end loop;
  end;

  procedure save_quotation(p_project_rec         prj_project%rowtype,
                           p_contract_id         number,
                           p_contract_seq        number,
                           p_con_calc_session_id out number,
                           p_user_id             number) is
    r_quotation_rec         prj_quotation%rowtype;
    r_hls_fin_calculator_hd hls_fin_calculator_hd%rowtype;
  
  begin
  
    begin
      select *
        into r_quotation_rec
        from prj_quotation
       where document_category = 'PROJECT'
         and document_id = p_project_rec.project_id
            /*and enabled_flag = 'Y'*/
            --and contract_seq = p_contract_seq
         and calc_session_id is not null
      /*and internal_confirm = 'Y'
      and external_confirm = 'Y'*/ --modify by zhuxianfei DERON
         for update nowait;
    exception
      when no_data_found then
        raise e_quotation_status_err;
      when too_many_rows then
        raise e_quotation_status_err;
    end;
  
    --do before create --add by chenlingfeng
    update prj_project pp
       set pp.product_code        = r_quotation_rec.product_code,
           pp.product_name        = r_quotation_rec.product_name,
           pp.sub_price_list      = r_quotation_rec.sub_price_list,
           pp.sub_price_list_name = r_quotation_rec.sub_price_list_name,
           pp.product_type        = r_quotation_rec.product_type,
           pp.sub_price_list_code = r_quotation_rec.sub_price_list_code
     where pp.project_id = p_project_rec.project_id;
  
    --do before create --add by chenlingfeng
    update hls_fin_calculator_hd pp
       set pp.product_code        = r_quotation_rec.product_code,
           pp.product_name        = r_quotation_rec.product_name,
           pp.sub_price_list      = r_quotation_rec.sub_price_list,
           pp.sub_price_list_name = r_quotation_rec.sub_price_list_name,
           pp.product_type        = r_quotation_rec.product_type --,
    -- pp.sub_price_list_code = r_quotation_rec.sub_price_list_code
     where pp.calc_session_id = r_quotation_rec.calc_session_id;
  
    /*if r_quotation_rec.create_contract_flag = 'Y' then
      raise e_quotation_created_err;
    end if;*/
  
    select *
      into r_hls_fin_calculator_hd
      from hls_fin_calculator_hd
     where calc_session_id = r_quotation_rec.calc_session_id;
  
    if r_hls_fin_calculator_hd.calc_successful = 'Y' then
    
      --复制计算器4张表的数据
      hls_fin_calculator_itfc_pkg.copy_calculator(p_from_calc_session_id => r_quotation_rec.calc_session_id,
                                                  p_to_calc_session_id   => p_con_calc_session_id,
                                                  p_user_id              => p_user_id);
      update hls_fin_calculator_hd
         set source_doc_category = 'CONTRACT',
             source_doc_id       = p_contract_id
       where calc_session_id = p_con_calc_session_id;
    
    else
      raise e_calculator_not_success_err;
    end if;
  
    if r_hls_fin_calculator_hd.calc_method is null then
      raise e_calc_method_notfound_err;
    end if;
  
    update prj_quotation
       set create_contract_flag = 'Y'
     where quotation_id = r_quotation_rec.quotation_id;
  
  end;

  procedure save_contract_billing_method(p_contract_id    number,
                                         p_billing_method varchar2,
                                         p_user_id        number) is
  begin
    insert into con_contract_billing_method
      (contract_id,
       billing_method,
       equipment_billing,
       rental_billing,
       principal_billing,
       principal_receipt,
       interest_billing,
       average_split,
       invoice_split_limit,
       split_rounding,
       bill_of_sale,
       invoice_line_limit,
       created_by,
       creation_date,
       last_updated_by,
       last_update_date,
       vat_invoice_split_limit,
       sales_tax_invoice_split_limit)
      (select p_contract_id,
              billing_method,
              equipment_billing,
              rental_billing,
              principal_billing,
              principal_receipt,
              interest_billing,
              average_split,
              invoice_split_limit,
              split_rounding,
              bill_of_sale,
              invoice_line_limit,
              p_user_id,
              sysdate,
              p_user_id,
              sysdate,
              vat_invoice_split_limit,
              sales_tax_invoice_split_limit
         from con_billing_method
        where billing_method = p_billing_method);
  end;

  procedure save_contract_cf_item(p_contract_id number,
                                  p_company_id  number,
                                  p_user_id     number) is
  begin
    insert into con_contract_cf_item
      (contract_id,
       cf_item,
       write_off_order,
       calc_penalty,
       created_by,
       creation_date,
       last_updated_by,
       last_update_date)
      (select p_contract_id,
              cf_item,
              write_off_order,
              calc_penalty,
              p_user_id,
              sysdate,
              p_user_id,
              sysdate
         from hls_cashflow_item_company
        where company_id = p_company_id);
  end;
  --合同序号和BP合同序号不一致,更新BP合同序号
  procedure update_seq_check_different(p_project_id      number,
                                       p_contract_seq    number,
                                       p_bp_contract_seq number,
                                       p_user_id         number) is
    v_bp_id_tenant number;
  begin
    if p_contract_seq <> p_bp_contract_seq then
      begin
        select bp_id
          into v_bp_id_tenant
          from prj_project_bp
         where project_id = p_project_id
           and bp_category = 'TENANT'
           and bp_id is not null
           and contract_seq = p_contract_seq;
        raise e_bp_id_tenant_err;
      exception
        when no_data_found then
          update prj_project_bp t
             set t.contract_seq     = p_contract_seq,
                 t.last_updated_by  = p_user_id,
                 t.last_update_date = sysdate
           where t.project_id = p_project_id
             and t.bp_category = 'TENANT'
             and t.bp_id is not null
             and t.contract_seq = p_bp_contract_seq;
      end;
    end if;
  end;
  --项目创建合同后更新prj_bp相关信息
  procedure update_prj_bp_after_save(p_project_id prj_project.project_id%type,
                                     p_user_id    number) is
  begin
    for r_prj_bp in (select d.bp_id, d.prj_bp_id
                       from prj_project_bp d
                      where d.project_id = p_project_id
                        and d.bp_id is not null) loop
      hls_document_transfer_pkg.doc_to_doc(p_from_doc_table => 'HLS_BP_MASTER',
                                           p_from_doc_pk    => r_prj_bp.bp_id,
                                           p_to_doc_table   => 'PRJ_PROJECT_BP',
                                           p_to_doc_pk      => r_prj_bp.prj_bp_id,
                                           p_function_code  => 'PRJ505',
                                           p_function_usage => 'CREATE',
                                           p_user_id        => p_user_id);
    end loop;
  end;

  --更新项目的prj_project_act_ctrler_hd
  procedure update_act_ctrler_after_save(p_project_id prj_project.project_id%type,
                                         p_user_id    number) is
  begin
    for r_act_ctrler in (select d.bp_id, d.prj_bp_id
                           from prj_project_act_ctrler_hd d
                          where d.project_id = p_project_id
                            and d.bp_id is not null) loop
      hls_document_transfer_pkg.doc_to_doc(p_from_doc_table => 'HLS_BP_MASTER',
                                           p_from_doc_pk    => r_act_ctrler.bp_id,
                                           p_to_doc_table   => 'PRJ_PROJECT_ACT_CTRLER_HD',
                                           p_to_doc_pk      => r_act_ctrler.prj_bp_id,
                                           p_function_code  => 'PRJ505',
                                           p_function_usage => 'CREATE',
                                           p_user_id        => p_user_id);
    end loop;
  end;

  ---add by zlf
  procedure update_contract_file_date(p_file_date   date,
                                      p_contract_id number,
                                      p_user_id     number) is
  begin
    update con_contract a
       set a.contract_file_date = p_file_date,
           a.last_updated_by    = p_user_id,
           a.last_update_date   = sysdate
     where a.contract_id = p_contract_id;
    --插入小状态
    yonda_doc_history_pkg.yonda_insert_doc_status(p_document_id       => p_contract_id,
                                                  p_document_category => 'CONTRACT',
                                                  p_doc_status        => '430',
                                                  p_user_id           => p_user_id);
  exception
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'UPDATE_CONTRACT_FILE_DATE');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  end;

  procedure update_cashflow_net_amount(p_contract_id number,
                                       p_user_id     number) is
    v_tax_type_id    number;
    v_tax_type_rate  number;
    v_net_due_amount number;
    v_net_principal  number;
    v_net_interest   number;
  begin
    for cur in (select cf.cashflow_id,
                       cf.cf_item,
                       cf.cf_type,
                       cf.due_amount,
                       cf.principal,
                       cf.interest,
                       cc.billing_method,
                       hm.rental_billing_flag,
                       hm.rental_tax_rate,
                       hm.principal_billing_flag,
                       hm.principal_tax_rate,
                       hm.interest_billing_flag,
                       hm.interest_tax_rate,
                       hm.other_billing_flag,
                       hm.other_tax_rate
                  from con_contract_cashflow cf,
                       con_contract          cc,
                       hls_billing_method    hm
                 where cf.contract_id = p_contract_id
                   and cf.contract_id = cc.contract_id
                   and cc.billing_method = hm.billing_method
                   and cf.cf_direction = 'INFLOW') loop
      --租金
      if cur.cf_type = 1 then
        --租金开票
        if cur.rental_billing_flag = 'Y' then
          v_tax_type_id    := cur.rental_tax_rate;
          v_tax_type_rate  := get_tax_type_rate(v_tax_type_id);
          v_net_due_amount := round(cur.due_amount / (1 + v_tax_type_rate),
                                    2);
          v_net_principal  := round(cur.principal / (1 + v_tax_type_rate),
                                    2);
          v_net_interest   := round(cur.interest / (1 + v_tax_type_rate), 2);
        
        else
          --本金开票
          if cur.principal_billing_flag = 'Y' then
            v_tax_type_id   := cur.principal_tax_rate;
            v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
            v_net_principal := round(cur.principal / (1 + v_tax_type_rate),
                                     2);
          end if;
        
          --利息开票
          if cur.interest_billing_flag = 'Y' then
            v_tax_type_id   := cur.interest_tax_rate;
            v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
            v_net_interest  := round(cur.interest / (1 + v_tax_type_rate),
                                     2);
          end if;
          v_net_due_amount := v_net_principal + v_net_interest;
        end if;
      else
        --判断行上是否定义
        begin
          select hb.tax_rate
            into v_tax_type_id
            from hls_billing_method_cf hb
           where hb.cf_item = cur.cf_item
             and hb.billing_method = cur.billing_method
             and hb.enabled_flag = 'Y';
        
          v_tax_type_rate  := get_tax_type_rate(v_tax_type_id);
          v_net_due_amount := round(cur.due_amount / (1 + v_tax_type_rate),
                                    2);
          v_net_principal  := round(cur.principal / (1 + v_tax_type_rate),
                                    2);
          v_net_interest   := round(cur.interest / (1 + v_tax_type_rate), 2);
        exception
          when no_data_found then
            --其他开票
            if cur.other_billing_flag = 'Y' then
              v_tax_type_id    := cur.other_tax_rate;
              v_tax_type_rate  := get_tax_type_rate(v_tax_type_id);
              v_net_due_amount := round(cur.due_amount /
                                        (1 + v_tax_type_rate),
                                        2);
              v_net_principal  := round(cur.principal /
                                        (1 + v_tax_type_rate),
                                        2);
              v_net_interest   := round(cur.interest /
                                        (1 + v_tax_type_rate),
                                        2);
            end if;
        end;
      end if;
    
      --更新cashflow
      if v_net_due_amount is not null or v_net_principal is not null or
         v_net_interest is not null then
        update con_contract_cashflow cf
           set cf.net_due_amount   = v_net_due_amount,
               cf.net_principal    = v_net_principal,
               cf.net_interest     = v_net_interest,
               cf.last_update_date = sysdate,
               cf.last_updated_by  = p_user_id
         where cf.cashflow_id = cur.cashflow_id;
      end if;
    end loop;
  end;

  function get_tax_rate(p_contract_id number, p_cf_item number) return number is
    v_rec_billing_method hls_billing_method%rowtype;
    v_tax_type_id        number;
    v_tax_type_rate      number;
    e_error exception;
  begin
    select hm.*
      into v_rec_billing_method
      from hls_billing_method hm
     where hm.billing_method =
           (select cc.billing_method
              from con_contract cc
             where cc.contract_id = p_contract_id);
  
    --租金
    if p_cf_item = 1 then
      select cc.tax_type_id
        into v_tax_type_id
        from con_contract cc
       where cc.contract_id = p_contract_id;
      --租金开票
      if v_rec_billing_method.rental_billing_flag = 'Y' then
        v_tax_type_id   := v_rec_billing_method.rental_tax_rate;
        v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
        --deron 未定义租金开票规则的时候 返回利息开票信息
      elsif v_rec_billing_method.interest_billing_flag = 'Y' then
        v_tax_type_id   := v_rec_billing_method.interest_tax_rate;
        v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
      elsif v_rec_billing_method.other_billing_flag = 'Y' then
        v_tax_type_id   := v_rec_billing_method.other_tax_rate;
        v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
      end if;
      /*elsif p_cf_item = 100 then
        --本金开票
        if v_rec_billing_method.principal_billing_flag = 'Y' then
          v_tax_type_id   := v_rec_billing_method.principal_tax_rate;
          v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
        elsif v_rec_billing_method.other_billing_flag = 'Y' then
          v_tax_type_id   := v_rec_billing_method.OTHER_TAX_RATE;
          v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
        end if;
      elsif p_cf_item = 101 then
        --利息开票
        if v_rec_billing_method.interest_billing_flag = 'Y' then
          v_tax_type_id   := v_rec_billing_method.interest_tax_rate;
          v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
        elsif v_rec_billing_method.other_billing_flag = 'Y' then
          v_tax_type_id   := v_rec_billing_method.OTHER_TAX_RATE;
          v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
        end if;*/
    else
      --判断行上是否定义
      begin
        select hb.tax_rate
          into v_tax_type_id
          from hls_billing_method_cf hb
         where hb.cf_item = p_cf_item
           and hb.billing_method = v_rec_billing_method.billing_method
           and hb.enabled_flag = 'Y';
      
        v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
      exception
        when no_data_found then
          --其他开票
          if v_rec_billing_method.other_billing_flag = 'Y' then
            v_tax_type_id   := v_rec_billing_method.other_tax_rate;
            v_tax_type_rate := get_tax_type_rate(v_tax_type_id);
          end if;
      end;
    end if;
    if v_tax_type_rate is null then
      raise e_error;
    else
      return v_tax_type_rate;
    end if;
  
  exception
    when e_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.GET_TAX_RATE_ERROR',
                                                      p_created_by              => -1,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'GET_TAX_RATE');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;
  --hongquan.dai 20180104 取tax_rate_id 
  function get_tax_rate_id(p_contract_id number, p_cf_item number)
    return number is
    v_rec_billing_method hls_billing_method%rowtype;
    v_tax_type_id        number;
    e_error exception;
  begin
    select hm.*
      into v_rec_billing_method
      from hls_billing_method hm
     where hm.billing_method =
           (select cc.billing_method
              from con_contract cc
             where cc.contract_id = p_contract_id);
  
    --租金
    if p_cf_item = 1 then
      --租金开票
      if v_rec_billing_method.rental_billing_flag = 'Y' then
        v_tax_type_id := v_rec_billing_method.rental_tax_rate;
        --deron 未定义租金开票规则的时候 返回利息开票信息
      elsif v_rec_billing_method.interest_billing_flag = 'Y' then
        --update by doosan 税改
        if v_rec_billing_method.interest_tax_rate_tp = '10' then
          select fc.tax_type_id
            into v_tax_type_id
            from con_contract cc, fnd_tax_type_codes fc
           where cc.vat_rate_of_interest = fc.tax_type_rate
             and cc.contract_id = p_contract_id
             and fc.tax_type_code like '%OUT%';
        else
          v_tax_type_id := v_rec_billing_method.interest_tax_rate;
        end if;
      elsif v_rec_billing_method.other_billing_flag = 'Y' then
        v_tax_type_id := v_rec_billing_method.other_tax_rate;
      end if;
    
    else
      --判断行上是否定义
      begin
        select hb.tax_rate
          into v_tax_type_id
          from hls_billing_method_cf hb
         where hb.cf_item = p_cf_item
           and hb.billing_method = v_rec_billing_method.billing_method
           and hb.enabled_flag = 'Y';
      
      exception
        when no_data_found then
          --其他开票
          if v_rec_billing_method.other_billing_flag = 'Y' then
            v_tax_type_id := v_rec_billing_method.other_tax_rate;
          end if;
      end;
    end if;
    if v_tax_type_id is null then
      raise e_error;
    else
      return v_tax_type_id;
    end if;
  
  exception
    when e_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.GET_TAX_RATE_ERROR',
                                                      p_created_by              => -1,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'GET_TAX_RATE_ID');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  function get_tax_type_rate(p_tax_type_id number default null) return number is
    v_tax_type_rate number;
  begin
    if p_tax_type_id is not null then
      select t.tax_type_rate
        into v_tax_type_rate
        from fnd_tax_type_codes t
       where t.tax_type_id = p_tax_type_id;
    else
      v_tax_type_rate := 0;
    end if;
    return v_tax_type_rate;
  end;

  --add by jack wu 2014-10-23
  procedure insert_contract_item_detail(p_contract_id number,
                                        p_user_id     number) as
    v_item_detail_id   number;
    v_count            number; --租赁物的数量
    v_gps_install_flag varchar2(30);
    v_con_contract_rec con_contract%rowtype;
    v_external_flag    varchar2(30);
  begin
    --DELETE FROM con_contract_lease_item WHERE contract_id = p_contract_id;
  
    ---modify by zlf
    v_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
    v_gps_install_flag := 'Y';
    --商业模式为管理人员,不用装gps
    if v_con_contract_rec.lease_channel = '02' then
      v_gps_install_flag := 'N';
    end if;
  
    --商业模式为试乘试驾时,体系内的可以不装gps,体系外的要装
    v_external_flag := hls_bp_credit_pkg.get_external_business_flag(p_contract_id => p_contract_id);
    if v_external_flag = 'N' and v_con_contract_rec.lease_channel = '00' then
      v_gps_install_flag := 'N';
    end if;
  
    for cur_item in (select ccli.contract_lease_item_id,
                            ccli.contract_id,
                            ccli.frame_for_prj,
                            ccli.engine_for_prj,
                            nvl(ccli.quantity, 1) quantity
                       from con_contract_lease_item ccli
                      where ccli.contract_id = p_contract_id) loop
      select count(*)
        into v_count
        from con_contract_item_detail t
       where t.contract_lease_item_id = cur_item.contract_lease_item_id;
      if v_count < cur_item.quantity then
        for cur_num in v_count + 1 .. cur_item.quantity loop
          select con_contract_item_detail_s.nextval
            into v_item_detail_id
            from dual;
          --modify by zlf
          --项目类租赁物导入可能有车架号和发动机号
          insert into con_contract_item_detail t
            (item_detail_id,
             contract_lease_item_id,
             contract_id,
             line_number,
             status,
             item_frame_number,
             item_engine_number,
             gps_install_flag,
             created_by,
             creation_date,
             last_updated_by,
             last_update_date)
          values
            (v_item_detail_id,
             cur_item.contract_lease_item_id,
             p_contract_id,
             cur_num,
             'NORMAL',
             cur_item.frame_for_prj,
             cur_item.engine_for_prj,
             v_gps_install_flag,
             p_user_id,
             sysdate,
             p_user_id,
             sysdate);
        end loop;
      elsif v_count > cur_item.quantity then
        delete from con_contract_item_detail t
         where t.contract_lease_item_id = cur_item.contract_lease_item_id
           and t.line_number > cur_item.quantity;
      end if;
    end loop;
  end;

  procedure save_contract_check(p_project_rec prj_project%rowtype,
                                p_user_id     number) is
    v_quantity        number;
    v_exist_count     number;
    v_vehicles_number number;
    e_lease_item_check_error    exception;
    e_lease_item_vehicles_error exception;
  begin
    select sum(pi.quantity), nvl(sum(pi.vehicles_number), 0)
      into v_quantity, v_vehicles_number
      from prj_project_lease_item pi
     where pi.project_id = p_project_rec.project_id;
  
    /* SELECT COUNT(1)
      INTO v_exist_count
      FROM con_contract_lease_item ci
     WHERE ci.project_lease_item_id IN (SELECT pi.project_lease_item_id
                                          FROM prj_project_lease_item pi
                                         WHERE pi.project_id = p_project_rec.project_id);
    IF v_quantity <= v_exist_count THEN
      RAISE e_lease_item_check_error;
    END IF;*/
    --hongquan.dai 20170110 保理不需要校验
    --remove by chenlingfeng for ds 2018-7-12 16:26:17
    /*If p_project_rec.division <> '09' Then
      If v_vehicles_number <= 0 Then
        Raise e_lease_item_vehicles_error;
      End If;
    End If;*/
  
  exception
    when e_lease_item_check_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.LEASE_CHECK_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_CHECK');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_lease_item_vehicles_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.LEASE_VEHICLES_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_CHECK');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  --复制项目层的保理应收账款信息 honguqan.dai 20180116
  procedure save_contract_factoring_info(p_project_id  number,
                                         p_contract_id number,
                                         p_user_id     number) is
  
    r_contract_factoring_info contract_factoring_info%rowtype;
  begin
    for rec in (select *
                  from prj_project_factor_info t
                 where t.project_id = p_project_id) loop
      r_contract_factoring_info.factor_contract_id := contract_factoring_info_s.nextval;
      r_contract_factoring_info.creation_date      := sysdate;
      r_contract_factoring_info.created_by         := p_user_id;
      r_contract_factoring_info.last_update_date   := sysdate;
      r_contract_factoring_info.last_updated_by    := p_user_id;
      r_contract_factoring_info.contract_id        := p_contract_id;
      r_contract_factoring_info.business_type      := rec.business_type;
      r_contract_factoring_info.accounts_amount    := rec.accounts_amount;
      r_contract_factoring_info.accounts_object    := rec.accounts_object;
      r_contract_factoring_info.policy_amount      := rec.policy_amount;
      r_contract_factoring_info.insurance_company  := rec.insurance_company;
      r_contract_factoring_info.note               := rec.note;
      insert into contract_factoring_info values r_contract_factoring_info;
    end loop;
  end;

  procedure save_contract_from_project(p_project_id        number,
                                       p_contract_seq      number,
                                       p_bp_contract_seq   number,
                                       p_con_document_type varchar2,
                                       p_con_contract_name varchar2,
                                       p_billing_method    varchar2,
                                       p_contract_id       out number,
                                       p_contract_number   out varchar2,
                                       p_user_id           number) is
    v_project_rec       prj_project%rowtype;
    v_contract_id       number;
    r_calculator_hd_rec hls_fin_calculator_hd%rowtype;
    v_calc_session_id   number;
  
    e_project_status_err     exception;
    e_fund_not_reverved      exception;
    e_quotation_status_error exception;
  
    e_lock_error exception;
    v_document_type     varchar2(30);
    v_document_category varchar2(30);
    pragma exception_init(e_lock_error, -54);
    v_ref_v02         varchar2(30);
    v_calc_successful varchar2(30);
    v_warning_message varchar2(2000);
    v_exists_flag     varchar2(5);
  
    v_insurance_method con_contract.insurance_method%type;
    v_insurer_id       number;
    v_exists           number;
    v_record_id        number;
  begin
  
    select *
      into v_project_rec
      from prj_project
     where project_id = p_project_id
       for update nowait;
  
    --状态检查
    --remove by chenlingfeng for ds 2018-7-12 16:18:06
    /*If Not v_project_rec.project_status In ('APPROVED', 'RECONSIDER') Then
      Raise e_project_status_err;
    End If;*/
  
    --DERON CHECK SOMTHING  
    if v_project_rec.division <> '02' then
      --库容业务不校验  modify zxf
      save_contract_check(p_project_rec => v_project_rec,
                          p_user_id     => p_user_id);
    end if;
  
    if v_project_rec.division <> '02' and v_project_rec.division <> '09' then
      --modify by lpc for deron bisiness_division
      select d.ref_v02
        into v_ref_v02
        from hls_division d
       where d.division = v_project_rec.division;
    
      update prj_project p
         set p.bisiness_division = v_ref_v02
       where p.project_id = p_project_id;
      --end
    end if;
    --dbms_output.put_line(v_project_rec.project_status);
  
    --  if nvl(v_project_rec.reservation_status, 'NOT') != 'RESERVED' then
    --  raise e_fund_not_reverved;
    -- end if;
    --状态检查
  
    v_contract_id := get_contract_id;
  
    --插入报价表 同时插入 contract,contract_cashflow
    --
    save_quotation(p_project_rec         => v_project_rec,
                   p_contract_id         => v_contract_id,
                   p_contract_seq        => p_contract_seq,
                   p_con_calc_session_id => v_calc_session_id,
                   p_user_id             => p_user_id);
  
    --复制项目层的保理应收账款信息 honguqan.dai 20180116
    if v_project_rec.division = '09' then
      save_contract_factoring_info(p_project_id  => p_project_id,
                                   p_contract_id => v_contract_id,
                                   p_user_id     => p_user_id);
    end if;
    --
    select *
      into r_calculator_hd_rec
      from hls_fin_calculator_hd
     where calc_session_id = v_calc_session_id;
    --合同序号和BP合同序号不一致,更新BP合同序号
    update_seq_check_different(p_project_id      => p_project_id,
                               p_contract_seq    => p_contract_seq,
                               p_bp_contract_seq => p_bp_contract_seq,
                               p_user_id         => p_user_id);
  
    --save contract
    save_contract(p_project_rec         => v_project_rec,
                  p_calculator_hd_rec   => r_calculator_hd_rec,
                  p_contract_id         => v_contract_id,
                  p_contract_seq        => p_contract_seq,
                  p_con_document_type   => p_con_document_type,
                  p_con_contract_name   => p_con_contract_name,
                  p_billing_method      => nvl(p_billing_method,
                                               r_calculator_hd_rec.billing_method),
                  p_con_contract_number => p_contract_number,
                  p_user_id             => p_user_id);
  
    p_contract_id := v_contract_id;
    --modify by lpc 9874 for deron 项目拆分合同,生成合同之后重新计算合同报价
    /*IF v_project_rec.division NOT IN ('02', '09') THEN
      IF r_calculator_hd_rec.price_list = 'DR_PRICE_25' THEN
        --进来先把行表的数据插入到临时表中,如果计算成功之后则删除数据
        INSERT INTO hls_fin_calculator_ln_temp
          SELECT *
            FROM hls_fin_calculator_ln
           WHERE calc_session_id = v_calc_session_id;
        --重算报价
        hls_fin_calculator_itfc_pkg.calculate(p_calc_session_id    => v_calc_session_id,
                                              p_recreate_h_formula => 'Y',
                                              p_recreate_l_formula => 'Y',
                                              p_calc_successful    => v_calc_successful,
                                              p_warning_message    => v_warning_message,
                                              p_user_id            => p_user_id);
        --更新报价行配置表的本金和利息 
        FOR c_rec IN (SELECT lp.times,
                             lp.principal,
                             lp.interest
                        FROM hls_fin_calculator_ln_temp lp
                       WHERE lp.calc_session_id = v_calc_session_id
                             AND lp.times <> 0) LOOP
          IF c_rec.times <> r_calculator_hd_rec.lease_times THEN
            IF c_rec.interest IS NULL THEN
              --如果利息为空就不更新
              UPDATE hls_fin_calculator_ln_formula lf
                 SET lf.principal = c_rec.principal
               WHERE lf.times = c_rec.times
                     AND lf.calc_session_id = v_calc_session_id;
            ELSE
              UPDATE hls_fin_calculator_ln_formula lf
                 SET lf.principal = c_rec.principal,
                     lf.interest  = c_rec.interest
               WHERE lf.times = c_rec.times
                     AND lf.calc_session_id = v_calc_session_id;
            END IF;
          ELSIF c_rec.times = r_calculator_hd_rec.lease_times THEN
            --任意现金流,如果是最后一期的逻辑则不更新本金,用来修正现金流
            IF c_rec.interest IS NOT NULL THEN
              UPDATE hls_fin_calculator_ln_formula lf
                 SET lf.interest = c_rec.interest
               WHERE lf.times = c_rec.times
                     AND lf.calc_session_id = v_calc_session_id;
            END IF;
          
          END IF;
        END LOOP;
        --更新完之后删除临时表信息并且计算报价
        DELETE hls_fin_calculator_ln_temp
         WHERE calc_session_id = v_calc_session_id;
      
        hls_fin_calculator_itfc_pkg.calculate(p_calc_session_id    => v_calc_session_id,
                                              p_recreate_h_formula => 'N',
                                              p_recreate_l_formula => 'N',
                                              p_calc_successful    => v_calc_successful,
                                              p_warning_message    => v_warning_message,
                                              p_user_id            => p_user_id);
      
      ELSE
        --报价方案重算计 modify by lpc 9874
        --remove by chenlingfeng for ds 2018-7-12 18:01:40
        \*hls_fin_calculator_itfc_pkg.calculate(p_calc_session_id    => v_calc_session_id,
        p_recreate_h_formula => 'Y',
        p_recreate_l_formula => 'Y',
        p_calc_successful    => v_calc_successful,
        p_warning_message    => v_warning_message,
        p_user_id            => p_user_id);*\
        IF r_calculator_hd_rec.calc_successful = 'N' THEN
          RAISE e_quotation_status_error;
        END IF;
      END IF;
    END IF;*/
    hls_document_flow_pkg.insert_document_flow(p_doc_category        => 'CONTRACT',
                                               p_doc_id              => v_contract_id,
                                               p_doc_line_id         => null,
                                               p_doc_number          => p_contract_number,
                                               p_source_doc_category => 'PROJECT',
                                               p_source_doc_id       => p_project_id,
                                               p_source_doc_line_id  => null,
                                               p_source_doc_number   => v_project_rec.project_number,
                                               p_user_id             => p_user_id);
  
    --save contract_cashflow
    save_contract_cashflow(p_contract_id           => v_contract_id,
                           p_fin_calculator_hd_rec => r_calculator_hd_rec,
                           p_user_id               => p_user_id);
  
    /*********德融二开程序  计算不含税金额 ********/
    /*update_cashflow_net_amount(p_contract_id => v_contract_id,
    p_user_id     => p_user_id);*/
  
    /********************end******************/
  
    --插入合同bp表
    save_contract_bp(p_project_id   => v_project_rec.project_id,
                     p_contract_id  => v_contract_id,
                     p_contract_seq => p_contract_seq,
                     p_user_id      => p_user_id);
  
    --add by chenlingfeng --加一个默认的保险公司
  
    select cc.insurance_method
      into v_insurance_method
      from con_contract cc
     where cc.contract_id = v_contract_id;
    if v_insurance_method = 'DS' then
      begin
      
        select 1
          into v_exists
          from dual
         where exists (select 1
                  from con_contract_bp t
                 where t.bp_category = 'INSURER'
                   and t.contract_id = p_contract_id);
      exception
        when no_data_found then
          begin
            select h.bp_id
              into v_insurer_id
              from hls_bp_master h
             where h.bp_category = 'INSURER'
               and rownum = 1;
            v_record_id := con_contract_bp_s.nextval;
            hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'HLS_BP_MASTER',
                                                 p_from_doc_pk           => v_insurer_id,
                                                 p_to_doc_table          => 'CON_CONTRACT_BP',
                                                 p_to_doc_pk             => v_record_id,
                                                 p_to_doc_column_1       => 'CONTRACT_ID',
                                                 p_to_doc_column_1_value => p_contract_id,
                                                 p_copy_method           => hls_document_transfer_pkg.c_doc_to_history,
                                                 p_user_id               => p_user_id);
          exception
            when no_data_found then
              null;
          end;
      end;
    end if;
    --end                
  
    --通过项目invoice_agent_id插入合同代理商BP
    --modify by zhuxianfei 20180731  注释原因 生成合同经销商重复报错
    /*save_con_bp_from_agent(p_project_id   => v_project_rec.project_id,
    p_contract_id  => v_contract_id,
    p_contract_seq => p_contract_seq,
    p_user_id      => p_user_id);*/
  
    save_ast_from_prj(p_project_id   => v_project_rec.project_id,
                      p_contract_id  => v_contract_id,
                      p_contract_seq => p_contract_seq,
                      p_user_id      => p_user_id);
  
    --插入hls_lease_item并更新PRJ_PROJECT_LEASE_ITEM
    save_hls_lease_item(p_project_id   => v_project_rec.project_id,
                        p_contract_seq => p_contract_seq,
                        p_user_id      => p_user_id);
  
    --插入hls_mortgage并更新PRJ_PROJECT_MORTGAGE
    save_hls_mortgage(p_project_id   => v_project_rec.project_id,
                      p_contract_seq => p_contract_seq,
                      p_user_id      => p_user_id);
    --插入合同lease_item表
    save_contract_lease_item(p_project_id   => v_project_rec.project_id,
                             p_contract_id  => v_contract_id,
                             p_contract_seq => p_contract_seq,
                             p_user_id      => p_user_id);
  
    --add by jack wu 2014-10-23
    /*insert_contract_item_detail(p_contract_id => p_contract_id,
    p_user_id     => p_user_id);*/
    --从合同lease_item到合同bp
    save_con_bp_from_lease_item(p_contract_id => v_contract_id,
                                p_user_id     => p_user_id);
  
    --从项目PRJ_PROJECT_ACT_CTRLER_HD到合同bp
    save_con_bp_from_act_ctrler(p_project_id   => v_project_rec.project_id,
                                p_contract_id  => v_contract_id,
                                p_contract_seq => p_contract_seq,
                                p_user_id      => p_user_id);
  
    --从项目PRJ_PROJECT_MORTGAGE到合同bp
    save_con_bp_from_mortgage(p_project_id   => v_project_rec.project_id,
                              p_contract_id  => v_contract_id,
                              p_contract_seq => p_contract_seq,
                              p_user_id      => p_user_id);
  
    --con_contract_cf_item;
    save_contract_cf_item(p_contract_id => v_contract_id,
                          p_company_id  => v_project_rec.company_id,
                          p_user_id     => p_user_id);
  
    --con_contract_billing_method
    save_contract_billing_method(p_contract_id    => v_contract_id,
                                 p_billing_method => nvl(p_billing_method,
                                                         r_calculator_hd_rec.billing_method),
                                 p_user_id        => p_user_id);
  
    --更新项目的bp表
    update_prj_bp_after_save(p_project_id => v_project_rec.project_id,
                             p_user_id    => p_user_id);
  
    --更新项目的prj_project_act_ctrler_hd
    update_act_ctrler_after_save(p_project_id => v_project_rec.project_id,
                                 p_user_id    => p_user_id);
  
    --add by xuls 2016-12-24 更新合同担保人序号 
    update_contract_bp_seq(p_contract_id, 'GUARANTOR');
  
    --插入项目操作历史
    hls_doc_operate_history_pkg.insert_doc_operate_history(p_document_category => 'PROJECT',
                                                           p_document_id       => p_project_id,
                                                           p_operation_code    => 'CON_CREATED',
                                                           p_user_id           => p_user_id,
                                                           p_operation_time    => sysdate,
                                                           p_description       => null);
    --插入合同操作历史
    hls_doc_operate_history_pkg.insert_doc_operate_history(p_document_category => 'CONTRACT',
                                                           p_document_id       => v_contract_id,
                                                           p_operation_code    => hls_doc_operate_history_pkg.c_generate,
                                                           p_user_id           => p_user_id,
                                                           p_operation_time    => sysdate,
                                                           p_description       => null);
  
    --2014-10-30 for doc_status
  
    select t.document_type, t.document_category
      into v_document_type, v_document_category
      from prj_project t
     where t.project_id = p_project_id;
    yonda_doc_history_pkg.yonda_insert_doc_status(p_document_id       => p_project_id,
                                                  p_document_type     => v_document_type,
                                                  p_document_category => v_document_category,
                                                  p_doc_status        => yonda_doc_history_pkg.yonda_prj_con_create,
                                                  p_instance_id       => null,
                                                  p_user_id           => p_user_id);
  
    select t.document_type, t.document_category
      into v_document_type, v_document_category
      from con_contract t
     where t.contract_id = v_contract_id;
    yonda_doc_history_pkg.yonda_insert_doc_status(p_document_id       => v_contract_id,
                                                  p_document_type     => v_document_type,
                                                  p_document_category => v_document_category,
                                                  p_doc_status        => yonda_doc_history_pkg.yonda_con_create,
                                                  p_instance_id       => null,
                                                  p_user_id           => p_user_id);
    --2017年12月07日10:16:07  注释for Deron
    /* --插入合同打印匹配条件的other_tem_clause写死
    --管理人员汽车租赁
    --汽车类报价信息存在lease_item上
    SELECT *
      INTO v_lease_item_rec
      FROM prj_project_lease_item a
     WHERE a.project_id = p_project_id;
    IF v_project_rec.lease_channel = '02' AND
       v_project_rec.document_type = 'CARLS' THEN
      --年付
      IF v_lease_item_rec.annual_pay_times = '1' THEN
        UPDATE con_contract a
           SET a.other_tmpt_clause = '10'
         WHERE a.contract_id = v_contract_id;
      END IF;
      --按月
      IF v_lease_item_rec.annual_pay_times = '12' THEN
        IF v_lease_item_rec.lease_start_date IS NOT NULL AND
           v_lease_item_rec.pmt IS NOT NULL THEN
          --一月不等
          UPDATE con_contract a
             SET a.other_tmpt_clause = '20'
           WHERE a.contract_id = v_contract_id;
        ELSE
          --月平均
          UPDATE con_contract a
             SET a.other_tmpt_clause = '30'
           WHERE a.contract_id = v_contract_id;
        
        END IF;
      END IF;
    END IF;
    --零售业务一年无息(汽车)
    IF v_project_rec.lease_channel = '01' AND
       v_project_rec.document_type = 'CARLS' THEN
      IF (to_number(v_lease_item_rec.lease_times) /
         to_number(v_lease_item_rec.annual_pay_times)) = 1 THEN
        UPDATE con_contract a
           SET a.other_tmpt_clause = '40'
         WHERE a.contract_id = v_contract_id;
      END IF;
    END IF;*/
  
    --add by Harry 9952 2018/10/15 更新直销手续费标志
    begin
    
      select 'Y'
        into v_exists_flag
        from con_contract_bp b
       where b.contract_id = v_contract_id
         and b.bp_category = 'AGENT'
         and b.bp_name like '%直销%'
         and rownum = 1;
    
      if v_exists_flag = 'Y' then
        update con_contract c
           set c.direct_lease_charge_flag = 'Y'
         where c.contract_id = v_contract_id;
      end if;
    
    exception
      when no_data_found then
        null;
    end;
  
  exception
  
    when e_fund_not_reverved then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'PRJ_PROJECT_PKG.FUND_NOT_RESERVED',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_quotation_created_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'PRJ_PROJECT_PKG.QUOTATION_IS_CREATED_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
      /* when e_quotation_status_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'PRJ_PROJECT_PKG.QUOTATION_STATUS_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');*/
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_get_contract_number_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.GET_CON_CONTRACT_NUMBER_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_project_status_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.PRJ_PROJECT_STATUS_CHECK_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
      --DBMS_OUTPUT.PUT_LINE('e_project_status_err');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_lock_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.LOCK_PRJ_PROJECT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
    
      --DBMS_OUTPUT.PUT_LINE('e_lock_error');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_bp_id_tenant_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.BP_ID_TENANT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
    
      --DBMS_OUTPUT.PUT_LINE('e_bp_id_tenant_err');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_calculator_not_success_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CALCULATOR_NOT_SUCCESSFUL_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
    
      --DBMS_OUTPUT.PUT_LINE('e_bp_id_tenant_err');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_calc_method_notfound_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CALC_METHOD_NOTFOUND_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_quotation_status_error then
    
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.QUOTATION_STATUS_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'SAVE_CONTRACT_FROM_PROJECT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  procedure save_contract_from_calculator(p_contract_id      in number,
                                          p_calc_session_id  in number,
                                          p_save_contract_hd in varchar2 default 'Y',
                                          p_save_contract_ln in varchar2 default 'Y',
                                          p_user_id          in number) is
  
    r_calculator_hd_rec       hls_fin_calculator_hd%rowtype;
    v_adj_amt                 number;
    v_0_disburse_cf_id        number;
    v_0_disburse_amt          number;
    v_before_0_disburse_count number;
    v_exists                  number;
    v_bill_method             varchar2(30);
  begin
  
    select *
      into r_calculator_hd_rec
      from hls_fin_calculator_hd
     where calc_session_id = p_calc_session_id;
  
    if r_calculator_hd_rec.calc_successful = 'N' then
      raise e_calculator_not_success_err;
    end if;
  
    if r_calculator_hd_rec.calc_method is null then
      raise e_calc_method_notfound_err; -- 合同的计算(本利分摊)方式不能为空
    end if;
  
    begin
      select 1
        into v_exists
        from dual
       where exists (select 1
                from con_contract_cashflow
               where contract_id = p_contract_id
                 and times > 0
                 and (write_off_flag in ('PARTIAL', 'FULL') or
                     received_amount is not null or
                     received_principal is not null or
                     received_interest is not null));
    
      --有过收付款的cf行,不能进行计算
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_SAVE_EXISTS_WRITEOFF_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
    
      --DBMS_OUTPUT.PUT_LINE('e_bp_id_tenant_err');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    exception
      when no_data_found then
        null;
    end;
  
    --单据复制
    if p_save_contract_hd = 'Y' then
      select cc.billing_method
        into v_bill_method
        from con_contract cc
       where cc.contract_id = p_contract_id;
    
      hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'HLS_FIN_CALCULATOR_HD',
                                           p_from_doc_pk           => p_calc_session_id,
                                           p_to_doc_table          => 'CON_CONTRACT',
                                           p_to_doc_pk             => p_contract_id,
                                           p_function_code         => null,
                                           p_function_usage        => null,
                                           p_to_doc_column_1       => 'BUSINESS_TYPE',
                                           p_to_doc_column_1_value => r_calculator_hd_rec.business_type,
                                           p_to_doc_column_2       => 'BILLING_METHOD',
                                           p_to_doc_column_2_value => v_bill_method,
                                           p_user_id               => p_user_id);
    end if;
  
    if p_save_contract_ln = 'Y' then
    
      delete from con_contract_cashflow
       where contract_id = p_contract_id
         and times > 0;
    
      begin
        select 1
          into v_exists
          from dual
         where exists (select 1
                  from con_contract_cashflow
                 where contract_id = p_contract_id
                   and times = 0
                   and cf_item in (
                                   --客户保证金 
                                   51,
                                   --  客户保证金退还
                                   52,
                                   -- 代理商代付保证金
                                   508,
                                   --  代理商代付保证金退还
                                   509,
                                   --  客户手续费
                                   3,
                                   --  代理商手续费 
                                   301)
                   and (write_off_flag in ('PARTIAL', 'FULL') or
                       received_amount is not null or
                       received_principal is not null or
                       received_interest is not null));
      
        --有过收付款的cf行,不能进行计算
        sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_SAVE_EXISTS_WRITEOFF_ERROR',
                                                        p_created_by              => p_user_id,
                                                        p_package_name            => 'CON_CONTRACT_PKG',
                                                        p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
      
        --DBMS_OUTPUT.PUT_LINE('e_bp_id_tenant_err');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      exception
        when no_data_found then
          null;
      end;
      delete from con_contract_cashflow
       where contract_id = p_contract_id
         and times = 0
         and cf_item in (
                         --客户保证金 
                         51,
                         --  客户保证金退还
                         52,
                         -- 代理商代付保证金
                         508,
                         --  代理商代付保证金退还
                         509,
                         --  客户手续费
                         3,
                         --  代理商手续费 
                         301);
    
      save_contract_cashflow(p_contract_id           => p_contract_id,
                             p_fin_calculator_hd_rec => r_calculator_hd_rec,
                             p_user_id               => p_user_id);
    
    end if;
  
    --自动处理0期投放额 start
    --由于吉利全部使用的是5.  所以改成5, 以前是cf_item=0
  
    begin
      select cashflow_id
        into v_0_disburse_cf_id
        from con_contract_cashflow
       where contract_id = p_contract_id
         and times = 0
         and cf_item = 0;
    exception
      when no_data_found then
        sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_SAVE_0_DISBURSE_NOTFOUND_ERROR',
                                                        p_created_by              => p_user_id,
                                                        p_package_name            => 'CON_CONTRACT_PKG',
                                                        p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
      
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
    end;
  
    select nvl(to_number(r_calculator_hd_rec.lease_item_amount), 0) -
           nvl(sum(due_amount), 0),
           count(1)
      into v_0_disburse_amt, v_before_0_disburse_count
      from con_contract_cashflow
     where contract_id = p_contract_id
       and cf_item = 5
       and times < 0;
  
    if v_before_0_disburse_count > 0 then
    
      if v_0_disburse_amt < 0 then
        sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_SAVE_0_DISBURSE_AMOUNT_ERROR',
                                                        p_created_by              => p_user_id,
                                                        p_package_name            => 'CON_CONTRACT_PKG',
                                                        p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
      
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      end if;
    
      update con_contract_cashflow
         set due_amount = v_0_disburse_amt
       where cashflow_id = v_0_disburse_cf_id;
    
    end if;
  
    --自动处理0期投放额 end
  
    begin
      select 1
        into v_exists
        from dual
       where exists (select 1
                from con_contract_cashflow
               where contract_id = p_contract_id
                 and times <= 0
                 and received_amount > 0
                 and nvl(received_amount, 0) > due_amount);
    
      --有过收付款的cf行,不能进行计算
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_SAVE_WRITEOFF_AMOUNT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
    
      --DBMS_OUTPUT.PUT_LINE('e_bp_id_tenant_err');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    exception
      when no_data_found then
        null;
    end;
  
  exception
    when e_calculator_not_success_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CALCULATOR_NOT_SUCCESSFUL_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_calc_method_notfound_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CALC_METHOD_NOTFOUND_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'SAVE_CONTRACT_FROM_CALCULATOR');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  procedure contract_print(p_contract_id number,
                           p_content_id  number,
                           p_user_id     number) is
    r_con_contract_rec  con_contract%rowtype;
    r_con_content_rec   con_contract_content%rowtype;
    v_document_type     varchar2(30);
    v_document_category varchar2(30);
  begin
  
    r_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
  
    con_contract_content_pkg.get_contract_content_rec(p_content_id           => p_content_id,
                                                      p_user_id              => p_user_id,
                                                      p_contract_content_rec => r_con_content_rec);
  
    /*check contract status*/
    con_contract_verify_pkg.contract_verify(p_contract_rec => r_con_contract_rec,
                                            p_action_type  => con_contract_verify_pkg.c_print,
                                            p_user_id      => p_user_id);
    ---modify by zlf 2015-1-6
    /*UPDATE con_contract
      SET print_status     = 'PRINTED',
          print_times      = nvl(print_times, 0) + 1,
          first_print_date = SYSDATE,
          last_update_date = SYSDATE,
          last_updated_by  = p_user_id
    WHERE contract_id = p_contract_id;*/
  
    update con_contract_content t
       set t.content_print_flag = 'Y',
           t.last_update_date   = sysdate,
           t.last_updated_by    = p_user_id
     where t.content_id = p_content_id;
  
    --插入合同操作历史
    hls_doc_operate_history_pkg.insert_doc_operate_history(p_document_category => 'CONTRACT',
                                                           p_document_id       => p_contract_id,
                                                           p_operation_code    => 'CON_PRINT',
                                                           p_user_id           => p_user_id,
                                                           p_operation_time    => sysdate,
                                                           p_description       => null);
  
    --2014-10-30 for doc_status
    select t.document_type, t.document_category
      into v_document_type, v_document_category
      from con_contract t
     where t.contract_id = p_contract_id;
    yonda_doc_history_pkg.yonda_insert_doc_status(p_document_id       => p_contract_id,
                                                  p_document_type     => v_document_type,
                                                  p_document_category => v_document_category,
                                                  p_doc_status        => yonda_doc_history_pkg.yonda_con_doc_printed,
                                                  p_instance_id       => null,
                                                  p_user_id           => p_user_id);
  exception
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'CONTRACT_PRINT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  procedure contract_print(p_contract_id number,
                           p_content_id  number,
                           p_file_path   varchar2,
                           p_file_name   varchar2,
                           p_type        varchar2,
                           p_user_id     number) is
    r_con_contract_rec con_contract%rowtype;
    r_con_content_rec  con_contract_content%rowtype;
    v_count            number;
  begin
  
    r_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
  
    con_contract_content_pkg.get_contract_content_rec(p_content_id           => p_content_id,
                                                      p_user_id              => p_user_id,
                                                      p_contract_content_rec => r_con_content_rec);
  
    /*check contract status*/
    con_contract_verify_pkg.contract_verify(p_contract_rec => r_con_contract_rec,
                                            p_action_type  => con_contract_verify_pkg.c_print,
                                            p_user_id      => p_user_id);
  
    update con_contract_content t
       set t.content_print_flag = 'Y',
           t.file_path          = p_file_path,
           t.file_name          = p_file_name || '.' || p_type,
           t.last_update_date   = sysdate,
           t.last_updated_by    = p_user_id,
           t.print_date         = sysdate
     where t.content_id = p_content_id;
  
    select count(*)
      into v_count
      from con_contract_content t
     where t.contract_id = p_contract_id
       and nvl(t.content_print_flag, 'N') = 'N';
  
    if v_count = 0 then
      update con_contract
         set print_status     = 'PRINTED',
             print_times      = nvl(print_times, 0) + 1,
             last_update_date = sysdate,
             last_updated_by  = p_user_id
       where contract_id = p_contract_id;
    end if;
  
    --插入合同操作历史
    hls_doc_operate_history_pkg.insert_doc_operate_history(p_document_category => 'CONTRACT',
                                                           p_document_id       => p_contract_id,
                                                           p_operation_code    => 'CON_PRINT',
                                                           p_user_id           => p_user_id,
                                                           p_operation_time    => sysdate,
                                                           p_description       => null);
  
  exception
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'CONTRACT_PRINT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;

  procedure contract_sign(p_contract_id             number,
                          p_exchange_rate_quotation varchar2,
                          p_exchange_rate_type      varchar2,
                          p_exchange_rate           number,
                          p_signing_date            date,
                          p_user_id                 number) is
  
    r_con_contract_rec con_contract%rowtype;
    v_calc_successful  varchar2(1);
    v_warning_message  varchar2(2000);
    v_approval_method  varchar2(30);
    e_calc_quotation_err exception;
  begin
    /*r_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
    * /
    
    /*check contract status*/
    /* con_contract_verify_pkg.contract_verify(p_contract_rec => r_con_contract_rec,
    p_action_type  => con_contract_verify_pkg.c_sign,
    p_user_id      => p_user_id);*/
  
    /*update contract status*/
    /* update con_contract
      set contract_status  = 'SIGN',
          signing_date     = p_signing_date,
          last_updated_by  = p_user_id,
          last_update_date = sysdate
    where contract_id = p_contract_id;*/
  
    /*二开程序*/
    /*con_contract_custom_pkg.after_contract_sign(p_contract_id  => p_contract_id,
    p_signing_date => p_signing_date,
    p_user_id      => p_user_id);*/
  
    --插入合同操作历史
    /*hls_doc_operate_history_pkg.insert_doc_operate_history(p_document_category => 'CONTRACT',
    p_document_id       => p_contract_id,
    p_operation_code    => 'CON_SIGN',
    p_user_id           => p_user_id,
    p_operation_time    => sysdate,
    p_description       => null);*/
  
    --add by:zhangxing5129
    --add date :2014-05-26
    --走工作流签约
  
    r_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
    select approval_method
      into v_approval_method
      from hls_document_type
     where document_category = r_con_contract_rec.document_category
       and document_type = r_con_contract_rec.document_type;
  
    if v_approval_method = 'WORK_FLOW' then
      con_contract_sign_workflow_pkg.start_workflow(p_contract_id  => p_contract_id,
                                                    p_user_id      => p_user_id,
                                                    p_signing_date => p_signing_date);
    else
      update con_contract c
         set c.contract_status  = 'APPROVING',
             c.signing_date     = p_signing_date,
             c.last_updated_by  = p_user_id,
             c.last_update_date = sysdate
       where c.contract_id = p_contract_id;
    end if;
  
  exception
    when e_calc_quotation_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CALCULATOR_NOT_SUCCESSFUL_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'CONTRACT_SIGN');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'CONTRACT_SIGN');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  end;

  --合同起租
  procedure contract_incept(p_contract_id                number,
                            p_exchange_rate_quotation    varchar2,
                            p_exchange_rate_type         varchar2,
                            p_exchange_rate              number,
                            p_inception_of_lease         date,
                            p_base_rate_new              number default null,
                            p_Refresh_cashflow_only_flag varchar2 default null, --add by chenlingfeng
                            p_user_id                    number) is
    r_con_contract_rec con_contract%rowtype;
    v_calc_successful  varchar2(1);
    v_warning_message  varchar2(2000);
    v_change_req_id    number;
  
    e_period_notfound_err exception;
    e_calc_quotation_err  exception;
    v_inception_of_lease       date;
    v_inception_50_flag        varchar2(1);
    v_first_inception_of_lease date;
    v_lease_end_date           date;
    v_repayment_date_day       number;
    v_first_pay_date           date;
    e_full_write_off_error exception;
    v_count_cf_item number;
  begin
  
    /***************************************************************************/
  
    r_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
  
    /*check contract status*/
    con_contract_verify_pkg.contract_verify(p_contract_rec => r_con_contract_rec,
                                            p_action_type  => con_contract_verify_pkg.c_incept,
                                            p_user_id      => p_user_id);
  
    --add by Harry 9952 2018/10/16
    --若不含直销手续费,则需要完全核销后才可以执行; 若含直销手续费,则代理商手续费可以不必完全核销
    /*select count(*)
      into v_count_301
      from con_contract_cashflow cc
     where cc.contract_id = p_contract_id
       and cc.cf_item = 301
       and cc.times = 0;
    
    if v_count_301 <> 0 then
    
      if r_con_contract_rec.direct_lease_charge_flag = 'Y' then
        null;
      else
      
        select count(*)
          into v_write_off_count
          from con_contract_cashflow cc
         where cc.contract_id = p_contract_id
           and cc.cf_item = 301
           and cc.times = 0
           and cc.write_off_flag <> 'FULL';
      
        if v_write_off_count <> 0 then
          raise e_full_write_off_error;
        end if;
      
      end if;
    
    end if;*/
  
    /*save history*/
    /*con_contract_history_pkg.create_history(p_contract_id => p_contract_id,
                                              p_usage_code  => 'CONTRACT_INCEPT',
                                              p_data_class  => 'HISTORY',
                                              p_user_id     => p_user_id);
    */
  
    --*************************************************************************
    --改为在前台判断逢五逢零
    /*
    --获取首期还款日
    SELECT MAX(ch.first_payment_date)
      INTO v_first_inception_of_lease
      FROM csh_payment_req_hd ch
     WHERE ch.contract_id = p_contract_id
       AND ch.approval_status = 'APPROVED'
       AND EXISTS (SELECT 1
              FROM csh_payment_req_ln    cl,
                   con_contract_cashflow ccc
             WHERE cl.payment_req_id = ch.payment_req_id
               AND cl.ref_doc_line_id = ccc.cashflow_id
               AND ccc.write_off_flag = 'FULL'
               AND ccc.cf_item = 5);
    
    --获取放款日
    SELECT MAX(ch.loan_date)
      INTO v_inception_of_lease
      FROM csh_payment_req_hd ch
     WHERE ch.contract_id = p_contract_id
       AND ch.approval_status = 'APPROVED'
       AND EXISTS (SELECT 1
              FROM csh_payment_req_ln    cl,
                   con_contract_cashflow ccc
             WHERE cl.payment_req_id = ch.payment_req_id
               AND cl.ref_doc_line_id = ccc.cashflow_id
               AND ccc.write_off_flag = 'FULL'
               AND ccc.cf_item = 5);*/
  
    v_inception_of_lease := nvl(p_inception_of_lease,
                                r_con_contract_rec.inception_of_lease);
  
    /*确认的还款日逻辑是:
    交付日期在1号-20号的合同,还款日是次月15号;
    交付日期在21号到月底的合同,还款日是次次月的8号。
    
    判断 可从price_list上判断 为DS_PRICE_60时 ,起租刷新日期 
    为 1-20号为次月的8号,
    21号以后为次次月8号
    add by chenlingfeng for ds 2018年8月2日*/
    if r_con_contract_rec.price_list = 'DS_PRICE_60' then
      v_first_inception_of_lease := con_contract_pkg.
                                    get_inception_of_lease_8(v_inception_of_lease);
    
    else
      v_first_inception_of_lease := con_contract_pkg.get_inception_of_lease(v_inception_of_lease);
    
      --
    end if;
  
    v_repayment_date_day := to_number(to_char(v_first_inception_of_lease,
                                              'dd'));
    --更新现金流
    create_cf_date(p_contract_rec       => r_con_contract_rec,
                   p_inception_of_lease => v_first_inception_of_lease,
                   p_inception_50_flag  => v_inception_50_flag);
  
    --modify by lpc 9874 for deron 更新车款投放的起租日期
  
    /*UPDATE con_contract_cashflow
      SET calc_date       = v_first_inception_of_lease,
          due_date        = v_first_inception_of_lease,
          fin_income_date = v_first_inception_of_lease,
          ln_user_col_d01 = v_first_inception_of_lease
    WHERE contract_id = p_contract_id
      AND cf_item = 5
      AND times = 0;*/
    --modfiy by chenlingfeng 更新0期所有现金流
    update con_contract_cashflow
       set calc_date       = r_con_contract_rec.lease_execution_date,
           due_date        = r_con_contract_rec.lease_execution_date,
           fin_income_date = r_con_contract_rec.lease_execution_date,
           ln_user_col_d01 = r_con_contract_rec.lease_execution_date
     where contract_id = p_contract_id
          
       and times = 0;
    --end
    con_contract_custom_pkg.before_contract_incept(p_contract_id          => p_contract_id,
                                                   p_inception_of_lease   => v_inception_of_lease,
                                                   p_lease_execution_date => r_con_contract_rec.lease_execution_date,
                                                   p_user_id              => p_user_id);
  
    --v_first_pay_date
  
    select min(ccc.due_date)
      into v_first_pay_date
      from con_contract_cashflow ccc
     where ccc.cf_status = 'RELEASE'
       and ccc.cf_direction = 'INFLOW'
       and ccc.due_date >= r_con_contract_rec.lease_execution_date
       and ccc.times <> 0
       and ccc.contract_id = p_contract_id;
  
    /*update contract status*/
    update con_contract
       set first_pay_date = v_first_pay_date, --v_first_inception_of_lease,
           
           last_updated_by  = p_user_id,
           last_update_date = sysdate
     where contract_id = p_contract_id;
  
    if p_Refresh_cashflow_only_flag = 'Y' then
      return;
    end if;
    begin
      select max(ccc.calc_date)
        into v_lease_end_date
        from con_contract_cashflow ccc
       where ccc.contract_id = p_contract_id
         and ccc.cf_item = '1'
         and ccc.cf_direction <> 'NONCASH';
    exception
      when others then
        null;
    end;
  
    /*update contract status*/
    update con_contract
       set contract_status         = 'INCEPT',
           exchange_rate_quotation = nvl(p_exchange_rate_quotation,
                                         'DIRECT QUOTATION'),
           exchange_rate_type      = nvl(p_exchange_rate_type, 'MANUAL'),
           exchange_rate           = nvl(p_exchange_rate, 1),
           inception_of_lease      = v_inception_of_lease,
           lease_start_date        = v_inception_of_lease,
           lease_end_date          = nvl(v_lease_end_date,
                                         add_months(v_first_inception_of_lease,
                                                    round(r_con_contract_rec.lease_term * 12,
                                                          0)) - 1),
           lease_execution_flag    = 'Y',
           first_pay_date          = v_first_pay_date, --v_first_inception_of_lease,
           repayment_date_day      = v_repayment_date_day, --还款日8 th/15 th
           last_updated_by         = p_user_id,
           last_update_date        = sysdate
     where contract_id = p_contract_id;
  
    update con_contract_cashflow
       set exchange_rate_quotation = nvl(p_exchange_rate_quotation,
                                         'DIRECT QUOTATION'),
           exchange_rate_type      = nvl(p_exchange_rate_type, 'MANUAL'),
           exchange_rate           = nvl(p_exchange_rate, 1)
     where contract_id = p_contract_id;
  
    if r_con_contract_rec.currency <>
       gld_common_pkg.get_company_currency_code(p_company_id => r_con_contract_rec.company_id) then
      --重新计算计算器
      update hls_fin_calculator_hd_formula
         set exchange_rate_quotation = nvl(p_exchange_rate_quotation,
                                           'DIRECT QUOTATION'),
             exchange_rate_type      = nvl(p_exchange_rate_type, 'MANUAL'),
             exchange_rate           = nvl(p_exchange_rate, 1),
             inception_of_lease      = to_char(p_inception_of_lease,
                                               'YYYY-MM-DD'),
             lease_start_date        = to_char(p_inception_of_lease,
                                               'YYYY-MM-DD')
       where calc_session_id = r_con_contract_rec.calc_session_id;
    
      hls_fin_calculator_itfc_pkg.calculate(p_calc_session_id    => r_con_contract_rec.calc_session_id,
                                            p_recreate_h_formula => 'N',
                                            p_recreate_l_formula => 'N',
                                            p_calc_successful    => v_calc_successful,
                                            p_warning_message    => v_warning_message,
                                            p_user_id            => p_user_id);
      if v_calc_successful <> 'Y' then
        raise e_calc_quotation_err;
      end if;
    
      /*save contract from calculator*/
      save_contract_from_calculator(p_contract_id      => p_contract_id,
                                    p_calc_session_id  => r_con_contract_rec.calc_session_id,
                                    p_save_contract_hd => 'Y',
                                    p_save_contract_ln => 'Y',
                                    p_user_id          => p_user_id);
    
    end if;
  
    /* 二开程序 */
    con_contract_custom_pkg.after_contract_incept(p_contract_id             => p_contract_id,
                                                  p_exchange_rate_quotation => p_exchange_rate_quotation,
                                                  p_exchange_rate_type      => p_exchange_rate_type,
                                                  p_exchange_rate           => p_exchange_rate,
                                                  p_inception_of_lease      => p_inception_of_lease,
                                                  p_user_id                 => p_user_id);
  
    --add by chenlingfeng
    --把这段逻辑提到前面 
    /*con_contract_custom_pkg.before_contract_incept(p_contract_id          => p_contract_id,
    p_inception_of_lease   => p_inception_of_lease,
    p_lease_execution_date => r_con_contract_rec.lease_execution_date,
    p_user_id              => p_user_id);*/
    --modify by wcs 2014-12-2
    if r_con_contract_rec.int_rate_type = 'FLOATING' then
      /*floating_rate_adj*/
      con_floating_interest_rate_pkg.floating_rate_adj(p_contract_id   => p_contract_id,
                                                       p_adj_date      => p_inception_of_lease,
                                                       p_base_rate_new => p_base_rate_new,
                                                       p_change_req_id => v_change_req_id,
                                                       p_user_id       => p_user_id);
    
      if v_change_req_id is not null then
        con_floating_interest_rate_pkg.floating_rate_adj_submit(p_change_req_id => v_change_req_id,
                                                                p_user_id       => p_user_id);
      
        con_floating_interest_rate_pkg.floating_rate_adj_confirm(p_change_req_id => v_change_req_id,
                                                                 p_user_id       => p_user_id);
      
        /*else
          \*calc_cny_amt*\
          con_floating_interest_rate_pkg.save_change_req(p_change_req_id   => v_change_req_id,
                                                         p_contract_rec    => r_con_contract_rec,
                                                         p_adj_date        => p_inception_of_lease,
                                                         p_base_rate_new   => r_con_contract_rec.base_rate,
                                                         p_adj_start_times => 1,
                                                         p_user_id         => p_user_id);
        
          begin
            select *
              into r_hls_fin_calc_method_rec
              from hls_fin_calc_method
             where calc_method = r_con_contract_rec.calc_method;
          exception
            when no_data_found then
              r_hls_fin_calc_method_rec.split_pi := 'N';
          end;
        
          con_floating_interest_rate_pkg.calc_cny_amt(p_contract_rec            => r_con_contract_rec,
                                                      p_change_req_id           => v_change_req_id,
                                                      p_adj_start_times         => 1,
                                                      p_hls_fin_calc_method_rec => r_hls_fin_calc_method_rec,
                                                      p_user_id                 => p_user_id);
        
          con_floating_interest_rate_pkg.floating_rate_adj_submit(p_change_req_id => v_change_req_id,
                                                                  p_user_id       => p_user_id);
        
          con_floating_interest_rate_pkg.floating_rate_adj_confirm(p_change_req_id => v_change_req_id,
                                                                   p_user_id       => p_user_id);
        
        */
      
      end if;
    end if;
    /* unearned_fin_income */
    /*begin
      select a.internal_period_num
        into v_start_internal_period_num
        from gld_periods a, fnd_companies b, gld_set_of_books c
       where b.company_id = r_con_contract_rec.company_id
         and b.set_of_books_id = c.set_of_books_id
         and c.period_set_code = a.period_set_code
         and a.adjustment_flag = 'N'
         and p_inception_of_lease between a.start_date and a.end_date;
    exception
      when no_data_found then
        raise e_period_notfound_err;
    end;*/
    -- ??
    con_unearned_fin_income_pkg.allocate_unearned_fin_income(p_contract_id               => p_contract_id,
                                                             p_start_internal_period_num => null,
                                                             p_user_id                   => p_user_id);
    --modify by lpc 9874 for doosan 手续费分摊
    --modify by chenlingfeng 添加在通用分摊中
    /*SELECT COUNT(*)
      INTO v_count_cf_item
      FROM con_contract_cashflow cc
     WHERE cc.contract_id = p_contract_id
       AND cf_item IN (3, 301);
    IF v_count_cf_item > 0 THEN
      con_unearned_fin_income_pkg.fee_sharing(p_contract_id => p_contract_id,
                                              p_user_id     => p_user_id);
    end if;*/
    /* create lease_incept je */
    con_contract_je_pkg.create_lease_incept_je(p_contract_id => p_contract_id,
                                               p_user_id     => p_user_id);
  
    --add by chenlingfeng --发送短信
    ds_send_sms_core_pkg.send_con_new_customer_sms(p_contract_id => p_contract_id,
                                                   p_user_id     => p_user_id);
    --end                                           
  
    --插入合同操作历史
    hls_doc_operate_history_pkg.insert_doc_operate_history(p_document_category => 'CONTRACT',
                                                           p_document_id       => p_contract_id,
                                                           p_operation_code    => 'CON_INCEPT',
                                                           p_user_id           => p_user_id,
                                                           p_operation_time    => sysdate,
                                                           p_description       => null);
    yonda_doc_history_pkg.yonda_insert_doc_status(p_document_id       => r_con_contract_rec.contract_id,
                                                  p_document_type     => r_con_contract_rec.document_type,
                                                  p_document_category => r_con_contract_rec.document_category,
                                                  p_doc_status        => yonda_doc_history_pkg.yonda_con_incept,
                                                  p_instance_id       => null,
                                                  p_user_id           => p_user_id);
  exception
    when e_calc_quotation_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CALCULATOR_NOT_SUCCESSFUL_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'CONTRACT_INCEPT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_period_notfound_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_PKG.CONTRACT_INCEPTION_PERIOD_NOTFOUND_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_PKG',
                                                      p_procedure_function_name => 'CONTRACT_INCEPT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_full_write_off_error then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '代理商手续费未完全核销,请检查!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'CONTRACT_INCEPT');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'CONTRACT_INCEPT');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  end;

  procedure contract_incept_rd_wf(p_contract_id number, p_user_id number) is
  begin
    contract_incept(p_contract_id             => p_contract_id,
                    p_exchange_rate_quotation => null,
                    p_exchange_rate_type      => null,
                    p_exchange_rate           => 1,
                    p_inception_of_lease      => trunc(sysdate),
                    --暂时传入当前日,待需求去确认后更改
                    p_base_rate_new => null,
                    p_user_id       => p_user_id);
  
  end;

  procedure contract_update_check(p_contract_id     number,
                                  p_user_id         number,
                                  p_contract_number out varchar2) is
  
    r_con_contract_rec con_contract%rowtype;
    v_contract_number  varchar2(100);
  
    v_dealer_cd    varchar2(50);
    v_chance_no    varchar2(50);
    v_Machine_Type varchar2(50);
    -- v_num              number;
    -- e_many_rows_err exception;
    -- e_test_err exception;
  begin
  
    r_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
  
    --执行审查无误更新时间                                     
    if r_con_contract_rec.execution_status = 'REVIEW_PASS' then
    
      update con_contract cc
         set cc.submit_date = nvl(cc.submit_date, trunc(sysdate))
       where cc.contract_id = r_con_contract_rec.contract_id;
    else
      update con_contract cc
         set cc.submit_date = null
       where cc.contract_id = r_con_contract_rec.contract_id;
    
    end if;
    --更新bp 带出默认值
    update con_contract_bp t
       set (t.bp_code,
            t.bp_class,
            t.bp_name,
            t.id_type,
            t.id_card_no,
            t.organization_code,
            t.tax_registry_num,
            t.cell_phone,
            t.id_card_address,
            t.SEND_ADDRESS,
            t.liv_street,
            t.phone,
            t.phone_2,
            t.bp_name_sp,
            t.id_no_sp,
            t.id_card_address_sp,
            t.cell_phone_sp,
            t.bp_name_leg,
            t.id_card_no_leg,
            t.default_value_flag) =
           (select t1.bp_code,
                   t1.bp_class,
                   t1.bp_name,
                   t1.id_type,
                   t1.id_card_no,
                   t1.organization_code,
                   t1.tax_registry_num,
                   nvl(t.cell_phone, t1.cell_phone),
                   nvl(t.id_card_address, t1.id_card_address),
                   nvl(t.SEND_ADDRESS, t1.SEND_ADDRESS),
                   nvl(t.liv_street, t1.liv_street),
                   nvl(t.phone, t1.phone),
                   nvl(t.phone_2, t1.phone_2),
                   t1.bp_name_sp,
                   nvl(t.id_no_sp, t1.id_no_sp),
                   nvl(t.id_card_address_sp, t1.id_card_address_sp),
                   nvl(t.cell_phone_sp, t1.cell_phone_sp),
                   t1.bp_name_leg,
                   nvl(t.id_card_no_leg, t1.id_card_no_leg),
                   'Y'
              from hls_bp_master t1
             where t1.bp_id = t.bp_id)
     where t.contract_id = r_con_contract_rec.contract_id
       and nvl(t.default_value_flag, 'N') = 'N';
    --
    v_contract_number := r_con_contract_rec.contract_number;
    p_contract_number := v_contract_number;
  
    -- 50节点 防止并发处理 20160808  start
    -- 1.首保与GPS上传
    --if v_num > 1 then
    --  raise e_many_rows_err;
    --end if;
  
    -- 2.附件信息
    -- end
  
    /*IF r_con_contract_rec.contract_status = 'NEW' THEN
      RETURN;
    END IF;*/
  
    begin
    
      select t2.dealer_cd, t2.chance_no, T2.Machine_Type
        into v_dealer_cd, v_chance_no, v_Machine_Type
        from con_contract t1, prj_project t2
       where t1.project_id = t2.project_id
            
         and t1.contract_id = p_contract_id;
    
      ds_document_interface_pkg.dcs_insert_prj_lease_item(p_dealer_cd    => v_dealer_cd,
                                                          p_chance_no    => v_chance_no,
                                                          p_machine_type => v_Machine_Type);
    exception
      when others then
        sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                    sqlerrm,
                                                       p_created_by              => p_user_id,
                                                       p_package_name            => 'CON_CONTRACT_PKG',
                                                       p_procedure_function_name => 'CONTRACT_UPDATE_CHECK');
      
    end;
  
    /*check contract status*/
    con_contract_verify_pkg.contract_verify(p_contract_rec => r_con_contract_rec,
                                            p_action_type  => con_contract_verify_pkg.c_update_check,
                                            p_user_id      => p_user_id);
  
    aut_document_authority_pkg.insert_trx_user_authority(p_company_id    => r_con_contract_rec.company_id,
                                                         p_owner_user_id => r_con_contract_rec.owner_user_id,
                                                         p_trx_category  => 'CONTRACT',
                                                         p_trx_id        => p_contract_id,
                                                         p_start_date    => trunc(sysdate),
                                                         p_end_date      => to_date('30000101',
                                                                                    'yyyymmdd'),
                                                         p_user_id       => p_user_id);
  
  exception
    /*
    when e_many_rows_err then
      rollback;
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '页面存在并发,请关闭页面再操作!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'contract_update_check');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
      return;*/
  
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'CONTRACT_UPDATE_CHECK');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  end;

  procedure contract_cancel(p_contract_id number, p_user_id number) is
  
    r_con_contract_rec con_contract%rowtype;
  begin
    r_con_contract_rec := get_contract_rec(p_contract_id => p_contract_id,
                                           p_user_id     => p_user_id);
  
    /*check contract status*/
    con_contract_verify_pkg.contract_verify(p_contract_rec => r_con_contract_rec,
                                            p_action_type  => con_contract_verify_pkg.c_cancel,
                                            p_user_id      => p_user_id);
  
    /*update contract status*/
    update con_contract
       set contract_status  = 'CANCEL',
           last_updated_by  = p_user_id,
           last_update_date = sysdate
     where contract_id = p_contract_id;
  
    --插入合同操作历史
    hls_doc_operate_history_pkg.insert_doc_operate_history(p_document_category => 'CONTRACT',
                                                           p_document_id       => p_contract_id,
                                                           p_operation_code    => 'CON_CANCEL',
                                                           p_user_id           => p_user_id,
                                                           p_operation_time    => sysdate,
                                                           p_description       => null);
  
  exception
    when others then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  sqlerrm,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CON_CONTRACT_PKG',
                                                     p_procedure_function_name => 'CONTRACT_CANCEL');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  end contract_cancel;

  --计算期数为0的due_date
  procedure create_cf0_date(p_con_contract_rec con_contract%rowtype,
                            p_due_date         date,
                            p_user_id          number) is
    v_calc_date date;
    v_due_date  date;
    v_fin_date  date;
  
  begin
  
    for v_record in (select *
                       from con_contract_cashflow t
                      where t.contract_id = p_con_contract_rec.contract_id
                        and t.times = 0
                        and t.cf_status = 'RELEASE') loop
      v_calc_date := p_due_date + 3;
      v_due_date  := nextduedate(v_calc_date,
                                 0,
                                 p_con_contract_rec.biz_day_convention);
      v_fin_date  := v_calc_date;
      update con_contract_cashflow t
         set t.calc_date        = v_calc_date,
             t.due_date         = v_due_date,
             t.fin_income_date  = v_fin_date,
             t.ln_user_col_d01  = v_calc_date,
             t.last_updated_by  = p_user_id,
             t.last_update_date = sysdate
       where cashflow_id = v_record.cashflow_id;
    
    end loop;
  end;

  -- 签约提交 改变信审、及法务审核状态

  function get_fin_amount(p_bp_id number) return number is
    v_fin_amount number;
    v_agent_type hls_bp_master.agent_type%type;
  begin
  
    select hm.agent_type
      into v_agent_type
      from hls_bp_master hm
     where hm.bp_id = p_bp_id;
  
    if v_agent_type = 'AGENT_GRT_DEPOSIT' then
      select sum(nvl(ccc.due_amount,0) - nvl(ccc.received_amount, 0))
        into v_fin_amount
        from con_contract_cashflow ccc
       where ccc.times <> 0
         and (ccc.cf_item = 1 or ccc.cf_item = 913)
         and ccc.cf_status = 'RELEASE'
         and ccc.contract_id in
             (select c1.contract_id
                from con_contract c1
               where c1.bp_id_agent_level1 = p_bp_id
                 and c1.contract_status = 'INCEPT'
                 and c1.data_class = 'NORMAL'
              union all
              select c.contract_id
                from con_contract c, hls_bp_master h
               where c.bp_id_agent_level1 = h.bp_id
                 and c.contract_status = 'INCEPT'
                 and c.data_class = 'NORMAL'
                 and h.parent_id = p_bp_id);
      return v_fin_amount;
    else
      return 99999999999;
    end if;
  end get_fin_amount;

  -- 合同维护判断是否为NP
  procedure get_bp_class(p_bp_id_tenant  number,
                         p_bp_class_flag in out varchar2) is
    v_bp_class_flag varchar2(100);
  
  begin
    select hb.bp_class
      into v_bp_class_flag
      from hls_bp_master hb
     where hb.bp_id = p_bp_id_tenant;
    p_bp_class_flag := v_bp_class_flag;
  end;

  procedure income_stat(p_date_from date, p_date_to date, p_user_id number) is
    v_date                date := trunc(p_date_from, 'MONTH');
    v_discount            number;
    v_write_off_principal number;
    v_write_off_interest  number;
    v_discount_agent      number;
    v_deposit_agent       number;
    v_deposit_tenant      number;
  begin
    delete from income_stat_temp;
    loop
    
      select sum(al.total_amount)
        into v_write_off_principal
        from acr_invoice_ln al, acr_invoice_hd ah
       where ah.invoice_date >= v_date
         and ah.invoice_date <= last_day(v_date)
         and ah.invoice_hd_id = al.invoice_hd_id
         and al.cf_item = 100;
    
      select sum(al.total_amount)
        into v_discount_agent
        from acr_invoice_ln al, acr_invoice_hd ah
       where ah.invoice_date >= v_date
         and ah.invoice_date <= last_day(v_date)
         and ah.invoice_hd_id = al.invoice_hd_id
         and al.cf_item = 912;
    
      select sum(al.total_amount)
        into v_discount
        from acr_invoice_ln al, acr_invoice_hd ah
       where ah.invoice_date >= v_date
         and ah.invoice_date <= last_day(v_date)
         and ah.invoice_hd_id = al.invoice_hd_id
         and al.cf_item = 913;
    
      select sum(al.total_amount)
        into v_write_off_interest
        from acr_invoice_ln al, acr_invoice_hd ah
       where ah.invoice_date >= v_date
         and ah.invoice_date <= last_day(v_date)
         and ah.invoice_hd_id = al.invoice_hd_id
         and al.cf_item = 101;
    
      select sum(al.total_amount)
        into v_deposit_agent
        from acr_invoice_ln al, acr_invoice_hd ah
       where ah.invoice_date >= v_date
         and ah.invoice_date <= last_day(v_date)
         and ah.invoice_hd_id = al.invoice_hd_id
         and al.cf_item = 55;
    
      select sum(al.total_amount)
        into v_deposit_tenant
        from acr_invoice_ln al, acr_invoice_hd ah
       where ah.invoice_date >= v_date
         and ah.invoice_date <= last_day(v_date)
         and ah.invoice_hd_id = al.invoice_hd_id
         and al.cf_item = 52;
    
      insert into income_stat_temp
      values
        (to_char(v_date, 'yyyy-mm'),
         nvl(v_discount, 0),
         nvl(v_deposit_agent, 0),
         nvl(v_discount, 0) + nvl(v_write_off_interest, 0) +
         nvl(v_write_off_principal, 0) + nvl(v_deposit_agent, 0),
         nvl(v_deposit_tenant, 0),
         nvl(v_discount, 0) + nvl(v_write_off_interest, 0) +
         nvl(v_write_off_principal, 0) + nvl(v_deposit_agent, 0) +
         nvl(v_deposit_tenant, 0),
         nvl(v_write_off_principal, 0),
         nvl(v_write_off_interest, 0),
         nvl(v_discount_agent, 0));
    
      v_date := add_months(v_date, 1);
      exit when v_date > p_date_to;
    end loop;
  end income_stat;

  function check_grt_amount(p_user_id number) return varchar2 is
    v_check_flag varchar2(1);
    v_agent_type hls_bp_master.agent_type%type;
    v_grt_amount number;
    v_fin_amount number;
    p_bp_id      number;
  begin
    select su.bp_id
      into p_bp_id
      from sys_user su
     where su.user_id = p_user_id;
  
    select hm.agent_type
      into v_agent_type
      from hls_bp_master hm
     where hm.bp_id = p_bp_id;
  
    if v_agent_type = 'AGENT_GRT_DEPOSIT' then
      select nvl(a.transaction_amount, 0) - nvl(a.write_off_amount, 0) -
             nvl(a.returned_amount, 0)
        into v_grt_amount
        from csh_transaction a
       where a.transaction_type = 'DEPOSIT'
         and a.returned_flag != 'RETURN'
         and a.bp_id = p_bp_id;
    
      select sum(nvl(ccc.due_amount,0) - nvl(ccc.received_amount, 0))
        into v_fin_amount
        from con_contract_cashflow ccc
       where ccc.times <> 0
         and (ccc.cf_item = 1 or ccc.cf_item = 913)
         and ccc.cf_status = 'RELEASE'
         and ccc.contract_id in
             (select c1.contract_id
                from con_contract c1
               where c1.bp_id_agent_level1 = p_bp_id
                 and c1.contract_status = 'INCEPT'
                 and c1.data_class = 'NORMAL'
              union all
              select c.contract_id
                from con_contract c, hls_bp_master h
               where c.bp_id_agent_level1 = h.bp_id
                 and c.contract_status = 'INCEPT'
                 and c.data_class = 'NORMAL'
                 and h.parent_id = p_bp_id);
      if nvl(v_grt_amount, 0) >= 0.1 * nvl(v_fin_amount, 0) then
        v_check_flag := 'Y';
      else
        v_check_flag := 'N';
      end if;
    elsif v_agent_type = 'AGENT_NO_REBATE' then
      select nvl(a.transaction_amount, 0) - nvl(a.write_off_amount, 0) -
             nvl(a.returned_amount, 0)
        into v_grt_amount
        from csh_transaction a, hls_bp_master hm
       where a.transaction_type = 'DEPOSIT'
         and a.bp_id = hm.parent_id
         and a.returned_flag != 'RETURN'
         and hm.bp_id = p_bp_id;
    
      select sum(nvl(ccc.due_amount,0) - nvl(ccc.received_amount, 0))
        into v_fin_amount
        from con_contract_cashflow ccc
       where ccc.times <> 0
         and (ccc.cf_item = 1 or ccc.cf_item = 913)
         and ccc.cf_status = 'RELEASE'
         and ccc.contract_id in
             (select c1.contract_id
                from con_contract c1
               where c1.bp_id_agent_level1 in
                     (select hm.bp_id
                        from hls_bp_master hm
                       where hm.parent_id =
                             (select h.parent_id
                                from hls_bp_master h
                               where h.bp_id = p_bp_id))
                 and c1.contract_status = 'INCEPT'
                 and c1.data_class = 'NORMAL'
              union all
              select c.contract_id
                from con_contract c, hls_bp_master h
               where c.bp_id_agent_level1 = h.parent_id
                 and c.contract_status = 'INCEPT'
                 and c.data_class = 'NORMAL'
                 and h.bp_id = p_bp_id);
      if nvl(v_grt_amount, 0) >= 0.1 * nvl(v_fin_amount, 0) then
        v_check_flag := 'Y';
      else
        v_check_flag := 'N';
      end if;
    end if;
    return v_check_flag;
  end check_grt_amount;

  procedure ACCOUNT_QUERY(p_account_type varchar2, --1:按照创建日期last_update_DATE统计查询  2:按照凭证日期JOURNAL_DATE统计查询查询
                          p_date_from    date,
                          p_date_to      date,
                          p_user_id      number) is
    v_date                   date;
    V_OPENING_AMOUNT         number;
    v_opening_amount_initial number;
    v_amount_dr              number;
    v_amount_cr              number;
    V_CLOSING_AMOUNT         number;
    V_DAY_AMOUNT_DR          number;
    V_DAY_AMOUNT_CR          number;
    V_CNT                    number;
  begin
    ---根据创建日期last_update_DATE统计查询2019/02/18
    if (p_account_type = 1) then
      delete from account_query_temp where open_flag <> 1;
      for rec in (select distinct hd.account_id
                    from hls_journal_detail hd
                   where not exists
                   (select *
                            from hls_journal_header a
                           where a.journal_header_id = hd.journal_header_id
                             and a.merge_parent_flag = 'Y')) loop
        v_date := p_date_from;
        if V_DATE <= P_DATE_TO then
          select sum(nvl(hd.amount_dr, 0) - nvl(hd.amount_cr, 0))
            into v_opening_amount
            from HLS_JOURNAL_DETAIL HD
           where (select to_char(h.last_update_DATE, 'yyyyMMdd')
                    from hls_journal_header h
                   where h.journal_header_id = hd.journal_header_id) <=
                 to_char(v_date, 'yyyyMMdd')
             and hd.account_id = rec.account_id;
        
          select count(*)
            into V_CNT
            from ACCOUNT_QUERY_INITIAL AQT
           where AQT.OPEN_FLAG = 1
             and AQT.ACCOUNT_ID = REC.ACCOUNT_ID;
          if V_CNT > 0 then
            select NVL(DAY_AMOUNT_DR, 0) - NVL(DAY_AMOUNT_CR, 0)
              into V_OPENING_AMOUNT_INITIAL
              from ACCOUNT_QUERY_INITIAL AQT
             where AQT.OPEN_FLAG = 1
               and AQT.ACCOUNT_ID = REC.ACCOUNT_ID;
          else
            V_OPENING_AMOUNT_INITIAL := 0;
          end if;
          /*
          SELECT SUM(nvl(hd.amount_dr, 0) - nvl(hd.amount_cr, 0))
            INTO v_closing_amount
            FROM hls_journal_detail hd
           WHERE (SELECT h.CREATION_DATE
                    FROM hls_journal_header h
                   WHERE h.journal_header_id = hd.journal_header_id) <=
                 p_date_to
             AND HD.ACCOUNT_ID = REC.ACCOUNT_ID;
             */
          --借方金额汇总取自凭证表按照CREATION_DATE统计
          select sum(nvl(hd.amount_dr, 0))
            into v_amount_dr
            from hls_journal_detail hd
           where (select TO_CHAR(h.last_update_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) >=
                 to_char(v_date, 'yyyyMMdd')
             and (select TO_CHAR(h.last_update_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) <=
                 to_char(p_date_to, 'yyyyMMdd')
             and HD.ACCOUNT_ID = REC.ACCOUNT_ID;
          ----贷方金额汇总取自凭证表按照CREATION_DATE统计
          select sum(nvl(hd.amount_cr, 0))
            into v_amount_cr
            from hls_journal_detail hd
           where (select TO_CHAR(h.last_update_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where h.journal_header_id = hd.journal_header_id) >=
                 to_char(v_date, 'yyyyMMdd')
             and (select TO_CHAR(h.last_update_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where h.journal_header_id = hd.journal_header_id) <=
                 to_char(p_date_to, 'yyyyMMdd')
             and HD.ACCOUNT_ID = REC.ACCOUNT_ID;
          --借方金额汇总取自凭证表按照p_date_to统计
          select sum(NVL(HD.AMOUNT_DR, 0))
            into v_day_amount_dr
            from HLS_JOURNAL_DETAIL HD
           where (select TO_CHAR(h.last_update_DATE, 'yyyyMMDD')
                    from HLS_JOURNAL_HEADER H
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) =
                 to_char(p_date_to, 'yyyyMMdd')
             and HD.ACCOUNT_ID = REC.ACCOUNT_ID;
          ----贷方金额汇总取自凭证表按照p_date_to统计
          select sum(NVL(HD.AMOUNT_CR, 0))
            into v_day_amount_cr
            from HLS_JOURNAL_DETAIL HD
           where (select TO_CHAR(h.last_update_DATE, 'yyyyMMDD')
                    from HLS_JOURNAL_HEADER H
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) =
                 to_char(p_date_to, 'yyyyMMdd')
             and hd.account_id = rec.account_id;
        
          insert into ACCOUNT_QUERY_TEMP
            (ACCOUNT_ID,
             TRANSACTION_DATE,
             OPENING_BALANCE,
             AMOUNT_DR,
             AMOUNT_CR,
             DAY_AMOUNT_DR,
             DAY_AMOUNT_CR,
             OPEN_FLAG,
             PERIOD_FROM,
             PERIOD_TO,
             ACCOUNT_TYPE)
          values
            (rec.account_id,
             V_DATE,
             nvl(v_opening_amount_initial, 0) + nvl(v_opening_amount, 0),
             v_amount_dr,
             V_AMOUNT_CR,
             V_DAY_AMOUNT_DR,
             V_DAY_AMOUNT_CR,
             0,
             p_date_from,
             p_date_to,
             p_account_type);
        end if;
      end loop;
      ---根据凭证日期JOURNAL_DATE统计查询2019/02/18
    else
      delete from account_query_temp;
      for rec in (select distinct hd.account_id
                    from hls_journal_detail hd
                   where not exists
                   (select *
                            from hls_journal_header a
                           where a.journal_header_id = hd.journal_header_id
                             and a.merge_parent_flag = 'Y')) loop
        v_date := p_date_from;
        if V_DATE <= P_DATE_TO then
          select sum(nvl(hd.amount_dr, 0) - nvl(hd.amount_cr, 0))
            into v_opening_amount
            from HLS_JOURNAL_DETAIL HD
           where (select to_char(h.JOURNAL_DATE, 'yyyyMMdd')
                    from hls_journal_header h
                   where h.journal_header_id = hd.journal_header_id) <=
                 to_char(v_date, 'yyyyMMdd')
             and hd.account_id = rec.account_id;
        
          select count(*)
            into V_CNT
            from ACCOUNT_QUERY_INITIAL AQT
           where AQT.OPEN_FLAG = 1
             and AQT.ACCOUNT_ID = REC.ACCOUNT_ID;
          if V_CNT > 0 then
            select NVL(DAY_AMOUNT_DR, 0) - NVL(DAY_AMOUNT_CR, 0)
              into V_OPENING_AMOUNT_INITIAL
              from ACCOUNT_QUERY_INITIAL AQT
             where AQT.OPEN_FLAG = 1
               and AQT.ACCOUNT_ID = REC.ACCOUNT_ID;
          else
            V_OPENING_AMOUNT_INITIAL := 0;
          end if;
          /*
          SELECT SUM(nvl(hd.amount_dr, 0) - nvl(hd.amount_cr, 0))
            INTO v_closing_amount
            FROM hls_journal_detail hd
           WHERE (SELECT h.CREATION_DATE
                    FROM hls_journal_header h
                   WHERE h.journal_header_id = hd.journal_header_id) <=
                 p_date_to
             AND HD.ACCOUNT_ID = REC.ACCOUNT_ID;
             */
          --借方金额汇总取自凭证表按照CREATION_DATE统计
          select sum(nvl(hd.amount_dr, 0))
            into v_amount_dr
            from hls_journal_detail hd
           where (select TO_CHAR(h.JOURNAL_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) >=
                 to_char(v_date, 'yyyyMMdd')
             and (select TO_CHAR(h.JOURNAL_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) <=
                 to_char(p_date_to, 'yyyyMMdd')
             and HD.ACCOUNT_ID = REC.ACCOUNT_ID;
          ----贷方金额汇总取自凭证表按照CREATION_DATE统计
          select sum(nvl(hd.amount_cr, 0))
            into v_amount_cr
            from hls_journal_detail hd
           where (select TO_CHAR(h.JOURNAL_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where h.journal_header_id = hd.journal_header_id) >=
                 to_char(v_date, 'yyyyMMdd')
             and (select TO_CHAR(h.JOURNAL_DATE, 'yyyyMMDD')
                    from hls_journal_header h
                   where h.journal_header_id = hd.journal_header_id) <=
                 to_char(p_date_to, 'yyyyMMdd')
             and HD.ACCOUNT_ID = REC.ACCOUNT_ID;
          --借方金额汇总取自凭证表按照p_date_to统计
          select sum(NVL(HD.AMOUNT_DR, 0))
            into v_day_amount_dr
            from HLS_JOURNAL_DETAIL HD
           where (select TO_CHAR(h.JOURNAL_DATE, 'yyyyMMDD')
                    from HLS_JOURNAL_HEADER H
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) =
                 to_char(p_date_to, 'yyyyMMdd')
             and HD.ACCOUNT_ID = REC.ACCOUNT_ID;
          ----贷方金额汇总取自凭证表按照p_date_to统计
          select sum(NVL(HD.AMOUNT_CR, 0))
            into v_day_amount_cr
            from HLS_JOURNAL_DETAIL HD
           where (select TO_CHAR(h.JOURNAL_DATE, 'yyyyMMDD')
                    from HLS_JOURNAL_HEADER H
                   where H.JOURNAL_HEADER_ID = HD.JOURNAL_HEADER_ID) =
                 to_char(p_date_to, 'yyyyMMdd')
             and hd.account_id = rec.account_id;
        
          insert into ACCOUNT_QUERY_TEMP
            (ACCOUNT_ID,
             TRANSACTION_DATE,
             OPENING_BALANCE,
             AMOUNT_DR,
             AMOUNT_CR,
             DAY_AMOUNT_DR,
             DAY_AMOUNT_CR,
             OPEN_FLAG,
             PERIOD_FROM,
             PERIOD_TO,
             ACCOUNT_TYPE)
          values
            (rec.account_id,
             V_DATE,
             nvl(v_opening_amount_initial, 0) + nvl(v_opening_amount, 0),
             v_amount_dr,
             V_AMOUNT_CR,
             V_DAY_AMOUNT_DR,
             V_DAY_AMOUNT_CR,
             0,
             p_date_from,
             p_date_to,
             p_account_type);
        end if;
      end loop;
    end if;
  
  end account_query;

  --是否逾期
  function get_if_overdue(p_cashflow_id number) return varchar2 is
    v_count      number;
    v_if_overdue varchar2(200);
  begin
    select count(1)
      into v_count
      from con_contract_cashflow co
     where co.cashflow_id = p_cashflow_id
       and co.due_date < sysdate
       and nvl(co.due_amount, 0) > nvl(co.received_amount, 0);
    if v_count = 0 then
      v_if_overdue := 'NORMAL';
    else
      v_if_overdue := 'OVERDUE';
    end if;
    return v_if_overdue;
  end;

  --获取担保人
  function get_guarantor(p_contract_id number) return varchar2 is
    v_bp_guarantor varchar2(2000); --担保人
  begin
    for rec in (select t.bp_name
                  from con_contract_bp_lv t
                 where bp_category = 'GUARANTOR'
                   and t.contract_id = p_contract_id
                   and t.enabled_flag = 'Y') loop
      v_bp_guarantor := v_bp_guarantor || rec.bp_name || ';';
    end loop;
    return v_bp_guarantor;
  end;

  procedure default_idtype_idcode_mobile(p_bp_id_tenant number,
                                         p_user_id      number,
                                         p_idtype       out varchar2,
                                         p_idtype_n     out varchar2,
                                         p_idcode       out varchar2,
                                         p_mobile       out varchar2) is
  
  begin
    select l.id_type    as idtype,
           l.id_type_n  as idtype_n,
           l.id_card_no as idcode,
           l.phone      as mobile
      into p_idtype, p_idtype_n, p_idcode, p_mobile
      from hls_bp_master_lv l
     where l.bp_id = p_bp_id_tenant;
  exception
    when others then
      null;
    
  end;

  --得到产品是否自定义
  procedure get_if_self_definition(p_product_id         number,
                                   p_user_id            number,
                                   p_if_self_definition out varchar2) is
  
  begin
    select if_self_definition
      into p_if_self_definition
      from hls_product_plan_definition h
     where h.product_plan_id = p_product_id;
  exception
    when others then
      null;
  end;

  --modify by lpc 9874 for deron 逢5逢0计算第一个合同的起租日
  function get_first_contract_day(p_contract_id number) return varchar2 is
    v_first_contract_day varchar2(10);
    v_count              number;
    v_first_contract_id  number;
  begin
    select count(1)
      into v_count
      from con_contract ct
     where ct.project_id =
           (select cc.project_id
              from con_contract cc
             where cc.contract_id = p_contract_id)
       and ct.data_class = 'NORMAL'
       and ct.contract_status = 'INCEPT'
       and ct.inception_of_lease is not null;
    if v_count > 0 then
      select t1.contract_id
        into v_first_contract_id
        from (select cc.contract_id
                from con_contract cc
               where cc.project_id =
                     (select cc.project_id
                        from con_contract cc
                       where cc.contract_id = p_contract_id)
                 and cc.data_class = 'NORMAL'
                 and cc.inception_of_lease is not null
               order by cc.creation_date) t1
       where rownum = 1;
      --取付款申请头表中审批通过的有车款投放日的首期付款日期
      select to_char(max(ch.first_payment_date), 'dd')
        into v_first_contract_day
        from csh_payment_req_hd ch
       where ch.contract_id = v_first_contract_id
         and ch.approval_status = 'APPROVED'
         and exists
       (select 1
                from csh_payment_req_ln cl, con_contract_cashflow ccc
               where cl.payment_req_id = ch.payment_req_id
                 and cl.ref_doc_line_id = ccc.cashflow_id
                 and ccc.write_off_flag = 'FULL'
                 and ccc.cf_item = 5);
    end if;
    return v_first_contract_day;
  end;
  --结清资料退还
  procedure update_send_info(p_contract_id       number,
                             p_cert_recipient01  varchar2,
                             p_cert_tracknum01   varchar2,
                             p_cert_send_date01  date,
                             p_cert_recipient02  varchar2,
                             p_cert_tracknum02   varchar2,
                             p_cert_send_date02  date,
                             p_trans_recipient01 varchar2,
                             p_trans_tracknum01  varchar2,
                             p_trans_send_date01 date,
                             p_trans_recipient02 varchar2,
                             p_trans_tracknum02  varchar2,
                             p_trans_send_date02 date,
                             p_note              varchar2,
                             p_user_id           number) is
    v_count number;
  begin
    select count(1)
      into v_count
      from con_contract_et_document a
     where a.contract_id = p_contract_id;
  
    if v_count < 1 then
      insert into con_contract_et_document
        (doc_id,
         contract_id,
         apply_id,
         print_flag,
         cert_recipient01,
         cert_tracknum01,
         cert_send_date01,
         cert_recipient02,
         cert_tracknum02,
         cert_send_date02,
         trans_recipient01,
         trans_tracknum01,
         trans_send_date01,
         trans_recipient02,
         trans_tracknum02,
         trans_send_date02,
         send_flag,
         note,
         created_by,
         creation_date,
         last_updated_by,
         last_update_date)
      values
        (con_contract_et_document_s.nextval,
         p_contract_id,
         null,
         'N',
         p_cert_recipient01,
         p_cert_tracknum01,
         p_cert_send_date01,
         p_cert_recipient02,
         p_cert_tracknum02,
         p_cert_send_date02,
         
         p_trans_recipient01,
         p_trans_tracknum01,
         p_trans_send_date01,
         
         p_trans_recipient02,
         p_trans_tracknum02,
         p_trans_send_date02,
         'N',
         p_note,
         p_user_id,
         sysdate,
         p_user_id,
         sysdate);
    else
      update con_contract_et_document t
         set t.cert_recipient01  = p_cert_recipient01,
             t.cert_tracknum01   = p_cert_tracknum01,
             t.cert_send_date01  = p_cert_send_date01,
             t.cert_recipient02  = p_cert_recipient02,
             t.cert_tracknum02   = p_cert_tracknum02,
             t.cert_send_date02  = p_cert_send_date02,
             t.trans_recipient01 = p_trans_recipient01,
             t.trans_tracknum01  = p_trans_tracknum01,
             t.trans_send_date01 = p_trans_send_date01,
             t.trans_recipient02 = p_trans_recipient02,
             t.trans_tracknum02  = p_trans_tracknum02,
             t.trans_send_date02 = p_trans_send_date02,
             t.note              = p_note,
             t.last_updated_by   = p_user_id,
             t.last_update_date  = sysdate
       where t.contract_id = p_contract_id;
    end if;
  
  exception
    when others then
      null;
  end;
  --doosan计算起租日逻辑
  function get_inception_of_lease(p_date date) return date is
    v_inception_of_lease date;
    v_day                number;
  begin
    select to_char(p_date, 'dd') into v_day from dual;
  
    if v_day <= 20 then
      v_inception_of_lease := to_date(to_char(add_months(p_date, 1),
                                              'yyyy-mm') || '-' || '15',
                                      'yyyy-mm-dd');
    else
      v_inception_of_lease := to_date(to_char(add_months(p_date, 2),
                                              'yyyy-mm') || '-' || '8',
                                      'yyyy-mm-dd');
    end if;
    return v_inception_of_lease;
  end;

  --doosan计算起租日逻辑8号
  function get_inception_of_lease_8(p_date date) return date is
    v_inception_of_lease date;
    v_day                number;
  begin
    select to_char(p_date, 'dd') into v_day from dual;
  
    if v_day <= 20 then
      v_inception_of_lease := to_date(to_char(add_months(p_date, 1),
                                              'yyyy-mm') || '-' || '8',
                                      'yyyy-mm-dd');
    else
      v_inception_of_lease := to_date(to_char(add_months(p_date, 2),
                                              'yyyy-mm') || '-' || '8',
                                      'yyyy-mm-dd');
    end if;
    return v_inception_of_lease;
  end;
  --删除合同bp信息 modify by lpc 9874 for doosan
  procedure delete_con_contract_bp(p_record_id number, p_user_id number) is
  
  begin
    delete from con_contract_bp c
     where c.record_id = p_record_id
       and c.bp_category <> 'TENANT';
  exception
    when no_data_found then
      null;
  end;

  --更新信息录入标志及事前确认工单 --add by  haojie.liu for doosan
  procedure update_contract_info_sign(p_contract_id number) is
    v_count number := 0;
  begin
    update con_contract c
       set c.information_completed = 'Y'
     where c.contract_id = p_contract_id;
    select count(*)
      into v_count
      from work_orders wo
     where wo.contract_id = p_contract_id;
    if v_count >= 1 then
      update work_orders wo
         set wo.call_status = 'N'
       where wo.contract_id = p_contract_id
         and wo.business_type = 0;
    end if;
  
  exception
    when no_data_found then
      null;
  end;
end con_contract_pkg;
/