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

paymentDeleleById()   A

Complexity

Conditions 4
Paths 10

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 21
rs 9.7666
c 0
b 0
f 0
cc 4
nc 10
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\Payment;
8
use App\Model\Order\Order;
9
use App\Model\Payment\Tax;
10
use App\Model\Payment\TaxOption;
11
use App\User;
12
use Bugsnag;
13
14
class TaxRatesAndCodeExpiryController extends BaseInvoiceController
15
{
16
    /**
17
     * Get tax when enabled.
18
     */
19
    public function getTaxWhenEnable($productid, $taxs, $userid)
20
    {
21
        $rate = $this->getRate($productid, $taxs, $userid);
22
        $taxs = ([$rate['taxs']['0']['name'], $rate['taxs']['0']['rate']]);
23
24
        return $taxs;
25
    }
26
27
    /**
28
     * GeT Total Rate.
29
     */
30
    public function getTotalRate($taxClassId, $productid, $taxs)
31
    {
32
        $cartController = new CartController();
33
        $taxs = $cartController->getTaxByPriority($taxClassId);
34
        $value = $cartController->getValueForOthers($productid, $taxClassId, $taxs);
35
        if ($value == 0) {
36
            $status = 0;
37
        }
38
        $rate = $value;
39
40
        return ['rate'=>$rate, 'taxes'=>$taxs];
41
    }
42
43
    /**
44
     * Get Grandtotal.
45
     **/
46
    public function getGrandTotal($code, $total, $cost, $productid, $currency)
47
    {
48
        if ($code) {
49
            $grand_total = $this->checkCode($code, $productid, $currency);
50
        } else {
51
            if (!$total) {
52
                $grand_total = $cost;
53
            } else {
54
                $grand_total = $total;
55
            }
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
            $rates = explode(',', $rate);
94
            $rule = new TaxOption();
95
            $rule = $rule->findOrFail(1);
96
            if ($rule->inclusive == 0) {
97
                foreach ($rates as $rate1) {
98
                    if ($rate1 != '') {
99
                        $rateTotal = str_replace('%', '', $rate1);
100
                        $total += $total * ($rateTotal / 100);
101
                    }
102
                }
103
            }
104
105
            return intval(round($total));
106
        } catch (\Exception $ex) {
107
            Bugsnag::notifyException($ex);
108
109
            throw new \Exception($ex->getMessage());
110
        }
111
    }
112
113
    public function getPrice($price, $price_model)
114
    {
115
        if ($price == '') {
116
            $price = $price_model->sales_price;
117
            if (!$price) {
118
                $price = $price_model->price;
119
            }
120
        }
121
122
        return $price;
123
    }
124
125
    public function checkExecution($invoiceid)
126
    {
127
        try {
128
            $response = false;
129
            $invoice = Invoice::find($invoiceid);
130
            // dd($invoice);
131
            $order = Order::where('invoice_id', $invoiceid);
132
            // dd($order);
133
            $order_invoice_relation = $invoice->orderRelation()->first();
134
            if ($order_invoice_relation) {
135
                $response = true;
136
            } elseif ($order->get()->count() > 0) {
137
                $response = true;
138
            }
139
140
            return $response;
141
        } catch (\Exception $e) {
142
            Bugsnag::notifyException($e);
143
144
            return redirect()->back()->with('fails', $e->getMessage());
145
        }
146
    }
147
148
    public function invoiceContent($invoiceid)
149
    {
150
        $invoice = $this->invoice->find($invoiceid);
151
        $items = $invoice->invoiceItem()->get();
152
        $content = '';
153
        if ($items->count() > 0) {
154
            foreach ($items as $item) {
155
                $content .= '<tr>'.
156
                        '<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>'.
157
                        '<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>'.
158
                        '<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>'.
159
                        '</tr>';
160
            }
161
        }
162
163
        return $content;
164
    }
165
166
    public function sendInvoiceMail($userid, $number, $total, $invoiceid)
167
    {
168
169
        //user
170
        $users = new User();
171
        $user = $users->find($userid);
172
        //check in the settings
173
        $settings = new \App\Model\Common\Setting();
174
        $setting = $settings->where('id', 1)->first();
175
        $invoiceurl = $this->invoiceUrl($invoiceid);
176
        //template
177
        $templates = new \App\Model\Common\Template();
178
        $temp_id = $setting->invoice;
179
        $template = $templates->where('id', $temp_id)->first();
180
        $from = $setting->email;
181
        $to = $user->email;
182
        $subject = $template->name;
183
        $data = $template->data;
184
        $replace = [
185
            'name'       => $user->first_name.' '.$user->last_name,
186
            'number'     => $number,
187
            'address'    => $user->address,
188
            'invoiceurl' => $invoiceurl,
189
            'content'    => $this->invoiceContent($invoiceid),
190
            'currency'   => $this->currency($invoiceid),
191
        ];
192
        $type = '';
193
        if ($template) {
194
            $type_id = $template->type;
195
            $temp_type = new \App\Model\Common\TemplateType();
196
            $type = $temp_type->where('id', $type_id)->first()->name;
197
        }
198
        //dd($type);
199
        $templateController = new \App\Http\Controllers\Common\TemplateController();
200
        $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
201
202
        return $mail;
203
    }
204
205
    public function invoiceUrl($invoiceid)
206
    {
207
        $url = url('my-invoice/'.$invoiceid);
208
209
        return $url;
210
    }
211
212
    public function paymentDeleleById($id)
213
    {
214
        try {
215
            $invoice_no = '';
216
            $payment = Payment::find($id);
217
            if ($payment) {
218
                $invoice_id = $payment->invoice_id;
219
                $invoice = Invoice::find($invoice_id);
220
                if ($invoice) {
221
                    $invoice_no = $invoice->number;
222
                }
223
                $payment->delete();
224
            } else {
225
                return redirect()->back()->with('fails', 'Can not delete');
226
            }
227
228
            return redirect()->back()->with('success', "Payment for invoice no: $invoice_no has Deleted Successfully");
229
        } catch (\Exception $e) {
230
            Bugsnag::notifyException($e);
231
232
            return redirect()->back()->with('fails', $e->getMessage());
233
        }
234
    }
235
236
      public function deleleById($id)
237
    {
238
        try {
239
            $invoice = Invoice::find($id);
240
            if ($invoice) {
241
                $invoice->delete();
242
            } else {
243
                return redirect()->back()->with('fails', 'Can not delete');
244
            }
245
246
            return redirect()->back()->with('success', "Invoice $invoice->number has Deleted Successfully");
247
        } catch (\Exception $e) {
248
            Bugsnag::notifyException($e);
249
250
            return redirect()->back()->with('fails', $e->getMessage());
251
        }
252
    }
253
254
}
255