ExcelService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\common\repositories\store\order\StoreImportDeliveryRepository;
  13. use app\common\repositories\store\order\StoreOrderRepository;
  14. use app\common\repositories\store\ExcelRepository;
  15. use app\common\repositories\store\order\StoreRefundOrderRepository;
  16. use app\common\repositories\system\merchant\FinancialRecordRepository;
  17. use think\Exception;
  18. use think\facade\Db;
  19. class ExcelService
  20. {
  21. public function getAll($data)
  22. {
  23. $this->{$data['type']}($data['where'],$data['excel_id']);
  24. }
  25. /**
  26. * TODO 导出操作
  27. * @param $id
  28. * @param $path
  29. * @param $header
  30. * @param $title
  31. * @param array $export
  32. * @param string $filename
  33. * @param array $end
  34. * @param string $suffix
  35. * @author Qinii
  36. * @day 3/17/21
  37. */
  38. public function export($id, $path, $header, $title, $export = [], $filename = '',$end = [],$suffix = 'xlsx')
  39. {
  40. try{
  41. $_path = SpreadsheetExcelService::instance()
  42. ->createOrActive()
  43. ->setExcelHeader($header,count($title['mark']) + 2)
  44. ->setExcelTile($title)
  45. ->setExcelContent($export)
  46. ->setExcelEnd($end)
  47. ->excelSave($filename, $suffix, $path);
  48. app()->make(ExcelRepository::class)->update($id,[
  49. 'name' => $filename.'.'.$suffix,
  50. 'status' => 1,
  51. 'path' => '/'.$_path
  52. ]);
  53. }catch (Exception $exception){
  54. app()->make(ExcelRepository::class)->update($id,[
  55. 'name' => $filename.'.'.$suffix,
  56. 'status' => 2,
  57. 'message' => $exception->getMessage()
  58. ]);
  59. }
  60. }
  61. /**
  62. * TODO 导出订单
  63. * @param array $where
  64. * @param int $id
  65. * @author Qinii
  66. * @day 2020-08-10
  67. */
  68. public function order(array $where,int $id)
  69. {
  70. $make = app()->make(StoreOrderRepository::class);
  71. $status = $where['status'];
  72. $del = $where['mer_id'] > 0 ? 0 : null;
  73. unset($where['status']);
  74. $query = $make->search($where,$del)->where($make->getOrderType($status))->order('order_id ASC');
  75. $list = $query->with([
  76. 'orderProduct',
  77. 'merchant' => function ($query) {return $query->field('mer_id,mer_name');},
  78. 'user.spread'
  79. ])->select()->each(function($item){
  80. $item['refund_price'] = app()->make(StoreRefundOrderRepository::class)->refundPirceByOrder([$item['order_id']]);
  81. return $item;
  82. });
  83. $header = ['序号','订单编号','订单类型','推广人','用户信息', '商品名称','商品规格','单商品总数','商品价格(元)','优惠','实付邮费(元)','实付金额(元)','已退款金额(元)', '收货人','收货人电话','收货地址','物流单号','下单时间','支付方式','支付状态','商家备注'];
  84. $title = [
  85. 'title' => '订单列表',
  86. 'sheets' => '订单信息',
  87. 'mark' => ['生成时间:' . date('Y-m-d H:i:s',time())],
  88. ];
  89. $export = $this->orderList($list->toArray());
  90. $filename = '订单列表_'.date('YmdHis');
  91. $end = [];
  92. return $this->export($id, 'order', $header, $title, $export,$filename, $end);
  93. }
  94. /**
  95. * TODO 整理订单信息
  96. * @param array $data
  97. * @return array
  98. * @author Qinii
  99. * @day 2020-08-10
  100. */
  101. public function orderList(array $data)
  102. {
  103. $result = [];
  104. if(empty($data)) return $result;
  105. $i = 1;
  106. foreach ($data as $item){
  107. foreach ($item['orderProduct'] as $key => $value){
  108. $result[] = [
  109. $i,
  110. $item['order_sn'],
  111. $item['order_type'] ? '核销订单':'普通订单',
  112. $item['user']['spread']['nickname'],
  113. $item['user']['nickname'],
  114. $value['cart_info']['product']['store_name'],
  115. $value['cart_info']['productAttr']['sku'],
  116. $value['product_num'],
  117. $value['cart_info']['productAttr']['price'],
  118. ($key == 0 ) ? $item['coupon_price'] : 0,
  119. ($key == 0 ) ? $item['pay_postage'] : 0,
  120. $value['product_price'],
  121. ($key == 0 ) ? $item['refund_price'] : 0,
  122. $item['real_name'],
  123. $item['user_phone'],
  124. $item['user_address'],
  125. $item['delivery_id'],
  126. $item['create_time'],
  127. $item['pay_type'] ? '微信': '余额',
  128. $item['paid'] ? '已支付':'未支付',
  129. $item['remark']
  130. ];
  131. $i++;
  132. }
  133. }
  134. return $result;
  135. }
  136. /**
  137. * TODO 流水记录导出
  138. * @param array $where
  139. * @param int $id
  140. * @author Qinii
  141. * @day 2020-08-10
  142. */
  143. public function financial(array $where,int $id)
  144. {
  145. $_key = [
  146. 'mer_accoubts' => '财务对账',
  147. 'sys_accoubts' => '财务对账',
  148. 'refund_order' => '退款订单',
  149. 'brokerage_one' => '一级分佣',
  150. 'brokerage_two' => '二级分佣',
  151. 'refund_brokerage_one' => '返还一级分佣',
  152. 'refund_brokerage_two' => '返还二级分佣',
  153. 'order' => '订单支付',
  154. ];
  155. $make = app()->make(FinancialRecordRepository::class);
  156. $query = $make->search($where)->with(['merchant']);
  157. $list = $query->select()->toArray();
  158. $header = ['序号','商户名称','交易流水单号','订单号','用户名','用户ID','交易类型','收入/支出','金额','创建时间'];
  159. $title = [
  160. 'title' => '流水列表',
  161. 'sheets' => '流水信息',
  162. 'mark' => ['生成时间:' . date('Y-m-d H:i:s',time())],
  163. ];
  164. $export = [];
  165. foreach ($list as $k => $v){
  166. $export[]=[
  167. $k + 1,
  168. $v['merchant']['mer_name'],
  169. $v['financial_record_sn'],
  170. $v['order_sn'],
  171. $v['user_info'],
  172. $v['user_id'],
  173. $_key[$v['financial_type']],
  174. $v['financial_pm'] ? '收入' : '支出',
  175. ($v['financial_pm'] ? '+ ' : '- ') . $v['number'],
  176. $v['create_time'],
  177. ];
  178. }
  179. $filename = '流水列表_'.date('YmdHis');
  180. return $this->export($id,'financial',$header,$title,$export,$filename,[],'xlsx');
  181. }
  182. /**
  183. * TODO 获取待发货订单 发货信息
  184. * @param array $where
  185. * @param int $id
  186. * @author Qinii
  187. * @day 3/13/21
  188. */
  189. public function delivery(array $where,int $id)
  190. {
  191. $make = app()->make(StoreOrderRepository::class);
  192. $list = $make->search($where)->where('order_type',0)->with(['orderProduct'])->order('order_id ASC')->select();
  193. $header = ['序号','订单编号','物流公司','物流编码','物流单号', '发货地址','用户信息','手机号','商品信息','支付时间'];
  194. $title = [
  195. 'title' => '批量发货单',
  196. 'sheets' => '发货信息',
  197. 'mark' => ['生成时间:' . date('Y-m-d H:i:s',time())],
  198. ];
  199. $filename = '批量发货单_'.date('YmdHis');
  200. $export = [];
  201. if($list){
  202. $data = $list->toArray();
  203. foreach ($data as $key => $item){
  204. $product = '';
  205. foreach ($item['orderProduct'] as $value){
  206. $product = $product.$value['cart_info']['product']['store_name'].'【'. $value['cart_info']['productAttr']['sku'] .'】【' . $value['product_num'].'】'.PHP_EOL;
  207. }
  208. $export[] = [
  209. $key + 1,
  210. $item['order_sn'] ?? '',
  211. '',
  212. $item['delivery_name']??"",
  213. $item['delivery_id']??"",
  214. $item['user_address']??"",
  215. $item['real_name'] ?? '',
  216. $item['user_phone'] ?? '',
  217. $product,
  218. $item['pay_time'] ?? '',
  219. ];
  220. }
  221. }
  222. $end = [];
  223. return $this->export($id,'delivery',$header,$title,$export,$filename,$end);
  224. }
  225. /**
  226. * TODO 导出 发货导入记录
  227. * @param array $where
  228. * @param int $id
  229. * @author Qinii
  230. * @day 3/17/21
  231. */
  232. public function importDelivery(array $where,int $id)
  233. {
  234. $make = app()->make(StoreImportDeliveryRepository::class);
  235. $list = $make->getSearch($where)->order('create_time ASC')->select();
  236. $title = [
  237. 'title' => '发货记录',
  238. 'sheets' => '发货信息',
  239. 'mark' => [
  240. '生成时间:' . date('Y-m-d H:i:s',time())
  241. ],
  242. ];
  243. $header = ['序号','订单编号','物流公司','物流单号', '发货状态','备注'];
  244. $filename = '发货单记录_'.date('YmdHis');
  245. $export = [];
  246. if($list){
  247. $data = $list->toArray();
  248. foreach ($data as $key => $item){
  249. $export[] = [
  250. $key + 2,
  251. $item['order_sn'],
  252. $item['delivery_name'],
  253. $item['delivery_id'],
  254. $item['status'],
  255. $item['mark'],
  256. ];
  257. }
  258. }
  259. $end = [];
  260. return $this->export($id,'delivery',$header,$title,$export,$filename,$end);
  261. }
  262. /**
  263. * TODO 平台/商户 导出日月账单信息
  264. * @param array $where
  265. * @param int $id
  266. * @author Qinii
  267. * @day 3/25/21
  268. */
  269. public function exportFinancial(array $where,int $id)
  270. {
  271. /*
  272. order 收入 公共 新订单
  273. brokerage_one 支出 公共 一级佣金
  274. brokerage_two 支出 公共 二级佣金
  275. order_charge 支出 商户 手续费
  276. order_true 支出 平台 商户入账
  277. refund_order 支出 公共 退款
  278. refund_brokerage_one 收入 公共 返还一级佣金
  279. refund_brokerage_two 收入 公共 返还二级佣金
  280. refund_charge 收入 商户 返还手续费
  281. refund_true 收入 平台 商户返还入账
  282. presell 收入 公共 新订单
  283. presell_charge 支出 商户 手续费
  284. presell_true 支出 平台 商户入账
  285. */
  286. $financialType = [
  287. 'order' => '订单支付',
  288. 'presell' => '预售订单(尾款)',
  289. 'brokerage_one' => '一级佣金',
  290. 'brokerage_two' => '二级佣金',
  291. 'order_charge' => '手续费',
  292. 'order_true' => '商户入账',
  293. 'refund_order' => '退款',
  294. 'refund_charge' => '返还手续费',
  295. 'refund_true' => '商户返还入账',
  296. 'presell_charge'=> '预售订单(手续费)',
  297. 'presell_true' => '商户入账',
  298. 'refund_brokerage_one' => '返还一级佣金',
  299. 'refund_brokerage_two' => '返还二级佣金',
  300. 'mer_presell' => '预售订单(总额)',
  301. 'order_presell' => '预售订单(定金)'
  302. ];
  303. $sys_pm_1 = ['order','presell','order_charge','order_presell','presell_charge','refund_brokerage_one','refund_brokerage_two'];
  304. $mer_pm_1 = ['order','presell','refund_charge','refund_brokerage_one','refund_brokerage_two','mer_presell'];
  305. $date_ = $where['date'];unset($where['date']);
  306. $make = app()->make(FinancialRecordRepository::class);
  307. $query = $make->search($where)->with(['orderInfo','refundOrder','merchant.merchantCategory']);
  308. if($where['type'] == 1){
  309. $title_ = '日账单';
  310. $start_date = $date_.' 00:00:00';
  311. $end_date = $date_.' 23:59:59';
  312. $query->whereDay('create_time',$date_);
  313. }else{
  314. $title_ = '月账单';
  315. $start_date = (date('Y-m-01', strtotime($date_)));
  316. $end_date = date('Y-m-d', strtotime("$start_date +1 month -1 day"));
  317. $query->whereMonth('create_time',$date_);
  318. }
  319. $list = $query->order('create_time DESC')->select();
  320. $income = $make->countIncome($where['type'],$where,$date_);
  321. $expend = $make->countExpend($where['type'],$where,$date_);
  322. $refund = $make->countRefund($where['type'],$where,$date_);
  323. // $charge = $make->countCharge($where['type'],$where,$date_);
  324. $charge = bcsub($income['number'],$expend['number'],2);
  325. $filename = $title_.'('.$date_.')'.time();
  326. $title = [];
  327. $header = [];
  328. $export = [];
  329. $end = [];
  330. //平台
  331. if(!$where['is_mer']){
  332. $header = ['序号','商户类别','商户分类','商户名称','订单编号','交易流水号','交易时间', '对方信息','交易类型','收支金额','备注'];
  333. foreach ($list as $key => $value){
  334. $export[] = [
  335. $key + 1,
  336. $value['merchant']['is_trader'] ? '自营' : '非自营',
  337. $value['merchant']['merchantCategory']['category_name'] ?? '平台',
  338. $value['merchant']['mer_name'] ?? '平台',
  339. $value['order_sn'],
  340. $value['financial_record_sn'],
  341. $value['create_time'],
  342. $value['user_info'],
  343. $financialType[$value['financial_type']],
  344. (in_array($value['financial_type'],$sys_pm_1) ? '+' : '-') . $value['number'],
  345. ''
  346. ];
  347. }
  348. $end = [
  349. '合计:平台应入账手续费 '.$charge,
  350. '收入合计: '.'订单支付'.$income['count'].'笔,'.'实际支付金额共:'.$income['number'].'元;',
  351. '支出合计: '.'佣金支出'.$expend['count_brokerage'].'笔,支出金额:'.$expend['number_brokerage'].'元;商户入账支出'.$expend['count_order'].'笔,支出金额:'.$expend['number_order'].'元;退款手续费'.$expend['count_charge'].'笔,支出金额'.$expend['number_charge'].'元;合计支出'.$expend['number'],
  352. ];
  353. //商户
  354. }else{
  355. $header = ['序号','订单编号','交易流水号','交易时间', '对方信息','交易类型','收支金额','备注'];
  356. $mer_name = '';
  357. foreach ($list as $key => $value){
  358. $export[] = [
  359. $key + 1,
  360. $value['order_sn'],
  361. $value['financial_record_sn'],
  362. $value['create_time'],
  363. $value['user_info'],
  364. $financialType[$value['financial_type']],
  365. (in_array($value['financial_type'],$mer_pm_1) ? '+' : '-') . $value['number'],
  366. ''
  367. ];
  368. $mer_name = $mer_name ? $mer_name : ($value['merchant']['mer_name'] ?? '');
  369. }
  370. $count_brokeage = $expend['count_brokerage'] + $expend['count_refund_brokerage'];
  371. $number_brokeage = bcsub($expend['number_brokerage'],$expend['number_refund_brokerage'],2);
  372. $count_charge = $expend['count_charge']+$expend['count_order_charge'];
  373. $number_charge = bcsub($expend['number_order_charge'],$expend['number_charge'],2);
  374. $end = [
  375. '合计:商户应入金额 '.$charge,
  376. '收入合计: '.'订单支付'.$income['count'].'笔,'.'实际支付金额共:'.$income['number'].'元;',
  377. '支出合计: '.'佣金支出'.$count_brokeage.'笔,支出金额:'.$number_brokeage.'元;退款'.$expend['count_refund'].'笔,支出金额:'.$expend['number_refund'].'元;平台手续费'.$count_charge.'笔,支出金额:'.$number_charge.'元;合计支出金额:'.$expend['number'].'元;',
  378. //'商户应入金额 '.$charge,
  379. ];
  380. $mer_name = '商户名称:'.$mer_name;
  381. }
  382. $title = [
  383. 'title' => $title_,
  384. 'sheets' => $title_.'信息',
  385. 'mark' => [
  386. $mer_name ?? '平台',
  387. '结算账期:【' .$start_date.'】至【'.$end_date.'】',
  388. '生成时间:' . date('Y-m-d H:i:s',time())
  389. ],
  390. ];
  391. return $this->export($id,'financial',$header,$title,$export,$filename,$end);
  392. }
  393. }