186 lines
7.3 KiB
PHP
186 lines
7.3 KiB
PHP
<?php
|
|
/**
|
|
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
|
|
* =========================================================
|
|
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
|
|
* ----------------------------------------------
|
|
* 官方网址: https://www.gobuysaas.com
|
|
* =========================================================
|
|
*/
|
|
|
|
namespace addon\store\model;
|
|
|
|
use app\model\BaseModel;
|
|
use app\model\order\OrderCommon;
|
|
|
|
/**
|
|
* 门店订单与结算
|
|
*/
|
|
class Settlement extends BaseModel
|
|
{
|
|
|
|
/**
|
|
* 店铺门店结算
|
|
* @param $site_id
|
|
* @param $end_time
|
|
* @return array
|
|
*/
|
|
public function settlement($site_id, $end_time)
|
|
{
|
|
//获取最近的店铺结算时间
|
|
$last_time = model('shop')->getInfo([ [ 'site_id', '=', $site_id ] ], 'store_settlement_time');
|
|
$start_time = $last_time[ 'store_settlement_time' ];
|
|
|
|
//查询门店结算配置
|
|
$config_model = new Config();
|
|
$config = $config_model->getStoreWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
|
|
if ($config[ 'is_settlement' ] != 1) {
|
|
return $this->success();
|
|
}
|
|
$period_type = $config[ 'period_type' ];
|
|
//结算周期为4的话是立即结算
|
|
if ($period_type == 4)
|
|
return $this->success();
|
|
|
|
$period_type = array ( 1 => 'day', 2 => 'week', 3 => 'month' )[ $period_type ] ?? 'day';
|
|
//只有开启周期性结算开关,才会进行
|
|
|
|
if (( $end_time - $start_time ) < ( 3600 * 20 )) {
|
|
return $this->success();
|
|
}
|
|
|
|
//店铺列表
|
|
$store_list = model('store')->getList([ [ 'site_id', '=', $site_id ] ], 'site_name, store_id, store_name, account');
|
|
model('store_settlement')->startTrans();
|
|
try {
|
|
$store_withdraw_model = new StoreWithdraw();
|
|
//循环各个店铺数据
|
|
foreach ($store_list as $k => $store) {
|
|
$store_id = $store[ 'store_id' ];
|
|
$online_settlement = model('order')->getInfo([
|
|
[ 'order_status', '=', 10 ],
|
|
[ 'is_settlement', '=', 0 ],
|
|
[ 'store_id', '=', $store_id ],
|
|
[ 'finish_time', '<=', $end_time ],
|
|
[ 'pay_type', '<>', 'OFFLINE_PAY' ],//todo 支付方式应该都可以
|
|
], 'sum(pay_money) as order_money, sum(refund_money) as refund_money, sum(commission) as commission');
|
|
$offline_settlement = model('order')->getInfo([
|
|
[ 'order_status', '=', 10 ],
|
|
[ 'is_settlement', '=', 0 ],
|
|
[ 'store_id', '=', $store_id ],
|
|
[ 'finish_time', '<=', $end_time ],
|
|
[ 'pay_type', '=', 'OFFLINE_PAY' ],
|
|
], 'sum(pay_money) as offline_order_money, sum(refund_money) as offline_refund_money');
|
|
//周期性结算会将所有抽成全部提现
|
|
$store_commission = $store[ 'account' ];
|
|
$settlement = [
|
|
'settlement_no' => date('YmdHi') . $store_id . rand(1111, 9999),
|
|
'site_id' => $site_id,
|
|
'site_name' => $store[ 'site_name' ],
|
|
'store_id' => $store_id,
|
|
'store_name' => $store[ 'store_name' ],
|
|
'order_money' => $online_settlement[ 'order_money' ] ?? 0,
|
|
'refund_money' => $online_settlement[ 'refund_money' ] ?? 0,
|
|
'commission' => $online_settlement[ 'commission' ] ?? 0,
|
|
'offline_order_money' => $offline_settlement[ 'offline_order_money' ] ?? 0,
|
|
'offline_refund_money' => $offline_settlement[ 'offline_refund_money' ] ?? 0,
|
|
'create_time' => $end_time,
|
|
'start_time' => $start_time,
|
|
'end_time' => $end_time,
|
|
'store_commission' => $store_commission
|
|
];
|
|
|
|
// 没有可提现金额的时候,关联的提现id为0, 默认认为已经结算提现成功
|
|
if ($store_commission > 0) {
|
|
//门店主动提现所有分成
|
|
$apply_params = array (
|
|
'site_id' => $site_id,
|
|
'store_id' => $store_id,
|
|
'money' => $store_commission,
|
|
'settlement_type' => $period_type
|
|
);
|
|
$withdraw_result = $store_withdraw_model->apply($apply_params);
|
|
$withdraw_id = $withdraw_result[ 'data' ];
|
|
$settlement[ 'withdraw_id' ] = $withdraw_id;
|
|
}
|
|
|
|
$store_settlement_id = model('store_settlement')->add($settlement);
|
|
model('order')->update([ 'store_settlement_id' => $store_settlement_id, 'is_settlement' => 1 ], [
|
|
[ 'order_status', '=', 10 ],
|
|
[ 'is_settlement', '=', 0 ],
|
|
[ 'store_id', '=', $store_id ],
|
|
[ 'finish_time', '<=', $end_time ],
|
|
// ['pay_type', 'not in', OrderCommon::$online_pay_type],
|
|
]);
|
|
|
|
}
|
|
model('shop')->update([ 'store_settlement_time' => $end_time ], [ [ 'site_id', '=', $site_id ] ]);
|
|
model('store_settlement')->commit();
|
|
return $this->success();
|
|
} catch (\Exception $e) {
|
|
model('store_settlement')->rollback();
|
|
return $this->error($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* getSettlementInfo 获取详情
|
|
* @param $condition
|
|
* @param string $fields
|
|
* @return array
|
|
*/
|
|
public function getSettlementInfo($condition, $fields = '*')
|
|
{
|
|
$res = model('store_settlement')->getInfo($condition, $fields);
|
|
return $this->success($res);
|
|
}
|
|
|
|
/**
|
|
* 修改结算记录
|
|
* @param
|
|
*/
|
|
public function editSettlement($data, $condition)
|
|
{
|
|
$res = model('store_settlement')->update($data, $condition);
|
|
return $this->success($res);
|
|
}
|
|
|
|
/**
|
|
* 获取店铺待结算订单金额
|
|
*/
|
|
public function getStoreSettlementData($condition = [])
|
|
{
|
|
$money_info = model('order')->getInfo($condition, 'sum(order_money) as order_money, sum(refund_money) as refund_money, sum(shop_money) as shop_money, sum(platform_money) as platform_money, sum(refund_shop_money) as refund_shop_money, sum(refund_platform_money) as refund_platform_money, sum(commission) as commission');
|
|
if (empty($money_info) || $money_info == null) {
|
|
|
|
$money_info = [
|
|
'order_money' => 0,
|
|
'refund_money' => 0,
|
|
'shop_money' => 0,
|
|
'platform_money' => 0,
|
|
'refund_shop_money' => 0,
|
|
'refund_platform_money' => 0,
|
|
'commission' => 0
|
|
];
|
|
|
|
}
|
|
|
|
return $money_info;
|
|
}
|
|
|
|
/**
|
|
* 获取店铺结算周期结算分页列表
|
|
* @param array $condition
|
|
* @param int $page
|
|
* @param int $page_size
|
|
* @param string $order
|
|
* @param string $field
|
|
* @return array
|
|
*/
|
|
public function getStoreSettlementPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
|
|
{
|
|
|
|
$list = model('store_settlement')->pageList($condition, $field, $order, $page, $page_size);
|
|
return $this->success($list);
|
|
}
|
|
} |