You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							303 lines
						
					
					
						
							8.8 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							303 lines
						
					
					
						
							8.8 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Http\Controllers\Api;
							 | 
						|
								
							 | 
						|
								use App\Common\PayType;
							 | 
						|
								use App\Common\StatementType;
							 | 
						|
								use App\Http\Controllers\Controller;
							 | 
						|
								use App\Models\Agent;
							 | 
						|
								use App\Models\Guide;
							 | 
						|
								use App\Models\IndustryOrder;
							 | 
						|
								use App\Models\Order;
							 | 
						|
								use App\Models\Supplier;
							 | 
						|
								use App\Models\Product;
							 | 
						|
								use App\Models\OrderProductItem;
							 | 
						|
								use App\Models\User;
							 | 
						|
								use App\Common\OrderStatus;
							 | 
						|
								use App\Service\WithdrawalService;
							 | 
						|
								use App\Traits\DemandTraits;
							 | 
						|
								use App\Traits\StatementTraits;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 * 订单核销
							 | 
						|
								 * Class VerificationController
							 | 
						|
								 * @package App\Http\Controllers\Api
							 | 
						|
								 */
							 | 
						|
								class VerificationController extends Controller
							 | 
						|
								{
							 | 
						|
									//核销小程序订单
							 | 
						|
									public function verify()
							 | 
						|
									{
							 | 
						|
										$input_verify_code = request()->input('verify_code'); //核销码
							 | 
						|
								
							 | 
						|
										$code_arr = explode('-', $input_verify_code);
							 | 
						|
										if (count($code_arr) != 2) {
							 | 
						|
											return $this->error('参数错误');
							 | 
						|
										}
							 | 
						|
										list($id, $verify_code) = $code_arr;
							 | 
						|
										$order = Order::where(['verify_code' => $verify_code])
							 | 
						|
											->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID, OrderStatus::REFUSED_REFUND])
							 | 
						|
											->find($id);
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error($input_verify_code . "核销码不存在或订单状态不允许核销");
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$mobile = User::where('id', $this->user_id)->value('mobile');
							 | 
						|
								
							 | 
						|
										$checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist();
							 | 
						|
										if ($checkMobile) {
							 | 
						|
											return $this->error('对不起,你没有核销权限,请联系管理员');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$order->status = OrderStatus::SUCCESS;
							 | 
						|
										if ($order->save()) {
							 | 
						|
								
							 | 
						|
											//分账
							 | 
						|
											//线下订单不分账
							 | 
						|
											if ($order->pay_type != PayType::OFFLINE) {
							 | 
						|
												$this->fund($order);
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										return $this->success();
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//行业产品订单核销
							 | 
						|
									public function industry_verify()
							 | 
						|
									{
							 | 
						|
										$input_verify_code = request()->input('verify_code'); //核销码
							 | 
						|
								
							 | 
						|
										$code_arr = explode('-', $input_verify_code);
							 | 
						|
										if (count($code_arr) != 2) {
							 | 
						|
											return $this->error('参数错误');
							 | 
						|
										}
							 | 
						|
										list($id, $verify_code) = $code_arr;
							 | 
						|
								
							 | 
						|
										$order = IndustryOrder::with('industryProduct:id,verify_mobile')
							 | 
						|
											->where(['status' => OrderStatus::OFFLINE_PAID, 'verify_code' => $verify_code])->find($id);
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error($input_verify_code . "核销码不存在或订单状态不允许核销");
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$user = User::find($this->user_id);
							 | 
						|
										if (!$user->mobile) {
							 | 
						|
											return $this->error('手机号与核销手机号不一致');
							 | 
						|
										} else if ($user->mobile != $order->industryProduct->verify_mobile) {
							 | 
						|
											return $this->error('对不起,你没有该订单的核销权限');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										DB::beginTransaction();
							 | 
						|
										try {
							 | 
						|
											//改变订单状态为已完成
							 | 
						|
											$order->status = OrderStatus::SUCCESS;
							 | 
						|
											$order->save();
							 | 
						|
								
							 | 
						|
											//扣除供应商的交易金
							 | 
						|
											$supplier = Supplier::find($order->supplier_id);
							 | 
						|
											$supplier->trade_balance = $supplier->trade_balance - $order->trade_deposit;
							 | 
						|
											$supplier->save(); //需要用save才能执行模型事件记录日志
							 | 
						|
								
							 | 
						|
											DB::commit();
							 | 
						|
											return $this->success('核销成功');
							 | 
						|
										} catch (\Exception $e) {
							 | 
						|
											DB::rollBack();
							 | 
						|
											return $this->error($e->getMessage());
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									public function fund($order)
							 | 
						|
									{
							 | 
						|
										$service = new WithdrawalService();
							 | 
						|
										DB::beginTransaction();
							 | 
						|
										try {
							 | 
						|
											//最后批量插入
							 | 
						|
											$statementCreate = [];
							 | 
						|
											$cost = 0;
							 | 
						|
											//如果有地接价格 分帐给地接
							 | 
						|
											if ($order->guide_price > 0) {
							 | 
						|
												$guidePrice = $order->guide_price;
							 | 
						|
												$cost = bcadd($cost, $order->guide_price, 6);
							 | 
						|
												//成本价 加上地接价格
							 | 
						|
												$statementCreate[] = $service->createByOrder(
							 | 
						|
													$order->guide_price,
							 | 
						|
													StatementType::ORDER,
							 | 
						|
													$order->guide->id,
							 | 
						|
													DemandTraits::$col[2],
							 | 
						|
													$order->id,
							 | 
						|
													StatementTraits::$type[0]
							 | 
						|
												);
							 | 
						|
												//抽成
							 | 
						|
												//if ($order->guide->rate > 0) {
							 | 
						|
												//	//计算抽成金额
							 | 
						|
												//	$guideCut = bcmul($order->guide_price, $order->guide->rate, 6);
							 | 
						|
												//	$cutPrice = $guideCut > 0 ? bcdiv($guideCut, 100, 6) : 0;
							 | 
						|
												//	//总后台抽成流水
							 | 
						|
												//	if ($cutPrice > 0) {
							 | 
						|
												//		$adminCreate[] = $service->createByOrderFormAdmin(
							 | 
						|
												//			$cutPrice,
							 | 
						|
												//			StatementType::CUT,
							 | 
						|
												//			$order->guide->id,
							 | 
						|
												//			DemandTraits::$col[2],
							 | 
						|
												//			$order->id,
							 | 
						|
												//		);
							 | 
						|
												//		//地接被抽成流水
							 | 
						|
												//		$statementCreate[] = $service->createByOrder(
							 | 
						|
												//			bcmul($cutPrice, -1, 2),
							 | 
						|
												//			StatementType::CUT,
							 | 
						|
												//			$order->guide->id,
							 | 
						|
												//			DemandTraits::$col[2],
							 | 
						|
												//			$order->id,
							 | 
						|
												//			StatementTraits::$type[0]
							 | 
						|
												//		);
							 | 
						|
												//$guidePrice = bcsub($order->guide_price, $cutPrice, 6);
							 | 
						|
												$guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
							 | 
						|
												$guide->balance = bcadd($guide->balance, $guidePrice, 6);
							 | 
						|
												$guide->save();
							 | 
						|
												//	}
							 | 
						|
												//}
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//分账给供应商
							 | 
						|
											$orderItem = OrderProductItem::query()
							 | 
						|
												->where('order_id', $order->id)
							 | 
						|
												->with('supplier')
							 | 
						|
												->select('*')
							 | 
						|
												->selectRaw('sum(price) as sum_price,sum(service_persons * num) as sum_persons')
							 | 
						|
												->groupBy('supplier_id')
							 | 
						|
												->get();
							 | 
						|
											foreach ($orderItem as $v) {
							 | 
						|
								
							 | 
						|
												$cost = bcadd($cost, $v->sum_price, 6);
							 | 
						|
												$supplierPrice = $v->sum_price;
							 | 
						|
												$statementCreate[] = $service->createByOrder(
							 | 
						|
													$v->sum_price,
							 | 
						|
													StatementType::ORDER,
							 | 
						|
													$v->supplier_id,
							 | 
						|
													DemandTraits::$col[1],
							 | 
						|
													$order->id,
							 | 
						|
													StatementTraits::$type[0]
							 | 
						|
												);
							 | 
						|
								
							 | 
						|
												//if ($v->supplier->rate > 0) {
							 | 
						|
												//	//计算抽成金额
							 | 
						|
												//	$supplierCut = bcmul($v->sum_price, $v->supplier->rate, 6);
							 | 
						|
												//	$cutPrice = $supplierCut > 0 ? bcdiv($supplierCut, 100, 6) : 0;
							 | 
						|
												//	if ($cutPrice > 0) {
							 | 
						|
												//		//总后台抽成流水
							 | 
						|
												//		$adminCreate[] = $service->createByOrderFormAdmin(
							 | 
						|
												//			$cutPrice,
							 | 
						|
												//			StatementType::CUT,
							 | 
						|
												//			$v->supplier_id,
							 | 
						|
												//			DemandTraits::$col[1],
							 | 
						|
												//			$order->id,
							 | 
						|
												//		);
							 | 
						|
												//		//供应商被抽成流水
							 | 
						|
												//		$statementCreate[] = $service->createByOrder(
							 | 
						|
												//			bcmul($cutPrice, -1, 6),
							 | 
						|
												//			StatementType::CUT,
							 | 
						|
												//			$v->supplier_id,
							 | 
						|
												//			DemandTraits::$col[1],
							 | 
						|
												//			$order->id,
							 | 
						|
												//			StatementTraits::$type[0]
							 | 
						|
												//		);
							 | 
						|
												//		$supplierPrice = bcsub($supplierPrice, $cutPrice, 6);
							 | 
						|
												//
							 | 
						|
												//	}
							 | 
						|
												//}
							 | 
						|
								
							 | 
						|
												$supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
							 | 
						|
												//处理交易金
							 | 
						|
												if ($order->single_price > 0 && $v->sum_persons > 0) {
							 | 
						|
													//计算交易金
							 | 
						|
													$deposit = bcmul($order->single_price,$v->sum_persons,6);
							 | 
						|
													//流水
							 | 
						|
													$statementCreate[] = $service->createByOrder(
							 | 
						|
																bcmul($deposit, -1, 6),
							 | 
						|
																StatementType::DEPOSIT,
							 | 
						|
																$v->supplier_id,
							 | 
						|
																DemandTraits::$col[1],
							 | 
						|
																$order->id,
							 | 
						|
																StatementTraits::$type[0]
							 | 
						|
															);
							 | 
						|
													//扣
							 | 
						|
													$supplier->balance = bcsub($supplier->balance,$deposit,6);
							 | 
						|
													//$supplier->balance = bcadd($supplier->deposit_used, $supplierPrice, 6);
							 | 
						|
													//$supplier->balance = bcsub($supplier->deposit_frozen, $supplierPrice, 6);
							 | 
						|
												}
							 | 
						|
								
							 | 
						|
												$supplier->balance = bcadd($supplier->balance, $supplierPrice, 6);
							 | 
						|
												$supplier->save();
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//分账给代理商
							 | 
						|
											//成本价 加上地接价格
							 | 
						|
											$agentPrice = bcsub($order->price, $cost, 2);
							 | 
						|
								
							 | 
						|
								
							 | 
						|
											$statementCreate[] = $service->createByOrder(
							 | 
						|
												$agentPrice,
							 | 
						|
												StatementType::ORDER,
							 | 
						|
												$order->agent_id,
							 | 
						|
												DemandTraits::$col[0],
							 | 
						|
												$order->id,
							 | 
						|
												StatementTraits::$type[0]
							 | 
						|
											);
							 | 
						|
												//
							 | 
						|
												////抽成
							 | 
						|
												//if ($order->agent->rate > 0) {
							 | 
						|
												//	//计算抽成金额
							 | 
						|
												//	$agentCut = bcmul($agentPrice, $order->agent->rate, 6);
							 | 
						|
												//	$cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
							 | 
						|
												//
							 | 
						|
												//	//总后台抽成流水
							 | 
						|
												//	if ($cutPrice > 0) {
							 | 
						|
												//		$adminCreate[] = $service->createByOrderFormAdmin(
							 | 
						|
												//			$cutPrice,
							 | 
						|
												//			StatementType::CUT,
							 | 
						|
												//			$order->agent->id,
							 | 
						|
												//			DemandTraits::$col[0],
							 | 
						|
												//			$order->id,
							 | 
						|
												//		);
							 | 
						|
												//		//代理商被抽成流水
							 | 
						|
												//		$statementCreate[] = $service->createByOrder(
							 | 
						|
												//			bcmul($cutPrice, -1, 6),
							 | 
						|
												//			StatementType::CUT,
							 | 
						|
												//			$order->agent->id,
							 | 
						|
												//			DemandTraits::$col[0],
							 | 
						|
												//			$order->id,
							 | 
						|
												//			StatementTraits::$type[0]
							 | 
						|
												//		);
							 | 
						|
												//		$agentPrice = bcsub($agentPrice, $cutPrice, 6);
							 | 
						|
												//	}
							 | 
						|
								
							 | 
						|
								
							 | 
						|
												//扣除微信支付手续费
							 | 
						|
												//$chargePrice = bcmul($order->price, 0.006, 6);
							 | 
						|
												//$statementCreate[] = $service->createByOrder(
							 | 
						|
												//	bcmul($chargePrice, -1, 6),
							 | 
						|
												//	StatementType::CHARGE,
							 | 
						|
												//	$order->agent_id,
							 | 
						|
												//	DemandTraits::$col[0],
							 | 
						|
												//	$order->id,
							 | 
						|
												//	StatementTraits::$type[0]
							 | 
						|
												//);
							 | 
						|
												//$agentPrice = bcsub($agentPrice, $chargePrice, 6);
							 | 
						|
												$agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
							 | 
						|
												$agent->balance = bcadd($agent->balance, $agentPrice, 6);
							 | 
						|
												$agent->save();
							 | 
						|
											//}
							 | 
						|
											//if (!empty($adminCreate)) {
							 | 
						|
											//	$order->statementAdmin()->createMany($adminCreate);
							 | 
						|
											//}
							 | 
						|
								
							 | 
						|
											if (!empty($statementCreate)) {
							 | 
						|
												$order->statement()->createMany($statementCreate);
							 | 
						|
											}
							 | 
						|
											DB::commit();
							 | 
						|
										} catch (\Exception $e) {
							 | 
						|
											DB::rollBack();
							 | 
						|
											return $this->error($e->getMessage());
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
								}
							 |