submit_approval_process
    online
    button
    javascript
    提交待审批
    onClickJavaScript
    false
    {!RequireScript("/soap/ajax/51.0/connection.js")}
{!RequireScript("/soap/ajax/51.0/apex.js")}
{!RequireScript("/resource/CommonUtilJs")}
//2021-10-22 gwy 版本更改为51.0
var getConnectDMLErrorMessages = function (results) {
	var messages = [],
	i = 0,
	len = results.length,
	r;
	for (; i < len; i++) {
		r = results[i];
		if (!r.getBoolean("success")) {
			messages = messages.concat(getConnectDMLMessagesOfAResult(r));
		}
	}
	return messages;
};
var getConnectDMLMessagesOfAResult = function (res) {
	var messages = [],
	errors = res.getArray("errors"),
	i = 0,
	len = errors.length,
	e;
	for (; i < len; i++) {
		e = errors[i];
		messages.push(e.message + " " + getConnectDMLErrorFields(e));
	}
	return messages;
};
var getConnectDMLErrorFields = function (error) {
	var fields = error.getArray('fields');
	if (fields.length > 0) {
		return "[" + fields.join(",") + "]"
	} else {
		return "";
	}
};
var foo = function() {
    var buttons = document.getElementsByName('submit_approval_process'.toLowerCase());
    for (var i=0; i<buttons.length; i++) {
        buttons[i].className = "btnDisabled";
        buttons[i].disabled = true;
    }
    if (!confirm("一旦提交此记录以待批准,根据您的设置您可能不再能够编辑此记录或将他从批准过程中调回。是否继续?")) {
        return;
    }
     //1540 you 试用(无询价)目的的备品申请单,不能关联询价信息!
    if('{!Rental_Apply__c.demo_purpose2__c}' == '试用(无询价)' && '{!Rental_Apply__c.Follow_UP_Opp__c}' !=null && '{!Rental_Apply__c.Follow_UP_Opp__c}' != ''){
	   alert('试用(无询价)目的的备品申请单,不能关联询价信息!');
       return;
	}
    // 希望到货日不能早于申请提交日-0418追加
    if ('{!Rental_Apply__c.Request_shipping_day__c}'  < '{!TODAY()}' ) {
        alert('希望到货日不能早于申请提交日');
        return;
    }
    var raesdList = new Array();
    raesdList = sforce.connection.query("select Id, Fixture_Model_No_F__c, Product_Status_Flag_F__c"
            + " from Rental_Apply_Equipment_Set_Detail__c where Rental_Apply__c ='{!Rental_Apply__c.Id}'");
    raesdList = raesdList.getArray("records");
    var modelSet = new Set();
    var stoppedSet = new Set();
    for(var i=0;i<raesdList.length;i++){
        modelSet.add(raesdList[i].Fixture_Model_No_F__c);
        if('false' == raesdList[i].Product_Status_Flag_F__c 
            && ('{!Rental_Apply__c.demo_purpose2__c}' == '试用(有询价)' || '{!Rental_Apply__c.demo_purpose2__c}' == '试用(无询价)'))
             {
            stoppedSet.add(raesdList[i].Fixture_Model_No_F__c);
        }
    }
    if(stoppedSet.size> 0) {
        alert( Array.from(stoppedSet).join(',') + ' 产品注册证状态为停止,不可申请');
        return;
    }
    if('{!Rental_Apply__c.demo_purpose2__c}' == '索赔QIS'){
        var DeliveryGood = new Array();
        DeliveryGood = sforce.connection.query("select Id, nonyushohin__r.Product2.Fixture_Model_No_T__c"
            + " from QIS_Report__c where id ='{!JSENCODE(Rental_Apply__c.QIS_numberId__c)}'");
        var records= DeliveryGood.getArray("records");
        if(records.length == 0 || !modelSet.has(records[0].nonyushohin__r.Product2.Fixture_Model_No_T__c)){
            alert('申请的型号必须与QIS申请型号一致');
            return;
        }
    }
    if('{!Rental_Apply__c.Repair__c}' != ''){
        var DeliveryGood = new Array();
        DeliveryGood = sforce.connection.query("select Id, Repair_Rank__c, DW_Sign_Txt__c, FSE_ApplyForRepair_Day__c, Delivered_Product__r.Product2.Fixture_Model_No_T__c"
            + " , ReRepairObject_F__c,Status1__c,Agreed_Date__c,Repair_Estimated_date_formula__c,Repair_Ordered_Date__c"
            + " , IfCheckFixture__c"
            + " , Repair_Final_Inspection_Date__c,Repair_Shipped_Date__c,Number_of_EffectiveContract__c, NewProductGuaranteeObject__c, Delivered_Product__r.Product2.Asset_Model_No__c"
            + " from Repair__c where id ='{!JSENCODE(Rental_Apply__c.RepairId__c)}'");
        // 修理Data records[0]
        var records= DeliveryGood.getArray("records");
        if('{!Rental_Apply__c.Demo_purpose1__c}'  == '维修代用' && '{!Rental_Apply__c.demo_purpose2__c}' != '索赔QIS') {
             if(!modelSet.has(records[0].Delivered_Product__r.Product2.Fixture_Model_No_T__c)){
                alert('申请的型号必须与送修的型号一致');
                return;
             }
        }
        if('{!Rental_Apply__c.Demo_purpose1__c}'  == '维修代用' && '{!Rental_Apply__c.demo_purpose2__c}'  == '一般用户' ){
            if (records[0].Repair_Estimated_date_formula__c == null) {
                alert('一般维修无报价日,不可借用备品');
                return;
            }
            else if (records[0].Repair_Estimated_date_formula__c < '2019-07-01' && records[0].Agreed_Date__c == null) {
                alert('报价日在2019/7/1之前且户同意日为空,不可借用备品');
                return;
            }
			//20210608 ljh SFDC-C3CCN4 start
			if(records[0].Repair_Rank__c  == '' || records[0].Repair_Rank__c  == null){
				alert('报价等级为空不能申请备品');
				return;
			}else{
				if(records[0].DW_Sign_Txt__c == 'false' && records[0].Repair_Rank__c == 'DW'){
					alert('DW报价等级下此型号不符合备品申请借用条件');
					return;
				}
				if(records[0].Repair_Rank__c == 'D1'
				||records[0].Repair_Rank__c == 'D2'
				||records[0].Repair_Rank__c == 'D3'
				||records[0].Repair_Rank__c == 'E2'){
					alert('报价等级不符合备品申请借用条件');
					return;
				}
			}
			//20210608 ljh SFDC-C3CCN4 end
        }
        if('{!Rental_Apply__c.Demo_purpose1__c}'  == '维修代用' && '{!Rental_Apply__c.demo_purpose2__c}'  == '市场多年保修' ){
            if (records[0].FSE_ApplyForRepair_Day__c == null) {
                alert('市场多年保修,没有[FSE修理申请日],不可借用备品');
                return;
            }
        }
        if('{!Rental_Apply__c.Demo_purpose1__c}'  == '维修代用' && '{!Rental_Apply__c.demo_purpose2__c}'  == '故障排查' ){
            if (records[0].FSE_ApplyForRepair_Day__c == null) {
                alert('故障排查,没有[FSE修理申请日],不可借用备品');
                return;
            }
            if(records[0].Repair_Ordered_Date__c != null) {
                alert('故障排查,[4.修理品RC受理日]必须为空');
                return;
            }
            if(records[0].IfCheckFixture__c == 'false'){
                alert('不满足故障排查目的');
                return;
            }
        }
		
        if ('{!Rental_Apply__c.RecordTypeId}' != "01210000000RHIn"
                && '{!$Profile.Id}' != '00e10000000Y3o5'
                && records[0].NewProductGuaranteeObject__c == '8: 市场多年保修'
                && '{!Rental_Apply__c.demo_purpose2__c}' != '市场多年保修') {
            alert('无偿区别标志为8: 市场多年保修,必须选择市场多年保修。');
        }
        if(records[0].Repair_Final_Inspection_Date__c != null){
            alert('存在修理最终检测日,不可借用备品');
            return;
        }
        if(records[0].Repair_Shipped_Date__c != null){
            alert('存在RC修理返送日,不可借用备品');
            return;
        }
        if(records[0].Status1__c =='0.删除' ||records[0].Status1__c =='0.取消' ||records[0].Status1__c =='5.完毕' ){
            alert('修理已经结束,不能申请备品');
            return;
        }
        if ( '{!Rental_Apply__c.demo_purpose2__c}'  == '再修理' && records[0].ReRepairObject_F__c == 'false') {
            alert('不属于再受理参考对象,不可借用备品');
            return;
        }
        if ('{!Rental_Apply__c.RecordTypeId}' != '01210000000RHIn'
                && '{!Rental_Apply__c.demo_purpose2__c}' != '保修用户'
                && '{!Rental_Apply__c.demo_purpose2__c}' != '市场多年保修'
                && records[0].Number_of_EffectiveContract__c == '有' ) {
            alert('有维修合同,必须选择保修用户.');
            return;
        }
        var AssetModelNo = records[0].Delivered_Product__r.Product2.Asset_Model_No__c;
        if ('{!Rental_Apply__c.RecordTypeId}' != '01210000000RHIn' && records[0].Number_of_EffectiveContract__c == '无'
                && (records[0].NewProductGuaranteeObject__c == '2: 服务多年保修'
                && (AssetModelNo == 'LTF-190-10-3D' || AssetModelNo == 'LTF-S190-5' || AssetModelNo == 'CYF-VHA' || AssetModelNo == 'CYF-VA2' || AssetModelNo == 'CYF-5A'|| AssetModelNo == 'LTF-S190-10'|| AssetModelNo == 'OER-AW'|| AssetModelNo == 'URF-V'|| AssetModelNo == 'URF-V2'|| AssetModelNo == 'URF-P6'))
                && '{!Rental_Apply__c.demo_purpose2__c}' != '保修用户' ) {
            alert('此设备型号多年保修,请选择保修用户.');
            return;
        }
        if ('{!Rental_Apply__c.RecordTypeId}' != '01210000000RHIn'
                && records[0].NewProductGuaranteeObject__c == '2: 服务多年保修'
                && (AssetModelNo == 'CV-V1' || AssetModelNo == 'CV-V1(A)' || AssetModelNo == 'CV-V1(B)' || AssetModelNo == 'GIF-LV1' || AssetModelNo == 'CF-LV1L' || AssetModelNo == 'CF-LV1I' || AssetModelNo == 'MAJ-1910')
                && ('{!Rental_Apply__c.demo_purpose2__c}' == '一般用户' || '{!Rental_Apply__c.demo_purpose2__c}' == '再修理')
        ) {
            alert('奥辉设备,保修期内不提供备品.');
            return;
        }
    }
    if ('{!Rental_Apply__c.SupplementCreated__c}' == '1' && '{!Rental_Apply__c.OPDPlan__c}' != '') {
        var raId = "{!Rental_Apply__c.Id}";
        var raesCountCheck = sforce.apex.execute("OpdPlanWebService", "raesCountCheck", {rentalApplyId: raId});
        if(raesCountCheck != 'OK'){
            alert(raesCountCheck);
            return;
        }
    }
    if('{!Rental_Apply__c.Campaign__c}' != null && '{!Rental_Apply__c.Campaign__c}' != ''){
        var DeliveryGood = new Array();
        DeliveryGood = sforce.connection.query("select Status, Rental_Apply_Flag__c from Campaign where id ='{!Rental_Apply__c.CampaignId__c}'");
        var records= DeliveryGood.getArray("records");
        var interval = records[0].Status;
        var records_Date =  records[0].Rental_Apply_Flag__c;
        if (interval == '草案中') {
             alert('学会状态为草案中,不能提交');
             return;
        }
        if (interval == '申请中') {
             alert('学会状态为申请中,不能提交');
             return;
        }
        if (interval == '已结束') {
             alert('学会状态为已结束,不能提交');
             return;
        }
        if (interval == '已提交报告') {
             alert('学会状态为已提交报告,不能提交');
             return;
        }
        if (interval == '取消申请中') {
             alert('学会状态为取消申请中,不能提交');
             return;
        }
        if (interval == '取消') {
             alert('学会状态为取消,不能提交');
             return;
        }
        if ('{!TODAY()}' >= '{!Rental_Apply__c.Request_shipping_day__c}' -7) {
             alert("必须提前于希望到货日7天以上提交申请");
             return;
        }
    }
    if('{!Rental_Apply__c.QIS_number__c}' != null && '{!Rental_Apply__c.QIS_number__c}' != ''){
        var DeliveryGood = new Array();
        DeliveryGood = sforce.connection.query("select id,next_action__c from QIS_report__c where id ='{!Rental_Apply__c.QIS_ID_Line__c}'");
        var records= DeliveryGood.getArray("records");
        var interval = records[0].next_action__c; 
        if (interval == '送回') {
            alert("QIS 已送回,不能再申请备品了");
            return;
        }
    }
    // share
    var userAccess = new Array();
    userAccess.push('{!Rental_Apply__c.applyUserId__c}_Edit');
   //   SWAG-BZMA8W 去掉服务经理部长的共享 start 
   //update lxy DLIU-BZNBG9 去掉服务经理部长共享
   //userAccess.push('{!Rental_Apply__c.JingliApprovalManagerId__c}_Read');
    // userAccess.push('{!Rental_Apply__c.SalesManagerId__c}_Read');//20210727 ljh SFDC-C54C33 update 共享触发器完成
    //update lxy DLIU-BZNBG9 去掉服务经理部长共享
   //userAccess.push('{!Rental_Apply__c.BuchangApprovalManagerId__c}_Read');
   //   SWAG-BZMA8W 去掉服务经理部长的共享 end
    //userAccess.push('{!Rental_Apply__c.BuchangApprovalManagerSalesId__c}_Read');//20210727 ljh SFDC-C54C33 update 共享触发器完成
    //userAccess.push('{!Rental_Apply__c.ZongjianApprovalManagerId__c}_Read');//20210727 ljh SFDC-C54C33 update 共享触发器完成
    //userAccess.push('{!Rental_Apply__c.Status__c}_Read');
    var rtn = sforce.apex.execute('ControllerUtil','setSObjectShare',{sobjectName:'Rental_Apply__Share',rowCause:'ApplyUserShare__c',parentId:'{!Rental_Apply__c.Id}',userAccess:userAccess,ownerId:'{!Rental_Apply__c.OwnerId}'});
    if (rtn != 'OK') {
        alert(rtn);
        return;
    }
    //追加备品申请状态确认,已经提交过的申请,不能重复提交Status__c
    if ('{!Rental_Apply__c.Status__c}' == '填写完毕' ||
        '{!Rental_Apply__c.Status__c}' == '申请中' ||
        '{!Rental_Apply__c.Status__c}' == '已批准' ||
        //现在申请书的Status__c已经没有引当完了状态。所以这里不需要判断
        //'{!Rental_Apply__c.Status__c}' == '引当完了' ||
        '{!Rental_Apply__c.Status__c}' == '已出库指示' ||
        '{!Rental_Apply__c.Status__c}' == '删除' ||
        '{!Rental_Apply__c.Status__c}' == '取消' ) {
        alert('请备品申请状态确认,已经提交过的申请,不能重复提交');
        return;
    }
        // 没有明细的一览check
    var raesList = new Array(); 
    raesList = sforce.connection.query("select Id from Rental_Apply_Equipment_Set__c where RetalFSetDetail_Cnt__c = 0 AND Rental_Apply__c ='{!Rental_Apply__c.Id}'");
    var records= raesList.getArray('records'); 
    if(records.length > 0){
        alert('有没有明细的借出备品配套一览,不能提交');
        return;
    }
    //2022-2-23 yjk 查询Rental_Apply__c 对象以获得SalesManager__c 的值 start
    var racs = sforce.connection.query("select id,Status__c, SalesManager__c from Rental_Apply__c where id = '{!Rental_Apply__c.Id}'");
    var rac = racs.getArray("records")[0];
    //2022-2-23 yjk 查询Rental_Apply__c 对象以获得SalesManager__c 的值 end
    rac.Status__c = "填写完毕";
    //2022-2-23 yjk 提交时将经理的Name字段赋给备品提交时经理(文本)字段 start
    var manageUsers = sforce.connection.query("select id,name from User where id = '"+rac.SalesManager__c +"'");
    rac.SalesManagerSubmit__c = manageUsers.getArray("records")[0].Name; 
    //2022-2-23 yjk 提交时将经理的Name字段赋给备品提交时经理(文本)字段 end
    var result = sforce.connection.update([rac]);
    var messages = getConnectDMLErrorMessages(result);
    if (messages.length > 0) {
        alert(messages.join("\n"));
        return;
    }
    window.location.href = window.location;
};
foo();