Completed
Push — development ( b3ab84...f1d660 )
by Ashutosh
22:33 queued 09:02
created

BaseInvoiceController   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 99
dl 0
loc 177
rs 8.8798
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A whenEndDateSet() 0 6 6
A whenStartDateSet() 0 7 6
B whenBothSet() 0 6 7
B getTaxWhenState() 0 41 9
A whenDateNotSet() 0 6 5
A checkExpiry() 0 15 3
A domain() 0 12 3
A getTaxWhenOtherCountry() 0 27 3
A postPayment() 0 21 2

How to fix   Complexity   

Complex Class

Complex classes like BaseInvoiceController 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 BaseInvoiceController, 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\Order\Invoice;
8
use App\Model\Payment\Plan;
9
use App\Model\Payment\Promotion;
10
use App\Model\Payment\Tax;
11
use App\Model\Payment\TaxClass;
12
use App\Model\Product\Product;
13
use App\User;
14
use Bugsnag;
15
use Illuminate\Http\Request;
16
17
class BaseInvoiceController extends Controller
18
{
19
20
    /**
21
     *Tax When state is not empty.
22
     */
23
    public function getTaxWhenState($user_state, $productid, $origin_state)
24
    {
25
        $taxes = [];
26
        $value = [];
27
        $cartController = new CartController();
28
        $c_gst = $user_state->c_gst;
29
        $s_gst = $user_state->s_gst;
30
        $i_gst = $user_state->i_gst;
31
        $ut_gst = $user_state->ut_gst;
32
        $state_code = $user_state->state_code;
33
        if ($state_code == $origin_state) {//If user and origin state are same
34
            $taxClassId = TaxClass::where('name', 'Intra State GST')
35
             ->pluck('id')->toArray(); //Get the class Id  of state
36
            if ($taxClassId) {
37
                $taxes = $cartController->getTaxByPriority($taxClassId);
38
                $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes);
39
            } else {
40
                $taxes = [0];
41
            }
42
        } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state
43
44
            $taxClassId = TaxClass::where('name', 'Inter State GST')
45
            ->pluck('id')->toArray(); //Get the class Id  of state
46
            if ($taxClassId) {
47
                $taxes = $cartController->getTaxByPriority($taxClassId);
48
                $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes);
49
            } else {
50
                $taxes = [0];
51
            }
52
        } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory
53
            $taxClassId = TaxClass::where('name', 'Union Territory GST')
54
        ->pluck('id')->toArray(); //Get the class Id  of state
55
            if ($taxClassId) {
56
                $taxes = $cartController->getTaxByPriority($taxClassId);
57
                $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes);
58
            } else {
59
                $taxes = [0];
60
            }
61
        }
62
63
        return ['taxes'=>$taxes, 'value'=>$value];
64
    }
65
66
    /**
67
     *Tax When from other Country.
68
     */
69
    public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid)
70
    {
71
        $cartController = new CartController();
72
        $taxClassId = Tax::where('state', $geoip_state)
73
        ->orWhere('country', $geoip_country)
74
        ->pluck('tax_classes_id')->first();
75
        $value = '';
76
        $rate = '';
77
        if ($taxClassId) { //if state equals the user State
78
79
            $taxes = $cartController->getTaxByPriority($taxClassId);
80
81
            // $taxes = $this->cartController::getTaxByPriority($taxClassId);
82
            $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
83
            $rate = $value;
84
        } else {//if Tax is selected for Any State Any Country
85
            $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first();
86
            if ($taxClassId) {
87
                $taxes = $cartController->getTaxByPriority($taxClassId);
88
                $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
89
                $rate = $value;
90
            } else {
91
                $taxes = [0];
92
            }
93
        }
94
95
        return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate];
96
    }
97
98
    public function whenDateNotSet($start, $end)
99
    {
100
        //both not set, always true
101
        if (($start == null || $start == '0000-00-00 00:00:00') &&
102
         ($end == null || $end == '0000-00-00 00:00:00')) {
103
            return 'success';
104
        }
105
    }
106
107
    public function whenStartDateSet($start, $end, $now)
108
    {
109
        //only starting date set, check the date is less or equel to today
110
        if (($start != null || $start != '0000-00-00 00:00:00')
111
         && ($end == null || $end == '0000-00-00 00:00:00')) {
112
            if ($start <= $now) {
113
                return 'success';
114
            }
115
        }
116
    }
117
118
    public function whenEndDateSet($start, $end, $now)
119
    {
120
        //only ending date set, check the date is greater or equel to today
121
        if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) {
122
            if ($end >= $now) {
123
                return 'success';
124
            }
125
        }
126
    }
127
128
    public function whenBothSet($start, $end, $now)
129
    {
130
        //both set
131
        if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) {
132
            if ($end >= $now && $start <= $now) {
133
                return 'success';
134
            }
135
        }
136
    }
137
138
    public function postPayment($invoiceid, Request $request)
139
    {
140
        $this->validate($request, [
141
            'payment_method' => 'required',
142
            'amount'         => 'required|numeric',
143
            'payment_date'   => 'required|date_format:Y-m-d',
144
        ]);
145
146
        try {
147
            $payment_method = $request->input('payment_method');
148
            $payment_status = 'success';
149
            $payment_date = $request->input('payment_date');
150
            $amount = $request->input('amount');
151
            $payment = $this->updateInvoicePayment($invoiceid, $payment_method,
152
                $payment_status, $payment_date, $amount);
153
154
            return redirect()->back()->with('success', 'Payment Accepted Successfully');
155
        } catch (\Exception $ex) {
156
            Bugsnag::notifyException($ex);
157
158
            return redirect()->back()->with('fails', $ex->getMessage());
159
        }
160
    }
161
162
   
163
164
    public function domain($id)
165
    {
166
        try {
167
            if (\Session::has('domain'.$id)) {
168
                $domain = \Session::get('domain'.$id);
169
            } else {
170
                $domain = '';
171
            }
172
173
            return $domain;
174
        } catch (\Exception $ex) {
175
            Bugsnag::notifyException($ex);
176
        }
177
    }
178
179
    public function checkExpiry($code = '')
180
    {
181
        try {
182
            if ($code != '') {
183
                $promotion = Promotion::where('code', $code)->first();
184
185
                $start = $promotion->start;
186
                $end = $promotion->expiry;
187
                $now = \Carbon\Carbon::now();
188
                $getExpiryStatus = $this->getExpiryStatus($start, $end, $now);
189
190
                return $getExpiryStatus;
191
            }
192
        } catch (\Exception $ex) {
193
            throw new \Exception(\Lang::get('message.check-expiry'));
194
        }
195
    }
196
}
197