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