middleware('permission:read-banking-accounts')->only('index'); $this->middleware('permission:create-banking-accounts')->only('create', 'store', 'import', 'template'); $this->middleware('permission:update-banking-accounts')->only('edit', 'update', 'export'); $this->middleware('permission:delete-banking-accounts')->only('destroy'); } /** * 列表页 */ public function index() { $storeName = mb_substr(trim((string) request()->query('store_name')), 0, 191); $fromDate = $this->dateFilter(request()->query('from')); $toDate = $this->dateFilter(request()->query('to')); $query = MarketingTaobaoAlipayBill::query(); if ($storeName !== '') { $query->where('store_name', $storeName); } if ($fromDate !== '') { $query->where('transaction_date', '>=', $fromDate); } if ($toDate !== '') { $query->where('transaction_date', '<=', $toDate); } $storeOptions = MarketingTaobaoAlipayBill::select('store_name') ->whereNotNull('store_name') ->distinct() ->orderBy('store_name') ->pluck('store_name') ->toArray(); return view('banking.marketing_taobao_alipay_bills.index', [ 'bills' => $query->orderBy('transaction_date', 'desc') ->orderBy('id', 'desc') ->paginate(setting('default.list_limit', 25)) ->appends([ 'store_name' => $storeName, 'from' => $fromDate, 'to' => $toDate, ]), 'filters' => [ 'store_name' => $storeName, 'from' => $fromDate, 'to' => $toDate, 'store_options' => $storeOptions, ], ]); } /** * 创建表单 */ public function create() { return view('banking.marketing_taobao_alipay_bills.form', [ 'bill' => new MarketingTaobaoAlipayBill([ 'company_id' => company_id(), 'transaction_date' => now()->toDateString(), 'bookkeeping_time' => now(), ]), ]); } /** * 下载导入模板 */ public function template() { return response()->streamDownload(function () { $csv = fopen('php://output', 'w'); fputs($csv, chr(0xEF) . chr(0xBB) . chr(0xBF)); // BOM for Excel fputcsv($csv, [ '店铺名称', '交易日期', '记账时间', '收支类型', '交易类型', '操作金额(元)', '操作后余额(元)', '备注', ]); fclose($csv); }, 'marketing_taobao_alipay_bills_template.csv', [ 'Content-Type' => 'text/csv; charset=UTF-8', ]); } /** * 批量导出 */ public function export(HttpRequest $request) { $ids = $request->validate([ 'ids' => ['nullable', 'array', 'max:5000'], 'ids.*' => ['integer'], ])['ids'] ?? []; $query = MarketingTaobaoAlipayBill::oldest('id'); if (!empty($ids)) { $query->whereKey($ids); } $bills = $query->get(); return response()->streamDownload(function () use ($bills) { $csv = fopen('php://output', 'w'); fputs($csv, chr(0xEF) . chr(0xBB) . chr(0xBF)); fputcsv($csv, [ '店铺名称', '交易日期', '记账时间', '收支类型', '交易类型', '操作金额(元)', '操作后余额(元)', '备注', ]); foreach ($bills as $bill) { fputcsv($csv, [ $bill->store_name, $bill->transaction_date->toDateString(), $bill->bookkeeping_time?->format('Y-m-d H:i:s'), $bill->income_expense_type, $bill->transaction_type, $bill->operation_amount, $bill->balance_after, $bill->notes, ]); } fclose($csv); }, 'marketing_taobao_alipay_bills_export.csv', [ 'Content-Type' => 'text/csv; charset=UTF-8', ]); } /** * 批量导入 */ public function import(HttpRequest $request): RedirectResponse { $validated = $request->validate([ 'import' => ['required', 'file', 'mimes:xlsx,xls,csv', 'max:5120'], ], [ 'import.required' => trans('marketing_taobao_alipay_bills.import_required'), 'import.mimes' => trans('marketing_taobao_alipay_bills.import_file_type'), 'import.max' => trans('marketing_taobao_alipay_bills.import_file_size'), ]); $file = $validated['import']; $handle = fopen($file->getRealPath(), 'r'); // Skip BOM $bom = fread($handle, 3); if ($bom !== chr(0xEF) . chr(0xBB) . chr(0xBF)) { rewind($handle); } // Skip header fgetcsv($handle); $count = 0; $companyId = company_id(); $userId = user()->id; while (($row = fgetcsv($handle)) !== false) { if (empty($row[0]) && empty($row[1])) { continue; } // Parse bookkeeping time - handle various formats $bookkeepingTime = null; if (!empty($row[2])) { try { $bookkeepingTime = \Carbon\Carbon::parse($row[2]); } catch (\Exception $e) { $bookkeepingTime = null; } } MarketingTaobaoAlipayBill::create([ 'company_id' => $companyId, 'store_name' => $row[0] ?? '', 'transaction_date' => $row[1] ?? now()->toDateString(), 'bookkeeping_time' => $bookkeepingTime, 'income_expense_type' => $row[3] ?? '支出', 'transaction_type' => $row[4] ?? null, 'operation_amount' => (float) ($row[5] ?? 0), 'balance_after' => (float) ($row[6] ?? 0), 'notes' => $row[7] ?? null, 'created_from' => 'core::ui', 'created_by' => $userId, ]); $count++; } fclose($handle); flash(trans('marketing_taobao_alipay_bills.import_success', ['count' => $count]))->success(); return redirect()->route('marketing-taobao-alipay-bills.index'); } /** * 存储新账单 */ public function store(Request $request): RedirectResponse { $data = $request->validated(); MarketingTaobaoAlipayBill::create(array_merge($data, [ 'company_id' => company_id(), 'created_from' => 'core::ui', 'created_by' => user()->id, ])); flash(trans('messages.success.created', ['type' => trans('marketing_taobao_alipay_bills.title')]))->success(); return redirect()->route('marketing-taobao-alipay-bills.index'); } /** * 编辑表单 */ public function edit(MarketingTaobaoAlipayBill $marketingTaobaoAlipayBill) { return view('banking.marketing_taobao_alipay_bills.form', [ 'bill' => $marketingTaobaoAlipayBill, ]); } /** * 更新账单 */ public function update(Request $request, MarketingTaobaoAlipayBill $marketingTaobaoAlipayBill): RedirectResponse { $marketingTaobaoAlipayBill->update($request->validated()); flash(trans('messages.success.updated', ['type' => trans('marketing_taobao_alipay_bills.title')]))->success(); return redirect()->route('marketing-taobao-alipay-bills.index'); } /** * 删除账单 */ public function destroy(MarketingTaobaoAlipayBill $marketingTaobaoAlipayBill): RedirectResponse { $marketingTaobaoAlipayBill->delete(); flash(trans('messages.success.deleted', ['type' => trans('marketing_taobao_alipay_bills.title')]))->success(); return redirect()->route('marketing-taobao-alipay-bills.index'); } private function dateFilter(mixed $value): string { $date = trim((string) $value); if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $date) !== 1) { return ''; } [$year, $month, $day] = array_map('intval', explode('-', $date)); return checkdate($month, $day, $year) ? $date : ''; } }