hls_fin_calculator_ccr_pkg.pck 37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
create or replace package hls_fin_calculator_ccr_pkg is

  -- Author  : ZHANGLEI
  -- Created : 2014/10/20 9:15:42
  -- Purpose :

  procedure create_calculate(p_change_req_id      in number,
                             p_calc_session_id    in number,
                             p_recreate_h_formula in varchar2 default 'N',
                             p_recreate_l_std     in varchar2 default 'N',
                             p_recreate_l_formula in varchar2 default 'N',
                             p_calc_successful    out varchar2,
                             p_warning_message    out varchar2,
                             p_user_id            in number);

end hls_fin_calculator_ccr_pkg;
/
create or replace package body hls_fin_calculator_ccr_pkg is

  function replace_k(p_source varchar2, p_replace_j number) return varchar2 is
  
  begin
    return hls_fin_calculator_itfc_pkg.replace_k(p_source    => p_source,
                                                 p_replace_j => p_replace_j);
  end;

  function replace_n(p_source varchar2, p_replace_j number) return varchar2 is
  
  begin
    return hls_fin_calculator_itfc_pkg.replace_n(p_source    => p_source,
                                                 p_replace_j => p_replace_j);
  end;

  procedure create_h_data(p_calc_session_id number,
                          p_contract_id     number,
                          p_change_req_id   number,
                          p_price_list      varchar2,
                          p_ccr_start_times number,
                          p_ccr_lease_times number,
                          p_user_id         number) is
  
    r_hls_fin_calculator_hd hls_fin_calculator_hd%rowtype;
  begin
  
    hls_document_transfer_pkg.doc_to_doc(p_from_doc_table        => 'CON_CONTRACT',
                                         p_from_doc_pk           => p_change_req_id,
                                         p_to_doc_table          => 'HLS_FIN_CALCULATOR_HD',
                                         p_to_doc_pk             => p_calc_session_id,
                                         p_to_doc_column_1       => 'source_doc_category',
                                         p_to_doc_column_1_value => 'CHANGE_REQ',
                                         p_to_doc_column_2       => 'source_doc_id',
                                         p_to_doc_column_2_value => p_change_req_id,
                                         p_to_doc_column_3       => 'price_list',
                                         p_to_doc_column_3_value => p_price_list,
                                         p_user_id               => p_user_id);
  
    -- replace hd.lease_times
    update hls_fin_calculator_hd
       set lease_times   = p_ccr_start_times + p_ccr_lease_times - 1,
           business_type =
           (select BUSINESS_TYPE
              from con_contract
             where contract_id = p_contract_id)
     where calc_session_id = p_calc_session_id;
  
    hls_document_transfer_pkg.doc_to_doc(p_from_doc_table => 'HLS_FIN_CALCULATOR_HD',
                                         p_from_doc_pk    => p_calc_session_id,
                                         p_to_doc_table   => 'HLS_FIN_CALCULATOR_HD_FORMULA',
                                         p_to_doc_pk      => p_calc_session_id,
                                         p_user_id        => p_user_id);
  
    hls_fin_calculator_itfc_pkg.calculator_h_formula(p_calc_session_id => p_calc_session_id,
                                                     p_user_id         => p_user_id);
  
  end;

  procedure create_l_data(p_calc_session_id    number,
                          p_contract_id        number,
                          p_change_req_id      number,
                          p_price_list         varchar2,
                          p_ccr_start_times    number,
                          p_ccr_lease_times    number,
                          p_recreate_l_std     varchar2,
                          p_recreate_l_formula varchar2,
                          p_user_id            number) is
    n number; --原合同总期数
    k number; --变更后总期数
    i number;
  
    v_lease_times           number;
    t_column_cf_item        hls_fin_calculator_itfc_pkg.column_cf_item_t;
    r_calculator_ln         hls_fin_calculator_ln%rowtype;
    r_calculator_ln_formula hls_fin_calculator_ln_formula%rowtype;
  
    ln_formula_record hls_fin_calculator_itfc_pkg.ln_formula_type;
    v_cf_item         varchar2(100);
    v_calc_line_id    number;
    v_exists          varchar2(1);
  
    v_sql varchar2(32767);
  
    v_matrix_j_initial  number;
    v_matrix_j_interval number;
    v_formula           varchar2(4000);
  
    v_old_calc_session_id number;
  
    e_lease_times_error   exception;
    e_times_formula_error exception;
  
    function get_column_value(p_source in varchar2, p_j in number)
      return number is
      i       number := 0;
      v_instr number;
    
      v_text  varchar2(2000);
      v_sql   varchar2(32767);
      v_value varchar2(1000);
    
      v_parent_column varchar2(100);
    
      e_ignore_flag exception;
    begin
    
      if substr(ltrim(p_source), 1, 1) <> '=' then
        return p_source;
      end if;
    
      v_text := p_source;
    
      --检索头数组 H
      loop
        i       := i + 1;
        v_instr := instr(upper(v_text),
                         hls_fin_calculator_core_pkg.c_h,
                         1,
                         i);
        if v_instr = 0 or v_instr is null then
          exit;
        end if;
        v_parent_column := null;
        for i in 1 .. length(v_text) - v_instr loop
          begin
            if substr(v_text, v_instr + i, 1) not in
               ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') then
            
              if i = 1 then
                raise e_ignore_flag; --H数组第一个字符必须是数组
              end if;
            
              v_parent_column := hls_fin_calculator_core_pkg.c_h ||
                                 substr(v_text, v_instr + 1, i - 1);
            
              begin
                select b.column_name
                  into v_parent_column
                  from hls_fin_calc_config b
                 where b.layout_area_seq = 'H'
                   and b.price_list = p_price_list
                   and b.column_code = v_parent_column;
              
                begin
                  v_sql := 'select ' || v_parent_column ||
                           ' from hls_fin_calculator_hd where calc_session_id = ' ||
                           p_calc_session_id;
                
                  execute immediate v_sql
                    into v_value;
                
                  v_text := substr(v_text, 1, v_instr - 1) || v_value ||
                            substr(v_text, v_instr + i);
                
                  --v_text := replace(v_text,v_parent_column,v_value);
                
                exception
                  when others then
                    raise e_ignore_flag;
                end;
              
              exception
                when no_data_found then
                  raise e_ignore_flag;
                
              end;
            
              exit;
            
            end if;
          
            if i = (length(v_text) - v_instr) then
              v_parent_column := hls_fin_calculator_core_pkg.c_h ||
                                 substr(v_text, v_instr + 1);
            
              begin
                select b.column_name
                  into v_parent_column
                  from hls_fin_calc_config b
                 where b.layout_area_seq = 'H'
                   and b.price_list = p_price_list
                   and b.column_code = v_parent_column;
              
                begin
                  v_sql := 'select ' || v_parent_column ||
                           ' from hls_fin_calculator_hd where calc_session_id = ' ||
                           p_calc_session_id;
                
                  execute immediate v_sql
                    into v_value;
                
                  v_text := substr(v_text, 1, v_instr - 1) || v_value;
                
                  --v_text := replace(v_text,v_parent_column,v_value);
                
                exception
                  when others then
                    raise e_ignore_flag;
                end;
              
              exception
                when no_data_found then
                  raise e_ignore_flag;
                
              end;
            
            end if;
          
          exception
            when e_ignore_flag then
            
              exit;
            
          end;
        end loop;
      
      end loop;
    
      v_value := hls_fin_calculator_itfc_pkg.execute_formula(v_text,
                                                             p_calc_session_id,
                                                             p_price_list);
      return v_value;
    exception
      when others then
        return null;
    end;
  
  begin
  
    select lease_times
      into v_lease_times
      from hls_fin_calculator_hd
     where calc_session_id = p_calc_session_id;
  
    if p_ccr_lease_times is null or p_ccr_lease_times < 0 or
       p_ccr_start_times is null or p_ccr_start_times < 0 then
      raise e_lease_times_error;
    end if;
  
    if p_recreate_l_std = 'Y' then
    
      delete from hls_fin_calculator_ln
       where calc_session_id = p_calc_session_id
         and to_number(times) < p_ccr_start_times;
      delete from hls_fin_calculator_ln_formula
       where calc_session_id = p_calc_session_id
         and to_number(times) < p_ccr_start_times;
    
      t_column_cf_item := hls_fin_calculator_itfc_pkg.get_column_cf_item(p_price_list => p_price_list);
    
      --------------------------------------
      --step 1 将调整之前的期数回写到计算器行表
      --------------------------------------
      i := t_column_cf_item.first;
      while t_column_cf_item.exists(i) loop
      
        v_cf_item := t_column_cf_item(i).cf_item;
      
        for c_cf in (select a.cashflow_id, a.times
                       from con_contract_cashflow a
                      where a.contract_id = p_change_req_id
                        and a.cf_item = v_cf_item
                        and a.times < p_ccr_start_times) loop
          if v_cf_item = 1 then
            for c_column in (select c.DATA_TYPE,
                                    c.COLUMN_NAME,
                                    c.COLUMN_NAME as COLUMN_NAME_cf,
                                    cc.column_id  as matrix_i
                               from user_tab_columns         c,
                                    hls_fin_calc_column_code cc
                              where c.TABLE_NAME = 'CON_CONTRACT_CASHFLOW'
                                and cc.table_name = 'HLS_FIN_CALCULATOR_LN'
                                and c.COLUMN_NAME = cc.column_name
                             union
                             select c.data_type,
                                    c.column_name,
                                    'DUE_AMOUNT' as column_name_cf,
                                    c.column_id as matrix_i
                               from hls_fin_calc_config c
                              where c.price_list = p_price_list
                                and c.column_name = 'RENTAL'
                                and c.layout_area_seq = 'L'
                             union
                             select c.data_type,
                                    c.column_name,
                                    'NET_DUE_AMOUNT' as column_name_cf,
                                    c.column_id as matrix_i
                               from hls_fin_calc_config c
                              where c.price_list = p_price_list
                                and c.column_name = 'NET_RENTAL'
                                and c.layout_area_seq = 'L'
                             union
                             select c.data_type,
                                    c.column_name,
                                    'VAT_DUE_AMOUNT' as column_name_cf,
                                    c.column_id as matrix_i
                               from hls_fin_calc_config c
                              where c.price_list = p_price_list
                                and c.column_name = 'VAT_RENTAL'
                                and c.layout_area_seq = 'L') loop
            
              execute immediate 'select ' || c_column.column_name_cf ||
                                ' from con_contract_cashflow where cashflow_id=' ||
                                c_cf.cashflow_id
                into v_formula;
            
              ln_formula_record(c_cf.times).n(c_column.matrix_i).column_name := c_column.column_name;
              ln_formula_record(c_cf.times).n(c_column.matrix_i).data_type := c_column.data_type;
              ln_formula_record(c_cf.times).n(c_column.matrix_i).formula := v_formula;
            
            end loop;
          elsif v_cf_item = 0 then
            for c_column in (select c.DATA_TYPE,
                                    c.COLUMN_NAME,
                                    c.COLUMN_NAME as COLUMN_NAME_cf,
                                    cc.column_id  as matrix_i
                               from user_tab_columns         c,
                                    hls_fin_calc_column_code cc
                              where c.TABLE_NAME = 'CON_CONTRACT_CASHFLOW'
                                and cc.table_name = 'HLS_FIN_CALCULATOR_LN'
                                and c.column_name = cc.column_name
                                and c.column_name <> t_column_cf_item(i)
                                   .column_name
                             union
                             select c.data_type,
                                    c.column_name,
                                    'DUE_AMOUNT' as column_name_cf,
                                    c.column_id as matrix_i
                               from hls_fin_calc_config c
                              where c.price_list = p_price_list
                                and c.layout_area_seq = 'L'
                                and c.column_name = t_column_cf_item(i)
                                   .column_name) loop
            
              execute immediate 'select ' || c_column.column_name_cf ||
                                ' from con_contract_cashflow where cashflow_id=' ||
                                c_cf.cashflow_id
                into v_formula;
            
              ln_formula_record(c_cf.times).n(c_column.matrix_i).column_name := c_column.column_name;
              ln_formula_record(c_cf.times).n(c_column.matrix_i).data_type := c_column.data_type;
              ln_formula_record(c_cf.times).n(c_column.matrix_i).formula := v_formula;
            
            end loop;
          
          else
            for c_column in (select c.data_type,
                                    c.column_name,
                                    'DUE_AMOUNT' as column_name_cf,
                                    c.column_id as matrix_i
                               from hls_fin_calc_config c
                              where c.price_list = p_price_list
                                and c.layout_area_seq = 'L'
                                and c.column_name = t_column_cf_item(i)
                                   .column_name
                             union
                             select c.data_type,
                                    c.column_name,
                                    'NET_DUE_AMOUNT' as column_name_cf,
                                    c.column_id as matrix_i
                               from hls_fin_calc_config c
                              where c.price_list = p_price_list
                                and c.layout_area_seq = 'L'
                                and c.column_name = t_column_cf_item(i)
                                   .net_column_name
                             union
                             select c.data_type,
                                    c.column_name,
                                    'VAT_DUE_AMOUNT' as column_name_cf,
                                    c.column_id as matrix_i
                               from hls_fin_calc_config c
                              where c.price_list = p_price_list
                                and c.layout_area_seq = 'L'
                                and c.column_name = t_column_cf_item(i)
                                   .vat_column_name
                             
                             ) loop
            
              execute immediate 'select ' || c_column.column_name_cf ||
                                ' from con_contract_cashflow where cashflow_id=' ||
                                c_cf.cashflow_id
                into v_formula;
            
              ln_formula_record(c_cf.times).n(c_column.matrix_i).column_name := c_column.column_name;
              ln_formula_record(c_cf.times).n(c_column.matrix_i).data_type := c_column.data_type;
              ln_formula_record(c_cf.times).n(c_column.matrix_i).formula := v_formula;
            
            end loop;
          end if;
        end loop;
      
        i := t_column_cf_item.next(i);
      end loop;
    end if;
  
    --------------------------------------
    --step 2 按新公式填写剩余的计算器行表
    --------------------------------------
    if p_recreate_l_formula = 'Y' then
      delete from hls_fin_calculator_ln
       where calc_session_id = p_calc_session_id
         and to_number(times) >= p_ccr_start_times;
      delete from hls_fin_calculator_ln_formula
       where calc_session_id = p_calc_session_id
         and to_number(times) >= p_ccr_start_times;
    
      for c_l_column in (select a.column_name, a.config_id, a.data_type
                           from hls_fin_calc_config a
                          where a.price_list = p_price_list
                            and a.layout_area_seq = 'L'
                            and a.enabled_flag = 'Y') loop
      
        for c_l_formula in (select b.matrix_i,
                                   b.matrix_j_type,
                                   b.matrix_j_initial,
                                   b.matrix_j_interval,
                                   b.calc_formula,
                                   b.loop_remain_times_only
                              from hls_fin_calc_config_formula b
                             where b.config_id = c_l_column.config_id
                               and b.enabled_flag = 'Y'
                             order by b.matrix_i asc,
                                      b.calc_seq asc,
                                      decode(b.matrix_j_type, 'CYCLE', 1, 2) asc
                            
                            ) loop
        
          /* 起始值字段如果没有输入=,自动补等号 */
          if substr(ltrim(c_l_formula.matrix_j_initial), 1, 1) <> '=' then
            c_l_formula.matrix_j_initial := '=' ||
                                            c_l_formula.matrix_j_initial;
          end if;
          /* 间隔 字段如果没有输入=,自动补等号 */
          if substr(ltrim(c_l_formula.matrix_j_interval), 1, 1) <> '=' then
            c_l_formula.matrix_j_interval := '=' ||
                                             c_l_formula.matrix_j_interval;
          end if;
        
          if c_l_formula.matrix_j_type = 'CYCLE' then
          
            v_matrix_j_initial := get_column_value(c_l_formula.matrix_j_initial,
                                                   null);
          
            v_matrix_j_interval := get_column_value(c_l_formula.matrix_j_interval,
                                                    null);
          
            if v_matrix_j_initial is null then
              sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'HLS050_MATRIX_J_INITIAL_VALUE_ERROR',
                                                              p_token_1                 => '',
                                                              p_token_value_1           => c_l_column.column_name,
                                                              p_token_2                 => '',
                                                              p_token_value_2           => c_l_formula.matrix_j_initial,
                                                              p_created_by              => -1,
                                                              p_package_name            => 'con_change_req_calc_itfc_pkg',
                                                              p_procedure_function_name => 'create_l_data');
            
              raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                      sys_raise_app_error_pkg.g_err_line_id);
            end if;
          
            if v_matrix_j_interval is null then
              sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'HLS050_MATRIX_J_INTERVAL_VALUE_ERROR',
                                                              p_token_1                 => '',
                                                              p_token_value_1           => c_l_column.column_name,
                                                              p_token_2                 => '',
                                                              p_token_value_2           => c_l_formula.matrix_j_interval,
                                                              p_created_by              => -1,
                                                              p_package_name            => 'con_change_req_calc_itfc_pkg',
                                                              p_procedure_function_name => 'create_l_data');
            
              raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                      sys_raise_app_error_pkg.g_err_line_id);
            end if;
          
            if v_matrix_j_interval > 0 then
              --从小到大循环
            
              if c_l_formula.loop_remain_times_only = 'Y' then
                for j in v_matrix_j_initial .. p_ccr_lease_times loop
                
                  if v_matrix_j_initial is not null and
                     v_matrix_j_interval is not null then
                    --能计算循环周期
                  
                    if j >= v_matrix_j_initial and
                       mod(j - v_matrix_j_initial, v_matrix_j_interval) = 0 then
                    
                      k := p_ccr_start_times + j - 1;
                    
                      v_formula := replace_k(c_l_formula.calc_formula, k);
                    
                      ln_formula_record(k).n(c_l_formula.matrix_i).column_name := c_l_column.column_name;
                      ln_formula_record(k).n(c_l_formula.matrix_i).data_type := c_l_column.data_type;
                      if c_l_column.column_name = 'TIMES' then
                        v_formula := get_column_value(v_formula, k);
                        if v_formula is null then
                          raise e_times_formula_error;
                        end if;
                      end if;
                      ln_formula_record(k).n(c_l_formula.matrix_i).formula := v_formula;
                    
                    end if;
                  
                  end if;
                end loop;
              
              else
                for j in v_matrix_j_initial .. v_lease_times loop
                
                  if v_matrix_j_initial is not null and
                     v_matrix_j_interval is not null then
                    --能计算循环周期
                  
                    if j >= v_matrix_j_initial and
                       mod(j - v_matrix_j_initial, v_matrix_j_interval) = 0 then
                    
                      n := j;
                    
                      v_formula := replace_n(c_l_formula.calc_formula, n);
                    
                      ln_formula_record(n).n(c_l_formula.matrix_i).column_name := c_l_column.column_name;
                      ln_formula_record(n).n(c_l_formula.matrix_i).data_type := c_l_column.data_type;
                      if c_l_column.column_name = 'TIMES' then
                        v_formula := get_column_value(v_formula, n);
                        if v_formula is null then
                          raise e_times_formula_error;
                        end if;
                      end if;
                      ln_formula_record(n).n(c_l_formula.matrix_i).formula := v_formula;
                    
                    end if;
                  
                  end if;
                end loop;
              
              end if;
            elsif v_matrix_j_interval < 0 then
              --从大到小循环
              if c_l_formula.loop_remain_times_only = 'Y' then
                for j in 0 .. v_matrix_j_initial loop
                
                  if v_matrix_j_initial is not null and
                     v_matrix_j_interval is not null then
                    --能计算循环周期
                  
                    if j <= v_matrix_j_initial and
                       mod(v_matrix_j_initial - j, v_matrix_j_interval) = 0 then
                    
                      k := p_ccr_start_times + j - 1;
                    
                      v_formula := replace_k(c_l_formula.calc_formula, k);
                    
                      ln_formula_record(k).n(c_l_formula.matrix_i).column_name := c_l_column.column_name;
                      ln_formula_record(k).n(c_l_formula.matrix_i).data_type := c_l_column.data_type;
                      if c_l_column.column_name = 'TIMES' then
                        v_formula := get_column_value(v_formula, k);
                        if v_formula is null then
                          raise e_times_formula_error;
                        end if;
                      end if;
                      ln_formula_record(k).n(c_l_formula.matrix_i).formula := v_formula;
                    
                    end if;
                  
                  end if;
                end loop;
              
              else
              
                for j in 0 .. v_matrix_j_initial loop
                
                  if v_matrix_j_initial is not null and
                     v_matrix_j_interval is not null then
                    --能计算循环周期
                  
                    if j <= v_matrix_j_initial and
                       mod(v_matrix_j_initial - j, v_matrix_j_interval) = 0 then
                    
                      n := j;
                    
                      v_formula := replace_n(c_l_formula.calc_formula, n);
                    
                      ln_formula_record(n).n(c_l_formula.matrix_i).column_name := c_l_column.column_name;
                      ln_formula_record(n).n(c_l_formula.matrix_i).data_type := c_l_column.data_type;
                      if c_l_column.column_name = 'TIMES' then
                        v_formula := get_column_value(v_formula, n);
                        if v_formula is null then
                          raise e_times_formula_error;
                        end if;
                      end if;
                      ln_formula_record(n).n(c_l_formula.matrix_i).formula := v_formula;
                    
                    end if;
                  
                  end if;
                end loop;
              
              end if;
            
            end if;
          
          elsif c_l_formula.matrix_j_type = 'FIXED' then
          
            v_matrix_j_initial := get_column_value(c_l_formula.matrix_j_initial,
                                                   null);
          
            if v_matrix_j_initial is null then
              sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'HLS050_MATRIX_J_INITIAL_VALUE_ERROR',
                                                              p_token_1                 => '',
                                                              p_token_value_1           => c_l_column.column_name,
                                                              p_token_2                 => '',
                                                              p_token_value_2           => c_l_formula.matrix_j_initial,
                                                              p_created_by              => -1,
                                                              p_package_name            => 'con_change_req_calc_itfc_pkg',
                                                              p_procedure_function_name => 'create_l_data');
            
              raise_application_error(sys_raise_app_error_pkg.c_error_number,
                                      sys_raise_app_error_pkg.g_err_line_id);
            end if;
          
            if c_l_formula.loop_remain_times_only = 'Y' then
              k := v_matrix_j_initial + p_ccr_start_times - 1;
            
              if k is not null then
              
                v_formula := replace_k(c_l_formula.calc_formula, k);
              
                if c_l_column.column_name = 'TIMES' then
                  v_formula := get_column_value(v_formula, k);
                  if v_formula is null then
                    raise e_times_formula_error;
                  end if;
                end if;
              
                ln_formula_record(k).n(c_l_formula.matrix_i).data_type := c_l_column.data_type;
                ln_formula_record(k).n(c_l_formula.matrix_i).column_name := c_l_column.column_name;
                ln_formula_record(k).n(c_l_formula.matrix_i).formula := v_formula;
              
              end if;
            
            else
              n := v_matrix_j_initial;
            
              if n is not null then
              
                v_formula := replace_n(c_l_formula.calc_formula, n);
              
                if c_l_column.column_name = 'TIMES' then
                  v_formula := get_column_value(v_formula, n);
                  if v_formula is null then
                    raise e_times_formula_error;
                  end if;
                end if;
              
                ln_formula_record(n).n(c_l_formula.matrix_i).data_type := c_l_column.data_type;
                ln_formula_record(n).n(c_l_formula.matrix_i).column_name := c_l_column.column_name;
                ln_formula_record(n).n(c_l_formula.matrix_i).formula := v_formula;
              
              end if;
            
            end if;
          end if;
        
        end loop;
      
      end loop;
    end if;
  
    hls_fin_calculator_itfc_pkg.save_l_formula(p_ln_formula_record => ln_formula_record,
                                               p_user_id           => p_user_id,
                                               p_calc_session_id   => p_calc_session_id);
  
    --计算复制于源价目表字段
    if p_recreate_l_std = 'Y' then
      select calc_session_id
        into v_old_calc_session_id
        from con_contract a
       where a.contract_id = p_contract_id;
    
      for cur in (select a.column_name
                    from hls_fin_calc_config a
                   where a.layout_area_seq = 'L'
                     and a.price_list = p_price_list
                     and a.ref_v03 = 'Y') loop
        v_sql := 'update hls_fin_calculator_ln_formula l set ' ||
                 cur.column_name || ' = (select ' || cur.column_name ||
                 ' from hls_fin_calculator_ln ol where ol.calc_session_id = ' ||
                 v_old_calc_session_id || ' and ol.times = l.times)' ||
                 ' where calc_session_id = ' || p_calc_session_id ||
                 ' and to_number(times) <' || p_ccr_start_times;
      
        execute immediate v_sql;
      end loop;
    
    end if;
  
  end;

  procedure create_calculate(p_change_req_id      in number,
                             p_calc_session_id    in number,
                             p_recreate_h_formula in varchar2 default 'N',
                             p_recreate_l_std     in varchar2 default 'N',
                             p_recreate_l_formula in varchar2 default 'N',
                             p_calc_successful    out varchar2,
                             p_warning_message    out varchar2,
                             p_user_id            in number) is
    column_list_array hls_fin_calculator_core_pkg.column_list_type;
  
    v_error_count number;
  
    r_hls_fin_calc_hd_rec hls_fin_calculator_hd%rowtype;
  
    r_contract    con_contract%rowtype;
    v_contract_id number;
  
    e_ccr_start_times_error exception;
  begin
  
    if p_calc_session_id is null then
      return;
    else
    
      execute immediate 'alter session set nls_date_format = ''yyyy-mm-dd''';
    
      r_contract := con_contract_pkg.get_contract_rec(p_contract_id => p_change_req_id,
                                                      p_user_id     => p_user_id);
    
      if r_contract.ccr_start_times is null or
         r_contract.ccr_outstanding_times is null then
        raise e_ccr_start_times_error;
      end if;
    
      if r_contract.ccr_start_times < 1 then
        raise e_ccr_start_times_error;
      end if;
    
      select contract_id
        into v_contract_id
        from con_contract_change_req
       where change_req_id = p_change_req_id;
    
      if p_recreate_h_formula = 'Y' then
        delete from hls_fin_calculator_hd
         where calc_session_id = p_calc_session_id;
        delete from hls_fin_calculator_hd_formula
         where calc_session_id = p_calc_session_id;
      
        --create h
        create_h_data(p_calc_session_id => p_calc_session_id,
                      p_contract_id     => v_contract_id,
                      p_change_req_id   => p_change_req_id,
                      p_price_list      => r_contract.price_list,
                      p_ccr_start_times => r_contract.ccr_start_times,
                      p_ccr_lease_times => r_contract.ccr_outstanding_times,
                      p_user_id         => p_user_id);
      
      end if;
    
      select *
        into r_hls_fin_calc_hd_rec
        from hls_fin_calculator_hd
       where calc_session_id = p_calc_session_id;
    
      if not (r_hls_fin_calc_hd_rec.source_doc_category = 'CHANGE_REQ' and
          r_hls_fin_calc_hd_rec.source_doc_id is not null) then
      
        return;
      end if;
    
      if p_recreate_l_formula = 'Y' then
      
        update hls_fin_calculator_hd_formula
           set lease_times = r_hls_fin_calc_hd_rec.ccr_start_times +
                             r_hls_fin_calc_hd_rec.ccr_outstanding_times - 1
         where calc_session_id = p_calc_session_id;
      
        update hls_fin_calculator_hd hd
           set lease_times = r_hls_fin_calc_hd_rec.ccr_start_times +
                             r_hls_fin_calc_hd_rec.ccr_outstanding_times - 1
         where calc_session_id = p_calc_session_id;
      
        --create l
        create_l_data(p_calc_session_id    => p_calc_session_id,
                      p_contract_id        => v_contract_id,
                      p_change_req_id      => p_change_req_id,
                      p_price_list         => r_hls_fin_calc_hd_rec.price_list,
                      p_ccr_start_times    => r_hls_fin_calc_hd_rec.ccr_start_times,
                      p_ccr_lease_times    => r_hls_fin_calc_hd_rec.ccr_outstanding_times,
                      p_recreate_l_std     => p_recreate_l_std,
                      p_recreate_l_formula => p_recreate_l_formula,
                      p_user_id            => p_user_id);
      end if;
    
    end if;
  
    hls_fin_calculator_itfc_pkg.calculate_main(p_calc_session_id => p_calc_session_id,
                                               p_calc_successful => p_calc_successful,
                                               p_warning_message => p_warning_message,
                                               p_user_id         => p_user_id);
  
  exception
    when e_ccr_start_times_error then
      sys_raise_app_error_pkg.raise_user_define_error(p_message_code            => 'HLS_FIN_CALCULATOR_CCR_PKG.CCR_START_TIMES_ERROR',
                                                      p_created_by              => p_user_id,
                                                      p_package_name            => 'hls_fin_calculator_ccr_pkg',
                                                      p_procedure_function_name => 'create_calculate');
      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            => 'hls_fin_calculator_ccr_pkg',
                                                     p_procedure_function_name => 'create_calculate');
      raise_application_error(sys_raise_app_error_pkg.c_error_number,
                              sys_raise_app_error_pkg.g_err_line_id);
  end;
end hls_fin_calculator_ccr_pkg;
/