Completed
Push — development ( 6f9f18...b1cef6 )
by Ashutosh
10:23
created

TaxRatesAndCodeExpiryController::paymentEditById()   A

Complexity

Conditions 2
Paths 13

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 28
rs 9.6
c 0
b 0
f 0
cc 2
nc 13
nop 1
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
    public function currency($invoiceid)
79
    {
80
        $invoice = Invoice::find($invoiceid);
81
        $currency_code = $invoice->currency;
82
        $cur = ' ';
83
        if ($invoice->grand_total == 0) {
84
            return $cur;
85
        }
86
        $currency = Currency::where('code', $currency_code)->first();
87
        if ($currency) {
88
            $cur = $currency->symbol;
89
            if (!$cur) {
90
                $cur = $currency->code;
91
            }
92
        }
93
94
        return $cur;
95
    }
96
97
    /**
98
     * get Subtotal.
99
     */
100
    public function getSubtotal($user_currency, $cart)
101
    {
102
        if ($user_currency == 'INR') {
103
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
104
        } else {
105
            $subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions());
106
        }
107
108
        return $subtotal;
109
    }
110
111
    public function calculateTotal($rate, $total)
112
    {
113
        try {
114
            $rates = explode(',', $rate);
115
            $rule = new TaxOption();
116
            $rule = $rule->findOrFail(1);
117
            if ($rule->inclusive == 0) {
118
                foreach ($rates as $rate1) {
119
                    if ($rate1 != '') {
120
                        $rateTotal = str_replace('%', '', $rate1);
121
                        $total += $total * ($rateTotal / 100);
122
                    }
123
                }
124
            }
125
126
            return intval(round($total));
127
        } catch (\Exception $ex) {
128
            Bugsnag::notifyException($ex);
129
130
            throw new \Exception($ex->getMessage());
131
        }
132
    }
133
134
    public function getPrice($price, $price_model)
135
    {
136
        if ($price == '') {
137
            $price = $price_model->sales_price;
138
            if (!$price) {
139
                $price = $price_model->price;
140
            }
141
        }
142
143
        return $price;
144
    }
145
146
    public function checkExecution($invoiceid)
147
    {
148
        try {
149
            $response = false;
150
            $invoice = Invoice::find($invoiceid);
151
152
            $order = Order::where('invoice_id', $invoiceid);
153
            $order_invoice_relation = $invoice->orderRelation()->first();
154
155
            if ($order_invoice_relation) {
156
                $response = true;
157
            } elseif ($order->get()->count() > 0) {
158
                $response = true;
159
            }
160
161
            return $response;
162
        } catch (\Exception $e) {
163
            Bugsnag::notifyException($e);
164
165
            return redirect()->back()->with('fails', $e->getMessage());
166
        }
167
    }
168
169
    public function invoiceContent($invoiceid)
170
    {
171
        $invoice = $this->invoice->find($invoiceid);
172
        $items = $invoice->invoiceItem()->get();
173
        $content = '';
174
        if ($items->count() > 0) {
175
            foreach ($items as $item) {
176
                $content .= '<tr>'.
177
                        '<td style="border-bottom: 1px solid#ccc; color: #333; 
178
                        font-family: Arial,sans-serif; font-size: 14px; line-height: 20px;
179
                         padding: 15px 8px;" valign="top">'.$invoice->number.'</td>'.
180
                        '<td style="border-bottom: 1px solid#ccc; color: #333; 
181
                        font-family: Arial,sans-serif; font-size: 14px; line-height: 20px;
182
                         padding: 15px 8px;" valign="top">'.$item->product_name.'</td>'.
183
                        '<td style="border-bottom: 1px solid#ccc; color: #333; 
184
                        font-family: Arial,sans-serif; font-size: 14px; line-height: 20px;
185
                         padding: 15px 8px;" valign="top">'.$this->currency($invoiceid).' '
186
                         .$item->subtotal.'</td>'.
187
                        '</tr>';
188
            }
189
        }
190
191
        return $content;
192
    }
193
194
    public function sendInvoiceMail($userid, $number, $total, $invoiceid)
