weizong song 1 month ago
parent c94f98f490
commit d722b84c80

@ -345,33 +345,79 @@ class StatisticsController extends CommonController
if (!$projects->count()) {
return $this->error($this->noProjects);
}
$mode = $request->mode ?: 'customer';
$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");
$before_datetime_text = date("Y-m-d H:i:s", $before_datetime);
DB::enableQueryLog();
$customers = (new Customer())
->whereNull("deleted_at")
->with([
"patients" => function ($query) use ($before_datetime) {
$query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc");
},
// "oneBalance" => function ($query) use ($before_datetime) {
// $query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc");
// }
])
// ->whereHas("oneBalance", function ($query) use ($before_datetime) {
// $query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->where("balance", ">", 0)->orderBy("id", "desc");
// })
->whereHas("orders", function ($query) use ($before_datetime, $project_id) {
$query
// ->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")
->where("project_id", $project_id);
})
->get();
$customers = collect();
$orderBalances = collect();
if ($mode == 'order') {
$orderBalances = Orders::query()
->withoutGlobalScopes()
->leftJoin("customers", "customers.id", "=", "orders.customer_id")
->leftJoin("patient", "patient.id", "=", "orders.patient_id")
->leftJoin("balance", function ($join) use ($before_datetime_text) {
$join->on("balance.order_id", "=", "orders.id")
->whereNull("balance.deleted_at")
->where("balance.created_at", "<=", $before_datetime_text);
})
->whereNull("orders.deleted_at")
->where("orders.project_id", $project_id)
->whereDate("orders.from_date", "<=", $before_date)
->whereDate("orders.to_date", ">=", $before_date)
->groupBy(
"orders.id",
"orders.serial",
"orders.from_date",
"orders.to_date",
"orders.contact",
"orders.mobile",
"customers.mobile",
"patient.name"
)
->orderBy("orders.from_date")
->orderBy("orders.id")
->get([
"orders.id",
"orders.serial",
"orders.from_date",
"orders.to_date",
"orders.contact",
"orders.mobile as contact_mobile",
"customers.mobile as customer_mobile",
"patient.name as patient_name",
DB::raw("COALESCE(SUM(balance.money), 0) as balance"),
]);
} else {
$customers = (new Customer())
->whereNull("deleted_at")
->with([
"patients" => function ($query) use ($before_datetime) {
$query->whereRaw("UNIX_TIMESTAMP(`created_at`) <= {$before_datetime}")->orderBy("id", "desc");
},
])
->whereHas("orders", function ($query) use ($before_datetime, $project_id) {
$query
->where("project_id", $project_id);
})
->get();
}
$laravel_duration = microtime(true) - LARAVEL_START;
return view($this->bladePath . ".customer-balance", compact("customers", "before_datetime", "project_id", "laravel_duration"));
return view($this->bladePath . ".customer-balance", compact(
"customers",
"orderBalances",
"before_datetime",
"before_date",
"before_datetime_text",
"project_id",
"laravel_duration",
"mode"
));
}
public function fixMonthLastDayCheckout(Request $request)

