@isTest(SeeAllData=false)
private class InquiryVerifyBatchTest {

    private static String quoteNo = 'textQutote01';
    public static String trade = '内貿';
    
    private static Id pricebookId = ControllerUtil.getStandardPricebook().Id;
    private static Boolean isUnitPrice = true;
    private static Boolean isOfferAmount = true;
    private static Boolean isTotalPrice = true;
    private static Boolean isDiscountRate = true;
    private static Boolean isDiscountAmount = true;
    private static Boolean isTradingPrice = true;
    private static Boolean isContractDetail = true;

    private static Date offerExpireDate = Date.today();
    private static String clientName = 'test client';
    private static String offerComment = 'test comment';
    private static Date offerPrintDate = Date.today();
    private static String agentName = 'test agent name';
    private static Date createdDateStr = Date.today();

    static testMethod void testMethod1() {
        InitData();
        // System.Test.StartTest();
        // try {
            ID exid = Database.executeBatch(new InquiryVerifyBatch(),1);
            // System.abortJob(exid);
        // } catch(Exception e){

        // }        
            // System.Test.StopTest();
    }

    private static void InitData() {
        User user = new User(Test_staff__c = true);
        user.LastName = '_サンブリッジ';
        user.FirstName = 'う';
        user.Alias = 'う';
        user.Email = 'olympusTest03@sunbridge.com';
        user.Username = 'olympusTest03@sunbridge.com';
        user.CommunityNickname = 'う';
        user.IsActive = true;
        user.EmailEncodingKey = 'ISO-2022-JP';
        user.TimeZoneSidKey = 'Asia/Tokyo';
        user.LocaleSidKey = 'ja_JP';
        user.LanguageLocaleKey = 'ja';
        user.ProfileId = System.Label.ProfileId_SystemAdmin;
        user.Job_Category__c = '销售推广';
        user.Province__c = '上海市';
        user.Use_Start_Date__c = Date.today().addMonths(-6);
        user.SalesManager__c = UserInfo.getUserId();
        user.BuchangApprovalManagerSales__c = UserInfo.getUserId();
        user.JingliApprovalManager__c = UserInfo.getUserId();
        user.BuchangApprovalManager__c = UserInfo.getUserId();
        user.ZongjianApprovalManager__c = UserInfo.getUserId();

        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;
        }


        StaticParameter.EscapeOpportunityBefUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;
        StaticParameter.EscapeNFM007Trigger = true;
        StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
        StaticParameter.EscapeSyncOpportunityTrigger = true;

        System.runAs(new User(Id = Userinfo.getUserId())) {
            insert user;
            // テストデータ
            Account company = new Account();
            company.RecordTypeId = rectCo[0].Id;
            company.Name         = 'NFM007TestCompany';
            insert 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;
            section.AgentCode_Ext__c = '9999900';
            insert section;

            Account depart = new Account();
            depart.RecordTypeId = rectDpt[0].Id;
            depart.Name         = '*';
            depart.Department_Name__c  = 'NFM007TestDepart';
            depart.ParentId            = section.Id;
            depart.Department_Class__c = section.Id;
            depart.Hospital__c         = company.Id;
            section.AgentCode_Ext__c = '9999900';
            insert depart;

            Opportunity opp = new Opportunity();
            opp.AccountId           = depart.Id;
            opp.Department_Class__c = section.Id;
            opp.Hospital__c         = company.Id;
            opp.SAP_Send_OK__c      = false;
            opp.CurrencyIsoCode = 'CNY';
            opp.Name                = 'GZ-SP-NFM007_1';
            opp.Trade__c            = '内貿';
            opp.StageName           = '引合';
            opp.CloseDate           = date.newinstance(2022, 11, 30);
            opp.Stock_apply_status__c = '申请中';
            opp.IsNextMonthOfVisit__c = false;
            opp.Pricebook2Id = pricebookId;
            opp.ET_SP_Consumption__c = false;
            opp.Opp_order_Type__c = '';
            opp.ThisPhase_Ship_Forecast__c  = false;
            opp.Equipment_Order_Flg__c  = false;
            opp.old_Oppo_No__c = '';
            opp.If_Account_Change__c  = false;
            // opp.ForecastAccuracyObject__c = true;

            // 待解决设置值后查询时为空的问题(batch中测试程序会跳过该条件)
            opp.OCM_man_province_cus_txt__c = '北京';
            opp.OCM_man_province_txt__c = '北京';
            // opp.SalesdepartmentForecast__c = '1.华北';
            insert opp;

            Quote quote = new Quote( Name = 'テスト見積');
            quote.OpportunityId = opp.Id;
            
            quote.Quote_No__c = quoteNo;
            quote.Unit_Price__c = isUnitPrice;
            quote.Offer_Amount__c = isOfferAmount;
            quote.TOTAL__c = isTotalPrice;
            quote.Pricebook2Id = pricebookId;
            quote.Discount__c = isDiscountRate;
            quote.Pricing__c = isDiscountAmount;
            quote.Preferential_Trading_Price__c = isTradingPrice;
            quote.Contract__c = isContractDetail;
            quote.Quote_Comment__c = offerComment;
            quote.QuoteName__c = 'テスト見積';
            quote.Dealer_Final_Price__c = 100;
            quote.Quote_Adjust_Calculate__c = 10;
            quote.Quote_Adjust_Amount__c = 10;
            quote.OCM_Agent1_Price__c = 10;
            quote.Agent1_Agent2_Price__c = 10;
            quote.quoteSavedDate__c = createdDateStr;
            quote.createdDate = createdDateStr;
            insert quote;

            Product2 prd1 = new Product2();
            prd1.ProductCode_Ext__c     = 'Prd1';
            prd1.ProductCode            = 'Prd1';
            prd1.Repair_Product_Code__c = 'Prd1_RP';
            prd1.Name                   = 'Prd1';
            prd1.Manual_Entry__c        = false;
            prd1.Category2__c = '本体';
            prd1.Category3__c = '纤维镜';
            prd1.Category4__c = 'CV';
            prd1.Category5__c = '260SL系列';
            prd1.Asset_Model_No__c = '1001';
            insert prd1;

            

            PricebookEntry entry = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = prd1.Id);
            entry.UnitPrice = 0;
            entry.IsActive = true;
            entry.UseStandardPrice = false;
            entry.CurrencyIsoCode = 'CNY';
            insert entry;

