新增供应商对账目录:财务端实时上传情况与行级差异(条码+单价匹配,四类口径),厂家端严格按模板上传(表头不一致/必填缺失/非数字/串厂一律失败);复用终版存储与流式读取。
62 lines
2.0 KiB
PHP
62 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Business;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementTemplate;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
|
/**
|
|
* 供应商对账:对账单模板下载 + 厂家对账单上传(严格按模板校验)。
|
|
*/
|
|
class SupplierReconciliation extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly SupplierStatementTemplate $template,
|
|
private readonly SupplierStatementStore $statements,
|
|
) {
|
|
$this->middleware('auth');
|
|
$this->middleware('company.identify');
|
|
}
|
|
|
|
public function template(): BinaryFileResponse
|
|
{
|
|
$path = $this->template->path();
|
|
abort_unless(is_file($path), 404, '对账单模板不存在。');
|
|
|
|
return response()->download($path, $this->template->filename());
|
|
}
|
|
|
|
public function upload(Request $request): JsonResponse|RedirectResponse
|
|
{
|
|
$request->validate([
|
|
'import' => ['required', 'file', 'mimes:xlsx,xls,csv', 'max:51200'],
|
|
'supplier' => ['required', 'string', 'max:191'],
|
|
]);
|
|
|
|
$supplier = trim((string) $request->input('supplier'));
|
|
if ($supplier === '') {
|
|
abort(422, '请选择厂家。');
|
|
}
|
|
|
|
$records = $this->template->parse($request->file('import'), $supplier);
|
|
$rows = $this->statements->save(
|
|
$supplier,
|
|
$records,
|
|
(string) $request->file('import')->getClientOriginalName(),
|
|
(int) (company_id() ?: 1),
|
|
user_id() ? (int) user_id() : null,
|
|
);
|
|
|
|
$message = "对账单已保存:{$supplier} 共 {$rows} 行。";
|
|
|
|
return $request->expectsJson()
|
|
? response()->json(['message' => $message, 'rows' => $rows])
|
|
: back()->with('success', $message);
|
|
}
|
|
}
|