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.
240 lines
7.4 KiB
240 lines
7.4 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\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\Traits\DemandTraits;
|
|
use App\Traits\SmsTraits;
|
|
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, OrderStatus::REFUSED_REFUND])
|
|
->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)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
//最后批量插入
|
|
$adminCreate = $statementCreate = [];
|
|
$cost = 0;
|
|
//如果有地接价格 分帐给地接
|
|
if ($order->guide_price > 0) {
|
|
$guidePrice = $order->guide_price;
|
|
$cost = bcadd($cost, $order->guide_price, 6);
|
|
//成本价 加上地接价格
|
|
$statementCreate[] = [
|
|
'price' => $order->guide_price,
|
|
'type' => StatementType::ORDER,
|
|
'user_id' => $order->guide->id,
|
|
'user_type' => DemandTraits::$col[2],
|
|
'order_id' => $order->id
|
|
];
|
|
//抽成
|
|
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[] = [
|
|
'price' => $cutPrice,
|
|
'type' => StatementType::CUT,
|
|
'cut_user_id' => $order->guide->id,
|
|
'cut_user_type' => DemandTraits::$col[2],
|
|
'order_id' => $order->id
|
|
];
|
|
//地接被抽成流水
|
|
$statementCreate[] = [
|
|
'price' => bcmul($cutPrice, -1, 2),
|
|
'type' => StatementType::CUT,
|
|
'user_id' => $order->guide->id,
|
|
'user_type' => DemandTraits::$col[2],
|
|
'order_id' => $order->id
|
|
];
|
|
$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[] = [
|
|
'price' => $v->sum_price,
|
|
'type' => StatementType::ORDER,
|
|
'user_id' => $v->supplier_id,
|
|
'user_type' => DemandTraits::$col[1],
|
|
'order_id' => $order->id
|
|
];
|
|
|
|
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[] = [
|
|
'price' => $cutPrice,
|
|
'type' => StatementType::CUT,
|
|
'cut_user_id' => $v->supplier_id,
|
|
'cut_user_type' => DemandTraits::$col[1],
|
|
'order_id' => $order->id
|
|
];
|
|
//供应商被抽成流水
|
|
$statementCreate[] = [
|
|
'price' => bcmul($cutPrice, -1, 6),
|
|
'type' => StatementType::CUT,
|
|
'user_id' => $v->supplier_id,
|
|
'user_type' => DemandTraits::$col[1],
|
|
'order_id' => $order->id
|
|
];
|
|
$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[] = [
|
|
'price' => $agentPrice,
|
|
'type' => StatementType::ORDER,
|
|
'user_id' => $order->agent_id,
|
|
'user_type' => DemandTraits::$col[0],
|
|
'order_id' => $order->id
|
|
];
|
|
|
|
//抽成
|
|
if ($order->agent->rate > 0) {
|
|
//计算抽成金额
|
|
$agentCut = bcmul($agentPrice, $order->agent->rate, 6);
|
|
$cutPrice = $agentCut > 0 ? bcdiv($agentCut, 100, 6) : 0;
|
|
|
|
//总后台抽成流水
|
|
if ($cutPrice > 0) {
|
|
$adminCreate[] = [
|
|
'price' => $cutPrice,
|
|
'type' => StatementType::CUT,
|
|
'cut_user_id' => $order->agent->id,
|
|
'cut_user_type' => DemandTraits::$col[0],
|
|
'order_id' => $order->id
|
|
];
|
|
//代理商被抽成流水
|
|
$statementCreate[] = [
|
|
'price' => bcmul($cutPrice, -1, 6),
|
|
'type' => StatementType::CUT,
|
|
'user_id' => $order->agent->id,
|
|
'user_type' => DemandTraits::$col[0],
|
|
'order_id' => $order->id
|
|
];
|
|
$agentPrice = bcsub($agentPrice, $cutPrice, 6);
|
|
}
|
|
}
|
|
|
|
//扣除微信支付手续费
|
|
$chargePrice = bcmul($order->price, 0.006, 6);
|
|
$statementCreate[] = [
|
|
'price' => bcmul($chargePrice, -1, 6),
|
|
'type' => StatementType::CHARGE,
|
|
'user_id' => $order->agent_id,
|
|
'user_type' => DemandTraits::$col[0],
|
|
'order_id' => $order->id
|
|
];
|
|
$agentPrice = bcsub($agentPrice, $chargePrice, 6);
|
|
$agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
|
|
$agent->balance = bcadd($agent->balance, $agentPrice, 6);
|
|
$agent->save();
|
|
}
|
|
//dd($adminCreate,$guideCreate);
|
|
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());
|
|
}
|
|
}
|
|
}
|