@isTest
private class RentalAgencyReceivedControllerTest {
    static Id raId = null;
    static void setupTestData2() {
        // 省
        Address_Level__c al = new Address_Level__c();
        al.Name = '東京';
        al.Level1_Code__c = 'CN-99';
        al.Level1_Sys_No__c = '999999';
        insert al;
        // 市
        Address_Level2__c al2 = new Address_Level2__c();
        al2.Level1_Code__c = 'CN-99';
        al2.Level1_Sys_No__c = '999999';
        al2.Level1_Name__c = '東京';
        al2.Name = '渋谷区';
        al2.Level2_Code__c = 'CN-9999';
        al2.Level2_Sys_No__c = '9999999';
        al2.Address_Level__c = al.id;
        insert al2;
        // 病院を作る
        Account hospital = new Account();
        hospital.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'HP'].id;
        hospital.Name = 'test hospital';
        hospital.Is_Active__c = '有効';
        hospital.Attribute_Type__c = '卫生部';
        hospital.Speciality_Type__c = '综合医院';
        hospital.Grade__c = '一级';
        hospital.OCM_Category__c = 'SLTV';
        hospital.Is_Medical__c = '医疗机构';
        hospital.State_Master__c = al.id;
        hospital.City_Master__c = al2.id;
        hospital.Town__c = '东京';
        insert hospital;

        // 戦略科室を得る
        Account[] strategicDep = [SELECT ID, Name FROM Account WHERE parentId = :hospital.Id AND recordType.DeveloperName = 'Department_Class_OTH'];
        // 診療科を作る
        Account dep = new Account();
        dep.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep.Name = 'test dep';
        dep.AgentCode_Ext__c = '9999998';
        dep.ParentId = strategicDep[0].Id;
        dep.Department_Class__c = strategicDep[0].Id;
        dep.Hospital__c = hospital.Id;

        Account dep2 = new Account();
        dep2.recordtypeId = [Select Id FROM RecordType WHERE IsActive = true and SobjectType = 'Account' and DeveloperName = 'Department_OTH'].id;
        dep2.Name = 'test dep2';
        dep2.AgentCode_Ext__c = System.Label.Account_Asset_FJZ;
        dep2.ParentId = strategicDep[0].Id;
        dep2.Department_Class__c = strategicDep[0].Id;
        dep2.Hospital__c = hospital.Id;
        insert new List<Account>{dep, dep2};

        // 产品
        Product2 pro1 = new Product2(Name='name01',IsActive=true,Family='GI',
                Fixture_Model_No__c='n01',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n01', Asset_Model_No__c = 'Pro1',
                ProductCode_Ext__c='pc01',Manual_Entry__c=false,ProductCode='BP1');
        Product2 pro2 = new Product2(Name='name02',IsActive=true,Family='GI',
                Fixture_Model_No__c='n02',Serial_Lot_No__c='S/N tracing',
                Fixture_Model_No_T__c = 'n02', Asset_Model_No__c = 'Pro2',
                ProductCode_Ext__c='pc02',Manual_Entry__c=false,ProductCode='BP2');
        insert new Product2[] {pro1, pro2};

        //备品借出申请
        Rental_Apply__c raObj = new Rental_Apply__c();
        raObj.Name = 'testra';
        raObj.Product_category__c = 'GI';
        raObj.Demo_purpose1__c = '其他';
        raObj.demo_purpose2__c = '其他';
        raObj.Person_In_Charge__c = Userinfo.getUserId();
        raObj.applyUser__c = Userinfo.getUserId();
        Map<String,Schema.RecordTypeInfo>  DEVELOPERNAMEMAP  = Schema.SObjectType.Rental_Apply__c.getRecordTypeInfosByDeveloperName();
        raObj.RecordTypeId = DEVELOPERNAMEMAP.get('AgencyRequest').getRecordTypeId();
        raObj.Demo_purpose_text__c = '123';
        raObj.Request_shipping_day__c = Date.toDay();
        raObj.Request_return_day__c = Date.toDay();

        raObj.direct_send__c = '上门自提';
        raObj.Loaner_received_staff__c = '王五';
        raObj.Loaner_received_staff_phone__c = '110';
        raObj.pickup_time__c = System.now();
        insert raObj;
        raId = raObj.Id;

           // 保有设备
        Asset asset1 = new Asset(Asset_Owner__c = 'Olympus');
        asset1.RecordTypeId = System.Label.Asset_RecordType;
        asset1.SerialNumber = 'A1';
        asset1.Name = 'A1';
        asset1.AccountId = dep.Id;
        asset1.Department_Class__c = strategicDep[0].Id;
        asset1.Hospital__c = hospital.Id;
        asset1.Product2Id = pro1.Id;
        asset1.Quantity = 1;
        asset1.Status = '使用中';
        asset1.Manage_type__c = '个体管理';
        asset1.Loaner_accsessary__c = false;
        asset1.AssetManageConfirm__c = true;
        asset1.Salesdepartment__c = '0.备品中心';
        asset1.Internal_asset_location__c = '大连';
        asset1.Product_category__c = 'GI';
        asset1.Equipment_Type__c = '产品试用';
        asset1.SalesProvince__c = '辽宁';
        asset1.CompanyOfEquipment__c = '大连';
        asset1.Internal_Asset_number__c = '0003';
        asset1.WH_location__c = '货架号1';
        asset1.Rental_Count__c = 0;
        asset1.Out_of_wh__c = 0;

