Test Failed
Push — development ( 43b23c...9767d3 )
by Ashutosh
08:48
created

TaxController::edit()   B

Complexity

Conditions 6
Paths 34

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 27
rs 8.9617
c 0
b 0
f 0
cc 6
nc 34
nop 1
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' 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
161
            return view('themes.default1.payment.tax.edit', 
162
                compact('tax', 'classes', 'txClass', 'states', 'state', 
163
                    'defaultValue'));
164
        } catch (\Exception $ex) {
165
            return redirect()->back()->with('fails', $ex->getMessage());
166
        }
167
    }
168
169
    /**
170
     * Update the specified resource in storage.
171
     *
172
     * @param int $id
173
     *
174
     * @return \Response
175
     */
176
    public function update($id, Request $request)
177
    {
178
        try {
179
            // dd($request->all());
180
            $defaultValue = ['Others', 'Intra State GST', 'Inter State GST', 'Union Territory GST'];
181
182
            if ($request->tax_classes_id == 0) {
183
                $taxClassesName = 'Others';
184
            } elseif ($request->tax_classes_id == 1) {
185
                $taxClassesName = 'Intra State GST';
186
            } elseif ($request->tax_classes_id == 2) {
187
                $taxClassesName = 'Inter State GST';
188
            } else {
189
                $taxClassesName = 'Union Territory GST';
190
            }
191
192
            $TaxClass = TaxClass::where('name', $taxClassesName)->first();
193
            if ($TaxClass == null) {
194
                $TaxClass = $this->tax_class->create(['name'=>$taxClassesName]);
195
            }
196
            $taxId = $TaxClass->id;
197
198
            $tax = $this->tax->where('id', $id)->first();
199
            // dd($tax);
200
            $tax->fill($request->except('tax_classes_id'))->save();
201
202
            $this->tax->where('id', $id)->update(['tax_classes_id'=> $taxId]);
203
            if ($taxClassesName != 'Others') {
204
                $country = 'IN';
205
                $state = '';
206
                $rate = '';
207
                $this->tax->where('id', $id)->update(['tax_classes_id'=> $taxId, 
208
                    'country'=>$country, 'state'=>$state, 'rate'=>$rate]);
209
            }
210
211
            // $tax->fill($request->input())->save();
212
213
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
214
        } catch (\Exception $ex) {
215
            return redirect()->back()->with('fails', $ex->getMessage());
216
        }
217
    }
218
219
    /**
220
     * Remove the specified resource from storage.
221
     *
222
     * @param int $id
223
     *
224
     * @return \Response
225
     */
226
    public function destroy(Request $request)
227
    {
228
        try {
229
            $ids = $request->input('select');
230
            if (!empty($ids)) {
231
                foreach ($ids as $id) {
232
                    $tax = $this->tax->where('id', $id)->first();
233
                    $taxClassId = $tax->tax_classes_id;
234
                    $taxClass = $this->tax_class->where('id', $taxClassId)->first();
235
                    if ($tax) {
236
                        $taxClass->delete();
237
                        $tax->delete();
238
                    } else {
239
                        echo "<div class='alert alert-danger alert-dismissable'>
240
                        <i class='fa fa-ban'></i>
241
                        <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!
242
                        </b> './* @scrutinizer ignore-type */ \Lang::get('message.failed').'
243
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
244
                            './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
245
                    </div>';
246
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
247
                    }
248
                }
249
                echo "<div class='alert alert-success alert-dismissable'>
250
                        <i class='fa fa-ban'></i>
251
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
252
                        /* @scrutinizer ignore-type */ \Lang::get('message.success').'
253
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
254
                            './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
255
                    </div>';
256
            } else {
257
                echo "<div class='alert alert-danger alert-dismissable'>
258
                        <i class='fa fa-ban'></i>
259
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
260
                        /* @scrutinizer ignore-type */ \Lang::get('message.failed').'
261
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
262
                            './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
263
                    </div>';
264
                //echo \Lang::get('message.select-a-row');
265
            }
266
        } catch (\Exception $e) {
267
            echo "<div class='alert alert-danger alert-dismissable'>
268
                        <i class='fa fa-ban'></i>
269
                        <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '.
270
                        /* @scrutinizer ignore-type */\Lang::get('message.failed').'
271
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
272
                            '.$e->getMessage().'
273
                    </div>';
274
        }
275
    }
276
277
    /**
278
     * @param Request $request
279
     * @param type    $state
280
     *
281
     * @return type
282
     */
283
    public function getState(Request $request, $state)
284
    {
285
        try {
286
            $id = $state;
287
            $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...
288
            // return $states;
289
            echo '<option value=>Select a State</option>';
290
            foreach ($states as $stateList) {
291
                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...
292
            }
293
        } catch (\Exception $ex) {
294
            echo "<option value=''>Problem while loading</option>";
295
296
            return redirect()->back()->with('fails', $ex->getMessage());
297
        }
298
    }
299
300
    /**
301
     * Store a newly created resource in storage.
302
     *
303
     * @return \Response
304
     */
305
306
    /**
307
     * @param Request $request
308
     *
309
     * @return type
310
     */
311
    public function options(Request $request)
312
    {
313
        try {
314
            $method = $request->method();
315
            if ($method == 'PATCH') {
316
                $rules = $this->tax_option->find(1);
317
                if (!$rules) {
318
                    $this->tax_option->create($request->input());
319
                } else {
320
                    $rules->fill($request->input())->save();
321
                }
322
            } else {
323
                $v = \Validator::make($request->all(), ['name' => 'required']);
324
                if ($v->fails()) {
325
                    return redirect()->back()
326
                                        ->withErrors($v)
327
                                        ->withInput();
328
                }
329
                $this->tax_class->fill($request
330
                    ->except('tax-name', 'level', 'active', 'country', 'country1', 'rate'))->save();
331
                $country = ($request->input('rate')) ? $request->input('country') : $request->input('country1');
332
333
                $this->tax->fill($request->except('tax-name', 'name', 'country'))->save();
334
                $taxClass = TaxClass::orderBy('id', 'DESC')->first();
335
                $tax = Tax::orderBy('id', 'DESC')->first();
336
                $tax->name = $request->input('tax-name');
337
                $tax->country = $country;
338
                $tax->tax_classes_id = $taxClass->id;
339
                $tax->save();
340
            }
341
342
            return redirect()->back()->with('success', \Lang::get('message.created-successfully'));
343
        } catch (\Exception $ex) {
344
            return redirect()->back()->with('fails', $ex->getMessage());
345
        }
346
    }
347
}
348