Order.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. //取消
  90. $rs = Db::name('order')->where('id',$id)->update(['status'=>4,'cancel_time'=>time(),'finish_time'=>time(),'cancel_reason'=>$reason]);
  91. if($rs === false){
  92. $this->error('取消失败');
  93. }
  94. $this->success('取消成功');
  95. }
  96. //完成
  97. public function finish(){
  98. $id = input('id',0);
  99. Db::startTrans();
  100. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  101. if($info['status'] != 2){
  102. Db::rollback();
  103. $this->error('当前订单不能完成');
  104. }
  105. //完成
  106. $rs = Db::name('order')->where('id',$id)->update(['status'=>3,'finish_time'=>time(),'staff_id'=>$this->auth->id]);
  107. if($rs === false){
  108. Db::rollback();
  109. $this->error('操作失败');
  110. }
  111. //给门店加钱
  112. if($info['ordertype'] == 3 && $info['paytype'] == 3 && $info['pay_fee'] > 0){
  113. $wallet_rs = model('walletcompany')->lockChangeAccountRemain($this->auth->company_id,'money',$info['pay_fee'],203,$remark='套餐订单完成服务','order',$id);
  114. if($wallet_rs['status'] === false){
  115. Db::rollback();
  116. $this->error($wallet_rs['msg']);
  117. }
  118. }
  119. Db::commit();
  120. //是否弹出保养
  121. $baoyang_switch = Db::name('servicetype')->where('id',$info['servicetype_id'])->value('baoyang_switch');
  122. $this->success('操作成功',$baoyang_switch);
  123. }
  124. //设置保养时间提醒
  125. public function baoyang(){
  126. $id = input('id',0);
  127. $info = Db::name('order')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  128. if(!$info){
  129. $this->error('不存在的订单');
  130. }
  131. //更新
  132. $next_date = input('next_date','');
  133. $next_carlicheng = input('next_carlicheng','');
  134. $data = [
  135. 'next_date' => $next_date,
  136. 'next_carlicheng' => $next_carlicheng,
  137. ];
  138. Db::name('order')->where('id',$id)->update($data);
  139. $this->success('操作成功');
  140. }
  141. //追加列表
  142. public function appen_lists(){
  143. $id = input('id',0);
  144. $info = Db::name('order')->field('id,orderno,pay_fee,appen_fee')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
  145. if(!$info){
  146. $this->error('不存在的订单');
  147. }
  148. $info['appen_list'] = Db::name('order_appen')->where('order_id',$id)->select();
  149. $this->success(1,$info);
  150. }
  151. //追加新费用
  152. public function appen_newone(){
  153. $id = input('id',0);
  154. Db::startTrans();
  155. $info = Db::name('order')->field('id,orderno,pay_fee,appen_fee')->where('id',$id)->where('company_id',$this->auth->company_id)->lock(true)->find();
  156. if(!$info){
  157. $this->error('不存在的订单');
  158. }
  159. //加入新的一条
  160. $name = input('name','');
  161. $price = intval(input('price',0));
  162. if($price <= 0){
  163. Db::rollback();
  164. $this->error('请填写正确的金额');
  165. }
  166. $data = [
  167. 'order_id' => $id,
  168. 'name' => $name,
  169. 'price' => $price,
  170. 'createtime' => time(),
  171. ];
  172. $log_id = Db::name('order_appen')->insertGetId($data);
  173. if(!$log_id){
  174. Db::rollback();
  175. $this->error('操作失败');
  176. }
  177. //计算追加总额做冗余
  178. $sum_price = Db::name('order_appen')->where('order_id',$id)->sum('price');
  179. $update = [
  180. 'appen_fee'=>$sum_price,
  181. 'total_fee'=>bcadd($sum_price,$info['pay_fee'],2),
  182. ];
  183. $rs = Db::name('order')->where('id',$id)->update($update);
  184. if($rs === false){
  185. Db::rollback();
  186. $this->error('操作失败');
  187. }
  188. //结束
  189. Db::commit();
  190. $this->success();
  191. }
  192. }