@isTest
private class ConInvoiceViewControllerTest
{
    //新建发票 以及编辑发票 invoiceId<>null
    static testMethod void conInvoiceViewTestedit_1(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectHos = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHos.size() == 0) {
            return;
        }
        

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        Account hosp2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

       Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
         insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            //invoiceorder.Order_ProType__c = 'ET';
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            
            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            System.Test.startTest();
            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;

            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2};

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            
            conTest.init();
 
            conTest.invoiceId =invoiceorder.Id;
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            //conTest.SecondDealer = agency1.id;
            //conTest.coc.Order_ForDealer__c = agency1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            
            //conTest.SortLimited();
            //conTest.InvoiceorderSearch();
            System.assertEquals(1, conTest.invoiceOrderRecoeds.size());
            conTest.invoiceOrderRecoeds[0].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[0].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[0].esd.Order_ForDealer__c = agency1.id;
            
            conTest.invoiceOrderRecoeds[0].check= true;

            conTest.save();
            //System.assertEquals('123',conTest.testTmp);
            conTest.nvoiceorderremind();
            conTest.saveAttachment();

            conTest.getdone();
            conTest.getExistOutbound();
            conTest.getInvoiceName();
            System.Test.stopTest();
      
        }
    }


    //新建发票 invoiceId=null
   static testMethod void conInvoiceViewTestedit_2(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET Email)'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];


        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectHos = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHos.size() == 0) {
            return;
        }
        //产品信息
        Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
        insert new Product2[] {prod01};

        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
        insert new Product2__c[] {pro1};

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        Account hosp2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

       Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
         insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            //invoiceorder.Order_ProType__c = 'ET';
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];

            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            System.Test.startTest();
            conTest.init();

            conTest.invoiceId =invoiceorder.Id;
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            //conTest.SecondDealer = agency1.id;
            //conTest.coc.Order_ForDealer__c = agency1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            
            //conTest.SortLimited();
            //conTest.InvoiceorderSearch();
            System.assertEquals(1, conTest.invoiceOrderRecoeds.size());
            conTest.invoiceOrderRecoeds[0].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[0].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[0].esd.Order_ForDealer__c = agency1.id;
            
            conTest.invoiceOrderRecoeds[0].check= true;
            conTest.invoiceId =null;
            conTest.save();
            System.Test.stopTest();
      
        }
    }
    //新建发票 invoiceId=null error check
   static testMethod void conInvoiceViewTestedit_3(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        Account hosp2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

       Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
         insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            insert new Product2__c[] {pro1};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            //invoiceorder.Order_ProType__c = 'ET';
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];

            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            System.Test.startTest();
            conTest.init();

            conTest.invoiceId =invoiceorder.Id;
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            //conTest.SecondDealer = agency1.id;
            //conTest.coc.Order_ForDealer__c = agency1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            
            //conTest.SortLimited();
            //conTest.InvoiceorderSearch();
            System.assertEquals(1, conTest.invoiceOrderRecoeds.size());
            conTest.invoiceOrderRecoeds[0].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[0].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[0].esd.Order_ForDealer__c = agency1.id;
            
            conTest.invoiceOrderRecoeds[0].check= true;
            conTest.invoiceId =null;

            //error1
            conTest.coc.Name = null;
            conTest.save();
            //error2
            conTest.coc.Name = 'invoice002';
            conTest.save();
            //error3
            conTest.coc.Name = 'invoice002';
            conTest.HospitalInfo = null;
            conTest.SecondDealer = null;
            conTest.coc.Order_ForDealerText__c = null;
            conTest.save();
            //error4
            conTest.coc.Name = 'invoice002';
            conTest.HospitalInfo = null;
            conTest.SecondDealer = agency1.Id;
            conTest.coc.Order_ForDealerText__c = '二级';
            conTest.save();
            //error5
            // conTest.coc.Name = 'invoice002';
            // conTest.HospitalInfo = hosp1.Id;
            // conTest.SecondDealer = agency1.Id;
            // conTest.coc.Order_ForDealerText__c = '二级';
            // conTest.save();
            //error6
            // conTest.coc.Name = 'invoice002';
            // conTest.HospitalInfo = hosp1.Id;
            // conTest.SecondDealer = agency1.Id;
            // conTest.coc.Order_ForDealerText__c = null;
            // conTest.save();
            //error6
            // conTest.coc.Name = 'invoice002';
            // conTest.HospitalInfo = hosp2.Id;
            // conTest.SecondDealer = agency2.Id;
            // conTest.coc.Order_ForCustomerText__c = null;
            // conTest.save();
            // conTest.coc.Name = 'invoice002';
            // conTest.HospitalInfo = hosp1.Id;
            // conTest.SecondDealer = null;
            // conTest.coc.Order_ForCustomerText__c = null;
            // conTest.save();
            //error6
            conTest.coc.Name = 'invoice002';
            conTest.HospitalInfo = null;
            conTest.SecondDealer = agency1.Id;
            conTest.coc.Order_ForCustomerText__c = 'Test';
            conTest.save();
            System.Test.stopTest();

      
        }
    }

    //新建发票 以及编辑发票invoiceId== NULL and deliveryId== NULL
    static testMethod void conInvoiceViewTestinit(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //经销商
        Account myAccount1 = new Account(name='testagency001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id );
        insert myAccount1;

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testagency1001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        insert agency1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

        Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
        insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user myUser_test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='testUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert myUser_test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            //invoiceorder.Order_ProType__c = 'ET';
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            //Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='草案中',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            //Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='已提交',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            
            Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            //insert new Consumable_order__c[]{testList1,testList2};

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;
            Consumable_order_LinkTable__c order_LinkTable2 = new Consumable_order_LinkTable__c(Name='order_LinkTable2',Outboundorder_Code_link__c = testList2.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable2;
            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            //出库单2明细1
            Consumable_orderdetails__c orderdet3 = new Consumable_orderdetails__c();
            orderdet3.Name = 'Test1_002001';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet3.Consumable_order__c = testList2.id;
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet3.Consumable_count__c = 1;
            orderdet3.Shipment_Count__c = 1;
            orderdet3.Consumable_product__c = pro2.Id;

             //出库单2明细2
            Consumable_orderdetails__c orderdet4 = new Consumable_orderdetails__c();
            orderdet4.Name = 'Test1_002002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet4.Consumable_order__c = testList2.id;
            orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet4.Consumable_count__c = 1;
            orderdet4.Shipment_Count__c = 1;
            orderdet4.Consumable_product__c = pro2.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2,orderdet3,orderdet4};
             //附件
            Consumable_accessories_invoice__c caic= new Consumable_accessories_invoice__c();
            caic.Attachment_ID__c = myAccount1.Id;
            caic.Accessories_type__c = '发票和明细';
            caic.Invoice_code__c = invoiceorder.id;
            insert caic;
            PageReference page = new PageReference('/apex/ConInvoiceView?KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            //contest.deliveryId=testList1.id;
            System.Test.startTest();
            conTest.init();

            //System.assertEquals(false, conTest.getdone());
            //System.assertEquals(false, conTest.getExistOutbound());
            //System.assertEquals('invoice01', conTest.getInvoiceName());
            //System.assertEquals(2, conTest.invoiceOrderRecoedsCount);
            conTest.InvoiceorderSearch();
    
            conTest.SortLimited();
            System.Test.stopTest();
        }
    }

    //新建发票 以及编辑发票invoiceId== NULL and deliveryId<> NULL
    static testMethod void conInvoiceViewTestinit_1(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //经销商
        Account myAccount1 = new Account(name='testagency001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id );
        insert myAccount1;

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testagency1001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        insert agency1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

        Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
        insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user myUser_test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='testUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert myUser_test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            //Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='草案中',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            //Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='已提交',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            
            Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            //insert new Consumable_order__c[]{testList1,testList2};

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;
            Consumable_order_LinkTable__c order_LinkTable2 = new Consumable_order_LinkTable__c(Name='order_LinkTable2',Outboundorder_Code_link__c = testList2.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable2;
            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            //出库单2明细1
            Consumable_orderdetails__c orderdet3 = new Consumable_orderdetails__c();
            orderdet3.Name = 'Test1_002001';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet3.Consumable_order__c = testList2.id;
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet3.Consumable_count__c = 1;
            orderdet3.Shipment_Count__c = 1;
            orderdet3.Consumable_product__c = pro2.Id;

             //出库单2明细2
            Consumable_orderdetails__c orderdet4 = new Consumable_orderdetails__c();
            orderdet4.Name = 'Test1_002002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet4.Consumable_order__c = testList2.id;
            orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet4.Consumable_count__c = 1;
            orderdet4.Shipment_Count__c = 1;
            orderdet4.Consumable_product__c = pro2.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2,orderdet3,orderdet4};
             //附件
            Consumable_accessories_invoice__c caic= new Consumable_accessories_invoice__c();
            caic.Attachment_ID__c = myAccount1.Id;
            caic.Accessories_type__c = '发票和明细';
            caic.Invoice_code__c = invoiceorder.id;
            insert caic;
            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.id);
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            contest.deliveryId=testList1.id;
            System.Test.startTest();
            conTest.init();
            conTest.approval();
            //System.assertEquals(false, conTest.getdone());
            //System.assertEquals(false, conTest.getExistOutbound());
            //System.assertEquals('invoice01', conTest.getInvoiceName());
            //System.assertEquals(2, conTest.invoiceOrderRecoedsCount);
            conTest.InvoiceorderSearch();
    
            conTest.SortLimited();
            System.Test.stopTest();
   
        }
    }

    //新建发票 以及编辑发票invoiceId<> NULL
    static testMethod void conInvoiceViewTestinit_2(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //经销商
        Account myAccount1 = new Account(name='testagency001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id );
        insert myAccount1;

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testagency1001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        insert agency1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

        Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
        insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user myUser_test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='testUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert myUser_test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            //Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='草案中',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            //Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='已提交',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            
            Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            //insert new Consumable_order__c[]{testList1,testList2};

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;
            Consumable_order_LinkTable__c order_LinkTable2 = new Consumable_order_LinkTable__c(Name='order_LinkTable2',Outboundorder_Code_link__c = testList2.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable2;
            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            //出库单2明细1
            Consumable_orderdetails__c orderdet3 = new Consumable_orderdetails__c();
            orderdet3.Name = 'Test1_002001';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet3.Consumable_order__c = testList2.id;
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet3.Consumable_count__c = 1;
            orderdet3.Shipment_Count__c = 1;
            orderdet3.Consumable_product__c = pro2.Id;

             //出库单2明细2
            Consumable_orderdetails__c orderdet4 = new Consumable_orderdetails__c();
            orderdet4.Name = 'Test1_002002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet4.Consumable_order__c = testList2.id;
            orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet4.Consumable_count__c = 1;
            orderdet4.Shipment_Count__c = 1;
            orderdet4.Consumable_product__c = pro2.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2,orderdet3,orderdet4};
             //附件
            Consumable_accessories_invoice__c caic= new Consumable_accessories_invoice__c();
            caic.Attachment_ID__c = myAccount1.Id;
            caic.Accessories_type__c = '发票和明细';
            caic.Invoice_code__c = invoiceorder.id;
            insert caic;
            PageReference page = new PageReference('/apex/ConInvoiceView?KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            contest.deliveryId=testList1.id;
            conTest.invoiceId =invoiceorder.Id;
            System.Test.startTest();
            conTest.init();

            //System.assertEquals(false, conTest.getdone());
            //System.assertEquals(false, conTest.getExistOutbound());
            //System.assertEquals('invoice01', conTest.getInvoiceName());
            //System.assertEquals(2, conTest.invoiceOrderRecoedsCount);
            conTest.InvoiceorderSearch();
    
            conTest.SortLimited();
            System.Test.stopTest();
   
        }
    }
    static testMethod void conInvoiceViewTestoutOrderchange(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        Account hosp2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

       Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
         insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            
            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            System.Test.startTest();
            insert testList1;
            insert testList2;

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;

            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2};

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            
            conTest.init();

            conTest.invoiceId =invoiceorder.Id;
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            //conTest.SecondDealer = agency1.id;
            //conTest.coc.Order_ForDealer__c = agency1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            
            //conTest.SortLimited();
            //conTest.InvoiceorderSearch();
            System.assertEquals(1, conTest.invoiceOrderRecoeds.size());
            conTest.invoiceOrderRecoeds[0].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[0].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[0].esd.Order_ForDealer__c = agency1.id;
            
            Map<String,String> insertDetMap = new Map<String,String>();
            conTest.outOrderchange(insertDetMap);

            insertDetMap.put(conTest.invoiceOrderRecoeds[0].esd.Id,invoiceorder.Id);
            conTest.deleteOutboundorder(insertDetMap,invoiceorder.Id);
            System.Test.stopTest();
      
        }
    }

    static testMethod void conInvoiceViewTestgetdefaultMapinfo(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        Account hosp2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

       Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
         insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            
            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            System.Test.startTest();
            insert testList1;
            insert testList2;

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;

            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2};

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            
            conTest.init();

            conTest.invoiceId =invoiceorder.Id;
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            //conTest.SecondDealer = agency1.id;
            //conTest.coc.Order_ForDealer__c = agency1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            
            //conTest.SortLimited();
            //conTest.InvoiceorderSearch();
            System.assertEquals(1, conTest.invoiceOrderRecoeds.size());
            conTest.invoiceOrderRecoeds[0].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[0].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[0].esd.Order_ForDealer__c = agency1.id;

            List<String> orderIdList = new List<String>();
             orderIdList.add(testList1.Id);
            conTest.getdefaultMapinfo(orderIdList);
            System.Test.stopTest();
        }
    }

   //从出库单直接开票
    static testMethod void conInvoiceViewRedirectTest(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //经销商
        Account myAccount1 = new Account(name='testagency001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id );
        insert myAccount1;

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testagency1001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        insert agency1;

        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

        Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
        insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user myUser_test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='testUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert myUser_test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            //Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='草案中',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            //Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='已提交',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            
            Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            testList1.Order_date__c = Date.today().addDays(2);
            testList2.Order_date__c = Date.today().addDays(2);
            testList1.Deliver_date__c = Date.today();
            testList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            //insert new Consumable_order__c[]{testList1,testList2};

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;
            Consumable_order_LinkTable__c order_LinkTable2 = new Consumable_order_LinkTable__c(Name='order_LinkTable2',Outboundorder_Code_link__c = testList2.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable2;
            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            //出库单2明细1
            Consumable_orderdetails__c orderdet3 = new Consumable_orderdetails__c();
            orderdet3.Name = 'Test1_002001';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet3.Consumable_order__c = testList2.id;
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet3.Consumable_count__c = 1;
            orderdet3.Shipment_Count__c = 1;
            orderdet3.Consumable_product__c = pro2.Id;

             //出库单2明细2
            Consumable_orderdetails__c orderdet4 = new Consumable_orderdetails__c();
            orderdet4.Name = 'Test1_002002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet4.Consumable_order__c = testList2.id;
            orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet4.Consumable_count__c = 1;
            orderdet4.Shipment_Count__c = 1;
            orderdet4.Consumable_product__c = pro2.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2,orderdet3,orderdet4};
             //附件
            Consumable_accessories_invoice__c caic= new Consumable_accessories_invoice__c();
            caic.Attachment_ID__c = myAccount1.Id;
            caic.Accessories_type__c = '发票和明细';
            caic.Invoice_code__c = invoiceorder.id;
            insert caic;
            PageReference page = new PageReference('/apex/ConInvoiceView?deliveryId='+testList1.Id+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            System.Test.startTest();
            conTest.init();

            conTest.coc.Name = 'invoice002';
            conTest.coc.Invoice_Date__c = Date.today();
            conTest.HospitalInfo = hosp1.Id;
            conTest.SecondDealer = null;
            conTest.coc.Order_ForCustomerText__c = 'Test';
            conTest.InvoiceorderSearch();
            conTest.save();
            page = new PageReference('/apex/ConInvoiceView?deliveryId='+''+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);
            conTest.save();

            ConInvoiceViewController conTest1 = new ConInvoiceViewController();
            conTest1.init();

            conTest1.coc.Name = 'invoice003';
            conTest1.coc.Invoice_Date__c = Date.today();
            conTest1.HospitalInfo = hosp1.Id;
            conTest1.SecondDealer = agency1.Id;
            conTest1.coc.Order_ForCustomerText__c = 'Test1';
            conTest1.coc.Order_ForDealerText__c = '二级';
            conTest1.InvoiceorderSearch();
            conTest1.approval();
            conTest1.setEditAble();
            conTest1.FilesUpload();
            conTest1.nvoiceorderremind();
            conTest1.saveAttachment();
            conTest1.save();
            System.Test.stopTest();
        }
    }

    
    //提交
    static testMethod void conInvoiceViewTestapproval(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];


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

        //经销商
        Account myAccount1 = new Account(name='testagency001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id );
        insert myAccount1;

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testagency1001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        insert agency1;

        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

        Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
        insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user myUser_test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='testUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert myUser_test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            //Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='草案中',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            //Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',SummonsStatus_c__c='已完成',Order_ForHospital__c =hosp1.Id,SummonsForDirction__c='直接销售给医院',Order_status__c='已提交',Order_type__c='传票',Order_ForDealer__c = agency1.Id, Dealer_info__c=myAccount1.Id,ConInvoice_Code__c = invoiceorder.id);
            
            Consumable_order__c testList1 = new Consumable_order__c(Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                RecordTypeId = System.Label.RT_ConOrder_Sale,
                Dealer_info__c=myAccount1.Id);
            Consumable_order__c testList2 = new Consumable_order__c(
                Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                RecordTypeId = System.Label.RT_ConOrder_Sale,
                Dealer_info__c=myAccount1.Id);
            testList1.Order_date__c = Date.today().addDays(2);
            testList2.Order_date__c = Date.today().addDays(2);
            testList1.Deliver_date__c = Date.today();
            testList2.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            //insert new Consumable_order__c[]{testList1,testList2};

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;
            Consumable_order_LinkTable__c order_LinkTable2 = new Consumable_order_LinkTable__c(Name='order_LinkTable2',Outboundorder_Code_link__c = testList2.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable2;
            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = invoiceorder.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            //出库单2明细1
            Consumable_orderdetails__c orderdet3 = new Consumable_orderdetails__c();
            orderdet3.Name = 'Test1_002001';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet3.Consumable_order__c = invoiceorder.Id;
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet3.Consumable_count__c = 1;
            orderdet3.Shipment_Count__c = 1;
            orderdet3.Consumable_product__c = pro2.Id;

             //出库单2明细2
            Consumable_orderdetails__c orderdet4 = new Consumable_orderdetails__c();
            orderdet4.Name = 'Test1_002002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet4.Consumable_order__c = testList2.id;
            orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet4.Consumable_count__c = 1;
            orderdet4.Shipment_Count__c = 1;
            orderdet4.Consumable_product__c = pro2.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2,orderdet3,orderdet4};
             //附件
            Consumable_accessories_invoice__c caic= new Consumable_accessories_invoice__c();
            caic.Attachment_ID__c = myAccount1.Id;
            caic.Accessories_type__c = '发票和明细';
            caic.Invoice_code__c = invoiceorder.id;
            insert caic;
            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            conTest.invoiceId = invoiceorder.Id;
            System.Test.startTest();
            conTest.init();

            conTest.approval();
            System.Test.stopTest();
        }
    }

    //新建发票 以及编辑发票 invoiceId<>null
    static testMethod void conInvoiceViewTestedit_1_new(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];

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

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id,AgentCode_Ext__c = '9999900');
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        

        Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com',Work_Location__c = '北京');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //客户名HospitalInfo
            Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
            insert hosp1;
            Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
            insert agencyHospitalLink1;
            //出库单1
            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                recordtypeid = System.Label.RT_ConOrder_Sale,
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票',
                Order_ForHospital__c=hosp1.Id,
                Dealer_Info__c = myAccount1.Id,
                Order_ProType__c = 'ET');
            //出库单1
            Consumable_order__c testList2 = new Consumable_order__c(
                Name='Test2',
                recordtypeid = System.Label.RT_ConOrder_Sale,
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',
                Order_type__c='传票',
                Order_ForHospital__c=hosp1.Id,
                Dealer_Info__c = myAccount1.Id,
                Order_ProType__c = 'ET');
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            System.Test.startTest();
            insert testList1;
            insert testList2;

            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001';
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Sale;
            orderdet1.Shipment_Count__c = 1;
            orderdet1.Delivery_List_RMB__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;
            insert orderdet1;

            //出库单1明细2
            Consumable_order_details2__c orderdet2 = new Consumable_order_details2__c();
            orderdet2.Name = 'Test1_001001';
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            orderdet2.Consumable_Sale_order__c = testList1.Id;
            orderdet2.Deliver_date__c = Date.today();
            orderdet2.Arrive_date__c = Date.today();
            orderdet2.Used_date__c = Date.today();
            orderdet2.Consumable_product__c = pro1.Id;
            insert orderdet2;

            PageReference page = new PageReference('/apex/ConInvoiceView');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            
            conTest.init();
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            System.assertEquals(1, conTest.invoiceOrderRecoeds.size());

            conTest.invoiceOrderRecoeds[0].check= true;

            conTest.save();
            System.Test.stopTest();
        }
    }

    //提交发票
    static testMethod void conInvoice_approval(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];


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

        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,AgentCode_Ext__c = '9999900');
        insert myAccount1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //客户名HospitalInfo
            Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
            insert hosp1;
            Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
            insert agencyHospitalLink1;

            //出库单1
            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                recordtypeid = System.Label.RT_ConOrder_Sale,
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票',
                Order_ForHospital__c=hosp1.Id,
                Dealer_Info__c = myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            insert testList1;

            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001';
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Sale;
            orderdet1.Shipment_Count__c = 1;
            orderdet1.Delivery_List_RMB__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;
            insert orderdet1;

            //出库单1明细2
            Consumable_order_details2__c orderdet2 = new Consumable_order_details2__c();
            orderdet2.Name = 'Test1_001001';
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            orderdet2.Consumable_Sale_order__c = testList1.Id;
            orderdet2.Deliver_date__c = Date.today();
            orderdet2.Arrive_date__c = Date.today();
            orderdet2.Used_date__c = Date.today();
            orderdet2.Consumable_product__c = pro1.Id;
            insert orderdet2;

            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='草案中';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            invoiceorder.Order_ForHospital__c =hosp1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            insert invoiceorder;

            //发票明细1
            Consumable_orderdetails__c invoicedet1 = new Consumable_orderdetails__c();
            invoicedet1.Name = 'invoice01_01';
            invoicedet1.Consumable_order__c = invoiceorder.Id;
            invoicedet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            invoicedet1.Invoiced_Count__c = 1;
            invoicedet1.Invoice_Unitprice__c = 1;
            invoicedet1.Consumable_product__c = pro1.Id;
            invoicedet1.Invoicedet1_OD_link__c = testList1.Id;
            insert invoicedet1;

            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId=' + invoiceorder.Id);
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            System.Test.startTest();
            conTest.init();

            System.assertEquals(1, conTest.invoiceOrderRecoeds.size());
            conTest.approval();
            System.Test.stopTest();

        }
    }

    //CHAN-BSS5SQ    update by rentongxiao 2020-08-26  start
    //新建发票 以及编辑发票 invoiceId<>null
    static testMethod void rentestreopen(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];


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

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        Account hosp2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

       Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
         insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='提交';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            //invoiceorder.Order_ProType__c = 'ET';
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            
            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList3 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList3.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            TestList3.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            insert testList3;
            System.Test.startTest();
            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            Consumable_order_LinkTable__c order_LinkTable2 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList2.Id,Invoice_Code_link__c =invoiceorder.Id);
            Consumable_order_LinkTable__c order_LinkTable3 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList3.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;
            insert order_LinkTable2;
            insert order_LinkTable3;

            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2};

            //出库单2明细1
            Consumable_orderdetails__c orderdet3 = new Consumable_orderdetails__c();
            orderdet3.Name = 'Test3_001002';
            //orderdet3.Consumable_principal__c = testList2.Id;
            orderdet3.Consumable_order__c = testList2.Id;
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet3.Consumable_count__c = 1;
            orderdet3.Consumable_product__c = pro1.Id;
            insert orderdet3;

            //出库单3明细1
            Consumable_orderdetails__c orderdet4 = new Consumable_orderdetails__c();
            orderdet4.Name = 'Test3_001002';
            //orderdet4.Consumable_principal__c = testList2.Id;
            orderdet4.Consumable_order__c = testList3.Id;
            orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet4.Consumable_count__c = 1;
            orderdet4.Consumable_product__c = pro1.Id;
            insert orderdet4;

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&reopen=isreopen&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            
            conTest.init();
 
            conTest.invoiceId =invoiceorder.Id;
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            //conTest.SecondDealer = agency1.id;
            //conTest.coc.Order_ForDealer__c = agency1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            
            //conTest.SortLimited();
            //conTest.InvoiceorderSearch();
            System.assertEquals(3, conTest.invoiceOrderRecoeds.size());
            conTest.invoiceOrderRecoeds[0].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[0].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[0].esd.Order_ForDealer__c = agency1.id;

            conTest.invoiceOrderRecoeds[1].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[1].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[1].esd.Order_ForDealer__c = agency1.id;


            conTest.invoiceOrderRecoeds[2].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[2].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[2].esd.Order_ForDealer__c = agency1.id;
            
            conTest.invoiceOrderRecoeds[0].check= true;
            conTest.invoiceOrderRecoeds[1].check= false;
            conTest.invoiceOrderRecoeds[2].check= true;

            conTest.save();
            //System.assertEquals('123',conTest.testTmp);
            conTest.nvoiceorderremind();
            conTest.saveAttachment();

            conTest.getdone();
            conTest.getExistOutbound();
            conTest.getInvoiceName();
            System.Test.stopTest();
      
        }
    }

    //CHAN-BSS5SQ    update by rentongxiao 2020-08-26  end
     //CHAN-BSS5SQ    update by rentongxiao 2020-08-26  start
    //新建发票 以及编辑发票 invoiceId<>null
    static testMethod void rentestreopen001(){
        // Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证(ET)'];


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

        //二级经销商SecondDealer
        Account agency1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        Account agency2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectCo[0].Id);
        //经销商
        Account myAccount1 = new Account(name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id);
        insert agency1;
        insert agency2;
        insert myAccount1;
        //客户名HospitalInfo
        Account hosp1 = new Account(name='Testhosp001',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        Account hosp2 = new Account(name='Testhosp002',Dealer_discount__c =10,RecordTypeId = rectHos[0].Id);
        insert hosp1;

       Dealer_elationship__c dealerelationship = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency1.id);
        insert dealerelationship;
        Dealer_elationship__c dealerelationship1 = new Dealer_elationship__c(Dealer_principal__c = myAccount1.id,Dealer_subordinate__c = agency2.id);
        insert dealerelationship1;
        
        Agency_Hospital_Link__c agencyHospitalLink1 = new Agency_Hospital_Link__c(Hospital__c = hosp1.id,Agency__c = myAccount1.id);
         insert agencyHospitalLink1;

        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount1.id);
        insert core;
        user MyUser_Test = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser@testorg.com');
        insert MyUser_Test;
        system.runAs(myUser_test){
            //产品信息
            Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
            insert new Product2[] {prod01, prod02};

            Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            insert new Product2__c[] {pro1, pro2};
            //发票信息
            Consumable_order__c invoiceorder = new Consumable_order__c();
            invoiceorder.Name='invoice01';
            invoiceorder.Order_type__c = '发票';
            invoiceorder.Invoice_status__c='提交';
            invoiceorder.Invoice_Date__c = Date.today();
            invoiceorder.RecordTypeid = System.Label.RT_ConOrder_Invoice;
            //invoiceorder.Order_ForHospital__c =hosp1.Id;
            //invoiceorder.Order_ForDealer__c = agency1.Id;
            invoiceorder.Dealer_Info__c=myAccount1.Id;
            //invoiceorder.Order_ProType__c = 'ET';
            insert invoiceorder;

            invoiceorder = [select id,Name from Consumable_order__c where id =:invoiceorder.id];
            //出库单
            
            Consumable_order__c testList1 = new Consumable_order__c(
                Name='Test1',
                Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',
                SummonsForDirction__c='直接销售给医院',
                Order_status__c='草案中',
                Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList2 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);

            Consumable_order__c testList3 = new Consumable_order__c(Name='Test2',Summons_Sale_Status__c='出货',
                SummonsStatus_c__c='已完成',SummonsForDirction__c='直接销售给医院',
                Order_status__c='已提交',Order_type__c='传票', 
                Dealer_info__c=myAccount1.Id);
            TestList1.Order_date__c = Date.today().addDays(2);
            TestList2.Order_date__c = Date.today().addDays(2);
            TestList3.Order_date__c = Date.today().addDays(2);
            TestList1.Deliver_date__c = Date.today();
            TestList2.Deliver_date__c = Date.today();
            TestList3.Deliver_date__c = Date.today();
            insert testList1;
            insert testList2;
            insert testList3;
            System.Test.startTest();
            Consumable_order_LinkTable__c order_LinkTable1 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList1.Id,Invoice_Code_link__c =invoiceorder.Id);
            Consumable_order_LinkTable__c order_LinkTable2 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList2.Id,Invoice_Code_link__c =invoiceorder.Id);
            Consumable_order_LinkTable__c order_LinkTable3 = new Consumable_order_LinkTable__c(Name='order_LinkTable1',Outboundorder_Code_link__c = testList3.Id,Invoice_Code_link__c =invoiceorder.Id);
            insert order_LinkTable1;
            insert order_LinkTable2;
            insert order_LinkTable3;

            //出库单1明细1
            Consumable_orderdetails__c orderdet1 = new Consumable_orderdetails__c();
            orderdet1.Name = 'Test1_001001';
            //orderdet1.Consumable_principal__c = testList1.Id;
            orderdet1.Consumable_order__c = testList1.Id;
            orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet1.Consumable_count__c = 1;
            orderdet1.Consumable_product__c = pro1.Id;

            //出库单1明细2
            Consumable_orderdetails__c orderdet2 = new Consumable_orderdetails__c();
            orderdet2.Name = 'Test1_001002';
            //orderdet2.Consumable_principal__c = testList2.Id;
            orderdet2.Consumable_order__c = testList1.Id;
            orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet2.Consumable_count__c = 1;
            orderdet2.Consumable_product__c = pro1.Id;
            insert new Consumable_orderdetails__c[]{orderdet1,orderdet2};

            //出库单2明细1
            Consumable_orderdetails__c orderdet3 = new Consumable_orderdetails__c();
            orderdet3.Name = 'Test3_001002';
            //orderdet3.Consumable_principal__c = testList2.Id;
            orderdet3.Consumable_order__c = testList2.Id;
            orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet3.Consumable_count__c = 1;
            orderdet3.Consumable_product__c = pro1.Id;
            insert orderdet3;

            //出库单3明细1
            Consumable_orderdetails__c orderdet4 = new Consumable_orderdetails__c();
            orderdet4.Name = 'Test3_001002';
            //orderdet4.Consumable_principal__c = testList2.Id;
            orderdet4.Consumable_order__c = testList3.Id;
            orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail1_Invoice;
            orderdet4.Consumable_count__c = 1;
            orderdet4.Consumable_product__c = pro1.Id;
            insert orderdet4;

            PageReference page = new PageReference('/apex/ConInvoiceView?invoiceId='+invoiceorder.Id+'&reopen=isreopen&KeyWords=Redirect');
            page.setRedirect(true);
            System.Test.setCurrentPage(page);

            ConInvoiceViewController conTest = new ConInvoiceViewController();
            
            conTest.init();
 
            conTest.invoiceId =invoiceorder.Id;
            conTest.coc.SummonsForDirction__c = '直接销售给医院';
            conTest.HospitalInfo = hosp1.id;
            //conTest.SecondDealer = agency1.id;
            //conTest.coc.Order_ForDealer__c = agency1.id;
            conTest.coc.Name = 'invoice002';
            conTest.coc.Order_ForCustomerText__c = 'AA';
            conTest.coc.Order_ForHospital__c = hosp1.id;
            conTest.coc.Invoice_Date__c = Date.today();

            
            //conTest.SortLimited();
            //conTest.InvoiceorderSearch();
            System.assertEquals(3, conTest.invoiceOrderRecoeds.size());
            conTest.invoiceOrderRecoeds[0].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[0].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[0].esd.Order_ForDealer__c = agency1.id;

            conTest.invoiceOrderRecoeds[1].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[1].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[1].esd.Order_ForDealer__c = agency1.id;


            conTest.invoiceOrderRecoeds[2].esd.Order_ForCustomerText__c = 'AA';
            conTest.invoiceOrderRecoeds[2].esd.Order_ForHospital__c =hosp1.Id;
            conTest.invoiceOrderRecoeds[2].esd.Order_ForDealer__c = agency1.id;
            
            conTest.invoiceOrderRecoeds[0].check= true;
            conTest.invoiceOrderRecoeds[1].check= false;
            conTest.invoiceOrderRecoeds[2].check= true;

            conTest.save();
            //System.assertEquals('123',conTest.testTmp);
            conTest.nvoiceorderremind();
            conTest.saveAttachment();

            conTest.getdone();
            conTest.getExistOutbound();
            conTest.getInvoiceName();
            conTest.deleteButton();
            System.Test.stopTest();
      
        }
    }

}