|
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 currency($invoiceid) |
|
166
|
|
|
{ |
|
167
|
|
|
$invoice = Invoice::find($invoiceid); |
|
168
|
|
|
$currency_code = $invoice->currency; |
|
169
|
|
|
$cur = ' '; |
|
170
|
|
|
if ($invoice->grand_total == 0) { |
|
171
|
|
|
return $cur; |
|
172
|
|
|
} |
|
173
|
|
|
$currency = Currency::where('code', $currency_code)->first(); |
|
174
|
|
|
if ($currency) { |
|
175
|
|
|
$cur = $currency->symbol; |
|
176
|
|
|
if (!$cur) { |
|
177
|
|
|
$cur = $currency->code; |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $cur; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function sendInvoiceMail($userid, $number, $total, $invoiceid) |
|
185
|
|
|
{ |
|
186
|
|
|
|
|
187
|
|
|
//user |
|
188
|
|
|
$users = new User(); |
|
189
|
|
|
$user = $users->find($userid); |
|
190
|
|
|
//check in the settings |
|
191
|
|
|
$settings = new \App\Model\Common\Setting(); |
|
192
|
|
|
$setting = $settings->where('id', 1)->first(); |
|
193
|
|
|
$invoiceurl = $this->invoiceUrl($invoiceid); |
|
194
|
|
|
//template |
|
195
|
|
|
$templates = new \App\Model\Common\Template(); |
|
196
|
|
|
$temp_id = $setting->invoice; |
|
197
|
|
|
$template = $templates->where('id', $temp_id)->first(); |
|
198
|
|
|
$from = $setting->email; |
|
199
|
|
|
$to = $user->email; |
|
200
|
|
|
$subject = $template->name; |
|
201
|
|
|
$data = $template->data; |
|
202
|
|
|
$replace = [ |
|
203
|
|
|
'name' => $user->first_name.' '.$user->last_name, |
|
204
|
|
|
'number' => $number, |
|
205
|
|
|
'address' => $user->address, |
|
206
|
|
|
'invoiceurl' => $invoiceurl, |
|
207
|
|
|
'content' => $this->invoiceContent($invoiceid), |
|
208
|
|
|
'currency' => $this->currency($invoiceid), |
|
209
|
|
|
]; |
|
210
|
|
|
$type = ''; |
|
211
|
|
|
if ($template) { |
|
212
|
|
|
$type_id = $template->type; |
|
213
|
|
|
$temp_type = new \App\Model\Common\TemplateType(); |
|
214
|
|
|
$type = $temp_type->where('id', $type_id)->first()->name; |
|
215
|
|
|
} |
|
216
|
|
|
//dd($type); |
|
217
|
|
|
$templateController = new \App\Http\Controllers\Common\TemplateController(); |
|
218
|
|
|
$mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type); |
|
219
|
|
|
|
|
220
|
|
|
return $mail; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
public function invoiceUrl($invoiceid) |
|
224
|
|
|
{ |
|
225
|
|
|
$url = url('my-invoice/'.$invoiceid); |
|
226
|
|
|
|
|
227
|
|
|
return $url; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
public function paymentDeleleById($id) |
|
231
|
|
|
{ |
|
232
|
|
|
try { |
|
233
|
|
|
$invoice_no = ''; |
|
234
|
|
|
$payment = Payment::find($id); |
|
235
|
|
|
if ($payment) { |
|
236
|
|
|
$invoice_id = $payment->invoice_id; |
|
237
|
|
|
$invoice = Invoice::find($invoice_id); |
|
238
|
|
|
if ($invoice) { |
|
239
|
|
|
$invoice_no = $invoice->number; |
|
240
|
|
|
} |
|
241
|
|
|
$payment->delete(); |
|
242
|
|
|
} else { |
|
243
|
|
|
return redirect()->back()->with('fails', 'Can not delete'); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return redirect()->back()->with('success', "Payment for invoice no: $invoice_no has Deleted Successfully"); |
|
247
|
|
|
} catch (\Exception $e) { |
|
248
|
|
|
Bugsnag::notifyException($e); |
|
249
|
|
|
|
|
250
|
|
|
return redirect()->back()->with('fails', $e->getMessage()); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
public function paymentEditById($id) |
|
255
|
|
|
{ |
|
256
|
|
|
try { |
|
257
|
|
|
$cltCont = new \App\Http\Controllers\User\ClientController(); |
|
258
|
|
|
$payment = Payment::find($id); |
|
259
|
|
|
$clientid = $payment->user_id; |
|
260
|
|
|
$invoice = new Invoice(); |
|
261
|
|
|
$order = new Order(); |
|
262
|
|
|
$invoices = $invoice->where('user_id', $clientid)->where('status', '=', 'pending') |
|
263
|
|
|
->orderBy('created_at', 'desc')->get(); |
|
264
|
|
|
$cltCont = new \App\Http\Controllers\User\ClientController(); |
|
265
|
|
|
$invoiceSum = $cltCont->getTotalInvoice($invoices); |
|
266
|
|
|
$amountReceived = $cltCont->getExtraAmt($clientid); |
|
267
|
|
|
$pendingAmount = $invoiceSum - $amountReceived; |
|
268
|
|
|
$client = $this->user->where('id', $clientid)->first(); |
|
269
|
|
|
$currency = $client->currency; |
|
270
|
|
|
$symbol = Currency::where('code', $currency)->pluck('symbol')->first(); |
|
271
|
|
|
$orders = $order->where('client', $clientid)->get(); |
|
272
|
|
|
|
|
273
|
|
|
return view('themes.default1.invoice.editPayment', |
|
274
|
|
|
compact('amountReceived','clientid', 'client', 'invoices', 'orders', |
|
275
|
|
|
'invoiceSum', 'amountReceived', 'pendingAmount', 'currency', 'symbol')); |
|
276
|
|
|
} catch (\Exception $e) { |
|
277
|
|
|
Bugsnag::notifyException($e); |
|
278
|
|
|
|
|
279
|
|
|
return redirect()->back()->with('fails', $e->getMessage()); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
public function deleleById($id) |
|
284
|
|
|
{ |
|
285
|
|
|
try { |
|
286
|
|
|
$invoice = Invoice::find($id); |
|
287
|
|
|
if ($invoice) { |
|
288
|
|
|
$invoice->delete(); |
|
289
|
|
|
} else { |
|
290
|
|
|
return redirect()->back()->with('fails', 'Can not delete'); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
return redirect()->back()->with('success', "Invoice $invoice->number has been Deleted Successfully"); |
|
294
|
|
|
} catch (\Exception $e) { |
|
295
|
|
|
Bugsnag::notifyException($e); |
|
296
|
|
|
|
|
297
|
|
|
return redirect()->back()->with('fails', $e->getMessage()); |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
public function getPromotionDetails($code) |
|
302
|
|
|
{ |
|
303
|
|
|
$promo = Promotion::where('code', $code)->first(); |
|
304
|
|
|
//check promotion code is valid |
|
305
|
|
|
if (!$promo) { |
|
306
|
|
|
throw new \Exception(\Lang::get('message.no-such-code')); |
|
307
|
|
|
} |
|
308
|
|
|
$relation = $promo->relation()->get(); |
|
309
|
|
|
//check the relation between code and product |
|
310
|
|
|
if (count($relation) == 0) { |
|
311
|
|
|
throw new \Exception(\Lang::get('message.no-product-related-to-this-code')); |
|
312
|
|
|
} |
|
313
|
|
|
//check the usess |
|
314
|
|
|
$cont = new \App\Http\Controllers\Payment\PromotionController(); |
|
315
|
|
|
$uses = $cont->checkNumberOfUses($code); |
|
316
|
|
|
//dd($uses); |
|
317
|
|
|
if ($uses != 'success') { |
|
318
|
|
|
throw new \Exception(\Lang::get('message.usage-of-code-completed')); |
|
319
|
|
|
} |
|
320
|
|
|
//check for the expiry date |
|
321
|
|
|
$expiry = $this->checkExpiry($code); |
|
322
|
|
|
if ($expiry != 'success') { |
|
323
|
|
|
throw new \Exception(\Lang::get('message.usage-of-code-expired')); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return $promo; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|