Completed
Push — development ( 517bd2...7c518b )
by Ashutosh
10:22
created

TaxController::index()   A

Complexity

Conditions 4
Paths 19

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
cc 4
nc 19
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()
0 ignored issues
show
Coding Style introduced by
Function name "__construct" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
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' value=".$model->id.' name=select[] id=check>';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 130 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
75
                            })
76
                            ->addColumn('tax_classes_id', function ($model) {
77
                                return ucfirst($this->tax_class->where('id', $model->tax_classes_id)->first()->name);
78
                            })
79
                            ->addColumn('name', function ($model) {
80
                                return ucfirst($model->name);
81
                            })
82
83
                            // ->showColumns('name', 'level')
84
                            ->addColumn('country', function ($model) {
85
                                if ($this->country->where('country_code_char2', $model->country)->first()) {
86
                                    return ucfirst($this->country->where('country_code_char2', $model->country)->first()->country_name);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 136 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
87
                                }
88
                            })
89
                            ->addColumn('state', function ($model) {
90
                                if ($this->state->where('state_subdivision_code', $model->state)->first()) {
91
                                    return $this->state->where('state_subdivision_code', $model->state)->first()->state_subdivision_name;
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
92
                                }
93
                            })
94
                            ->addColumn('rate', function ($model) {
95
                                return $model->rate;
96
                            })
97
98
                            // ->showColumns('rate')
99
                            ->addColumn('action', function ($model) {
100
                                return '<a href='.url('tax/'.$model->id.'/edit')." class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' style='color:white;'> </i>&nbsp;&nbsp;Edit</a>";
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 191 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
101
                            })
102
                            ->rawColumns(['checkbox', 'tax_classes_id', 'name', 'country', 'state', 'rate', 'action'])
103
                            ->make(true);
104
    }
105
106
    public function getTaxTable()
107
    {
108
        return \DataTables::of(TaxByState::select('id', 'state', 'c_gst', 's_gst', 'i_gst', 'ut_gst')->get())
109
                         ->addColumn('id', function ($model) {
110
                             return $model->id;
111
                         })
112
113
                         ->addColumn('state', function ($model) {
114
                             return ucfirst($model->state);
115
                         })
116
                         ->addColumn('c_gst', function ($model) {
117
                             return ucfirst($model->c_gst);
118
                         })
119
                         ->addColumn('s_gst', function ($model) {
120
                             return ucfirst($model->s_gst);
121
                         })
122
                         ->addColumn('i_gst', function ($model) {
123
                             return ucfirst($model->i_gst);
124
                         })
125
                         ->addColumn('ut_gst', function ($model) {
126
                             return ucfirst($model->ut_gst);
127
                         })
128
                          ->rawColumns(['id', 'state',  'c_gst', 's_gst', 'i_gst', 'ut_gst'])
129
                          ->make(true);
130
    }
131
132
    /**
133
     * Show the form for editing the specified resource.
134
     *
135
     * @param int $id
136
     *
137
     * @return \Response
138
     */
139
    public function edit($id)
