186 lines
6.9 KiB
PHP
186 lines
6.9 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\SupplierReturnConfirmation;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementTemplate;
|
|
use Modules\EcommerceFinance\View\SupplierReconciliationPageData;
|
|
use RuntimeException;
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
/**
|
|
* 供应商对账:对账单模板下载 + 厂家对账单上传(严格按模板校验)。
|
|
*/
|
|
class SupplierReconciliation extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly SupplierStatementTemplate $template,
|
|
private readonly SupplierStatementStore $statements,
|
|
private readonly SupplierReturnConfirmation $returnConfirmations,
|
|
private readonly SupplierReconciliationPageData $pageData,
|
|
) {
|
|
$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);
|
|
}
|
|
|
|
public function confirmReturn(Request $request): RedirectResponse
|
|
{
|
|
return $this->markReturn($request, SupplierReturnConfirmation::STATUS_CONFIRMED, '已确认该行退货扣减。');
|
|
}
|
|
|
|
public function disputeReturn(Request $request): RedirectResponse
|
|
{
|
|
return $this->markReturn($request, SupplierReturnConfirmation::STATUS_DISPUTED, '已提交异议,复核通过前该行暂不扣减。');
|
|
}
|
|
|
|
public function confirmAllReturns(Request $request): RedirectResponse
|
|
{
|
|
[$supplier, $companyId] = $this->returnTarget($request);
|
|
$keys = array_values(array_map(
|
|
static fn (array $row): string => (string) ($row['_action_key'] ?? ''),
|
|
$this->pageData->returnRowsForFilters($supplier, $companyId, $request->all()),
|
|
));
|
|
$confirmed = $this->returnConfirmations->confirmAll(
|
|
$supplier,
|
|
$keys,
|
|
$companyId,
|
|
user_id() ? (int) user_id() : null,
|
|
);
|
|
|
|
return back()->with('success', "已全部确认:{$confirmed} 行退货扣减生效(异议行保持暂不扣减)。");
|
|
}
|
|
|
|
public function exportReturns(Request $request): StreamedResponse
|
|
{
|
|
[$supplier, $companyId] = $this->returnTarget($request);
|
|
$rows = $this->pageData->returnRowsForFilters($supplier, $companyId, $request->query());
|
|
$filenameSupplier = preg_replace('/[\\\\\\/:*?"<>|]+/u', '_', $supplier) ?: '厂家';
|
|
$filename = $filenameSupplier . '_退货明细_' . now()->format('Ymd_His') . '.csv';
|
|
|
|
return response()->streamDownload(function () use ($rows): void {
|
|
$csv = fopen('php://output', 'w');
|
|
if ($csv === false) {
|
|
throw new RuntimeException('无法创建导出文件。');
|
|
}
|
|
|
|
fwrite($csv, "\xEF\xBB\xBF");
|
|
fputcsv($csv, [
|
|
'发货时间', '明细供应商', '条形码', '商品名称', '发货量', '单价', '金额',
|
|
'供应商条码', '备注', '标题', 'wms配货单号', '仓库名称', '快递单号', '类型', '确认状态',
|
|
]);
|
|
|
|
foreach ($rows as $row) {
|
|
fputcsv($csv, array_map(
|
|
fn (mixed $value): string => $this->csvCell($value),
|
|
[
|
|
$row['send_time'] ?? '',
|
|
$row['supplier'] ?? '',
|
|
$row['barcode'] ?? '',
|
|
$row['sku_name'] ?? '',
|
|
$row['qty'] ?? '',
|
|
$row['unit_price'] ?? '',
|
|
$row['amount'] ?? '',
|
|
$row['supplier_barcode'] ?? '',
|
|
$row['remark'] ?? '',
|
|
$row['title'] ?? '',
|
|
$row['wms_pick_no'] ?? '',
|
|
$row['warehouse'] ?? '',
|
|
$row['express_no'] ?? '',
|
|
'退货',
|
|
$this->returnStatusLabel((string) ($row['_status'] ?? '')),
|
|
],
|
|
));
|
|
}
|
|
|
|
fclose($csv);
|
|
}, $filename, [
|
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
'Cache-Control' => 'private, no-store',
|
|
]);
|
|
}
|
|
|
|
private function markReturn(Request $request, string $status, string $message): RedirectResponse
|
|
{
|
|
$request->validate(['key' => ['required', 'string', 'max:191']]);
|
|
[$supplier, $companyId] = $this->returnTarget($request);
|
|
$this->returnConfirmations->mark(
|
|
$supplier,
|
|
(string) $request->input('key'),
|
|
$status,
|
|
$companyId,
|
|
user_id() ? (int) user_id() : null,
|
|
);
|
|
|
|
return back()->with('success', $message);
|
|
}
|
|
|
|
/** @return array{0:string,1:int} */
|
|
private function returnTarget(Request $request): array
|
|
{
|
|
$request->validate(['supplier' => ['required', 'string', 'max:191']]);
|
|
|
|
return [trim((string) $request->input('supplier')), (int) (company_id() ?: 1)];
|
|
}
|
|
|
|
private function returnStatusLabel(string $status): string
|
|
{
|
|
return match ($status) {
|
|
SupplierReturnConfirmation::STATUS_CONFIRMED => '已确认',
|
|
SupplierReturnConfirmation::STATUS_DISPUTED => '异议中',
|
|
default => '待确认',
|
|
};
|
|
}
|
|
|
|
private function csvCell(mixed $value): string
|
|
{
|
|
$text = trim((string) $value);
|
|
if (is_numeric(str_replace(',', '', $text))) {
|
|
return $text;
|
|
}
|
|
|
|
return preg_match('/^[=+\-@\t\r]/u', $text) === 1 ? "\t" . $text : $text;
|
|
}
|
|
}
|