185 lines
6.2 KiB
PHP
185 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Endpoint\Api\Http\Controllers\Dealer;
|
|
|
|
use App\Endpoint\Api\Http\Controllers\Controller;
|
|
use App\Endpoint\Api\Http\Resources\Dealer\DealerEarningResource;
|
|
use App\Endpoint\Api\Http\Resources\Dealer\DealerEarningSimpleResource;
|
|
use App\Enums\DealerEarningStatus;
|
|
use App\Enums\DealerWalletAction;
|
|
use App\Exceptions\BizException;
|
|
use App\Exceptions\PayPasswordIncorrectException;
|
|
use App\Helpers\Paginator;
|
|
use App\Models\DealerEarning;
|
|
use App\Services\Dealer\WalletService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Throwable;
|
|
|
|
class EarningController extends Controller
|
|
{
|
|
/**
|
|
* 列表
|
|
*
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$cate = $request->input('cate', '');//获取订单类别
|
|
$user = $request->user();
|
|
switch ($cate) {
|
|
case 'pending'://
|
|
$query = $user->dealerPayEarnings()->with('user')->onlyPending()->whereNotNull('settle_at');
|
|
break;
|
|
case 'paid':
|
|
$query = $user->dealerEarnings()->onlyPaid()->whereNotNull('settle_at');
|
|
break;
|
|
default://全部
|
|
$query = DealerEarning::with('user')->where(function ($q) use ($user) {
|
|
return $q->where('user_id', $user->id)->orWhere('payer_id', $user->id);
|
|
});
|
|
break;
|
|
}
|
|
|
|
$earnings = $query->latest('id')->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
|
return DealerEarningSimpleResource::collection($earnings);
|
|
}
|
|
|
|
/**
|
|
* 详情
|
|
*
|
|
* @param [type] $id
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function show($id, Request $request)
|
|
{
|
|
$user = $request->user();
|
|
$earning = DealerEarning::with(['user', 'user.userInfo'])->where(function ($q) use ($user) {
|
|
return $q->where('user_id', $user->id)->orWhere('payer_id', $user->id);
|
|
})->with('earningable')->findOrFail($id);
|
|
return DealerEarningResource::make($earning);
|
|
}
|
|
|
|
/**
|
|
* 确认打款
|
|
*
|
|
* @param [type] $id
|
|
* @param Request $request
|
|
* @return void
|
|
*/
|
|
public function payEarning($id, Request $request)
|
|
{
|
|
$earning = DealerEarning::findOrFail($id);
|
|
$user = $request->user();
|
|
if (!$earning->isPayer($user->id)) {
|
|
throw new BizException('无法操作该订单');
|
|
}
|
|
|
|
$input = $request->validate([
|
|
'pay_way' => ['bail', 'string'],
|
|
'pay_image' => ['bail', 'required_if:pay_way,offline', 'string'],
|
|
'pay_password' => ['bail', 'required_if:pay_way,wallet'],
|
|
], [
|
|
'pay_image.required_if' => '打款凭证 不能为空。',
|
|
'pay_password.required_if' => '支付密码 不能为空。',
|
|
], [
|
|
'pay_image' => '打款凭证',
|
|
'pay_way' => '支付方式',
|
|
'pay_password' => '支付密码',
|
|
]);
|
|
|
|
$payWay = $input['pay_way'] ?? DealerEarning::PAY_WAY_OFFLINE;
|
|
|
|
if (
|
|
$payWay === DealerEarning::PAY_WAY_WALLET &&
|
|
!$user->wallet?->verifyPassword($input['pay_password'])
|
|
) {
|
|
throw new PayPasswordIncorrectException();
|
|
}
|
|
|
|
if ($earning->isPending()) {
|
|
try {
|
|
DB::beginTransaction();
|
|
switch ($payWay) {
|
|
case DealerEarning::PAY_WAY_WALLET:
|
|
//支付余额
|
|
$walletService = new WalletService();
|
|
|
|
// 扣除打款人余额
|
|
$walletService->changeBalance(
|
|
$user,
|
|
bcmul($earning->total_earnings, '-1', 2),
|
|
DealerWalletAction::EarningOut,
|
|
"渠道补贴:【{$earning->remark}】",
|
|
$earning
|
|
);
|
|
|
|
// 增加收款人余额
|
|
if ($earning->user) {
|
|
$walletService->changeBalance(
|
|
$earning->user,
|
|
$earning->total_earnings,
|
|
DealerWalletAction::EarningIn,
|
|
"渠道补贴:【{$earning->remark}】",
|
|
$earning
|
|
);
|
|
}
|
|
|
|
$earning->update([
|
|
'status' => DealerEarningStatus::Completed,
|
|
'pay_way' => DealerEarning::PAY_WAY_WALLET,
|
|
'pay_info' => $earning->getPayInfo(),
|
|
'pay_at' => now(),
|
|
]);
|
|
|
|
break;
|
|
case DealerEarning::PAY_WAY_OFFLINE:
|
|
$earning->update([
|
|
'status' => DealerEarningStatus::Paid,
|
|
'pay_way' => DealerEarning::PAY_WAY_OFFLINE,
|
|
'pay_info' => $earning->getPayInfo(),
|
|
'pay_image'=> $input['pay_image'],
|
|
'pay_at' => now(),
|
|
]);
|
|
break;
|
|
default:
|
|
throw new BizException('支付方式不存在');
|
|
break;
|
|
}
|
|
DB::commit();
|
|
} catch (BizException $e) {
|
|
throw $e;
|
|
} catch (Throwable $th) {
|
|
DB::rollBack();
|
|
report($th);
|
|
throw new BizException('系统错误,请稍后再试');
|
|
}
|
|
}
|
|
return response()->noContent();
|
|
}
|
|
|
|
/**
|
|
* 确认收款
|
|
*
|
|
* @param [type] $id
|
|
* @param Request $request
|
|
*/
|
|
public function confirmEarning($id, Request $request)
|
|
{
|
|
$earning = DealerEarning::findOrFail($id);
|
|
|
|
if (!$earning->isUser($request->user()->id)) {
|
|
throw new BizException('无法操作该订单');
|
|
}
|
|
|
|
if ($earning->isPaid()) {
|
|
$earning->update([
|
|
'status' => DealerEarningStatus::Completed,
|
|
]);
|
|
}
|
|
return response()->noContent();
|
|
}
|
|
}
|