        Asset asset2 = new Asset(Asset_Owner__c = 'Olympus');
        asset2.RecordTypeId = System.Label.Asset_RecordType;
        asset2.SerialNumber = 'A2';
        asset2.Name = 'A2';
        asset2.AccountId = dep.Id;
        asset2.Department_Class__c = strategicDep[0].Id;
        asset2.Hospital__c = hospital.Id;
        asset2.Product2Id = pro2.Id;
        asset2.Quantity = 5;
        asset2.Status = '使用中';
        asset2.Manage_type__c = '数量管理';
        asset2.Loaner_accsessary__c = true;
        asset2.AssetManageConfirm__c = true;
        asset2.Salesdepartment__c = '0.备品中心';
        asset2.Internal_asset_location__c = '大连';
        asset2.Product_category__c = 'GI';
        asset2.Equipment_Type__c = '产品试用';
        asset2.SalesProvince__c = '辽宁';
        asset2.CompanyOfEquipment__c = '大连';
        asset2.WH_location__c = '货架号1';
        asset2.Rental_Count__c = 0;
        asset2.Out_of_wh__c = 0;

        insert new Asset[] {asset1, asset2};

        Fixture_Set__c fsObj1 = new Fixture_Set__c();
        fsObj1.Name = 'set1';
        fsObj1.Fixture_Set_Body_Model_No__c = 'modelNo1';
        fsObj1.Loaner_name__c = 'name1';
        insert fsObj1;
        // 备品配套明细
        Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
        fsdObjA1.Name = '备品配套明细名1';
        fsdObjA1.Name_CHN_Created__c = '中文名称1';
        fsdObjA1.Product2__c = pro1.Id;
        fsdObjA1.Fixture_Set__c = fsObj1.Id;
        fsdObjA1.Is_Body__c = true;
        fsdObjA1.Is_Optional__c = false;
        fsdObjA1.Is_OneToOne__c = false;
        fsdObjA1.UniqueKey__c = fsObj1.Id + ':' + pro1.Id;
        fsdObjA1.SortInt__c = 1;

        Fixture_Set_Detail__c fsdObjA2 = new Fixture_Set_Detail__c();
        fsdObjA2.Name = '备品配套明细名2';
        fsdObjA2.Name_CHN_Created__c = '中文名称2';
        fsdObjA2.Product2__c = pro2.Id;
        fsdObjA2.Fixture_Set__c = fsObj1.Id;
        fsdObjA2.Is_Body__c = false;
        fsdObjA2.Is_Optional__c = false;
        fsdObjA2.Is_OneToOne__c = true;
        fsdObjA2.UniqueKey__c = fsObj1.Id + ':' + pro2.Id;
        fsdObjA2.SortInt__c = 2;
        insert new Fixture_Set_Detail__c[] {fsdObjA1, fsdObjA2};

        // 借出备品配套一览
        Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
        raesObj.Rental_Apply__c = raObj.Id;
        raesObj.Fixture_Set__c = fsObj1.Id;
        raesObj.Cancel_Select__c = false;
        raesObj.Rental_Start_Date__c = Date.toDay();
        raesObj.Rental_End_Date__c = Date.toDay();
        raesObj.IndexFromUniqueKey__c = 1;
        raesObj.UniqueKey__c = '1:'+ fsObj1.Id + ':1';
        insert raesObj;

        // 借出备品配套一览明细
        Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObj1.Name = fsdObjA1.Name;
        raesdObj1.Rental_Apply__c = raObj.Id;
        raesdObj1.Fixture_Set_Detail__c = fsdObjA1.Id;
        raesdObj1.Rental_Num__c = 1;
        raesdObj1.Queue_Number__c = null;
        raesdObj1.Is_Body__c = true;
        raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
        raesdObj1.IndexFromUniqueKey__c = 1;
        raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA1.Id + ':1';
        //raesdObj1.FSD_OneToOneAccessory_Cnt__c = 10;
        //raesdObj1.FSD_Is_Optional__c = false;
        //raesdObj1.FSD_Is_OneToOne__c = false;
        //raesdObj1.ApplyPersonAppended__c = false;
        raesdObj1.FSD_Fixture_Model_No__c = 'n01';
        raesdObj1.Fixture_Model_No_text__c = 'n01';
        raesdObj1.Salesdepartment_before__c = '1.华北营业本部';
        raesdObj1.Internal_asset_location_before__c = '大连';
        raesdObj1.Product_category_text__c = 'GI';
        raesdObj1.Equipment_Type_text__c = '产品试用';

