@isTest
private class NFM110WebServiceTest {
    static Account company;
    static Account section1;        // 呼吸科
    static Account section2;        // 消化科
    static Account olympus;
    static Account olympus1;        // 呼吸科
    static Account olympus2;        // 消化科
    static Account depart1;
    static Account depart2;

    static void initHpData() {
        RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        RecordType rectDpt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        RecordType rectDpt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        // テストデータ
        company = new Account(RecordTypeId = rectCo.Id, Name = 'NFM110TestCompany');
        olympus = new Account(RecordTypeId = rectCo.Id, AgentCode_Ext__c = '9999999', Name = 'olympus');
        insert new Account[] {company, olympus};
        company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id =:company.Id];
        section1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId =:company.Id and RecordType.name = '戦略科室分類 呼吸科'];
        section2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId =:company.Id and RecordType.name = '戦略科室分類 消化科'];
        olympus1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId =:olympus.Id and RecordType.name = '戦略科室分類 呼吸科'];
        olympus2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId =:olympus.Id and RecordType.name = '戦略科室分類 消化科'];
System.debug('section1.Department_Class_Label__c='+section1.Department_Class_Label__c);
System.debug('section2.Department_Class_Label__c='+section2.Department_Class_Label__c);
        depart1 = new Account(RecordTypeId = rectDpt1.Id, Name = '*', ParentId  = section1.Id, Department_Class__c = section1.Id, Hospital__c = company.Id,
                Department_Name__c = 'NFM110TestDepart1');
        depart2 = new Account(RecordTypeId = rectDpt2.Id, Name = '*', ParentId  = section2.Id, Department_Class__c = section2.Id, Hospital__c = company.Id,
                Department_Name__c = 'NFM110TestDepart2');
        Account ocm = new Account(RecordTypeId = rectDpt2.Id, Name = '*', ParentId  = section2.Id, Department_Class__c = section2.Id, Hospital__c = company.Id,
                AgentCode_Ext__c = '9999900', Department_Name__c = 'NFM110TestDepart3');
        Account olympus_return = new Account(RecordTypeId = rectDpt2.Id, Name = '*', ParentId  = olympus2.Id, Department_Class__c = olympus2.Id, Hospital__c = olympus.Id,
                AgentCode_Ext__c = '9999901', Department_Name__c = 'olympus_return');
        insert new Account[] {depart1, depart2, ocm, olympus_return};
        depart1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id =:depart1.Id];
        depart2 = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id =:depart2.Id];
    }
    
    @isTest
    static void test_void() {
        initHpData();
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;
        Product2__c pro1 = new Product2__c(Name='NFM110Prd1',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;
        // null
        NFM110WebService.NFM110(null);

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        // Monitoringをセットしない
        NFM110WebService.NFM110(ProductsDelivery);
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';
        // GeneralDataをセットしない
        NFM110WebService.NFM110(ProductsDelivery);

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        // generalData1.DeliveryNoteをセットしない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '02';
        NFM110WebService.NFM110(ProductsDelivery);

        generalData1.DeliveryNote = 'DeliveryNote';
        // DnInformationをセットしない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '03';
        NFM110WebService.NFM110(ProductsDelivery);

        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        // 病院と診療科の管理コードをセットしない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '04';
        NFM110WebService.NFM110(ProductsDelivery);

        generalData1.EndUserNo       = 'NFM110TestABCD';
        generalData1.DepartmentNo    = 'NFM110TestDCBA';
        // 病院と診療科の管理コードが存在しない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '05';
        NFM110WebService.NFM110(ProductsDelivery);
        generalData1.EndUserNo       = company.Management_Code_Auto__c;
        // 診療科の管理コードが存在しない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '06';
        NFM110WebService.NFM110(ProductsDelivery);

        generalData1.EndUserNo       = company.Management_Code_Auto__c;
        generalData1.DepartmentNo    = depart1.Management_Code_Auto__c;
        // DnInformationの必須項目をセットしない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '07';
        NFM110WebService.NFM110(ProductsDelivery);

        dnInfo1.SorLMark        = 'S';
        // DnInformationの必須項目をセットしない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '08';
        NFM110WebService.NFM110(ProductsDelivery);

        dnInfo1.OTCode          = 'NFM110Prd1';
        // DnInformationの必須項目をセットしない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '09';
        NFM110WebService.NFM110(ProductsDelivery);

        dnInfo1.OTCode          = 'NFM110TstPrd1234';
        dnInfo1.SerialNoorLotNo = '2577010001675';
        // 商品コードが存在しない
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '10';
        NFM110WebService.NFM110(ProductsDelivery);

        Asset[] rslts = [select Id from Asset];
        System.assertEquals(0, rslts.size());
    }
    
    @isTest
    static void test_insert() {
        initHpData();
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;
        Product2__c pro1 = new Product2__c(Name='NFM110Prd1',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();

        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        dnInfo1.SorLMark        = 'S';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.SerialNoorLotNo = '2577010001675';

        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Asset[] rslts = [select Id, Return_Flag__c, AccountId, Department_Class__c, Hospital__c, Product2Id, SerialNumber, SLMark__c, IF_Information_From__c, InstallDate from Asset];
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM110' order by CreatedDate desc];
        //System.assertEquals('1', rowbl[0].ErrorLog__c);
        System.assertEquals(1, rslts.size());
        if(rslts.size() > 0){
            System.assertEquals(1, rslts.size());
            System.assertEquals(false, rslts[0].Return_Flag__c);
            System.assertEquals(depart1.Id, rslts[0].AccountId);
            System.assertEquals(section1.Id, rslts[0].Department_Class__c);
            System.assertEquals(company.Id, rslts[0].Hospital__c);
            System.assertEquals(prd.Id, rslts[0].Product2Id);
            System.assertEquals(dnInfo1.SerialNoorLotNo, rslts[0]. SerialNumber);
            System.assertEquals('Serial Number', rslts[0]. SLMark__c);
            System.assertEquals(true, rslts[0]. IF_Information_From__c);
            System.assertEquals(null, rslts[0]. InstallDate);
        }
        
    }
    
    // DepartmentNo = null
    @isTest
    static void test_insert_01() {
        initHpData();
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;
        Product2__c pro1 = new Product2__c(Name='NFM110Prd1',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;
        Opportunity opp = new Opportunity(Name='aiueo', StageName='出荷', CloseDate=Date.today());
        insert opp;
        Opportunity[] opps = [select Id, Name, Opportunity_No__c from Opportunity where Id = :opp.Id ];
        Statu_Achievements__c sta = new Statu_Achievements__c(
            Name='test01',
            Opportunity__c = opp.Id,
            DeliveryStatus__c = 'test',
            PaymentRate__c = '1234',
            ContractAmount__c = 0,
            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;


        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = null;
        generalData1.SoNo          = sta.Name;
        generalData1.InquiryNo     = opps[0].Opportunity_No__c + ',';
        dnInfo1.SorLMark        = 'S';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.SerialNoorLotNo = '2577010001675';
        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Asset[] rslts = [select Id, Hospital__c, AccountId, Product2Id, Backorder__c from Asset];
        //System.assertEquals(1, rslts.size());
        //System.assertEquals(company.Id, rslts[0].Hospital__c);
        //System.assertEquals(company.Id, rslts[0].AccountId);
        //System.assertEquals(prd.Id, rslts[0].Product2Id);
        //System.assertEquals(sta.Id, rslts[0].Backorder__c);
    }

    @isTest
    static void coc_test() {
        initHpData();

        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }

        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;

        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;

        Consumable_order__c coc = new Consumable_order__c(Name = 'Name001',Order_status__c = '批准',RecordTypeid = conOrder_Order[0].Id);
        insert coc;
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',Consumable_order__c = coc.id,Consumable_Product__c =pro1.id,RecordTypeId =conOrderDetail_Order[0].Id);
        insert codc;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        //generalData1.SoNo = coc.Name;
        generalData1.InquiryNo = coc.Name + ',';
        dnInfo1.SorLMark        = 'S';
        dnInfo1.OTCode          = pro1.OT_CODE_Text__c;
        dnInfo1.SerialNoorLotNo = '2577010001675';

        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Consumable_order__c[] coc1 = [select id,name from Consumable_order__c where id =:coc.id];
        System.assertEquals(1, coc1.size());
    }
    
    @isTest
    static void test_update() {
        User loginUser = [Select Id, Alias, Province__c from User where Id =: UserInfo.getUserId()];
        loginUser.Batch_User__c = true;
        loginUser.responsibility__c = '华北营业一部';
        loginUser.OCM_Management_Province__c = '北京';
        update loginUser;

        initHpData();
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false,MDM_Model_No__c = 'NFM110Ast1');
        insert prd;
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c = prd.Id);
        insert pro1;
        Asset ast = new Asset(Name='NFM110Ast1', AccountId=depart1.Id, Department_Class__c=section1.Id, Hospital__c=company.Id, Product2Id=prd.Id,
            SerialNumber='2577010001675', Guarantee_period_for_products__c=Date.today()
        );
        insert ast;
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='出荷', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert opp1;
        Statu_Achievements__c sta11 = new Statu_Achievements__c(Name='SO0000001', Opportunity__c=opp1.Id, ContractNO__c='sta11', DeliveryStatus__c = '未交付',ContractAmount__c = 0);
        insert sta11;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        generalData1.SoNo = sta11.Name;
        generalData1.InquiryNo = sta11.Name + ',';
        dnInfo1.SorLMark        = 'S';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.Barcode          = 'testbarcode001';
        dnInfo1.SerialNoorLotNo = ast.SerialNumber;
        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        
        List<Product2> prdList = [select Id, Name, ProductCode, ProductCode_Ext__c, MDM_Model_No__c
                                        from Product2
                                       where ProductCode_Ext__c = :prd.ProductCode_Ext__c];
        System.assertEquals(1, prdList.size());
        Asset[] rslts = [select Id, Return_Flag__c from Asset where Id=:ast.Id];
        System.assertEquals(1, rslts.size());

        // retry_cnt__c がクリアされること
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = true and Type__c = 'NFM110' order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;
        NFM110WebService.execute(rowbl[0].id);
        System.Test.stopTest();
        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_delete() {
        User loginUser = [Select Id, Alias, Province__c from User where Id =: UserInfo.getUserId()];
        loginUser.Batch_User__c = true;
        loginUser.responsibility__c = '华北营业一部';
        loginUser.OCM_Management_Province__c = '北京';
        update loginUser;

        initHpData();
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;
        Asset ast = new Asset(Name='NFM110Ast1', AccountId=depart1.Id, Department_Class__c=section1.Id, Hospital__c=company.Id, Product2Id=prd.Id,
            SerialNumber='2577010001675', Guarantee_period_for_products__c=Date.today()
        );
        insert ast;
        Opportunity opp1 = new Opportunity(Name='aiueo1', StageName='出荷', CurrencyIsoCode='CNY', CloseDate=Date.today());
        insert opp1;
        Statu_Achievements__c sta11 = new Statu_Achievements__c(Name='SO0000001', Opportunity__c=opp1.Id, ContractNO__c='sta11', DeliveryStatus__c = '未交付',ContractAmount__c = 0);
        insert sta11;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        generalData1.SoNo = sta11.Name;
        generalData1.InquiryNo = sta11.Name + ',';
        generalData1.ReturnMark    = '1';
        dnInfo1.SorLMark        = 'S';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.SerialNoorLotNo = ast.SerialNumber;
        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Asset[] rslts = [select Id, Return_Flag__c from Asset where Id=:ast.Id];
        System.assertEquals(1, rslts.size());
        //System.assertEquals(true, rslts[0].Return_Flag__c);
    }

    @isTest
    static void coc_send() {
        initHpData();

        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }

        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;

        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;

        Consumable_order__c coc = new Consumable_order__c(Name = 'Name001',Order_status__c = '批准',RecordTypeid = conOrder_Order[0].Id);
        insert coc;
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',Consumable_order__c = coc.id,Consumable_Product__c =pro1.id,RecordTypeId =conOrderDetail_Order[0].Id);
        insert codc;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        //generalData1.SoNo = coc.Name;
        generalData1.InquiryNo =  'Name001,';
        dnInfo1.SorLMark        = 'S';
        dnInfo1.OTCode          = pro1.OT_CODE_Text__c;
        dnInfo1.SerialNoorLotNo = '2577010001675';

        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Consumable_order__c[] coc1 = [select id,name from Consumable_order__c where id =:coc.id];
        System.assertEquals(1, coc1.size());
    }
    @isTest
    static void coc_consumable() {
        initHpData();

        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectCotac = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCotac.size() == 0) {
            return;
        }
        //测试经销商信息
        Account myAccount2 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|5|55,Test02|2|4');
        insert myAccount2;
        //测试合同信息
        Account contact = new Account(name='Testaccount002', RecordTypeId = rectCotac[0].Id,SpecialDealerName__c = 'Testaccount002',
                                      ParentId = myAccount2.Id,Agent_Ref__c = myAccount2.Id,Delete_Flag__c = false,
                                      Contract_Department_Class__c = 'ET',Contract_Decide_Start_Date__c = Date.today().addDays(-1),
                                      Contract_Decide_End_Date__c = Date.today().addDays(1),AgencyContract_Management_Code__c = '5555555');
        insert contact;
        contact = [select Management_Code__c from Account where id = :contact.Id];

        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false,Dealer_special_Object__c = true);
        insert prd;

        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;

        Consumable_order__c coc = new Consumable_order__c(Name = 'Name001',Order_status__c = '批准',RecordTypeid = conOrder_Order[0].Id);
        insert coc;
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',Consumable_order__c = coc.id,Consumable_Product__c =pro1.id,RecordTypeId =conOrderDetail_Order[0].Id,Consumable_count__c = 1);
        insert codc;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = contact.Management_Code__c;
        generalData1.DepartmentNo  = contact.Management_Code__c;
        //generalData1.SoNo = coc.Name;
        generalData1.InquiryNo  =  'Name001,Name001';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = pro1.OT_CODE_Text__c;
        dnInfo1.SerialNoorLotNo = '85K';
        dnInfo1.Barcode         = 'testbarcode001250AAAAA';
        dnInfo1.TracingCode     = 'AAAAA';
        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Consumable_order__c[] coc1 = [select id,name from Consumable_order__c where id =:coc.id];
        System.assertEquals(1, coc1.size());
        //List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM110' order by CreatedDate desc];
        //System.assertEquals('1', rowbl[0].ErrorLog__c);
        Consumable_order_details2__c[] cod2 = [select id,name from Consumable_order_details2__c where Consumable_order_minor__c =:coc.id];
        System.assertEquals(1, cod2.size());
        
    }
    
    @isTest
    static void coc_consumable_InquiryNo() {
        initHpData();

        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectCotac = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCotac.size() == 0) {
            return;
        }
        //测试经销商信息
        Account myAccount2 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|5|55,Test02|2|4');
        insert myAccount2;
        //测试合同信息
        Account contact = new Account(name='Testaccount002', RecordTypeId = rectCotac[0].Id,SpecialDealerName__c = 'Testaccount002',ParentId = myAccount2.Id,Agent_Ref__c = myAccount2.Id,Delete_Flag__c = false,Contract_Department_Class__c = 'ET',Contract_Decide_Start_Date__c = Date.today().addDays(-1),Contract_Decide_End_Date__c = Date.today().addDays(1));
        insert contact;
        contact = [select Management_Code__c from Account where id = :contact.Id];
        //System.assertEquals('99999', contact.Management_Code__c);
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false,Dealer_special_Object__c = true);
        insert prd;

        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;

        Consumable_order__c coc = new Consumable_order__c(Name = 'Name002',Order_status__c = '批准',RecordTypeid = conOrder_Order[0].Id);
        insert coc;
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',Consumable_order__c = coc.id,Consumable_Product__c =pro1.id,RecordTypeId =conOrderDetail_Order[0].Id,Consumable_count__c = 1);
        insert codc;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = '000' + contact.Management_Code__c;
        generalData1.DepartmentNo  = '000' + contact.Management_Code__c;
        //generalData1.SoNo = coc.Name;
        generalData1.InquiryNo  =  'Name002,Name002';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = pro1.OT_CODE_Text__c;
        dnInfo1.SerialNoorLotNo = '85K';
        dnInfo1.Barcode         = 'testbarcode001250AAAAA';
        dnInfo1.TracingCode     = 'AAAAA';

        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Consumable_order__c[] coc1 = [select id,name from Consumable_order__c where id =:coc.id];
        System.assertEquals(1, coc1.size());
        List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM110' order by CreatedDate desc];
        //System.assertEquals('1', rowbl[0].ErrorLog__c);
        Consumable_order_details2__c[] cod2 = [select id,name from Consumable_order_details2__c];
        System.assertEquals(1, cod2.size());
    }
    @isTest
    //直接到货再入库对应
    static void coc_doubleArrive() {
        initHpData();
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectCotac = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCotac.size() == 0) {
            return;
        }
        //测试经销商信息
        Account myAccount2 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|5|55,Test02|2|4');
        insert myAccount2;
        //测试合同信息
        Account contact = new Account(name='Testaccount002', RecordTypeId = rectCotac[0].Id,SpecialDealerName__c = 'Testaccount002',ParentId = myAccount2.Id,Agent_Ref__c = myAccount2.Id,Delete_Flag__c = false,Contract_Department_Class__c = 'ET',Contract_Decide_Start_Date__c = Date.today().addDays(-1),Contract_Decide_End_Date__c = Date.today().addDays(1));
        insert contact;
        contact = [select Management_Code__c from Account where id = :contact.Id];

        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false,Dealer_special_Object__c = true);
        insert prd;

        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;

        Consumable_order__c coc = new Consumable_order__c(Name = 'Name001',Order_status__c = '批准',RecordTypeid = conOrder_Order[0].Id);
        insert coc;
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',Consumable_order__c = coc.id,Consumable_Product__c =pro1.id,RecordTypeId =conOrderDetail_Order[0].Id);
        insert codc;
        Consumable_order_details2__c codc2 = new Consumable_order_details2__c(Name='det2001',Consumable_order_minor__c = coc.id,Consumable_Product__c =pro1.id,RecordTypeId =System.Label.RT_ConOrderDetail2_Delivery,
                                              Asset_Model_No__c = 'NFM110Prd1',Bar_Code__c = 'testbarcode0011085K250AAAAA',Arrive_date__c = Date.today(),Direct_Arrive_Product__c = true,TracingCode__c = 'AAAAA',SerialLotNo__c = '85K');
        insert codc2;

        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = contact.Management_Code__c;
        generalData1.DepartmentNo  = contact.Management_Code__c;
        //generalData1.SoNo = coc.Name;
        generalData1.InquiryNo  =  'Name001,Name001';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = pro1.OT_CODE_Text__c;
        dnInfo1.SerialNoorLotNo = '85K';
        dnInfo1.Barcode         = 'testbarcode0011085K250AAAAA';
        dnInfo1.TracingCode     = 'AAAAA';

        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Consumable_order_details2__c[] cod2 = [select id,name,Direct_Arrive_Product__c from Consumable_order_details2__c];
        System.assertEquals(1, cod2.size());
        System.assertEquals(false, cod2[0].Direct_Arrive_Product__c);
        //List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM110' order by CreatedDate desc];
        //System.assertEquals('1', rowbl[0].ErrorLog__c);
    }

    //促销产品
    static testMethod void coc_romotionPro() {
        initHpData();
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }

        List<RecordType> rectContract = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCo.size() == 0) {
            return;
        }
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        // 产品
        Product2 prd1 = new Product2(
            Name='name01',
            IsActive=true,
            Asset_Model_No__c='NFM110Prd1',
            ProductCode = 'NFM110Prd1',
            ProductCode_Ext__c = 'NFM110Prd1',
            Manual_Entry__c=false,
            //SFDA_Status__c='有効',
            Intra_Trade_List_RMB_Date1__c=Date.today().addDays(-1),
            Intra_Trade_List_RMB_Date2__c=Date.today().addDays(-1),
            Intra_Trade_List_RMB_End_Date1__c=Date.today().addDays(1),
            Intra_Trade_List_RMB_End_Date2__c=Date.today().addDays(1),
            Intra_Trade_List_RMB_1__c=100,
            Intra_Trade_List_RMB_2__c=200,
            Intra_Trade_Cost_RMB_Date1__c=Date.today().addDays(-1),
            Intra_Trade_Cost_RMB_Date2__c=Date.today().addDays(-1),
            Intra_Trade_Cost_RMB_End_Date1__c=Date.today().addDays(1),
            Intra_Trade_Cost_RMB_End_Date2__c=Date.today().addDays(1),
            Intra_Trade_Cost_RMB_1__c=10,
            Intra_Trade_Cost_RMB_2__c=20,
            Dealer_special_Object__c = true,
            SFDA_Approbated_Status__c = '不要',
            Product_Status__c = '正常销售'

        );
        insert new Product2[] {prd1};

        // 価格表エントリを作成する
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=prd1.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert new PricebookEntry[] {entry};
        //经销商、联系人、用户信息
        Account myAccount1 = new Account(name='Testaccount001',
                                        Dealer_discount__c =20,
                                        Ban_On_Use_Date__c = Date.today().addDays(1),
                                        Business_Paper_Expiration_Date__c = Date.today().addDays(1),
                                        Tax_Practice_Expiration_Date__c = Date.today().addDays(1),
                                        Medical_Equipment_Expiration_Date__c = Date.today().addDays(1),
                                        RecordTypeId = rectCo[0].Id);
        insert myAccount1;
        //医疗器械经营许可证
        License_Information__c linc = new License_Information__c( name='Test20181204',
                                                LicenseType__c = '医疗器械经营许可证',
                                                BusinessLicense__c = '20180522',
                                                ValidFrom__c = date.newinstance(2018, 05, 22),
                                                ValidTo__c = date.newinstance(2088, 05, 22),
                                                Scope3__c = '6815;6822;6823;6825',
                                                LicenseAndAccount__c = myAccount1.Id
                                                );
        insert linc;
        Product_Register__c prc = new Product_Register__c(Name = '国械注进20162220210',
                                                MedPrdClass__c = '3',
                                                ValidFrom__c = date.newinstance(2018, 07, 22),
                                                ValidTo__c = date.newinstance(2028, 07, 22),
                                                RegisterNoClass_Old__c = '6815',
                                                RegisterNoClass_New__c = '6815',
                                                RegisterNoStatus__c = '不要'
                                                );
        insert prc;
        //新旧关系对照表
        Product_Register_contrast__c prcc = new Product_Register_contrast__c(
                                                Name = 'Test20181204',
                                                Register_new__c = '314',
                                                Register_old__c = '36815'
                                                );
        insert prcc;
        //产品-产品注册证关系
        Product_Register_Link__c  prlc = new Product_Register_Link__c(
                                                Product2__c = prd1.Id,
                                                Product_Register__c = prc.Id
                                                ); 
        insert prlc;
        Account acc1 = [select Id,Name,Is_Active_Formula__c from Account where Id = : myAccount1.Id];
        System.assertEquals('有效', acc1.Is_Active_Formula__c);
        //经销商有效合同
        Account myAccount2 = new Account(name='Testaccount002',
                                            RecordTypeId = rectContract[0].Id,
                                            Contract_Decide_Start_Date__c = Date.today().addDays(-1),
                                            Contract_Decide_End_Date__c =Date.today().addDays(1),
                                            Contract_Start_Date__c = Date.today().addDays(-1),
                                            Contract_End_Date__c =Date.today().addDays(1),
                                            Agent_Ref__c =myAccount1.Id,
                                            ET_SP_Dealer__c = true,
                                            Business_Assistant__c = UserInfo.getUserId(),
                                            ParentId =myAccount1.Id);
        insert myAccount2;
        Account acc2 = [select Id,Name,Management_Code__c,Is_Active_Formula__c from Account where Id = : myAccount2.Id];
        System.assertEquals('有效', acc2.Is_Active_Formula__c);


        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd1.id);
        //Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='NFM110Prd2',Product2__c=prd2.id);
        insert new Product2__c[] {pro1};

        Consumable_order__c coc = new Consumable_order__c(Name = 'Name001',
                                                            Dealer_Info__c = myAccount1.Id,
                                                            Order_status__c = '批准',
                                                            RecordTypeid = conOrder_Order[0].Id,
                                                            Contract_application_decision__c ='Name001'
                                                            ,SalesManager__c = UserInfo.getUserId(),
                                                            Order_effective_contact__c = myAccount2.Id
                                                            );
        insert coc;
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',                                                                            Consumable_order__c = coc.id,
                                                                            Consumable_Product__c =pro1.id,
                                                                            RecordTypeId =conOrderDetail_Order[0].Id,
                                                                            Consumable_count__c = 2,
                                                                            Intra_Trade_List_RMB__c = 100);
        insert codc;
        System.Test.startTest();
        SendConsumableordertosap.sendSAP(coc.id);
        //System.assertEquals('11111111', SendConsumableordertosap.sendSAP(coc.id));
        //System.Test.stopTest();
        //coc.Name= 'Name0011';
        //update coc;
        //Opportunity opp1 = new Opportunity(Name='aiueo10107', StageName='出荷', CurrencyIsoCode='CNY', CloseDate=Date.today(),Purchase_Type__c = 'ET24時間販売'
        //                     ,Sales_Root__c = '販売店');
        //insert opp1;
        Opportunity[] coc12 = [select id,Name,Opportunity_No__c,SAP_Province__c from Opportunity];
        System.assertEquals(1, coc12.size());
        
        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = acc2.Management_Code__c;
        generalData1.DepartmentNo  = acc2.Management_Code__c;
        //generalData1.SoNo = coc.Name;
        generalData1.InquiryNo  =   coc12[0].Opportunity_No__c + ',';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = pro1.OT_CODE_Text__c;
        dnInfo1.SerialNoorLotNo = '85K';
        dnInfo1.Barcode         = 'testbarcode001250AAAAA';
        dnInfo1.TracingCode     = 'AAAAA';

        
        //System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Consumable_order_details2__c[] cod2 = [select id,name,Direct_Arrive_Product__c from Consumable_order_details2__c];
        System.assertEquals(1, cod2.size());
        System.assertEquals(false, cod2[0].Direct_Arrive_Product__c);
        //List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM110' order by CreatedDate desc];
        //System.assertEquals('1', rowbl[0].ErrorLog__c);
    }
    //促销产品
    static testMethod void coc_romotionPro1() {
        initHpData();
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        List<RecordType> conOrder_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_order__c' and Name = '01 订单'];
        if (conOrder_Order.size() == 0) {
            return;
        }

        List<RecordType> conOrderDetail_Order = [select Id from RecordType where IsActive = true and SobjectType = 'Consumable_orderdetails__c' and Name = '订单'];
        if (conOrderDetail_Order.size() == 0) {
            return;
        }

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }

        List<RecordType> rectContract = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCo.size() == 0) {
            return;
        }
        Id pricebookId = ControllerUtil.getStandardPricebook().Id;
        // 产品
        Product2 prd1 = new Product2(
            Name='name01',
            IsActive=true,
            Asset_Model_No__c='NFM110Prd1',
            ProductCode = 'NFM110Prd1',
            ProductCode_Ext__c = 'NFM110Prd1',
            Manual_Entry__c=false,
            //SFDA_Status__c='有効',
            Intra_Trade_List_RMB_Date1__c=Date.today().addDays(-1),
            Intra_Trade_List_RMB_Date2__c=Date.today().addDays(-1),
            Intra_Trade_List_RMB_End_Date1__c=Date.today().addDays(1),
            Intra_Trade_List_RMB_End_Date2__c=Date.today().addDays(1),
            Intra_Trade_List_RMB_1__c=100,
            Intra_Trade_List_RMB_2__c=200,
            Intra_Trade_Cost_RMB_Date1__c=Date.today().addDays(-1),
            Intra_Trade_Cost_RMB_Date2__c=Date.today().addDays(-1),
            Intra_Trade_Cost_RMB_End_Date1__c=Date.today().addDays(1),
            Intra_Trade_Cost_RMB_End_Date2__c=Date.today().addDays(1),
            Intra_Trade_Cost_RMB_1__c=10,
            Intra_Trade_Cost_RMB_2__c=20,
            Dealer_special_Object__c = true,
            SFDA_Approbated_Status__c = '不要',
            Product_Status__c = '正常销售'

        );
        insert new Product2[] {prd1};

        // 価格表エントリを作成する
        PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=prd1.Id);
        entry.UnitPrice = 0;
        entry.IsActive = true;
        entry.UseStandardPrice = false;
        entry.CurrencyIsoCode = 'CNY';
        insert new PricebookEntry[] {entry};
        //经销商、联系人、用户信息
        Account myAccount1 = new Account(name='Testaccount001',
                                        Dealer_discount__c =20,
                                        Ban_On_Use_Date__c = Date.today().addDays(1),
                                        Business_Paper_Expiration_Date__c = Date.today().addDays(1),
                                        Tax_Practice_Expiration_Date__c = Date.today().addDays(1),
                                        Medical_Equipment_Expiration_Date__c = Date.today().addDays(1),
                                        RecordTypeId = rectCo[0].Id);
        insert myAccount1;
        //医疗器械经营许可证
        License_Information__c linc = new License_Information__c( name='Test20181204',
                                                LicenseType__c = '医疗器械经营许可证',
                                                BusinessLicense__c = '20180522',
                                                ValidFrom__c = date.newinstance(2018, 05, 22),
                                                ValidTo__c = date.newinstance(2088, 05, 22),
                                                Scope3__c = '6815;6822;6823;6825',
                                                LicenseAndAccount__c = myAccount1.Id
                                                );
        insert linc;
        Product_Register__c prc = new Product_Register__c(Name = '国械注进20162220210',
                                                MedPrdClass__c = '3',
                                                ValidFrom__c = date.newinstance(2018, 07, 22),
                                                ValidTo__c = date.newinstance(2028, 07, 22),
                                                RegisterNoClass_Old__c = '6815',
                                                RegisterNoClass_New__c = '6815',
                                                RegisterNoStatus__c = '不要'
                                                );
        insert prc;
        //新旧关系对照表
        Product_Register_contrast__c prcc = new Product_Register_contrast__c(
                                                Name = 'Test20181204',
                                                Register_new__c = '314',
                                                Register_old__c = '36815'
                                                );
        insert prcc;
        //产品-产品注册证关系
        Product_Register_Link__c  prlc = new Product_Register_Link__c(
                                                Product2__c = prd1.Id,
                                                Product_Register__c = prc.Id
                                                ); 
        insert prlc;
        Account acc1 = [select Id,Name,Is_Active_Formula__c from Account where Id = : myAccount1.Id];
        System.assertEquals('有效', acc1.Is_Active_Formula__c);
        //经销商有效合同
        Account myAccount2 = new Account(name='Testaccount002',
                                            RecordTypeId = rectContract[0].Id,
                                            Contract_Decide_Start_Date__c = Date.today().addDays(-1),
                                            Contract_Decide_End_Date__c =Date.today().addDays(1),
                                            Contract_Start_Date__c = Date.today().addDays(-1),
                                            Contract_End_Date__c =Date.today().addDays(1),
                                            Agent_Ref__c =myAccount1.Id,
                                            ET_SP_Dealer__c = true,
                                            Business_Assistant__c = UserInfo.getUserId(),
                                            ParentId =myAccount1.Id);
        insert myAccount2;
        Account acc2 = [select Id,Name,Management_Code__c,Is_Active_Formula__c from Account where Id = : myAccount2.Id];
        System.assertEquals('有效', acc2.Is_Active_Formula__c);


        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd1.id);
        //Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='NFM110Prd2',Product2__c=prd2.id);
        insert new Product2__c[] {pro1};

        Consumable_order__c coc = new Consumable_order__c(Name = 'Name001',
                                                            Dealer_Info__c = myAccount1.Id,
                                                            Order_status__c = '批准',
                                                            RecordTypeid = conOrder_Order[0].Id,
                                                            Contract_application_decision__c ='Name001'
                                                            ,SalesManager__c = UserInfo.getUserId(),
                                                            Order_effective_contact__c = myAccount2.Id
                                                            );
        insert coc;
        Consumable_orderdetails__c codc = new Consumable_orderdetails__c(Name='orderdetails001',                                                                            Consumable_order__c = coc.id,
                                                                            Consumable_Product__c =pro1.id,
                                                                            RecordTypeId =conOrderDetail_Order[0].Id,
                                                                            Consumable_count__c = 2,
                                                                            Intra_Trade_List_RMB__c = 100);
        insert codc;
        System.Test.startTest();
        //SendConsumableordertosap.sendSAP(coc.id);
        //System.assertEquals('11111111', SendConsumableordertosap.sendSAP(coc.id));
        //System.Test.stopTest();
        //coc.Name= 'Name0011';
        //update coc;
        Opportunity opp1 = new Opportunity(Name='aiueo10107', StageName='出荷', CurrencyIsoCode='CNY', CloseDate=Date.today(),Purchase_Type__c = 'ET24時間販売'
                             ,Sales_Root__c = '販売店');
        insert opp1;
        Opportunity[] coc12 = [select id,Name,Opportunity_No__c,SAP_Province__c from Opportunity];
        System.assertEquals(1, coc12.size());
        
        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = acc2.Management_Code__c;
        generalData1.DepartmentNo  = acc2.Management_Code__c;
        //generalData1.SoNo = coc.Name;
        generalData1.InquiryNo  =   coc12[0].Opportunity_No__c + ',';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = pro1.OT_CODE_Text__c;
        dnInfo1.SerialNoorLotNo = '85K';
        dnInfo1.Barcode         = 'testbarcode001250AAAAA';
        dnInfo1.TracingCode     = 'AAAAA';

        
        //System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Consumable_order_details2__c[] cod2 = [select id,name,Direct_Arrive_Product__c from Consumable_order_details2__c];
        System.assertEquals(1, cod2.size());
        System.assertEquals(false, cod2[0].Direct_Arrive_Product__c);
        //List<BatchIF_Log__c> rowbl = [Select Id, Log__c, ErrorLog__c from BatchIF_Log__c where RowDataFlg__c = false and Type__c = 'NFM110' order by CreatedDate desc];
        //System.assertEquals('1', rowbl[0].ErrorLog__c);
    }

    @isTest
    //直销产品发货
    static void direct_sales() {
        initHpData();
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false);
        insert prd;
        Product2__c pro1 = new Product2__c(Name='NFM110Prd1',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;
        Opportunity opp = new Opportunity(Name='aiueo', StageName='出荷', CloseDate=Date.today(),Sales_Root__c = 'OCM直接販売');
        insert opp;
        Opportunity opps = [select Id, Name, Opportunity_No__c,Sales_Root__c from Opportunity where Id = :opp.Id ];
        System.assertEquals('OCM直接販売', opps.Sales_Root__c);
        Statu_Achievements__c sta = new Statu_Achievements__c(
            Name='test01',
            Opportunity__c = opp.Id,
            DeliveryStatus__c = 'test',
            PaymentRate__c = '1234',
            ContractAmount__c = 0,
            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;


        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        generalData1.SoNo          = sta.Name;
        generalData1.InquiryNo     = opps.Opportunity_No__c + ',test01';
        generalData1.ReturnMark    = '';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.SerialNoorLotNo = '2577010001675';
        dnInfo1.Barcode = 'Barcodetest250AAAA';
        dnInfo1.TracingCode     = 'AAAAA';
        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Asset[] rslts = [select Id, Hospital__c, AccountId, Product2Id, Backorder__c from Asset];
        System.assertEquals(1, rslts.size());
    }

    @isTest
    //直销退货发货
    static void direct_return() {
        initHpData();
        Product2 prd = new Product2(Name = 'NFM110Prd1', ProductCode = 'NFM110Prd1', ProductCode_Ext__c = 'NFM110Prd1', Manual_Entry__c = false,Dealer_special_Object__c = true,MDM_Model_No__c = 'NFM110Prd1');
        insert prd;
        Product2__c pro1 = new Product2__c(Name='NFM110Prd1',OT_CODE_Text__c='NFM110Prd1',Product2__c=prd.id);
        insert pro1;
        Product2 prd2 = new Product2(Name = 'NFM110Prd2', ProductCode = 'NFM110Prd2', ProductCode_Ext__c = 'NFM110Prd2', Manual_Entry__c = false,Dealer_special_Object__c = true,MDM_Model_No__c = 'NFM110Prd1');
        insert prd2;
        Product2__c pro2 = new Product2__c(Name='NFM110Prd2',OT_CODE_Text__c='NFM110Prd2',Product2__c=prd2.id);
        insert pro2;
        Asset ast = new Asset(Name='NFM110Ast2', AccountId=depart1.Id, Department_Class__c=section1.Id, Hospital__c=company.Id, Product2Id=prd.Id,
            SerialNumber='2577010001676(BBBBB)', Guarantee_period_for_products__c=Date.today()
        );
        insert ast;
        Opportunity opp = new Opportunity(Name='aiueo', StageName='出荷', CloseDate=Date.today(),Sales_Root__c = 'OCM直接販売');
        insert opp;
        
        Opportunity opps = [select Id, Name, Opportunity_No__c,Sales_Root__c from Opportunity where Id = :opp.Id ];
        System.assertEquals('OCM直接販売', opps.Sales_Root__c);
        Statu_Achievements__c sta = new Statu_Achievements__c(
            Name='test01',
            Opportunity__c = opp.Id,
            DeliveryStatus__c = 'test',
            PaymentRate__c = '1234',
            ContractAmount__c = 0,
            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;


        NFM110WebService.GeneralData generalData1 = new NFM110WebService.GeneralData();
        NFM110WebService.ProductsDelivery ProductsDelivery = new NFM110WebService.ProductsDelivery();
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        ProductsDelivery.Monitoring = new NFMUtil.Monitoring();
        ProductsDelivery.Monitoring.MessageGroupNumber = nowStr + '01';

        ProductsDelivery.GeneralData = new NFM110WebService.GeneralData[] { generalData1 };
        generalData1.DeliveryNote = 'DeliveryNote';
        NFM110WebService.DnInformation dnInfo1 = new NFM110WebService.DnInformation();
        NFM110WebService.DnInformation dnInfo2 = new NFM110WebService.DnInformation();
        generalData1.DnInformation = new NFM110WebService.DnInformation[] { dnInfo1,dnInfo2 };
        generalData1.EndUserNo     = company.Management_Code_Auto__c;
        generalData1.DepartmentNo  = depart1.Management_Code_Auto__c;
        generalData1.SoNo          = sta.Name;
        generalData1.InquiryNo     = opps.Opportunity_No__c + ',test01';
        generalData1.ReturnMark    = '1';
        dnInfo1.SorLMark        = 'L';
        dnInfo1.OTCode          = prd.ProductCode_Ext__c;
        dnInfo1.SerialNoorLotNo = '2577010001675';
        dnInfo1.Barcode = 'Barcodetest250AAAA';
        dnInfo1.TracingCode     = 'AAAAA';
        dnInfo1.Qty             = '1';
        dnInfo2.SorLMark        = 'L';
        dnInfo2.OTCode          = prd2.ProductCode_Ext__c;
        dnInfo2.SerialNoorLotNo = '2577010001676';
        dnInfo2.Barcode = 'Barcodetest250BBBBB';
        dnInfo2.TracingCode     = 'BBBBB';
        dnInfo2.Qty             = '1';
        
        System.Test.startTest();
        NFM110WebService.NFM110(ProductsDelivery);
        System.Test.stopTest();

        Asset[] rslts = [select Id, Hospital__c, AccountId, Product2Id, Backorder__c from Asset];
        System.assertEquals(1, rslts.size());
        Consumable_order_details2__c[] cod2 = [select id,name,Direct_Arrive_Product__c from Consumable_order_details2__c];
        System.assertEquals(2, cod2.size());
    }
}