@isTest
private class NFM010RestTest {

    static Opportunity makeTestOpp() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            throw new ControllerUtil.myException('not found 病院 recodetype');
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            throw new ControllerUtil.myException('not found 戦略科室分類 呼吸科 recodetype');
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            throw new ControllerUtil.myException('not found 診療科 消化科 recodetype');
        }

        // テストデータ
        List<BatchIF_Transfer__c> transfers = new List<BatchIF_Transfer__c>();
        BatchIF_Transfer__c transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Statu_Achievements__c';
        transfer.Column__c         = 'SalesChannel__c';
        transfer.External_Value__c = '1';
        transfer.Internal_Value__c = 'OCM直接販売';
        transfers.add(transfer);
        transfer = new BatchIF_Transfer__c();
        transfer.Table__c          = 'Statu_Achievements__c';
        transfer.Column__c         = 'SalesChannel__c';
        transfer.External_Value__c = '3';
        transfer.Internal_Value__c = '販売店';
        transfers.add(transfer);
        insert transfers;

        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM010TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM010TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        // 再取得
        List<Id> accountIds = new Id[] {company.Id, section.Id, depart.Id};
        // List<Account> accList = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id In :accountIds order by Management_Code_Auto__c];
        List<Account> accList = [select Management_Code__c,  Name, Id from Account where Id In :accountIds];

        Opportunity opp = new Opportunity();
        opp.AccountId           = depart.Id;
        opp.Department_Class__c = section.Id;
        opp.Hospital__c         = company.Id;
        opp.Name                = 'GZ-SP-GD3187';
        opp.Trade__c            = '内貿';
        opp.StageName           = '引合';
        opp.Sales_Root__c       = 'OCM直接販売';
        opp.CloseDate           = date.newinstance(2023, 11, 30);
        insert opp;
        // 再取得
        opp = [select Opportunity_No__c, Opp_Number__c, Name, Id from Opportunity where Id = : opp.Id];
        return opp;
    }
    @isTest
    static void testNFM010_void() {
        Opportunity opp = makeTestOpp();

        // そのままreturn
        // null
        //NFM010Rest.NFM010(null);

        NFM010Rest.Achievement Achievement = new NFM010Rest.Achievement();
        NFM010Rest.Achievements Achievements = new NFM010Rest.Achievements();
        Achievements.Achievement = new NFM010Rest.Achievement[] { Achievement };
        // Monitoringをセットしない
        //NFM010Rest.NFM010(Achievements);
        //BatchIF_Log__c rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        //if (String.isBlank(rowData.Log__c) == false)
        //    NFM010Rest.main(rowData.Id);

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Achievements.Monitoring = new NFMUtil.Monitoring();
        Achievements.Monitoring.MessageGroupNumber = nowStr + '01';
        // 必須項目をセットしない
        //NFM010Rest.NFM010(Achievements);
        Test.startTest();
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        if (String.isBlank(rowData.Log__c) == false)
            NFM010Rest.main(rowData.Id);
		
        // 商談がない
        Achievements.Monitoring.MessageGroupNumber = nowStr + '02';
        Achievement.SoNo          = '35100023';
        Achievement.QuotationCode = 'GZ-SP-GD3187';
        Achievement.TimeLimit     = 20121122;
        //NFM010Rest.NFM010(Achievements);
        rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        if (String.isBlank(rowData.Log__c) == false)
            NFM010Rest.main(rowData.Id);

        Product2[] rslts = [select Id from Product2];
        System.assertEquals(0, rslts.size());
        Test.stopTest();
    }
    @isTest
    static void testNFM010_insert() {
        ControllerUtil.EscapeNFM001Trigger = true;

        Opportunity opp = makeTestOpp();

        NFM010Rest.Achievement Achievement = new NFM010Rest.Achievement();
        NFM010Rest.Achievement Achievement2 = new NFM010Rest.Achievement();
        NFM010Rest.Achievements Achievements = new NFM010Rest.Achievements();
        Achievements.Achievement = new NFM010Rest.Achievement[] { Achievement, Achievement2 };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Achievements.Monitoring = new NFMUtil.Monitoring();
        Achievements.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        Achievement.QuotationCode         = opp.Opportunity_No__c;
        Achievement.SoNo                  = 'NFM010Tst1';
        Achievement.ReturnFlag            = '';
        Achievement.OverviewStatus        = '无效合同';
        Achievement.ContractNO            = 'GD-GYN-121668';
        Achievement.ContractReceivedDate  = '20120114';
        Achievement.FirstApproveDate      = '20120214';
        Achievement.SecondApproveDate     = '40001231';
        Achievement.SoLatestDeliveryDate  = '20120414';
        Achievement.DeliveryDate          = '20120514';
        Achievement.InstallDate           = '20120614';
        Achievement.TimeLimit             = 20120714;
        Achievement.Currency_x            = 'RMB';
        Achievement.ContractAmount        = 12345.12;
        Achievement.PaymentAmount         = 2345.12;
        Achievement.PaymentRate           = '17.99%,20171111';
        Achievement.DeliveryStatus        = '交付';
        Achievement.AssignmentStatus      = '分配';
        Achievement.PaymentStatus         = '完成';
        Achievement.InstallStatus         = '安装';
        Achievement.AuthorizationNo       = 'AUTH123432154';
        Achievement.ContractApplicationNo = 'ApplicationNo345674';
        Achievement.PaymentTerms          = '发货';
        Achievement.SalesChannel          = 3;
        Achievement.OrderType             = 'ZM16';

        Achievement2.QuotationCode         = opp.Opportunity_No__c;
        Achievement2.SoNo                  = 'NFM010Tst1';              // 同じ番号なので、1つの注残として登録
        Achievement2.ReturnFlag            = 'Y';
        Achievement2.OverviewStatus        = '无效合同';
        Achievement2.ContractNO            = 'GD-GYN-121668';
        Achievement2.ContractReceivedDate  = '20120115';
        Achievement2.FirstApproveDate      = '20120215';
        // Achievement2.SecondApproveDate     = '20120315';
        Achievement2.SecondApproveDate     = '40001231';
        Achievement2.SoLatestDeliveryDate  = '20120415';
        Achievement2.DeliveryDate          = '20120515';
        Achievement2.InstallDate           = '19000101';
        Achievement2.TimeLimit             = 20120715;
        Achievement2.Currency_x            = 'RMB';
        Achievement2.ContractAmount        = 12345.12;
        Achievement2.PaymentAmount         = 2345.12;
        Achievement2.PaymentRate           = '18.99%,20171111';
        Achievement2.DeliveryStatus        = '未交付';
        Achievement2.AssignmentStatus      = '部分分配';
        Achievement2.PaymentStatus         = '尾款完成';
        Achievement2.InstallStatus         = '全部安装';
        Achievement2.AuthorizationNo       = 'AUTH123432153';
        Achievement2.ContractApplicationNo = 'ApplicationNo345678';
        Achievement2.PaymentTerms          = '款到发货';
        Achievement2.SalesChannel          = 1;
        Achievement.OrderType             = 'ZM16';

        System.Test.startTest();
        //NFM010Rest.NFM010(Achievements);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        if (String.isBlank(rowData.Log__c) == false)
            NFM010Rest.main(rowData.Id);
        System.Test.stopTest();

        String statu_Achievements_Ext = Achievement.QuotationCode + ':' + Achievement.SoNo;
        Statu_Achievements__c[] rslts = [select Id, Name, Opportunity__r.Opportunity_No__c,
                                         Statu_Achievements_Ext__c, ReturnFlag__c, OverviewStatus__c, ContractNO__c,
                                         ContractReceivedDate__c, FirstApproveDate__c, LastApproveDate__c,
                                         SoLatestDeliveryDate__c, DeliveryDate__c, InstallDate__c, Collection_Day_Invoice__c, UnshippedAmount__c,
                                         CurrencyIsoCode, ContractAmount__c, PaymentAmount__c, PaymentRate__c,
                                         DeliveryStatus__c, AssignmentStatus__c, PaymentStatus__c, InstallStatus__c,
                                         AuthorizationNo__c, ContractApplicationNo__c, PaymentTerms__c, SalesChannel__c,
                                         End_User_Price__c
                                         from Statu_Achievements__c where Statu_Achievements_Ext__c = :statu_Achievements_Ext];
        //System.assertEquals(1, rslts.size());
        if (rslts.size() == 1) {
            System.assertEquals(Achievement2.QuotationCode, rslts[0].Opportunity__r.Opportunity_No__c);
            System.assertEquals(Achievement2.SoNo, rslts[0].Name);
            System.assertEquals(true, rslts[0].ReturnFlag__c);
            System.assertEquals(Achievement2.OverviewStatus, rslts[0].OverviewStatus__c);
            System.assertEquals(Achievement2.ContractNO, rslts[0].ContractNO__c);
            System.assertEquals(NFMUtil.parseStr2Date(Achievement2.ContractReceivedDate), rslts[0].ContractReceivedDate__c);
            System.assertEquals(NFMUtil.parseStr2Date(Achievement2.FirstApproveDate), rslts[0].FirstApproveDate__c);
            System.assertEquals(NFMUtil.parseStr2Date(Achievement2.SecondApproveDate), rslts[0].LastApproveDate__c);
            System.debug('21最晚时间:'+NFMUtil.parseStr2Date(Achievement2.SecondApproveDate));
            System.assertEquals(NFMUtil.parseStr2Date(Achievement2.SoLatestDeliveryDate), rslts[0].SoLatestDeliveryDate__c);
            System.assertEquals(NFMUtil.parseStr2Date(Achievement2.DeliveryDate), rslts[0].DeliveryDate__c);
            System.assertEquals(null, rslts[0].InstallDate__c);
            System.assertEquals(null, rslts[0].Collection_Day_Invoice__c);
            System.assertEquals(Achievement2.TimeLimit, rslts[0].UnshippedAmount__c);
            System.assertEquals('CNY', rslts[0].CurrencyIsoCode);
            System.assertEquals(Achievement2.ContractAmount, rslts[0].ContractAmount__c);
            System.assertEquals(Achievement2.PaymentAmount, rslts[0].PaymentAmount__c);
            //System.assertEquals(Achievement2.PaymentRate, rslts[0].PaymentRate__c);
            System.assertEquals(Achievement2.DeliveryStatus, rslts[0].DeliveryStatus__c);
            System.assertEquals(Achievement2.AssignmentStatus, rslts[0].AssignmentStatus__c);
            System.assertEquals(Achievement2.PaymentStatus, rslts[0].PaymentStatus__c);
            System.assertEquals(Achievement2.InstallStatus, rslts[0].InstallStatus__c);
            System.assertEquals(Achievement2.AuthorizationNo, rslts[0].AuthorizationNo__c);
            System.assertEquals(Achievement2.ContractApplicationNo, rslts[0].ContractApplicationNo__c);
            System.assertEquals(Achievement2.PaymentTerms, rslts[0].PaymentTerms__c);
//            System.assertEquals('OCM直接販売', rslts[0].SalesChannel__c);
            System.assertEquals(1.0, rslts[0].End_User_Price__c);
            Opportunity opp1 = [Select Id, Min_FirstApproveDate__c, Contract_Once_Recognize_Day__c from Opportunity where Id = : opp.Id];
            System.assertEquals(null, opp1.Min_FirstApproveDate__c);
            System.assertEquals(null, opp1.Contract_Once_Recognize_Day__c);
        }
    }