            List<Quote> qList = [Select Id, CurrencyIsoCode from Quote where Id = : quote.Id];
            System.assertEquals(1, qList.size());
            System.assertEquals('CNY', qList[0].CurrencyIsoCode);

            QuoteLineItem target = new QuoteLineItem();
            target.Name__c = 'テスト商品';
            target.QuoteId = quote.Id;
            target.Quantity = 1;
            target.Cost_Subtotal__c = 0;
            target.Cost__c = 200;
            //        target.TotalPrice = 100;
            target.UnitPrice = 10;
            target.Product2Id = prd1.id;
            target.PricebookEntryId = entry.Id;
            insert target;

            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 = prd1.Id;
            saledet1.Used_account__c = section.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);
            saledet1.Send_Date__c = null;
            //saledet1.Dealer_Shipment__c = false;
            saledet1.Arrive_date__c = null;
            //saledet1.Dealer_Arrive__c = false;
            saledet1.Used_date__c = null;
            //saledet1.Dealer_Saled__c = false;
            saledet1.Deliver_date__c = Date.today().addDays(-10);
            insert saledet1;

            asset ast = new asset();
            ast.Name                             = prd1.Name;
            ast.Product2Id                       = prd1.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             = saledet1.SerialLotNo__c  + '(' + saledet1.TracingCode__c + ')';
            ast.Asset_Owner__c                   = '经销商资产';
            ast.AccountId                        = depart.Id;
            ast.Department_Class__c              = section.Id;
            // ast.Asset_owner_delaer_name__c       = company.Id;
            ast.Hospital__c                      = company.Id;
            insert ast;

            String flag='NG2';
            String d1='产品试用';
            String dept='医疗华北营业本部';
            // User user = new User(Test_staff__c = true);
            // user.LastName = '_サンブリッジ';
            // user.FirstName = 'う';
            // user.Alias = 'う';
            // user.Email = 'olympusTest03@sunbridge.com';
            // user.Username = 'olympusTest03@sunbridge.com';
            // user.CommunityNickname = 'う';
            // user.IsActive = true;
            // user.EmailEncodingKey = 'ISO-2022-JP';
            // user.TimeZoneSidKey = 'Asia/Tokyo';
            // user.LocaleSidKey = 'ja_JP';
            // user.LanguageLocaleKey = 'ja';
            // user.ProfileId = System.Label.ProfileId_SystemAdmin;
            // user.Province__c = '北京';
            // user.Dept__c = dept;
            // user.Use_Start_Date__c = Date.today().addMonths(-6);
            // insert user;

            FixtureDeliverySlip__c fdsObj = new FixtureDeliverySlip__c();
             fdsObj.Name = '00001';
             fdsObj.DeliveryCompany__c = '利讯';
             fdsObj.Distributor_method__c = '陆运';
             fdsObj.DeliveryType__c = '发货';
             fdsObj.Wh_Staff__c = Userinfo.getUserId();
             fdsObj.Shippment_loaner_time__c = System.now();
             insert fdsObj;

            Fixture_Set__c fsObj1 = new Fixture_Set__c();
            fsObj1.Name = 'set1';
            fsObj1.Fixture_Set_Body_Model_No__c = 'modelNo1';
            fsObj1.Loaner_name__c = 'name1';
            insert fsObj1;

            Fixture_Set_Detail__c fsdObjA1 = new Fixture_Set_Detail__c();
             // 备品配套明细
             fsdObjA1.Name = '备品配套明细名1';
             fsdObjA1.Name_CHN_Created__c = '中文名称1';
             fsdObjA1.Product2__c = prd1.Id;
             fsdObjA1.Fixture_Set__c = fsObj1.Id;
             fsdObjA1.Is_Body__c = true;
             fsdObjA1.Is_Optional__c = false;
             fsdObjA1.UniqueKey__c = fsObj1.Id + ':' + prd1.Id;
             fsdObjA1.SortInt__c = 1;
             fsdObjA1.Quantity__c = 1;
             insert fsdObjA1;

              Contact contact2 = new Contact();
             contact2.AccountId = depart.Id;
             contact2.FirstName = '責任者';
             contact2.LastName = 'test1经销商';
             insert contact2;

            Rental_Apply__c raObj = new Rental_Apply__c();
            raObj.Name = 'testra';
            raObj.OwnerId = user.Id;
            raObj.Product_category__c = 'GI';
            raObj.Demo_purpose1__c ='产品试用';
            raObj.demo_purpose2__c = '试用（无询价）';
            raObj.direct_send__c = '医疗机构';
            raObj.Loaner_received_staff__c = '王五';
            raObj.Loaner_received_staff_phone__c = '110';
            raObj.direct_shippment_address__c = '北京市';
            raObj.Request_shipping_day__c = Date.toDay();
            raObj.Hope_Lonaer_date_Num__c = 16;
            //raObj.Request_return_day__c = Date.toDay();
            raObj.Phone_number__c = '1234567890';
            raObj.Request_approval_time__c = Datetime.newInstance(1970, 1, 1);
            raObj.Strategic_dept__c = section.Id;
            raObj.Account__c = depart.Id;
            raObj.Loaner_medical_Staff__c = contact2.Id;
            raObj.Hospital__c = section.Id;
            insert raObj;

             Rental_Apply_Equipment_Set__c raesObj = new Rental_Apply_Equipment_Set__c();
             // 借出备品配套一览
             raesObj.Rental_Apply__c = raObj.Id;
             raesObj.Fixture_Set__c = fsObj1.Id;
             raesObj.Cancel_Select__c = false;
             raesObj.Rental_Start_Date__c = Date.toDay();
             raesObj.Rental_End_Date__c = Date.toDay();
             raesObj.IndexFromUniqueKey__c = 1;
             raesObj.UniqueKey__c = '1:'+ fsObj1.Id + ':1';
             raesObj.Final_reply_day_text__c = Date.today();
             insert raesObj;

            Rental_Apply_Equipment_Set_Detail__c raesdObj1 = new Rental_Apply_Equipment_Set_Detail__c();
             raesdObj1.Rental_Apply__c = raObj.Id;
             raesdObj1.Fixture_Set_Detail__c = fsdObjA1.Id;
             raesdObj1.Rental_Num__c = 1;
             raesdObj1.Queue_Number__c = 14;
             raesdObj1.Queue_Day__c=date.today();
             raesdObj1.Queue_Time__c=Time.newInstance(1,1,1,1);
             raesdObj1.Is_Body__c = true;
             raesdObj1.Rental_Apply_Equipment_Set__c = raesObj.Id;
             raesdObj1.DeliverySlip__c = fdsObj.Id;
             raesdObj1.IndexFromUniqueKey__c = 1;
             raesdObj1.UniqueKey__c = '1:'+ raesObj.Id + ':' + fsdObjA1.Id + ':1';
             raesdObj1.FSD_OneToOneAccessory_Cnt__c = 2;
             raesdObj1.FSD_Is_Optional__c = false;
             raesdObj1.FSD_Is_OneToOne__c = false;
             raesdObj1.ApplyPersonAppended__c = false;
             raesdObj1.Inspection_result_after_Final__c = 'OK';
             raesdObj1.After_Inspection_time_Final__c = Date.today();
             raesdObj1.Check_lost_Item_Final__c = 'OK';
             raesdObj1.Arrival_in_wh__c = false;
             raesdObj1.FSD_Fixture_Model_No__c = 'n01';
             raesdObj1.Fixture_Model_No_text__c = 'n01';
             raesdObj1.Salesdepartment_before__c = '3.西北营业本部';
             raesdObj1.Internal_asset_location_before__c = '北京 备品中心';
             raesdObj1.Product_category_text__c = 'GI';
             raesdObj1.Equipment_Type_text__c = '产品试用';
             raesdObj1.Cancel_Select__c=false;
             raesdObj1.IsAdjust__c=false;
             raesdObj1.Asset__c = ast.id;
             raesdObj1.Shippment_loaner_time__c = createdDateStr.addMonths(-5);
             insert raesdObj1;
            
        }
    }
}