Order.php 7.9 KB

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