Merge branch 'optimize' into dev

This commit is contained in:
luofei 2024-01-30 17:49:24 +08:00
commit 5fe3177046
9 changed files with 229 additions and 210 deletions

View File

@ -62,9 +62,11 @@ class StoreActivityOrderDao extends BaseDao
public function repeal(int $groupOrderId)
{
$model = StoreActivityOrder::where('group_order_id', $groupOrderId)->find();
$model->status = StoreActivityOrder::STATUS_INVALID;
if (!$model->save()) {
throw new \Exception('活动订单保存失败');
if ($model) {
$model->status = StoreActivityOrder::STATUS_INVALID;
if (!$model->save()) {
throw new \Exception('活动订单保存失败');
}
}
}

View File

@ -163,11 +163,14 @@ class StoreActivityUserDao extends BaseDao
$userInfo = User::where('spread_uid', $userId)->field('uid,nickname,avatar')->select()->toArray();
$storeConsumptionUserDao = new StoreConsumptionUserDao();
$scope = $storeConsumptionUserDao->getScope($consumption, $myOrder['pay_price']);
// 订单有效金额为实付金额+红包金额
$orderValidAmount = bcadd($myOrder['pay_price'], $myOrder['red_pack'], 2);
$orderValidAmount = min($orderValidAmount, $storeConsumptionUserDao->maxAmount);
$orderQuery = StoreActivityOrder::where('spread_id', $userId)
->whereIn('user_id', array_column($userInfo, 'uid'))
->where('activity_id', $activityId)
->where('is_first_order', StoreActivityOrder::IS_FIRST_ORDER)
->where('pay_price', '>=', $scope['start']);
->where('pay_price', '>=', $orderValidAmount);
if (!empty($groupOrderIds)) {
unset($groupOrderIds[0]);
$orderQuery->whereIn('group_order_id', $groupOrderIds);
@ -177,7 +180,7 @@ class StoreActivityUserDao extends BaseDao
$orders = $orderQuery->select()->toArray();
$orders = reset_index($orders, 'user_id');
foreach ($userInfo as &$user) {
$user['target_amount'] = $scope['start'];
$user['target_amount'] = $orderValidAmount;
$user['is_finish'] = isset($orders[$user['uid']]) ? 1 : 0;
}
return ['target' => $scope['num'], 'allow_receive' => count($orders) >= $scope['num'], 'user_info' => $userInfo];

View File

@ -2,13 +2,13 @@
namespace app\common\dao\store\consumption;
use app\common\dao\system\financial\FinancialDao;
use app\common\model\store\consumption\StoreConsumption;
use app\common\model\store\consumption\StoreConsumptionUser;
use app\common\model\store\order\StoreOrder;
use app\common\model\system\financial\Financial;
use app\common\model\system\merchant\FinancialRecord;
use app\common\model\system\merchant\Merchant;
use app\common\model\user\User;
use app\common\repositories\system\merchant\FinancialRecordRepository;
use app\common\repositories\system\merchant\MerchantRepository;
use crmeb\utils\Curl;
use think\facade\Log;
@ -19,40 +19,29 @@ class CommissionDao
/**
* 活动首单商户佣金 (支付成功后调用)
* @param $order
* @param $finance
* @param $financeSn
* @param $financeDao
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function firstOrderCommission($order, $finance, $financeSn, $financeIndex)
public function firstOrderCommission($order, $financeDao)
{
$consumption = StoreConsumption::where('status', 1)->where('type', StoreConsumption::TYPE_FIRST_ORDER_COMMISSION)->find();
if (empty($consumption)) {
return $finance;
return $financeDao;
}
$storeConsumptionDao = new StoreConsumptionUserDao();
$isFirstOrder = $storeConsumptionDao->isFirstOrder($order['uid'], $consumption['start_time'], $consumption['end_time']);
if (!$isFirstOrder) {
return $finance;
return $financeDao;
}
$commission = bcmul($order['pay_price'], 0.01, 2);
if ($commission > 0 && $order['order_type'] == 1) {
// 订单为自提且佣金大于0
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $order->user->nickname,
'user_id' => $order['uid'],
'financial_type' => 'first_order_commission', // TODO 这个类型应该是长期的
'financial_pm' => 0,
'type' => 2,
'number' => $commission,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . $financeIndex
];
$financeIndex++;
$financeDao->user = $order->user;
$financeDao->order = $order;
$financeDao->platformOut($commission, 'first_order_commission');
app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission);
}
// 给镇合伙人、村合伙人、小组服务团队、店铺分佣,仅直推
@ -63,18 +52,9 @@ class CommissionDao
$userId = Merchant::where('mer_id', $merchantId)->value('uid');
$user = User::where('uid', $userId)->find();
$commission = bcmul($order['pay_price'], 0.03, 2);
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $user['nickname'],
'user_id' => $user['uid'],
'financial_type' => 'first_order_commission',
'financial_pm' => 0,
'type' => 2,
'number' => $commission,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . $financeIndex
];
$financeDao->user = $user;
$financeDao->order = $order;
$financeDao->platformOut($commission, 'first_order_commission');
app()->make(MerchantRepository::class)->addLockMoney($order['mer_id'], 'order', $order['order_id'], $commission);
$redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2);
if ($redPack > 0) {
@ -88,7 +68,7 @@ class CommissionDao
$this->sendCommission($order, $promotionCode);
}
}
return $finance;
return $financeDao;
}
/**
@ -105,30 +85,20 @@ class CommissionDao
if (empty($consumption)) {
return [];
}
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financeSn = $financialRecordRepository->getSn();
$users = $this->getUsers($data['user']);
$order = StoreOrder::where('order_id', $data['order_id'])->find();
if (empty($order)) {
return [];
}
$finance = [];
$result = [];
foreach ($users as $k => $user) {
$financeDao = new FinancialDao();
foreach ($users as $user) {
$commission = bcdiv($user['user_profit'], 100, 2);
if ($commission > 0) {
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $user['nickname'],
'user_id' => $user['uid'],
'financial_type' => $user['type'] == 3 ? 'order_commission' : 'first_order_commission', // TODO 配送员的佣金类型需要调整
'financial_pm' => 0,
'type' => 2,
'number' => $commission,
'mer_id' => $order['mer_id'],
'financial_record_sn' => $financeSn . ($k + 1)
];
$financeDao->user = $user;
$financeDao->order = $order;
$financialType = $user['type'] == 3 ? 'order_commission' : 'first_order_commission'; // TODO 配送员的佣金类型需要调整
$financeDao->platformOut($commission, $financialType);
$result[] = $user;
}
$redPack = bcmul($order['pay_price'], $consumption['config']['red_pack_rate'], 2);
@ -140,9 +110,7 @@ class CommissionDao
}
}
}
if (count($finance) > 0) {
$financialRecordRepository->insertAll($finance);
}
$financeDao->save();
return $result;
}
@ -186,57 +154,38 @@ class CommissionDao
*/
public function refundByOrder($refundOrder)
{
$commission = bcmul($refundOrder->order['pay_price'], 0.01, 2);
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financeSn = $financialRecordRepository->getSn();
if ($commission > 0 && $refundOrder->order['order_type'] == 1) {
// 订单为自提且佣金大于0
$finance[] = [
'order_id' => $refundOrder->order->order_id,
'order_sn' => $refundOrder->order->order_sn,
'user_info' => $refundOrder->order->user->nickname,
'user_id' => $refundOrder->order['uid'],
'financial_type' => 'first_order_commission_refund',
'financial_pm' => 1,
'type' => 2,
'number' => $commission,
'mer_id' => $refundOrder->order->mer_id,
'financial_record_sn' => $financeSn
];
app()->make(MerchantRepository::class)->subLockMoney($refundOrder->order['mer_id'], 'order', $refundOrder->order['order_id'], $commission);
}
// 是否已经退过佣金
$refunded = Financial::where('order_id', $refundOrder->order['order_id'])
$refunded = FinancialRecord::where('order_id', $refundOrder->order['order_id'])
->whereIn('financial_type', ['order_commission_refund', 'first_order_commission_refund'])
->count();
if ($refunded > 0) {
return;
}
$commission = bcmul($refundOrder->order['pay_price'], 0.01, 2);
$financeDao = new FinancialDao();
if ($commission > 0 && $refundOrder->order['order_type'] == 1 && $refundOrder->order['source'] != 105) {
// 订单为自提且佣金大于0下单的店铺退佣金
$financeDao->user = $refundOrder->order->user;
$financeDao->order = $refundOrder->order;
$financeDao->platformIn($commission, 'first_order_commission_refund');
app()->make(MerchantRepository::class)->subLockMoney($refundOrder->order['mer_id'], 'order', $refundOrder->order['order_id'], $commission);
}
// 退佣金和红包
$finance = [];
$financeRecord = Financial::where('order_id', $refundOrder->order['order_id'])
$financeRecord = FinancialRecord::where('order_id', $refundOrder->order['order_id'])
->whereIn('financial_type', ['order_commission', 'first_order_commission'])
->select()->toArray();
->field('user_id uid,user_info nickname')->select()->toArray();
if (empty($financeRecord)) {
return;
}
$redPack = bcmul($refundOrder->order['pay_price'], 0.07, 2);
(new StoreConsumptionUserDao())->refundByCommission($refundOrder->order['uid'], $refundOrder->order->order_id, $redPack);
foreach ($financeRecord as $k => $item) {
$finance[] = [
'order_id' => $refundOrder->order->order_id,
'order_sn' => $refundOrder->order->order_sn,
'user_info' => $item['user_info'],
'user_id' => $item['user_id'],
'financial_type' => $item['financial_type'] . '_refund',
'financial_pm' => 1,
'type' => 2,
'number' => $item['number'],
'mer_id' => $item['mer_id'],
'financial_record_sn' => $financeSn . ($k + 1)
];
}
if (count($finance) > 0) {
$financialRecordRepository->insertAll($finance);
foreach ($financeRecord as $item) {
$financeDao->user = $item;
$financeDao->order = $refundOrder->order;
$financeDao->platformIn($item['number'], $item['financial_type'] . '_refund');
(new StoreConsumptionUserDao())->refundByCommission($item['uid'], $refundOrder->order->order_id, $redPack);
}
$financeDao->save();
$promotionCode = User::where('uid', $refundOrder['uid'])->value('promotion_code');
if ($promotionCode) {
@ -266,30 +215,20 @@ class CommissionDao
*/
public function refundByCallback($data)
{
$finance = [];
$result = [];
$users = $this->getUsers($data['user']);
$order = StoreOrder::where('order_id', $data['order_id'])->find();
if (empty($order) || empty($users)) {
return [];
}
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financeSn = $financialRecordRepository->getSn();
foreach ($users as $k => $user) {
$financeDao = new FinancialDao();
foreach ($users as $user) {
$commission = bcdiv($user['user_profit'], 100, 2);
if ($commission > 0) {
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $user['nickname'],
'user_id' => $user['uid'],
'financial_type' => ($user['type'] == 3 ? 'order_commission' : 'first_order_commission') . '_refund',
'financial_pm' => 1,
'type' => 2,
'number' => $commission,
'mer_id' => $order['mer_id'],
'financial_record_sn' => $financeSn . ($k + 1)
];
$financeDao->user = $user;
$financeDao->order = $order;
$financialType = ($user['type'] == 3 ? 'order_commission' : 'first_order_commission') . '_refund';
$financeDao->platformIn($commission, $financialType);
$result[] = $user;
}
$redPack = bcmul($order['pay_price'], 0.07, 2);
@ -301,9 +240,7 @@ class CommissionDao
}
}
}
if (count($finance) > 0) {
$financialRecordRepository->insertAll($finance);
}
$financeDao->save();
return $result;
}

View File

@ -380,7 +380,7 @@ class StoreConsumptionUserDao extends BaseDao
{
$order = StoreOrder::where('order_id', $refundOrder['order_id'])->find();
$model = StoreConsumptionUser::where('uid', $refundOrder['uid'])->where('coupon_user_id', $order['consumption_id'])->find();
if (empty($model)) {
if (empty($model) || $order['source'] == 105) {
return;
}
$model->balance = bcadd($model->balance, $refundOrder['refund_consumption'], 2);

View File

@ -16,10 +16,18 @@ namespace app\common\dao\system\financial;
use app\common\dao\BaseDao;
use app\common\model\system\financial\Financial;
use app\common\model\system\merchant\FinancialRecord;
use app\common\repositories\system\merchant\FinancialRecordRepository;
class FinancialDao extends BaseDao
{
public $order;
public $user;
public $index = 0;
public $financeSn;
public $list = [];
protected function getModel(): string
{
return Financial::class;
@ -79,4 +87,75 @@ class FinancialDao extends BaseDao
return $query;
}
/**
* 平台出账财务流水
* @param $number
* @param $financialType
* @param $merId
*/
public function platformOut($number, $financialType, $merId = '')
{
$this->setData($number, $financialType, 0, 2, $merId);
}
/**
* 平台入账财务流水
* @param $number
* @param $financialType
* @param $merId
*/
public function platformIn($number, $financialType, $merId = '')
{
$this->setData($number, $financialType, 1, 2, $merId);
}
/**
* 公共入账财务流水
* @param $number
* @param $financialType
* @param $merId
*/
public function publicOut($number, $financialType, $merId = '')
{
$this->setData($number, $financialType, 0, 1, $merId);
}
/**
* 公共入账财务流水
* @param $number
* @param $financialType
* @param $merId
*/
public function publicIn($number, $financialType, $merId = '')
{
$this->setData($number, $financialType, 1, 1, $merId);
}
public function setData($number, $financialType, $pm, $type = 2, $merId = '')
{
if (empty($this->financeSn)) {
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$this->financeSn = $financialRecordRepository->getSn();
}
$this->list[] = [
'order_id' => $this->order->order_id,
'order_sn' => $this->order->order_sn,
'user_info' => $this->user['nickname'],
'user_id' => $this->user['uid'],
'financial_type' => $financialType,
'financial_pm' => $pm,
'type' => $type,
'number' => $number,
'mer_id' => !empty($merId) ? $merId : $this->order->mer_id,
'financial_record_sn' => $this->financeSn . ($this->index++)
];
}
public function save()
{
if (count($this->list) > 0) {
(new FinancialRecord())->insertAll($this->list);
}
}
}

View File

@ -16,6 +16,7 @@ use app\common\dao\store\consumption\StoreConsumptionUserDao;
use app\common\dao\store\order\StoreCartDao;
use app\common\dao\store\order\StoreOrderDao;
use app\common\dao\store\StoreActivityDao;
use app\common\dao\system\financial\FinancialDao;
use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreOrderInterest;
@ -215,18 +216,16 @@ class StoreOrderRepository extends BaseRepository
$orderStatus = [];
$groupOrder->append(['orderList.orderProduct']);
$flag = true;
$finance = [];
$profitsharing = [];
$financialRecordRepository = app()->make(FinancialRecordRepository::class);
$financeSn = $financialRecordRepository->getSn();
$userMerchantRepository = app()->make(UserMerchantRepository::class);
$storeOrderProfitsharingRepository = app()->make(StoreOrderProfitsharingRepository::class);
$uid = $groupOrder->uid;
$i = 1;
$i = 0;
// $isVipCoupon = app()->make(StoreGroupOrderRepository::class)->isVipCoupon($groupOrder);
//订单记录
$storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class);
$svipDiscount = 0;
$financeDao = new FinancialDao();
foreach ($groupOrder->orderList as $_k => $order) {
(new StoreActivityDao())->saveOrderProduct(2, $order);
$order->paid = 1;
@ -302,70 +301,29 @@ class StoreOrderRepository extends BaseRepository
Queue::push(SendGoodsCodeJob::class, $order);
}
$financeDao->order = $order;
$financeDao->user = $groupOrder->user;
$financialType = $presell ? 'order_presell' : 'order';
// 商户流水账单数据
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $groupOrder->user->nickname,
'user_id' => $uid,
'financial_type' => $presell ? 'order_presell' : 'order',
'financial_pm' => 1,
'type' => $presell ? 2 : 1,
'number' => $order->pay_price,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . ($i++)
];
$financeDao->setData($order->pay_price, $financialType, 1, $presell ? 2 : 1);
if ($order->source == 103) {
$_payPrice = $order->procure_price;
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $groupOrder->user->nickname,
'user_id' => $uid,
'financial_type' => 'supply_chain',
'financial_pm' => 0,
'type' => 1,
'number' => $_payPrice,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . ($i++)
];
$financeDao->publicOut($_payPrice, 'supply_chain');
//市级供应链
$product_mer_id = Db::name('store_order_product')->where('order_id', $order->order_id)->value('product_mer_id');
if ($product_mer_id) {
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $groupOrder->user->nickname,
'user_id' => $uid,
'financial_type' => 'order',
'financial_pm' => 0,
'type' => 1,
'number' => $_payPrice,
'mer_id' => $product_mer_id,
'financial_record_sn' => $financeSn . (($i++)+1)
];
$financeDao->publicOut($_payPrice, 'order', $product_mer_id);
//市级供应链押金计算
if ($_payPrice > 0) {
/** @var MerchantRepository $merchantRepo */
$merchantRepo = app()->make(MerchantRepository::class);
$merchantRepo->merId = $product_mer_id;
$merchantRepo->forceMargin = false;
[$_payCityPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++);
[$_payCityPrice, $financeDao] = $merchantRepo->deductDeposit($_payPrice, $order, $financeDao);
}
if (isset($_payCityPrice)) {
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $groupOrder->user->nickname,
'user_id' => $uid,
'financial_type' => 'order_true',
'financial_pm' => 0,
'type' => 2,
'number' => $_payCityPrice,
'mer_id' => $product_mer_id,
'financial_record_sn' => $financeSn . (($i++)+2)
];
$financeDao->platformOut($_payCityPrice, 'order_true', $product_mer_id);
if (!$is_combine) {
app()->make(MerchantRepository::class)->addLockMoney($product_mer_id, 'order', $order->order_id, $_payCityPrice);
}
@ -415,18 +373,7 @@ class StoreOrderRepository extends BaseRepository
if (!$presell) {
if ($order['commission_rate'] > 0 || $order->source == 103) {
//支出手续费
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $groupOrder->user->nickname,
'user_id' => $uid,
'financial_type' => 'order_charge',
'financial_pm' => 0,
'type' => 1,
'number' => $_order_rate,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . ($i++)
];
$financeDao->publicOut($_order_rate, 'order_charge');
}
//押金计算
if ($_payPrice > 0) {
@ -434,34 +381,12 @@ class StoreOrderRepository extends BaseRepository
$merchantRepo = app()->make(MerchantRepository::class);
$merchantRepo->merId = $order->mer_id;
$merchantRepo->forceMargin = false;
[$_payPrice, $finance, $increase] = $merchantRepo->autoMargin($_payPrice, $order, $finance, $financeSn, $i++);
[$_payCityPrice, $financeDao] = $merchantRepo->deductDeposit($_payPrice, $order, $financeDao);
}
if ($order->source == 103) {
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $groupOrder->user->nickname,
'user_id' => $uid,
'financial_type' => 'commission_to_cloud_warehouse',
'financial_pm' => 0,
'type' => 1,
'number' => $_payPrice,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . ($i++)
];
$financeDao->publicOut($_payPrice, 'commission_to_cloud_warehouse');
} else {
$finance[] = [
'order_id' => $order->order_id,
'order_sn' => $order->order_sn,
'user_info' => $groupOrder->user->nickname,
'user_id' => $uid,
'financial_type' => 'order_true',
'financial_pm' => 0,
'type' => 2,
'number' => $_payPrice,
'mer_id' => $order->mer_id,
'financial_record_sn' => $financeSn . ($i++)
];
$financeDao->platformOut($_payPrice, 'order_true');
}
@ -508,7 +433,7 @@ class StoreOrderRepository extends BaseRepository
$this->autoPrinter($order->order_id, $order->mer_id);
if ($order['pay_price'] > 0 && $order['source'] == 103) {
// "惠农供销,谱写数字新篇章"活动首单分润,商户和村、小组合伙人
$finance = (new CommissionDao())->firstOrderCommission($order, $finance, $financeSn, $i++);
$financeDao = (new CommissionDao())->firstOrderCommission($order, $financeDao);
}
}
//分销判断
@ -525,7 +450,7 @@ class StoreOrderRepository extends BaseRepository
if (count($profitsharing)) {
$storeOrderProfitsharingRepository->insertAll($profitsharing);
}
$financialRecordRepository->insertAll($finance);
$financeDao->save();
$storeOrderStatusRepository->batchCreateLog($orderStatus);
if (count($groupOrder['give_coupon_ids']) > 0)
$groupOrder['give_coupon_ids'] = app()->make(StoreCouponRepository::class)->getGiveCoupon($groupOrder['give_coupon_ids'])->column('coupon_id');

View File

@ -21,6 +21,8 @@ use app\common\dao\store\StoreActivityOrderDao;
use app\common\model\store\order\StoreGroupOrder;
use app\common\model\store\order\StoreOrder;
use app\common\model\store\order\StoreRefundOrder;
use app\common\model\store\product\CloudProduct;
use app\common\model\store\StoreActivityOrderProduct;
use app\common\model\system\merchant\FinancialRecord;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\product\ProductRepository;
@ -1186,6 +1188,12 @@ class StoreRefundOrderRepository extends BaseRepository
(new CommissionDao())->refundByOrder($refundOrder);
$productIds = CloudProduct::where('activity_id', 2)->column('product_id');
StoreActivityOrderProduct::where('user_id', $refundOrder->order['uid'])
->whereIn('product_id', $productIds)
->whereIn('activity_id', 2)
->update(['status' => 0]);
app()->make(FinancialRecordRepository::class)->dec([
'order_id' => $refundOrder->refund_order_id,
'order_sn' => $refundOrder->refund_order_sn,

View File

@ -1761,7 +1761,7 @@ class ProductRepository extends BaseRepository
]
], $product['mer_id']);
app()->make(SpuRepository::class)->changeStatus($id, $product->product_type);
event('product.sell', ['product_id' => [$id,'status'=>$status]]);
event('product.sell', ['product_id' => [$id], 'status'=>$status]);
$this->switchShow($id, $status, 'is_used', 0);
}

