jh-admin/app/model/shop/ShopAccount.php

169 lines
6.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
*/
namespace app\model\shop;
use app\model\BaseModel;
use think\Exception;
/**
* Common: 店铺账户
* Author: wu-hui
* Time: 2024/07/17 14:57
* Class ShopAccount
* @package app\model\shop
*/
class ShopAccount extends BaseModel
{
private $typeName = [
'legumes_integral' => '豆豆积分',
'legumes_integral_community' => '豆豆积分佣金',
];
/**
* Common: 获取指定店铺的收入流水信息列表
* Author: wu-hui
* Time: 2024/07/18 9:23
* @param $page
* @param $params
* @return array
*/
public function getPageList($page, $params)
{
// 生成查询条件
$where = [];
if (isset($params['site_id']) && $params['site_id'] !== '') $where[] = ['a.site_id', '=', $params['site_id']];
if (isset($params['order_no']) && $params['order_no'] !== '') $where[] = ['o.order_no', '=', $params['order_no']];
if (isset($params['status']) && $params['status'] !== '') $where[] = ['a.status', '=', (int)$params['status']];
// 关联查询
$join = [
['order o', 'o.order_id = a.join_id', 'left'],
];
$field = [
'a.*',
'o.order_type',
'o.order_type_name',
'o.order_no'
];
$result = model('shop_account')->pageList($where, $field, 'a.id DESC', $page, PAGE_LIST_ROWS, 'a', $join);
return $this->success($result);
}
/**
* Common: 订单抵扣积分 结算到店铺
* Author: wu-hui
* Time: 2024/07/17 16:19
* @param $orderId
*/
public function orderIntegralSettlement($orderId)
{
// 订单信息获取
$field = 'order_id,site_id,legumes_integral_money,legumes_integral_community,order_type';
$orderInfo = model('order')->getInfo(['order_id' => $orderId], $field);
// 计算:当豆豆积分抵扣金额 - 平台服务与技术抵扣佣金 不等于0时进行积分变更操作增加 or 减少。如果等于0则无变更操作
$changeIntegral = (float)sprintf("%.2f", $orderInfo['legumes_integral_money'] - $orderInfo['legumes_integral_community']);
if ($changeIntegral != 0) {
// 增加待结算流水信息
$params = [
'site_id' => $orderInfo['site_id'],
'join_id' => $orderInfo['order_id'],
'type' => 'legumes_integral',
'money' => $changeIntegral,
'remark' => '订单使用积分结算到收入'
];
$id = $this->addPendingSettlement($params);
// 判断:如果是收银台信息 则立即结算
if ((int)$orderInfo['order_type'] == 5) {
$this->accountSettlement($id, $orderId);
}
} else {
trace(['order_id' => $orderId], '店铺收入 - 订单抵扣积分,结算到店铺 - 无结算积分');
}
}
/**
* Common: 增加待结算记录
* Author: wu-hui
* Time: 2024/07/17 15:50
* @param array $params
* @return int|string|void
*/
public function addPendingSettlement(array $params)
{
model('shop_account')->startTrans();
try {
// 判断:流水信息是否已经存在
$isHas = (int)model('shop_account')->getValue([
'site_id' => $params['site_id'],
'join_id' => $params['join_id'],
'account_type' => $params['type'],
], 'id');
if ($isHas > 0) return $isHas;
// 增加流水记录
$id = model('shop_account')->add([
'site_id' => $params['site_id'],
'join_id' => $params['join_id'],
'account_type' => $params['type'],
'account_type_name' => $this->typeName[$params['type']] ?? '',
'account_data' => $params['money'],
'status' => 0,
'remark' => $params['remark']
]);
model('shop_account')->commit();
return $id;
} catch (\Exception $e) {
model('shop_account')->rollback();
trace(['params' => $params, 'msg' => $e->getMessage()], '店铺收入 - 增加待结算记录 - 错误');
}
}
/**
* Common: 流水结算
* Author: wu-hui
* Time: 2024/07/17 16:17
* @param int $id
* @param int $orderId
*/
public function accountSettlement(int $id = 0, int $orderId = 0)
{
model('shop_account')->startTrans();
try {
// 获取信息
$accountInfo = [];
if ($id > 0) $accountInfo = model('shop_account')->getInfo(['id' => $id, 'status' => 0]);
else if ($orderId > 0) $accountInfo = model('shop_account')->getInfo(['join_id' => $orderId, 'status' => 0]);
if (!$accountInfo) throw new Exception('不存在未结算的流水!');
$shopInfo = model('shop')->getInfo(['site_id' => $accountInfo['site_id']], 'total_money,withdrawable_money');
if (!$shopInfo) throw new Exception('店铺信息不存在!');
// 修改店铺收入
model('shop')->update([
'total_money' => (float)sprintf("%.2f", (float)$shopInfo['total_money'] + (float)$accountInfo['account_data']),
'withdrawable_money' => (float)sprintf("%.2f", (float)$shopInfo['withdrawable_money'] + (float)$accountInfo['account_data']),
], [
['site_id', '=', $accountInfo['site_id']]
]);
// 修改结算信息
model('shop_account')->update([
'status' => 1
], [
['id', '=', $accountInfo['id']]
]);
model('shop_account')->commit();
} catch (\Exception $e) {
model('shop_account')->rollback();
trace(['id' => $id, 'order_id' => $orderId, 'msg' => $e->getMessage()], '店铺收入 - 流水结算 - 错误');
}
}
}