调整押金自动扣除

This commit is contained in:
luofei 2023-05-22 13:52:15 +08:00
parent bbcf370a2d
commit 342a5b74f7
5 changed files with 96 additions and 16 deletions

@ -38,6 +38,7 @@ use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserBrokerageRepository; use app\common\repositories\user\UserBrokerageRepository;
use app\common\repositories\user\UserMerchantRepository; use app\common\repositories\user\UserMerchantRepository;
use app\common\repositories\user\UserRepository; use app\common\repositories\user\UserRepository;
use crmeb\jobs\AutoMarginJob;
use crmeb\jobs\PayGiveCouponJob; use crmeb\jobs\PayGiveCouponJob;
use crmeb\jobs\ProductImportJob; use crmeb\jobs\ProductImportJob;
use crmeb\jobs\SendSmsJob; use crmeb\jobs\SendSmsJob;
@ -53,7 +54,6 @@ use crmeb\services\SwooleTaskService;
use Exception; use Exception;
use FormBuilder\Factory\Elm; use FormBuilder\Factory\Elm;
use FormBuilder\Form; use FormBuilder\Form;
use http\Exception\InvalidArgumentException;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException; use think\db\exception\ModelNotFoundException;
@ -485,7 +485,7 @@ class StoreOrderRepository extends BaseRepository
'mer_id' => $order->mer_id, 'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . $index 'financial_record_sn' => $financeSn . $index
]; ];
$merchant->save(['paid_margin' => bcadd($merchant['paid_margin'], $margin, 2)]); Queue::push(AutoMarginJob::class, ['merId' => $order->mer_id, 'margin' => $margin]);
} }
} }

