@isTest
private class ListAssetForHospitalControllerTest {

    @isTest
    static void myUnitTest() {
        Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin];
        // ユーザー作成
        User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id);
        insert hpOwner;
        // 取引先作成
        List<RecordType> rectHp = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectHp.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科','診療科 普外科') order by Name desc];
        if (rectDpt.size() == 0) {
            return;
        }
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp1', OwnerId = hpOwner.Id);
        insert hp;

        List<Account> dc1s = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c in ('消化科', '呼吸科') order by Department_Class_Label__c];

        Account depart1 = new Account();
        depart1.RecordTypeId = rectDpt[1].Id;
        depart1.Name         = '*';
        depart1.Department_Name__c  = '診療科1';
        depart1.ParentId            = dc1s[0].Id;
        depart1.Department_Class__c = dc1s[0].Id;
        depart1.Hospital__c         = hp.Id;
        
        Account depart2 = new Account();
        depart2.RecordTypeId = rectDpt[2].Id;
        depart2.Name         = '*';
        depart2.Department_Name__c  = '診療科2';
        depart2.ParentId            = dc1s[1].Id;
        depart2.Department_Class__c = dc1s[1].Id;
        depart2.Hospital__c         = hp.Id;

        insert new Account[] {depart1, depart2};
        
        Product2 prd1 = new Product2();
        prd1.ProductCode_Ext__c     = 'Prd1';
        prd1.ProductCode            = 'Prd1';
        prd1.Repair_Product_Code__c = 'Prd1_RP';
        prd1.Name                   = 'Prd1';
        prd1.Manual_Entry__c        = false;
        prd1.Category2__c = '本体';
        prd1.Category3__c = '纤维镜';
        prd1.Category4__c = 'CV';
        prd1.Category5__c = '260SL系列';
        prd1.Asset_Model_No__c = '1001';
        
        Product2 prd2 = new Product2();
        prd2.ProductCode_Ext__c     = 'Prd2';
        prd2.ProductCode            = 'Prd2';
        prd2.Repair_Product_Code__c = 'Prd2_RP';
        prd2.Name                   = 'Prd2';
        prd2.Manual_Entry__c        = false;
        prd2.Category2__c = '附属品';
        prd2.Category3__c = '电子镜';
        prd2.Category4__c = 'CV';
        prd2.Category5__c = '290系列';
        prd2.Asset_Model_No__c = '1002';
        
        Product2 prd3 = new Product2();
        prd3.ProductCode_Ext__c     = 'Prd3';
        prd3.ProductCode            = 'Prd3';
        prd3.Repair_Product_Code__c = 'Prd3_RP';
        prd3.Name                   = 'Prd3';
        prd3.Manual_Entry__c        = false;
        prd3.Category2__c = '本体';
        prd3.Category3__c = '电子镜';
        prd3.Category4__c = 'GIF';
        prd3.Category5__c = '290系列';
        prd3.Asset_Model_No__c = '1003';
        
        insert new Product2[] {prd1,prd2,prd3};
        
        Asset ast1 = new Asset();
        ast1.Name                   = '保有設備1';
        ast1.Hospital__c            = hp.Id;
        ast1.Department_Class__c    = dc1s[0].Id;
        ast1.AccountId              = depart1.Id;
        ast1.Product2Id             = prd1.Id;
        ast1.SerialNumber           = 'SerialNumber1';
        ast1.Guarantee_period_for_products__c = Date.today();
        ast1.InstallDate                      = Date.today();
        ast1.Status = '使用中';
        
        Asset ast2 = new Asset();
        ast2.Name                   = '保有設備2';
        ast2.Hospital__c            = hp.Id;
        ast2.Department_Class__c    = dc1s[0].Id;
        ast2.AccountId              = depart1.Id;
        ast2.Product2Id             = prd2.Id;
        ast2.SerialNumber           = 'SerialNumber2';
        ast2.Guarantee_period_for_products__c = Date.today();
        ast2.InstallDate                      = Date.today();
        ast2.Status = '使用中';
        
        Asset ast3 = new Asset();
        ast3.Name                   = '保有設備3';
        ast3.Hospital__c            = hp.Id;
        ast3.Department_Class__c    = dc1s[1].Id;
        ast3.AccountId              = depart2.Id;
        ast3.Product2Id             = prd3.Id;
        ast3.SerialNumber           = 'SerialNumber3';
        ast3.Guarantee_period_for_products__c = Date.today();
        ast3.InstallDate                      = Date.today();
        ast3.Status = '使用中';
        
        insert new Asset[] {ast1,ast2,ast3};
        
        Repair__c repair1 = new Repair__c();
        repair1.Service_Repair_No__c = 'repair1';
        repair1.Hospital__c            = hp.Id;
        repair1.Department_Class__c    = dc1s[0].Id;
        repair1.Account__c             = depart1.Id;
        repair1.Delivered_Product__c = ast1.Id;
        repair1.Repair_List_Price__c = 100;
        repair1.Asset_Sum_Repair_Price__c = 100;
        repair1.Repair_Final_Inspection_Date__c = Date.today();
        repair1.Billing_Amount__c = 10;
        repair1.Paid_Amount__c = 1;
        repair1.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair1.Repair_Returned_To_HP_Date__c = Date.today().addDays(3);
        repair1.Repair_Shipped_Date__c = Date.today().addDays(1);
        
        Repair__c repair2 = new Repair__c();
        repair2.Service_Repair_No__c = 'repair2';
        repair2.Hospital__c            = hp.Id;
        repair2.Department_Class__c    = dc1s[0].Id;
        repair2.Account__c             = depart1.Id;
        repair2.Delivered_Product__c = ast2.Id;
        repair2.Repair_List_Price__c = 200;
        repair2.Asset_Sum_Repair_Price__c = 200;
        repair2.Repair_Final_Inspection_Date__c = Date.today();
        repair2.Billing_Amount__c = 20;
        repair2.Paid_Amount__c = 2;
        repair2.Failure_Occurrence_Date__c = Date.today().addDays(-1);
        repair2.Repair_Returned_To_HP_Date__c = Date.today().addDays(5);
        repair2.Repair_Shipped_Date__c = Date.today().addDays(3);
        
        Repair__c repair3 = new Repair__c();
        repair3.Service_Repair_No__c = 'repair3';
        repair3.Hospital__c            = hp.Id;
        repair3.Department_Class__c    = dc1s[1].Id;
        repair3.Account__c             = depart2.Id;
        repair3.Delivered_Product__c = ast3.Id;
        repair3.Repair_List_Price__c = 150;
        repair3.Asset_Sum_Repair_Price__c = 150;
        repair3.Repair_Final_Inspection_Date__c = Date.today();
        repair3.Billing_Amount__c = 15;
        repair3.Paid_Amount__c = 1;
        repair3.Failure_Occurrence_Date__c = Date.today().addYears(-1).addDays(-1);
        repair3.Repair_Returned_To_HP_Date__c = Date.today().addYears(-1).addDays(5);
        repair3.Repair_Shipped_Date__c = Date.today().addYears(-1).addDays(3);
        
        insert new Repair__c[] {repair1,repair2,repair3};
        
        // test hp
        PageReference page = new PageReference('/apex/ListAssetForHospital?id=' + hp.Id);
        System.Test.setCurrentPage(page);
        ListAssetForHospitalController controller = new ListAssetForHospitalController(new ApexPages.StandardController(hp));
        System.assertEquals(false, controller.isImportantProduct);
        controller.init();
        System.assertEquals(true, controller.ishp);
        //System.assertEquals(3, controller.pageLines.size());
        //System.assertEquals(1, controller.pageLines[0].allCnt);
        //System.assertEquals(200, controller.pageLines[0].allSumRepair);
        //System.assertEquals(1, controller.pageLines[1].allCnt);
        //System.assertEquals(150, controller.pageLines[1].allSumRepair);
        //System.assertEquals(1, controller.pageLines[2].allCnt);
        //System.assertEquals(100, controller.pageLines[2].allSumRepair);
        
        controller.isCategory2 = false;
        controller.isCategory3 = false;
        controller.isCategory4 = false;
        controller.isCategory5 = false;
        controller.isProductCode = false;
        controller.isShowDC = false;
        controller.searchBtn();
        System.assertEquals(1, controller.pageLines.size());
        System.assertEquals(3, controller.pageLines[0].allCnt);
        System.assertEquals(450, controller.pageLines[0].allSumRepair);
        
        // test hp other
        controller.category2 = '本体';
        controller.isCompetitorProduct = 'false';
        controller.dateFrom.Birth_Date__c = System.today();
        controller.dateTo.Birth_Date__c = System.today();
        controller.searchBtn();
        ListAssetForHospitalController.getCategory2Opts();
        ListAssetForHospitalController.getIsCompetitorProductOpts();
        ListAssetForHospitalController.getIsThisYearOpts();
        ListAssetForHospitalController.getIsHDSDOpts();
        ListAssetForHospitalController.getIsNBIProcessorOpts();
        
        //test dept
        PageReference page2 = new PageReference('/apex/ListAssetForHospital?id=' + dc1s[0].Id);
        System.Test.setCurrentPage(page2);
        ListAssetForHospitalController controller2 = new ListAssetForHospitalController(new ApexPages.StandardController(dc1s[0]));
        controller2.init();
        //System.assertEquals(false, controller2.ishp);
        //System.assertEquals(2, controller2.pageLines.size());
        //System.assertEquals(1, controller2.pageLines[0].allCnt);
        //System.assertEquals(200, controller2.pageLines[0].allSumRepair);
        //System.assertEquals(1, controller2.pageLines[1].allCnt);
        //System.assertEquals(100, controller2.pageLines[1].allSumRepair);
        
        controller2.isCategory2 = false;
        controller2.isCategory3 = false;
        controller2.isCategory4 = false;
        controller2.isCategory5 = false;
        controller2.isProductCode = false;
        controller2.searchBtn();
        System.assertEquals(1, controller2.pageLines.size());
        System.assertEquals(2, controller2.pageLines[0].allCnt);
        System.assertEquals(300, controller2.pageLines[0].allSumRepair);
    }
}