cus_con_contract_repo_pkg.pck 141 KB
Newer Older
王炜's avatar
王炜 committed
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
create or replace package cus_con_contract_repo_pkg is

  -- Author  : Spencer 3893
  -- Created : 9/10/2019 2:21:13 PM
  -- Purpose : 合同回购
  function get_contract_repo_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;
  --add by Spencer 3893 20190911 回购申请                             
  procedure create_change_req_repo(p_contract_id     in number,
                                   p_req_type        in varchar2,
                                   p_user_id         in number,
                                   p_simulation_flag in varchar2 default 'N',
                                   p_change_req_id   out number);

  /*  --回购现金流计算
  procedure calc_repo_cashflows(p_contract_id in number,
                                p_user_id     in number);*/
  --通过回购解约日获取回购相关信息                      
  procedure calc_repo_date_amount(p_contract_id                in number,
                                  p_repurchase_date            date,
                                  p_last_rent_due_date         out date,
                                  p_sum_received_rent_amount   out number,
                                  p_sum_unreceived_rent_amount out number,
                                  p_sum_overdue_amount         out number,
                                  p_sum_unreceived_principal   out number,
                                  p_due_amount                 out number,
                                  p_penalty                    out number, --逾期罚息
                                  p_fund_possession_cost       out number, --资金占用费
                                  p_user_id                    in number);
  --提交工作流
  procedure submit_change_req_repo(p_change_req_id in number,
                                   p_layout_code   in varchar2,
                                   p_user_id       in number);

  --单边量求解后回写变更现金流                                  
  procedure calc_repo_cashflow(p_contract_id     in number,
                               p_calc_session_id IN NUMBER,
                               p_user_id         IN NUMBER,
                               p_msg             OUT VARCHAR2);
  --一次性回购计算现金流  
  PROCEDURE calc_one_time_repo_cashflow(p_contract_id IN NUMBER,
                                        p_user_id     IN NUMBER);
                                        
  --回购计算租赁物铁甲的保证金自动抵扣核销 
  PROCEDURE calc_cashflow_division_tj(p_contract_id IN NUMBER,
                                        p_user_id     IN NUMBER);
  --总经理节点审批通过时,消息通知会计部长(高燕)、法务合规部长(赵捷)
  PROCEDURE repo_wfl_approved_mail(p_change_req_id number,
                                   p_user_id       IN NUMBER);
  --审批结束时,发送给提交人
  PROCEDURE repo_wfl_mail_to_submit(p_change_req_id number,
                                    p_user_id       IN NUMBER,
                                    p_wfl_flag_desc varchar2);
                                    
  --回租合同解约回购拆税
  procedure deal_repur_cash_leaseback(p_contract_id number,
                                      p_interest out number);
   
   
