@isTest
private class ConsumableOrderDetail2TriggerTest {
    static testMethod void testInsert() {
        StaticParameter.ConsumableAssetHanderTrigger = 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);
        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 co = new Consumable_order__c();
        co.Name = 'testorderArrive';
        insert co;
        //插入明细1
        Consumable_orderdetails__c Orderdet6 = new Consumable_orderdetails__c();
        Orderdet6.Name = 'ZSD_01_001001';
        Orderdet6.Consumable_order__c = co.Id;
        Orderdet6.RecordTypeId = System.Label.RT_ConOrderDetail1_Order  ;
        Orderdet6.Consumable_Count__c = 1;
        Orderdet6.Consumable_Product__c = pro1.Id;
        Orderdet6.Intra_Trade_List_RMB__c = 1;
        //Orderdet6.Box_Piece__c = '盒';
        insert Orderdet6;

        //插入发货明细
        Consumable_order_details2__c cod1 = new Consumable_order_details2__c();
        cod1.Name = 'testorderdetail21';
        cod1.Consumable_order_minor__c = co.id;
        Date d = Date.newInstance(2017, 7, 3);
        cod1.Arrive_date__c = d;
        cod1.Consumable_Product__c = pro1.Id;
        cod1.Asset_Model_No__c = 'Test01';
        upsert cod1;

        //插入发货明细2
        Consumable_order_details2__c cod2 = new Consumable_order_details2__c();
        cod2.Name = 'testorderdetail22';
        cod2.Asset_Model_No__c = 'Test01';
        cod2.Consumable_order_minor__c = co.id;
        cod2.Consumable_Arrived_order__c = co.id;
        cod2.Deliver_date__c = d;
        cod2.Consumable_Return_order__c = co.Id;
        upsert cod2;

        Consumable_order__c order1 = [select id, OrderNumber_arrived__c, Shipment_Date__c, Delivery_detail_count__c from Consumable_order__c where Id = :co.Id];
        //System.assertEquals(1,order1.OrderNumber_arrived__c);
        //System.assertEquals(d,order1.Shipment_Date__c);
        //System.assertEquals(1,order1.Delivery_detail_count__c);

        //更新发货明细
        Consumable_order_details2__c cod3 = new Consumable_order_details2__c();
        cod3.id = cod2.Id;
        Date d1 = Date.newInstance(2017, 7, 4);
        cod3.Arrive_date__c = d1;
        upsert cod3;

        //Consumable_order__c order2 = [select id,OrderNumber_arrived__c from Consumable_order__c where Id = :co.Id];
        //System.assertEquals(2,order2.OrderNumber_arrived__c);

        //删除发货明细
        Consumable_order_details2__c cod4 = new Consumable_order_details2__c();
        cod4.id = cod2.Id;
        delete cod4;


        //Consumable_order__c order3 = [select id,OrderNumber_arrived__c from Consumable_order__c where Id = :co.Id];
        //System.assertEquals(1,order3.OrderNumber_arrived__c);