195
    {
196
197
        //user
198
        $users = new User();
199
        $user = $users->find($userid);
200
        //check in the settings
201
        $settings = new \App\Model\Common\Setting();
202
        $setting = $settings->where('id', 1)->first();
203
        $invoiceurl = $this->invoiceUrl($invoiceid);
204
        //template
205
        $templates = new \App\Model\Common\Template();
206
        $temp_id = $setting->invoice;
207
        $template = $templates->where('id', $temp_id)->first();
208
        $from = $setting->email;
209
        $to = $user->email;
210
        $subject = $template->name;
211
        $data = $template->data;
212
        $replace = [
213
            'name'       => $user->first_name.' '.$user->last_name,
214
            'number'     => $number,
215
            'address'    => $user->address,
216
            'invoiceurl' => $invoiceurl,
217
            'content'    => $this->invoiceContent($invoiceid),
218
            'currency'   => $this->currency($invoiceid),
219
        ];
220
        $type = '';
221
        if ($template) {
222
            $type_id = $template->type;
223
            $temp_type = new \App\Model\Common\TemplateType();
224
            $type = $temp_type->where('id', $type_id)->first()->name;
225
        }
226
        //dd($type);
227
        $templateController = new \App\Http\Controllers\Common\TemplateController();
228
        $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
229
230
        return $mail;
231
    }
232
233
    public function invoiceUrl($invoiceid)
234
    {
235
        $url = url('my-invoice/'.$invoiceid);
236
237
        return $url;
238
    }
239
240
    public function paymentDeleleById($id)
241
    {
242
        try {
243
            $invoice_no = '';
244
            $payment = Payment::find($id);
245
            if ($payment) {
246
                $invoice_id = $payment->invoice_id;
247
                $invoice = Invoice::find($invoice_id);
248
                if ($invoice) {
249
                    $invoice_no = $invoice->number;
250
                }
251
                $payment->delete();
252
            } else {
253
                return redirect()->back()->with('fails', 'Can not delete');
254
            }
255
256
            return redirect()->back()->with('success', "Payment for invoice no: $invoice_no has Deleted Successfully");
257
        } catch (\Exception $e) {
258
            Bugsnag::notifyException($e);
259
260
            return redirect()->back()->with('fails', $e->getMessage());
261
        }
262
    }
263
264
    public function paymentEditById($id)
265
    {
266
        try {
267
            $cltCont = new \App\Http\Controllers\User\ClientController();
268
             $amountReceived = $cltCont->getAmountPaid($id);
269
            $payment = Payment::find($id);
270
            $clientid = $payment->user_id;
271
             $invoice = new Invoice();
272
            $order = new Order();
273
            $invoices = $invoice->where('user_id', $clientid)->where('status', '=', 'pending')->orderBy('created_at', 'desc')->get();
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
274
            $cltCont = new \App\Http\Controllers\User\ClientController();
275
            $invoiceSum = $cltCont->getTotalInvoice($invoices);
276
            $amountReceived = $cltCont->getAmountPaid($clientid);
277
            $pendingAmount = $invoiceSum - $amountReceived;
278
            $client = $this->user->where('id', $clientid)->first();
279
            $currency = $client->currency;
280
            $orders = $order->where('client', $clientid)->get();
281
              return view('themes.default1.invoice.editPayment',compact('amountReceived','clientid', 'client', 'invoices',  'orders',
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 133 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
282
                  'invoiceSum', 'amountReceived', 'pendingAmount', 'currency'));
283
284
285
286
287
        
288
            
289
        } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Order\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
290
             Bugsnag::notifyException($e);
291
             return redirect()->back()->with('fails', $e->getMessage());
292
        }
293
    }
294
295
    public function deleleById($id)
296
    {
297
        try {
298
            $invoice = Invoice::find($id);
299
            if ($invoice) {
300
                $invoice->delete();
301
            } else {
302
                return redirect()->back()->with('fails', 'Can not delete');
303
            }
304
305
            return redirect()->back()->with('success', "Invoice $invoice->number has been Deleted Successfully");
306
        } catch (\Exception $e) {
307
            Bugsnag::notifyException($e);
308
309
            return redirect()->back()->with('fails', $e->getMessage());
310
        }
311
    }
312
313
    public function getPromotionDetails($code)
314
    {
315
        $promo = Promotion::where('code', $code)->first();
316
        //check promotion code is valid
317
        if (!$promo) {
318
            throw new \Exception(\Lang::get('message.no-such-code'));
319
        }
320
        $relation = $promo->relation()->get();
321
        //check the relation between code and product
322
        if (count($relation) == 0) {
323
            throw new \Exception(\Lang::get('message.no-product-related-to-this-code'));
324
        }
325
        //check the usess
326
        $cont = new \App\Http\Controllers\Payment\PromotionController();
327
        $uses = $cont->checkNumberOfUses($code);
328
        //dd($uses);
329
        if ($uses != 'success') {
330
            throw new \Exception(\Lang::get('message.usage-of-code-completed'));
331
        }
332
        //check for the expiry date
333
        $expiry = $this->checkExpiry($code);
334
        if ($expiry != 'success') {
335
            throw new \Exception(\Lang::get('message.usage-of-code-expired'));
336
        }
337
338
        return $promo;
339
    }
340
}
341