Completed
Push — development ( 42348a...d8b245 )
by Ashutosh
10:20
created

BaseInvoiceController::whenStartDateSet()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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