Order.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 订单
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Order extends Backend
  11. {
  12. /**
  13. * Order模型对象
  14. * @var \app\admin\model\Order
  15. */
  16. protected $model = null;
  17. protected $noNeedLogin = ['xieyipdf'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Order;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. $this->view->assign("roadtypeList", $this->model->getRoadtypeList());
  24. $this->view->assign("jxtypeList", $this->model->getJxtypeList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->with(['product','usera','userb'])
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->getRelation('product')->visible(['name']);
  53. $row->getRelation('usera')->visible(['nickname','mobile']);
  54. $row->getRelation('userb')->visible(['nickname','mobile']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 退款
  63. */
  64. public function refund(){
  65. $id = input('id',0);
  66. $row = Db::name('order')->where('id',$id)->find();
  67. if($this->request->isPost()){
  68. if($row['status'] != 21){
  69. $this->error('订单状态有误,请勿重复提交');
  70. }
  71. $refund_reason = input('refund_reason','');
  72. $refund_remark = input('refund_remark','');
  73. $refund_fee = input('refund_fee',0);
  74. if($refund_fee > $row['pay_fee']){
  75. $this->error('退还金额不能大于实际支付金额'.$row['pay_fee']);
  76. }
  77. //更新订单
  78. $update = [
  79. 'refund_reason' => $refund_reason,
  80. 'refund_time' => time(),
  81. 'refund_remark' => $refund_remark,
  82. 'refund_fee' => $refund_fee,
  83. 'status' => 22,
  84. ];
  85. Db::startTrans();
  86. $rs = Db::name('order')->where('id',$id)->update($update);
  87. if($rs === false){
  88. Db::rollback();
  89. $this->error('提交失败');
  90. }
  91. //车票还回去
  92. $order_id = $id;
  93. $order_road = Db::name('order_road')->where('order_id',$order_id)->lock(true)->select();
  94. foreach($order_road as $key => $road){
  95. $chufabanci = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->lock(true)->find();
  96. if(empty($chufabanci)){
  97. continue;
  98. }
  99. $update = [
  100. 'ticket_remain' => $chufabanci['ticket_remain'] + $road['ticket_number'],
  101. ];
  102. $rs_ticket = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->update($update);
  103. if($rs_ticket === false){
  104. Db::rollback();
  105. $this->error('退回票失败');
  106. }
  107. }
  108. //
  109. if($refund_fee > 0){
  110. //退款
  111. $refund_result = $this->old_refund($row, 'order',$refund_fee,'订单申请退款');
  112. if($refund_result !== true){
  113. Db::rollback();
  114. $this->error($refund_result);
  115. }
  116. }
  117. Db::commit();
  118. $this->success('退款完成');
  119. }
  120. $this->view->assign("row", $row);
  121. return $this->view->fetch();
  122. }
  123. // 退款
  124. private function old_refund($order,$table, $refund_price,$remark = '')
  125. {
  126. $order['pay_type'] = 'wechat';
  127. // 生成退款单
  128. $refund_data = [
  129. 'order_id' => $order['id'],
  130. 'out_refund_no'=> createUniqueNo('R',$order['id']),
  131. 'pay_fee' => $order['pay_fee'],
  132. 'refund_price' => $refund_price,
  133. 'pay_type' => $order['pay_type'],
  134. 'status' => 0,
  135. 'createtime' => time(),
  136. 'updatetime' => time(),
  137. 'table' => $table,
  138. 'table_id' => $order['id'],
  139. ];
  140. $refund_log_id = Db::name('order_refund_log')->insertGetId($refund_data);
  141. if(!$refund_log_id){
  142. return '退款失败';
  143. }
  144. if ($order['pay_type'] == 'wechat') {
  145. // 微信|支付宝退款
  146. // 退款数据
  147. $order_data = [
  148. 'out_trade_no' => $order['pay_out_trade_no']
  149. ];
  150. if ($order['pay_type'] == 'wechat') {
  151. //$order['pay_fee'] = 0.01;//测试支付强制0.01元
  152. $total_fee = $order['pay_fee'] * 100;
  153. $refund_fee = $refund_price * 100;
  154. $order_data = array_merge($order_data, [
  155. 'out_refund_no' => $refund_data['out_refund_no'],
  156. 'total_fee' => $total_fee,
  157. 'refund_fee' => $refund_fee,
  158. 'refund_desc' => $remark,
  159. ]);
  160. }
  161. //
  162. if ($order['pay_type'] == 'wechat') {
  163. $wxpay = new \app\common\library\Wxpay;
  164. $result = $wxpay->WxPayRefund($order_data);
  165. // 微信通知回调 pay->notifyr
  166. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  167. Db::name('order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  168. return true;
  169. } else {
  170. return $result['return_msg'].'-'.$result['err_code_des'];
  171. }
  172. }
  173. // { // 微信返回结果
  174. // "return_code":"SUCCESS",
  175. // "return_msg":"OK",
  176. // "appid":"wx39cd0799d4567dd0",
  177. // "mch_id":"1481069012",
  178. // "nonce_str":"huW9eIAb5BDPn0Ma",
  179. // "sign":"250316740B263FE53F5DFF50AF5A8FA1",
  180. // "result_code":"SUCCESS",
  181. // "transaction_id":"4200000497202004072822298902",
  182. // "out_trade_no":"202010300857029180027000",
  183. // "out_refund_no":"1586241595",
  184. // "refund_id":"50300603862020040700031444448",
  185. // "refund_channel":[],
  186. // "refund_fee":"1",
  187. // "coupon_refund_fee":"0",
  188. // "total_fee":"1",
  189. // "cash_fee":"1",
  190. // "coupon_refund_count":"0",
  191. // "cash_refund_fee":"1
  192. // }
  193. // { // 支付宝返回结果
  194. // "code": "10000",
  195. // "msg": "Success",
  196. // "buyer_logon_id": "157***@163.com",
  197. // "buyer_user_id": "2088902485164146",
  198. // "fund_change": "Y",
  199. // "gmt_refund_pay": "2020-08-15 16:11:45",
  200. // "out_trade_no": "202002460317545607015300",
  201. // "refund_fee": "0.01",
  202. // "send_back_fee": "0.00",
  203. // "trade_no": "2020081522001464141438570535"
  204. // }
  205. }
  206. return true;
  207. }
  208. /**
  209. * 取消售后
  210. */
  211. public function refundcancel(){
  212. if($this->request->isAjax()){
  213. $id = input('id',0);
  214. Db::startTrans();
  215. $row = Db::name('order')->where('id',$id)->lock(true)->find();
  216. if($row['status'] != 21){
  217. Db::rollback();
  218. $this->error('订单状态有误,请勿重复提交');
  219. }
  220. //更新订单
  221. $update = [
  222. 'refund_remark' => '后台取消售后',
  223. 'status' => 1,
  224. ];
  225. $rs = Db::name('order')->where('id',$id)->update($update);
  226. if($rs === false){
  227. Db::rollback();
  228. $this->error('操作失败');
  229. }
  230. Db::commit();
  231. $this->success('取消售后完成');
  232. }
  233. }
  234. /**
  235. * 协议导出
  236. */
  237. public function xieyipdf(){
  238. $id = input('id');
  239. $info = Db::name('order')->where('id',$id)->find();
  240. $this->assign('info',$info);
  241. //
  242. $xieyi = Db::name('product_xieyi')->where('id',$info['xieyi_id'])->find();
  243. $this->assign('content',$xieyi['content']);
  244. //
  245. $this->view->engine->layout(false);
  246. return $this->view->fetch();
  247. }
  248. }