DepreciationsController.php
Go to the documentation of this file.
1 <?php
2 namespace App\Http\Controllers;
3 
4 use Input;
5 use Lang;
7 use Redirect;
9 use DB;
10 use Str;
11 use View;
12 use Auth;
13 
21 {
31  public function getIndex()
32  {
33  // Show the page
34  return View::make('depreciations/index', compact('depreciations'));
35  }
36 
37 
46  public function getCreate()
47  {
48  // Show the page
49  $depreciation_options = \App\Helpers\Helper::depreciationList();
50  return View::make('depreciations/edit')->with('depreciation_options', $depreciation_options)->with('depreciation', new Depreciation);
51  }
52 
53 
62  public function postCreate()
63  {
64 
65  // get the POST data
66  $new = Input::all();
67 
68  // create a new instance
69  $depreciation = new Depreciation();
70 
71  // Depreciation data
72  $depreciation->name = e(Input::get('name'));
73  $depreciation->months = e(Input::get('months'));
74  $depreciation->user_id = Auth::user()->id;
75 
76  // Was the asset created?
77  if ($depreciation->save()) {
78  // Redirect to the new depreciation page
79  return Redirect::to("admin/settings/depreciations")->with('success', trans('admin/depreciations/message.create.success'));
80  }
81 
82  return Redirect::back()->withInput()->withErrors($depreciation->getErrors());
83 
84  }
85 
95  public function getEdit($depreciationId = null)
96  {
97  // Check if the depreciation exists
98  if (is_null($depreciation = Depreciation::find($depreciationId))) {
99  // Redirect to the blogs management page
100  return Redirect::to('admin/settings/depreciations')->with('error', trans('admin/depreciations/message.does_not_exist'));
101  }
102 
103  // Show the page
104  //$depreciation_options = array('' => 'Top Level') + Depreciation::lists('name', 'id');
105 
106  $depreciation_options = array('' => 'Top Level') + DB::table('depreciations')->where('id', '!=', $depreciationId)->lists('name', 'id');
107  return View::make('depreciations/edit', compact('depreciation'))->with('depreciation_options', $depreciation_options);
108  }
109 
110 
120  public function postEdit($depreciationId = null)
121  {
122  // Check if the depreciation exists
123  if (is_null($depreciation = Depreciation::find($depreciationId))) {
124  // Redirect to the blogs management page
125  return Redirect::to('admin/settings/depreciations')->with('error', trans('admin/depreciations/message.does_not_exist'));
126  }
127 
128  // Depreciation data
129  $depreciation->name = e(Input::get('name'));
130  $depreciation->months = e(Input::get('months'));
131 
132  // Was the asset created?
133  if ($depreciation->save()) {
134  // Redirect to the depreciation page
135  return Redirect::to("admin/settings/depreciations/")->with('success', trans('admin/depreciations/message.update.success'));
136  }
137 
138  return Redirect::back()->withInput()->withErrors($depreciation->getErrors());
139 
140 
141  }
142 
152  public function getDelete($depreciationId)
153  {
154  // Check if the depreciation exists
155  if (is_null($depreciation = Depreciation::find($depreciationId))) {
156  // Redirect to the blogs management page
157  return Redirect::to('admin/settings/depreciations')->with('error', trans('admin/depreciations/message.not_found'));
158  }
159 
160  if ($depreciation->has_models() > 0) {
161 
162  // Redirect to the asset management page
163  return Redirect::to('admin/settings/depreciations')->with('error', trans('admin/depreciations/message.assoc_users'));
164  } else {
165 
166  $depreciation->delete();
167 
168  // Redirect to the depreciations management page
169  return Redirect::to('admin/settings/depreciations')->with('success', trans('admin/depreciations/message.delete.success'));
170  }
171 
172  }
173 
174 
184  public function getDatatable()
185  {
186  $depreciations = Depreciation::select(array('id','name','months'));
187 
188  if (Input::has('search')) {
189  $depreciations = $depreciations->TextSearch(e(Input::get('search')));
190  }
191 
192  if (Input::has('offset')) {
193  $offset = e(Input::get('offset'));
194  } else {
195  $offset = 0;
196  }
197 
198  if (Input::has('limit')) {
199  $limit = e(Input::get('limit'));
200  } else {
201  $limit = 50;
202  }
203 
204  $allowed_columns = ['id','name','months'];
205  $order = Input::get('order') === 'asc' ? 'asc' : 'desc';
206  $sort = in_array(Input::get('sort'), $allowed_columns) ? Input::get('sort') : 'created_at';
207 
208  $depreciations->orderBy($sort, $order);
209 
210  $depreciationsCount = $depreciations->count();
211  $depreciations = $depreciations->skip($offset)->take($limit)->get();
212 
213  $rows = array();
214 
215  foreach ($depreciations as $depreciation) {
216  $actions = '<a href="'.route('update/depreciations', $depreciation->id).'" class="btn btn-warning btn-sm" style="margin-right:5px;"><i class="fa fa-pencil icon-white"></i></a><a data-html="false" class="btn delete-asset btn-danger btn-sm" data-toggle="modal" href="'.route('delete/depreciations', $depreciation->id).'" data-content="'.trans('admin/depreciations/message.delete.confirm').'" data-title="'.trans('general.delete').' '.htmlspecialchars($depreciation->name).'?" onClick="return false;"><i class="fa fa-trash icon-white"></i></a>';
217 
218  $rows[] = array(
219  'id' => $depreciation->id,
220  'name' => e($depreciation->name),
221  'months' => e($depreciation->months),
222  'actions' => $actions
223  );
224  }
225 
226  $data = array('total' => $depreciationsCount, 'rows' => $rows);
227 
228  return $data;
229 
230  }
231 }
getDatatable()
Generates the JSON used to display the depreciation listing.
postCreate()
Validates and stores the new depreciation data.
getDelete($depreciationId)
Validates and deletes a selected depreciation.
This controller handles all actions related to Depreciations for the Snipe-IT Asset Management applic...
getIndex()
Returns a view that invokes the ajax tables which actually contains the content for the depreciation ...
static depreciationList()
Definition: Helper.php:125
getCreate()
Returns a view that displays a form to create a new depreciation.
postEdit($depreciationId=null)
Validates and stores the updated depreciation data.
getEdit($depreciationId=null)
Returns a view that displays a form to update a depreciation.