147 lines
5.8 KiB
PHP
147 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\EcommerceFinance\Repositories\DingProcessDetailMirrorRepository;
|
|
use Modules\EcommerceFinance\Services\DingProcessDetailMirrorImporter;
|
|
use Modules\EcommerceFinance\Services\DingProcessRefreshQueue;
|
|
use Modules\EcommerceFinance\View\ProcessDetailsPageData;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class ProcessDetails extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly DingProcessDetailMirrorRepository $repository,
|
|
private readonly DingProcessRefreshQueue $refreshQueue,
|
|
) {}
|
|
|
|
public function show(Request $request)
|
|
{
|
|
if ($request->boolean('refresh_current')) {
|
|
return $this->requestRefresh($request);
|
|
}
|
|
if ($request->filled('refresh_status')) {
|
|
return $this->refreshStatus((string) $request->query('refresh_status'));
|
|
}
|
|
|
|
$pageData = app(ProcessDetailsPageData::class)->page($request);
|
|
|
|
return view('business.pages.financial-operations.process-details', [
|
|
...$pageData,
|
|
'section' => ['slug' => 'process-data', 'title' => '流程数据'],
|
|
'page' => ['slug' => 'oa-process', 'title' => 'OA流程'],
|
|
'pageUrl' => url('/' . company_id() . '/workspace/process-data/oa-process'),
|
|
]);
|
|
}
|
|
|
|
public function approvalSummary(Request $request)
|
|
{
|
|
$pageData = app(ProcessDetailsPageData::class)->approvalSummaryPage($request);
|
|
$backFilters = array_filter(
|
|
$pageData['filters'],
|
|
static fn (mixed $value): bool => $value !== '' && $value !== null,
|
|
);
|
|
$backUrl = url('/' . company_id() . '/workspace/process-data/oa-process')
|
|
. ($backFilters === [] ? '' : '?' . http_build_query($backFilters));
|
|
|
|
return view('business.pages.financial-operations.process-details', [
|
|
...$pageData,
|
|
'section' => ['slug' => 'process-data', 'title' => '流程数据'],
|
|
'page' => ['slug' => 'oa-process', 'title' => 'OA流程'],
|
|
'pageUrl' => url('/' . company_id() . '/workspace/process-data/oa-process/approval-summary'),
|
|
'backUrl' => $backUrl,
|
|
]);
|
|
}
|
|
|
|
public function export(Request $request): StreamedResponse
|
|
{
|
|
$filters = ProcessDetailsPageData::normalizeFilters($request);
|
|
|
|
return response()->streamDownload(function () use ($filters): void {
|
|
$stream = fopen('php://output', 'wb');
|
|
fwrite($stream, "\xEF\xBB\xBF");
|
|
$headers = DingProcessDetailMirrorImporter::HEADERS;
|
|
array_splice($headers, 2, 0, ['发起人部门']);
|
|
fputcsv($stream, [...$headers, '付款状态', '付款差额']);
|
|
|
|
$number = 0;
|
|
foreach ($this->repository->exportRows($filters) as $row) {
|
|
$number++;
|
|
fputcsv($stream, [
|
|
$number,
|
|
(string) ($row['business_id'] ?? ''),
|
|
(string) ($row['originator_department'] ?? ''),
|
|
(string) ($row['originator_name'] ?? ''),
|
|
(string) ($row['status'] ?? ''),
|
|
(string) ($row['document_type'] ?? ''),
|
|
(string) ($row['purpose'] ?? ''),
|
|
(int) ($row['line_no'] ?? 0),
|
|
(string) ($row['account_code'] ?? ''),
|
|
(string) ($row['account_name'] ?? ''),
|
|
$row['amount'] ?? '',
|
|
(string) ($row['bank_account_code'] ?? ''),
|
|
(string) ($row['bank_account_name'] ?? ''),
|
|
$row['bank_amount'] ?? '',
|
|
(string) ($row['payment_method'] ?? ''),
|
|
(string) ($row['paid_at'] ?? ''),
|
|
(string) ($row['payment_state'] ?? ''),
|
|
isset($row['payment_difference'])
|
|
? number_format((float) $row['payment_difference'], 2, '.', '')
|
|
: '',
|
|
]);
|
|
}
|
|
|
|
fclose($stream);
|
|
}, 'OA流程_' . now()->format('Ymd_His') . '.csv', [
|
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
]);
|
|
}
|
|
|
|
public function requestRefresh(Request $request): JsonResponse
|
|
{
|
|
$businessIds = $request->input('business_ids', $request->query('business_ids', []));
|
|
if (is_string($businessIds)) {
|
|
$businessIds = array_values(array_filter(array_map('trim', explode(',', $businessIds))));
|
|
}
|
|
$request->merge(['business_ids' => $businessIds]);
|
|
$validated = $request->validate([
|
|
'business_ids' => ['required', 'array', 'min:1', 'max:50'],
|
|
'business_ids.*' => ['required', 'string', 'max:32', 'regex:/^[A-Za-z0-9_-]+$/'],
|
|
]);
|
|
$activeBusinessIds = $this->repository->activeBusinessIds($validated['business_ids']);
|
|
$requestState = $this->refreshQueue->enqueue(
|
|
$activeBusinessIds,
|
|
(int) company_id(),
|
|
(int) user_id(),
|
|
);
|
|
|
|
return response()
|
|
->json([
|
|
'success' => true,
|
|
...$requestState,
|
|
'message' => $requestState['queued']
|
|
? '当前页进行中的流程已进入刷新队列。'
|
|
: '当前页没有审批中的流程需要刷新。',
|
|
])
|
|
->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
public function refreshStatus(string $token): JsonResponse
|
|
{
|
|
$status = $this->refreshQueue->status(
|
|
$token,
|
|
(int) company_id(),
|
|
(int) user_id(),
|
|
);
|
|
abort_if($status === null, 404);
|
|
|
|
return response()
|
|
->json(['success' => true, 'refresh' => $status])
|
|
->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
}
|