BaseInvoiceController::getCodeFromSession()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Order;
4
5
use App\Http\Controllers\Front\CartController;
6
use App\Model\Order\Invoice;
7
use App\Model\Order\InvoiceItem;
8
use App\Model\Payment\Promotion;
9
use App\Model\Payment\Tax;
10
use App\Model\Payment\TaxClass;
11
use App\Model\Payment\TaxOption;
12
use App\User;
13
use Bugsnag;
14
use Illuminate\Http\Request;
15
16
class BaseInvoiceController extends ExtendedBaseInvoiceController
17
{
18
    /**
19
     *Tax When state is not empty.
20
     */
21
    public function getTaxWhenState($user_state, $productid, $origin_state)
22
    {
23
        $taxes = [];
24
        $value = [];
25
        $cartController = new CartController();
26
        $c_gst = $user_state->c_gst;
27
        $s_gst = $user_state->s_gst;
28
        $i_gst = $user_state->i_gst;
29
        $ut_gst = $user_state->ut_gst;
30
        $state_code = $user_state->state_code;
31
        if ($state_code == $origin_state) {//If user and origin state are same
32
            $taxClassId = TaxClass::where('name', 'Intra State GST')
33
             ->pluck('id')->toArray(); //Get the class Id  of state
34
            if ($taxClassId) {
35
                $taxes = $cartController->getTaxByPriority($taxClassId);
36
                $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes);
37
            } else {
38
                $taxes = [0];
39
            }
40
        } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state
41
            $taxClassId = TaxClass::where('name', 'Inter State GST')
42
            ->pluck('id')->toArray(); //Get the class Id  of state
43
            if ($taxClassId) {
44
                $taxes = $cartController->getTaxByPriority($taxClassId);
45
                $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes);
46
            } else {
47
                $taxes = [0];
48
            }
49
        } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory
50
            $taxClassId = TaxClass::where('name', 'Union Territory GST')
51
            ->pluck('id')->toArray(); //Get the class Id  of state
52
            if ($taxClassId) {
53
                $taxes = $cartController->getTaxByPriority($taxClassId);
54
                $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes);
55
            } else {
56
                $taxes = [0];
57
            }
58
        }
59
60
        return ['taxes'=>$taxes, 'value'=>$value];
61
    }
62
63
    /**
64
     *Tax When from other Country.
65
     */
66
    public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid)
67
    {
68
        $cartController = new CartController();
69
        $taxClassId = Tax::where('state', $geoip_state)
70
        ->orWhere('country', $geoip_country)
71
        ->pluck('tax_classes_id')->first();
72
        $value = '';
73
        $rate = '';
74
        if ($taxClassId) { //if state equals the user State
75
            $taxes = $cartController->getTaxByPriority($taxClassId);
76
77
            // $taxes = $this->cartController::getTaxByPriority($taxClassId);
78
            $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
79
            $rate = $value;
80
        } else {//if Tax is selected for Any State Any Country
81
            $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first();
82
            if ($taxClassId) {
83
                $taxes = $cartController->getTaxByPriority($taxClassId);
84
                $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
85
                $rate = $value;
86
            } else {
87
                $taxes = [0];
88
            }
89
        }
90
91
        return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate];
92
    }
93
94
    public function getExpiryStatus($start, $end, $now)
95
    {
96
        $whenDateNotSet = $this->whenDateNotSet($start, $end);
97
        if ($whenDateNotSet) {
98
            return $whenDateNotSet;
99
        }
100
        $whenStartDateSet = $this->whenStartDateSet($start, $end, $now);
101
        if ($whenStartDateSet) {
102
            return $whenStartDateSet;
103
        }
104
        $whenEndDateSet = $this->whenEndDateSet($start, $end, $now);
105
        if ($whenEndDateSet) {
106
            return $whenEndDateSet;
107
        }
108
        $whenBothAreSet = $this->whenBothSet($start, $end, $now);
109
        if ($whenBothAreSet) {
110
            return $whenBothAreSet;
111
        }
112
    }
113
114
    public function whenDateNotSet($start, $end)
115
    {
116
        //both not set, always true
117
        if (($start == null || $start == '0000-00-00 00:00:00') &&
118
         ($end == null || $end == '0000-00-00 00:00:00')) {
119
            return 'success';
120
        }
121
    }
122
123
    public function whenStartDateSet($start, $end, $now)