        Rental_Apply_Equipment_Set_Detail__c raesdObj2 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObj2.Name = fsdObjA2.Name;
        raesdObj2.Rental_Apply__c = raObj.Id;
        raesdObj2.Fixture_Set_Detail__c = fsdObjA2.Id;
        raesdObj2.Rental_Num__c = 1;
        raesdObj2.Queue_Number__c = null;
        raesdObj2.Is_Body__c = false;
        raesdObj2.Rental_Apply_Equipment_Set__c = raesObj.Id;
        raesdObj2.IndexFromUniqueKey__c = 1;
        raesdObj2.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA2.Id + ':1';
        //raesdObj2.FSD_OneToOneAccessory_Cnt__c = 1;
        //raesdObj2.FSD_Is_Optional__c = false;
        //raesdObj2.FSD_Is_OneToOne__c = false;
        //raesdObj2.ApplyPersonAppended__c = false;
        raesdObj2.FSD_Fixture_Model_No__c = 'n01';
        raesdObj2.Fixture_Model_No_text__c = 'n01';
        raesdObj2.Salesdepartment_before__c = '1.华北营业本部';
        raesdObj2.Internal_asset_location_before__c = '大连';
        raesdObj2.Product_category_text__c = 'GI';
        raesdObj2.Equipment_Type_text__c = '产品试用';

