@isTest
private class LoanerTakeBackControllerTest {
	static void setupTestData() {
        OlympusCalendar__c oc1 = new OlympusCalendar__c(Date__c = Date.today().addDays(1), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc2 = new OlympusCalendar__c(Date__c = Date.today().addDays(2), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc3 = new OlympusCalendar__c(Date__c = Date.today().addDays(3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc4 = new OlympusCalendar__c(Date__c = Date.today().addDays(4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc5 = new OlympusCalendar__c(Date__c = Date.today().addDays(5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc6 = new OlympusCalendar__c(Date__c = Date.today().addDays(6), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc7 = new OlympusCalendar__c(Date__c = Date.today().addDays(7), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc8 = new OlympusCalendar__c(Date__c = Date.today().addDays(8), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc9 = new OlympusCalendar__c(Date__c = Date.today().addDays(9), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc10 = new OlympusCalendar__c(Date__c = Date.today().addDays(10), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc11 = new OlympusCalendar__c(Date__c = Date.today().addDays(11), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc12 = new OlympusCalendar__c(Date__c = Date.today().addDays(12), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc13 = new OlympusCalendar__c(Date__c = Date.today().addDays(-1), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc14 = new OlympusCalendar__c(Date__c = Date.today().addDays(-2), ChangeToHoliday__c=true, ChangeToWorkday__c=false);
        OlympusCalendar__c oc15 = new OlympusCalendar__c(Date__c = Date.today().addDays(-3), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc16 = new OlympusCalendar__c(Date__c = Date.today().addDays(-4), ChangeToHoliday__c=false, ChangeToWorkday__c=true);
        OlympusCalendar__c oc17 = new OlympusCalendar__c(Date__c = Date.today().addDays(-5), ChangeToHoliday__c=false, ChangeToWorkday__c=true);

        insert new OlympusCalendar__c[] {oc1,oc2,oc3,oc4,oc5,oc6,oc7,oc8,oc9,oc10,oc11,oc12,oc13,oc14,oc15,oc16,oc17};
    }
	@isTest static void test_method_one() {
                setupTestData();
		// Implement test code
		List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'IE'];
        
                List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer IE'];
                
                // 新建备品借出申请
                loaner_application__c loaner = new loaner_application__c();
                loaner.Name = 'TEST';
                loaner.RecordTypeId = rectLo[0].id;
                loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE';
                loaner.DEMO_PURPOSE__C = '演示';
                loaner.Request_shipping_Date__c = Date.today();
                loaner.Request_return_Date__c = Date.today();
                loaner.Status__c = '已出库指示';
                loaner.Return_Track_Company__c = '物流公司';
                //loaner.Return_Trake_Staff__c = '返品人';
                insert loaner;

                // 新建 客户
                Account acc = new Account();
                acc.RecordTypeId = rectCo[0].Id;
                acc.Name         = '客户テスト1';
                acc.DivisionName__c = 'DivisionName__c';
                acc.FacilityName__c = 'FacilityName__c';
                acc.PostCode__c='000000';
                insert acc;
                 
                // 新建 资产
                List<Asset> In_asset = new List<Asset>();

                Asset asset = new Asset();
                asset.Name = 'テスト機器1';
                asset.AccountId = acc.Id;
                asset.SerialNumber = 'testserial';
                In_asset.add(asset);

                asset = new Asset();
                asset.Name = 'テスト機器2';
                asset.AccountId = acc.Id;
                asset.SerialNumber = 'testserial';
                In_asset.add(asset);

                asset = new Asset();
                asset.Name = 'テスト機器3';
                asset.AccountId = acc.Id;
                asset.SerialNumber = 'testserial';
                In_asset.add(asset);

                insert In_asset;

                // 新建 借出明细
                List<loaner_application_detail__c> In_detail = new List<loaner_application_detail__c>();
                loaner_application_detail__c detail = new loaner_application_detail__c();

                detail.loaner_application__c = loaner.Id;
                detail.loaner__c = In_asset[0].id;
                detail.name = '000';
                detail.RAESD_STATUS__C = '已回寄';
                In_detail.add(detail);

                detail = new loaner_application_detail__c();
                detail.loaner_application__c = loaner.Id;
                detail.loaner__c = In_asset[1].id;
                detail.name = '111';
                detail.RAESD_STATUS__C = '已回寄';
                In_detail.add(detail);

                detail = new loaner_application_detail__c();
                detail.loaner_application__c = loaner.Id;
                detail.loaner__c = In_asset[2].id;
                detail.name = '222';
                detail.RAESD_STATUS__C = '已回寄';
                In_detail.add(detail);

                insert In_detail;

                //
                PageReference page = new PageReference('/apex/LoanerTakeBackController?Id=' + loaner.Id + '&step=ReceivedLoaner');
                System.Test.setCurrentPage(page);
                LoanerTakeBackController  controller = new LoanerTakeBackController();


                controller.init();
                
                controller.dataLines[0].checkFlag = true;
                controller.dataLines[0].lad.Check_lost_Item__c = '欠品';
                controller.dataLines[0].lad.ReceiptRemark__c = '备注';
                
                controller.dataLines[1].checkFlag = true;
                controller.dataLines[1].lad.Check_lost_Item__c = 'OK';

                controller.dataLines[2].checkFlag = true;
                controller.dataLines[2].lad.Check_lost_Item__c = '消耗';

                controller.saveBtn();
                //system.assertEquals(null,ApexPages.getMessages()[0].getDetail());

                controller.cancelBtn();
                controller.getDatalineSize();

                //List<loaner_application__c> laList = [select Id,Status__c from loaner_application__c where Id = :laId];
                List<loaner_application_detail__c> updList = [select name, RAESD_Status__c from loaner_application_detail__c order by name];
                system.assertEquals('欠品中', updList[0].RAESD_Status__c);
                system.assertEquals('已回收', updList[1].RAESD_Status__c);
                system.assertEquals('已消耗', updList[2].RAESD_Status__c);


                List<loaner_application__c> laList = [select Status__c from loaner_application__c];
                system.assertEquals('欠品确认中', laList[0].Status__c);
	}
	
    @isTest static void test_method_two() {
        // Implement test code
        setupTestData();
        // Implement test code
        List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'ANI'];

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer ANI'];

        // 新建备品借出申请
        loaner_application__c loaner = new loaner_application__c();
        loaner.Name = 'TEST';
        loaner.RecordTypeId = rectLo[0].id;
        loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE2';
        loaner.DEMO_PURPOSE__C = '培训';
        loaner.Request_shipping_Date__c = Date.today();
        loaner.Request_return_Date__c = Date.today();
        loaner.Status__c = '已出库指示';
        loaner.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner;

        loaner_application__c loaner2 = new loaner_application__c();
        loaner2.Name = 'TEST';
        loaner2.RecordTypeId = rectLo[0].id;
        loaner2.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE2';
        loaner2.DEMO_PURPOSE__C = '培训';
        loaner2.Request_shipping_Date__c = Date.today();
        loaner2.Request_return_Date__c = Date.today();
        loaner2.Status__c = '已出库指示';
        loaner2.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner2;

        loaner_application__c loaner3 = new loaner_application__c();
        loaner3.Name = 'TEST';
        loaner3.RecordTypeId = rectLo[0].id;
        loaner3.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE2';
        loaner3.DEMO_PURPOSE__C = '培训';
        loaner3.Request_shipping_Date__c = Date.today();
        loaner3.Request_return_Date__c = Date.today();
        loaner3.Status__c = '已出库指示';
        loaner3.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner3;

        //代理商用户
        List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Profile prof = [select Id from Profile where Name = 'SSBG DEALER ANI'];
        Account dealer = new Account();
        dealer.Name = 'test dealer';
        dealer.RecordTypeId = rectDealer[0].Id;
        dealer.ProductSegment__c = 'BS';
        dealer.PostCode__c = '000000';
        insert dealer;
        Contact core = new Contact(email = 'jplumber@salesforce.com', firstname = 'Joe', lastname = 'Plumber', accountid = dealer.id);
        insert core;
        User testUser = New User(ContactId = core.id,ViolationsID__c = loaner.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 = 'testUser01@prectech.com');
        insert testUser;

        User_Violations__c uv = new User_Violations__c();
        uv.Is_locking__c = true;
        uv.Is_effective__c = true;
        uv.Locking_Start_Date__c = Date.today();
        uv.Violations_User__c = testUser.Id;
        uv.Violations_Loaner__c = loaner.id;
        uv.Violations_reason__c = '超期未归还';
        insert uv;

        User_Violations__c uv2 = new User_Violations__c();
        uv2.Is_locking__c = true;
        uv2.Is_effective__c = true;
        uv2.Locking_Start_Date__c = Date.today();
        uv2.Violations_User__c = testUser.Id;
        uv2.Violations_Loaner__c = loaner2.id;
        uv2.Violations_reason__c = '系统自动收货';
        insert uv2;

        User_Violations__c uv3 = new User_Violations__c();
        uv3.Is_locking__c = true;
        uv3.Is_effective__c = true;
        uv3.Locking_Start_Date__c = Date.today();
        uv3.Violations_User__c = testUser.Id;
        uv3.Violations_Loaner__c = loaner3.id;
        uv3.Violations_reason__c = '系统自动收货';
        insert uv3;

        // 新建 客户
        Account acc = new Account();
        acc.RecordTypeId = rectCo[0].Id;
        acc.Name         = '客户テスト1';
        acc.DivisionName__c = 'DivisionName__c';
        acc.FacilityName__c = 'FacilityName__c';
        acc.PostCode__c = '000000';
        insert acc;

        // 新建 资产
        List<Asset> In_asset = new List<Asset>();

        Asset asset = new Asset();
        asset.Name = 'テスト機器1';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器2';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器3';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        insert In_asset;

        // 新建 借出明细
        List<loaner_application_detail__c> In_detail = new List<loaner_application_detail__c>();
        loaner_application_detail__c detail = new loaner_application_detail__c();

        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[0].id;
        detail.name = '000';
        detail.RAESD_STATUS__C = '已回寄';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[1].id;
        detail.name = '111';
        detail.RAESD_STATUS__C = '已回寄';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[2].id;
        detail.name = '222';
        detail.RAESD_STATUS__C = '已回寄';
        In_detail.add(detail);

        insert In_detail;

        //
        PageReference page = new PageReference('/apex/LoanerTakeBackController?Id=' + loaner.Id + '&step=ReceivedLoaner');
        System.Test.setCurrentPage(page);
        LoanerTakeBackController  controller = new LoanerTakeBackController();


        controller.init();

        controller.dataLines[0].checkFlag = true;
        controller.dataLines[0].lad.Check_lost_Item__c = 'OK';
        //controller.dataLines[0].lad.ReceiptRemark__c = '备注';

        controller.dataLines[1].checkFlag = true;
        controller.dataLines[1].lad.Check_lost_Item__c = 'OK';

        controller.dataLines[2].checkFlag = true;
        controller.dataLines[2].lad.Check_lost_Item__c = 'OK';

        controller.saveBtn();
        //system.assertEquals(null,ApexPages.getMessages()[0].getDetail());

        controller.cancelBtn();
        controller.getDatalineSize();

        //List<loaner_application__c> laList = [select Id,Status__c from loaner_application__c where Id = :laId];
        List<loaner_application_detail__c> updList = [select name, RAESD_Status__c from loaner_application_detail__c order by name];
        //system.assertEquals('欠品中', updList[0].RAESD_Status__c);
        //system.assertEquals('已回收', updList[1].RAESD_Status__c);
        //system.assertEquals('已消耗', updList[2].RAESD_Status__c);


        List<loaner_application__c> laList = [select Status__c from loaner_application__c];
        //system.assertEquals('全部回收', laList[0].Status__c);
    }

    @isTest static void test_method_three() {
        // Implement test code
        setupTestData();
        // Implement test code
        List<RecordType> rectLo = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'ANI'];

        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Customer ANI'];

        // 新建备品借出申请
        loaner_application__c loaner = new loaner_application__c();
        loaner.Name = 'TEST';
        loaner.RecordTypeId = rectLo[0].id;
        loaner.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE2';
        loaner.DEMO_PURPOSE__C = '培训';
        loaner.Request_shipping_Date__c = Date.today();
        loaner.Request_return_Date__c = Date.today();
        loaner.Status__c = '已出库指示';
        loaner.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner;

        loaner_application__c loaner2 = new loaner_application__c();
        loaner2.Name = 'TEST';
        loaner2.RecordTypeId = rectLo[0].id;
        loaner2.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE2';
        loaner2.DEMO_PURPOSE__C = '培训';
        loaner2.Request_shipping_Date__c = Date.today();
        loaner2.Request_return_Date__c = Date.today();
        loaner2.Status__c = '已出库指示';
        loaner2.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner2;

        loaner_application__c loaner3 = new loaner_application__c();
        loaner3.Name = 'TEST';
        loaner3.RecordTypeId = rectLo[0].id;
        loaner3.EC_CODE_1__C = 'DV2-CAMELEONS-CUBE2';
        loaner3.DEMO_PURPOSE__C = '培训';
        loaner3.Request_shipping_Date__c = Date.today();
        loaner3.Request_return_Date__c = Date.today();
        loaner3.Status__c = '已出库指示';
        loaner3.Return_Track_Company__c = '物流公司';
        //loaner.Return_Trake_Staff__c = '返品人';
        insert loaner3;

        //代理商用户
        List<RecordType> rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer'];
        Profile prof = [select Id from Profile where Name = 'SSBG DEALER ANI'];
        Account dealer = new Account();
        dealer.Name = 'test dealer';
        dealer.RecordTypeId = rectDealer[0].Id;
        dealer.ProductSegment__c = 'BS';
        dealer.PostCode__c = '000000';
        insert dealer;
        Contact core = new Contact(email = 'jplumber@salesforce.com', firstname = 'Joe', lastname = 'Plumber', accountid = dealer.id);
        insert core;
        User testUser = New User(ContactId = core.id,ViolationsID__c = loaner.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 = 'testUser01@prectech.com');
        insert testUser;

        User_Violations__c uv = new User_Violations__c();
        uv.Is_locking__c = true;
        uv.Is_effective__c = true;
        uv.Locking_Start_Date__c = Date.today();
        uv.Violations_User__c = testUser.Id;
        uv.Violations_Loaner__c = loaner.id;
        uv.Violations_reason__c = '超期未归还';
        insert uv;

        //User_Violations__c uv2 = new User_Violations__c();
        //uv2.Is_locking__c = true;
        //uv2.Is_effective__c = true;
        //uv2.Locking_Start_Date__c = Date.today();
        //uv2.Violations_User__c = testUser.Id;
        //uv2.Violations_Loaner__c = loaner2.id;
        //uv2.Violations_reason__c = '系统自动收货';
        //insert uv2;

        //User_Violations__c uv3 = new User_Violations__c();
        //uv3.Is_locking__c = true;
        //uv3.Is_effective__c = true;
        //uv3.Locking_Start_Date__c = Date.today();
        //uv3.Violations_User__c = testUser.Id;
        //uv3.Violations_Loaner__c = loaner3.id;
        //uv3.Violations_reason__c = '系统自动收货';
        //insert uv3;

        // 新建 客户
        Account acc = new Account();
        acc.RecordTypeId = rectCo[0].Id;
        acc.Name         = '客户テスト1';
        acc.DivisionName__c = 'DivisionName__c';
        acc.FacilityName__c = 'FacilityName__c';
        acc.PostCode__c = '000000';
        insert acc;

        // 新建 资产
        List<Asset> In_asset = new List<Asset>();

        Asset asset = new Asset();
        asset.Name = 'テスト機器1';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器2';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        asset = new Asset();
        asset.Name = 'テスト機器3';
        asset.AccountId = acc.Id;
        asset.SerialNumber = 'testserial';
        In_asset.add(asset);

        insert In_asset;

        // 新建 借出明细
        List<loaner_application_detail__c> In_detail = new List<loaner_application_detail__c>();
        loaner_application_detail__c detail = new loaner_application_detail__c();

        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[0].id;
        detail.name = '000';
        detail.RAESD_STATUS__C = '已回寄';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[1].id;
        detail.name = '111';
        detail.RAESD_STATUS__C = '已回寄';
        In_detail.add(detail);

        detail = new loaner_application_detail__c();
        detail.loaner_application__c = loaner.Id;
        detail.loaner__c = In_asset[2].id;
        detail.name = '222';
        detail.RAESD_STATUS__C = '已回寄';
        In_detail.add(detail);

        insert In_detail;

        //
        PageReference page = new PageReference('/apex/LoanerTakeBackController?Id=' + loaner.Id + '&step=ReceivedLoaner');
        System.Test.setCurrentPage(page);
        LoanerTakeBackController  controller = new LoanerTakeBackController();


        controller.init();

        controller.dataLines[0].checkFlag = true;
        controller.dataLines[0].lad.Check_lost_Item__c = 'OK';
        //controller.dataLines[0].lad.ReceiptRemark__c = '备注';

        controller.dataLines[1].checkFlag = true;
        controller.dataLines[1].lad.Check_lost_Item__c = 'OK';

        controller.dataLines[2].checkFlag = true;
        controller.dataLines[2].lad.Check_lost_Item__c = 'OK';

        controller.saveBtn();
        //system.assertEquals(null,ApexPages.getMessages()[0].getDetail());

        controller.cancelBtn();
        controller.getDatalineSize();

        //List<loaner_application__c> laList = [select Id,Status__c from loaner_application__c where Id = :laId];
        List<loaner_application_detail__c> updList = [select name, RAESD_Status__c from loaner_application_detail__c order by name];
        //system.assertEquals('欠品中', updList[0].RAESD_Status__c);
        //system.assertEquals('已回收', updList[1].RAESD_Status__c);
        //system.assertEquals('已消耗', updList[2].RAESD_Status__c);


        List<loaner_application__c> laList = [select Status__c from loaner_application__c];
        //system.assertEquals('全部回收', laList[0].Status__c);
    }
	
}