|
|
<?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\Order;use App\Models\Supplier;use App\Models\Product;use App\Models\OrderProductItem;use App\Models\User;use App\Common\OrderStatus;use App\Service\SmsService;use App\Service\WithdrawalService;use App\Traits\DemandTraits;use App\Traits\SmsTraits;use App\Traits\StatementTraits;use EasyWeChat\Factory;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Storage;
class VerificationController extends Controller{ //核销订单
public function verify() { $input_verify_code = request()->input('verify_code'); //订单ID
$code_arr = explode('-', $input_verify_code); if (count($code_arr) != 2) { return $this->error('参数错误'); } list($id, $verify_code) = $code_arr; $order = Order::with(['agentProduct:id,verifier', 'user', 'agent', 'guide']) ->where(['verify_code' => $verify_code]) ->whereIn('status', [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::OFFLINE_PAID]) ->find($id); if (!$order) { return $this->error('订单不存在或订单状态不允许核销'); }
$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); } //短信
if (env('SMS_SWITCH', '') == true) { if (!empty($order->user->mobile)) { (new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['user']], [$order->user->mobile]);//用户
} $supplierIds = OrderProductItem::query()->with('supplier')->where('order_id', $order->id)->distinct()->pluck('supplier_id'); $phone = Supplier::query()->whereIn('id', $supplierIds)->pluck('contact_phone')->toArray(); (new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['supplier']], $phone);//供应商
(new SmsService)->send('verify', [$order->order_no, SmsTraits::$systeaNameText['agent']], [$order->agent->contact_phone]);//代理商
} }
return $this->success(); }
public function fund($order) { $service = new WithdrawalService(); DB::beginTransaction(); try { //最后批量插入
$adminCreate = $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') ->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(); $supplier->balance = bcadd($supplier->balance, $supplierPrice, 6); $supplier->save(); }
//分账给代理商
//成本价 加上地接价格
$agentPrice = bcsub($order->price, $cost, 2);
if ($agentPrice > 0) { $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()); } }}
|