208 lines
8.8 KiB
PHP
208 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Http\Controllers;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\EcommerceFinance\Repositories\DingInvoiceDataMirrorRepository;
|
|
use Modules\EcommerceFinance\Services\DingTalkApprovalAttachmentDownloader;
|
|
use Modules\EcommerceFinance\Services\DingInvoiceRefreshQueue;
|
|
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
|
class InvoiceData extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly DingInvoiceDataMirrorRepository $repository,
|
|
) {}
|
|
|
|
public function export(Request $request): StreamedResponse
|
|
{
|
|
$filters = $this->filters($request);
|
|
|
|
return response()->streamDownload(function () use ($filters): void {
|
|
$stream = fopen('php://output', 'wb');
|
|
fwrite($stream, "\xEF\xBB\xBF");
|
|
|
|
if ($filters['view'] === DingInvoiceDataMirrorRepository::VIEW_FLOWS) {
|
|
fputcsv($stream, [
|
|
'流程号',
|
|
'标题',
|
|
'提交人',
|
|
'报销类型',
|
|
'费用类型',
|
|
'收款人',
|
|
'打款时间',
|
|
'本月付款金额',
|
|
'付款合计',
|
|
'发票张数',
|
|
'发票价税合计',
|
|
'付款差额',
|
|
'扫描状态',
|
|
'扫描说明',
|
|
]);
|
|
foreach ($this->repository->exportRows($filters) as $row) {
|
|
$payment = (float) ($row['payment_amount'] ?? 0);
|
|
$invoice = (float) ($row['invoice_total'] ?? 0);
|
|
fputcsv($stream, [
|
|
(string) ($row['business_id'] ?? ''),
|
|
(string) ($row['title'] ?? ''),
|
|
(string) ($row['originator_name'] ?? ''),
|
|
(string) ($row['document_type'] ?? ''),
|
|
(string) ($row['expense_type'] ?? ''),
|
|
(string) ($row['payee_name'] ?? ''),
|
|
(string) ($row['paid_at'] ?? ''),
|
|
number_format($payment, 2, '.', ''),
|
|
number_format((float) ($row['payment_total'] ?? 0), 2, '.', ''),
|
|
(int) ($row['invoice_count'] ?? 0),
|
|
number_format($invoice, 2, '.', ''),
|
|
number_format(round($payment - $invoice, 2), 2, '.', ''),
|
|
(string) ($row['scan_status'] ?? ''),
|
|
(string) ($row['last_error'] ?? ''),
|
|
]);
|
|
}
|
|
} else {
|
|
fputcsv($stream, [
|
|
'流程号',
|
|
'标题',
|
|
'提交人',
|
|
'报销类型',
|
|
'收款人',
|
|
'打款时间',
|
|
'本月付款金额',
|
|
'发票类型',
|
|
'销售方名称',
|
|
'销售方税号',
|
|
'购买方名称',
|
|
'购买方税号',
|
|
'价税合计',
|
|
'发票号码',
|
|
'开票日期',
|
|
'项目名称',
|
|
'金额',
|
|
'税率',
|
|
'税额',
|
|
'付款差额',
|
|
'附件名称',
|
|
]);
|
|
foreach ($this->repository->exportRows($filters) as $row) {
|
|
$payment = (float) ($row['payment_amount'] ?? 0);
|
|
$invoice = (float) ($row['invoice_total'] ?? 0);
|
|
fputcsv($stream, [
|
|
(string) ($row['business_id'] ?? ''),
|
|
(string) ($row['title'] ?? ''),
|
|
(string) ($row['originator_name'] ?? ''),
|
|
(string) ($row['document_type'] ?? ''),
|
|
(string) ($row['payee_name'] ?? ''),
|
|
(string) ($row['paid_at'] ?? ''),
|
|
number_format($payment, 2, '.', ''),
|
|
(string) ($row['invoice_type'] ?? ''),
|
|
(string) ($row['seller_name'] ?? ''),
|
|
(string) ($row['seller_tax_no'] ?? ''),
|
|
(string) ($row['buyer_name'] ?? ''),
|
|
(string) ($row['buyer_tax_no'] ?? ''),
|
|
number_format($invoice, 2, '.', ''),
|
|
(string) ($row['invoice_number'] ?? ''),
|
|
(string) ($row['invoice_date'] ?? ''),
|
|
(string) ($row['item_name'] ?? ''),
|
|
$row['amount_without_tax'] === null
|
|
? ''
|
|
: number_format((float) $row['amount_without_tax'], 2, '.', ''),
|
|
(string) ($row['tax_rate'] ?? ''),
|
|
$row['tax_amount'] === null
|
|
? ''
|
|
: number_format((float) $row['tax_amount'], 2, '.', ''),
|
|
number_format(round($payment - $invoice, 2), 2, '.', ''),
|
|
(string) ($row['source_file_name'] ?? ''),
|
|
]);
|
|
}
|
|
}
|
|
fclose($stream);
|
|
}, 'OA发票数据_' . ($filters['month'] ?: 'all') . '_' . now()->format('Ymd_His') . '.csv', [
|
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
'Cache-Control' => 'private, no-store',
|
|
]);
|
|
}
|
|
|
|
public function attachment(int $invoice): RedirectResponse
|
|
{
|
|
// The row endpoint is intentionally not used for arbitrary IDs. Query the
|
|
// exact invoice record, then let DingTalk enforce whether the current app
|
|
// can mint a temporary download URL for that process attachment.
|
|
$invoiceRow = \Illuminate\Support\Facades\DB::connection(
|
|
(string) config('upstream_mirrors.connection', 'business_mirror'),
|
|
)->table('ding_invoice_record_mirror')
|
|
->where('id', $invoice)
|
|
->where('source_scope', $this->repository->scope())
|
|
->first();
|
|
abort_if($invoiceRow === null, 404);
|
|
|
|
$processInstanceId = trim((string) ($invoiceRow->process_instance_id ?? ''));
|
|
$fileId = trim((string) ($invoiceRow->source_file_id ?? ''));
|
|
abort_if($processInstanceId === '' || $fileId === '', 404, '该发票没有可下载的原始附件。');
|
|
|
|
$uri = app(DingTalkApprovalAttachmentDownloader::class)
|
|
->downloadUri($processInstanceId, $fileId);
|
|
abort_if($uri === null, 404, '原始发票附件暂时无法下载。');
|
|
|
|
return redirect()->away($uri);
|
|
}
|
|
|
|
public function requestRefresh(Request $request): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'month' => ['required', 'date_format:Y-m'],
|
|
'force' => ['nullable', 'boolean'],
|
|
]);
|
|
$state = app(DingInvoiceRefreshQueue::class)->enqueue(
|
|
$validated['month'],
|
|
(bool) ($validated['force'] ?? true),
|
|
(int) company_id(),
|
|
(int) user_id(),
|
|
);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
...$state,
|
|
'message' => $state['queued']
|
|
? '发票抓取任务已进入队列。'
|
|
: '该月份已有发票抓取任务正在排队或处理中。',
|
|
])->header('Cache-Control', 'no-store');
|
|
}
|
|
|
|
public function refreshStatus(string $token): JsonResponse
|
|
{
|
|
$status = app(DingInvoiceRefreshQueue::class)->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');
|
|
}
|
|
|
|
/** @return array<string,string> */
|
|
private function filters(Request $request): array
|
|
{
|
|
$month = trim((string) $request->query('month', ''));
|
|
$view = $request->query('invoice_view') === DingInvoiceDataMirrorRepository::VIEW_FLOWS
|
|
? DingInvoiceDataMirrorRepository::VIEW_FLOWS
|
|
: DingInvoiceDataMirrorRepository::VIEW_DETAIL;
|
|
$state = (string) $request->query('invoice_state', 'all');
|
|
|
|
return [
|
|
'month' => preg_match('/^\d{4}-\d{2}$/', $month) === 1 ? $month : '',
|
|
'view' => $view,
|
|
'invoice_state' => array_key_exists($state, DingInvoiceDataMirrorRepository::INVOICE_STATES)
|
|
? $state
|
|
: 'all',
|
|
'invoice_type' => mb_substr(trim((string) $request->query('invoice_type', '')), 0, 100),
|
|
'keyword' => mb_substr(trim((string) $request->query('keyword', '')), 0, 100),
|
|
];
|
|
}
|
|
}
|