|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Order; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use App\Model\Common\Setting; |
|
7
|
|
|
use App\Model\Common\Template; |
|
8
|
|
|
use App\Model\Order\Invoice; |
|
9
|
|
|
use App\Model\Order\InvoiceItem; |
|
10
|
|
|
use App\Model\Order\Payment; |
|
11
|
|
|
use App\Model\Payment\Currency; |
|
12
|
|
|
use App\Model\Payment\Promotion; |
|
13
|
|
|
use App\Model\Payment\Tax; |
|
14
|
|
|
use App\Model\Payment\TaxOption; |
|
15
|
|
|
use App\Model\Product\Price; |
|
16
|
|
|
use App\Model\Product\Product; |
|
17
|
|
|
//use Symfony\Component\HttpFoundation\Request as Requests; |
|
18
|
|
|
use App\User; |
|
19
|
|
|
use Illuminate\Http\Request; |
|
20
|
|
|
use Input; |
|
21
|
|
|
|
|
22
|
|
|
class InvoiceController extends Controller |
|
23
|
|
|
{ |
|
24
|
|
|
public $invoice; |
|
25
|
|
|
public $invoiceItem; |
|
26
|
|
|
public $user; |
|
27
|
|
|
public $template; |
|
28
|
|
|
public $setting; |
|
29
|
|
|
public $payment; |
|
30
|
|
|
public $product; |
|
31
|
|
|
public $price; |
|
32
|
|
|
public $promotion; |
|
33
|
|
|
public $currency; |
|
34
|
|
|
public $tax; |
|
35
|
|
|
public $tax_option; |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function __construct() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->middleware('auth'); |
|
40
|
|
|
// $this->middleware('admin'); |
|
41
|
|
|
|
|
42
|
|
|
$invoice = new Invoice(); |
|
43
|
|
|
$this->invoice = $invoice; |
|
44
|
|
|
|
|
45
|
|
|
$invoiceItem = new InvoiceItem(); |
|
46
|
|
|
$this->invoiceItem = $invoiceItem; |
|
47
|
|
|
|
|
48
|
|
|
$user = new User(); |
|
49
|
|
|
$this->user = $user; |
|
50
|
|
|
|
|
51
|
|
|
$template = new Template(); |
|
52
|
|
|
$this->template = $template; |
|
53
|
|
|
|
|
54
|
|
|
$seting = new Setting(); |
|
55
|
|
|
$this->setting = $seting; |
|
56
|
|
|
|
|
57
|
|
|
$payment = new Payment(); |
|
58
|
|
|
$this->payment = $payment; |
|
59
|
|
|
|
|
60
|
|
|
$product = new Product(); |
|
61
|
|
|
$this->product = $product; |
|
62
|
|
|
|
|
63
|
|
|
$price = new Price(); |
|
64
|
|
|
$this->price = $price; |
|
65
|
|
|
|
|
66
|
|
|
$promotion = new Promotion(); |
|
67
|
|
|
$this->promotion = $promotion; |
|
68
|
|
|
|
|
69
|
|
|
$currency = new Currency(); |
|
70
|
|
|
$this->currency = $currency; |
|
71
|
|
|
|
|
72
|
|
|
$tax = new Tax(); |
|
73
|
|
|
$this->tax = $tax; |
|
74
|
|
|
|
|
75
|
|
|
$tax_option = new TaxOption(); |
|
76
|
|
|
$this->tax_option = $tax_option; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function index() |
|
80
|
|
|
{ |
|
81
|
|
|
try { |
|
82
|
|
|
//dd($this->invoice->get()); |
|
83
|
|
|
return view('themes.default1.invoice.index'); |
|
84
|
|
|
} catch (\Exception $ex) { |
|
85
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function GetInvoices() |
|
90
|
|
|
{ |
|
91
|
|
|
//dd($this->invoice->get()); |
|
92
|
|
|
//$invoice = \DB::table('invoices'); |
|
93
|
|
|
return \Datatable::query($this->invoice->select('id', 'user_id', 'number', 'date', 'grand_total', 'status', 'created_at')) |
|
94
|
|
|
->addColumn('#', function ($model) { |
|
95
|
|
|
return "<input type='checkbox' value=".$model->id.' name=select[] id=check>'; |
|
96
|
|
|
}) |
|
97
|
|
|
->addColumn('user_id', function ($model) { |
|
98
|
|
|
|
|
99
|
|
|
$first = $this->user->where('id', $model->user_id)->first()->first_name; |
|
100
|
|
|
$last = $this->user->where('id', $model->user_id)->first()->last_name; |
|
101
|
|
|
$id = $this->user->where('id', $model->user_id)->first()->id; |
|
102
|
|
|
|
|
103
|
|
|
return '<a href='.url('clients/'.$id).'>'.ucfirst($first).' '.ucfirst($last).'</a>'; |
|
104
|
|
|
}) |
|
105
|
|
|
->showColumns('number', 'created_at', 'grand_total', 'status') |
|
106
|
|
|
->addColumn('action', function ($model) { |
|
107
|
|
|
$order = \App\Model\Order\Order::where('invoice_id', $model->id)->first(); |
|
108
|
|
|
if (!$order) { |
|
109
|
|
|
$action = '<a href='.url('order/execute?invoiceid='.$model->id)." class='btn btn-sm btn-primary'>Execute Order</a>"; |
|
110
|
|
|
} else { |
|
111
|
|
|
$action = ''; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return '<a href='.url('invoices/show?invoiceid='.$model->id)." class='btn btn-sm btn-primary'>View</a>" |
|
115
|
|
|
." $action"; |
|
116
|
|
|
}) |
|
117
|
|
|
->searchColumns('created_at', 'user_id', 'number', 'grand_total', 'status') |
|
118
|
|
|
->orderColumns('created_at', 'user_id', 'number', 'grand_total', 'status') |
|
119
|
|
|
->make(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function show(Request $request) |
|
123
|
|
|
{ |
|
124
|
|
|
try { |
|
125
|
|
|
$id = $request->input('invoiceid'); |
|
126
|
|
|
$invoice = $this->invoice->where('id', $id)->first(); |
|
127
|
|
|
$invoiceItems = $this->invoiceItem->where('invoice_id', $id)->get(); |
|
128
|
|
|
$user = $this->user->find($invoice->user_id); |
|
129
|
|
|
|
|
130
|
|
|
return view('themes.default1.invoice.show', compact('invoiceItems', 'invoice', 'user')); |
|
131
|
|
|
} catch (\Exception $ex) { |
|
132
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* not in use case. |
|
138
|
|
|
* |
|
139
|
|
|
* @param Request $request |
|
140
|
|
|
* |
|
141
|
|
|
* @return type |
|
142
|
|
|
*/ |
|
143
|
|
|
public function generateById(Request $request) |
|
144
|
|
|
{ |
|
145
|
|
|
try { |
|
146
|
|
|
$clientid = $request->input('clientid'); |
|
147
|
|
|
//dd($clientid); |
|
148
|
|
|
if ($clientid) { |
|
149
|
|
|
$user = new User(); |
|
150
|
|
|
$user = $user->where('id', $clientid)->first(); |
|
151
|
|
|
if (!$user) { |
|
152
|
|
|
return redirect()->back()->with('fails', 'Invalid user'); |
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
$user = ''; |
|
156
|
|
|
} |
|
157
|
|
|
$products = $this->product->lists('name', 'id')->toArray(); |
|
158
|
|
|
$currency = $this->currency->lists('name', 'code')->toArray(); |
|
159
|
|
|
|
|
160
|
|
|
return view('themes.default1.invoice.generate', compact('user', 'products', 'currency')); |
|
161
|
|
|
} catch (\Exception $ex) { |
|
162
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
public function invoiceGenerateByForm($user_id = '') |
|
167
|
|
|
{ |
|
168
|
|
|
$v = \Validator::make(\Input::get(), [ |
|
|
|
|
|
|
169
|
|
|
'product' => 'required', |
|
170
|
|
|
//'price' => 'numeric' |
|
171
|
|
|
]); |
|
172
|
|
|
if ($v->fails()) { |
|
173
|
|
|
return redirect()->back()->withError([ |
|
|
|
|
|
|
174
|
|
|
'product' => 'required', |
|
175
|
|
|
'price' => 'required', |
|
176
|
|
|
])->withInput(); |
|
177
|
|
|
} |
|
178
|
|
|
try { |
|
179
|
|
|
if ($user_id == '') { |
|
180
|
|
|
$user_id = \Input::get('user'); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$productid = Input::get('product'); |
|
184
|
|
|
$code = Input::get('code'); |
|
185
|
|
|
$total = Input::get('price'); |
|
186
|
|
|
//dd($total); |
|
187
|
|
|
$currency = $this->user->find($user_id)->currency; |
|
188
|
|
|
if (!$currency) { |
|
189
|
|
|
$currency = 'USD'; |
|
190
|
|
|
} |
|
191
|
|
|
if ($currency == 1) { |
|
192
|
|
|
$currency = 'USD'; |
|
193
|
|
|
} else { |
|
194
|
|
|
$currency = 'INR'; |
|
195
|
|
|
} |
|
196
|
|
|
$number = rand(11111111, 99999999); |
|
197
|
|
|
$date = \Carbon\Carbon::now(); |
|
198
|
|
|
$product = $this->product->findOrFail($productid); |
|
199
|
|
|
$price = $product->price()->where('currency', $currency)->first(); |
|
200
|
|
|
//dd($price); |
|
201
|
|
|
$cost = $price->sales_price; |
|
202
|
|
|
if (!$cost) { |
|
203
|
|
|
$cost = $price->price; |
|
204
|
|
|
} |
|
205
|
|
|
if ($cost != $total) { |
|
206
|
|
|
$grand_total = $total; |
|
207
|
|
|
} |
|
208
|
|
|
//dd($cost); |
|
209
|
|
|
if ($code) { |
|
210
|
|
|
$grand_total = $this->checkCode($code, $productid); |
|
211
|
|
|
//dd($grand_total); |
|
212
|
|
|
} else { |
|
213
|
|
|
if (!$total) { |
|
214
|
|
|
$grand_total = $cost; |
|
215
|
|
|
} else { |
|
216
|
|
|
$grand_total = $total; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
$grand_total = $grand_total; |
|
|
|
|
|
|
220
|
|
|
//dd($grand_total); |
|
221
|
|
|
$tax = $this->checkTax($product->id); |
|
222
|
|
|
//dd($tax); |
|
223
|
|
|
$tax_name = ''; |
|
224
|
|
|
$tax_rate = ''; |
|
225
|
|
View Code Duplication |
if (!empty($tax)) { |
|
226
|
|
|
foreach ($tax as $key => $value) { |
|
227
|
|
|
//dd($value); |
|
228
|
|
|
$tax_name .= $value['name'].','; |
|
229
|
|
|
$tax_rate .= $value['rate'].','; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
//dd('dsjcgv'); |
|
233
|
|
|
$grand_total = $this->calculateTotal($tax_rate, $grand_total); |
|
234
|
|
|
|
|
235
|
|
|
//dd($grand_total); |
|
236
|
|
|
$grand_total = \App\Http\Controllers\Front\CartController::rounding($grand_total); |
|
237
|
|
|
|
|
238
|
|
|
$invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total, 'currency' => $currency, 'status' => 'pending']); |
|
239
|
|
|
if ($grand_total > 0) { |
|
240
|
|
|
$this->doPayment('admin', $invoice->id, $grand_total, '', $user_id); |
|
241
|
|
|
} |
|
242
|
|
|
$items = $this->createInvoiceItemsByAdmin($invoice->id, $productid, $code, $total, $currency); |
|
243
|
|
|
if ($items) { |
|
244
|
|
|
return redirect()->back()->with('success', \Lang::get('message.invoice-generated-successfully')); |
|
245
|
|
|
} else { |
|
246
|
|
|
return redirect()->back()->with('fails', \Lang::get('message.can-not-generate-invoice')); |
|
247
|
|
|
} |
|
248
|
|
|
} catch (\Exception $ex) { |
|
249
|
|
|
dd($ex); |
|
250
|
|
|
|
|
251
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Generate invoice. |
|
257
|
|
|
* |
|
258
|
|
|
* @throws \Exception |
|
259
|
|
|
*/ |
|
260
|
|
|
public function GenerateInvoice() |
|
261
|
|
|
{ |
|
262
|
|
|
try { |
|
263
|
|
|
$tax_rule = new \App\Model\Payment\TaxOption(); |
|
264
|
|
|
$rule = $tax_rule->findOrFail(1); |
|
265
|
|
|
$rounding = $rule->rounding; |
|
266
|
|
|
|
|
267
|
|
|
$user_id = \Auth::user()->id; |
|
268
|
|
|
$grand_total = \Cart::getSubTotal(); |
|
269
|
|
|
|
|
270
|
|
|
$number = rand(11111111, 99999999); |
|
271
|
|
|
$date = \Carbon\Carbon::now(); |
|
272
|
|
|
|
|
273
|
|
|
if ($rounding == 1) { |
|
274
|
|
|
$grand_total = round($grand_total); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
$invoice = $this->invoice->create(['user_id' => $user_id, 'number' => $number, 'date' => $date, 'grand_total' => $grand_total, 'status' => 'pending']); |
|
278
|
|
|
//dd($invoice); |
|
279
|
|
|
foreach (\Cart::getContent() as $cart) { |
|
280
|
|
|
$this->createInvoiceItems($invoice->id, $cart); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $invoice; |
|
284
|
|
|
} catch (\Exception $ex) { |
|
285
|
|
|
dd($ex); |
|
286
|
|
|
throw new \Exception('Can not Generate Invoice'); |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
public function createInvoiceItems($invoiceid, $cart) |
|
291
|
|
|
{ |
|
292
|
|
|
try { |
|
293
|
|
|
$product_name = $cart->name; |
|
294
|
|
|
$regular_price = $cart->price; |
|
295
|
|
|
$quantity = $cart->quantity; |
|
296
|
|
|
$domain = $this->domain($cart->id); |
|
297
|
|
|
//dd($quantity); |
|
298
|
|
|
$subtotal = \App\Http\Controllers\Front\CartController::rounding($cart->getPriceSumWithConditions()); |
|
299
|
|
|
|
|
300
|
|
|
$tax_name = ''; |
|
301
|
|
|
$tax_percentage = ''; |
|
302
|
|
|
|
|
303
|
|
|
foreach ($cart->attributes['tax'] as $tax) { |
|
304
|
|
|
//dd($tax['name']); |
|
305
|
|
|
$tax_name .= $tax['name'].','; |
|
306
|
|
|
$tax_percentage .= $tax['rate'].','; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
// dd($tax_name); |
|
310
|
|
|
|
|
311
|
|
|
$invoiceItem = $this->invoiceItem->create([ |
|
312
|
|
|
'invoice_id' => $invoiceid, |
|
313
|
|
|
'product_name' => $product_name, |
|
314
|
|
|
'regular_price' => $regular_price, |
|
315
|
|
|
'quantity' => $quantity, |
|
316
|
|
|
'tax_name' => $tax_name, |
|
317
|
|
|
'tax_percentage' => $tax_percentage, |
|
318
|
|
|
'subtotal' => $subtotal, |
|
319
|
|
|
'domain' => $domain, |
|
320
|
|
|
]); |
|
321
|
|
|
|
|
322
|
|
|
return $invoiceItem; |
|
323
|
|
|
} catch (\Exception $ex) { |
|
324
|
|
|
dd($ex); |
|
325
|
|
|
throw new \Exception('Can not create Invoice Items'); |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
public function doPayment($payment_method, $invoiceid, $amount, $parent_id = '', $userid = '', $payment_status = 'pending') |
|
330
|
|
|
{ |
|
331
|
|
|
try { |
|
332
|
|
|
if ($amount > 0) { |
|
333
|
|
|
if ($userid == '') { |
|
334
|
|
|
$userid = \Auth::user()->id; |
|
335
|
|
|
} |
|
336
|
|
|
if ($amount == 0) { |
|
337
|
|
|
$payment_status = 'success'; |
|
338
|
|
|
} |
|
339
|
|
|
$this->payment->create([ |
|
340
|
|
|
'parent_id' => $parent_id, |
|
341
|
|
|
'invoice_id' => $invoiceid, |
|
342
|
|
|
'user_id' => $userid, |
|
343
|
|
|
'amount' => $amount, |
|
344
|
|
|
'payment_method' => $payment_method, |
|
345
|
|
|
'payment_status' => $payment_status, |
|
346
|
|
|
]); |
|
347
|
|
|
$this->updateInvoice($invoiceid); |
|
348
|
|
|
} |
|
349
|
|
|
} catch (\Exception $ex) { |
|
350
|
|
|
throw new \Exception($ex->getMessage()); |
|
351
|
|
|
} |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
public function createInvoiceItemsByAdmin($invoiceid, $productid, $code = '', $price = '', $currency = 'USD') |
|
355
|
|
|
{ |
|
356
|
|
|
try { |
|
357
|
|
|
$discount = ''; |
|
358
|
|
|
$mode = ''; |
|
359
|
|
|
$product = $this->product->findOrFail($productid); |
|
360
|
|
|
$price_model = $this->price->where('product_id', $product->id)->where('currency', $currency)->first(); |
|
361
|
|
|
if ($price == '') { |
|
362
|
|
|
$price = $price_model->sales_price; |
|
363
|
|
|
if (!$price) { |
|
364
|
|
|
$price = $price_model->price; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
$subtotal = $price; |
|
368
|
|
|
//dd($subtotal); |
|
369
|
|
|
if ($code) { |
|
370
|
|
|
$subtotal = $this->checkCode($code, $productid); |
|
371
|
|
|
$mode = 'coupon'; |
|
372
|
|
|
$discount = $price - $subtotal; |
|
373
|
|
|
} |
|
374
|
|
|
$tax = $this->checkTax($product->id); |
|
375
|
|
|
//dd($tax); |
|
376
|
|
|
$tax_name = ''; |
|
377
|
|
|
$tax_rate = ''; |
|
378
|
|
View Code Duplication |
if (!empty($tax)) { |
|
379
|
|
|
foreach ($tax as $key => $value) { |
|
380
|
|
|
//dd($value); |
|
381
|
|
|
$tax_name .= $value['name'].','; |
|
382
|
|
|
$tax_rate .= $value['rate'].','; |
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
$subtotal = $this->calculateTotal($tax_rate, $subtotal); |
|
386
|
|
|
$domain = $this->domain($productid); |
|
387
|
|
|
$items = $this->invoiceItem->create([ |
|
388
|
|
|
'invoice_id' => $invoiceid, |
|
389
|
|
|
'product_name' => $product->name, |
|
390
|
|
|
'regular_price' => $price, |
|
391
|
|
|
'quantity' => 1, |
|
392
|
|
|
'discount' => $discount, |
|
393
|
|
|
'discount_mode' => $mode, |
|
394
|
|
|
'subtotal' => \App\Http\Controllers\Front\CartController::rounding($subtotal), |
|
395
|
|
|
'tax_name' => $tax_name, |
|
396
|
|
|
'tax_percentage' => $tax_rate, |
|
397
|
|
|
'domain' => $domain, |
|
398
|
|
|
]); |
|
399
|
|
|
|
|
400
|
|
|
return $items; |
|
401
|
|
|
} catch (\Exception $ex) { |
|
402
|
|
|
dd($ex); |
|
403
|
|
|
|
|
404
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
405
|
|
|
} |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
public function checkCode($code, $productid) |
|
409
|
|
|
{ |
|
410
|
|
|
try { |
|
411
|
|
|
if ($code != '') { |
|
412
|
|
|
$promo = $this->promotion->where('code', $code)->first(); |
|
413
|
|
|
//check promotion code is valid |
|
414
|
|
|
if (!$promo) { |
|
415
|
|
|
throw new \Exception(\Lang::get('message.no-such-code')); |
|
416
|
|
|
} |
|
417
|
|
|
$relation = $promo->relation()->get(); |
|
418
|
|
|
//check the relation between code and product |
|
419
|
|
|
if (count($relation) == 0) { |
|
420
|
|
|
throw new \Exception(\Lang::get('message.no-product-related-to-this-code')); |
|
421
|
|
|
} |
|
422
|
|
|
//check the usess |
|
423
|
|
|
$uses = $this->checkNumberOfUses($code); |
|
424
|
|
|
//dd($uses); |
|
425
|
|
|
if ($uses != 'success') { |
|
426
|
|
|
throw new \Exception(\Lang::get('message.usage-of-code-completed')); |
|
427
|
|
|
} |
|
428
|
|
|
//check for the expiry date |
|
429
|
|
|
$expiry = $this->checkExpiry($code); |
|
430
|
|
|
//dd($expiry); |
|
431
|
|
|
if ($expiry != 'success') { |
|
432
|
|
|
throw new \Exception(\Lang::get('message.usage-of-code-expired')); |
|
433
|
|
|
} |
|
434
|
|
|
$value = $this->findCostAfterDiscount($promo->id, $productid); |
|
435
|
|
|
|
|
436
|
|
|
return $value; |
|
437
|
|
|
} else { |
|
438
|
|
|
$product = $this->product->find($productid); |
|
439
|
|
|
$price = $product->price()->sales_price; |
|
440
|
|
|
if (!$price) { |
|
441
|
|
|
$price = $product->price()->price; |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
return $price; |
|
445
|
|
|
} |
|
446
|
|
|
} catch (\Exception $ex) { |
|
447
|
|
|
dd($ex); |
|
448
|
|
|
throw new \Exception(\Lang::get('message.check-code-error')); |
|
449
|
|
|
} |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
View Code Duplication |
public function findCostAfterDiscount($promoid, $productid) |
|
453
|
|
|
{ |
|
454
|
|
|
try { |
|
455
|
|
|
$promotion = $this->promotion->findOrFail($promoid); |
|
456
|
|
|
$product = $this->product->findOrFail($productid); |
|
457
|
|
|
$promotion_type = $promotion->type; |
|
458
|
|
|
$promotion_value = $promotion->value; |
|
459
|
|
|
$product_price = $product->price()->first()->sales_price; |
|
460
|
|
|
if (!$product_price) { |
|
461
|
|
|
$product_price = $product->price()->first()->price; |
|
462
|
|
|
} |
|
463
|
|
|
$updated_price = $this->findCost($promotion_type, $promotion_value, $product_price, $productid); |
|
464
|
|
|
|
|
465
|
|
|
return $updated_price; |
|
466
|
|
|
} catch (\Exception $ex) { |
|
467
|
|
|
throw new \Exception(\Lang::get('message.find-discount-error')); |
|
468
|
|
|
} |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
public function findCost($type, $value, $price, $productid) |
|
472
|
|
|
{ |
|
473
|
|
|
try { |
|
474
|
|
|
switch ($type) { |
|
475
|
|
|
|
|
476
|
|
|
case 1: |
|
477
|
|
|
$percentage = $price * ($value / 100); |
|
478
|
|
|
|
|
479
|
|
|
return $price - $percentage; |
|
480
|
|
|
case 2: |
|
481
|
|
|
return $price - $value; |
|
482
|
|
|
case 3: |
|
483
|
|
|
return $value; |
|
484
|
|
|
case 4: |
|
485
|
|
|
return 0; |
|
486
|
|
|
} |
|
487
|
|
|
} catch (\Exception $ex) { |
|
|
|
|
|
|
488
|
|
|
throw new \Exception(\Lang::get('message.find-cost-error')); |
|
489
|
|
|
} |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
View Code Duplication |
public function checkNumberOfUses($code) |
|
493
|
|
|
{ |
|
494
|
|
|
try { |
|
495
|
|
|
$promotion = $this->promotion->where('code', $code)->first(); |
|
496
|
|
|
$uses = $promotion->uses; |
|
497
|
|
|
if ($uses == 0) { |
|
498
|
|
|
return 'success'; |
|
499
|
|
|
} |
|
500
|
|
|
$used_number = $this->invoice->where('coupon_code', $code)->count(); |
|
501
|
|
|
if ($uses >= $used_number) { |
|
502
|
|
|
return 'success'; |
|
503
|
|
|
} else { |
|
504
|
|
|
return 'fails'; |
|
505
|
|
|
} |
|
506
|
|
|
} catch (\Exception $ex) { |
|
507
|
|
|
throw new \Exception(\Lang::get('message.find-cost-error')); |
|
508
|
|
|
} |
|
509
|
|
|
} |
|
510
|
|
|
|
|
511
|
|
|
public function checkExpiry($code = '') |
|
512
|
|
|
{ |
|
513
|
|
|
try { |
|
514
|
|
|
if ($code != '') { |
|
515
|
|
|
$promotion = $this->promotion->where('code', $code)->first(); |
|
516
|
|
|
$start = $promotion->start; |
|
517
|
|
|
$end = $promotion->expiry; |
|
518
|
|
|
//dd($end); |
|
519
|
|
|
$now = \Carbon\Carbon::now(); |
|
520
|
|
|
//both not set, always true |
|
521
|
|
|
if (($start == null || $start == '0000-00-00 00:00:00') && ($end == null || $end == '0000-00-00 00:00:00')) { |
|
522
|
|
|
return 'success'; |
|
523
|
|
|
} |
|
524
|
|
|
//only starting date set, check the date is less or equel to today |
|
525
|
|
View Code Duplication |
if (($start != null || $start != '0000-00-00 00:00:00') && ($end == null || $end == '0000-00-00 00:00:00')) { |
|
526
|
|
|
if ($start <= $now) { |
|
527
|
|
|
return 'success'; |
|
528
|
|
|
} |
|
529
|
|
|
} |
|
530
|
|
|
//only ending date set, check the date is greater or equel to today |
|
531
|
|
View Code Duplication |
if (($end != null || $end != '0000-00-00 00:00:00') && ($start == null || $start == '0000-00-00 00:00:00')) { |
|
532
|
|
|
if ($end >= $now) { |
|
533
|
|
|
return 'success'; |
|
534
|
|
|
} |
|
535
|
|
|
} |
|
536
|
|
|
//both set |
|
537
|
|
|
if (($end != null || $start != '0000-00-00 00:00:00') && ($start != null || $start != '0000-00-00 00:00:00')) { |
|
538
|
|
|
if ($end >= $now && $start <= $now) { |
|
539
|
|
|
return 'success'; |
|
540
|
|
|
} |
|
541
|
|
|
} |
|
542
|
|
|
} else { |
|
|
|
|
|
|
543
|
|
|
} |
|
544
|
|
|
} catch (\Exception $ex) { |
|
545
|
|
|
dd($ex); |
|
546
|
|
|
throw new \Exception(\Lang::get('message.check-expiry')); |
|
547
|
|
|
} |
|
548
|
|
|
} |
|
549
|
|
|
|
|
550
|
|
|
public function checkTax($productid) |
|
551
|
|
|
{ |
|
552
|
|
|
try { |
|
553
|
|
|
//dd($productid); |
|
554
|
|
|
$taxs[0] = ['name' => 'null', 'rate' => 0]; |
|
|
|
|
|
|
555
|
|
|
$product = $this->product->findOrFail($productid); |
|
556
|
|
|
if ($this->tax_option->findOrFail(1)->inclusive == 0) { |
|
557
|
|
|
if ($product->tax()->first()) { |
|
558
|
|
|
$tax_class_id = $product->tax()->first()->tax_class_id; |
|
559
|
|
|
} else { |
|
560
|
|
|
return $taxs; |
|
561
|
|
|
} |
|
562
|
|
|
|
|
563
|
|
|
if ($this->tax_option->findOrFail(1)->tax_enable == 1) { |
|
564
|
|
|
$cart_controller = new \App\Http\Controllers\Front\CartController(); |
|
565
|
|
|
$taxes = $cart_controller->getTaxByPriority($tax_class_id); |
|
566
|
|
|
|
|
567
|
|
|
foreach ($taxes as $key => $tax) { |
|
568
|
|
|
//dd($tax); |
|
569
|
|
|
if ($tax->compound == 1) { |
|
570
|
|
|
$taxs[$key] = ['name' => $tax->name, 'rate' => $tax->rate]; |
|
571
|
|
|
} else { |
|
572
|
|
|
$rate = ''; |
|
573
|
|
|
$rate += $tax->rate; |
|
574
|
|
|
$taxs[$key] = ['name' => $tax->name, 'rate' => $rate]; |
|
575
|
|
|
} |
|
576
|
|
|
} |
|
577
|
|
|
} |
|
578
|
|
|
} |
|
579
|
|
|
//dd($taxs); |
|
580
|
|
|
return $taxs; |
|
581
|
|
|
} catch (\Exception $ex) { |
|
582
|
|
|
dd($ex); |
|
583
|
|
|
throw new \Exception(\Lang::get('message.check-tax-error')); |
|
584
|
|
|
} |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
public function pdf(Request $request) |
|
588
|
|
|
{ |
|
589
|
|
|
try { |
|
590
|
|
|
$id = $request->input('invoiceid'); |
|
591
|
|
|
if (!$id) { |
|
592
|
|
|
return redirect()->back()->with('fails', \Lang::get('message.no-invoice-id')); |
|
593
|
|
|
} |
|
594
|
|
|
$invoice = $this->invoice->where('id', $id)->first(); |
|
595
|
|
|
if (!$invoice) { |
|
596
|
|
|
return redirect()->back()->with('fails', \Lang::get('message.invalid-invoice-id')); |
|
597
|
|
|
} |
|
598
|
|
|
$invoiceItems = $this->invoiceItem->where('invoice_id', $id)->get(); |
|
599
|
|
|
$user = $this->user->find($invoice->user_id); |
|
600
|
|
|
//return view('themes.default1.invoice.pdfinvoice', compact('invoiceItems', 'invoice', 'user')); |
|
601
|
|
|
$pdf = \PDF::loadView('themes.default1.invoice.pdfinvoice', compact('invoiceItems', 'invoice', 'user')); |
|
602
|
|
|
|
|
603
|
|
|
return $pdf->download($user->first_name.'-invoice.pdf'); |
|
604
|
|
|
} catch (\Exception $ex) { |
|
605
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
606
|
|
|
} |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
public function calculateTotal($rate, $total) |
|
610
|
|
|
{ |
|
611
|
|
|
try { |
|
612
|
|
|
//dd($total); |
|
613
|
|
|
$rates = explode(',', $rate); |
|
614
|
|
|
//dd($rates); |
|
615
|
|
|
// $total = ''; |
|
616
|
|
|
$rule = new TaxOption(); |
|
617
|
|
|
$rule = $rule->findOrFail(1); |
|
618
|
|
|
if ($rule->tax_enable == 1 && $rule->inclusive == 0) { |
|
619
|
|
|
foreach ($rates as $rate) { |
|
620
|
|
|
$total += $total * ($rate / 100); |
|
621
|
|
|
} |
|
622
|
|
|
} |
|
623
|
|
|
//dd($total); |
|
624
|
|
|
return $total; |
|
625
|
|
|
} catch (\Exception $ex) { |
|
626
|
|
|
dd($ex); |
|
627
|
|
|
throw new \Exception($ex->getMessage()); |
|
628
|
|
|
} |
|
629
|
|
|
} |
|
630
|
|
|
|
|
631
|
|
|
/** |
|
632
|
|
|
* Remove the specified resource from storage. |
|
633
|
|
|
* |
|
634
|
|
|
* @param int $id |
|
|
|
|
|
|
635
|
|
|
* |
|
636
|
|
|
* @return Response |
|
637
|
|
|
*/ |
|
638
|
|
View Code Duplication |
public function destroy(Request $request) |
|
639
|
|
|
{ |
|
640
|
|
|
try { |
|
641
|
|
|
$ids = $request->input('select'); |
|
642
|
|
|
if (!empty($ids)) { |
|
643
|
|
|
foreach ($ids as $id) { |
|
|
|
|
|
|
644
|
|
|
$invoice = $this->invoice->where('id', $id)->first(); |
|
645
|
|
|
if ($invoice) { |
|
646
|
|
|
$invoice->delete(); |
|
647
|
|
|
} else { |
|
648
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
649
|
|
|
<i class='fa fa-ban'></i> |
|
650
|
|
|
<b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').' |
|
651
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
652
|
|
|
'.\Lang::get('message.no-record').' |
|
653
|
|
|
</div>'; |
|
654
|
|
|
//echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
|
655
|
|
|
} |
|
656
|
|
|
} |
|
657
|
|
|
echo "<div class='alert alert-success alert-dismissable'> |
|
658
|
|
|
<i class='fa fa-ban'></i> |
|
659
|
|
|
<b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.success').' |
|
660
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
661
|
|
|
'.\Lang::get('message.deleted-successfully').' |
|
662
|
|
|
</div>'; |
|
663
|
|
|
} else { |
|
664
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
665
|
|
|
<i class='fa fa-ban'></i> |
|
666
|
|
|
<b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').' |
|
667
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
668
|
|
|
'.\Lang::get('message.select-a-row').' |
|
669
|
|
|
</div>'; |
|
670
|
|
|
//echo \Lang::get('message.select-a-row'); |
|
671
|
|
|
} |
|
672
|
|
|
} catch (\Exception $e) { |
|
673
|
|
|
echo "<div class='alert alert-danger alert-dismissable'> |
|
674
|
|
|
<i class='fa fa-ban'></i> |
|
675
|
|
|
<b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').' |
|
676
|
|
|
<button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
|
677
|
|
|
'.$e->getMessage().' |
|
678
|
|
|
</div>'; |
|
679
|
|
|
} |
|
680
|
|
|
} |
|
681
|
|
|
|
|
682
|
|
|
public function domain($id) |
|
683
|
|
|
{ |
|
684
|
|
|
try { |
|
685
|
|
|
if (\Session::has('domain'.$id)) { |
|
686
|
|
|
$domain = \Session::get('domain'.$id); |
|
687
|
|
|
} else { |
|
688
|
|
|
$domain = ''; |
|
689
|
|
|
} |
|
690
|
|
|
|
|
691
|
|
|
return $domain; |
|
692
|
|
|
} catch (\Exception $ex) { |
|
|
|
|
|
|
693
|
|
|
} |
|
694
|
|
|
} |
|
695
|
|
|
|
|
696
|
|
|
public function updateInvoice($invoiceid) |
|
697
|
|
|
{ |
|
698
|
|
|
try { |
|
699
|
|
|
$invoice = $this->invoice->findOrFail($invoiceid); |
|
700
|
|
|
$payment = $this->payment->where('invoice_id', $invoiceid)->where('payment_status', 'success')->lists('amount')->toArray(); |
|
701
|
|
|
$total = array_sum($payment); |
|
702
|
|
|
if ($total < $invoice->grand_total) { |
|
703
|
|
|
$invoice->status = 'pending'; |
|
704
|
|
|
} |
|
705
|
|
|
if ($total >= $invoice->grand_total) { |
|
706
|
|
|
$invoice->status = 'success'; |
|
707
|
|
|
} |
|
708
|
|
|
if ($total > $invoice->grand_total) { |
|
709
|
|
|
$user = $invoice->user()->first(); |
|
710
|
|
|
$balance = $total - $invoice->grand_total; |
|
711
|
|
|
$user->debit = $balance; |
|
712
|
|
|
$user->save(); |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
$invoice->save(); |
|
716
|
|
|
} catch (\Exception $ex) { |
|
717
|
|
|
throw new \Exception($ex->getMessage()); |
|
718
|
|
|
} |
|
719
|
|
|
} |
|
720
|
|
|
} |
|
721
|
|
|
|
This check looks for function calls that miss required arguments.