Completed
Push — development ( a546b2...2b11c6 )
by Ashutosh
09:55
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' value=".$model->id.' name=select[] id=check>';
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);
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;
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>";
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
    /**
134
     * Show the form for editing the specified resource.
135
     *
136
     * @param int $id
137
     *
138
     * @return \Response
139
     */
140
    public function edit($id)
141
    {
142
        try {
143
            $tax = $this->tax->where('id', $id)->first();
144
            $txClass = $this->tax_class->where('id', $tax->tax_classes_id)->first();
145
            if ($txClass->name == 'Others') {
146
                $classes = 0;
147
            } elseif ($txClass->name == 'Intra State GST') {
148
                $classes = 1;
149
            } elseif ($txClass->name == 'Inter State GST') {
150
                $classes = 2;
151
            } else {
152
                $classes = 3;
153
            }
154
            $defaultValue = ['Others', 'Intra State GST', 'Inter State GST', 'Union Territory GST'];
155
156
             $state = \App\Http\Controllers\Front\CartController::getStateByCode($tax->state);
157
            $states = \App\Http\Controllers\Front\CartController::findStateByRegionId($tax->country);
158
            if (count($classes) == 0) {
159
                $classes = $this->tax_class->get();
160
            }
161
162
            return view('themes.default1.payment.tax.edit', compact('tax', 'classes', 'txClass', 'states', 'state', 'defaultValue'));
163
        } catch (\Exception $ex) {
164
            return redirect()->back()->with('fails', $ex->getMessage());
165
        }
166
    }
167
168
    /**
169
     * Update the specified resource in storage.
170
     *
171
     * @param int $id
172
     *
173
     * @return \Response
174
     */
175
    public function update($id, Request $request)
176
    {
177
        try {
178
            // dd($request->all());
179
            $defaultValue = ['Others', 'Intra State GST', 'Inter State GST', 'Union Territory GST'];
180
181
            if ($request->tax_classes_id == 0) {
182
                $taxClassesName = 'Others';
183
            } elseif ($request->tax_classes_id == 1) {
184
                $taxClassesName = 'Intra State GST';
185
            } elseif ($request->tax_classes_id == 2) {
186
                $taxClassesName = 'Inter State GST';
187
            } else {
188
                $taxClassesName = 'Union Territory GST';
189
            }
190
191
            $TaxClass = TaxClass::where('name', $taxClassesName)->first();
192
            if ($TaxClass == null) {
193
                $TaxClass = $this->tax_class->create(['name'=>$taxClassesName]);
194
            }
195
            $taxId = $TaxClass->id;
196
197
            $tax = $this->tax->where('id', $id)->first();
198
            // dd($tax);
199
            $tax->fill($request->except('tax_classes_id'))->save();
200
201
            $this->tax->where('id', $id)->update(['tax_classes_id'=> $taxId]);
202
            if ($taxClassesName != 'Others') {
203
                $country = 'IN';
204
                $state = '';
205
                $rate = '';
206
                $this->tax->where('id', $id)->update(['tax_classes_id'=> $taxId, 'country'=>$country, 'state'=>$state, 'rate'=>$rate]);
207
            }
208
209
            // $tax->fill($request->input())->save();
210
211
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
212
        } catch (\Exception $ex) {
213
            return redirect()->back()->with('fails', $ex->getMessage());
214
        }
215
    }
216
217
    /**
218
     * Remove the specified resource from storage.
219
     *
220
     * @param int $id
221
     *
222
     * @return \Response
223
     */
224
    public function destroy(Request $request)
