Completed
Push — development ( 877883...280d38 )
by Ashutosh
10:04
created

TaxRatesAndCodeExpiryController::deleleById()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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