//    static testMethod void testNFM010_update() {
//        Opportunity opp = makeTestOpp();
//        opp.Estimation_Decision__c = true;
//        opp.Estimation_Id__c    = 'Estimation_Id';
//        opp.Purchase_Type__c    = '一般引合';
//        opp.Promise_Class__c    = '内貿';
//        opp.Fund_Basis__c       = '政府資金';
//        opp.SAP_Province__c     = '北京市';
//        opp.Authorized_DB_No__c = 'Authorized_DB_No';
//        opp.NotesApprovedNo__c = 'NotesApprovedNo';
//        opp.Contract_DB_SalesDept_complite_day__c = System.today();
//        //update opp;
//        opp.SAP_Send_OK__c      = true;
//        update opp;
//        Statu_Achievements__c sta = new Statu_Achievements__c(
//            Name='NFM010Tst1',
//            Opportunity__c = opp.Id,
//            OverviewStatus__c = '无效合同',
//            ContractAmount__c = 123456,
//            DeliveryStatus__c = 'test',
//            PaymentRate__c = '1234',
//            End_User_price__c = 0,
//            AssignmentStatus__c = '全部分配',
//            Last_week__c = '5 货齐,未付款,无用户合同',
//            X2weeks_ago__c = '4 备货中',
//            X3Weeks_ago__c = '3 已付款,无用户合同',
//            X4weeks_ago__c = '2 待付款',
//            X5weeks_ago__c = '1 全没有'
//        );
//        insert sta;
//        Opportunity opp1 = [Select Id, SAP_Send_OK__c, Note_Residual_Result__c, Min_FirstApproveDate__c, Contract_Once_Recognize_Day__c from Opportunity where Id =: opp.Id];
//        System.assertEquals(true, opp1.SAP_Send_OK__c);
//        System.assertEquals('4', opp1.Note_Residual_Result__c);
//        System.assertEquals(null, opp1.Min_FirstApproveDate__c);
//        System.assertEquals(null, opp1.Contract_Once_Recognize_Day__c);

