diff --git a/app/Admin/Controllers/DealerEarningController.php b/app/Admin/Controllers/DealerEarningController.php index de4ed3a1..63756e31 100644 --- a/app/Admin/Controllers/DealerEarningController.php +++ b/app/Admin/Controllers/DealerEarningController.php @@ -81,6 +81,11 @@ class DealerEarningController extends AdminController return $this->payer_id ? $this->payer?->phone : '公司'; }); // $grid->column('pay_info'); + $grid->column('pay_way')->using(DealerEarningModel::$payWayText)->label([ + DealerEarningModel::PAY_WAY_WALLET=>'warning', + DealerEarningModel::PAY_WAY_OFFLINE=>'danger', + 'none'=>'#b3b9bf', + ]); $grid->column('pay_at'); // $grid->column('pay_image'); @@ -151,6 +156,11 @@ class DealerEarningController extends AdminController $show->field('payer.phone', '打款人')->as(function () { return $this->payer_id ? $this->payer?->phone : '公司'; }); + $show->field('pay_way', '支付方式')->using(DealerEarningModel::$payWayText)->dot([ + DealerEarningModel::PAY_WAY_WALLET=>'warning', + DealerEarningModel::PAY_WAY_OFFLINE=>'danger', + 'none'=>'#b3b9bf', + ]); $show->field('pay_image')->image(); // $show->field('pay_info'); $show->field('pay_at'); diff --git a/app/Admin/Controllers/DealerOrderController.php b/app/Admin/Controllers/DealerOrderController.php index c0524db3..1181807c 100644 --- a/app/Admin/Controllers/DealerOrderController.php +++ b/app/Admin/Controllers/DealerOrderController.php @@ -137,6 +137,7 @@ class DealerOrderController extends AdminController $builder = DealerOrder::with(['user', 'userInfo', 'consignor']); $column->row(Show::make($id, $builder, function (Show $show) { $show->field('sn'); + $show->field('created_at'); $show->field('order_status')->as(function ($v) { return $this->order_status; })->using(DealerOrderStatus::texts())->dot([ @@ -157,6 +158,7 @@ class DealerOrderController extends AdminController $show->field('pay_way', '支付方式')->using(DealerOrderModel::$payWayText)->dot($this->payWayDots); $show->field('pay_sn', '支付流水号'); $show->field('pay_time', '支付时间'); + $show->field('paied_time'); switch ($show->model()->pay_way) { case DealerOrderModel::PAY_WAY_OFFLINE: diff --git a/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php b/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php index b46fced5..dd027fad 100644 --- a/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php +++ b/app/Endpoint/Api/Http/Controllers/Dealer/EarningController.php @@ -6,10 +6,15 @@ 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 { @@ -25,13 +30,13 @@ class EarningController extends Controller $user = $request->user(); switch ($cate) { case 'pending':// - $query = $user->dealerPayEarnings()->onlyPending()->whereNotNull('settle_at'); + $query = $user->dealerPayEarnings()->with('user')->onlyPending()->whereNotNull('settle_at'); break; case 'paid': $query = $user->dealerEarnings()->onlyPaid()->whereNotNull('settle_at'); break; default://全部 - $query = DealerEarning::where(function ($q) use ($user) { + $query = DealerEarning::with('user')->where(function ($q) use ($user) { return $q->where('user_id', $user->id)->orWhere('payer_id', $user->id); }); break; @@ -51,7 +56,7 @@ class EarningController extends Controller public function show($id, Request $request) { $user = $request->user(); - $earning = DealerEarning::where(function ($q) use ($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); @@ -67,21 +72,90 @@ class EarningController extends Controller public function payEarning($id, Request $request) { $earning = DealerEarning::findOrFail($id); - - if (!$earning->isPayer($request->user()->id)) { + $user = $request->user(); + if (!$earning->isPayer($user->id)) { throw new BizException('无法操作该订单'); } $input = $request->validate([ - 'pay_image' => ['bail', 'required', 'string'], + '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()) { - $earning->update([ - 'status' => DealerEarningStatus::Paid, - 'pay_info' => $earning->getPayInfo(), - 'pay_image'=> $input['pay_image'], - 'pay_at' => now(), - ]); + 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(); } diff --git a/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningResource.php b/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningResource.php index 243153e2..fa00dfa2 100644 --- a/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningResource.php +++ b/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningResource.php @@ -27,11 +27,16 @@ class DealerEarningResource extends JsonResource 'status' => $this->status_format, 'status_name' => $this->status_name, 'remark' => $this->remark, + 'pay_way' => $this->pay_way ?? '', 'pay_info' => $this->getPayInfo(), 'pay_image' => $this->pay_image, 'pay_at' => $this->pay_at?->toDateTimeString(), 'is_payer' => $this->payer_id ? ($this->payer_id == $request->user()->id) : false, 'purchase_subsidy_logs' => $purchaseSubsidyLogs ? DealerEarningSubsidyLogResource::collection($purchaseSubsidyLogs) : null, + 'beneficiary' => $this->user ? [ + 'nickname' => $this->user->userInfo?->nickname ?? '', + 'phone'=>$this->user->phone ?? '', + ] : null, ]; } } diff --git a/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningSimpleResource.php b/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningSimpleResource.php index d8ae05c1..adbc0265 100644 --- a/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningSimpleResource.php +++ b/app/Endpoint/Api/Http/Resources/Dealer/DealerEarningSimpleResource.php @@ -22,6 +22,7 @@ class DealerEarningSimpleResource extends JsonResource 'status' => $this->status_format, 'status_name' => $this->status_name, 'is_payer' => $this->payer_id ? ($this->payer_id == $request->user()->id) : false, + 'beneficiary_phone' => $this->user?->phone ?? '', // 'settle_at' ]; } diff --git a/app/Enums/DealerWalletAction.php b/app/Enums/DealerWalletAction.php index 328a5809..6eae5c51 100644 --- a/app/Enums/DealerWalletAction.php +++ b/app/Enums/DealerWalletAction.php @@ -13,4 +13,6 @@ enum DealerWalletAction: int { case OrderIncome = 8; case TransferIn = 9; case TransferOut = 10; + case EarningIn = 11; + case EarningOut = 12; } diff --git a/app/Models/DealerEarning.php b/app/Models/DealerEarning.php index 5a9548bb..93765683 100644 --- a/app/Models/DealerEarning.php +++ b/app/Models/DealerEarning.php @@ -18,6 +18,9 @@ class DealerEarning extends Model 'status' => DealerEarningStatus::Pending, ]; + public const PAY_WAY_WALLET = 'wallet'; // 余额 + public const PAY_WAY_OFFLINE = 'offline'; // 线下支付 + protected $casts = [ 'lvl' => DealerLvl::class, 'status' => DealerEarningStatus::class, @@ -42,6 +45,12 @@ class DealerEarning extends Model 'settle_at', 'status', 'remark', + 'pay_way', + ]; + + public static $payWayText = [ + self::PAY_WAY_WALLET => '余额支付', + self::PAY_WAY_OFFLINE => '线下打款', ]; public function user() diff --git a/database/migrations/2022_02_23_104107_add_pay_way_to_dealer_earnings_table.php b/database/migrations/2022_02_23_104107_add_pay_way_to_dealer_earnings_table.php new file mode 100644 index 00000000..e8b642f7 --- /dev/null +++ b/database/migrations/2022_02_23_104107_add_pay_way_to_dealer_earnings_table.php @@ -0,0 +1,34 @@ +string('pay_way')->nullable()->comment('1线下打款,2余额'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('dealer_earnings', function (Blueprint $table) { + // + $table->dropColumn('pay_way'); + }); + } +} diff --git a/resources/lang/zh_CN/dealer-earning.php b/resources/lang/zh_CN/dealer-earning.php index 5cc89b99..d48b704f 100644 --- a/resources/lang/zh_CN/dealer-earning.php +++ b/resources/lang/zh_CN/dealer-earning.php @@ -27,6 +27,7 @@ return [ 'status_format' => '状态', 'remark' => '备注', 'pay_image' => '打款凭证', + 'pay_way'=>'支付方式', ], 'options' => [ ],