| Total Complexity | 54 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Complex classes like TaxCalculation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TaxCalculation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 |