csh_payment_req_pkg.pck 145 KB
Newer Older
stone's avatar
stone 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 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770
CREATE OR REPLACE PACKAGE csh_payment_req_pkg IS

  -- Author  : qwm
  -- Created : 2013/5/24 16:35:21
  -- Purpose :
  /*procedure csh_payment_req_amount_check(p_cashflow_id        number,
  p_amount             number,
  p_company_id         number,
  p_user_id            number,
  p_transaction_date   date,
  p_amount_flag        out varchar2,
  p_payment_req_number out varchar2,
  p_document_type      varchar2);*/
  FUNCTION get_bp_id(p_contract_id NUMBER) RETURN NUMBER;

  PROCEDURE check_cf_after_ins_temp(p_session_id NUMBER, p_user_id NUMBER);

  PROCEDURE check_cf_0_total_amt(p_contract_id NUMBER, p_user_id NUMBER);

  FUNCTION get_bp_category_desc(p_contract_id NUMBER, p_bp_id NUMBER)
    RETURN VARCHAR2;

  FUNCTION get_payment_req_number(p_document_type     VARCHAR2,
                                  p_transaction_date  DATE,
                                  p_company_id        NUMBER,
                                  p_user_id           NUMBER,
                                  p_document_category VARCHAR2)
    RETURN VARCHAR2;

  PROCEDURE payment_submit(p_payment_req_id NUMBER, p_user_id NUMBER);

  PROCEDURE change_print_status(p_contract_id NUMBER, p_user_id NUMBER);

  PROCEDURE change_printed_status(p_contract_id NUMBER, p_user_id NUMBER);

  PROCEDURE change_applied_status(p_contract_id NUMBER, p_user_id NUMBER);

  PROCEDURE csh_payment_apply(p_contract_id NUMBER, p_user_id NUMBER);

  PROCEDURE csh_payment_print(p_contract_id NUMBER, p_user_id NUMBER);

  PROCEDURE upd_csh_payment_after_reverse(p_transaction_id csh_transaction.transaction_id%TYPE,
                                          p_user_id        NUMBER);

  PROCEDURE close_csh_payment_req_hd(p_payment_req_id NUMBER,
                                     p_closed_date    DATE,
                                     p_closed_note    VARCHAR2,
                                     p_user_id        NUMBER);

  PROCEDURE check_csh_payment_bp_unique(p_payment_req_id NUMBER,
                                        p_user_id        NUMBER);

  PROCEDURE check_csh_payment_bp(p_payment_req_id NUMBER, p_user_id NUMBER);

  --保证金退还 付款申请 校验 add by Harry 9952 2017/2/24
  PROCEDURE check_csh_payment_bond(p_payment_req_id NUMBER,
                                   p_user_id        NUMBER);
  PROCEDURE payment_csh_payment(p_payment_req_id          NUMBER,
                                p_payment_req_ln_id       NUMBER,
                                p_pay_date                DATE,
                                p_pay_amount              NUMBER,
                                p_company_id              NUMBER,
                                p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                p_period_name             csh_transaction.period_name%TYPE,
                                p_exchange_rate_type      csh_transaction.exchange_rate_type%TYPE,
                                p_exchange_rate_quotation csh_transaction.exchange_rate_quotation%TYPE,
                                p_exchange_rate           csh_transaction.exchange_rate%TYPE,
                                p_bank_account_id         csh_transaction.bank_account_id%TYPE,
                                p_bank_account_num        VARCHAR2,
                                p_description             csh_transaction.description%TYPE,
                                p_user_id                 NUMBER,
                                p_cashflow_id             NUMBER,
                                p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                p_bp_bank_account_id      csh_transaction.bp_bank_account_id%TYPE,
                                p_bp_bank_account_num     csh_transaction.bp_bank_account_num%TYPE,
                                p_handling_charge         csh_transaction.handling_charge%TYPE,
                                p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                p_merge_flag              VARCHAR2 DEFAULT 'N',
                                p_payment_behalf_flag     VARCHAR2 DEFAULT 'N',
                                p_payment_status          OUT VARCHAR2,
                                p_transaction_id          IN OUT NUMBER,
                                p_bp_category             varchar2 default null,
                                p_due_amount              NUMBER DEFAULT NULL);
  PROCEDURE payment_csh_payment(p_payment_req_id          NUMBER,
                                p_session_id              NUMBER,
                                p_pay_date                DATE,
                                p_pay_amount              NUMBER,
                                p_company_id              NUMBER,
                                p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                p_period_name             csh_transaction.period_name%TYPE,
                                p_exchange_rate_type      csh_transaction.exchange_rate_type%TYPE,
                                p_exchange_rate_quotation csh_transaction.exchange_rate_quotation%TYPE,
                                p_exchange_rate           csh_transaction.exchange_rate%TYPE,
                                p_bank_account_id         csh_transaction.bank_account_id%TYPE,
                                p_bank_account_num        VARCHAR2,
                                p_description             csh_transaction.description%TYPE,
                                p_user_id                 NUMBER,
                                p_cashflow_id             NUMBER,
                                p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                p_bp_bank_account_id      csh_transaction.bp_bank_account_id%TYPE,
                                p_bp_bank_account_num     csh_transaction.bp_bank_account_num%TYPE,
                                p_handling_charge         csh_transaction.handling_charge%TYPE,
                                p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                p_payment_status          OUT VARCHAR2,
                                p_merge_flag              VARCHAR2 DEFAULT 'N',
                                p_payment_behalf_flag     VARCHAR2 DEFAULT 'N',
                                p_this_pay_agent_amount   NUMBER,
                                p_this_pay_vender_amount  NUMBER,
                                p_bank_flag               VARCHAR2 DEFAULT 'N',
                                p_bp_category             varchar2 default null,
                                p_due_amount              NUMBER DEFAULT NULL);

  PROCEDURE ins_csh_pay_cashflow_id_temp(p_session_id  NUMBER,
                                         p_cashflow_id NUMBER,
                                         p_user_id     NUMBER);

  PROCEDURE del_csh_pay_cashflow_id_temp(p_session_id NUMBER,
                                         p_user_id    NUMBER);

  PROCEDURE ins_csh_payment_req_hd(p_payment_req_id       IN OUT NUMBER,
                                   p_company_id           NUMBER,
                                   p_document_type        VARCHAR2,
                                   p_payment_req_number   VARCHAR2,
                                   p_req_date             DATE,
                                   p_apply_pay_date       DATE,
                                   p_transaction_category VARCHAR2,
                                   p_distribution_set_id  NUMBER,
                                   p_payment_method_id    NUMBER,
                                   p_bp_id                NUMBER,
                                   p_bp_category          VARCHAR2,
                                   p_amount               NUMBER,
                                   p_currency_code        VARCHAR2,
                                   p_description          VARCHAR2,
                                   p_submitted_flag       VARCHAR2,
                                   p_printed_flag         VARCHAR2,
                                   p_printed_times        NUMBER,
                                   p_last_print_date      DATE,
                                   p_closed_flag          VARCHAR2,
                                   p_closed_date          DATE,
                                   p_closed_note          VARCHAR2,
                                   p_approval_status      VARCHAR2,
                                   p_approval_date        DATE,
                                   p_approval_note        VARCHAR2,
                                   p_bp_bank_account_id   NUMBER DEFAULT NULL,
                                   p_bp_bank_account_num  VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_account_name VARCHAR2 DEFAULT NULL,
                                   p_business_type        VARCHAR2 DEFAULT NULL,
                                   p_branch_id            NUMBER,
                                   p_city_id              NUMBER,
                                   p_user_id              NUMBER,
                                   p_csh_bp_name          VARCHAR2 DEFAULT NULL);

  PROCEDURE ins_csh_payment_req_ln(p_payment_req_ln_id      IN OUT NUMBER,
                                   p_payment_req_id         NUMBER,
                                   p_payment_req_ln_type    VARCHAR2,
                                   p_ref_doc_category       VARCHAR2,
                                   p_ref_doc_id             NUMBER,
                                   p_ref_doc_line_id        NUMBER,
                                   p_bp_id                  NUMBER,
                                   p_currency_code          VARCHAR2,
                                   p_amount                 NUMBER,
                                   p_amount_paid            NUMBER,
                                   p_description            VARCHAR2,
                                   p_payment_status         VARCHAR2,
                                   p_payment_completed_date DATE,
                                   p_apply_pay_date         DATE,
                                   p_payment_method_id      NUMBER,
                                   p_bank_account_id        NUMBER,
                                   p_bank_account_num       VARCHAR2,
                                   p_bank_account_name      VARCHAR2,
                                   p_bank_branch_name       VARCHAR2,
                                   p_bp_bank_account_id     NUMBER DEFAULT NULL,
                                   p_bp_bank_account_num    VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_account_name   VARCHAR2 DEFAULT NULL,
                                   p_user_id                NUMBER,
                                   p_company_id             NUMBER);

  PROCEDURE del_csh_payment_req_ln(p_payment_req_ln_id NUMBER,
                                   p_user_id           NUMBER);

  PROCEDURE del_csh_payment_req_hd(p_payment_req_id NUMBER,
                                   p_user_id        NUMBER);

  PROCEDURE update_csh_payment_flag(p_payment_req_id NUMBER,
                                    p_payment_flag   VARCHAR2,
                                    p_user_id        NUMBER);

  PROCEDURE update_payment_req_number(p_payment_req_id     IN NUMBER,
                                      p_payment_req_number IN VARCHAR2);

  --add by zxf                               
  PROCEDURE update_payment_req_contract_id(p_payment_req_id IN NUMBER,
                                           p_contract_id    IN NUMBER);

  PROCEDURE check_ddct_amount(p_payment_req_id IN NUMBER);

  PROCEDURE check_contract_payment(p_contract_id    NUMBER,
                                   p_payment_req_id NUMBER,
                                   p_user_id        NUMBER);

  --add by 9188 检验付款申请工作流的所有合同是否全部核销
  PROCEDURE check_sch_payment_con(p_payment_req_id NUMBER,
                                  p_user_id        NUMBER);

  --add by Harry 9952 保证金退还-付款申请校验
  PROCEDURE check_status_and_flag(p_cashflow_id NUMBER, p_user_id NUMBER);

  PROCEDURE create_cdd(p_payment_req_id NUMBER,
                       p_user_id        NUMBER,
                       p_cdd_list_id    IN OUT NUMBER);
  PROCEDURE save_cdd_item_from_templt(p_document_category VARCHAR2,
                                      p_document_type     VARCHAR2,
                                      p_document_id       NUMBER,
                                      p_company_id        NUMBER,
                                      p_user_id           NUMBER,
                                      p_cdd_list_id       IN OUT NUMBER);

