cxfoot/modules/api/models/RecordForm.php
2023-11-22 11:17:56 +08:00

235 lines
6.6 KiB
PHP

<?php
/**
* @author Any
* @description KISS
* @date 2020-12-2
* @version 1.0.0
*
* _____LOG_____
*
*/
namespace app\modules\api\models;
use app\models\Report;
use function AlibabaCloud\Client\value;
use app\components\FlashStorage;
use app\components\SiteHelper;
use app\models\BallCart;
use app\models\Coach;
use app\models\DeviceUniqueBindUser;
use app\models\Order;
use app\models\Store;
use app\models\StoreUser;
use app\models\User;
use app\components\auth\AToken;
use app\components\EncryptHelper;
use app\modules\api\components\ApiHelper;
use app\modules\api\components\GetDistance;
use yii\data\Pagination;
class RecordForm extends ApiModel
{
public $store_id;
public $coach_id;
public $cx_mch_id;
public $user_id;
public $user_type;
public $store_type;
//门店管理员-2 总部管理员-8 BOSS管理员-7
public $type = [2, 7, 8];
public $id;
public function rules()
{
return [
[['limit', 'page'], 'integer'],
[['page'], 'default', 'value' => 1],
];
}
/**
* 数据列表
*/
public function search()
{
if (!in_array($this->user_type, $this->type)) {
return $this->apiReturnError('账号错误');
}
//门店管理员 1-门店管理员 2-门店服务员 3-门店财务人员
if ($this->user_type == 2) {
$storeUser = ApiHelper::findOneUserStoreId($this->user_id);
if ($storeUser['user_type'] == 2) {
return $this->apiReturnError('账号错误');
}
if (empty($this->store_id)) {
return $this->apiReturnError('门店ID不能为空');
}
}
//门店总数
$countStore = Store::find()->count('id');
//球车总数
$ballCartCount = BallCart::find()->andFilterWhere(['store_id' => $this->store_id])->count('id');
$orderData = Order::find()->andFilterWhere([
'store_id' => $this->store_id
])->andWhere(['is_delete' => 0, 'is_pay' => '1']);
//总租赁次数
$order_count = $orderData->count();
// $orderData = array_column($orderData, 'total_pay_price');
//总收入
$order_money = $orderData->sum('total_pay_price');
$data = [
'share_money' => 0,
'store_num' => $countStore, //门店总数
'ball_num' => $ballCartCount, //球车总数
'money' => round($order_money, 2), //总收入
'lease_num' => $order_count //总租赁次数
];
return [
'code' => 0,
'msg' => 'ok',
'data' => $data,
];
}
/**
* 获取月份租赁次数
* @param $date /月份
*/
public function getLessNum($date)
{
if (!$date) {
return $this->apiReturnError('时间错误');
}
$date = strtotime($date);
$riqi = $this->getMonthBeginAndEnd($date);
$start_date = $riqi['begin'];
$end_date = $riqi['end'];
$store_id = $this->store_id ? $this->store_id : 0;
$arr = [];
$arr[] = $this->countLess($start_date, $end_date, $store_id);
if ($store_id > 0) {
$where = ['store_id' => $this->store_id];
}
$count = Order::find()->where(['between', 'created_at', $start_date, $end_date])->andFilterWhere($where)->andWhere([
'is_delete' => 0,
'is_pay' => 1,
])->count('id');
return [
'code' => 0,
'msg' => 'ok',
'data' => $arr,
'count' => $count
];
}
/**
* 获取球车租赁收入
*/
public function getMoneySum($date)
{
if (!$date) {
return $this->apiReturnError('时间错误');
}
$date = strtotime($date);
$riqi = $this->getMonthBeginAndEnd($date);
$start_date = $riqi['begin'];
$end_date = $riqi['end'];
$store_id = $this->store_id ? $this->store_id : 0;
$arr = [];
$arr[] = $this->sumMoney($start_date, $end_date, $store_id);
if ($store_id > 0) {
$where = ['store_id' => $this->store_id];
}
$sum = Order::find()->where(['between', 'created_at', $start_date, $end_date])->andFilterWhere($where)->andWhere([
'is_delete' => 0,
'is_pay' => 1,
])->sum('total_pay_price');
$sum = $sum ? $sum : 0;
return [
'code' => 0,
'msg' => 'ok',
'data' => $arr,
'money' => $sum
];
}
/**
*
* 获取指定月份的开始时间戳跟结束时间戳
* @param int $timestamp
* @return array
*/
function getMonthBeginAndEnd($timestamp = 0)
{
$timestamp = $timestamp ? $timestamp : time();
$year = date('Y', $timestamp);
$month = date('m', $timestamp);
$d = date('t', strtotime($year . '-' . $month));
return ['begin' => strtotime($year . '-' . $month), 'end' => mktime(23, 59, 59, $month, $d, $year)];
}
/**
* 统计租赁次数
*/
public function countLess($start_date, $end_date, $store_id)
{
if ($store_id != 0) {
$where = " AND (`store_id` = $store_id) ";
}
$sql = "SELECT from_unixtime(`created_at`, '%Y-%m-%d') AS `riqi`, count(`id`) AS `count`
FROM `cx_order` WHERE(`created_at` BETWEEN $start_date AND $end_date) $where AND (`is_delete` = 0) AND (`is_pay` = '1') GROUP BY from_unixtime(`created_at`, '%Y-%m-%d')";
$query = (new Order())->find();
$query->sql = $sql;
$create = $query->createCommand();
$record = $create->queryAll();
return $record;
}
/**
* 统计租赁金额
*/
public function sumMoney($start_date, $end_date, $store_id)
{
if ($store_id != 0) {
$where = " AND (`store_id` = $store_id) ";
}
$sql = "SELECT from_unixtime(`created_at`, '%Y-%m-%d') AS `riqi`, SUM(`total_pay_price`) AS `sum_money`
FROM `cx_order` WHERE(`created_at` BETWEEN $start_date AND $end_date) $where AND (`status` = 0) AND (`is_delete` = 0) AND (`is_pay` = '1') GROUP BY from_unixtime(`created_at`, '%Y-%m-%d')";
$query = (new Order())->find();
$query->sql = $sql;
$create = $query->createCommand();
$record = $create->queryAll();
return $record;
}
}