View File

@ -14,6 +14,7 @@
namespace app\common\repositories\system\merchant;
use app\common\dao\system\financial\FinancialDao;
use app\common\dao\system\merchant\MerchantDao;
use app\common\dao\system\serve\ServeOrderDao;
use app\common\model\store\order\StoreGroupOrder;
@ -676,6 +677,7 @@ class MerchantRepository extends BaseRepository
}
/**
* @deprecated 用下面的 deductDeposit
* 自动扣除押金
* @param $income
* @param $order
@ -748,4 +750,67 @@ class MerchantRepository extends BaseRepository
return [bcsub($income,$margin,2), $finance, true];
}
/**
* 自动扣除押金
* @param $income
* @param $order
* @param FinancialDao $financeDao
* @return array
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function deductDeposit($income, $order, FinancialDao $financeDao)
{
$merchant = Merchant::find($this->merId);
// $margin_type = Db::name('MerchantType')->where('mer_type_id', $merchant['type_id'])->value('margin');
//商户押金大于支付押金 或者forceMargin==false 直接返回 不计算押金
if ($merchant['paid_margin']>= $merchant['margin']|| ($this->forceMargin === false && $merchant['auto_margin_rate'] == 0)) {
return [$income, $financeDao];
}
$rate = $this->forceMargin ? 100 : $merchant['auto_margin_rate'];
// //商户押金未完全缴纳且设置了自动扣除比例
$margin= bcmul($income, bcdiv($rate, 100,2), 2);
// $margin = min(bcsub($margin, $merchant['paid_margin'], 2), $margin);
// $income = max(bcsub($income, $margin, 2), 0);
// if ($margin <= 0) {
// return [$income, $financeDao];
// }
$financeDao->publicOut($margin, 'auto_margin', $this->merId);
if(bcadd($merchant['paid_margin'],$margin)>=$merchant['margin']){
$is_margin=10;
}else{
$is_margin=1;
}
$orderInfo = [
'type_id' => $merchant['type_id'],
'is_margin' => $is_margin,
'margin' => $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' => $margin,
'store_order_id' => $order['order_id'],
];
$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 保存出错', 500);
}
$merchant->paid_margin = bcadd($margin, $merchant->paid_margin, 2);
$merchant->ot_margin = $merchant->paid_margin;
$merchant->is_margin = $is_margin;
if ($merchant->save() === false) {
throw new \Exception('merchant 保存出错', 500);
}
return [bcsub($income,$margin,2), $financeDao];
}
}