| Conditions | 35 |
| Paths | 2824 |
| Total Lines | 214 |
| Code Lines | 157 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 155 | public function checkTax($productid) |
||
| 156 | { |
||
| 157 | try { |
||
| 158 | $tax_condition = array(); |
||
| 159 | $tax_attribute = array(); |
||
| 160 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0]; |
||
| 161 | $taxCondition[0] = new \Darryldecode\Cart\CartCondition([ |
||
|
|
|||
| 162 | 'name' => 'null', |
||
| 163 | 'type' => 'tax', |
||
| 164 | 'target' => 'item', |
||
| 165 | 'value' => '0%', |
||
| 166 | ]); |
||
| 167 | $cont = new \App\Http\Controllers\Front\GetPageTemplateController(); |
||
| 168 | $location = $cont->getLocation(); |
||
| 169 | |||
| 170 | $country = $this->findCountryByGeoip($location['countryCode']); |
||
| 171 | $states = $this->findStateByRegionId($location['countryCode']); |
||
| 172 | $states = \App\Model\Common\State::pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
||
| 173 | $state_code = $location['countryCode'].'-'.$location['region']; |
||
| 174 | $state = $this->getStateByCode($state_code); |
||
| 175 | $mobile_code = $this->getMobileCodeByIso($location['countryCode']); |
||
| 176 | $country_iso = $location['countryCode']; |
||
| 177 | $geoip_country = ''; |
||
| 178 | $geoip_state = ''; |
||
| 179 | if (\Auth::user()) { |
||
| 180 | $geoip_country = \Auth::user()->country; |
||
| 181 | $geoip_state = \Auth::user()->state; |
||
| 182 | } |
||
| 183 | if ($geoip_country == '') { |
||
| 184 | $geoip_country = \App\Http\Controllers\Front\CartController::findCountryByGeoip($country_iso); |
||
| 185 | } |
||
| 186 | $geoip_state_array = \App\Http\Controllers\Front\CartController::getStateByCode($state_code); |
||
| 187 | if ($geoip_state == '') { |
||
| 188 | if (array_key_exists('id', $geoip_state_array)) { |
||
| 189 | $geoip_state = $geoip_state_array['id']; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($this->tax_option->findOrFail(1)->inclusive == 0) { |
||
| 194 | $tax_rule = $this->tax_option->findOrFail(1); |
||
| 195 | $shop = $tax_rule->shop_inclusive; |
||
| 196 | $cart = $tax_rule->cart_inclusive; |
||
| 197 | $tax_enable = $this->tax_option->findOrFail(1)->tax_enable; |
||
| 198 | //Check the state of user for calculating GST(cgst,igst,utgst,sgst) |
||
| 199 | $user_state = TaxByState::where('state_code', $geoip_state)->first(); |
||
| 200 | $origin_state = $this->setting->first()->state; //Get the State of origin |
||
| 201 | $tax_class_id = TaxProductRelation::where('product_id', $productid)->pluck('tax_class_id')->toArray(); |
||
| 202 | |||
| 203 | if ($tax_class_id) {//If the product is allowed for tax (Check in tax_product relation table) |
||
| 204 | if ($tax_enable == 1) {//If GST is Enabled |
||
| 205 | |||
| 206 | $state_code = ''; |
||
| 207 | $c_gst = ''; |
||
| 208 | $s_gst = ''; |
||
| 209 | $i_gst = ''; |
||
| 210 | $ut_gst = ''; |
||
| 211 | $value = ''; |
||
| 212 | $rate = ''; |
||
| 213 | $status = 1; |
||
| 214 | |||
| 215 | if ($user_state != '') {//Get the CGST,SGST,IGST,STATE_CODE of the user |
||
| 216 | |||
| 217 | $c_gst = $user_state->c_gst; |
||
| 218 | $s_gst = $user_state->s_gst; |
||
| 219 | $i_gst = $user_state->i_gst; |
||
| 220 | $ut_gst = $user_state->ut_gst; |
||
| 221 | $state_code = $user_state->state_code; |
||
| 222 | if ($state_code == $origin_state) {//If user and origin state are same |
||
| 223 | |||
| 224 | $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 225 | if ($taxClassId) { |
||
| 226 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 227 | $value = $this->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes); |
||
| 228 | |||
| 229 | if ($value == '') { |
||
| 230 | $status = 0; |
||
| 231 | } |
||
| 232 | } else { |
||
| 233 | $taxes = [0]; |
||
| 234 | } |
||
| 235 | } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state |
||
| 236 | |||
| 237 | $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 238 | if ($taxClassId) { |
||
| 239 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 240 | $value = $this->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes); |
||
| 241 | if ($value == '') { |
||
| 242 | $status = 0; |
||
| 243 | } |
||
| 244 | } else { |
||
| 245 | $taxes = [0]; |
||
| 246 | } |
||
| 247 | } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory |
||
| 248 | $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id of state |
||
| 249 | if ($taxClassId) { |
||
| 250 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 251 | $value = $this->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes); |
||
| 252 | if ($value == '') { |
||
| 253 | $status = 0; |
||
| 254 | } |
||
| 255 | } else { |
||
| 256 | $taxes = [0]; |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } else {//If user from other Country |
||
| 260 | $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); |
||
| 261 | |||
| 262 | if ($taxClassId) { //if state equals the user State or country equals user country |
||
| 263 | |||
| 264 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 265 | $value = $this->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 266 | if ($value == '') { |
||
| 267 | $status = 0; |
||
| 268 | } |
||
| 269 | $rate = $value; |
||
| 270 | } else {//if Tax is selected for Any Country Any State |
||
| 271 | $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first(); |
||
| 272 | if ($taxClassId) { |
||
| 273 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 274 | $value = $this->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 275 | if ($value == '') { |
||
| 276 | $status = 0; |
||
| 277 | } |
||
| 278 | $rate = $value; |
||
| 279 | } else { |
||
| 280 | $taxes = [0]; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | foreach ($taxes as $key => $tax) { |
||
| 285 | |||
| 286 | //All the da a attribute that is sent to the checkout Page if tax_compound=0 |
||
| 287 | if ($taxes[0]) { |
||
| 288 | $tax_attribute[$key] = ['name' => $tax->name, 'c_gst'=>$c_gst, 's_gst'=>$s_gst, 'i_gst'=>$i_gst, 'ut_gst'=>$ut_gst, 'state'=>$state_code, 'origin_state'=>$origin_state, 'tax_enable'=>$tax_enable, 'rate'=>$value, 'status'=>$status]; |
||
| 289 | |||
| 290 | $taxCondition[0] = new \Darryldecode\Cart\CartCondition([ |
||
| 291 | |||
| 292 | 'name' => 'no compound', |
||
| 293 | 'type' => 'tax', |
||
| 294 | 'target' => 'item', |
||
| 295 | 'value' => $value, |
||
| 296 | ]); |
||
| 297 | } else { |
||
| 298 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0]; |
||
| 299 | $taxCondition[0] = new \Darryldecode\Cart\CartCondition([ |
||
| 300 | 'name' => 'null', |
||
| 301 | 'type' => 'tax', |
||
| 302 | 'target' => 'item', |
||
| 303 | 'value' => '0%', |
||
| 304 | ]); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } elseif ($tax_enable == 0) {//If Tax enable is 0 |
||
| 308 | $status = 1; |
||
| 309 | if ($this->tax_option->findOrFail(1)->tax_enable == 0) { |
||
| 310 | $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first(); //In case of India when other tax is available and tax is not enabled |
||
| 311 | if ($taxClassId) { |
||
| 312 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 313 | $value = $this->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 314 | if ($value == 0) { |
||
| 315 | $status = 0; |
||
| 316 | } |
||
| 317 | $rate = $value; |
||
| 318 | foreach ($taxes as $key => $tax) { |
||
| 319 | $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $value, 'tax_enable'=>0, 'status' => $status]; |
||
| 320 | $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([ |
||
| 321 | |||
| 322 | 'name' => $tax->name, |
||
| 323 | 'type' => 'tax', |
||
| 324 | 'target' => 'item', |
||
| 325 | 'value' => $value, |
||
| 326 | ]); |
||
| 327 | } |
||
| 328 | } else {//In case of other country when tax is available and tax is not enabled(Applicable when Global Tax class for any country and state is not there) |
||
| 329 | $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first(); |
||
| 330 | if ($taxClassId) { //if state equals the user State |
||
| 331 | $taxes = $this->getTaxByPriority($taxClassId); |
||
| 332 | $value = $this->getValueForOthers($productid, $taxClassId, $taxes); |
||
| 333 | if ($value == '') { |
||
| 334 | $status = 0; |
||
| 335 | } |
||
| 336 | $rate = $value; |
||
| 337 | } |
||
| 338 | foreach ($taxes as $key => $tax) { |
||
| 339 | $tax_attribute[$key] = ['name' => $tax->name, 'rate' => $value, 'tax_enable'=>0, 'status' => $status]; |
||
| 340 | $taxCondition[$key] = new \Darryldecode\Cart\CartCondition([ |
||
| 341 | |||
| 342 | 'name' => $tax->name, |
||
| 343 | 'type' => 'tax', |
||
| 344 | 'target' => 'item', |
||
| 345 | 'value' => $value, |
||
| 346 | ]); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | } |
||
| 351 | } else { |
||
| 352 | $tax_attribute[0] = ['name' => 'null', 'rate' => 0, 'tax_enable' =>0]; |
||
| 353 | $taxCondition[0] = new \Darryldecode\Cart\CartCondition([ |
||
| 354 | 'name' => 'null', |
||
| 355 | 'type' => 'tax', |
||
| 356 | 'target' => 'item', |
||
| 357 | 'value' => '0%', |
||
| 358 | ]); |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | $currency_attribute = $this->addCurrencyAttributes($productid); |
||
| 363 | |||
| 364 | return ['conditions' => $taxCondition, 'attributes' => ['tax' => $tax_attribute, 'currency' => $currency_attribute]]; |
||
| 365 | } catch (\Exception $ex) { |
||
| 366 | Bugsnag::notifyException($ex); |
||
| 367 | |||
| 368 | throw new \Exception('Can not check the tax'); |
||
| 369 | } |
||
| 666 |