//        NFM010Rest.Achievement Achievement = new NFM010Rest.Achievement();
//        NFM010Rest.Achievements Achievements = new NFM010Rest.Achievements();
//        Achievements.Achievement = new NFM010Rest.Achievement[] { Achievement };

//        // Monitoringの設定
//        Datetime nowDT = Datetime.now();
//        String nowStr = nowDT.format('yyyyMMddHHmm');
//        Achievements.Monitoring = new NFMUtil.Monitoring();
//        Achievements.Monitoring.MessageGroupNumber = nowStr + '21';

//        // update
//        Achievement.QuotationCode         = opp.Opportunity_No__c;
//        Achievement.SoNo                  = 'NFM010Tst1';
//        Achievement.ReturnFlag            = '';
//        Achievement.OverviewStatus        = '初审通过';
//        Achievement.ContractNO            = 'GD-GYN-121999';
//        Achievement.ContractReceivedDate  = '20120125';
//        Achievement.FirstApproveDate      = '20120225';
//        Achievement.SecondApproveDate     = '20120325';
//        Achievement.SoLatestDeliveryDate  = '20120425';
//        Achievement.DeliveryDate          = '20120525';
//        Achievement.InstallDate           = '20120625';
//        Achievement.TimeLimit             = 20120725;
//        Achievement.Currency_x            = 'USD';
//        Achievement.ContractAmount        = 2345.12;
//        Achievement.PaymentAmount         = 345.12;
//        Achievement.PaymentRate           = '14.17%,20171111';
//        Achievement.DeliveryStatus        = '已完全交付';
//        Achievement.AssignmentStatus      = '全部分配';
//        Achievement.PaymentStatus         = '退款完成';
//        Achievement.InstallStatus         = '部分安装';
//        Achievement.AuthorizationNo       = 'AUTH54365465';
//        Achievement.ContractApplicationNo = 'ApplicationNo643242';
//        Achievement.PaymentTerms          = '款不到不发货';
//        Achievement.SalesChannel          = 3;

