Order.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 订单管理
  7. */
  8. namespace app\popupshop\controller;
  9. use think\facade\Request;
  10. use app\common\controller\Manage;
  11. use app\popupshop\model\Order as AppOrder;
  12. use app\popupshop\model\OrderCache;
  13. class Order extends Manage{
  14. public function initialize() {
  15. parent::initialize();
  16. $this->assign('pathMaps',[['name'=>'订单管理','url'=>url("popupshop/Order/index")]]);
  17. }
  18. /**
  19. * 订单库
  20. * @return void
  21. */
  22. protected function orderList(){
  23. $starttime = Request::param('starttime');
  24. $endtime = Request::param('endtime');
  25. if(!empty($starttime) && !empty($endtime)){
  26. $starttime = strtotime(date('Y-m-d 00:00:00',strtotime($starttime)));
  27. $endtime = strtotime(date('Y-m-d 23:59:59',strtotime($endtime)));
  28. if($starttime > $endtime){
  29. $this->error('开始日期不能大于结束日期');
  30. }
  31. if($endtime-$starttime >= 518401){
  32. $this->error('只支持连续6天的查找');
  33. }
  34. }
  35. $view['keyword'] = Request::param('keyword');
  36. $view['status'] = Request::param('status/d',0);
  37. $path[] = ['name'=>'订单管理','url'=>url("popupshop/Order/index")];
  38. $condition = [];
  39. if(!empty($starttime) && !empty($endtime)){
  40. $condition[] = ['order_starttime','>=',$starttime];
  41. $condition[] = ['order_starttime','<=',$endtime];
  42. }
  43. if(!empty($view['keyword'])){
  44. $condition = [['order_no','=',trim($view['keyword'])]];
  45. }
  46. $condition[] = ['member_miniapp_id', '=', $this->member_miniapp_id];
  47. $view['count'] = AppOrder::where($condition)->count();
  48. $view['payment'] = AppOrder::where($condition)->where(['paid_at' => 1, 'is_del' => 0])->sum('order_amount');
  49. $view['no_payment'] = AppOrder::where($condition)->where(['paid_at' => 0, 'is_del' => 0])->sum('order_amount');
  50. switch ($view['status']) {
  51. case 1:
  52. $condition[] = ['paid_at','=',0];
  53. $condition[] = ['is_del','=',0];
  54. $path[] = ['name'=>'未付款','url'=>'javascript:;'];
  55. break;
  56. case 2:
  57. $condition[] = ['paid_at','=',1];
  58. $condition[] = ['is_del','=',0];
  59. $path[] = ['name'=>'已付款','url'=>'javascript:;'];
  60. break;
  61. case 3:
  62. $condition[] = ['express_status','=',1];
  63. $condition[] = ['is_del','=',0];
  64. $condition[] = ['status','=',0];;
  65. $path[] = ['name'=>'已发货','url'=>'javascript:;'];
  66. break;
  67. case 4:
  68. $condition[] = ['is_del','=',1];
  69. $path[] = ['name'=>'回收站','url'=>'javascript:;'];
  70. break;
  71. case 5:
  72. $condition[] = ['status','=',1];
  73. $condition[] = ['is_del','=',0];;
  74. $path[] = ['name'=>'已结单','url'=>'javascript:;'];
  75. break;
  76. default:
  77. $condition[] = ['is_del','=',0];
  78. break;
  79. }
  80. $view['lists'] = AppOrder::where($condition)->order('id desc')->paginate(10);
  81. $view['order'] = AppOrder::order_data($view['lists']);
  82. $view['starttime'] = empty($starttime) ? time() : $starttime;
  83. $view['endtime'] = empty($endtime) ? time() : $endtime;;
  84. $view['pathMaps'] = $path;
  85. return $view;
  86. }
  87. /**
  88. * 订单列表
  89. */
  90. public function index(){
  91. return view()->assign(self::orderList());
  92. }
  93. /**
  94. * 导出Excel
  95. * @return void
  96. */
  97. public function excel(){
  98. header("Content-type: text/plain");
  99. header("Accept-Ranges: bytes");
  100. header("Content-type:application/vnd.ms-excel");
  101. header("Content-Disposition:attachment;filename=Order_".date('Y-m-d').".xls");
  102. header("Pragma: no-cache");
  103. header("Expires: 0");
  104. return view()->assign(self::orderList());
  105. }
  106. /**
  107. * 订单预览
  108. */
  109. public function view(){
  110. $order_no = Request::param('order_no');
  111. $view['order'] = AppOrder::order_data(AppOrder::getOrder($order_no,0,0));
  112. return view()->assign($view);
  113. }
  114. /**
  115. * 发货
  116. */
  117. public function sendgoods(){
  118. if(request()->isAjax()){
  119. $data = [
  120. 'order_no' => Request::param('order_no/s'),
  121. 'express_company' => Request::param('express_company/s'),
  122. 'express_no' => Request::param('express_no/s'),
  123. ];
  124. $validate = $this->validate($data,'Order.sendgoods');
  125. if(true !== $validate){
  126. return enjson(0,$validate);
  127. }
  128. //判断当前商品是否完成
  129. $result = AppOrder::where(['paid_at' => 1,'order_no' => $data['order_no']])->find();
  130. if(empty($result)){
  131. return json(['code'=>0,'msg'=>'商品没有满足发货条件']);
  132. }
  133. $data['express_company'] = $data['express_company'];
  134. $data['express_no'] = $data['express_no'];
  135. $data['express_status'] = 1;
  136. $data['express_starttime'] = time();
  137. $rel = AppOrder::where(['id' => $result->id])->update($data);
  138. if($rel){
  139. return enjson(200,'操作成功',['url' => url('popupshop/Order/index',['order_no' => $data['order_no']])]);
  140. }
  141. return enjson(0);
  142. }else{
  143. $order_no = Request::param('order_no/s');
  144. $view['order'] = AppOrder::order_data(AppOrder::getOrder($order_no,0,0));
  145. if(empty($view['order'])){
  146. $this->error("404 NOT FOUND");
  147. }
  148. $view['order_no'] = $order_no;
  149. return view()->assign($view);
  150. }
  151. }
  152. /**
  153. * 后台人工修改订单为已发货
  154. */
  155. public function paid(){
  156. $order_no = Request::param('order_no');
  157. $result = AppOrder::where(['paid_at' => 0,'order_no' => $order_no])->update(['paid_at' => 1,'payment_id' => 0,'order_starttime' => time(),'paid_no' => 'GM'.order_no()]);
  158. if($result){
  159. return enjson(200,'操作成功',['url' => url('popupshop/Order/index',['order_no' => $order_no,'status' => Request::param('status/d',0)])]);
  160. }
  161. return enjson(0);
  162. }
  163. /**
  164. * 删除
  165. */
  166. public function delete($order_no){
  167. $rel = AppOrder::where(['order_no' => $order_no])->find();
  168. if($rel){
  169. if($rel['is_del'] == 0){
  170. $result = AppOrder::update(['is_del'=>1],['order_no' => $order_no]);
  171. }else{
  172. $result = AppOrder::where(['order_no' => $order_no])->delete();
  173. if($result){
  174. OrderCache::where(['order_no' => $order_no])->delete();
  175. }
  176. }
  177. if($result){
  178. return enjson(200,'操作成功',['url' => url('popupshop/Order/index',['status' => Request::param('status/d',0)])]);
  179. }
  180. }
  181. return enjson(0);
  182. }
  183. /**
  184. * 清空回收站
  185. */
  186. public function alldelete(){
  187. $rel = AppOrder::where(['is_del'=>1])->select();
  188. if($rel){
  189. foreach ($rel as $value) {
  190. AppOrder::where(['order_no' => $value['order_no']])->delete();
  191. OrderCache::where(['order_no' => $value['order_no']])->delete();
  192. }
  193. return enjson(200,'操作成功',['url' => url('popupshop/Order/index',['status' => Request::param('status/d',0)])]);
  194. }
  195. return enjson(0);
  196. }
  197. /**
  198. * 确认收货
  199. */
  200. public function completion(){
  201. $order_no = Request::param('order_no');
  202. $result = AppOrder::update(['status'=>1],['express_status'=>1,'paid_at' => 1,'order_no' => $order_no]);
  203. if($result){
  204. return enjson(200,'操作成功',['url' => url('popupshop/Order/index',['order_no' => $order_no,'status' => Request::param('status/d',0)])]);
  205. }else{
  206. return enjson(0);
  207. }
  208. }
  209. /**
  210. * 订单完成
  211. */
  212. public function force_completion(){
  213. $order_no = Request::param('order_no');
  214. $result = AppOrder::update(['status'=>1],['order_no' => $order_no,'paid_at' => 0]);
  215. if($result){
  216. return enjson(200,'操作成功',['url' => url('popupshop/Order/index',['order_no' => $order_no,'status' => Request::param('status/d',0)])]);
  217. }else{
  218. return enjson(0);
  219. }
  220. }
  221. }