csh_transaction_pkg.pck 203 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 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534
CREATE OR REPLACE PACKAGE csh_transaction_pkg IS

  -- Author  : GAOYANG
  -- Created : 2013/5/9 14:43:26
  -- Purpose : 现金事务
  -- Version : 1.18

  ---退款
  csh_trx_return_flag_p      CONSTANT VARCHAR2(30) := 'PARTIAL';
  csh_trx_return_flag_f      CONSTANT VARCHAR2(30) := 'FULL';
  csh_trx_return_flag_n      CONSTANT VARCHAR2(30) := 'NOT';
  csh_trx_return_flag_return CONSTANT VARCHAR2(30) := 'RETURN'; --退款类型
  --核销
  csh_trx_write_off_flag_p CONSTANT VARCHAR2(30) := 'PARTIAL'; -- 部分核销
  csh_trx_write_off_flag_n CONSTANT VARCHAR2(30) := 'NOT'; -- 未核销
  csh_trx_write_off_flag_f CONSTANT VARCHAR2(30) := 'FULL'; -- 完全核销

  ---冲销
  csh_trx_reversed_flag_n CONSTANT VARCHAR2(1) := 'N';
  csh_trx_reversed_flag_r CONSTANT VARCHAR2(1) := 'R';
  csh_trx_reversed_flag_w CONSTANT VARCHAR2(1) := 'W';

  csh_trx_type_receipt    CONSTANT VARCHAR2(30) := 'RECEIPT'; -- 收款类型
  csh_trx_type_prereceipt CONSTANT VARCHAR2(30) := 'ADVANCE_RECEIPT'; -- 预收款类型
  csh_trx_type_payment    CONSTANT VARCHAR2(30) := 'PAYMENT'; -- 付款类型
  csh_trx_type_deduction  CONSTANT VARCHAR2(30) := 'DEDUCTION'; -- 收付抵扣

  csh_trx_category_business      CONSTANT VARCHAR2(30) := 'BUSINESS'; -- 收款类别
  csh_trx_category_loan_withdraw CONSTANT VARCHAR2(30) := 'LOAN_WITHDRAW'; -- 银行借款
  csh_trx_category_capital_int   CONSTANT VARCHAR2(30) := 'LOAN_REPAYMENT_CI'; -- 还本付息
  csh_trx_category_capital       CONSTANT VARCHAR2(30) := 'LOAN_REPAYMENT_C'; -- 本金
  csh_trx_category_interest      CONSTANT VARCHAR2(30) := 'LOAN_REPAYMENT_I'; -- 利息
  csh_trx_category_factoring     CONSTANT VARCHAR2(30) := 'LOAN_REPAYMENT_F'; -- 保理还款
  csh_trx_category_factoring_fee CONSTANT VARCHAR2(30) := 'LOAN_REPAYMENT_FF'; -- 保理费

  csh_trx_type_deposit CONSTANT VARCHAR2(30) := 'DEPOSIT'; --保证金类型
  csh_trx_type_risk    CONSTANT VARCHAR2(30) := 'RISK'; --风险金类型

  ---过帐
  csh_trx_posted_flag_yes CONSTANT VARCHAR2(1) := 'Y';
  csh_trx_posted_flag_no  CONSTANT VARCHAR2(1) := 'N';

  /* 现金事务编号获取函数 */
  FUNCTION get_csh_transaction_num(p_transaction_type VARCHAR2,
                                   p_transaction_date DATE,
                                   p_company_id       NUMBER,
                                   p_user_id          NUMBER) RETURN VARCHAR2;

  PROCEDURE lock_csh_transaction(p_transaction_id      IN NUMBER,
                                 p_user_id             IN NUMBER,
                                 p_csh_transaction_rec OUT csh_transaction%ROWTYPE);

  --更新现金事务核销状态
  PROCEDURE set_csh_trx_write_off_status(p_csh_trx_rec      csh_transaction%ROWTYPE,
                                         p_write_off_amount NUMBER,
                                         p_user_id          NUMBER);

  --更新被反冲现金事务
  PROCEDURE update_csh_trx_after_reverse(p_transaction_id      NUMBER,
                                         p_reversed_date       DATE,
                                         p_reserved_csh_trx_id NUMBER,
                                         p_user_id             NUMBER);

  --核销反冲后更新原现金事务核销金额
  PROCEDURE update_trx_amt_after_reverse(p_transaction_id NUMBER,
                                         p_reverse_amount NUMBER,
                                         p_user_id        NUMBER);

  FUNCTION is_trx_return(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN;

  FUNCTION is_trx_reverse(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN;

  /*modify by xuls 2014-9-16
  add ref_contract_id
   */
  PROCEDURE insert_csh_transaction(p_transaction_id          OUT csh_transaction.transaction_id%TYPE,
                                   p_transaction_num         csh_transaction.transaction_num%TYPE,
                                   p_transaction_category    csh_transaction.transaction_category%TYPE,
                                   p_transaction_type        csh_transaction.transaction_type%TYPE,
                                   p_transaction_date        csh_transaction.transaction_date%TYPE,
                                   p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                   p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                   p_company_id              csh_transaction.company_id%TYPE,
                                   p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                   p_period_name             csh_transaction.period_name%TYPE,
                                   p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                   p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                   p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                   p_currency_code           csh_transaction.currency_code%TYPE,
                                   p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                   p_bp_id                   csh_transaction.bp_id%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_bp_bank_account_name    csh_transaction.bp_bank_account_name%TYPE DEFAULT NULL, --add by Spener 3893 20160722
                                   p_description             csh_transaction.description%TYPE,
                                   p_handling_charge         csh_transaction.handling_charge%TYPE,
                                   p_posted_flag             csh_transaction.posted_flag%TYPE,
                                   p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                   p_reversed_date           csh_transaction.reversed_date%TYPE,
                                   p_returned_flag           csh_transaction.returned_flag%TYPE,
                                   p_returned_amount         csh_transaction.returned_amount%TYPE,
                                   p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                   p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                   p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                   p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                   p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                   p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                   p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                   p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                   p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                   p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                   p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                   p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                   p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                   p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                   p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                   p_user_id                 csh_transaction.created_by%TYPE,
                                   p_ref_contract_id         csh_transaction.ref_contract_id%TYPE DEFAULT NULL,
                                   p_receipt_type            csh_transaction.receipt_type%TYPE DEFAULT NULL,
                                   p_csh_bp_name             csh_transaction.csh_bp_name%TYPE DEFAULT NULL,
                                   p_ref_n01                 NUMBER DEFAULT NULL,
                                   p_bp_bank_name            VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_branch_name     VARCHAR2 DEFAULT NULL,
                                   p_collection_classes      VARCHAR2 DEFAULT NULL,
                                   p_opposite_band_na        VARCHAR2 DEFAULT NULL,
                                   p_banka                   VARCHAR2 DEFAULT NULL,
                                   p_purpose                 VARCHAR2 DEFAULT NULL,
                                   p_gsber                   VARCHAR2 DEFAULT NULL,
                                   p_sus_belnr               VARCHAR2 DEFAULT NULL,
                                   p_assigns_contract_id     NUMBER DEFAULT NULL,
                                   p_conds_account_num       VARCHAR2 DEFAULT NULL,
                                   p_conds_account_name      VARCHAR2 DEFAULT NULL,
                                   p_bp_virtual_bank_account VARCHAR2 DEFAULT NULL,
                                   
                                   p_return_id NUMBER DEFAULT NULL,
                                   p_zuonr     VARCHAR2 DEFAULT NULL,
                                   --add by sf  
                                   p_paid_byother_flag IN VARCHAR2,
                                   p_ref_v05           IN VARCHAR2,
                                   p_status            IN VARCHAR2 DEFAULT NULL
                                   --end;
                                   );

  PROCEDURE update_csh_transaction(p_transaction_id          csh_transaction.transaction_id%TYPE,
                                   p_transaction_num         csh_transaction.transaction_num%TYPE,
                                   p_transaction_category    csh_transaction.transaction_category%TYPE,
                                   p_transaction_type        csh_transaction.transaction_type%TYPE,
                                   p_transaction_date        csh_transaction.transaction_date%TYPE,
                                   p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                   p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                   p_company_id              csh_transaction.company_id%TYPE,
                                   p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                   p_period_name             csh_transaction.period_name%TYPE,
                                   p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                   p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                   p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                   p_currency_code           csh_transaction.currency_code%TYPE,
                                   p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                   p_bp_id                   csh_transaction.bp_id%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_description             csh_transaction.description%TYPE,
                                   p_handling_charge         csh_transaction.handling_charge%TYPE,
                                   p_posted_flag             csh_transaction.posted_flag%TYPE,
                                   p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                   p_reversed_date           csh_transaction.reversed_date%TYPE,
                                   p_returned_flag           csh_transaction.returned_flag%TYPE,
                                   p_returned_amount         csh_transaction.returned_amount%TYPE,
                                   p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                   p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                   p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                   p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                   p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                   p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                   p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                   p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                   p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                   p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                   p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                   p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                   p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                   p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                   p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                   p_user_id                 csh_transaction.created_by%TYPE,
                                   p_ref_contract_id         csh_transaction.ref_contract_id%TYPE DEFAULT NULL,
                                   p_ref_n01                 NUMBER DEFAULT NULL,
                                   p_bp_bank_name            VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_branch_name     VARCHAR2 DEFAULT NULL,
                                   p_collection_classes      VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_account_name    VARCHAR2 DEFAULT NULL,
                                   p_opposite_band_na        VARCHAR2 DEFAULT NULL,
                                   p_banka                   VARCHAR2 DEFAULT NULL,
                                   p_purpose                 VARCHAR2 DEFAULT NULL,
                                   p_gsber                   VARCHAR2 DEFAULT NULL,
                                   p_sus_belnr               VARCHAR2 DEFAULT NULL,
                                   p_bp_virtual_bank_account VARCHAR2 DEFAULT NULL);
--更新退款现金事务信息
  PROCEDURE update_csh_trx_after_return(p_transaction_id  NUMBER,
                                        p_returned_amount NUMBER,
                                        p_user_id         NUMBER); 
 --货币兑换维护
  PROCEDURE update_currency_exchange_trx(p_transaction_id          csh_transaction.transaction_id%TYPE,
                                         p_transaction_num         csh_transaction.transaction_num%TYPE,
                                         p_transaction_category    csh_transaction.transaction_category%TYPE,
                                         p_transaction_type        csh_transaction.transaction_type%TYPE,
                                         p_transaction_date        csh_transaction.transaction_date%TYPE,
                                         p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                         p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                         p_company_id              csh_transaction.company_id%TYPE,
                                         p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                         p_period_name             csh_transaction.period_name%TYPE,
                                         p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                         p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                         p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                         p_currency_code           csh_transaction.currency_code%TYPE,
                                         p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                         p_bp_id                   csh_transaction.bp_id%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_description             csh_transaction.description%TYPE,
                                         p_handling_charge         csh_transaction.handling_charge%TYPE,
                                         p_posted_flag             csh_transaction.posted_flag%TYPE,
                                         p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                         p_reversed_date           csh_transaction.reversed_date%TYPE,
                                         p_returned_flag           csh_transaction.returned_flag%TYPE,
                                         p_returned_amount         csh_transaction.returned_amount%TYPE,
                                         p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                         p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                         p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                         p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                         p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                         p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                         p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                         p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                         p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                         p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                         p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                         p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                         p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                         p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                         p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                         p_user_id                 csh_transaction.created_by%TYPE);

  --货币兑换互记TWIN_CSH_TRX_ID
  PROCEDURE save_twin_csh_trx_id(p_transaction_num csh_transaction.transaction_num%TYPE,
                                 p_user_id         NUMBER);

  --货币兑换过账以后维护
  PROCEDURE upd_currency_trx_after_post(p_transaction_id csh_transaction.transaction_id%TYPE,
                                        p_bank_slip_num  csh_transaction.bank_slip_num%TYPE,
                                        p_description    csh_transaction.description%TYPE,
                                        p_user_id        csh_transaction.created_by%TYPE);
  --过账以后维护
  PROCEDURE update_csh_transaction_post(p_transaction_id     csh_transaction.transaction_id%TYPE,
                                        p_bank_slip_num      csh_transaction.bank_slip_num%TYPE,
                                        p_bp_bank_account_id csh_transaction.bp_bank_account_id%TYPE,
                                        p_user_id            csh_transaction.created_by%TYPE);

  PROCEDURE update_csh_post_flag(p_transaction_id NUMBER,
                                 p_user_id        NUMBER,
                                 p_claim          VARCHAR2 DEFAULT NULL);
  PROCEDURE delete_csh_transaction(p_transaction_id csh_transaction.transaction_id%TYPE,
                                   p_user_id        csh_transaction.created_by%TYPE);
  PROCEDURE post_csh_transaction(p_transaction_id csh_transaction.transaction_id%TYPE,
                                 p_user_id        csh_transaction.created_by%TYPE);

  --通用的反冲入口
  PROCEDURE reverse_csh_transaction(p_transaction_id         csh_transaction.transaction_id%TYPE,
                                    p_reversed_date          csh_transaction.reversed_date%TYPE,
                                    p_description            csh_transaction.description%TYPE,
                                    p_user_id                csh_transaction.created_by%TYPE,
                                    p_reverse_transaction_id OUT csh_transaction.transaction_id%TYPE);
  --收款反冲入口
  PROCEDURE reverse_receipt_csh_trx(p_transaction_id csh_transaction.transaction_id%TYPE,
                                    p_reversed_date  csh_transaction.reversed_date%TYPE,
                                    p_bank_slip_num  csh_transaction.bank_slip_num%TYPE,
                                    p_description    csh_transaction.description%TYPE,
                                    p_user_id        csh_transaction.created_by%TYPE);

  PROCEDURE return_csh_transaction(p_transaction_id      csh_transaction.transaction_id%TYPE,
                                   p_returned_date       DATE,
                                   p_returned_amount     csh_transaction.returned_amount%TYPE,
                                   p_bank_slip_num       csh_transaction.bank_slip_num%TYPE,
                                   p_payment_method_id   csh_transaction.payment_method_id%TYPE,
                                   p_bank_account_id     csh_transaction.bank_account_id%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_description         csh_transaction.description%TYPE,
                                   p_user_id             csh_transaction.created_by%TYPE,
                                   p_return_id           NUMBER DEFAULT NULL);
  PROCEDURE delete_interface(p_batch_id NUMBER, p_user_id NUMBER);

  PROCEDURE insert_interface(p_header_id NUMBER,
                             p_batch_id  NUMBER,
                             p_user_id   NUMBER);

  PROCEDURE check_data(p_batch_id   NUMBER,
                       p_user_id    NUMBER,
                       p_check_type VARCHAR2 DEFAULT NULL,
                       p_return_id  OUT NUMBER);

  PROCEDURE save_data(p_batch_id     NUMBER,
                      p_user_id      NUMBER,
                      p_company_id   NUMBER,
                      v_save_message OUT VARCHAR2);

  --收款导入新建现金事务
  PROCEDURE save_transaction_data(p_batch_id     NUMBER,
                                  p_user_id      NUMBER,
                                  p_company_id   NUMBER,
                                  v_save_message OUT VARCHAR2);

  PROCEDURE risk_vat_insert(p_vat        NUMBER,
                            p_company_id NUMBER,
                            p_user_id    NUMBER);

  --add by shen                          
  --插入手工收款新增数据,检验流水号的唯一性
  PROCEDURE save_csh_transaction_data(p_transaction_date    IN VARCHAR2,
                                      p_cashflow_amount     IN NUMBER,
                                      p_bank_slip_num       IN VARCHAR2,
                                      p_paid_byother_flag   IN VARCHAR2,
                                      p_bp_id               IN NUMBER,
                                      p_collection_classes  IN NUMBER,
                                      p_description         IN VARCHAR2,
                                      p_bp_bank_account_id  IN NUMBER,
                                      p_bp_bank_name        IN VARCHAR2,
                                      p_bp_bank_account_num IN VARCHAR2,
                                      p_bank_account_id     IN VARCHAR2,
                                      p_user_id             IN VARCHAR2);
  --excel导入收款新增csh_transaction表
  PROCEDURE csh_transaction_import_check(p_header_id NUMBER,
                                         p_user_id   NUMBER);
  --改变单据的状态
  PROCEDURE update_confirmed_flag(p_transaction_id        IN NUMBER,
                                  p_wanted_confirmed_flag IN VARCHAR2,
                                  p_write_off_flag        IN VARCHAR2 DEFAULT NULL,
                                  p_user_id               IN VARCHAR2);

  --根据银行卡号获得银行账号的id
  PROCEDURE get_bank_account_id(p_bank_account_num    IN VARCHAR2,
                                p_bp_bank_account_num IN VARCHAR2,
                                p_bank_account_id     OUT NUMBER,
                                p_bp_category         OUT VARCHAR2,
                                p_bp_id               OUT NUMBER,
                                p_csh_bp_name         OUT VARCHAR2,
                                p_user_id             IN VARCHAR2);
   PROCEDURE get_bank_account_info(p_bank_account_num    IN VARCHAR2,
                                p_bp_bank_account_name IN VARCHAR2,
                                p_bank_account_id     OUT NUMBER,
                                p_bp_category         OUT VARCHAR2,
                                p_bp_id               OUT NUMBER,
                                p_csh_bp_name         OUT VARCHAR2,
                                p_user_id             IN VARCHAR2);
  --excel 导入收款数据
  PROCEDURE save_receipt_import(p_transaction_id          OUT csh_transaction.transaction_id%TYPE,
                                p_transaction_num         csh_transaction.transaction_num%TYPE,
                                p_transaction_category    csh_transaction.transaction_category%TYPE,
                                p_transaction_type        csh_transaction.transaction_type%TYPE,
                                p_transaction_date        csh_transaction.transaction_date%TYPE,
                                p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                p_company_id              csh_transaction.company_id%TYPE,
                                p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                p_period_name             csh_transaction.period_name%TYPE,
                                p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                p_currency_code           csh_transaction.currency_code%TYPE,
                                p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                p_bp_id                   csh_transaction.bp_id%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_bp_bank_account_name    csh_transaction.bp_bank_account_name%TYPE DEFAULT NULL, --add by Spener 3893 20160722
                                p_description             csh_transaction.description%TYPE,
                                p_handling_charge         csh_transaction.handling_charge%TYPE,
                                p_posted_flag             csh_transaction.posted_flag%TYPE,
                                p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                p_reversed_date           csh_transaction.reversed_date%TYPE,
                                p_returned_flag           csh_transaction.returned_flag%TYPE,
                                p_returned_amount         csh_transaction.returned_amount%TYPE,
                                p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                p_user_id                 csh_transaction.created_by%TYPE,
                                p_ref_contract_id         csh_transaction.ref_contract_id%TYPE DEFAULT NULL,
                                p_receipt_type            csh_transaction.receipt_type%TYPE DEFAULT NULL,
                                p_csh_bp_name             csh_transaction.csh_bp_name%TYPE DEFAULT NULL,
                                p_ref_n01                 NUMBER DEFAULT NULL,
                                p_bp_bank_name            VARCHAR2 DEFAULT NULL,
                                p_bp_bank_branch_name     VARCHAR2 DEFAULT NULL,
                                p_collection_classes      VARCHAR2 DEFAULT NULL,
                                p_opposite_band_na        VARCHAR2 DEFAULT NULL,
                                p_banka                   VARCHAR2 DEFAULT NULL,
                                p_purpose                 VARCHAR2 DEFAULT NULL,
                                p_gsber                   VARCHAR2 DEFAULT NULL,
                                p_sus_belnr               VARCHAR2 DEFAULT NULL,
                                p_assigns_contract_id     NUMBER DEFAULT NULL,
                                p_conds_account_num       VARCHAR2 DEFAULT NULL,
                                p_conds_account_name      VARCHAR2 DEFAULT NULL,
                                p_bp_virtual_bank_account VARCHAR2 DEFAULT NULL,
                                
                                p_return_id NUMBER DEFAULT NULL,
                                p_zuonr     VARCHAR2 DEFAULT NULL,
                                --add by sf  
                                p_paid_byother_flag IN VARCHAR2,
                                p_ref_v05           IN VARCHAR2,
                                --该行信息是否正确
                                p_status           IN VARCHAR2,
                                p_bank_account_num IN VARCHAR2
                                --end;
                                );
 procedure update_confirmer_by_condition(
                               p_transaction_id   in number,
                               p_user_id          in varchar2
                               );
  --修改单据的状态(作废,驳回,以及确认单据)
 procedure update_muti_confirmed_flag(
                               p_transaction_id          in number,
                               p_wanted_confirmed_flag   in varchar2 ,
                               p_wanted_written_off_flag in varchar2 default null,
                               p_confirmed_by            in varchar2 default null,
                               p_user_id                 in varchar2
                          );
END csh_transaction_pkg;
/
CREATE OR REPLACE PACKAGE BODY csh_transaction_pkg IS

  v_err_code VARCHAR2(2000);
  e_lock_trx_error EXCEPTION;
  PRAGMA EXCEPTION_INIT(e_lock_trx_error, -54);

  FUNCTION get_csh_transaction_num(p_transaction_type VARCHAR2,
                                   p_transaction_date DATE,
                                   p_company_id       NUMBER,
                                   p_user_id          NUMBER) RETURN VARCHAR2 IS
    v_transaction_num csh_transaction.transaction_num%TYPE;
    e_trx_num_error EXCEPTION;
    v_transaction_type VARCHAR2(200);
  BEGIN
  
    IF p_transaction_type = 'DEPOSIT_POOL_RECEIPT' THEN
      v_transaction_type := 'ADVANCE_RECEIPT';
    ELSE
      v_transaction_type := p_transaction_type;
    END IF;
  
    v_transaction_num := fnd_code_rule_pkg.get_rule_next_auto_num(p_document_category => 'CSH_TRX',
                                                                  p_document_type     => v_transaction_type,
                                                                  p_company_id        => p_company_id,
                                                                  p_operation_unit_id => NULL,
                                                                  p_operation_date    => p_transaction_date,
                                                                  p_created_by        => p_user_id);
    IF nvl(v_transaction_num, fnd_code_rule_pkg.c_error) =
       fnd_code_rule_pkg.c_error THEN
      RAISE e_trx_num_error;
    END IF;
    RETURN v_transaction_num;
  EXCEPTION
    WHEN e_trx_num_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_TRANSACTION_NUM_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'get_csh_transaction_num');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --录入0金额校验
  PROCEDURE check_zero_amount(p_csh_transaction_rec IN csh_transaction%ROWTYPE,
                              p_user_id             IN NUMBER) IS
    v_zero_amounts_allowed VARCHAR2(1);
    e_amount_error EXCEPTION;
  BEGIN
  
    IF p_csh_transaction_rec.transaction_amount <> 0 OR
       (p_csh_transaction_rec.transaction_amount = 0 AND
       p_csh_transaction_rec.transaction_type = csh_trx_type_deduction) THEN
      RETURN;
    END IF;
  
    /*SELECT nvl(d.zero_amounts_allowed, 'N')
      INTO v_zero_amounts_allowed
      FROM csh_bank_account_v d
     WHERE d.bank_account_id = p_csh_transaction_rec.bank_account_id;
    
    IF v_zero_amounts_allowed = 'N' THEN
      RAISE e_amount_error;
    END IF;*/
  EXCEPTION
    WHEN e_amount_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_ZERO_AMOUNT_CHECK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'zero_amount_check');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  PROCEDURE lock_csh_transaction(p_transaction_id      IN NUMBER,
                                 p_user_id             IN NUMBER,
                                 p_csh_transaction_rec OUT csh_transaction%ROWTYPE) IS
  
  BEGIN
    SELECT *
      INTO p_csh_transaction_rec
      FROM csh_transaction t
     WHERE t.transaction_id = p_transaction_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_transaction_pkg',
                                                      p_procedure_function_name => 'lock_csh_transaction');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --更新现金事务核销状态(包括已核销金额)
  PROCEDURE set_csh_trx_write_off_status(p_csh_trx_rec      csh_transaction%ROWTYPE,
                                         p_write_off_amount NUMBER,
                                         p_user_id          NUMBER) IS
  
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    v_sum_write_off_amt   NUMBER := 0;
    v_sum_check_amount    NUMBER := 0;
    v_write_off_flag      VARCHAR2(30);
    v_last_write_off_date DATE;
  
    e_write_off_amount_error EXCEPTION;
  BEGIN
    IF p_csh_trx_rec.transaction_type = csh_trx_type_deduction THEN
      RETURN;
    END IF;
  
    lock_csh_transaction(p_transaction_id      => p_csh_trx_rec.transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    v_sum_write_off_amt := nvl(p_write_off_amount, 0) +
                           nvl(v_csh_transaction_rec.write_off_amount, 0);
    v_sum_check_amount  := nvl(p_write_off_amount, 0) +
                           nvl(v_csh_transaction_rec.write_off_amount, 0) +
                           nvl(v_csh_transaction_rec.returned_amount, 0);
    IF v_sum_check_amount = 0 THEN
      v_write_off_flag      := csh_trx_write_off_flag_n;
      v_last_write_off_date := v_csh_transaction_rec.full_write_off_date;
    ELSIF v_sum_check_amount > 0 AND
          v_sum_check_amount <
          nvl(v_csh_transaction_rec.transaction_amount, 0) THEN
      v_write_off_flag      := csh_trx_write_off_flag_p;
      v_last_write_off_date := v_csh_transaction_rec.full_write_off_date;
    ELSIF v_sum_check_amount =
          nvl(v_csh_transaction_rec.transaction_amount, 0) THEN
      v_write_off_flag      := csh_trx_write_off_flag_f;
      v_last_write_off_date := csh_write_off_pkg.get_last_write_off_date(p_transaction_id => v_csh_transaction_rec.transaction_id);
    ELSE
      RAISE e_write_off_amount_error;
    END IF;
  
    UPDATE csh_transaction t
       SET t.write_off_flag      = v_write_off_flag,
           t.write_off_amount    = v_sum_write_off_amt,
           t.full_write_off_date = v_last_write_off_date,
           t.last_update_date    = SYSDATE,
           t.last_updated_by     = p_user_id
     WHERE t.transaction_id = v_csh_transaction_rec.transaction_id;
  
  EXCEPTION
    WHEN e_write_off_amount_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH_WRITE_OFF_AMOUNT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'set_csh_trx_write_off_status');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --更新退款现金事务信息
  PROCEDURE update_csh_trx_after_return(p_transaction_id  NUMBER,
                                        p_returned_amount NUMBER,
                                        p_user_id         NUMBER) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    v_returned_flag       csh_transaction.returned_flag%TYPE;
    v_write_off_flag      csh_transaction.write_off_flag%TYPE;
  
    v_sum_returned_amount NUMBER := 0;
    v_sum_writeoff_amount NUMBER := 0;
  
    e_sum_returned_amount_error EXCEPTION;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    v_sum_returned_amount := nvl(v_csh_transaction_rec.returned_amount, 0) +
                             nvl(p_returned_amount, 0);
    v_sum_writeoff_amount := nvl(v_csh_transaction_rec.returned_amount, 0) +
                             nvl(p_returned_amount, 0) +
                             nvl(v_csh_transaction_rec.write_off_amount, 0);
  
    IF v_sum_writeoff_amount = v_csh_transaction_rec.transaction_amount THEN
      v_write_off_flag := csh_trx_write_off_flag_f;
    ELSIF v_sum_writeoff_amount = 0 THEN
      v_write_off_flag := csh_trx_write_off_flag_n;
    ELSIF v_sum_writeoff_amount > 0 AND
          v_sum_writeoff_amount < v_csh_transaction_rec.transaction_amount THEN
      v_write_off_flag := csh_trx_write_off_flag_p;
    ELSE
      RAISE e_sum_returned_amount_error;
    END IF;
  
    IF v_sum_returned_amount = v_csh_transaction_rec.transaction_amount THEN
      v_returned_flag := csh_transaction_pkg.csh_trx_return_flag_f;
    ELSIF v_sum_returned_amount = 0 THEN
      v_returned_flag := csh_transaction_pkg.csh_trx_return_flag_n;
    ELSIF v_sum_returned_amount > 0 AND
          v_sum_returned_amount < v_csh_transaction_rec.transaction_amount THEN
      v_returned_flag := csh_transaction_pkg.csh_trx_return_flag_p;
    ELSE
      RAISE e_sum_returned_amount_error;
    END IF;
  
    UPDATE csh_transaction t
       SET t.returned_flag    = v_returned_flag,
           t.returned_amount  = v_sum_returned_amount,
           t.write_off_flag   = v_write_off_flag,
           t.last_update_date = SYSDATE,
           t.last_updated_by  = p_user_id
     WHERE t.transaction_id = v_csh_transaction_rec.transaction_id;
  EXCEPTION
    WHEN e_sum_returned_amount_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH_RETURNED_AMOUNT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'update_csh_trx_after_return');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --更新被反冲现金事务
  PROCEDURE update_csh_trx_after_reverse(p_transaction_id      NUMBER,
                                         p_reversed_date       DATE,
                                         p_reserved_csh_trx_id NUMBER,
                                         p_user_id             NUMBER) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    UPDATE csh_transaction t
       SET t.reversed_flag       = csh_transaction_pkg.csh_trx_reversed_flag_w,
           t.reversed_date       = p_reversed_date,
           t.reversed_csh_trx_id = p_reserved_csh_trx_id,
           t.last_update_date    = SYSDATE,
           t.last_updated_by     = p_user_id
     WHERE t.transaction_id = v_csh_transaction_rec.transaction_id;
    BEGIN
      --add by chenlingfeng for ds 删除预分配记录
      csh_transaction_interface_pkg.delete_write_off_by_trx(p_transaction_id => v_csh_transaction_rec.transaction_id);
    EXCEPTION
      WHEN OTHERS THEN
        NULL;
    END;
  END;

  --退款反冲后更新原退款现金事务信息
  PROCEDURE upd_rtn_csh_trx_after_reverse(p_transaction_id NUMBER,
                                          p_user_id        NUMBER) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    v_source_csh_trx_rec  csh_transaction%ROWTYPE;
    v_returned_flag       csh_transaction.returned_flag%TYPE;
    v_write_off_flag      csh_transaction.write_off_flag%TYPE;
    v_sum_returned_amount NUMBER := 0;
    v_sum_writeoff_amount NUMBER := 0;
    e_sum_returned_amount_error EXCEPTION;
  BEGIN
    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_transaction(p_transaction_id      => v_csh_transaction_rec.return_from_csh_trx_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_source_csh_trx_rec);
  
    v_sum_returned_amount := nvl(v_source_csh_trx_rec.returned_amount, 0) -
                             nvl(v_csh_transaction_rec.transaction_amount,
                                 0);
  
    v_sum_writeoff_amount := v_sum_returned_amount +
                             nvl(v_source_csh_trx_rec.write_off_amount, 0);
  
    IF v_sum_writeoff_amount = v_source_csh_trx_rec.transaction_amount THEN
      v_write_off_flag := csh_trx_write_off_flag_f;
    ELSIF v_sum_writeoff_amount = 0 THEN
      v_write_off_flag := csh_trx_write_off_flag_n;
    ELSIF v_sum_writeoff_amount > 0 AND
          v_sum_writeoff_amount < v_source_csh_trx_rec.transaction_amount THEN
      v_write_off_flag := csh_trx_write_off_flag_p;
    ELSE
      RAISE e_sum_returned_amount_error;
    END IF;
  
    IF v_sum_returned_amount = 0 THEN
      v_returned_flag := csh_trx_return_flag_n;
    ELSIF v_sum_returned_amount = v_source_csh_trx_rec.transaction_amount THEN
      v_returned_flag := csh_trx_return_flag_f;
    ELSIF v_sum_returned_amount > 0 AND
          v_sum_returned_amount < v_source_csh_trx_rec.transaction_amount THEN
      v_returned_flag := csh_trx_return_flag_p;
    ELSE
      RAISE e_sum_returned_amount_error;
    END IF;
  
    UPDATE csh_transaction t
       SET t.returned_amount  = v_sum_returned_amount,
           t.returned_flag    = v_returned_flag,
           t.last_update_date = SYSDATE,
           t.last_updated_by  = p_user_id
     WHERE t.transaction_id = v_csh_transaction_rec.return_from_csh_trx_id;
  EXCEPTION
    WHEN e_sum_returned_amount_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH_RETURNED_AMOUNT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'upd_rtn_csh_trx_after_reverse');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --核销反冲后更新被反冲现金事务核销金额
  PROCEDURE update_trx_amt_after_reverse(p_transaction_id NUMBER,
                                         p_reverse_amount NUMBER,
                                         p_user_id        NUMBER) IS
    v_csh_transaction_rec  csh_transaction%ROWTYPE;
    v_sum_write_off_amount NUMBER;
  
    v_sum_check_amount    NUMBER;
    v_write_off_flag      VARCHAR2(30);
    v_last_write_off_date DATE;
    e_sum_writeoff_amount_error EXCEPTION;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    --抵扣的不用更新现金流金额                     
    IF v_csh_transaction_rec.transaction_type = csh_trx_type_deduction THEN
      RETURN;
    END IF;
  
    v_sum_write_off_amount := nvl(v_csh_transaction_rec.write_off_amount, 0) -
                              nvl(p_reverse_amount, 0);
  
    v_sum_check_amount := nvl(v_csh_transaction_rec.write_off_amount, 0) +
                          nvl(v_csh_transaction_rec.returned_amount, 0) -
                          nvl(p_reverse_amount, 0);
  
    IF v_sum_check_amount = 0 THEN
      v_write_off_flag      := csh_trx_write_off_flag_n;
      v_last_write_off_date := NULL;
    ELSIF v_sum_check_amount > 0 AND
          v_sum_check_amount < v_csh_transaction_rec.transaction_amount THEN
      v_write_off_flag      := csh_trx_write_off_flag_p;
      v_last_write_off_date := NULL;
    ELSIF v_sum_check_amount = v_csh_transaction_rec.transaction_amount THEN
      v_write_off_flag      := csh_trx_write_off_flag_f;
      v_last_write_off_date := csh_write_off_pkg.get_last_write_off_date(p_transaction_id => v_csh_transaction_rec.transaction_id);
    ELSE
      RAISE e_sum_writeoff_amount_error;
    END IF;
    --反冲后更新审批状态为新建  20170216  JIANGLEI
    UPDATE csh_transaction t
       SET t.write_off_amount    = v_sum_write_off_amount,
           t.write_off_flag      = v_write_off_flag,
           t.full_write_off_date = v_last_write_off_date,
           t.status              = 'NEW',
           t.last_update_date    = SYSDATE,
           t.last_updated_by     = p_user_id
     WHERE t.transaction_id = v_csh_transaction_rec.transaction_id;
  EXCEPTION
    WHEN e_sum_writeoff_amount_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH_WRITEOFF_AMOUNT_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'update_trx_amt_after_reverse');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --更新银行水单
  PROCEDURE update_bank_slip_num(p_transaction_id csh_transaction.transaction_id%TYPE,
                                 p_bank_slip_num  csh_transaction.bank_slip_num%TYPE,
                                 p_user_id        csh_transaction.created_by%TYPE) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    UPDATE csh_transaction t
       SET t.bank_slip_num    = p_bank_slip_num,
           t.last_update_date = SYSDATE,
           t.last_updated_by  = p_user_id
     WHERE t.transaction_id = v_csh_transaction_rec.transaction_id;
  END;
  FUNCTION is_trx_return(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF nvl(p_csh_transaction_rec.posted_flag,
           csh_transaction_pkg.csh_trx_posted_flag_yes) <>
       csh_transaction_pkg.csh_trx_posted_flag_yes THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_POSTED_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.returned_flag,
              csh_transaction_pkg.csh_trx_return_flag_n) =
          csh_transaction_pkg.csh_trx_return_flag_f THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_RETURN_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.reversed_flag,
              csh_transaction_pkg.csh_trx_reversed_flag_n) <>
          csh_transaction_pkg.csh_trx_reversed_flag_n THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_REVERSED_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.write_off_flag,
              csh_transaction_pkg.csh_trx_write_off_flag_n) =
          csh_transaction_pkg.csh_trx_write_off_flag_f THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_WRITE_OFF_FLAG_ERROR';
      RETURN FALSE;
    ELSE
      RETURN TRUE;
    END IF;
  END;
  FUNCTION is_trx_reverse(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF nvl(p_csh_transaction_rec.posted_flag, csh_trx_posted_flag_yes) <>
       csh_trx_posted_flag_yes THEN
      v_err_code := 'CSH_TRANSACTION_PKG.REVERSE_POSTED_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.returned_flag, csh_trx_return_flag_n) NOT IN
          (csh_trx_return_flag_n, csh_trx_return_flag_return) THEN
      v_err_code := 'CSH_TRANSACTION_PKG.REVERSE_RETURN_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.reversed_flag, csh_trx_reversed_flag_n) <>
          csh_trx_reversed_flag_n THEN
      v_err_code := 'CSH_TRANSACTION_PKG.REVERSE_REVERSED_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.write_off_flag,
              csh_trx_write_off_flag_n) <> csh_trx_write_off_flag_n AND
          nvl(p_csh_transaction_rec.returned_flag, csh_trx_return_flag_n) <>
          csh_trx_return_flag_return AND
          p_csh_transaction_rec.transaction_type NOT IN
          (csh_trx_type_payment, 'DEDUCTION') THEN
      v_err_code := 'CSH_TRANSACTION_PKG.REVERSE_WRITE_OFF_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.write_off_flag,
              csh_trx_write_off_flag_n) <> csh_trx_write_off_flag_f AND
          nvl(p_csh_transaction_rec.returned_flag, csh_trx_return_flag_n) <>
          csh_trx_return_flag_n AND
          p_csh_transaction_rec.transaction_type = csh_trx_type_payment THEN
      v_err_code := 'CSH_TRANSACTION_PKG.PAYMENT_REVERSE_WRITE_OFF_FLAG_ERROR';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.returned_flag, csh_trx_return_flag_n) =
          csh_trx_return_flag_return AND
          nvl(p_csh_transaction_rec.write_off_flag,
              csh_trx_write_off_flag_n) <> csh_trx_write_off_flag_f THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_WRITE_OFF_FLAG_ERROR';
      RETURN FALSE;
    ELSE
      RETURN TRUE;
    END IF;
  END;

  FUNCTION is_trx_post(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF nvl(p_csh_transaction_rec.posted_flag,
           csh_transaction_pkg.csh_trx_posted_flag_no) =
       csh_transaction_pkg.csh_trx_posted_flag_no THEN
      RETURN TRUE;
    ELSE
      RETURN FALSE;
    END IF;
  END;
  FUNCTION is_trx_update(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF nvl(p_csh_transaction_rec.posted_flag,
           csh_transaction_pkg.csh_trx_posted_flag_no) =
       csh_transaction_pkg.csh_trx_posted_flag_no THEN
      RETURN TRUE;
    ELSE
      RETURN FALSE;
    END IF;
  END;
  FUNCTION is_trx_update_after_post(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF nvl(p_csh_transaction_rec.posted_flag,
           csh_transaction_pkg.csh_trx_posted_flag_no) <>
       csh_transaction_pkg.csh_trx_posted_flag_yes THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH510_POST_STATUS_CHECK';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.reversed_flag,
              csh_transaction_pkg.csh_trx_reversed_flag_n) <>
          csh_transaction_pkg.csh_trx_reversed_flag_n THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH510_REVERSE_FLAG_CHECK';
      RETURN FALSE;
    ELSE
      RETURN TRUE;
    END IF;
  END;
  FUNCTION is_trx_delete(p_csh_transaction_rec IN csh_transaction%ROWTYPE)
    RETURN BOOLEAN IS
  BEGIN
    IF nvl(p_csh_transaction_rec.posted_flag,
           csh_transaction_pkg.csh_trx_posted_flag_no) <>
       csh_transaction_pkg.csh_trx_posted_flag_no THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH510_DELETE_STATUS_CHECK';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.reversed_flag,
              csh_transaction_pkg.csh_trx_reversed_flag_n) <>
          csh_transaction_pkg.csh_trx_reversed_flag_n THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH510_DELETE_REVERSE_CHECK';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.returned_flag,
              csh_transaction_pkg.csh_trx_return_flag_n) <>
          csh_transaction_pkg.csh_trx_return_flag_n THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH510_DELETE_RETURN_CHECK';
      RETURN FALSE;
    ELSIF nvl(p_csh_transaction_rec.write_off_flag,
              csh_transaction_pkg.csh_trx_write_off_flag_n) <>
          csh_transaction_pkg.csh_trx_write_off_flag_n THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH510_DELETE_WRITEOFF_CHECK';
      RETURN FALSE;
    ELSE
      RETURN TRUE;
    END IF;
  END;
  PROCEDURE insert_csh_transaction(p_csh_transaction_rec IN OUT csh_transaction%ROWTYPE) IS
  BEGIN
    IF p_csh_transaction_rec.transaction_id IS NULL THEN
      SELECT csh_transaction_s.nextval
        INTO p_csh_transaction_rec.transaction_id
        FROM dual;
    END IF;
    INSERT INTO csh_transaction VALUES p_csh_transaction_rec;
  END;

  /*add by xuls for ref_contract_id 2014-9-26
   用于增加记录收款合同号
  */
  PROCEDURE insert_csh_transaction(p_transaction_id          OUT csh_transaction.transaction_id%TYPE,
                                   p_transaction_num         csh_transaction.transaction_num%TYPE,
                                   p_transaction_category    csh_transaction.transaction_category%TYPE,
                                   p_transaction_type        csh_transaction.transaction_type%TYPE,
                                   p_transaction_date        csh_transaction.transaction_date%TYPE,
                                   p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                   p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                   p_company_id              csh_transaction.company_id%TYPE,
                                   p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                   p_period_name             csh_transaction.period_name%TYPE,
                                   p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                   p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                   p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                   p_currency_code           csh_transaction.currency_code%TYPE,
                                   p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                   p_bp_id                   csh_transaction.bp_id%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_bp_bank_account_name    csh_transaction.bp_bank_account_name%TYPE DEFAULT NULL, --add by Spener 3893 20160722
                                   p_description             csh_transaction.description%TYPE,
                                   p_handling_charge         csh_transaction.handling_charge%TYPE,
                                   p_posted_flag             csh_transaction.posted_flag%TYPE,
                                   p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                   p_reversed_date           csh_transaction.reversed_date%TYPE,
                                   p_returned_flag           csh_transaction.returned_flag%TYPE,
                                   p_returned_amount         csh_transaction.returned_amount%TYPE,
                                   p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                   p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                   p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                   p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                   p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                   p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                   p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                   p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                   p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                   p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                   p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                   p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                   p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                   p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                   p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                   p_user_id                 csh_transaction.created_by%TYPE,
                                   p_ref_contract_id         csh_transaction.ref_contract_id%TYPE DEFAULT NULL,
                                   p_receipt_type            csh_transaction.receipt_type%TYPE DEFAULT NULL,
                                   p_csh_bp_name             csh_transaction.csh_bp_name%TYPE DEFAULT NULL,
                                   p_ref_n01                 NUMBER DEFAULT NULL,
                                   --add by chenlingfeng
                                   p_bp_bank_name            VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_branch_name     VARCHAR2 DEFAULT NULL,
                                   p_collection_classes      VARCHAR2 DEFAULT NULL,
                                   p_opposite_band_na        VARCHAR2 DEFAULT NULL,
                                   p_banka                   VARCHAR2 DEFAULT NULL,
                                   p_purpose                 VARCHAR2 DEFAULT NULL,
                                   p_gsber                   VARCHAR2 DEFAULT NULL,
                                   p_sus_belnr               VARCHAR2 DEFAULT NULL,
                                   p_assigns_contract_id     NUMBER DEFAULT NULL,
                                   p_conds_account_num       VARCHAR2 DEFAULT NULL,
                                   p_conds_account_name      VARCHAR2 DEFAULT NULL,
                                   p_bp_virtual_bank_account VARCHAR2 DEFAULT NULL,
                                   --end
                                   p_return_id NUMBER DEFAULT NULL,
                                   p_zuonr     VARCHAR2 DEFAULT NULL,
                                   --add by sf  
                                   p_paid_byother_flag IN VARCHAR2,
                                   p_ref_v05           IN VARCHAR2,
                                   p_status            IN VARCHAR2 DEFAULT NULL
                                   --end;
                                   ) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    v_transaction_num     csh_transaction.transaction_num%TYPE;
  
    e_trx_num_error EXCEPTION;
    v_csh_transaction_id NUMBER;
    -- add by sf
    v_exit_flag NUMBER := 0;
    e_slip_num_already_exist EXCEPTION;
    --end
  BEGIN
    --判断插入的状态是否zhengqur
    -- if p_status is null then return; end if;
    --add by sf  
    --检查流水号的唯一性
    SELECT COUNT(1)
      INTO v_exit_flag
      FROM csh_transaction
     WHERE bank_slip_num = p_bank_slip_num
       AND transaction_category = 'BUSINESS'
       AND transaction_type = 'RECEIPT';
    IF v_exit_flag != 0 AND p_transaction_type = 'RECEIPT' AND
       p_transaction_category = 'BUSINESS' THEN
      RAISE e_slip_num_already_exist;
    END IF;
    --end;
    SELECT csh_transaction_s.nextval INTO v_csh_transaction_id FROM dual;
    v_csh_transaction_rec.transaction_id := v_csh_transaction_id;
    IF p_transaction_num IS NULL THEN
      v_transaction_num := get_csh_transaction_num(p_transaction_type => p_transaction_type,
                                                   p_company_id       => p_company_id,
                                                   p_transaction_date => p_transaction_date,
                                                   p_user_id          => p_user_id);
    ELSE
      v_transaction_num := p_transaction_num;
    END IF;
  
    v_csh_transaction_rec.transaction_num         := v_transaction_num;
    v_csh_transaction_rec.transaction_category    := p_transaction_category;
    v_csh_transaction_rec.transaction_type        := p_transaction_type;
    v_csh_transaction_rec.transaction_date        := p_transaction_date;
    v_csh_transaction_rec.penalty_calc_date       := p_penalty_calc_date;
    v_csh_transaction_rec.bank_slip_num           := p_bank_slip_num;
    v_csh_transaction_rec.company_id              := p_company_id;
    v_csh_transaction_rec.internal_period_num     := p_internal_period_num;
    v_csh_transaction_rec.period_name             := p_period_name;
    v_csh_transaction_rec.payment_method_id       := p_payment_method_id;
    v_csh_transaction_rec.distribution_set_id     := p_distribution_set_id;
    v_csh_transaction_rec.cashflow_amount         := p_cashflow_amount;
    v_csh_transaction_rec.currency_code           := p_currency_code;
    v_csh_transaction_rec.transaction_amount      := p_transaction_amount;
    v_csh_transaction_rec.exchange_rate_type      := p_exchange_rate_type;
    v_csh_transaction_rec.exchange_rate_quotation := p_exchange_rate_quotation;
    v_csh_transaction_rec.exchange_rate           := p_exchange_rate;
    v_csh_transaction_rec.bank_account_id         := p_bank_account_id;
    v_csh_transaction_rec.bp_category             := p_bp_category;
    v_csh_transaction_rec.bp_id                   := p_bp_id;
    v_csh_transaction_rec.bp_bank_account_id      := p_bp_bank_account_id;
    v_csh_transaction_rec.bp_bank_account_num     := p_bp_bank_account_num;
    v_csh_transaction_rec.bp_bank_account_name    := p_bp_bank_account_name;
    v_csh_transaction_rec.description             := p_description;
    v_csh_transaction_rec.handling_charge         := p_handling_charge;
    v_csh_transaction_rec.posted_flag             := nvl(p_posted_flag, 'N');
    v_csh_transaction_rec.reversed_flag           := nvl(p_reversed_flag,
                                                         csh_transaction_pkg.csh_trx_reversed_flag_n);
    v_csh_transaction_rec.reversed_date           := p_reversed_date;
    v_csh_transaction_rec.returned_flag           := nvl(p_returned_flag,
                                                         csh_transaction_pkg.csh_trx_return_flag_n);
    v_csh_transaction_rec.returned_amount         := p_returned_amount;
    v_csh_transaction_rec.write_off_flag          := nvl(p_write_off_flag,
                                                         csh_transaction_pkg.csh_trx_write_off_flag_n);
    v_csh_transaction_rec.write_off_amount        := p_write_off_amount;
    v_csh_transaction_rec.full_write_off_date     := p_full_write_off_date;
    v_csh_transaction_rec.twin_csh_trx_id         := p_twin_csh_trx_id;
    v_csh_transaction_rec.return_from_csh_trx_id  := p_return_from_csh_trx_id;
    v_csh_transaction_rec.reversed_csh_trx_id     := p_reversed_csh_trx_id;
    v_csh_transaction_rec.source_csh_trx_type     := p_source_csh_trx_type;
    v_csh_transaction_rec.source_doc_category     := p_source_doc_category;
    v_csh_transaction_rec.source_csh_trx_id       := p_source_csh_trx_id;
    v_csh_transaction_rec.source_doc_type         := p_source_doc_type;
    v_csh_transaction_rec.source_doc_id           := p_source_doc_id;
    v_csh_transaction_rec.source_doc_line_id      := p_source_doc_line_id;
    v_csh_transaction_rec.create_je_mothed        := p_create_je_mothed;
    v_csh_transaction_rec.create_je_flag          := nvl(p_create_je_flag,
                                                         'N');
    v_csh_transaction_rec.gld_interface_flag      := nvl(p_gld_interface_flag,
                                                         'N');
    v_csh_transaction_rec.creation_date           := SYSDATE;
    v_csh_transaction_rec.created_by              := p_user_id;
    v_csh_transaction_rec.last_update_date        := SYSDATE;
    v_csh_transaction_rec.last_updated_by         := p_user_id;
    v_csh_transaction_rec.csh_bp_name             := p_csh_bp_name;
    v_csh_transaction_rec.receipt_type            := p_receipt_type;
    /*add by xuls for ref_contract_id 2014-9-26
     用于增加记录收款合同号
    */
    v_csh_transaction_rec.ref_contract_id := p_ref_contract_id;
    v_csh_transaction_rec.ref_n01         := p_ref_n01;
  
    --add by chenlingfeng
    v_csh_transaction_rec.bp_bank_name        := p_bp_bank_name;
    v_csh_transaction_rec.bp_bank_branch_name := p_bp_bank_branch_name;
    v_csh_transaction_rec.collection_classes  := p_collection_classes;
    v_csh_transaction_rec.review_status       := 'NOT';
  
    v_csh_transaction_rec.opposite_band_na := p_opposite_band_na;
    v_csh_transaction_rec.banka            := p_banka;
    v_csh_transaction_rec.purpose          := p_purpose;
    v_csh_transaction_rec.gsber            := p_gsber;
    v_csh_transaction_rec.sus_belnr        := p_sus_belnr;
  
    v_csh_transaction_rec.assigns_contract_id := p_assigns_contract_id;
  
    v_csh_transaction_rec.conds_account_num       := p_conds_account_num;
    v_csh_transaction_rec.conds_account_name      := p_conds_account_name;
    v_csh_transaction_rec.bp_virtual_bank_account := p_bp_virtual_bank_account;
    --end
    v_csh_transaction_rec.zuonr := p_zuonr;
    -- add by sf 增加代付以及备注字段
    v_csh_transaction_rec.ref_v05           := p_ref_v05;
    v_csh_transaction_rec.paid_byother_flag := p_paid_byother_flag;
    v_csh_transaction_rec.confirmed_flag    := 'NEW';
    v_csh_transaction_rec.write_off_flag    := 'DISABLED';
    --将退款金额置为0
    v_csh_transaction_rec.returned_amount := 0;
    --end;
    insert_csh_transaction(v_csh_transaction_rec);
  
    --add by Harry 9952 2018/11/13
    IF p_return_id IS NOT NULL THEN
      UPDATE csh_transaction_return r
         SET r.return_transaction_id = v_csh_transaction_id
       WHERE r.return_id = p_return_id;
    END IF;
  
    check_zero_amount(p_csh_transaction_rec => v_csh_transaction_rec,
                      p_user_id             => p_user_id);
  
    --针对界面保存并过账按钮处理
    IF nvl(p_posted_flag, csh_transaction_pkg.csh_trx_posted_flag_no) =
       csh_transaction_pkg.csh_trx_posted_flag_yes THEN
      post_csh_transaction(p_transaction_id => v_csh_transaction_rec.transaction_id,
                           p_user_id        => p_user_id);
    END IF;
    p_transaction_id := v_csh_transaction_rec.transaction_id;
  EXCEPTION
    --add by shen--
    WHEN e_slip_num_already_exist THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'E_SLIP_NUM_ALREADY_EXIST',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'save_csh_transaction_data');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
      --end;
    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_transaction_pkg',
                                                     p_procedure_function_name => 'insert_csh_transaction');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  END;
  /*add by xuls for ref_contract_id 2014-9-26
   用于增加记录收款合同号
  */
  PROCEDURE update_csh_transaction(p_transaction_id          csh_transaction.transaction_id%TYPE,
                                   p_transaction_num         csh_transaction.transaction_num%TYPE,
                                   p_transaction_category    csh_transaction.transaction_category%TYPE,
                                   p_transaction_type        csh_transaction.transaction_type%TYPE,
                                   p_transaction_date        csh_transaction.transaction_date%TYPE,
                                   p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                   p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                   p_company_id              csh_transaction.company_id%TYPE,
                                   p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                   p_period_name             csh_transaction.period_name%TYPE,
                                   p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                   p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                   p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                   p_currency_code           csh_transaction.currency_code%TYPE,
                                   p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                   p_bp_id                   csh_transaction.bp_id%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_description             csh_transaction.description%TYPE,
                                   p_handling_charge         csh_transaction.handling_charge%TYPE,
                                   p_posted_flag             csh_transaction.posted_flag%TYPE,
                                   p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                   p_reversed_date           csh_transaction.reversed_date%TYPE,
                                   p_returned_flag           csh_transaction.returned_flag%TYPE,
                                   p_returned_amount         csh_transaction.returned_amount%TYPE,
                                   p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                   p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                   p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                   p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                   p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                   p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                   p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                   p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                   p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                   p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                   p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                   p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                   p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                   p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                   p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                   p_user_id                 csh_transaction.created_by%TYPE,
                                   p_ref_contract_id         csh_transaction.ref_contract_id%TYPE DEFAULT NULL,
                                   p_ref_n01                 NUMBER DEFAULT NULL,
                                   p_bp_bank_name            VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_branch_name     VARCHAR2 DEFAULT NULL,
                                   p_collection_classes      VARCHAR2 DEFAULT NULL,
                                   p_bp_bank_account_name    VARCHAR2 DEFAULT NULL,
                                   p_opposite_band_na        VARCHAR2 DEFAULT NULL,
                                   p_banka                   VARCHAR2 DEFAULT NULL,
                                   p_purpose                 VARCHAR2 DEFAULT NULL,
                                   p_gsber                   VARCHAR2 DEFAULT NULL,
                                   p_sus_belnr               VARCHAR2 DEFAULT NULL,
                                   p_bp_virtual_bank_account VARCHAR2 DEFAULT NULL) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    e_status_error           EXCEPTION;
    e_transaction_type_error EXCEPTION;
  BEGIN
  
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    IF NOT is_trx_update(v_csh_transaction_rec) THEN
      RAISE e_status_error;
    END IF;
  
    IF v_csh_transaction_rec.transaction_type <>
       csh_transaction_pkg.csh_trx_type_receipt THEN
      RAISE e_transaction_type_error;
    END IF;
  
    v_csh_transaction_rec.transaction_amount := p_transaction_amount;
    v_csh_transaction_rec.bank_account_id    := p_bank_account_id;
  
    check_zero_amount(p_csh_transaction_rec => v_csh_transaction_rec,
                      p_user_id             => p_user_id);
  
    UPDATE csh_transaction t1
       SET t1.transaction_category    = p_transaction_category,
           t1.transaction_type        = p_transaction_type,
           t1.transaction_date        = p_transaction_date,
           t1.penalty_calc_date       = p_penalty_calc_date,
           t1.bank_slip_num           = p_bank_slip_num,
           t1.company_id              = p_company_id,
           t1.internal_period_num     = p_internal_period_num,
           t1.period_name             = p_period_name,
           t1.payment_method_id       = p_payment_method_id,
           t1.distribution_set_id     = p_distribution_set_id,
           t1.cashflow_amount         = p_cashflow_amount,
           t1.currency_code           = p_currency_code,
           t1.transaction_amount      = p_transaction_amount,
           t1.exchange_rate_type      = p_exchange_rate_type,
           t1.exchange_rate_quotation = p_exchange_rate_quotation,
           t1.exchange_rate           = p_exchange_rate,
           t1.bank_account_id         = p_bank_account_id,
           t1.bp_category             = p_bp_category,
           t1.bp_id                   = p_bp_id,
           t1.bp_bank_account_id      = p_bp_bank_account_id,
           t1.bp_bank_account_num     = p_bp_bank_account_num,
           t1.description             = p_description,
           t1.handling_charge         = p_handling_charge,
           t1.posted_flag             = csh_transaction_pkg.csh_trx_posted_flag_no,
           t1.reversed_flag           = nvl(p_reversed_flag,
                                            csh_transaction_pkg.csh_trx_reversed_flag_n),
           t1.reversed_date           = p_reversed_date,
           t1.returned_flag           = nvl(p_returned_flag,
                                            csh_transaction_pkg.csh_trx_return_flag_n),
           t1.returned_amount         = p_returned_amount,
           t1.write_off_flag          = nvl(p_write_off_flag,
                                            csh_transaction_pkg.csh_trx_write_off_flag_n),
           t1.write_off_amount        = p_write_off_amount,
           t1.full_write_off_date     = p_full_write_off_date,
           t1.twin_csh_trx_id         = p_twin_csh_trx_id,
           t1.return_from_csh_trx_id  = p_return_from_csh_trx_id,
           t1.reversed_csh_trx_id     = p_reversed_csh_trx_id,
           t1.source_csh_trx_type     = p_source_csh_trx_type,
           t1.source_csh_trx_id       = p_source_csh_trx_id,
           t1.source_doc_category     = p_source_doc_category,
           t1.source_doc_type         = p_source_doc_type,
           t1.source_doc_id           = p_source_doc_id,
           t1.source_doc_line_id      = p_source_doc_line_id,
           t1.create_je_mothed        = p_create_je_mothed,
           t1.create_je_flag          = nvl(p_create_je_flag, 'N'),
           t1.gld_interface_flag      = nvl(p_gld_interface_flag, 'N'),
           t1.last_update_date        = SYSDATE,
           t1.last_updated_by         = p_user_id,
           t1.ref_contract_id         = p_ref_contract_id,
           t1.ref_n01                 = p_ref_n01,
           --add by chenlingfeng
           t1.bp_bank_name            = p_bp_bank_name,
           t1.bp_bank_branch_name     = p_bp_bank_branch_name,
           t1.bp_bank_account_name    = p_bp_bank_account_name,
           t1.collection_classes      = p_collection_classes,
           t1.opposite_band_na        = p_opposite_band_na,
           t1.banka                   = p_banka,
           t1.purpose                 = p_purpose,
           t1.gsber                   = p_gsber,
           t1.sus_belnr               = p_sus_belnr,
           t1.bp_virtual_bank_account = p_bp_virtual_bank_account
    --end
     WHERE t1.transaction_id = p_transaction_id;
    --针对界面保存并过账按钮处理
    IF nvl(p_posted_flag, csh_transaction_pkg.csh_trx_posted_flag_no) =
       csh_transaction_pkg.csh_trx_posted_flag_yes THEN
      post_csh_transaction(p_transaction_id => v_csh_transaction_rec.transaction_id,
                           p_user_id        => p_user_id);
    END IF;
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_UPDATE_STATUS_CHECK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'update_status_check');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    WHEN e_transaction_type_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_TRANSACTION_TYPE_CHECK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'update_status_check');
      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_transaction_pkg',
                                                     p_procedure_function_name => 'update_csh_transaction');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --货币兑换维护
  PROCEDURE update_currency_exchange_trx(p_transaction_id          csh_transaction.transaction_id%TYPE,
                                         p_transaction_num         csh_transaction.transaction_num%TYPE,
                                         p_transaction_category    csh_transaction.transaction_category%TYPE,
                                         p_transaction_type        csh_transaction.transaction_type%TYPE,
                                         p_transaction_date        csh_transaction.transaction_date%TYPE,
                                         p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                         p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                         p_company_id              csh_transaction.company_id%TYPE,
                                         p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                         p_period_name             csh_transaction.period_name%TYPE,
                                         p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                         p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                         p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                         p_currency_code           csh_transaction.currency_code%TYPE,
                                         p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                         p_bp_id                   csh_transaction.bp_id%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_description             csh_transaction.description%TYPE,
                                         p_handling_charge         csh_transaction.handling_charge%TYPE,
                                         p_posted_flag             csh_transaction.posted_flag%TYPE,
                                         p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                         p_reversed_date           csh_transaction.reversed_date%TYPE,
                                         p_returned_flag           csh_transaction.returned_flag%TYPE,
                                         p_returned_amount         csh_transaction.returned_amount%TYPE,
                                         p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                         p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                         p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                         p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                         p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                         p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                         p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                         p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                         p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                         p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                         p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                         p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                         p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                         p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                         p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                         p_user_id                 csh_transaction.created_by%TYPE) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    e_status_error EXCEPTION;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    IF NOT is_trx_update(v_csh_transaction_rec) THEN
      RAISE e_status_error;
    END IF;
  
    v_csh_transaction_rec.transaction_amount := p_transaction_amount;
    v_csh_transaction_rec.bank_account_id    := p_bank_account_id;
  
    check_zero_amount(p_csh_transaction_rec => v_csh_transaction_rec,
                      p_user_id             => p_user_id);
  
    UPDATE csh_transaction t1
       SET t1.transaction_category    = p_transaction_category,
           t1.transaction_type        = p_transaction_type,
           t1.transaction_date        = p_transaction_date,
           t1.penalty_calc_date       = p_penalty_calc_date,
           t1.bank_slip_num           = p_bank_slip_num,
           t1.company_id              = p_company_id,
           t1.internal_period_num     = p_internal_period_num,
           t1.period_name             = p_period_name,
           t1.payment_method_id       = p_payment_method_id,
           t1.distribution_set_id     = p_distribution_set_id,
           t1.cashflow_amount         = p_cashflow_amount,
           t1.currency_code           = p_currency_code,
           t1.transaction_amount      = p_transaction_amount,
           t1.exchange_rate_type      = p_exchange_rate_type,
           t1.exchange_rate_quotation = p_exchange_rate_quotation,
           t1.exchange_rate           = p_exchange_rate,
           t1.bank_account_id         = p_bank_account_id,
           t1.bp_category             = p_bp_category,
           t1.bp_id                   = p_bp_id,
           t1.bp_bank_account_id      = p_bp_bank_account_id,
           t1.bp_bank_account_num     = p_bp_bank_account_num,
           t1.description             = p_description,
           t1.handling_charge         = p_handling_charge,
           t1.posted_flag             = csh_transaction_pkg.csh_trx_posted_flag_no,
           t1.reversed_flag           = nvl(p_reversed_flag,
                                            csh_transaction_pkg.csh_trx_reversed_flag_n),
           t1.reversed_date           = p_reversed_date,
           t1.returned_flag           = nvl(p_returned_flag,
                                            csh_transaction_pkg.csh_trx_return_flag_n),
           t1.returned_amount         = p_returned_amount,
           t1.write_off_flag          = nvl(p_write_off_flag,
                                            csh_transaction_pkg.csh_trx_write_off_flag_n),
           t1.write_off_amount        = p_write_off_amount,
           t1.full_write_off_date     = p_full_write_off_date,
           t1.twin_csh_trx_id         = p_twin_csh_trx_id,
           t1.return_from_csh_trx_id  = p_return_from_csh_trx_id,
           t1.reversed_csh_trx_id     = p_reversed_csh_trx_id,
           t1.source_csh_trx_type     = p_source_csh_trx_type,
           t1.source_csh_trx_id       = p_source_csh_trx_id,
           t1.source_doc_category     = p_source_doc_category,
           t1.source_doc_type         = p_source_doc_type,
           t1.source_doc_id           = p_source_doc_id,
           t1.source_doc_line_id      = p_source_doc_line_id,
           t1.create_je_mothed        = p_create_je_mothed,
           t1.create_je_flag          = nvl(p_create_je_flag, 'N'),
           t1.gld_interface_flag      = nvl(p_gld_interface_flag, 'N'),
           t1.last_update_date        = SYSDATE,
           t1.last_updated_by         = p_user_id
     WHERE t1.transaction_id = p_transaction_id;
    --针对界面保存并过账按钮处理
    IF nvl(p_posted_flag, csh_transaction_pkg.csh_trx_posted_flag_no) =
       csh_transaction_pkg.csh_trx_posted_flag_yes THEN
      post_csh_transaction(p_transaction_id => v_csh_transaction_rec.transaction_id,
                           p_user_id        => p_user_id);
    END IF;
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_UPDATE_STATUS_CHECK',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'update_currency_exchange_trx');
      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_transaction_pkg',
                                                     p_procedure_function_name => 'update_currency_exchange_trx');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --货币兑换互记TWIN_CSH_TRX_ID
  PROCEDURE save_twin_csh_trx_id(p_transaction_num csh_transaction.transaction_num%TYPE,
                                 p_user_id         NUMBER) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
  BEGIN
    FOR r_trx_rec IN (SELECT d.transaction_id
                        FROM csh_transaction d
                       WHERE d.transaction_num = p_transaction_num) LOOP
      lock_csh_transaction(p_transaction_id      => r_trx_rec.transaction_id,
                           p_user_id             => p_user_id,
                           p_csh_transaction_rec => v_csh_transaction_rec);
      UPDATE csh_transaction c
         SET c.twin_csh_trx_id  = r_trx_rec.transaction_id,
             c.last_updated_by  = p_user_id,
             c.last_update_date = SYSDATE
       WHERE c.transaction_num = p_transaction_num
         AND c.transaction_id <> r_trx_rec.transaction_id;
    END LOOP;
  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_transaction_pkg',
                                                     p_procedure_function_name => 'save_twin_csh_trx_id');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --货币兑换过账以后维护
  PROCEDURE upd_currency_trx_after_post(p_transaction_id csh_transaction.transaction_id%TYPE,
                                        p_bank_slip_num  csh_transaction.bank_slip_num%TYPE,
                                        p_description    csh_transaction.description%TYPE,
                                        p_user_id        csh_transaction.created_by%TYPE) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    e_flag_error EXCEPTION;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    IF NOT is_trx_update_after_post(v_csh_transaction_rec) THEN
      RAISE e_flag_error;
    END IF;
  
    UPDATE csh_transaction t
       SET t.bank_slip_num    = p_bank_slip_num,
           t.description      = p_description,
           t.last_update_date = SYSDATE,
           t.last_updated_by  = p_user_id
     WHERE t.transaction_id = p_transaction_id;
  EXCEPTION
    WHEN e_flag_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => v_err_code,
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'upd_currency_trx_after_post');
      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_transaction_pkg',
                                                     p_procedure_function_name => 'upd_currency_trx_after_post');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  --过账以后维护
  PROCEDURE update_csh_transaction_post(p_transaction_id     csh_transaction.transaction_id%TYPE,
                                        p_bank_slip_num      csh_transaction.bank_slip_num%TYPE,
                                        p_bp_bank_account_id csh_transaction.bp_bank_account_id%TYPE,
                                        p_user_id            csh_transaction.created_by%TYPE) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
  
    e_flag_error EXCEPTION;
  BEGIN
  
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    /*IF NOT is_trx_update_after_post(v_csh_transaction_rec) THEN
      RAISE e_flag_error;
    END IF;*/
  
    UPDATE csh_transaction t
       SET t.bank_slip_num      = p_bank_slip_num,
           t.bp_bank_account_id = p_bp_bank_account_id,
           t.last_update_date   = SYSDATE,
           t.last_updated_by    = p_user_id
     WHERE t.transaction_id = p_transaction_id;
  
  EXCEPTION
    WHEN e_flag_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => v_err_code,
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'update_status_check');
      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_transaction_pkg',
                                                     p_procedure_function_name => 'update_csh_transaction_post');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  PROCEDURE update_csh_post_flag(p_transaction_id NUMBER,
                                 p_user_id        NUMBER,
                                 p_claim          VARCHAR2 DEFAULT NULL) IS
  BEGIN
    /*update csh_transaction ct
      set ct.posted_flag = 'Y', ct.last_updated_by = p_user_id
    where ct.transaction_id = p_transaction_id;*/
    IF p_claim IS NULL THEN
      UPDATE csh_transaction ct
         SET ct.posted_flag     = 'Y',
             ct.posted_date     = trunc(SYSDATE),
             ct.last_updated_by = p_user_id
       WHERE ct.transaction_id = p_transaction_id;
    ELSIF p_claim = 'F' THEN
      UPDATE csh_transaction ct
         SET ct.posted_flag = 'F', ct.last_updated_by = p_user_id
       WHERE ct.transaction_id = p_transaction_id;
    ELSIF p_claim = 'Y' THEN
      UPDATE csh_transaction ct
         SET ct.posted_flag     = 'Y',
             ct.posted_date     = trunc(SYSDATE),
             ct.last_updated_by = p_user_id
       WHERE ct.transaction_id = p_transaction_id;
    END IF;
  END;

  PROCEDURE delete_csh_transaction(p_transaction_id csh_transaction.transaction_id%TYPE,
                                   p_user_id        csh_transaction.created_by%TYPE) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
  
    e_status_error EXCEPTION;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
  
    IF NOT is_trx_delete(v_csh_transaction_rec) THEN
      RAISE e_status_error;
    END IF;
  
    DELETE FROM csh_transaction t
     WHERE t.transaction_id = p_transaction_id;
  
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => v_err_code,
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'delete_status_check',
                                                      p_token_1                 => '#TRANSACTION_NUM',
                                                      p_token_value_1           => v_csh_transaction_rec.transaction_num);
      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_transaction_pkg',
                                                     p_procedure_function_name => 'delete_csh_transaction');
    
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  PROCEDURE post_csh_transaction(p_transaction_id csh_transaction.transaction_id%TYPE,
                                 p_user_id        csh_transaction.created_by%TYPE) IS
    v_csh_transaction_rec csh_transaction%ROWTYPE;
  
    e_status_error EXCEPTION;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
  
    /*IF NOT is_trx_post(v_csh_transaction_rec) THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH510_POSTED_ERROR';
      RAISE e_status_error;
    END IF;*/
    IF v_csh_transaction_rec.bp_virtual_bank_account IS NOT NULL THEN
      UPDATE csh_transaction t
         SET t.bp_bank_account_num     = v_csh_transaction_rec.bp_virtual_bank_account,
             t.bp_virtual_bank_account = v_csh_transaction_rec.bp_bank_account_num
       WHERE t.transaction_id = v_csh_transaction_rec.transaction_id;
    
    END IF;
    /*UPDATE csh_transaction t
       SET t.posted_flag      = csh_transaction_pkg.csh_trx_posted_flag_yes,
           t.POSTED_DATE      = trunc(SYSDATE),
           t.last_update_date = SYSDATE,
           t.last_updated_by  = p_user_id
     WHERE t.transaction_id = v_csh_transaction_rec.transaction_id;
    --现金事务过账生成的凭证
    csh_transaction_je_pkg.create_transaction_je(p_transaction_id => p_transaction_id,
                                                 p_user_id        => p_user_id);*/
  
    csh_transaction_custom_pkg.after_post_trx(p_transaction_id => p_transaction_id,
                                              p_user_id        => p_user_id);
  EXCEPTION
    WHEN e_status_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => v_err_code,
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'post_csh_transaction',
                                                      p_token_1                 => '#TRANSACTION_NUM',
                                                      p_token_value_1           => v_csh_transaction_rec.transaction_num);
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --通用的反冲入口(收款反冲,付款反冲,收款退款反冲,预收款退款反冲)
  PROCEDURE reverse_csh_transaction(p_transaction_id         csh_transaction.transaction_id%TYPE,
                                    p_reversed_date          csh_transaction.reversed_date%TYPE,
                                    p_description            csh_transaction.description%TYPE,
                                    p_user_id                csh_transaction.created_by%TYPE,
                                    p_reverse_transaction_id OUT csh_transaction.transaction_id%TYPE) IS
  
    v_period_name         VARCHAR2(30);
    v_transaction_id      NUMBER;
    v_write_off_id        NUMBER;
    v_internal_period_num gld_periods.internal_period_num%TYPE;
  
    v_csh_transaction_rec csh_transaction%ROWTYPE;
  
    e_reverse_error EXCEPTION;
  BEGIN
  
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
  
    IF NOT is_trx_reverse(v_csh_transaction_rec) THEN
      RAISE e_reverse_error;
    END IF;
  
    --校验反冲日期  modify by zhuxianfei 20140224
    IF trunc(p_reversed_date) <
       trunc(v_csh_transaction_rec.transaction_date) THEN
      v_err_code := 'CSH_TRANSACTION_PKG.REVERSE_DATE_ERROR';
      RAISE e_reverse_error;
    END IF;
    -- 期间
    v_period_name := gld_common_pkg.get_gld_period_name(p_company_id => v_csh_transaction_rec.company_id,
                                                        p_date       => p_reversed_date);
    IF v_period_name IS NULL THEN
      v_err_code := 'CSH_TRANSACTION_PKG.REVERSE_PERIOD_ERROR';
      RAISE e_reverse_error;
    END IF;
    v_internal_period_num := gld_common_pkg.get_gld_internal_period_num(p_company_id  => v_csh_transaction_rec.company_id,
                                                                        p_period_name => v_period_name);
    IF v_internal_period_num IS NULL THEN
      v_err_code := 'CSH_TRANSACTION_PKG.REVERSE_PERIOD_NUM_ERROR';
      RAISE e_reverse_error;
    END IF;
    --预收款和收款的退款反冲
    IF v_csh_transaction_rec.transaction_category =
       csh_trx_category_business AND
       (v_csh_transaction_rec.transaction_type = csh_trx_type_prereceipt OR
       nvl(v_csh_transaction_rec.returned_flag, csh_trx_return_flag_n) =
       csh_trx_return_flag_return) THEN
      --退款反冲后更新原退款现金事务信息
      IF v_csh_transaction_rec.returned_flag = csh_trx_return_flag_return THEN
        upd_rtn_csh_trx_after_reverse(p_transaction_id => p_transaction_id,
                                      p_user_id        => p_user_id);
      END IF;
      FOR csh_write_off_rec IN (SELECT *
                                  FROM csh_write_off t
                                 WHERE t.subsequent_csh_trx_id =
                                       p_transaction_id
                                   AND nvl(t.reversed_flag,
                                           csh_trx_reversed_flag_n) =
                                       csh_trx_reversed_flag_n) LOOP
        csh_write_off_pkg.reverse_write_off(p_reverse_write_off_id => v_write_off_id,
                                            p_write_off_id         => csh_write_off_rec.write_off_id,
                                            p_reversed_date        => p_reversed_date,
                                            p_description          => p_description,
                                            p_user_id              => p_user_id,
                                            p_from_csh_trx_flag    => 'Y');
      END LOOP;
      --付款反冲中的支付反冲
    ELSIF v_csh_transaction_rec.transaction_category =
          csh_trx_category_business AND
          v_csh_transaction_rec.transaction_type = csh_trx_type_payment THEN
      --更新付款申请
      csh_payment_req_pkg.upd_csh_payment_after_reverse(p_transaction_id => p_transaction_id,
                                                        p_user_id        => p_user_id);
      FOR csh_write_off_rec IN (SELECT *
                                  FROM csh_write_off t
                                 WHERE t.csh_transaction_id =
                                       p_transaction_id
                                   AND nvl(t.reversed_flag,
                                           csh_trx_reversed_flag_n) =
                                       csh_trx_reversed_flag_n) LOOP
        csh_write_off_pkg.reverse_write_off(p_reverse_write_off_id => v_write_off_id,
                                            p_write_off_id         => csh_write_off_rec.write_off_id,
                                            p_reversed_date        => p_reversed_date,
                                            p_description          => p_description,
                                            p_user_id              => p_user_id,
                                            p_from_csh_trx_flag    => 'Y');
      END LOOP;
    ELSIF v_csh_transaction_rec.transaction_category =
          csh_trx_category_business AND
          v_csh_transaction_rec.transaction_type = csh_trx_type_deduction THEN
    
      FOR csh_write_off_rec IN (SELECT *
                                  FROM csh_write_off t
                                 WHERE t.csh_transaction_id =
                                       p_transaction_id
                                   AND nvl(t.reversed_flag,
                                           csh_trx_reversed_flag_n) =
                                       csh_trx_reversed_flag_n) LOOP
        csh_write_off_pkg.reverse_write_off(p_reverse_write_off_id => v_write_off_id,
                                            p_write_off_id         => csh_write_off_rec.write_off_id,
                                            p_reversed_date        => p_reversed_date,
                                            p_description          => p_description,
                                            p_user_id              => p_user_id,
                                            p_from_csh_trx_flag    => 'Y');
      END LOOP;
    END IF;
  
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
  
    insert_csh_transaction(p_transaction_id          => v_transaction_id,
                           p_transaction_num         => '',
                           p_transaction_category    => v_csh_transaction_rec.transaction_category,
                           p_transaction_type        => v_csh_transaction_rec.transaction_type,
                           p_transaction_date        => p_reversed_date,
                           p_penalty_calc_date       => v_csh_transaction_rec.penalty_calc_date,
                           p_bank_slip_num           => v_csh_transaction_rec.bank_slip_num,
                           p_company_id              => v_csh_transaction_rec.company_id,
                           p_internal_period_num     => v_internal_period_num,
                           p_period_name             => v_period_name,
                           p_payment_method_id       => v_csh_transaction_rec.payment_method_id,
                           p_distribution_set_id     => v_csh_transaction_rec.distribution_set_id,
                           p_cashflow_amount         => -1 *
                                                        v_csh_transaction_rec.cashflow_amount,
                           p_currency_code           => v_csh_transaction_rec.currency_code,
                           p_transaction_amount      => -1 *
                                                        v_csh_transaction_rec.transaction_amount,
                           p_exchange_rate_type      => v_csh_transaction_rec.exchange_rate_type,
                           p_exchange_rate_quotation => v_csh_transaction_rec.exchange_rate_quotation,
                           p_exchange_rate           => v_csh_transaction_rec.exchange_rate,
                           p_bank_account_id         => v_csh_transaction_rec.bank_account_id,
                           p_bp_category             => v_csh_transaction_rec.bp_category,
                           p_bp_id                   => v_csh_transaction_rec.bp_id,
                           p_bp_bank_account_id      => v_csh_transaction_rec.bp_bank_account_id,
                           p_bp_bank_account_num     => v_csh_transaction_rec.bp_bank_account_num,
                           p_description             => p_description,
                           p_handling_charge         => v_csh_transaction_rec.handling_charge,
                           p_posted_flag             => csh_transaction_pkg.csh_trx_posted_flag_yes,
                           p_reversed_flag           => csh_transaction_pkg.csh_trx_reversed_flag_r,
                           p_reversed_date           => '',
                           p_returned_flag           => v_csh_transaction_rec.returned_flag,
                           p_returned_amount         => v_csh_transaction_rec.returned_amount,
                           p_write_off_flag          => v_csh_transaction_rec.write_off_flag,
                           p_write_off_amount        => v_csh_transaction_rec.write_off_amount,
                           p_full_write_off_date     => v_csh_transaction_rec.full_write_off_date,
                           p_twin_csh_trx_id         => v_csh_transaction_rec.twin_csh_trx_id,
                           p_return_from_csh_trx_id  => v_csh_transaction_rec.return_from_csh_trx_id,
                           p_reversed_csh_trx_id     => p_transaction_id,
                           p_source_csh_trx_type     => v_csh_transaction_rec.source_csh_trx_type,
                           p_source_csh_trx_id       => v_csh_transaction_rec.source_csh_trx_id,
                           p_source_doc_category     => v_csh_transaction_rec.source_doc_category,
                           p_source_doc_type         => v_csh_transaction_rec.source_doc_type,
                           p_source_doc_id           => v_csh_transaction_rec.source_doc_id,
                           p_source_doc_line_id      => v_csh_transaction_rec.source_doc_line_id,
                           p_create_je_mothed        => v_csh_transaction_rec.create_je_mothed,
                           p_create_je_flag          => 'N',
                           p_gld_interface_flag      => 'N',
                           p_user_id                 => p_user_id,
                           p_csh_bp_name             => v_csh_transaction_rec.csh_bp_name,
                           --add by sf  
                           p_paid_byother_flag => v_csh_transaction_rec.paid_byother_flag,
                           p_ref_v05           => v_csh_transaction_rec.ref_v05
                           --end;
                           );
    --更新被反冲现金事务
    update_csh_trx_after_reverse(p_transaction_id      => p_transaction_id,
                                 p_reversed_date       => p_reversed_date,
                                 p_reserved_csh_trx_id => v_transaction_id,
                                 p_user_id             => p_user_id);
    --生成现金事务的过账反冲凭证
    csh_transaction_je_pkg.create_transaction_reverse_je(p_transaction_id => v_transaction_id,
                                                         p_user_id        => p_user_id);
    p_reverse_transaction_id := v_transaction_id;
  EXCEPTION
    WHEN e_reverse_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => v_err_code,
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'reverse_csh_transaction',
                                                      p_token_1                 => '#TRANSACTION_NUM',
                                                      p_token_value_1           => v_csh_transaction_rec.transaction_num);
      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_transaction_pkg',
                                                     p_procedure_function_name => 'reverse_csh_transaction');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --收款反冲入口
  PROCEDURE reverse_receipt_csh_trx(p_transaction_id csh_transaction.transaction_id%TYPE,
                                    p_reversed_date  csh_transaction.reversed_date%TYPE,
                                    p_bank_slip_num  csh_transaction.bank_slip_num%TYPE,
                                    p_description    csh_transaction.description%TYPE,
                                    p_user_id        csh_transaction.created_by%TYPE) IS
    e_reverse_error EXCEPTION;
    v_csh_transaction_rec csh_transaction%ROWTYPE;
    v_transaction_id      csh_transaction.transaction_id%TYPE;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
  
    reverse_csh_transaction(p_transaction_id         => p_transaction_id,
                            p_reversed_date          => p_reversed_date,
                            p_description            => p_description,
                            p_user_id                => p_user_id,
                            p_reverse_transaction_id => v_transaction_id);
    --更新银行水单
    update_bank_slip_num(p_transaction_id => v_transaction_id,
                         p_bank_slip_num  => p_bank_slip_num,
                         p_user_id        => p_user_id);
  
    -- 反冲回来 过账标志变为N
    UPDATE csh_transaction ct
       SET ct.posted_flag   = 'N',
           ct.posted_date   = NULL,
           ct.reversed_flag = 'N'
     WHERE ct.transaction_id = p_transaction_id;
  
  EXCEPTION
    WHEN e_reverse_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => v_err_code,
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'reverse_receipt_csh_trx',
                                                      p_token_1                 => '#TRANSACTION_NUM',
                                                      p_token_value_1           => v_csh_transaction_rec.transaction_num);
      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_transaction_pkg',
                                                     p_procedure_function_name => 'reverse_receipt_csh_trx');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --收款退款入口
  PROCEDURE return_csh_transaction(p_transaction_id      csh_transaction.transaction_id%TYPE,
                                   p_returned_date       DATE,
                                   p_returned_amount     csh_transaction.returned_amount%TYPE,
                                   p_bank_slip_num       csh_transaction.bank_slip_num%TYPE,
                                   p_payment_method_id   csh_transaction.payment_method_id%TYPE,
                                   p_bank_account_id     csh_transaction.bank_account_id%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_description         csh_transaction.description%TYPE,
                                   p_user_id             csh_transaction.created_by%TYPE,
                                   p_return_id           NUMBER DEFAULT NULL) IS
    v_period_name         VARCHAR2(30);
    v_transaction_id      NUMBER;
    v_write_off_id        NUMBER;
    v_internal_period_num gld_periods.internal_period_num%TYPE;
  
    v_csh_transaction_rec    csh_transaction%ROWTYPE;
    v_return_transaction_rec csh_transaction%ROWTYPE;
    v_fin_create_je_flag     NUMBER;
  
    e_return_error EXCEPTION;
  BEGIN
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
  
    IF NOT csh_transaction_pkg.is_trx_return(v_csh_transaction_rec) THEN
      v_err_code := 'CSH_TRANSACTION_PKG.CSH_TRX_RETURN_FLAG_ERROR';
      RAISE e_return_error;
    END IF;
    --校验退款日期
    IF p_returned_date < v_csh_transaction_rec.transaction_date THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_DATE_ERROR';
      RAISE e_return_error;
    END IF;
    -- 期间
    v_period_name := gld_common_pkg.get_gld_period_name(p_company_id => v_csh_transaction_rec.company_id,
                                                        p_date       => p_returned_date);
    IF v_period_name IS NULL THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_PERIOD_ERROR';
      RAISE e_return_error;
    END IF;
    v_internal_period_num := gld_common_pkg.get_gld_internal_period_num(p_company_id  => v_csh_transaction_rec.company_id,
                                                                        p_period_name => v_period_name);
    IF v_internal_period_num IS NULL THEN
      v_err_code := 'CSH_TRANSACTION_PKG.RETURN_PERIOD_NUM_ERROR';
      RAISE e_return_error;
    END IF;
  
    insert_csh_transaction(p_transaction_id          => v_transaction_id,
                           p_transaction_num         => '',
                           p_transaction_category    => v_csh_transaction_rec.transaction_category,
                           p_transaction_type        => v_csh_transaction_rec.transaction_type,
                           p_transaction_date        => trunc(SYSDATE),
                           p_penalty_calc_date       => '',
                           p_bank_slip_num           => p_bank_slip_num,
                           p_company_id              => v_csh_transaction_rec.company_id,
                           p_internal_period_num     => v_internal_period_num,
                           p_period_name             => v_period_name,
                           p_payment_method_id       => p_payment_method_id,
                           p_distribution_set_id     => v_csh_transaction_rec.distribution_set_id,
                           p_cashflow_amount         => -1 *
                                                        p_returned_amount,
                           p_currency_code           => v_csh_transaction_rec.currency_code,
                           p_transaction_amount      => p_returned_amount,
                           p_exchange_rate_type      => v_csh_transaction_rec.exchange_rate_type,
                           p_exchange_rate_quotation => v_csh_transaction_rec.exchange_rate_quotation,
                           p_exchange_rate           => v_csh_transaction_rec.exchange_rate,
                           p_bank_account_id         => p_bank_account_id,
                           p_bp_category             => v_csh_transaction_rec.bp_category,
                           p_bp_id                   => v_csh_transaction_rec.bp_id,
                           p_bp_bank_account_id      => p_bp_bank_account_id,
                           p_bp_bank_account_num     => p_bp_bank_account_num,
                           p_description             => p_description,
                           p_handling_charge         => '',
                           p_posted_flag             => csh_transaction_pkg.csh_trx_posted_flag_yes,
                           p_reversed_flag           => csh_transaction_pkg.csh_trx_reversed_flag_n,
                           p_reversed_date           => '',
                           p_returned_flag           => csh_transaction_pkg.csh_trx_return_flag_return,
                           p_returned_amount         => '',
                           p_write_off_flag          => csh_transaction_pkg.csh_trx_write_off_flag_f,
                           p_write_off_amount        => '',
                           p_full_write_off_date     => '',
                           p_twin_csh_trx_id         => '',
                           p_return_from_csh_trx_id  => p_transaction_id,
                           p_reversed_csh_trx_id     => '',
                           p_source_csh_trx_type     => v_csh_transaction_rec.transaction_type,
                           p_source_csh_trx_id       => p_transaction_id,
                           p_source_doc_category     => '',
                           p_source_doc_type         => '',
                           p_source_doc_id           => '',
                           p_source_doc_line_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             => v_csh_transaction_rec.csh_bp_name,
                           p_collection_classes      => v_csh_transaction_rec.collection_classes,
                           p_return_id               => p_return_id,
                           --add by sf  
                           p_paid_byother_flag => v_csh_transaction_rec.paid_byother_flag,
                           p_ref_v05           => v_csh_transaction_rec.ref_v05
                           --end;
                           );
  
    SELECT *
      INTO v_return_transaction_rec
      FROM csh_transaction ct
     WHERE ct.transaction_id = v_transaction_id;
  
    --更新被退款现金事务信息
    update_csh_trx_after_return(p_transaction_id  => p_transaction_id,
                                p_returned_amount => p_returned_amount,
                                p_user_id         => p_user_id);
    --收款、预收款的退款需要在核销表中插一条退款事务
    --add by syj 保证金池退款
    IF v_csh_transaction_rec.transaction_type IN
       (csh_trx_type_prereceipt,
        csh_trx_type_receipt,
        csh_trx_type_deposit,
        'DEPOSIT_POOL_RECEIPT') THEN
    
      csh_write_off_pkg.return_write_off(p_return_write_off_id   => v_write_off_id,
                                         p_transaction_id        => p_transaction_id,
                                         p_return_transaction_id => v_transaction_id,
                                         p_returned_date         => p_returned_date,
                                         p_returned_amount       => p_returned_amount,
                                         p_description           => p_description,
                                         p_user_id               => p_user_id);
    END IF;
    --modif by nick 冯 for  derong 吉利二开部分,为了是以退款金额来出凭证,注释掉,使用上面write off pkg 里出凭证。 
    /* IF v_csh_transaction_rec.transaction_type = 'ADVANCE_RECEIPT' THEN
      v_fin_create_je_flag := gld_je_template_pkg.create_je(p_je_transaction_code     => 'CSH_CONSOLIDATION',
                                                            p_company_id              => v_return_transaction_rec.company_id,
                                                            p_je_company_id           => NULL,
                                                            p_user_id                 => p_user_id,
                                                            p_journal_header_id       => NULL,
                                                            p_hd_source_table         => 'CSH_TRANSACTION',
                                                            p_hd_source_id            => v_transaction_id,
                                                            p_ln_source_table         => 'CSH_TRANSACTION',
                                                            p_ln_source_id            => v_transaction_id,
                                                            p_journal_date            => v_return_transaction_rec.transaction_date,
                                                            p_transaction_date        => v_return_transaction_rec.transaction_date,
                                                            p_currency_code           => v_return_transaction_rec.currency_code,
                                                            p_exchange_rate_quotation => v_return_transaction_rec.exchange_rate_quotation,
                                                            p_hd_exchange_rate_type   => v_return_transaction_rec.exchange_rate_type,
                                                            p_hd_exchange_rate        => v_return_transaction_rec.exchange_rate,
                                                            p_ln_exchange_rate_type   => v_return_transaction_rec.exchange_rate_type,
                                                            p_ln_exchange_rate        => v_return_transaction_rec.exchange_rate,
                                                            p_account_indicator       => NULL,
                                                            p_ignore_history_check    => NULL,
                                                            p_reverse_hd_source_id    => NULL);
      IF v_fin_create_je_flag = 0 THEN
        UPDATE csh_transaction
           SET create_je_flag   = 'Y',
               last_update_date = SYSDATE,
               last_updated_by  = p_user_id
         WHERE transaction_id = v_transaction_id;
      END IF;
    END IF;*/
  
  EXCEPTION
    WHEN e_return_error THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => v_err_code,
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'return_csh_transaction',
                                                      p_token_1                 => '#TRANSACTION_NUM',
                                                      p_token_value_1           => v_csh_transaction_rec.transaction_num);
      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_transaction_pkg',
                                                     p_procedure_function_name => 'return_csh_transaction');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;

  ---add by zlf   2014/11/24

  PROCEDURE delete_interface(p_batch_id NUMBER, p_user_id NUMBER) IS
  BEGIN
    DELETE FROM csh_transaction_temp t;
    --WHERE t.batch_id = p_batch_id;
  EXCEPTION
    WHEN OTHERS THEN
      sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => dbms_utility.format_error_backtrace || ' ' ||
                                                                                  SQLERRM,
                                                     p_created_by              => p_user_id,
                                                     p_package_name            => 'CSH_TRANSACTION',
                                                     p_procedure_function_name => 'DELETE_INTERFACE');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END delete_interface;

  PROCEDURE insert_interface(p_header_id NUMBER,
                             p_batch_id  NUMBER,
                             p_user_id   NUMBER) IS
    CURSOR c_data IS
      SELECT *
        FROM fnd_interface_lines
       WHERE header_id = p_header_id
         AND line_number >= 2;
  BEGIN
  
    --删除临时表数据
    delete_interface(p_header_id, p_user_id);
  
    FOR c_record IN c_data LOOP
    
      INSERT INTO csh_transaction_temp
        (batch_id,
         csh_transaction_temp_id,
         transaction_date,
         description,
         transaction_amount,
         bp_bank_account_num,
         bp_name,
         project_number,
         bank_account_num,
         --bank_serial_num,
         --sys_date,
         created_by,
         creation_date,
         last_updated_by,
         last_update_date)
      VALUES
        (p_batch_id,
         csh_transaction_temp_s.nextval,
         to_date(c_record.attribute_1, 'YYYY-MM-DD'), --日期
         c_record.attribute_2, --备注
         to_number(c_record.attribute_3), --金额
         c_record.attribute_4, --打款账号
         c_record.attribute_5, --打款账户名
         c_record.attribute_6, --订单编号
         c_record.attribute_7, --收款账号
         --to_date(c_record.attribute_8, 'YYYY-MM-DD'),
         p_user_id,
         SYSDATE,
         p_user_id,
         SYSDATE);
    END LOOP;
  END;

  PROCEDURE delete_temp(p_batch_id NUMBER) IS
  BEGIN
    --delete from fnd_interface_lines t where t.line_id = p_batch_id;
    DELETE FROM csh_transaction_temp t
    
     WHERE t.batch_id = p_batch_id;
    DELETE FROM csh_transaction_temp_logs;
  END delete_temp;

  PROCEDURE delete_error_logs(p_batch_id NUMBER) IS
  
  BEGIN
    DELETE FROM csh_transaction_temp_logs a WHERE a.batch_id = p_batch_id;
  END delete_error_logs;

  PROCEDURE txn_log(p_temp     IN csh_transaction_temp%ROWTYPE,
                    p_log_text IN VARCHAR2,
                    p_user_id  IN NUMBER) IS
  BEGIN
    INSERT INTO csh_transaction_temp_logs
      (batch_id,
       csh_transaction_temp_log_id,
       transaction_date,
       description,
       transaction_amount,
       bp_bank_account_num,
       bp_name,
       bank_account_num,
       created_by,
       creation_date,
       last_updated_by,
       last_update_date,
       log_text)
    VALUES
      (p_temp.batch_id,
       csh_transaction_temp_logs_s.nextval,
       p_temp.transaction_date,
       p_temp.description,
       p_temp.transaction_amount,
       p_temp.bp_bank_account_num,
       p_temp.bp_name,
       p_temp.bank_account_num,
       p_user_id,
       SYSDATE,
       p_user_id,
       SYSDATE,
       p_log_text);
  END txn_log;

  PROCEDURE yonda_auto_write_off_log(p_transaction_id  NUMBER DEFAULT NULL,
                                     p_write_off_id    NUMBER DEFAULT NULL,
                                     p_bp_id           NUMBER DEFAULT NULL,
                                     p_contract_id     NUMBER DEFAULT NULL,
                                     p_bank_account_id NUMBER DEFAULT NULL,
                                     p_message         VARCHAR2) AS
    PRAGMA AUTONOMOUS_TRANSACTION;
  BEGIN
    INSERT INTO yonda_csh_excel_write_off_log
      (log_id,
       log_date,
       transaction_id,
       write_off_id,
       bp_id,
       contract_id,
       bank_account_id,
       message)
    VALUES
      (yonda_auto_write_off_log_s.nextval,
       SYSDATE,
       p_transaction_id,
       p_write_off_id,
       p_bp_id,
       p_contract_id,
       p_bank_account_id,
       p_message);
    COMMIT;
  END yonda_auto_write_off_log;

  PROCEDURE check_data(p_batch_id   NUMBER,
                       p_user_id    NUMBER,
                       p_check_type VARCHAR2 DEFAULT NULL,
                       p_return_id  OUT NUMBER) IS
    v_bp_name_exist_flag         VARCHAR2(2) := 'N';
    v_bp_bank_account_exist_flag VARCHAR2(2) := 'N';
    v_project_number_exist_flag  VARCHAR2(2) := 'N';
    v_bp_name_num                NUMBER;
    v_column_num                 NUMBER := 1;
    v_line_num                   NUMBER := 3;
    v_log_text                   VARCHAR(500);
    e_bp_name_num_error          EXCEPTION;
    e_date_null_error            EXCEPTION;
    e_description_null_error     EXCEPTION;
    e_amount_null_error          EXCEPTION;
    e_bp_account_null_error      EXCEPTION;
    e_bank_serial_num_null_error EXCEPTION;
  
  BEGIN
    delete_error_logs(p_batch_id);
    p_return_id := 1;
    FOR c_record IN (SELECT *
                       FROM csh_transaction_temp c
                      WHERE c.batch_id = p_batch_id) LOOP
      BEGIN
        IF c_record.transaction_date IS NULL THEN
          RAISE e_date_null_error;
        END IF;
      EXCEPTION
        WHEN e_date_null_error THEN
          p_return_id  := 0;
          v_column_num := 1;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,入账日期为空';
          txn_log(c_record, v_log_text, p_user_id);
      END;
      /*begin
        if c_record.description is null then
          raise e_description_null_error;
        end if;
      exception
        when e_description_null_error then
          p_return_id  := 0;
          v_column_num := 2;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,用途为空';
          txn_log(c_record, v_log_text, p_user_id);
      end;*/
      BEGIN
        IF c_record.transaction_amount IS NULL THEN
          RAISE e_amount_null_error;
        END IF;
      EXCEPTION
        WHEN e_amount_null_error THEN
          p_return_id  := 0;
          v_column_num := 3;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,入账金额为空';
          txn_log(c_record, v_log_text, p_user_id);
      END;
      /*begin
        if c_record.bp_bank_account_num is null then
          raise e_bp_account_null_error;
        end if;
      exception
        when e_bp_account_null_error then
          p_return_id  := 0;
          v_column_num := 4;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,对方账户为空';
          txn_log(c_record, v_log_text, p_user_id);
      end;*/
      /*      begin
        select 'Y'
          into v_bp_name_exist_flag
          from dual
         where exists (select 1
                  from con_contract_lv v
                 where v.bp_id_tenant_n = c_record.bp_name
                   and v.data_class = 'NORMAL');
      exception
        when no_data_found then
          p_return_id  := 0;
          v_column_num := 5;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,对方户名在系统中不存在';
          txn_log(c_record, v_log_text, p_user_id);
      END;*/
    
      /*begin
        select count(1)
          into v_bp_name_num
          from con_contract_lv v
         where v.bp_id_tenant_n = c_record.bp_name
           and v.data_class = 'NORMAL';
        if v_bp_name_num > 1 then
          raise e_bp_name_num_error;
        end if;
      exception
        when e_bp_name_num_error then
          p_return_id  := 0;
          v_column_num := 5;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,对方户名在系统中存在多个';
          txn_log(c_record, v_log_text, p_user_id);
      end;*/
      --无支付表 收款导入不校验合同号  2018年3月13日18:17:56 for deron
      IF p_check_type <> 'TRANSACTION' THEN
        BEGIN
          SELECT 'Y'
            INTO v_project_number_exist_flag
            FROM dual
           WHERE EXISTS
           (SELECT 1
                    FROM prj_project v
                   WHERE v.project_number = c_record.project_number);
        EXCEPTION
          WHEN no_data_found THEN
            p_return_id  := 0;
            v_column_num := 6;
            v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                            '列,订单在系统中不存在';
            txn_log(c_record, v_log_text, p_user_id);
          
        END;
      END IF;
    
      BEGIN
        SELECT 'Y'
          INTO v_bp_bank_account_exist_flag
          FROM dual
         WHERE EXISTS
         (SELECT 1
                  FROM csh_bank_account_v v
                 WHERE v.bank_account_num = c_record.bank_account_num);
      EXCEPTION
        WHEN no_data_found THEN
          p_return_id  := 0;
          v_column_num := 7;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,收款账号在系统中不存在';
          txn_log(c_record, v_log_text, p_user_id);
        
      END;
    
      --应黄蓉要求 银行流水号 为非必输
      /*BEGIN
        IF c_record.bank_serial_num IS NULL THEN
          RAISE e_bank_serial_num_null_error;
        END IF;
      EXCEPTION
        WHEN e_bank_serial_num_null_error THEN
          p_return_id  := 0;
          v_column_num := 7;
          v_log_text   := '导入文档中第' || v_line_num || '行第' || v_column_num ||
                          '列,银行流水号为空';
          txn_log(c_record, v_log_text, p_user_id);
      END;*/
    
      v_line_num := v_line_num + 1;
    END LOOP;
  END;

  /*PROCEDURE save_data
  (
    p_batch_id     NUMBER,
    p_user_id      NUMBER,
    p_company_id   NUMBER,
    v_save_message OUT VARCHAR2
  ) IS
    v_bp_id                     NUMBER;
    v_bank_account_id           NUMBER;
    v_transaction_id            NUMBER;
    v_transaction_num           VARCHAR2(30);
    v_period_name               VARCHAR2(30);
    v_internal_period_num       NUMBER;
    v_currency_code             VARCHAR2(30);
    v_bp_category               VARCHAR2(30);
    v_bp_bank_account_id        NUMBER;
    v_contract_num              NUMBER;
    v_cashflow_num              NUMBER;
    v_min_times                 NUMBER;
    v_session_id                NUMBER;
    v_con_contract_cashflow_rec con_contract_cashflow%ROWTYPE;
    v_contract_id               NUMBER;
    v_is_error                  BOOLEAN := FALSE;
    v_write_off_flag            VARCHAR2(30) := 'Y';
    e_null_contract_error     EXCEPTION;
    e_too_many_contract_error EXCEPTION;
    e_null_cashflow_error     EXCEPTION;
    v_contract_rec con_contract%ROWTYPE;
  BEGIN
    FOR c_record IN (SELECT * FROM csh_transaction_temp a WHERE a.batch_id = p_batch_id) LOOP
      -- check bp
      BEGIN
        IF c_record.bp_name IS NULL THEN
          v_bp_id       := NULL;
          v_bp_category := NULL;
          yonda_auto_write_off_log(p_message => '来自帐号:' || c_record.bank_account_num || '的一笔收款' ||
                                                c_record.transaction_amount || '没有对方户名,无法匹配商业伙伴,无法自动核销');
        ELSE
          BEGIN
            SELECT v.bp_id_tenant
              INTO v_bp_id
              FROM con_contract_lv v
             WHERE c_record.bp_name = v.bp_id_tenant_n
                   AND rownum = 1;
            SELECT t.bp_category INTO v_bp_category FROM hls_bp_master t WHERE t.bp_id = v_bp_id;
          EXCEPTION
            WHEN NO_DATA_FOUND THEN
              v_bp_id       := NULL;
              v_bp_category := NULL;
              yonda_auto_write_off_log(p_message => c_record.bp_name || ',一笔收款' || c_record.transaction_amount ||
                                                    '未找到对应的商业伙伴,无法自动核销');
          END;
        END IF;
      END;
      -- check account
      BEGIN
        IF c_record.bank_account_num IS NULL THEN
          v_bank_account_id    := NULL;
          v_currency_code      := 'CNY';
          v_bp_bank_account_id := NULL;
        ELSE
          SELECT cbav.bank_account_id,
                 cbav.currency_code,
                 nvl(cbav.bank_account_id,
                     NULL)
            INTO v_bank_account_id,
                 v_currency_code,
                 v_bp_bank_account_id
            FROM csh_bank_account_v cbav
           WHERE cbav.bank_account_num = c_record.bank_account_num
                 AND cbav.enabled_flag = 'Y';
        END IF;
      END;
  
      SELECT v.period_name,
             gld_common_pkg.get_gld_internal_period_num(p_company_id,
                                                        v.period_name) internal_period_num
        INTO v_period_name,
             v_internal_period_num
        FROM (SELECT gld_common_pkg.get_gld_period_name(p_company_id,
                                                        trunc(c_record.transaction_date)) period_name
                FROM dual) v;
  
      insert_csh_transaction(p_transaction_id          => v_transaction_id,
                             p_transaction_num         => v_transaction_num,
                             p_transaction_category    => 'BUSINESS',
                             p_transaction_type        => 'RECEIPT',
                             p_transaction_date        => trunc(c_record.transaction_date),
                             p_penalty_calc_date       => trunc(c_record.transaction_date),
                             p_bank_slip_num           => NULL,
                             p_company_id              => p_company_id,
                             p_internal_period_num     => v_internal_period_num,
                             p_period_name             => v_period_name,
                             p_payment_method_id       => 1,
                             p_distribution_set_id     => NULL,
                             p_cashflow_amount         => c_record.transaction_amount,
                             p_currency_code           => v_currency_code,
                             p_transaction_amount      => c_record.transaction_amount,
                             p_exchange_rate_type      => NULL,
                             p_exchange_rate_quotation => NULL,
                             p_exchange_rate           => 1,
                             p_bank_account_id         => v_bank_account_id,
                             p_bp_category             => v_bp_category,
                             p_bp_id                   => v_bp_id,
                             p_bp_bank_account_id      => v_bp_bank_account_id,
                             p_bp_bank_account_num     => c_record.bp_bank_account_num,
                             p_description             => c_record.description,
                             p_handling_charge         => 0,
                             p_posted_flag             => 'Y',
                             p_reversed_flag           => 'N',
                             p_reversed_date           => NULL,
                             p_returned_flag           => 'NOT',
                             p_returned_amount         => NULL,
                             p_write_off_flag          => 'NOT',
                             p_write_off_amount        => NULL,
                             p_full_write_off_date     => NULL,
                             p_twin_csh_trx_id         => NULL,
                             p_return_from_csh_trx_id  => NULL,
                             p_reversed_csh_trx_id     => NULL,
                             p_source_csh_trx_type     => NULL,
                             p_source_csh_trx_id       => NULL,
                             p_source_doc_category     => NULL,
                             p_source_doc_type         => NULL,
                             p_source_doc_id           => NULL,
                             p_source_doc_line_id      => NULL,
                             p_create_je_mothed        => NULL,
                             p_create_je_flag          => 'N',
                             p_gld_interface_flag      => 'N',
                             p_user_id                 => p_user_id,
                             p_ref_contract_id         => NULL);
      UPDATE csh_transaction ct SET ct.receipt_type = 'IMPORT' WHERE ct.transaction_id = v_transaction_id;
      --begin to write off
      SAVEPOINT start_auto_write_off;
      BEGIN
        SELECT COUNT(1)
          INTO v_contract_num
          FROM con_contract c
         WHERE c.bp_id_tenant = v_bp_id
               AND data_class = 'NORMAL'
               AND c.contract_status NOT IN ('CANCEL',
                                             'AD',
                                             'ET',
                                             'TERMINATE'); -- add by qianming 20150819
        ----如果未根据承租人找到合同,或者找到多条合同,则不核销
  
        IF v_contract_num < 1 THEN
          v_write_off_flag := 'N';
          IF v_bp_id IS NOT NULL THEN
            yonda_auto_write_off_log(p_transaction_id  => v_transaction_id,
                                     p_bp_id           => v_bp_id,
                                     p_bank_account_id => v_bank_account_id,
                                     p_message         => c_record.bp_name || ',一笔收款' || c_record.transaction_amount ||
                                                          '未匹配对应的合同,无法自动核销');
          END IF;
          \*raise e_null_contract_error;*\
        ELSIF v_contract_num > 1 THEN
          v_write_off_flag := 'N';
          yonda_auto_write_off_log(p_transaction_id  => v_transaction_id,
                                   p_bp_id           => v_bp_id,
                                   p_bank_account_id => v_bank_account_id,
                                   p_message         => c_record.bp_name || ',一笔收款' || c_record.transaction_amount ||
                                                        '匹配到多个合同,无法自动核销');
          \*raise e_too_many_contract_error;*\
        ELSE
          ---如果只找到一条合同,记录合同id,并查看与收款金额匹配的该合同的现金流
          SELECT c.contract_id
            INTO v_contract_id
            FROM con_contract c
           WHERE c.bp_id_tenant = v_bp_id
                 AND data_class = 'NORMAL'
                 AND c.contract_status NOT IN ('CANCEL',
                                               'AD',
                                               'ET',
                                               'TERMINATE');
          SELECT cc.* INTO v_contract_rec FROM con_contract cc WHERE cc.contract_id = v_contract_id;
          \*check contract status*\
          IF v_contract_rec.contract_status IN ('SIGN',
                                                'PRINTED',
                                                'NEW') THEN
            yonda_auto_write_off_log(p_transaction_id  => v_transaction_id,
                                     p_bp_id           => v_bp_id,
                                     p_contract_id     => v_contract_id,
                                     p_bank_account_id => v_bank_account_id,
                                     p_message         => c_record.bp_name || ',一笔收款' || c_record.transaction_amount ||
                                                          '匹配的合同状态不正确,无法自动核销');
          ELSE
            SELECT COUNT(1)
              INTO v_cashflow_num
              FROM con_contract_cashflow ccf
             WHERE ccf.cf_status = 'RELEASE'
                   AND ccf.cf_direction = 'INFLOW'
                   AND ccf.due_amount - nvl(ccf.received_amount,
                                            0) = c_record.transaction_amount
                   AND ccf.contract_id = v_contract_id;
  
            IF v_cashflow_num < 1 THEN
              v_write_off_flag := 'N';
              yonda_auto_write_off_log(p_transaction_id  => v_transaction_id,
                                       p_bp_id           => v_bp_id,
                                       p_bank_account_id => v_bank_account_id,
                                       p_message         => c_record.bp_name || ',一笔收款' || c_record.transaction_amount ||
                                                            '匹配的合同没有可核销现金流,无法自动核销');
              \*raise e_null_cashflow_error;*\
            ELSE
              \*raise e_null_cashflow_error;*\
              ---如果找到多条未核销的现金流,则先核销期数最小的那一条
              SELECT MIN(ccf.times)
                INTO v_min_times
                FROM con_contract_cashflow ccf
               WHERE ccf.cf_status = 'RELEASE'
                     AND ccf.cf_direction = 'INFLOW'
                     AND ccf.due_amount - nvl(ccf.received_amount,
                                              0) = c_record.transaction_amount
                     AND ccf.contract_id = v_contract_id;
  
              SELECT *
                INTO v_con_contract_cashflow_rec
                FROM con_contract_cashflow ccf
               WHERE ccf.cf_status = 'RELEASE'
                     AND ccf.cf_direction = 'INFLOW'
                     AND ccf.due_amount - nvl(ccf.received_amount,
                                              0) = c_record.transaction_amount
                     AND ccf.times = v_min_times
                     AND ccf.contract_id = v_contract_id;
              IF NOT v_is_error AND v_write_off_flag = 'Y' THEN
                BEGIN
                  v_session_id := sys_session_s.nextval;
                  csh_write_off_pkg.insert_csh_write_off_temp(p_session_id           => v_session_id,
                                                              p_write_off_type       => 'RECEIPT_CREDIT',
                                                              p_transaction_category => 'BUSINESS',
                                                              p_transaction_type     => 'RECEIPT',
                                                              p_write_off_date       => trunc(SYSDATE),
                                                              p_write_off_due_amount => c_record.transaction_amount,
                                                              p_write_off_principal  => v_con_contract_cashflow_rec.principal -
                                                                                        nvl(v_con_contract_cashflow_rec.received_principal,
                                                                                            0),
                                                              p_write_off_interest   => v_con_contract_cashflow_rec.interest -
                                                                                        nvl(v_con_contract_cashflow_rec.received_interest,
                                                                                            0),
                                                              p_company_id           => p_company_id,
                                                              p_document_category    => 'CONTRACT',
                                                              p_document_id          => v_contract_id,
                                                              p_document_line_id     => v_con_contract_cashflow_rec.cashflow_id,
                                                              p_description          => '收款导入自动核销生成',
                                                              p_user_id              => p_user_id);
                  csh_write_off_pkg.main_write_off(p_session_id     => v_session_id,
                                                   p_transaction_id => v_transaction_id,
                                                   p_user_id        => p_user_id);
  
                  v_save_message := '提交成功,并已自动核销!';
                  yonda_auto_write_off_log(p_transaction_id  => v_transaction_id,
                                           p_bp_id           => v_bp_id,
                                           p_contract_id     => v_contract_id,
                                           p_bank_account_id => v_bank_account_id,
                                           p_message         => c_record.bp_name || ',一笔收款' ||
                                                                c_record.transaction_amount || v_save_message);
                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_TRANSACTION',
                                                                   p_procedure_function_name => 'SAVE_DATA');
                    raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                            sys_raise_app_error_pkg.g_err_line_id);
                    ROLLBACK TO start_auto_write_off;
                    v_is_error     := TRUE;
                    v_save_message := '未能自动核销,请检查数据!';
                    yonda_auto_write_off_log(p_transaction_id  => v_transaction_id,
                                             p_bp_id           => v_bp_id,
                                             p_contract_id     => v_contract_id,
                                             p_bank_account_id => v_bank_account_id,
                                             p_message         => c_record.bp_name || ',一笔收款' ||
                                                                  c_record.transaction_amount || v_save_message);
                END;
              END IF;
            END IF;
          END IF;
        END IF;
      EXCEPTION
        WHEN e_null_contract_error THEN
          sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_IMPORT_CONTRACT_NOT_FOUND',
                                                          p_created_by              => p_user_id,
                                                          p_package_name            => 'csh_transaction_pkg',
                                                          p_procedure_function_name => 'save_data');
          raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                  sys_raise_app_error_pkg.g_err_line_id);
          ROLLBACK TO start_auto_write_off;
          v_is_error     := TRUE;
          v_save_message := '未找到相应合同,无法自动核销!';
        WHEN e_too_many_contract_error THEN
          sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_IMPORT_TOO_MANY_CONTRACT',
                                                          p_created_by              => p_user_id,
                                                          p_package_name            => 'csh_transaction_pkg',
                                                          p_procedure_function_name => 'save_data');
          raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                  sys_raise_app_error_pkg.g_err_line_id);
          ROLLBACK TO start_auto_write_off;
          v_is_error     := TRUE;
          v_save_message := '找到多条合同,无法自动核销!';
        WHEN e_null_cashflow_error THEN
          sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'CSH_TRANSACTION_PKG.CSH510_IMPORT_WRITE_OFF_AMOUNT_NOT_MATCH',
                                                          p_created_by              => p_user_id,
                                                          p_package_name            => 'csh_transaction_pkg',
                                                          p_procedure_function_name => 'save_data');
          raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                  sys_raise_app_error_pkg.g_err_line_id);
          ROLLBACK TO start_auto_write_off;
          v_is_error     := TRUE;
          v_save_message := '未找到对应现金流,无法自动核销,请检查金额!';
      END;
    END LOOP;
    --删除临时表数据
    delete_interface(p_batch_id,
                     p_user_id);
    delete_temp(p_batch_id);
  END;*/

  --收款导入并核销
  PROCEDURE save_data(p_batch_id     NUMBER,
                      p_user_id      NUMBER,
                      p_company_id   NUMBER,
                      v_save_message OUT VARCHAR2) AS
  BEGIN
    /*sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该功能正在维护中,请稍后尝试!',
                                                   p_created_by              => p_user_id,
                                                   p_package_name            => 'CSH_TRANSACTION',
                                                   p_procedure_function_name => 'DELETE_INTERFACE');
    raise_application_error(sys_raise_app_error_pkg.c_error_number,
                            sys_raise_app_error_pkg.g_err_line_id);*/
    -- transaction & write off
    csh_batch_transaction_pkg.excel_auto_tran_and_write_off(p_batch_id   => p_batch_id,
                                                            p_user_id    => p_user_id,
                                                            p_company_id => p_company_id);
    --删除临时表数据
    delete_interface(p_batch_id, p_user_id);
    delete_temp(p_batch_id);
  END save_data;

  --收款导入新建现金事务
  PROCEDURE save_transaction_data(p_batch_id     NUMBER,
                                  p_user_id      NUMBER,
                                  p_company_id   NUMBER,
                                  v_save_message OUT VARCHAR2) AS
  BEGIN
    /*sys_raise_app_error_pkg.raise_sys_others_error(p_message                 => '该功能正在维护中,请稍后尝试!',
                                                   p_created_by              => p_user_id,
                                                   p_package_name            => 'CSH_TRANSACTION',
                                                   p_procedure_function_name => 'DELETE_INTERFACE');
    raise_application_error(sys_raise_app_error_pkg.c_error_number,
                            sys_raise_app_error_pkg.g_err_line_id);*/
    -- transaction 
    csh_batch_transaction_pkg.excel_auto_transaction(p_batch_id   => p_batch_id,
                                                     p_user_id    => p_user_id,
                                                     p_company_id => p_company_id);
    --删除临时表数据
    delete_interface(p_batch_id, p_user_id);
    delete_temp(p_batch_id);
  END save_transaction_data;

  PROCEDURE risk_vat_insert(p_vat        NUMBER,
                            p_company_id NUMBER,
                            p_user_id    NUMBER) IS
    r_transaction csh_transaction%ROWTYPE;
    r_cwo         csh_write_off%ROWTYPE;
  BEGIN
    SELECT c.*
      INTO r_transaction
      FROM csh_transaction c
     WHERE c.transaction_type = 'RISK'
       AND c.company_id = p_company_id
       FOR UPDATE;
  
    r_cwo.write_off_id         := csh_write_off_s.nextval;
    r_cwo.write_off_type       := 'RISK_CREDIT';
    r_cwo.write_off_date       := trunc(SYSDATE);
    r_cwo.internal_period_num  := to_char(SYSDATE, 'YYYYMM');
    r_cwo.period_name          := to_char(SYSDATE, 'YYYY-MM');
    r_cwo.csh_transaction_id   := r_transaction.transaction_id;
    r_cwo.csh_write_off_amount := p_vat;
    r_cwo.reversed_flag        := 'N';
    r_cwo.write_off_due_amount := p_vat;
    r_cwo.create_je_flag       := 'Y';
    r_cwo.gld_interface_flag   := 'N';
    r_cwo.created_by           := p_user_id;
    r_cwo.creation_date        := SYSDATE;
    r_cwo.last_updated_by      := p_user_id;
    r_cwo.last_update_date     := SYSDATE;
    r_cwo.pay_dec              := '支付风险金税金';
    INSERT INTO csh_write_off VALUES r_cwo;
    UPDATE csh_transaction o
       SET o.write_off_amount = nvl(o.write_off_amount, 0) + p_vat
     WHERE o.transaction_id = r_transaction.transaction_id;
  
  END risk_vat_insert;
  --插入手工收款新增数据,检验流水号的唯一性
  PROCEDURE save_csh_transaction_data(p_transaction_date    IN VARCHAR2,
                                      p_cashflow_amount     IN NUMBER,
                                      p_bank_slip_num       IN VARCHAR2,
                                      p_paid_byother_flag   IN VARCHAR2,
                                      p_bp_id               IN NUMBER,
                                      p_collection_classes  IN NUMBER,
                                      p_description         IN VARCHAR2,
                                      p_bp_bank_account_id  IN NUMBER,
                                      p_bp_bank_name        IN VARCHAR2,
                                      p_bp_bank_account_num IN VARCHAR2,
                                      p_bank_account_id     IN VARCHAR2,
                                      p_user_id             IN VARCHAR2) IS
    v_exit_flag NUMBER := 0;
    e_slip_num_already_exist EXCEPTION;
  BEGIN
    --检查流水号的唯一性
    SELECT COUNT(1)
      INTO v_exit_flag
      FROM csh_transaction
     WHERE bank_slip_num = p_bank_slip_num;
    IF v_exit_flag != 0 THEN
      RAISE e_slip_num_already_exist;
    END IF;
    --插入数据
    INSERT INTO csh_transaction
      (transaction_id,
       transaction_date,
       cashflow_amount,
       bank_slip_num,
       paid_byother_flag,
       bp_id,
       collection_classes,
       description,
       bp_bank_account_id,
       bp_bank_name,
       bp_bank_account_num,
       bank_account_id)
    VALUES
      (csh_transaction_s.nextval,
       to_date(p_transaction_date, 'yyyy-mm-dd'),
       p_cashflow_amount,
       p_bank_slip_num,
       p_paid_byother_flag,
       p_bp_id,
       p_collection_classes,
       p_description,
       p_bp_bank_account_id,
       p_bp_bank_name,
       p_bp_bank_account_num,
       p_bank_account_id);
  EXCEPTION
    WHEN e_slip_num_already_exist THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'E_SLIP_NUM_ALREADY_EXIST',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'save_csh_transaction_data');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --add by shen 
  --excel导入收款新增csh_transaction表
  PROCEDURE csh_transaction_import_check(p_header_id NUMBER,
                                         p_user_id   NUMBER) IS
    cur                  fnd_interface_lines%ROWTYPE;
    cur_excel_check      fnd_interface_lines%ROWTYPE;
    v_error_message      VARCHAR2(2000);
    v_excel_error_message      VARCHAR2(2000);
    v_transaction_id     NUMBER;
    v_transaction_amount csh_transaction.transaction_amount%TYPE;
    v_bank_slip_num_flag NUMBER := 0;
    v_bp_flag            NUMBER := -1;
  BEGIN
  
    --将数字,文字等格式去除
    UPDATE fnd_interface_lines ln
       SET ln.attribute_1  = TRIM(ln.attribute_1),
           ln.attribute_2  = TRIM(ln.attribute_2),
           ln.attribute_3  = TRIM(ln.attribute_3),
           ln.attribute_5  = TRIM(ln.attribute_5),
           ln.attribute_8  = TRIM(ln.attribute_8),
           ln.attribute_9  = TRIM(ln.attribute_9),
           ln.attribute_10 = TRIM(ln.attribute_10),
           ln.attribute_14 = TRIM(ln.attribute_14),
           ln.attribute_23 = TRIM(ln.attribute_23),
           ln.attribute_24 = TRIM(ln.attribute_24)
     WHERE ln.header_id = p_header_id
       AND ln.line_number > 0;
  
    FOR cur IN (SELECT *
                  FROM fnd_interface_lines ln
                 WHERE ln.header_id = p_header_id
                   AND ln.line_number > 0
                 ORDER BY ln.line_number) LOOP
      v_error_message := NULL;
    
      --必输字段校验
      IF (cur.attribute_1 IS NULL) THEN
        v_error_message := v_error_message || ' 账号信息不能为空! ';
      END IF;
      IF (cur.attribute_8 IS NULL) THEN
        v_error_message := v_error_message || ' 对方账户信息不能为空! ';
      END IF;
      BEGIN
        --判断是否是数字
        SELECT to_number(cur.attribute_14)
          INTO v_transaction_amount
          FROM dual;
        IF v_transaction_amount < 0 THEN
          v_error_message := v_error_message || '收款金额不能小于0! ';
        END IF;
        --判断流水号是否已经存在
        SELECT COUNT(1)
          INTO v_bank_slip_num_flag
          FROM csh_transaction
         WHERE bank_slip_num = cur.attribute_5;
        IF v_bank_slip_num_flag != 0 THEN
          v_error_message := v_error_message || '该流水号已经存在';
        END IF;
        --判断是否有商业伙伴
        --select count(1) into v_bp_flag
        --   from hls_bp_master
        ----  where bank_account_num=cur.attribute_8;
        --if v_bp_flag !=1 then 
        --  v_error_message := v_error_message || '没有该账户对应的商业伙伴';
        --end if;
      EXCEPTION
        WHEN OTHERS THEN
          v_error_message := v_error_message || '数据出错,请重新导入';
        
      END;
      IF v_error_message IS NOT NULL THEN
        UPDATE fnd_interface_lines ln
           SET ln.error_message = v_error_message, ln.status = 'ERROR'
         WHERE ln.line_id = cur.line_id;
      END IF;
    
    END LOOP;
    
     --excel中有重复的数据
      for cur_excel_check in (
            select *
      from fnd_interface_lines ln
     where ln.header_id = p_header_id
       and ln.attribute_5 in (SELECT ln.attribute_5
                                FROM fnd_interface_lines ln
                               WHERE ln.header_id = p_header_id
                                 AND ln.line_number > 0
                               group by ln.attribute_5
                              having count(1) > 1)
        )  loop
        v_excel_error_message:=null;
        v_excel_error_message:=cur_excel_check.error_message||' 该条数据的流水号重复';
        --更新数据
         UPDATE fnd_interface_lines ln
           SET ln.error_message = v_excel_error_message, ln.status = 'ERROR'
         WHERE ln.line_id = cur_excel_check.line_id;
      end loop;
        
  
  END;
  --add by shen
  PROCEDURE update_confirmed_flag(p_transaction_id        IN NUMBER,
                                  p_wanted_confirmed_flag IN VARCHAR2,
                                  p_write_off_flag        IN VARCHAR2 DEFAULT NULL,
                                  p_user_id               IN VARCHAR2) IS
    v_csh_transaction_rec      csh_transaction%ROWTYPE;
    v_csh_transaction_plan_rec csh_transaction_plan%ROWTYPE;
  BEGIN
    --锁表
    lock_csh_transaction(p_transaction_id      => p_transaction_id,
                         p_user_id             => p_user_id,
                         p_csh_transaction_rec => v_csh_transaction_rec);
    UPDATE csh_transaction
       SET confirmed_flag = p_wanted_confirmed_flag
     WHERE transaction_id = p_transaction_id;
    --判断核销标志,并判断是否更新
    IF p_write_off_flag IS NOT NULL THEN
      UPDATE csh_transaction
         SET write_off_flag = p_write_off_flag
       WHERE transaction_id = p_transaction_id;
    END IF;
  
    --确认之后生成保证金、预收款的预选项
    IF p_wanted_confirmed_flag = 'APPROVED' THEN
      v_csh_transaction_plan_rec.plan_id        := csh_transaction_plan_s.nextval;
      v_csh_transaction_plan_rec.transaction_id := p_transaction_id;
      v_csh_transaction_plan_rec.write_off_type := 'ADVANCE_RECEIPT';
      v_csh_transaction_plan_rec.bp_id          := v_csh_transaction_rec.bp_id;
      v_csh_transaction_plan_rec.write_off_date := v_csh_transaction_rec.transaction_date;
      v_csh_transaction_plan_rec.created_by     := v_csh_transaction_rec.created_by;
      v_csh_transaction_plan_rec.creation_date  := SYSDATE;
      v_csh_transaction_plan_rec.company_id     := v_csh_transaction_rec.company_id;
    
      --插入预收款预选项
      INSERT INTO csh_transaction_plan VALUES v_csh_transaction_plan_rec;
      --插入保证金预选项
      IF v_csh_transaction_rec.bp_category = 'AGENT' THEN
        v_csh_transaction_plan_rec.write_off_type := 'DEPOSIT';
        INSERT INTO csh_transaction_plan VALUES v_csh_transaction_plan_rec;
      END IF;
    END IF;
  
  END;

  PROCEDURE create_deposit_advance_plan(p_transaction_id NUMBER,
                                        p_user_id        NUMBER) IS
    v_csh_transaction_rec      csh_transaction%ROWTYPE;
    v_csh_transaction_plan_rec csh_transaction_plan%ROWTYPE;
  BEGIN
    SELECT *
      INTO v_csh_transaction_rec
      FROM csh_transaction
     WHERE transaction_id = p_transaction_id;
  
    v_csh_transaction_plan_rec.plan_id        := csh_transaction_plan_s.nextval;
    v_csh_transaction_plan_rec.transaction_id := p_transaction_id;
    v_csh_transaction_plan_rec.write_off_type := 'RECEIPT_ADVANCE_RECEIPT';
    v_csh_transaction_plan_rec.bp_id          := v_csh_transaction_rec.bp_id;
    v_csh_transaction_plan_rec.write_off_date := v_csh_transaction_rec.transaction_date;
    v_csh_transaction_plan_rec.created_by     := v_csh_transaction_rec.created_by;
    v_csh_transaction_plan_rec.creation_date  := SYSDATE;
    v_csh_transaction_plan_rec.company_id     := v_csh_transaction_rec.company_id;
  
    --插入预收款预选项
    INSERT INTO csh_transaction_plan VALUES v_csh_transaction_plan_rec;
    --插入保证金预选项
    IF v_csh_transaction_rec.bp_category = 'AGENT' THEN
      v_csh_transaction_plan_rec.plan_id        := csh_transaction_plan_s.nextval;
      v_csh_transaction_plan_rec.write_off_type := 'RECEIPT_DEPOSIT_POOL';
      INSERT INTO csh_transaction_plan VALUES v_csh_transaction_plan_rec;
    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_transaction_pkg',
                                                     p_procedure_function_name => 'return_csh_transaction');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  END;
  --根据银行卡号获得银行账号的id
  PROCEDURE get_bank_account_id(p_bank_account_num    IN VARCHAR2,
                                p_bp_bank_account_num IN VARCHAR2,
                                p_bank_account_id     OUT NUMBER,
                                p_bp_category         OUT VARCHAR2,
                                p_bp_id               OUT NUMBER,
                                p_csh_bp_name         OUT VARCHAR2,
                                p_user_id             IN VARCHAR2) IS
    v_bank_account_id NUMBER;
    v_bp_category     hls_bp_master.bp_category%TYPE;
    v_bp_id           hls_bp_master.bp_id%TYPE;
    v_csh_bp_name     hls_bp_master.bp_name%TYPE;
  
    -- e_no_this_account Exception;
  BEGIN
    SELECT nvl(bank_account_id, -1)
      INTO v_bank_account_id
      FROM csh_bank_account
     WHERE bank_account_num = p_bank_account_num;
    --我方银行账号的id
    p_bank_account_id := v_bank_account_id;
    --根据对方的银行账号获得商业伙伴的信息
    SELECT bp_category, bp_id, bp_name
      INTO v_bp_category, v_bp_id, v_csh_bp_name
      FROM hls_bp_master
     WHERE bank_account_num = p_bp_bank_account_num;
    p_bp_category := v_bp_category;
    p_bp_id       := v_bp_id;
    p_csh_bp_name := v_csh_bp_name;
    --当没有对应的bp 时,会发生异常
  EXCEPTION
    WHEN OTHERS THEN
      -- p_bp_category :=null;
      p_bp_id := -1;
      -- p_csh_bp_name :=null;
  END;
  --根据银行卡号获得银行账号的id,根据bp_name 匹配数据
  PROCEDURE get_bank_account_info(p_bank_account_num     IN VARCHAR2,
                                  p_bp_bank_account_name IN VARCHAR2,
                                  p_bank_account_id      OUT NUMBER,
                                  p_bp_category          OUT VARCHAR2,
                                  p_bp_id                OUT NUMBER,
                                  p_csh_bp_name          OUT VARCHAR2,
                                  p_user_id              IN VARCHAR2) IS
    v_bank_account_id NUMBER;
    v_bp_category     hls_bp_master.bp_category%TYPE;
    v_bp_id           hls_bp_master.bp_id%TYPE;
    v_csh_bp_name     hls_bp_master.bp_name%TYPE;
  
    -- e_no_this_account Exception;
  BEGIN
    SELECT nvl(bank_account_id, -1)
      INTO v_bank_account_id
      FROM csh_bank_account
     WHERE bank_account_num = p_bank_account_num;
    --我方银行账号的id
    p_bank_account_id := v_bank_account_id;
    --根据对方的银行账号获得商业伙伴的信息
    SELECT bp_category, bp_id, bp_name
      INTO v_bp_category, v_bp_id, v_csh_bp_name
      FROM hls_bp_master
     WHERE bp_name = p_bp_bank_account_name;
    p_bp_category := v_bp_category;
    p_bp_id       := v_bp_id;
    p_csh_bp_name := v_csh_bp_name;
    --当没有对应的bp 时,会发生异常
  EXCEPTION
    WHEN OTHERS THEN
      -- p_bp_category :=null;
      p_bp_id := -1;
      -- p_csh_bp_name :=null;
  END;
  --excel 导入数据
  PROCEDURE save_receipt_import(p_transaction_id          OUT csh_transaction.transaction_id%TYPE,
                                p_transaction_num         csh_transaction.transaction_num%TYPE,
                                p_transaction_category    csh_transaction.transaction_category%TYPE,
                                p_transaction_type        csh_transaction.transaction_type%TYPE,
                                p_transaction_date        csh_transaction.transaction_date%TYPE,
                                p_penalty_calc_date       csh_transaction.penalty_calc_date%TYPE,
                                p_bank_slip_num           csh_transaction.bank_slip_num%TYPE,
                                p_company_id              csh_transaction.company_id%TYPE,
                                p_internal_period_num     csh_transaction.internal_period_num%TYPE,
                                p_period_name             csh_transaction.period_name%TYPE,
                                p_payment_method_id       csh_transaction.payment_method_id%TYPE,
                                p_distribution_set_id     csh_transaction.distribution_set_id%TYPE,
                                p_cashflow_amount         csh_transaction.cashflow_amount%TYPE,
                                p_currency_code           csh_transaction.currency_code%TYPE,
                                p_transaction_amount      csh_transaction.transaction_amount%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_bp_category             csh_transaction.bp_category%TYPE,
                                p_bp_id                   csh_transaction.bp_id%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_bp_bank_account_name    csh_transaction.bp_bank_account_name%TYPE DEFAULT NULL, --add by Spener 3893 20160722
                                p_description             csh_transaction.description%TYPE,
                                p_handling_charge         csh_transaction.handling_charge%TYPE,
                                p_posted_flag             csh_transaction.posted_flag%TYPE,
                                p_reversed_flag           csh_transaction.reversed_flag%TYPE,
                                p_reversed_date           csh_transaction.reversed_date%TYPE,
                                p_returned_flag           csh_transaction.returned_flag%TYPE,
                                p_returned_amount         csh_transaction.returned_amount%TYPE,
                                p_write_off_flag          csh_transaction.write_off_flag%TYPE,
                                p_write_off_amount        csh_transaction.write_off_amount%TYPE,
                                p_full_write_off_date     csh_transaction.full_write_off_date%TYPE,
                                p_twin_csh_trx_id         csh_transaction.twin_csh_trx_id%TYPE,
                                p_return_from_csh_trx_id  csh_transaction.return_from_csh_trx_id%TYPE,
                                p_reversed_csh_trx_id     csh_transaction.reversed_csh_trx_id%TYPE,
                                p_source_csh_trx_type     csh_transaction.source_csh_trx_type%TYPE,
                                p_source_csh_trx_id       csh_transaction.source_csh_trx_id%TYPE,
                                p_source_doc_category     csh_transaction.source_doc_category%TYPE,
                                p_source_doc_type         csh_transaction.source_doc_type%TYPE,
                                p_source_doc_id           csh_transaction.source_doc_id%TYPE,
                                p_source_doc_line_id      csh_transaction.source_doc_line_id%TYPE,
                                p_create_je_mothed        csh_transaction.create_je_mothed%TYPE,
                                p_create_je_flag          csh_transaction.create_je_flag%TYPE,
                                p_gld_interface_flag      csh_transaction.gld_interface_flag%TYPE,
                                p_user_id                 csh_transaction.created_by%TYPE,
                                p_ref_contract_id         csh_transaction.ref_contract_id%TYPE DEFAULT NULL,
                                p_receipt_type            csh_transaction.receipt_type%TYPE DEFAULT NULL,
                                p_csh_bp_name             csh_transaction.csh_bp_name%TYPE DEFAULT NULL,
                                p_ref_n01                 NUMBER DEFAULT NULL,
                                p_bp_bank_name            VARCHAR2 DEFAULT NULL,
                                p_bp_bank_branch_name     VARCHAR2 DEFAULT NULL,
                                p_collection_classes      VARCHAR2 DEFAULT NULL,
                                p_opposite_band_na        VARCHAR2 DEFAULT NULL,
                                p_banka                   VARCHAR2 DEFAULT NULL,
                                p_purpose                 VARCHAR2 DEFAULT NULL,
                                p_gsber                   VARCHAR2 DEFAULT NULL,
                                p_sus_belnr               VARCHAR2 DEFAULT NULL,
                                p_assigns_contract_id     NUMBER DEFAULT NULL,
                                p_conds_account_num       VARCHAR2 DEFAULT NULL,
                                p_conds_account_name      VARCHAR2 DEFAULT NULL,
                                p_bp_virtual_bank_account VARCHAR2 DEFAULT NULL,
                                
                                p_return_id NUMBER DEFAULT NULL,
                                p_zuonr     VARCHAR2 DEFAULT NULL,
                                --add by sf  
                                p_paid_byother_flag IN VARCHAR2,
                                p_ref_v05           IN VARCHAR2,
                                --该行信息是否正确
                                p_status           IN VARCHAR2,
                                p_bank_account_num IN VARCHAR2
                                --end;
                                ) IS
    v_bank_account_id        NUMBER;
    v_bp_category            hls_bp_master.bp_category%TYPE;
    v_bp_id                  hls_bp_master.bp_id%TYPE;
    v_csh_bp_name            hls_bp_master.bp_name%TYPE;
    v_real_bp_id             hls_bp_master.bp_id%TYPE;
    v_real_bp_name           hls_bp_master.bp_name%TYPE;
    v_real_bp_category       hls_bp_master.bp_category%TYPE;
    v_real_paid_byother_flag csh_transaction.paid_byother_flag%TYPE;
    --周期时间
    v_internal_period_num csh_transaction.internal_period_num%TYPE;
    v_period_name         csh_transaction.period_name%TYPE;
    --   v_date               varchar2(100);
    v_transcation_id csh_transaction.transaction_id%TYPE;
  BEGIN
    --判断该条记录是否有错
    IF p_status IS NOT NULL THEN
      RETURN;
    END IF;
    -- i.  若能找到唯一值且该商业伙伴类型为承租人,则代付字段为“否”、商业伙伴字段为匹配到的商业伙伴;
    --ii.  若能找到唯一值且该商业伙伴类型为代理店,则代付字段为“是”、商业伙伴字段为匹配到的商业伙伴;
    --iii. 若无法找到匹配项或匹配到多项,则代付字段为“是”、商业伙伴字段为空;
    --获得商业伙伴的信息
    csh_transaction_pkg.get_bank_account_info(p_bank_account_num     => p_bank_account_num,
                                              p_bp_bank_account_name => p_bp_bank_account_name,
                                              p_bank_account_id      => v_bank_account_id,
                                              p_bp_category          => v_bp_category,
                                              p_bp_id                => v_bp_id,
                                              p_csh_bp_name          => v_csh_bp_name,
                                              p_user_id              => p_user_id);
    --修改为bp_name 进行匹配
  
    --第三种情况
    IF v_bp_id = -1 THEN
      BEGIN
        v_real_paid_byother_flag := 'T';
        v_real_bp_id             := NULL;
        v_real_bp_name           := NULL;
        v_real_bp_category       := NULL;
      END;
    ELSIF v_bp_id != -1 AND v_bp_category = 'TENANT' THEN
      BEGIN
        v_real_paid_byother_flag := 'F';
        v_real_bp_id             := v_bp_id;
        v_real_bp_name           := v_csh_bp_name;
        v_real_bp_category       := v_bp_category;
      END;
    ELSIF v_bp_id != -1 AND v_bp_category = 'AGENT' THEN
      BEGIN
        v_real_paid_byother_flag := 'T';
        v_real_bp_id             := v_bp_id;
        v_real_bp_name           := v_csh_bp_name;
        v_real_bp_category       := v_bp_category;
      END;
    END IF;
  
    --处理周期字段
    --  select to_char(p_transaction_date,'yyyy-mm-dd') into v_date  from dual; 
    SELECT v.period_name,
           gld_common_pkg.get_gld_internal_period_num(p_company_id,
                                                      v.period_name)
      INTO v_period_name, v_internal_period_num
    
    --gld_common_pkg.get_gld_internal_period_num(p_company_id,v.period_name) period_num,
    --gld_common_pkg.get_period_year(p_company_id,v.period_name) period_year,
    --gld_common_pkg.get_period_set_code(p_company_id) period_set_code
      FROM (SELECT gld_common_pkg.get_gld_period_name(p_company_id,
                                                      to_date(to_char(p_transaction_date,
                                                                      'yyyy-mm-dd'),
                                                              'yyyy-mm-dd')) period_name
              FROM dual) v;
  
    --调用插入函数
    csh_transaction_pkg.insert_csh_transaction(p_transaction_id          => v_transcation_id,
                                               p_transaction_num         => p_transaction_num,
                                               p_transaction_category    => p_transaction_category,
                                               p_transaction_type        => p_transaction_type,
                                               p_transaction_date        => p_transaction_date,
                                               p_penalty_calc_date       => p_penalty_calc_date,
                                               p_bank_slip_num           => p_bank_slip_num,
                                               p_company_id              => p_company_id,
                                               p_internal_period_num     => v_internal_period_num,
                                               p_period_name             => v_period_name,
                                               p_payment_method_id       => p_payment_method_id,
                                               p_distribution_set_id     => p_distribution_set_id,
                                               p_cashflow_amount         => p_cashflow_amount,
                                               p_currency_code           => p_currency_code,
                                               p_transaction_amount      => p_transaction_amount,
                                               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         => v_bank_account_id,
                                               p_bp_category             => v_real_bp_category,
                                               p_bp_id                   => v_real_bp_id,
                                               p_bp_bank_account_id      => p_bp_bank_account_id,
                                               p_bp_bank_account_num     => p_bp_bank_account_num,
                                               p_description             => p_description,
                                               p_handling_charge         => p_handling_charge,
                                               p_posted_flag             => p_posted_flag,
                                               p_reversed_flag           => p_reversed_flag,
                                               p_reversed_date           => p_reversed_date,
                                               p_returned_flag           => p_returned_flag,
                                               p_returned_amount         => p_returned_amount,
                                               p_write_off_flag          => p_write_off_flag,
                                               p_write_off_amount        => p_write_off_amount,
                                               p_full_write_off_date     => p_full_write_off_date,
                                               p_twin_csh_trx_id         => p_twin_csh_trx_id,
                                               p_return_from_csh_trx_id  => p_return_from_csh_trx_id,
                                               p_reversed_csh_trx_id     => p_reversed_csh_trx_id,
                                               p_source_csh_trx_type     => p_source_csh_trx_type,
                                               p_source_csh_trx_id       => p_source_csh_trx_id,
                                               p_source_doc_category     => p_source_doc_category,
                                               p_source_doc_type         => p_source_doc_type,
                                               p_source_doc_id           => p_source_doc_id,
                                               p_source_doc_line_id      => p_source_doc_line_id,
                                               p_create_je_mothed        => p_create_je_mothed,
                                               p_create_je_flag          => p_create_je_flag,
                                               p_gld_interface_flag      => p_gld_interface_flag,
                                               p_user_id                 => p_user_id,
                                               p_ref_contract_id         => p_ref_contract_id,
                                               p_receipt_type            => p_receipt_type,
                                               p_csh_bp_name             => v_real_bp_name,
                                               p_ref_n01                 => p_ref_n01,
                                               p_bp_bank_name            => p_bp_bank_name,
                                               p_bp_bank_branch_name     => p_bp_bank_branch_name,
                                               p_collection_classes      => p_collection_classes,
                                               p_bp_bank_account_name    => p_bp_bank_account_name,
                                               p_opposite_band_na        => p_opposite_band_na,
                                               p_banka                   => p_banka,
                                               p_purpose                 => p_purpose,
                                               p_gsber                   => p_gsber,
                                               p_sus_belnr               => p_sus_belnr,
                                               p_bp_virtual_bank_account => p_bp_virtual_bank_account,
                                               p_zuonr                   => p_bank_slip_num,
                                               p_paid_byother_flag       => v_real_paid_byother_flag,
                                               p_ref_v05                 => p_ref_v05,
                                               p_status                  => p_status);
  END;
  --选择下一个确认人
  PROCEDURE update_confirmer_by_condition(p_transaction_id IN NUMBER,
                                          p_user_id        IN VARCHAR2) IS
    v_receipt_type      csh_transaction.receipt_type%TYPE;
    v_bp_category       csh_transaction.bp_category%TYPE;
    v_paid_byother_flag csh_transaction.paid_byother_flag%TYPE;
    v_confirmed_flag    csh_transaction.confirmed_flag%TYPE;
  BEGIN
    --查找出相关信息
    SELECT c.receipt_type, c.bp_category, c.paid_byother_flag
      INTO v_receipt_type, v_bp_category, v_paid_byother_flag
      FROM csh_transaction c
     WHERE c.transaction_id = p_transaction_id;
    --如果是手动收款
    IF v_receipt_type = 'MANUAL' THEN
      BEGIN
        IF v_bp_category = 'AGENT' OR v_paid_byother_flag = 'T' THEN
          v_confirmed_flag := 'ACCAUDITING';
        ELSIF v_bp_category = 'TENANT' THEN
          v_confirmed_flag := 'DEBTAUDITING';
        END IF;
      END;
      --excel导入
    ELSIF v_receipt_type = 'IMPORT' THEN
      BEGIN
        SELECT bp_category
          INTO v_bp_category
          FROM hls_bp_master
         WHERE bp_name =
               (SELECT c.bp_bank_account_name
                  FROM csh_transaction c
                 WHERE c.transaction_id = p_transaction_id);
        IF v_bp_category = 'TENANT' THEN
          v_confirmed_flag := 'DEBTAUDITING';
        ELSIF v_bp_category = 'AGENT' THEN
          v_confirmed_flag := 'ACCAUDITING';
        ELSIF v_bp_category IS NULL THEN
          v_confirmed_flag := 'ACCAUDITING';
        END IF;
      END;
    END IF;
  
    UPDATE csh_transaction c
       SET c.confirmed_flag = v_confirmed_flag
     WHERE c.transaction_id = p_transaction_id;
  
  EXCEPTION
    WHEN OTHERS THEN
      BEGIN
        v_confirmed_flag := 'ACCAUDITING';
        UPDATE csh_transaction c
           SET c.confirmed_flag = v_confirmed_flag
         WHERE c.transaction_id = p_transaction_id;
      END;
    
  END;
  --修改单据的状态(作废,驳回,以及确认单据)
  PROCEDURE update_muti_confirmed_flag(p_transaction_id          IN NUMBER,
                                       p_wanted_confirmed_flag   IN VARCHAR2,
                                       p_wanted_written_off_flag IN VARCHAR2 DEFAULT NULL,
                                       p_confirmed_by            IN VARCHAR2 DEFAULT NULL,
                                       p_user_id                 IN VARCHAR2) IS
    v_created_by csh_transaction.created_by%TYPE;
    E_SAME_RECODER EXCEPTION;
  
  BEGIN
    --更新单据状态
    UPDATE csh_transaction c
       SET c.confirmed_flag = p_wanted_confirmed_flag
     WHERE c.transaction_id = p_transaction_id;
    --如果是最终的确认单据,改变核销状态并加上确认人和确认时间
    IF p_wanted_written_off_flag IS NOT NULL THEN
      UPDATE csh_transaction c
         SET c.write_off_flag = p_wanted_written_off_flag
       WHERE c.transaction_id = p_transaction_id;
      --加上确认人和确认时间,需要另一个债权内勤进行确认
      --查找出制单人和当前用户进行比较    
      IF p_confirmed_by = '00217' THEN
        BEGIN
          SELECT c.created_by
            INTO v_created_by
            FROM csh_transaction c
           WHERE c.transaction_id = p_transaction_id;
          IF p_user_id = v_created_by THEN
            RAISE E_SAME_RECODER;
          END IF;
          --如果不是债权内勤,则进行确认
          UPDATE csh_transaction c
             SET c.debt_confirmed_by   = p_user_id,
                 c.debt_confirmed_date = SYSDATE
           WHERE c.transaction_id = p_transaction_id;
        
        END;
        --会计担当直接确认
      ELSIF p_confirmed_by = '00321' THEN
        UPDATE csh_transaction c
           SET c.acc_confirmed_by   = p_user_id,
               c.acc_confirmed_date = SYSDATE
         WHERE c.transaction_id = p_transaction_id;
      END IF;
      IF p_wanted_written_off_flag = 'NOT' AND
         p_wanted_confirmed_flag = 'APPROVED' THEN
        UPDATE csh_transaction
           SET posted_flag = 'Y'
         WHERE transaction_id = p_transaction_id;
        create_deposit_advance_plan(p_transaction_id => p_transaction_id,
                                    p_user_id        => p_user_id);
      END IF;
    END IF;
  EXCEPTION
    WHEN E_SAME_RECODER THEN
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'E_SAME_RECODER',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'csh_transaction_pkg',
                                                      p_procedure_function_name => 'update_muti_confirmed_flag');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
    
  END;

END csh_transaction_pkg;
/