Completed
Push — development ( c0a597...4b3f17 )
by Ashutosh
11:26
created

TaxRatesAndCodeExpiryController   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 51
eloc 101
dl 0
loc 207
rs 7.92
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getTotalRate() 0 11 2
A getGrandTotal() 0 13 3
A getTaxWhenOtherCountry() 0 25 3
A calculateTotal() 0 20 5
A getMessage() 0 10 2
A getSubtotal() 0 9 2
A getTaxWhenEnable() 0 6 1
B getTaxWhenState() 0 36 9
A whenDateNotSet() 0 6 5
A whenStartDateSet() 0 7 6
A whenEndDateSet() 0 6 6
B whenBothSet() 0 6 7

How to fix   Complexity   

Complex Class

Complex classes like TaxRatesAndCodeExpiryController 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 TaxRatesAndCodeExpiryController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Controllers\Controller;
6
use App\Http\Controllers\Front\CartController;
7
use App\Model\Payment\Tax;
8
use App\Model\Payment\TaxClass;
9
use App\Model\Payment\TaxOption;
10
use Bugsnag;
11
12
class TaxRatesAndCodeExpiryController extends Controller
13
{
14
    /**
15
     *Tax When state is not empty.
16
     */
17
    public function getTaxWhenState($user_state, $productid, $origin_state)
18
    {
19
        $cartController = new CartController();
20
        $c_gst = $user_state->c_gst;
21
        $s_gst = $user_state->s_gst;
22
        $i_gst = $user_state->i_gst;
23
        $ut_gst = $user_state->ut_gst;
24
        $state_code = $user_state->state_code;
25
        if ($state_code == $origin_state) {//If user and origin state are same
26
             $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id  of state
27
               if ($taxClassId) {
28
                   $taxes = $cartController->getTaxByPriority($taxClassId);
29
                   $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes);
30
               } else {
31
                   $taxes = [0];
32
               }
33
        } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state
34
35
            $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id  of state
36
            if ($taxClassId) {
37
                $taxes = $cartController->getTaxByPriority($taxClassId);
38
                $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes);
39
            } else {
40
                $taxes = [0];
41
            }
42
        } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory
43
        $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id  of state
44
         if ($taxClassId) {
45
             $taxes = $cartController->getTaxByPriority($taxClassId);
46
             $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes);
47
         } else {
48
             $taxes = [0];
49
         }
50
        }
51
52
        return ['taxes'=>$taxes, 'value'=>$value];
53
    }
54
55
    /**
56
     *Tax When from other Country.
57
     */
58
    public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid)
59
    {
60
        $cartController = new CartController();
61
        $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first();
62
        $value = '';
63
        $rate = '';
64
        if ($taxClassId) { //if state equals the user State
65
66
            $taxes = $cartController->getTaxByPriority($taxClassId);
67
68
            // $taxes = $this->cartController::getTaxByPriority($taxClassId);
69
            $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
70
            $rate = $value;
71
        } else {//if Tax is selected for Any State Any Country
72
            $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first();
73
            if ($taxClassId) {
74
                $taxes = $cartController->getTaxByPriority($taxClassId);
75
                $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
76
                $rate = $value;
77
            } else {
78
                $taxes = [0];
79
            }
80
        }
81
82
        return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate];
83
    }
84
85
    /**
86
     * Get tax when enabled.
87
     */
88
    public function getTaxWhenEnable($productid, $taxs, $userid)
89
    {
90
        $rate = $this->getRate($productid, $taxs, $userid);
91
        $taxs = ([$rate['taxs']['0']['name'], $rate['taxs']['0']['rate']]);
92
93
        return $taxs;
94
    }
95
96
    /**
97
     * GeT Total Rate.
98
     */
99
    public function getTotalRate($taxClassId, $productid, $taxs)
100
    {
101
        $cartController = new CartController();
102
        $taxs = $cartController->getTaxByPriority($taxClassId);
103
        $value = $cartController->getValueForOthers($productid, $taxClassId, $taxs);
104
        if ($value == 0) {
105
            $status = 0;
106
        }
107
        $rate = $value;
108
109
        return ['rate'=>$rate, 'taxes'=>$taxs];
110
    }
111
112
    /**
113
     * Get Grandtotal.
114
     **/
115
    public function getGrandTotal($code, $total, $cost, $productid, $currency)
116
    {
117
        if ($code) {
118
            $grand_total = $this->checkCode($code, $productid, $currency);
119
        } else {
120
            if (!$total) {
121
                $grand_total = $cost;
122
            } else {
123
                $grand_total = $total;
124
            }
125
        }
126
127
        return $grand_total;
128
    }
129
130
    /**
131
     * Get Message on Invoice Generation.
132
     **/
133
    public function getMessage($items, $user_id)
134
    {
135
        if ($items) {
136
            $this->sendmailClientAgent($user_id, $items->invoice_id);
137
            $result = ['success' => \Lang::get('message.invoice-generated-successfully')];
138
        } else {
139
            $result = ['fails' => \Lang::get('message.can-not-generate-invoice')];
140
        }
141
142
        return $result;
143
    }
144
145
    /**
146
     * get Subtotal.
147
     */
148
    public function getSubtotal($user_currency, $cart)
149
    {
150
        if ($user_currency == 'INR') {
151
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
152
        } else {
153
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
154
        }
155
156
        return $subtotal;
157
    }
158
159
    public function calculateTotal($rate, $total)
160
    {
161
        try {
162
            $rates = explode(',', $rate);
163
            $rule = new TaxOption();
164
            $rule = $rule->findOrFail(1);
165
            if ($rule->inclusive == 0) {
166
                foreach ($rates as $rate) {
0 ignored issues
show
introduced by
$rate is overwriting one of the parameters of this function.
Loading history...
167
                    if ($rate != '') {
168
                        $rate = str_replace('%', '', $rate);
169
                        $total += $total * ($rate / 100);
170
                    }
171
                }
172
            }
173
174
            return intval(round($total));
175
        } catch (\Exception $ex) {
176
            Bugsnag::notifyException($ex);
177
178
            throw new \Exception($ex->getMessage());
179
        }
180
    }
181
182
183
    public function whenDateNotSet($start, $end)
184
    {
185
        //both not set, always true
186
        if (($start == null || $start == '0000-00-00 00:00:00') &&
187
         ($end == null || $end == '0000-00-00 00:00:00')) {
188
            return 'success';
189
        }
190
    }
191
192
    public function whenStartDateSet($start, $end, $now)
193
    {
194
        //only starting date set, check the date is less or equel to today
195
        if (($start != null || $start != '0000-00-00 00:00:00')
196
         && ($end == null || $end == '0000-00-00 00:00:00')) {
197
            if ($start <= $now) {
198
                return 'success';
199
            }
200
        }
201
    }
202
203
    public function whenEndDateSet($start, $end, $now)
204
    {
205
        //only ending date set, check the date is greater or equel to today
206
        if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) {
207
            if ($end >= $now) {
208
                return 'success';
209
            }
210
        }
211
    }
212
213
    public function whenBothSet($start, $end, $now)
214
    {
215
        //both set
216
        if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) {
217
            if ($end >= $now && $start <= $now) {
218
                return 'success';
219
            }
220
        }
221
    }
222
}
223