228 lines
8.8 KiB
PHP
228 lines
8.8 KiB
PHP
<?php
|
|
/**
|
|
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
|
|
* =========================================================
|
|
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
|
|
* ----------------------------------------------
|
|
* 官方网址: https://www.gobuysaas.com
|
|
* =========================================================
|
|
*/
|
|
|
|
namespace addon\store\model;
|
|
|
|
use app\model\BaseModel;
|
|
use think\facade\Db;
|
|
|
|
class StoreAccount extends BaseModel
|
|
{
|
|
public $period_types = [1, 2, 3];//转账周期类型1.天 2. 周 3. 月
|
|
|
|
public $from_type = [
|
|
'order' => [
|
|
'type_name' => '门店结算',
|
|
'type_url' => '',
|
|
],
|
|
'refund' => [
|
|
'type_name' => '订单退款',
|
|
'type_url' => '',
|
|
],
|
|
'withdraw' => [
|
|
'type_name' => '提现',
|
|
'type_url' => '',
|
|
],
|
|
];
|
|
|
|
/**
|
|
* 获取门店转账设置
|
|
*/
|
|
public function getStoreWithdrawConfig($site_id)
|
|
{
|
|
$config = new Config();
|
|
$res = $config->getStoreWithdrawConfig($site_id);
|
|
return $res;
|
|
}
|
|
|
|
/**
|
|
* 获取门店待结算订单金额
|
|
*/
|
|
public function getWaitSettlementInfo($store_id)
|
|
{
|
|
$money_info = model('order')->getInfo([
|
|
['store_id', '=', $store_id],
|
|
['order_status', '=', 10],
|
|
['store_settlement_id', '=', 0]
|
|
], '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 $params
|
|
*/
|
|
public function addStoreAccount($params)
|
|
{
|
|
$site_id = $params['site_id'];
|
|
$store_id = $params['store_id'];
|
|
$account_data = $params['account_data'];
|
|
$remark = $params['remark'];
|
|
$from_type = $params['from_type'];
|
|
$related_id = $params['related_id'];
|
|
$is_limit = $params['is_limit'] ?? 1;//是否限制不能小于0
|
|
model('store_account')->startTrans();
|
|
try {
|
|
//账户检测
|
|
$store_account = Db::name('store')->where([
|
|
['store_id', '=', $store_id],
|
|
['site_id', '=', $site_id]
|
|
])->field('account')->lock(true)->find();
|
|
$account_new_data = round((float)$store_account['account'] + (float)$account_data, 2);
|
|
if ($is_limit == 1 && (float)$account_new_data < 0) {
|
|
model('store_account')->rollback();
|
|
$msg = '账户余额不足';
|
|
return $this->error('', $msg);
|
|
}
|
|
//添加记录
|
|
$type_info = $this->from_type[$from_type];
|
|
$data = array(
|
|
'site_id' => $site_id,
|
|
'store_id' => $store_id,
|
|
'account_data' => $account_data,
|
|
'from_type' => $from_type,
|
|
'type_name' => $type_info['type_name'],
|
|
'create_time' => time(),
|
|
'remark' => $remark,
|
|
'related_id' => $related_id,
|
|
);
|
|
|
|
model('store_account')->add($data);
|
|
//账户更新
|
|
model('store')->update(['account' => $account_new_data], [
|
|
['store_id', '=', $store_id]
|
|
]);
|
|
model('store_account')->commit();
|
|
return $this->success();
|
|
} catch (\Exception $e) {
|
|
model('store_account')->rollback();
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取账户分页列表
|
|
* @param array $condition
|
|
* @param int $page
|
|
* @param int $page_size
|
|
* @param string $order
|
|
* @param string $field
|
|
* @return array|\multitype
|
|
*/
|
|
public function getStoreAccountPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc,id desc', $field = '*', $alias = 'a', $join = [])
|
|
{
|
|
$list = model('store_account')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
|
|
return $this->success($list);
|
|
}
|
|
|
|
/**
|
|
* 获取账户列表
|
|
* @param array $condition
|
|
* @param string $field
|
|
* @param string $order
|
|
* @param null $limit
|
|
* @return array|\multitype
|
|
*/
|
|
public function getStoreAccountList($condition = [], $field = '*', $order = '', $limit = null)
|
|
{
|
|
$list = model('store_account')->getList($condition, $field, $order, '', '', '', $limit);
|
|
return $this->success($list);
|
|
}
|
|
|
|
/***
|
|
* 获取门店分账
|
|
* @param $data
|
|
* @return void
|
|
*/
|
|
public function getOrderAccounts($order_info = [])
|
|
{
|
|
if (isset($order_info['store_id'])) {
|
|
$store_id = $order_info['store_id'];
|
|
$config = new Config();
|
|
$configInfo = $config->getStoreWithdrawConfig($order_info['site_id'])['data']['value'];
|
|
if ($configInfo['is_settlement'] == 1) {
|
|
$store_info = model('store')->getInfo([['store_id', '=', $store_id]], '*');
|
|
if (!$store_info['aliapy_auto_settlement']) return [];
|
|
$settlement_rate = $configInfo['settlement_rate'];
|
|
if ($store_info['settlement_rate'] == 1) {
|
|
$settlement_rate = $store_info['settlement_rate'];
|
|
}
|
|
$bank_type = $store_info['bank_type'];
|
|
$order_money = $order_info['order_money'];
|
|
//todo 可能还有退款
|
|
$settlement_cost = $configInfo['settlement_cost'];
|
|
$settlement_cost_array = explode(',', $settlement_cost);
|
|
$base_money = $order_money;
|
|
$base_money -= $order_info['refund_money'] ?? 0;//todo 这儿涉及到一个问题,退款的金额里面包含一部分余额,可能结算金额不太一致
|
|
$base_money = $base_money < 0 ? 0 : $base_money;
|
|
if (in_array('balance', $settlement_cost_array)) {
|
|
$balance_money = $order_info['balance_money'];
|
|
$base_money -= $balance_money;
|
|
}
|
|
if (in_array('point', $settlement_cost_array)) {
|
|
$point_money = $order_info['point_money'];
|
|
$base_money += $point_money;
|
|
}
|
|
if (in_array('fenxiao_commission', $settlement_cost_array)) {
|
|
$commission = $order_info['commission'];
|
|
$base_money -= $commission;
|
|
}
|
|
//优惠券比较特殊, 不过不扣除要价格优惠券抵扣金额加回去
|
|
if (!in_array('coupon', $settlement_cost_array)) {
|
|
$coupon_money = $order_info['coupon_money'];
|
|
$base_money += $coupon_money;
|
|
}
|
|
if ($base_money > 0) {
|
|
$amount = $base_money * $settlement_rate / 100; //默认抽佣
|
|
$account = [];
|
|
if ($bank_type == 2) {
|
|
$acc = [
|
|
'site_id' => $order_info['site_id'],
|
|
'ag_site_id' => 0,
|
|
'is_platform' => 5,
|
|
'order_id' => $order_info['order_id'],
|
|
'member_id' => $order_info['member_id'],
|
|
'out_trade_no' => $order_info['out_trade_no'],
|
|
'trade_no' => $order_info['trade_no'] ?? '', //订单号
|
|
'is_video_number' => $order_info['is_video_number'] ?: 0,
|
|
'fee_commission' => $settlement_rate, //佣金比例
|
|
'pay_money' => $order_info['pay_money'],//支付金额,
|
|
'amount' => $amount,
|
|
'account_type' => 'loginName', //授权账号类型 loginName登录账号 userId 商户PID
|
|
'channel_type' => $order_info['order_from'],
|
|
'promotion_type' => $order_info['promotion_type'],
|
|
'reason' => '门店利润分账',
|
|
'realname' => $store_info['bank_user_name'],
|
|
'account' => $store_info['bank_type_account'],
|
|
];
|
|
$account[] = $acc;
|
|
return $account;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|