DinnerRefuseOrder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace app\admin\controller;
  3. use think\App;
  4. use think\Db;
  5. use think\Exception;
  6. class DinnerRefuseOrder extends Auth
  7. {
  8. public function __construct(App $app = null) {
  9. parent::__construct($app);
  10. $this->table='dinner_order_refuse';
  11. }
  12. public function index(){
  13. if(request()->isAjax()){
  14. //分页参数
  15. $length = input('rows',10,'intval'); //每页条数
  16. $page = input('page',1,'intval'); //第几页
  17. $start = ($page - 1) * $length; //分页开始位置
  18. //排序
  19. $sortRow = input('sidx','id','trim'); //排序列
  20. $sort = input('sord','desc','trim'); //排序方式
  21. $order = $sortRow.' '.$sort;
  22. $title = input('sn','','trim');
  23. if($title){
  24. $map[] = ['b.sn','like','%'.$title.'%'];
  25. }
  26. $map[] = ['a.org_id','=',$this->orgId];
  27. $map[] = ['b.state','<>',0];
  28. $map= empty($map) ? true: $map;
  29. //数据查询
  30. $lists = db($this->table)
  31. ->alias('a')
  32. ->join('dinner_order b','a.dinner_order_id=b.id')
  33. ->field('b.id,b.sn,b.price,b.name,b.mobile,b.address,b.remark,b.pay_time,a.create_time,a.op_time,a.status,a.id as refuse_id')
  34. ->where($map)->limit($start,$length)->order(
  35. ['a.id'=>'desc']
  36. )->select();
  37. foreach ($lists as $k=>$v){
  38. $all = Db::name('dinner_order_item')
  39. ->alias('a')
  40. ->join('dinner b','a.dinner_id=b.id')
  41. ->join('dinner_type c','a.dinner_type_id=c.id')
  42. ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name')
  43. ->where('a.order_id',$v['id'])
  44. ->select();
  45. $item = '';
  46. foreach ($all as $k2=>$v2){
  47. $ss = $k2+1;
  48. if($ss<10){
  49. $ss= '0'.$ss;
  50. }
  51. $ur = url('dinner/detail').'?id='.$v2['dinner_id'];
  52. $item.=''.$ss.',<span class="label" style="border-radius:0rem !important;background-color: '.$v2['color'].';color: white">'.$v2['dinner_type_name'].'</span><a data-title="详情" href="javascript:;" url="'.$ur.'">'.$v2['dinner_name'].'*'.$v2['num'];
  53. if(count($all)>1 && $k2<count($all)){
  54. $item.='<br/>';
  55. }
  56. }
  57. $lists[$k]['item'] = $item;
  58. $refuse = Db::name('dinner_order_refuse_item')
  59. ->alias('a')
  60. ->join('dinner b','a.dinner_id=b.id')
  61. ->join('dinner_type c','a.dinner_type_id=c.id')
  62. ->field('a.*,b.name as dinner_name,c.color,c.name as dinner_type_name')
  63. ->where('a.order_refuse_id',$v['refuse_id'])
  64. ->select();
  65. $item_refuse = '';
  66. foreach ($refuse as $k3=>$v3){
  67. $ss1 = $k3+1;
  68. if($ss1<10){
  69. $ss1= '0'.$ss1;
  70. }
  71. $ur = url('dinner/detail').'?id='.$v3['dinner_id'];
  72. $item_refuse.=''.$ss1.',<span class="label" style="border-radius:0rem !important;background-color: '.$v3['color'].';color: white">'.$v3['dinner_type_name'].'</span><a onclick="layer_open(this,0)" data-title="详情" href="javascript:;" url="'.$ur.'">'.$v3['dinner_name'].'*'.$v3['num'];
  73. if(count($refuse)>1 && $k3<count($refuse)){
  74. $item_refuse.='<br/>';
  75. }
  76. }
  77. $lists[$k]['item_refuse'] = $item_refuse;
  78. $status_txt = '';
  79. if($v['status']==4){
  80. $status_txt ='<span class="label" style="border-radius:0rem !important;background-color: #d15b47;color: white">正在退款中</span><br/>'.$v['sn'].'';
  81. }elseif ($v['status']==5){
  82. $status_txt ='<span class="label" style="border-radius:0rem !important;background-color: #82af6f;color: white">已退款</span><br/>'.$v['sn'].'';
  83. }elseif ($v['status']==6){
  84. $status_txt ='<span class="label" style="border-radius:0rem !important;background-color: #3a87ad;color: white">申请退款被拒</span><br/>'.$v['sn'].'';
  85. }
  86. $lists[$k]['status_txt'] = $status_txt;
  87. $lists[$k]['op_time'] = $v['op_time']?$v['op_time']:'';
  88. }
  89. //数据返回
  90. $totalCount = db($this->table)
  91. ->alias('a')
  92. ->join('dinner_order b','a.dinner_order_id=b.id')
  93. ->where($map)->count();
  94. $totalPage = ceil($totalCount/$length);
  95. $result['page'] = $page;
  96. $result['total'] = $totalPage;
  97. $result['records'] = $totalCount;
  98. $result['rows'] = $lists;
  99. return json($result);
  100. }else{
  101. return $this->fetch();
  102. }
  103. }
  104. public function option($id=0){
  105. if(request()->isPost()){
  106. $res = Db::name($this->table)
  107. ->where('org_id',$this->orgId)
  108. ->where('id',$id)
  109. ->find();
  110. if($res['status']!==4){
  111. $this->error('当前订单已操作');
  112. }
  113. $order = Db::name('dinner_order')->where('id',$res['dinner_order_id'])->find();
  114. if(!$order){
  115. $this->error('订单不存在');
  116. }
  117. $status = input('status','');
  118. $remark = input('refuse_remark','');
  119. if(!in_array($status,[5,6])){
  120. $this->error('参数错误');
  121. }
  122. Db::startTrans();
  123. try{
  124. if($order['cate'] == 0){
  125. $r = Db::name('dinner_order')
  126. ->where('id',$res['dinner_order_id'])
  127. ->update([
  128. 'state'=>$status,
  129. 'update_time'=>getTime()
  130. ]);
  131. if(!$r){
  132. \exception('操作失败');
  133. }
  134. }else{
  135. if($status==5){
  136. $checkAll = Db::name('dinner_order_item')
  137. ->where('order_id',$res['dinner_order_id'])
  138. ->where('is_refuse',0)
  139. ->find();
  140. if(!$checkAll){//已退款
  141. Db::name('dinner_order')
  142. ->where('id',$res['dinner_order_id'])
  143. ->update([
  144. 'state'=>5,
  145. 'update_time'=>getTime()
  146. ]);
  147. }
  148. }
  149. }
  150. $r = Db::name($this->table)
  151. ->where('id',$id)
  152. ->update([
  153. 'status'=>$status,
  154. 'op_user_id'=>$this->userId,
  155. 'op_time'=>getTime(),
  156. 'refuse_remark'=>$remark
  157. ]);
  158. if(!$r){
  159. \exception('操作失败');
  160. }
  161. if($status==5&&$res['order_type']==1&&$res['is_replace']==0) {
  162. $checkAll = Db::name('dinner_order_item')
  163. ->where('order_id',$res['dinner_order_id'])
  164. ->where('is_refuse',0)
  165. ->find();
  166. if($checkAll){//部分退款
  167. Db::name('dinner_order')
  168. ->where('id',$res['dinner_order_id'])
  169. ->update([
  170. 'state'=>7,
  171. 'update_time'=>getTime()
  172. ]);
  173. }
  174. if($res['refuse_price'] > 0){
  175. $orderInfo = Db::name('dinner_order')
  176. ->where('id',$res['dinner_order_id'])
  177. ->find();
  178. $config =get_pay_wechat($orderInfo['org_id']);
  179. if(empty($config['mch_id']) || empty($config['key'])){
  180. \exception('微信商户信息未配置,请联系管理员');
  181. }
  182. $app = \EasyWeChat\Factory::payment($config);
  183. $totalFee = intval(round($orderInfo['price']*100));
  184. $refundFee = intval(round($res['refuse_price']*100));
  185. $ret = $app->refund->byOutTradeNumber($orderInfo['sn'], 'T'.$orderInfo['sn'], $totalFee, $refundFee);
  186. if($ret['return_code'] != 'SUCCESS' || $ret['result_code'] != 'SUCCESS'){
  187. trace(json_encode($ret),'error');
  188. \exception('退款申请提交失败');
  189. }
  190. }
  191. }
  192. if($status==6){ // 拒绝,返还
  193. $all = Db::name('dinner_order_refuse_item')->where('order_refuse_id',$id)->select();
  194. if($order['cate'] == 0){
  195. foreach ($all as $k=>$v){
  196. Db::name('dinner_order_item')
  197. ->where('dinner_id',$v['dinner_id'])
  198. ->where('dinner_type_id',$v['dinner_type_id'])
  199. ->where('is_refuse',1)
  200. ->setField('is_refuse',0);
  201. }
  202. }else{
  203. foreach ($all as $k=>$v){
  204. Db::name('dinner_order_item')
  205. ->where('dinner_id',$v['dinner_id'])
  206. ->where('dinner_type_id',$v['dinner_type_id'])
  207. ->where('order_id',$res['dinner_order_id'])
  208. ->where('day',$v['day'])
  209. ->where('is_refuse',1)
  210. ->update(['is_refuse'=>0]);
  211. }
  212. }
  213. }
  214. Db::commit();
  215. $this->success('操作成功');
  216. }catch (Exception $e){
  217. Db::rollback();
  218. $this->error($e->getMessage());
  219. }
  220. }else{
  221. $this->assign('id',$id);
  222. return $this->fetch();
  223. }
  224. }
  225. }