From 8aa7355485f2ab700f91415218a3ea4b1378c48b Mon Sep 17 00:00:00 2001 From: lewis <604446095@qq.com> Date: Tue, 11 Feb 2025 17:59:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=87=BA=E9=97=A8?= =?UTF-8?q?=E5=BA=97=E8=B4=A2=E5=8A=A1=E6=B5=81=E6=B0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StoreFinanceFlowController.php | 7 ++ .../StoreFinanceFlowLogic.php | 61 +++++++++++++++++ .../service/xlsx/StoreFinanceFlowXlsx.php | 65 +++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 app/common/service/xlsx/StoreFinanceFlowXlsx.php diff --git a/app/admin/controller/store_finance_flow/StoreFinanceFlowController.php b/app/admin/controller/store_finance_flow/StoreFinanceFlowController.php index a63bf0025..e89f95b5f 100644 --- a/app/admin/controller/store_finance_flow/StoreFinanceFlowController.php +++ b/app/admin/controller/store_finance_flow/StoreFinanceFlowController.php @@ -77,5 +77,12 @@ class StoreFinanceFlowController extends BaseAdminController return $this->data($result); } + public function export() + { + $params = $this->request->post(); + $file_path = StoreFinanceFlowLogic::export($params); + return $this->success('导出成功', ['url' => $file_path]); + } + } \ No newline at end of file diff --git a/app/admin/logic/store_finance_flow/StoreFinanceFlowLogic.php b/app/admin/logic/store_finance_flow/StoreFinanceFlowLogic.php index c39bd660e..a00ef144c 100644 --- a/app/admin/logic/store_finance_flow/StoreFinanceFlowLogic.php +++ b/app/admin/logic/store_finance_flow/StoreFinanceFlowLogic.php @@ -3,8 +3,14 @@ namespace app\admin\logic\store_finance_flow; +use app\common\enum\OrderEnum; +use app\common\enum\PayEnum; use app\common\model\store_finance_flow\StoreFinanceFlow; use app\common\logic\BaseLogic; +use app\common\model\system_store\SystemStore; +use app\common\model\system_store\SystemStoreStaff; +use app\common\model\user\User; +use app\common\service\xlsx\StoreFinanceFlowXlsx; use support\exception\BusinessException; use think\facade\Db; @@ -90,4 +96,59 @@ class StoreFinanceFlowLogic extends BaseLogic { return StoreFinanceFlow::findOrEmpty($params['id'])->toArray(); } + + public static function export($params) + { + $query = StoreFinanceFlow::where('financial_pm', 1); + if (!empty($params['type'])) { + if ($params['type'] == OrderEnum::ORDER_HANDLING_FEES || $params['type'] == OrderEnum::OTHER_ORDER_OBTAINS) { + $query->where('financial_type', $params['type']); + } elseif ($params['type'] == 11) { + $query->whereIn('financial_type', [OrderEnum::ORDER_MARGIN, OrderEnum::MERCHANT_ORDER_OBTAINS]); + } else { + $query->whereIn('financial_type', [OrderEnum::VIP_ORDER_OBTAINS, OrderEnum::VILLAGE_ORDER_OBTAINS, OrderEnum::BRIGADE_ORDER_OBTAINS]); + } + } + if (!empty($params['store_id'])) { + $query->where('store_id', $params['store_id']); + } + if (!empty($params['start_time'])) { + $query->where('create_time', '>=', strtotime($params['start_time'])); + } + if (!empty($params['end_time'])) { + $query->where('create_time', '<=', strtotime($params['end_time'])); + } + if (!empty($params['user_id'])) { + $query->where('user_id', $params['user_id']); + } + if (!empty($params['order_sn'])) { + $query->where('order_sn', 'like', "%{$params['order_sn']}%"); + } + $data = $query->order('id desc')->select()->toArray(); + $users = User::field('id,nickname,real_name')->whereIn('id', array_unique(array_column($data, 'user_id')))->select()->toArray(); + $users = reset_index($users, 'id'); + $stores = SystemStore::field('id,name')->whereIn('id', array_unique(array_column($data, 'store_id')))->select()->toArray(); + $stores = reset_index($stores, 'id'); + foreach ($data as &$item) { + $user = $users[$item['user_id']] ?? []; + if ($item['user_id'] <= 0 || empty($user)) { + $item['nickname'] = '游客'; + } else { + $item['nickname'] = $user['real_name']!=''?$user['real_name']:$user['nickname'].'|'.$item['user_id']; + } + $item['number'] = '+' . $item['number']; + $store = $stores[$item['store_id']] ?? []; + $item['store_name'] = $store['name'] ?? ''; + } + if ($params['type'] == 3) { + $title = '手续费'; + } elseif ($params['type'] == 11) { + $title = '其他收益'; + } elseif ($params['type'] == 16) { + $title = '损耗'; + } else { + $title = '佣金'; + } + return (new StoreFinanceFlowXlsx())->export($data, $title, ''); + } } \ No newline at end of file diff --git a/app/common/service/xlsx/StoreFinanceFlowXlsx.php b/app/common/service/xlsx/StoreFinanceFlowXlsx.php new file mode 100644 index 000000000..8dd5913e9 --- /dev/null +++ b/app/common/service/xlsx/StoreFinanceFlowXlsx.php @@ -0,0 +1,65 @@ +getActiveSheet(); + $sheet->setCellValue('A1', '交易单号'); + $sheet->setCellValue('B1', '交易时间'); + $sheet->setCellValue('C1', '金额'); + $sheet->setCellValue('D1', '门店'); + $sheet->setCellValue('E1', '用户'); + $sheet->setCellValue('F1', '流水类型'); + + // 设置默认的单元格样式 + $defaultStyle = [ + 'alignment' => [ + 'horizontal' => Alignment::HORIZONTAL_CENTER, + 'vertical' => Alignment::VERTICAL_CENTER, + ], + ]; + // 应用默认样式到整个工作表 + $spreadsheet->getDefaultStyle()->applyFromArray($defaultStyle); + + $column = 2; + foreach ($data as $k => $item) { + $sheet->setCellValue("A$column", $item['order_sn']); + $sheet->setCellValue("B$column", $item['create_time']); + $sheet->setCellValue("C$column", $item['number']); + $sheet->setCellValue("D$column", $item['store_name']); + $sheet->setCellValue("E$column", $item['nickname']); + $sheet->setCellValue("F$column", $type); + $column++; + } + + // 定义线框样式 + $styleArray = [ + 'borders' => [ + 'allBorders' => [ + 'borderStyle' => Border::BORDER_THIN, // 线框样式 + 'color' => ['argb' => '000000'], // 线框颜色 + ], + ], + ]; + $sheet->getStyle('A1:F' . $column)->applyFromArray($styleArray); + + $writer = new Xlsx($spreadsheet); + $url = '/export/' . "门店财务流水 - $type " . date('YmdHi') . '.xlsx'; + $file_path = public_path() . $url; + if (!is_dir(dirname($file_path))) { + mkdir(dirname($file_path), 0777, true); + } + $writer->save($file_path); + return getenv('APP_URL') . $url; + } + +}