Completed
Push — development ( 3e307e...0dc251 )
by Ashutosh
11:44
created

TaxController::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 0
nc 1
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Payment;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Common\Country;
7
use App\Model\Common\State;
8
use App\Model\Payment\Tax;
9
use App\Model\Payment\TaxByState;
10
use App\Model\Payment\TaxClass;
11
use App\Model\Payment\TaxOption;
12
use Illuminate\Http\Request;
13
14
class TaxController extends Controller
15
{
16
    public $tax;
17
    public $country;
18
    public $state;
19
    public $tax_option;
20
    public $tax_class;
21
22
    public function __construct()
23
    {
24
        $this->middleware('auth', ['except' => 'getState']);
25
        $this->middleware('admin', ['except' => 'getState']);
26
27
        $tax = new Tax();
28
        $this->tax = $tax;
29
30
        $country = new Country();
31
        $this->country = $country;
32
33
        $state = new State();
34
        $this->state = $state;
35
36
        $tax_option = new TaxOption();
37
        $this->tax_option = $tax_option;
38
39
        $tax_class = new TaxClass();
40
        $this->tax_class = $tax_class;
41
    }
42
43
    /**
44
     * Display a listing of the resource.
45
     *
46
     * @return \Response
47
     */
48
    public function index()
49
    {
50
        try {
51
            $options = $this->tax_option->find(1);
52
            if (!$options) {
53
                $options = '';
54
            }
55
            $classes = $this->tax_class->pluck('name', 'id')->toArray();
56
            if (count($classes) == 0) {
57
                $classes = $this->tax_class->get();
58
            }
59
            $gstNo = $this->tax_option->find(1);
60
61
            return view('themes.default1.payment.tax.index', compact('options', 'classes', 'gstNo'));
62
        } catch (\Exception $ex) {
63
            return redirect()->back()->with('fails', $ex->getMessage());
64
        }
65
    }
66
67
    /**
68
     * @return type
69
     */
70
    public function getTax()
71
    {
72
        return \DataTables::of($this->tax->select('id', 'tax_classes_id', 'name', 'country', 'state', 'rate')->get())
73
                            ->addColumn('checkbox', function ($model) {
74
                                return "<input type='checkbox' class='tax_checkbox' 
75
                                value=".$model->id.' name=select[] id=check>';
76
                            })
77
                            ->addColumn('tax_classes_id', function ($model) {
78
                                return ucfirst($this->tax_class->where('id', $model->tax_classes_id)->first()->name);
79
                            })
80
                            ->addColumn('name', function ($model) {
81
                                return ucfirst($model->name);
82
                            })
83
84
                            // ->showColumns('name', 'level')
85
                            ->addColumn('country', function ($model) {
86
                                if ($this->country->where('country_code_char2', $model->country)->first()) {
87
                                    return ucfirst($this->country
88
                                      ->where('country_code_char2', $model->country)->first()->country_name);
89
                                }
90
                            })
91
                            ->addColumn('state', function ($model) {
92
                                if ($this->state->where('state_subdivision_code', $model->state)->first()) {
93
                                    return $this->state
94
                                    ->where('state_subdivision_code', $model->state)
95
                                    ->first()->state_subdivision_name;
96
                                }
97
                            })
98
                            ->addColumn('rate', function ($model) {
99
                                return $model->rate;
100
                            })
101
102
                            // ->showColumns('rate')
103
                            ->addColumn('action', function ($model) {
104
                                return '<a href='.url('tax/'.$model->id.'/edit').
105
                                " class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' 
106
                                style='color:white;'> </i>&nbsp;&nbsp;Edit</a>";
107
                            })
108
                            ->rawColumns(['checkbox', 'tax_classes_id', 'name', 'country', 'state', 'rate', 'action'])
109
                            ->make(true);
110
    }
111
112
    public function getTaxTable()
113
    {
114
        return \DataTables::of(TaxByState::select('id', 'state', 'c_gst', 's_gst', 'i_gst', 'ut_gst')->get())
115
                         ->addColumn('id', function ($model) {
116
                             return $model->id;
117
                         })
118
119
                         ->addColumn('state', function ($model) {
120
                             return ucfirst($model->state);
121
                         })
122
                         ->addColumn('c_gst', function ($model) {
123
                             return ucfirst($model->c_gst);
124
                         })
125
                         ->addColumn('s_gst', function ($model) {
126
                             return ucfirst($model->s_gst);
127
                         })
128
                         ->addColumn('i_gst', function ($model) {
129
                             return ucfirst($model->i_gst);
130
                         })
131
                         ->addColumn('ut_gst', function ($model) {
132
                             return ucfirst($model->ut_gst);
133
                         })
134
                          ->rawColumns(['id', 'state',  'c_gst', 's_gst', 'i_gst', 'ut_gst'])
135
                          ->make(true);
136
    }
137
138
    
139
140
    /**
141
     * Show the form for editing the specified resource.
142
     *
143
     * @param int $id
144
     *
145
     * @return \Response
146
     */
147
    public function edit($id)
148
    {
149
        try {
150
            $tax = $this->tax->where('id', $id)->first();
151
            $txClass = $this->tax_class->where('id', $tax->tax_classes_id)->first();
152
            if ($txClass->name == 'Others') {
153
                $classes = 0;
154
            } elseif ($txClass->name == 'Intra State GST') {
155
                $classes = 1;
156
            } elseif ($txClass->name == 'Inter State GST') {
157
                $classes = 2;
158
            } else {
159
                $classes = 3;
160
            }
161
            $defaultValue = ['Others', 'Intra State GST', 'Inter State GST', 'Union Territory GST'];
162
163
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($tax->state);
164
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($tax->country);
165
            if (count($classes) == 0) {
166
                $classes = $this->tax_class->get();
167
            }
168
169
            return view('themes.default1.payment.tax.edit',
170
                compact('tax', 'classes', 'txClass', 'states', 'state',
171
172
                    'defaultValue'));
173
        } catch (\Exception $ex) {
174
            return redirect()->back()->with('fails', $ex->getMessage());
175
        }
176
    }
177
178
    /**
179
     * Update the specified resource in storage.
180
     *
181
     * @param int $id
182
     *
183
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Payment\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
184
     */
185
    public function update($id, Request $request)
186
    {
187
        try {
188
            // dd($request->all());
189
            $defaultValue = ['Others', 'Intra State GST', 'Inter State GST', 'Union Territory GST'];
190
191
            if ($request->tax_classes_id == 0) {
192
                $taxClassesName = 'Others';
193
            } elseif ($request->tax_classes_id == 1) {
194
                $taxClassesName = 'Intra State GST';
195
            } elseif ($request->tax_classes_id == 2) {
196
                $taxClassesName = 'Inter State GST';
197
            } else {
198
                $taxClassesName = 'Union Territory GST';
199
            }
200
201
            $TaxClass = TaxClass::where('name', $taxClassesName)->first();
202
            if ($TaxClass == null) {
203
                $TaxClass = $this->tax_class->create(['name'=>$taxClassesName]);
204
            }
205
            $taxId = $TaxClass->id;
206
207
            $tax = $this->tax->where('id', $id)->first();
208
            // dd($tax);
209
            $tax->fill($request->except('tax_classes_id'))->save();
210
211
            $this->tax->where('id', $id)->update(['tax_classes_id'=> $taxId]);
212
            if ($taxClassesName != 'Others') {
213
                $country = 'IN';
214
                $state = '';
215
                $rate = '';
216
                $this->tax->where('id', $id)
217
                ->update(['tax_classes_id'=> $taxId, 'country'=>$country, 'state'=>$state, 'rate'=>$rate]);
218
            }
219
220
            // $tax->fill($request->input())->save();
221
222
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
223
        } catch (\Exception $ex) {
224
            return redirect()->back()->with('fails', $ex->getMessage());
225
        }
226
    }
227
228
    /**
229
     * Remove the specified resource from storage.
230
     *
231
     * @param int $id
232
     *
233
     * @return Response
234
     */
235
    public function destroy(Request $request)
236
    {
237
        try {
238
            $ids = $request->input('select');
239
            if (!empty($ids)) {
240
                foreach ($ids as $id) {
241
                    $tax = $this->tax->where('id', $id)->first();
242
                    $taxClassId = $tax->tax_classes_id;
243
                    $taxClass = $this->tax_class->where('id', $taxClassId)->first();
244
                    if ($tax) {
245
                        $taxClass->delete();
246
                        $tax->delete();
247
                    } else {
248
                        echo "<div class='alert alert-danger alert-dismissable'>
249
                        <i class='fa fa-ban'></i>
250
251
                        <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!
252
                        </b> './* @scrutinizer ignore-type */ \Lang::get('message.failed').'
253
254
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
255
                            '.\Lang::get('message.no-record').'
256
                    </div>';
257
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
258
                    }
259
                }
260
                echo "<div class='alert alert-success alert-dismissable'>
261
                        <i class='fa fa-ban'></i>
262
263
                        <b>".\Lang::get('message.alert').'!</b> '.
264
                        /* @scrutinizer ignore-type */ \Lang::get('message.success').'
265
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
266
                            './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
267
268
                    </div>';
269
            } else {
270
                echo "<div class='alert alert-danger alert-dismissable'>
271
                        <i class='fa fa-ban'></i>
272
273
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
274
                        /* @scrutinizer ignore-type */ \Lang::get('message.failed').'
275
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
276
                            './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
277
278
                    </div>';
279
                //echo \Lang::get('message.select-a-row');
280
            }
281
        } catch (\Exception $e) {
282
            echo "<div class='alert alert-danger alert-dismissable'>
283
                        <i class='fa fa-ban'></i>
284
                        <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
285
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
286
                            '.$e->getMessage().'
287
                    </div>';
288
        }
289
    }
290
291
    /**
292
     * @param Request $request
293
     * @param type    $state
294
     *
295
     * @return type
296
     */
297
    public function getState(Request $request, $stateid)
298
    {
299
        try {
300
            $id = $stateid;
301
            $states = \App\Model\Common\State::where('country_code_char2', $id)
302
            ->orderBy('state_subdivision_name', 'asc')->get();
303
            echo '<option value=>Select a State</option>';
304
            foreach ($states as $state) {
305
                echo '<option value='.$state->state_subdivision_code.'>'.$state->state_subdivision_name.'</option>';
306
            }
307
        } catch (\Exception $ex) {
308
            echo "<option value=''>Problem while loading</option>";
309
310
            return redirect()->back()->with('fails', $ex->getMessage());
311
        }
312
    }
313
314
    /**
315
     * Store a newly created resource in storage.
316
     *
317
     * @return Response
318
     */
319
320
    /**
321
     * @param Request $request
322
     *
323
     * @return type
324
     */
325
    public function options(Request $request)
326
    {
327
        if ($request->input('name') == 'Others') {
328
            // if($request->)
329
            $this->validate($request, [
330
            'rate'        => 'required',
331
        ]);
332
        }
333
334
        try {
335
            $method = $request->method();
336
            if ($method == 'PATCH') {
337
                $rules = $this->tax_option->find(1);
338
                if (!$rules) {
339
                    $this->tax_option->create($request->input());
340
                } else {
341
                    $rules->fill($request->input())->save();
342
                }
343
            } else {
344
                $v = \Validator::make($request->all(), ['name' => 'required']);
345
                if ($v->fails()) {
346
                    return redirect()->back()
347
                                        ->withErrors($v)
348
                                        ->withInput();
349
                }
350
                $this->tax_class->fill($request->except('tax-name', 'level',
351
                  'active', 'country', 'country1', 'rate'))->save();
352
                $country = ($request->input('rate')) ? $request->input('country') : $request->input('country1');
353
354
                $this->tax->fill($request->except('tax-name', 'name', 'country'))->save();
355
                $taxClass = TaxClass::orderBy('id', 'DESC')->first();
356
                $tax = Tax::orderBy('id', 'DESC')->first();
357
                $tax->name = $request->input('tax-name');
358
                $tax->country = $country;
359
                $tax->tax_classes_id = $taxClass->id;
360
                $tax->save();
361
            }
362
363
            return redirect()->back()->with('success', \Lang::get('message.created-successfully'));
364
        } catch (\Exception $ex) {
365
            dd($ex);
366
367
            return redirect()->back()->with('fails', $ex->getMessage());
368
        }
369
    }
370
}
371