@startuml
title DCF.dcfp102 產生進貨折讓傳票流程圖

start
:初始化變數 pg_comp_id, pg_user, pg_sysdate, pg_effect_date, pg_yymm;
:迭代 c1x 游標 (遍歷符合條件的進貨折讓資料);
while (有下一筆資料?) is (是)
    if (acct_date > 月底) then (是)
        :將 pg_voucher_date 設為月底;
    else (否)
        :將 pg_voucher_date 設為 acct_date;
    endif

    :設置 pg_yyyy, pg_mm, pg_cvoucher_date;
    :取得傳票號碼 (get_voucher);
    if (pg_rtn_code != '0') then (是)
        :返回 pg_rtn_code;
        stop
    endif

    :新增傳票主檔 (insert_voucher_master);
    if (pg_rtn_code != '0') then (是)
        :返回 pg_rtn_code;
        stop
    endif

    :設置 pg_seq_no 為 1;

    :迭代 c1a 游標 (處理進貨折讓明細);
    while (有下一筆資料?) is (是)
        :查詢供應商名稱 (dc_name) 並存入 ds_dc_name;
        if (查詢失敗?) then (是)
            :將 ds_dc_name 設為 null;
        endif

        :設定折讓金額 pg_disc_amt;
        :新增傳票明細 (借方) (insert_voucher_detail);
        if (pg_rtn_code != '0') then (是)
            :返回 pg_rtn_code;
            stop
        endif

        :新增應付款項明細 (insert_ap_detail);
        if (pg_rtn_code != '0') then (是)
            :返回 pg_rtn_code;
            stop
        endif

        :迭代 c1b 游標 (處理進貨折讓詳細資訊);
        while (有下一筆資料?) is (是)
            :新增應收款項主檔 (ar_master);
            if (pg_rtn_code != '0') then (是)
                :返回 pg_rtn_code;
                stop
            endif

            :新增傳票明細 (貸方) (insert_voucher_detail);
            if (pg_rtn_code != '0') then (是)
                :返回 pg_rtn_code;
                stop
            endif
        endwhile

        :更新 DCF_PIV_DISC_TRF 的傳票號碼;
    endwhile
endwhile

:return '0';
stop
@enduml