@isTest
private class NFM008WebServiceTest {

    static testMethod void resend_test() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM008TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        // 再取得
        List<Id> accountIds = new Id[] {company.Id, section.Id, depart.Id};
        List<Account> accList = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id In :accountIds order by Management_Code_Auto__c];

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'NFM008Prd1';
        prd.ProductCode            = 'NFM008Prd1';
        prd.Name                   = 'NFM008Prd1';
        prd.Manual_Entry__c        = false;
        insert prd;

        Asset ast = new Asset();
        ast.Name = 'NFM008Ast1';
        ast.Product2Id = prd.Id;
        ast.SerialNumber = '2577010001675';
        ast.Hospital__c = accList[0].Id;
        ast.Department_Class__c = accList[1].Id;
        ast.AccountId = accList[2].Id;
        insert ast;

        User loginUser = [Select Id, Alias, Province__c from User where Id =: UserInfo.getUserId()];
        loginUser.Batch_User__c = true;
        update loginUser;
        
        NFM008WebService.InstallationInfo InstallationInfo = new NFM008WebService.InstallationInfo();
        NFM008WebService.InstallationInformation InstallationInformation = new NFM008WebService.InstallationInformation();
        NFM008WebService.InstallationInfoH InstallationInfoH = new NFM008WebService.InstallationInfoH();
        InstallationInfo.InstallationInfoH = InstallationInfoH;
        NFM008WebService.InstallationInfoI InstallationInfoI = new NFM008WebService.InstallationInfoI();
        InstallationInfo.InstallationInfoI = new NFM008WebService.InstallationInfoI[] { InstallationInfoI };
        InstallationInformation.InstallationInfo = new NFM008WebService.InstallationInfo[] { InstallationInfo };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        InstallationInformation.Monitoring = new NFMUtil.Monitoring();
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '21';

        // update
        InstallationInfoH.PostingDate       = '20120112';
        InstallationInfoH.CompanyCode       = accList[0].Management_Code_Auto__c;
        InstallationInfoH.DepartmentCode    = accList[2].Management_Code_Auto__c;
        InstallationInfoI.MaterialNo        = 'NFM008Prd1';
        InstallationInfoI.SerialNo          = '2577010001675';
        InstallationInfoI.InstallationDateI = '20120118';
        InstallationInfoI.SLMark            = 'L';
        InstallationInfoI.GuaranteePeriod   = '20131021';
        InstallationInfoI.Location          = '病房';
        InstallationInfoI.ReturnExchangeReplenishMark = '3';
        InstallationInfoI.PurposeOfAdvice   = '3';

        System.Test.startTest();
        NFM008WebService.NFM008(InstallationInformation);
        System.Test.stopTest();
        String productSerialNo = InstallationInfoI.MaterialNo + ':' + InstallationInfoI.SerialNo;
        List<Asset> astList = [select Id, Product2.ProductCode, SerialNumber, Product_Serial_No__c,
                                      Product2Id, AccountId, Department_Class__c, Hospital__c,
                                      Posting_Date__c, InstallDate, SLMark__c,
                                      Guarantee_period_for_products__c, Installation_Site__c
                                 from Asset
                                where Product_Serial_No__c = :productSerialNo and Information_From__c <> '失单'];
        System.assertEquals(1, astList.size());
        if (astList.size() == 1) {
            //System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoH.PostingDate), astList[0].Posting_Date__c);
            //System.assertEquals(accList[2].Id, astList[0].AccountId);
            //System.assertEquals(accList[1].Id, astList[0].Department_Class__c);
            //System.assertEquals(accList[0].Id, astList[0].Hospital__c);
            //System.assertEquals(InstallationInfoI.MaterialNo, astList[0].Product2.ProductCode);
            //System.assertEquals(InstallationInfoI.SerialNo, astList[0].SerialNumber);
            //System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoI.InstallationDateI), astList[0].InstallDate);
            //System.assertEquals(InstallationInfoI.SLMark, astList[0].SLMark__c);
            //System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoI.GuaranteePeriod), astList[0].Guarantee_period_for_products__c);
            //System.assertEquals(InstallationInfoI.Location, astList[0].Installation_Site__c);
        }

        List<BatchIF_Log__c> rowbl = [Select Id, Log__c,
                                            ErrorLog__c
                                        from BatchIF_Log__c
                                        where RowDataFlg__c = true
                                        and Type__c = 'NFM008'
                                        order by CreatedDate desc];
        System.assertEquals(1, rowbl.size());
        rowbl[0].retry_cnt__c = 1;
        update rowbl;

        NFM008WebService.execute(rowbl[0].id);

        List<BatchIF_Log__c> bl = [Select Id, Is_Error__c,
                Type__c, Log__c, ErrorLog__c,retry_cnt__c
                from BatchIF_Log__c
                where Id = :rowbl[0].id];
        //System.assertEquals(1, bl.size());
        //System.assertEquals(0, bl[0].retry_cnt__c);

    }
    
    static testMethod void testNFM008_void() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ

        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM008TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        // 再取得
        List<Id> accountIds = new Id[] {company.Id, section.Id, depart.Id};
        List<Account> accList = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id In :accountIds order by Management_Code_Auto__c];

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'NFM008Prd1';
        prd.ProductCode            = 'NFM008Prd1';
        prd.Name                   = 'NFM008Prd1';
        prd.Manual_Entry__c        = false;
        insert prd;
        // null
        NFM008WebService.NFM008(null);

        NFM008WebService.InstallationInfo InstallationInfo = new NFM008WebService.InstallationInfo();
        NFM008WebService.InstallationInformation InstallationInformation = new NFM008WebService.InstallationInformation();
        InstallationInformation.InstallationInfo = new NFM008WebService.InstallationInfo[] { InstallationInfo };
        
        // Monitoringをセットしない
        NFM008WebService.NFM008(InstallationInformation);
        
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        InstallationInformation.Monitoring = new NFMUtil.Monitoring();
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '01';
        // InstallationInfoHをセットしない
        NFM008WebService.NFM008(InstallationInformation);

        NFM008WebService.InstallationInfoH InstallationInfoH = new NFM008WebService.InstallationInfoH();
        InstallationInfo.InstallationInfoH = InstallationInfoH;
        // InstallationInfoIをセットしない
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '02';
        NFM008WebService.NFM008(InstallationInformation);

        NFM008WebService.InstallationInfoI InstallationInfoI = new NFM008WebService.InstallationInfoI();
        InstallationInfo.InstallationInfoI = new NFM008WebService.InstallationInfoI[] { InstallationInfoI };
        // 病院と診療科の管理コードをセットしない
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '03';
        NFM008WebService.NFM008(InstallationInformation);

        InstallationInfoH.CompanyCode       = 'NFM008TestABCD';
        InstallationInfoH.DepartmentCode    = 'NFM008TestDCBA';
        // 病院と診療科の管理コードが存在しない
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '04';
        NFM008WebService.NFM008(InstallationInformation);
        InstallationInfoH.CompanyCode       = accList[0].Management_Code_Auto__c;
        // 診療科の管理コードが存在しない
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '05';
        NFM008WebService.NFM008(InstallationInformation);

        InstallationInfoH.CompanyCode       = accList[0].Management_Code_Auto__c;
        InstallationInfoH.DepartmentCode    = accList[2].Management_Code_Auto__c;
        // InstallationInfoIの必須項目をセットしない
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '06';
        NFM008WebService.NFM008(InstallationInformation);

        InstallationInfoI.MaterialNo        = 'NFM008Prd1';
        // 必須項目をセットしない
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '08';
        NFM008WebService.NFM008(InstallationInformation);

        InstallationInfoI.MaterialNo        = 'NFM008TstPrd1234';
        InstallationInfoI.SerialNo          = '2577010001675';
        // 商品コードが存在しない
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '07';
        NFM008WebService.NFM008(InstallationInformation);

        Asset[] rslts = [select Id from Asset];
        System.assertEquals(0, rslts.size());
    }

    static testMethod void testNFM008_insert() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM008TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        // 再取得
        List<Id> accountIds = new Id[] {company.Id, section.Id, depart.Id};
        List<Account> accList = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id In :accountIds order by Management_Code_Auto__c];

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'NFM008Prd1';
        prd.ProductCode            = 'NFM008Prd1';
        prd.Name                   = 'NFM008Prd1';
        prd.Manual_Entry__c        = false;
        insert prd;

        Asset ast = new Asset();
        ast.Name = 'NFM008Ast1';
        ast.Product2Id = prd.Id;
        ast.SerialNumber = '2577010001675';
        ast.Hospital__c = accList[0].Id;
        ast.Department_Class__c = accList[1].Id;
        ast.AccountId = accList[2].Id;
        insert ast;

        Asset ast2 = new Asset();
        ast2.Name = 'NFM008Ast1';
        ast2.Product2Id = prd.Id;
        ast2.SerialNumber = '2577010001676';
        ast2.Hospital__c = accList[0].Id;
        ast2.Department_Class__c = accList[1].Id;
        ast2.AccountId = accList[2].Id;
        insert ast2;

        NFM008WebService.InstallationInfo InstallationInfo = new NFM008WebService.InstallationInfo();
        NFM008WebService.InstallationInformation InstallationInformation = new NFM008WebService.InstallationInformation();
        NFM008WebService.InstallationInfoH InstallationInfoH = new NFM008WebService.InstallationInfoH();
        InstallationInfo.InstallationInfoH = InstallationInfoH;
        NFM008WebService.InstallationInfoI InstallationInfoI1 = new NFM008WebService.InstallationInfoI();
        NFM008WebService.InstallationInfoI InstallationInfoI2 = new NFM008WebService.InstallationInfoI();
        InstallationInfo.InstallationInfoI = new NFM008WebService.InstallationInfoI[] { InstallationInfoI1,  InstallationInfoI2};
        InstallationInformation.InstallationInfo = new NFM008WebService.InstallationInfo[] { InstallationInfo };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        InstallationInformation.Monitoring = new NFMUtil.Monitoring();
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '11';

        // insert
        InstallationInfoH.PostingDate       = '20110112';
        InstallationInfoH.CompanyCode       = '0' + accList[0].Management_Code_Auto__c;
        InstallationInfoH.DepartmentCode    = '0' + accList[2].Management_Code_Auto__c;
        InstallationInfoH.PurposeOfAdvice   = '2';
        InstallationInfoI1.MaterialNo        = 'NFM008Prd1';
        InstallationInfoI1.SerialNo          = '2577010001675';
        InstallationInfoI1.InstallationDateI = '20110118';
        InstallationInfoI1.SLMark            = 'S';
        InstallationInfoI1.GuaranteePeriod   = '20121021';
        InstallationInfoI1.Location          = '门诊';
        InstallationInfoI1.ReturnExchangeReplenishMark = '2';
        InstallationInfoI1.PurposeOfAdvice   = '2';
        InstallationInfoI2.MaterialNo        = 'NFM008Prd1';
        InstallationInfoI2.SerialNo          = '2577010001676';
        InstallationInfoI2.InstallationDateI = '20110118';
        InstallationInfoI2.SLMark            = 'S';
        InstallationInfoI2.GuaranteePeriod   = '20121021';
        InstallationInfoI2.Location          = '门诊';
        InstallationInfoI2.ReturnExchangeReplenishMark = '2';
        InstallationInfoI2.PurposeOfAdvice   = '2';

        System.Test.startTest();
        NFM008WebService.NFM008(InstallationInformation);
        System.Test.stopTest();

        String productSerialNo = InstallationInfoI1.MaterialNo + ':' + InstallationInfoI1.SerialNo;
        List<Asset> astList = [select Id, Product2.ProductCode, SerialNumber, Product_Serial_No__c,
                                      Product2Id, AccountId, Department_Class__c, Hospital__c,
                                      Posting_Date__c, InstallDate, SLMark__c,
                                      Guarantee_period_for_products__c, Installation_Site__c
                                 from Asset
                                where Product_Serial_No__c = :productSerialNo and Information_From__c <> '失单'];
        System.assertEquals(1, astList.size());
        //if (astList.size() == 1) {
        //    System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoH.PostingDate), astList[0].Posting_Date__c);
        //    System.assertEquals(accList[2].Id, astList[0].AccountId);
        //    System.assertEquals(accList[1].Id, astList[0].Department_Class__c);
        //    System.assertEquals(accList[0].Id, astList[0].Hospital__c);
        //    System.assertEquals(InstallationInfoI1.MaterialNo, astList[0].Product2.ProductCode);
        //    System.assertEquals(InstallationInfoI1.SerialNo, astList[0].SerialNumber);
        //    System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoI1.InstallationDateI), astList[0].InstallDate);
        //    System.assertEquals(InstallationInfoI1.SLMark, astList[0].SLMark__c);
        //    System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoI1.GuaranteePeriod), astList[0].Guarantee_period_for_products__c);
        //    System.assertEquals(InstallationInfoI1.Location, astList[0].Installation_Site__c);
        //}
        productSerialNo = InstallationInfoI2.MaterialNo + ':' + InstallationInfoI2.SerialNo;
        astList = [select Id, Product2.ProductCode, SerialNumber, Product_Serial_No__c,
                                      Product2Id, AccountId, Department_Class__c, Hospital__c,
                                      Posting_Date__c, InstallDate, SLMark__c,
                                      Guarantee_period_for_products__c, Installation_Site__c
                                 from Asset
                                where Product_Serial_No__c = :productSerialNo and Information_From__c <> '失单'];
        System.assertEquals(1, astList.size());
    }
    
    static testMethod void testNFM008_update() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM008TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        // 再取得
        List<Id> accountIds = new Id[] {company.Id, section.Id, depart.Id};
        List<Account> accList = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id In :accountIds order by Management_Code_Auto__c];

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'NFM008Prd1';
        prd.ProductCode            = 'NFM008Prd1';
        prd.Name                   = 'NFM008Prd1';
        prd.Manual_Entry__c        = false;
        insert prd;

        Asset ast = new Asset();
        ast.Name = 'NFM008Ast1';
        ast.Product2Id = prd.Id;
        ast.SerialNumber = '2577010001675';
        ast.Hospital__c = accList[0].Id;
        ast.Department_Class__c = accList[1].Id;
        ast.AccountId = accList[2].Id;
        insert ast;

        User loginUser = [Select Id, Alias, Province__c from User where Id =: UserInfo.getUserId()];
        loginUser.Batch_User__c = true;
        update loginUser;
        
        NFM008WebService.InstallationInfo InstallationInfo = new NFM008WebService.InstallationInfo();
        NFM008WebService.InstallationInformation InstallationInformation = new NFM008WebService.InstallationInformation();
        NFM008WebService.InstallationInfoH InstallationInfoH = new NFM008WebService.InstallationInfoH();
        InstallationInfo.InstallationInfoH = InstallationInfoH;
        NFM008WebService.InstallationInfoI InstallationInfoI = new NFM008WebService.InstallationInfoI();
        InstallationInfo.InstallationInfoI = new NFM008WebService.InstallationInfoI[] { InstallationInfoI };
        InstallationInformation.InstallationInfo = new NFM008WebService.InstallationInfo[] { InstallationInfo };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        InstallationInformation.Monitoring = new NFMUtil.Monitoring();
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '21';

        // update
        InstallationInfoH.PostingDate       = '20120112';
        InstallationInfoH.CompanyCode       = accList[0].Management_Code_Auto__c;
        InstallationInfoH.DepartmentCode    = accList[2].Management_Code_Auto__c;
        InstallationInfoI.MaterialNo        = 'NFM008Prd1';
        InstallationInfoI.SerialNo          = '2577010001675';
        InstallationInfoI.InstallationDateI = '20120118';
        InstallationInfoI.SLMark            = 'L';
        InstallationInfoI.GuaranteePeriod   = '20131021';
        InstallationInfoI.Location          = '病房';
        InstallationInfoI.ReturnExchangeReplenishMark = '3';
        InstallationInfoI.PurposeOfAdvice   = '3';

        System.Test.startTest();
        NFM008WebService.NFM008(InstallationInformation);
        System.Test.stopTest();
        String productSerialNo = InstallationInfoI.MaterialNo + ':' + InstallationInfoI.SerialNo;
        List<Asset> astList = [select Id, Product2.ProductCode, SerialNumber, Product_Serial_No__c,
                                      Product2Id, AccountId, Department_Class__c, Hospital__c,
                                      Posting_Date__c, InstallDate, SLMark__c,
                                      Guarantee_period_for_products__c, Installation_Site__c
                                 from Asset
                                where Product_Serial_No__c = :productSerialNo and Information_From__c <> '失单'];
        System.assertEquals(1, astList.size());
        if (astList.size() == 1) {
            //System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoH.PostingDate), astList[0].Posting_Date__c);
            //System.assertEquals(accList[2].Id, astList[0].AccountId);
            //System.assertEquals(accList[1].Id, astList[0].Department_Class__c);
            //System.assertEquals(accList[0].Id, astList[0].Hospital__c);
            //System.assertEquals(InstallationInfoI.MaterialNo, astList[0].Product2.ProductCode);
            //System.assertEquals(InstallationInfoI.SerialNo, astList[0].SerialNumber);
            //System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoI.InstallationDateI), astList[0].InstallDate);
            //System.assertEquals(InstallationInfoI.SLMark, astList[0].SLMark__c);
            //System.assertEquals(NFMUtil.parseStr2Date(InstallationInfoI.GuaranteePeriod), astList[0].Guarantee_period_for_products__c);
            //System.assertEquals(InstallationInfoI.Location, astList[0].Installation_Site__c);
        }
    }

    static testMethod void testNFM008_delete() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ
        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM008TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        // 再取得
        List<Id> accountIds = new Id[] {company.Id, section.Id, depart.Id};
        List<Account> accList = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id In :accountIds order by Management_Code_Auto__c];

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'NFM008Prd1';
        prd.ProductCode            = 'NFM008Prd1';
        prd.Name                   = 'NFM008Prd1';
        prd.Manual_Entry__c        = false;
        insert prd;
        User loginUser = [Select Id, Alias, Province__c from User where Id =: UserInfo.getUserId()];
        loginUser.Batch_User__c = true;
        update loginUser;
        NFM008WebService.InstallationInfo InstallationInfo = new NFM008WebService.InstallationInfo();
        NFM008WebService.InstallationInformation InstallationInformation = new NFM008WebService.InstallationInformation();
        NFM008WebService.InstallationInfoH InstallationInfoH = new NFM008WebService.InstallationInfoH();
        InstallationInfo.InstallationInfoH = InstallationInfoH;
        NFM008WebService.InstallationInfoI InstallationInfoI = new NFM008WebService.InstallationInfoI();
        InstallationInfo.InstallationInfoI = new NFM008WebService.InstallationInfoI[] { InstallationInfoI };
        InstallationInformation.InstallationInfo = new NFM008WebService.InstallationInfo[] { InstallationInfo };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        InstallationInformation.Monitoring = new NFMUtil.Monitoring();
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '31';

        // InstallationInfoI.PurposeOfAdvice = 1
        InstallationInfoH.PostingDate       = '20120112';
        InstallationInfoH.CompanyCode       = accList[0].Management_Code_Auto__c;
        InstallationInfoH.DepartmentCode    = accList[2].Management_Code_Auto__c;
        InstallationInfoH.PurposeOfAdvice   = '3';
        InstallationInfoI.MaterialNo        = 'NFM008Prd1';
        InstallationInfoI.SerialNo          = '2577010001675';
        InstallationInfoI.InstallationDateI = '20120118';
        InstallationInfoI.SLMark            = 'L';
        InstallationInfoI.GuaranteePeriod   = '20131021';
        InstallationInfoI.Location          = '病房';
        InstallationInfoI.ReturnExchangeReplenishMark = '3';
        InstallationInfoI.PurposeOfAdvice   = '1';
        
        //List<Asset> astList = [select Id, Return_Flag__c from Asset where Product_Serial_No__c = :productSerialNo and Information_From__c <> '失单'];
        //System.assertEquals(1, astList.size());
        //System.assertEquals(false, astList[0].Return_Flag__c);

        System.Test.startTest();
        NFM008WebService.NFM008(InstallationInformation);
        System.Test.stopTest();
        String productSerialNo = InstallationInfoI.MaterialNo + ':' + InstallationInfoI.SerialNo;
        List<Asset> astList = [select Id, Return_Flag__c from Asset where Product_Serial_No__c = :productSerialNo and Information_From__c <> '失单'];
        //System.assertEquals(1, astList.size());
        //System.assertEquals(true, astList[0].Return_Flag__c);
    }

    static testMethod void testNFM008_return() {
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科'];
        if (rectSct.size() == 0) {
            return;
        }
        List<RecordType> rectDpt = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
        if (rectDpt.size() == 0) {
            return;
        }

        // テストデータ


        Account company = new Account();
        company.RecordTypeId = rectCo[0].Id;
        company.Name         = 'NFM008TestCompany';
        upsert company;
        Account section = new Account();
        section.RecordTypeId = rectSct[0].Id;
        section.Name         = '*';
        section.Department_Class_Label__c = '消化科';
        section.ParentId                  = company.Id;
        section.Hospital_Department_Class__c = company.Id;
        upsert section;
        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = 'NFM008TestDepart';
        depart.ParentId            = section.Id;
        depart.Department_Class__c = section.Id;
        depart.Hospital__c         = company.Id;
        upsert depart;
        // 再取得
        List<Id> accountIds = new Id[] {company.Id, section.Id, depart.Id};
        List<Account> accList = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id In :accountIds order by Management_Code_Auto__c];

        Product2 prd = new Product2();
        prd.ProductCode_Ext__c     = 'NFM008Prd1';
        prd.ProductCode            = 'NFM008Prd1';
        prd.Name                   = 'NFM008Prd1';
        prd.Manual_Entry__c        = false;
        insert prd;
        NFM008WebService.InstallationInfo InstallationInfo = new NFM008WebService.InstallationInfo();
        NFM008WebService.InstallationInformation InstallationInformation = new NFM008WebService.InstallationInformation();
        NFM008WebService.InstallationInfoH InstallationInfoH = new NFM008WebService.InstallationInfoH();
        InstallationInfo.InstallationInfoH = InstallationInfoH;
        NFM008WebService.InstallationInfoI InstallationInfoI = new NFM008WebService.InstallationInfoI();
        InstallationInfo.InstallationInfoI = new NFM008WebService.InstallationInfoI[] { InstallationInfoI };
        InstallationInformation.InstallationInfo = new NFM008WebService.InstallationInfo[] { InstallationInfo };

        // Monitoringの設定
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        InstallationInformation.Monitoring = new NFMUtil.Monitoring();
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '41';

        // InstallationInfoI.ReturnExchangeReplenishMark = 1 にてdelete
        InstallationInfoH.PostingDate       = '20120112';
        InstallationInfoH.CompanyCode       = accList[0].Management_Code_Auto__c;
        InstallationInfoH.DepartmentCode    = accList[2].Management_Code_Auto__c;
        InstallationInfoH.PurposeOfAdvice   = '3';
        InstallationInfoI.MaterialNo        = 'NFM008Prd1';
        InstallationInfoI.SerialNo          = '2577010001676';
        InstallationInfoI.InstallationDateI = '20120118';
        InstallationInfoI.SLMark            = 'L';
        InstallationInfoI.GuaranteePeriod   = '20131021';
        InstallationInfoI.Location          = '病房';
        InstallationInfoI.ReturnExchangeReplenishMark   = '1';
        InstallationInfoH.PurposeOfAdvice   = '3';
        String productSerialNo = InstallationInfoI.MaterialNo + ':' + InstallationInfoI.SerialNo;
        //List<Asset> astList = [select Id, Return_Flag__c from Asset where Product_Serial_No__c = :productSerialNo];
        //System.assertEquals(1, astList.size());
        //System.assertEquals(false, astList[0].Return_Flag__c);

        System.Test.startTest();
        NFM008WebService.NFM008(InstallationInformation);
        System.Test.stopTest();
        List<Asset> astList = [select Id, Return_Flag__c from Asset where Product_Serial_No__c = :productSerialNo];
        //System.assertEquals(1, astList.size());
        //System.assertEquals(true, astList[0].Return_Flag__c);

        // 再度 InstallationInfoI.ReturnExchangeReplenishMark = 1 にてdelete
        InstallationInformation.Monitoring.MessageGroupNumber = nowStr + '42';
        NFM008WebService.NFM008(InstallationInformation);
        astList = [select Id, Return_Flag__c from Asset where Product_Serial_No__c = :productSerialNo];
        //System.assertEquals(1, astList.size());
        //System.assertEquals(true, astList[0].Return_Flag__c);
    }
}