end cus_con_contract_repo_pkg;
/
create or replace package body cus_con_contract_repo_pkg is
  e_lock_table exception;
  pragma exception_init(e_lock_table, -54);

  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            => 'cus_con_contract_repo_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_repo_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.repurchase_contract_number%type;
    v_agent_bp_code varchar2(30);
    v_price_ref_v05 varchar2(200);
    v_product_code  varchar2(2000);
  
    e_product_code_err exception;
    e_get_contract_number_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;
  
    v_no := fnd_code_rule_pkg.get_rule_next_auto_num(p_document_category => 'CHANGE_REQUEST',
                                                     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            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'get_contract_repo_number');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;
  function get_con_change_req_rec(p_change_req_id con_contract_change_req.change_req_id%type,
                                  p_user_id       con_contract_change_req.created_by%type)
    return con_contract_change_req%rowtype is
    v_change_req_rec con_contract_change_req%rowtype;
  begin
    select *
      into v_change_req_rec
      from con_contract_change_req t
     where t.change_req_id = p_change_req_id
       for update nowait;
    return v_change_req_rec;
  exception
    when e_lock_table then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_CHANGE_REQ_PKG.CON_CHANGE_REQ_LOCK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'cus_con_contract_repo_pkg',
                                                      p_procedure_function_name => 'get_con_change_req_rec');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;
  --add by Spencer 3893 20190911 回购申请                             
  procedure create_change_req_repo(p_contract_id     in number,
                                   p_req_type        in varchar2,
                                   p_user_id         in number,
                                   p_simulation_flag in varchar2 default 'N',
                                   p_change_req_id   out number) is
    v_change_req_id number;
  
    v_contract_rec          con_contract%rowtype;
    v_calc_session_id       number;
    r_hls_fin_calculator_hd hls_fin_calculator_hd%rowtype;
    v_con_change_re_rec     con_contract_change_req%rowtype;
    --事故单I-10185
    v_count number;
    begin
  
    v_contract_rec := get_contract_rec(p_contract_id, p_user_id);
  
    --历史合同迁移过来无报价,手动insert 头表
    v_calc_session_id := v_contract_rec.calc_session_id;
    if v_calc_session_id is null then
      r_hls_fin_calculator_hd                 := null;
      v_calc_session_id                       := hls_fin_calculator_hd_s.nextval;
      r_hls_fin_calculator_hd.calc_session_id := v_calc_session_id;
      r_hls_fin_calculator_hd.company_id      := v_contract_rec.company_id;
      r_hls_fin_calculator_hd.price_list      := v_contract_rec.price_list;
      insert into hls_fin_calculator_hd values r_hls_fin_calculator_hd;
      update con_contract c
         set c.calc_session_id = v_calc_session_id
       where c.contract_id = v_contract_rec.contract_id;
    end if;
    --事故单I-10185 发起解约回购如果存在付款的现金流,则不能发起解约回购
   select count(1)
   into v_count
   from csh_payment_req_hd cph, csh_payment_req_ln ln
  where cph.payment_req_id = ln.payment_req_id
    and ln.ref_doc_id = p_contract_id
    and ln.ref_doc_line_id in
        (select t.cashflow_id
           from con_contract_cashflow t
          where t.contract_id = p_contract_id
            and t.cf_direction = 'OUTFLOW'
            and t.write_off_flag <> 'FULL'
            and t.cf_item = '0')
    and cph.approval_status in('NEW','APPROVING');
   
   if v_count>0 then
       sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同已经发起了付款申请或者正在付款申请流程中,不能发起解约回购!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'create_change_req_repo');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
       
     end if;
  
    if v_contract_rec.contract_status <> 'INCEPT' and p_req_type = 'REPUR' then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '只有起租状态的合同才能进行回购申请!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'create_change_req_repo');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    end if;
  
    con_contract_history_pkg.create_change_req(p_contract_id       => p_contract_id,
                                               p_change_req_id     => v_change_req_id,
                                               p_req_date          => sysdate,
                                               p_ref_v01           => p_user_id,
                                               p_description       => null,
                                               p_change_quotation  => null,
                                               p_change_lease_item => null,
                                               p_change_bp         => null,
                                               p_req_doc_type      => p_req_type,
                                               p_simulation_flag   => p_simulation_flag,
                                               p_repo_change_flag  => 'Y',
                                               p_user_id           => p_user_id);
    /* --生成回购编号                                            
    v_contract_rec.repurchase_contract_number := get_contract_repo_number(p_document_type => p_req_type,
                                                                          p_document_date => sysdate,
                                                                          p_company_id    => v_contract_rec.company_id,
                                                                          p_user_id       => p_user_id);*/
  
    select *
      into v_con_change_re_rec
      from con_contract_change_req c
     where c.change_req_id = v_change_req_id;
    --更新回购编号到变更流程中的新合同
    update con_contract c
       set c.repurchase_contract_number = v_con_change_re_rec.change_req_number,
           c.last_updated_by            = p_user_id,
           c.last_update_date           = sysdate
     where c.contract_id = v_change_req_id;
  
    cus_con_change_req_pkg.insert_hls_document_use_seal(p_change_req_id       => v_change_req_id,
                                                        p_document_table      => 'CON_CONTRACT_CHANGE_REQ',
                                                        p_instruments_types   => '解除融资租赁合同协议',
                                                        p_common_seal         => 'Y',
                                                        p_corporate_visa_seal => 'Y',
                                                        p_legal_seal          => 'N',
                                                        p_con_special_seal    => 'N',
                                                        p_financial_seal      => 'N',
                                                        p_supervisio_seal     => 'N',
                                                        p_seal_number         => 'SEAL_O_F_NUMBER',
                                                        p_user_id             => p_user_id);
    cus_con_change_req_pkg.insert_hls_document_use_seal(p_change_req_id       => v_change_req_id,
                                                        p_document_table      => 'CON_CONTRACT_CHANGE_REQ',
                                                        p_instruments_types   => '债权债务抵销协议',
                                                        p_common_seal         => 'Y',
                                                        p_corporate_visa_seal => 'Y',
                                                        p_legal_seal          => 'N',
                                                        p_con_special_seal    => 'N',
                                                        p_financial_seal      => 'N',
                                                        p_supervisio_seal     => 'N',
                                                        p_seal_number         => 'SEAL_O_F_NUMBER',
                                                        p_user_id             => p_user_id);
    cus_con_change_req_pkg.insert_hls_document_use_seal(p_change_req_id       => v_change_req_id,
                                                        p_document_table      => 'CON_CONTRACT_CHANGE_REQ',
                                                        p_instruments_types   => '代理店回购补充协议(更新版)',
                                                        p_common_seal         => 'Y',
                                                        p_corporate_visa_seal => 'Y',
                                                        p_legal_seal          => 'N',
                                                        p_con_special_seal    => 'N',
                                                        p_financial_seal      => 'N',
                                                        p_supervisio_seal     => 'N',
                                                        p_seal_number         => 'SEAL_O_F_NUMBER',
                                                        p_user_id             => p_user_id);
    p_change_req_id := v_change_req_id;
  end;
  procedure delete_cashflow(p_contract_id number, p_times number) is
  begin
    delete from con_contract_cashflow cf
     where cf.times > p_times
       and cf.cf_item in (1, 9)
       and cf.contract_id = p_contract_id;
  end;
  -- 手续费处理
  procedure sc_cashflow_hander(p_contract_id           number,
                               p_service_charges       number,
                               p_paymengt_deadlinedate date,
                               p_times                 number,
                               p_user_id               in number) is
    v_cashflow_rec          con_contract_cashflow%rowtype;
    v_outstanding_principal number; --剩余本金
    v_principal             number; --本金
    v_interest              number; --利息
    v_due_date              date;
  begin
    v_interest              := round(p_service_charges * 0.13 / 1.13, 2);
    v_principal             := round(p_service_charges / 1.13, 2);
    v_outstanding_principal := 0;
    v_due_date              := p_paymengt_deadlinedate;
  
    v_cashflow_rec.cashflow_id                := con_contract_cashflow_s.nextval;
    v_cashflow_rec.contract_id                := p_contract_id;
    v_cashflow_rec.cf_item                    := 304;
    v_cashflow_rec.cf_type                    := 3;
    v_cashflow_rec.cf_direction               := 'INFLOW';
    v_cashflow_rec.cf_status                  := 'RELEASE';
    v_cashflow_rec.times                      := p_times;
    v_cashflow_rec.calc_date                  := v_due_date;
    v_cashflow_rec.due_date                   := v_due_date;
    v_cashflow_rec.due_amount                 := p_service_charges;
    v_cashflow_rec.net_due_amount             := round(p_service_charges / 1.13,
                                                       2);
    v_cashflow_rec.vat_due_amount             := round(p_service_charges * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.principal                  := v_principal;
    v_cashflow_rec.net_principal              := round(v_principal / 1.13,
                                                       2);
    v_cashflow_rec.vat_principal              := round(v_principal * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.interest                   := v_interest;
    v_cashflow_rec.net_interest               := round(v_interest / 1.13, 2);
    v_cashflow_rec.vat_interest               := round(v_interest * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.received_amount            := 0;
    v_cashflow_rec.received_principal         := 0;
    v_cashflow_rec.received_interest          := 0;
    v_cashflow_rec.outstanding_prin_tax_incld := v_outstanding_principal;
    v_cashflow_rec.write_off_flag             := 'NOT';
    v_cashflow_rec.overdue_status             := 'NOT';
    v_cashflow_rec.penalty_process_status     := 'NORMAL';
    v_cashflow_rec.billing_status             := 'N';
  
    v_cashflow_rec.generated_source := 'MANUAL';
    v_cashflow_rec.created_by       := p_user_id;
    v_cashflow_rec.creation_date    := sysdate;
    v_cashflow_rec.last_updated_by  := p_user_id;
    v_cashflow_rec.last_update_date := sysdate;
    insert into con_contract_cashflow values v_cashflow_rec;
  end sc_cashflow_hander;
  -- Refactored procedure fund_prossession_cost_hander 
  procedure fund_prossession_cost_hander(p_contract_id           number,
                                         p_fund_possession_cost  number,
                                         p_paymengt_deadlinedate date,
                                         p_times                 number,
                                         p_user_id               number) is
    v_cashflow_rec          con_contract_cashflow%rowtype;
    v_outstanding_principal number; --剩余本金
    v_principal             number; --本金
    v_interest              number; --利息
    v_due_date              date;
  begin
    v_interest              := round(p_fund_possession_cost * 0.13 / 1.13,
                                     2);
    v_principal             := round(p_fund_possession_cost / 1.13, 2);
    v_outstanding_principal := 0;
    v_due_date              := p_paymengt_deadlinedate;
  
    v_cashflow_rec.cashflow_id                := con_contract_cashflow_s.nextval;
    v_cashflow_rec.contract_id                := p_contract_id;
    v_cashflow_rec.cf_item                    := 14;
    v_cashflow_rec.cf_type                    := 14;
    v_cashflow_rec.cf_direction               := 'INFLOW';
    v_cashflow_rec.cf_status                  := 'RELEASE';
    v_cashflow_rec.times                      := p_times;
    v_cashflow_rec.calc_date                  := v_due_date;
    v_cashflow_rec.due_date                   := v_due_date;
    v_cashflow_rec.due_amount                 := p_fund_possession_cost;
    v_cashflow_rec.net_due_amount             := round(p_fund_possession_cost / 1.13,
                                                       2);
    v_cashflow_rec.vat_due_amount             := round(p_fund_possession_cost * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.principal                  := v_principal;
    v_cashflow_rec.net_principal              := round(v_principal / 1.13,
                                                       2);
    v_cashflow_rec.vat_principal              := round(v_principal * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.interest                   := v_interest;
    v_cashflow_rec.net_interest               := round(v_interest / 1.13, 2);
    v_cashflow_rec.vat_interest               := round(v_interest * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.received_amount            := 0;
    v_cashflow_rec.received_principal         := 0;
    v_cashflow_rec.received_interest          := 0;
    v_cashflow_rec.outstanding_prin_tax_incld := v_outstanding_principal;
    v_cashflow_rec.write_off_flag             := 'NOT';
    v_cashflow_rec.overdue_status             := 'NOT';
    v_cashflow_rec.penalty_process_status     := 'NORMAL';
    v_cashflow_rec.billing_status             := 'N';
  
    v_cashflow_rec.generated_source := 'MANUAL';
    v_cashflow_rec.created_by       := p_user_id;
    v_cashflow_rec.creation_date    := sysdate;
    v_cashflow_rec.last_updated_by  := p_user_id;
    v_cashflow_rec.last_update_date := sysdate;
    insert into con_contract_cashflow values v_cashflow_rec;
  end fund_prossession_cost_hander;
  /*  --回购现金流计算
  procedure calc_repo_cashflows(p_contract_id in number,
                                p_user_id     in number) is
    v_contract_rec con_contract%rowtype;
    v_cashflow_rec con_contract_cashflow%rowtype;
  
    v_due_amount            number; --每期租金
    v_outstanding_principal number; --剩余本金
    v_principal             number; --本金
    v_interest              number; --利息
    v_due_date              date;
    v_calc_due_date         date;
    v_times                 number;
    v_calc_days             number;
  
    e_due_amount_error exception;
    e_repo_calc_amount_error exception;
  begin
    --回购合同
    v_contract_rec := get_contract_rec(p_contract_id, p_user_id);
    select *
      into v_cashflow_rec
      from con_contract_cashflow f
     where f.due_date = v_contract_rec.last_rent_due_date
       and f.cf_item = 1
       and f.cf_type = 1
       and f.contract_id = p_contract_id;
  
    --计算前删除本次变更起始期及之后期,
    delete_cashflow(p_contract_id, v_cashflow_rec.times);
    -- 一次性回购     
    if v_contract_rec.repurchase_types = 'ONE_TIME_REPURCHASE' then
      v_times                 := v_cashflow_rec.times + 1;
      v_cashflow_rec          := null;
      v_interest              := round(v_contract_rec.repurchase_tatal_amount * 0.13 / 1.13,
                                       2);
      v_principal             := round(v_contract_rec.repurchase_tatal_amount / 1.13,
                                       2);
      v_outstanding_principal := 0;
      v_due_date              := v_contract_rec.paymengt_deadlinedate;
    
      v_cashflow_rec.cashflow_id                := con_contract_cashflow_s.nextval;
      v_cashflow_rec.contract_id                := v_contract_rec.contract_id;
      v_cashflow_rec.cf_item                    := 1;
      v_cashflow_rec.cf_type                    := 1;
      v_cashflow_rec.cf_direction               := 'INFLOW';
      v_cashflow_rec.cf_status                  := 'RELEASE';
      v_cashflow_rec.times                      := v_times;
      v_cashflow_rec.calc_date                  := v_due_date;
      v_cashflow_rec.due_date                   := v_due_date;
      v_cashflow_rec.due_amount                 := v_contract_rec.repurchase_tatal_amount;
      v_cashflow_rec.net_due_amount             := round(v_contract_rec.repurchase_tatal_amount / 1.13,
                                                         2);
      v_cashflow_rec.vat_due_amount             := round(v_contract_rec.repurchase_tatal_amount * 0.13 / 1.13,
                                                         2);
      v_cashflow_rec.principal                  := v_principal;
      v_cashflow_rec.net_principal              := round(v_principal / 1.13,
                                                         2);
      v_cashflow_rec.vat_principal              := round(v_principal * 0.13 / 1.13,
                                                         2);
      v_cashflow_rec.interest                   := v_interest;
      v_cashflow_rec.net_interest               := round(v_interest / 1.13,
                                                         2);
      v_cashflow_rec.vat_interest               := round(v_interest * 0.13 / 1.13,
                                                         2);
      v_cashflow_rec.received_amount            := 0;
      v_cashflow_rec.received_principal         := 0;
      v_cashflow_rec.received_interest          := 0;
      v_cashflow_rec.outstanding_prin_tax_incld := v_outstanding_principal;
      v_cashflow_rec.write_off_flag             := 'NOT';
      v_cashflow_rec.overdue_status             := 'NOT';
      v_cashflow_rec.penalty_process_status     := 'NORMAL';
      v_cashflow_rec.billing_status             := 'N';
    
      v_cashflow_rec.generated_source := 'MANUAL';
      v_cashflow_rec.created_by       := p_user_id;
      v_cashflow_rec.creation_date    := sysdate;
      v_cashflow_rec.last_updated_by  := p_user_id;
      v_cashflow_rec.last_update_date := sysdate;
      insert into con_contract_cashflow values v_cashflow_rec;
      --手续费
      if v_contract_rec.repurchase_service_charges is not null and
         v_contract_rec.repurchase_service_charges > 0 then
        v_cashflow_rec := null;
        sc_cashflow_hander(p_contract_id           => v_contract_rec.contract_id,
                           p_service_charges       => v_contract_rec.repurchase_service_charges,
                           p_paymengt_deadlinedate => v_contract_rec.paymengt_deadlinedate,
                           p_times                 => v_times,
                           p_user_id               => p_user_id);
      end if;
      --资金占用费
      if v_contract_rec.fund_possession_cost is not null and
         v_contract_rec.fund_possession_cost > 0 then
        v_cashflow_rec := null;
        fund_prossession_cost_hander(p_contract_id           => v_contract_rec.contract_id,
                                     p_fund_possession_cost  => v_contract_rec.fund_possession_cost,
                                     p_paymengt_deadlinedate => v_contract_rec.paymengt_deadlinedate,
                                     p_times                 => v_times,
                                     p_user_id               => p_user_id);
      end if;
    
      --分期回购  
    elsif v_contract_rec.repurchase_types = 'STAGE_REPURCHASE' then
      v_outstanding_principal := v_contract_rec.repurchase_tatal_amount; --回购价款总额
      if v_outstanding_principal > 0 then
        -- 计算租金
        select round(abs(hls_financial_calculate_pkg.pmt(nvl(v_contract_rec.repurchase_interest_rate,
                                                             v_contract_rec.int_rate_display) / 12, --租赁年利率/支付频率
                                                         v_contract_rec.payment_period, --租赁期限
                                                         v_outstanding_principal, --剩余本金
                                                         0,
                                                         0)),
                     2)
          into v_due_amount
          from dual;
      
        if nvl(v_due_amount, 0) <= 0 then
          hls_sys_log_pkg.log('回购合同计算租金异常,计算利率:' ||
                              v_contract_rec.repurchase_interest_rate ||
                              ' ,支付频率:' ||
                              v_contract_rec.payment_frequency ||
                              ' ,租赁期限:' || v_contract_rec.payment_period ||
                              ' ,回购价款总额:' || v_outstanding_principal);
          raise e_due_amount_error;
        end if;
        v_due_date := v_contract_rec.paymengt_deadlinedate;
        v_times    := v_cashflow_rec.times + 1;
        for cur in 1 .. v_contract_rec.payment_period loop
          v_cashflow_rec := null;
          --最后一期
          if cur = v_contract_rec.payment_period then
            v_interest              := v_due_amount -
                                       v_outstanding_principal;
            v_principal             := v_outstanding_principal;
            v_outstanding_principal := 0;
          else
            if cur >= 2 then
              v_calc_due_date := add_months(v_due_date, 1);
              v_calc_days     := v_calc_due_date - v_due_date;
              v_interest      := round(v_outstanding_principal *
                                       nvl(v_calc_days, 1) *
                                       (nvl(v_contract_rec.repurchase_interest_rate,
                                            v_contract_rec.int_rate_display) / 360),
                                       2);
            else
              --第一期利息天数
              v_interest := round(v_outstanding_principal * 1 *
                                  (nvl(v_contract_rec.repurchase_interest_rate,
                                       v_contract_rec.int_rate_display) / 360),
                                  2);
            end if;
            v_principal             := v_due_amount - v_interest;
            v_outstanding_principal := v_outstanding_principal -
                                       v_principal;
          end if;
          v_cashflow_rec.cashflow_id                := con_contract_cashflow_s.nextval;
          v_cashflow_rec.contract_id                := v_contract_rec.contract_id;
          v_cashflow_rec.cf_item                    := 1;
          v_cashflow_rec.cf_type                    := 1;
          v_cashflow_rec.cf_direction               := 'INFLOW';
          v_cashflow_rec.cf_status                  := 'RELEASE';
          v_cashflow_rec.times                      := v_times;
          v_cashflow_rec.calc_date                  := v_due_date;
          v_cashflow_rec.due_date                   := v_due_date;
          v_cashflow_rec.due_amount                 := v_due_amount;
          v_cashflow_rec.net_due_amount             := round(v_due_amount / 1.13,
                                                             2);
          v_cashflow_rec.vat_due_amount             := round(v_due_amount * 0.13 / 1.13,
                                                             2);
          v_cashflow_rec.principal                  := v_principal;
          v_cashflow_rec.net_principal              := round(v_principal / 1.13,
                                                             2);
          v_cashflow_rec.vat_principal              := round(v_principal * 0.13 / 1.13,
                                                             2);
          v_cashflow_rec.interest                   := v_interest;
          v_cashflow_rec.net_interest               := round(v_interest / 1.13,
                                                             2);
          v_cashflow_rec.vat_interest               := round(v_interest * 0.13 / 1.13,
                                                             2);
          v_cashflow_rec.received_amount            := 0;
          v_cashflow_rec.received_principal         := 0;
          v_cashflow_rec.received_interest          := 0;
          v_cashflow_rec.outstanding_prin_tax_incld := v_outstanding_principal;
          v_cashflow_rec.write_off_flag             := 'NOT';
          v_cashflow_rec.overdue_status             := 'NOT';
          v_cashflow_rec.penalty_process_status     := 'NORMAL';
          v_cashflow_rec.billing_status             := 'N';
        
          v_cashflow_rec.generated_source := 'MANUAL';
          v_cashflow_rec.created_by       := p_user_id;
          v_cashflow_rec.creation_date    := sysdate;
          v_cashflow_rec.last_updated_by  := p_user_id;
          v_cashflow_rec.last_update_date := sysdate;
        
          insert into con_contract_cashflow values v_cashflow_rec;
          --最后期不需要校验剩余本金是否小于0
          if cur < 6 then
            v_due_date := add_months(v_due_date, 1);
            v_times    := v_times + 1;
          
            if v_outstanding_principal < 0 then
              raise e_repo_calc_amount_error;
            end if;
          end if;
        end loop;
        --手续费
        if v_contract_rec.repurchase_service_charges is not null and
           v_contract_rec.repurchase_service_charges > 0 then
          v_cashflow_rec := null;
          sc_cashflow_hander(p_contract_id           => v_contract_rec.contract_id,
                             p_service_charges       => v_contract_rec.repurchase_service_charges,
                             p_paymengt_deadlinedate => v_contract_rec.paymengt_deadlinedate,
                             p_times                 => v_times,
                             p_user_id               => p_user_id);
        end if;
        --资金占用费
        if v_contract_rec.fund_possession_cost is not null and
           v_contract_rec.fund_possession_cost > 0 then
          v_cashflow_rec := null;
          fund_prossession_cost_hander(p_contract_id           => v_contract_rec.contract_id,
                                       p_fund_possession_cost  => v_contract_rec.fund_possession_cost,
                                       p_paymengt_deadlinedate => v_contract_rec.paymengt_deadlinedate,
                                       p_times                 => v_times,
                                       p_user_id               => p_user_id);
        end if;
      elsif v_outstanding_principal = 0 then
        raise e_repo_calc_amount_error;
      end if;
    end if;
  
  exception
    when e_repo_calc_amount_error then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '回购价款总额小于0,请检查!',
                                                     p_created_by              => -1,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_cashflows');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_due_amount_error then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '回购合同计算租金出错,请联系系统管理员!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_cashflows');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end calc_repo_cashflows;*/
  --通过回购解约日获取回购相关信息                   
  procedure calc_repo_date_amount(p_contract_id                in number,
                                  p_repurchase_date            date,
                                  p_last_rent_due_date         out date,
                                  p_sum_received_rent_amount   out number,
                                  p_sum_unreceived_rent_amount out number,
                                  p_sum_overdue_amount         out number,
                                  p_sum_unreceived_principal   out number,
                                  p_due_amount                 out number,
                                  p_penalty                    out number, --逾期罚息
                                  p_fund_possession_cost       out number, --资金占用费
                                  -- p_sum_received_penalty_amount out number, --已收违约金
                                  p_user_id in number) is
    v_contract_rec       con_contract%rowtype;
    v_old_contract_rec   con_contract%rowtype;
    v_cashflow_rec       con_contract_cashflow%rowtype;
    v_times              number;
    v_last_rent_due_date date;
    --已回收租金
    v_sum_received_rent_amount number;
    --剩余租金
    v_sum_unreceived_rent_amount number;
    --逾期租金
    v_sum_overdue_amount number;
    --剩余本金
    v_sum_unreceived_principal number;
    -- 逾期罚息
    v_penalty number;
    --已收违约金
    v_sum_received_penalty_amount number;
    v_due_amount                  number;
    v_overdue_max_days            number;
    v_count                       number := 0;
    v_et_date                     date;
    e_et_date_err exception;
    e_write_off_flagg_err exception;
    e_error exception;
    v_ccr_start_times         number;
    r_con_contract_change_req con_contract_change_req%ROWTYPE;
    v_fund_possession_rate    number;
    v_fund_possession_cost    number;
    v_fund_possession_time    number;
    e_defin_error exception;
    e_err_msg          varchar2(2000);
    v_max_penalty_date date;
    v_max_error_date   date;
    v_max_due_date     date;
  begin
    select *
      into r_con_contract_change_req
      from con_contract_change_req t
     where t.change_req_id = p_contract_id;
  
    --变更前合同
    select *
      into v_old_contract_rec
      from con_contract t
     where t.contract_id = r_con_contract_change_req.contract_id;
    select count(1)
      into v_count
      from con_contract_cashflow ccc
     where ccc.contract_id = v_old_contract_rec.contract_id
       and decode(ccc.cf_item,
                  1,
                  ccc.due_date,
                  8,
                  ccc.due_date,
                  9,
                  (select due_date
                     from con_contract_cashflow
                    where contract_id = v_old_contract_rec.contract_id
                      and cf_item = 1
                      and times = ccc.times)) >= p_repurchase_date
       and ccc.cf_item in (1, 8, 9)
       and ccc.cf_direction = 'INFLOW'
       and ccc.cf_status = 'RELEASE'
       and ((nvl(ccc.write_off_flag, 'NOT') <> 'NOT') OR
           (nvl(ccc.billing_status, 'NOT') <> 'NOT'));
    if v_count > 0 then
      raise e_error;
    end if;
    v_count := 0;
  
    begin
      select max(decode(ccc.cf_item,
                        1,
                        ccc.due_date,
                        8,
                        ccc.due_date,
                        9,
                        (select due_date
                           from con_contract_cashflow
                          where contract_id = v_old_contract_rec.contract_id
                            and cf_item = 1
                            and times = ccc.times)))
        into v_max_error_date
        from con_contract_cashflow ccc
       where ccc.contract_id = v_old_contract_rec.contract_id
         and ccc.cf_item in (1, 8, 9)
         and ccc.cf_direction = 'INFLOW'
         and ccc.cf_status = 'RELEASE'
         and nvl(ccc.write_off_flag, 'NOT') <> 'NOT';
    exception
      when no_data_found then
        v_max_error_date := null;
    end;
  
    if v_max_error_date is not null and
       v_max_error_date > p_repurchase_date then
      e_err_msg := '解约回购日期所在期或之后的租金,留购价,违约金已存在还款和开票记录,请先反冲!';
      raise e_defin_error;
    end if;
    /*
    select max(a.due_date)
      into v_et_date
      from con_contract_cashflow a
     where a.cf_item = 1
       and a.contract_id = v_old_contract_rec.contract_id;
    if v_et_date < p_repurchase_date then
      raise e_et_date_err;
    end if;*/
    select max(a.times)
      into v_times
      from con_contract_cashflow a
     where decode(a.cf_item,
                  1,
                  a.due_date,
                  9,
                  (select due_date
                     from con_contract_cashflow
                    where contract_id = v_old_contract_rec.contract_id
                      and cf_item = 1
                      and times = a.times)) >= p_repurchase_date
       and a.cf_item in (1, 9)
       and a.write_off_flag in ('PARTIAL', 'FULL')
       and a.contract_id = v_old_contract_rec.contract_id;
  
    if v_times >= 1 then
      raise e_write_off_flagg_err;
    end if;
    /* v_count := 0;
    select count(1)
      into v_count
      from con_contract_cashflow ccc
     where ccc.contract_id = v_old_contract_rec.contract_id
       and decode(ccc.cf_item,
                  1,
                  ccc.due_date,
                  9,
                  (select due_date
                     from con_contract_cashflow
                    where contract_id = v_old_contract_rec.contract_id
                      and cf_item = 1
                      and times = ccc.times)) < trunc(p_repurchase_date)
       and ccc.cf_item in (1, 9)
       and ccc.cf_direction = 'INFLOW'
       and ccc.cf_status = 'RELEASE'
       and (nvl(ccc.write_off_flag, 'NOT') <> 'FULL')
       and decode(ccc.cf_item,
                  1,
                  ccc.due_date,
                  9,
                  (select due_date
                     from con_contract_cashflow
                    where contract_id = v_old_contract_rec.contract_id
                      and cf_item = 1
                      and times = ccc.times)) > trunc(sysdate);
    if v_count > 0 then
      e_err_msg := '解约回购日之前的租金,不可以有未完全核销且未逾期的租金!';
      raise e_defin_error;
    end if;*/
  
    v_contract_rec := get_contract_rec(p_contract_id, p_user_id);
  
    select min(a.times)
      into v_ccr_start_times
      from con_contract_cashflow a
     where a.due_date >= p_repurchase_date
       and a.cf_item = 1
       and a.cf_direction != 'NONCASH'
       and a.contract_id = v_old_contract_rec.contract_id;
    if v_ccr_start_times is null then
      v_ccr_start_times := v_old_contract_rec.lease_times + 1;
    end if;
    --前期租金支付约定日
    --取还款计划中小于‘解约回购日’的、租金大于零的、租金应收日的最大值
    begin
      select max(f.due_date)
        into v_last_rent_due_date
        from con_contract_cashflow f
       where f.due_date < p_repurchase_date
         and f.cf_item = 1
         and f.cf_type = 1
        -- and nvl(f.due_amount, 0) > 0
         and f.contract_id = v_old_contract_rec.contract_id;
    
    end;
    p_last_rent_due_date := v_last_rent_due_date;
  
    --已回收租金 已核销各期租金之和+首付款
    select sum(nvl(ccc.received_amount, 0)) +
           nvl(v_old_contract_rec.down_payment, 0)
      into v_sum_received_rent_amount
      from con_contract_cashflow ccc
     where ccc.contract_id = v_old_contract_rec.contract_id
       and ccc.cf_status = 'RELEASE'
       and ccc.cf_item = 1;
    --已收违约金
    select nvl((select sum(nvl(ccc.received_amount, 0))
                 from con_contract_cashflow ccc
                where ccc.contract_id = v_old_contract_rec.contract_id
                  and ccc.cf_status = 'RELEASE'
                  and ccc.cf_item = 9),
               0)
      into v_sum_received_penalty_amount
      from dual;
  
    --剩余租金
    --变更起始期(含)至最后一期租金应收金额
    select sum(nvl(ccc.due_amount, 0))
      into v_sum_unreceived_rent_amount
      from con_contract_cashflow ccc
     where ccc.contract_id = v_old_contract_rec.contract_id
       and ccc.times >= v_ccr_start_times
       and ccc.cf_status = 'RELEASE'
       and ccc.cf_item = 1;
  
    --资金占用天数
    select trunc(p_repurchase_date) - trunc(v_last_rent_due_date) + 1
      into v_fund_possession_time
      from dual;
  
    --资金占用费率 默认带出合同利率
    v_fund_possession_rate := v_old_contract_rec.int_rate_display;
  
    --剩余本金:提前结清起始期(提前结清日所在期;若等于预定支付日,起始期为下一期)上一期的剩余本金  
    begin
    select
    --f.principal - nvl(f.received_principal, 0)
     f.outstanding_principal
      into v_sum_unreceived_principal
      from con_contract_cashflow f
     where f.due_date = v_last_rent_due_date
       and f.cf_item = 1
       and f.cf_type = 1
       and f.contract_id = v_old_contract_rec.contract_id
       and f.due_amount>0;
       exception
         --0租金现金流继续往后找
         when no_data_found then
       select t1.outstanding_principal
        into v_sum_unreceived_principal
        from  (    select
       f.cashflow_id,
     f.outstanding_principal+nvl(f.principal,0) outstanding_principal
     
      from con_contract_cashflow f
     where f.due_date > v_last_rent_due_date
       and f.cf_item = 1
       and f.cf_type = 1
       and f.contract_id = v_old_contract_rec.contract_id
       and f.due_amount>0
       order by f.times) t1 where rownum=1;
   end;
  
    --资金占用费:剩余本金×资金占用费率/整年天数×占用资金天数
    v_fund_possession_cost := v_sum_unreceived_principal *
                              v_fund_possession_time *
                              v_fund_possession_rate / 360;
  
    --逾期租金:解约回购起始期之前未收租金之和
    select nvl(sum(f.due_amount), 0) - nvl(sum(f.received_amount), 0)
      into v_sum_overdue_amount
      from con_contract_cashflow f
     where f.due_date <= v_last_rent_due_date
       and f.cf_item = 1
       and f.cf_type = 1
       and f.contract_id = v_old_contract_rec.contract_id;
  
    /*  -- 逾期罚息:解约回购起始期之前未收罚息之和;
    select nvl(sum(f.due_amount), 0) - nvl(sum(f.received_amount), 0)
      into v_penalty
      from con_contract_cashflow f
     where f.times < v_ccr_start_times
       and f.cf_item = 9
       and f.contract_id = v_old_contract_rec.contract_id;*/
    v_penalty := 0;
    --重选罚息
    FOR c_contract_cashflow IN (SELECT a.*
                                  FROM con_contract_cashflow a,
                                       con_contract_cf_item  b
                                 WHERE a.contract_id =
                                       v_old_contract_rec.contract_id
                                   AND a.cf_status = 'RELEASE'
                                   AND a.due_date <= v_last_rent_due_date
                                      --只有正常的才计算罚息。
                                      -- AND a.penalty_process_status = 'NORMAL'
                                   AND a.cf_direction = 'INFLOW'
                                   AND a.contract_id = b.contract_id
                                   AND a.cf_item = b.cf_item
                                   AND b.calc_penalty = 'Y'
                                 ORDER BY times, due_date) LOOP
    
      if nvl(c_contract_cashflow.penalty_process_status, 'N') = 'NORMAL' then
        v_penalty := v_penalty +
                     CON_OVERDUE_PENALTY_PKG.CALC_PENALTY_FOR_WRITE_OFF(p_cashflow_id  => c_contract_cashflow.cashflow_id,
                                                                        p_calc_date    => p_repurchase_date,
                                                                        p_user_id      => p_user_id,
                                                                        p_penalty_flag => 'N');
        -- if nvl(c_contract_cashflow.write_off_flag, 'NOT') <> 'FULL' then
      
        select v_penalty -
               nvl((SELECT sum(nvl(a.received_amount, 0))
                     FROM con_contract_cashflow a
                    WHERE a.contract_id = v_old_contract_rec.contract_id
                      AND a.cf_status = 'RELEASE'
                      AND a.cf_item = 9
                      AND a.times = c_contract_cashflow.times),
                   0)
          into v_penalty
          from dual;
        -- end if;
      end if;
    
      if nvl(c_contract_cashflow.penalty_process_status, 'N') <> 'NORMAL' and
         c_contract_cashflow.cf_item <> 8 then
        select v_penalty +
               nvl((SELECT sum(nvl(a.due_amount, 0) -
                              nvl(a.received_amount, 0))
                     FROM con_contract_cashflow a
                    WHERE a.contract_id = v_old_contract_rec.contract_id
                      AND a.cf_status = 'RELEASE'
                      AND a.cf_item = 9
                      AND a.times = c_contract_cashflow.times),
                   0)
          into v_penalty
          from dual;
      end if;
    
    end loop;
  
    p_sum_overdue_amount         := v_sum_overdue_amount;
    p_due_amount                 := v_penalty;
    p_sum_unreceived_principal   := v_sum_unreceived_principal;
    p_last_rent_due_date         := v_last_rent_due_date;
    p_sum_received_rent_amount   := v_sum_received_rent_amount;
    p_sum_unreceived_rent_amount := v_sum_unreceived_rent_amount;
    p_fund_possession_cost       := v_fund_possession_cost;
  
    update con_contract_change_req
       set repurchase_date    = p_repurchase_date,
           ccr_start_times    = v_ccr_start_times,
           last_update_date   = sysdate,
           last_rent_due_date = v_last_rent_due_date,
           last_updated_by    = p_user_id,
           ref_v07            = 'N'
     where change_req_id = p_contract_id;
    update con_contract ct
       set ct.offset_flag = 'N'
     where contract_id = p_contract_id;
  exception
  
    when e_defin_error then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => e_err_msg,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_date_amount');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_error then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '解约回购日期所在期或之后的租金,留购金,违约金已存在还款和开票记录,请先反冲!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_date_amount');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_write_off_flagg_err then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '第' ||
                                                                                  v_times ||
                                                                                  '期已有核销记录,请重新选择期数' ||
                                                                                  v_times ||
                                                                                  '之后数据发起回购!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_date_amount');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_et_date_err then
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '解约回购日期应小于合同结束日!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_date_amount');
    
      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            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_date_amount');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end calc_repo_date_amount;

  --提交工作流
  procedure submit_change_req_repo(p_change_req_id in number,
                                   p_layout_code   in varchar2,
                                   p_user_id       in number) is
    r_change_req_rec con_contract_change_req%rowtype;
    r_change_con_rec con_contract%rowtype;
  
    v_approval_method hls_document_type.approval_method%type;
    v_calc_hd_rec     hls_fin_calculator_hd%rowtype;
  
    v_success_flag varchar2(1);
  
    e_change_req_status_error exception;
  
    e_calc_error exception;
    e_simulation_error exception;
    e_calc_session_id_notfound exception;
    e_is_calc_flag_err exception;
    e_attachment_error exception;
    v_mail_list_id Number;
    v_time         Varchar2(1000);
    v_mail_to      Varchar2(1000);
    v_mail_cc      Varchar2(1000);
    v_subject      Varchar2(1000);
    v_body         Long;
    ---解约回购提交处添加校验  18083 2022/11/15
    v_tax_type_rate    Number;
    v_count number;
    v_max_times number;
    e_tax_amount_error exception;
  begin
    /*
      hls_document_compare_pkg.con_contract_compare(p_table_name    => 'CON_CONTRACT',
                                                    p_change_req_id => p_change_req_id,
                                                    p_from_table_pk => '',
                                                    p_to_table_pk   => p_change_req_id,
                                                    p_user_id       => p_user_id);
    */
    /*  r_change_req_rec := get_con_change_req_rec(p_change_req_id => p_change_req_id,
    p_user_id       => p_user_id);*/
    select *
      into r_change_req_rec
      from con_contract_change_req t
     where t.change_req_id = p_change_req_id;
  
    /*add by Harry 9952 2017/1/19
    purpose: verify the atttachment required property when submit the document
    */
    --应deron业务需要去除校验
    /* select count(*)
      into v_request_num
      from prj_cdd_item_doc_ref      dr,
           prj_cdd_item_check        ic,
           prj_cdd_item_list_grp_tab gt
     where dr.document_table = 'PRJ_PROJECT'
       and dr.document_id =
           (select c.project_id
              from con_contract c
             where c.contract_id = r_change_req_rec.contract_id)
       and dr.check_id = ic.check_id
       and ic.cdd_item_id = gt.cdd_item_id
       and gt.important_flag = 'Y'
       and not exists (select 1
              from fnd_atm_attachment_multi fam
             where fam.table_name = 'PRJ_CDD_ITEM_CHECK'
               and fam.table_pk_value = ic.check_id);
    if v_request_num > 0 then
      raise e_attachment_error;
    end if;*/
  
    /*  r_change_con_rec := con_contract_pkg.get_contract_rec(p_contract_id => p_change_req_id,
    p_user_id     => p_user_id);*/
  
    select *
      into r_change_con_rec
      from con_contract t
     where t.contract_id = p_change_req_id;
  
    if r_change_req_rec.req_status not in ('NEW', 'REJECT') then
      raise e_change_req_status_error;
    end if;
    
    --解约回购流程提交前校验解约回购最后一期倒减税额是否与现金流税额一致  18083 2022/11/15
    if r_change_con_rec.business_type='LEASE' then
    v_tax_type_rate :=0.13;
    else
      v_tax_type_rate :=0.06;
    end if;
    select max(ccc.times)
       into v_max_times
      from con_contract_cashflow ccc
     where ccc.contract_id = p_change_req_id
       and ccc.cf_item = 250;
       
    select count(*)
      into v_count
      from con_contract_cashflow cc
     where cc.contract_id = p_change_req_id
       and cc.cf_item = 250
       and abs(cc.vat_due_amount -
               round(cc.due_amount / (1 + v_tax_type_rate) * v_tax_type_rate, 2)) > 1
      and cc.times=v_max_times;
      
      if v_count>0 then
        null;
        --raise e_tax_amount_error; dev环境中税额计算不对,代码与正式环境中代码有差异,暂时注释此处校验
      end if;
  
    /* if r_change_con_rec.calc_session_id is null then
      raise e_calc_session_id_notfound;
    else
      select calc_successful
        into v_success_flag
        from hls_fin_calculator_hd
       where calc_session_id = r_change_con_rec.calc_session_id;
    
      if nvl(v_success_flag, 'N') <> 'Y' then
        raise e_calc_error;
      end if;
    
    end if;*/
  
    /*   con_change_req_custom_pkg.before_submit_change_req(p_change_req_id => p_change_req_id,
    p_user_id       => p_user_id);*/
  
    /* con_contract_history_pkg.calc_change_history(p_change_req_id => p_change_req_id,
    p_user_id       => p_user_id);*/
  
    cus_con_change_req_wfl_pkg.workflow_start(p_change_req_rec => r_change_req_rec,
                                              p_change_con_rec => r_change_con_rec,
                                              p_layout_code    => p_layout_code,
                                              p_user_id        => p_user_id);
    /*
    CON_CONTRACT_CHANGE_REQ_PKG.CHANGE_REQ_CONFIRM(p_change_req_id,
                                                   p_user_id => p_user_id);*/
    --回购提交流程发送邮件给代理店上级
    SELECT replace(wm_concat(ee.email), ',', ';')
      into v_mail_to
      FROM con_contract       cc,
           con_contract_bp    ccb,
           hls_bp_master      hbm,
           exp_org_position   eop,
           exp_emp_assign_e_v eea,
           exp_employees      ee
     where cc.contract_id = ccb.contract_id
       and hbm.bp_code = ccb.bp_code
       and ccb.bp_category = 'AGENT'
       and eea.employee_id = ee.employee_id
       and eop.unit_id = hbm.unit_id
       and eea.position_id = eop.parent_position_id
       and cc.CONTRACT_id = r_change_req_rec.change_req_id;
    v_time    := to_char(Sysdate, 'yyyy"年"mm"月"dd"日"');
    v_subject := '回购审批工作流审批通知';
    v_body    := '尊敬的领导,您好:' || chr(13) || chr(10) || chr(13) || chr(10) ||
                 chr(32) || chr(32) || chr(32) || chr(32) || chr(32) ||
                 chr(32) || '回购申请工作流-回购编号:' ||
                 r_change_req_rec.change_req_number || '合同编号:' ||
                 r_change_con_rec.contract_number ||
                 '已经到达您的审批节点,请您前往进行审批。
  http://10.203.0.125:8082/hlcm_leasing/login' || chr(13) ||
                 chr(10) || chr(13) || chr(10) || ' 十分感谢!';
    Select zj_sys_mailing_list_s.nextval Into v_mail_list_id From dual;
    ---待发送邮件记录插入
    zj_sys_mail_pkg.insert_mailing_list(p_mail_list_id   => v_mail_list_id,
                                        p_mail_to        => v_mail_to,
                                        p_mail_cc        => v_mail_cc,
                                        p_subject        => v_subject,
                                        p_body           => v_body,
                                        p_user_id        => p_user_id,
                                        p_mail_source    => Null,
                                        p_mail_source_id => Null,
                                        p_content_type   => 'text/plain');
  exception
    when e_attachment_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'PRJ_PROJECT_WORKFLOW_PKG.ATTACHMENT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'cus_con_contract_repo_pkg',
                                                      p_procedure_function_name => 'submit_change_req_repo');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_change_req_status_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_CHANGE_REQ_PKG.CHANGE_REQ_STATUS_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'cus_con_contract_repo_pkg',
                                                      p_procedure_function_name => 'submit_change_req_repo');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
     WHEN e_tax_amount_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '回购现金流税额不正确,请重新计算!',
                                                     p_created_by              => -1,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'submit_change_req_repo');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);

    when e_calc_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_CHANGE_REQ_PKG.CALCULATOR_STATUS_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'cus_con_contract_repo_pkg',
                                                      p_procedure_function_name => 'submit_change_req_repo');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    when e_is_calc_flag_err then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_CHANGE_REQ_PKG.IS_CALC_FLAG_ERR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'cus_con_contract_repo_pkg',
                                                      p_procedure_function_name => 'submit_change_req_repo');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_calc_session_id_notfound then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_CHANGE_REQ_PKG.CALC_SESSION_ID_NOTFOUND',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'cus_con_contract_repo_pkg',
                                                      p_procedure_function_name => 'submit_change_req_repo');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
    when e_simulation_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_CHANGE_REQ_PKG.SIMULATION_FLAG_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'cus_con_contract_repo_pkg',
                                                      p_procedure_function_name => 'submit_change_req_repo');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  end submit_change_req_repo;
  --回购申请创建现金流
  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,
                                   p_max_calc_time     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;
    v_times                    number;
  begin
  
    --回购变更特殊赋值
    v_cf_item      := 250;
    v_cf_type      := 1;
    v_cf_direction := 'INFLOW';
    v_cf_status    := 'RELEASE';
    v_times        := p_calculator_ln_rec.times + p_max_calc_time;
  
    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                       := v_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               := 'REPUR';
    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 into con_contract_cashflow values v_contract_cashflow_rec;
  
  end;

  procedure save_contract_cashflow(p_contract_id           number,
                                   p_fin_calculator_hd_rec hls_fin_calculator_hd%rowtype,
                                   p_user_id               number,
                                   p_max_calc_time         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
                                       and to_number(times) > 0
                                     order by to_number(times)) loop
    
      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,
                             p_max_calc_time     => p_max_calc_time);
    end loop;
    
  end;
  
  procedure restore_cashflow_from_con(p_change_req_id number,
                                      p_contract_id   number,
                                      p_user_id       number) is
     doc_pk_list hls_document_transfer_pkg.t_doc_pk_list;
    i           number;
    v_new_id    number;
    begin
          --CON_CONTRACT_CASHFLOW
    delete from con_contract_cashflow
     where contract_id = p_change_req_id;
    doc_pk_list.delete;
    delete from hls_document_history_ref r
     where r.document_history_id=p_change_req_id and r.table_name='CON_CONTRACT_CASHFLOW';
    i := 0;
    for c_cf in (select cashflow_id, calc_line_id, times
                   from con_contract_cashflow
                  where contract_id = p_contract_id
                  order by cashflow_id) loop
      i := i + 1;
      select con_contract_cashflow_s.nextval into v_new_id from dual;
    
      doc_pk_list(i).from_doc_pk := c_cf.cashflow_id;
      doc_pk_list(i).to_doc_pk := v_new_id;
    
      hls_document_history_pkg.create_document_history_ref(p_document_history_id => p_change_req_id,
                                                           p_table_name          => 'CON_CONTRACT_CASHFLOW',
                                                           p_from_pk_id          => c_cf.cashflow_id,
                                                           p_to_pk_id            => v_new_id,
                                                           p_user_id             => p_user_id);
    
      prj_cdd_item_pkg.prj_cdd_item_doc_ref_copy(p_from_document_table => 'CON_CONTRACT_CASHFLOW',
                                                 p_from_document_id    => c_cf.cashflow_id,
                                                 p_to_document_table   => 'CON_CONTRACT_CASHFLOW',
                                                 p_to_document_id      => v_new_id,
                                                 p_user_id             => p_user_id);
    
    end loop;
    hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'CON_CONTRACT_CASHFLOW',
                                         p_to_doc_table          => 'CON_CONTRACT_CASHFLOW',
                                         p_doc_pk_list           => doc_pk_list,
                                         p_copy_method           => 'DOC_TO_HISTORY',
                                         p_to_doc_column_1       => 'contract_id',
                                         p_to_doc_column_1_value => p_change_req_id,
                                         p_user_id               => p_user_id);

    --CON_UNEARNED_FINANCE_INCOME
    delete from con_unearned_finance_income
     where contract_id = p_change_req_id;
    doc_pk_list.delete;
    delete from hls_document_history_ref r
     where r.document_history_id = p_change_req_id
       and r.table_name = 'CON_UNEARNED_FINANCE_INCOME';
    i := 0;
    for c_cf in (select record_id
                   from con_unearned_finance_income
                  where contract_id = p_contract_id) loop
      i         := i + 1;
      select con_unearned_finance_income_s.nextval into v_new_id from dual;
    
      doc_pk_list(i).from_doc_pk := c_cf.record_id;
      doc_pk_list(i).to_doc_pk := v_new_id;
    
      hls_document_history_pkg.create_document_history_ref(p_document_history_id => p_change_req_id,
                                                           p_table_name          => 'CON_UNEARNED_FINANCE_INCOME',
                                                           p_from_pk_id          => c_cf.record_id,
                                                           p_to_pk_id            => v_new_id,
                                                           p_user_id             => p_user_id);
    
    
    end loop;
    hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'CON_UNEARNED_FINANCE_INCOME',
                                         p_to_doc_table          => 'CON_UNEARNED_FINANCE_INCOME',
                                         p_doc_pk_list           => doc_pk_list,
                                         p_copy_method           => 'DOC_TO_HISTORY',
                                         p_to_doc_column_1       => 'contract_id',
                                         p_to_doc_column_1_value => p_change_req_id,
                                         p_user_id               => p_user_id);
  
    update con_unearned_finance_income a
       set a.cashflow_id = (select r.from_pk_id
                              from hls_document_history_ref r
                             where r.document_history_id =
                                   p_change_req_id
                               and r.table_name = 'CON_CONTRACT_CASHFLOW'
                               and r.to_pk_id = a.cashflow_id)
     where a.contract_id = p_change_req_id
       and a.cashflow_id is not null;
   end;
  
  --事故单I-10652 回租解约回购利息及本金计算
  procedure deal_repur_cash_leaseback(p_contract_id   in number,
                            p_interest out number) is
    v_contract_rec             con_contract%rowtype;
    r_con_contract_change_req  con_contract_change_req%ROWTYPE;
    v_old_contract_rec         con_contract%rowtype;
    v_vat_principal            number;
    v_due_residual             number;
    v_overdue_interest  number;
    v_vat_sum_principal        number;
    v_vat_fund_possession_cost number;
    v_vat_service_charges      number;
    v_penalty                  number;
    v_tax_type_rate            number;
  begin
    select *
      into r_con_contract_change_req
      from con_contract_change_req t
     where t.change_req_id = p_contract_id;
  
    --变更前合同
    select *
      into v_old_contract_rec
      from con_contract t
     where t.contract_id = r_con_contract_change_req.contract_id;
    --变更中合同
    select *
      into v_contract_rec
      from con_contract t
     where t.contract_id = p_contract_id;
   /* select ft.tax_type_rate
      into v_tax_type_rate
      from fnd_tax_type_codes ft
     where ft.tax_type_id = v_contract_rec.tax_type_id;*/
   --有部分入金的情况:回购金中的利息之和=部分入金逾期租金中利息未收部分(如营业实收≥当期利息则为0)+逾期租金中的利息部分+留购费+滞纳金(回购金额中的)
   --无部分入金的情况:回购金中的利息之和=逾期租金中的利息部分+留购费+滞纳金(回购金额中的)
   --解约回购本金:解约回购租金-解约回购利息
    
    --逾期租金求和
    --未核销 -> 取interest
    --部分核销 手动公式计算
    for c_over_due_amount in (select *
                                from con_contract_cashflow f
                               where f.due_date <=
                                     r_con_contract_change_req.last_rent_due_date
                                 and f.cf_item = 1
                                 and f.cf_type = 1
                                 and f.contract_id =
                                     v_old_contract_rec.contract_id
                                 and f.write_off_flag <> 'FULL') loop
      if c_over_due_amount.write_off_flag = 'PARTIAL' then
        --当期营业实收”≤“当期应收本金”的则“当期利息”全部要算到回购金里面
        if c_over_due_amount.received_amount<=c_over_due_amount.principal then
          v_overdue_interest :=nvl(v_overdue_interest, 0)+nvl(c_over_due_amount.interest,0);
          else
        --“当期营业实收”>“当期应收本金”的则“当期应收-当期营业实收”的部分要算在回购金里面
        v_overdue_interest := nvl(v_overdue_interest, 0) +
                                    (c_over_due_amount.due_amount -
                                     nvl(c_over_due_amount.received_amount,
                                         0));
         end if;
       elsif c_over_due_amount.write_off_flag = 'NOT' then
        v_overdue_interest := nvl(v_overdue_interest, 0) +
                                    c_over_due_amount.interest;
      end if;
    end loop;
    --留购金
    begin
      select ccw.due_amount
        into v_due_residual
        from con_contract_cashflow ccw
       where ccw.contract_id = v_contract_rec.contract_id
         and cf_item = 8;
    exception
      when others then
        v_due_residual := 0;
    end;
    
    --滞纳金
    v_penalty :=v_contract_rec.ccr_due_amount;
    p_interest :=nvl(v_overdue_interest,0)+nvl(v_due_residual,0)+nvl(v_penalty,0);
  end;
  
  procedure deal_repur_cash(p_contract_id   in number,
                            p_vat_principal out number) is
    v_contract_rec             con_contract%rowtype;
    r_con_contract_change_req  con_contract_change_req%ROWTYPE;
    v_old_contract_rec         con_contract%rowtype;
    v_vat_principal            number;
    v_vat_residual             number;
    v_overdue_vat_due_amount   number;
    v_vat_sum_principal        number;
    v_vat_fund_possession_cost number;
    v_vat_service_charges      number;
    v_penalty                  number;
    v_tax_type_rate            number;
  begin
    select *
      into r_con_contract_change_req
      from con_contract_change_req t
     where t.change_req_id = p_contract_id;
  
    --变更前合同
    select *
      into v_old_contract_rec
      from con_contract t
     where t.contract_id = r_con_contract_change_req.contract_id;
    --变更中合同
    select *
      into v_contract_rec
      from con_contract t
     where t.contract_id = p_contract_id;
    select ft.tax_type_rate
      into v_tax_type_rate
      from fnd_tax_type_codes ft
     where ft.tax_type_id = v_contract_rec.tax_type_id;
    --vat_due_amount字段:回购当期~最后1期/本金求和+留购金+逾期租金求和+逾期罚息求和+资金占用费+回购手续费
    --注:当逾期租金和逾期罚息有部分核销的情况时,对未核销的行数,取Vat_due_amount;
    --对部分核销的行数,只能自己计算,先计算vat,后计算net
  
    --回购当期~最后1期/本金求和
    begin
      select sum(ccw.vat_principal)
        into v_vat_principal
        from con_contract_cashflow ccw
       where ccw.contract_id = v_contract_rec.contract_id
         and ccw.times >= r_con_contract_change_req.ccr_start_times
         and ccw.times <= v_old_contract_rec.lease_times
         and cf_item = 1;
    exception
      when others then
        v_vat_principal := 0;
    end;
    --留购金
    begin
      select ccw.vat_due_amount
        into v_vat_residual
        from con_contract_cashflow ccw
       where ccw.contract_id = v_contract_rec.contract_id
         and cf_item = 8;
    exception
      when others then
        v_vat_residual := 0;
    end;
    --逾期租金求和
    --未核销 -> 取vat_due_amount
    --部分核销 手动公式计算
    for c_over_due_amount in (select *
                                from con_contract_cashflow f
                               where f.due_date <=
                                     r_con_contract_change_req.last_rent_due_date
                                 and f.cf_item = 1
                                 and f.cf_type = 1
                                 and f.contract_id =
                                     v_old_contract_rec.contract_id
                                 and f.write_off_flag <> 'FULL') loop
      if c_over_due_amount.write_off_flag = 'PARTIAL' then
        v_overdue_vat_due_amount := nvl(v_overdue_vat_due_amount, 0) +
                                    (c_over_due_amount.due_amount -
                                     nvl(c_over_due_amount.received_amount,
                                         0)) / (1 + v_tax_type_rate) *
                                    v_tax_type_rate;
      elsif c_over_due_amount.write_off_flag = 'NOT' then
        v_overdue_vat_due_amount := nvl(v_overdue_vat_due_amount, 0) +
                                    c_over_due_amount.vat_due_amount;
      end if;
    end loop;
  
    --逾期罚息求和
    --判断调整后罚息是否和逾期罚息字段一致
    --未核销 -> 取vat_due_amount
    --部分核销 手动公式计算
    if nvl(v_contract_rec.ccr_due_amount, 0) =
       nvl(v_contract_rec.due_amount, 0) then
      for c_overdue_penalty in (select *
                                  from con_contract_cashflow f
                                 where f.times <
                                       r_con_contract_change_req.ccr_start_times
                                   and f.cf_item = 9
                                   and f.contract_id =
                                       v_old_contract_rec.contract_id
                                   and f.write_off_flag <> 'FULL') loop
        if c_overdue_penalty.write_off_flag = 'PARTIAL' then
          v_penalty := nvl(v_penalty, 0) +
                       (c_overdue_penalty.due_amount -
                        nvl(c_overdue_penalty.received_amount, 0)) /
                       (1 + v_tax_type_rate) * v_tax_type_rate;
        elsif c_overdue_penalty.write_off_flag = 'NOT' then
          v_penalty := nvl(v_penalty, 0) + c_overdue_penalty.due_amount/
                       (1 + v_tax_type_rate) * v_tax_type_rate;
        end if;
      end loop;
    else
      v_penalty := round(nvl(v_contract_rec.ccr_due_amount, 0) /
                         (1 + v_tax_type_rate) * v_tax_type_rate,
                         2);
    end if;
    v_vat_fund_possession_cost := round(nvl(v_contract_rec.fund_possession_cost,
                                            0) / (1 + v_tax_type_rate) *
                                        v_tax_type_rate,
                                        2);
    v_vat_service_charges      := round(nvl(v_contract_rec.repurchase_service_charges,
                                            0) / (1 + v_tax_type_rate) *
                                        v_tax_type_rate,
                                        2);
    p_vat_principal            := nvl(v_vat_principal, 0) +
                                  nvl(v_vat_residual, 0) +
                                  nvl(round(v_overdue_vat_due_amount, 2), 0) +
                                  nvl(round(v_penalty, 2), 0) +
                                  v_vat_fund_possession_cost +
                                  v_vat_service_charges;
  end;
  
  ---铁甲业务保证金自动核销
  --变更中先插入临时表
  --现金流相关字段先行更行change_req,模拟变更前已完成核销动作
  --到时候normal核销完成再执行覆盖normal现金流,亦无问题
  procedure calc_deposit_auto_split(p_change_req_id number,
                                    p_user_id number) is
     r_con_contract_rec  con_contract%rowtype;  
     v_deposit_deduct number; 
     v_write_off_interest number;
     v_write_off_princpal number;
     v_write_off_flag     varchar2(30);                     
    begin
      select cc.*
       into r_con_contract_rec from con_contract cc where cc.contract_id=p_change_req_id;
       v_deposit_deduct:=nvl(r_con_contract_rec.deposit_deduct,0);
      if v_deposit_deduct>0 and  r_con_contract_rec.division='70' then
      delete from csh_write_off_interface c where c.contract_id= p_change_req_id and c.trx_interface_id is null;
       for cashflow_tj in (select * from con_contract_cashflow c 
                         where c.contract_id=p_change_req_id
                               and c.write_off_flag <>'FULL'
                               and c.cf_item in ('1','9')
                               and c.cf_status!='CANCEL'
                               order by c.times,decode(c.cf_item,'9',1,2)) LOOP
          v_write_off_interest:=null;
          v_write_off_princpal:=null;
            --排除刚好等于0的情况避免插入为0数据
          if v_deposit_deduct=0 then
            exit;
          end if;
          if (cashflow_tj.due_amount-nvl(cashflow_tj.received_amount,0))>v_deposit_deduct then
                v_write_off_flag:='PARTIAL';
                
               if cashflow_tj.cf_item=1 then
                    if (cashflow_tj.principal-nvl(cashflow_tj.received_principal,0))>=v_deposit_deduct then
                      v_write_off_interest:=0;
                      v_write_off_princpal:=v_deposit_deduct; 
                      else
                      v_write_off_interest:=v_deposit_deduct-(cashflow_tj.principal-nvl(cashflow_tj.received_principal,0));
                      v_write_off_princpal:=cashflow_tj.principal-nvl(cashflow_tj.received_principal,0);  
                    end if;
                end if; 
          
               update con_contract_cashflow c
                  set c.received_amount    = v_deposit_deduct +
                                             nvl(cashflow_tj.received_amount,
                                                 0),
                      c.received_principal = nvl(cashflow_tj.received_principal,
                                                 0) +
                                             nvl(v_write_off_princpal, 0),
                      c.received_interest = nvl(cashflow_tj.received_interest,
                                                 0) +
                                             nvl(v_write_off_interest, 0),
                      c.write_off_flag=v_write_off_flag                       
                where c.cashflow_id = cashflow_tj.cashflow_id;
               
               insert into csh_write_off_interface(write_off_interface_id,
                                                   record_type,
                                                   batch_id,
                                                   trx_interface_id,
                                                   trx_number,
                                                   csh_bank_slip_num,
                                                   company_id,
                                                   contract_number,
                                                   contract_id,
                                                   write_off_cashflow_id,
                                                   write_off_type,
                                                   write_off_date,
                                                   write_off_period_name,
                                                   write_off_period_num,
                                                   write_off_times,
                                                   write_off_cf_item,
                                                   write_off_amount,
                                                   write_off_principal,
                                                   write_off_interest,
                                                   write_off_description,
                                                   write_off_bp_id,
                                                   check_flag,
                                                   post_flag,
                                                   write_off_id,
                                                   error_message,
                                                   creation_date,
                                                   created_by,
                                                   last_update_date,
                                                   last_updated_by,
                                                   write_off_classification,
                                                   agent_id,
                                                   assigns_contract_id,
                                                   journal_date,
                                                   conds_account_num,
                                                   conds_account_name) values
                                                    (csh_write_off_interface_s.nextval, 'MANUAL', 
                                                    null, null, null, null, 1, r_con_contract_rec.contract_number, 
                                                    cashflow_tj.contract_id, cashflow_tj.cashflow_id, 'DEPOSIT_CREDIT',
                                                      sysdate,  --核销日期
                                                    to_char(sysdate,'yyyy-mm'), --核销日所在期间
                                                    to_number(to_char(sysdate,'yyyymm')),   --核销日所在月份
                                                    cashflow_tj.times, cashflow_tj.cf_item,
                                                    v_deposit_deduct, --核销金额
                                                    v_write_off_princpal,  --核销本金
                                                    v_write_off_interest,            --核销利息
                                                    null, 
                                                    r_con_contract_rec.bp_id_tenant,
                                                    'Y', null, null, null, sysdate, 1, sysdate, 1,
                                                        null, null, cashflow_tj.contract_id, sysdate,
                                                         null, null);
               exit;
          else 
             v_write_off_flag:='FULL';
                
               if cashflow_tj.cf_item=1 then
                      v_write_off_interest:=(cashflow_tj.interest-nvl(cashflow_tj.received_interest,0));
                      v_write_off_princpal:=(cashflow_tj.principal-nvl(cashflow_tj.received_principal,0)); 
                end if; 
          
               update con_contract_cashflow c
                  set c.received_amount    = c.due_amount,
                      c.received_principal = c.principal,
                      c.received_interest =  c.interest,
                      c.write_off_flag=v_write_off_flag                       
                where c.cashflow_id = cashflow_tj.cashflow_id;
             insert into csh_write_off_interface(write_off_interface_id,
                                                 record_type,
                                                 batch_id,
                                                 trx_interface_id,
                                                 trx_number,
                                                 csh_bank_slip_num,
                                                 company_id,
                                                 contract_number,
                                                 contract_id,
                                                 write_off_cashflow_id,
                                                 write_off_type,
                                                 write_off_date,
                                                 write_off_period_name,
                                                 write_off_period_num,
                                                 write_off_times,
                                                 write_off_cf_item,
                                                 write_off_amount,
                                                 write_off_principal,
                                                 write_off_interest,
                                                 write_off_description,
                                                 write_off_bp_id,
                                                 check_flag,
                                                 post_flag,
                                                 write_off_id,
                                                 error_message,
                                                 creation_date,
                                                 created_by,
                                                 last_update_date,
                                                 last_updated_by,
                                                 write_off_classification,
                                                 agent_id,
                                                 assigns_contract_id,
                                                 journal_date,
                                                 conds_account_num,
                                                 conds_account_name) values (csh_write_off_interface_s.nextval,
                                                  'MANUAL', null, null, null, null, 1,
                                                   r_con_contract_rec.contract_number, cashflow_tj.contract_id, 
                                                   cashflow_tj.cashflow_id, 'DEPOSIT_CREDIT', 
                                                    sysdate,  --核销日期
                                                    to_char(sysdate,'yyyy-mm'), --核销日所在期间
                                                    to_number(to_char(sysdate,'yyyymm')),   --核销日所在月份
                                                    cashflow_tj.times, cashflow_tj.cf_item, 
                                                    cashflow_tj.due_amount, --核销金额
                                                    v_write_off_princpal,  --核销本金
                                                    v_write_off_interest,            --核销利息
                                                       null, 
                                                    r_con_contract_rec.bp_id_tenant, 'Y', null, null, null,
                                                     sysdate, 1, sysdate, 1, null, null, 
                                                     cashflow_tj.contract_id, sysdate, null, null);                                  
             v_deposit_deduct:=v_deposit_deduct-cashflow_tj.due_amount+nvl(cashflow_tj.received_amount,0);
             
          end if;
                               
        END LOOP;
    end if;
    end;

  procedure calc_repo_cashflow(p_contract_id     in number,
                               p_calc_session_id IN NUMBER,
                               p_user_id         IN NUMBER,
                               p_msg             OUT VARCHAR2) is
    r_con_contract_rec        con_contract%rowtype;
    v_max_calc_time           NUMBER;
    r_con_contract_change_req con_contract_change_req%ROWTYPE;
    r_calculator_hd_rec       hls_fin_calculator_hd%rowtype;
    v_new_calc_session_id     number;
    v_min_overdue_times       number;
    v_start_change_times      number;
    v_vat_principal           number;
    v_deposit_deduct          number;
    v_division    varchar2(30);
    
      --事故单I-10652
    v_cashflow_rec            con_contract_cashflow%ROWTYPE;
    v_interest              NUMBER; --利息
    v_principal             NUMBER; --本金
    v_outstanding_principal NUMBER; --剩余本金
    v_due_date              DATE;
    v_interest_back  number; --回租合同利息
  begin
  
    --step 1 单变量求解
    aux_single_variable_calc_pkg.calc_main(p_calc_session_id => p_calc_session_id,
                                           p_user_id         => p_user_id,
                                           p_msg             => p_msg);
    /* 
    --复制计算器4张表的数据
    hls_fin_calculator_itfc_pkg.copy_calculator(p_from_calc_session_id => p_calc_session_id,
                                                p_to_calc_session_id   => v_new_calc_session_id,
                                                p_user_id              => p_user_id);*/
    select *
      into r_con_contract_rec
      from con_contract t
     where t.contract_id = p_contract_id;
    SELECT *
      INTO r_con_contract_change_req
      FROM con_contract_change_req t
     WHERE t.change_req_id = p_contract_id;
  
    /*    select *
     into r_calculator_hd_rec
     from hls_fin_calculator_hd h
    where h.source_doc_category = 'CHANGE_CONTRACT'
      and h.source_doc_id = r_con_contract_change_req.contract_id;*/
    select *
      into r_calculator_hd_rec
      from hls_fin_calculator_hd h
     where h.calc_session_id = p_calc_session_id;
  
    --删除前一次变更计算插入解约回购租金的逻辑
    delete from con_contract_cashflow t
     where t.contract_id = p_contract_id
       and t.cf_item in (250);
     
     v_deposit_deduct:=nvl(r_con_contract_rec.deposit_deduct,0);
     v_division:=nvl(r_con_contract_rec.division,0);
    --还原前一次变更计算冻结的逻辑
    /*update con_contract_cashflow
       set cf_direction = 'INFLOW', cf_status = 'RELEASE'
     where contract_id = p_contract_id
       and cf_direction = 'NONCASH'
       and cf_status = 'BLOCK'
       and ln_user_col_v03 = 'REPUR';*/
    restore_cashflow_from_con(p_change_req_id =>p_contract_id,
                              p_contract_id =>r_con_contract_change_req.contract_id,
                              p_user_id     =>p_user_id);

   if v_deposit_deduct=0 then
    --重算的罚息现金流更新
     FOR c_contract_cashflow IN (SELECT a.*
                                  FROM con_contract_cashflow a,
                                       con_contract_cf_item  b
                                 WHERE a.contract_id = p_contract_id
                                   AND a.cf_status = 'RELEASE'
                                   AND a.due_date <=
                                       r_con_contract_change_req.last_rent_due_date
                                      --只有正常的才计算罚息。
                                   AND a.penalty_process_status = 'NORMAL'
                                   AND a.cf_direction = 'INFLOW'
                                   AND a.contract_id = b.contract_id
                                   AND a.cf_item = b.cf_item
                                   AND b.calc_penalty = 'Y'
                                 ORDER BY times, due_date) LOOP
      CON_OVERDUE_PENALTY_PKG.overdue_dayend(p_contract_cashflow_rec => c_contract_cashflow,
                                             p_contract_rec          => r_con_contract_rec,
                                             p_calc_date             => r_con_contract_change_req.repurchase_date,
                                             p_user_id               => p_user_id);
    
     END LOOP;
    end if;
    
    --铁甲业务保证金自动核销
    calc_deposit_auto_split(p_change_req_id =>p_contract_id,p_user_id=>p_user_id);
    
   
    --step 3 拼接变更起始日之前的现金流和变更起始日之后的现金流 组成新的cashflow
    --获取变更期数之前的最大一期  即前期支付预定日那期期数
    select cw.times
      into v_max_calc_time
      from con_contract_cashflow cw
     where cw.contract_id = r_con_contract_change_req.contract_id
       and cw.due_date = r_con_contract_rec.last_rent_due_date
       and cw.cf_item = 1;
    --获取逾期最小一期期数 冻结
    select min(cw.times)
      into v_min_overdue_times
      from con_contract_cashflow cw
     where cw.contract_id = r_con_contract_change_req.contract_id
       and cw.times <= v_max_calc_time
       and cw.write_off_flag <> 'FULL'
       and cw.cf_item in (1, 8)
       and nvl(cw.due_amount, 0) <> 0;
    if v_min_overdue_times is null then
      v_min_overdue_times := r_con_contract_change_req.ccr_start_times;
    end if;
    select max(cw.times)
      into v_start_change_times
      from con_contract_cashflow cw
     where cw.contract_id = r_con_contract_change_req.contract_id
       and cw.times <= v_max_calc_time
       and cw.write_off_flag <> 'FULL'
       and cw.cf_item in (1, 8)
       and nvl(cw.due_amount, 0) <> 0;
  
    --冻结变更日往后的现金流
    update con_contract_cashflow caw
       set caw.cf_status       = 'BLOCK',
           caw.cf_direction    = 'NONCASH',
           caw.ln_user_col_v03 = 'REPUR'
     where caw.contract_id = r_con_contract_rec.contract_id
       and ((caw.times >= v_min_overdue_times) or
           nvl(caw.due_amount, 0) = 0);
  
    --拼接变更后的报价
    save_contract_cashflow(p_contract_id           => p_contract_id,
                           p_fin_calculator_hd_rec => r_calculator_hd_rec,
                           p_user_id               => p_user_id,
                           p_max_calc_time         => r_con_contract_change_req.ccr_start_times - 1);
  
    --更新本次报价计算为Y
  
    update con_contract ct
       set ct.offset_flag = 'Y'
     where ct.contract_id = p_contract_id;
     
    --分期回购最后一期拆税区分直租和回租
    --if r_con_contract_rec.business_type='LEASE' then
    --处理最后一期解约回购本金利息拆税
    deal_repur_cash(p_contract_id   => p_contract_id,
                    p_vat_principal => v_vat_principal);
    hls_sys_log_pkg.log('分期回购合同id' || p_contract_id || '税额' ||v_vat_principal||'期数'||r_con_contract_change_req.ccr_start_times||'租赁期数'||r_calculator_hd_rec.lease_times);
  
    --分期回购倒减
    select v_vat_principal - sum(vat_principal)
      into v_vat_principal
      from con_contract_cashflow
     where contract_id = p_contract_id
       and cf_item = 250
       and times > 0
       and times < r_con_contract_change_req.ccr_start_times +
           to_number(r_calculator_hd_rec.lease_times) - 1;
  
    update con_contract_cashflow caw
       set caw.vat_principal  = v_vat_principal,
           caw.net_principal  = caw.principal - v_vat_principal,
           caw.vat_due_amount = v_vat_principal + caw.vat_interest,
           caw.net_due_amount = caw.due_amount - caw.vat_due_amount
     where caw.contract_id = r_con_contract_rec.contract_id
       and caw.cf_item = 250
       and caw.times = r_con_contract_change_req.ccr_start_times +
           to_number(r_calculator_hd_rec.lease_times) - 1;
           
    /* elsif r_con_contract_rec.business_type='LEASEBACK' THEN
     deal_repur_cash_leaseback(p_contract_id  =>p_contract_id,
                               p_interest =>v_interest_back);
     hls_sys_log_pkg.log('合同id' || p_contract_id || '税额' ||v_interest_back);
     update con_contract_cashflow caw
       set caw.interest=v_interest_back,
           caw.vat_interest=round(v_interest_back*0.06/1.06,2),
           caw.net_interest=v_interest_back-round(v_interest_back*0.06/1.06,2),
           caw.principal=caw.due_amount-v_interest_back,
           caw.vat_principal  = 0,
           caw.net_principal  = caw.due_amount-v_interest_back,
           caw.vat_due_amount = round(caw.due_amount*0.06/1.06,2),
           caw.net_due_amount = caw.due_amount - round(caw.due_amount*0.06/1.06,2)
     where caw.contract_id = r_con_contract_rec.contract_id
       and cf_item = 250
       and caw.times = r_con_contract_change_req.ccr_start_times +
           to_number(r_calculator_hd_rec.lease_times) - 1;
     end if;*/
      
     ----事故单I-10652  回租合同新增加一期开票现金流     
     if r_con_contract_rec.business_type='LEASEBACK' then
          ---插入一条售后回租分期回购本金
    v_cashflow_rec          := NULL;
    v_interest              := 0;
    v_principal             := round(r_con_contract_rec.repurchase_tatal_amount,
                                     2);
    v_outstanding_principal := 0;
    v_due_date              := r_con_contract_rec.paymengt_deadlinedate;
  
    v_cashflow_rec.cashflow_id                := con_contract_cashflow_s.nextval;
    v_cashflow_rec.contract_id                := r_con_contract_rec.contract_id;
    v_cashflow_rec.cf_item                    := 251;
    v_cashflow_rec.cf_type                    := 1;
    v_cashflow_rec.cf_direction               := 'INFLOW';
    v_cashflow_rec.cf_status                  := 'RELEASE';
    v_cashflow_rec.times                      := r_con_contract_change_req.ccr_start_times;
    v_cashflow_rec.calc_date                  := v_due_date;
    v_cashflow_rec.due_date                   := v_due_date;
    v_cashflow_rec.due_amount                 := r_con_contract_rec.repurchase_tatal_amount;
    v_cashflow_rec.vat_due_amount             := round(r_con_contract_rec.repurchase_tatal_amount * 0.06 / 1.06,
                                                       2);
    v_cashflow_rec.net_due_amount             := v_cashflow_rec.due_amount -
                                                 v_cashflow_rec.vat_due_amount;
    v_cashflow_rec.principal                  := r_con_contract_rec.repurchase_tatal_amount;
    v_cashflow_rec.vat_principal              := round(v_principal * 0.06 / 1.06,
                                                       2);
    v_cashflow_rec.net_principal              := v_cashflow_rec.principal -
                                                 v_cashflow_rec.vat_principal;
    v_cashflow_rec.interest                   := 0;
    v_cashflow_rec.net_interest               := 0;
    v_cashflow_rec.vat_interest               := 0;
    v_cashflow_rec.received_amount            := 0;
    v_cashflow_rec.received_principal         := 0;
    v_cashflow_rec.received_interest          := 0;
    v_cashflow_rec.outstanding_prin_tax_incld := v_outstanding_principal;
    v_cashflow_rec.write_off_flag             := 'NOT';
    v_cashflow_rec.overdue_status             := 'N';
    v_cashflow_rec.penalty_process_status     := 'NORMAL';
    v_cashflow_rec.billing_status             := 'NOT';
    v_cashflow_rec.ln_user_col_v03            := 'REPUR';
    v_cashflow_rec.generated_source           := 'MANUAL';
    v_cashflow_rec.created_by                 := p_user_id;
    v_cashflow_rec.creation_date              := SYSDATE;
    v_cashflow_rec.last_updated_by            := p_user_id;
    v_cashflow_rec.last_update_date           := SYSDATE;
    INSERT INTO con_contract_cashflow VALUES v_cashflow_rec;
    ---回租合同拆税
    cus_con_contract_repo_pkg.deal_repur_cash_leaseback(p_contract_id  =>p_contract_id,
                               p_interest =>v_interest_back);
      
     update con_contract_cashflow caw
       set caw.interest=v_interest_back,
           caw.vat_interest=round(v_interest_back*0.06/1.06,2),
           caw.net_interest=v_interest_back-round(v_interest_back*0.06/1.06,2),
           caw.principal=caw.due_amount-v_interest_back,
           caw.vat_principal  = 0,
           caw.net_principal  = caw.due_amount-v_interest_back,
           caw.vat_due_amount = round(caw.due_amount*0.06/1.06,2),
           caw.net_due_amount = caw.due_amount - round(caw.due_amount*0.06/1.06,2),
           --回租合同插入一次性现金流仅用作开票,无其它用途,此处需更新收款金额
           caw.received_interest=v_interest_back,
           caw.received_principal=caw.due_amount-v_interest_back
     where caw.contract_id = r_con_contract_rec.contract_id
       and cf_item = 251;
       end if;
       
  
  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            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_repo_cashflow');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  end calc_repo_cashflow;

  PROCEDURE calc_one_time_repo_cashflow(p_contract_id IN NUMBER,
                                        p_user_id     IN NUMBER) IS
    r_con_contract_rec        con_contract%ROWTYPE;
    v_cashflow_rec            con_contract_cashflow%ROWTYPE;
    r_con_contract_change_req con_contract_change_req%ROWTYPE;
    v_max_calc_time           NUMBER;
    v_min_overdue_times       number;
    v_start_change_times      number;
  
    v_due_amount            NUMBER; --每期租金
    v_outstanding_principal NUMBER; --剩余本金
    v_principal             NUMBER; --本金
    v_interest              NUMBER; --利息
    v_due_date              DATE;
    v_calc_due_date         DATE;
    v_times                 NUMBER;
    v_calc_days             NUMBER;
  
    e_due_amount_error EXCEPTION;
    e_repo_calc_amount_error EXCEPTION;
    v_vat_principal number;
    v_deposit_deduct number;
    v_division varchar2(10);
    --事故单I-10652 
    v_interest_back  number; --回租合同利息
    --事故单I-11074 
    e_last_rent_due_amount_error exception;
    
  BEGIN
    --回购合同
    select *
      into r_con_contract_rec
      from con_contract t
     where t.contract_id = p_contract_id;
    SELECT *
      INTO r_con_contract_change_req
      FROM con_contract_change_req t
     WHERE t.change_req_id = p_contract_id;
     
     --事故单I-11074 修改回购日直接退回导致的前期租金支付日不一致问题处理
     if r_con_contract_rec.last_rent_due_date<>r_con_contract_change_req.last_rent_due_date then
       raise e_last_rent_due_amount_error;
     end if;
     restore_cashflow_from_con(p_change_req_id =>p_contract_id,
                              p_contract_id =>r_con_contract_change_req.contract_id,
                              p_user_id     =>p_user_id);
    /*--删除前一次变更计算插入解约回购租金的逻辑
    delete from con_contract_cashflow t
     where t.contract_id = p_contract_id
       and t.cf_item in (250);
  
    --还原前一次变更计算冻结的逻辑
    update con_contract_cashflow
       set cf_direction = 'INFLOW', cf_status = 'RELEASE'
     where contract_id = p_contract_id
       and cf_direction = 'NONCASH'
       and cf_status = 'BLOCK'
       and ln_user_col_v03 = 'REPUR';
  
    --删除解约回购日之后的罚息
    delete from con_contract_cashflow t
     where t.contract_id = p_contract_id
       and t.cf_item = 9
       and t.times > r_con_contract_change_req.ccr_start_times
       and t.write_off_flag = 'NOT';*/
     --回购校验
     con_change_single_calc_pkg.check_deposit_deduct(p_contract_id,p_user_id);
    --重算的罚息现金流更新
    FOR c_contract_cashflow IN (SELECT a.*
                                  FROM con_contract_cashflow a,
                                       con_contract_cf_item  b
                                 WHERE a.contract_id = p_contract_id
                                   AND a.cf_status = 'RELEASE'
                                   AND a.due_date <=
                                       r_con_contract_change_req.last_rent_due_date
                                      --只有正常的才计算罚息。
                                   AND a.penalty_process_status = 'NORMAL'
                                   AND a.cf_direction = 'INFLOW'
                                   AND a.contract_id = b.contract_id
                                   AND a.cf_item = b.cf_item
                                   AND b.calc_penalty = 'Y'
                                 ORDER BY times, due_date) LOOP
    
      CON_OVERDUE_PENALTY_PKG.overdue_dayend(p_contract_cashflow_rec => c_contract_cashflow,
                                             p_contract_rec          => r_con_contract_rec,
                                             p_calc_date             => r_con_contract_change_req.repurchase_date,
                                             p_user_id               => p_user_id);
    
    END LOOP;
    
    --铁甲业务保证金自动核销
    calc_deposit_auto_split(p_change_req_id =>p_contract_id,p_user_id=>p_user_id);
    
    --step 3 拼接变更起始日之前的现金流和变更起始日之后的现金流 组成新的cashflow
    --获取变更期数之前的最大一期  即前期支付预定日那期期数
    select cw.times
      into v_max_calc_time
      from con_contract_cashflow cw
     where cw.contract_id = r_con_contract_change_req.contract_id
       and cw.due_date = r_con_contract_rec.last_rent_due_date
       and cw.cf_item = 1;
    --获取逾期最小一期期数 冻结
    select min(cw.times)
      into v_min_overdue_times
      from con_contract_cashflow cw
     where cw.contract_id = r_con_contract_change_req.contract_id
       and cw.times <= v_max_calc_time
       and cw.write_off_flag <> 'FULL'
       and cw.cf_item in (1, 8)
       and nvl(cw.due_amount, 0) <> 0;
    if v_min_overdue_times is null then
      v_min_overdue_times := r_con_contract_change_req.ccr_start_times;
    end if;
    select max(cw.times)
      into v_start_change_times
      from con_contract_cashflow cw
     where cw.contract_id = r_con_contract_change_req.contract_id
       and cw.times <= v_max_calc_time
       and cw.write_off_flag <> 'FULL'
       and cw.cf_item in (1, 8)
       and nvl(cw.due_amount, 0) <> 0;
    --冻结变更日往后的现金流
    update con_contract_cashflow caw
       set caw.cf_status       = 'BLOCK',
           caw.cf_direction    = 'NONCASH',
           caw.ln_user_col_v03 = 'REPUR'
     where caw.contract_id = r_con_contract_rec.contract_id
       and ((caw.times >= v_min_overdue_times) or
           nvl(caw.due_amount, 0) = 0);
    -- 插入一条一次性回购数据       
    v_cashflow_rec          := NULL;
    v_interest              := 0;
    v_principal             := round(r_con_contract_rec.repurchase_tatal_amount,
                                     2);
    v_outstanding_principal := 0;
    v_due_date              := r_con_contract_rec.paymengt_deadlinedate;
  
    v_cashflow_rec.cashflow_id                := con_contract_cashflow_s.nextval;
    v_cashflow_rec.contract_id                := r_con_contract_rec.contract_id;
    v_cashflow_rec.cf_item                    := 250;
    v_cashflow_rec.cf_type                    := 1;
    v_cashflow_rec.cf_direction               := 'INFLOW';
    v_cashflow_rec.cf_status                  := 'RELEASE';
    v_cashflow_rec.times                      := r_con_contract_change_req.ccr_start_times;
    v_cashflow_rec.calc_date                  := v_due_date;
    v_cashflow_rec.due_date                   := v_due_date;
    v_cashflow_rec.due_amount                 := r_con_contract_rec.repurchase_tatal_amount;
    v_cashflow_rec.vat_due_amount             := round(r_con_contract_rec.repurchase_tatal_amount * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.net_due_amount             := v_cashflow_rec.due_amount -
                                                 v_cashflow_rec.vat_due_amount;
    v_cashflow_rec.principal                  := r_con_contract_rec.repurchase_tatal_amount;
    v_cashflow_rec.vat_principal              := round(v_principal * 0.13 / 1.13,
                                                       2);
    v_cashflow_rec.net_principal              := v_cashflow_rec.principal -
                                                 v_cashflow_rec.vat_principal;
    v_cashflow_rec.interest                   := 0;
    v_cashflow_rec.net_interest               := 0;
    v_cashflow_rec.vat_interest               := 0;
    v_cashflow_rec.received_amount            := 0;
    v_cashflow_rec.received_principal         := 0;
    v_cashflow_rec.received_interest          := 0;
    v_cashflow_rec.outstanding_prin_tax_incld := v_outstanding_principal;
    v_cashflow_rec.write_off_flag             := 'NOT';
    v_cashflow_rec.overdue_status             := 'N';
    v_cashflow_rec.penalty_process_status     := 'NORMAL';
    v_cashflow_rec.billing_status             := 'NOT';
    v_cashflow_rec.ln_user_col_v03            := 'REPUR';
    v_cashflow_rec.generated_source           := 'MANUAL';
    v_cashflow_rec.created_by                 := p_user_id;
    v_cashflow_rec.creation_date              := SYSDATE;
    v_cashflow_rec.last_updated_by            := p_user_id;
    v_cashflow_rec.last_update_date           := SYSDATE;
    INSERT INTO con_contract_cashflow VALUES v_cashflow_rec;
  
    --更新本次报价计算为Y
    update con_contract ct
       set ct.offset_flag = 'Y'
     where ct.contract_id = p_contract_id;
    
    --事故单I-10652 回租合同解约回购单独处理
    if r_con_contract_rec.business_type='LEASE' then
    --处理最后一期解约回购本金利息拆税
    deal_repur_cash(p_contract_id   => p_contract_id,
                    p_vat_principal => v_vat_principal);
    hls_sys_log_pkg.log('合同id' || p_contract_id || '税额' ||v_vat_principal);
    update con_contract_cashflow caw
       set caw.vat_principal  = v_vat_principal,
           caw.net_principal  = caw.principal - v_vat_principal,
           caw.vat_due_amount = v_vat_principal,
           caw.net_due_amount = caw.due_amount - v_vat_principal
     where caw.contract_id = r_con_contract_rec.contract_id
       and cf_item = 250;
       
   elsif r_con_contract_rec.business_type='LEASEBACK' THEN
     deal_repur_cash_leaseback(p_contract_id  =>p_contract_id,
                               p_interest =>v_interest_back);
     hls_sys_log_pkg.log('合同id' || p_contract_id || '税额' ||v_interest_back);
     update con_contract_cashflow caw
       set caw.interest=v_interest_back,
           caw.vat_interest=round(v_interest_back*0.06/1.06,2),
           caw.net_interest=v_interest_back-round(v_interest_back*0.06/1.06,2),
           caw.principal=caw.due_amount-v_interest_back,
           caw.vat_principal  = 0,
           caw.net_principal  = caw.due_amount-v_interest_back,
           caw.vat_due_amount = round(caw.due_amount*0.06/1.06,2),
           caw.net_due_amount = caw.due_amount - round(caw.due_amount*0.06/1.06,2)
     where caw.contract_id = r_con_contract_rec.contract_id
       and cf_item = 250;
     end if;
  
  EXCEPTION
    WHEN e_repo_calc_amount_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '回购价款总额小于0,请检查!',
                                                     p_created_by              => -1,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_one_time_repo_cashflow');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_due_amount_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '回购合同计算租金出错,请联系系统管理员!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_one_time_repo_cashflow');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
     WHEN e_last_rent_due_amount_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '前期租金支付日发生了变更,请取消本次变更申请重新发起变更!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_one_time_repo_cashflow');
    
      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            => 'cus_con_contract_repo_pkg',
                                                     p_procedure_function_name => 'calc_one_time_repo_cashflow');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END calc_one_time_repo_cashflow;
  
  PROCEDURE calc_cashflow_division_tj(p_contract_id IN NUMBER,
                                        p_user_id     IN NUMBER) is
  r_con_contract_rec        con_contract%ROWTYPE;
  v_deposit_deduct          number; 
  v_division                 varchar2(10);                                       
  begin 
     --回购合同
    select *
      into r_con_contract_rec
      from con_contract t
     where t.contract_id = p_contract_id;      
     
     --铁甲业务保证金自动核销
    select nvl(req.deposit_deduct,0) into v_deposit_deduct from con_contract req  Where req.contract_id = r_con_contract_rec.contract_id;
    select req.division into v_division from con_contract req  Where req.contract_id = r_con_contract_rec.contract_id;

    if v_deposit_deduct>0 and v_division='70' and r_con_contract_rec.contract_id<>null then
       delete from csh_write_off_interface c where c.contract_id= r_con_contract_rec.contract_id and c.trx_interface_id is null;
       for cashflow_tj in (select * from con_contract_cashflow c 
                         where c.contract_id=r_con_contract_rec.contract_id
                               and c.write_off_flag <>'FULL'
                               and c.cf_item in ('1','9')
                               order by c.times,decode(c.cf_item,'9',1,2)) LOOP
          if (cashflow_tj.due_amount-nvl(cashflow_tj.received_amount,0))>=v_deposit_deduct then
               update con_contract_cashflow c set c.received_amount=v_deposit_deduct+nvl(cashflow_tj.received_amount,0)
                                               where c.cashflow_id=cashflow_tj.cashflow_id;
               insert into csh_write_off_interface values (csh_write_off_interface_s.nextval, 'MANUAL', null, null, null, null, 1, r_con_contract_rec.contract_number, cashflow_tj.contract_id, cashflow_tj.cashflow_id, 'DEPOSIT_CREDIT', sysdate, '2018-11', 201811, cashflow_tj.times, cashflow_tj.cf_item, v_deposit_deduct, v_deposit_deduct, null, null, r_con_contract_rec.bp_id_tenant, 'Y', null, null, null, sysdate, 1, sysdate, 1, null, null, cashflow_tj.contract_id, sysdate, null, null);
               exit;
          else 
             update con_contract_cashflow c set c.received_amount=cashflow_tj.due_amount
                                               where c.cashflow_id=cashflow_tj.cashflow_id;
             insert into csh_write_off_interface values (csh_write_off_interface_s.nextval, 'MANUAL', null, null, null, null, 1, r_con_contract_rec.contract_number, cashflow_tj.contract_id, cashflow_tj.cashflow_id, 'DEPOSIT_CREDIT', sysdate, '2018-11', 201811, cashflow_tj.times, cashflow_tj.cf_item, cashflow_tj.due_amount, cashflow_tj.due_amount, null, null, r_con_contract_rec.bp_id_tenant, 'Y', null, null, null, sysdate, 1, sysdate, 1, null, null, cashflow_tj.contract_id, sysdate, null, null);                                  
             v_deposit_deduct:=v_deposit_deduct-cashflow_tj.due_amount+nvl(cashflow_tj.received_amount,0);
             
          end if;
                               
        END LOOP;
    end if;
                                    
  END   calc_cashflow_division_tj;                                    
  
  PROCEDURE repo_wfl_approved_mail(p_change_req_id number,
                                   p_user_id       IN NUMBER) is
    r_change_con_rec    con_contract%rowtype;
    v_bp_name           varchar2(1000);
    v_repurchase_type_n varchar2(1000);
    v_sumbit_user_id    number;
    v_mail_list_id      Number;
    v_contract_number   varchar2(1000);
    v_time              Varchar2(1000);
    v_mail_to           Varchar2(1000);
    v_mail_cc           Varchar2(1000);
    v_subject           Varchar2(1000);
    v_body              Long;
    v_repurchase_date   DATE;
  begin
    --邮件文本变量字段
    select t.contract_number,
           hbm.bp_name,
           (SELECT v.code_value_name
              FROM sys_code_values_v v
             WHERE v.code = 'REPURCHASE_TYPES'
               AND v.code_value = t.REPURCHASE_TYPES) AS REPURCHASE_TYPES_n,
           t.repurchase_date
      into v_contract_number,
           v_bp_name,
           v_repurchase_type_n,
           v_repurchase_date
      from con_contract t, hls_bp_master hbm
     where t.contract_id = p_change_req_id
       and t.bp_id_tenant = hbm.bp_id;
    --mail_to 邮箱
    -- 013 GAOYAN   015 ZHAOJIE
    /*select replace(wm_concat(email), ',', ';')
     into v_mail_to
     from exp_employees
    where employee_code in ('GAOYAN', 'ZHAOJIE');*/
    -- @Description:  @Date:  @Modified:Liyuan.Chen 
    v_mail_to := sys_parameter_pkg.email_by_position('013') ||
                 sys_parameter_pkg.email_by_position('015');
  
    Select zj_sys_mailing_list_s.nextval Into v_mail_list_id From dual;
    v_time    := to_char(v_repurchase_date, 'yyyy"年"mm"月"dd"日"');
    v_subject := '回购审批工作流审批通知';
    v_body    := '尊敬的领导,您好:' || chr(13) || chr(10) || chr(13) || chr(10) ||
                 chr(32) || chr(32) || chr(32) || chr(32) || chr(32) ||
                 chr(32) || '合同编号:' || v_contract_number || ',承租人:' ||
                 v_bp_name || ',通过了' || v_repurchase_type_n || '申请,解约回购日:' ||
                 v_time || chr(13) || chr(10) || chr(13) || chr(10) ||
                 '请知悉,谢谢!';
    Select zj_sys_mailing_list_s.nextval Into v_mail_list_id From dual;
    ---待发送邮件记录插入
    zj_sys_mail_pkg.insert_mailing_list(p_mail_list_id   => v_mail_list_id,
                                        p_mail_to        => v_mail_to,
                                        p_mail_cc        => v_mail_cc,
                                        p_subject        => v_subject,
                                        p_body           => v_body,
                                        p_user_id        => p_user_id,
                                        p_mail_source    => Null,
                                        p_mail_source_id => Null,
                                        p_content_type   => 'text/plain');
    --发送给提交人
    repo_wfl_mail_to_submit(p_change_req_id => p_change_req_id,
                            p_user_id       => p_user_id,
                            p_wfl_flag_desc => '通过');
  
  end repo_wfl_approved_mail;

  PROCEDURE repo_wfl_mail_to_submit(p_change_req_id number,
                                    p_user_id       IN NUMBER,
                                    p_wfl_flag_desc varchar2) is
    r_change_req_rec    con_contract_change_req%rowtype;
    r_change_con_rec    con_contract%rowtype;
    v_mail_list_id      Number;
    v_contract_number   varchar2(1000);
    v_change_req_number varchar2(1000);
    v_time              Varchar2(1000);
    v_mail_to           Varchar2(1000);
    v_mail_cc           Varchar2(1000);
    v_subject           Varchar2(1000);
    v_body              Long;
  begin
    ----邮件文本变量字段,mail_to
    select t.change_req_number, ee.email
      into v_change_req_number, v_mail_to
      from con_contract_change_req t, sys_user su, exp_employees ee
     where t.change_req_id = p_change_req_id
       and t.created_by = su.user_id
       and su.employee_id = ee.employee_id;
    --合同信息
    select *
      into r_change_con_rec
      from con_contract t
     where t.contract_id = p_change_req_id;
  
    v_time    := to_char(Sysdate, 'yyyy"年"mm"月"dd"日"');
    v_subject := '回购审批工作流审批通知';
    v_body    := '您好:' || chr(13) || chr(10) || chr(13) || chr(10) ||
                 chr(32) || chr(32) || chr(32) || chr(32) || chr(32) ||
                 chr(32) || '回购申请工作流-回购编号:' || v_change_req_number ||
                 '合同编号:' || r_change_con_rec.contract_number || ',工作流审批已' ||
                 p_wfl_flag_desc || '。' || '
  http://10.203.0.125:8082/hlcm_leasing/login' || chr(13) ||
                 chr(10) || chr(13) || chr(10) || '请知悉,谢谢!';
    Select zj_sys_mailing_list_s.nextval Into v_mail_list_id From dual;
    ---待发送邮件记录插入
    zj_sys_mail_pkg.insert_mailing_list(p_mail_list_id   => v_mail_list_id,
                                        p_mail_to        => v_mail_to,
                                        p_mail_cc        => v_mail_cc,
                                        p_subject        => v_subject,
                                        p_body           => v_body,
                                        p_user_id        => p_user_id,
                                        p_mail_source    => Null,
                                        p_mail_source_id => Null,
                                        p_content_type   => 'text/plain');
  end repo_wfl_mail_to_submit;
end cus_con_contract_repo_pkg;
/