|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use App\Model\Payment\Tax; |
|
6
|
|
|
use App\Model\Payment\TaxProductRelation; |
|
7
|
|
|
use Cart; |
|
8
|
|
|
|
|
9
|
|
|
trait TaxCalculation |
|
10
|
|
|
{ |
|
11
|
|
|
public function __construct($s_gst = '', $c_gst = '', $state_code = '', $ut_gst = '', $i_gst = '') |
|
12
|
|
|
{ |
|
13
|
|
|
$this->c_gst = $c_gst; |
|
14
|
|
|
$this->s_gst = $s_gst; |
|
15
|
|
|
$this->state_code = $state_code; |
|
16
|
|
|
$this->ut_gst = $ut_gst; |
|
17
|
|
|
$this->i_gst = $i_gst; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function getDetailsWhenUserStateIsIndian($user_state, $origin_state, $productid, $geoip_state, $geoip_country, $status = 1) |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
if ($user_state != '') {//Get the CGST,SGST,IGST,STATE_CODE of the user,if user from INdia |
|
23
|
|
|
$c_gst = $user_state->c_gst; |
|
24
|
|
|
$s_gst = $user_state->s_gst; |
|
25
|
|
|
$i_gst = $user_state->i_gst; |
|
26
|
|
|
$ut_gst = $user_state->ut_gst; |
|
27
|
|
|
$state_code = $user_state->state_code; |
|
28
|
|
|
|
|
29
|
|
|
if ($state_code == $origin_state) {//If user and origin state are same |
|
30
|
|
|
$rateForSameState = $this->getTaxWhenIndianSameState($user_state, $origin_state, $productid, $c_gst, $s_gst, $state_code, $status); |
|
|
|
|
|
|
31
|
|
|
$taxes = $rateForSameState['taxes']; |
|
32
|
|
|
$status = $rateForSameState['status']; |
|
33
|
|
|
$value = $rateForSameState['value']; |
|
34
|
|
|
} elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state |
|
35
|
|
|
$rateForOtherState = $this->getTaxWhenIndianOtherState($user_state, $origin_state, $productid, $i_gst, $state_code, $status); |
|
|
|
|
|
|
36
|
|
|
$taxes = $rateForOtherState['taxes']; |
|
37
|
|
|
$status = $rateForOtherState['status']; |
|
38
|
|
|
$value = $rateForOtherState['value']; |
|
39
|
|
|
} elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory |
|
40
|
|
|
$rateForUnionTerritory = $this->getTaxWhenUnionTerritory($user_state, $origin_state, $productid, $c_gst, $ut_gst, $state_code, $status); |
|
|
|
|
|
|
41
|
|
|
$taxes = $rateForUnionTerritory['taxes']; |
|
42
|
|
|
$status = $rateForUnionTerritory['status']; |
|
43
|
|
|
$value = $rateForUnionTerritory['value']; |
|
44
|
|
|
} |
|
45
|
|
|
} else { |
|
46
|
|
|
$details = $this->getDetailsWhenUserFromOtherCountry($user_state, $geoip_state, $geoip_country, $productid, $status); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
return $details; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return ['taxes'=> $taxes, 'status'=>$status, 'value'=>$value, |
|
52
|
|
|
'rate' => $value, 'cgst'=>$c_gst, 'sgst'=>$s_gst, 'igst'=>$i_gst, 'utgst'=>$ut_gst, 'statecode'=>$state_code, ]; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getDetailsWhenUserFromOtherCountry($user_state, $geoip_state, $geoip_country, $productid, $status = 1) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
|
|
$taxClassId = Tax::where('state', $geoip_state) |
|
58
|
|
|
->orWhere('country', $geoip_country) |
|
59
|
|
|
->pluck('tax_classes_id')->first(); |
|
60
|
|
|
if ($taxClassId) { //if state equals the user State or country equals user country |
|
61
|
|
|
$taxForSpecificCountry = $this->getTaxForSpecificCountry($taxClassId, $productid, $status); |
|
|
|
|
|
|
62
|
|
|
$taxes = $taxForSpecificCountry['taxes']; |
|
63
|
|
|
$status = $taxForSpecificCountry['status']; |
|
64
|
|
|
$value = $taxForSpecificCountry['value']; |
|
65
|
|
|
$rate = $taxForSpecificCountry['value']; |
|
66
|
|
|
} else {//if Tax is selected for Any Country Any State |
|
67
|
|
|
$taxClassId = Tax::where('country', '') |
|
68
|
|
|
->where('state', 'Any State') |
|
69
|
|
|
->pluck('tax_classes_id')->first(); |
|
70
|
|
|
if ($taxClassId) { |
|
71
|
|
|
$taxForAnyCountry = $this->getTaxForAnyCountry($taxClassId, $productid, $status); |
|
|
|
|
|
|
72
|
|
|
$taxes = $taxForAnyCountry['taxes']; |
|
73
|
|
|
$status = $taxForAnyCountry['status']; |
|
74
|
|
|
$value = $taxForAnyCountry['value']; |
|
75
|
|
|
$rate = $taxForAnyCountry['value']; |
|
76
|
|
|
} else { |
|
77
|
|
|
$value = ''; |
|
78
|
|
|
$rate = ''; |
|
79
|
|
|
$taxes = [0]; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return ['taxes'=>$taxes, 'status'=> $status, 'value'=>$value, 'rate'=>$rate, 'cgst'=>'', 'sgst'=>'', 'igst'=>'', 'utgst'=>'', 'statecode'=>'']; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function whenOtherTaxAvailableAndTaxNotEnable($taxClassId, $productid) |
|
87
|
|
|
{ |
|
88
|
|
|
$status = 1; |
|
89
|
|
|
$taxClassId = Tax::where('country', '') |
|
90
|
|
|
->where('state', 'Any State') |
|
91
|
|
|
->pluck('tax_classes_id')->first(); //In case of India when |
|
92
|
|
|
// other tax is available and tax is not enabled |
|
93
|
|
|
if ($taxClassId) { |
|
94
|
|
|
$taxes = $this->getTaxByPriority($taxClassId); |
|
95
|
|
|
$value = $this->getValueForOthers($productid, $taxClassId, $taxes); |
|
96
|
|
|
if ($value == 0) { |
|
97
|
|
|
$status = 0; |
|
98
|
|
|
} |
|
99
|
|
|
$rate = $value; |
|
100
|
|
|
} else {//In case of other country |
|
101
|
|
|
//when tax is available and tax is not enabled |
|
102
|
|
|
//(Applicable when Global Tax class for any country and state is not there) |
|
103
|
|
|
$taxClassId = Tax::where('state', $geoip_state) |
|
|
|
|
|
|
104
|
|
|
->orWhere('country', $geoip_country) |
|
|
|
|
|
|
105
|
|
|
->pluck('tax_classes_id')->first(); |
|
106
|
|
|
if ($taxClassId) { //if state equals the user State |
|
107
|
|
|
$taxes = $this->getTaxByPriority($taxClassId); |
|
108
|
|
|
$value = $this->getValueForOthers($productid, $taxClassId, $taxes); |
|
109
|
|
|
if ($value == '') { |
|
110
|
|
|
$status = 0; |
|
111
|
|
|
} |
|
112
|
|
|
$rate = $value; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return ['taxes'=>$taxes, 'value'=>$value, 'status'=>$status]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get tax value for Same State. |
|
121
|
|
|
* |
|
122
|
|
|
* @param int $productid |
|
123
|
|
|
* @param type $c_gst |
|
124
|
|
|
* @param type $s_gst |
|
125
|
|
|
* return type |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes) |
|
128
|
|
|
{ |
|
129
|
|
|
try { |
|
130
|
|
|
$value = ''; |
|
131
|
|
|
$value = $taxes->toArray()[0]['active'] ? |
|
132
|
|
|
|
|
133
|
|
|
(TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ? |
|
134
|
|
|
$c_gst + $s_gst.'%' : 0) : 0; |
|
135
|
|
|
|
|
136
|
|
|
return $value; |
|
137
|
|
|
} catch (Exception $ex) { |
|
|
|
|
|
|
138
|
|
|
Bugsnag::notifyException($ex); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Get tax value for Other States. |
|
146
|
|
|
* |
|
147
|
|
|
* @param type $productid |
|
148
|
|
|
* @param type $i_gst |
|
149
|
|
|
* return type |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getValueForOtherState($productid, $i_gst, $taxClassId, $taxes) |
|
152
|
|
|
{ |
|
153
|
|
|
$value = ''; |
|
154
|
|
|
$value = $taxes->toArray()[0]['active'] ? //If the Current Class is active |
|
155
|
|
|
(TaxProductRelation::where('product_id', $productid)->where('tax_class_id', $taxClassId)->count() ? |
|
156
|
|
|
$i_gst.'%' : 0) : 0; //IGST |
|
157
|
|
|
|
|
158
|
|
|
return $value; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get tax value for Union Territory. |
|
163
|
|
|
* |
|
164
|
|
|
* @param type $productid |
|
165
|
|
|
* @param type $c_gst |
|
166
|
|
|
* @param type $ut_gst |
|
167
|
|
|
* return type |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes) |
|
170
|
|
|
{ |
|
171
|
|
|
$value = ''; |
|
172
|
|
|
$value = $taxes->toArray()[0]['active'] ? |
|
173
|
|
|
(TaxProductRelation::where('product_id', $productid) |
|
174
|
|
|
->where('tax_class_id', $taxClassId)->count() ? $ut_gst + $c_gst.'%' : 0) : 0; |
|
175
|
|
|
|
|
176
|
|
|
return $value; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function otherRate($productid) |
|
180
|
|
|
{ |
|
181
|
|
|
$otherRate = ''; |
|
182
|
|
|
|
|
183
|
|
|
return $otherRate; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function getValueForOthers($productid, $taxClassId, $taxes) |
|
187
|
|
|
{ |
|
188
|
|
|
$otherRate = 0; |
|
189
|
|
|
$status = $taxes->toArray()[0]['active']; |
|
190
|
|
|
if ($status && (TaxProductRelation::where('product_id', $productid) |
|
191
|
|
|
->where('tax_class_id', $taxClassId)->count() > 0)) { |
|
192
|
|
|
$otherRate = Tax::where('tax_classes_id', $taxClassId)->first()->rate; |
|
193
|
|
|
} |
|
194
|
|
|
$value = $otherRate.'%'; |
|
195
|
|
|
|
|
196
|
|
|
return $value; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param type $tax_class_id |
|
201
|
|
|
* |
|
202
|
|
|
* @throws \Exception |
|
203
|
|
|
* |
|
204
|
|
|
* @return type |
|
205
|
|
|
*/ |
|
206
|
|
|
public function getTaxByPriority($taxClassId) |
|
207
|
|
|
{ |
|
208
|
|
|
try { |
|
209
|
|
|
$taxe_relation = Tax::where('tax_classes_id', $taxClassId)->get(); |
|
210
|
|
|
|
|
211
|
|
|
return $taxe_relation; |
|
212
|
|
|
} catch (\Exception $ex) { |
|
213
|
|
|
Bugsnag::notifyException($ex); |
|
214
|
|
|
|
|
215
|
|
|
throw new \Exception('error in get tax priority'); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function cartRemove(Request $request) |
|
|
|
|
|
|
220
|
|
|
{ |
|
221
|
|
|
$id = $request->input('id'); |
|
222
|
|
|
Cart::remove($id); |
|
223
|
|
|
|
|
224
|
|
|
return 'success'; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param type $price |
|
229
|
|
|
* |
|
230
|
|
|
* @throws \Exception |
|
231
|
|
|
* |
|
232
|
|
|
* @return type |
|
233
|
|
|
*/ |
|
234
|
|
|
public static function rounding($price) |
|
235
|
|
|
{ |
|
236
|
|
|
try { |
|
237
|
|
|
$tax_rule = new \App\Model\Payment\TaxOption(); |
|
238
|
|
|
$rule = $tax_rule->findOrFail(1); |
|
239
|
|
|
$rounding = $rule->rounding; |
|
240
|
|
|
|
|
241
|
|
|
return $price; |
|
242
|
|
|
} catch (\Exception $ex) { |
|
243
|
|
|
Bugsnag::notifyException($ex); |
|
244
|
|
|
// throw new \Exception('error in get tax priority'); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param type $rate |
|
250
|
|
|
* @param type $price |
|
251
|
|
|
* |
|
252
|
|
|
* @return type |
|
253
|
|
|
*/ |
|
254
|
|
|
public static function taxValue($rate, $price) |
|
255
|
|
|
{ |
|
256
|
|
|
try { |
|
257
|
|
|
$result = ''; |
|
258
|
|
|
if ($rate) { |
|
|
|
|
|
|
259
|
|
|
$rate = str_replace('%', '', $rate); |
|
260
|
|
|
$tax = intval($price) * ($rate / 100); |
|
261
|
|
|
$result = $tax; |
|
262
|
|
|
|
|
263
|
|
|
$result = self::rounding($result); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return $result; |
|
267
|
|
|
} catch (\Exception $ex) { |
|
268
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @throws \Exception |
|
274
|
|
|
* |
|
275
|
|
|
* @return bool |
|
276
|
|
|
*/ |
|
277
|
|
|
public function checkCurrencySession() |
|
278
|
|
|
{ |
|
279
|
|
|
try { |
|
280
|
|
|
if (Session::has('currency')) { |
|
|
|
|
|
|
281
|
|
|
return true; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
return false; |
|
285
|
|
|
} catch (\Exception $ex) { |
|
286
|
|
|
throw new \Exception($ex->getMessage()); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
|
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.