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() { return view('banking.marketing_pinduoduo_bills.index', [ 'bills' => MarketingPinduoduoBill::orderBy('transaction_time', 'desc') ->orderBy('id', 'desc') ->paginate(setting('default.list_limit', 25)), ]); } /** * 创建表单 */ public function create() { return view('banking.marketing_pinduoduo_bills.form', [ 'bill' => new MarketingPinduoduoBill([ 'company_id' => company_id(), 'transaction_time' => now(), ]), ]); } /** * 下载导入模板 */ public function template() { return response()->streamDownload(function () { $csv = fopen('php://output', 'w'); fputs($csv, chr(0xEF) . chr(0xBB) . chr(0xBF)); fputcsv($csv, [ '店铺名称', '时间', '资金类型', '流水类型', '交易金额', '余额', '交易摘要', ]); fclose($csv); }, 'marketing_pinduoduo_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 = MarketingPinduoduoBill::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_time?->format('Y-m-d H:i:s'), $bill->fund_type, $bill->flow_type, $bill->transaction_amount, $bill->balance, $bill->transaction_summary, ]); } fclose($csv); }, 'marketing_pinduoduo_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_pinduoduo_bills.import_required'), 'import.mimes' => trans('marketing_pinduoduo_bills.import_file_type'), 'import.max' => trans('marketing_pinduoduo_bills.import_file_size'), ]); $file = $validated['import']; $handle = fopen($file->getRealPath(), 'r'); $bom = fread($handle, 3); if ($bom !== chr(0xEF) . chr(0xBB) . chr(0xBF)) { rewind($handle); } fgetcsv($handle); $count = 0; $companyId = company_id(); $userId = user()->id; while (($row = fgetcsv($handle)) !== false) { if (empty($row[0]) && empty($row[1])) { continue; } $transactionTime = null; if (!empty($row[1])) { try { $transactionTime = \Carbon\Carbon::parse($row[1]); } catch (\Exception $e) { $transactionTime = null; } } MarketingPinduoduoBill::create([ 'company_id' => $companyId, 'store_name' => $row[0] ?? '', 'transaction_time' => $transactionTime, 'fund_type' => $row[2] ?? '', 'flow_type' => $row[3] ?? null, 'transaction_amount' => (float) ($row[4] ?? 0), 'balance' => (float) ($row[5] ?? 0), 'transaction_summary' => $row[6] ?? null, 'created_from' => 'core::ui', 'created_by' => $userId, ]); $count++; } fclose($handle); flash(trans('marketing_pinduoduo_bills.import_success', ['count' => $count]))->success(); return redirect()->route('marketing-pinduoduo-bills.index'); } /** * 存储新账单 */ public function store(Request $request): RedirectResponse { $data = $request->validated(); MarketingPinduoduoBill::create(array_merge($data, [ 'company_id' => company_id(), 'created_from' => 'core::ui', 'created_by' => user()->id, ])); flash(trans('messages.success.created', ['type' => trans('marketing_pinduoduo_bills.title')]))->success(); return redirect()->route('marketing-pinduoduo-bills.index'); } /** * 编辑表单 */ public function edit(MarketingPinduoduoBill $marketingPinduoduoBill) { return view('banking.marketing_pinduoduo_bills.form', [ 'bill' => $marketingPinduoduoBill, ]); } /** * 更新账单 */ public function update(Request $request, MarketingPinduoduoBill $marketingPinduoduoBill): RedirectResponse { $marketingPinduoduoBill->update($request->validated()); flash(trans('messages.success.updated', ['type' => trans('marketing_pinduoduo_bills.title')]))->success(); return redirect()->route('marketing-pinduoduo-bills.index'); } /** * 删除账单 */ public function destroy(MarketingPinduoduoBill $marketingPinduoduoBill): RedirectResponse { $marketingPinduoduoBill->delete(); flash(trans('messages.success.deleted', ['type' => trans('marketing_pinduoduo_bills.title')]))->success(); return redirect()->route('marketing-pinduoduo-bills.index'); } }