generated from liutk/owl-admin-base
49 lines
1.4 KiB
PHP
49 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Account;
|
|
|
|
use App\Http\Controllers\Api\Controller;
|
|
use App\Models\Ledger;
|
|
use App\Models\TaskPerformance;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
|
|
class StorePerformanceController extends Controller
|
|
{
|
|
public function __invoke(Request $request)
|
|
{
|
|
$request->validate(
|
|
rules: [
|
|
'month' => ['filled', 'date_format:Y-m'],
|
|
],
|
|
attributes: [
|
|
'month' => '月份',
|
|
],
|
|
);
|
|
|
|
/** @var \Carbon\Carbon */
|
|
$month = Carbon::createFromFormat('Y-m', $request->input('month') ?: date('Y-m'));
|
|
|
|
/** @var \App\Models\Employee */
|
|
$user = $request->user();
|
|
|
|
/**
|
|
* 当月的业绩指标任务
|
|
*
|
|
* @var \App\Models\TaskPerformance
|
|
*/
|
|
$taskPerformance = TaskPerformance::where('store_id', $user->store_id)
|
|
->where('month', $month->format('Y-m'))
|
|
->first();
|
|
|
|
$actualPerformance = Ledger::where('store_id', $user->store_id)
|
|
->whereBetween('date', [$month->copy()->startOfMonth()->format('Y-m-d'), $month->copy()->endOfMonth()->format('Y-m-d')])
|
|
->sum('sales');
|
|
|
|
return [
|
|
'actual_performance' => trim_zeros($actualPerformance),
|
|
'expected_performance' => trim_zeros($taskPerformance->expected_performance ?? 0),
|
|
];
|
|
}
|
|
}
|