Order.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\common\service\UserService;
  6. /**
  7. * 用户订单
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Order extends Backend
  12. {
  13. /**
  14. * Order模型对象
  15. * @var \app\admin\model\Order
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Order;
  22. $this->view->assign("ordertypeList", $this->model->getOrdertypeList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("paytypeList", $this->model->getPaytypeList());
  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. //只能看自己的
  47. $where_op = [];
  48. if($this->auth->company_id){
  49. $where_op['order.company_id'] = $this->auth->company_id;
  50. }
  51. $list = $this->model
  52. ->with(['company','staff','user','preorder','servicetype'])
  53. ->where($where)
  54. ->where($where_op)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. foreach ($list as $row) {
  58. $row->getRelation('company')->visible(['name']);
  59. $row->getRelation('staff')->visible(['truename','mobile']);
  60. $row->getRelation('user')->visible(['nickname','mobile']);
  61. $row->getRelation('preorder')->visible(['pre_order_no']);
  62. $row->getRelation('servicetype')->visible(['title']);
  63. }
  64. $result = array("total" => $list->total(), "rows" => $list->items());
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 在线开单
  71. */
  72. public function submitordernew(){
  73. if(!$this->request->isPost()){
  74. $this->assign('staff_id',$this->auth->staff_id);
  75. return $this->view->fetch();
  76. }
  77. $data = request_post_hub([
  78. 'user_name','user_car_number','user_mobile','user_address',
  79. 'servicetype_id','server_time','server_info','server_images','pay_fee'
  80. ]);
  81. Db::startTrans();
  82. $data['pre_order_id'] = 0;
  83. //检查用户
  84. $user_info = Db::name('user')->where('mobile',$data['user_mobile'])->find();
  85. if(empty($user_info)){
  86. Db::rollback();
  87. $this->error('不存在的用户,请先让客户扫店铺码注册');
  88. }
  89. $data['user_id'] = $user_info['id'];
  90. //检索car_id,没必要了
  91. //准备数据
  92. $data['orderno'] = createUniqueNo('O',$user_info['id']);
  93. $data['ordertype'] = 2; //类型:1=预约下单,2=在线下单,3=套餐订单
  94. $data['company_id'] = $this->auth->company_id;
  95. $data['staff_id'] = $this->auth->staff_id;
  96. $data['total_fee'] = $data['pay_fee'];
  97. $data['status'] = 2; //2=已支付,待处理
  98. $data['createtime'] = time();
  99. $order_id = Db::name('order')->insertGetId($data);
  100. if(!$order_id){
  101. Db::rollback();
  102. $this->error('下单失败');
  103. }
  104. Db::commit();
  105. $this->success('下单完成',$order_id);
  106. }
  107. /**
  108. * 取消
  109. */
  110. public function cancel(){
  111. $id = input('id',0);
  112. if(!$this->request->ispost()){
  113. $this->assign('id',$id);
  114. return $this->view->fetch();
  115. }
  116. $reason = input('cancel_reason','');
  117. $info = Db::name('order')->where('id',$id)->find();
  118. if($info['status'] == 4){
  119. $this->error('当前订单已经取消');
  120. }
  121. if($info['status'] != 2){
  122. $this->error('当前订单状态不能取消');
  123. }
  124. if($info['ordertype'] == 3){
  125. $this->error('套餐订单不能取消');
  126. }
  127. $time = time();
  128. //同步到预约单,这一步不该有
  129. if (!empty($info['pre_order_id'])) {
  130. $preOrderWhere['id'] = $info['pre_order_id'];
  131. $preOrder = Db::name('pre_order')->where($preOrderWhere)->find();
  132. if (!empty($preOrder) && $preOrder['pre_order_status'] != 0) {
  133. $preOrderData = ['pre_order_status'=>0,'cancel_time'=>$time,'cancel_reason'=>$reason];
  134. $preOrderRes = Db::name('pre_order')->where($preOrderWhere)->update($preOrderData);
  135. if (!$preOrderRes) {
  136. $this->error('预约单取消失败');
  137. }
  138. }
  139. }
  140. //取消
  141. $rs = Db::name('order')->where('id',$id)->update(['status'=>4,'cancel_time'=>time(),'finish_time'=>time(),'cancel_reason'=>$reason]);
  142. if($rs === false){
  143. $this->error('取消失败');
  144. }
  145. //订单取消发送消息
  146. $userService = new UserService();
  147. $params['order_id'] = $id;
  148. $userService->msgOrder($params);
  149. $this->success('取消成功');
  150. }
  151. /**
  152. * 完成
  153. */
  154. public function finish(){
  155. $id = input('id',0);
  156. if(!$this->request->ispost()){
  157. $this->assign('id',$id);
  158. $info = Db::name('order')->where('id',$id)->find();
  159. $this->assign('info',$info);
  160. $this->assign('staff_id',$this->auth->staff_id);
  161. //是否弹出保养
  162. $baoyang_switch = Db::name('servicetype')->where('id',$info['servicetype_id'])->value('baoyang_switch');
  163. $this->assign('baoyang_switch',$baoyang_switch);
  164. return $this->view->fetch();
  165. }
  166. Db::startTrans();
  167. $info = Db::name('order')->where('id',$id)->lock(true)->find();
  168. if($info['status'] != 2){
  169. Db::rollback();
  170. $this->error('当前订单不能完成');
  171. }
  172. //保养更新
  173. $next_date = input('next_date','');
  174. $next_carlicheng = input('next_carlicheng','');
  175. $baoyang_data = [
  176. 'next_date' => $next_date,
  177. 'next_carlicheng' => $next_carlicheng,
  178. ];
  179. //完成
  180. $time = time();
  181. $updateData = ['status'=>3,'finish_time'=>time(),'staff_id'=>$this->auth->staff_id];
  182. $updateData = array_merge($updateData,$baoyang_data);
  183. $rs = Db::name('order')->where('id',$id)->update($updateData);
  184. if($rs === false){
  185. Db::rollback();
  186. $this->error('操作失败');
  187. }
  188. //给门店加钱
  189. if($info['ordertype'] == 3 && $info['paytype'] == 3 && $info['pay_fee'] > 0){
  190. $wallet_rs = model('walletcompany')->lockChangeAccountRemain($this->auth->company_id,'money',$info['pay_fee'],203,$remark='套餐订单完成服务','order',$id);
  191. if($wallet_rs['status'] === false){
  192. Db::rollback();
  193. $this->error($wallet_rs['msg']);
  194. }
  195. }
  196. Db::commit();
  197. //订单取消发送消息
  198. $userService = new UserService();
  199. $params['order_id'] = $id;
  200. $userService->msgOrder($params);
  201. $this->success('操作成功');
  202. }
  203. }