Completed
Push — development ( 26d9cf...abf04d )
by Ashutosh
09:30
created

BaseInvoiceController::getCodeValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
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\Order\Payment;
9
use App\Model\Payment\Promotion;
10
use App\Model\Payment\Tax;
11
use App\Model\Payment\TaxClass;
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 whenDateNotSet($start, $end)
95
    {
96
        //both not set, always true
97
        if (($start == null || $start == '0000-00-00 00:00:00') &&
98
         ($end == null || $end == '0000-00-00 00:00:00')) {
99
            return 'success';
100
        }
101
    }
102
103
    public function whenStartDateSet($start, $end, $now)
104
    {
105
        //only starting date set, check the date is less or equel to today
106
        if (($start != null || $start != '0000-00-00 00:00:00')
107
         && ($end == null || $end == '0000-00-00 00:00:00')) {
108
            if ($start <= $now) {
109
                return 'success';
110
            }
111
        }
112
    }
113
114
    public function whenEndDateSet($start, $end, $now)
115
    {
116
        //only ending date set, check the date is greater or equel to today
117
        if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) {
118
            if ($end >= $now) {
119
                return 'success';
120
            }
121
        }
122
    }
123
124
    public function whenBothSet($start, $end, $now)
125
    {
126
        //both set
127
        if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) {
128
            if ($end >= $now && $start <= $now) {
129
                return 'success';
130
            }
131
        }
132
    }
133
134
    public function postPayment($invoiceid, Request $request)
135
    {
136
        $this->validate($request, [
137
            'payment_method' => 'required',
138
            'amount'         => 'required|numeric',
139
            'payment_date'   => 'required|date_format:Y-m-d',
140
        ]);
141
142
        try {
143
            $payment_method = $request->input('payment_method');
144
            $payment_status = 'success';
145
            $payment_date = $request->input('payment_date');
146
            $amount = $request->input('amount');
147
            $payment = $this->updateInvoicePayment(
148
                $invoiceid,
149
                $payment_method,
150
                $payment_status,
151
                $payment_date,
152
                $amount
153
            );
154
155
            return redirect()->back()->with('success', 'Payment Accepted Successfully');
156
        } catch (\Exception $ex) {
157
            Bugsnag::notifyException($ex);
158
159
            return redirect()->back()->with('fails', $ex->getMessage());
160
        }
161
    }
162
163
    public function domain($id)
164
    {
165
        try {
166
            if (\Session::has('domain'.$id)) {
167
                $domain = \Session::get('domain'.$id);
168
            } else {
169
                $domain = '';
170
            }
171
172
            return $domain;
173
        } catch (\Exception $ex) {
174
            Bugsnag::notifyException($ex);
175
        }
176
    }
177
178
    public function checkExpiry($code = '')
179
    {
180
        try {
181
            if ($code != '') {
182
                $promotion = Promotion::where('code', $code)->first();
183
184
                $start = $promotion->start;
185
                $end = $promotion->expiry;
186
                $now = \Carbon\Carbon::now();
187
                $getExpiryStatus = $this->getExpiryStatus($start, $end, $now);
188
189
                return $getExpiryStatus;
190
            }
191
        } catch (\Exception $ex) {
192
            throw new \Exception(\Lang::get('message.check-expiry'));
193
        }
194
    }
195
196
    /*
197
    *Edit Invoice Total.
198
    */
199
    public function invoiceTotalChange(Request $request)
200
    {
201
        $total = $request->input('total');
202
        if ($total == '') {
203
            $total = 0;
204
        }
205
        $number = $request->input('number');
206
        $invoiceId = Invoice::where('number', $number)->value('id');
207
        $invoiceItem = InvoiceItem::where('invoice_id', $invoiceId)->update(['subtotal'=>$total]);
208
        $invoices = Invoice::where('number', $number)->update(['grand_total'=>$total]);
209
    }
210
211
    public function getCodeValue($promo, $code)
212
    {
213
        if ($promo && $code) {
214
            return $promo->value;
215
        }
216
        return ;
217
    }
218
}
219