140
    {
141
        try {
142
            $tax = $this->tax->where('id', $id)->first();
143
            $txClass = $this->tax_class->where('id', $tax->tax_classes_id)->first();
144
            if ($txClass->name == 'Others') {
145
                $classes = 0;
146
            } elseif ($txClass->name == 'Intra State GST') {
147
                $classes = 1;
148
            } elseif ($txClass->name == 'Inter State GST') {
149
                $classes = 2;
150
            } else {
151
                $classes = 3;
152
            }
153
            $defaultValue = ['Others', 'Intra State GST', 'Inter State GST', 'Union Territory GST'];
154
155
            $state = \App\Http\Controllers\Front\CartController::getStateByCode($tax->state);
156
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($tax->country);
157
            if (count($classes) == 0) {
158
                $classes = $this->tax_class->get();
159
            }
160
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SL on line 160 at column 0
Loading history...
161
             return view('themes.default1.payment.tax.edit', 
162
                compact('tax', 'classes', 'txClass', 'states', 'state', 
163
=======
164
165
            return view('themes.default1.payment.tax.edit',
166
                compact('tax', 'classes', 'txClass', 'states', 'state',
167
>>>>>>> 517bd2a5f9d484a8cfaa09565e62e3269033bb6e
168
                    'defaultValue'));
169
        } catch (\Exception $ex) {
170
            return redirect()->back()->with('fails', $ex->getMessage());
171
        }
172
    }
173
174
    /**
175
     * Update the specified resource in storage.
176
     *
177
     * @param int $id
178
     *
179
     * @return \Response
180
     */
181
    public function update($id, Request $request)
182
    {
183
        try {
184
            // dd($request->all());
185
            $defaultValue = ['Others', 'Intra State GST', 'Inter State GST', 'Union Territory GST'];
186
187
            if ($request->tax_classes_id == 0) {
188
                $taxClassesName = 'Others';
189
            } elseif ($request->tax_classes_id == 1) {
190
                $taxClassesName = 'Intra State GST';
191
            } elseif ($request->tax_classes_id == 2) {
192
                $taxClassesName = 'Inter State GST';
193
            } else {
194
                $taxClassesName = 'Union Territory GST';
195
            }
196
197
            $TaxClass = TaxClass::where('name', $taxClassesName)->first();
198
            if ($TaxClass == null) {
199
                $TaxClass = $this->tax_class->create(['name'=>$taxClassesName]);
200
            }
201
            $taxId = $TaxClass->id;
202
203
            $tax = $this->tax->where('id', $id)->first();
204
            // dd($tax);
205
            $tax->fill($request->except('tax_classes_id'))->save();
206
207
            $this->tax->where('id', $id)->update(['tax_classes_id'=> $taxId]);
208
            if ($taxClassesName != 'Others') {
209
                $country = 'IN';
210
                $state = '';
211
                $rate = '';
212
                $this->tax->where('id', $id)->update(['tax_classes_id'=> $taxId,
213
                    'country'                                         => $country, 'state'=>$state, 'rate'=>$rate, ]);
214
            }
215
216
            // $tax->fill($request->input())->save();
217
218
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
219
        } catch (\Exception $ex) {
220
            return redirect()->back()->with('fails', $ex->getMessage());
221
        }
222
    }
223
224
    /**
225
     * Remove the specified resource from storage.
226
     *
227
     * @param int $id
228
     *
229
     * @return \Response
230
     */
231
    public function destroy(Request $request)
232
    {
233
        try {
234
            $ids = $request->input('select');
235
            if (!empty($ids)) {
236
                foreach ($ids as $id) {
237
                    $tax = $this->tax->where('id', $id)->first();
238
                    $taxClassId = $tax->tax_classes_id;
239
                    $taxClass = $this->tax_class->where('id', $taxClassId)->first();
240
                    if ($tax) {
241
                        $taxClass->delete();
242
                        $tax->delete();
243
                    } else {
244
                        echo "<div class='alert alert-danger alert-dismissable'>
245
                        <i class='fa fa-ban'></i>
246
                        <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!
247
                        </b> './* @scrutinizer ignore-type */ \Lang::get('message.failed').'
248
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
249
                            './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
250
                    </div>';
251
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
252
                    }
253
                }
254
                echo "<div class='alert alert-success alert-dismissable'>
255
                        <i class='fa fa-ban'></i>
256
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
257
                        /* @scrutinizer ignore-type */ \Lang::get('message.success').'
258
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
259
                            './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
260
                    </div>';
261
            } else {
262
                echo "<div class='alert alert-danger alert-dismissable'>
263
                        <i class='fa fa-ban'></i>
264
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
265
                        /* @scrutinizer ignore-type */ \Lang::get('message.failed').'
266
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
267
                            './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
268
                    </div>';
269
                //echo \Lang::get('message.select-a-row');
270
            }
271
        } catch (\Exception $e) {
272
            echo "<div class='alert alert-danger alert-dismissable'>
273
                        <i class='fa fa-ban'></i>
274
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
275
                        /* @scrutinizer ignore-type */\Lang::get('message.failed').'
276
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
277
                            '.$e->getMessage().'
278
                    </div>';
279
        }
280
    }
281
282
    /**
283
     * @param Request $request
284
     * @param type    $state
285
     *
286
     * @return type
287
     */
288
    public function getState(Request $request, $state)
289
    {
290
        try {
291
            $id = $state;
292
            $states = \App\Model\Common\State::where('country_code_char2', $id)->orderBy('state_subdivision_name', 'asc')->get();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 129 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
293
            // return $states;
294
            echo '<option value=>Select a State</option>';
295
            foreach ($states as $stateList) {
296
                echo '<option value='.$stateList->state_subdivision_code.'>'.$stateList->state_subdivision_name.'</option>';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
297
            }
298
        } catch (\Exception $ex) {
299
            echo "<option value=''>Problem while loading</option>";
300
301
            return redirect()->back()->with('fails', $ex->getMessage());
302
        }
303
    }
304
305
    /**
306
     * Store a newly created resource in storage.
307
     *
308
     * @return \Response
309
     */
310
311
    /**
312
     * @param Request $request
313
     *
314
     * @return type
315
     */
316
    public function options(Request $request)
317
    {
318
        try {
319
            $method = $request->method();
320
            if ($method == 'PATCH') {
321
                $rules = $this->tax_option->find(1);
322
                if (!$rules) {
323
                    $this->tax_option->create($request->input());
324
                } else {
325
                    $rules->fill($request->input())->save();
326
                }
327
            } else {
328
                $v = \Validator::make($request->all(), ['name' => 'required']);
329
                if ($v->fails()) {
330
                    return redirect()->back()
331
                                        ->withErrors($v)
332
                                        ->withInput();
333
                }
334
                $this->tax_class->fill($request
335
                    ->except('tax-name', 'level', 'active', 'country', 'country1', 'rate'))->save();
336
                $country = ($request->input('rate')) ? $request->input('country') : $request->input('country1');
337
338
                $this->tax->fill($request->except('tax-name', 'name', 'country'))->save();
339
                $taxClass = TaxClass::orderBy('id', 'DESC')->first();
340
                $tax = Tax::orderBy('id', 'DESC')->first();
341
                $tax->name = $request->input('tax-name');
342
                $tax->country = $country;
343
                $tax->tax_classes_id = $taxClass->id;
344
                $tax->save();
345
            }
346
347
            return redirect()->back()->with('success', \Lang::get('message.created-successfully'));
348
        } catch (\Exception $ex) {
349
            return redirect()->back()->with('fails', $ex->getMessage());
350
        }
351
    }
352
}
353