        Rental_Apply_Equipment_Set_Detail__c raesdObj3 = new Rental_Apply_Equipment_Set_Detail__c();
        raesdObj3.Name = fsdObjA2.Name;
        raesdObj3.Rental_Apply__c = raObj.Id;
        raesdObj3.Fixture_Set_Detail__c = fsdObjA2.Id;
        raesdObj3.Rental_Num__c = 1;
        raesdObj3.Queue_Number__c = null;
        raesdObj3.Is_Body__c = false;
        raesdObj3.Rental_Apply_Equipment_Set__c = raesObj.Id;
        raesdObj3.IndexFromUniqueKey__c = 2;
        raesdObj3.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA2.Id + ':2';
        raesdObj3.FSD_Fixture_Model_No__c = 'n01';
        raesdObj3.Fixture_Model_No_text__c = 'n01';
        raesdObj3.Salesdepartment_before__c = '1.华北营业本部';
        raesdObj3.Internal_asset_location_before__c = '大连';
        raesdObj3.Product_category_text__c = 'GI';
        raesdObj3.Equipment_Type_text__c = '产品试用';
        insert new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1, raesdObj2, raesdObj3};

        raesObj.First_RAESD__c = raesdObj1.Id;
        update raesObj;

        // 申请单变为已批准
        raObj.Status__c = '已批准';
        update raObj;

        FixtureDeliverySlip__c fdsObj = new FixtureDeliverySlip__c();
        fdsObj.Name = '00001';
        fdsObj.DeliveryCompany__c = '利讯';
        fdsObj.Distributor_method__c = '陆运';
        fdsObj.DeliveryType__c = '发货';
        fdsObj.Wh_Staff__c = Userinfo.getUserId();
        fdsObj.Shippment_loaner_time__c = System.now();

        FixtureDeliverySlip__c fdsObj2 = new FixtureDeliverySlip__c();
        fdsObj2.Name = '00002';
        fdsObj2.DeliveryCompany__c = '利讯';
        fdsObj2.Distributor_method__c = '陆运';
        fdsObj2.DeliveryType__c = '回寄';
        fdsObj2.Wh_Staff__c = Userinfo.getUserId();
        fdsObj2.Shippment_loaner_time__c = System.now();
        insert new List<FixtureDeliverySlip__c>{fdsObj,fdsObj2};

        raesdObj1.StockDown__c = true;
        raesdObj1.StockDown_time__c = Date.today();
        raesdObj1.Inspection_result__c = 'OK';
        raesdObj1.DeliverySlip__c = fdsObj.Id;
        raesdObj1.Return_DeliverySlip__c = fdsObj2.Id;
        raesdObj1.Shipment_request_time2__c = Date.toDay();
        raesdObj1.Select_time__c = System.now();
        raesdObj1.Asset__c = asset1.Id;

        raesdObj2.StockDown__c = true;
        raesdObj2.StockDown_time__c = Date.today();
        raesdObj2.Inspection_result__c = 'OK';
        raesdObj2.DeliverySlip__c = fdsObj.Id;
        raesdObj2.Return_DeliverySlip__c = fdsObj2.Id;
        raesdObj2.Shipment_request_time2__c = Date.toDay();
        raesdObj2.Select_time__c = System.now();
        raesdObj2.Asset__c = asset2.Id;
        raesdObj2.Fixture_OneToOne_Link_Id__c = 'abc';

        raesdObj3.StockDown__c = true;
        raesdObj3.StockDown_time__c = Date.today();
        raesdObj3.Inspection_result__c = 'OK';
        raesdObj3.DeliverySlip__c = fdsObj.Id;
        raesdObj3.Return_DeliverySlip__c = fdsObj2.Id;
        raesdObj3.Shipment_request_time2__c = Date.toDay();
        raesdObj3.Select_time__c = System.now();
        raesdObj3.Asset__c = asset2.Id;
        raesdObj3.Fixture_OneToOne_Link_Id__c = 'abc';
        update new Rental_Apply_Equipment_Set_Detail__c[] {raesdObj1, raesdObj2, raesdObj3};

        OlympusCalendar__c oc = new OlympusCalendar__c();
        oc.Date__c = System.now().date();
        oc.After_2_WorkDay__c = System.now().addDays(2).date();
        insert oc;
    }

    @isTest
    public static void test_lostAndCDS() {
        setupTestData2();

        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();
        Test.startTest();

        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);
        controller.stepName = '欠品确认&CDS';
        controller.esdList[0].checked = true;
        controller.esdList[0].lostFinished = 'OK';
        controller.esdList[0].cdsFinished = 'OK';
        controller.lostAndCDS();
        System.assertEquals('OK', controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(true, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals(true, controller.done_flg);

        controller.lostResult = '欠品';
        controller.cdsResult = 'OK';
        controller.filter();
        System.assertEquals(true, controller.esdList[0].hidden);
        controller.clearFilter();
        System.assertEquals('', controller.lostResult);
        System.assertEquals('', controller.cdsResult);

        Test.stopTest();
    }
    @isTest
    public static void test_lostAndCDS_error() {
        setupTestData2();

        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();
        Test.startTest();

        controller.init();
        System.assertEquals(3, controller.esdList.size());

        controller.lostAndCDS();
        System.assertEquals(null, controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(false, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals(false, controller.done_flg);

        Test.stopTest();
        List<Apexpages.message> msgs = ApexPages.getMessages();
        System.assertEquals('未选择操作的一览', msgs[0].getDetail());
    }

    @isTest
    public static void test_stockupOK_OK() {
        setupTestData2();
        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();

        Test.startTest();
        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);

        controller.stepName = '欠品确认&CDS';
        controller.esdList[0].checked = true;
        controller.esdList[0].lostFinished = 'OK';
        controller.esdList[0].cdsFinished = 'OK';
        controller.esdList[1].lostFinished = 'OK';
        controller.esdList[1].cdsFinished = 'OK';
        controller.esdList[2].lostFinished = 'OK';
        controller.esdList[2].cdsFinished = 'OK';

        controller.lostAndCDS();
        System.assertEquals('OK', controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(true, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals(true, controller.done_flg);

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        controller.esdList[0].rec.Arrival_wh_Result_Agency__c = 'OK';
        controller.esdList[1].rec.Arrival_wh_Result_Agency__c = 'OK';
        controller.esdList[2].rec.Arrival_wh_Result_Agency__c = 'OK';
        controller.stockup();
        System.assertEquals(true, controller.done_flg);
        System.assertEquals(1, [SELECT Id FROM Fixture_OneToOne_Link__c].size());
        Test.stopTest();
    }
    @isTest
    public static void test_stockupAb_OK() {
        setupTestData2();
        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();

        Test.startTest();
        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);

        controller.stepName = '欠品确认&CDS';
        controller.esdList[0].checked = true;
        controller.esdList[0].lostFinished = 'OK';
        controller.esdList[0].cdsFinished = 'OK';
        controller.esdList[1].lostFinished = 'OK';
        controller.esdList[1].cdsFinished = 'OK';
        controller.esdList[2].lostFinished = 'OK';
        controller.esdList[2].cdsFinished = 'OK';

        controller.lostAndCDS();
        System.assertEquals('OK', controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(true, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals(true, controller.done_flg);

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        controller.esdList[0].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.esdList[1].rec.Arrival_wh_Result_Agency__c = '待修理';
        controller.esdList[2].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.stockup();
        System.assertEquals(true, controller.done_flg);
        System.assertEquals(1, [SELECT Id FROM Fixture_OneToOne_Link__c].size());
        Test.stopTest();
    }
    @isTest
    public static void test_stockupLost_OK() {
        setupTestData2();
        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();

        Test.startTest();
        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);

        controller.stepName = '欠品确认&CDS';
        controller.esdList[0].checked = true;
        controller.esdList[0].lostFinished = '欠品';
        //controller.esdList[0].cdsFinished = 'OK';
        controller.esdList[1].lostFinished = 'OK';
        controller.esdList[1].cdsFinished = 'OK';
        controller.esdList[2].lostFinished = 'OK';
        controller.esdList[2].cdsFinished = 'OK';

        controller.lostAndCDS();
        System.assertEquals('欠品', controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(false, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals(true, controller.done_flg);

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        //controller.esdList[0].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.esdList[1].rec.Arrival_wh_Result_Agency__c = 'OK';
        controller.esdList[2].rec.Arrival_wh_Result_Agency__c = 'OK';
        controller.stockup();
        System.assertEquals(true, controller.done_flg);
        System.assertEquals(1, [SELECT Id FROM Fixture_OneToOne_Link__c].size());
        Test.stopTest();
    }
    @isTest
    public static void test_stockup_error() {
        setupTestData2();

        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();
        Test.startTest();

        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);

        controller.stepName = '回库确认';
        System.assertEquals(null, controller.esdList[0].lostFinished);
        System.assertEquals('', controller.esdList[0].cdsFinished);
        controller.esdList[0].checked = true;
        controller.esdList[0].rec.Arrival_wh_Result_Agency__c = 'OK';
        controller.stockup();
        Test.stopTest();

        List<Apexpages.message> msgs = ApexPages.getMessages();
        System.assertEquals('n01未完成欠品确认&CDS，不可回库！', msgs[0].getDetail());
    }

    @isTest
    public static void test_init_error() {

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + '12345');
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();
        Test.startTest();

        controller.init();

        Test.stopTest();
        List<Apexpages.message> msgs = ApexPages.getMessages();
        System.assertEquals('备品借出申请不存在！', msgs[0].getDetail());
    }
    // 回库后有盘点
    @isTest
    public static void test_stockupAb_OK_Inventory() {
        setupTestData2();
        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();

        Test.startTest();
        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);

        controller.stepName = '欠品确认&CDS';
        controller.esdList[0].checked = true;
        controller.esdList[0].lostFinished = 'OK';
        controller.esdList[0].cdsFinished = 'OK';
        controller.esdList[1].lostFinished = 'OK';
        controller.esdList[1].cdsFinished = 'OK';
        controller.esdList[2].lostFinished = 'OK';
        controller.esdList[2].cdsFinished = 'OK';

        controller.lostAndCDS();
        System.assertEquals('OK', controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(true, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals(true, controller.done_flg);

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        controller.esdList[0].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.esdList[1].rec.Arrival_wh_Result_Agency__c = '待修理';
        controller.esdList[2].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.stockup();
        System.assertEquals(true, controller.done_flg);
        System.assertEquals(1, [SELECT Id FROM Fixture_OneToOne_Link__c].size());

        SS_Batch_Column_Mapping__c InventoryHeaderName = new SS_Batch_Column_Mapping__c(Name = 'Inventory_Header_Name', SS_TableName__c='Inventory_Header_Name');
        InventoryHeaderName.From_Column_004__c = '大连';
        InventoryHeaderName.SS_Column_004__c = 'DL';
        insert new SS_Batch_Column_Mapping__c[] {InventoryHeaderName};

        Inventory_Header__c header = new Inventory_Header__c();
        header.Inventory_Start_Date__c = System.today();
        header.Internal_asset_location__c = '大连';
        header.UniqueKey__c='1111';
        header.Inventory_Status__c = '盘点中';
        insert header;

        controller.init();
        System.assertEquals(false, controller.esdList[0].canChangeResult);
        System.assertEquals(false, controller.esdList[1].canChangeResult);
        System.assertEquals(false, controller.esdList[2].canChangeResult);

        Test.stopTest();
    }
    // 回库后有调拨
    @isTest
    public static void test_stockupAb_OK_Transfer() {
        setupTestData2();
        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();

        Test.startTest();
        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);

        controller.stepName = '欠品确认&CDS';
        controller.esdList[0].checked = true;
        controller.esdList[0].lostFinished = 'OK';
        controller.esdList[0].cdsFinished = 'OK';
        controller.esdList[1].lostFinished = 'OK';
        controller.esdList[1].cdsFinished = 'OK';
        controller.esdList[2].lostFinished = 'OK';
        controller.esdList[2].cdsFinished = 'OK';

        controller.lostAndCDS();
        System.assertEquals('OK', controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(true, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals(true, controller.done_flg);

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        controller.esdList[0].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.esdList[1].rec.Arrival_wh_Result_Agency__c = '待修理';
        controller.esdList[2].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.stockup();
        System.assertEquals(true, controller.done_flg);
        System.assertEquals(1, [SELECT Id FROM Fixture_OneToOne_Link__c].size());

        setupTransfer();
        controller.init();
        System.assertEquals(false, controller.esdList[0].canChangeResult);
        System.assertEquals(false, controller.esdList[1].canChangeResult);
        System.assertEquals(false, controller.esdList[2].canChangeResult);

        Test.stopTest();
    }
    private static void setupTransfer() {
        setupTransfer('all');
    }
    private static void setupTransfer(String testcase) {
        TransferApply__c raObj = new TransferApply__c();
        raObj.Name = 'testra';
        raObj.OwnerId = Userinfo.getUserId();
        raObj.From_Location__c = '大连';
        raObj.Contact_Person__c = Userinfo.getUserId();
        Map<String,Schema.RecordTypeInfo>  DEVELOPERNAMEMAP  = Schema.SObjectType.TransferApply__c.getRecordTypeInfosByDeveloperName();
        raObj.RecordTypeId = DEVELOPERNAMEMAP.get('AgencyToCenter').getRecordTypeId();
        raObj.Destination_location__c = '上海 备品中心';
        insert raObj;

        TransferApplySummary__c raesObj = new TransferApplySummary__c();
        raesObj.TransferApply__c = raObj.Id;
        raesObj.Fixture_Set__c = [SELECT Id FROM Fixture_Set__c LIMIT 1].Id;
        raesObj.Cancel_Select__c = false;
        raesObj.IndexFromUniqueKey__c = 1;
        insert raesObj;

        TransferApplyDetail__c raesdObj1 = new TransferApplyDetail__c();
        TransferApplyDetail__c raesdObj2 = new TransferApplyDetail__c();
        TransferApplyDetail__c raesdObj3 = new TransferApplyDetail__c();
        List<Fixture_Set_Detail__c> fsdObjs = [SELECT Id FROM Fixture_Set_Detail__c ORDER BY Name];
        List<Asset> assets = [SELECT Id FROM Asset ORDER BY SerialNumber];
        raesdObj1.TransferApply__c = raObj.Id;
        raesdObj1.Fixture_Set_Detail__c = fsdObjs[0].Id;
        raesdObj1.TransferApplySummary__c = raesObj.Id;
        raesdObj1.IndexFromUniqueKey__c = 1;
        raesdObj1.FSD_OneToOneAccessory_Cnt__c = 2;
        raesdObj1.ApplyPersonAppended__c = false;
        raesdObj1.TransferCount__c = 1;
        raesdObj1.FSD_SortInt__c = 01;
        raesdObj1.Asset__c = assets[0].Id;
        raesdObj1.Main_OneToOne__c = true;
        raesdObj1.Loaner_accsessary__c = false;
        raesdObj1.FSD_Name_CHN__c = 'name01';
        raesdObj1.TransferType__c = '待废弃';

        raesdObj2.TransferApply__c = raObj.Id;
        raesdObj2.Fixture_Set_Detail__c = fsdObjs[1].Id;
        raesdObj2.TransferApplySummary__c = raesObj.Id;
        raesdObj2.IndexFromUniqueKey__c = 2;
        raesdObj2.FSD_OneToOneAccessory_Cnt__c = 2;
        raesdObj2.ApplyPersonAppended__c = false;
        raesdObj2.TransferCount__c = 1;
        raesdObj2.FSD_SortInt__c = 02;
        raesdObj2.Asset__c = assets[1].Id;
        raesdObj2.Loaner_accsessary__c = true;
        raesdObj2.OneToOneAccessory__c = true;
        raesdObj2.FSD_Name_CHN__c = 'name02';
        raesdObj2.TransferType__c = '待修理';


        raesdObj3.TransferApply__c = raObj.Id;
        raesdObj3.Fixture_Set_Detail__c = fsdObjs[1].Id;
        raesdObj3.TransferApplySummary__c = raesObj.Id;
        raesdObj3.IndexFromUniqueKey__c = 3;
        raesdObj3.FSD_OneToOneAccessory_Cnt__c = 2;
        raesdObj3.ApplyPersonAppended__c = false;
        raesdObj3.FSD_SortInt__c = 02;
        raesdObj3.Asset__c = assets[1].Id;
        raesdObj3.OneToOneAccessory__c = true;
        raesdObj3.Loaner_accsessary__c = true;
        raesdObj3.FSD_Name_CHN__c = 'name02';
        raesdObj3.TransferType__c = '待废弃';
        switch on testcase {
            when 'all' {
                insert new TransferApplyDetail__c[] {raesdObj1,raesdObj2,raesdObj3};
            }
            when 'main' {
                insert new TransferApplyDetail__c[] {raesdObj1,raesdObj2};
            }
            when 'accessory' {
                insert new TransferApplyDetail__c[] {raesdObj2};
            }
        }
    }
    private static void setupTransferAccessory() {
        TransferApply__c raObj = new TransferApply__c();
        raObj.Name = 'testra';
        raObj.OwnerId = Userinfo.getUserId();
        raObj.From_Location__c = '北京';
        raObj.Contact_Person__c = Userinfo.getUserId();
        Map<String,Schema.RecordTypeInfo>  DEVELOPERNAMEMAP  = Schema.SObjectType.TransferApply__c.getRecordTypeInfosByDeveloperName();
        raObj.RecordTypeId = DEVELOPERNAMEMAP.get('AgencyToCenter').getRecordTypeId();
        raObj.Destination_location__c = '上海 备品中心';
        insert raObj;

        TransferApplySummary__c raesObj = new TransferApplySummary__c();
        raesObj.TransferApply__c = raObj.Id;
        raesObj.Fixture_Set__c = [SELECT Id FROM Fixture_Set__c LIMIT 1].Id;
        raesObj.Cancel_Select__c = false;
        raesObj.IndexFromUniqueKey__c = 1;
        insert raesObj;

        TransferApplyDetail__c raesdObj2 = new TransferApplyDetail__c();
        List<Fixture_Set_Detail__c> fsdObjs = [SELECT Id FROM Fixture_Set_Detail__c ORDER BY Name];
        List<Asset> assets = [SELECT Id FROM Asset ORDER BY SerialNumber];

        raesdObj2.TransferApply__c = raObj.Id;
        raesdObj2.TransferApplySummary__c = raesObj.Id;
        raesdObj2.IndexFromUniqueKey__c = 2;
        raesdObj2.FSD_OneToOneAccessory_Cnt__c = 1;
        raesdObj2.ApplyPersonAppended__c = false;
        raesdObj2.TransferCount__c = 1;
        raesdObj2.FSD_SortInt__c = 01;
        raesdObj2.Asset__c = assets[1].Id;
        raesdObj2.Loaner_accsessary__c = true;
        raesdObj2.OneToOneAccessory__c = false;
        raesdObj2.TransferType__c = '待修理';
        insert raesdObj2;
    }
    private static void setupTransferMain() {
        TransferApply__c raObj = new TransferApply__c();
        raObj.Name = 'testra';
        raObj.OwnerId = Userinfo.getUserId();
        raObj.From_Location__c = '北京';
        raObj.Contact_Person__c = Userinfo.getUserId();
        Map<String,Schema.RecordTypeInfo>  DEVELOPERNAMEMAP  = Schema.SObjectType.TransferApply__c.getRecordTypeInfosByDeveloperName();
        raObj.RecordTypeId = DEVELOPERNAMEMAP.get('AgencyToCenter').getRecordTypeId();
        raObj.Destination_location__c = '上海 备品中心';
        insert raObj;

        TransferApplySummary__c raesObj = new TransferApplySummary__c();
        raesObj.TransferApply__c = raObj.Id;
        raesObj.Fixture_Set__c = [SELECT Id FROM Fixture_Set__c LIMIT 1].Id;
        raesObj.Cancel_Select__c = false;
        raesObj.IndexFromUniqueKey__c = 1;
        insert raesObj;

        TransferApplyDetail__c raesdObj1 = new TransferApplyDetail__c();
        TransferApplyDetail__c raesdObj2 = new TransferApplyDetail__c();
        List<Fixture_Set_Detail__c> fsdObjs = [SELECT Id FROM Fixture_Set_Detail__c ORDER BY Name];
        List<Asset> assets = [SELECT Id FROM Asset ORDER BY SerialNumber];
        raesdObj1.TransferApply__c = raObj.Id;
        raesdObj1.Fixture_Set_Detail__c = fsdObjs[0].Id;
        raesdObj1.TransferApplySummary__c = raesObj.Id;
        raesdObj1.IndexFromUniqueKey__c = 1;
        raesdObj1.FSD_OneToOneAccessory_Cnt__c = 2;
        raesdObj1.ApplyPersonAppended__c = false;
        raesdObj1.TransferCount__c = 1;
        raesdObj1.FSD_SortInt__c = 01;
        raesdObj1.Asset__c = assets[0].Id;
        raesdObj1.Main_OneToOne__c = true;
        raesdObj1.Loaner_accsessary__c = false;
        raesdObj1.FSD_Name_CHN__c = 'name01';
        raesdObj1.TransferType__c = '待废弃';

        raesdObj2.TransferApply__c = raObj.Id;
        raesdObj2.Fixture_Set_Detail__c = fsdObjs[1].Id;
        raesdObj2.TransferApplySummary__c = raesObj.Id;
        raesdObj2.IndexFromUniqueKey__c = 2;
        raesdObj2.FSD_OneToOneAccessory_Cnt__c = 1;
        raesdObj2.ApplyPersonAppended__c = false;
        raesdObj2.TransferCount__c = 1;
        raesdObj2.FSD_SortInt__c = 01;
        raesdObj2.Asset__c = assets[1].Id;
        raesdObj2.Loaner_accsessary__c = true;
        raesdObj2.OneToOneAccessory__c = false;
        raesdObj2.FSD_Name_CHN__c = 'name02';
        raesdObj2.TransferType__c = '待修理';

        insert new TransferApplyDetail__c[] {raesdObj1,raesdObj2};
    }
    // 1 主体上架，2 主体调拨，3附属品上架
    @isTest
    public static void test_stockup_AfterMainTransfer() {
        Oly_TriggerHandler.bypass(AssetHandler.class.getName());

        setupTestData2();

        List<Rental_Apply_Equipment_Set_Detail__c> raesds = [
        SELECT Id,Asset__c,Rental_Apply_Equipment_Set__r.First_RAESD__r.Asset__c FROM Rental_Apply_Equipment_Set_Detail__c
        ORDER BY Rental_Apply_Equipment_Set__r.Name, Rental_Apply_Equipment_Set__c, Is_Body__c DESC, Name];
        System.debug('test+'+raesds);
        Id userId = Userinfo.getUserId();
        Datetime now = System.now();
        raesds[0].Check_lost_Item__c='OK';
        raesds[0].CDS_complete__c=true;
        raesds[0].Arrival_wh_Result_Agency__c = '移至报废区';
        raesds[0].Inspection_result_after__c = 'NG';
        raesds[0].Inspection_result_after_NG__c = '废弃';
        raesds[0].After_Inspection_time__c = now;
        raesds[0].Inspection_staff_After__c = userId;
        raesds[0].Arrival_in_wh__c = true;
        raesds[0].Arrival_wh_time_Agency_First__c = now;
        raesds[0].Arrival_wh_After_2_Workday__c = now.addDays(2).date();
        raesds[0].Return_wh_chenk_staff__c = userId;

        raesds[1].Check_lost_Item__c='OK';
        raesds[1].CDS_complete__c=true;
        raesds[1].Arrival_wh_Result_Agency__c = '待修理';
        raesds[1].Inspection_result_after__c = 'NG';
        raesds[1].Inspection_result_after_NG__c = '维修';
        raesds[1].After_Inspection_time__c = now;
        raesds[1].Inspection_staff_After__c = userId;
        raesds[1].Arrival_in_wh__c = true;
        raesds[1].Arrival_wh_time_Agency_First__c = now;
        raesds[1].Arrival_wh_After_2_Workday__c = now.addDays(2).date();
        raesds[1].Return_wh_chenk_staff__c = userId;

        raesds[2].Check_lost_Item__c='OK';
        raesds[2].CDS_complete__c=true;

        update raesds;
        Fixture_OneToOne_Link__c link = new Fixture_OneToOne_Link__c(
            Main_Asset__c=raesds[0].Asset__c, Accessory_Asset__c=raesds[1].Asset__c,
            Quantity__c = 1, CountForRepair__c=1);
        insert link;

        Test.startTest();

        setupTransfer('main');

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raId);
        Test.setCurrentPage(ref);
        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();

        controller.init();

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        controller.esdList[2].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.stockup();

        System.assertEquals(true, controller.done_flg);
        List<Fixture_OneToOne_Link__c> links = [SELECT Id,Quantity__c, CountForRepair__c, Abandoned_RealThing__c FROM Fixture_OneToOne_Link__c];
        System.debug('test+'+links);
        System.assertEquals(1, links.size());
        System.assertEquals(1, links[0].Quantity__c);
        System.assertEquals(1, links[0].CountForRepair__c);
        System.assertEquals(0, links[0].Abandoned_RealThing__c);
        Test.stopTest();

    }
    // 1 附属品上架，2 附属品部分调拨，3主体上架
    @isTest
    public static void test_stockup_AfterAccessoryTransfer() {
        Oly_TriggerHandler.bypass(AssetHandler.class.getName());
        setupTestData2();

        List<Rental_Apply__c> raList = [SELECT Id FROM Rental_Apply__c];

        PageReference ref = new PageReference('/apex/RentalAgencyReceived?id=' + raList[0].Id);
        Test.setCurrentPage(ref);

        RentalAgencyReceivedController controller = new RentalAgencyReceivedController();

        controller.init();
        System.assertEquals(3, controller.esdList.size());
        System.assertEquals(true, controller.esdList[0].editable);
        System.assertEquals(true, controller.esdList[1].editable);

        controller.stepName = '欠品确认&CDS';
        controller.esdList[0].checked = true;
        controller.esdList[0].lostFinished = '欠品';
        controller.esdList[1].lostFinished = 'OK';
        controller.esdList[1].cdsFinished = 'OK';
        controller.esdList[2].lostFinished = 'OK';
        controller.esdList[2].cdsFinished = 'OK';

        controller.lostAndCDS();
        System.assertEquals('欠品', controller.esdList[0].rec.Check_lost_Item__c);
        System.assertEquals(false, controller.esdList[0].rec.CDS_complete__c);
        System.assertEquals('OK', controller.esdList[1].rec.Check_lost_Item__c);
        System.assertEquals(true, controller.esdList[1].rec.CDS_complete__c);
        System.assertEquals('OK', controller.esdList[2].rec.Check_lost_Item__c);
        System.assertEquals(true, controller.esdList[2].rec.CDS_complete__c);
        System.assertEquals(true, controller.done_flg);

        Test.startTest();

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        controller.esdList[1].rec.Arrival_wh_Result_Agency__c = '待修理';
        controller.esdList[2].rec.Arrival_wh_Result_Agency__c = '移至报废区';
        controller.stockup();
        System.assertEquals(true, controller.done_flg);
        System.assertEquals(0, [SELECT Id FROM Fixture_OneToOne_Link__c].size());

        setupTransfer('accessory');

        FixtureDeliverySlip__c fdsObj2 = [SELECT Id FROM FixtureDeliverySlip__c WHERE DeliveryType__c='回寄'];
        update new Rental_Apply_Equipment_Set_Detail__c(Id =controller.esdList[0].rec.Id, Return_DeliverySlip__c = fdsObj2.Id,
        Check_lost_Item__c='OK',  CDS_complete__c=true);

        controller.init();

        controller.stepName = '回库确认';
        controller.esdList[0].checked = true;
        controller.esdList[0].rec.Arrival_wh_Result_Agency__c = '待修理';
        controller.stockup();

        System.assertEquals(true, controller.done_flg);
        List<Fixture_OneToOne_Link__c> links = [SELECT Id,Quantity__c, CountForRepair__c, Abandoned_RealThing__c FROM Fixture_OneToOne_Link__c];
        System.assertEquals(1, links.size());
        System.assertEquals(1, links[0].Quantity__c);
        System.assertEquals(1, links[0].Abandoned_RealThing__c);
        Test.stopTest();

    }
}