diff --git a/app/common/model/system/merchant/FinancialRecord.php b/app/common/model/system/merchant/FinancialRecord.php index 3a62f102..c3d7ecf8 100755 --- a/app/common/model/system/merchant/FinancialRecord.php +++ b/app/common/model/system/merchant/FinancialRecord.php @@ -61,13 +61,14 @@ class FinancialRecord extends BaseModel case 'commission_to_platform': return '平台手续费:' . $this->number; case 'platform_consumption': - return '平台优惠:' . $this->number; + return '使用平台优惠:' . $this->number; case 'auto_margin': return '保证金:' . $this->number; case 'commission_to_promoter': - return '推广佣金:' . $this->number; + $nickname = User::where('uid', $this->user_id)->value('nickname'); + return "“{$nickname}”获得推广佣金:" . $this->number; default: - return ''; + return '商户入账:' . $this->number; } } diff --git a/app/common/repositories/store/order/StoreOrderRepository.php b/app/common/repositories/store/order/StoreOrderRepository.php index b0985eca..86cb7bc1 100755 --- a/app/common/repositories/store/order/StoreOrderRepository.php +++ b/app/common/repositories/store/order/StoreOrderRepository.php @@ -327,7 +327,7 @@ class StoreOrderRepository extends BaseRepository $platformCommission = bcmul($orderValidAmount, (string)$commission_rate, 2); if ($commission_rate > 0 && $platformCommission > 0) { $orderValidAmount = bcsub($orderValidAmount, $platformCommission, 2); - if ($promoterCommission > 0) { + if ($promoterCommission > 0 && !empty($promoter)) { $platformCommission = bcsub($platformCommission, $promoterCommission, 2); } $financeDao->platformIn($platformCommission, 'commission_to_platform', $order['mer_id']); diff --git a/app/controller/admin/system/merchant/FinancialRecord.php b/app/controller/admin/system/merchant/FinancialRecord.php index c860bc65..e40ce2b7 100755 --- a/app/controller/admin/system/merchant/FinancialRecord.php +++ b/app/controller/admin/system/merchant/FinancialRecord.php @@ -161,4 +161,28 @@ class FinancialRecord extends BaseController return app('json')->success($data); } + /** + * TODO 导出文件 + * @param $type + * @author Qinii + * @day 3/25/21 + */ + public function exportDetail2($type) + { + [$page, $limit] = $this->getPage(); + $date = $this->request->param('date'); + $where['date'] = empty($date) ? date('Y-m-d',time()) : $date ; + $where['type'] = $type; + $where['is_mer'] = $this->request->merId() ?? 0 ; + try { + $merchant = $this->request->merchant(); + }catch (\Exception $e){ + $merchant = []; + } + /** @var ExcelService $make */ + $make = app()->make(ExcelService::class); + $data = $make->exportFinancial2($where,$page,$limit,$merchant); + return app('json')->success($data); + } + } diff --git a/crmeb/services/ExcelService.php b/crmeb/services/ExcelService.php index 4e53ef3c..ce520590 100755 --- a/crmeb/services/ExcelService.php +++ b/crmeb/services/ExcelService.php @@ -314,7 +314,7 @@ class ExcelService } /** - * TODO 平台/商户 导出日月账单信息 + * 平台/商户 导出日月账单信息 * @param array $where * @param int $id * @author Qinii @@ -356,46 +356,34 @@ class ExcelService $limit = 20; $count = $query->count(); $i = 1; - /** @var StoreOrderRepository $order_make */ $order_make = app()->make(StoreOrderRepository::class); //平台 if (!$where['is_mer']) { $header = ['商户类别', '商户分类', '商户名称', '总订单号', '订单编号', '交易流水号', '交易时间', '对方信息', '交易类型', '收支金额', '备注']; - $list = $query->where('financial_type', 'order')->page($page, $limit)->order('create_time DESC')->select(); - foreach ($list as $financialRecord) { - $otherRecords = FinancialRecord::where('order_id', $financialRecord['order_id']) - ->where('financial_type', '<>', 'order') - ->field('financial_type,number') - ->select(); - $mark = []; - foreach ($otherRecords as $otherRecord) { - /** @var FinancialRecord $otherRecord */ - if (!empty($otherRecord->getMark())) { - $mark[] = $otherRecord->getMark(); - } - } - if ($financialRecord['merchant']) { - $exportItem = [ - $financialRecord['merchant']['is_trader'] ? '自营' : '非自营', - $financialRecord['merchant']['merchantCategory']['category_name'] ?? '平台', - $financialRecord['merchant']['mer_name'] ?? '平台', + $list = $query->page($page, $limit)->order('create_time DESC')->select(); + foreach ($list as $k => $value) { + $order = $order_make->get($value['order_id']); + if ($value['merchant']) { + $export[$k] = [ + $value['merchant']['is_trader'] ? '自营' : '非自营', + $value['merchant']['merchantCategory']['category_name'] ?? '平台', + $value['merchant']['mer_name'] ?? '平台', ]; }else{ - $exportItem = [ + $export[$k] = [ '平台', '平台', '平台', ]; } - $exportItem[] = ['groupOrder']['group_order_sn'] ?? '无数据'; - $exportItem[] = $financialRecord['order_sn']; - $exportItem[] = $financialRecord['financial_record_sn']; - $exportItem[] = $financialRecord['create_time']; - $exportItem[] = $financialRecord['user_info']; - $exportItem[] = $financialType[$financialRecord['financial_type']]; - $exportItem[] = (in_array($financialRecord['financial_type'], $sys_pm_1) ? '+' : '-') . $financialRecord['number']; - $exportItem[] = implode(',', $mark); - $export[] = $exportItem; + $export[$k][] = ['groupOrder']['group_order_sn'] ?? '无数据'; + $export[$k][] = $value['order_sn']; + $export[$k][] = $value['financial_record_sn']; + $export[$k][] = $value['create_time']; + $export[$k][] = $value['user_info']; + $export[$k][] = $financialType[$value['financial_type']]; + $export[$k][] = (in_array($value['financial_type'], $sys_pm_1) ? '+' : '-') . $value['number']; + $export[$k][] = ''; } $foot = [ '合计:平台应入账手续费 ' . $charge, @@ -452,6 +440,95 @@ class ExcelService return compact('count', 'header', 'title', 'export', 'foot', 'filename'); } + /** + * 平台导出日月账单信息 + * @param array $where + * @param int $page + * @param int $limit + * @param $merchant + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function exportFinancial2(array $where, int $page, int $limit, $merchant = []) + { + $financialType = FinancialRecordRepository::TYPE_CN_MAP; + $date_ = $where['date']; + unset($where['date']); + /** @var FinancialRecordRepository $make */ + $make = app()->make(FinancialRecordRepository::class); + $query = $make->search($where)->with(['orderInfo', 'refundOrder', 'merchant.merchantCategory']); + if ($where['type'] == 1) { + $title_ = '日账单'; + $start_date = $date_ . ' 00:00:00'; + $end_date = $date_ . ' 23:59:59'; + $query->whereDay('create_time', $date_); + } else { + $title_ = '月账单'; + $start_date = (date('Y-m-01', strtotime($date_))); + $end_date = date('Y-m-d', strtotime("$start_date +1 month -1 day")); + $query->whereMonth('create_time', $date_); + } + $query->where('financial_type', 'order'); + $income = $make->countIncome($where['type'], $where, $date_, $merchant); + $expend = $make->countExpend($where['type'], $where, $date_, $merchant); + $charge = bcsub($income['number'], $expend['number'], 2); + $filename = $title_ . '(' . $date_ . ')' . time(); + $export = []; + $limit = 20; + $count = $query->count(); + $header = ['商户类别', '商户分类', '商户名称', '总订单号', '订单编号', '交易流水号', '交易时间', '对方信息', '交易类型', '收支金额', '备注']; + $list = $query->page($page, $limit)->order('create_time DESC')->select(); + foreach ($list as $financialRecord) { + $otherRecords = FinancialRecord::where('order_id', $financialRecord['order_id']) + ->whereIn('financial_type', ['commission_to_platform', 'platform_consumption', 'auto_margin', 'commission_to_promoter', 'merchant_order']) + ->field('financial_type,number') + ->select(); + $mark = []; + foreach ($otherRecords as $otherRecord) { + /** @var FinancialRecord $otherRecord */ + if (!empty($otherRecord->getMark())) { + $mark[] = $otherRecord->getMark(); + } + } + if ($financialRecord['merchant']) { + $exportItem = [ + $financialRecord['merchant']['is_trader'] ? '自营' : '非自营', + $financialRecord['merchant']['merchantCategory']['category_name'] ?? '平台', + $financialRecord['merchant']['mer_name'] ?? '平台', + ]; + }else{ + $exportItem = [ + '平台', + '平台', + '平台', + ]; + } + $exportItem[] = ['groupOrder']['group_order_sn'] ?? '无数据'; + $exportItem[] = $financialRecord['order_sn']; + $exportItem[] = $financialRecord['financial_record_sn']; + $exportItem[] = $financialRecord['create_time']; + $exportItem[] = $financialRecord['user_info']; + $exportItem[] = $financialType[$financialRecord['financial_type']]; + $exportItem[] = $financialRecord['number']; + $exportItem[] = implode(',', $mark); + $export[] = $exportItem; + } + $foot = [ + '合计:平台应入账手续费 ' . $charge, + '收入合计: ' . '订单支付' . $income['count'] . '笔,' . '实际支付金额共:' . $income['number'] . '元;', + '支出合计: ' . '佣金支出' . $expend['count_brokerage'] . '笔,支出金额:' . $expend['number_brokerage'] . '元;商户入账支出' . $expend['count_order'] . '笔,支出金额:' . $expend['number_order'] . '元;退款手续费' . $expend['count_charge'] . '笔,支出金额' . $expend['number_charge'] . '元;合计支出' . $expend['number'], + ]; + $title = [ + $title_, + $mer_name ?? '平台', + '结算账期:【' . $start_date . '】至【' . $end_date . '】', + '生成时间:' . date('Y-m-d H:i:s', time()) + ]; + return compact('count', 'header', 'title', 'export', 'foot', 'filename'); + } + /** * TODO 退款单导出 * @param array $where diff --git a/route/admin/accounts.php b/route/admin/accounts.php index 6da6e1bb..15b82f4f 100755 --- a/route/admin/accounts.php +++ b/route/admin/accounts.php @@ -196,6 +196,9 @@ Route::group(function () { Route::get('detail_export/:type', '/exportDetail')->name('systemFinancialRecordDetailExport')->option([ '_alias' => '导出', ]); + Route::get('detail_export2/:type', '/exportDetail2')->name('systemFinancialRecordDetailExport2')->option([ + '_alias' => '导出', + ]); })->prefix('admin.system.merchant.FinancialRecord')->option([ '_path' => '/accounts/statement', '_auth' => true,