@isTest
private class ConsumableOrderTriggerTest
{
	static testMethod void ConsumableTest_NewOrder(){
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        List<RecordType> rectCotac = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約'];
        if (rectCotac.size() == 0) {
            return;
        }
        Profile prof = [select Id from Profile where Name ='901_经销商社区普通权限_2重验证'];
        Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効');
        Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効');
        insert new Product2[]{prod01,prod02};
        Account myAccount2 = new Account(Name='Testaccount002',Dealer_discount__c =20,RecordTypeId = rectCo[0].Id,Product_Limit_Date__c = 'Test01|2|4,Test02|3|5');
        insert myAccount2;


        Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=myAccount2.id);
        insert core;
        user myUser_Test = New User(ContactId = core.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='testUser@testorg.com');
        insert myUser_Test;
        Account contact = new Account(name='Testaccount002',RecordTypeId = rectCotac[0].Id,Business_Assistant__c =myUser_Test.Id ,SpecialDealerName__c = 'Testaccount002',ParentId = myAccount2.Id,Agent_Ref__c = myAccount2.Id,Delete_Flag__c = false,Contract_Department_Class__c = 'ET',Contract_Decide_Start_Date__c = Date.today().addDays(-1),Contract_Decide_End_Date__c = Date.today().addDays(1),MaxActivityDate__c = Date.today());
        insert contact;

        //----------------------------
        StaticParameter.ConsumableAssetHanderTrigger = true;
        Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
        Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
        insert new Product2__c[] {pro1, pro2};

        Opportunity opp = new Opportunity();
        opp.Opportunity_No__c = 'XB-153P-06-S151289';
        opp.Opportunity_Category__c = 'ET';
        opp.StageName = '注残';
        opp.Authorized_DB_No__c = 'XB-153P-06-S151289';
        opp.Name = '::XB-153P-06-S151289';
        opp.Close_Forecasted_Date__c = Date.today();
        opp.CloseDate = Date.today();
        opp.Opp_Confirmation_Day__c = Date.today();
        insert opp;

        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 = myAccount2.Id;
        Order1.Order_effective_contact__c = contact.Id;
        Order1.Consumable_pdf_insert_day__c = Date.today();
        Order1.Opportunity__c = opp.Id;
        Order1.RecordTypeid = System.Label.RT_ConOrder_Order;
        insert  Order1;

        List<Consumable_order__c> abc = [select Order_effective_contact__c, SalesManager__c from Consumable_order__c where Id = :Order1.Id];
        System.assertEquals(1, abc.size());
        System.assertEquals(contact.Id, abc[0].Order_effective_contact__c);

        

        Statu_Achievements__c Statu1 = new Statu_Achievements__c();
        Statu1.ContractNO__c = 'SH-ET-191025';
        Statu1.Opportunity__c = opp.Id;
        Statu1.ContractAmount__c = 100;
        insert Statu1;
        //----------------------------

        //其他订单明细
        //fahuo
        Consumable_orderdetails__c Orderdet = new Consumable_orderdetails__c();
        Orderdet.Name = 'OCM_01_00101';
        Orderdet.Consumable_order__c = Order1.Id;
        Orderdet.RecordTypeId = System.Label.RT_ConOrderDetail1_Order;
        Orderdet.Consumable_Product__c = pro1.Id;
        Orderdet.Consumable_count__c = 1;
        insert Orderdet;

        //到货明细
        Consumable_order_details2__c Orderdet2 = new Consumable_order_details2__c();
        Orderdet2.Name = 'OCM_01_001002';
        Orderdet2.Consumable_order_minor__c = Order1.Id;
        Orderdet2.Consumable_Product__c = pro1.Id;
        Orderdet2.Asset_Model_No__c = 'Pro001';
        Orderdet2.Arrive_date__c = Date.today();
        Orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
        Orderdet2.Bar_Code__c = '01049531702003111115120017181000105ZK250BXJRI';
        
        insert Orderdet2;

        Order1.Order_status__c = '已提交';
        update Order1;
        abc = [select Order_effective_contact__c, SalesManager__c from Consumable_order__c where Id = :Order1.Id];

        //----------------------------

        system.runAs(myUser_Test){
            // Product2__c pro1 = new Product2__c(Name='Pro001',OT_CODE_Text__c='Test001',Product2__c = prod01.Id);
            // Product2__c pro2 = new Product2__c(Name='Pro002',OT_CODE_Text__c='Test002',Product2__c = prod02.Id);
            // insert new Product2__c[] {pro1, pro2};
        	//Account contact = new Account(name='Testaccount002',RecordTypeId = rectCotac[0].Id,Business_Assistant__c =myUser_Test.Id ,SpecialDealerName__c = 'Testaccount002',ParentId = myAccount2.Id,Agent_Ref__c = myAccount2.Id,Delete_Flag__c = false,Contract_Department_Class__c = 'ET',Contract_Decide_Start_Date__c = Date.today().addDays(-1),Contract_Decide_End_Date__c = Date.today().addDays(1));
            

            // 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 = myAccount2.Id;
            // Order1.Order_effective_contact__c = contact.Id;
            // Order1.Consumable_pdf_insert_day__c = Date.today();
            // Order1.Opportunity__c = '0061s000004G5e2AAC';
            // Order1.RecordTypeid = System.Label.RT_ConOrder_Order;
            // insert  Order1;

            // List<Consumable_order__c> abc = [select Order_effective_contact__c, SalesManager__c from Consumable_order__c where Id = :Order1.Id];
            // System.assertEquals(1, abc.size());
            // System.assertEquals(contact.Id, abc[0].Order_effective_contact__c);

            //其他订单明细
            //fahuo
            // Consumable_orderdetails__c Orderdet = new Consumable_orderdetails__c();
            // Orderdet.Name = 'OCM_01_00101';
            // Orderdet.Consumable_order__c = Order1.Id;
            // Orderdet.RecordTypeId = System.Label.RT_ConOrderDetail1_Order;
            // Orderdet.Consumable_Product__c = pro1.Id;
            // Orderdet.Consumable_count__c = 1;
            // insert Orderdet;

            // //到货明细
            // Consumable_order_details2__c Orderdet2 = new Consumable_order_details2__c();
            // Orderdet2.Name = 'OCM_01_001002';
            // Orderdet2.Consumable_order_minor__c = Order1.Id;
            // Orderdet2.Consumable_Product__c = pro1.Id;
            // Orderdet2.Asset_Model_No__c = 'Pro001';
            // Orderdet2.Arrive_date__c = Date.today();
            // Orderdet2.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            // Orderdet2.Bar_Code__c = '01049531702003111115120017181000105ZK250BXJRI';
            // insert Orderdet2;

            // Order1.Order_status__c = '已提交';
            // update Order1;
            // abc = [select Order_effective_contact__c, SalesManager__c from Consumable_order__c where Id = :Order1.Id];
            //System.assertEquals(1, abc.size());
            //System.assertEquals(myUser_Test.Id, abc[0].SalesManager__c);
        }
    }
}