108 lines
5.5 KiB
PHP
108 lines
5.5 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Business;
|
||
|
||
use App\Support\CourierBills\CourierBillStore;
|
||
use App\Support\CourierBills\CourierUploadLink;
|
||
use App\Support\CourierBills\FreightBillTemplate;
|
||
use App\Support\CourierBills\PortalFreightBillImports;
|
||
use Carbon\CarbonImmutable;
|
||
use Illuminate\Http\RedirectResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Routing\Controller;
|
||
use Illuminate\Validation\ValidationException;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
use Symfony\Component\HttpFoundation\StreamedResponse;
|
||
|
||
/**
|
||
* 推送到快递钉钉对账群的免登录上传页:下载模板、按模板上传这一家这个月的账单,上传后进入快递账单对账。
|
||
*/
|
||
final class CourierUploadShare extends Controller
|
||
{
|
||
public function __construct(
|
||
private readonly CourierBillStore $store,
|
||
private readonly CourierUploadLink $links,
|
||
private readonly FreightBillTemplate $template,
|
||
private readonly PortalFreightBillImports $imports,
|
||
) {}
|
||
|
||
public function show(Request $request, string $token): Response
|
||
{
|
||
$t = $this->target($request, $token);
|
||
$expires = $this->expiresAt($request);
|
||
$files = $this->store->db()->table('courier_bill_files')->where('group_id', $t['group']->id)->where('bill_month', $t['month'])
|
||
->orderByDesc('id')->limit(20)->get();
|
||
$pending = array_map(static fn (array $file): object => (object) [
|
||
'original_name' => $file['name'], 'row_count' => $file['count'], 'amount' => $file['amount'],
|
||
'status' => $file['status'], 'message' => $file['message'], 'created_at' => $file['at'],
|
||
], $this->imports->recent($t['companyId'], (string) $t['group']->name, (int) $t['group']->id, $t['month']));
|
||
$files = $files->concat($pending)->sortByDesc('created_at')->take(20)->values();
|
||
|
||
return response()->view('business.pages.courier-payables.courier-bills.upload-share', [
|
||
'group' => $t['group'], 'month' => $t['month'], 'monthLabel' => self::monthLabel($t['month']), 'files' => $files,
|
||
'expiresAt' => $expires, 'confirmed' => $t['confirmed'],
|
||
'uploadUrl' => $this->links->signedRoute('signed.courier-bill-upload.upload', $token, $t['companyId'], $expires),
|
||
'templateUrl' => $this->links->signedRoute('signed.courier-bill-upload.template', $token, $t['companyId'], $expires),
|
||
])->withHeaders(['Cache-Control' => 'private, no-store, max-age=0', 'X-Robots-Tag' => 'noindex, nofollow, noarchive',
|
||
'Referrer-Policy' => 'no-referrer']);
|
||
}
|
||
|
||
public function template(Request $request, string $token): StreamedResponse
|
||
{
|
||
$t = $this->target($request, $token);
|
||
|
||
return $this->template->download($t['group']->name . '_' . $t['month'] . '_账单上传模板.xlsx');
|
||
}
|
||
|
||
public function upload(Request $request, string $token): RedirectResponse
|
||
{
|
||
$t = $this->target($request, $token);
|
||
$back = $this->links->signedRoute('signed.courier-bill-upload.show', $token, $t['companyId'], $this->expiresAt($request));
|
||
if ($t['confirmed']) {
|
||
throw ValidationException::withMessages(['upload' => '这个月的账单财务已确认结算,不能再上传;有问题请在群里联系财务。']);
|
||
}
|
||
$request->validate(['import' => ['required', 'file', 'max:51200']], [], ['import' => '账单文件']);
|
||
$file = $request->file('import');
|
||
$extension = strtolower($file->getClientOriginalExtension());
|
||
if (! in_array($extension, ['xlsx', 'xls', 'csv'], true)) {
|
||
throw ValidationException::withMessages(['upload' => '只支持 xlsx、xls、csv,请用上传模板。']);
|
||
}
|
||
$this->imports->submit($file, [
|
||
'name' => (string) $t['group']->name, 'kind' => 'courier', 'month' => $t['month'],
|
||
'target_id' => (int) $t['group']->id, 'company_id' => $t['companyId'],
|
||
'source' => 'courier_upload_link',
|
||
'uploader' => mb_substr($t['group']->name . '(快递上传链接)', 0, 64),
|
||
]);
|
||
|
||
return redirect()->to($back)->with('success', '文件已收到,正在等待校验和读入。刷新本页可查看结果,读入成功后会向已配置的钉钉群发送回执。');
|
||
}
|
||
|
||
/** @return array{companyId:int,group:object,month:string,confirmed:bool} */
|
||
private function target(Request $request, string $token): array
|
||
{
|
||
$companyId = (int) ($request->route('company_id') ?: company_id() ?: 1);
|
||
$payload = $this->links->resolveToken($token, $companyId);
|
||
abort_if($payload === null, 404, '链接无效或已过期。');
|
||
$group = collect($this->store->groups())->firstWhere('id', $payload['group_id']);
|
||
abort_if($group === null, 404, '链接无效或已过期。');
|
||
$confirmed = $this->store->db()->table('courier_bill_statements')->where('group_id', $group->id)
|
||
->where('bill_month', $payload['month'])->where('status', 'confirmed')->exists();
|
||
|
||
return ['companyId' => $companyId, 'group' => $group, 'month' => $payload['month'], 'confirmed' => $confirmed];
|
||
}
|
||
|
||
private function expiresAt(Request $request): CarbonImmutable
|
||
{
|
||
$expires = (int) $request->query('expires', 0);
|
||
|
||
return $expires > 0 ? CarbonImmutable::createFromTimestamp($expires) : CarbonImmutable::now()->addDays(CourierUploadLink::TTL_DAYS);
|
||
}
|
||
|
||
private static function monthLabel(string $month): string
|
||
{
|
||
[$y, $m] = explode('-', $month);
|
||
|
||
return (int) $y . '年' . (int) $m . '月';
|
||
}
|
||
}
|