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.
302 lines
12 KiB
302 lines
12 KiB
|
5 years ago
|
<?php
|
||
|
|
/**
|
||
|
|
* Created by PhpStorm.
|
||
|
|
* User: weizongsong
|
||
|
|
* Date: 2019-04-12
|
||
|
|
* Time: 22:34
|
||
|
|
*/
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Customer;
|
||
|
|
use App\Models\Balance;
|
||
|
|
use App\Models\Factor;
|
||
|
|
use App\Models\FactorItems;
|
||
|
|
use App\Models\OrderItems;
|
||
|
|
use App\Models\Orders;
|
||
|
|
use App\Models\Paramedic;
|
||
|
|
use App\Models\Product;
|
||
|
|
use App\Models\Project;
|
||
|
|
use App\Models\Recharge;
|
||
|
|
use App\Models\Refund;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
|
||
|
|
class StatisticsController extends CommonController
|
||
|
|
{
|
||
|
|
public $bladePath = "admin.statistics";
|
||
|
|
public $urlPrefix = "admin/statistics";
|
||
|
|
public $modelName = "数据统计";
|
||
|
|
public $noProjects = "用户名下没有可以管理的项目";
|
||
|
|
|
||
|
|
public function _checkProjects()
|
||
|
|
{
|
||
|
|
$projects = (new Project())->adminProject()->get();
|
||
|
|
view()->share(compact("projects"));
|
||
|
|
return $projects;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function _getMonths()
|
||
|
|
{
|
||
|
|
$months = [];
|
||
|
|
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 _getMonthData()
|
||
|
|
{
|
||
|
|
$projects = $this->_checkProjects();
|
||
|
|
$project_id = request()->project_id ?? $projects->first()->id;
|
||
|
|
$project = $projects->keyBy("id")->toArray()["{$project_id}"];
|
||
|
|
|
||
|
|
$month = request()->month ?? date("Y-m");
|
||
|
|
$months = $this->_getMonths();
|
||
|
|
|
||
|
|
DB::enableQueryLog();
|
||
|
|
$paramedics = (new Paramedic())->ofProject($project_id)->with(["orderItems" => function ($query) use ($month) {
|
||
|
|
$query->whereRaw("(DATE_FORMAT(`service_date`,'%Y-%m') = '{$month}' or DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}')")
|
||
|
|
->where("total", ">", 0)
|
||
|
|
->whereHas("order")
|
||
|
|
->with(["order", "product", "productItem", "productParamedicLevel", "paramedic", "bed", "room", "building", "area"])
|
||
|
|
->orderBy("id");
|
||
|
|
}])->get();
|
||
|
|
|
||
|
|
$allItems = collect();
|
||
|
|
foreach ($paramedics as $paramedic) {
|
||
|
|
foreach ($paramedic->orderItems as $orderItem) {
|
||
|
|
if ($orderItem->paid_at) {
|
||
|
|
$paid_at_this_month = Carbon::parse($orderItem->paid_at)->isSameMonth($month) ? true : false;
|
||
|
|
} else {
|
||
|
|
$paid_at_this_month = false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (Carbon::parse($orderItem->service_date)->isSameMonth($month)) {
|
||
|
|
$orderItem->service_date_in_this_month = $orderItem->service_date;
|
||
|
|
} else {
|
||
|
|
$orderItem->service_date_in_this_month = OrderItems::PREV_MONTH;
|
||
|
|
}
|
||
|
|
|
||
|
|
$orderItem->paid_at_this_month = $paid_at_this_month;
|
||
|
|
$orderItem = $orderItem->calculateFee();
|
||
|
|
}
|
||
|
|
$paramedic->originalOrderItems = $paramedic->orderItems;
|
||
|
|
$allItems = $allItems->concat($paramedic->orderItems);
|
||
|
|
$paramedic->orderItems = $paramedic->orderItems->groupBy("service_date_in_this_month");
|
||
|
|
}
|
||
|
|
|
||
|
|
$days = date("t", strtotime($month));
|
||
|
|
$dates = [];
|
||
|
|
for ($i = 1; $i <= $days; $i++) {
|
||
|
|
$dd = $i < 10 ? "0" . $i : $i;
|
||
|
|
$dates[] = $dd;
|
||
|
|
}
|
||
|
|
$dates[] = OrderItems::PREV_MONTH;
|
||
|
|
|
||
|
|
view()->share(compact("projects", "project_id", "month", "months", "dates", "paramedics", "allItems"));
|
||
|
|
return compact("projects", "project_id", "month", "months", "dates", "paramedics", "allItems");
|
||
|
|
}
|
||
|
|
|
||
|
|
public function salary(Request $request)
|
||
|
|
{
|
||
|
|
$projects = $this->_checkProjects();
|
||
|
|
if (!$projects->count()) {
|
||
|
|
return $this->error($this->noProjects);
|
||
|
|
}
|
||
|
|
$this->_getMonthData();
|
||
|
|
|
||
|
|
return view($this->urlPrefix . "/salary");
|
||
|
|
}
|
||
|
|
|
||
|
|
public function overview(Request $request)
|
||
|
|
{
|
||
|
|
$projects = $this->_checkProjects();
|
||
|
|
if (!$projects->count()) {
|
||
|
|
return $this->error($this->noProjects);
|
||
|
|
}
|
||
|
|
$project_id = request()->project_id ?? $projects->first()->id;
|
||
|
|
$project = Project::find($project_id);
|
||
|
|
|
||
|
|
$month = request()->month ?? date("Y-m");
|
||
|
|
$months = $this->_getMonths();
|
||
|
|
|
||
|
|
$start_timestamp = strtotime($month);
|
||
|
|
$end_timestamp = strtotime("+1 month", strtotime($month));
|
||
|
|
//根据项目获取相关数据
|
||
|
|
$prev_month_balance = Balance::whereRaw("UNIX_TIMESTAMP(`created_at`) < " . $start_timestamp)
|
||
|
|
->whereHas("order", function ($query) use ($project_id) {
|
||
|
|
$query->where("project_id", $project_id);
|
||
|
|
})->sum("money");
|
||
|
|
$this_month_balance = Balance::whereRaw("UNIX_TIMESTAMP(`created_at`) < " . $end_timestamp)
|
||
|
|
->whereHas("order", function ($query) use ($project_id) {
|
||
|
|
$query->where("project_id", $project_id);
|
||
|
|
})->sum("money");
|
||
|
|
$this_month_balances = Balance::whereRaw("UNIX_TIMESTAMP(`created_at`) >= " . $start_timestamp . " and UNIX_TIMESTAMP(`created_at`) < " . $end_timestamp)->get();
|
||
|
|
|
||
|
|
return view($this->urlPrefix . "/overview", compact("project_id", "month", "project", "prev_month_balance", "this_month_balance", "this_month_balances"));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function syncOrderItems(Request $request)
|
||
|
|
{
|
||
|
|
// if (!$request->month) {
|
||
|
|
// dd("月份不正确");
|
||
|
|
// return $this->error("月份不正确");
|
||
|
|
// }
|
||
|
|
// if (strtotime($request->month) < strtotime(date("Y-m", strtotime("-1 month")))) {
|
||
|
|
// dd("上月份之前的数据不支持此操作");
|
||
|
|
// return $this->error("上月份之前的数据不支持此操作");
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// $month = $request->month;
|
||
|
|
// $model = OrderItems::whereRaw("DATE_FORMAT(`service_date`,'%Y-%m') = '{$month}'");
|
||
|
|
|
||
|
|
//采用指定订单号
|
||
|
|
$orders = ["20201102000008", "20201201000006", "20201105000008"];
|
||
|
|
$order_ids = Orders::whereIn("serial",$orders)->pluck("id")->toArray();
|
||
|
|
$model = OrderItems::whereIn("order_id", $order_ids);
|
||
|
|
//采用指定订单号结束
|
||
|
|
|
||
|
|
if ($request->last_id) {
|
||
|
|
$model = $model->where("id", ">", $request->last_id);
|
||
|
|
}
|
||
|
|
$orderItems = $model->with("order")->limit(20)->get();
|
||
|
|
|
||
|
|
if (!$orderItems->count()) {
|
||
|
|
dd("已处理完毕");
|
||
|
|
}
|
||
|
|
|
||
|
|
DB::beginTransaction();
|
||
|
|
try {
|
||
|
|
foreach ($orderItems as $orderItem) {
|
||
|
|
$factors = json_decode($orderItem->factors);
|
||
|
|
foreach ($factors as $factor) {
|
||
|
|
$current_factor = FactorItems::find($factor->factor_item_id);
|
||
|
|
$factor->fee = $current_factor->fee;
|
||
|
|
$factor->fee_percent = $current_factor->fee_percent;
|
||
|
|
$factor->factor_name = $current_factor->name;
|
||
|
|
}
|
||
|
|
|
||
|
|
$orderItem->update([
|
||
|
|
"factors" => json_encode($factors)
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
DB::commit();
|
||
|
|
return $this->success("处理成功一批,正在跳转到下一批!", url($this->urlPrefix . "/salary/sync-order-items?month={$request->month}&last_id=" . $orderItems->last()->id));
|
||
|
|
} catch (\Exception $exception) {
|
||
|
|
DB::rollBack();
|
||
|
|
dd($exception->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function fixMonthLastDayCheckout(Request $request)
|
||
|
|
{
|
||
|
|
$day = $request->day;
|
||
|
|
if (!$day) {
|
||
|
|
dd("日期不正确");
|
||
|
|
}
|
||
|
|
DB::enableQueryLog();
|
||
|
|
$orders = Orders::whereIn("serial", ["20201130000003", "20201130000005", "20201128000004", "20201126000006", "20201127000005", "20201126000003", "20201130000004", "20201127000008", "20201130000001", "20201126000008"])->get();
|
||
|
|
$orderItems = OrderItems::with(["balance", "order"])
|
||
|
|
->whereNotNull("paid_at")
|
||
|
|
->whereRaw("UNIX_TIMESTAMP(`paid_at`) > " . strtotime("2020-12-01"))
|
||
|
|
->whereRaw("UNIX_TIMESTAMP(`paid_at`) < " . strtotime("2020-12-04"))
|
||
|
|
->whereIn("order_id", $orders->pluck("id")->toArray())
|
||
|
|
->limit(100)->get();
|
||
|
|
try {
|
||
|
|
foreach ($orderItems as $orderItem) {
|
||
|
|
$new_paid_at = "{$day} 22:00:01";
|
||
|
|
$orderItem->update([
|
||
|
|
"paid_at" => $new_paid_at
|
||
|
|
]);
|
||
|
|
$orderItem->balance->update([
|
||
|
|
"created_at" => $new_paid_at,
|
||
|
|
"remark" => "本条经过时间校准处理。原始创建时间为:" . $orderItem->balance->created_at
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
DB::commit();
|
||
|
|
dd("处理完毕");
|
||
|
|
} catch (\Exception $exception) {
|
||
|
|
DB::rollBack();
|
||
|
|
dd($exception->getMessage());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function fixBalanceOrderId()
|
||
|
|
{
|
||
|
|
$balances = Balance::whereNull("order_id")->with("belongs")->limit("100")->get();
|
||
|
|
if (!$balances->count()) {
|
||
|
|
return $this->success("处理完毕", url($this->urlPrefix . "/overview"));
|
||
|
|
}
|
||
|
|
foreach ($balances as $balance) {
|
||
|
|
$balance->timestamps = false;
|
||
|
|
$balance->update([
|
||
|
|
"order_id" => $balance->belongs->order_id
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $this->success("处理完毕" . $balances->count() . "条数据,正在进入下一批数据。", url($this->urlPrefix . "/finance/fix-balance-order-id?timer=" . time()));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function finance(Request $request)
|
||
|
|
{
|
||
|
|
$projects = $this->_checkProjects();
|
||
|
|
if (!$projects->count()) {
|
||
|
|
return $this->error($this->noProjects);
|
||
|
|
}
|
||
|
|
|
||
|
|
$project_id = request()->project_id ?? $projects->first()->id;
|
||
|
|
$project = $projects->keyBy("id")->toArray()["{$project_id}"];
|
||
|
|
|
||
|
|
$month = request()->month ?? date("Y-m");
|
||
|
|
$months = $this->_getMonths();
|
||
|
|
|
||
|
|
$recharges = Recharge::with(["manager", "order", "patient"])->withCount("refunds")->whereNotNull("paid_at")->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'")->get();
|
||
|
|
$refunds = Refund::whereNotNull("paid_at")->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'")->get();
|
||
|
|
|
||
|
|
return view($this->bladePath . ".finance", compact("recharges", "refunds", "projects", "project_id", "months", "month"));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function memberBalanceByDate(Request $request)
|
||
|
|
{
|
||
|
|
$projects = $this->_checkProjects();
|
||
|
|
if (!$projects->count()) {
|
||
|
|
return $this->error($this->noProjects);
|
||
|
|
}
|
||
|
|
$project_id = request()->project_id ?? $projects->first()->id;
|
||
|
|
|
||
|
|
$before_date = $request->before_date ?: date("Y-m-d");
|
||
|
|
$before_datetime = strtotime($before_date . " 23:59:59");
|
||
|
|
|
||
|
|
DB::enableQueryLog();
|
||
|
|
//todo:子订单的数量增加,用更省查询时间的方式获取项目相关数据
|
||
|
|
$customers = (new Customer())
|
||
|
|
->with([
|
||
|
|
"patients" => function ($query) use ($before_datetime) {
|
||
|
|
$query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc");
|
||
|
|
},
|
||
|
|
"balances" => function ($query) use ($before_datetime) {
|
||
|
|
$query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc");
|
||
|
|
}
|
||
|
|
])
|
||
|
|
->whereHas("balances", function ($query) use ($before_datetime, $project_id) {
|
||
|
|
$query
|
||
|
|
->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")
|
||
|
|
->whereHas("order", function ($query) use ($project_id) {
|
||
|
|
$query->where("project_id", $project_id);
|
||
|
|
});
|
||
|
|
})
|
||
|
|
->get();
|
||
|
|
|
||
|
|
return view($this->bladePath . ".customer-balance", compact("customers", "before_datetime"));
|
||
|
|
}
|
||
|
|
}
|