You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
7.8 KiB

5 years ago
<?php
namespace App\Http\Controllers\Admin;
2 years ago
use App\Models\AdminAreaLink;
use App\Models\Area;
2 years ago
use App\Models\Bed;
2 years ago
use App\Models\FactorItems;
5 years ago
use App\Models\OrderItems;
2 years ago
use App\Models\Orders;
use App\Models\Paramedic;
2 years ago
use App\Models\Product;
use App\Models\ProductItems;
2 years ago
use App\Models\Project;
2 years ago
use App\Models\Refund;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Spatie\Permission\Models\Role;
5 years ago
class HomeController extends CommonController
{
public $bladePath = "admin";
public function index()
{
2 years ago
$project_id = request()->project_id;
if (empty($project_id)) {
$project_id = Project::first()->id;
}
$counts = [];
// 订单总数
$counts["orders_all"] = (new Orders())->ofProject($project_id)->count();
// 待处理
$counts["orders_pending"] = (new Orders())->ofProject($project_id)->whereIn("status", [Orders::STATUS_UNCONFIRMED, Orders::STATUS_UNASSIGNED])->count();
// 在陪患者
$counts["orders_ongoing"] = (new Orders())->ofProject($project_id)->where("status", Orders::STATUS_ONGOING)->count();
// 日派工
$counts["orders_ongoing_in_today"] = (new Orders())->ofProject($project_id)
->where("status", Orders::STATUS_ONGOING)
->whereRaw("DATEDIFF(`from_date`,now()) = 0")
->count();
// 已完成
$counts["orders_finished"] = (new Orders())->ofProject($project_id)->where("status", Orders::STATUS_FINISHED)->count();
// 日结算
$counts["orders_finished_in_today"] = (new Orders())->ofProject($project_id)
->where("status", Orders::STATUS_FINISHED)
->whereRaw("DATEDIFF(`to_date`,now()) = 0")
->count();
// 护工数量
$counts["paramedics"] = (new Paramedic())->ofProject($project_id)->count();
// 今日应收
$counts["bill_today"] = (new OrderItems())->where("service_date", date("Y-m-d"))->whereHas("order", function ($query) use ($project_id) {
$query->ofProject($project_id);
})->sum("total");
// 今日退款
$counts["refund_today"] = (new Refund())->whereNotNull("paid_at")->whereRaw("DATEDIFF(`paid_at`,now()) = 0")->whereHas("order", function ($query) use ($project_id) {
$query->ofProject($project_id);
})->sum("money");
// 在岗护工
$counts["paramedic_has_order"] = (new Paramedic())->ofProject($project_id)->whereHas("orders", function ($query) {
$query->where("status", Orders::STATUS_ONGOING);
})->count();
// 各产品占比
$allMontn = [
date("Y") . '-01',
date("Y") . '-02',
date("Y") . '-03',
date("Y") . '-04',
date("Y") . '-05',
date("Y") . '-06',
date("Y") . '-07',
date("Y") . '-08',
date("Y") . '-09',
date("Y") . '-10',
date("Y") . '-11',
date("Y") . '-12',
];
// 销售额
$saleList = [];
foreach ($allMontn as $month) {
$saleList[] = [
'month' => $month,
'total' => (new OrderItems())->where("service_date", $month)->whereHas("order", function ($query) use ($project_id) {
$query->ofProject($project_id);
})->sum("total")
];
}
// 订单量
$orderList = [];
foreach ($allMontn as $month) {
$orderList[] = [
'month' => $month,
'total' => (new Orders())->ofProject($project_id)->where('created_at', 'like', $month . '%')->count()
];
}
2 years ago
//dd($this->huli(6));
2 years ago
if (is_mobile()) {
2 years ago
return view($this->bladePath . ".mobile_home");
2 years ago
} else {
2 years ago
return view($this->bladePath . ".home");
}
5 years ago
}
2 years ago
2 years ago
public function huli($project_id)
{
$data = Area::where('project_id', $project_id)->with('project', 'building')->get();
$product = Product::where('project_id', $project_id)->first();
$productItem = ProductItems::where('product_id', $product->id)->get();
$factor = FactorItems::where('factor_id', $product->statistic_factor_id)->get();
2 years ago
$lies = $this->getLies($productItem, $factor);
2 years ago
// 获取所有列
$lie = [];
if(isset($data[0]->lies)){
$lie = array_column($data[0]->lies, 'name');
}
2 years ago
dd($lies);
2 years ago
}
/**
* 获取动态列
*/
2 years ago
public function getLies($productItem, $factor)
2 years ago
{
$list = [];
foreach ($productItem as $item) {
foreach ($factor as $factor_item) {
$total = OrderItems::where('product_item_id', $item->id)
->whereRaw("factors like '%\"factor_item_id\": $factor_item->id%'")
->sum('total');
$list [] = [
'name' => $item->price + $factor_item->price . '元/天',
'total_price' => $item->price + $factor_item->price,
'product_item_id' => $item->id,
'factor_item_id' => $factor_item->id,
'total' => $total
];
}
}
return $list;
}
2 years ago
/**
* 统计
*/
public function statistic()
{
// 护工陪护
$paramedic = [
'total' => Paramedic::count(),
2 years ago
'has_accompany' => Paramedic::whereHas('ongoingOrders')->count(),
'no_accompany' => Paramedic::whereDoesntHave('ongoingOrders')->count(),
'accompany_order' => Orders::where('status', Orders::STATUS_ONGOING)->count(),
2 years ago
];
// 床位陪护
$bed = [
'total' => Bed::count(),
2 years ago
'has_accompany' => Bed::whereHas('onGoingOrder')->count(),
'no_accompany' => Bed::whereDoesntHave('onGoingOrder')->count(),
2 years ago
];
// 陪护统计表
$project = Project::get();
foreach ($project as $item) {
2 years ago
$item->paramedic_total = Paramedic::where('project_id', $item->id)->count();
2 years ago
$item->total = Orders::where('project_id', $item->id)->count();
2 years ago
}
// 最新陪护单
$lastOrder = Orders::with('project')
->orderBy('id', 'desc')
->limit(8)
->get();
// 最新出院信息
$outOrder = Orders::with('project')
2 years ago
->where('status', Orders::STATUS_FINISHED)
2 years ago
->orderBy('id', 'desc')
->limit(8)
->get();
// 最新投诉信息
2 years ago
$tip = Orders::with('project')
->whereNotNull('comment')
->orderBy('id', 'desc')
->limit(8)
->get();
2 years ago
// 上月满意度评价分析
2 years ago
$lastMonth = date('Y-m', strtotime('-1 month'));
2 years ago
$satisfied = Orders::with('project', 'paramedic')
->whereNotNull('comment')
2 years ago
->where('created_at', 'like', '%' . $lastMonth . '%')
2 years ago
->orderBy('score', 'desc')
->limit(8)
->get();
2 years ago
// 本月营收分析
2 years ago
$income = Project::get();
foreach ($income as $item) {
$item->total = Orders::where('project_id', $item->id)
->where('created_at', 'like', '%' . date('Y-m') . '%')
->where('status', Orders::STATUS_FINISHED)
->sum('paid_total');
}
2 years ago
// 本月陪护订单
2 years ago
$monthAccompany = Project::get();
2 years ago
foreach ($monthAccompany as $item) {
2 years ago
$item->total = Orders::where('project_id', $item->id)
->where('created_at', 'like', '%' . date('Y-m') . '%')
->count();
}
2 years ago
return $this->ajaxSuccess('获取成功', compact('paramedic', 'bed', 'project',
'lastOrder', 'outOrder', 'tip', 'satisfied', 'income', 'monthAccompany'));
}
5 years ago
}