OrderController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Option;
  4. use App\Models\Order;
  5. use App\Models\OrderDevice;
  6. use App\Models\Project;
  7. use Illuminate\Http\Request;
  8. class OrderController extends BaseController
  9. {
  10. protected $model;
  11. protected $project;
  12. protected $model_name = '订单';
  13. protected $pre_uri = '/admin/Order/';
  14. protected $view_path = 'admin.orders.';
  15. protected $redirect_index = '/admin/Order/index';
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->model = new Order();
  20. $this->project = new Project();
  21. }
  22. public function index()
  23. {
  24. $project_options = $this->project->getOptions();
  25. $status_options = Option::get('orders', 'status');
  26. $type_options = [['name' => '外部租赁', 'id' => 1], ['name' => '内部调用', 'id' => 2]];
  27. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  28. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri', 'project_options', 'status_options', 'type_options'));
  29. }
  30. public function get(Request $request)
  31. {
  32. $items = $this->model->where('id', '>', 0)->with('project', 'user')->orderBy('created_at', 'desc');
  33. $tmp_items = collect(['order_number']);
  34. foreach($tmp_items as $tmp_item) {
  35. if($request->has($tmp_item) && !empty($request->input($tmp_item))) {
  36. $items = $items->where($tmp_item, 'like', '%' . $request->input($tmp_item) . '%');
  37. }
  38. }
  39. $select_items = collect(['status', 'type', 'project_id']);
  40. foreach($select_items as $select_item) {
  41. if($request->has($select_item) && !empty($request->input($select_item))) {
  42. $items = $items->where($select_item, '=', $request->input($select_item));
  43. }
  44. }
  45. if ($request->input('order_id'))
  46. {
  47. $items->where('id','like','%'.request('order_id').'%');
  48. }
  49. $items = $items->paginate();
  50. foreach($items as $item) {
  51. $item->order_id = $item->id ? $item->id : '';
  52. $item->project_name = $item->project ? $item->project->name : '';
  53. $item->work_point = $item->work_point_id ? $item->workpoints?($item->workpoints->name ? $item->workpoints->name : '') :'': '';
  54. // dd($item->workpoints);
  55. //方法区
  56. $order = OrderDevice::where('order_id',$item->order_id)->get()->toArray();
  57. $order_num = count($order);
  58. $total_price = 0;
  59. if ($item->type == 1)
  60. {
  61. foreach ($order as $value)
  62. {
  63. $one_price = ($value['quantity']*$value['price'])/100;
  64. $total_price += $one_price;
  65. }
  66. }
  67. $item->order_num = $item->id ? $order_num : '';
  68. $item->total_price = $item->id ? $total_price : '';
  69. $item->remark = $item->remark ? $item->remark : '';
  70. $item->user_name = $item->user ? $item->user->name : '';
  71. $item->status_name = $item->getStatusName();
  72. $item->option = Option::find($item->status);
  73. $color = $item->option ? $item->option->color : '';
  74. $item->status_name = '<span style="color: ' . $color . '">' . $item->status_name . '</span>';
  75. $item->type_name = $item->type == 1 ? '<span class="layui-badge layui-bg-green">外部租赁</span>' : '<span class="layui-badge layui-bg-blue">内部调用</span>';
  76. }
  77. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  78. }
  79. public function create()
  80. {
  81. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  82. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  83. }
  84. public function store(Request $request)
  85. {
  86. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  87. $validator = $this->model->getValidator($request, 'store');
  88. if($validator->fails()) {
  89. return back()->withErrors($validator)->withInput();
  90. }
  91. $data = $request->input('data');
  92. $res = $this->model->create($data);
  93. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  94. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  95. }
  96. public function edit(Request $request)
  97. {
  98. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  99. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  100. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  101. }
  102. public function update(Request $request)
  103. {
  104. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  105. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  106. $validator = $this->model->getValidator($request, 'update');
  107. if($validator->fails()) {
  108. return back()->withErrors($validator)->withInput();
  109. }
  110. $data = $request->input('data');
  111. $res = $this->model->where('id', $request->input('id'))->update($data);
  112. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  113. return back()->with(['sg_success_info' => '编辑成功']);
  114. }
  115. public function delete(Request $request)
  116. {
  117. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  118. $res = $item->delete();
  119. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  120. return response()->json(['status' => 'success', 'info' => '操作成功']);
  121. }
  122. }