6
0
Fork 0

Merge branch 'develop' of gitee.com:zi-chunsheng-e-commerce/mall-server into 1.x

release
李静 2022-02-23 11:14:20 +08:00
commit 390fed8478
9 changed files with 150 additions and 12 deletions

View File

@ -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');

View File

@ -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:

View File

@ -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();
}

View File

@ -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,
];
}
}

View File

@ -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'
];
}

View File

@ -13,4 +13,6 @@ enum DealerWalletAction: int {
case OrderIncome = 8;
case TransferIn = 9;
case TransferOut = 10;
case EarningIn = 11;
case EarningOut = 12;
}

View File

@ -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()

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPayWayToDealerEarningsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dealer_earnings', function (Blueprint $table) {
//
$table->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');
});
}
}

View File

@ -27,6 +27,7 @@ return [
'status_format' => '状态',
'remark' => '备注',
'pay_image' => '打款凭证',
'pay_way'=>'支付方式',
],
'options' => [
],