generated from liutk/owl-admin-base
91 lines
3.4 KiB
PHP
91 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Controllers\Finance;
|
|
|
|
use App\Admin\Controllers\AdminController;
|
|
use App\Admin\Filters\LedgerItemFilter;
|
|
use App\Models\Keyword;
|
|
use App\Models\LedgerItem;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* @property mixed $name
|
|
*/
|
|
class SalesStatisticController extends AdminController
|
|
{
|
|
public function index()
|
|
{
|
|
if ($this->actionOfGetData()) {
|
|
return $this->response()->success([
|
|
'items' => $this->getLotteryTypeStatistics(request()),
|
|
]);
|
|
}
|
|
|
|
return $this->response()->success(
|
|
$this->baseList(
|
|
$this->baseCRUD()
|
|
->headerToolbar([
|
|
amis('filter-toggler')->align('right'),
|
|
])
|
|
->footerToolbar([])
|
|
->bulkActions([])
|
|
->filter($this->baseFilter()->body([
|
|
amis()->GroupControl()->mode('horizontal')->body([
|
|
amis()->DateRangeControl('date_range', '日期')
|
|
->valueFormat('YYYY-MM-DD')
|
|
->columnRatio(6),
|
|
amis()->InputCityControl('region', '区域')
|
|
->allowDistrict(false)
|
|
->extractValue(false),
|
|
amis()->SelectControl('store_id', __('finance.ledger.store'))
|
|
->source(admin_url('api/stores?region=${region}'))
|
|
->labelField('title')
|
|
->valueField('id')
|
|
->clearable(),
|
|
]),
|
|
]))
|
|
->filterDefaultVisible()
|
|
->columns([
|
|
amis()->TableColumn('name', '彩种'),
|
|
amis()->TableColumn('sales', '销量'),
|
|
amis()->TableColumn('expenditure', '兑奖'),
|
|
])
|
|
->affixRow([
|
|
['type' => 'text', 'text' => '合计'],
|
|
['type' => 'tpl', 'text' => '${items|pick:sales|sum}'],
|
|
['type' => 'tpl', 'text' => '${items|pick:expenditure|sum}'],
|
|
])
|
|
)
|
|
);
|
|
}
|
|
|
|
protected function getLotteryTypeStatistics(Request $request): array
|
|
{
|
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
$lotteryTypes = Keyword::where('parent_key', 'lottery_type')->get();
|
|
|
|
/** @var \Illuminate\Database\Eloquent\Collection */
|
|
$statistics = LedgerItem::select([
|
|
'ledger_item_type_id',
|
|
DB::raw('SUM(sales) as sales'),
|
|
DB::raw('SUM(expenditure) as expenditure'),
|
|
])
|
|
->filter($request->input(), LedgerItemFilter::class)
|
|
->whereIn('ledger_item_type_id', $lotteryTypes->pluck('key'))
|
|
->groupBy('ledger_item_type_id')
|
|
->get()
|
|
->keyBy('ledger_item_type_id');
|
|
|
|
return $lotteryTypes->map(function ($lotteryType) use ($statistics) {
|
|
$statistic = $statistics->get($lotteryType->key);
|
|
|
|
return [
|
|
'name' => $lotteryType->name,
|
|
'sales' => trim_zeros($statistic->sales ?? '0.00'),
|
|
'expenditure' => trim_zeros($statistic->expenditure ?? '0.00'),
|
|
];
|
|
})->all();
|
|
}
|
|
}
|