jingcai-php/app/Http/Controllers/Api/Seller/ReportController.php

1417 lines
50 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* @createtime 2023/5/19
* @author wild
* @copyright PhpStorm
*/
namespace App\Http\Controllers\Api\Seller;
use App\Console\Commands\Report\GenerateDaySeller;
use App\Console\Commands\Report\GenerateDayShop;
use App\Enums\BillType;
use App\Enums\BoolEnum;
use App\Enums\ClientType;
use App\Enums\LottState;
use App\Enums\OrderType;
use App\Enums\PayState;
use App\Model\AgentSale;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerBill;
use App\Model\Customer\CustomerRecharge;
use App\Model\LotteryType;
use App\Model\Order;
use App\Model\Report\ReportDaySeller;
use App\Model\Report\ReportDayShop;
use App\Model\Seller\Seller;
use App\Model\Seller\Shop;
use App\Model\Seller\ShopBill;
use App\Model\Seller\ShopCooperateBill;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
class ReportController extends BaseController
{
/**
* @api {GET} /api/seller/seller/report/dashboard 数据统计
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [seller_id] 员工id(用员工列表接口)
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "seller":{ // 单个员工
* "seller_id":1,
* "shop_id":1,
* "cdate":"20230520", // 日期
* "chupiao_money":0, // 出票
* "send_prize":0, // 派奖
* "incr_money":0, // 手工加款
* "reduce_money":0, // 手工扣款
* "updated_at":"2023-05-20 09:54:43",
* "created_at":"2023-05-20 09:54:43",
* "id":4
* }
* "current":{ // 当前查询日
* "id":9,
* "cdate":"20230520", // 日期
* "shop_id":1,
* "register_num":0, // 注册用户数
* "buyer_num":0, // 购彩用户数
* "shop_banlance":"1099.88", // 店铺余额
* "customers_balance":"13792.24", // 托管余额
* "send_prize":0, // 派奖
* "chupiao_money":0, // 出票
* "chupiao_fee_money":0, // 出票服务费
* "reduce_money":0, // 手工扣款
* "incr_money":0, // 手工加款
* "created_at":"2023-05-20 09:50:36",
* "updated_at":"2023-05-20 09:52:00",
* "deleted_at":null,
* "cdate_str":"05月20日" // 日期
* },
* "last":{ // 查询日前一天
* }
* }
* }
*/
public function dashboard(Request $request)
{
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$sellerId = $request->input('seller_id');
if (!$dateStart) {
$dateStart = date('Y-m-d');
} else {
$dateStart = date('Y-m-d', strtotime($dateStart));
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
} else {
$dateEnd = date('Y-m-d', strtotime($dateEnd));
}
$currentDay = date('Y-m-d');
$isToday = false;
if ($currentDay >= $dateStart && $currentDay <= $dateEnd) {
$isToday = true;
}
/** @var Seller $seller */
$seller = $this->seller();
if (!$seller->isAdmin()) {
$sellerId = $seller->id;
}
$receiveQuery = Order::where('shop_id', $this->shopId())
->where('receive_user_id', '>', 0);
if ($sellerId) {
$receiveQuery = $receiveQuery->where('receive_user_id', $sellerId);
}
$receiveData = $receiveQuery->count();
$cdate = date('Ymd', strtotime($dateStart));
// 店员的统计信息
if ($sellerId) {
$seller = Seller::where('shop_id', $this->shopId())->find($sellerId);
ThrowException::isTrue(!$seller, '店员不存在');
if ($isToday) {
$generateDaySeller = new GenerateDaySeller();
$generateDaySeller->report($seller, date('Ymd'));
}
$sellerReport = ReportDaySeller::select([
DB::raw('sum(chupiao_money) as chupiao_money'),
DB::raw('sum(send_prize) as send_prize'),
DB::raw('sum(win_prize) as win_prize'),
DB::raw('sum(incr_money) as incr_money'),
DB::raw('sum(reduce_money) as reduce_money'),
])
->where('seller_id', $seller->id)
->where('cdate', '>=', date('Ymd', strtotime($dateStart)))
->where('cdate', '<=', date('Ymd', strtotime($dateEnd)))
->first();
return $this->jsonSuccess([
'report' => $sellerReport,
'jiedan_num' => $receiveData,
'jiedan_list' => [],
]);
}
$shop = Shop::find($this->shopId());
ThrowException::isTrue(!$shop, '数据异常s!');
$generateDayShop = new GenerateDayShop();
$currentReport = $generateDayShop->report($shop, date('Ymd'));
$lastReport = ReportDayShop::where('shop_id', $shop->id)
->where('cdate', date('Ymd', strtotime('-1 day')))
->first();
$report = ReportDayShop::select([
DB::raw('sum(register_num) as register_num'),
DB::raw('sum(buyer_num) as buyer_num'),
DB::raw('sum(shop_banlance) as shop_banlance'),
DB::raw('sum(customers_balance) as customers_balance'),
DB::raw('sum(send_prize) as send_prize'),
DB::raw('sum(customer_withdraw) as customer_withdraw'),
DB::raw('sum(win_prize) as win_prize'),
DB::raw('sum(chupiao_money) as chupiao_money'),
DB::raw('sum(chupiao_fee_money) as chupiao_fee_money'),
DB::raw('sum(reduce_money) as reduce_money'),
DB::raw('sum(incr_money) as incr_money'),
DB::raw('sum(customer_recharge) as customer_recharge'),
DB::raw('sum(cooperate_brokerage) as cooperate_brokerage'),
])
->where('shop_id', $shop->id)
->where('cdate', '>=', date('Ymd', strtotime($dateStart)))
->where('cdate', '<=', date('Ymd', strtotime($dateEnd)))
->first();
$sql = 'select customer_id, sum(money) as money, count(*) as count from `order` where shop_id=? and created_date >=? and created_date<=? and lottery_state in (1,2,3,4,5,7) group by customer_id';
$res = DB::select($sql, [$shop->id, date('Ymd', strtotime($dateStart)), date('Ymd', strtotime($dateEnd))]);
$report->buyer_num = count($res);
return $this->jsonSuccess([
'current' => $currentReport,
'last' => $lastReport,
'report' => $report,
'jiedan_num' => $receiveData,
'jiedan_list' => [],
]);
}
/**
* @api {GET} /api/seller/seller/report/chupiao 数据统计-出票
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} [date_start] 开始时间 默认当天
* @apiParam {String} [date_end] 结束时间
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "list":[
* {
* "money":"8.0000", // 金额
* "count":2, // 订单数
* "lottery_type_id":2,
* "lottery_type":{ // 彩种
* "id":2,
* "type":"jclq",
* "status":1,
* "name":"竞彩篮球",
* "icon":"",
* "info":"1场赛事可投",
* "created_at":null,
* "updated_at":"2023-07-26 21:33:23",
* "deleted_at":null
* },
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":null,
* "gendan_brokerage":null,
* "order_closed":false,
* "lottery_is_wined":false,
* "money_show":8,
* "union_state_show":"",
* "close_time_str":""
* }
* ],
* "total_money":8 // 金额总数
* }
* }
*/
public function chupiao(Request $request)
{
$this->checkMasterNext();
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
if (!$dateStart) {
$dateStart = date('Ymd');
} else {
$dateStart = date('Ymd', strtotime($dateStart));
}
if ($dateEnd) {
$dateEnd = date('Ymd', strtotime($dateEnd));
}
$query = Order::select([
DB::raw('sum(IF(type = 3, union_money, money)) as money'),
DB::raw('count(*) as count'),
'lottery_type_id',
])
->usable()
->where('draft_shop_id', $this->shopId());
if ($dateStart) {
$query->where('draft_date', '>=', $dateStart);
}
if ($dateEnd) {
$query->where('draft_date', '<=', $dateEnd);
}
$lotteryTypes = LotteryType::get()->keyBy('id');
$res = $query->groupBy('lottery_type_id')->get();
$totalMoney = 0;
foreach ($res as $item) {
$totalMoney += $item->money;
$item->lottery_type = Arr::get($lotteryTypes, $item->lottery_type_id);
}
return $this->jsonSuccess([
'list' => $res,
'total_money' => $totalMoney,
]);
}
/**
* @api {GET} /api/seller/seller/report/paijiang 数据统计-派奖
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} [date_start] 开始时间 默认当天
* @apiParam {String} [date_end] 结束时间
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "list":[
* {
* "money":"8.0000", // 金额
* "count":2, // 订单数
* "lottery_type_id":2,
* "lottery_type":{ // 彩种
* "id":2,
* "type":"jclq",
* "status":1,
* "name":"竞彩篮球",
* "icon":"",
* "info":"1场赛事可投",
* "created_at":null,
* "updated_at":"2023-07-26 21:33:23",
* "deleted_at":null
* },
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":null,
* "gendan_brokerage":null,
* "order_closed":false,
* "lottery_is_wined":false,
* "money_show":8,
* "union_state_show":"",
* "close_time_str":""
* }
* ],
* "total_money":8 // 金额总数
* }
* }
*/
public function paijiang(Request $request)
{
$this->checkMasterNext();
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
if (!$dateStart) {
$dateStart = date('Ymd');
} else {
$dateStart = date('Ymd', strtotime($dateStart));
}
if ($dateEnd) {
$dateEnd = date('Ymd', strtotime($dateEnd));
}
$query = Order::select([
DB::raw('sum(money) as money'),
DB::raw('sum(lottery_send_prize) as lottery_send_prize'),
DB::raw('count(*) as count'),
'lottery_type_id',
])
->where('send_date', '>', 0)
->where('draft_shop_id', $this->shopId());
if ($dateStart) {
$query->where('send_date', '>=', $dateStart);
}
if ($dateEnd) {
$query->where('send_date', '<=', $dateEnd);
}
$lotteryTypes = LotteryType::get()->keyBy('id');
$res = $query->groupBy('lottery_type_id')->get();
// $totalMoney = 0;
$totalSendPrize = 0;
foreach ($res as $item) {
// $totalMoney += $item->money;
$totalSendPrize += $item->lottery_send_prize;
$item->lottery_type = Arr::get($lotteryTypes, $item->lottery_type_id);
}
return $this->jsonSuccess([
'list' => $res,
'total_money' => $totalSendPrize,
]);
}
/**
* @api {GET} /api/seller/seller/report/win_list 数据统计-中奖列表
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} [date_start] 开始时间 默认当天
* @apiParam {String} [date_end] 结束时间
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "list":[
* {
* "money":"8.0000", // 金额
* "count":2, // 订单数
* "lottery_type_id":2,
* "lottery_type":{ // 彩种
* "id":2,
* "type":"jclq",
* "status":1,
* "name":"竞彩篮球",
* "icon":"",
* "info":"1场赛事可投",
* "created_at":null,
* "updated_at":"2023-07-26 21:33:23",
* "deleted_at":null
* },
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":null,
* "gendan_brokerage":null,
* "order_closed":false,
* "lottery_is_wined":false,
* "money_show":8,
* "union_state_show":"",
* "close_time_str":""
* }
* ],
* "total_money":8 // 金额总数
* }
* }
*/
public function winList(Request $request)
{
$this->checkMasterNext();
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
if (!$dateStart) {
$dateStart = date('Ymd');
} else {
$dateStart = date('Ymd', strtotime($dateStart));
}
if ($dateEnd) {
$dateEnd = date('Ymd', strtotime($dateEnd));
}
$query = Order::select([
DB::raw('sum(money) as money'),
DB::raw('sum(lottery_should_send_prize) as lottery_should_send_prize'),
DB::raw('count(*) as count'),
'lottery_type_id',
])
->where('draft_date', '>', 0)
->whereIn('lottery_state', [LottState::WIN, LottState::SEND])
->where('draft_shop_id', $this->shopId());
if ($dateStart) {
$query->where('draft_date', '>=', $dateStart);
}
if ($dateEnd) {
$query->where('draft_date', '<=', $dateEnd);
}
$lotteryTypes = LotteryType::get()->keyBy('id');
$res = $query->groupBy('lottery_type_id')->get();
// $totalMoney = 0;
$totalSendPrize = 0;
foreach ($res as $item) {
// $totalMoney += $item->money;
$totalSendPrize += $item->lottery_should_send_prize;
$item->lottery_type = Arr::get($lotteryTypes, $item->lottery_type_id);
}
return $this->jsonSuccess([
'list' => $res,
'total_money' => $totalSendPrize,
]);
}
/**
* @api {GET} /api/seller/seller/report/order_list 数据统计-派奖/出票订单列表
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} type chupiao|paijiang
* @apiParam {Int} lottery_type_id 彩种类型id
* @apiParam {String} [date_start] 开始时间 默认当天
* @apiParam {String} [date_end] 结束时间
* @apiSuccessExample {json} 返回结果,参考订单列表
* {
* "code":200,
* "message":"",
* "data":{}
* }
*/
public function orderList(Request $request)
{
$type = $request->input('type');
if (!in_array($type, ['chupiao', 'paijiang', 'win_list'])) {
return $this->jsonFailed('类型不对');
}
$lotteryTypeId = $request->input('lottery_type_id');
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
if (!$dateStart) {
$dateStart = date('Ymd');
} else {
$dateStart = date('Ymd', strtotime($dateStart));
}
if ($dateEnd) {
$dateEnd = date('Ymd', strtotime($dateEnd));
}
$query = Order::with([
'lottery:id,name',
'customer:id,nickname,phone,remark,avatar',
'lotteryType:id,name,type',
])->select('*')
->where('draft_shop_id', $this->shopId())
->where('pay_state', PayState::SUCCESS);
if ($lotteryTypeId) {
$query->where('lottery_type_id', $lotteryTypeId);
}
if ($type == 'paijiang') {
if ($dateStart) {
$query->where('send_date', '>=', $dateStart);
}
if ($dateEnd) {
$query->where('send_date', '<=', $dateEnd);
}
} else if ($type == 'win_list') {
$query->whereIn('lottery_state', [LottState::WIN, LottState::SEND]);
if ($dateStart) {
$query->where('draft_date', '>=', $dateStart);
}
if ($dateEnd) {
$query->where('draft_date', '<=', $dateEnd);
}
} else {
if ($dateStart) {
$query->where('draft_date', '>=', $dateStart);
}
if ($dateEnd) {
$query->where('draft_date', '<=', $dateEnd);
}
}
$list = $query->orderBy('id', 'desc')->paging();
return $this->jsonSuccess($list);
}
/**
* @api {POST} /api/seller/seller/report/agent 代理统计
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} [start_month] 月份,默认当前月(2001-01)
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "agents":[
* {
* "id":2,
* "shop_id":1,
* "agent_id":1,
* "agent":0, // 开启状态
* "agent_brokerage":0, // 佣金比例
* "agent_at":"2023-04-10 21:20:03",
* "agent_money":0, // 销售额
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"未知",
* "hide_name":"****11",
* "agent_days":41 // 代理时长
* }
* ],
* "total":{
* "money":0,// 销售额
* "brokerage":0,// 应付佣金
* "register_num":0 // 代理注册人数
* }
* }
* }
*/
public function agent(Request $request)
{
$this->checkMasterNext();
$month = $request->input('start_month');
if (!$month) {
$month = date('Y-m');
}
$startDate = date('Ymd', strtotime($month . '-01'));
$endDate = date('Ymd', strtotime($month . '-31'));
$money = 0;
$brokerage = 0;
$agentMoneys = [];
$registerNum = Customer::where('shop_id', $this->shopId())
->where('regist_agent_date', '>=', $startDate)
->where('regist_agent_date', '<=', $endDate)
->count();
$customers = Customer::where('shop_id', $this->shopId())
->where('agent', 1)
->whereNotNull('agent_at')
->get();
foreach ($customers as $customer) {
$customer->agent_money = $this->agentMoney($customer->id, $this->shopId());
}
return $this->jsonSuccess([
'agents' => $customers,
'total' => [
'money' => $money,
'brokerage' => $brokerage,
'register_num' => $registerNum
]
]);
}
public function agentMoney($agentId, $shopId) {
$customerIdList = Customer::where('shop_id', $shopId)
->where('agent_id', $agentId)
->pluck('id')
->toArray();
if (!$customerIdList) {
return 0;
}
return Order::select('money')
->agentUsable()
->where('shop_id', $this->shopId())
->whereIn('customer_id', $customerIdList)
->where('pay_state', PayState::SUCCESS)
->sum('money');
}
/**
* @api {POST} /api/seller/seller/report/agent_regist 代理统计-注册数
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} agent_id 代理id
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当月第一天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果 参考用户列表数据
* {
* }
*/
public function agentRegist(Request $request)
{
$this->checkMasterNext();
$agentId = $request->input('agent_id');
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
ThrowException::isTrue(!$agentId, '销售不存在');
$agent = Customer::where('shop_id', $this->shopId())
->find($agentId);
ThrowException::isTrue(!$agent, '销售不存在');
if (!$dateStart) {
$dateStart = date('Y-m-01');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$list = Customer::where('shop_id', $this->shopId())
->where('agent_id', $agentId)
->where('regist_agent_date', '>=', $dateStart)
->where('regist_agent_date', '<=', $dateEnd)
->paging();
return $this->jsonSuccess($list);
}
/**
* @api {POST} /api/seller/seller/report/agent_order 代理统计-订单列表
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} agent_id 代理id
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当月第一天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果 参考订单列表数据
* {
* }
*/
public function agentOrder(Request $request)
{
$this->checkMasterNext();
$size = $request->input('size');
$agentId = $request->input('agent_id');
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
ThrowException::isTrue(!$agentId, '销售不存在');
$agent = Customer::where('shop_id', $this->shopId())
->find($agentId);
ThrowException::isTrue(!$agent, '销售不存在');
if (!$dateStart) {
$dateStart = date('Y-m-01');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$customerIdList = Customer::where('shop_id', $this->shopId())
->where('agent_id', $agentId)
->pluck('id')
->toArray();
if (!$customerIdList) {
return $this->jsonSuccess();
}
$list = Order::with([
'lottery:id,name',
'customer:id,nickname,phone,remark,avatar'
])
->select([
'id',
'pid',
'order_sn',
'lottery_id',
'customer_id',
'shop_id',
'lottery_type_id',
'type',
'pay_at',
'lottery_state',
'lottery_prize',
'lottery_should_send_prize',
'type_mode',
'created_at',
'updated_at',
'created_date',
DB::raw('IF(type = 3, union_money, money) AS money')
])
->agentUsable()
->where('shop_id', $this->shopId())
->whereIn('customer_id', $customerIdList)
->where('pay_state', PayState::SUCCESS)
->where('created_date', '>=', $dateStart)
->where('created_date', '<=', $dateEnd)
->paging($size);
$allMoneyData = Order::select(
DB::raw('SUM(IF(type = 3, union_money, money)) AS money')
)
->agentUsable()
->where('shop_id', $this->shopId())
->whereIn('customer_id', $customerIdList)
->where('pay_state', PayState::SUCCESS)
->where('created_date', '>=', $dateStart)
->where('created_date', '<=', $dateEnd)
->first();
$allMoney = $allMoneyData->money ?? 0;
return $this->jsonSuccess([
'list' => $list,
'total_money' => $allMoney,
]);
}
/**
* @api {POST} /api/seller/seller/report/customer_bills 统计-加扣款详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [seller_id] 店员id
* @apiParam {Int} type 9加款10减款
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果 参考订单列表数据
* {
* "code":200,
* "message":"",
* "data":{
* "current_page":1,
* "data":[
* {
* "id":250,
* "type":9,
* "customer_id":100,
* "begin_balance":"3289.4592",
* "begin_balance_withdraw":"3088.1147",
* "begin_balance_cash":"200.2345",
* "begin_balance_freeze":"1.1100",
* "ie":"+",
* "money":"100.0000", // 操作的金额
* "end_balance":"3389.4592",
* "end_balance_withdraw":"3088.1147",
* "end_balance_cash":"300.2345",
* "end_balance_freeze":"1.1100",
* "recharge_id":0,
* "order_id":0,
* "title":"异常业务加款",
* "seller_id":1000,
* "shop_bill_id":0,
* "remark_img_path":"",
* "remark":"",
* "created_date":20231029,
* "created_at":"2023-10-29 19:35:34",
* "updated_at":"2023-10-29 19:35:34",
* "deleted_at":null,
* "customer_name":"忽东忽西",
* "customer_nickname":"测试彩民一号",
* "seller_name":"第一家店主",
* "seller_nickname":"第一家店主"
* }
* ],
* "from":1,
* "per_page":20,
* "to":6,
* "total":6
* }
* }
*/
public function customerBills(Request $request)
{
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$sellerId = $request->input('seller_id');
$type = $request->input('type', BillType::SELLER_INCR);
$size = $request->input('size', 20);
if (!$dateStart) {
$dateStart = date('Y-m-d');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
if ($sellerId) {
$seller = Seller::where('shop_id', $this->shopId())->find($sellerId);
ThrowException::isTrue(!$seller, '店员不存在!');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$query = CustomerBill::leftJoin('customer', 'customer.id', 'customer_bill.customer_id')
->leftJoin('seller', 'seller.id', 'customer_bill.seller_id')
->select(['customer_bill.*',
DB::raw('customer.name as customer_name'),
DB::raw('customer.nickname as customer_nickname'),
DB::raw('seller.name as seller_name'),
DB::raw('seller.nickname as seller_nickname'),
])
->where('customer_bill.created_date', '>=', $dateStart)
->where('customer_bill.created_date', '<=', $dateEnd)
->where('customer_bill.type', $type)
->where('customer.shop_id', $this->shopId());
if ($sellerId) {
$query->where('seller_id', $sellerId);
}
$total = $query->sum('customer_bill.money');
$list = $query->orderBy('customer_bill.id', 'desc')
->paging($size);
return $this->jsonSuccess([
'list' => $list,
'total_money' => Helps::floatFormat($total),
]);
}
/**
* @api {POST} /api/seller/seller/report/customer_recharge 统计-充值详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果 参考订单列表数据
* {
* "code":200,
* "message":"",
* "data":{
* "list":{
* "current_page":1,
* "data":[
* {
* "id":16,
* "customer_id":100,
* "order_id":131,
* "recharge_sn":"RC202310103062810851",
* "out_trade_no":"",
* "pay_channel_id":14,
* "pay_type":"alipay",
* "pay_money":"0.7700", // 充值金额:需要自己格式化
* "received_money":"0.0000",
* "pay_state":"success",
* "pay_at":null,
* "created_at":"2023-10-10 20:49:08",
* "updated_at":"2023-10-10 20:49:08",
* "deleted_at":null,
* "customer_name":"忽东忽西",
* "customer_nickname":"测试彩民一号"
* }
* ],
* "from":1,
* "to":1,
* "total":1
* },
* "total_money":"0.7700"
* }
* }
*/
public function customerRecharge(Request $request)
{
$payChannelId = $request->input('pay_channel_id');
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$size = $request->input('size', 20);
if (!$dateStart) {
$dateStart = date('Y-m-d');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$query = CustomerBill::leftJoin('customer', 'customer.id', 'customer_bill.customer_id')
->leftJoin('customer_recharge', 'customer_recharge.id', 'customer_bill.recharge_id')
->leftJoin('shop_pay_channel', 'shop_pay_channel.id', 'customer_recharge.pay_channel_id')
->select(['customer_bill.*',
'shop_pay_channel.pay_type',
'shop_pay_channel.pay_channel',
'shop_pay_channel.channel_name',
DB::raw('customer.name as customer_name'),
DB::raw('customer.nickname as customer_nickname'),
])
->where('customer_bill.type', BillType::RECHARGE)
->where('customer_bill.created_date', '>=', $dateStart)
->where('customer_bill.created_date', '<=', $dateEnd)
->where('customer.shop_id', $this->shopId());
if ($payChannelId) {
$query->where('shop_pay_channel.id', $payChannelId);
}
$total = $query->sum('customer_bill.money');
$list = $query->orderBy('customer_bill.id', 'desc')
->paging($size);
return $this->jsonSuccess([
'list' => $list,
'total_money' => Helps::floatFormat($total),
]);
}
/**
* @api {POST} /api/seller/seller/report/shop_bills 统计-出票服务费详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "list":{
* "current_page":1,
* "data":[
* {
* "id":20,
* "type":2,
* "seller_id":1000,
* "shop_id":2000,
* "order_id":181,
* "begin_balance":"99922.4660",
* "begin_balance_withdraw":"0.0000",
* "begin_balance_cash":"99922.4660",
* "begin_balance_freeze":"0.0000",
* "seller_balance":"0.0000",
* "ie":"-",
* "money":"0.0480", // 出票服务费,需要格式化
* "end_balance":"99922.4180",
* "end_balance_withdraw":"0.0000",
* "end_balance_cash":"99922.4180",
* "end_balance_freeze":"0.0000",
* "title":"出票基础服务费(订单12元)",
* "remark":"",
* "created_date":20231030,
* "created_at":"2023-10-30 13:00:57", // 出票时间
* "updated_at":"2023-10-30 13:00:57",
* "deleted_at":null,
* "order":{
* "id":181,
* "lottery_type_id":1,
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":null,
* "gendan_brokerage":null,
* "order_closed":false,
* "lottery_is_wined":false,
* "money_show":0,
* "union_state_show":"",
* "close_time_str":"",
* "lottery_type":{
* "id":1,
* "name":"竞彩足球" // 彩种
* }
* }
* }
* ],
* "from":1,
* "to":1,
* "total":1
* },
* "total_money":0.05
* }
* }
*/
public function shopBills(Request $request)
{
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$size = $request->input('size', 20);
if (!$dateStart) {
$dateStart = date('Y-m-d');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$query = ShopBill::with('order:id,lottery_type_id', 'order.lotteryType:id,name')
->where('created_date', '>=', $dateStart)
->where('created_date', '<=', $dateEnd)
->where('shop_id', $this->shopId())
->where('type', ShopBill::TYPE_FEE);
$total = $query->sum('money');
$list = $query->orderBy('id', 'desc')
->paging($size);
return $this->jsonSuccess([
'list' => $list,
'total_money' => Helps::floatFormat($total),
]);
}
/**
* @api {POST} /api/seller/seller/report/shop_coop_bills 统计-合作出票服务费详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "list":{
* "current_page":1,
* "data":[
* {
* "id":8,
* "shop_cooperate_id":3,
* "type":3,
* "seller_id":1000,
* "seller_shop_id":2000,
* "shop_id":2019,
* "order_id":11,
* "lottery_type_id":0,
* "cooperate_brokerage":"0.4000",
* "begin_balance":"1010.0000",
* "ie":"-",
* "money":"19.6000",
* "end_balance":"990.4000",
* "title":"出票",
* "remark":"",
* "created_date":20230730,
* "created_at":"2023-07-30 19:39:43",
* "updated_at":"2023-07-30 19:39:43",
* "deleted_at":null,
* "order":{
* "id":11,
* "lottery_type_id":1,
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":null,
* "gendan_brokerage":null,
* "order_closed":false,
* "lottery_is_wined":false,
* "money_show":0,
* "union_state_show":"",
* "close_time_str":"",
* "lottery_type":{
* "id":1,
* "name":"竞彩足球"
* }
* }
* }
* ],
* "from":1,
* "to":1,
* "total":1
* },
* "total_money":19.6
* }
* }
*/
public function shopCoopBills(Request $request)
{
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$size = $request->input('size', 20);
if (!$dateStart) {
$dateStart = date('Y-m-d');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$query = ShopCooperateBill::with('order:id,lottery_type_id', 'order.lotteryType:id,name')
->where('created_date', '>=', $dateStart)
->where('created_date', '<=', $dateEnd)
->where('seller_shop_id', $this->shopId())
->where('type', ShopCooperateBill::TYPE_TICKET);
$total = $query->sum('money');
$list = $query->orderBy('id', 'desc')
->paging($size);
return $this->jsonSuccess([
'list' => $list,
'total_money' => Helps::floatFormat($total),
]);
}
/**
* @api {POST} /api/seller/seller/report/registers 统计-注册用户详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "current_page":1,
* "data":[
* {
* "id":100,
* "avatar":"/uploads/avatar/202310/SA1qNqz2piJ6hLRqCH8pA2tS0BOmIhXsxLIXF7Wc.jpg",
* "name":"忽东忽西",
* "nickname":"测试彩民一号",
* "agent_id":134,
* "created_at":"2023-06-02T08:25:54.000000Z",
* "client_type":2, // 1android,2ios,0未知
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"Ios",
* "hide_name":"测**",
* "agent_days":"未知",
* "avatar_url":"http://jingcai.quickf.jpg",
* "agentor":{ // 代理人信息
* "id":134,
* "name":"11111",
* "nickname":"11111",
* "level_name":"储备组长",
* "level_score_group":"5000",
* "lottery_state_name":"",
* "client_type_name":"未知",
* "hide_name":"1**",
* "agent_days":"未知",
* "avatar_url":""
* }
* },
* ],
* "from":1,
* "to":4,
* "total":4
* }
* }
*/
public function registers(Request $request)
{
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$size = $request->input('size', 20);
if (!$dateStart) {
$dateStart = date('Y-m-d');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$dateStart = date('Y-m-d 00:00:00', strtotime($dateStart));
$dateEnd = date('Y-m-d 23:59:59', strtotime($dateEnd));
$list = Customer::with('agentor:id,name,nickname')
->select([
'id', 'avatar', 'name', 'nickname', 'agent_id', 'created_at', 'client_type'
])
->where('shop_id', $this->shopId())
->where('created_at', '>=', $dateStart)
->where('created_at', '<=', $dateEnd)
->paging($size);
return $this->jsonSuccess($list);
}
/**
* @api {POST} /api/seller/seller/report/buyers 统计-购彩用户详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} [date_start] 开始(2001-01-01,默认当天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":[
* {
* "customer_id":100,
* "money":"962.0000",
* "count":11,
* "id":100,
* "name":"忽东忽西",
* "nickname":"测试彩民一号",
* "avatar":"/uploads/avatar/2023",
* "avatar_url":"http://jingcai.quipg"
* },
* ]
* }
*/
public function buyers(Request $request)
{
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$size = $request->input('size', 20);
if (!$dateStart) {
$dateStart = date('Y-m-d');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$startTime = strtotime($dateStart);
$endTime = strtotime($dateEnd);
if (($endTime - $startTime) / 86400 > 62) {
ThrowException::run('时间跨度不可超过两个月');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$lotteryStates = [
LottState::WIN,
LottState::LOSS,
LottState::WAIT,
LottState::DRAFT,
LottState::PENDING,
LottState::SEND,
];
$sql = 'select t.*, customer.id,customer.name,customer.nickname,customer.avatar from (select customer_id, sum(if (type=3,union_money,money)) as money, count(*) as count from `order` where shop_id=? and created_date >=? and created_date<=? and lottery_state in (1,2,3,4,5,7) group by customer_id) as t left join customer on t.customer_id=customer.id';
$res = DB::select($sql, [$this->shopId(), $dateStart, $dateEnd]);
$totalCount = 0;
$totalMoney = 0;
foreach ($res as $item) {
$item->avatar_url = Helps::fullUrl($item->avatar);
$item->money = Helps::floatFormat($item->money);
$totalMoney += $item->money;
$totalCount += 1;
}
$res = collect($res)->sortByDesc('money');
$list = [];
foreach ($res as $item) {
$list[] = $item;
}
return $this->jsonSuccess([
'list' => $list,
'total_money' => $totalMoney,
'total_num' => $totalCount,
]);
}
/**
* @api {POST} /api/seller/report/shop_coop_brokerage 统计-合作收入详情
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [size] 每页条数
* @apiParam {String} [date_start] 开始(2001-01-01,默认当天)
* @apiParam {String} [date_end] 开始(2001-01-01,默认当天)
* @apiSuccessExample {json} 返回结果
* {
* "code":200,
* "message":"",
* "data":{
* "list":{
* "current_page":1,
* "data":[
* {
* "id":8,
* "shop_cooperate_id":3,
* "type":3,
* "seller_id":1000,
* "seller_shop_id":2000,
* "shop_id":2019,
* "order_id":11,
* "lottery_type_id":0,
* "cooperate_brokerage":"0.4000", // 合作收入
* "begin_balance":"1010.0000",
* "ie":"-",
* "money":"19.6000",
* "end_balance":"990.4000",
* "title":"出票",
* "remark":"",
* "created_date":20230730,
* "created_at":"2023-07-30 19:39:43",
* "updated_at":"2023-07-30 19:39:43",
* "deleted_at":null,
* "order":{
* "id":11,
* "lottery_type_id":1,
* "play_type_name":"",
* "buy_type_name":"",
* "pass_mode_name":[
*
* ],
* "lottery_estimate_send_prize":null,
* "gendan_brokerage":null,
* "order_closed":false,
* "lottery_is_wined":false,
* "money_show":0,
* "union_state_show":"",
* "close_time_str":"",
* "lottery_type":{
* "id":1,
* "name":"竞彩足球"
* }
* }
* }
* ],
* "from":1,
* "to":1,
* "total":1
* },
* "total_money":19.6
* }
* }
*/
public function shopCoopBrokerage(Request $request)
{
$dateStart = $request->input('date_start');
$dateEnd = $request->input('date_end');
$size = $request->input('size', 20);
if (!$dateStart) {
$dateStart = date('Y-m-d');
}
if (!$dateEnd) {
$dateEnd = date('Y-m-d');
}
$dateStart = date('Ymd', strtotime($dateStart));
$dateEnd = date('Ymd', strtotime($dateEnd));
$query = ShopCooperateBill::with('order:id,lottery_type_id', 'order.lotteryType:id,name')
->where('created_date', '>=', $dateStart)
->where('created_date', '<=', $dateEnd)
->where('seller_shop_id', $this->shopId())
->where('type', ShopCooperateBill::TYPE_TICKET);
$total = $query->sum('cooperate_brokerage');
$list = $query->orderBy('id', 'desc')
->paging($size);
return $this->jsonSuccess([
'list' => $list,
'total_money' => Helps::floatFormat($total),
]);
}
}