Completed
Push — development ( db1c54...00218f )
by Ashutosh
10:15
created

TaxRatesAndCodeExpiryController::checkExecution()   A

Complexity

Conditions 4
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
cc 4
nc 7
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\Model\Payment\TaxOption;
10
use App\Model\Order\Order;
11
use App\Model\Order\Invoice;
12
use App\User;
13
use Bugsnag;
14
15
class TaxRatesAndCodeExpiryController extends Controller
16
{
17
    /**
18
     *Tax When state is not empty.
19
     */
20
    public function getTaxWhenState($user_state, $productid, $origin_state)
21
    {
22
        $cartController = new CartController();
23
        $c_gst = $user_state->c_gst;
24
        $s_gst = $user_state->s_gst;
25
        $i_gst = $user_state->i_gst;
26
        $ut_gst = $user_state->ut_gst;
27
        $state_code = $user_state->state_code;
28
        if ($state_code == $origin_state) {//If user and origin state are same
29
             $taxClassId = TaxClass::where('name', 'Intra State GST')->pluck('id')->toArray(); //Get the class Id  of state
30
               if ($taxClassId) {
31
                   $taxes = $cartController->getTaxByPriority($taxClassId);
32
                   $value = $cartController->getValueForSameState($productid, $c_gst, $s_gst, $taxClassId, $taxes);
33
               } else {
34
                   $taxes = [0];
35
               }
36
        } elseif ($state_code != $origin_state && $ut_gst == 'NULL') {//If user is from other state
37
38
            $taxClassId = TaxClass::where('name', 'Inter State GST')->pluck('id')->toArray(); //Get the class Id  of state
39
            if ($taxClassId) {
40
                $taxes = $cartController->getTaxByPriority($taxClassId);
41
                $value = $cartController->getValueForOtherState($productid, $i_gst, $taxClassId, $taxes);
42
            } else {
43
                $taxes = [0];
44
            }
45
        } elseif ($state_code != $origin_state && $ut_gst != 'NULL') {//if user from Union Territory
46
        $taxClassId = TaxClass::where('name', 'Union Territory GST')->pluck('id')->toArray(); //Get the class Id  of state
47
         if ($taxClassId) {
48
             $taxes = $cartController->getTaxByPriority($taxClassId);
49
             $value = $cartController->getValueForUnionTerritory($productid, $c_gst, $ut_gst, $taxClassId, $taxes);
50
         } else {
51
             $taxes = [0];
52
         }
53
        }
54
55
        return ['taxes'=>$taxes, 'value'=>$value];
56
    }
57
58
    /**
59
     *Tax When from other Country.
60
     */
61
    public function getTaxWhenOtherCountry($geoip_state, $geoip_country, $productid)
62
    {
63
        $cartController = new CartController();
64
        $taxClassId = Tax::where('state', $geoip_state)->orWhere('country', $geoip_country)->pluck('tax_classes_id')->first();
65
        $value = '';
66
        $rate = '';
67
        if ($taxClassId) { //if state equals the user State
68
69
            $taxes = $cartController->getTaxByPriority($taxClassId);
70
71
            // $taxes = $this->cartController::getTaxByPriority($taxClassId);
72
            $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
73
            $rate = $value;
74
        } else {//if Tax is selected for Any State Any Country
75
            $taxClassId = Tax::where('country', '')->where('state', 'Any State')->pluck('tax_classes_id')->first();
76
            if ($taxClassId) {
77
                $taxes = $cartController->getTaxByPriority($taxClassId);
78
                $value = $cartController->getValueForOthers($productid, $taxClassId, $taxes);
79
                $rate = $value;
80
            } else {
81
                $taxes = [0];
82
            }
83
        }
84
85
        return ['taxes'=>$taxes, 'value'=>$value, 'rate'=>$rate];
86
    }
87
88
    /**
89
     * Get tax when enabled.
90
     */
91
    public function getTaxWhenEnable($productid, $taxs, $userid)
92
    {
93
        $rate = $this->getRate($productid, $taxs, $userid);
94
        $taxs = ([$rate['taxs']['0']['name'], $rate['taxs']['0']['rate']]);
95
96
        return $taxs;
97
    }
98
99
    /**
100
     * GeT Total Rate.
101
     */
102
    public function getTotalRate($taxClassId, $productid, $taxs)
103
    {
104
        $cartController = new CartController();
105
        $taxs = $cartController->getTaxByPriority($taxClassId);
106
        $value = $cartController->getValueForOthers($productid, $taxClassId, $taxs);
107
        if ($value == 0) {
108
            $status = 0;
109
        }
110
        $rate = $value;
111
112
        return ['rate'=>$rate, 'taxes'=>$taxs];
113
    }
114
115
    /**
116
     * Get Grandtotal.
117
     **/
118
    public function getGrandTotal($code, $total, $cost, $productid, $currency)
119
    {
120
        if ($code) {
121
            $grand_total = $this->checkCode($code, $productid, $currency);
122
        } else {
123
            if (!$total) {
124
                $grand_total = $cost;
125
            } else {
126
                $grand_total = $total;
127
            }
128
        }
129
130
        return $grand_total;
131
    }
132
133
    /**
134
     * Get Message on Invoice Generation.
135
     **/
136
    public function getMessage($items, $user_id)
137
    {
138
        if ($items) {
139
            $this->sendmailClientAgent($user_id, $items->invoice_id);
140
            $result = ['success' => \Lang::get('message.invoice-generated-successfully')];
141
        } else {
142
            $result = ['fails' => \Lang::get('message.can-not-generate-invoice')];
143
        }
144
145
        return $result;
146
    }
147
148
    /**
149
     * get Subtotal.
150
     */
151
    public function getSubtotal($user_currency, $cart)
152
    {
153
        if ($user_currency == 'INR') {
154
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
155
        } else {
156
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
157
        }
158
159
        return $subtotal;
160
    }
161
162
    public function calculateTotal($rate, $total)
163
    {
164
        try {
165
            $rates = explode(',', $rate);
166
            $rule = new TaxOption();
167
            $rule = $rule->findOrFail(1);
168
            if ($rule->inclusive == 0) {
169
                foreach ($rates as $rate1) {
170
                    if ($rate1 != '') {
171
                        $rateTotal = str_replace('%', '', $rate1);
172
                        $total += $total * ($rateTotal / 100);
173
                    }
174
                }
175
            }
176
177
            return intval(round($total));
178
        } catch (\Exception $ex) {
179
            Bugsnag::notifyException($ex);
180
181
            throw new \Exception($ex->getMessage());
182
        }
183
    }
184
185
    public function whenDateNotSet($start, $end)
186
    {
187
        //both not set, always true
188
        if (($start == null || $start == '0000-00-00 00:00:00') &&
189
         ($end == null || $end == '0000-00-00 00:00:00')) {
190
            return 'success';
191
        }
192
    }
193
194
    public function whenStartDateSet($start, $end, $now)
195
    {
196
        //only starting date set, check the date is less or equel to today
197
        if (($start != null || $start != '0000-00-00 00:00:00')
198
         && ($end == null || $end == '0000-00-00 00:00:00')) {
199
            if ($start <= $now) {
200
                return 'success';
201
            }
202
        }
203
    }
204
205
    public function whenEndDateSet($start, $end, $now)
206
    {
207
        //only ending date set, check the date is greater or equel to today
208
        if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) {
209
            if ($end >= $now) {
210
                return 'success';
211
            }
212
        }
213
    }