@ -317,7 +317,7 @@ class FinancialRepository extends BaseRepository
$res = app()->make(MerchantRepository::class)->get($merId); $res = app()->make(MerchantRepository::class)->get($merId);
if ($res['is_margin'] == -1) throw new ValidateException('请勿重复申请'); if ($res['is_margin'] == -1) throw new ValidateException('请勿重复申请');
if (!in_array($res['is_margin'],[10,-10]) || $res['margin'] <= 0) if (!in_array($res['is_margin'],[10,-10]) || $res['paid_margin'] <= 0)
throw new ValidateException('无可退保证金'); throw new ValidateException('无可退保证金');
$order = app()->make(ServeOrderRepository::class)->getWhere([ $order = app()->make(ServeOrderRepository::class)->getWhere([
@ -341,7 +341,7 @@ class FinancialRepository extends BaseRepository
'mer_id' => $merId, 'mer_id' => $merId,
'mer_money' => 0, 'mer_money' => 0,
'financial_sn' => $sn, 'financial_sn' => $sn,
'extract_money' => $res['margin'], 'extract_money' => $res['paid_margin'],
'financial_type' => 1, 'financial_type' => 1,
'financial_account' => json_encode($financial_account,JSON_UNESCAPED_UNICODE), 'financial_account' => json_encode($financial_account,JSON_UNESCAPED_UNICODE),
'financial_status' => 0, 'financial_status' => 0,
@ -373,7 +373,7 @@ class FinancialRepository extends BaseRepository
$where['is_del'] = 0; $where['is_del'] = 0;
$query = $this->dao->search($where)->with([ $query = $this->dao->search($where)->with([
'merchant' => function($query){ 'merchant' => function($query){
$query->field('mer_id,mer_name,is_trader,mer_avatar,type_id,mer_phone,mer_address,is_margin,margin,real_name'); $query->field('mer_id,mer_name,is_trader,mer_avatar,type_id,mer_phone,mer_address,is_margin,margin,real_name,paid_margin');
$query->with([ $query->with([
'merchantType', 'merchantType',
'marginOrder' => function($query){ 'marginOrder' => function($query){
@ -383,7 +383,9 @@ class FinancialRepository extends BaseRepository
} }
]); ]);
$count = $query->count(); $count = $query->count();
$list = $query->page($page, $limit)->select(); $list = $query->page($page, $limit)->select()->each(function ($item){
return $item->merchant->margin = $item->merchant->paid_margin;
});
return compact('count','list'); return compact('count','list');
} }

@ -57,6 +57,12 @@ use think\Model;
class MerchantRepository extends BaseRepository class MerchantRepository extends BaseRepository
{ {
const WithoutMargin = 0; //不需要保证金
const NeedMargin = 1; //需要保证金
const PaidMargin = 10; //已支付保证金
const RefundMargin = -1; //申请退还保证金
const RefuseMargin = -10; //拒绝退还保证金
/** /**
* MerchantRepository constructor. * MerchantRepository constructor.
* @param MerchantDao $dao * @param MerchantDao $dao
@ -598,8 +604,8 @@ class MerchantRepository extends BaseRepository
$form->setRule([ $form->setRule([
Elm::input('mer_name', '商户名称', $merchant->mer_name)->disabled(true), Elm::input('mer_name', '商户名称', $merchant->mer_name)->disabled(true),
Elm::input('mer_id', '商户ID', $merchant->mer_id)->disabled(true), Elm::input('mer_id', '商户ID', $merchant->mer_id)->disabled(true),
Elm::input('margin', '商户剩余保证金', $merchant->margin)->disabled(true), Elm::input('margin', '商户剩余保证金', $merchant->paid_margin)->disabled(true),
Elm::number('number', '保证金扣除金额', 0)->max($merchant->margin)->precision(2)->required(), Elm::number('number', '保证金扣除金额', 0)->max($merchant->paid_margin)->precision(2)->required(),
Elm::text('mark', '保证金扣除原因')->required(), Elm::text('mark', '保证金扣除原因')->required(),
]); ]);
return $form->setTitle('扣除保证金'); return $form->setTitle('扣除保证金');
@ -622,14 +628,15 @@ class MerchantRepository extends BaseRepository
throw new ValidateException('扣除保证金额不能小于0'); throw new ValidateException('扣除保证金额不能小于0');
} }
if (bccomp($merechant->margin, $data['number'], 2) == -1) { if (bccomp($merechant->paid_margin, $data['number'], 2) == -1) {
throw new ValidateException('扣除保证金额不足'); throw new ValidateException('扣除保证金额不足');
} }
$data['balance'] = bcsub($merechant->margin, $data['number'], 2); $data['balance'] = bcsub($merechant->paid_margin, $data['number'], 2);
Db::transaction(function () use ($merechant, $data) { Db::transaction(function () use ($merechant, $data) {
$merechant->margin = $data['balance']; $merechant->paid_margin = $data['balance'];
$merechant->ot_margin = $data['balance'];
$merechant->save(); $merechant->save();
app()->make(UserBillRepository::class)->bill(0, 'mer_margin', $data['type'], 0, $data); app()->make(UserBillRepository::class)->bill(0, 'mer_margin', $data['type'], 0, $data);
}); });

@ -43,6 +43,10 @@ class ServeOrderRepository extends BaseRepository
//同城配送delivery //同城配送delivery
const TYPE_DELIVERY = 20; const TYPE_DELIVERY = 20;
const PAY_TYPE_WECHAT = 1; //微信支付
const PAY_TYPE_ALI = 2; //支付宝支付
const PAY_TYPE_BALANCE = 3; //余额支付
/** /**
* TODO 购买一号通 支付 * TODO 购买一号通 支付

@ -0,0 +1,67 @@
<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\jobs;
use app\common\dao\system\serve\ServeOrderDao;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use app\common\repositories\system\serve\ServeOrderRepository;
use crmeb\interfaces\JobInterface;
use think\facade\Log;
class AutoMarginJob implements JobInterface
{
public function fire($job, $data)
{
Log::info('utoMarginStart' . var_export($data, 1));
try {
$merchant = app()->make(MerchantRepository::class)->get($data['merId']);
$orderInfo = [
'type_id' => $merchant['type_id'],
'is_margin' => $merchant['is_margin'],
'margin' => $data['margin'],
];
$values = [
'status' => 1,
'is_del' => 0,
'mer_id' => $merchant['mer_id'],
'type' => ServeOrderRepository::TYPE_MARGIN,
'meal_id'=> $merchant['type_id'],
'pay_type' => ServeOrderRepository::PAY_TYPE_BALANCE,
'order_info' => json_encode($orderInfo,JSON_UNESCAPED_UNICODE),
'pay_price' => $data['margin'],
];
$values['order_sn'] = app()->make(StoreOrderRepository::class)->getNewOrderId('cs');
$values['pay_time'] = date('y_m-d H:i:s', time());
if (!app()->make(ServeOrderDao::class)->create($values)) {
throw new \Exception('serve_order 保存出错');
}
$merchant->paid_margin = bcadd($data['margin'], $merchant->paid_margin, 2);
$merchant->ot_margin = $merchant->paid_margin;
$merchant->is_margin = MerchantRepository::PaidMargin;
$merchant->save();
} catch (\Exception $exception) {
Log::info('更新商户保证金出错:' . var_export($exception, 1));
} finally {
Log::info('autoMarginEnd' . var_export($data, 1));
}
}
public function failed($data)
{
// TODO: Implement failed() method.
}
}