Order.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use app\common\service\UserService;
  5. use think\Db;
  6. use alipaysdkphpallmaster\aop\AopClient;
  7. use alipaysdkphpallmaster\aop\request\AlipayTradePayRequest;
  8. /**
  9. * 订单管理
  10. */
  11. class Order extends Apic
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = '*';
  15. private function status_text($status){
  16. $arr = [
  17. 1 => '待支付',
  18. 2 => '待处理',
  19. 3 => '已完成',
  20. 4 => '已取消',
  21. ];
  22. return isset($arr[$status]) ? $arr[$status] : '';
  23. }
  24. //
  25. public function lists(){
  26. $keyword = input('keyword','');
  27. $starttime = input('starttime',0);
  28. $endtime = input('endtime',0);
  29. $servicetype_id = intval(input('servicetype_id',0));
  30. $status = intval(input('status',0));
  31. $order = 'order.id desc';
  32. $where = [
  33. 'company_id' => $this->auth->company_id,
  34. ];
  35. //员工
  36. if($this->auth->type == 2){
  37. $where['staff_id'] = $this->auth->id;
  38. }//员工
  39. if($starttime || $endtime){
  40. $where['createtime'] = ['between',$starttime,$endtime];
  41. }
  42. if($servicetype_id){
  43. $where['servicetype_id'] = $servicetype_id;
  44. }
  45. if($status){
  46. $where['status'] = $status; //状态:2=待处理,3=已核销(完成),4=已取消
  47. if($status > 2){
  48. $order = 'order.finish_time desc';
  49. }
  50. }
  51. if(!empty($keyword))
  52. {
  53. $where['user_car_number|user_mobile'] = ['LIKE','%'.$keyword.'%'];
  54. }
  55. $list = Db::name('order')->alias('order')
  56. ->join('servicetype','order.servicetype_id = servicetype.id','LEFT')
  57. ->field('order.id,orderno,ordertype,user_name,user_car_number,createtime,servicetype_id,server_info,status,finish_time,cancel_reason,cancel_time,servicetype.title as servicetype_title,servicetype.baoyang_switch')->where($where)->order($order)->autopage()->select();
  58. foreach($list as $key => &$val){
  59. $val['status_text'] = $this->status_text($val['status']);
  60. }
  61. $this->success(1,$list);
  62. }
  63. //详情
  64. public function info(){
  65. $id = input('id',0);
  66. $info = Db::name('order')->alias('order')
  67. ->join('servicetype','order.servicetype_id = servicetype.id','LEFT')
  68. ->field('order.*,servicetype.title as servicetype_title,servicetype.baoyang_switch')
  69. ->where('order.id',$id)->find();
  70. $info = info_domain_image($info,['server_images']);
  71. $info['status_text'] = $this->status_text($info['status']);
  72. $info['total_fee'] = bcadd($info['pay_fee'],$info['appen_fee'],2);
  73. $info['appen_list'] = Db::name('order_appen')->where('order_id',$id)->select();
  74. $this->success(1,$info);
  75. }
  76. //取消
  77. public function cancel(){
  78. $id = input('id',0);
  79. $reason = input('reason','');
  80. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  81. if($info['status'] == 4){
  82. $this->error('当前订单已经取消');
  83. }
  84. if($info['status'] != 0 && $info['status'] != 2){
  85. $this->error('当前订单状态不能取消');
  86. }
  87. if($info['ordertype'] == 3){
  88. $this->error('套餐订单不能取消');
  89. }
  90. $time = time();
  91. //同步到预约单
  92. if (!empty($info['pre_order_id'])) {
  93. $preOrderWhere['id'] = $info['pre_order_id'];
  94. $preOrder = Db::name('pre_order')->where($preOrderWhere)->find();
  95. if (!empty($preOrder) && $preOrder['pre_order_status'] != 0) {
  96. $preOrderData = ['pre_order_status'=>0,'cancel_time'=>$time,'cancel_reason'=>$reason];
  97. $preOrderRes = Db::name('pre_order')->where($preOrderWhere)->update($preOrderData);
  98. if (!$preOrderRes) {
  99. $this->error('预约单取消失败');
  100. }
  101. }
  102. }
  103. //取消
  104. $rs = Db::name('order')->where('id',$id)->update(['status'=>4,'cancel_time'=>time(),'finish_time'=>time(),'cancel_reason'=>$reason]);
  105. if($rs === false){
  106. $this->error('取消失败');
  107. }
  108. //订单取消发送消息
  109. $userService = new UserService();
  110. $params['order_id'] = $id;
  111. $userService->msgOrder($params);
  112. $this->success('取消成功');
  113. }
  114. //完成
  115. public function finish(){
  116. $id = input('id',0);
  117. Db::startTrans();
  118. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  119. if($info['status'] != 2){
  120. Db::rollback();
  121. $this->error('当前订单不能完成');
  122. }
  123. //完成
  124. $rs = Db::name('order')->where('id',$id)->update(['status'=>3,'finish_time'=>time(),'staff_id'=>$this->auth->id]);
  125. if($rs === false){
  126. Db::rollback();
  127. $this->error('操作失败');
  128. }
  129. //给门店加钱
  130. if($info['ordertype'] == 3 && $info['paytype'] == 3 && $info['pay_fee'] > 0){
  131. $wallet_rs = model('walletcompany')->lockChangeAccountRemain($this->auth->company_id,'money',$info['pay_fee'],203,$remark='套餐订单完成服务','order',$id);
  132. if($wallet_rs['status'] === false){
  133. Db::rollback();
  134. $this->error($wallet_rs['msg']);
  135. }
  136. }
  137. Db::commit();
  138. //订单取消发送消息
  139. $userService = new UserService();
  140. $params['order_id'] = $id;
  141. $userService->msgOrder($params);
  142. //是否弹出保养
  143. $baoyang_switch = Db::name('servicetype')->where('id',$info['servicetype_id'])->value('baoyang_switch');
  144. $this->success('操作成功',$baoyang_switch);
  145. }
  146. //设置保养时间提醒
  147. public function baoyang(){
  148. $id = input('id',0);
  149. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  150. if(!$info){
  151. $this->error('不存在的订单');
  152. }
  153. //更新
  154. $next_date = input('next_date','');
  155. $next_carlicheng = input('next_carlicheng','');
  156. $data = [
  157. 'next_date' => $next_date,
  158. 'next_carlicheng' => $next_carlicheng,
  159. ];
  160. Db::name('order')->where('id',$id)->update($data);
  161. $this->success('操作成功');
  162. }
  163. //追加列表
  164. public function appen_lists(){
  165. $id = input('id',0);
  166. $info = Db::name('order')->field('id,orderno,pay_fee,appen_fee')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  167. if(!$info){
  168. $this->error('不存在的订单');
  169. }
  170. $info['appen_list'] = Db::name('order_appen')->where('order_id',$id)->select();
  171. $this->success(1,$info);
  172. }
  173. //追加新费用
  174. public function appen_newone(){
  175. $id = input('id',0);
  176. Db::startTrans();
  177. $info = Db::name('order')->field('id,orderno,pay_fee,appen_fee')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  178. if(!$info){
  179. $this->error('不存在的订单');
  180. }
  181. //加入新的一条
  182. $name = input('name','');
  183. $price = intval(input('price',0));
  184. if($price <= 0){
  185. Db::rollback();
  186. $this->error('请填写正确的金额');
  187. }
  188. $data = [
  189. 'order_id' => $id,
  190. 'name' => $name,
  191. 'price' => $price,
  192. 'createtime' => time(),
  193. ];
  194. $log_id = Db::name('order_appen')->insertGetId($data);
  195. if(!$log_id){
  196. Db::rollback();
  197. $this->error('操作失败');
  198. }
  199. //计算追加总额做冗余
  200. $sum_price = Db::name('order_appen')->where('order_id',$id)->sum('price');
  201. $update = [
  202. 'appen_fee'=>$sum_price,
  203. 'total_fee'=>bcadd($sum_price,$info['pay_fee'],2),
  204. ];
  205. $rs = Db::name('order')->where('id',$id)->update($update);
  206. if($rs === false){
  207. Db::rollback();
  208. $this->error('操作失败');
  209. }
  210. //结束
  211. Db::commit();
  212. $this->success();
  213. }
  214. }