Completed
Branch feature-dynamic-fields (3b03cc)
by Ashutosh
09:05
created

TaxRatesAndCodeExpiryController::getGrandTotal()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 5
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\Order;
8
use App\Model\Order\Payment;
9
use App\Model\Payment\Currency;
10
use App\Model\Payment\Promotion;
11
use App\Model\Payment\Tax;
12
use App\Model\Payment\TaxOption;
13
use App\User;
14
use Bugsnag;
15
16
class TaxRatesAndCodeExpiryController extends BaseInvoiceController
17
{
18
    /**
19
     * Get tax when enabled.
20
     */
21
    public function getTaxWhenEnable($productid, $taxs, $userid)
22
    {
23
        $rate = $this->getRate($productid, $taxs, $userid);
24
        $taxs = ([$rate['taxs']['0']['name'], $rate['taxs']['0']['rate']]);
25
26
        return $taxs;
27
    }
28
29
    /**
30
     * GeT Total Rate.
31
     */
32
    public function getTotalRate($taxClassId, $productid, $taxs)
33
    {
34
        $cartController = new CartController();
35
        $taxs = $cartController->getTaxByPriority($taxClassId);
36
        $value = $cartController->getValueForOthers($productid, $taxClassId, $taxs);
37
        if ($value == 0) {
38
            $status = 0;
39
        }
40
        $rate = $value;
41
42
        return ['rate'=>$rate, 'taxes'=>$taxs];
43
    }
44
45
    /**
46
     * Get Grandtotal.
47
     **/
48
    public function getGrandTotal($code, $total, $cost, $productid, $currency)
49
    {
50
        if ($code) {
51
            $grand_total = $this->checkCode($code, $productid, $currency);
52
        } else {
53
            if (!$total) {
54
                $grand_total = $cost;
55
            } else {
56
                $grand_total = $total;
57
            }
58
        }
59
60
        return $grand_total;
61
    }
62
63
    /**
64
     * Get Message on Invoice Generation.
65
     **/
66
    public function getMessage($items, $user_id)
67
    {
68
        if ($items) {
69
            $this->sendmailClientAgent($user_id, $items->invoice_id);
70
            $result = ['success' => \Lang::get('message.invoice-generated-successfully')];
71
        } else {
72
            $result = ['fails' => \Lang::get('message.can-not-generate-invoice')];
73
        }
74
75
        return $result;
76
    }
77
78
    /**
79
     * get Subtotal.
80
     */
81
    public function getSubtotal($user_currency, $cart)
82
    {
83
        if ($user_currency == 'INR') {
84
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
85
        } else {
86
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
87
        }
88
89
        return $subtotal;
90
    }
91
92
    public function calculateTotal($rate, $total)
93
    {
94
        try {
95
            $total = intval($total);
96
            $rates = explode(',', $rate);
97
            $rule = new TaxOption();
98
            $rule = $rule->findOrFail(1);
99
            if ($rule->inclusive == 0) {
100
                foreach ($rates as $rate1) {
101
                    if ($rate1 != '') {
102
                        $rateTotal = str_replace('%', '', $rate1);
103
                        $total += $total * ($rateTotal / 100);
104
                    }
105
                }
106
            }
107
108
            return intval(round($total));
109
        } catch (\Exception $ex) {
110
            app('log')->warning($ex->getMessage());
111
            Bugsnag::notifyException($ex);
112
113
            throw new \Exception($ex->getMessage());
114
        }
115
    }
116
117
    public function checkExecution($invoiceid)
118
    {
119
        try {
120
            $response = false;
121
            $invoice = Invoice::find($invoiceid);
122
123
            $order = Order::where('invoice_id', $invoiceid);
124
            $order_invoice_relation = $invoice->orderRelation()->first();
125
126
            if ($order_invoice_relation) {
127
                $response = true;
128
            } elseif ($order->get()->count() > 0) {
129
                $response = true;
130
            }
131
132
            return $response;
133
        } catch (\Exception $e) {
134
            Bugsnag::notifyException($e);
135
136
            return redirect()->back()->with('fails', $e->getMessage());
137
        }
138
    }
139
140
    public function invoiceContent($invoiceid)
141
    {
142
        $invoice = $this->invoice->find($invoiceid);
143
        $items = $invoice->invoiceItem()->get();
144
        $content = '';
145
        if ($items->count() > 0) {
146
            foreach ($items as $item) {
147
                $content .= '<tr>'.
148
                        '<td style="border-bottom: 1px solid#ccc; color: #333; 
149
                        font-family: Arial,sans-serif; font-size: 14px; line-height: 20px;
150
                         padding: 15px 8px;" valign="top">'.$invoice->number.'</td>'.
151
                        '<td style="border-bottom: 1px solid#ccc; color: #333; 
152
                        font-family: Arial,sans-serif; font-size: 14px; line-height: 20px;
153
                         padding: 15px 8px;" valign="top">'.$item->product_name.'</td>'.
154
                        '<td style="border-bottom: 1px solid#ccc; color: #333; 
155
                        font-family: Arial,sans-serif; font-size: 14px; line-height: 20px;
156
                         padding: 15px 8px;" valign="top">'.$this->currency($invoiceid).' '
157
                         .$item->subtotal.'</td>'.
158
                        '</tr>';
159
            }
160
        }
161
162
        return $content;
163
    }
164
165
    public function sendInvoiceMail($userid, $number, $total, $invoiceid)
166
    {
167
168
        //user
169
        $users = new User();
170
        $user = $users->find($userid);
171
        //check in the settings
172
        $settings = new \App\Model\Common\Setting();
173
        $setting = $settings->where('id', 1)->first();
174
        $invoiceurl = $this->invoiceUrl($invoiceid);
175
        //template
176
        $templates = new \App\Model\Common\Template();
177
        $temp_id = $setting->invoice;
178
        $template = $templates->where('id', $temp_id)->first();
179
        $from = $setting->email;
180
        $to = $user->email;
181
        $subject = $template->name;
182
        $data = $template->data;
183
        $replace = [
184
            'name'       => $user->first_name.' '.$user->last_name,
185
            'number'     => $number,
186
            'address'    => $user->address,
187
            'invoiceurl' => $invoiceurl,
188
            'content'    => $this->invoiceContent($invoiceid),
189
            'currency'   => $this->currency($invoiceid),
190
        ];
191
        $type = '';
192
        if ($template) {
193
            $type_id = $template->type;
194
            $temp_type = new \App\Model\Common\TemplateType();
195
            $type = $temp_type->where('id', $type_id)->first()->name;
196
        }
197
        //dd($type);
198
        $templateController = new \App\Http\Controllers\Common\TemplateController();
199
        $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
200
201
        return $mail;
202
    }
203
204
    public function invoiceUrl($invoiceid)
205
    {
206
        $url = url('my-invoice/'.$invoiceid);
207
208
        return $url;
209
    }
210
211
    public function paymentDeleleById($id)
212
    {
213
        try {
214
            $invoice_no = '';
215
            $payment = Payment::find($id);
216
            if ($payment) {
217
                $invoice_id = $payment->invoice_id;
218
                $invoice = Invoice::find($invoice_id);
219
                if ($invoice) {
220
                    $invoice_no = $invoice->number;
221
                }
222
                $payment->delete();
223
            } else {
224
                return redirect()->back()->with('fails', 'Can not delete');
225
            }
226
227
            return redirect()->back()->with('success', "Payment for invoice no: $invoice_no has Deleted Successfully");
228
        } catch (\Exception $e) {
229
            Bugsnag::notifyException($e);
230
231
            return redirect()->back()->with('fails', $e->getMessage());
232
        }
233
    }
234
235
    public function paymentEditById($id)
236
    {
237
        try {
238
            $cltCont = new \App\Http\Controllers\User\ClientController();
239
            $amountReceived = $cltCont->getAmountPaid($id);
240
            $payment = Payment::find($id);
241
            $clientid = $payment->user_id;
242
            $invoice = new Invoice();
243
            $order = new Order();
244
            $invoices = $invoice->where('user_id', $clientid)->where('status', '=', 'pending')
245
            ->orderBy('created_at', 'desc')->get();
246
            $cltCont = new \App\Http\Controllers\User\ClientController();
247
            $invoiceSum = $cltCont->getTotalInvoice($invoices);
248
            $amountReceived = $cltCont->getExtraAmt($clientid);
249
            $pendingAmount = $invoiceSum - $amountReceived;
250
            $client = $this->user->where('id', $clientid)->first();
251
            $currency = $client->currency;
252
            $symbol = Currency::where('code', $currency)->pluck('symbol')->first();
253
            $orders = $order->where('client', $clientid)->get();
254
255
            return view('themes.default1.invoice.editPayment',
256
                compact('amountReceived','clientid', 'client', 'invoices',  'orders',
257
                  'invoiceSum', 'amountReceived', 'pendingAmount', 'currency', 'symbol'));
258
        } catch (\Exception $e) {
259
            Bugsnag::notifyException($e);
260
261
            return redirect()->back()->with('fails', $e->getMessage());
262
        }
263
    }
264
265
    public function deleleById($id)
266
    {
267
        try {
268
            $invoice = Invoice::find($id);
269
            if ($invoice) {
270
                $invoice->delete();
271
            } else {
272
                return redirect()->back()->with('fails', 'Can not delete');
273
            }
274
275
            return redirect()->back()->with('success', "Invoice $invoice->number has been Deleted Successfully");
276
        } catch (\Exception $e) {
277
            Bugsnag::notifyException($e);
278
279
            return redirect()->back()->with('fails', $e->getMessage());
280
        }
281
    }
282
283
    public function getPromotionDetails($code)
284
    {
285
        $promo = Promotion::where('code', $code)->first();
286
        //check promotion code is valid
287
        if (!$promo) {
288
            throw new \Exception(\Lang::get('message.no-such-code'));
289
        }
290
        $relation = $promo->relation()->get();
291
        //check the relation between code and product
292
        if (count($relation) == 0) {
293
            throw new \Exception(\Lang::get('message.no-product-related-to-this-code'));
294
        }
295
        //check the usess
296
        $cont = new \App\Http\Controllers\Payment\PromotionController();
297
        $uses = $cont->checkNumberOfUses($code);
298
        //dd($uses);
299
        if ($uses != 'success') {
300
            throw new \Exception(\Lang::get('message.usage-of-code-completed'));
301
        }
302
        //check for the expiry date
303
        $expiry = $this->checkExpiry($code);
304
        if ($expiry != 'success') {
305
            throw new \Exception(\Lang::get('message.usage-of-code-expired'));
306
        }
307
308
        return $promo;
309
    }
310
}
311