AfterSalesController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/10/19
  8. * Time: 17:11
  9. */
  10. namespace app\backend\modules\refund\controllers;
  11. use app\backend\modules\refund\models\OrderRefund;
  12. use app\backend\modules\refund\services\AfterSalesExport;
  13. use app\common\components\BaseController;
  14. use app\common\exceptions\AppException;
  15. use app\common\exceptions\ShopException;
  16. class AfterSalesController extends BaseController
  17. {
  18. /**
  19. * @return OrderRefund
  20. */
  21. protected function refundModel()
  22. {
  23. return new OrderRefund();
  24. }
  25. /**
  26. * @return OrderRefund
  27. */
  28. protected function orderRefund()
  29. {
  30. return $this->refundModel()->uniacid();
  31. }
  32. public function index()
  33. {
  34. return view('refund.after-sales.list', [
  35. 'data' => json_encode($this->viewData())
  36. ])->render();
  37. }
  38. /**
  39. * @return array
  40. */
  41. protected function viewData():array
  42. {
  43. return [];
  44. }
  45. public function getList()
  46. {
  47. $search = request()->input('search');
  48. $orderRefundModel = $this->orderRefund()->backendSearch($search);
  49. $count['total_price'] = $orderRefundModel->sum('yz_order_refund.price');
  50. $page = $orderRefundModel->orderBy('yz_order_refund.id', 'desc')->paginate(15);
  51. $count['total'] = $page->total();
  52. $data['count'] = $count;
  53. $page->map(function ($refund) {
  54. $refund->order->setAppends(['status_name','pay_type_name','fixed_button']);
  55. // $refund->order->makeHidden(['backend_button_models','row_bottom']);
  56. });
  57. $data['list'] = $page->toArray();
  58. $data['extra_param'] = $this->mergeExtraParam() ?: [];
  59. return $this->successJson('list', $data);
  60. }
  61. public function detail()
  62. {
  63. $id = intval(request()->input('id'));
  64. if (empty($id)) {
  65. throw new AppException('参数为空');
  66. }
  67. $refund = $this->refundModel()::detail($id);
  68. if (!$refund) {
  69. throw new AppException('售后记录不存在');
  70. }
  71. $refund->order->dispatch = null;
  72. if(!$refund->order->expressmany->isEmpty() && $refund->order->status>1){
  73. $order = $refund->order;
  74. $dispatch = [];
  75. //兼容以前的 因为批量发货并不会把快递id赋值给订单商品
  76. if($order->is_all_send_goods==0){
  77. $express = $order->express->getExpress($order->express->express_code, $order->express->express_sn);
  78. $dispatch[0]['order_express_id'] = $order->expressmany[0]->id;
  79. $dispatch[0]['express_sn'] = $order->expressmany[0]->express_sn;
  80. $dispatch[0]['company_name'] = $order->expressmany[0]->express_company_name;
  81. $dispatch[0]['data'] = $express['data'];
  82. $dispatch[0]['thumb'] = $order->hasManyOrderGoods[0]->thumb;
  83. $dispatch[0]['tel'] = '95533';
  84. $dispatch[0]['status_name'] = $express['status_name'];
  85. $dispatch[0]['count'] = count($order->hasManyOrderGoods);
  86. $dispatch[0]['goods'] = $order->hasManyOrderGoods;
  87. }else{
  88. $expressmany = $order->expressmany;
  89. foreach ($expressmany as $k=>$v){
  90. $express = $order->express->getExpress($v->express_code, $v->express_sn);
  91. $dispatch[$k]['order_express_id'] = $v->id;
  92. $dispatch[$k]['express_sn'] = $v->express_sn;
  93. $dispatch[$k]['company_name'] = $v->express_company_name;
  94. $dispatch[$k]['data'] = $express['data'];
  95. $dispatch[$k]['thumb'] = $v->ordergoods[0]->thumb;
  96. $dispatch[$k]['tel'] = '95533';
  97. $dispatch[$k]['status_name'] = $express['status_name'];
  98. $dispatch[$k]['count'] = count($v['ordergoods']);
  99. $dispatch[$k]['goods'] = $v['ordergoods'];
  100. }
  101. }
  102. $refund->order->dispatch = $dispatch;
  103. }
  104. $refund->order->setAppends(['status_name','pay_type_name','fixed_button']);
  105. //如当前售后不是订单正在进行中的就不显示售后操作
  106. $refund->backend_button_models = [];
  107. if ($refund->order->refund_id && $refund->order->refund_id == $refund->id) {
  108. $refund->backend_button_models = $refund->getBackendButtonModels();
  109. }
  110. $refund->refundSteps = $refund->getBackendRefundSteps();
  111. $data['refund'] = $refund->toArray();
  112. return view('refund.after-sales.detail', [
  113. 'data' => json_encode($this->detailViewData($data))
  114. ])->render();
  115. }
  116. /**
  117. * @param $data
  118. * @return array
  119. */
  120. public function detailViewData($data):array
  121. {
  122. return $data;
  123. }
  124. public function export()
  125. {
  126. $search = request()->input('search');
  127. $orderRefundModel = $this->orderRefund()->backendSearch($search);
  128. $list = $orderRefundModel->orderBy('yz_order_refund.id', 'desc')->get();
  129. // $list = $list->map(function ($refund) {
  130. // $refund->order->setAppends(['status_name','pay_type_name','fixed_button']);
  131. // });
  132. if ($list->isEmpty()) {
  133. throw new ShopException('没有可导出的售后记录');
  134. }
  135. foreach ($list as $key => $item) {
  136. $export_data[] = [
  137. 'order_sn' => $item->order->order_sn,
  138. 'refund_sn' => $item->refund_sn,
  139. 'price' => $item->price,
  140. 'uid' => $item->uid,
  141. 'nickname' => $this->getNickname($item->hasOneMember->nickname),
  142. 'order_type_name' => $item->order_type_name,
  143. 'refund_type_name' => $item->refund_type_name,
  144. 'status_name' => $item->status_name,
  145. 'goods' => $item->refundOrderGoods->toArray(),
  146. 'create_time' => $item->create_time->toDateTimeString(),
  147. 'refund_time' => $item->refund_time ? $item->refund_time->toDateTimeString() : '',
  148. 'reason' => $item->reason,
  149. 'part_refund_name' => $item->part_refund_name,
  150. ];
  151. }
  152. $file_name = date('Ymdhis', time()) . '售后列表导出.xls';
  153. return \app\exports\ExcelService::customExport(new AfterSalesExport($export_data),$file_name);
  154. }
  155. protected function getNickname($nickname)
  156. {
  157. if (substr($nickname, 0, strlen('=')) === '=') {
  158. $nickname = ',' . $nickname;
  159. }
  160. //去除微信昵称中会带有emoji和特殊符号(颜文字等)否则执行到该值时,后续数据导致空白丢失
  161. $nickname = preg_replace_callback('/./u',function (array $match) {
  162. return strlen($match[0]) >= 4 ? '' : $match[0];
  163. }, $nickname);
  164. return $nickname;
  165. }
  166. protected function mergeExtraParam()
  167. {
  168. $extraParam = [
  169. 'package_deliver' => app('plugins')->isEnabled('package-deliver'),
  170. 'team_dividend' => app('plugins')->isEnabled('team-dividend'),
  171. 'printer' => (app('plugins')->isEnabled('printer') || app('plugins')->isEnabled('more-printer'))
  172. ];
  173. return $extraParam;
  174. }
  175. }