- 统一上传模板:单号、省、重量、运费、送货备注、面单、费率;严格校验表头,单号、运费必填,重量/面单/费率须为数字 - 代发厂家用现有供应商对账钉钉登录,页面多「上传运费账单」,传入代发运费对账(按厂家名对上代发厂家设置) - 快递:钉钉组织部门名是快递对账分组名或承运商名的,也能登录厂家门户,只看到上传快递账单,传入快递账单对账 - 只能给自己绑定的那家上传;财务在同一页面可代传;上传后发钉钉群回执(配了机器人的话) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UNTa9mVziToe5w52mmrrar
121 lines
5.7 KiB
PHP
121 lines
5.7 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Business;
|
||
|
||
use App\Services\Auth\SupplierPortalAccess;
|
||
use App\Support\CourierBills\CourierBillStore;
|
||
use App\Support\CourierBills\FreightBillTemplate;
|
||
use App\Support\CourierBills\PortalFreightBills;
|
||
use App\Support\DropshipFreight\DropshipFreightStore;
|
||
use Illuminate\Http\RedirectResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Routing\Controller;
|
||
use Illuminate\Validation\ValidationException;
|
||
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
||
use RuntimeException;
|
||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||
use Throwable;
|
||
|
||
/**
|
||
* 厂家门户(钉钉登录的供应商对账入口)上传运费账单:代发厂家传代发运费账单,快递传快递账单,统一模板、严格校验。
|
||
* 厂家/快递只能给钉钉组织里绑定的自己上传;财务在同一页面可以代任意一家上传。
|
||
*/
|
||
final class PortalFreightBillController extends Controller
|
||
{
|
||
public const KINDS = ['dropship' => '代发运费账单', 'courier' => '快递账单'];
|
||
|
||
public function __construct(
|
||
private readonly FreightBillTemplate $template,
|
||
private readonly PortalFreightBills $portal,
|
||
private readonly SupplierPortalAccess $access,
|
||
) {
|
||
$this->middleware('auth');
|
||
$this->middleware('company.identify');
|
||
}
|
||
|
||
public function template(): StreamedResponse
|
||
{
|
||
return $this->template->download('运费账单上传模板.xlsx');
|
||
}
|
||
|
||
public function upload(Request $request): RedirectResponse
|
||
{
|
||
$data = $request->validate([
|
||
'kind' => ['required', 'in:dropship,courier'],
|
||
'month' => ['required', 'date_format:Y-m'],
|
||
'group' => ['nullable', 'integer'],
|
||
'import' => ['required', 'file', 'max:51200'],
|
||
], [], ['import' => '账单文件', 'month' => '账单月份']);
|
||
$file = $request->file('import');
|
||
$extension = strtolower($file->getClientOriginalExtension());
|
||
if (! in_array($extension, ['xlsx', 'xls', 'csv'], true)) {
|
||
throw ValidationException::withMessages(['freight_bill' => '只支持 xlsx、xls、csv,请用上传模板。']);
|
||
}
|
||
$name = $this->effectiveName($request);
|
||
$original = (string) $file->getClientOriginalName();
|
||
try {
|
||
$summary = $this->template->validate((string) $file->getRealPath(), $extension);
|
||
} catch (RuntimeException $error) {
|
||
throw ValidationException::withMessages(['freight_bill' => $error->getMessage()]);
|
||
}
|
||
$uploader = mb_substr((string) ($request->user()?->name ?: $request->user()?->email ?: '厂家门户') . '(门户)', 0, 64);
|
||
|
||
if ($data['kind'] === 'dropship') {
|
||
$factory = $this->portal->factory($name);
|
||
if ($factory === null) {
|
||
throw ValidationException::withMessages(['freight_bill' => '「' . $name . '」不在代发厂家里,请联系财务。']);
|
||
}
|
||
$id = app(DropshipFreightStore::class)->uploadFor($original, (string) $file->getRealPath(), $data['month'], (int) $factory->id, $uploader);
|
||
$row = app(DropshipFreightStore::class)->db()->table('dropship_bill_files')->find($id);
|
||
} else {
|
||
$groups = $this->portal->groups($name);
|
||
if ($groups === []) {
|
||
throw ValidationException::withMessages(['freight_bill' => '「' . $name . '」不在快递账单对账分组里,请联系财务。']);
|
||
}
|
||
$group = count($groups) === 1 ? $groups[0] : collect($groups)->firstWhere('id', (int) ($data['group'] ?? 0));
|
||
if ($group === null) {
|
||
throw ValidationException::withMessages(['freight_bill' => '请选择是哪一个快递的账单。']);
|
||
}
|
||
$ids = app(CourierBillStore::class)->upload([$file], $data['month'], [(int) $group->id], $uploader);
|
||
$row = app(CourierBillStore::class)->db()->table('courier_bill_files')->find($ids[0] ?? 0);
|
||
}
|
||
if ($row === null || $row->status !== 'parsed') {
|
||
throw ValidationException::withMessages(['freight_bill' => '文件已收到,但没有读入:' . ($row->message ?? '未知原因') . '。请联系财务。']);
|
||
}
|
||
|
||
$message = '已上传' . self::KINDS[$data['kind']] . ':' . $name . ' ' . $data['month'] . ',' . number_format($summary['rows']) . ' 行,运费合计 '
|
||
. number_format($summary['amount'], 2) . ' 元,已进入财务核对';
|
||
try {
|
||
if (app(SupplierDingTalkNotifier::class)->sendFreightBillUploadReceipt($name, self::KINDS[$data['kind']], $data['month'], $original, $summary)) {
|
||
$message .= ';上传回执已发到钉钉群';
|
||
}
|
||
} catch (Throwable $error) {
|
||
report($error);
|
||
}
|
||
|
||
return back()->with('success', $message . '。');
|
||
}
|
||
|
||
/** 厂家/快递账号只能是钉钉组织绑定的那几家;财务账号按页面上选的那家。 */
|
||
private function effectiveName(Request $request): string
|
||
{
|
||
$user = $request->user();
|
||
$name = trim((string) $request->input('supplier', ''));
|
||
if ($user && SupplierPortalAccess::isSupplierUser($user)) {
|
||
$names = $this->access->supplierNamesFor($user);
|
||
abort_if($names === [], 403, '当前钉钉账号未绑定有效厂家,请联系财务人员。');
|
||
if (count($names) === 1 && $name === '') {
|
||
return $names[0];
|
||
}
|
||
abort_unless(in_array($name, $names, true), 403, '该厂家不在当前账号的授权范围内。');
|
||
|
||
return $name;
|
||
}
|
||
if ($name === '') {
|
||
throw ValidationException::withMessages(['freight_bill' => '请先选择厂家。']);
|
||
}
|
||
|
||
return $name;
|
||
}
|
||
}
|