/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 * Unit tests are class methods that verify whether a particular piece
 * of code is working properly. Unit test methods take no arguments,
 * commit no data to the database, and are flagged with the testMethod
 * keyword in the method definition.
 *
 * All test methods in an organization are executed whenever Apex code is deployed
 * to a production organization to confirm correctness, ensure code
 * coverage, and prevent regressions. All Apex classes are
 * required to have at least 75% code coverage in order to be deployed
 * to a production organization. In addition, all triggers must have some code coverage.
 *
 * The @isTest class annotation indicates this class only contains test
 * methods. Classes defined with the @isTest annotation do not count against
 * the organization size limit for all Apex scripts.
 *
 * See the Apex Language Reference for more information about Testing and Code Coverage.
 */
@isTest
private class SyncMBStatuAchievementsTriggerTest {

    // MB_注残
    static testMethod void SyncMBStatuAchievementsTest() {
        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 = '病院'];
        List<RecordType> rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科'];
        Account hp = new Account(RecordTypeId = rectHp[0].Id, Name = 'SoakupTestHp', OwnerId = hpOwner.Id);
        insert hp;
        List<Account> dcs = [Select Id, Name, Department_Class_Label__c from Account where Parent.Id = :hp.Id and Department_Class_Label__c = '呼吸科'];

        Account depart = new Account();
        depart.RecordTypeId = rectDpt[0].Id;
        depart.Name         = '*';
        depart.Department_Name__c  = '診療科';
        depart.ParentId            = dcs[0].Id;
        depart.Department_Class__c = dcs[0].Id;
        depart.Hospital__c         = hp.Id;
        insert depart;

        RecordType rectOpp = [select id from RecordType where IsActive = true and SobjectType = 'Opportunity' and DeveloperName = 'Opportunity'];
        Opportunity opp = new Opportunity(
            Name = 'test opp',
            StageName = '引合',
            CurrencyIsoCode = 'CNY',
            CloseDate = Date.today(),
            AccountId = depart.Id,
            RecordTypeId = rectOpp.Id,
            Closing_Bid_Date__c = Date.today().addDays(-5),
            Hospital__c = hp.Id,
            Competitor__c = 'A'
        );

        List<MB_Statu_Achievements__c> mblist = [select Id from MB_Statu_Achievements__c];
        System.assertEquals(0, mblist.size());

        insert opp;
        Statu_Achievements__c sta1 = new Statu_Achievements__c();
        sta1.Statu_Achievements_Ext__c = 'opp:SoNo1';
        sta1.Name                      = 'SoNo1';
        sta1.Opportunity__c            = opp.Id;
        sta1.ReturnFlag__c             = false;
        sta1.OverviewStatus__c         = 'OverviewStatus__c';
        sta1.ContractNO__c             = 'ContractNO1';
        sta1.ContractReceivedDate__c   = null;
        sta1.FirstApproveDate__c       = null;
        sta1.LastApproveDate__c        = null;
        sta1.SoLatestDeliveryDate__c   = null;
        sta1.DeliveryDate__c           = null;
        sta1.InstallDate__c            = null;
        sta1.Collection_Day_Invoice__c = System.today();
        sta1.CurrencyIsoCode           = opp.CurrencyIsoCode;
        sta1.ContractAmount__c         = 0;
        sta1.PaymentAmount__c          = 0;
        sta1.PaymentRate__c            = '0';
        sta1.DeliveryStatus__c         = 'DeliveryStatus__c';
        sta1.AssignmentStatus__c       = 'AssignmentStatus__c';
        sta1.PaymentStatus__c          = 'PaymentStatus__c';
        sta1.InstallStatus__c          = 'InstallStatus__c';
        sta1.AuthorizationNo__c        = 'AuthorizationNo__c';
        sta1.ContractApplicationNo__c  = 'ContractApplicationNo__c';
        sta1.PaymentTerms__c           = 'PaymentTerms__c';
        sta1.UnshippedAmount__c        = 0;
        sta1.SalesChannel__c           = 'SalesChannel__c';
        sta1.SoLatestDeliveryDate__c   = Date.today().addDays(5);
        Statu_Achievements__c sta2 = new Statu_Achievements__c();
        sta2.Statu_Achievements_Ext__c = 'opp:SoNo2';
        sta2.Name                      = 'SoNo2';
        sta2.Opportunity__c            = opp.Id;
        sta2.ReturnFlag__c             = false;
        sta2.OverviewStatus__c         = 'OverviewStatus__c';
        sta2.ContractNO__c             = 'ContractNO2';
        sta2.ContractReceivedDate__c   = null;
        sta2.FirstApproveDate__c       = null;
        sta2.LastApproveDate__c        = null;
        sta2.SoLatestDeliveryDate__c   = null;
        sta2.DeliveryDate__c           = null;
        sta2.InstallDate__c            = null;
        sta2.Collection_Day_Invoice__c = System.today();
        sta2.CurrencyIsoCode           = opp.CurrencyIsoCode;
        sta2.ContractAmount__c         = 0;
        sta2.PaymentAmount__c          = 0;
        sta2.PaymentRate__c            = '0';
        sta2.DeliveryStatus__c         = 'DeliveryStatus__c';
        sta2.AssignmentStatus__c       = 'AssignmentStatus__c';
        sta2.PaymentStatus__c          = 'PaymentStatus__c';
        sta2.InstallStatus__c          = 'InstallStatus__c';
        sta2.AuthorizationNo__c        = 'AuthorizationNo__c';
        sta2.ContractApplicationNo__c  = 'ContractApplicationNo__c';
        sta2.PaymentTerms__c           = 'PaymentTerms__c';
        sta2.UnshippedAmount__c        = 0;
        sta2.SalesChannel__c           = 'SalesChannel__c';
        sta2.SoLatestDeliveryDate__c   = Date.today().addDays(5);
        insert new Statu_Achievements__c[] {sta1, sta2};

        mblist = [select Id, Statu_Achievements__c from MB_Statu_Achievements__c order by Statu_Achievements__r.Id];
        //System.assertEquals(2, mblist.size());
        //System.assertEquals(sta1.Id, mblist[0].Statu_Achievements__c);
        //System.assertEquals(sta2.Id, mblist[1].Statu_Achievements__c);

        opp.SAP_Province__c = '北京市';
        update opp;

        mblist = [select Id, State_Text__c, Opp_OCM_text__c, Opportunity_Category_Text__c, Distributor_InCharge_opp__c from MB_Statu_Achievements__c];
        //System.assertEquals('北京市', mblist[0].State_Text__c);

        //delete sta1;

        mblist = [select Id, Statu_Achievements__c from MB_Statu_Achievements__c order by Statu_Achievements__r.Id];
        //System.assertEquals(1, mblist.size());
    }
}