Order.php 7.2 KB

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