Files
xin-procurement/app/Http/Controllers/Recon/SettlementController.php
T
2026-07-23 20:41:25 +08:00

78 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers\Recon;
use App\Exceptions\RepositoryException;
use App\Models\SettlementModel;
use App\Services\ExportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\AnnoRoute\Attribute\GetRoute;
use Modules\AnnoRoute\Attribute\RequestAttribute;
use Modules\Common\Http\Controllers\BaseController;
use Symfony\Component\HttpFoundation\Response;
/**
* 结算表管理(D9 生成于对账结算,D10 导出下载存档)
*/
#[RequestAttribute('/recon/settlement', 'recon.settlement')]
class SettlementController extends BaseController
{
protected array $searchField = [
'settlement_no' => 'like',
'recon_id' => '=',
'store_id' => '=',
'status' => '=',
];
/** 结算表列表 */
#[GetRoute(authorize: 'query')]
public function query(Request $request): JsonResponse
{
$params = $request->all();
$pageSize = $params['pageSize'] ?? 10;
$data = $this->buildSearch(
$params,
SettlementModel::query()->with(['store:id,name', 'recon:id,recon_no,title'])
)
->orderBy('id', 'desc')
->paginate($pageSize)
->toArray();
return $this->success($data);
}
/** 结算表详情 */
#[GetRoute(route: '/{id}', authorize: 'query', where: ['id' => '[0-9]+'])]
public function detail(int $id): JsonResponse
{
$settlement = SettlementModel::with(['store:id,name', 'recon:id,recon_no,title', 'operator:id,nickname'])
->find($id);
if (empty($settlement)) {
throw new RepositoryException('结算表不存在');
}
return $this->success($settlement->toArray());
}
/**
* D10 导出下载:?format=xlsx|pdf,成功后回写 file_path 存档标记
*/
#[GetRoute(route: '/{id}/download', authorize: 'download', where: ['id' => '[0-9]+'])]
public function download(int $id, Request $request): Response
{
$settlement = SettlementModel::find($id);
if (empty($settlement)) {
throw new RepositoryException('结算表不存在');
}
$format = (string) $request->query('format', ExportService::FORMAT_XLSX);
$response = app(ExportService::class)->download('settlement', $settlement, $format);
// 同步流式下载不落盘,file_path 仅作存档标记(后续切队列导出时替换为真实文件路径)
$extension = $format === ExportService::FORMAT_PDF ? 'pdf' : 'xlsx';
$settlement->file_path = 'exports/settlement/' . $settlement->settlement_no . '.' . $extension;
$settlement->save();
return $response;
}
}