diff --git a/app/common/logic/StoreFinanceFlowLogic.php b/app/common/logic/StoreFinanceFlowLogic.php index e1fabed7..e5c3d4c8 100644 --- a/app/common/logic/StoreFinanceFlowLogic.php +++ b/app/common/logic/StoreFinanceFlowLogic.php @@ -148,24 +148,16 @@ class StoreFinanceFlowLogic extends BaseLogic { $store = SystemStore::where('id', $store_id)->find(); $capitalFlowDao = new CapitalFlowLogic($store, 'store'); - if ($money > 0) { - //判断是否是押金 - if($store['paid_deposit']<$store['security_deposit']){ - StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 11])->update(['status' => 1,'number'=>$money]); - StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 2])->update(['status' => 1,'number'=>0]); - - $capitalFlowDao->storeIncome('store_paid_deposit_add', 'order', $order_id, $money,'','paid_deposit'); - SystemStore::where('id', $store_id)->inc('paid_deposit', $money)->update(); - }else{ - $capitalFlowDao->storeIncome('store_money_add', 'order', $order_id, $money,'','store_money'); - SystemStore::where('id', $store_id)->inc('store_money', $money)->update(); - } - } - if ($deposit > 0) { + if ($deposit > 0 && $store['paid_deposit'] < $store['security_deposit']) { StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 11])->update(['status' => 1]); $capitalFlowDao->storeIncome('store_paid_deposit_add', 'order', $order_id, $deposit,'','paid_deposit'); SystemStore::where('id', $store_id)->inc('paid_deposit', $deposit)->update(); } + if ($money > 0) { + StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 2])->update(['status' => 1]); + $capitalFlowDao->storeIncome('store_money_add', 'order', $order_id, $money,'','store_money'); + SystemStore::where('id', $store_id)->inc('store_money', $money)->update(); + } $find = StoreFinanceFlow::where(['order_id' => $order_id, 'financial_pm' => 1, 'financial_type' => 16, 'status' => 0])->find(); StoreFinanceFlow::where(['order_id' => $order_id, 'financial_type' => 16])->update(['status' => 1]); if ($find) { diff --git a/app/common/service/RefundOrderService.php b/app/common/service/RefundOrderService.php index 4d5d2752..83fe6044 100644 --- a/app/common/service/RefundOrderService.php +++ b/app/common/service/RefundOrderService.php @@ -3,12 +3,15 @@ namespace app\common\service; use app\common\enum\OrderEnum; +use app\common\logic\CapitalFlowLogic; use app\common\logic\CommissionnLogic; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_finance_flow\StoreFinanceFlow; use app\common\model\store_finance_flow_product\StoreFinanceFlowProduct; use app\common\model\store_order\StoreOrder; use app\common\model\store_order_cart_info\StoreOrderCartInfo; +use app\common\model\system_store\SystemStore; +use app\common\model\user\User; use app\common\service\pay\PayTool; use Exception; use support\exception\BusinessException; @@ -28,21 +31,17 @@ class RefundOrderService if (empty($order)) { throw new BusinessException('订单不存在'); } - if (!$order->allowRefund()) { - throw new BusinessException('订单不能退款'); - } $orderCartProducts = StoreOrderCartInfo::where('oid', $order['id'])->whereIn('id', $params['old_cart_id'])->select(); - foreach ($orderCartProducts as $orderCartProduct) { - if ($orderCartProduct['is_pay'] != 1 || $orderCartProduct['status'] != StoreOrderCartInfo::StatusWait) { - throw new BusinessException('订单商品状态异常'); - } + if ($order->status == 2 && $order->is_writeoff == 1) { + $this->refundStoreMoney($order); + $this->refundCommission($order); } $refundAmount = $this->calculate($orderCartProducts); $this->refundMoney($order, $refundAmount); $this->updateProductStock($orderCartProducts); $this->updateOrderProductStatus($order, $params['old_cart_id']); $this->updateOrderStatus($order, $refundAmount, count($params['old_cart_id'])); - $this->refundCommission($order, $orderCartProducts); + $this->resetStoreFinanceFlow($order, $orderCartProducts); Db::commit(); } catch (Exception $e) { Db::rollback(); @@ -59,9 +58,7 @@ class RefundOrderService { $amount = '0.00'; foreach ($orderCartProducts as $orderCartProduct) { - if ($orderCartProduct['is_pay'] == 1 && $orderCartProduct['status'] == StoreOrderCartInfo::StatusWait) { - $amount = bcadd($amount, $orderCartProduct['total_price'], 2); - } + $amount = bcadd($amount, $orderCartProduct['total_price'], 2); } return $amount; } @@ -143,7 +140,7 @@ class RefundOrderService * @throws DbException * @throws ModelNotFoundException */ - public function refundCommission($order, $storeOrderProducts): void + public function resetStoreFinanceFlow($order, $storeOrderProducts): void { $productIds = array_unique(array_column($storeOrderProducts->toArray(), 'product_id')); StoreFinanceFlowProduct::where('oid', $order['id'])->whereIn('product_id', $productIds)->update(['number' => 0, 'update_time' => strtotime(time())]); @@ -154,4 +151,41 @@ class RefundOrderService CommissionnLogic::setStore($order, $village_uid, $brigade_uid, $transaction_id); } + /** + * 扣除店铺余额和押金 + * @param $order + * @return void + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function refundStoreMoney($order): void + { + $margin = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', OrderEnum::ORDER_MARGIN)->value('number'); + $income = StoreFinanceFlow::where('order_id', $order['id'])->where('financial_type', OrderEnum::MERCHANT_ORDER_OBTAINS)->value('number'); + if (empty($margin) && empty($income)) { + return; + } + $store = SystemStore::where('id', $order['store_id'])->find(); + if ($margin > 0) { + $store->paid_deposit = bcsub($store->paid_deposit, $margin, 2); + } + if ($income > 0) { + $store->store_money = bcsub($store->store_money, $income, 2); + } + $store->save(); + } + + public function refundCommission($order) + { + $list = StoreFinanceFlow::where(['order_id' => $order['id'], 'financial_pm' => 1, 'status' => 0])->where('other_uid', '>', 0)->select(); + foreach ($list as $v) { + $v->save(['status' => 1]); + $find = User::where('id', $v['other_uid'])->find(); + $capitalFlowDao = new CapitalFlowLogic($find); + $capitalFlowDao->userExpense('system_balance_sub', 'order', $order['id'], $v['number']); + $find->dec('now_money', $v['number'])->update(); + } + } + } \ No newline at end of file