Order.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. ini_set('memory_limit','3000M');
  37. set_time_limit(0);
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->with(['product','usera','userb'])
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. foreach ($list as $row) {
  54. $row->getRelation('product')->visible(['name']);
  55. $row->getRelation('usera')->visible(['nickname','mobile']);
  56. $row->getRelation('userb')->visible(['nickname','mobile']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 退款
  65. */
  66. public function refund(){
  67. $id = input('id',0);
  68. $row = Db::name('order')->where('id',$id)->find();
  69. if($this->request->isPost()){
  70. if($row['status'] != 21){
  71. $this->error('订单状态有误,请勿重复提交');
  72. }
  73. $refund_reason = input('refund_reason','');
  74. $refund_remark = input('refund_remark','');
  75. $refund_fee = input('refund_fee',0);
  76. if($refund_fee > $row['pay_fee']){
  77. $this->error('退还金额不能大于实际支付金额'.$row['pay_fee']);
  78. }
  79. //更新订单
  80. $update = [
  81. 'refund_reason' => $refund_reason,
  82. 'refund_time' => time(),
  83. 'refund_remark' => $refund_remark,
  84. 'refund_fee' => $refund_fee,
  85. 'status' => 22,
  86. ];
  87. Db::startTrans();
  88. $rs = Db::name('order')->where('id',$id)->update($update);
  89. if($rs === false){
  90. Db::rollback();
  91. $this->error('提交失败');
  92. }
  93. //车票还回去
  94. $order_id = $id;
  95. $order_road = Db::name('order_road')->where('order_id',$order_id)->lock(true)->select();
  96. foreach($order_road as $key => $road){
  97. $chufabanci = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->lock(true)->find();
  98. if(empty($chufabanci)){
  99. continue;
  100. }
  101. $update = [
  102. 'ticket_remain' => $chufabanci['ticket_remain'] + $road['ticket_number'],
  103. ];
  104. $rs_ticket = Db::name('product_chufabanci')->where('id',$road['chufabanci_id'])->update($update);
  105. if($rs_ticket === false){
  106. Db::rollback();
  107. $this->error('退回票失败');
  108. }
  109. }
  110. //
  111. if($refund_fee > 0){
  112. //退款
  113. $refund_result = $this->old_refund($row, 'order',$refund_fee,'订单申请退款');
  114. if($refund_result !== true){
  115. Db::rollback();
  116. $this->error($refund_result);
  117. }
  118. }
  119. Db::commit();
  120. $this->success('退款完成');
  121. }
  122. $this->view->assign("row", $row);
  123. return $this->view->fetch();
  124. }
  125. // 退款
  126. private function old_refund($order,$table, $refund_price,$remark = '')
  127. {
  128. $order['pay_type'] = 'wechat';
  129. // 生成退款单
  130. $refund_data = [
  131. 'order_id' => $order['id'],
  132. 'out_refund_no'=> createUniqueNo('R',$order['id']),
  133. 'pay_fee' => $order['pay_fee'],
  134. 'refund_price' => $refund_price,
  135. 'pay_type' => $order['pay_type'],
  136. 'status' => 0,
  137. 'createtime' => time(),
  138. 'updatetime' => time(),
  139. 'table' => $table,
  140. 'table_id' => $order['id'],
  141. ];
  142. $refund_log_id = Db::name('order_refund_log')->insertGetId($refund_data);
  143. if(!$refund_log_id){
  144. return '退款失败';
  145. }
  146. if ($order['pay_type'] == 'wechat') {
  147. // 微信|支付宝退款
  148. // 退款数据
  149. $order_data = [
  150. 'out_trade_no' => $order['pay_out_trade_no']
  151. ];
  152. if ($order['pay_type'] == 'wechat') {
  153. //$order['pay_fee'] = 0.01;//测试支付强制0.01元
  154. $total_fee = $order['pay_fee'] * 100;
  155. $refund_fee = $refund_price * 100;
  156. $order_data = array_merge($order_data, [
  157. 'out_refund_no' => $refund_data['out_refund_no'],
  158. 'total_fee' => $total_fee,
  159. 'refund_fee' => $refund_fee,
  160. 'refund_desc' => $remark,
  161. ]);
  162. }
  163. //
  164. if ($order['pay_type'] == 'wechat') {
  165. $wxpay = new \app\common\library\Wxpay;
  166. $result = $wxpay->WxPayRefund($order_data);
  167. // 微信通知回调 pay->notifyr
  168. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  169. Db::name('order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  170. return true;
  171. } else {
  172. return $result['return_msg'].'-'.$result['err_code_des'];
  173. }
  174. }
  175. // { // 微信返回结果
  176. // "return_code":"SUCCESS",
  177. // "return_msg":"OK",
  178. // "appid":"wx39cd0799d4567dd0",
  179. // "mch_id":"1481069012",
  180. // "nonce_str":"huW9eIAb5BDPn0Ma",
  181. // "sign":"250316740B263FE53F5DFF50AF5A8FA1",
  182. // "result_code":"SUCCESS",
  183. // "transaction_id":"4200000497202004072822298902",
  184. // "out_trade_no":"202010300857029180027000",
  185. // "out_refund_no":"1586241595",
  186. // "refund_id":"50300603862020040700031444448",
  187. // "refund_channel":[],
  188. // "refund_fee":"1",
  189. // "coupon_refund_fee":"0",
  190. // "total_fee":"1",
  191. // "cash_fee":"1",
  192. // "coupon_refund_count":"0",
  193. // "cash_refund_fee":"1
  194. // }
  195. // { // 支付宝返回结果
  196. // "code": "10000",
  197. // "msg": "Success",
  198. // "buyer_logon_id": "157***@163.com",
  199. // "buyer_user_id": "2088902485164146",
  200. // "fund_change": "Y",
  201. // "gmt_refund_pay": "2020-08-15 16:11:45",
  202. // "out_trade_no": "202002460317545607015300",
  203. // "refund_fee": "0.01",
  204. // "send_back_fee": "0.00",
  205. // "trade_no": "2020081522001464141438570535"
  206. // }
  207. }
  208. return true;
  209. }
  210. /**
  211. * 取消售后
  212. */
  213. public function refundcancel(){
  214. if($this->request->isAjax()){
  215. $id = input('id',0);
  216. Db::startTrans();
  217. $row = Db::name('order')->where('id',$id)->lock(true)->find();
  218. if($row['status'] != 21){
  219. Db::rollback();
  220. $this->error('订单状态有误,请勿重复提交');
  221. }
  222. //更新订单
  223. $update = [
  224. 'refund_remark' => '后台取消售后',
  225. 'status' => 1,
  226. ];
  227. $rs = Db::name('order')->where('id',$id)->update($update);
  228. if($rs === false){
  229. Db::rollback();
  230. $this->error('操作失败');
  231. }
  232. Db::commit();
  233. $this->success('取消售后完成');
  234. }
  235. }
  236. /**
  237. * 协议导出
  238. */
  239. public function xieyipdf(){
  240. $id = input('id');
  241. $info = Db::name('order')->where('id',$id)->find();
  242. $this->assign('info',$info);
  243. //
  244. $xieyi = Db::name('product_xieyi')->where('id',$info['xieyi_id'])->find();
  245. $this->assign('content',$xieyi['content']);
  246. //
  247. $this->view->engine->layout(false);
  248. return $this->view->fetch();
  249. }
  250. }