You've already forked qlg.tsgz.moe
							
							Init Repo
This commit is contained in:
		
							
								
								
									
										297
									
								
								hyhproject/common/model/OrderRefunds.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										297
									
								
								hyhproject/common/model/OrderRefunds.php
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,297 @@ | ||||
| <?php | ||||
| namespace wstmart\common\model; | ||||
| use think\Db; | ||||
| /** | ||||
|  * ============================================================================ | ||||
|  * 退款业务处理类 | ||||
|  */ | ||||
| class OrderRefunds extends Base{ | ||||
| 	/** | ||||
| 	 * 用户申请退款 | ||||
| 	 */ | ||||
| 	public function refund($uId=0){ | ||||
| 		$orderId = (int)input('post.id'); | ||||
| 		$reason = (int)input('post.reason'); | ||||
| 		$content = input('post.content'); | ||||
|  | ||||
| 		//$money = (float)input('post.money'); | ||||
| 		$userId = ($uId==0)?(int)session('WST_USER.userId'):$uId; | ||||
| 		//if($money<=0)return WSTReturn("请填写退款金额"); | ||||
| 		$order = Db::name('orders')->alias('o')->join('__ORDER_REFUNDS__ orf','orf.orderId=o.orderId','left')->join('__SHOPS__ s','o.shopId=s.shopId','left') | ||||
| 		           ->where(['o.userId'=>$userId,'o.orderId'=>$orderId,'o.orderStatus'=>['in',[-3,-1]]]) | ||||
| 		           ->field('o.orderId,s.userId,o.shopId,o.orderStatus,o.payFrom,o.orderNo,o.realTotalMoney,o.isPay,o.productNum,o.couponsNum,o.wangNum,o.payType,o.useScore,orf.id refundId')->find(); | ||||
| 		$productNum = (float)input('post.productNum'); | ||||
| 		$couponsNum = (float)input('post.couponsNum'); | ||||
| 		$wangNum = (float)input('post.wangNum'); | ||||
| 		if($productNum > $order['productNum']){ | ||||
| 			return WSTReturn("最多可退产品券:".$order['productNum']); | ||||
| 		} | ||||
| 		if($couponsNum > $order['couponsNum']){ | ||||
| 			return WSTReturn("最多可退优惠券:".$order['couponsNum']); | ||||
| 		} | ||||
| 		if($wangNum > $order['wangNum']){ | ||||
| 			return WSTReturn("最多可退旺旺券:".$order['wangNum']); | ||||
| 		} | ||||
| 		$money = $productNum + $couponsNum + $wangNum; | ||||
| 		$reasonData = WSTDatas('REFUND_TYPE',$reason); | ||||
| 		if(empty($reasonData))return WSTReturn("无效的退款原因"); | ||||
| 		if($reason==10000 && $content=='')return WSTReturn("请输入退款原因"); | ||||
| 		if(empty($order))return WSTReturn('操作失败,请检查订单状态是否已改变'); | ||||
| 		$allowRequest = false; | ||||
| 		if($order['isPay']==1 || ($order['payType']==0 && $order['useScore']>0)){ | ||||
| 			$allowRequest = true; | ||||
| 		} | ||||
| 		if(!$allowRequest)return WSTReturn("您的退款申请已提交,请留意退款信息"); | ||||
| 		// if($money>$order['realTotalMoney'])return WSTReturn("申请退款金额不能大于实支付金额"); | ||||
| 		//查看退款申请是否已存在 | ||||
| 		$orfId = $this->where('orderId',$orderId)->value('id'); | ||||
| 		// orderStatus:-3:用户拒收 -2:未付款的订单 -1:用户取消 0:待发货 1:配送中 2:用户确认收货 | ||||
| 		Db::startTrans(); | ||||
| 		try{ | ||||
| 			$result = false; | ||||
| 			//如果退款单存在就进行编辑 | ||||
| 			if($orfId>0){ | ||||
| 				$object = $this->get($orfId); | ||||
| 				$object->refundReson = $reason; | ||||
| 				if($reason==10000)$object->refundOtherReson = $content; | ||||
| 				$object->backMoney = $money; | ||||
| 				$object->backProductNum = $productNum; | ||||
| 				$object->backCouponsNum = $couponsNum; | ||||
| 				$object->backWangNum = $wangNum; | ||||
| 				$object->refundStatus = 0; | ||||
| 				$result = $object->save(); | ||||
| 			}else{ | ||||
| 				$data = []; | ||||
| 				$data['orderId'] = $orderId;	 | ||||
| 	            $data['refundTo'] = 0; | ||||
| 	            $data['refundReson'] = $reason; | ||||
| 	            if($reason==10000)$data['refundOtherReson'] = $content; | ||||
| 	            $data['backMoney'] = $money; | ||||
| 	            $data['backProductNum'] = $productNum; | ||||
| 				$data['backCouponsNum'] = $couponsNum; | ||||
| 	            $data['backWangNum'] = $wangNum; | ||||
| 	            $data['createTime'] = date('Y-m-d H:i:s'); | ||||
| 	            $data['refundStatus'] = ($order['orderStatus']==-1)?1:0; | ||||
| 	            $result = $this->save($data); | ||||
| 			}			 | ||||
|             if(false !== $result){ | ||||
|             	//拒收、取消申请退款的话要给商家发送信息 | ||||
|             	if($order['orderStatus']!=-1){ | ||||
|             		$tpl = WSTMsgTemplates('ORDER_REFUND_CONFER'); | ||||
| 	                if( $tpl['tplContent']!='' && $tpl['status']=='1'){ | ||||
| 	                    $find = ['${ORDER_NO}']; | ||||
| 	                    $replace = [$order['orderNo']]; | ||||
| 	                     | ||||
| 	                	$msg = array(); | ||||
| 			            $msg["shopId"] = $order['shopId']; | ||||
| 			            $msg["tplCode"] = $tpl["tplCode"]; | ||||
| 			            $msg["msgType"] = 1; | ||||
| 			            $msg["content"] = str_replace($find,$replace,$tpl['tplContent']); | ||||
| 			            $msg["msgJson"] = ['from'=>1,'dataId'=>$orderId]; | ||||
| 			            model("common/MessageQueues")->add($msg); | ||||
| 	                } | ||||
| 	                  | ||||
| 	                //微信消息 | ||||
| 					if((int)WSTConf('CONF.wxenabled')==1){ | ||||
| 						$params = []; | ||||
| 						$params['ORDER_NO'] = $order['orderNo']; | ||||
| 					    $params['REASON'] = $reasonData['dataName'].(($reason==10000)?" - ".$content:"");              | ||||
| 						$params['MONEY'] = $money.(($order['useScore']>0)?("【退回积分:".$order['useScore']."】"):""); | ||||
| 				        | ||||
| 						$msg = array(); | ||||
| 						$tplCode = "WX_ORDER_REFUND_CONFER"; | ||||
| 						$msg["shopId"] = $order['shopId']; | ||||
| 			            $msg["tplCode"] = $tplCode; | ||||
| 			            $msg["msgType"] = 4; | ||||
| 			            $msg["paramJson"] = ['CODE'=>$tplCode,'URL'=>Url('wechat/orders/sellerorder','',true,true),'params'=>$params]; | ||||
| 			            $msg["msgJson"] = ""; | ||||
| 			            model("common/MessageQueues")->add($msg); | ||||
| 					}  | ||||
| 			    }else{ | ||||
| 			    	//判断是否需要发送管理员短信 | ||||
| 					$tpl = WSTMsgTemplates('PHONE_ADMIN_REFUND_ORDER'); | ||||
| 					if((int)WSTConf('CONF.smsOpen')==1 && (int)WSTConf('CONF.smsRefundOrderTip')==1 &&  $tpl['tplContent']!='' && $tpl['status']=='1'){ | ||||
| 						$params = ['tpl'=>$tpl,'params'=>['ORDER_NO'=>$order['orderNo']]]; | ||||
| 						$staffs = Db::name('staffs')->where(['staffId'=>['in',explode(',',WSTConf('CONF.refundOrderTipUsers'))],'staffStatus'=>1,'dataFlag'=>1])->field('staffPhone')->select(); | ||||
| 						for($i=0;$i<count($staffs);$i++){ | ||||
| 							if($staffs[$i]['staffPhone']=='')continue; | ||||
| 							$m = new LogSms(); | ||||
| 							$rv = $m->sendAdminSMS(0,$staffs[$i]['staffPhone'],$params,'refund',''); | ||||
| 						} | ||||
| 					} | ||||
| 					//微信消息 | ||||
| 					if((int)WSTConf('CONF.wxenabled')==1){ | ||||
| 						//判断是否需要发送给管理员消息 | ||||
| 		                if((int)WSTConf('CONF.wxRefundOrderTip')==1){ | ||||
| 		                	$params = []; | ||||
| 						    $params['ORDER_NO'] = $order['orderNo']; | ||||
| 					        $params['REASON'] = $reasonData['dataName'].(($reason==10000)?" - ".$content:"");              | ||||
| 						    $params['MONEY'] = $money.(($order['useScore']>0)?("【退回积分:".$order['useScore']."】"):""); | ||||
| 			            	WSTWxBatchMessage(['CODE'=>'WX_ADMIN_ORDER_REFUND','userType'=>3,'userId'=>explode(',',WSTConf('CONF.refundOrderTipUsers')),'params'=>$params]); | ||||
| 		                } | ||||
| 					} | ||||
| 			    } | ||||
|             	Db::commit(); | ||||
|                 return WSTReturn('您的退款申请已提交,请留意退款信息',1); | ||||
|             } | ||||
| 		}catch (\Exception $e) { | ||||
| 		    Db::rollback();errLog($e); | ||||
| 	    } | ||||
| 	    return WSTReturn('操作失败',-1); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * 获取订单价格以及申请退款价格 | ||||
| 	 */ | ||||
| 	public function getRefundMoneyByOrder($orderId = 0){ | ||||
| 		$result= Db::name('orders')->alias('o')->join('__ORDER_REFUNDS__ orf','orf.orderId=o.orderId')->where('orf.id',$orderId) | ||||
| 				->field('o.orderId,o.payFrom,orderNo,goodsMoney,deliverMoney,useScore,scoreMoney,totalMoney,realTotalMoney,orf.backMoney')->find(); | ||||
| 		if($result['payFrom']=="ect"){ | ||||
| 			$ectPrice=db('orders_ect')->where('orderId',$result['orderId'])->value('ectPrice'); | ||||
| 			$result['ectNum']=$result['backMoney']/$ectPrice; | ||||
| 		} | ||||
| 		$result['ectNum']=isset($result['ectNum'])?$result['ectNum']:""; | ||||
| 		return $result; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * 商家处理是否同意退款 | ||||
| 	 */ | ||||
| 	public function shopRefund(){ | ||||
|         $id = (int)input('id'); | ||||
|         $refundStatus = (int)input('refundStatus'); | ||||
|         $content = input('content'); | ||||
|         if($id==0)return WSTReturn('无效的操作'); | ||||
|         if(!in_array($refundStatus,[1,-1]))return WSTReturn('无效的操作'); | ||||
|         if($refundStatus==-1 && $content=='')return WSTReturn('请输入拒绝原因'); | ||||
|         Db::startTrans(); | ||||
|         try{ | ||||
|         	//返还商品库存 | ||||
| //        	$goods=db('order_goods')->alias('og') | ||||
| //        	->join('order_refunds or','or.orderId=og.orderId','left') | ||||
| //        	->join('orders o','o.orderId=og.orderId','left') | ||||
| //        	->join('goods g','g.goodsId=og.goodsId','left') | ||||
| //        	->where('or.id',$id)->field('g.goodsId,g.goodsStock,g.saleNum,og.goodsNum,g.isSpec,o.orderCode') | ||||
| //        	->select(); | ||||
| //	        //dump($goods);die; | ||||
| //	        //返还商品库存 | ||||
| //			foreach ($goods as $key => $v){ | ||||
| //				//处理虚拟产品 | ||||
| //				if($v['orderCode']=='order'){ | ||||
| //					//修改库存 | ||||
| //					 // dump($v['isSpec']);die; | ||||
| //					if($v['isSpec']>0){ | ||||
| //				        Db::name('goods_specs')->where('id',$v['goodsSpecId'])->setInc('specStock',$v['goodsNum']); | ||||
| //				        Db::name('goods_specs')->where('id',$v['goodsSpecId'])->setDec('saleNum',$v['goodsNum']); | ||||
| //					} | ||||
| //					Db::name('goods')->where('goodsId',$v['goodsId'])->setInc('goodsStock',$v['goodsNum']); | ||||
| //					Db::name('goods')->where('goodsId',$v['goodsId'])->setDec('saleNum',$v['goodsNum']); | ||||
| //				} | ||||
| // | ||||
| //        	} | ||||
|         	$object = $this->get($id); | ||||
|         	$order = Db::name('orders')->where('orderId',$object->orderId)->field('userId,shopId,orderNo,orderId,useScore')->find(); | ||||
|             if(!$order || $order['shopId'] != input('post.shopId')){ | ||||
|             	return WSTReturn('未找到此订单'); | ||||
|             } | ||||
|             $object->refundStatus = $refundStatus; | ||||
|             if($object->refundStatus==-1)$object->shopRejectReason = $content; | ||||
|             $result = $object->save(); | ||||
|             if(false !== $result){ | ||||
|             	//如果是拒收话要给用户发信息 | ||||
|             	if($refundStatus==-1){ | ||||
|             		$tpl = WSTMsgTemplates('ORDER_REFUND_FAIL'); | ||||
| 	                if( $tpl['tplContent']!='' && $tpl['status']=='1'){ | ||||
| 	                    $find = ['${ORDER_NO}','${REASON}']; | ||||
| 	                    $replace = [$order['orderNo'],$content]; | ||||
| 	                    WSTSendMsg($order['userId'],str_replace($find,$replace,$tpl['tplContent']),['from'=>1,'dataId'=>$order['orderId']]); | ||||
| 	                }  | ||||
| 	                //微信消息 | ||||
| 					if((int)WSTConf('CONF.wxenabled')==1){ | ||||
| 						$reasonData = WSTDatas('REFUND_TYPE',$object->refundReson); | ||||
| 						$params = []; | ||||
| 						$params['ORDER_NO'] = $order['orderNo']; | ||||
| 					    $params['REASON'] = $reasonData['dataName'].(($object->refundReson==10000)?" - ".$object->refundOtherReson:""); | ||||
| 					    $params['SHOP_REASON'] = $object->shopRejectReason;              | ||||
| 						$params['MONEY'] = $object->backMoney.(($order['useScore']>0)?("【退回积分:".$order['useScore']."】"):""); | ||||
| 				        WSTWxMessage(['CODE'=>'WX_ORDER_REFUND_FAIL','userId'=>$order['userId'],'URL'=>Url('wechat/orders/index','',true,true),'params'=>$params]); | ||||
| 					}   | ||||
|             	}else{ | ||||
|             		//判断是否需要发送管理员短信 | ||||
| 					$tpl = WSTMsgTemplates('PHONE_ADMIN_REFUND_ORDER'); | ||||
| 					if((int)WSTConf('CONF.smsOpen')==1 && (int)WSTConf('CONF.smsRefundOrderTip')==1 &&  $tpl['tplContent']!='' && $tpl['status']=='1'){ | ||||
| 						$params = ['tpl'=>$tpl,'params'=>['ORDER_NO'=>$order['orderNo']]]; | ||||
| 						$staffs = Db::name('staffs')->where(['staffId'=>['in',explode(',',WSTConf('CONF.refundOrderTipUsers'))],'staffStatus'=>1,'dataFlag'=>1])->field('staffPhone')->select(); | ||||
| 						for($i=0;$i<count($staffs);$i++){ | ||||
| 							if($staffs[$i]['staffPhone']=='')continue; | ||||
| 							$m = new LogSms(); | ||||
| 							$rv = $m->sendAdminSMS(0,$staffs[$i]['staffPhone'],$params,'shoprefund',''); | ||||
| 						} | ||||
| 					} | ||||
| 					//微信消息 | ||||
| 					if((int)WSTConf('CONF.wxenabled')==1){ | ||||
| 						//判断是否需要发送给管理员消息 | ||||
| 		                if((int)WSTConf('CONF.wxRefundOrderTip')==1){ | ||||
| 		                	$reasonData = WSTDatas('REFUND_TYPE',$object->refundReson); | ||||
| 		                	$params = []; | ||||
| 						    $params['ORDER_NO'] = $order['orderNo']; | ||||
| 					        $params['REASON'] = $reasonData['dataName'].(($object->refundReson==10000)?" - ".$object->refundOtherReson:"");            | ||||
| 						    $params['MONEY'] = $object->backMoney.(($order['useScore']>0)?("【退回积分:".$order['useScore']."】"):""); | ||||
| 			            	WSTWxBatchMessage(['CODE'=>'WX_ADMIN_ORDER_REFUND','userType'=>3,'userId'=>explode(',',WSTConf('CONF.refundOrderTipUsers')),'params'=>$params]); | ||||
| 		                } | ||||
| 					} | ||||
|             	} | ||||
|             	Db::commit(); | ||||
|             	return WSTReturn('操作成功',1); | ||||
|             } | ||||
|         }catch (\Exception $e) { | ||||
| 		    Db::rollback();errLog($e); | ||||
| 	    } | ||||
| 	    return WSTReturn('操作失败',-1);    | ||||
| 	} | ||||
| 	/** | ||||
| 	 * 临时申请退款接口 | ||||
| 	 */ | ||||
| 	public function byRefund($orderNo){ | ||||
| 		//$orderNo = input('orderNo'); | ||||
| 		$order = Db::name('orders o')->where(['o.orderNo'=>$orderNo,'o.orderStatus'=>['in',[0,1]]]) | ||||
| 		           ->field('o.orderId,o.shopId,o.orderStatus,o.orderNo,o.realTotalMoney,o.isPay,o.payType,o.useScore')->find(); | ||||
| 		if(empty($order))return WSTReturn('操作失败,请检查订单状态是否已改变',-1); | ||||
| 		$allowRequest = false; | ||||
| 		if($order['isPay']==1 || ($order['payType']==0 && $order['useScore']>0)){ | ||||
| 			$allowRequest = true; | ||||
| 		} | ||||
| 		$isfind = false; | ||||
| 		if($this->where(['orderId'=>$order['orderId']])->find()){ | ||||
| 			$isfind = true; | ||||
| 		} | ||||
| 		if(!$allowRequest || $isfind)return WSTReturn("退款申请已提交,请不要重复提交",-1); | ||||
| 		$money=$order['realTotalMoney']; | ||||
| 		Db::startTrans(); | ||||
| 		try{ | ||||
| 			if($order['orderStatus']==0){ | ||||
| 				db('orders')->where('orderNo',$orderNo)->update(['orderStatus'=>-1]); | ||||
| 			} | ||||
| 			if($order['orderStatus']==1){ | ||||
| 				db('orders')->where('orderNo',$orderNo)->update(['orderStatus'=>-3]); | ||||
| 			} | ||||
| 			$result = false; | ||||
| 			//退款单进行编辑 | ||||
| 			$data = []; | ||||
| 			$data['orderId'] = $order['orderId'];	 | ||||
|             $data['refundTo'] = 0; | ||||
|             $data['refundReson'] = 5; | ||||
|             $data['backMoney'] = $money; | ||||
|             $data['createTime'] = date('Y-m-d H:i:s'); | ||||
|             $data['refundStatus'] = 1; | ||||
|             $result = $this->insert($data);			 | ||||
|             if($result){ | ||||
| 				Db::commit(); | ||||
|                 return WSTReturn('您的退款申请已提交,请留意退款信息',1); | ||||
|             } | ||||
| 		}catch (\Exception $e) { | ||||
| 		    Db::rollback();errLog($e); | ||||
| 	    } | ||||
| 	    return WSTReturn('操作失败',-1); | ||||
| 	} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user