END csh_payment_req_pkg;
/
CREATE OR REPLACE PACKAGE BODY csh_payment_req_pkg IS

  v_err_code VARCHAR2(2000);

  e_lock_trx_error EXCEPTION;

  e_lock_error EXCEPTION;

  PRAGMA EXCEPTION_INIT(e_lock_trx_error, -54);

  PRAGMA EXCEPTION_INIT(e_lock_error, -54);

  FUNCTION get_bp_id(p_contract_id NUMBER) RETURN NUMBER IS
    v_bp_id hls_bp_master.bp_id%TYPE;
    --v_contract_rec con_contract%ROWTYPE;
  BEGIN
    --库融和保理的经销商不必输,如果为空则取bp_id_agent_level1
    /*SELECT nvl(t.bp_id_agent_level1,
    t.bp_id_tenant)*/
    /*SELECT t.bp_id_tenant
     INTO v_bp_id
     FROM con_contract t
    WHERE t.contract_id = p_contract_id;*/
    SELECT t3.bp_id
      INTO v_bp_id
      FROM con_contract_bp t2, hls_bp_master t3
      --delete by
    WHERE
       -- t2.bp_category = 'AGENT' AND 
       t3.bp_id = t2.bp_id
       AND t2.contract_id = p_contract_id;
    RETURN v_bp_id;
  EXCEPTION
    WHEN OTHERS THEN
      RETURN NULL;
  END;

  PROCEDURE check_cf_after_ins_temp(p_session_id NUMBER, p_user_id NUMBER) IS
    v_62_count  NUMBER;
    v_52_count  NUMBER;
    v_cf_count  NUMBER;
    v_con_count NUMBER;
  BEGIN
    --应付保费个数
    SELECT COUNT(1)
      INTO v_62_count
      FROM csh_payment_cashflow_id_temp t1, con_contract_cashflow ccc
     WHERE t1.cashflow_id = ccc.cashflow_id
       AND ccc.cf_item = 62
       AND t1.session_id = p_session_id;
  
    --现金流个数
    SELECT COUNT(1)
      INTO v_cf_count
      FROM csh_payment_cashflow_id_temp t1, con_contract_cashflow ccc
     WHERE t1.cashflow_id = ccc.cashflow_id
       AND t1.session_id = p_session_id;
  
    --校验
    --cancel by shen
    --IF v_62_count > 0 AND v_cf_count <> v_62_count THEN
     -- sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '应付保费只能单独发起付款申请!',
       --                                              p_created_by              => p_user_id,
     --                                                p_package_name            => 'csh_payment_req_pkg',
     --                                                p_procedure_function_name => 'check_cf_after_ins_temp');
     -- raise_application_error(sys_raise_app_error_pkg.c_error_number,
       --                       sys_raise_app_error_pkg.g_err_line_id);
    --END IF;
  
    IF v_62_count > 0 THEN
      NULL;
    ELSE
      --合同个数
    /*  SELECT COUNT(1)
        INTO v_cf_count
        FROM (SELECT ccc.contract_id
                FROM csh_payment_cashflow_id_temp t1,
                     con_contract_cashflow        ccc
               WHERE t1.cashflow_id = ccc.cashflow_id
                 AND t1.session_id = p_session_id
               GROUP BY ccc.contract_id) t;
    
      IF v_cf_count > 1 THEN
        sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '请选择合同号相同的单据!',
                                                       p_created_by              => p_user_id,
                                                       p_package_name            => 'csh_payment_req_pkg',
                                                       p_procedure_function_name => 'check_cf_after_ins_temp');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      END IF;*/
    
      SELECT COUNT(1)
        INTO v_52_count
        FROM csh_payment_cashflow_id_temp t1, con_contract_cashflow ccc
       WHERE t1.cashflow_id = ccc.cashflow_id
         AND ccc.cf_item IN ('52', '509')
         AND t1.session_id = p_session_id;
      IF v_52_count > 0 AND v_cf_count <> v_52_count THEN
        sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '保证金退还只能单独发起付款申请!',
                                                       p_created_by              => p_user_id,
                                                       p_package_name            => 'csh_payment_req_pkg',
                                                       p_procedure_function_name => 'check_cf_after_ins_temp');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      END IF;
    END IF;
  END;

  PROCEDURE check_cf_0_total_amt(p_contract_id NUMBER, p_user_id NUMBER) IS
    v_cf_0_total_amt NUMBER;
    e_cf_0_total_amt_err EXCEPTION;
    v_contract_rec con_contract%ROWTYPE;
    v_count        NUMBER;
    v_count1       NUMBER;
    v_count2       NUMBER;
    v_count3       NUMBER;
    v_count4       NUMBER;
    v_count5       NUMBER;
    v_count6       NUMBER;
  BEGIN
    SELECT *
      INTO v_contract_rec
      FROM con_contract t
     WHERE t.contract_id = p_contract_id
       FOR UPDATE NOWAIT;
  
    SELECT COUNT(1)
      INTO v_count5
      FROM csh_payment_cashflow_id_temp t1
     WHERE t1.cashflow_id IN
           (SELECT ccc.cashflow_id
              FROM con_contract_cashflow ccc
             WHERE ccc.contract_id = p_contract_id
               AND ccc.cf_item IN ('52', '509'));
    SELECT COUNT(1)
      INTO v_count6
      FROM csh_payment_cashflow_id_temp t1
     WHERE t1.cashflow_id IN
           (SELECT ccc.cashflow_id
              FROM con_contract_cashflow ccc
             WHERE ccc.contract_id = p_contract_id);
  
    IF v_count5 > 0 AND v_count5 <> v_count6 THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '保证金退还只能单独发起付款申请!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_cf_0_total_amt');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    END IF;
    /* --判断首付款是否支付
    SELECT COUNT(1)
      INTO v_count
      FROM con_contract_cashflow tt
     WHERE tt.contract_id = p_contract_id
       AND tt.cf_item = 2
       AND tt.cf_direction = 'INFLOW'
       AND tt.cf_status = 'RELEASE'
       AND tt.write_off_flag = 'FULL';
    --除首付款延期的合同外,如果首付款现金流(2)没有支付,则不能发起付款申请
    IF nvl(v_contract_rec.hd_user_col_v09,
           0) NOT IN ('20',
                      '30')
       AND v_count = 0 THEN
    
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同首付款没有支付,不能发起付款申请!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_cf_0_total_amt');
      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.invoice_uploaded_flag = 'N' THEN
      --查询现金流表未付款的破碎器成本、机械成本
      --获取临时表中符合现金流条件的个数
      SELECT COUNT(1)
        INTO v_count1
        FROM csh_payment_cashflow_id_temp t1
       WHERE t1.cashflow_id IN (SELECT ccc.cashflow_id
                                  FROM con_contract_cashflow ccc
                                 WHERE ccc.contract_id = p_contract_id
                                   AND ccc.cf_item IN ('5',
                                                       '998')
                                   AND ccc.write_off_flag = 'NOT');
      --获取现金流表未付款的破碎器成本、机械成本的个数
      SELECT COUNT(1)
        INTO v_count2
        FROM con_contract_cashflow ccc
       WHERE ccc.contract_id = p_contract_id
         AND ccc.cf_item IN ('5',
                             '998')
         AND ccc.write_off_flag = 'NOT';
      --如果选择的现金流不包含破碎器成本或者机械成本,则报错
      IF v_count1 < v_count2 THEN
        sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同未未开票,请勾选正确的成本进行发起!',
                                                       p_created_by              => p_user_id,
                                                       p_package_name            => 'csh_payment_req_pkg',
                                                       p_procedure_function_name => 'check_cf_0_total_amt');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      END IF;
    ELSE
      --已开票时
      --查询现金流表未付款的破碎器成本&利润、机械成本&利润
      --获取临时表中符合现金流条件的个数
      SELECT COUNT(1)
        INTO v_count3
        FROM csh_payment_cashflow_id_temp t1
       WHERE t1.cashflow_id IN (SELECT ccc.cashflow_id
                                  FROM con_contract_cashflow ccc
                                 WHERE ccc.contract_id = p_contract_id
                                   AND ccc.cf_item IN ('5',
                                                       '997',
                                                       '995',
                                                       '998')
                                   AND ccc.write_off_flag = 'NOT');
      --获取现金流表未付款的破碎器成本、机械成本的个数
      SELECT COUNT(1)
        INTO v_count4
        FROM con_contract_cashflow ccc
       WHERE ccc.contract_id = p_contract_id
         AND ccc.cf_item IN ('5',
                             '997',
                             '995',
                             '998')
         AND ccc.write_off_flag = 'NOT';
      --如果选择的现金流不包含破碎器成本或者机械成本,则报错
      IF v_count3 < v_count4 THEN
        sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同未未开票,请勾选正确的成本和利润进行发起!',
                                                       p_created_by              => p_user_id,
                                                       p_package_name            => 'csh_payment_req_pkg',
                                                       p_procedure_function_name => 'check_cf_0_total_amt');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      END IF;
    END IF;*/
  
  EXCEPTION
    WHEN e_cf_0_total_amt_err THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CON_CONTRACT_VERIFY_PKG.CONTRACT_UPDATE_CF_0_TOTAL_AMT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'CON_CONTRACT_VERIFY_PKG',
                                                      p_procedure_function_name => 'CONTRACT_UPDATE_CHECK',
                                                      p_token_1                 => '#contract_number',
                                                      p_token_value_1           => v_contract_rec.contract_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_last_writeoff_date(p_cashflow_id NUMBER) RETURN DATE IS
    v_last_write_off_date DATE;
  BEGIN
    SELECT MAX(w.write_off_date)
      INTO v_last_write_off_date
      FROM csh_write_off w
     WHERE w.cashflow_id = p_cashflow_id;
    RETURN v_last_write_off_date;
  END;

  FUNCTION get_bp_category_desc(p_contract_id NUMBER, p_bp_id NUMBER)
    RETURN VARCHAR2 IS
    v_attribute VARCHAR2(32767);
  BEGIN
    FOR r_bp_category_rec IN (SELECT c.description
                                FROM con_contract_bp t, hls_bp_category c
                               WHERE t.bp_category = c.bp_category
                                 AND t.contract_id = p_contract_id
                                 AND t.bp_id = p_bp_id) LOOP
      IF instr(v_attribute, r_bp_category_rec.description) = 0 OR
         instr(v_attribute, r_bp_category_rec.description) IS NULL THEN
        v_attribute := v_attribute || ',' || r_bp_category_rec.description;
      END IF;
    END LOOP;
    IF v_attribute IS NOT NULL THEN
      v_attribute := substr(v_attribute, 2);
    END IF;
    RETURN v_attribute;
  EXCEPTION
    WHEN OTHERS THEN
      RETURN NULL;
  END;

  --索合同行表
  PROCEDURE lock_con_contract_cashflow(p_cashflow_id      con_contract_cashflow.cashflow_id%TYPE,
                                       p_user_id          NUMBER,
                                       p_con_cashflow_rec OUT con_contract_cashflow%ROWTYPE) IS
  BEGIN
    SELECT *
      INTO p_con_cashflow_rec
      FROM con_contract_cashflow t
     WHERE t.cashflow_id = p_cashflow_id
       FOR UPDATE NOWAIT;
  EXCEPTION
    WHEN e_lock_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_WRITE_OFF_PKG.CON_LOCK_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'lock_con_contract_cashflow');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --锁csh_transaction
  PROCEDURE lock_csh_payment_req_hd(p_payment_req_id         IN NUMBER,
                                    p_user_id                IN NUMBER,
                                    p_csh_payment_req_hd_rec OUT csh_payment_req_hd%ROWTYPE) IS
  BEGIN
    SELECT *
      INTO p_csh_payment_req_hd_rec
      FROM csh_payment_req_hd t
     WHERE t.payment_req_id = p_payment_req_id
       FOR UPDATE NOWAIT;
  EXCEPTION
    WHEN e_lock_trx_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_LOCK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'lock_csh_payment_req_hd');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  ---zlf
  ---锁合同
  PROCEDURE lock_con_contract_rec(p_contract_id      IN NUMBER,
                                  p_user_id          IN NUMBER,
                                  p_con_contract_rec OUT con_contract%ROWTYPE) IS
  BEGIN
    SELECT *
      INTO p_con_contract_rec
      FROM con_contract t
     WHERE t.contract_id = p_contract_id
       FOR UPDATE NOWAIT;
  EXCEPTION
    WHEN e_lock_trx_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_LOCK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'lock_con_contract_rec');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE lock_csh_payment_req_ln(p_payment_req_ln_id      IN NUMBER,
                                    p_user_id                IN NUMBER,
                                    p_csh_payment_req_ln_rec OUT csh_payment_req_ln%ROWTYPE) IS
  BEGIN
    SELECT *
      INTO p_csh_payment_req_ln_rec
      FROM csh_payment_req_ln t
     WHERE t.payment_req_ln_id = p_payment_req_ln_id
       FOR UPDATE NOWAIT;
  EXCEPTION
    WHEN e_lock_trx_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_LOCK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'lock_csh_payment_req_ln');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  FUNCTION is_payment_close(v_csh_payment_req_hd_rec IN csh_payment_req_hd%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF v_csh_payment_req_hd_rec.closed_flag = 'Y' THEN
      RETURN FALSE;
    ELSE
      RETURN TRUE;
    END IF;
  END;

  FUNCTION is_payment_submitted(v_csh_payment_req_hd_rec IN csh_payment_req_hd%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF v_csh_payment_req_hd_rec.submitted_flag = 'Y' THEN
      RETURN FALSE;
    ELSE
      RETURN TRUE;
    END IF;
  END;

  FUNCTION is_payment_pay(v_csh_payment_req_hd_rec IN csh_payment_req_hd%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF v_csh_payment_req_hd_rec.approval_status <> 'APPROVED' THEN
      RETURN FALSE;
    ELSE
      RETURN TRUE;
    END IF;
  END;

  PROCEDURE check_csh_payment_bp_unique(p_payment_req_id NUMBER,
                                        p_user_id        NUMBER) IS
    v_bp_id NUMBER;
  BEGIN
    SELECT DISTINCT l.bp_id
      INTO v_bp_id
      FROM csh_payment_req_ln l
     WHERE l.payment_req_id = p_payment_req_id;
  EXCEPTION
    WHEN too_many_rows THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '付款对象必须唯一',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_csh_payment_bp_unique');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE check_csh_payment_bp(p_payment_req_id NUMBER, p_user_id NUMBER) IS
    v_bp_name VARCHAR2(200);
    e_ln_bp_error EXCEPTION;
  BEGIN
    SELECT hbm.bp_name
      INTO v_bp_name
      FROM csh_payment_req_ln l, hls_bp_master hbm
     WHERE l.payment_req_id = p_payment_req_id
       AND l.bp_id = hbm.bp_id
       AND l.ref_doc_category = 'CONTRACT'
       AND NOT EXISTS (SELECT 1
              FROM con_contract_bp ccb
             WHERE ccb.contract_id = l.ref_doc_id
               AND ccb.bp_id = l.bp_id
               AND ccb.enabled_flag = 'Y')
       AND rownum = 1;
    RAISE e_ln_bp_error;
  EXCEPTION
    WHEN no_data_found THEN
      NULL;
    WHEN e_ln_bp_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该付款对象在对应的合同中不存在--' ||
                                                                                  v_bp_name,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_csh_payment_bp');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --保证金退还 付款申请 校验 add by Harry 9952 2017/2/24
  PROCEDURE check_csh_payment_bond(p_payment_req_id NUMBER,
                                   p_user_id        NUMBER) IS
    v_bp_name VARCHAR2(200);
    e_ln_bp_error EXCEPTION;
    v_write_off_flag VARCHAR2(30);
    v_count          NUMBER;
    v_cashflow_id    NUMBER;
    e_flag_or_status_error EXCEPTION;
  BEGIN
    SELECT l.ref_doc_line_id
      INTO v_cashflow_id
      FROM csh_payment_req_ln l
     WHERE l.payment_req_id = p_payment_req_id;
    SELECT c.write_off_flag
      INTO v_write_off_flag
      FROM con_contract_cashflow c
     WHERE c.cashflow_id = v_cashflow_id;
    SELECT COUNT(*)
      INTO v_count
      FROM csh_payment_req_hd h
     WHERE h.payment_req_id IN
           (SELECT l.payment_req_id
              FROM csh_payment_req_ln l
             WHERE l.ref_doc_line_id = v_cashflow_id)
       AND h.approval_status = 'APPROVING';
    IF v_write_off_flag = 'FULL' OR v_count <> 0 THEN
      RAISE e_flag_or_status_error;
    END IF;
    SELECT hbm.bp_name
      INTO v_bp_name
      FROM csh_payment_req_ln l, hls_bp_master hbm
     WHERE l.payment_req_id = p_payment_req_id
       AND l.bp_id = hbm.bp_id
       AND l.ref_doc_category = 'CONTRACT'
       AND NOT EXISTS (SELECT 1
              FROM con_contract_bp ccb
             WHERE ccb.contract_id = l.ref_doc_id
               AND ccb.bp_id = l.bp_id
               AND ccb.enabled_flag = 'Y')
       AND rownum = 1;
    RAISE e_ln_bp_error;
  EXCEPTION
    WHEN no_data_found THEN
      NULL;
    WHEN e_ln_bp_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该付款对象在对应的合同中不存在--' ||
                                                                                  v_bp_name,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_csh_payment_bond');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_flag_or_status_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该现金流已核销完成或已提交申请!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_csh_payment_bond');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --关闭申请头
  PROCEDURE close_csh_payment_req_hd(p_payment_req_id NUMBER,
                                     p_closed_date    DATE,
                                     p_closed_note    VARCHAR2,
                                     p_user_id        NUMBER) IS
    v_csh_payment_req_hd_rec csh_payment_req_hd%ROWTYPE;
    e_status_error EXCEPTION;
  BEGIN
    lock_csh_payment_req_hd(p_payment_req_id         => p_payment_req_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_hd_rec => v_csh_payment_req_hd_rec);
    IF NOT is_payment_close(v_csh_payment_req_hd_rec) THEN
      RAISE e_status_error;
    END IF;
    UPDATE csh_payment_req_hd
       SET closed_flag      = 'Y',
           closed_note      = p_closed_note,
           closed_date      = SYSDATE,
           last_update_date = SYSDATE,
           last_updated_by  = p_user_id
     WHERE payment_req_id = p_payment_req_id;
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => '',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'close_csh_payment_req_hd');
      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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'close_csh_payment_req_hd');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  FUNCTION get_payment_req_number(p_document_type     VARCHAR2,
                                  p_transaction_date  DATE,
                                  p_company_id        NUMBER,
                                  p_user_id           NUMBER,
                                  p_document_category VARCHAR2)
    RETURN VARCHAR2 IS
  BEGIN
    RETURN fnd_code_rule_pkg.get_rule_next_auto_num(p_document_category => p_document_category,
                                                    p_document_type     => p_document_type,
                                                    p_company_id        => p_company_id,
                                                    p_operation_unit_id => NULL,
                                                    p_operation_date    => p_transaction_date,
                                                    p_created_by        => p_user_id);
  END;

  PROCEDURE csh_payment_req_amount_check(p_cashflow_id NUMBER,
                                         p_amount      NUMBER,
                                         p_company_id  NUMBER,
                                         p_user_id     NUMBER) IS
    v_residual_amount NUMBER;
    e_req_amount_check      EXCEPTION;
    e_req_amount_zero_check EXCEPTION;
    v_amount_flag      VARCHAR2(1);
    v_req_amount       NUMBER;
    v_due_amount       NUMBER;
    v_amount           NUMBER;
    v_amount_paid      NUMBER;
    v_amount_approving NUMBER;
  BEGIN
    SELECT nvl(f.due_amount, 0)
      INTO v_due_amount
      FROM con_contract_cashflow f
     WHERE f.cashflow_id = p_cashflow_id;
    SELECT SUM(l.amount)
      INTO v_amount
      FROM csh_payment_req_ln l, csh_payment_req_hd h
     WHERE l.ref_doc_line_id = p_cashflow_id
       AND l.payment_req_id = h.payment_req_id
       AND h.submitted_flag = 'Y'
       AND h.approval_status = 'APPROVED'
       AND nvl(h.closed_flag, 'N') <> 'Y';
    SELECT SUM(nvl(l.amount_paid, 0))
      INTO v_amount_paid
      FROM csh_payment_req_ln l, csh_payment_req_hd h
     WHERE l.ref_doc_line_id = p_cashflow_id
       AND l.payment_req_id = h.payment_req_id
       AND h.submitted_flag = 'Y'
       AND h.approval_status = 'APPROVED'
       AND nvl(h.closed_flag, 'N') = 'Y';
    SELECT SUM(l.amount)
      INTO v_amount_approving
      FROM csh_payment_req_ln l, csh_payment_req_hd h
     WHERE l.ref_doc_line_id = p_cashflow_id
       AND l.payment_req_id = h.payment_req_id
       AND h.submitted_flag = 'Y'
       AND h.approval_status = 'APPROVING';
    IF p_amount > (v_due_amount - nvl(v_amount, 0) - nvl(v_amount_paid, 0) -
       nvl(v_amount_approving, 0)) THEN
      RAISE e_req_amount_check;
    ELSIF p_amount = 0 OR p_amount IS NULL THEN
      RAISE e_req_amount_zero_check;
    END IF;
  EXCEPTION
    WHEN e_req_amount_check THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.CSH501_REQ_AMOUNT_CHECK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'csh_payment_req_amount_check');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_req_amount_zero_check THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.CSH501_REQ_AMOUNT_ZERO_CHECK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'csh_payment_req_amount_check');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE upd_csh_payment_after_reverse(p_transaction_id csh_transaction.transaction_id%TYPE,
                                          p_user_id        NUMBER) IS
    v_csh_transaction_rec    csh_transaction%ROWTYPE;
    v_csh_payment_req_ln_rec csh_payment_req_ln%ROWTYPE;
    v_amount_paid            NUMBER;
    e_bp_category_err exception;
  BEGIN
    csh_transaction_pkg.lock_csh_transaction(p_transaction_id      => p_transaction_id,
                                             p_user_id             => p_user_id,
                                             p_csh_transaction_rec => v_csh_transaction_rec);
    lock_csh_payment_req_ln(p_payment_req_ln_id      => v_csh_transaction_rec.source_doc_line_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_ln_rec => v_csh_payment_req_ln_rec);
    --付款总金额字段减少
    v_amount_paid := nvl(v_csh_payment_req_ln_rec.amount_paid, 0) -
                     nvl(v_csh_transaction_rec.transaction_amount, 0);
   
    IF v_amount_paid - v_csh_payment_req_ln_rec.amount < 0 THEN
      UPDATE csh_payment_req_ln l
         SET l.amount_paid            = v_amount_paid,
             --UPDATE BY SHEN 
             --l.payment_status         = 'PORTION',
             l.payment_status         = 'PARTIAL',
             l.payment_completed_date = SYSDATE,
             l.last_update_date       = SYSDATE,
             l.last_updated_by        = p_user_id
       WHERE l.payment_req_ln_id = v_csh_transaction_rec.source_doc_line_id;
    ELSIF v_amount_paid = 0 THEN
      UPDATE csh_payment_req_ln l
         SET l.amount_paid            = v_amount_paid,
             l.payment_status         = 'NOT',
             l.payment_completed_date = SYSDATE,
             l.last_update_date       = SYSDATE,
             l.last_updated_by        = p_user_id
       WHERE l.payment_req_ln_id = v_csh_transaction_rec.source_doc_line_id;
    END IF;
    --根据bp_category 更新付款行的代理商已付金额和厂商已付金额
    --add by shen
    if v_csh_transaction_rec.bp_category='AGENT' THEN
        UPDATE csh_payment_req_ln cprl
          set cprl.amount_agent_paid=nvl(cprl.amount_agent_paid,0)-v_csh_transaction_rec.transaction_amount
          where cprl.payment_req_ln_id= v_csh_transaction_rec.source_doc_line_id;
    elsif  v_csh_transaction_rec.bp_category='VENDER' THEN
        UPDATE csh_payment_req_ln cprl
          set cprl.amount_vender_paid=nvl(cprl.amount_vender_paid,0)-v_csh_transaction_rec.transaction_amount
          where cprl.payment_req_ln_id= v_csh_transaction_rec.source_doc_line_id;
    --不是厂商和代理店则报错
    Else  raise e_bp_category_err;
      
    end if;
      
    UPDATE csh_payment_req_hd h
       SET h.closed_flag = 'N'
     WHERE h.payment_req_id = v_csh_transaction_rec.source_doc_id;
   --异常处理
   Exception
     WHEN e_bp_category_err THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '付款反冲的对象必须为代理店或者厂商!',
                                                                                 
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'upd_csh_payment_after_reverse');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE insert_csh_trx_source(p_transaction_id      NUMBER,
                                  p_source_doc_category VARCHAR2,
                                  p_source_doc_type     VARCHAR2,
                                  p_source_doc_id       NUMBER,
                                  p_source_doc_line_id  NUMBER,
                                  p_source_amt          NUMBER,
                                  p_user_id             NUMBER) IS
  BEGIN
    INSERT INTO csh_transaction_source
      (transaction_source_id,
       transaction_id,
       source_doc_category,
       source_doc_type,
       source_doc_id,
       source_doc_line_id,
       source_amt,
       creation_date,
       created_by,
       last_update_date,
       last_updated_by)
    VALUES
      (csh_transaction_source_s.nextval,
       p_transaction_id,
       p_source_doc_category,
       p_source_doc_type,
       p_source_doc_id,
       p_source_doc_line_id,
       p_source_amt,
       SYSDATE,
       p_user_id,
       SYSDATE,
       p_user_id);
  END;

  PROCEDURE payment_csh_payment_debt(p_csh_payment_req_hd_rec  csh_payment_req_hd%ROWTYPE,
                                     p_csh_payment_req_ln_rec  csh_payment_req_ln%ROWTYPE,
                                     p_con_cashflow_rec        con_contract_cashflow%ROWTYPE,
                                     p_pay_date                DATE,
                                     p_pay_amount              NUMBER,
                                     p_company_id              NUMBER,
                                     p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                     p_period_name             csh_transaction.period_name%TYPE,
                                     p_exchange_rate_type      csh_transaction.exchange_rate_type%TYPE,
                                     p_exchange_rate_quotation csh_transaction.exchange_rate_quotation%TYPE,
                                     p_exchange_rate           csh_transaction.exchange_rate%TYPE,
                                     p_bank_account_id         csh_transaction.bank_account_id%TYPE,
                                     p_bank_account_num        VARCHAR2,
                                     p_description             csh_transaction.description%TYPE,
                                     p_user_id                 NUMBER,
                                     p_cashflow_id             NUMBER,
                                     p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                     p_bp_bank_account_id      csh_transaction.bp_bank_account_id%TYPE,
                                     p_bp_bank_account_num     csh_transaction.bp_bank_account_num%TYPE,
                                     p_handling_charge         csh_transaction.handling_charge%TYPE,
                                     p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                     p_merge_flag              VARCHAR2,
                                     p_payment_behalf_flag     VARCHAR2, --todo Spencer 3893 add 20160722 for 是否代付核销
                                     p_transaction_id          IN OUT NUMBER,
                                     p_bp_category             varchar2 default null,
                                     p_due_amount              NUMBER DEFAULT NULL) IS
    e_trx_num_error    EXCEPTION;
    e_req_amount_check EXCEPTION;
    v_csh_transaction_rec  csh_transaction%ROWTYPE;
    v_transaction_id       NUMBER;
    v_write_off_id         NUMBER;
    v_count_status         NUMBER;
    v_bp_category          VARCHAR2(100);
    v_amount               NUMBER;
    v_amount_paid          NUMBER;
    v_deduction_flag       VARCHAR2(1) := 'N';
    v_deduction_amount     NUMBER := 0;
    v_session_id           NUMBER;
    v_bp_bank_account_name csh_transaction.bp_bank_account_name%TYPE;
    v_ddct_transaction_id  NUMBER;
  BEGIN
    BEGIN
      SELECT ba.bank_account_name
        INTO v_bp_bank_account_name
        FROM hls_bp_master_bank_account ba
       WHERE ba.bank_account_id = p_bp_bank_account_id;
    EXCEPTION
      WHEN OTHERS THEN
        NULL;
    END;
    /* 不修改参数,使用不重复的id,代替session_id */
    v_session_id := -1 * p_csh_payment_req_ln_rec.payment_req_ln_id;
    BEGIN
      SELECT 'Y'
        INTO v_deduction_flag
        FROM dual
       WHERE EXISTS (SELECT 1
                FROM csh_payment_req_ln_ddct a
               WHERE a.payment_req_ln_id =
                     p_csh_payment_req_ln_rec.payment_req_ln_id
                 AND nvl(a.deduction_flag, 'N') = 'N');
    EXCEPTION
      WHEN no_data_found THEN
        v_deduction_flag := 'N';
    END;
    FOR c_deduction_rec IN (SELECT *
                              FROM csh_payment_req_ln_ddct a
                             WHERE a.payment_req_ln_id =
                                   p_csh_payment_req_ln_rec.payment_req_ln_id
                               AND nvl(a.deduction_flag, 'N') = 'N') LOOP
      --insert into csh_write_off_temp
      IF c_deduction_rec.amount > 0 THEN
        INSERT INTO csh_write_off_temp
          (session_id,
           write_off_type,
           transaction_category,
           transaction_type,
           write_off_date,
           write_off_due_amount,
           write_off_principal,
           write_off_interest,
           write_off_due_amount_cny,
           write_off_principal_cny,
           write_off_interest_cny,
           exchange_rate,
           company_id,
           document_category,
           document_id,
           document_line_id,
           opposite_doc_category,
           opposite_doc_type,
           opposite_doc_id,
           opposite_doc_line_id,
           opposite_doc_detail_id,
           opposite_write_off_amount,
           description,
           creation_date,
           created_by,
           last_update_date,
           last_updated_by)
        VALUES
          (v_session_id,
           csh_write_off_pkg.write_off_type_receipt_credit,
           'CSH_TRX',
           csh_transaction_pkg.csh_trx_type_deduction,
           p_pay_date,
           c_deduction_rec.amount,
           c_deduction_rec.principal,
           c_deduction_rec.interest,
           NULL,
           NULL,
           NULL,
           p_exchange_rate,
           p_csh_payment_req_hd_rec.company_id,
           'CONTRACT',
           c_deduction_rec.ref_doc_id,
           c_deduction_rec.ref_doc_line_id,
           'PAYMENT_REQ_DDCT', --opposite_doc_category ,
           'PAYMENT_REQ_DDCT', --opposite_doc_type ,
           p_csh_payment_req_ln_rec.payment_req_id, --opposite_doc_id ,
           p_csh_payment_req_ln_rec.payment_req_ln_id, --opposite_doc_line_id ,
           c_deduction_rec.payment_req_ln_ddct_id, --opposite_doc_detail_id ,
           NULL, --opposite_write_off_amount ,
           NULL,
           SYSDATE,
           p_user_id,
           SYSDATE,
           p_user_id);
      END IF;
      v_deduction_amount := v_deduction_amount +
                            nvl(c_deduction_rec.amount, 0);
      UPDATE csh_payment_req_ln_ddct
         SET deduction_flag = 'Y'
       WHERE payment_req_ln_ddct_id =
             c_deduction_rec.payment_req_ln_ddct_id;
    END LOOP;
    IF v_deduction_flag = 'Y' AND v_deduction_amount > 0 THEN
      INSERT INTO csh_write_off_temp
        (session_id,
         write_off_type,
         transaction_category,
         transaction_type,
         write_off_date,
         write_off_due_amount,
         write_off_principal,
         write_off_interest,
         write_off_due_amount_cny,
         write_off_principal_cny,
         write_off_interest_cny,
         exchange_rate,
         company_id,
         document_category,
         document_id,
         document_line_id,
         description,
         creation_date,
         created_by,
         last_update_date,
         last_updated_by)
      VALUES
        (v_session_id,
         csh_write_off_pkg.write_off_type_payment_debt,
         'CSH_TRX',
         csh_transaction_pkg.csh_trx_type_deduction,
         p_pay_date,
         v_deduction_amount,
         NULL,
         NULL,
         NULL,
         NULL,
         NULL,
         p_exchange_rate,
         p_csh_payment_req_hd_rec.company_id,
         'CONTRACT',
         p_csh_payment_req_ln_rec.ref_doc_id,
         p_csh_payment_req_ln_rec.ref_doc_line_id,
         NULL,
         SYSDATE,
         p_user_id,
         SYSDATE,
         p_user_id);
      csh_write_off_pkg.deduction_write_off(p_session_id          => v_session_id,
                                            p_company_id          => p_csh_payment_req_hd_rec.company_id,
                                            p_bp_id               => p_csh_payment_req_ln_rec.bp_id,
                                            p_transaction_date    => p_pay_date,
                                            p_description         => '',
                                            p_source_csh_trx_type => '',
                                            p_source_csh_trx_id   => '',
                                            p_source_doc_category => p_csh_payment_req_hd_rec.document_category,
                                            p_source_doc_type     => p_csh_payment_req_hd_rec.document_type,
                                            p_source_doc_id       => p_csh_payment_req_hd_rec.payment_req_id,
                                            p_source_doc_line_id  => p_csh_payment_req_ln_rec.payment_req_ln_id,
                                            p_user_id             => p_user_id,
                                            p_transaction_id      => v_ddct_transaction_id);
      UPDATE csh_payment_req_ln t
         SET amount_paid = nvl(amount_paid, 0) + v_deduction_amount
       WHERE t.payment_req_ln_id =
             p_csh_payment_req_ln_rec.payment_req_ln_id;
    END IF;
    --预付款抵扣
    FOR c_prepay IN (SELECT p.prepayment_trx_id,
                            p.write_off_amt,
                            p.payment_req_ln_prepay_id
                       FROM csh_payment_req_ln_prepay p
                      WHERE p.payment_req_ln_id =
                            p_csh_payment_req_ln_rec.payment_req_ln_id
                        AND nvl(p.write_off_flag, 'N') = 'N') LOOP
      csh_write_off_pkg.insert_csh_write_off(p_write_off_id                => v_write_off_id,
                                             p_write_off_type              => csh_write_off_pkg.write_off_type_pre_debt,
                                             p_write_off_date              => p_pay_date,
                                             p_internal_period_num         => p_internal_period_num,
                                             p_period_name                 => p_period_name,
                                             p_csh_transaction_id          => c_prepay.prepayment_trx_id,
                                             p_csh_write_off_amount        => c_prepay.write_off_amt,
                                             p_subsequent_csh_trx_id       => '',
                                             p_subseq_csh_write_off_amount => '',
                                             p_reversed_flag               => 'N',
                                             p_reversed_write_off_id       => '',
                                             p_reversed_date               => '',
                                             p_cashflow_id                 => p_con_cashflow_rec.cashflow_id,
                                             p_contract_id                 => p_con_cashflow_rec.contract_id,
                                             p_times                       => p_con_cashflow_rec.times,
                                             p_cf_item                     => p_con_cashflow_rec.cf_item,
                                             p_cf_type                     => p_con_cashflow_rec.cf_type,
                                             p_penalty_calc_date           => '',
                                             p_write_off_due_amount        => c_prepay.write_off_amt,
                                             p_write_off_principal         => '',
                                             p_write_off_interest          => '',
                                             p_opposite_doc_category       => '',
                                             p_opposite_doc_type           => '',
                                             p_opposite_doc_id             => '',
                                             p_opposite_doc_line_id        => '',
                                             p_opposite_doc_detail_id      => '',
                                             p_opposite_write_off_amount   => '',
                                             p_create_je_mothed            => '',
                                             p_create_je_flag              => 'N',
                                             p_gld_interface_flag          => 'N',
                                             p_description                 => p_description,
                                             p_user_id                     => p_user_id);
      csh_write_off_pkg.execute_write_off(p_write_off_id => v_write_off_id,
                                          p_user_id      => p_user_id);
      UPDATE csh_payment_req_ln_prepay p
         SET p.write_off_flag = 'Y'
       WHERE p.payment_req_ln_prepay_id = c_prepay.payment_req_ln_prepay_id;
      UPDATE csh_payment_req_ln t
         SET amount_paid = nvl(amount_paid, 0) + c_prepay.write_off_amt
       WHERE t.payment_req_ln_id =
             p_csh_payment_req_ln_rec.payment_req_ln_id;
    END LOOP;
    IF p_pay_amount > 0 THEN
      --付款核销  delete by shen bp_category 由前面付款的对象决定
      /*SELECT m.bp_category
        INTO v_bp_category
        FROM hls_bp_master m
       WHERE m.bp_id = p_csh_payment_req_ln_rec.bp_id;*/
       
       v_bp_category := p_bp_category;
      IF p_merge_flag = 'Y' THEN
        IF p_transaction_id IS NULL THEN
          --第一条
          csh_transaction_pkg.insert_csh_transaction(p_transaction_id          => v_transaction_id,
                                                     p_transaction_num         => '',
                                                     p_transaction_category    => csh_transaction_pkg.csh_trx_category_business,
                                                     p_transaction_type        => csh_transaction_pkg.csh_trx_type_payment,
                                                     p_transaction_date        => p_pay_date,
                                                     p_penalty_calc_date       => '',
                                                     p_bank_slip_num           => p_bank_slip_num,
                                                     p_company_id              => p_company_id,
                                                     p_internal_period_num     => p_internal_period_num,
                                                     p_period_name             => p_period_name,
                                                     p_payment_method_id       => p_payment_method_id,
                                                     p_distribution_set_id     => '',
                                                     p_cashflow_amount         => -1 *
                                                                                  p_pay_amount,
                                                     p_currency_code           => p_csh_payment_req_ln_rec.currency_code,
                                                     p_transaction_amount      => p_pay_amount,
                                                     p_exchange_rate_type      => p_exchange_rate_type,
                                                     p_exchange_rate_quotation => 'DIRECT QUOTATION',
                                                     p_exchange_rate           => p_exchange_rate,
                                                     p_bank_account_id         => p_bank_account_id,
                                                     p_bp_category             => v_bp_category,
                                                     p_bp_id                   => p_csh_payment_req_ln_rec.bp_id,
                                                     p_bp_bank_account_id      => nvl(p_bp_bank_account_id,
                                                                                      p_csh_payment_req_ln_rec.bp_bank_account_id),
                                                     p_bp_bank_account_num     => nvl(p_bp_bank_account_num,
                                                                                      p_csh_payment_req_ln_rec.bp_bank_account_num),
                                                     p_description             => p_description,
                                                     p_handling_charge         => p_handling_charge,
                                                     --不需要凭证,直接将过账标志置为Y
                                                     p_posted_flag             => 'Y',
                                                     p_reversed_flag           => 'N',
                                                     p_reversed_date           => '',
                                                     p_returned_flag           => 'NOT',
                                                     p_returned_amount         => '',
                                                     p_write_off_flag          => 'FULL',
                                                     p_write_off_amount        => '',
                                                     p_full_write_off_date     => SYSDATE,
                                                     p_twin_csh_trx_id         => '',
                                                     p_return_from_csh_trx_id  => '',
                                                     p_reversed_csh_trx_id     => '',
                                                     p_source_csh_trx_type     => '',
                                                     p_source_csh_trx_id       => '',
                                                     p_source_doc_category     => p_csh_payment_req_hd_rec.document_category,
                                                     p_source_doc_type         => p_csh_payment_req_hd_rec.document_type,
                                                     p_source_doc_id           => p_csh_payment_req_hd_rec.payment_req_id,
                                                     p_source_doc_line_id      => p_csh_payment_req_ln_rec.payment_req_ln_id,
                                                     p_create_je_mothed        => '',
                                                     p_create_je_flag          => 'N',
                                                     p_gld_interface_flag      => 'N',
                                                     p_user_id                 => p_user_id,
                                                     p_csh_bp_name             => '',
                                                     p_paid_byother_flag       => NULL,
                                                     p_ref_v05                 => '付款核销');
          p_transaction_id := v_transaction_id;
        ELSE
          v_transaction_id := p_transaction_id;
          UPDATE csh_transaction t
             SET t.cashflow_amount    = t.cashflow_amount +
                                        (-1) * p_pay_amount,
                 t.transaction_amount = t.transaction_amount + p_pay_amount
           WHERE t.transaction_id = v_transaction_id;
        END IF;
        csh_write_off_pkg.insert_csh_write_off(p_write_off_id                => v_write_off_id,
                                               p_write_off_type              => csh_write_off_pkg.write_off_type_payment_debt,
                                               p_write_off_date              => p_pay_date,
                                               p_internal_period_num         => p_internal_period_num,
                                               p_period_name                 => p_period_name,
                                               p_csh_transaction_id          => v_transaction_id,
                                               p_csh_write_off_amount        => p_pay_amount,
                                               p_subsequent_csh_trx_id       => '',
                                               p_subseq_csh_write_off_amount => '',
                                               p_reversed_flag               => 'N',
                                               p_reversed_write_off_id       => '',
                                               p_reversed_date               => '',
                                               p_cashflow_id                 => p_con_cashflow_rec.cashflow_id,
                                               p_contract_id                 => p_con_cashflow_rec.contract_id,
                                               p_times                       => p_con_cashflow_rec.times,
                                               p_cf_item                     => p_con_cashflow_rec.cf_item,
                                               p_cf_type                     => p_con_cashflow_rec.cf_type,
                                               p_penalty_calc_date           => '',
                                               p_write_off_due_amount        => p_pay_amount,
                                               p_write_off_principal         => '',
                                               p_write_off_interest          => '',
                                               p_opposite_doc_category       => '',
                                               p_opposite_doc_type           => '',
                                               p_opposite_doc_id             => '',
                                               p_opposite_doc_line_id        => '',
                                               p_opposite_doc_detail_id      => '',
                                               p_opposite_write_off_amount   => '',
                                               p_create_je_mothed            => '',
                                               p_create_je_flag              => 'N',
                                               p_gld_interface_flag          => 'N',
                                               p_description                 => p_description,
                                               p_user_id                     => p_user_id,
                                               p_due_amount                  => p_due_amount);
        csh_write_off_pkg.execute_write_off(p_write_off_id => v_write_off_id,
                                            p_user_id      => p_user_id);
      ELSE
        -- 不合并,逐行处理
        csh_transaction_pkg.insert_csh_transaction(p_transaction_id          => v_transaction_id,
                                                   p_transaction_num         => '',
                                                   p_transaction_category    => csh_transaction_pkg.csh_trx_category_business,
                                                   p_transaction_type        => csh_transaction_pkg.csh_trx_type_payment,
                                                   p_transaction_date        => p_pay_date,
                                                   p_penalty_calc_date       => '',
                                                   p_bank_slip_num           => p_bank_slip_num,
                                                   p_company_id              => p_company_id,
                                                   p_internal_period_num     => p_internal_period_num,
                                                   p_period_name             => p_period_name,
                                                   p_payment_method_id       => p_payment_method_id,
                                                   p_distribution_set_id     => '',
                                                   p_cashflow_amount         => -1 *
                                                                                p_pay_amount,
                                                   p_currency_code           => p_csh_payment_req_ln_rec.currency_code,
                                                   p_transaction_amount      => p_pay_amount,
                                                   p_exchange_rate_type      => p_exchange_rate_type,
                                                   p_exchange_rate_quotation => 'DIRECT QUOTATION',
                                                   p_exchange_rate           => p_exchange_rate,
                                                   p_bank_account_id         => p_bank_account_id,
                                                   p_bp_category             => v_bp_category,
                                                   p_bp_id                   => p_csh_payment_req_ln_rec.bp_id,
                                                   p_bp_bank_account_id      => nvl(p_bp_bank_account_id,
                                                                                    p_csh_payment_req_ln_rec.bp_bank_account_id),
                                                   p_bp_bank_account_num     => nvl(p_bp_bank_account_num,
                                                                                    p_csh_payment_req_ln_rec.bp_bank_account_num),
                                                   p_description             => p_description,
                                                   p_handling_charge         => p_handling_charge,
                                                   p_posted_flag             => 'Y',
                                                   p_reversed_flag           => 'N',
                                                   p_reversed_date           => '',
                                                   p_returned_flag           => 'NOT',
                                                   p_returned_amount         => '',
                                                   p_write_off_flag          => 'FULL',
                                                   p_write_off_amount        => '',
                                                   p_full_write_off_date     => SYSDATE,
                                                   p_twin_csh_trx_id         => '',
                                                   p_return_from_csh_trx_id  => '',
                                                   p_reversed_csh_trx_id     => '',
                                                   p_source_csh_trx_type     => '',
                                                   p_source_csh_trx_id       => '',
                                                   p_source_doc_category     => p_csh_payment_req_hd_rec.document_category,
                                                   p_source_doc_type         => p_csh_payment_req_hd_rec.document_type,
                                                   p_source_doc_id           => p_csh_payment_req_hd_rec.payment_req_id,
                                                   p_source_doc_line_id      => p_csh_payment_req_ln_rec.payment_req_ln_id,
                                                   p_create_je_mothed        => '',
                                                   p_create_je_flag          => 'N',
                                                   p_gld_interface_flag      => 'N',
                                                   p_user_id                 => p_user_id,
                                                   p_csh_bp_name             => '',
                                                   p_paid_byother_flag       => NULL,
                                                   p_ref_v05                 => '付款核销');
        csh_write_off_pkg.insert_csh_write_off(p_write_off_id                => v_write_off_id,
                                               p_write_off_type              => csh_write_off_pkg.write_off_type_payment_debt,
                                               p_write_off_date              => p_pay_date,
                                               p_internal_period_num         => p_internal_period_num,
                                               p_period_name                 => p_period_name,
                                               p_csh_transaction_id          => v_transaction_id,
                                               p_csh_write_off_amount        => p_pay_amount,
                                               p_subsequent_csh_trx_id       => '',
                                               p_subseq_csh_write_off_amount => '',
                                               p_reversed_flag               => 'N',
                                               p_reversed_write_off_id       => '',
                                               p_reversed_date               => '',
                                               p_cashflow_id                 => p_con_cashflow_rec.cashflow_id,
                                               p_contract_id                 => p_con_cashflow_rec.contract_id,
                                               p_times                       => p_con_cashflow_rec.times,
                                               p_cf_item                     => p_con_cashflow_rec.cf_item,
                                               p_cf_type                     => p_con_cashflow_rec.cf_type,
                                               p_penalty_calc_date           => '',
                                               p_write_off_due_amount        => p_pay_amount,
                                               p_write_off_principal         => '',
                                               p_write_off_interest          => '',
                                               p_opposite_doc_category       => '',
                                               p_opposite_doc_type           => '',
                                               p_opposite_doc_id             => '',
                                               p_opposite_doc_line_id        => '',
                                               p_opposite_doc_detail_id      => '',
                                               p_opposite_write_off_amount   => '',
                                               p_create_je_mothed            => '',
                                               p_create_je_flag              => 'N',
                                               p_gld_interface_flag          => 'N',
                                               p_description                 => p_description,
                                               p_user_id                     => p_user_id,
                                               p_due_amount                  => p_due_amount);
        csh_write_off_pkg.execute_write_off(p_write_off_id => v_write_off_id,
                                            p_user_id      => p_user_id);
        csh_transaction_pkg.post_csh_transaction(p_transaction_id => v_transaction_id,
                                                 p_user_id        => p_user_id);
        /* --现金事务核销时,现金事务及核销行 合并生成的凭证
        csh_transaction_je_pkg.create_trx_write_off_je(p_transaction_id => v_transaction_id,
                                                       p_user_id        => p_user_id);*/
      END IF;
      p_transaction_id := v_transaction_id;
      insert_csh_trx_source(p_transaction_id      => p_transaction_id,
                            p_source_doc_category => p_csh_payment_req_hd_rec.document_category,
                            p_source_doc_type     => p_csh_payment_req_hd_rec.document_type,
                            p_source_doc_id       => p_csh_payment_req_hd_rec.payment_req_id,
                            p_source_doc_line_id  => p_csh_payment_req_ln_rec.payment_req_ln_id,
                            p_source_amt          => p_pay_amount,
                            p_user_id             => p_user_id);
    END IF;
  END;

  PROCEDURE payment_csh_payment(p_payment_req_id          NUMBER,
                                p_payment_req_ln_id       NUMBER,
                                p_pay_date                DATE,
                                p_pay_amount              NUMBER,
                                p_company_id              NUMBER,
                                p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                p_period_name             csh_transaction.period_name%TYPE,
                                p_exchange_rate_type      csh_transaction.exchange_rate_type%TYPE,
                                p_exchange_rate_quotation csh_transaction.exchange_rate_quotation%TYPE,
                                p_exchange_rate           csh_transaction.exchange_rate%TYPE,
                                p_bank_account_id         csh_transaction.bank_account_id%TYPE,
                                p_bank_account_num        VARCHAR2,
                                p_description             csh_transaction.description%TYPE,
                                p_user_id                 NUMBER,
                                p_cashflow_id             NUMBER,
                                p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                p_bp_bank_account_id      csh_transaction.bp_bank_account_id%TYPE,
                                p_bp_bank_account_num     csh_transaction.bp_bank_account_num%TYPE,
                                p_handling_charge         csh_transaction.handling_charge%TYPE,
                                p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                p_merge_flag              VARCHAR2 DEFAULT 'N',
                                p_payment_behalf_flag     VARCHAR2 DEFAULT 'N',
                                p_payment_status          OUT VARCHAR2,
                                p_transaction_id          IN OUT NUMBER,
                                p_bp_category             varchar2 default null,
                                p_due_amount              NUMBER DEFAULT NULL) IS
    v_amount                 NUMBER;
    v_amount_paid            NUMBER;
    v_amt_tmp                NUMBER;
    v_csh_payment_req_hd_rec csh_payment_req_hd%ROWTYPE;
    v_csh_payment_req_ln_rec csh_payment_req_ln%ROWTYPE;
    e_trx_num_error    EXCEPTION;
    e_req_amount_check EXCEPTION;
    v_count_status     NUMBER;
    v_con_cashflow_rec con_contract_cashflow%ROWTYPE;
    v_deduction_flag   VARCHAR2(1) := 'N';
    v_deduction_amount NUMBER := 0;
    v_session_id       NUMBER;
    e_status_error EXCEPTION;
  BEGIN
    lock_csh_payment_req_hd(p_payment_req_id         => p_payment_req_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_hd_rec => v_csh_payment_req_hd_rec);
    lock_csh_payment_req_ln(p_payment_req_ln_id      => p_payment_req_ln_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_ln_rec => v_csh_payment_req_ln_rec);
    IF v_csh_payment_req_ln_rec.payment_req_ln_type = 'DEBT' THEN
      lock_con_contract_cashflow(p_cashflow_id      => v_csh_payment_req_ln_rec.ref_doc_line_id,
                                 p_user_id          => p_user_id,
                                 p_con_cashflow_rec => v_con_cashflow_rec);
    END IF;
    --判断是否是审批通过的单据
    IF NOT is_payment_pay(v_csh_payment_req_hd_rec) THEN
      RAISE e_status_error;
    END IF;
    --本次申请金额字段,分开为厂商和代理店
    IF p_pay_amount < 0 THEN
      RAISE e_req_amount_check;
    END IF;
    SELECT nvl((SELECT SUM(d.amount)
                 FROM csh_payment_req_ln_ddct d, con_contract_cashflow cf
                WHERE d.payment_req_ln_id = p_payment_req_ln_id
                  AND nvl(d.deduction_flag, 'N') = 'N'
                  AND nvl(cf.payment_deduction_flag, 'N') = 'Y'
                  AND cf.write_off_flag != 'FULL'
                  AND d.ref_doc_category = 'CONTRACT'
                  AND d.ref_doc_id = cf.contract_id
                  AND d.ref_doc_line_id = cf.cashflow_id),
                  --预支付,付款这里没有用到
               0) + nvl((SELECT SUM(p.write_off_amt)
                          FROM csh_payment_req_ln_prepay p
                         WHERE p.payment_req_ln_id = p_payment_req_ln_id
                           AND nvl(p.write_off_flag, 'N') = 'N'),
                        0)
      INTO v_amt_tmp
      FROM dual;
      --已付款加上本次付款加上抵扣金额 和申请金额进行比较
    IF v_amt_tmp + p_pay_amount +
       nvl(v_csh_payment_req_ln_rec.amount_paid, 0) >
       v_csh_payment_req_ln_rec.amount THEN
      RAISE e_req_amount_check;
    END IF;
    IF v_csh_payment_req_ln_rec.payment_req_ln_type = 'DEBT' THEN
      payment_csh_payment_debt(p_csh_payment_req_hd_rec  => v_csh_payment_req_hd_rec,
                               p_csh_payment_req_ln_rec  => v_csh_payment_req_ln_rec,
                               p_con_cashflow_rec        => v_con_cashflow_rec,
                               p_pay_date                => p_pay_date,
                               p_pay_amount              => p_pay_amount,
                               p_company_id              => p_company_id,
                               p_internal_period_num     => p_internal_period_num,
                               p_period_name             => p_period_name,
                               p_exchange_rate_type      => p_exchange_rate_type,
                               p_exchange_rate_quotation => p_exchange_rate_quotation,
                               p_exchange_rate           => p_exchange_rate,
                               p_bank_account_id         => p_bank_account_id,
                               p_bank_account_num        => p_bank_account_num,
                               p_description             => p_description,
                               p_user_id                 => p_user_id,
                               p_cashflow_id             => p_cashflow_id,
                               p_bank_slip_num           => p_bank_slip_num,
                               p_bp_bank_account_id      => p_bp_bank_account_id,
                               p_bp_bank_account_num     => p_bp_bank_account_num,
                               p_handling_charge         => p_handling_charge,
                               p_payment_method_id       => p_payment_method_id,
                               p_merge_flag              => p_merge_flag,
                               p_payment_behalf_flag     => p_payment_behalf_flag,
                               p_transaction_id          => p_transaction_id,
                               -- by shen
                               p_bp_category             => p_bp_category,
                               p_due_amount              => p_due_amount);
    ELSE
      RETURN;
    END IF;
    SELECT nvl(amount_paid, 0), nvl(amount, 0)
      INTO v_amount_paid, v_amount
      FROM csh_payment_req_ln
     WHERE payment_req_ln_id = p_payment_req_ln_id;
    IF p_pay_amount + v_amount_paid = v_amount THEN
      UPDATE csh_payment_req_ln l
         SET l.payment_status         = 'FULL',
             l.amount_paid            = nvl(l.amount_paid, 0) +
                                        nvl(p_pay_amount, 0),
             l.payment_completed_date = SYSDATE,
             l.last_update_date       = SYSDATE,
             l.last_updated_by        = p_user_id
       WHERE l.payment_req_ln_id = p_payment_req_ln_id;
      p_payment_status := 'FULL';
    ELSIF p_pay_amount + v_amount_paid > v_amount THEN
      RAISE e_req_amount_check;
    ELSE
      UPDATE csh_payment_req_ln l
         SET l.payment_status         = 'PARTIAL',
             l.amount_paid            = nvl(l.amount_paid, 0) +
                                        nvl(p_pay_amount, 0),
             l.payment_completed_date = NULL,
             l.last_update_date       = SYSDATE,
             l.last_updated_by        = p_user_id
       WHERE l.payment_req_ln_id = p_payment_req_ln_id;
      p_payment_status := 'PARTIAL';
    END IF;
    SELECT COUNT(*)
      INTO v_count_status
      FROM csh_payment_req_ln l
     WHERE l.payment_req_id = p_payment_req_id
       AND l.payment_status <> 'FULL';
    --关闭申请头状态
    IF v_count_status = 0 THEN
      UPDATE csh_payment_req_hd
         SET closed_flag      = 'Y',
             closed_date      = SYSDATE,
             last_update_date = SYSDATE,
             last_updated_by  = p_user_id
       WHERE payment_req_id = p_payment_req_id;
    END IF;
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.PAYMENT_CSH_PAYMENT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'payment_csh_payment');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_req_amount_check THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.CSH501_PAYMENT_AMOUNT_CHECK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'payment_csh_payment');
      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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'payment_csh_payment');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE payment_csh_payment(p_payment_req_id          NUMBER,
                                p_session_id              NUMBER,
                                p_pay_date                DATE,
                                p_pay_amount              NUMBER,
                                p_company_id              NUMBER,
                                p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                p_period_name             csh_transaction.period_name%TYPE,
                                p_exchange_rate_type      csh_transaction.exchange_rate_type%TYPE,
                                p_exchange_rate_quotation csh_transaction.exchange_rate_quotation%TYPE,
                                p_exchange_rate           csh_transaction.exchange_rate%TYPE,
                                p_bank_account_id         csh_transaction.bank_account_id%TYPE,
                                p_bank_account_num        VARCHAR2,
                                p_description             csh_transaction.description%TYPE,
                                p_user_id                 NUMBER,
                                p_cashflow_id             NUMBER,
                                p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                p_bp_bank_account_id      csh_transaction.bp_bank_account_id%TYPE,
                                p_bp_bank_account_num     csh_transaction.bp_bank_account_num%TYPE,
                                p_handling_charge         csh_transaction.handling_charge%TYPE,
                                p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                p_payment_status          OUT VARCHAR2,
                                p_merge_flag              VARCHAR2 DEFAULT 'N',
                                p_payment_behalf_flag     VARCHAR2 DEFAULT 'N',
                                p_this_pay_agent_amount   number,
                                p_this_pay_vender_amount  number,
                                p_bank_flag               VARCHAR2 DEFAULT 'N',
                                p_bp_category             varchar2 default null,
                                p_due_amount              NUMBER DEFAULT NULL) IS
    v_payment_status VARCHAR2(30);
    v_transaction_id NUMBER;
    e_pay_check_error EXCEPTION;
    v_bank_status VARCHAR2(30);
    v_already_agent_paid number;
    v_already_vender_paid number;
  BEGIN
    /*for c_req_group in (select l.payment_req_ln_type,sum(t.amt) as pay_amt
                          from csh_payment_req_ln_tmp t,
                               csh_payment_req_ln l
                         where t.session_id = p_sessioN_id
                           and t.type = 'PAYMENT_REQ_PAYMENT'
                           and l.payment_req_ln_id = t.id
                           and l.payment_req_id = p_payment_req_id
                          group by l.payment_req_ln_type)
    loop
      if c_req_group.payment_req_ln_type = 'PREPAYMENT' then
        null;
      elsif c_req_group.payment_req_ln_type = 'DEBT' then
        null;
      end if;
    end loop; */
    IF p_bank_flag = 'N' THEN
      --如果不是银行付款,需要检测是否已经发起银行交易
      SELECT h.nc_pay_state
        INTO v_bank_status
        FROM csh_payment_req_hd h
       WHERE h.payment_req_id = p_payment_req_id;
      IF v_bank_status IS NOT NULL THEN
        RAISE e_pay_check_error;
      END IF;
    END IF;
    FOR c_req_group IN (SELECT l.payment_req_ln_type,
                               l.bp_id,
                               l.currency_code
                          FROM csh_payment_req_ln_tmp t,
                               csh_payment_req_ln     l
                         WHERE t.session_id = p_session_id
                           AND t.type = 'PAYMENT_REQ_PAYMENT'
                           AND t.id = l.payment_req_ln_id
                         GROUP BY l.payment_req_ln_type,
                                  l.bp_id,
                                  l.currency_code) LOOP
      v_transaction_id := NULL;
      FOR c_req IN (SELECT t.id, t.amt
                      FROM csh_payment_req_ln_tmp t, csh_payment_req_ln l
                     WHERE t.session_id = p_session_id
                       AND t.type = 'PAYMENT_REQ_PAYMENT'
                       AND t.id = l.payment_req_ln_id
                       AND l.payment_req_ln_type =
                           c_req_group.payment_req_ln_type
                       AND l.bp_id = c_req_group.bp_id
                       AND l.currency_code = c_req_group.currency_code) LOOP
        payment_csh_payment(p_payment_req_id          => p_payment_req_id,
                            p_payment_req_ln_id       => c_req.id,
                            p_pay_date                => p_pay_date,
                            p_pay_amount              => c_req.amt,
                            p_company_id              => p_company_id,
                            p_internal_period_num     => p_internal_period_num,
                            p_period_name             => p_period_name,
                            p_exchange_rate_type      => p_exchange_rate_type,
                            p_exchange_rate_quotation => p_exchange_rate_quotation,
                            p_exchange_rate           => p_exchange_rate,
                            p_bank_account_id         => p_bank_account_id,
                            p_bank_account_num        => p_bank_account_num,
                            p_description             => p_description,
                            p_user_id                 => p_user_id,
                            p_cashflow_id             => p_cashflow_id,
                            p_bank_slip_num           => p_bank_slip_num,
                            p_bp_bank_account_id      => p_bp_bank_account_id,
                            p_bp_bank_account_num     => p_bp_bank_account_num,
                            p_handling_charge         => p_handling_charge,
                            p_payment_method_id       => p_payment_method_id,
                            p_payment_status          => v_payment_status,
                            p_merge_flag              => p_merge_flag,
                            p_payment_behalf_flag     => p_payment_behalf_flag,
                            p_transaction_id          => v_transaction_id,
                            --add by shen 
                            p_bp_category             => p_bp_category,
                            p_due_amount              => p_due_amount);
       --查询已付款金额
       select cprl.amount_agent_paid,cprl.amount_vender_paid
          into v_already_agent_paid,v_already_vender_paid
          from csh_payment_req_ln cprl
          where cprl.payment_req_ln_id=c_req.id
          for update;
        --更新金额字段
       update csh_payment_req_ln  cprl
       set cprl.amount_agent_paid=nvl(v_already_agent_paid,0)+nvl(p_this_pay_agent_amount,0),
           cprl.amount_vender_paid=nvl(v_already_vender_paid,0)+nvl(p_this_pay_vender_amount,0)
        where cprl.payment_req_ln_id=c_req.id;
       
      END LOOP;
      IF p_merge_flag = 'Y' THEN
        csh_transaction_pkg.post_csh_transaction(p_transaction_id => v_transaction_id,
                                                 p_user_id        => p_user_id);
        csh_transaction_je_pkg.create_trx_write_off_je(p_transaction_id => v_transaction_id,
                                                       p_user_id        => p_user_id);
      END IF;
    END LOOP;
    DELETE FROM csh_payment_req_ln_tmp
     WHERE session_id = p_session_id
       AND TYPE = 'PAYMENT_REQ_PAYMENT';
    --20170216 JAINGLEI  放款成功短信  
    /*hls_sys_sms_job_pkg.generate_pay_succuess_sms(p_payment_req_id => p_payment_req_id,
    p_user_id        => p_user_id);*/
  EXCEPTION
    WHEN e_pay_check_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '已经发起银行付款,不允许手工支付',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'payment_csh_payment');
      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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'payment_csh_payment');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE payment_submit(p_payment_req_id NUMBER, p_user_id NUMBER) IS
    v_csh_payment_req_hd_rec csh_payment_req_hd%ROWTYPE;
    e_status_error EXCEPTION;
    v_approval_method hls_document_type.approval_method%TYPE;
    v_credit_flag     VARCHAR2(100);
    e_credit_error EXCEPTION;
    v_hd_user_col_v05 NUMBER;
    v_count           NUMBER;
    v_mortgage_flag   VARCHAR2(1);
    e_mortgage_error EXCEPTION;
    v_division         VARCHAR2(2);
    v_con_contract_rec con_contract%ROWTYPE;
  BEGIN
    lock_csh_payment_req_hd(p_payment_req_id         => p_payment_req_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_hd_rec => v_csh_payment_req_hd_rec);
  
    --付款前校验支付表代扣账户信息是否维护 modify by lpc for deron
    /*BEGIN
      SELECT cc.*
        INTO v_con_contract_rec
        FROM con_contract cc
       WHERE cc.contract_id = v_csh_payment_req_hd_rec.contract_id;
      IF v_con_contract_rec.deduction_flag IS NULL
         OR v_con_contract_rec.bankacctname IS NULL
         OR v_con_contract_rec.bankacctid IS NULL
         OR v_con_contract_rec.bankid IS NULL
         OR v_con_contract_rec.acctype IS NULL
         OR v_con_contract_rec.bank_account_id IS NULL THEN
        sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '付款前请先去支付表代扣账户维护功能维护账户信息!',
                                                       p_created_by              => p_user_id,
                                                       p_package_name            => 'prj_project_workflow_pkg',
                                                       p_procedure_function_name => 'workflow_start');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
      END IF;
    EXCEPTION
      WHEN no_data_found THEN
        sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '未找到合同!',
                                                       p_created_by              => p_user_id,
                                                       p_package_name            => 'prj_project_workflow_pkg',
                                                       p_procedure_function_name => 'workflow_start');
        raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                sys_raise_app_error_pkg.g_err_line_id);
    END;
    --校验授信额度 add by zhuxianfei 20180130
    v_credit_flag := hls_bp_master_credit_pkg.calc_overdue_amount_credit(p_bp_id          => v_csh_payment_req_hd_rec.bp_id,
                                                                         p_finance_amount => v_csh_payment_req_hd_rec.amount);
    IF v_credit_flag = 'N' THEN
      RAISE e_credit_error;
    END IF;
    
    --产品方案中是先抵押后放款时需要检验抵押信息 add by zhuxianfei 20180226
    --modify begin
    BEGIN
      SELECT --ppd.PRODUCT_NUMBER,
       ppd.hd_user_col_v05
        INTO v_hd_user_col_v05
        FROM hls_product_plan_definition ppd
       WHERE ppd.product_plan_id IN (SELECT pq.product_id
                                       FROM prj_quotation pq
                                      WHERE pq.document_id = (SELECT cc.project_id
                                                                FROM con_contract cc
                                                               WHERE cc.contract_id = v_csh_payment_req_hd_rec.contract_id));
    EXCEPTION
      WHEN no_data_found THEN
        NULL;
    END;
    SELECT division
      INTO v_division
      FROM con_contract
     WHERE contract_id = v_csh_payment_req_hd_rec.contract_id;
    --  1:先抵押后付款
    IF v_hd_user_col_v05 = 1
       AND v_division <> '02' THEN
      FOR lease_rec IN (SELECT ci.contract_lease_item_id
                          FROM con_contract_lease_item ci
                         WHERE ci.contract_id = v_csh_payment_req_hd_rec.contract_id)
      LOOP
        BEGIN
          SELECT clm.contract_mortgage_id
            INTO v_count
            FROM con_contract_lease_mortgage clm
           WHERE clm.contract_lease_item_id = lease_rec.contract_lease_item_id;
        EXCEPTION
          WHEN no_data_found THEN
            v_count := 0;
        END;
        IF v_count = 0 THEN
          RAISE e_mortgage_error;
        END IF;
      END LOOP;
    END IF;
    --modify end
    --校验可抵扣金额
    check_ddct_amount(p_payment_req_id => p_payment_req_id);
    */
    IF NOT is_payment_submitted(v_csh_payment_req_hd_rec) THEN
      RAISE e_status_error;
    END IF;
    FOR csh_payment_req_ln_rec IN (SELECT *
                                     FROM csh_payment_req_ln l
                                    WHERE l.payment_req_id =
                                          p_payment_req_id) LOOP
      csh_payment_req_amount_check(p_cashflow_id => csh_payment_req_ln_rec.ref_doc_line_id,
                                   p_amount      => csh_payment_req_ln_rec.amount,
                                   p_company_id  => NULL,
                                   p_user_id     => p_user_id);
    END LOOP;
  
    SELECT COUNT(1)
      INTO v_count
      FROM csh_payment_req_ln t
     WHERE t.payment_req_id = p_payment_req_id;
  
    IF nvl(v_count, 0) < 1 THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '未找到付款申请数据,请检查数据',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'payment_submit');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    END IF;
    SELECT approval_method
      INTO v_approval_method
      FROM hls_document_type
     WHERE document_category = v_csh_payment_req_hd_rec.document_category
       AND document_type = v_csh_payment_req_hd_rec.document_type;
    /*if v_approval_method = 'WORK_FLOW' then*/
    --启动工作流
    csh_payment_workflow_pkg.workflow_start(p_payment_req_id, p_user_id);
    /*else
      update csh_payment_req_hd
         set approval_status  = 'APPROVING',
             last_updated_by  = p_user_id,
             last_update_date = sysdate
       where payment_req_id = p_payment_req_id;
    end if;*/
    UPDATE csh_payment_req_hd t
       SET t.submitted_flag = 'Y'
     WHERE t.payment_req_id = p_payment_req_id;
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.SUBMIT_CSH_PAYMENT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'payment_submit');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_credit_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该商业伙伴的授信额度已占用完毕,无可使用额度,请核查!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'prj_project_workflow_pkg',
                                                     p_procedure_function_name => 'workflow_start');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_mortgage_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该产品为先抵押后付款模式,请先维护租赁物抵押信息!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'prj_project_workflow_pkg',
                                                     p_procedure_function_name => 'workflow_start');
      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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'payment_submit');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE ins_csh_pay_cashflow_id_temp(p_session_id  NUMBER,
                                         p_cashflow_id NUMBER,
                                         p_user_id     NUMBER) IS
    v_contract_cashflow_rec con_contract_cashflow%ROWTYPE;
    e_amount_before_confirm EXCEPTION;
    v_contract_id NUMBER;
  BEGIN
  
    v_contract_cashflow_rec := con_contract_pkg.get_cashflow_rec(p_cashflow_id => p_cashflow_id,
                                                                 p_user_id     => p_user_id);
  
    /* IF v_contract_cashflow_rec.estimated_due_amount IS NULL
       AND v_contract_cashflow_rec.cf_status = 'RELEASE' THEN
      RAISE e_amount_before_confirm;
    END IF;*/
  
    INSERT INTO csh_payment_cashflow_id_temp
    VALUES
      (p_session_id,
       p_cashflow_id,
       SYSDATE,
       p_user_id,
       SYSDATE,
       p_user_id,
       v_contract_cashflow_rec.contract_id);
    --插入付款头表
  EXCEPTION
    WHEN e_amount_before_confirm THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.AMOUNT_BEFORE_CONFIRM',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'ins_csh_pay_cashflow_id_temp');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE del_csh_pay_cashflow_id_temp(p_session_id NUMBER,
                                         p_user_id    NUMBER) IS
  BEGIN
    DELETE FROM csh_payment_cashflow_id_temp t
     WHERE t.session_id = p_session_id
        OR (SYSDATE - t.creation_date) > 1;
  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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'del_csh_pay_cashflow_id_temp');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END del_csh_pay_cashflow_id_temp;

  PROCEDURE ins_csh_payment_req_hd(p_payment_req_id       IN OUT NUMBER,
                                   p_company_id           NUMBER,
                                   p_document_type        VARCHAR2,
                                   p_payment_req_number   VARCHAR2,
                                   p_req_date             DATE,
                                   p_apply_pay_date       DATE,
                                   p_transaction_category VARCHAR2,
                                   p_distribution_set_id  NUMBER,
                                   p_payment_method_id    NUMBER,
                                   p_bp_id                NUMBER,
                                   p_bp_category          VARCHAR2,
                                   p_amount               NUMBER,
                                   p_currency_code        VARCHAR2,
                                   p_description          VARCHAR2,
                                   p_submitted_flag       VARCHAR2,
                                   p_printed_flag         VARCHAR2,
                                   p_printed_times        NUMBER,
                                   p_last_print_date      DATE,
                                   p_closed_flag          VARCHAR2,
                                   p_closed_date          DATE,
                                   p_closed_note          VARCHAR2,
                                   p_approval_status      VARCHAR2,
                                   p_approval_date        DATE,
                                   p_approval_note        VARCHAR2,
                                   p_bp_bank_account_id   NUMBER DEFAULT NULL,
                                   p_bp_bank_account_num  VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_account_name VARCHAR2 DEFAULT NULL,
                                   p_business_type        VARCHAR2 DEFAULT NULL,
                                   p_branch_id            NUMBER,
                                   p_city_id              NUMBER,
                                   p_user_id              NUMBER,
                                   p_csh_bp_name          VARCHAR2 DEFAULT NULL) IS
    v_payment_req_id         NUMBER;
    v_csh_payment_req_hd_rec csh_payment_req_hd%ROWTYPE;
    v_payment_req_number     VARCHAR2(100);
    v_bank_full_name         VARCHAR2(1000);
  BEGIN
    IF p_payment_req_id IS NULL THEN
      v_payment_req_number := get_payment_req_number(p_document_type     => p_document_type,
                                                     p_transaction_date  => SYSDATE,
                                                     p_company_id        => p_company_id,
                                                     p_user_id           => p_user_id,
                                                     p_document_category => 'PAYMENT_REQ');
      SELECT csh_payment_req_hd_s.nextval INTO p_payment_req_id FROM dual;
      --add by Harry 9952 2017/2/28  创建付款申请时,添加头表开户行字段
      SELECT hp.bank_branch_name
        INTO v_bank_full_name
        FROM hls_bp_master_bank_account hp
       WHERE hp.bank_account_id = p_bp_bank_account_id;
      INSERT INTO csh_payment_req_hd
        (payment_req_id,
         company_id,
         document_type,
         document_category,
         payment_req_number,
         req_date,
         apply_pay_date,
         transaction_category,
         distribution_set_id,
         payment_method_id,
         bp_id,
         bp_category,
         amount,
         currency_code,
         description,
         submitted_flag,
         printed_flag,
         printed_times,
         last_print_date,
         closed_flag,
         closed_date,
         closed_note,
         approval_status,
         approval_date,
         approval_note,
         owner_user_id,
         creation_date,
         created_by,
         last_update_date,
         last_updated_by,
         bp_bank_account_id,
         bp_bank_account_num,
         bp_bank_account_name,
         business_type,
         bank_full_name,
         branch_id,
         city_id)
      VALUES
        (p_payment_req_id,
         p_company_id,
         p_document_type,
         'PAYMENT_REQ',
         v_payment_req_number,
         nvl(p_req_date, SYSDATE),
         p_apply_pay_date,
         'BUSINESS',
         '',
         '',
         p_bp_id,
         p_bp_category,
         p_amount,
         p_currency_code,
         p_description,
         'N',
         'N',
         '',
         '',
         'N',
         '',
         '',
         'NEW',
         '',
         '',
         p_user_id,
         SYSDATE,
         p_user_id,
         SYSDATE,
         p_user_id,
         p_bp_bank_account_id,
         p_bp_bank_account_num,
         p_bp_bank_account_name,
         p_business_type,
         v_bank_full_name,
         p_branch_id,
         p_city_id);
      --增加权限
      aut_document_authority_pkg.insert_trx_user_authority(p_company_id    => p_company_id,
                                                           p_owner_user_id => p_user_id,
                                                           p_trx_category  => 'PAYMENT_REQ',
                                                           p_trx_id        => p_payment_req_id,
                                                           p_start_date    => trunc(SYSDATE),
                                                           p_end_date      => to_date('30000101',
                                                                                      'yyyymmdd'),
                                                           p_user_id       => p_user_id);
      --提交状态更新
      /* payment_submit(p_payment_req_id => p_payment_req_id,
      p_user_id        => p_user_id);*/
    ELSE
      UPDATE csh_payment_req_hd t1
         SET t1.req_date         = nvl(p_req_date, SYSDATE),
             t1.apply_pay_date   = p_apply_pay_date,
             t1.bp_id            = p_bp_id,
             t1.bp_category      = p_bp_category,
             t1.amount           = p_amount,
             t1.currency_code    = p_currency_code,
             t1.description      = p_description,
             t1.last_update_date = SYSDATE,
             t1.last_updated_by  = p_user_id
       WHERE t1.payment_req_id = p_payment_req_id;
    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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'ins_csh_payment_req_hd');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE ins_csh_payment_req_ln(p_payment_req_ln_id      IN OUT NUMBER,
                                   p_payment_req_id         NUMBER,
                                   p_payment_req_ln_type    VARCHAR2,
                                   p_ref_doc_category       VARCHAR2,
                                   p_ref_doc_id             NUMBER,
                                   p_ref_doc_line_id        NUMBER,
                                   p_bp_id                  NUMBER,
                                   p_currency_code          VARCHAR2,
                                   p_amount                 NUMBER,
                                   p_amount_paid            NUMBER,
                                   p_description            VARCHAR2,
                                   p_payment_status         VARCHAR2,
                                   p_payment_completed_date DATE,
                                   p_apply_pay_date         DATE,
                                   p_payment_method_id      NUMBER,
                                   p_bank_account_id        NUMBER,
                                   p_bank_account_num       VARCHAR2,
                                   p_bank_account_name      VARCHAR2,
                                   p_bank_branch_name       VARCHAR2,
                                   p_bp_bank_account_id     NUMBER DEFAULT NULL,
                                   p_bp_bank_account_num    VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_account_name   VARCHAR2 DEFAULT NULL,
                                   p_user_id                NUMBER,
                                   p_company_id             NUMBER) IS
    --v_amount_flag varchar2(1) ;
    v_document_category con_contract.document_category%TYPE;
  BEGIN
    --申请金额校验
    csh_payment_req_amount_check(p_cashflow_id => p_ref_doc_line_id,
                                 p_amount      => p_amount,
                                 p_company_id  => p_company_id,
                                 p_user_id     => p_user_id);
    SELECT t.document_category
      INTO v_document_category
      FROM con_contract t
     WHERE t.contract_id = p_ref_doc_id
       FOR UPDATE NOWAIT;
    IF p_payment_req_ln_id IS NULL THEN
      SELECT csh_payment_req_ln_s.nextval
        INTO p_payment_req_ln_id
        FROM dual;
      INSERT INTO csh_payment_req_ln
        (payment_req_ln_id,
         payment_req_id,
         payment_req_ln_type,
         ref_doc_category,
         ref_doc_id,
         ref_doc_line_id,
         bp_id,
         currency_code,
         amount,
         amount_paid,
         description,
         payment_status,
         payment_completed_date,
         apply_pay_date,
         payment_method_id,
         bank_account_id,
         bank_account_num,
         bank_account_name,
         bank_branch_name,
         bp_bank_account_id,
         bp_bank_account_num,
         bp_bank_account_name,
         creation_date,
         created_by,
         last_update_date,
         last_updated_by)
      VALUES
        (p_payment_req_ln_id,
         p_payment_req_id,
         'DEBT',
         v_document_category,
         p_ref_doc_id,
         p_ref_doc_line_id,
         p_bp_id,
         p_currency_code,
         p_amount,
         p_amount_paid,
         p_description,
         'NOT',
         p_payment_completed_date,
         p_apply_pay_date,
         p_payment_method_id,
         p_bank_account_id,
         p_bank_account_num,
         p_bank_account_name,
         p_bank_branch_name,
         p_bp_bank_account_id,
         p_bp_bank_account_num,
         p_bp_bank_account_name,
         SYSDATE,
         p_user_id,
         SYSDATE,
         p_user_id);
    ELSE
      UPDATE csh_payment_req_ln t1
         SET t1.bp_id                = p_bp_id,
             t1.currency_code        = p_currency_code,
             t1.amount               = p_amount,
             t1.amount_paid          = p_amount_paid,
             t1.description          = p_description,
             t1.apply_pay_date       = p_apply_pay_date,
             t1.payment_method_id    = p_payment_method_id,
             t1.bank_account_id      = p_bank_account_id,
             t1.bank_account_num     = p_bank_account_num,
             t1.bank_account_name    = p_bank_account_name,
             t1.bank_branch_name     = p_bank_branch_name,
             t1.bp_bank_account_id   = p_bp_bank_account_id,
             t1.bp_bank_account_num  = p_bp_bank_account_num,
             t1.bp_bank_account_name = p_bp_bank_account_name,
             t1.last_update_date     = SYSDATE,
             t1.last_updated_by      = p_user_id
       WHERE t1.payment_req_ln_id = p_payment_req_ln_id;
    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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'ins_csh_payment_req_ln');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE del_csh_payment_req_ln(p_payment_req_ln_id NUMBER,
                                   p_user_id           NUMBER) IS
    v_csh_payment_req_hd_rec csh_payment_req_hd%ROWTYPE;
    v_csh_payment_req_ln_rec csh_payment_req_ln%ROWTYPE;
    e_status_error EXCEPTION;
  BEGIN
    lock_csh_payment_req_ln(p_payment_req_ln_id      => p_payment_req_ln_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_ln_rec => v_csh_payment_req_ln_rec);
    lock_csh_payment_req_hd(p_payment_req_id         => v_csh_payment_req_ln_rec.payment_req_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_hd_rec => v_csh_payment_req_hd_rec);
    /*if not is_payment_submitted(v_csh_payment_req_hd_rec) then
      raise e_status_error;
    end if;*/
    IF nvl(v_csh_payment_req_ln_rec.payment_status, 'NOT') <> 'NOT' THEN
      RAISE e_status_error;
    END IF;
    DELETE FROM csh_payment_req_ln l
     WHERE l.payment_req_ln_id = p_payment_req_ln_id;
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该付款申请行已支付,不能删除',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'del_csh_payment_req_ln');
      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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'del_csh_payment_req_ln');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE del_csh_payment_req_hd(p_payment_req_id NUMBER,
                                   p_user_id        NUMBER) IS
    v_csh_payment_req_hd_rec csh_payment_req_hd%ROWTYPE;
    e_status_error EXCEPTION;
  BEGIN
    lock_csh_payment_req_hd(p_payment_req_id         => p_payment_req_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_hd_rec => v_csh_payment_req_hd_rec);
    IF NOT is_payment_submitted(v_csh_payment_req_hd_rec) THEN
      RAISE e_status_error;
    END IF;
    --删除抵扣信息  modify by zhuxianfei 20180323 --begin
    DELETE FROM csh_payment_req_ln_ddct ld
     WHERE ld.ref_doc_line_id IN
           (SELECT t1.ref_doc_line_id
              FROM csh_payment_req_ln_ddct_lv t1
             WHERE payment_req_id = p_payment_req_id);
    --end
    DELETE FROM csh_payment_req_hd h
     WHERE h.payment_req_id = p_payment_req_id;
    DELETE FROM csh_payment_req_ln l
     WHERE l.payment_req_id = p_payment_req_id;
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.DELETE_CSH_PAYMENT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'del_csh_payment_req_hd');
      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            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'del_csh_payment_req_hd');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE change_print_status(p_contract_id NUMBER, p_user_id NUMBER) IS
    v_con_contract_rec con_contract%ROWTYPE;
  BEGIN
    lock_con_contract_rec(p_contract_id      => p_contract_id,
                          p_user_id          => p_user_id,
                          p_con_contract_rec => v_con_contract_rec);
    UPDATE con_contract t
       SET t.pay_req_print_status = 'PRINT',
           t.last_update_date     = SYSDATE,
           t.last_updated_by      = p_user_id
     WHERE t.contract_id = v_con_contract_rec.contract_id;
  END;

  PROCEDURE change_printed_status(p_contract_id NUMBER, p_user_id NUMBER) IS
    v_con_contract_rec con_contract%ROWTYPE;
  BEGIN
    /*    lock_con_contract_rec(p_contract_id      => p_contract_id,
    p_user_id          => p_user_id,
    p_con_contract_rec => v_con_contract_rec);*/
    UPDATE con_contract t
       SET t.pay_req_print_status = 'PRINTED',
           t.last_update_date     = SYSDATE,
           t.last_updated_by      = p_user_id
     WHERE t.contract_id = p_contract_id;
  END;

  PROCEDURE change_applied_status(p_contract_id NUMBER, p_user_id NUMBER) IS
    v_con_contract_rec con_contract%ROWTYPE;
  BEGIN
    lock_con_contract_rec(p_contract_id      => p_contract_id,
                          p_user_id          => p_user_id,
                          p_con_contract_rec => v_con_contract_rec);
    UPDATE con_contract t
       SET t.pay_req_print_status = 'APPLIED',
           t.last_update_date     = SYSDATE,
           t.last_updated_by      = p_user_id
     WHERE t.contract_id = v_con_contract_rec.contract_id;
  END;

  PROCEDURE csh_payment_apply(p_contract_id NUMBER, p_user_id NUMBER) IS
    v_con_contract_rec     con_contract%ROWTYPE;
    v_pay_req_print_status VARCHAR2(30);
    v_inception_of_lease   DATE;
    e_lease_date_null    EXCEPTION;
    e_print_status_error EXCEPTION;
  BEGIN
    lock_con_contract_rec(p_contract_id      => p_contract_id,
                          p_user_id          => p_user_id,
                          p_con_contract_rec => v_con_contract_rec);
    v_pay_req_print_status := v_con_contract_rec.pay_req_print_status;
    v_inception_of_lease   := v_con_contract_rec.inception_of_lease;
    IF v_pay_req_print_status IN ('PRINT', 'PRINTED', 'APPLIED') THEN
      RAISE e_print_status_error;
    END IF;
    IF v_inception_of_lease IS NULL OR v_inception_of_lease = '' THEN
      RAISE e_lease_date_null;
    END IF;
    change_applied_status(p_contract_id => v_con_contract_rec.contract_id,
                          p_user_id     => p_user_id);
  EXCEPTION
    WHEN e_print_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.PAYMENT_REQ_PRINT_STATUS_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'csh_payment_apply');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_lease_date_null THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.LEASE_DATE_NULL',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'csh_payment_apply');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE csh_payment_print(p_contract_id NUMBER, p_user_id NUMBER) IS
    v_con_contract_rec     con_contract%ROWTYPE;
    v_pay_req_print_status VARCHAR2(30);
    e_req_print_status_error EXCEPTION;
  BEGIN
    lock_con_contract_rec(p_contract_id      => p_contract_id,
                          p_user_id          => p_user_id,
                          p_con_contract_rec => v_con_contract_rec);
    v_pay_req_print_status := v_con_contract_rec.pay_req_print_status;
    /*  if v_pay_req_print_status is null or v_pay_req_print_status != 'PRINT' then
      raise e_req_print_status_error;
    end if;*/
    change_printed_status(p_contract_id => v_con_contract_rec.contract_id,
                          p_user_id     => p_user_id);
    --for doc status
    yonda_doc_history_pkg.yonda_insert_doc_status(p_document_id       => v_con_contract_rec.contract_id,
                                                  p_document_type     => v_con_contract_rec.document_type,
                                                  p_document_category => v_con_contract_rec.document_category,
                                                  p_doc_status        => yonda_doc_history_pkg.yonda_con_payment_doc_print,
                                                  p_instance_id       => NULL,
                                                  p_user_id           => p_user_id);
  EXCEPTION
    WHEN e_req_print_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_PAYMENT_REQ_PKG.REQ_PRINT_STATUS_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_payment_req_pkg',
                                                      p_procedure_function_name => 'csh_payment_print');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE update_csh_payment_flag(p_payment_req_id NUMBER,
                                    p_payment_flag   VARCHAR2,
                                    p_user_id        NUMBER) IS
  BEGIN
    UPDATE csh_payment_req_hd hd
       SET hd.payment_flag     = p_payment_flag,
           hd.last_update_date = SYSDATE,
           hd.last_updated_by  = p_user_id
     WHERE hd.payment_req_id = p_payment_req_id;
  END;

  PROCEDURE update_payment_req_contract_id(p_payment_req_id     IN NUMBER,
                                           p_payment_req_number IN VARCHAR2) IS
  BEGIN
    UPDATE csh_payment_req_hd h
       SET h.payment_req_number = p_payment_req_number
     WHERE h.payment_req_id = p_payment_req_id;
  END;

  --创建cdd_list_id
  PROCEDURE create_cdd(p_payment_req_id NUMBER,
                       p_user_id        NUMBER,
                       p_cdd_list_id    IN OUT NUMBER) IS
    v_csh_payment_req_record csh_payment_req_hd%ROWTYPE;
  BEGIN
    lock_csh_payment_req_hd(p_payment_req_id         => p_payment_req_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_hd_rec => v_csh_payment_req_record);
    IF p_cdd_list_id IS NULL THEN
      save_cdd_item_from_templt(p_document_category => v_csh_payment_req_record.document_category,
                                p_document_type     => v_csh_payment_req_record.document_type,
                                p_document_id       => p_payment_req_id,
                                p_company_id        => v_csh_payment_req_record.company_id,
                                p_user_id           => p_user_id,
                                p_cdd_list_id       => p_cdd_list_id);
      UPDATE csh_payment_req_hd d
         SET d.cdd_list_id      = p_cdd_list_id,
             d.last_updated_by  = p_user_id,
             d.last_update_date = SYSDATE
       WHERE d.payment_req_id = p_payment_req_id
         AND d.cdd_list_id IS NULL;
    END IF;
  END;

  --创建cdd
  PROCEDURE save_cdd_item_from_templt(p_document_category VARCHAR2,
                                      p_document_type     VARCHAR2,
                                      p_document_id       NUMBER,
                                      p_company_id        NUMBER,
                                      p_user_id           NUMBER,
                                      p_cdd_list_id       IN OUT NUMBER) IS
    v_csh_payment_req_record csh_payment_req_hd%ROWTYPE;
    v_cdd_item_id            NUMBER;
    v_templet_head_code      VARCHAR2(1000);
    v_templet_head_id        NUMBER;
    v_cond_para1             VARCHAR2(255) := 'NEW';
    v_cond_para2             VARCHAR2(255);
    v_cond_para3             VARCHAR2(255);
    v_cond_para4             VARCHAR2(255);
    v_cond_para5             VARCHAR2(255);
    v_bp_class               VARCHAR2(20);
  BEGIN
    lock_csh_payment_req_hd(p_payment_req_id         => p_document_id,
                            p_user_id                => p_user_id,
                            p_csh_payment_req_hd_rec => v_csh_payment_req_record);
    SELECT h.bp_class
      INTO v_bp_class
      FROM con_contract t, hls_bp_master h
     WHERE t.bp_id_tenant = h.bp_id
       AND t.contract_id = v_csh_payment_req_record.contract_id;
  
    sys_condition_cdd_pkg.matching_condition(p_condition_code    => 'CDD00',
                                             p_company_id        => v_csh_payment_req_record.company_id,
                                             p_role_id           => '',
                                             p_user_id           => p_user_id,
                                             p_function_code     => '',
                                             p_currency          => '',
                                             p_lease_org         => '',
                                             p_lease_channel     => '',
                                             p_division          => '',
                                             p_business_type     => '',
                                             p_document_category => '',
                                             p_document_type     => '',
                                             p_bp_class          => v_bp_class,
                                             p_cond_para1        => v_cond_para1,
                                             p_cond_para2        => v_cond_para2,
                                             p_cond_para3        => v_cond_para3,
                                             p_cond_para4        => v_cond_para4,
                                             p_cond_para5        => v_cond_para5,
                                             p_cdd_code          => v_templet_head_code);
    BEGIN
      SELECT h.templet_head_id
        INTO v_templet_head_id
        FROM prj_cdd_item_templet_hd h
       WHERE h.templet_code = v_templet_head_code
         AND h.enabled_flag = 'Y';
      prj_cdd_item_pkg.create_cdd_item_from_templet(p_templet_head_id => v_templet_head_id,
                                                    p_cdd_list_id     => p_cdd_list_id,
                                                    p_user_id         => p_user_id);
    EXCEPTION
      WHEN no_data_found THEN
        NULL;
    END;
  END;

  PROCEDURE update_payment_req_number(p_payment_req_id     IN NUMBER,
                                      p_payment_req_number IN VARCHAR2) IS
  BEGIN
    UPDATE csh_payment_req_hd h
       SET h.payment_req_number = p_payment_req_number
     WHERE h.payment_req_id = p_payment_req_id;
  END;

  /*Function get_payment_record(p_payment_req_id Number, p_user_id Number)
    Return csh_payment_req_hd%Rowtype Is
    v_record csh_payment_req_hd%Rowtype;
  Begin
  
    Select *
      Into v_record
      From csh_payment_req_hd v
     Where v.payment_req_id = p_payment_req_id
       For Update Nowait;
    Return v_record;
  
  End;*/
  PROCEDURE save_cdd_item_doc_ref(p_source_document_table VARCHAR2,
                                  p_source_document_id    NUMBER,
                                  p_to_document_table     VARCHAR2,
                                  p_to_document_id        NUMBER,
                                  p_user_id               NUMBER) IS
  BEGIN
    --save prj_cdd_item_doc_ref
    INSERT INTO prj_cdd_item_doc_ref
      (doc_ref_id,
       document_table,
       document_id,
       check_id,
       created_by,
       creation_date,
       last_updated_by,
       last_update_date)
      (SELECT prj_cdd_item_doc_ref_s.nextval,
              p_to_document_table,
              p_to_document_id,
              check_id,
              p_user_id,
              SYSDATE,
              p_user_id,
              SYSDATE
         FROM prj_cdd_item_doc_ref
        WHERE document_table = p_source_document_table
          AND document_id = p_source_document_id);
  END;

  --插入合同id
  PROCEDURE update_payment_req_contract_id(p_payment_req_id IN NUMBER,
                                           p_contract_id    IN NUMBER) IS
    v_cdd_list_id NUMBER;
  BEGIN
    SELECT cc.cdd_list_id
      INTO v_cdd_list_id
      FROM con_contract cc
     WHERE cc.contract_id = p_contract_id;
    UPDATE csh_payment_req_hd h
       SET h.contract_id = p_contract_id, h.cdd_list_id = v_cdd_list_id
     WHERE h.payment_req_id = p_payment_req_id;
    --复制附件
    /*save_cdd_item_doc_ref(p_source_document_table => 'CON_CONTRACT',
    p_source_document_id    => p_contract_id,
    p_to_document_table     => 'CSH_PAYMENT_REQ_HD',
    p_to_document_id        => p_payment_req_id,
    p_user_id               => 1);*/
  END;

  --检验可抵扣金额
  PROCEDURE check_ddct_amount(p_payment_req_id IN NUMBER) IS
    v_cdd_list_id       NUMBER;
    v_payment_req_ln_id NUMBER;
    v_un_due_amount     NUMBER;
    e_ddct_amount_error EXCEPTION;
  BEGIN
    --可以多行
    /*Select l.payment_req_ln_id
      Into v_payment_req_ln_id
      From csh_payment_req_ln l
     Where l.payment_req_id = p_payment_req_id;
    
    Select Sum(dt.un_due_amount)
      Into v_un_due_amount
      From csh_payment_req_ln_ddct_lv dt
     Where dt.payment_req_ln_id = v_payment_req_ln_id;*/
    SELECT SUM(dt.un_due_amount)
      INTO v_un_due_amount
      FROM csh_payment_req_ln_ddct_lv dt
     WHERE dt.payment_req_ln_id IN
           (SELECT l.payment_req_ln_id
              FROM csh_payment_req_ln l
             WHERE l.payment_req_id = p_payment_req_id);
    IF v_un_due_amount < 0 THEN
      RAISE e_ddct_amount_error;
    END IF;
  EXCEPTION
    WHEN e_ddct_amount_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '可抵扣金额小于0,请检查该条单据!',
                                                     p_created_by              => 1,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_ddct_amount');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN no_data_found THEN
      NULL;
  END;

  PROCEDURE check_contract_payment(p_contract_id    NUMBER,
                                   p_payment_req_id NUMBER,
                                   p_user_id        NUMBER) IS
    v_count NUMBER;
    e_con_not_found_payment_err EXCEPTION;
  BEGIN
    SELECT COUNT(1)
      INTO v_count
      FROM csh_payment_req_ln l
     WHERE l.ref_doc_category = 'CONTRACT'
       AND l.ref_doc_id = p_contract_id
       AND l.payment_req_id = p_payment_req_id
       AND l.payment_status <> 'FULL';
    IF v_count >= 1 THEN
      RAISE e_con_not_found_payment_err;
    END IF;
  EXCEPTION
    WHEN e_con_not_found_payment_err THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同尚未全部完成支付!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_contract_payment');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --add by 9188 检验付款申请工作流的所有合同是否全部核销
  PROCEDURE check_sch_payment_con(p_payment_req_id NUMBER,
                                  p_user_id        NUMBER) IS
    v_count NUMBER := 0;
    e_csh_payment_err EXCEPTION;
  BEGIN
    FOR cu IN (SELECT *
                 FROM csh_payment_req_ln t1
                WHERE t1.payment_req_id = p_payment_req_id
                  AND t1.ref_doc_category = 'CONTRACT') LOOP
      IF (cu.payment_status <> 'FULL') THEN
        v_count := v_count + 1;
      END IF;
    END LOOP;
    IF (v_count >= 1) THEN
      RAISE e_csh_payment_err;
    END IF;
  EXCEPTION
    WHEN e_csh_payment_err THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该付款申请下存在尚未全部完成支付的合同!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_sch_payment_con');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END check_sch_payment_con;

  --add by Harry 9952 保证金退还-付款申请校验
  PROCEDURE check_status_and_flag(p_cashflow_id NUMBER, p_user_id NUMBER) IS
    v_count_1 NUMBER;
    v_count_2 NUMBER;
    v_count_3 NUMBER;
    e_closed_flag_error EXCEPTION;
    e_approving_error   EXCEPTION;
    e_csh_trx_error     EXCEPTION;
  BEGIN
    SELECT COUNT(*)
      INTO v_count_1
      FROM csh_payment_req_hd h
     WHERE h.payment_req_id IN
           (SELECT ln.payment_req_id
              FROM csh_payment_req_ln ln
             WHERE ln.ref_doc_line_id = p_cashflow_id)
       AND h.closed_flag != 'Y';
    SELECT COUNT(*)
      INTO v_count_2
      FROM csh_payment_req_hd h
     WHERE h.payment_req_id IN
           (SELECT l.payment_req_id
              FROM csh_payment_req_ln l
             WHERE l.ref_doc_line_id = p_cashflow_id)
       AND h.approval_status = 'APPROVING';
    -- 收付抵扣工作流中
    SELECT COUNT(*)
      INTO v_count_3
      FROM hls_common_wfl c, con_contract_cashflow d, csh_write_off_temp t
     WHERE c.status = 'APPROVING'
       AND c.wfl_instance_id = t.wfl_instance_id
       AND t.document_line_id = d.cashflow_id
       AND d.cashflow_id = p_cashflow_id
       AND t.document_category = 'CSH_TRX';
    /* if v_count_1 <> 0 then
      raise e_closed_flag_error;
    end if;*/
    IF v_count_2 <> 0 THEN
      RAISE e_approving_error;
    END IF;
    /*    if v_count_3 <> 0 then
      raise e_csh_trx_error;
    end if;*/
  EXCEPTION
    WHEN e_closed_flag_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同客户保证金正在抵扣工作流中,请勿继续抵扣或支付!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_status_and_flag');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_approving_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同客户保证金正在付款工作流中,请勿继续抵扣或支付!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_status_and_flag');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_csh_trx_error THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该合同客户保证金正在抵扣工作流中,请勿继续抵扣或支付!',
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'csh_payment_req_pkg',
                                                     p_procedure_function_name => 'check_status_and_flag');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END check_status_and_flag;

END csh_payment_req_pkg;
/