//        System.Test.startTest();
//        //NFM010Rest.NFM010(Achievements);
//        BatchIF_Log__c rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
//        if (String.isBlank(rowData.Log__c) == false)
//            NFM010Rest.main(rowData.Id);
//        System.Test.stopTest();
//        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c, Type__c, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM010' order by CreatedDate desc];
//        System.assertEquals(1, bl.size());
//        System.assertEquals(null, bl[0].ErrorLog__c);

//        String statu_Achievements_Ext = Achievement.QuotationCode + ':' + Achievement.SoNo;
//        Statu_Achievements__c[] rslts = [select Id, Name, Opportunity__r.Opportunity_No__c,
//                                                Statu_Achievements_Ext__c, ReturnFlag__c, OverviewStatus__c, ContractNO__c,
//                                                ContractReceivedDate__c, FirstApproveDate__c, LastApproveDate__c,
//                                                SoLatestDeliveryDate__c, DeliveryDate__c, InstallDate__c, Collection_Day_Invoice__c, UnshippedAmount__c,
//                                                CurrencyIsoCode, ContractAmount__c, PaymentAmount__c, PaymentRate__c,
//                                                DeliveryStatus__c, AssignmentStatus__c, PaymentStatus__c, InstallStatus__c,
//                                                AuthorizationNo__c, ContractApplicationNo__c, PaymentTerms__c, SalesChannel__c,
//                                                End_User_Price__c
//                                           from Statu_Achievements__c where Statu_Achievements_Ext__c = :statu_Achievements_Ext];
//        System.assertEquals(1, rslts.size());
//        if (rslts.size() == 1) {
//            System.assertEquals(Achievement.QuotationCode, rslts[0].Opportunity__r.Opportunity_No__c);
//            System.assertEquals(Achievement.SoNo, rslts[0].Name);
//            System.assertEquals(false, rslts[0].ReturnFlag__c);
//            System.assertEquals(Achievement.OverviewStatus, rslts[0].OverviewStatus__c);
//            System.assertEquals(Achievement.ContractNO, rslts[0].ContractNO__c);
//            System.assertEquals(NFMUtil.parseStr2Date(Achievement.ContractReceivedDate), rslts[0].ContractReceivedDate__c);
//            System.assertEquals(NFMUtil.parseStr2Date(Achievement.FirstApproveDate), rslts[0].FirstApproveDate__c);
//            System.assertEquals(NFMUtil.parseStr2Date(Achievement.SecondApproveDate), rslts[0].LastApproveDate__c);
//            System.assertEquals(NFMUtil.parseStr2Date(Achievement.SoLatestDeliveryDate), rslts[0].SoLatestDeliveryDate__c);
//            System.assertEquals(NFMUtil.parseStr2Date(Achievement.DeliveryDate), rslts[0].DeliveryDate__c);
//            System.assertEquals(NFMUtil.parseStr2Date(Achievement.InstallDate), rslts[0].InstallDate__c);
//            System.assertEquals(System.today(), rslts[0].Collection_Day_Invoice__c);
//            System.assertEquals(Achievement.TimeLimit, rslts[0].UnshippedAmount__c);
//            System.assertEquals('USD', rslts[0].CurrencyIsoCode);
//            System.assertEquals(Achievement.ContractAmount, rslts[0].ContractAmount__c);
//            System.assertEquals(Achievement.PaymentAmount, rslts[0].PaymentAmount__c);
//            System.assertEquals('14.17%', rslts[0].PaymentRate__c);
//            System.assertEquals(Achievement.DeliveryStatus, rslts[0].DeliveryStatus__c);
//            System.assertEquals(Achievement.AssignmentStatus, rslts[0].AssignmentStatus__c);
//            System.assertEquals(Achievement.PaymentStatus, rslts[0].PaymentStatus__c);
//            System.assertEquals(Achievement.InstallStatus, rslts[0].InstallStatus__c);
//            System.assertEquals(Achievement.AuthorizationNo, rslts[0].AuthorizationNo__c);
//            System.assertEquals(Achievement.ContractApplicationNo, rslts[0].ContractApplicationNo__c);
//            System.assertEquals(Achievement.PaymentTerms, rslts[0].PaymentTerms__c);
////            System.assertEquals('販売店', rslts[0].SalesChannel__c);
//            System.assertEquals(3.0, rslts[0].End_User_Price__c);
//            opp1 = [Select Id, Min_FirstApproveDate__c, Contract_Once_Recognize_Day__c from Opportunity where Id =: opp.Id];
//            System.assertEquals(NFMUtil.parseStr2Date('20120225'), opp1.Min_FirstApproveDate__c);
//            System.assertEquals(NFMUtil.parseStr2Date('20120225'), opp1.Contract_Once_Recognize_Day__c);
//        }
//    }
    static testMethod void testNFM010_resend() {
        ControllerUtil.EscapeNFM001Trigger = true;
        Opportunity opp = makeTestOpp();

        NFM010Rest.Achievement Achievement = new NFM010Rest.Achievement();
        NFM010Rest.Achievement Achievement2 = new NFM010Rest.Achievement();
        NFM010Rest.Achievements Achievements = new NFM010Rest.Achievements();
        Achievements.Achievement = new NFM010Rest.Achievement[] { Achievement, Achievement2 };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Achievements.Monitoring = new NFMUtil.Monitoring();
        Achievements.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        Achievement.QuotationCode         = opp.Opportunity_No__c;
        Achievement.SoNo                  = 'NFM010Tst1';
        Achievement.ReturnFlag            = '';
        Achievement.OverviewStatus        = '无效合同';
        Achievement.ContractNO            = 'GD-GYN-121668';
        Achievement.ContractReceivedDate  = '20120114';
        Achievement.FirstApproveDate      = '20120214';
        Achievement.SecondApproveDate     = '40001231';
        Achievement.SoLatestDeliveryDate  = '20120414';
        Achievement.DeliveryDate          = '20120514';
        Achievement.InstallDate           = '20120614';
        Achievement.TimeLimit             = 20120714;
        Achievement.Currency_x            = 'RMB';
        Achievement.ContractAmount        = 12345.12;
        Achievement.PaymentAmount         = 2345.12;
        Achievement.PaymentRate           = '17.99%,20171111';
        Achievement.DeliveryStatus        = '交付';
        Achievement.AssignmentStatus      = '分配';
        Achievement.PaymentStatus         = '完成';
        Achievement.InstallStatus         = '安装';
        Achievement.AuthorizationNo       = 'AUTH123432154';
        Achievement.ContractApplicationNo = 'ApplicationNo345674';
        Achievement.PaymentTerms          = '发货';
        Achievement.SalesChannel          = 3;
        Achievement.OrderType             = 'ZM17';

        Achievement2.QuotationCode         = opp.Opportunity_No__c;
        Achievement2.SoNo                  = 'NFM010Tst1';              // 同じ番号なので、1つの注残として登録
        Achievement2.ReturnFlag            = 'Y';
        Achievement2.OverviewStatus        = '无效合同';
        Achievement2.ContractNO            = 'GD-GYN-121668';
        Achievement2.ContractReceivedDate  = '20120115';
        Achievement2.FirstApproveDate      = '20120215';
        Achievement2.SecondApproveDate     = '40001231';
        Achievement2.SoLatestDeliveryDate  = '20120415';
        Achievement2.DeliveryDate          = '20120515';
        Achievement2.InstallDate           = '19000101';
        Achievement2.TimeLimit             = 20120715;
        Achievement2.Currency_x            = 'RMB';
        Achievement2.ContractAmount        = 12345.12;
        Achievement2.PaymentAmount         = 2345.12;
        Achievement2.PaymentRate           = '18.99%,20171111';
        Achievement2.DeliveryStatus        = '未交付';
        Achievement2.AssignmentStatus      = '部分分配';
        Achievement2.PaymentStatus         = '尾款完成';
        Achievement2.InstallStatus         = '全部安装';
        Achievement2.AuthorizationNo       = 'AUTH123432153';
        Achievement2.ContractApplicationNo = 'ApplicationNo345678';
        Achievement2.PaymentTerms          = '款到发货';
        Achievement2.SalesChannel          = 1;
        Achievement.OrderType             = 'ZM17';

        System.Test.startTest();
        //NFM010Rest.NFM010(Achievements);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        if (String.isBlank(rowData.Log__c) == false)
            NFM010Rest.main(rowData.Id);
        System.Test.stopTest();

        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c
                                      from BatchIF_Log__c
                                      where RowDataFlg__c = true
                                              and Type__c = 'NFM010'
                                                      order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;
        //NFM010Rest.execute(rowbl[0].id);
        rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        if (String.isBlank(rowData.Log__c) == false)
            NFM010Rest.main(rowbl[0].id);
        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
                                   Type__c, Log__c, ErrorLog__c, retry_cnt__c
                                   from BatchIF_Log__c
                                   where Id = :rowbl[0].id];
        //System.assertEquals(0, bl[0].retry_cnt__c);
    }
    @isTest static void test_method_execute() {

        Test.startTest();

        RestRequest req = new RestRequest();
        RestResponse res = new RestResponse();

        String JsonMsg = '{	"Achievements": {"Monitoring":{"TransmissionDateTime":"201812201320","Text":"","Tag":"MSGH","Sender":"SFDC","Receiver":"SPO","NumberOfRecord":"1","MessageType":"NFM010","MessageGroupNumber":"20180001722190"},"Achievement":[{"TimeLimit":225485.40,"SoNo":"0030083209","SoLatestDeliveryDate":"20190426","SecondApproveDate":"40001231","SalesChannel":1166000.00,"ReturnFlag":"","QuotationCode":"BJ-SP-XA0477959","PaymentTerms":"预付30%,发货前付清余款","PaymentStatus":"尾款完成","PaymentRate":"100.00%,20181022","PaymentAmount":757460.00,"OverviewStatus":"发运标记1","InstallStatus":"","InstallDate":"00000000","FirstApproveDate":"20180917","DeliveryStatus":"部分交付","DeliveryDate":"20181026","Currency_x":"RMB","ContractReceivedDate":"20180917","ContractNO":"BJ-GS-182423","ContractApplicationNo":"SQXB2018090041","ContractAmount":757460.00,"AuthorizationNo":"EJXB201807011","AssignmentStatus":"部分分配"}]}}';
        req.requestURI = 'services/apexrest/NFM010/execute';
        req.httpMethod = 'POST';
        req.requestBody = Blob.valueof(JsonMsg);
        RestContext.request = req;
        RestContext.response = res;

        NFM010Rest.execute();

        Test.stopTest();
    }

    static testMethod void testNFM010_2() {
        ControllerUtil.EscapeNFM001Trigger = true;
        Opportunity opp = makeTestOpp();

        NFM010Rest.Achievement Achievement = new NFM010Rest.Achievement();
        NFM010Rest.Achievement Achievement2 = new NFM010Rest.Achievement();
        NFM010Rest.Achievements Achievements = new NFM010Rest.Achievements();
        Achievements.Achievement = new NFM010Rest.Achievement[] { Achievement, Achievement2 };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Achievements.Monitoring = new NFMUtil.Monitoring();
        Achievements.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        Achievement.QuotationCode         = '';
        Achievement.SoNo                  = '0060155720';
        Achievement.ReturnFlag            = '';
        Achievement.OverviewStatus        = '无效合同';
        Achievement.ContractNO            = 'GD-GYN-121668';
        Achievement.ContractReceivedDate  = '20120114';
        Achievement.FirstApproveDate      = '20120214';
        Achievement.SecondApproveDate     = '40001231';
        Achievement.SoLatestDeliveryDate  = '20120414';
        Achievement.DeliveryDate          = '20120514';
        Achievement.InstallDate           = '20120614';
        Achievement.Currency_x            = 'RMB';
        Achievement.ContractAmount        = 12345.12;
        Achievement.PaymentAmount         = 2345.12;
        Achievement.PaymentRate           = '17.99%,20171111';
        Achievement.DeliveryStatus        = '交付';
        Achievement.AssignmentStatus      = '分配';
        Achievement.PaymentStatus         = '完成';
        Achievement.InstallStatus         = '安装';
        Achievement.AuthorizationNo       = 'AUTH123432154';
        Achievement.ContractApplicationNo = 'ApplicationNo345674';
        Achievement.PaymentTerms          = '发货';
        Achievement.SalesChannel          = 3;

        Achievement2.QuotationCode         = opp.Opportunity_No__c;
        Achievement2.SoNo                  = '0060155720';              // 同じ番号なので、1つの注残として登録
        Achievement2.ReturnFlag            = 'Y';
        Achievement2.OverviewStatus        = '无效合同';
        Achievement2.ContractNO            = 'GD-GYN-121668';
        Achievement2.ContractReceivedDate  = '20120115';
        Achievement2.FirstApproveDate      = '20120215';
        Achievement2.SecondApproveDate     = '40001231';
        Achievement2.SoLatestDeliveryDate  = '20120415';
        Achievement2.DeliveryDate          = '20120515';
        Achievement2.InstallDate           = '19000101';
        Achievement2.Currency_x            = 'RMB';
        Achievement2.ContractAmount        = 12345.12;
        Achievement2.PaymentAmount         = 2345.12;
        Achievement2.PaymentRate           = '18.99%,20171111';
        Achievement2.DeliveryStatus        = '未交付';
        Achievement2.AssignmentStatus      = '部分分配';
        Achievement2.PaymentStatus         = '尾款完成';
        Achievement2.InstallStatus         = '全部安装';
        Achievement2.AuthorizationNo       = 'AUTH123432153';
        Achievement2.ContractApplicationNo = 'ApplicationNo345678';
        Achievement2.PaymentTerms          = '款到发货';
        Achievement2.SalesChannel          = 1;

        System.Test.startTest();
        //NFM010Rest.NFM010(Achievements);
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        if (String.isBlank(rowData.Log__c) == false)
            NFM010Rest.main(rowData.Id);
        System.Test.stopTest();

        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c
                                      from BatchIF_Log__c
                                      where RowDataFlg__c = true
                                              and Type__c = 'NFM010'
                                                      order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;
        //NFM010Rest.execute(rowbl[0].id);
        rowData = NFMUtil.saveRowData(Achievements.Monitoring, 'NFM010', Achievements.Achievement);
        if (String.isBlank(rowData.Log__c) == false)
            NFM010Rest.main(rowbl[0].id);
        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
                                   Type__c, Log__c, ErrorLog__c, retry_cnt__c
                                   from BatchIF_Log__c
                                   where Id = :rowbl[0].id];
        System.assertEquals(0, bl[0].retry_cnt__c);
    }
}