227 lines
9.6 KiB
PHP
227 lines
9.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\EcommerceFinance\Repositories\DingInvoiceDataMirrorRepository;
|
|
use Modules\EcommerceFinance\Services\DingInvoicePreviewStore;
|
|
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'] ?? ''),
|
|
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'] ?? ''),
|
|
(string) ($row['paid_at'] ?? ''),
|
|
]);
|
|
}
|
|
} 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['expense_type'] ?? ''),
|
|
(string) ($row['payee_name'] ?? ''),
|
|
(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($payment, 2, '.', ''),
|
|
number_format((float) ($row['payment_total'] ?? 0), 2, '.', ''),
|
|
(int) ($row['invoice_count'] ?? 0),
|
|
abs(round($payment - $invoice, 2)) <= 0.005 ? '一致' : '有差异',
|
|
number_format(round(
|
|
$payment - (float) ($row['flow_invoice_total'] ?? 0),
|
|
2,
|
|
), 2, '.', ''),
|
|
(string) ($row['source_file_name'] ?? ''),
|
|
(string) ($row['ocr_provider'] ?? ''),
|
|
(string) ($row['paid_at'] ?? ''),
|
|
]);
|
|
}
|
|
}
|
|
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): StreamedResponse
|
|
{
|
|
$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);
|
|
|
|
$preview = app(DingInvoicePreviewStore::class)->ensureStored($invoiceRow);
|
|
abort_if($preview === null, 404, '该发票暂无图片或 PDF 原件可预览。');
|
|
|
|
$disk = Storage::disk('uploads');
|
|
abort_unless($disk->exists($preview['path']), 404);
|
|
|
|
$name = trim((string) ($invoiceRow->source_file_name ?? ''));
|
|
if ($name === '') {
|
|
$name = 'invoice-preview.' . $preview['extension'];
|
|
} elseif (strtolower((string) pathinfo($name, PATHINFO_EXTENSION)) !== $preview['extension']) {
|
|
$name = pathinfo($name, PATHINFO_FILENAME) . '.' . $preview['extension'];
|
|
}
|
|
|
|
return $disk->response($preview['path'], basename($name), [
|
|
'Content-Type' => $preview['mime'],
|
|
'Cache-Control' => 'private, max-age=300',
|
|
'X-Content-Type-Options' => 'nosniff',
|
|
], 'inline');
|
|
}
|
|
|
|
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),
|
|
];
|
|
}
|
|
}
|