Completed
Push — development ( 42348a...d8b245 )
by Ashutosh
10:20
created

TaxRatesAndCodeExpiryController   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 28
eloc 101
dl 0
loc 198
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A invoiceContent() 0 16 3
A getTotalRate() 0 11 2
A checkExecution() 0 20 4
A sendInvoiceMail() 0 37 2
A invoiceUrl() 0 5 1
A getGrandTotal() 0 13 3
A calculateTotal() 0 20 5
A getPrice() 0 10 3
A getMessage() 0 10 2
A getSubtotal() 0 9 2
A getTaxWhenEnable() 0 6 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\Order\Invoice;
8
use App\Model\Order\Order;
9
use App\Model\Payment\Tax;
10
use App\Model\Payment\TaxClass;
11
use App\Model\Payment\TaxOption;
12
use App\User;
13
use Bugsnag;
14
15
class TaxRatesAndCodeExpiryController extends BaseInvoiceController
16
{
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
            $rates = explode(',', $rate);
96
            $rule = new TaxOption();
97
            $rule = $rule->findOrFail(1);
98
            if ($rule->inclusive == 0) {
99
                foreach ($rates as $rate1) {
100
                    if ($rate1 != '') {
101
                        $rateTotal = str_replace('%', '', $rate1);
102
                        $total += $total * ($rateTotal / 100);
103
                    }
104
                }
105
            }
106
107
            return intval(round($total));
108
        } catch (\Exception $ex) {
109
            Bugsnag::notifyException($ex);
110
111
            throw new \Exception($ex->getMessage());
112
        }
113
    }
114
115
116
    public function getPrice($price, $price_model)
117
    {
118
        if ($price == '') {
119
            $price = $price_model->sales_price;
120
            if (!$price) {
121
                $price = $price_model->price;
122
            }
123
        }
124
125
        return $price;
126
    }
127
128
    public function checkExecution($invoiceid)
129
    {
130
        try {
131
            $response = false;
132
            $invoice = Invoice::find($invoiceid);
133
            // dd($invoice);
134
            $order = Order::where('invoice_id', $invoiceid);
135
            // dd($order);
136
            $order_invoice_relation = $invoice->orderRelation()->first();
137
            if ($order_invoice_relation) {
138
                $response = true;
139
            } elseif ($order->get()->count() > 0) {
140
                $response = true;
141
            }
142
143
            return $response;
144
        } catch (\Exception $e) {
145
            Bugsnag::notifyException($e);
146
147
            return redirect()->back()->with('fails', $e->getMessage());
148
        }
149
    }
150
151
    public function invoiceContent($invoiceid)
152
    {
153
        $invoice = $this->invoice->find($invoiceid);
154
        $items = $invoice->invoiceItem()->get();
155
        $content = '';
156
        if ($items->count() > 0) {
157
            foreach ($items as $item) {
158
                $content .= '<tr>'.
159
                        '<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>'.
160
                        '<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>'.
161
                        '<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>'.
162
                        '</tr>';
163
            }
164
        }
165
166
        return $content;
167
    }
168
169
    public function sendInvoiceMail($userid, $number, $total, $invoiceid)
170
    {
171
172
        //user
173
        $users = new User();
174
        $user = $users->find($userid);
175
        //check in the settings
176
        $settings = new \App\Model\Common\Setting();
177
        $setting = $settings->where('id', 1)->first();
178
        $invoiceurl = $this->invoiceUrl($invoiceid);
179
        //template
180
        $templates = new \App\Model\Common\Template();
181
        $temp_id = $setting->invoice;
182
        $template = $templates->where('id', $temp_id)->first();
183
        $from = $setting->email;
184
        $to = $user->email;
185
        $subject = $template->name;
186
        $data = $template->data;
187
        $replace = [
188
            'name'       => $user->first_name.' '.$user->last_name,
189
            'number'     => $number,
190
            'address'    => $user->address,
191
            'invoiceurl' => $invoiceurl,
192
            'content'    => $this->invoiceContent($invoiceid),
193
            'currency'   => $this->currency($invoiceid),
194
        ];
195
        $type = '';
196
        if ($template) {
197
            $type_id = $template->type;
198
            $temp_type = new \App\Model\Common\TemplateType();
199
            $type = $temp_type->where('id', $type_id)->first()->name;
200
        }
201
        //dd($type);
202
        $templateController = new \App\Http\Controllers\Common\TemplateController();
203
        $mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type);
204
205
        return $mail;
206
    }
207
208
    public function invoiceUrl($invoiceid)
209
    {
210
        $url = url('my-invoice/'.$invoiceid);
211
212
        return $url;
213
    }
214
}
215