123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409 |
- <?php
-
-
- namespace app\shop\logic\finance;
-
-
- use app\common\basics\Logic;
- use app\common\enum\NoticeEnum;
- use app\common\enum\OrderEnum;
- use app\common\enum\ShopEnum;
- use app\common\enum\PayEnum;
- use app\common\enum\OrderRefundEnum;
- use app\common\enum\ShopWithdrawEnum;
- use app\common\enum\WithdrawalEnum;
- use app\common\enum\AfterSaleEnum;
- use app\common\model\order\Order;
- use app\common\model\shop\Shop;
- use app\common\model\shop\ShopAccountLog;
- use app\common\model\shop\ShopAlipay;
- use app\common\model\shop\ShopBank;
- use app\common\model\shop\ShopWithdrawal;
- use app\common\model\shop\ShopSettlement;
- use app\common\server\ConfigServer;
- use app\common\server\ExportExcelServer;
- use think\Exception;
- use think\facade\Db;
-
- class WithdrawalLogic extends Logic
- {
-
-
- public static function lists($get, $shop_id, $is_export = false)
- {
- try {
- $where[] = ['shop_id', '=', $shop_id];
- $where[] = ['status', '=', $get['type'] ?? 0];
-
- if (!empty($get['start_time']) and $get['start_time']) {
- $where[] = ['create_time', '>=', strtotime($get['start_time'])];
- }
-
- if (!empty($get['end_time']) and $get['end_time']) {
- $where[] = ['create_time', '<=', strtotime($get['end_time'])];
- }
-
-
- if (true === $is_export) {
- return self::export($where);
- }
-
- $model = new ShopWithdrawal();
- $lists = $model->field(true)
- ->where($where)
- ->order('id desc')
- ->paginate([
- 'page' => $get['page'],
- 'list_rows' => $get['limit'],
- 'var_page' => 'page'
- ])->toArray();
-
- foreach ($lists['data'] as &$item) {
- $item['status'] = WithdrawalEnum::getStatusDesc($item['status']);
- }
-
- return ['count' => $lists['total'], 'lists' => $lists['data']];
- } catch (\Exception $e) {
- return ['error' => $e->getMessage()];
- }
- }
-
-
-
- public static function summary($shop_id)
- {
- $orderModel = new Order();
-
-
- $detail['dealOrderCount'] = $orderModel->where(['shop_id' => $shop_id, 'pay_status' => OrderEnum::PAY_STATUS_PAID])->count();
-
- $detail['businessMoney'] = $orderModel->where(['shop_id' => $shop_id, 'pay_status' => OrderEnum::PAY_STATUS_PAID])->sum('order_amount');
-
- $detail['refundMoney'] = $orderModel->where(['shop_id' => $shop_id])->where('refund_status', '>', 0)->sum('order_amount');
- }
-
-
-
- public static function detail($id, $shop_id)
- {
- $withdrawal = (new ShopWithdrawal())->findOrEmpty($id)->toArray();
- $shop = (new Shop())->with(['category'])->findOrEmpty($shop_id)->toArray();
- $bank = (new ShopBank())->findOrEmpty($withdrawal['bank_id'])->toArray();
- $alipay = (new ShopAlipay())->findOrEmpty($withdrawal['alipay_id'])->toArray();
-
- $shop['type'] = ShopEnum::getShopTypeDesc($shop['type']);
- $withdrawal['status_text'] = WithdrawalEnum::getStatusDesc($withdrawal['status']);
- $withdrawal['type_text'] = ShopWithdrawEnum::getTypeText($withdrawal['type']);
-
- return [ 'withdrawal' => $withdrawal, 'shop' => $shop, 'bank' => $bank, 'alipay' => $alipay ];
- }
-
-
-
- public static function statistics($shop_id)
- {
- $model = new ShopWithdrawal();
- $apply = $model->where(['status' => WithdrawalEnum::APPLY_STATUS, 'shop_id' => $shop_id])->count();
- $handle = $model->where(['status' => WithdrawalEnum::HANDLE_STATUS, 'shop_id' => $shop_id])->count();
- $success = $model->where(['status' => WithdrawalEnum::SUCCESS_STATUS, 'shop_id' => $shop_id])->count();
- $error = $model->where(['status' => WithdrawalEnum::ERROR_STATUS, 'shop_id' => $shop_id])->count();
-
-
- $modelOrder = new Order();
- $modelShopWithdrawal = new ShopWithdrawal();
- $orderNum = $modelOrder
- ->where([
- ['shop_id', '=', $shop_id],
- ['pay_status', '>', PayEnum::UNPAID]
- ])
- ->count('id');
-
- $orderAmount = $modelOrder
- ->where([
- ['shop_id', '=', $shop_id],
- ['pay_status', '>', PayEnum::UNPAID]
- ])
- ->sum('order_amount');
-
-
- $refundAmount = $modelOrder
- ->where([
- ['shop_id', '=', $shop_id],
- ['shipping_status', '=', OrderEnum::SHIPPING_NO],
- ['pay_status', '=', PayEnum::REFUNDED],
- ['refund_status', 'in', [OrderEnum::REFUND_STATUS_PART_REFUND, OrderEnum::REFUND_STATUS_ALL_REFUND]],
- ])
- ->sum('refund_amount');
-
-
- $refundAmountIng = $modelOrder->alias('o')
- ->join('order_refund or', 'or.order_id = o.id')
- ->where([
- ['o.shop_id', '=', $shop_id],
- ['o.shipping_status', '=', OrderEnum::SHIPPING_NO],
- ['or.refund_status', '<>', OrderRefundEnum::REFUND_STATUS_COMPLETE]
- ])
- ->sum('or.refund_amount');
-
-
- $salesRefundAmount = $modelOrder->alias('o')
- ->join('after_sale as', 'as.order_id = o.id')
- ->where([
- ['o.shop_id', '=', $shop_id],
- ['o.shipping_status', '=', OrderEnum::SHIPPING_FINISH],
- ['as.status', '=', AfterSaleEnum::STATUS_COMPLETE]
- ])
- ->sum('as.refund_price');
-
-
- $salesRefundAmountIng = $modelOrder->alias('o')
- ->join('after_sale as', 'as.order_id = o.id')
- ->where([
- ['o.shop_id', '=', $shop_id],
- ['o.shipping_status', '=', OrderEnum::SHIPPING_FINISH],
- ['as.status', '=', AfterSaleEnum::STATUS_WAITING]
- ])
- ->sum('as.refund_price');
-
-
- $modelShopSettlement = new ShopSettlement();
- $settleOrederNum = $modelShopSettlement
- ->where([
- ['shop_id', '=', $shop_id],
- ])
- ->sum('deal_order_count');
-
-
- $settleOrederAmount = $modelShopSettlement
- ->where([
- ['shop_id', '=', $shop_id],
- ])
- ->sum('business_money');
-
-
- $settleOrederAmountWait = $modelOrder
- ->where([
- ['shop_id', '=', $shop_id],
- ['refund_status', '=', 0 ],
- ['settle_id', '=', OrderEnum::SETTLE_WAIT]
- ])
- ->sum('order_amount');
-
-
- $settleDistributionAmount = $modelShopSettlement
- ->where([
- ['shop_id', '=', $shop_id],
- ])
- ->sum('distribution_money');
-
-
- $settleWithdrawalAmount = $modelShopSettlement
- ->where([
- ['shop_id', '=', $shop_id],
- ])
- ->sum('entry_account_money');
-
-
- $settlePoundageAmount = $modelShopSettlement
- ->where([
- ['shop_id', '=', $shop_id],
- ])
- ->sum('trade_service_fee');
-
-
- $withdrawaLeftamount = $modelShopWithdrawal
- ->where([
- ['shop_id', '=', $shop_id],
- ['status', '=', WithdrawalEnum::SUCCESS_STATUS]
- ])
- ->sum('apply_amount');
-
-
- $withdrawaLeftamountIng = $modelShopWithdrawal
- ->where([
- ['shop_id', '=', $shop_id],
- ['status', 'in', [WithdrawalEnum::APPLY_STATUS, WithdrawalEnum::HANDLE_STATUS]]
- ])
- ->sum('apply_amount');
-
-
- $modelShop = new Shop();
- $shopWallet = $modelShop
- ->where([
- ['id', '=', $shop_id]
- ])
- ->sum('wallet');
-
-
- $procedMoney = $modelShopWithdrawal
- ->where([
- ['shop_id', '=', $shop_id],
- ['status', '=', WithdrawalEnum::SUCCESS_STATUS]
- ])
- ->sum('poundage_amount');
-
-
- return [
- 'apply' => $apply,
- 'handle' => $handle,
- 'success' => $success,
- 'error' => $error,
- 'orderNum' => $orderNum,
- 'orderAmount' => $orderAmount,
- 'refundAmount' => $refundAmount,
- 'refundAmountIng' => $refundAmountIng,
- 'salesRefundAmount' => $salesRefundAmount,
- 'salesRefundAmountIng' => $salesRefundAmountIng,
- 'settleOrederNum' => $settleOrederNum,
- 'settleOrederAmount' => $settleOrederAmount,
- 'settleOrederAmountWait' => $settleOrederAmountWait,
- 'settleDistributionAmount' => $settleDistributionAmount,
- 'settleWithdrawalAmount' => $settleWithdrawalAmount,
- 'settlePoundageAmount' => $settlePoundageAmount,
- 'withdrawaLeftamount' => $withdrawaLeftamount,
- 'procedMoney' => $procedMoney,
- 'withdrawaLeftamountIng' => $withdrawaLeftamountIng,
- 'shopWallet' => $shopWallet,
- ];
- }
-
-
-
- public static function add($post, $shop_id)
- {
- Db::startTrans();
- try {
-
- $min_withdrawal_money = ConfigServer::get('shop_withdrawal', 'min_withdrawal_money', 0);
- $max_withdrawal_money = ConfigServer::get('shop_withdrawal', 'max_withdrawal_money', 0);
- $withdrawal_service_charge = ConfigServer::get('shop_withdrawal', 'withdrawal_service_charge', 0);
-
-
- $shop = (new Shop())->findOrEmpty($shop_id)->toArray();
- $wallet = $shop['wallet'];
-
-
- if (floatval($post['apply_amount']) > $wallet)
- throw new Exception('账户余额不足');
- if (floatval($post['apply_amount']) < $min_withdrawal_money)
- throw new Exception('最低提现金额不能少于' . $min_withdrawal_money . '元');
- if (floatval($post['apply_amount']) > $max_withdrawal_money)
- throw new Exception('最高提现金额不能大于' . $max_withdrawal_money . '元');
-
-
- $poundage_amount = 0;
- if ($withdrawal_service_charge > 0) {
- $proportion = $withdrawal_service_charge / 100;
- $poundage_amount = $post['apply_amount'] * $proportion;
- $poundage_amount = $poundage_amount <= 0 ? 0 : $poundage_amount;
- }
-
-
- $withdrawal = ShopWithdrawal::create([
- 'sn' => createSn('shop_withdrawal', 'sn'),
- 'bank_id' => $post['bank_id'] ?? 0,
- 'alipay_id' => $post['alipay_id'] ?? 0,
- 'type' => $post['type'],
- 'shop_id' => $shop_id,
- 'apply_amount' => floatval($post['apply_amount']),
- 'left_amount' => $post['apply_amount'] - $poundage_amount,
- 'poundage_amount' => $poundage_amount,
- 'poundage_ratio' => $withdrawal_service_charge,
- 'status' => WithdrawalEnum::APPLY_STATUS
- ]);
-
- Shop::update([
- 'wallet' => ['dec', floatval($post['apply_amount'])],
- 'update_time' => time()
- ], ['id' => $shop_id]);
- $left_amount = Shop::where(['id' => $shop_id])->value('wallet');
-
- $logType = ShopAccountLog::withdrawal_stay_money;
- ShopAccountLog::decData($shop_id, $logType, $post['apply_amount'], $left_amount, [
- 'source_id' => $withdrawal['id'],
- 'source_sn' => $withdrawal['sn'],
- 'remark' => '商家提现'
- ]);
-
- $platform_contacts = ConfigServer::get('website_platform', 'platform_mobile');
- if (!empty($platform_contacts)) {
- event('Notice', [
- 'scene' => NoticeEnum::SHOP_WITHDRAWAL_NOTICE_PLATFORM,
- 'mobile' => $platform_contacts,
- 'params' => [
- 'shop_withdrawal_sn' => $withdrawal['sn'],
- 'shop_name' => $shop['name'],
- ]
- ]);
- }
-
- Db::commit();
- return true;
- } catch (\Exception $e) {
- Db::rollback();
- static::$error = $e->getMessage();
- return false;
- }
- }
-
-
-
- public static function export($where)
- {
- try {
- $lists = (new ShopWithdrawal())
- ->field(true)
- ->where($where)
- ->select()->toArray();
-
- foreach ($lists as &$item) {
- $item['status'] = WithdrawalEnum::getStatusDesc($item['status']);
- }
-
- $excelFields = [
- 'sn' => '提现单号',
- 'apply_amount' => '提现金额',
- 'poundage_amount' => '提现手续费',
- 'left_amount' => '到账金额',
- 'status' => '提现状态',
- 'create_time' => '提现时间',
- ];
-
- $export = new ExportExcelServer();
- $export->setFileName('财务中心');
- $result = $export->createExcel($excelFields, $lists);
-
- return ['url' => $result];
-
- } catch (\Exception $e) {
- self::$error = $e->getMessage();
- return false;
- }
- }
- }
|