@isTest
private class ConsumableAssetHanderTest{
	static Account company;   //医院
	static Account section1;  //科室ET
	static Account agent;  //经销商
	static Account contact;  //契约
	static Account olympus;  //Olympus社内 其他 经销商资产
	static void initHpData() {
		RecordType rectagent = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        RecordType rectContract = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        RecordType rect9900 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 その他'];
		RecordType rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'];
        RecordType rectDpt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類ET'];
        //RecordType rectDpt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科'];
		company = new Account(RecordTypeId = rectCo.Id, Name = 'NFM110TestCompany');
        olympus = new Account(RecordTypeId = rectCo.Id, AgentCode_Ext__c = '9999900', Name = 'olympus');
        insert new Account[] {company, olympus};
        company = [select Management_Code__c, Management_Code_Auto__c, Name, Id from Account where Id =:company.Id];
        section1 = [select Management_Code__c, Management_Code_Auto__c, Name, Id, Department_Class_Label__c from Account where ParentId =:company.Id and RecordType.name = '戦略科室分類ET'];
		//
		agent = new Account(name='Testaccount001',
                                        Dealer_discount__c =20,
                                        Ban_On_Use_Date__c = Date.today().addDays(1),
                                        Business_Paper_Expiration_Date__c = Date.today().addDays(1),
                                        Tax_Practice_Expiration_Date__c = Date.today().addDays(1),
                                        Medical_Equipment_Expiration_Date__c = Date.today().addDays(1),
                                        RecordTypeId = rectagent.Id);
        insert agent;
        //经销商有效合同
        contact = new Account(name='Testaccount002',
                                            RecordTypeId = rectContract.Id,
                                            Contract_Decide_Start_Date__c = Date.today().addDays(-1),
                                            Contract_Decide_End_Date__c =Date.today().addDays(1),
                                            Contract_Start_Date__c = Date.today().addDays(-1),
                                            Contract_End_Date__c =Date.today().addDays(1),
                                            Agent_Ref__c =agent.Id,
                                            ET_SP_Dealer__c = true,
                                            ParentId =agent.Id);
        insert contact;
	}



	@isTest
	static void saleOrshipment(){
		initHpData();
		//产品信息
		Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Packing_list_manual__c=12, Manual_Entry__c = false);
        insert new Product2[]{prod01};
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
        insert new Product2__c[] {pro1};
        //出库单信息
		Consumable_order__c saleOrder = new Consumable_order__c();
        saleOrder.Name = 'OCM_02_005';
        saleOrder.Order_status__c = '批准';
        saleOrder.Order_type__c = '传票';
        saleOrder.RecordTypeid = System.Label.RT_ConOrder_Order;
        saleOrder.Dealer_Info__c = agent.Id;
        saleOrder.Order_ForHospital__c = company.Id;
        saleOrder.SummonsForDirction__c ='直接销售给医院';
        saleOrder.IsShipment__c = true;
        insert  saleOrder;

        //销售
        Consumable_order_details2__c saledet1 = new Consumable_order_details2__c();
        saledet1.Name = 'OCM_01_001001';
        saledet1.Consumable_Sale_order__c = saleOrder.Id;
        saledet1.Consumable_ZS_order__c = saleOrder.Id;
        saledet1.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
        saledet1.Bar_Code__c = '0114953170032070111804001082K250BBBBB';
        saledet1.Consumable_Product__c = pro1.Id;
        saledet1.Used_account__c = contact.Id;
        saledet1.Arrive_date__c = Date.today().addDays(-1);
        saledet1.Delivery_List_RMB__c = 11;
        saledet1.Box_Piece__c = '盒';
        saledet1.TracingCode__c = 'BBBBB';
        saledet1.SerialLotNo__c = '82K';
        insert saledet1;

        saledet1.Used_date__c = Date.today();

