其余文件
This commit is contained in:
367
app/shop/logic/finance/SettlementLogic.php
Normal file
367
app/shop/logic/finance/SettlementLogic.php
Normal file
@ -0,0 +1,367 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\shop\logic\finance;
|
||||
|
||||
|
||||
use app\common\basics\Logic;
|
||||
use app\common\enum\OrderEnum;
|
||||
use app\common\enum\PayEnum;
|
||||
use app\common\model\AfterSale;
|
||||
use app\common\model\order\Order;
|
||||
use app\common\model\shop\Shop;
|
||||
use app\common\model\shop\ShopAccountLog;
|
||||
use app\common\model\shop\ShopSettlement;
|
||||
use app\common\model\shop\ShopSettlementRecord;
|
||||
use app\common\server\ConfigServer;
|
||||
use app\common\server\ExportExcelServer;
|
||||
use think\Exception;
|
||||
use think\facade\Db;
|
||||
|
||||
class SettlementLogic extends Logic
|
||||
{
|
||||
/**
|
||||
* @Notes: 结算列表
|
||||
* @Author: 张无忌
|
||||
* @param $get
|
||||
* @param $shop_id
|
||||
* @return array
|
||||
*/
|
||||
public static function lists($get, $shop_id, $is_export = false)
|
||||
{
|
||||
try {
|
||||
$where[] = ['shop_id', '=', $shop_id];
|
||||
if (!empty($get['start_time']) and $get['start_time'])
|
||||
$where[] = ['start_time', '>=', strtotime($get['start_time'])];
|
||||
if (!empty($get['end_time']) and $get['end_time'])
|
||||
$where[] = ['end_time', '>=', strtotime($get['end_time'])];
|
||||
|
||||
// 导出Excel
|
||||
if (true === $is_export) {
|
||||
return self::export($where);
|
||||
}
|
||||
|
||||
$model = new ShopSettlement();
|
||||
$lists = $model->field(true)
|
||||
->where($where)
|
||||
->order('id', 'desc')
|
||||
->paginate([
|
||||
'page' => $get['page'],
|
||||
'list_rows' => $get['limit'],
|
||||
'var_page' => 'page'
|
||||
])
|
||||
->toArray();
|
||||
|
||||
return ['count'=>$lists['total'], 'lists'=>$lists['data']];
|
||||
} catch (\Exception $e) {
|
||||
return ['error'=>$e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 结算详细
|
||||
* @Author: 张无忌
|
||||
* @param $get
|
||||
* @return array
|
||||
*/
|
||||
public static function detail($get)
|
||||
{
|
||||
try {
|
||||
$where[] = ['settle_id', '=', (int)$get['settle_id']];
|
||||
if (!empty($get['order_sn']) and $get['order_sn'])
|
||||
$where[] = ['order_sn', 'like', '%'.$get['order_sn'].'%'];
|
||||
|
||||
$model = new ShopSettlementRecord();
|
||||
$lists = $model->field(true)
|
||||
->where($where)
|
||||
->order('id', 'asc')
|
||||
->paginate([
|
||||
'page' => $get['page'],
|
||||
'list_rows' => $get['limit'],
|
||||
'var_page' => 'page'
|
||||
])
|
||||
->toArray();
|
||||
|
||||
return ['count'=>$lists['total'], 'lists'=>$lists['data']];
|
||||
} catch (\Exception $e) {
|
||||
return ['error'=>$e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 提交结算
|
||||
* @Author: 张无忌
|
||||
* @param $shop_id
|
||||
* @return bool
|
||||
*/
|
||||
public static function add($shop_id)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 1、获取售后时长时间搓
|
||||
$time = time();
|
||||
$afterSaleTime = ConfigServer::get('transaction', 'order_after_sale_days', 7);
|
||||
$afterSaleTime = intval($afterSaleTime * 24 * 60 * 60);
|
||||
|
||||
// 2、查询所有可结算订单
|
||||
$orders = (new Order())->field([
|
||||
'id,order_sn,order_status,pay_status,refund_status,is_cancel',
|
||||
'order_amount,refund_amount,distribution_money,confirm_take_time'
|
||||
])
|
||||
->whereRaw("confirm_take_time+$afterSaleTime < $time")
|
||||
->where([
|
||||
['shop_id', '=', $shop_id],
|
||||
['settle_id', '=', 0],
|
||||
['order_status', '=', OrderEnum::ORDER_STATUS_COMPLETE],
|
||||
['pay_status', '=', OrderEnum::PAY_STATUS_PAID],
|
||||
['confirm_take_time', '>', 0]
|
||||
])->select()->toArray();
|
||||
|
||||
if (!$orders) throw new Exception('暂无可结算订单金额');
|
||||
|
||||
// 3、检测订单是否再售后
|
||||
$afterSale = (new AfterSale())->whereIn('order_id', array_column($orders, 'id'))
|
||||
->whereIn('status', [
|
||||
AfterSale::STATUS_APPLY_REFUND,
|
||||
AfterSale::STATUS_WAIT_RETURN_GOODS,
|
||||
AfterSale::STATUS_WAIT_RECEIVE_GOODS,
|
||||
AfterSale::STATUS_WAIT_REFUND
|
||||
])->where('del', 0)->select()->toArray();
|
||||
|
||||
$afterSaleOrderIds = array_column($afterSale, 'order_id');
|
||||
$afterSaleOrderIds = array_unique($afterSaleOrderIds);
|
||||
if (!empty($afterSaleOrderIds)) {
|
||||
$orderIds = array_column($orders, 'id');
|
||||
if (count($orderIds) <= count($afterSaleOrderIds)) {
|
||||
throw new Exception('暂无可结算订单金额!');
|
||||
}
|
||||
}
|
||||
|
||||
// 4、生成结算批次记录
|
||||
$settleSn = createSn('shop_settlement', 'settle_sn', 'JS');
|
||||
$settle = ShopSettlement::create(['shop_id'=>$shop_id, 'settle_sn'=>$settleSn, 'create_time'=>$time,]);
|
||||
$data = self::handleSettlementByOrder($orders, $settle, $time);
|
||||
|
||||
// 5、回调更新结算批次信息
|
||||
ShopSettlement::update([
|
||||
'deal_order_count' => $data['dealOrderCount'],
|
||||
'business_money' => $data['businessMoney'] ,
|
||||
'refund_order_money' => $data['refundOrderMoney'],
|
||||
'after_sales_money' => $data['afterSalesMoney'],
|
||||
'distribution_money' => $data['distributionMoney'],
|
||||
'entry_account_money' => $data['entryAccountMoney'],
|
||||
'trade_service_fee' => $data['totalTradeServiceFee'],
|
||||
'trade_service_ratio' => $data['trade_service_ratio'],
|
||||
], ['id'=>$settle['id']]);
|
||||
|
||||
// 6、记录商家结算流水记录
|
||||
$logType = ShopAccountLog::settlement_add_money;
|
||||
ShopAccountLog::incData($shop_id, $logType, $data['entryAccountMoney'], -1, [
|
||||
'source_id' => $settle['id'],
|
||||
'source_sn' => $settleSn,
|
||||
'remark' => '商家对账结算'
|
||||
]);
|
||||
|
||||
// 7、把钱记录到商家钱包
|
||||
Shop::update(['wallet'=>['inc', $data['entryAccountMoney']]], ['id'=>$shop_id]);
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
static::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 对订单进行结算
|
||||
* 实际入账金额 = (订单实付金额 - 分销金额 - 退款金额) * 交易服务费比例
|
||||
* @Author: 张无忌
|
||||
* @param $orders (订单列表数据)
|
||||
* @param $settle (结算单信息)
|
||||
* @param $time (当前时间戳)
|
||||
* @return array
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function handleSettlementByOrder($orders, $settle, $time)
|
||||
{
|
||||
// 1、获取交易服务费比例
|
||||
$tradeServiceRatio = (new Shop())->where(['id'=>$settle['shop_id']])->value('trade_service_fee') ?? 0;
|
||||
|
||||
// 2、结算数据汇总统计
|
||||
$data = [
|
||||
'dealOrderCount' => 0, //总已结算成交订单数
|
||||
'businessMoney' => 0, //总已结算营业额
|
||||
'refundOrderMoney' => 0, //总退款订单金额
|
||||
'afterSalesMoney' => 0, //总售后退款金额
|
||||
'distributionMoney' => 0, //总已结算分销佣金金额
|
||||
'entryAccountMoney' => 0, //总已结算入账金额
|
||||
'totalTradeServiceFee' => 0, //总交易服务费费用
|
||||
'trade_service_ratio' => $tradeServiceRatio //交易服务费比例(%)
|
||||
];
|
||||
|
||||
// 3、处理结算每个订单
|
||||
$settle_record = [];
|
||||
$orders_update = [];
|
||||
foreach ($orders as $order) {
|
||||
$afterSale = (new AfterSale())->where(['order_id'=>$order['id']])
|
||||
->whereIn('status', [
|
||||
AfterSale::STATUS_APPLY_REFUND,
|
||||
AfterSale::STATUS_WAIT_RETURN_GOODS,
|
||||
AfterSale::STATUS_WAIT_RECEIVE_GOODS,
|
||||
AfterSale::STATUS_WAIT_REFUND
|
||||
])->where('del',0)->findOrEmpty();
|
||||
|
||||
if (!$afterSale->isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data['dealOrderCount'] += 1;
|
||||
$data['businessMoney'] += $order['order_amount'];
|
||||
$data['refundOrderMoney'] += $order['is_cancel'] ? $order['refund_amount'] : 0;
|
||||
$data['afterSalesMoney'] += $order['is_cancel'] ? 0 : $order['refund_amount'];
|
||||
$data['distributionMoney'] += $order['distribution_money'];
|
||||
|
||||
$orderMoney = $order['order_amount'] - $order['distribution_money'] - ($order['refund_amount'] ?: 0);
|
||||
$orderMoney = $orderMoney < 0 ? 0 : $orderMoney;
|
||||
$tradeServiceFee = $orderMoney * ($tradeServiceRatio / 100); // 本单交易服务费
|
||||
$actualGetMoney = $orderMoney - $tradeServiceFee; // 实际到账金额
|
||||
$entryAmount = $actualGetMoney;
|
||||
$data['entryAccountMoney'] += $actualGetMoney;
|
||||
$data['totalTradeServiceFee'] += $tradeServiceFee;
|
||||
|
||||
$settle_record[] = [
|
||||
'settle_id' => $settle['id'],
|
||||
'order_id' => $order['id'],
|
||||
'settle_sn' => $settle['settle_sn'],
|
||||
'order_sn' => $order['order_sn'],
|
||||
'order_amount' => $order['order_amount'],
|
||||
'refund_amount' => $order['is_cancel'] ? ($order['refund_amount'] ? : 0) : 0,
|
||||
'after_sales_amount' => $order['is_cancel'] ? 0 : ($order['refund_amount'] ? : 0),
|
||||
'distribution_amount' => $order['distribution_money'],
|
||||
'entry_account_amount' => $entryAmount,
|
||||
'trade_service_fee' => $tradeServiceFee,
|
||||
'trade_service_ratio' => $tradeServiceRatio,
|
||||
'order_complete_time' => $order['confirm_take_time'],
|
||||
'create_time' => $time
|
||||
];
|
||||
|
||||
$orders_update[] = [
|
||||
'id' => $order['id'],
|
||||
'settle_id' => $settle['id'],
|
||||
'settle_amount' => $entryAmount,
|
||||
'update_time' => $time
|
||||
];
|
||||
}
|
||||
|
||||
// 4、保存更新
|
||||
if (!empty($settle_record)) {
|
||||
(new ShopSettlementRecord())->saveAll($settle_record);
|
||||
(new Order())->saveAll($orders_update);
|
||||
}
|
||||
// 5、返回汇总数据
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Notes: 结算统计
|
||||
* @return array
|
||||
*/
|
||||
public static function statistics($shop_id = 0)
|
||||
{
|
||||
$where[] = ['shop_id', '=' , $shop_id];
|
||||
|
||||
$modelOrder = new Order();;
|
||||
|
||||
//已结算成交订单数
|
||||
$modelShopSettlement = new ShopSettlement();
|
||||
$settleOrederNum = $modelShopSettlement
|
||||
->where($where)
|
||||
->sum('deal_order_count');
|
||||
|
||||
//已结算营业额
|
||||
$settleOrederAmount = $modelShopSettlement
|
||||
->where($where)
|
||||
->sum('business_money');
|
||||
|
||||
//待结算营业额
|
||||
$settleOrederAmountWait = $modelOrder
|
||||
->where([
|
||||
['shop_id', '=' , $shop_id],
|
||||
['refund_status', '=', 0 ],
|
||||
['settle_id', '=', OrderEnum::SETTLE_WAIT],
|
||||
['order_status', 'in', [OrderEnum::ORDER_STATUS_DELIVERY,OrderEnum::ORDER_STATUS_GOODS,OrderEnum::ORDER_STATUS_COMPLETE]]
|
||||
])
|
||||
->sum('order_amount');
|
||||
|
||||
//已结算分销佣金金额
|
||||
$settleDistributionAmount = $modelShopSettlement
|
||||
->where($where)
|
||||
->sum('distribution_money');
|
||||
|
||||
//已结算入账金额
|
||||
$settleWithdrawalAmount = $modelShopSettlement
|
||||
->where($where)
|
||||
->sum('entry_account_money');
|
||||
|
||||
//已结算交易服务费
|
||||
$settlePoundageAmount = $modelShopSettlement
|
||||
->where($where)
|
||||
->sum('trade_service_fee');
|
||||
|
||||
return [
|
||||
'settleOrederNum' => $settleOrederNum, //已结算成交订单数
|
||||
'settleOrederAmount' => $settleOrederAmount, //已结算营业额
|
||||
'settleOrederAmountWait' => $settleOrederAmountWait, //待结算营业额
|
||||
'settleDistributionAmount' => $settleDistributionAmount, //已结算分销佣金金额
|
||||
'settleWithdrawalAmount' => $settleWithdrawalAmount, //已结算入账金额
|
||||
'settlePoundageAmount' => $settlePoundageAmount, //已结算交易服务费
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @notes 导出
|
||||
* @param $where
|
||||
* @return array|false
|
||||
* @author 段誉
|
||||
* @date 2022/4/24 11:59
|
||||
*/
|
||||
public static function export($where)
|
||||
{
|
||||
try {
|
||||
$model = new ShopSettlement();
|
||||
$lists = $model->field(true)
|
||||
->where($where)
|
||||
->order('id', 'desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$excelFields = [
|
||||
'settle_sn' => '结算批次号',
|
||||
'deal_order_count' => '已结算成交订单数',
|
||||
'business_money' => '已结算营业额',
|
||||
'refund_order_money' => '退款订单金额',
|
||||
'after_sales_money' => '售后退款金额',
|
||||
'distribution_money' => '已结算分销佣金金额',
|
||||
'entry_account_money' => '已结算入账金额',
|
||||
'create_time' => '结算时间',
|
||||
];
|
||||
|
||||
$export = new ExportExcelServer();
|
||||
$export->setFileName('财务结算');
|
||||
$export->setExportNumber(['settle_sn']);
|
||||
$result = $export->createExcel($excelFields, $lists);
|
||||
|
||||
return ['url' => $result];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
126
app/shop/logic/finance/ShopLogic.php
Normal file
126
app/shop/logic/finance/ShopLogic.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace app\shop\logic\finance;
|
||||
|
||||
|
||||
use app\common\basics\Logic;
|
||||
use app\common\enum\ShopEnum;
|
||||
use app\common\model\shop\ShopAccountLog;
|
||||
use app\common\server\ExportExcelServer;
|
||||
use app\common\server\UrlServer;
|
||||
|
||||
class ShopLogic extends Logic
|
||||
{
|
||||
/**
|
||||
* @Notes: 账户明细
|
||||
* @param $get
|
||||
* @param $shop_id (商家ID)
|
||||
* @return array
|
||||
*/
|
||||
public static function account($get, $shop_id, $is_export = false)
|
||||
{
|
||||
$where[] = ['shop_id', '=', (int)$shop_id];
|
||||
if (isset($get['search_key']) && $get['search_key']) {
|
||||
switch($get['search_key']){
|
||||
case 'settle':
|
||||
$where[] = ['SAL.source_type', '=', ShopAccountLog::settlement_add_money];
|
||||
break;
|
||||
case 'withdrawal':
|
||||
$where[] = ['SAL.source_type', '=', ShopAccountLog::withdrawal_dec_money];
|
||||
break;
|
||||
case 'withdrawal_stay':
|
||||
$where[] = ['SAL.source_type', '=', ShopAccountLog::withdrawal_stay_money];
|
||||
break;
|
||||
case 'withdrawal_error':
|
||||
$where[] = ['SAL.source_type', '=', ShopAccountLog::withdrawal_fail_money];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($get['start_time']) and $get['start_time']) {
|
||||
$where[] = ['SAL.create_time', '>=', strtotime($get['start_time'])];
|
||||
}
|
||||
|
||||
if (!empty($get['end_time']) and $get['end_time']) {
|
||||
$where[] = ['SAL.create_time', '<=', strtotime($get['end_time'])];
|
||||
}
|
||||
|
||||
if (true === $is_export) {
|
||||
return self::export($where);
|
||||
}
|
||||
|
||||
$model = new ShopAccountLog();
|
||||
$lists = $model->alias('SAL')
|
||||
->field(['SAL.*', 'S.name,S.logo,S.type'])
|
||||
->join('shop S', 'S.id = SAL.shop_id')
|
||||
->where($where)
|
||||
->order('create_time','desc')
|
||||
->paginate([
|
||||
'page' => $get['page'],
|
||||
'list_rows' => $get['limit'],
|
||||
'var_page' => 'page'
|
||||
])->toArray();
|
||||
|
||||
|
||||
foreach ($lists['data'] as &$item) {
|
||||
$item['type'] = ShopEnum::getShopTypeDesc($item['type']);
|
||||
$item['source_type'] = ShopAccountLog::getSourceType($item['source_type']);
|
||||
$symbol = $item['change_type'] === 1 ? '+' : '-';
|
||||
$item['change_amount'] = $symbol.$item['change_amount'];
|
||||
$item['logo'] = UrlServer::getFileUrl($item['logo']);
|
||||
}
|
||||
|
||||
return ['count'=>$lists['total'], 'lists'=>$lists['data']];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 导出
|
||||
* @param $where
|
||||
* @return array|false
|
||||
* @author 段誉
|
||||
* @date 2022/4/24 11:59
|
||||
*/
|
||||
public static function export($where)
|
||||
{
|
||||
try {
|
||||
$lists = (new ShopAccountLog())->alias('SAL')
|
||||
->field(['SAL.*', 'S.name,S.logo,S.type'])
|
||||
->join('shop S', 'S.id = SAL.shop_id')
|
||||
->where($where)
|
||||
->order('create_time','desc')
|
||||
->select()->toArray();
|
||||
|
||||
foreach ($lists as &$item) {
|
||||
$item['type'] = ShopEnum::getShopTypeDesc($item['type']);
|
||||
$item['source_type'] = ShopAccountLog::getSourceType($item['source_type']);
|
||||
$symbol = $item['change_type'] === 1 ? '+' : '-';
|
||||
$item['change_amount'] = $symbol.$item['change_amount'];
|
||||
}
|
||||
|
||||
$excelFields = [
|
||||
'name' => '商家名称',
|
||||
'type' => '商家类型',
|
||||
'log_sn' => '明细流水号',
|
||||
'source_sn' => '来源单号',
|
||||
'source_type' => '明细类型',
|
||||
'change_amount' => '变动金额',
|
||||
'left_amount' => '剩余金额',
|
||||
'create_time' => '记录时间',
|
||||
];
|
||||
|
||||
$export = new ExportExcelServer();
|
||||
$export->setFileName('账户明细');
|
||||
$export->setExportNumber(['log_sn', 'source_sn']);
|
||||
$result = $export->createExcel($excelFields, $lists);
|
||||
|
||||
return ['url' => $result];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
self::$error = $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
410
app/shop/logic/finance/WithdrawalLogic.php
Normal file
410
app/shop/logic/finance/WithdrawalLogic.php
Normal file
@ -0,0 +1,410 @@
|
||||
<?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
|
||||
{
|
||||
/**
|
||||
* @Notes: 商家提现列表
|
||||
* @Author: 张无忌
|
||||
* @param $get
|
||||
* @param $shop_id
|
||||
* @return array
|
||||
*/
|
||||
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()];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 财务汇总
|
||||
* @Author: 张无忌
|
||||
* @param $shop_id
|
||||
*/
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 提现详细
|
||||
* @Author: 张无忌
|
||||
* @param $id
|
||||
* @param $shop_id
|
||||
* @return array
|
||||
*/
|
||||
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 ];
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 选项卡与汇总统计详细
|
||||
* @param $shop_id
|
||||
* @return array
|
||||
*/
|
||||
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],
|
||||
['order_status', 'in', [OrderEnum::ORDER_STATUS_DELIVERY,OrderEnum::ORDER_STATUS_GOODS,OrderEnum::ORDER_STATUS_COMPLETE]]
|
||||
])
|
||||
->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, //可提现金额
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @Notes: 申请提现
|
||||
* @Author: 张无忌
|
||||
* @param $post
|
||||
* @param $shop_id
|
||||
* @return bool
|
||||
*/
|
||||
public static function add($post, $shop_id)
|
||||
{
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 1、获取提现条件
|
||||
$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);
|
||||
|
||||
// 2、获取商家信息
|
||||
$shop = (new Shop())->findOrEmpty($shop_id)->toArray();
|
||||
$wallet = $shop['wallet'];
|
||||
|
||||
// 3、验证条件是否满足
|
||||
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 . '元');
|
||||
|
||||
// 4、获取商家提现手续费
|
||||
$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;
|
||||
}
|
||||
|
||||
// 5、创建申请记录
|
||||
$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
|
||||
]);
|
||||
// 6、扣除商家可提现金额
|
||||
Shop::update([
|
||||
'wallet' => ['dec', floatval($post['apply_amount'])],
|
||||
'update_time' => time()
|
||||
], ['id' => $shop_id]);
|
||||
$left_amount = Shop::where(['id' => $shop_id])->value('wallet');
|
||||
// 7、增加提现流水记录(待提现)
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 导出
|
||||
* @param $where
|
||||
* @return array|false
|
||||
* @author 段誉
|
||||
* @date 2022/4/24 11:59
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user