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.

346 lines
13 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;
5 years ago
use App\Libs\AlipayF2F;
5 years ago
use App\Libs\WxMicroPay;
5 years ago
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()
{
3 years ago
$projects = (new Project())->adminProject()->orderBy("id","desc")->get();
5 years ago
view()->share(compact("projects"));
return $projects;
}
3 years ago
public function getYears()
{
$start_year = config("start_year");
$years = [];
for ($i = date("Y"); $i >= $start_year; $i--) {
$years[] = $i;
}
view()->share(compact("years"));
}
5 years ago
public function _getMonths()
{
$months = [];
3 years ago
for ($i = 1; $i <= 12; $i++) {
$mm = $i < 10 ? "0" . $i : $i;
$months[] = (date("Y") - 2) . "-" . $mm;
}
5 years ago
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();
3 years ago
$paramedics = (new Paramedic())->where(function ($query) use ($project_id, $month) {
3 years ago
$order_item_paramedic_ids = (new OrderItems())
->whereRaw("(DATE_FORMAT(`service_date`,'%Y-%m') = '{$month}' or DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}')")
->whereHas("order", function ($query) use ($project_id) {
$query->where("project_id", $project_id);
})->pluck("paramedic_id")->toArray();
3 years ago
$query->where("project_id", $project_id)->orWhereIn("id", $order_item_paramedic_ids);
3 years ago
})->with(["orderItems" => function ($query) use ($month) {
5 years ago
$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();
3 years ago
$laravel_duration = microtime(true) - LARAVEL_START;
5 years ago
3 years ago
return view($this->urlPrefix . "/salary", compact("laravel_duration"));
5 years ago
}
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");
5 years ago
$this_month_balances = Balance::whereRaw("UNIX_TIMESTAMP(`created_at`) >= " . $start_timestamp . " and UNIX_TIMESTAMP(`created_at`) < " . $end_timestamp)
->whereHas("order", function ($query) use ($project_id) {
$query->where("project_id", $project_id);
})->get();
5 years ago
return view($this->urlPrefix . "/overview", compact("project_id", "month", "project", "prev_month_balance", "this_month_balance", "this_month_balances"));
}
public function syncOrderItems(Request $request)
{
5 years ago
DB::enableQueryLog();
5 years ago
//采用指定订单号
3 years ago
$model = OrderItems::whereHas("order", function ($query) {
$query->whereIn("serial", [
5 years ago
"20210910000003",
"20210914000006",
"20210826000004",
"20210909000022",
"20210903000007",
"20210918000013",
"20210906000015"
]);
3 years ago
})->where("service_date", ">=", "2021-09-01");
5 years ago
//采用指定订单号结束
if ($request->last_id) {
$model = $model->where("id", ">", $request->last_id);
}
5 years ago
$orderItems = $model->with("order")->limit(50)->get();
5 years ago
if (!$orderItems->count()) {
dd("已处理完毕");
}
DB::beginTransaction();
try {
foreach ($orderItems as $orderItem) {
$factors = json_decode($orderItem->factors);
foreach ($factors as $factor) {
5 years ago
$current_factor = FactorItems::find(3);
5 years ago
$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 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();
5 years ago
$recharges = Recharge::with(["manager", "order", "patient"])
->withCount("refunds")
->whereNotNull("paid_at")
->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'")
->whereHas("order", function ($query) use ($project_id) {
$query->where("project_id", $project_id);
})
->get();
$refunds = Refund::whereNotNull("paid_at")
->whereRaw("DATE_FORMAT(`paid_at`,'%Y-%m') = '{$month}'")
->whereHas("order", function ($query) use ($project_id) {
$query->where("project_id", $project_id);
})
->get();
5 years ago
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();
$customers = (new Customer())
3 years ago
->whereNull("deleted_at")
5 years ago
->with([
"patients" => function ($query) use ($before_datetime) {
$query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc");
},
3 years ago
// "oneBalance" => function ($query) use ($before_datetime) {
// $query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc");
// }
5 years ago
])
3 years ago
// ->whereHas("oneBalance", function ($query) use ($before_datetime) {
// $query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->where("balance", ">", 0)->orderBy("id", "desc");
// })
3 years ago
->whereHas("orders", function ($query) use ($before_datetime, $project_id) {
$query
3 years ago
// ->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")
3 years ago
->where("project_id", $project_id);
})
3 years ago
->get();
5 years ago
3 years ago
$laravel_duration = microtime(true) - LARAVEL_START;
return view($this->bladePath . ".customer-balance", compact("customers", "before_datetime", "project_id", "laravel_duration"));
5 years ago
}
5 years ago
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()));
}
3 years ago
public function manualQueryRecharge($id)
{
5 years ago
$recharge = Recharge::find($id);
5 years ago
$res = (new AlipayF2F())->manualQuery($recharge);
5 years ago
dd($res);
}
5 years ago
}