        System.Test.startTest();
        update saledet1;
        List<asset> assList =  [select Id,SerialNumber,Hospital__r.Name from asset  where SerialNumber = '82K(BBBBB)'];
        System.assertEquals(1, assList.size());
        System.Test.stopTest();

	}

    @isTest
    static void ruturnGoodTest(){
        initHpData();
        //产品信息
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Packing_list_manual__c=12, Manual_Entry__c = false);
        insert new Product2[]{prod01};
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
        insert new Product2__c[] {pro1};
        //出库单信息
        Consumable_order__c saleOrder = new Consumable_order__c();
        saleOrder.Name = 'OCM_02_005';
        saleOrder.Order_status__c = '批准';
        saleOrder.Order_type__c = '传票';
        saleOrder.RecordTypeid = System.Label.RT_ConOrder_Order;
        saleOrder.Dealer_Info__c = agent.Id;
        saleOrder.Order_ForHospital__c = company.Id;
        saleOrder.SummonsForDirction__c ='直接销售给医院';
        saleOrder.IsShipment__c = true;
        insert  saleOrder;

        //销售
        Consumable_order_details2__c saledet1 = new Consumable_order_details2__c();
        saledet1.Name = 'OCM_01_001001';
        saledet1.Consumable_Sale_order__c = saleOrder.Id;
        saledet1.Consumable_ZS_order__c = saleOrder.Id;
        saledet1.Consumable_Arrived_order__c = saleOrder.Id;
        saledet1.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
        saledet1.Bar_Code__c = '0114953170032070111804001082K250BBBBB';
        saledet1.Consumable_Product__c = pro1.Id;
        saledet1.Used_account__c = contact.Id;
        saledet1.Arrive_date__c = Date.today().addDays(-1);
        saledet1.Delivery_List_RMB__c = 11;
        saledet1.Box_Piece__c = '个';
        saledet1.TracingCode__c = 'BBBBB';
        saledet1.SerialLotNo__c = '82K';
        saledet1.Used_date__c = Date.today();
        insert saledet1;

        saledet1.Return_date__c = Date.today();

        System.Test.startTest();
        update saledet1;
        List<asset> assList =  [select Id,SerialNumber,Hospital__r.Name from asset  where SerialNumber = '82K(BBBBB)'];
        System.assertEquals(1, assList.size());
        System.Test.stopTest();

    }

    @isTest
    static void arrBarcodeChangeTest(){
        initHpData();
        //产品信息
        StaticParameter.EscapeConsumableOrderDetail2Trigger = true;
        Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true,Packing_list_manual__c=12, Manual_Entry__c = false);
        insert new Product2[]{prod01};
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
        insert new Product2__c[] {pro1};
        //订单
        Consumable_order__c Order1 = new Consumable_order__c();
        Order1.Name = 'OCM_01_001';
        Order1.Order_status__c = '批准';
        Order1.Order_type__c = '订单';
        Order1.Dealer_Info__c = agent.Id;
        Order1.RecordTypeid = System.Label.RT_ConOrder_Delivery;
        Order1.Total_num__c = 3;
        Order1.OrderNumber_arrived__c =1;
        Order1.Delivery_detail_count__c=1;
        Order1.Order_ProType__c = 'ET';
        insert  Order1;

        //经销商到货单

        Consumable_order__c arriveOrder = new Consumable_order__c();
        arriveOrder.Name = 'testArrive';
        arriveOrder.Order_status__c = '批准';
        arriveOrder.Deliver_date__c = Date.today();
        arriveOrder.Dealer_Info__c = agent.Id;
        arriveOrder.RecordTypeid = System.Label.RT_ConOrder_Arrive;
        insert  arriveOrder;

        //fahuo
        Consumable_order_details2__c saledet1 = new Consumable_order_details2__c();
        saledet1.Name = 'OCM_01_001001';
        saledet1.Consumable_order_minor__c = Order1.Id;
        saledet1.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
        saledet1.Bar_Code__c = '0114953170032070111804001082K250BBBBB';
        saledet1.Consumable_Product__c = pro1.Id;
        saledet1.Used_account__c = contact.Id;
        saledet1.Delivery_List_RMB__c = 11;
        saledet1.Box_Piece__c = '盒';
        saledet1.TracingCode__c = 'BBBBB';
        saledet1.SerialLotNo__c = '82K';
        saledet1.Guarantee_period_for_products__c = Date.today().addDays(5);
        saledet1.Sterilization_limit__c = Date.today().addDays(5);
        insert saledet1;

        asset ast = new asset();
        ast.Name = 
        ast.Name                             = prod01.Name;
        ast.Product2Id                       = prod01.Id;
        ast.SerialNumber                     = saledet1.SerialLotNo__c + '(' + saledet1.TracingCode__c + ')';
        ast.SLMark__c                        = 'Serial Number';         // 固定 dnInfo.SorLMark;
        ast.Guarantee_period_for_products__c = saledet1.Guarantee_period_for_products__c;
        ast.TracingCode__c                   = saledet1.TracingCode__c;
        ast.Guaranteen_end__c                = saledet1.Sterilization_limit__c;
        ast.Barcode__c                       = saledet1.Bar_Code__c;
        ast.Product_Serial_No__c             = pro1.OT_CODE_Text__c + ':' + saledet1.SerialLotNo__c  + '(' + saledet1.TracingCode__c + ')';
        ast.Asset_Owner__c                   = '经销商资产';
        ast.AccountId                        = olympus.Id;
        ast.Department_Class__c              = contact.Id;
        ast.Asset_owner_delaer_name__c       = agent.Id;
        ast.Hospital__c                      = agent.Id;

        insert ast;

        System.Test.startTest();
        List<asset> assList =  [select Id,SerialNumber,Hospital__r.Name from asset  where SerialNumber = '82K(BBBBB)'];
        System.assertEquals(1, assList.size());
        saledet1.Consumable_Arrived_order__c = arriveOrder.Id;
        saledet1.Arrive_date__c = Date.today();
        saledet1.Bar_Code__c = '0114953170032070101804001082K250BBBBB';
        update saledet1;
        assList =  [select Id,SerialNumber,Barcode__c from asset  where SerialNumber = '82K(BBBBB)'];
        System.assertEquals('0114953170032070101804001082K250BBBBB', assList[0].Barcode__c);
        System.Test.stopTest();

    }
}