146 lines
4.1 KiB
PHP

<?php
namespace Modules\EcommerceFinance\Http\Controllers\Api;
use App\Abstracts\Http\ApiController;
use Modules\EcommerceFinance\Repositories\GoodsReconciliationRepository;
use Modules\EcommerceFinance\Services\JianSunQueryCache;
use Modules\EcommerceFinance\Services\SupplierPayableOverrideStore;
/**
* 货款对账 API
*
* 提供供应商应付-货款对账页面的全部数据接口:
* GET /api/ecommerce-finance/goods-reconciliations 列表(分页 + 筛选)
* GET /api/ecommerce-finance/goods-reconciliations/summary 统计卡片
* GET /api/ecommerce-finance/goods-reconciliations/options 筛选器选项
*/
class GoodsReconciliations extends ApiController
{
private GoodsReconciliationRepository $repo;
private JianSunQueryCache $cache;
public function __construct(GoodsReconciliationRepository $repo, JianSunQueryCache $cache, private readonly SupplierPayableOverrideStore $overrides)
{
$this->repo = $repo;
$this->cache = $cache;
}
/**
* 货款对账列表(分页 + 筛选)
*/
public function index()
{
$filters = $this->buildFilters();
$page = max((int) request('page', 1), 1);
$limit = $this->limit();
$result = $this->cache->remember(
'goods.list',
['filters' => $filters, 'page' => $page, 'limit' => $limit],
fn () => $this->repo->list($filters, $page, $limit)
);
$result['data'] = $this->overrides->decorate('payment-reconciliation', $result['data'] ?? [], (int) (company_id() ?: 1));
return $this->snapshotResponse([
'data' => $result['data'],
'total' => $result['total'],
'page' => $page,
'per_page' => $limit,
'last_page' => (int) ceil($result['total'] / $limit),
]);
}
/**
* 汇总统计卡片
*/
public function summary()
{
$filters = $this->buildFilters();
$summary = $this->cache->remember(
'goods.summary',
['filters' => $filters],
fn () => $this->repo->summary($filters)
);
return $this->snapshotResponse($summary);
}
/**
* 筛选器选项(供应商/仓库下拉列表等)
*/
public function options()
{
$options = $this->cache->remember(
'goods.options',
[],
fn () => $this->repo->filterOptions()
);
return $this->snapshotResponse($options);
}
// ===================================================================
// Helpers
// ===================================================================
private function snapshotResponse(array $payload)
{
$response = response()->json($payload);
foreach ($this->cache->responseHeaders() as $name => $value) {
$response->headers->set($name, $value);
}
return $response;
}
/**
* 从请求参数构建筛选条件
*/
private function buildFilters(): array
{
$filters = [];
// 记录类型
if ($type = request('record_type')) {
$filters['record_type'] = $type; // purchase | sale | all
}
// 文本筛选
$textFields = ['supplier', 'stock_no', 'purchase_no', 'product_code', 'express_no', 'warehouse'];
foreach ($textFields as $field) {
if ($val = request($field)) {
$filters[$field] = trim($val);
}
}
// 日期范围
$dateFields = ['send_time_from', 'send_time_to', 'create_time_from', 'create_time_to'];
foreach ($dateFields as $field) {
if ($val = request($field)) {
$filters[$field] = $val;
}
}
// 发出状态
if (($sent = request('sent_status')) && $sent !== 'all') {
$filters['sent_status'] = $sent; // sent | unsent
}
// 是否有差异
if (request('has_diff')) {
$filters['has_diff'] = true;
}
return $filters;
}
private function limit(): int
{
return min(max((int) request('limit', 25), 1), 100);
}
}