@ -19,9 +19,23 @@
<div class="col-12">
<div class="card">
<div class="card-body" style="overflow: auto">
<ul class="nav nav-tabs nav-bordered mb-3">
<li class="nav-item">
<a href="{{ url()->current() . '?' . http_build_query(array_merge(request()->query(), ['mode' => 'customer'])) }}"
class="nav-link @if(($mode ?? 'customer') == 'customer') active @endif">
客户余额
</a>
</li>
<li class="nav-item">
<a href="{{ url()->current() . '?' . http_build_query(array_merge(request()->query(), ['mode' => 'order'])) }}"
class="nav-link @if(($mode ?? 'customer') == 'order') active @endif">
订单时点余额
</a>
</li>
</ul>
<div class="mb-3">
<form class="form-inline">
<input type="hidden" name="mode" value="{{ $mode ?? 'customer' }}">
<select class="form-control mr-1" name="project_id"
onchange="$(this).closest('form').submit()">
@foreach($projects as $pp)
@ -29,42 +43,79 @@
value="{{$pp->id}}" @if($pp->id == $project_id) {{ "selected" }}@endif>{{$pp->name}}</option>
@endforeach
</select>
{{-- <label class="control-label mr-1">截止</label><input--}}
{{-- value="{{ date("Y-m-d",$before_datetime) }}" class="form-control" name="before_date"--}}
{{-- data-plugin="date-picker">--}}
@if(($mode ?? 'customer') == 'order')
<label class="control-label mr-1">截止日期</label><input
value="{{ $before_date }}" class="form-control mr-1" name="before_date"
data-plugin="date-picker">
@endif
<button type="submit" class="btn btn-primary ml-1">查询</button>
</form>
</div>
<h5>客户余额表</h5>
<table class="table table-bordered mb-0 @if(\App\Models\CommonModel::checkExport())table-datatable @endif">
<thead>
<tr>
<th class="p-1" style="white-space: nowrap">序号</th>
<th class="p-1" style="white-space: nowrap">客户手机号</th>
<th class="p-1" style="white-space: nowrap">客户家属</th>
<th class="p-1" style="white-space: nowrap">截止日期</th>
<th class="p-1" style="white-space: nowrap">未结算余额</th>
</tr>
</thead>
<tbody>
@foreach($customers as $item)
@if(!$item->oneBalance)
@continue
@elseif (!($item->oneBalance->balance > 0))
@continue
@endif
@if(($mode ?? 'customer') == 'order')
<h5>订单时点余额表</h5>
<p class="text-muted mb-2">统计时点:{{ $before_datetime_text }}</p>
<table class="table table-bordered mb-0 @if(\App\Models\CommonModel::checkExport())table-datatable @endif">
<thead>
<tr>
<th class="p-1" style="white-space: nowrap">序号</th>
<th class="p-1" style="white-space: nowrap">订单号</th>
<th class="p-1" style="white-space: nowrap">客户手机号</th>
<th class="p-1" style="white-space: nowrap">联系人</th>
<th class="p-1" style="white-space: nowrap">联系电话</th>
<th class="p-1" style="white-space: nowrap">被陪护人</th>
<th class="p-1" style="white-space: nowrap">服务开始</th>
<th class="p-1" style="white-space: nowrap">服务结束</th>
<th class="p-1" style="white-space: nowrap">时点余额</th>
</tr>
</thead>
<tbody>
@foreach($orderBalances as $item)
<tr>
<td class="p-1">{{ $loop->iteration }}</td>
<td class="p-1">{{ $item->serial }}</td>
<td class="p-1">{{ $item->customer_mobile }}</td>
<td class="p-1">{{ $item->contact }}</td>
<td class="p-1">{{ $item->contact_mobile }}</td>
<td class="p-1">{{ $item->patient_name ?: "无" }}</td>
<td class="p-1">{{ $item->from_date }}</td>
<td class="p-1">{{ $item->to_date }}</td>
<td class="p-1">{{ $item->balance }}</td>
</tr>
@endforeach
</tbody>
</table>
@else
<h5>客户余额表</h5>
<p class="text-muted mb-2">统计时点:{{ $before_datetime_text }}</p>
<table class="table table-bordered mb-0 @if(\App\Models\CommonModel::checkExport())table-datatable @endif">
<thead>
<tr>
<td class="p-1">{{ $loop->iteration }}</td>
<td class="p-1">{{ $item->mobile }}</td>
<td class="p-1">{{ $item->patients->count() ? $item->patients->last()->name : "无" }}</td>
<td class="p-1">{{ date("Y-m-d",$before_datetime) }}</td>
<td class="p-1">{{ $item->balance }}</td>
<th class="p-1" style="white-space: nowrap">序号</th>
<th class="p-1" style="white-space: nowrap">客户手机号</th>
<th class="p-1" style="white-space: nowrap">客户家属</th>
<th class="p-1" style="white-space: nowrap">截止日期</th>
<th class="p-1" style="white-space: nowrap">未结算余额</th>
</tr>
@endforeach
</tbody>
</table>
</thead>
<tbody>
@foreach($customers as $item)
@if(!$item->oneBalance)
@continue
@elseif (!($item->oneBalance->balance > 0))
@continue
@endif
<tr>
<td class="p-1">{{ $loop->iteration }}</td>
<td class="p-1">{{ $item->mobile }}</td>
<td class="p-1">{{ $item->patients->count() ? $item->patients->last()->name : "无" }}</td>
<td class="p-1">{{ $before_date }}</td>
<td class="p-1">{{ $item->balance }}</td>
</tr>
@endforeach
</tbody>
</table>
@endif
</div>
</div>
</div>

Loading…
Cancel
Save