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.

413 lines
17 KiB

<?php
/**
* Created by PhpStorm.
* User: weizongsong
* Date: 2019-04-12
* Time: 22:34
*/
namespace App\Http\Controllers\Admin;
use App\Exports\OrdersExport;
use App\Models\AdminAreaLink;
use App\Models\AdminBuildingLink;
use App\Models\Area;
use App\Models\Bed;
use App\Models\Building;
use App\Models\OrderItems;
use App\Models\Orders;
use App\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Spatie\Permission\Models\Role;
class OrdersController extends CommonController
{
public $bladePath = "admin.orders";
public $urlPrefix = "admin/orders";
public $modelName = "订单";
public $modelClass = Orders::class;
public function index(Request $request)
{
$projects = (new StatisticsController())->_checkProjects();
if (!$projects->count()) {
return $this->error($this->noProjects);
}
$project_id = request()->project_id ?? $projects->first()->id;
$project = Project::find($project_id);
$status = $request->get('status', 20);
$building_id = $request->get('building_id');
$area_id = $request->get('area_id');
// 判断是否护士长
$userId = auth()->id();
$roleId = Role::where('name', 'like', '%护士长%')->where('guard_name', 'admin')->value('id');
$hushizhang = DB::table('model_has_roles')->where('role_id', $roleId)
->where('model_type', 'App\Admin')->where('model_id', $userId)->count();
// 是否院方管理
$roleId = Role::where('name', 'like', '%院方管理%')->where('guard_name', 'admin')->value('id');
$yuanfang = DB::table('model_has_roles')->where('role_id', $roleId)
->where('model_type', 'App\Admin')->where('model_id', $userId)->count();
// 楼栋 - 如果是院方管理,只显示有权限的楼栋
$buildingsQuery = Building::where('project_id', $project_id);
if ($yuanfang) {
$user = auth()->user();
$buildingId = AdminBuildingLink::where('project_id', $project_id)->where('admin_id', $user->id)->pluck('building_id');
if ($buildingId->isNotEmpty()) {
$buildingsQuery = $buildingsQuery->whereIn('id', $buildingId);
}
}
$buildings = $buildingsQuery->get();
// 病区 - 如果是护士长,只显示有权限的病区;如果是院方管理,只显示有权限楼栋下的病区
$areasQuery = Area::where('project_id', $project_id);
if ($hushizhang) {
$user = auth()->user();
$areaId = AdminAreaLink::where('project_id', $project_id)->where('admin_id', $user->id)->pluck('area_id');
if ($areaId->isNotEmpty()) {
$areasQuery = $areasQuery->whereIn('id', $areaId);
}
} elseif ($yuanfang) {
$user = auth()->user();
$buildingId = AdminBuildingLink::where('project_id', $project_id)->where('admin_id', $user->id)->pluck('building_id');
if ($buildingId->isNotEmpty()) {
$areasQuery = $areasQuery->whereIn('building_id', $buildingId);
}
}
$areas = $areasQuery->get();
$month = request()->month ?? '全部';
$months = (new OrdersController())->_getMonths();
if ($month != '全部') {
$start_timestamp = strtotime($month);
$end_timestamp = strtotime("+1 month", strtotime($month));
$this->model = $this->model->whereRaw("UNIX_TIMESTAMP(`created_at`) >= " . $start_timestamp . " and UNIX_TIMESTAMP(`created_at`) < " . $end_timestamp);
}
$this->model = $this->model->where("project_id", $project_id);
// 订单号搜索(精确或模糊)
if ($request->filled('order_no')) {
$this->model = $this->model->where("serial", "like", "%" . trim($request->order_no) . "%");
}
if ($request->keyword) {
$this->model = $this->model->where(function ($query) use ($request) {
$query->where("serial", "like", "%" . $request->keyword . "%")
->orWhere("contact", "like", "%" . $request->keyword . "%")
->orWhere("mobile", "like", "%" . $request->keyword . "%");
});
}
if ($hushizhang) {
$user = auth()->user();
$areaId = AdminAreaLink::where('project_id', $project_id)->where('admin_id', $user->id)->pluck('area_id');
if ($areaId->isNotEmpty()) {
$bedList = Bed::whereIn('area_id', $areaId)->pluck('id');
if ($bedList->isNotEmpty()) {
$this->model = $this->model->whereIn('bed_id', $bedList);
} else {
// 如果没有关联的床位,返回空结果
$this->model = $this->model->whereIn('bed_id', []);
}
} else {
// 如果没有关联的病区,返回空结果
$this->model = $this->model->whereIn('bed_id', []);
}
}
// 院方管理角色:根据 building 权限过滤
if ($yuanfang) {
$user = auth()->user();
$buildingId = AdminBuildingLink::where('project_id', $project_id)->where('admin_id', $user->id)->pluck('building_id');
if ($buildingId->isNotEmpty()) {
$areaId = Area::whereIn('building_id', $buildingId)->pluck('id');
if ($areaId->isNotEmpty()) {
$bedList = Bed::whereIn('area_id', $areaId)->pluck('id');
if ($bedList->isNotEmpty()) {
$this->model = $this->model->whereIn('bed_id', $bedList);
} else {
// 如果没有关联的床位,返回空结果
$this->model = $this->model->whereIn('bed_id', []);
}
} else {
// 如果楼栋下没有病区,返回空结果
$this->model = $this->model->whereIn('bed_id', []);
}
} else {
// 如果没有关联的楼栋,返回空结果
$this->model = $this->model->whereIn('bed_id', []);
}
}
if ($request->date) {
$this->model = $this->model->whereHas("orderItems", function ($query) use ($month, $request) {
$query->where("service_date", $month . "-" . $request->date);
});
}
$data = $this->model->with([
"orderItems",
"firstItem",
"lastItem",
"project",
"product",
"customer",
"manager",
"bed" => function ($query) {
$query->with(["room", "building", "area"]);
},
"orderAgreementByLast" => function ($query) {
$query->with('file');
}
])->where('status', $status)
->where(function ($query) use ($building_id, $area_id) {
if ($building_id || $area_id) {
$query->whereHas('bed', function ($q) use ($building_id, $area_id) {
if (isset($building_id)) {
$q->where('building_id', $building_id);
}
if (isset($area_id)) {
$q->where('area_id', $area_id);
}
});
}
})->orderBy("id", "desc");
if (request()->is_export) {
$data = $data->get();
foreach ($data as $order) {
$factors = json_decode($order->factors, true);
$order->department = (collect($factors)->where('factor_name', '所在科室')->first()['factor_item_name']) ?? '';
}
return Excel::download(new OrdersExport($data), "订单导出.xlsx");
}
$data = $data->paginate(10);
foreach ($data as $order) {
$order = $order->refreshTotal();
$factors = json_decode($order->factors, true);
$order->department = (collect($factors)->where('factor_name', '所在科室')->first()['factor_item_name']) ?? '';
}
// 订单状态
$order_status_list = [
'0' => '待确认',
'10' => '待指派',
'20' => '进行中',
'100' => '已完成'
];
return view($this->bladePath . ".index", compact('area_id', 'building_id', 'buildings', 'areas', "status", "data", "project_id", "month", "project", "hushizhang", "yuanfang", "order_status_list"));
}
public function _getMonths()
{
$months = [];
$months[] = '全部';
for ($i = 1; $i <= 12; $i++) {
$mm = $i < 10 ? "0" . $i : $i;
$months[] = (date("Y") - 2) . "-" . $mm;
}
for ($i = 1; $i <= 12; $i++) {
$mm = $i < 10 ? "0" . $i : $i;
$months[] = (date("Y") - 1) . "-" . $mm;
}
for ($i = 1; $i <= 12; $i++) {
$mm = $i < 10 ? "0" . $i : $i;
$months[] = date("Y") . "-" . $mm;
}
view()->share(compact("months"));
return $months;
}
public function changeItem(Request $request)
{
$item = OrderItems::with(["order", "siblings"])->find($request->item_id);
if (request()->is_batch) {
$order_items = $item->siblings;
$total = $item->siblings->count();
} else {
$order_items = [$item];
$total = 1;
}
DB::beginTransaction();
try {
foreach ($order_items as $order_item) {
if ($request->has("total") && $order_item->paid_at && $request->total != $order_item->total) {
return $this->error("子订单已扣款,不支持后台更改价格,请通过其他方法进行更改");
}
if ($request->has("total") && $order_item->total == 0 && $request->total != $order_item->total) {
return $this->error("未服务子订单不支持后台更改价格,请通过其他方法进行更改");
}
if ($request->has("total")) {
$order_item->update([
"total" => $request->total
]);
}
if ($request->factor_item) {
$factors = json_decode($order_item->factors, true);
foreach ($factors as &$factor) {
if (!$factor["used_for_fee"]) continue;
foreach ($request->factor_item as $k => $v) {
if ($k != $factor["factor_item_id"]) continue;
$factor["fee_percent"] = $v["fee_percent"];
$factor["fee"] = $v["fee"];
}
}
$order_item->update([
"factors" => json_encode($factors)
]);
}
}
DB::commit();
return $this->success("处理成功" . $total . "条数据!");
} catch (\Exception $exception) {
DB::rollBack();
return $this->error($exception->getMessage() . $exception->getLine());
}
}
public function getItem($id)
{
$order_item = OrderItems::with("order")->find($id);
$order_item = $order_item->calculateFee();
return $this->ajaxResponse($order_item);
}
public function score()
{
$projects = (new StatisticsController())->_checkProjects();
if (!$projects->count()) {
return $this->error($this->noProjects);
}
$project_id = request()->project_id ?? $projects->first()->id;
$user = auth()->user();
$areaId = AdminAreaLink::where('project_id', $project_id)->where('admin_id', $user->id)->pluck('area_id');
$bedList = Bed::whereIn('area_id', $areaId)->pluck('id');
$this->model = $this->model->where("project_id", $project_id)->whereNotNull("scored_at");
if (request()->keyword) {
$this->model = $this->model->where(function ($query) {
$query->where("serial", "like", "%" . request()->keyword . "%")
->orWhere("contact", "like", "%" . request()->keyword . "%")
->orWhere("mobile", "like", "%" . request()->keyword . "%");
})->where(function ($query) use ($bedList) {
if ($bedList->isNotEmpty()) {
$query->whereIn('bed_id', $bedList);
}
});
}
$data = $this->model
->with([
"orderItems",
"project",
"product",
"customer",
"manager",
"bed" => function ($query) {
$query->with(["room", "building", "area"]);
}
])
->orderBy("id", "desc")
->paginate(10);
return view($this->bladePath . ".score", compact("data", "project_id"));
}
/**
* 陪护一览
*/
public function artboard(Request $request)
{
$projects = (new StatisticsController())->_checkProjects();
$building_id = $request->get('building_id');
$defaultProjectsId = ($projects[0]->id) ?? '';
$project_id = $request->get('project_id', $defaultProjectsId);
$userId = auth()->id();
// 判断是否护士长
$roleId = Role::where('name', 'like', '%护士长%')->where('guard_name', 'admin')->value('id');
$hushizhang = DB::table('model_has_roles')->where('role_id', $roleId)
->where('model_type', 'App\Admin')
->where('model_id', $userId)->count();
// 是否院方管理
$roleId = Role::where('name', 'like', '%院方管理%')->where('guard_name', 'admin')->value('id');
$yuanfang = DB::table('model_has_roles')->where('role_id', $roleId)
->where('model_type', 'App\Admin')
->where('model_id', $userId)->count();
$areaId = [];
$buildingId = [];
if ($hushizhang) {
$user = auth()->user();
$areaId = AdminAreaLink::where(function ($qeury) use ($project_id) {
if ($project_id) {
$qeury->where('project_id', $project_id);
}
})->where('admin_id', $user->id)->pluck('area_id');
} elseif ($yuanfang) {
$user = auth()->user();
$buildingId = AdminBuildingLink::where(function ($qeury) use ($project_id) {
if ($project_id) {
$qeury->where('project_id', $project_id);
}
})->where('admin_id', $user->id)->pluck('building_id');
}
$data = Area::where(function ($query) use ($areaId, $buildingId, $building_id) {
if ($areaId) {
$query->whereIn('area.id', $areaId);
}
if ($buildingId) {
$query->whereIn('area.building_id', $buildingId);
}
if ($building_id) {
$query->where('building_id', $building_id);
}
})->with(['project',
"beds" => function ($query) use ($request) {
$query->has("onGoingOrder");
$query->with(["onGoingOrder" => function ($query) {
$query->leftJoin("paramedic", "orders.paramedic_id", "=", "paramedic.id")
->select("orders.id", "orders.bed_id", "orders.paramedic_id", "orders.status", "paramedic.name as paramedic_name");
}])->select("bed.id", "bed.name", "bed.room_id", "bed.area_id", "bed.myindex")
->leftJoin("room", "bed.room_id", "=", "room.id")
->addSelect("room.name as room_name")
->orderBy("room.name")
->orderBy("bed.name");
}])->whereRaw("`area`.`project_id` = '{$project_id}'")
->orderBy("building.myindex")
->orderBy("area.myindex")
->select("area.id", "area.name", "area.building_id", "area.project_id")
->leftJoin("building", "area.building_id", "=", "building.id")
->addSelect("building.name as building_name")
->withCount("beds")
->get();
$data = $data->filter(function ($item) {
return $item->beds->count();
});
if ($data->first()) {
$data = $data->toArray();
}
// 获取楼栋 - 如果是院方管理,只显示有权限的楼栋
$buildingsQuery = Building::where('project_id', $project_id);
if ($yuanfang) {
$user = auth()->user();
$buildingId = AdminBuildingLink::where('project_id', $project_id)->where('admin_id', $user->id)->pluck('building_id');
if ($buildingId->isNotEmpty()) {
$buildingsQuery = $buildingsQuery->whereIn('id', $buildingId);
}
}
$buildings = $buildingsQuery->get();
return view($this->bladePath . ".artboard", compact("data", "projects", "project_id", "buildings", "building_id"));
}
}