Completed
Push — development ( 55bb16...2dbf65 )
by Ashutosh
09:53
created

BaseInvoiceController::pdf()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 27
rs 9.0444
c 0
b 0
f 0
cc 6
nc 16
nop 1
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\User;
10
use Bugsnag;
11
use Illuminate\Http\Request;
12
13
class BaseInvoiceController extends Controller
14
{
15
    /**
16
     *Tax When state is not empty.
17
     */
18
    public function getTaxWhenState($user_state, $productid, $origin_state)
19
    {
20
        $cartController = new CartController();
21
        $c_gst = $user_state->c_gst;
22
        $s_gst = $user_state->s_gst;
23
        $i_gst = $user_state->i_gst;
24
        $ut_gst = $user_state->ut_gst;
25
        $state_code = $user_state->state_code;
26
        if ($state_code == $origin_state) {//If user and origin state are same
27
             $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id  of state
28
               if ($taxClassId) {
29
                   $taxes = $cartController->getTaxByPriority($taxClassId);
30
                   $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes);
31
               } else {
32
                   $taxes = [0];
33
               }
34
        } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state
35
36
            $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id  of state
37
            if ($taxClassId) {
38
                $taxes = $cartController->getTaxByPriority($taxClassId);
39
                $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes);
40
            } else {
41
                $taxes = [0];
42
            }
43
        } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory
44
        $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id  of state
45
         if ($taxClassId) {
46
             $taxes = $cartController->getTaxByPriority($taxClassId);
47
             $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes);
48
         } else {
49
             $taxes = [0];
50
         }
51
        }
52
53
        return ['taxes'=>$taxes, 'value'=>$value];
54
    }
55
56
    /**
57
     *Tax When from other Country.
58
     */
59
    public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid)
60
    {
61
        $cartController = new CartController();
62
        $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first();
63
        $value = '';
64
        $rate = '';
65
        if ($taxClassId) { //if state equals the user State
66
67
            $taxes = $cartController->getTaxByPriority($taxClassId);
68
69
            // $taxes = $this->cartController::getTaxByPriority($taxClassId);
70
            $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
71
            $rate = $value;
72
        } else {//if Tax is selected for Any State Any Country
73
            $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first();
74
            if ($taxClassId) {
75
                $taxes = $cartController->getTaxByPriority($taxClassId);
76
                $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
77
                $rate = $value;
78
            } else {
79
                $taxes = [0];
80
            }
81
        }
82
83
        return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate];
84
    }
85
86
    public function getExpiryStatus($start, $end, $now)
87
    {
88
        $whenDateNotSet = $this->whenDateNotSet($start, $end);
89
        if ($whenDateNotSet) {
90
            return $whenDateNotSet;
91
        }
92
        $whenStartDateSet = $this->whenStartDateSet($start, $end, $now);
93
        if ($whenStartDateSet) {
94
            return $whenStartDateSet;
95
        }
96
        $whenEndDateSet = $this->whenEndDateSet($start, $end, $now);
97
        if ($whenEndDateSet) {
98
            return $whenEndDateSet;
99
        }
100
        $whenBothAreSet = $this->whenBothSet($start, $end, $now);
101
        if ($whenBothAreSet) {
102
            return $whenBothAreSet;
103
        }
104
    }
105
106
    public function whenDateNotSet($start, $end)
107
    {
108
        //both not set, always true
109
        if (($start == null || $start == '0000-00-00 00:00:00') &&
110
         ($end == null || $end == '0000-00-00 00:00:00')) {
111
            return 'success';
112
        }
113
    }
114
115
    public function whenStartDateSet($start, $end, $now)
116
    {
117
        //only starting date set, check the date is less or equel to today
118
        if (($start != null || $start != '0000-00-00 00:00:00')
119
         && ($end == null || $end == '0000-00-00 00:00:00')) {
120
            if ($start <= $now) {
121
                return 'success';
122
            }
123
        }
124
    }
125
126
    public function whenEndDateSet($start, $end, $now)
127
    {
128
        //only ending date set, check the date is greater or equel to today
129
        if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) {
130
            if ($end >= $now) {
131
                return 'success';
132
            }
133
        }
134
    }
135
136
    public function whenBothSet($start, $end, $now)
137
    {
138
        //both set
139
        if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) {
140
            if ($end >= $now && $start <= $now) {
141
                return 'success';
142
            }
143
        }
144
    }
145
146
    public function postPayment($invoiceid, Request $request)
147
    {
148
        $this->validate($request, [
149
            'payment_method' => 'required',
150
            'amount'         => 'required|numeric',
151
            'payment_date'   => 'required|date_format:Y-m-d',
152
        ]);
153
154
        try {
155
            $payment_method = $request->input('payment_method');
156
            $payment_status = 'success';
157
            $payment_date = $request->input('payment_date');
158
            $amount = $request->input('amount');
159
            $payment = $this->updateInvoicePayment($invoiceid, $payment_method, $payment_status, $payment_date, $amount);
160
161
            return redirect()->back()->with('success', 'Payment Accepted Successfully');
162
        } catch (\Exception $ex) {
163
            Bugsnag::notifyException($ex);
164
165
            return redirect()->back()->with('fails', $ex->getMessage());
166
        }
167
    }
168
169
170
    public function pdf(Request $request)
171
    {
172
        try {
173
            $id = $request->input('invoiceid');
174
            if (!$id) {
175
                return redirect()->back()->with('fails', \Lang::get('message.no-invoice-id'));
176
            }
177
            $invoice = $this->invoice->where('id', $id)->first();
178
            if (!$invoice) {
179
                return redirect()->back()->with('fails', \Lang::get('message.invalid-invoice-id'));
180
            }
181
            $invoiceItems = $this->invoiceItem->where('invoice_id', $id)->get();
182
            if ($invoiceItems->count() == 0) {
183
                return redirect()->back()->with('fails', \Lang::get('message.invalid-invoice-id'));
184
            }
185
            $user = $this->user->find($invoice->user_id);
186
            if (!$user) {
187
                return redirect()->back()->with('fails', 'No User');
188
            }
189
            $pdf = \PDF::loadView('themes.default1.invoice.newpdf', compact('invoiceItems', 'invoice', 'user'));
190
            // $pdf = \PDF::loadView('themes.default1.invoice.newpdf', compact('invoiceItems', 'invoice', 'user'));
191
192
            return $pdf->download($user->first_name.'-invoice.pdf');
193
        } catch (\Exception $ex) {
194
            Bugsnag::notifyException($ex);
195
196
            return redirect()->back()->with('fails', $ex->getMessage());
197
        }
198
    }
199
200
    public function setDomain($productid, $domain)
201
    {
202
        try {
203
            if (\Session::has('domain'.$productid)) {
204
                \Session::forget('domain'.$productid);
205
            }
206
            \Session::put('domain'.$productid, $domain);
207
        } catch (\Exception $ex) {
208
            Bugsnag::notifyException($ex);
209
210
            throw new \Exception($ex->getMessage());
211
        }
212
    }
213
214
    public function domain($id)
215
    {
216
        try {
217
            if (\Session::has('domain'.$id)) {
218
                $domain = \Session::get('domain'.$id);
219
            } else {
220
                $domain = '';
221
            }
222
223
            return $domain;
224
        } catch (\Exception $ex) {
225
            Bugsnag::notifyException($ex);
226
        }
227
    }
228
229
230
231
}
232