EpormasCounterController::create()   B
last analyzed

Complexity

Conditions 2
Paths 5

Size

Total Lines 27
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 25
nc 5
nop 0
1
<?php
2
3
namespace Bantenprov\DashboardEpormas\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Input;
9
use Bantenprov\DashboardEpormas\Models\EpormasCounter;
10
use Bantenprov\DashboardEpormas\Models\EpormasCategory;
11
use Bantenprov\DashboardEpormas\Models\EpormasCity;
12
use Validator, Image, Session, File, Response, Redirect, Exception;
13
14
class EpormasCounterController extends Controller
15
{
16
17
  /**
18
   * Display a listing of the resource.
19
   *
20
   * @return Response
21
   */
22
  public function index()
23
  {
24
      try {
25
          $error = false;
26
          $statusCode = 200;
27
          $title = 'Success';
28
          $type = 'success';
29
          $message = 'Success';
30
          $result = EpormasCounter::whereNull('deleted_at')
31
                          ->with('getCity')
32
                          ->with('getCategory')
33
                          ->get();
34
      } catch (Exception $e) {
35
          $error = true;
36
          $statusCode = 404;
37
          $title = 'Error';
38
          $type = 'error';
39
          $message = 'Error';
40
          $result = 'Not Found';
41
      } finally {
42
          return Response::json(array(
43
            'error' => $error,
44
            'status' => $statusCode,
45
            'title' => $title,
46
            'type' => $type,
47
            'message' => $message,
48
            'result' => $result
49
          ));
50
      }
51
  }
52
53
  /**
54
   * Show the form for creating a new resource.
55
   *
56
   * @return Response
57
   */
58
  public function create()
59
  {
60
      try {
61
          $category = EpormasCategory::all();
62
          $city = EpormasCity::all();
63
          $error = false;
64
          $statusCode = 200;
65
          $title = 'Success';
66
          $type = 'success';
67
          $message = 'Success';
68
      } catch (Exception $e) {
69
          $category = 'Not Found';
70
          $city = 'Not Found';
71
          $error = true;
72
          $statusCode = 404;
73
          $title = 'Error';
74
          $type = 'error';
75
          $message = 'Error';
76
      } finally {
77
          return Response::json(array(
78
            'error' => $error,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $error does not seem to be defined for all execution paths leading up to this point.
Loading history...
79
            'status' => $statusCode,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $statusCode does not seem to be defined for all execution paths leading up to this point.
Loading history...
80
            'title' => $title,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
81
            'type' => $type,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $type does not seem to be defined for all execution paths leading up to this point.
Loading history...
82
            'category' => $category,
83
            'city' => $city,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $city does not seem to be defined for all execution paths leading up to this point.
Loading history...
84
            'message' => $message
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.
Loading history...
85
          ));
86
      }
87
  }
88
89
  /**
90
   * Store a newly created resource in storage.
91
   *
92
   * @return Response
93
   */
94
  public function store(Request $request, $version='')
95
  {
96
        $path = \Request::path();
97
        $explode = explode('/', $path);
98
99
        $from = 'form';
100
        if(in_array('api',$explode)){
101
          $from = 'api';
102
        }
103
104
        $via = $from;
105
        if($version != '' && $version != 'store'){
106
          $via .= '-'.$version;
107
        }
108
109
      	$rules = [
110
            'count' => 'required|numeric',
111
            'city_id' => 'required|numeric',
112
            'category_id' => 'required|numeric',
113
            'tanggal' => 'required|date',
114
        ];
115
116
        $validator = Validator::make($request->all(), $rules);
117
        if ($validator->fails()) {
118
          return Response::json(array(
119
              'title' => 'Error',
120
              'type'  => 'error',
121
              'message' => $validator->errors()->all()
122
          ));
123
        }
124
125
        $format = date('Y-m-d', strtotime(str_replace(' ','-',$request->tanggal)));
126
        $resultcek = EpormasCounter::whereNull('deleted_at')
127
                             ->where('tanggal','like','%'.$format.'%')
128
                             ->where('category_id',$request->category_id)
129
                             ->where('city_id',$request->city_id)
130
                             ->groupBy('tahun','bulan','category_id','city_id')
131
                             ->orderBy('bulan')
132
                             ->count();
133
        if($resultcek > 0){
134
          return Response::json(array(
135
              'title' => 'Error',
136
              'type'  => 'error',
137
              'message' => 'Data has already been taken.'
138
          ));
139
        }
140
141
        $date = explode("-",$format);
142
        $data = EpormasCounter::whereNull('deleted_at')
143
                       ->where('tahun', $date[0])
144
                       ->where('bulan', $date[1])
145
                       ->where('category_id',$request->category_id)
146
                       ->where('city_id',$request->city_id)
147
                       ->groupBy('tahun','bulan','category_id','city_id')
148
                       ->orderBy('bulan')
149
                       ->count();
150
        if($data > 0){
151
          return Response::json(array(
152
              'title' => 'Error',
153
              'type'  => 'error',
154
              'message' => 'Data has already been taken.'
155
          ));
156
        }
157
158
        $error = false;
159
        $statusCode = 200;
160
        $title = 'Success';
161
        $type = 'success';
162
        $message = 'Data created successfully';
163
        $result = EpormasCounter::create([
164
            'count' => $request->count,
165
            'category_id' => $request->category_id,
166
            'city_id' => $request->city_id,
167
            'tahun' => $date[0],
168
            'bulan' => $date[1],
169
            'tanggal' => $format,
170
            'user_id' => $request->user_id,
171
            'via' => $via
172
        ]);
173
        return Response::json(array(
174
          'error' => $error,
175
          'status' => $statusCode,
176
          'title' => $title,
177
          'type' => $type,
178
          'message' => $message,
179
          'result' => $result
180
        ));
181
  }
182
183
  /**
184
   * Display the specified resource.
185
   *
186
   * @param  int  $id
187
   * @return Response
188
   */
189
  public function show($version='', $id)
190
  {
191
        try {
192
            $error = false;
193
            $statusCode = 200;
194
            $title = 'Success';
195
            $type = 'success';
196
            $message = 'Success';
197
            $result = EpormasCounter::whereNull('deleted_at')
198
                            ->with('getCity')
199
                            ->with('getCategory')
200
                            ->find($id);
201
        } catch (Exception $e) {
202
            $error = true;
203
            $statusCode = 404;
204
            $title = 'Error';
205
            $type = 'error';
206
            $message = 'Error';
207
            $result = 'Not Found';
208
        } finally {
209
            return Response::json(array(
210
              'error' => $error,
211
              'status' => $statusCode,
212
              'title' => $title,
213
              'type' => $type,
214
              'message' => $message,
215
              'result' => $result
216
            ));
217
        }
218
  }
219
220
  /**
221
   * Show the form for editing the specified resource.
222
   *
223
   * @param  int  $id
224
   * @return Response
225
   */
226
  public function edit($id)
227
  {
228
        try {
229
            $category = EpormasCategory::all();
230
            $city = EpormasCity::all();
231
            $error = false;
232
            $statusCode = 200;
233
            $title = 'Success';
234
            $type = 'success';
235
            $message = 'Success';
236
            $result = EpormasCounter::whereNull('deleted_at')
237
                            ->with('getCity')
238
                            ->with('getCategory')
239
                            ->find($id);
240
            if($result->tanggal){
241
              $format = date('Y-m-d', strtotime($result->tanggal));
242
            }else {
243
              $format = 'Not Found';
244
            }
245
246
        } catch (Exception $e) {
247
            $category = 'Not Found';
248
            $city = 'Not Found';
249
            $error = true;
250
            $statusCode = 404;
251
            $title = 'Error';
252
            $type = 'error';
253
            $message = 'Error';
254
            $result = 'Not Found';
255
            $format = 'Not Found';
256
        } finally {
257
            return Response::json(array(
258
              'error' => $error,
259
              'status' => $statusCode,
260
              'title' => $title,
261
              'type' => $type,
262
              'message' => $message,
263
              'city' => $city,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $city does not seem to be defined for all execution paths leading up to this point.
Loading history...
264
              'category' => $category,
265
              'tanggal' => $format,
266
              'result' => $result
267
            ));
268
        }
269
  }
270
271
  /**
272
   * Update the specified resource in storage.
273
   *
274
   * @param  int  $id
275
   * @return Response
276
   */
277
  public function update(Request $request, $version='', $id)
278
  {
279
        $result = EpormasCounter::whereNull('deleted_at')
280
                        ->with('getCity')
281
                        ->with('getCategory')
282
                        ->find($id);
283
284
        $rules = [
285
            'count' => 'required|numeric',
286
            'city_id' => 'required|numeric',
287
            'category_id' => 'required|numeric',
288
            'tanggal' => 'required|date',
289
        ];
290
291
        $validator = Validator::make($request->all(), $rules);
292
        if ($validator->fails()) {
293
          return Response::json(array(
294
              'title' => 'Error',
295
              'type'  => 'error',
296
              'message' => $validator->errors()->all()
297
          ));
298
        }
299
300
        $format = date('Y-m-d', strtotime(str_replace(' ','-',$request->tanggal)));
301
        if($result->city_id != $request->city_id || $result->category_id != $request->category_id){
302
            $resultcek = EpormasCounter::whereNull('deleted_at')
303
                                 ->where('tanggal','like','%'.$format.'%')
304
            		                 ->where('category_id',$request->category_id)
305
            		                 ->where('city_id',$request->city_id)
306
                                 ->groupBy('tahun','bulan','category_id','city_id')
307
                                 ->orderBy('bulan')
308
                                 ->count();
309
            if($resultcek > 0){
310
              return Response::json(array(
311
                  'title' => 'Error',
312
                  'type'  => 'error',
313
                  'message' => 'Data has already been taken.'
314
              ));
315
            }
316
        }
317
318
        $date = explode("-",$format);
319
        $dates = date('Y-m-d', strtotime($result->tanggal));
320
        if($dates != $format){
321
            $resultcek = EpormasCounter::whereNull('deleted_at')
322
            		                 ->where('category_id',$request->category_id)
323
            		                 ->where('city_id',$request->city_id)
324
                                 ->where('tanggal','like','%'.$format.'%')
325
                                 ->groupBy('tahun','bulan','category_id','city_id')
326
                                 ->orderBy('bulan')
327
                                 ->count();
328
            if($resultcek > 0){
329
              return Response::json(array(
330
                  'title' => 'Error',
331
                  'type'  => 'error',
332
                  'message' => 'Data has already been taken.'
333
              ));
334
            }
335
336
            $data = EpormasCounter::whereNull('deleted_at')
337
                           ->where('tahun', $date[0])
338
                           ->where('bulan', $date[1])
339
            		           ->where('category_id',$request->category_id)
340
            		           ->where('city_id',$request->city_id)
341
                           ->groupBy('tahun','bulan','category_id','city_id')
342
                           ->orderBy('bulan')
343
                           ->count();
344
            if($data > 0){
345
              return Response::json(array(
346
                  'title' => 'Error',
347
                  'type'  => 'error',
348
                  'message' => 'Data has already been taken.'
349
              ));
350
            }
351
        }
352
353
        $path = \Request::path();
354
        $explode = explode('/', $path);
355
356
        $from = $result->via;
357
        $user_id = $result->user_id;
358
        if(in_array('api',$explode)){
359
          $from = 'api';
360
          $user_id = $request->user_id;
361
        }
362
363
        $via = $from;
364
        if($version != '' && $version != 'update'){
365
          $via .= '-'.$version;
366
        }
367
368
        $error = false;
369
        $statusCode = 200;
370
        $title = 'Success';
371
        $type = 'success';
372
        $message = 'Data updated successfully';
373
        $result->update([
374
            'count' => $request->count,
375
            'category_id' => $request->category_id,
376
            'city_id' => $request->city_id,
377
            'tahun' => $date[0],
378
            'bulan' => $date[1],
379
            'tanggal' => $format,
380
            'user_id' => $user_id,
381
            'via' => $via
382
        ]);
383
        return Response::json(array(
384
          'error' => $error,
385
          'status' => $statusCode,
386
          'title' => $title,
387
          'type' => $type,
388
          'message' => $message,
389
          'result' => $result
390
        ));
391
  }
392
393
  /**
394
   * Remove the specified resource from storage.
395
   *
396
   * @param  int  $id
397
   * @return Response
398
   */
399
  public function destroy($id)
400
  {
401
      EpormasCounter::find($id)->delete();
402
      $error = false;
403
      $statusCode = 200;
404
      $title = 'Success';
405
      $type = 'success';
406
      $message = 'Data deleted successfully';
407
      return Response::json(array(
408
        'error' => $error,
409
        'status' => $statusCode,
410
        'title' => $title,
411
        'type' => $type,
412
        'message' => $message
413
      ));
414
  }
415
416
}
417