214
215
    public function whenBothSet($start, $end, $now)
216
    {
217
        //both set
218
        if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) {
219
            if ($end >= $now && $start <= $now) {
220
                return 'success';
221
            }
222
        }
223
    }
224
225
    public function getPrice($price, $price_model)
226
    {
227
        if ($price == '') {
228
            $price = $price_model->sales_price;
229
            if (!$price) {
230
                $price = $price_model->price;
231
            }
232
        }
233
234
        return $price;
235
    }
236
237
    public function checkExecution($invoiceid)
238
    {
239
        try {
240
            $response = false;
241
            $invoice = Invoice::find($invoiceid);
242
            // dd($invoice);
243
            $order = Order::where('invoice_id', $invoiceid);
244
            // dd($order);
245
            $order_invoice_relation = $invoice->orderRelation()->first();
246
            if ($order_invoice_relation) {
247
                $response = true;
248
            } elseif ($order->get()->count() > 0) {
249
                $response = true;
250
            }
251
252
            return $response;
253
        } catch (\Exception $e) {
254
            Bugsnag::notifyException($e);
255
256
            return redirect()->back()->with('fails', $e->getMessage());
257
        }
258
    }
259
260
    public function invoiceContent($invoiceid)
261
    {
262
        $invoice = $this->invoice->find($invoiceid);
263
        $items = $invoice->invoiceItem()->get();
264
        $content = '';
265
        if ($items->count() > 0) {
266
            foreach ($items as $item) {
267
                $content .= '<tr>'.
268
                        '<td style="border-bottom: 1px solid#ccc; color: #333; font-family: Arial,sans-serif; font-size: 14px; line-height: 20px; padding: 15px 8px;" valign="top">'.$invoice->number.'</td>'.
269
                        '<td style="border-bottom: 1px solid#ccc; color: #333; font-family: Arial,sans-serif; font-size: 14px; line-height: 20px; padding: 15px 8px;" valign="top">'.$item->product_name.'</td>'.
270
                        '<td style="border-bottom: 1px solid#ccc; color: #333; font-family: Arial,sans-serif; font-size: 14px; line-height: 20px; padding: 15px 8px;" valign="top">'.$this->currency($invoiceid).' '.$item->subtotal.'</td>'.
271
                        '</tr>';
272
            }
273
        }
274
275
        return $content;
276
    }
277
278
    public function sendInvoiceMail($userid, $number, $total, $invoiceid)
279
    {
280
281
        //user
282
        $users = new User();
283
        $user = $users->find($userid);
284
        //check in the settings
285
        $settings = new \App\Model\Common\Setting();
286
        $setting = $settings->where('id', 1)->first();
287
        $invoiceurl = $this->invoiceUrl($invoiceid);
288
        //template
289
        $templates = new \App\Model\Common\Template();
290
        $temp_id = $setting->invoice;
291
        $template = $templates->where('id', $temp_id)->first();
292
        $from = $setting->email;
293
        $to = $user->email;
294
        $subject = $template->name;
295
        $data = $template->data;
296
        $replace = [
297
            'name'       => $user->first_name.' '.$user->last_name,
298
            'number'     => $number,
299
            'address'    => $user->address,
300
            'invoiceurl' => $invoiceurl,
301
            'content'    => $this->invoiceContent($invoiceid),
302
            'currency'   => $this->currency($invoiceid),
303
        ];
304
        $type = '';
305
        if ($template) {
306
            $type_id = $template->type;
307
            $temp_type = new \App\Model\Common\TemplateType();
308
            $type = $temp_type->where('id', $type_id)->first()->name;
309
        }
310
        //dd($type);
311
        $templateController = new \App\Http\Controllers\Common\TemplateController();
312
        $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
313
314
        return $mail;
315
    }
316
317
    
318
    public function invoiceUrl($invoiceid)
319
    {
320
        $url = url('my-invoice/'.$invoiceid);
321
322
        return $url;
323
    }
324
325
326
}
327