OrderOverviewController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Device;
  4. use App\Models\DeviceName;
  5. use App\Models\InnerDeviceNamesModel;
  6. use App\Models\Order;
  7. use App\Models\OrderDevice;
  8. use App\Models\OrderOverviewModel;
  9. use App\Models\Spec;
  10. use App\Models\User;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Log;
  13. class OrderOverviewController extends BaseController
  14. {
  15. protected $model;
  16. protected $department;
  17. protected $model_name = '账单明细列表';
  18. protected $pre_uri = '/admin/OrderOverview/';
  19. protected $view_path = 'admin.order-overviews.';
  20. protected $redirect_index = '/admin/OrderOverview/index';
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->model = new OrderOverviewModel();
  25. $this->order_device = new OrderDevice();
  26. }
  27. public function index()
  28. {
  29. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  30. return view($this->view_path . 'index', compact('model', 'model_name','pre_uri'));
  31. }
  32. public function orderDetails(Request $request)
  33. {
  34. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  35. return view($this->view_path . 'order-details', compact('model', 'model_name','pre_uri'));
  36. }
  37. public function get(Request $request)
  38. {
  39. if($request->input('type') == 'details') {
  40. $before_time = strtotime($request->input('date'));
  41. $now = strtotime('+1 month',$before_time);
  42. $now = date('Y-m-d H:i:s',$now);
  43. $order_arr = Order::where('project_id',$request->input('project_id'))
  44. ->where('status',3)
  45. ->where('type',1)
  46. ->where('updated_at','>=',$before_time)
  47. ->where('updated_at','<',$now)
  48. ->pluck('id')->toArray();
  49. $items = $this->order_device->where('project_id',$request->input('project_id'))->whereIn('order_id',$order_arr);
  50. }else{
  51. $items = $this->model->where('id', '>', 0)->orderBy('id','desc');
  52. }
  53. $items = $items->paginate(15);
  54. if ($request->input('type') == 'details')
  55. {
  56. foreach ($items as $item) {
  57. $device = $item->device ? $item->device->name : '';
  58. $device_name = $item->device_name ? $item->device_name->name : '';
  59. $spec_name = $item->spec ? $item->spec->name : '';
  60. $item->name = $device.'-'.$device_name.'-'.$spec_name;
  61. $user_id = Order::where('id',$item->order_id)->value('user_id');
  62. $item->user_name = User::where('id',$user_id)->value('name');
  63. $item->price = ($item->price*$item->quantity)/100;
  64. }
  65. }else
  66. {
  67. foreach ($items as $item) {
  68. $item->dates = date('Y-m',strtotime($item->date));
  69. $item->project_name = $item->projects ? $item->projects->name : '';
  70. $item->total_price = $item->total_price/100;
  71. $item->status = $item->status?'已确认':'未确认';
  72. $item->confirmation_user_id = $item->user ? $item->user->name : '';
  73. $item->phone = $item->user ? $item->user->phone : '';
  74. }
  75. }
  76. return response()->json(['code' => 0, 'message' => '', 'count' => $items->total(), 'data' => $items->items()]);
  77. }
  78. public function create()
  79. {
  80. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  81. return view($this->view_path . 'create', compact('model', 'model_name','pre_uri'));
  82. }
  83. public function store(Request $request)
  84. {
  85. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  86. $validator = $this->model->getValidator($request, 'store');
  87. if($validator->fails()) {
  88. return back()->withErrors($validator)->withInput();
  89. }
  90. $data = $request->input('data');
  91. $res = $this->model->create($data);
  92. if(empty($res)) return back()->withErrors(['sg_error_info' => '保存失败']);
  93. return redirect($this->pre_uri . 'create')->with(['sg_success_info' => '创建成功']);
  94. }
  95. public function edit(Request $request)
  96. {
  97. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  98. list($model, $model_name, $pre_uri) = array($this->model, $this->model_name, $this->pre_uri);
  99. return view($this->view_path . 'edit', compact('model', 'model_name', 'pre_uri', 'item'));
  100. }
  101. public function update(Request $request)
  102. {
  103. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return back()->withErrors(['sg_error_info' => '找不到要编辑的数据']);
  104. if(empty($request->input('data')) || !is_array($request->input('data'))) return back()->withErrors(['sg_error_info' => '数据错误']);
  105. $validator = $this->model->getValidator($request, 'update');
  106. if($validator->fails()) {
  107. return back()->withErrors($validator)->withInput();
  108. }
  109. $data = $request->input('data');
  110. $res = $this->model->where('id', $request->input('id'))->update($data);
  111. if(!$res) return back()->withErrors(['sg_error_info' => '数据库保存失败!']);
  112. return back()->with(['sg_success_info' => '编辑成功']);
  113. }
  114. public function delete(Request $request)
  115. {
  116. if(empty($request->input('id')) || empty($item = $this->model->find($request->input('id')))) return response()->json(['status' => 'fail', 'info' => '找不到要删除的数据']);
  117. DeviceName::where('device_id', $item->id)->delete();
  118. Spec::where('device_id', $item->id)->delete();
  119. $res = $item->delete();
  120. if (!$res) return response()->json(['status' => 'fail', 'info' => '删除失败']);
  121. return response()->json(['status' => 'success', 'info' => '操作成功']);
  122. }
  123. }