        //testarrivedSet();
        //testF();
    }

    private static void testarrivedSet() {
        Consumable_order__c co = new Consumable_order__c();
        co.Name = 'testCo';
        List<RecordType> rtList = [select Id from RecordType where DeveloperName = 'Arrive' and SobjectType = 'Consumable_order__c'];
        co.RecordTypeId = rtList[0].Id;
        upsert co;

        Product2__c p = new Product2__c();
        p.Name = 'testModel';
        upsert p;

        //生成订单
        Consumable_orderdetails__c odd1 = new Consumable_orderdetails__c();
        odd1.Name = 'testorderdetail1';
        odd1.Consumable_order__c = co.Id;
        odd1.Dealer_Custom_Price__c = 1000;
        odd1.Consumable_count__c = 10;
        odd1.Invoiced_Procount__c = 5;
        odd1.InvoicedProCost_RMB__c = 5000;
        odd1.Shipment_Count__c = 8;
        odd1.Delivery_List_RMB__c = 1000;
        //odd1.ReturnGoods_unitPrice__c=1000;
        odd1.RrturnPro_count__c = 2;
        upsert odd1;
        System.Test.startTest();
        Consumable_order_details2__c cod1 = new Consumable_order_details2__c();
        cod1.Name = 'testCod1';
        cod1.Asset_Model_No__c = 'testModel';
        cod1.Consumable_product__c = p.Id;
        cod1.Consumable_order_minor__c = co.Id;
        //cod1.Consumable_Arrived_order__c = co.Id;
        rtList = [select Id, developername from RecordType where DeveloperName = 'Detail2_Delivery' and SobjectType = 'Consumable_order_details2__c'];
        cod1.RecordTypeId = rtList[0].Id;
        cod1.Intra_Trade_List_RMB__c = 1000;

        upsert cod1;

        //Consumable_orderdetails__c detail1 = [select Id, Name, Asset_Model_No__c, Consumable_Arrivecount__c from Consumable_orderdetails__c where Consumable_order__c = :co.Id and Asset_Model_No__c =:cod1.Asset_Model_No__c];

        //System.assertEquals(detail1.Asset_Model_No__c, cod1.Asset_Model_No__c);
        //System.assertEquals(detail1.Consumable_Arrivecount__c, 1);

        Consumable_order_details2__c cod2 = new Consumable_order_details2__c();
        cod2.id = cod1.Id;
        cod2.Consumable_Arrived_order__c = co.Id;
        cod2.RecordTypeId = rtList[0].Id;
        cod2.Intra_Trade_List_RMB__c = 1000;

        upsert cod2;

        //detail1 = [select Id, Name, Asset_Model_No__c, Consumable_Arrivecount__c from Consumable_orderdetails__c where Consumable_order__c = :co.Id and Asset_Model_No__c =:cod1.Asset_Model_No__c];

        //System.assertEquals(detail1.Consumable_Arrivecount__c, 2);

        Consumable_order_details2__c cod3 = new Consumable_order_details2__c();
        cod3.id = cod1.id;
        cod3.Name = 'testCod1';
        cod3.Asset_Model_No__c = 'testModel';
        cod3.Consumable_product__c = p.Id;
        cod3.Consumable_order_minor__c = co.Id;
        cod3.RecordTypeId = rtList[0].Id;
        cod3.Intra_Trade_List_RMB__c = 1000;
        cod3.Return_date__c = date.today();

        upsert cod3;

        delete cod2;
        System.Test.stopTest();
        //detail1 = [select Id, Name, Asset_Model_No__c, Consumable_Arrivecount__c from Consumable_orderdetails__c where Consumable_order__c = :co.Id and Asset_Model_No__c =:cod1.Asset_Model_No__c];

        //System.assertEquals(detail1.Consumable_Arrivecount__c, 2);
    }

    private static void testReturnSet() {
        Consumable_order__c co = new Consumable_order__c();
        co.Name = 'testCo';
        List<RecordType> rtList = [select Id from RecordType where DeveloperName = 'ReturnGoods' and SobjectType = 'Consumable_order__c'];
        co.RecordTypeId = rtList[0].Id;
        upsert co;

        Product2__c p = new Product2__c();
        p.Name = 'testModel';
        upsert p;

        //生成订单
        Consumable_orderdetails__c odd1 = new Consumable_orderdetails__c();
        odd1.Name = 'testorderdetail1';
        odd1.Consumable_order__c = co.Id;
        odd1.Dealer_Custom_Price__c = 1000;
        odd1.Consumable_count__c = 10;
        odd1.Invoiced_Procount__c = 5;
        odd1.InvoicedProCost_RMB__c = 5000;
        odd1.Shipment_Count__c = 8;
        odd1.Delivery_List_RMB__c = 1000;
        //odd1.ReturnGoods_unitPrice__c=1000;
        odd1.RrturnPro_count__c = 2;
        upsert odd1;

        Consumable_order_details2__c cod1 = new Consumable_order_details2__c();
        cod1.Name = 'testCod1';
        cod1.Asset_Model_No__c = 'testModel';
        cod1.Consumable_product__c = p.Id;
        cod1.Consumable_order_minor__c = co.Id;
        cod1.Consumable_Return_order__c = co.Id;
        cod1.Return_date__c = date.today();
        rtList = [select Id, developername from RecordType where DeveloperName = 'Detail2_Delivery' and SobjectType = 'Consumable_order_details2__c'];
        cod1.RecordTypeId = rtList[0].Id;
        cod1.Intra_Trade_List_RMB__c = 1000;
        System.Test.startTest();
        upsert cod1;

        //Consumable_orderdetails__c detail1 = [select Id, Name, Asset_Model_No__c, Consumable_Arrivecount__c from Consumable_orderdetails__c where Consumable_order__c = :co.Id and Asset_Model_No__c =:cod1.Asset_Model_No__c];

        //System.assertEquals(detail1.Asset_Model_No__c, cod1.Asset_Model_No__c);
        //System.assertEquals(detail1.Consumable_Arrivecount__c, 1);

        Consumable_order_details2__c cod2 = new Consumable_order_details2__c();
        cod2.id = cod1.id;
        cod2.Consumable_Return_order__c = co.Id;
        cod2.RecordTypeId = rtList[0].Id;
        cod2.Intra_Trade_List_RMB__c = 1000;
        cod2.Return_date__c = date.today();
        upsert cod2;

        //detail1 = [select Id, Name, Asset_Model_No__c, Consumable_Arrivecount__c from Consumable_orderdetails__c where Consumable_order__c = :co.Id and Asset_Model_No__c =:cod1.Asset_Model_No__c];

        //System.assertEquals(detail1.Consumable_Arrivecount__c, 2);

        Consumable_order_details2__c cod3 = new Consumable_order_details2__c();
        cod3.id = cod1.id;
        cod3.Intra_Trade_List_RMB__c = 1000;
        cod3.Return_date__c = date.today();

        upsert cod3;
        delete cod2;
        System.Test.stopTest();
        //detail1 = [select Id, Name, Asset_Model_No__c, Consumable_Arrivecount__c from Consumable_orderdetails__c where Consumable_order__c = :co.Id and Asset_Model_No__c =:cod1.Asset_Model_No__c];

        //System.assertEquals(detail1.Consumable_Arrivecount__c, 2);
    }


    //返品
    static testMethod void productReturn_new() {
        //Oly_TriggerHandler.bypass('NFM701ControllerHandler');
        StaticParameter.ConsumableAssetHanderTrigger = true;
        List<RecordType> rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店'];
        if (rectCo.size() == 0) {
            return;
        }
        Profile prof = [select Id from Profile where Name = '901_经销商社区普通权限_2重验证(ET)'];//



        Account myAccount2 = new Account(name = 'Testaccount002', Dealer_discount__c = 20, RecordTypeId = rectCo[0].Id);
        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;
        system.runAs(myUser_Test) {
                        
        Product2 prod01 = new Product2(Name='Test01',ProductCode='Test01',Asset_Model_No__c = 'Test01',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
        Product2 prod02 = new Product2(Name='Test02',ProductCode='Test02',Asset_Model_No__c = 'Test02',SFDA_Status__c = '有効',Dealer_special_Object__c = true);
        insert new Product2[]{prod01,prod02};
        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};
            //订单
            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.RecordTypeid = System.Label.RT_ConOrder_Delivery;
            insert  Order1;
            //Consumable_order__c coc = [select Dealer_Info__r.name from Consumable_order__c where Id =:Order1.Id];
            //system.assertEquals('qeee',coc.Dealer_Info__r.name);

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

            //发货明细
            //fahuo
            Consumable_order_details2__c Orderdet1 = new Consumable_order_details2__c();
            Orderdet1.Name = 'OCM_01_001001';
            Orderdet1.Consumable_order_minor__c = Order1.Id;
            Orderdet1.Consumable_Product__c = pro1.Id;
            Orderdet1.Asset_Model_No__c = 'Pro001';
            Orderdet1.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            Orderdet1.Box_Piece__c ='盒';
            Orderdet1.Bar_Code__c = '01049531702003111115120017181000105ZK250BXJRA';
            //daohuo
            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.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            Orderdet2.Box_Piece__c ='盒';
            Orderdet2.Arrive_date__c = Date.today();
            //Orderdet2.Send_Date__c = Date.today();
            //Orderdet2.Used_date__c= Date.today();
            Orderdet2.Bar_Code__c = '01049531702003111115120017181000105ZK250BXJRB';
            //sale
            Consumable_order_details2__c Orderdet3 = new Consumable_order_details2__c();
            Orderdet3.Name = 'OCM_01_001003';
            Orderdet3.Consumable_order_minor__c = Order1.Id;
            Orderdet3.Consumable_Product__c = pro1.Id;
            Orderdet3.Asset_Model_No__c = 'Pro001';
            Orderdet3.Box_Piece__c ='盒';
            Orderdet3.Used_date__c= Date.today();
            Orderdet3.Arrive_date__c = Date.today();
            Orderdet3.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            Orderdet3.Bar_Code__c = '01049531702003111115120017181000105ZK250BXJRC';
            //出库
            Consumable_order_details2__c Orderdet4 = new Consumable_order_details2__c();
            Orderdet4.Name = 'OCM_01_001004';
            Orderdet4.Consumable_order_minor__c = Order1.Id;
            Orderdet4.Consumable_Product__c = pro1.Id;
            Orderdet4.Asset_Model_No__c = 'Pro001';
            Orderdet4.Box_Piece__c ='个';
            Orderdet4.Send_Date__c = Date.today();
            Orderdet4.Arrive_date__c = Date.today();
            Orderdet4.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            Orderdet4.Bar_Code__c = '01049531702003111115120017181000105ZK250BXJRN';
            //quxiao
            Consumable_order_details2__c Orderdet5 = new Consumable_order_details2__c();
            Orderdet5.Name = 'OCM_01_001005';
            Orderdet5.Consumable_order_minor__c = Order1.Id;
            Orderdet5.Consumable_Product__c = pro1.Id;
            Orderdet5.Asset_Model_No__c = 'Pro001';
            Orderdet5.Cancellation_Date__c = Date.today();
            Orderdet5.Cancellation_Flag__c = true;
            Orderdet5.RecordTypeId = System.Label.RT_ConOrderDetail2_Delivery;
            Orderdet5.Bar_Code__c = '01049531702003111115120017181000105ZK250BXJRZ';
            Orderdet5.Box_Piece__c ='个';
            insert new Consumable_order_details2__c[] {Orderdet1, Orderdet2,Orderdet3,Orderdet4,Orderdet5};

            PageReference page = new PageReference('/apex/ArriveGoods?ArrType=ReG');
            System.Test.setCurrentPage(page);
            ArriveGoodsController Controller = new ArriveGoodsController();
            //初始化测试
            System.Test.startTest();
            Controller.init();
            //排序
            Controller.barcode = '01049531702003111115120017181000105ZK250BXJRA\n01049531702003111115120017181000105ZK250BXJRB\n01049531702003111115120017181000105ZK250BXJRC\n01049531702003111115120017181000105ZK250BXJRN\n01049531702003111115120017181000105ZK250BXJRZ\n01049531702003111115120017181000105ZK250BXJRR';
            Controller.SearchPro();
            System.assertEquals(5, controller.ConsumableorderdetailsRecordserror.size());
            // System.assertEquals(0, controller.ShowGoodsofReturnList.size());
            // controller.ShowGoodsofReturnList[0].esd.Return_reason__c = '多发';
            // controller.ShowGoodsofReturnList[1].esd.Return_reason__c = '多发';
            // controller.ShowGoodsofReturnList[0].esd.Rrturn_count__c = 1;
            // Controller.UpdateGoodsOfReturn();

            list<String> list1 = new list<String>();
            list1.add(Controller.barcode = '01234567898');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '012345678901234A67');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '0123456789012345117AA01');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '01234567890123456789012349990001');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '0123456789012345177AA0000000000001');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '0123456789012345177AA1111111111111');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '241345');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '2413456');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '24134567');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '241345678');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '2413456789');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '24134567890');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '241345678901');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '0112345678901231177890121215678901234');
            Controller.inventoryEntryNoESetId(list1);

            list1.clear();
            list1.add(Controller.barcode = '0112345678901231106789012105678901234');
            Controller.inventoryEntryNoESetId(list1);
            System.Test.stopTest();
            // String[] azzz = new String[]{'1','2'};
            // system.debug(azzz[3]);
        }
    }


}