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.
212 lines
6.2 KiB
212 lines
6.2 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\AgentProduct;
|
|
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');
|
|
|
|
if ($order->product_id) {
|
|
$checkMobile = Product::query()->whereIn('id', explode(',', $order->product_ids))->where('verify_mobile', $mobile)->doesntExist();
|
|
} else {
|
|
$checkMobile = AgentProduct::where(['id' => $order->agent_product_id, '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]
|
|
);
|
|
|
|
$guide = Guide::query()->where('id', $order->guide->id)->lockForUpdate()->first();
|
|
$guide->balance = bcadd($guide->balance, $guidePrice, 6);
|
|
$guide->save();
|
|
}
|
|
|
|
/**
|
|
* 线下付款已经在app/AdminAgent/Extensions/Grid/ChangeOrderStatus.php扣过了
|
|
* 这里不能再重复扣除。如果需要解除,同时还要注意下退款的情况
|
|
*/
|
|
if ($order->pay_type != PayType::OFFLINE) {
|
|
//分账给供应商
|
|
$orderItem = OrderProductItem::query()
|
|
->where([
|
|
['order_id', '=', $order->id],
|
|
['supplier_id', '<>', 0], //supplier_id=0是代理商自营产品,供应商没有分账权利
|
|
])
|
|
->with('supplier')
|
|
->select('*')
|
|
->selectRaw('sum(price) as sum_price,sum(single_deposit * 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]
|
|
);
|
|
|
|
$supplier = Supplier::query()->where('id', $v->supplier_id)->lockForUpdate()->first();
|
|
//处理交易金
|
|
if ($v->sum_persons > 0) {
|
|
//计算交易金
|
|
$deposit = $v->sum_persons;
|
|
|
|
//扣
|
|
$supplier->trade_balance = bcsub($supplier->trade_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]
|
|
);
|
|
|
|
$agent = Agent::query()->where('id', $order->agent->id)->lockForUpdate()->first();
|
|
$agent->balance = bcadd($agent->balance, $agentPrice, 6);
|
|
$agent->save();
|
|
}
|
|
|
|
if (!empty($statementCreate)) {
|
|
$order->statement()->createMany($statementCreate);
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|