DROP PACKAGE NAIS.NAIS_IMPORT_REPAIR
/

CREATE OR REPLACE PACKAGE NAIS.NAIS_IMPORT_REPAIR
IS

FUNCTION NAIS_SP_IMPORT_REPAIR
RETURN VARCHAR2;

END;
/

CREATE OR REPLACE PACKAGE BODY NAIS.NAIS_IMPORT_REPAIR
IS
FUNCTION NAIS_SP_IMPORT_REPAIR

RETURN VARCHAR2 IS

BEGIN
    DECLARE
        /* 修理品注文基本IFワークに検索処理 検索用 */
        CURSOR CUR1 IS
            SELECT
            ORD_DATA_CLS,
            ORD_ORDER_NO_1,
            ORD_ORDER_NO_2,
            ORD_ORDER_NO_3,
            ORD_ORDER_CLS,
            ORD_REQUEST_CLS,
            ORD_DEPT_CLS,
            ORD_OWNER_CD,
            VENDER_CD,
            ORD_DLV_SITE_CD,
            ORD_DLV_SITE_NM,
            ORD_USAGE_MODEL_CD,
            ORD_CURRENCY_CLS,
            ORD_CLAIM_NO,
            ORD_DIRECTIVE_NO,
            ORD_ESTM_NO,
            ORD_DEMANDER_CD,
            ORD_SUPPLIER_CD,
            ORD_PURVEYOR_CD,
            ORD_DEMAND_DATE,
            ORD_ORDER_DATE,
            ORD_ORDER_RAIT,
            ORD_DATA_ENTRY_DATE,
            ORD_DATA_ENTRY_TIME,
            ORD_DATA_DELETE_DATE,
            ORD_DATA_DELETE_TIME,
            ORD_AAC_OR_OTHER_CLS,
            ORD_FOREIGN_ORDER_NO_1,
            ORD_FOREIGN_ORDER_NO_2,
            ORD_VENDER_DPT,
            ORD_VENDER_PERSON,
            ORD_HD_TREATMENT_CLS,
            ORD_ISSURANCE_CLS,
            ORD_FIX_CLS,
            ORD_TOTAL_AMT,
            ORD_FOREIGN_TOTAL_AMT,
            ORD_ITEM_QTY,
            ORD_PAGE_QTY,
            ORD_ORDER_NOTE1,
            ORD_ORDER_NOTE2,
            ORD_ORDER_NOTE3,
            ORD_ORDER_NOTE4,
            ORD_DEMAND_NOTE1,
            ORD_DEMAND_NOTE2,
            ORD_DEMAND_NOTE3,
            ORD_DEMAND_NOTE4,
            INS_FUNCTION_ID,
            INS_USER_CD,
            INS_DATETIME,
            UPD_FUNCTION_ID,
            UPD_USER_CD,
            UPD_DATETIME
            FROM
            NAIS_I_REPAIR_ORDER_HD;
        /* 修理品注文に検索処理 検索用 */
        CURSOR CUR2 IS
            SELECT
            ORD_ORDER_NO_1,
            ORD_ORDER_NO_2,
            ORD_ORDER_NO_3,
            ORD_DATA_CLS,
            ORD_ORDER_CLS,
            ORD_REQUEST_CLS,
            ORD_DEPT_CLS,
            ORD_OWNER_CD,
            VENDER_CD,
            ORD_DLV_SITE_CD,
            ORD_DLV_SITE_NM,
            ORD_USAGE_MODEL_CD,
            ORD_CURRENCY_CLS,
            ORD_CLAIM_NO,
            ORD_DIRECTIVE_NO,
            ORD_ESTM_NO,
            ORD_DEMANDER_CD,
            ORD_SUPPLIER_CD,
            ORD_PURVEYOR_CD,
            ORD_DEMAND_DATE,
            ORD_ORDER_DATE,
            ORD_ORDER_RAIT,
            ORD_DATA_ENTRY_DATE,
            ORD_DATA_ENTRY_TIME,
            ORD_DATA_DELETE_DATE,
            ORD_DATA_DELETE_TIME,
            ORD_AAC_OR_OTHER_CLS,
            ORD_FOREIGN_ORDER_NO_1,
            ORD_FOREIGN_ORDER_NO_2,
            ORD_VENDER_DPT,
            ORD_VENDER_PERSON,
            ORD_HD_TREATMENT_CLS,
            ORD_ISSURANCE_CLS,
            ORD_FIX_CLS,
            ORD_TOTAL_AMT,
            ORD_FOREIGN_TOTAL_AMT,
            ORD_ITEM_QTY,
            ORD_PAGE_QTY,
            ORD_ORDER_NOTE1,
            ORD_ORDER_NOTE2,
            ORD_ORDER_NOTE3,
            ORD_ORDER_NOTE4,
            ORD_DEMAND_NOTE1,
            ORD_DEMAND_NOTE2,
            ORD_DEMAND_NOTE3,
            ORD_DEMAND_NOTE4,
            ORD_ORDER_REMARKS,
            BAT_UPD_DATETIME,
            BAT_UPD_USER_CD,
            INS_FUNCTION_ID,
            INS_USER_CD,
            INS_DATETIME,
            UPD_FUNCTION_ID,
            UPD_USER_CD,
            UPD_DATETIME
            FROM
            NAIS_T_REPAIR_ORDER_HD;
        /* 修理品注文明細I/Fワークに検索処理 検索用 */
        CURSOR CUR3 IS
            SELECT
            ORD_DATA_CLS,
            ORD_ORDER_NO_1,
            ORD_ORDER_NO_2,
            ORD_ORDER_NO_3,
            ORD_ITEM_NO,
            AA_JANO,
            ORD_JANO_REG_CLS,
            ORD_DEMAND_ITEM_NO,
            ORD_ORDER_PARTS_NO,
            ORD_ARRIVAL_PARTS_NO,
            ORD_ORDER_PARTS_NM,
            ORD_ARRIVAL_PARTS_NM,
            ORD_ORDER_SERIAL_NO,
            ORD_ARRIVAL_SERIAL_NO,
            ORD_TAG1,
            ORD_TAG2,
            ORD_WORK_CLS,
            ORD_TROUBLE_FIND_CD,
            ORD_TROUBLE_FIND_NM,
            ORD_TROUBLE_CONDITION_CD,
            ORD_TROUBLE_CONDITION_NM,
            ORD_TT,
            ORD_TT_PRINT_CLS,
            ORD_TSO_HOURS,
            ORD_TSO_PRINT_CLS,
            ORD_TSI_HOURS,
            ORD_TSI_PRINT_CLS,
            ORD_TAKE_OFF_AIRF_NO,
            ORD_TAKE_OFF_SERIAL_NO,
            ORD_MEMO,
            ORD_DESIRED_DLV_DATE,
            ORD_DLV_DATE,
            ORD_USE_AIRF_NM,
            ATA_CD,
            ORD_MODEL_ATA,
            ORD_DEMAND_QTY,
            ORD_ORDER_QTY,
            ORD_ORDER_UNIT_COST,
            ORD_ORDER_FOREIGN_UNIT_COST,
            ORD_ORDER_AMT,
            ORD_ORDER_FOREIGN_AMT,
            ORD_ARRIVAL_DATE,
            ORD_ARRIVAL_FIXED_QTY,
            ORD_ARRIVAL_QTY,
            ORD_ARRIVAL_AMT,
            ORD_ARRIVAL_FOREIGN_AMT,
            ORD_ARRIVAL_RAIT,
            ORD_PARTITIAL_DLV_CNT,
            ORD_PAYMENT_DATE,
            ORD_PAYMENT_QTY,
            ORD_PAYMENT_YEN_AMT,
            ORD_PAYMENT_FOREIGN_AMT,
            ORD_PAYMENT_ACTUAL_DATE,
            ORD_DT_TREATMENT_CLS,
            ORD_ISSURANCE_CLS,
            ORD_FIX_CLS,
            ORD_ABSENCE_CLS,
            ORD_STS_CLS,
            ORD_DATA_ENTRY_DATE,
            ORD_DATA_ENTRY_TIME,
            ORD_DATA_DELETE_DATE,
            ORD_DATA_DELETE_TIME,
            ORD_REFERENCE_KEY,
            INS_FUNCTION_ID,
            INS_USER_CD,
            INS_DATETIME,
            UPD_FUNCTION_ID,
            UPD_USER_CD,
            UPD_DATETIME
            FROM
            NAIS_I_REPAIR_ORDER_DT;
        /* 修理品注文明細に検索処理 検索用 */
        CURSOR CUR4 IS
            SELECT
            ORD_ORDER_NO_1,
            ORD_ORDER_NO_2,
            ORD_ORDER_NO_3,
            ORD_ITEM_NO,
            ORD_DATA_CLS,
            AA_JANO,
            ORD_JANO_REG_CLS,
            ORD_DEMAND_ITEM_NO,
            ORD_ORDER_PARTS_NO,
            ORD_ARRIVAL_PARTS_NO,
            ORD_ORDER_PARTS_NM,
            ORD_ARRIVAL_PARTS_NM,
            ORD_ORDER_SERIAL_NO,
            ORD_ARRIVAL_SERIAL_NO,
            ORD_TAG1,
            ORD_TAG2,
            ORD_WORK_CLS,
            ORD_TROUBLE_FIND_CD,
            ORD_TROUBLE_FIND_NM,
            ORD_TROUBLE_CONDITION_CD,
            ORD_TROUBLE_CONDITION_NM,
            ORD_TT,
            ORD_TT_PRINT_CLS,
            ORD_TSO_HOURS,
            ORD_TSO_PRINT_CLS,
            ORD_TSI_HOURS,
            ORD_TSI_PRINT_CLS,
            ORD_TAKE_OFF_AIRF_NO,
            ORD_TAKE_OFF_SERIAL_NO,
            ORD_MEMO,
            ORD_DESIRED_DLV_DATE,
            ORD_DLV_DATE,
            ORD_USE_AIRF_NM,
            ATA_CD,
            ORD_MODEL_ATA,
            ORD_DEMAND_QTY,
            ORD_ORDER_QTY,
            ORD_ORDER_UNIT_COST,
            ORD_ORDER_FOREIGN_UNIT_COST,
            ORD_ORDER_AMT,
            ORD_ORDER_FOREIGN_AMT,
            ORD_ARRIVAL_DATE,
            ORD_ARRIVAL_FIXED_QTY,
            ORD_ARRIVAL_QTY,
            ORD_ARRIVAL_AMT,
            ORD_ARRIVAL_FOREIGN_AMT,
            ORD_ARRIVAL_RAIT,
            ORD_PARTITIAL_DLV_CNT,
            ORD_PAYMENT_DATE,
            ORD_PAYMENT_QTY,
            ORD_PAYMENT_YEN_AMT,
            ORD_PAYMENT_FOREIGN_AMT,
            ORD_PAYMENT_ACTUAL_DATE,
            ORD_DT_TREATMENT_CLS,
            ORD_ISSURANCE_CLS,
            ORD_FIX_CLS,
            ORD_ABSENCE_CLS,
            ORD_STS_CLS,
            ORD_DATA_ENTRY_DATE,
            ORD_DATA_ENTRY_TIME,
            ORD_DATA_DELETE_DATE,
            ORD_DATA_DELETE_TIME,
            ORD_REFERENCE_KEY,
            ORD_WORK_CLS_NM,
            ORD_OANO,
            ORD_VENDER_PARTS_NO,
            ORD_QUOTE_NO,
            ORD_WORK_APPROVAL_DATE,
            ORD_TURN_AROUND_TIME_SCHDL,
            ORD_DMRNO,
            ORD_UNREPAIRABLE_AMT,
            ORD_RPR_REMARKS,
            ORD_RESERVE_COMMENT,
            ORD_TROUBLE_COMMENT_CLS,
            ORD_DLV_INVOICE_NO,
            ORD_SEND_DATE,
            ORD_SEND_POINT_CLS,
            ORD_DLV_AWB_NO,
            ORD_INPUT_FILE_NO,
            DOCUMENT_CD,
            ORD_ESTM_FIXED_AMT,
            ORD_ESTM_YEN_AMT,
            ORD_FACTORY_SHIPMENT_DATE,
            ORD_ARRIVAL_INVOICE_NO,
            ORD_RETURN_SITE_CLS,
            ORD_ARRIVAL_AWB_NO,
            ORD_ENTRY_FILE_NO,
            ORD_RPR_ARRIVAL_REMARKS,
            BAT_UPD_DATETIME,
            BAT_UPD_USER_CD,
            INS_FUNCTION_ID,
            INS_USER_CD,
            INS_DATETIME,
            UPD_FUNCTION_ID,
            UPD_USER_CD,
            UPD_DATETIME
            FROM
            NAIS_T_REPAIR_ORDER_DT;
        ICOUNT NUMBER;
        /* 注文品見積確定金額 */
        T_ORD_ESTM_FIXED_AMT NUMBER(12, 2);
        /* 注文品作業承認日 */
        T_ORD_WORK_APPROVAL_DATE VARCHAR2(10);
        /* 注文品注文時レート */
        I_ORD_ORDER_RAIT NUMBER(6, 2);
        /* 注文品見積金額(邦貨) */
        T_ORD_ESTM_YEN_AMT_1 NUMBER(14, 0);
        T_ORD_ESTM_YEN_AMT_2 NUMBER(14, 0);

    BEGIN
        FOR EACHROW IN CUR1 LOOP
            SELECT
            COUNT(*) INTO ICOUNT
            FROM
            NAIS_T_REPAIR_ORDER_HD
            WHERE
            ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
            AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
            AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3;

            /*
              以下の場合は、修理品注文のデータを更新する。

              検索条件
              修理品注文基本IFワーク.注文品注文番号1 = 修理品注文.注文品注文番号1 AND
              修理品注文基本IFワーク.注文品注文番号2 = 修理品注文.注文品注文番号2 AND
              修理品注文基本IFワーク.注文品注文番号3 = 修理品注文.注文品注文番号3

            */
            IF ICOUNT > 0 THEN
                UPDATE NAIS_T_REPAIR_ORDER_HD SET
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1,
                ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2,
                ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3,
                ORD_DATA_CLS = EACHROW.ORD_DATA_CLS,
                ORD_ORDER_CLS = EACHROW.ORD_ORDER_CLS,
                ORD_REQUEST_CLS = EACHROW.ORD_REQUEST_CLS,
                ORD_DEPT_CLS = EACHROW.ORD_DEPT_CLS,
                ORD_OWNER_CD = EACHROW.ORD_OWNER_CD,
                VENDER_CD = EACHROW.VENDER_CD,
                ORD_DLV_SITE_CD = EACHROW.ORD_DLV_SITE_CD,
                ORD_DLV_SITE_NM = EACHROW.ORD_DLV_SITE_NM,
                ORD_USAGE_MODEL_CD = EACHROW.ORD_USAGE_MODEL_CD,
                ORD_CURRENCY_CLS = EACHROW.ORD_CURRENCY_CLS,
                ORD_CLAIM_NO = EACHROW.ORD_CLAIM_NO,
                ORD_DIRECTIVE_NO = EACHROW.ORD_DIRECTIVE_NO,
                ORD_ESTM_NO = EACHROW.ORD_ESTM_NO,
                ORD_DEMANDER_CD = EACHROW.ORD_DEMANDER_CD,
                ORD_SUPPLIER_CD = EACHROW.ORD_SUPPLIER_CD,
                ORD_PURVEYOR_CD = EACHROW.ORD_PURVEYOR_CD,
                ORD_DEMAND_DATE = EACHROW.ORD_DEMAND_DATE,
                ORD_ORDER_DATE = EACHROW.ORD_ORDER_DATE,
                ORD_ORDER_RAIT = EACHROW.ORD_ORDER_RAIT,
                ORD_DATA_ENTRY_DATE = EACHROW.ORD_DATA_ENTRY_DATE,
                ORD_DATA_ENTRY_TIME = EACHROW.ORD_DATA_ENTRY_TIME,
                ORD_DATA_DELETE_DATE = EACHROW.ORD_DATA_DELETE_DATE,
                ORD_DATA_DELETE_TIME = EACHROW.ORD_DATA_DELETE_TIME,
                ORD_AAC_OR_OTHER_CLS = EACHROW.ORD_AAC_OR_OTHER_CLS,
                ORD_FOREIGN_ORDER_NO_1 = EACHROW.ORD_FOREIGN_ORDER_NO_1,
                ORD_FOREIGN_ORDER_NO_2 = EACHROW.ORD_FOREIGN_ORDER_NO_2,
                ORD_VENDER_DPT = EACHROW.ORD_VENDER_DPT,
                ORD_VENDER_PERSON = EACHROW.ORD_VENDER_PERSON,
                ORD_HD_TREATMENT_CLS = EACHROW.ORD_HD_TREATMENT_CLS,
                ORD_ISSURANCE_CLS = EACHROW.ORD_ISSURANCE_CLS,
                ORD_FIX_CLS = EACHROW.ORD_FIX_CLS,
                ORD_TOTAL_AMT = EACHROW.ORD_TOTAL_AMT,
                ORD_FOREIGN_TOTAL_AMT = EACHROW.ORD_FOREIGN_TOTAL_AMT,
                ORD_ITEM_QTY = EACHROW.ORD_ITEM_QTY,
                ORD_PAGE_QTY = EACHROW.ORD_PAGE_QTY,
                ORD_ORDER_NOTE1 = EACHROW.ORD_ORDER_NOTE1,
                ORD_ORDER_NOTE2 = EACHROW.ORD_ORDER_NOTE2,
                ORD_ORDER_NOTE3 = EACHROW.ORD_ORDER_NOTE3,
                ORD_ORDER_NOTE4 = EACHROW.ORD_ORDER_NOTE4,
                ORD_DEMAND_NOTE1 = EACHROW.ORD_DEMAND_NOTE1,
                ORD_DEMAND_NOTE2 = EACHROW.ORD_DEMAND_NOTE2,
                ORD_DEMAND_NOTE3 = EACHROW.ORD_DEMAND_NOTE3,
                ORD_DEMAND_NOTE4 = EACHROW.ORD_DEMAND_NOTE4,
                BAT_UPD_DATETIME = SYSDATE,
                BAT_UPD_USER_CD = EACHROW.UPD_USER_CD,
                UPD_FUNCTION_ID = EACHROW.UPD_FUNCTION_ID
                WHERE
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
                AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
                AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3;
            ELSE
                /*
                  注文品注文番号1~3が
                  修理品注文基本IFワークにのみ存在する場合は、

                  修理品注文にデータを作成する
                */
                INSERT INTO NAIS_T_REPAIR_ORDER_HD(
                ORD_ORDER_NO_1,
                ORD_ORDER_NO_2,
                ORD_ORDER_NO_3,
                ORD_DATA_CLS,
                ORD_ORDER_CLS,
                ORD_REQUEST_CLS,
                ORD_DEPT_CLS,
                ORD_OWNER_CD,
                VENDER_CD,
                ORD_DLV_SITE_CD,
                ORD_DLV_SITE_NM,
                ORD_USAGE_MODEL_CD,
                ORD_CURRENCY_CLS,
                ORD_CLAIM_NO,
                ORD_DIRECTIVE_NO,
                ORD_ESTM_NO,
                ORD_DEMANDER_CD,
                ORD_SUPPLIER_CD,
                ORD_PURVEYOR_CD,
                ORD_DEMAND_DATE,
                ORD_ORDER_DATE,
                ORD_ORDER_RAIT,
                ORD_DATA_ENTRY_DATE,
                ORD_DATA_ENTRY_TIME,
                ORD_DATA_DELETE_DATE,
                ORD_DATA_DELETE_TIME,
                ORD_AAC_OR_OTHER_CLS,
                ORD_FOREIGN_ORDER_NO_1,
                ORD_FOREIGN_ORDER_NO_2,
                ORD_VENDER_DPT,
                ORD_VENDER_PERSON,
                ORD_HD_TREATMENT_CLS,
                ORD_ISSURANCE_CLS,
                ORD_FIX_CLS,
                ORD_TOTAL_AMT,
                ORD_FOREIGN_TOTAL_AMT,
                ORD_ITEM_QTY,
                ORD_PAGE_QTY,
                ORD_ORDER_NOTE1,
                ORD_ORDER_NOTE2,
                ORD_ORDER_NOTE3,
                ORD_ORDER_NOTE4,
                ORD_DEMAND_NOTE1,
                ORD_DEMAND_NOTE2,
                ORD_DEMAND_NOTE3,
                ORD_DEMAND_NOTE4,
                ORD_ORDER_REMARKS,
                BAT_UPD_DATETIME,
                BAT_UPD_USER_CD,
                INS_FUNCTION_ID,
                INS_USER_CD,
                INS_DATETIME,
                UPD_FUNCTION_ID,
                UPD_USER_CD,
                UPD_DATETIME)
                VALUES(
                EACHROW.ORD_ORDER_NO_1,
                EACHROW.ORD_ORDER_NO_2,
                EACHROW.ORD_ORDER_NO_3,
                EACHROW.ORD_DATA_CLS,
                EACHROW.ORD_ORDER_CLS,
                EACHROW.ORD_REQUEST_CLS,
                EACHROW.ORD_DEPT_CLS,
                EACHROW.ORD_OWNER_CD,
                EACHROW.VENDER_CD,
                EACHROW.ORD_DLV_SITE_CD,
                EACHROW.ORD_DLV_SITE_NM,
                EACHROW.ORD_USAGE_MODEL_CD,
                EACHROW.ORD_CURRENCY_CLS,
                EACHROW.ORD_CLAIM_NO,
                EACHROW.ORD_DIRECTIVE_NO,
                EACHROW.ORD_ESTM_NO,
                EACHROW.ORD_DEMANDER_CD,
                EACHROW.ORD_SUPPLIER_CD,
                EACHROW.ORD_PURVEYOR_CD,
                EACHROW.ORD_DEMAND_DATE,
                EACHROW.ORD_ORDER_DATE,
                EACHROW.ORD_ORDER_RAIT,
                EACHROW.ORD_DATA_ENTRY_DATE,
                EACHROW.ORD_DATA_ENTRY_TIME,
                EACHROW.ORD_DATA_DELETE_DATE,
                EACHROW.ORD_DATA_DELETE_TIME,
                EACHROW.ORD_AAC_OR_OTHER_CLS,
                EACHROW.ORD_FOREIGN_ORDER_NO_1,
                EACHROW.ORD_FOREIGN_ORDER_NO_2,
                EACHROW.ORD_VENDER_DPT,
                EACHROW.ORD_VENDER_PERSON,
                EACHROW.ORD_HD_TREATMENT_CLS,
                EACHROW.ORD_ISSURANCE_CLS,
                EACHROW.ORD_FIX_CLS,
                EACHROW.ORD_TOTAL_AMT,
                EACHROW.ORD_FOREIGN_TOTAL_AMT,
                EACHROW.ORD_ITEM_QTY,
                EACHROW.ORD_PAGE_QTY,
                EACHROW.ORD_ORDER_NOTE1,
                EACHROW.ORD_ORDER_NOTE2,
                EACHROW.ORD_ORDER_NOTE3,
                EACHROW.ORD_ORDER_NOTE4,
                EACHROW.ORD_DEMAND_NOTE1,
                EACHROW.ORD_DEMAND_NOTE2,
                EACHROW.ORD_DEMAND_NOTE3,
                EACHROW.ORD_DEMAND_NOTE4,
                NULL,
                SYSDATE,
                EACHROW.UPD_USER_CD,
                EACHROW.INS_FUNCTION_ID,
                EACHROW.INS_USER_CD,
                SYSDATE,
                EACHROW.UPD_FUNCTION_ID,
                EACHROW.UPD_USER_CD,
                SYSDATE
                );
            END IF;
        END LOOP;

        FOR EACHROW IN CUR2 LOOP
            SELECT
            COUNT(*) INTO ICOUNT
            FROM NAIS_I_REPAIR_ORDER_HD
            WHERE
            ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
            AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
            AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3;

            /*
              注文品注文番号1~3が修理品注文にのみ存在する場合は、

              修理品注文とそれに紐づく修理品注文明細のデータを削除する。

              削除データ抽出条件(修理品注文明細)

              修理品注文.注文品注文番号1 = 修理品注文明細.注文品注文番号1 AND
              修理品注文.注文品注文番号2 = 修理品注文明細.注文品注文番号2 AND
              修理品注文.注文品注文番号3 = 修理品注文明細.注文品注文番号3

            */
            IF ICOUNT < 1 THEN
                /* 修理品注文のデータを削除する */
                DELETE FROM NAIS_T_REPAIR_ORDER_HD
                WHERE
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
                AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
                AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3;

                /* 修理品注文明細のデータを削除する */
                DELETE FROM NAIS_T_REPAIR_ORDER_DT
                WHERE
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
                AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
                AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3;
            END IF;
        END LOOP;

        FOR EACHROW IN CUR3 LOOP
            SELECT
            COUNT(*) INTO ICOUNT
            FROM
            NAIS_T_REPAIR_ORDER_DT
            WHERE
            ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
            AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
            AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3
            AND ORD_ITEM_NO = EACHROW.ORD_ITEM_NO;

            /*
              修理品注文基本IFワーク.注文品注文時レート検索処理

              修理品注文明細IFワーク.注文品注文番号1 = 修理品注文基本IFワーク.注文品注文番号1 AND
              修理品注文明細IFワーク.注文品注文番号2 = 修理品注文基本IFワーク.注文品注文番号2 AND
              修理品注文明細IFワーク.注文品注文番号3 = 修理品注文基本IFワーク.注文品注文番号3

            */
            SELECT
            ORD_ORDER_RAIT INTO I_ORD_ORDER_RAIT
            FROM
            NAIS_I_REPAIR_ORDER_HD
            WHERE
            ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
            AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
            AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3;

            /* 注文品見積金額(邦貨) */
            T_ORD_ESTM_YEN_AMT_1 := NULL;
            T_ORD_ESTM_YEN_AMT_2 := NULL;

            /*
              以下の場合は、修理品注文明細のデータを更新する。

              検索条件
              修理品注文明細IFワーク.注文品注文番号1 = 修理品注文明細.注文品注文番号1 AND
              修理品注文明細IFワーク.注文品注文番号2 = 修理品注文明細.注文品注文番号2 AND
              修理品注文明細IFワーク.注文品注文番号3 = 修理品注文明細.注文品注文番号3 AND
              修理品注文明細IFワーク.注文品項目番号 = 修理品注文明細.注文品項目番号
            */
            IF ICOUNT > 0 THEN
                SELECT
                /* 注文品作業承認日 */
                ORD_WORK_APPROVAL_DATE,
                /* 注文品見積確定金額 */
                ORD_ESTM_FIXED_AMT
                INTO
                T_ORD_WORK_APPROVAL_DATE,
                T_ORD_ESTM_FIXED_AMT
                FROM
                NAIS_T_REPAIR_ORDER_DT
                WHERE
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
                AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
                AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3
                AND ORD_ITEM_NO = EACHROW.ORD_ITEM_NO;

                /* 修理品注文明細.注文品作業承認日 IS NULL
                   修理品注文明細.注文品見積確定金額 ← 修理品注文明細IFワーク.注文品注文外貨金額

                */
                IF T_ORD_WORK_APPROVAL_DATE IS NULL THEN
                    /* 注文品見積確定金額 */
                    T_ORD_ESTM_FIXED_AMT := EACHROW.ORD_ORDER_FOREIGN_AMT;
                END IF;
                IF T_ORD_ESTM_FIXED_AMT IS NOT NULL AND I_ORD_ORDER_RAIT IS NOT NULL THEN
                    /* 注文品見積金額(邦貨) */
                    T_ORD_ESTM_YEN_AMT_1 := ROUND(T_ORD_ESTM_FIXED_AMT * I_ORD_ORDER_RAIT);
                END IF;

                /* 修理品注文明細のデータを更新する */
                UPDATE NAIS_T_REPAIR_ORDER_DT SET
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1,
                ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2,
                ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3,
                ORD_ITEM_NO = EACHROW.ORD_ITEM_NO,
                ORD_DATA_CLS = EACHROW.ORD_DATA_CLS,
                AA_JANO = EACHROW.AA_JANO,
                ORD_JANO_REG_CLS = EACHROW.ORD_JANO_REG_CLS,
                ORD_DEMAND_ITEM_NO = EACHROW.ORD_DEMAND_ITEM_NO,
                ORD_ORDER_PARTS_NO = EACHROW.ORD_ORDER_PARTS_NO,
                ORD_ARRIVAL_PARTS_NO = EACHROW.ORD_ARRIVAL_PARTS_NO,
                ORD_ORDER_PARTS_NM = EACHROW.ORD_ORDER_PARTS_NM,
                ORD_ARRIVAL_PARTS_NM = EACHROW.ORD_ARRIVAL_PARTS_NM,
                ORD_ORDER_SERIAL_NO = EACHROW.ORD_ORDER_SERIAL_NO,
                ORD_ARRIVAL_SERIAL_NO = EACHROW.ORD_ARRIVAL_SERIAL_NO,
                ORD_TAG1 = EACHROW.ORD_TAG1,
                ORD_TAG2 = EACHROW.ORD_TAG2,
                ORD_WORK_CLS = EACHROW.ORD_WORK_CLS,
                ORD_TROUBLE_FIND_CD = EACHROW.ORD_TROUBLE_FIND_CD,
                ORD_TROUBLE_FIND_NM = EACHROW.ORD_TROUBLE_FIND_NM,
                ORD_TROUBLE_CONDITION_CD = EACHROW.ORD_TROUBLE_CONDITION_CD,
                ORD_TROUBLE_CONDITION_NM = EACHROW.ORD_TROUBLE_CONDITION_NM,
                ORD_TT = EACHROW.ORD_TT,
                ORD_TT_PRINT_CLS = EACHROW.ORD_TT_PRINT_CLS,
                ORD_TSO_HOURS = EACHROW.ORD_TSO_HOURS,
                ORD_TSO_PRINT_CLS = EACHROW.ORD_TSO_PRINT_CLS,
                ORD_TSI_HOURS = EACHROW.ORD_TSI_HOURS,
                ORD_TSI_PRINT_CLS = EACHROW.ORD_TSI_PRINT_CLS,
                ORD_TAKE_OFF_AIRF_NO = EACHROW.ORD_TAKE_OFF_AIRF_NO,
                ORD_TAKE_OFF_SERIAL_NO = EACHROW.ORD_TAKE_OFF_SERIAL_NO,
                ORD_MEMO = EACHROW.ORD_MEMO,
                ORD_DESIRED_DLV_DATE = EACHROW.ORD_DESIRED_DLV_DATE,
                ORD_DLV_DATE = EACHROW.ORD_DLV_DATE,
                ORD_USE_AIRF_NM = EACHROW.ORD_USE_AIRF_NM,
                ATA_CD = EACHROW.ATA_CD,
                ORD_MODEL_ATA = EACHROW.ORD_MODEL_ATA,
                ORD_DEMAND_QTY = EACHROW.ORD_DEMAND_QTY,
                ORD_ORDER_QTY = EACHROW.ORD_ORDER_QTY,
                ORD_ORDER_UNIT_COST = EACHROW.ORD_ORDER_UNIT_COST,
                ORD_ORDER_FOREIGN_UNIT_COST = EACHROW.ORD_ORDER_FOREIGN_UNIT_COST,
                ORD_ORDER_AMT = EACHROW.ORD_ORDER_AMT,
                ORD_ORDER_FOREIGN_AMT = EACHROW.ORD_ORDER_FOREIGN_AMT,
                ORD_ARRIVAL_DATE = EACHROW.ORD_ARRIVAL_DATE,
                ORD_ARRIVAL_FIXED_QTY = EACHROW.ORD_ARRIVAL_FIXED_QTY,
                ORD_ARRIVAL_QTY = EACHROW.ORD_ARRIVAL_QTY,
                ORD_ARRIVAL_AMT = EACHROW.ORD_ARRIVAL_AMT,
                ORD_ARRIVAL_FOREIGN_AMT = EACHROW.ORD_ARRIVAL_FOREIGN_AMT,
                ORD_ARRIVAL_RAIT = EACHROW.ORD_ARRIVAL_RAIT,
                ORD_PARTITIAL_DLV_CNT = EACHROW.ORD_PARTITIAL_DLV_CNT,
                ORD_PAYMENT_DATE = EACHROW.ORD_PAYMENT_DATE,
                ORD_PAYMENT_QTY = EACHROW.ORD_PAYMENT_QTY,
                ORD_PAYMENT_YEN_AMT = EACHROW.ORD_PAYMENT_YEN_AMT,
                ORD_PAYMENT_FOREIGN_AMT = EACHROW.ORD_PAYMENT_FOREIGN_AMT,
                ORD_PAYMENT_ACTUAL_DATE = EACHROW.ORD_PAYMENT_ACTUAL_DATE,
                ORD_DT_TREATMENT_CLS = EACHROW.ORD_DT_TREATMENT_CLS,
                ORD_ISSURANCE_CLS = EACHROW.ORD_ISSURANCE_CLS,
                ORD_FIX_CLS = EACHROW.ORD_FIX_CLS,
                ORD_ABSENCE_CLS = EACHROW.ORD_ABSENCE_CLS,
                ORD_STS_CLS = EACHROW.ORD_STS_CLS,
                ORD_DATA_ENTRY_DATE = EACHROW.ORD_DATA_ENTRY_DATE,
                ORD_DATA_ENTRY_TIME = EACHROW.ORD_DATA_ENTRY_TIME,
                ORD_DATA_DELETE_DATE = EACHROW.ORD_DATA_DELETE_DATE,
                ORD_DATA_DELETE_TIME = EACHROW.ORD_DATA_DELETE_TIME,
                ORD_REFERENCE_KEY = EACHROW.ORD_REFERENCE_KEY,
                ORD_ESTM_FIXED_AMT = T_ORD_ESTM_FIXED_AMT,
                ORD_ESTM_YEN_AMT = T_ORD_ESTM_YEN_AMT_1,
                BAT_UPD_DATETIME = SYSDATE,
                BAT_UPD_USER_CD = EACHROW.UPD_USER_CD,
                UPD_FUNCTION_ID = EACHROW.UPD_FUNCTION_ID
                WHERE
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
                AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
                AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3
                AND ORD_ITEM_NO = EACHROW.ORD_ITEM_NO;
            ELSE
                IF EACHROW.ORD_ORDER_FOREIGN_AMT IS NOT NULL AND I_ORD_ORDER_RAIT IS NOT NULL THEN
                    /* 注文品見積金額(邦貨) */
                    T_ORD_ESTM_YEN_AMT_2 := ROUND(EACHROW.ORD_ORDER_FOREIGN_AMT * I_ORD_ORDER_RAIT);
                END IF;

                /*
                  注文品注文番号1~3,注文品項目番号が

                  修理品注文明細IFワークにのみ存在する場合は、

                  修理品注文明細にデータを作成する。

                */
                INSERT INTO NAIS_T_REPAIR_ORDER_DT(
                ORD_ORDER_NO_1,
                ORD_ORDER_NO_2,
                ORD_ORDER_NO_3,
                ORD_ITEM_NO,
                ORD_DATA_CLS,
                AA_JANO,
                ORD_JANO_REG_CLS,
                ORD_DEMAND_ITEM_NO,
                ORD_ORDER_PARTS_NO,
                ORD_ARRIVAL_PARTS_NO,
                ORD_ORDER_PARTS_NM,
                ORD_ARRIVAL_PARTS_NM,
                ORD_ORDER_SERIAL_NO,
                ORD_ARRIVAL_SERIAL_NO,
                ORD_TAG1,
                ORD_TAG2,
                ORD_WORK_CLS,
                ORD_TROUBLE_FIND_CD,
                ORD_TROUBLE_FIND_NM,
                ORD_TROUBLE_CONDITION_CD,
                ORD_TROUBLE_CONDITION_NM,
                ORD_TT,
                ORD_TT_PRINT_CLS,
                ORD_TSO_HOURS,
                ORD_TSO_PRINT_CLS,
                ORD_TSI_HOURS,
                ORD_TSI_PRINT_CLS,
                ORD_TAKE_OFF_AIRF_NO,
                ORD_TAKE_OFF_SERIAL_NO,
                ORD_MEMO,
                ORD_DESIRED_DLV_DATE,
                ORD_DLV_DATE,
                ORD_USE_AIRF_NM,
                ATA_CD,
                ORD_MODEL_ATA,
                ORD_DEMAND_QTY,
                ORD_ORDER_QTY,
                ORD_ORDER_UNIT_COST,
                ORD_ORDER_FOREIGN_UNIT_COST,
                ORD_ORDER_AMT,
                ORD_ORDER_FOREIGN_AMT,
                ORD_ARRIVAL_DATE,
                ORD_ARRIVAL_FIXED_QTY,
                ORD_ARRIVAL_QTY,
                ORD_ARRIVAL_AMT,
                ORD_ARRIVAL_FOREIGN_AMT,
                ORD_ARRIVAL_RAIT,
                ORD_PARTITIAL_DLV_CNT,
                ORD_PAYMENT_DATE,
                ORD_PAYMENT_QTY,
                ORD_PAYMENT_YEN_AMT,
                ORD_PAYMENT_FOREIGN_AMT,
                ORD_PAYMENT_ACTUAL_DATE,
                ORD_DT_TREATMENT_CLS,
                ORD_ISSURANCE_CLS,
                ORD_FIX_CLS,
                ORD_ABSENCE_CLS,
                ORD_STS_CLS,
                ORD_DATA_ENTRY_DATE,
                ORD_DATA_ENTRY_TIME,
                ORD_DATA_DELETE_DATE,
                ORD_DATA_DELETE_TIME,
                ORD_REFERENCE_KEY,
                ORD_WORK_CLS_NM,
                ORD_OANO,
                ORD_VENDER_PARTS_NO,
                ORD_QUOTE_NO,
                ORD_WORK_APPROVAL_DATE,
                ORD_TURN_AROUND_TIME_SCHDL,
                ORD_DMRNO,
                ORD_UNREPAIRABLE_AMT,
                ORD_RPR_REMARKS,
                ORD_RESERVE_COMMENT,
                ORD_TROUBLE_COMMENT_CLS,
                ORD_DLV_INVOICE_NO,
                ORD_SEND_DATE,
                ORD_SEND_POINT_CLS,
                ORD_DLV_AWB_NO,
                ORD_INPUT_FILE_NO,
                DOCUMENT_CD,
                ORD_ESTM_FIXED_AMT,
                ORD_ESTM_YEN_AMT,
                ORD_FACTORY_SHIPMENT_DATE,
                ORD_ARRIVAL_INVOICE_NO,
                ORD_RETURN_SITE_CLS,
                ORD_ARRIVAL_AWB_NO,
                ORD_ENTRY_FILE_NO,
                ORD_RPR_ARRIVAL_REMARKS,
                BAT_UPD_DATETIME,
                BAT_UPD_USER_CD,
                INS_FUNCTION_ID,
                INS_USER_CD,
                INS_DATETIME,
                UPD_FUNCTION_ID,
                UPD_USER_CD,
                UPD_DATETIME
                ) VALUES (
                EACHROW.ORD_ORDER_NO_1,
                EACHROW.ORD_ORDER_NO_2,
                EACHROW.ORD_ORDER_NO_3,
                EACHROW.ORD_ITEM_NO,
                EACHROW.ORD_DATA_CLS,
                EACHROW.AA_JANO,
                EACHROW.ORD_JANO_REG_CLS,
                EACHROW.ORD_DEMAND_ITEM_NO,
                EACHROW.ORD_ORDER_PARTS_NO,
                EACHROW.ORD_ARRIVAL_PARTS_NO,
                EACHROW.ORD_ORDER_PARTS_NM,
                EACHROW.ORD_ARRIVAL_PARTS_NM,
                EACHROW.ORD_ORDER_SERIAL_NO,
                EACHROW.ORD_ARRIVAL_SERIAL_NO,
                EACHROW.ORD_TAG1,
                EACHROW.ORD_TAG2,
                EACHROW.ORD_WORK_CLS,
                EACHROW.ORD_TROUBLE_FIND_CD,
                EACHROW.ORD_TROUBLE_FIND_NM,
                EACHROW.ORD_TROUBLE_CONDITION_CD,
                EACHROW.ORD_TROUBLE_CONDITION_NM,
                EACHROW.ORD_TT,
                EACHROW.ORD_TT_PRINT_CLS,
                EACHROW.ORD_TSO_HOURS,
                EACHROW.ORD_TSO_PRINT_CLS,
                EACHROW.ORD_TSI_HOURS,
                EACHROW.ORD_TSI_PRINT_CLS,
                EACHROW.ORD_TAKE_OFF_AIRF_NO,
                EACHROW.ORD_TAKE_OFF_SERIAL_NO,
                EACHROW.ORD_MEMO,
                EACHROW.ORD_DESIRED_DLV_DATE,
                EACHROW.ORD_DLV_DATE,
                EACHROW.ORD_USE_AIRF_NM,
                EACHROW.ATA_CD,
                EACHROW.ORD_MODEL_ATA,
                EACHROW.ORD_DEMAND_QTY,
                EACHROW.ORD_ORDER_QTY,
                EACHROW.ORD_ORDER_UNIT_COST,
                EACHROW.ORD_ORDER_FOREIGN_UNIT_COST,
                EACHROW.ORD_ORDER_AMT,
                EACHROW.ORD_ORDER_FOREIGN_AMT,
                EACHROW.ORD_ARRIVAL_DATE,
                EACHROW.ORD_ARRIVAL_FIXED_QTY,
                EACHROW.ORD_ARRIVAL_QTY,
                EACHROW.ORD_ARRIVAL_AMT,
                EACHROW.ORD_ARRIVAL_FOREIGN_AMT,
                EACHROW.ORD_ARRIVAL_RAIT,
                EACHROW.ORD_PARTITIAL_DLV_CNT,
                EACHROW.ORD_PAYMENT_DATE,
                EACHROW.ORD_PAYMENT_QTY,
                EACHROW.ORD_PAYMENT_YEN_AMT,
                EACHROW.ORD_PAYMENT_FOREIGN_AMT,
                EACHROW.ORD_PAYMENT_ACTUAL_DATE,
                EACHROW.ORD_DT_TREATMENT_CLS,
                EACHROW.ORD_ISSURANCE_CLS,
                EACHROW.ORD_FIX_CLS,
                EACHROW.ORD_ABSENCE_CLS,
                EACHROW.ORD_STS_CLS,
                EACHROW.ORD_DATA_ENTRY_DATE,
                EACHROW.ORD_DATA_ENTRY_TIME,
                EACHROW.ORD_DATA_DELETE_DATE,
                EACHROW.ORD_DATA_DELETE_TIME,
                EACHROW.ORD_REFERENCE_KEY,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                EACHROW.ORD_ORDER_FOREIGN_AMT,
                T_ORD_ESTM_YEN_AMT_2,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                NULL,
                SYSDATE,
                EACHROW.UPD_USER_CD,
                EACHROW.INS_FUNCTION_ID,
                EACHROW.INS_USER_CD,
                SYSDATE,
                EACHROW.UPD_FUNCTION_ID,
                EACHROW.UPD_USER_CD,
                SYSDATE
                );
            END IF;
        END LOOP;

        FOR EACHROW IN CUR4 LOOP
            SELECT
            COUNT(*) INTO ICOUNT
            FROM NAIS_I_REPAIR_ORDER_DT
            WHERE
            ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
            AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
            AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3
            AND ORD_ITEM_NO = EACHROW.ORD_ITEM_NO;

            /*
              注文品注文番号1~3,注文品項目番号が

              修理品注文明細にのみ存在する場合は、

              修理品注文明細のデータを削除する。

            */
            IF ICOUNT < 1 THEN
                /* 修理品注文明細のデータを削除する */
                DELETE FROM NAIS_T_REPAIR_ORDER_DT
                WHERE
                ORD_ORDER_NO_1 = EACHROW.ORD_ORDER_NO_1
                AND ORD_ORDER_NO_2 = EACHROW.ORD_ORDER_NO_2
                AND ORD_ORDER_NO_3 = EACHROW.ORD_ORDER_NO_3
                AND ORD_ITEM_NO = EACHROW.ORD_ITEM_NO;
            END IF;
        END LOOP;
    END;

    /* 正常終了 */
    RETURN(0);

    EXCEPTION
        WHEN NO_DATA_FOUND
        THEN
            RETURN( -1403 );
        WHEN OTHERS
        THEN
            DECLARE
            err_code NUMBER:= SQLCODE;
            err_msg VARCHAR2(300) := SQLERRM;
            BEGIN
                RETURN( err_code );
            END;
    END;
END;
/