225
    {
226
        try {
227
            $ids = $request->input('select');
228
            if (!empty($ids)) {
229
                foreach ($ids as $id) {
230
                    $tax = $this->tax->where('id', $id)->first();
231
                    $taxClassId = $tax->tax_classes_id;
232
                    $taxClass = $this->tax_class->where('id', $taxClassId)->first();
233
                    if ($tax) {
234
                        $taxClass->delete();
235
                        $tax->delete();
236
                    } else {
237
                        echo "<div class='alert alert-danger alert-dismissable'>
238
                        <i class='fa fa-ban'></i>
239
                        <b>"./* @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ \Lang::get('message.failed').'
240
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
241
                            './* @scrutinizer ignore-type */\Lang::get('message.no-record').'
242
                    </div>';
243
                        //echo \Lang::get('message.no-record') . '  [id=>' . $id . ']';
244
                    }
245
                }
246
                echo "<div class='alert alert-success alert-dismissable'>
247
                        <i class='fa fa-ban'></i>
248
                        <b>".\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ \Lang::get('message.success').'
0 ignored issues
show
Bug introduced by
Are you sure Lang::get('message.alert') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

248
                        <b>"./** @scrutinizer ignore-type */ \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ \Lang::get('message.success').'
Loading history...
249
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
250
                            './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').'
251
                    </div>';
252
            } else {
253
                echo "<div class='alert alert-danger alert-dismissable'>
254
                        <i class='fa fa-ban'></i>
255
                        <b>".\Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ \Lang::get('message.failed').'
256
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
257
                            './* @scrutinizer ignore-type */ \Lang::get('message.select-a-row').'
258
                    </div>';
259
                //echo \Lang::get('message.select-a-row');
260
            }
261
        } catch (\Exception $e) {
262
            echo "<div class='alert alert-danger alert-dismissable'>
263
                        <i class='fa fa-ban'></i>
264
                        <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').'
0 ignored issues
show
Bug introduced by
Are you sure Lang::get('message.failed') of type null|string|array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

264
                        <b>".\Lang::get('message.alert').'!</b> './** @scrutinizer ignore-type */ \Lang::get('message.failed').'
Loading history...
265
                        <button type=button class=close data-dismiss=alert aria-hidden=true>&times;</button>
266
                            '.$e->getMessage().'
267
                    </div>';
268
        }
269
    }
270
271
    /**
272
     * @param Request $request
273
     * @param type    $state
274
     *
275
     * @return type
276
     */
277
    public function getState(Request $request, $state)
278
    {
279
        try {
280
            $id = $state;
281
            $states = \App\Model\Common\State::where('country_code_char2', $id)->orderBy('state_subdivision_name', 'asc')->get();
282
            // return $states;
283
            echo '<option value=>Select a State</option>';
284
            foreach ($states as $stateList) {
285
                echo '<option value='.$stateList->state_subdivision_code.'>'.$stateList->state_subdivision_name.'</option>';
286
            }
287
        } catch (\Exception $ex) {
288
            echo "<option value=''>Problem while loading</option>";
289
290
            return redirect()->back()->with('fails', $ex->getMessage());
291
        }
292
    }
293
294
    /**
295
     * Store a newly created resource in storage.
296
     *
297
     * @return \Response
298
     */
299
300
    /**
301
     * @param Request $request
302
     *
303
     * @return type
304
     */
305
    public function options(Request $request)
306
    {
307
        try {
308
            $method = $request->method();
309
            if ($method == 'PATCH') {
310
                $rules = $this->tax_option->find(1);
311
                if (!$rules) {
312
                    $this->tax_option->create($request->input());
313
                } else {
314
                    $rules->fill($request->input())->save();
315
                }
316
            } else {
317
                $v = \Validator::make($request->all(), ['name' => 'required']);
318
                if ($v->fails()) {
319
                    return redirect()->back()
320
                                        ->withErrors($v)
321
                                        ->withInput();
322
                }
323
                $this->tax_class->fill($request->except('tax-name', 'level', 'active', 'country', 'country1', 'rate'))->save();
324
                $country = ($request->input('rate')) ? $request->input('country') : $request->input('country1');
325
326
                $this->tax->fill($request->except('tax-name', 'name', 'country'))->save();
327
                $taxClass = TaxClass::orderBy('id', 'DESC')->first();
328
                $tax = Tax::orderBy('id', 'DESC')->first();
329
                $tax->name = $request->input('tax-name');
330
                $tax->country = $country;
331
                $tax->tax_classes_id = $taxClass->id;
332
                $tax->save();
333
            }
334
335
            return redirect()->back()->with('success', \Lang::get('message.created-successfully'));
336
        } catch (\Exception $ex) {
337
            return redirect()->back()->with('fails', $ex->getMessage());
338
        }
339
    }
340
}
341