diff --git a/app/common/dao/system/financial/FinancialRecordDao.php b/app/common/dao/system/financial/FinancialRecordDao.php index 1eb37c6a..84ac5818 100644 --- a/app/common/dao/system/financial/FinancialRecordDao.php +++ b/app/common/dao/system/financial/FinancialRecordDao.php @@ -64,6 +64,28 @@ class FinancialRecordDao extends BaseDao $this->setData($number, $financialType, 1, 1, $merId); } + /** + * 商户入账财务流水 + * @param $number + * @param $financialType + * @param $merId + */ + public function merchantOut($number, $financialType, $merId = '') + { + $this->setData($number, $financialType, 0, 0, $merId); + } + + /** + * 商户入账财务流水 + * @param $number + * @param $financialType + * @param $merId + */ + public function merchantIn($number, $financialType, $merId = '') + { + $this->setData($number, $financialType, 1, 0, $merId); + } + public function setData($number, $financialType, $pm, $type = 2, $merId = '') { if (empty($this->financeSn)) { diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index 3dad843e..5619da9e 100644 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -12,6 +12,7 @@ namespace app\common\repositories\store\order; use app\common\dao\store\order\StoreOrderDao; +use app\common\dao\system\financial\FinancialRecordDao; use app\common\model\store\order\StoreGroupOrder; use app\common\model\store\order\StoreOrder; use app\common\model\user\User; @@ -217,6 +218,7 @@ class StoreOrderRepository extends BaseRepository $storeOrderStatusRepository = app()->make(StoreOrderStatusRepository::class); $svipDiscount = 0; $isPoints = false; + $financeDao = new FinancialRecordDao(); foreach ($groupOrder->orderList as $_k => $order) { $order->paid = 1; $order->pay_time = $time; @@ -280,108 +282,62 @@ class StoreOrderRepository extends BaseRepository } } - $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->order = $order; + $financeDao->user = $groupOrder->user; + $financialType = $presell ? 'order_presell' : 'order'; + // 平台订单收入流水账单数据 + $financeDao->platformIn($order->total_price, $financialType); + if ($order->platform_coupon_price > 0) { + // 平台支出优惠金额 + $financialType = $isVipCoupon ? 'order_svip_coupon' : 'order_platform_coupon'; + $financeDao->platformOut($order->platform_coupon_price, $financialType); + } $_payPrice = bcsub($order->pay_price, bcadd($order['extension_one'], $order['extension_two'], 3), 2); + $orderValidAmount = bcsub($order->total_price, bcadd($order['extension_one'], $order['extension_two'], 3), 2); if ($presell) { if (isset($order->orderProduct[0]['cart_info']['presell_extension_one']) && $order->orderProduct[0]['cart_info']['presell_extension_one'] > 0) { - $_payPrice = bcadd($_payPrice, $order->orderProduct[0]['cart_info']['presell_extension_one'], 2); + $orderValidAmount = bcadd($orderValidAmount, $order->orderProduct[0]['cart_info']['presell_extension_one'], 2); } if (isset($order->orderProduct[0]['cart_info']['presell_extension_two']) && $order->orderProduct[0]['cart_info']['presell_extension_two'] > 0) { - $_payPrice = bcadd($_payPrice, $order->orderProduct[0]['cart_info']['presell_extension_two'], 2); + $orderValidAmount = bcadd($orderValidAmount, $order->orderProduct[0]['cart_info']['presell_extension_two'], 2); } } - - $_order_rate = 0; - - if ($order['commission_rate'] > 0) { - $commission_rate = bcdiv((string)$order['commission_rate'],'100',6); - $_order_rate = bcmul($_payPrice, (string)$commission_rate, 2); - - $_payPrice = bcsub($_payPrice, $_order_rate, 2); + // 平台支出推广费 + $promoter = $order->merchant->promoter(); + $promoterCommission = bcmul($orderValidAmount, 0.003, 2); + if (!empty($promoter) && $promoterCommission > 0) { + $financeDao->platformOut($promoterCommission, 'commission_to_promoter'); } + // 平台收入手续费 + $commission_rate = bcdiv((string)$order['commission_rate'],'100',6); + $platformCommission = bcmul($orderValidAmount, (string)$commission_rate, 2); + if ($commission_rate > 0 && $platformCommission > 0) { + $orderValidAmount = bcsub($orderValidAmount, $platformCommission, 2); + if ($promoterCommission > 0 && !empty($promoter)) { + $platformCommission = bcsub($platformCommission, $promoterCommission, 2); + } + $financeDao->platformIn($platformCommission, 'commission_to_platform', $order['mer_id']); + } + + if ($orderValidAmount > 0) { + /** @var MerchantRepository $merchantRepo */ + $merchantRepo = app()->make(MerchantRepository::class); + $merchantRepo->merId = $order['mer_id']; + $merchantRepo->forceMargin = false; + [$orderValidAmount, $financeDao] = $merchantRepo->deductDeposit($orderValidAmount, $order, $financeDao); + } + // 商户收入金额 + $financialType = $order['activity_type'] == 20 ? 'points_order_true' : 'order_true'; + $financeDao->platformOut($orderValidAmount, $financialType, $order['mer_id']); + if (!$presell) { if ($order['extension_one'] > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'brokerage_one', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $order['extension_one'], - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; + $financeDao->merchantOut($order['extension_one'], 'brokerage_one', $order['mer_id']); } - if ($order['extension_two'] > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => 'brokerage_two', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $order['extension_two'], - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - } - if ($order['commission_rate'] > 0) { - $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++) - ]; - } - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => $order['activity_type'] ==20 ? 'points_order_true' : 'order_true', - 'financial_pm' => 0, - 'type' => 2, - 'number' => $_payPrice, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - if ($order->platform_coupon_price > 0) { - $finance[] = [ - 'order_id' => $order->order_id, - 'order_sn' => $order->order_sn, - 'user_info' => $groupOrder->user->nickname, - 'user_id' => $uid, - 'financial_type' => $isVipCoupon ? 'order_svip_coupon' : 'order_platform_coupon', - 'financial_pm' => 0, - 'type' => 1, - 'number' => $order->platform_coupon_price, - 'mer_id' => $order->mer_id, - 'financial_record_sn' => $financeSn . ($i++) - ]; - $_payPrice = bcadd($_payPrice, $order->platform_coupon_price, 2); + $financeDao->merchantOut($order['extension_two'], 'brokerage_two', $order['mer_id']); } if (!$is_combine) { app()->make(MerchantRepository::class)->addLockMoney($order->mer_id, 'order', $order->order_id, $_payPrice); @@ -410,7 +366,7 @@ class StoreOrderRepository extends BaseRepository //自动打印订单 $this->autoPrinter($order->order_id, $order->mer_id); - if ($order->orderProduct[0]->product->isPlatformCard()) { + if ($order->orderProduct[0]->product->isPlatformCard() || $isPickupCard) { //购物卡自动发货 $deliveryData = [ 'delivery_type' => 3, diff --git a/app/common/repositories/system/merchant/MerchantRepository.php b/app/common/repositories/system/merchant/MerchantRepository.php index 7979e59f..21806b33 100644 --- a/app/common/repositories/system/merchant/MerchantRepository.php +++ b/app/common/repositories/system/merchant/MerchantRepository.php @@ -15,9 +15,13 @@ namespace app\common\repositories\system\merchant; use app\common\dao\store\StoreCategoryDao; +use app\common\dao\system\financial\FinancialDao; +use app\common\dao\system\financial\FinancialRecordDao; use app\common\dao\system\merchant\MerchantDao; +use app\common\dao\system\serve\ServeOrderDao; use app\common\model\store\order\StoreOrder; use app\common\model\store\product\ProductReply; +use app\common\model\system\merchant\Merchant; use app\common\repositories\BaseRepository; use app\common\repositories\store\coupon\StoreCouponRepository; use app\common\repositories\store\coupon\StoreCouponUserRepository; @@ -64,6 +68,9 @@ use think\Model; class MerchantRepository extends BaseRepository { + public $merId; + public $forceMargin = false; //强制扣除押金 + /** * MerchantRepository constructor. * @param MerchantDao $dao @@ -886,4 +893,67 @@ class MerchantRepository extends BaseRepository $data['mer_certificate'] = merchantConfig($id, 'mer_certificate'); return $data; } + + /** + * 自动扣除押金 + * @param $income + * @param $order + * @param FinancialRecordDao $financeDao + * @return array + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + */ + public function deductDeposit($income, $order, FinancialDao $financeDao) + { + $merchant = Merchant::find($this->merId); + //商户押金大于支付押金 或者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($merchant['margin'], $merchant['paid_margin'], 2), $margin); + $income = max(bcsub($income, $margin, 2), 0); + if ($margin <= 0) { + return [$income, $financeDao]; + } + $financeDao->platformIn($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 [$income, $financeDao]; + } + } diff --git a/app/common/repositories/system/serve/ServeOrderRepository.php b/app/common/repositories/system/serve/ServeOrderRepository.php index 2660f947..a7641e0f 100644 --- a/app/common/repositories/system/serve/ServeOrderRepository.php +++ b/app/common/repositories/system/serve/ServeOrderRepository.php @@ -50,6 +50,10 @@ class ServeOrderRepository extends BaseRepository const PAY_TYPE_ALIPAY= 2; const PAY_TYPE_SYS = 3; + const PAY_TYPE_WECHAT = 1; //微信支付 + const PAY_TYPE_ALI = 2; //支付宝支付 + const PAY_TYPE_BALANCE = 3; //余额支付 + /** * TODO 购买一号通 支付 diff --git a/app/controller/api/store/order/ScanPay.php b/app/controller/api/store/order/ScanPay.php new file mode 100644 index 00000000..3950e85b --- /dev/null +++ b/app/controller/api/store/order/ScanPay.php @@ -0,0 +1,47 @@ +request->get('mer_id'); + $cateId = env('PICKUP_CARD_CATE_ID'); + $product = Product::where('mer_id', $merId)->where('cate_id', $cateId)->find(); + if (empty($product)) { + return app('json')->fail('商品已下架'); + } + $param = $this->request->params(['type', ['product_type', 0]]); + /** @var ProductRepository $productRepo */ + $productRepo = app()->make(ProductRepository::class); + $productRepo->force = true; + $data = $productRepo->detail((int)$product['product_id'], $this->request->userInfo, $param['product_type']); + if (!$data) { + return app('json')->fail('商品已下架'); + } + return app('json')->success($data); + } + + public function qrcode() + { + $merId = $this->request->get('mer_id'); + /** @var Common $common */ + $common = app()->make(Common::class); + $siteUrl = systemConfig('site_url'); + $user = $this->request->userInfo(); + $data = $common->Qrcode(['code' => $siteUrl . 'pages/payment/get_payment?mer_id=' . $merId, 'id' => $user['uid']]); + return app('json')->success(['url' => $data]); + } + +}