124
    {
125
        //only starting date set, check the date is less or equel to today
126
        if (($start != null || $start != '0000-00-00 00:00:00')
127
         && ($end == null || $end == '0000-00-00 00:00:00')) {
128
            if ($start <= $now) {
129
                return 'success';
130
            }
131
        }
132
    }
133
134
    public function whenEndDateSet($start, $end, $now)
135
    {
136
        //only ending date set, check the date is greater or equel to today
137
        if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) {
138
            if ($end >= $now) {
139
                return 'success';
140
            }
141
        }
142
    }
143
144
    public function whenBothSet($start, $end, $now)
145
    {
146
        //both set
147
        if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) {
148
            if ($end >= $now && $start <= $now) {
149
                return 'success';
150
            }
151
        }
152
    }
153
154
    public function postPayment($invoiceid, Request $request)
155
    {
156
        $this->validate($request, [
157
            'payment_method' => 'required',
158
            'amount'         => 'required|numeric',
159
            'payment_date'   => 'required|date_format:Y-m-d',
160
        ]);
161
162
        try {
163
            $payment_method = $request->input('payment_method');
164
            $payment_status = 'success';
165
            $payment_date = $request->input('payment_date');
166
            $amount = $request->input('amount');
167
            $payment = $this->updateInvoicePayment(
168
                $invoiceid,
169
                $payment_method,
170
                $payment_status,
171
                $payment_date,
172
                $amount
173
            );
174
175
            return redirect()->back()->with('success', 'Payment Accepted Successfully');
176
        } catch (\Exception $ex) {
177
            Bugsnag::notifyException($ex);
178
179
            return redirect()->back()->with('fails', $ex->getMessage());
180
        }
181
    }
182
183
    public function domain($id)
184
    {
185
        try {
186
            if (\Session::has('domain'.$id)) {
187
                $domain = \Session::get('domain'.$id);
188
            } else {
189
                $domain = '';
190
            }
191
192
            return $domain;
193
        } catch (\Exception $ex) {
194
            Bugsnag::notifyException($ex);
195
        }
196
    }
197
198
    public function checkExpiry($code = '')
199
    {
200
        try {
201
            if ($code != '') {
202
                $promotion = Promotion::where('code', $code)->first();
203
204
                $start = $promotion->start;
205
                $end = $promotion->expiry;
206
                $now = \Carbon\Carbon::now();
207
                $getExpiryStatus = $this->getExpiryStatus($start, $end, $now);
208
209
                return $getExpiryStatus;
210
            }
211
        } catch (\Exception $ex) {
212
            throw new \Exception(\Lang::get('message.check-expiry'));
213
        }
214
    }
215
216
    /*
217
    *Edit Invoice Total.
218
    */
219
    public function invoiceTotalChange(Request $request)
220
    {
221
        $total = $request->input('total');
222
        if ($total == '') {
223
            $total = 0;
224
        }
225
        $number = $request->input('number');
226
        $invoiceId = Invoice::where('number', $number)->value('id');
227
        $invoiceItem = InvoiceItem::where('invoice_id', $invoiceId)->update(['subtotal'=>$total]);
228
        $invoices = Invoice::where('number', $number)->update(['grand_total'=>$total]);
229
    }
230
231
    public function calculateTotal($rate, $total)
232
    {
233
        try {
234
            $total = intval($total);
235
            $rates = explode(',', $rate);
236
            $rule = new TaxOption();
237
            $rule = $rule->findOrFail(1);
238
            if ($rule->inclusive == 0) {
239
                foreach ($rates as $rate1) {
240
                    if ($rate1 != '') {
241
                        $rateTotal = str_replace('%', '', $rate1);
242
                        $total += $total * ($rateTotal / 100);
243
                    }
244
                }
245
            }
246
247
            return intval(round($total));
248
        } catch (\Exception $ex) {
249
            app('log')->warning($ex->getMessage());
250
            Bugsnag::notifyException($ex);
251
252
            throw new \Exception($ex->getMessage());
253
        }
254
    }
255
256
    /**
257
     * Check if Session has Code and Value of Code.
258
     *
259
     * @author Ashutosh Pathak <[email protected]>
260
     *
261
     * @date   2019-02-22T13:10:50+0530
262
     *
263
     * @return array
264
     */
265
    protected function getCodeFromSession()
266
    {
267
        $code = '';
268
        $codevalue = '';
269
        if (\Session::has('code')) {//If coupon code is applied get it here from Session
270
            $code = \Session::get('code');
271
            $codevalue = \Session::get('codevalue');
272
        }
273
274
        return ['code'=> $code, 'codevalue'=>$codevalue];
275
    }
276
}
277