|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Front; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Common\TemplateController; |
|
6
|
|
|
use App\Http\Controllers\Controller; |
|
7
|
|
|
use App\Model\Payment\Currency; |
|
8
|
|
|
use App\Model\Payment\Tax; |
|
9
|
|
|
use App\Model\Payment\TaxRules; |
|
10
|
|
|
use App\Model\Product\Product; |
|
11
|
|
|
use Cart; |
|
12
|
|
|
use Illuminate\Http\Request; |
|
13
|
|
|
|
|
14
|
|
|
class CartController extends Controller { |
|
15
|
|
|
|
|
16
|
|
|
public $templateController; |
|
17
|
|
|
public $product; |
|
18
|
|
|
public $currency; |
|
19
|
|
|
public $taxRules; |
|
20
|
|
|
public $addons; |
|
21
|
|
|
public $addonRelation; |
|
22
|
|
|
public $licence; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct() { |
|
25
|
|
|
$templateController = new TemplateController(); |
|
26
|
|
|
$this->templateController = $templateController; |
|
27
|
|
|
|
|
28
|
|
|
$product = new Product(); |
|
29
|
|
|
$this->product = $product; |
|
30
|
|
|
|
|
31
|
|
|
$currency = new Currency(); |
|
32
|
|
|
$this->currency = $currency; |
|
33
|
|
|
|
|
34
|
|
|
$tax = new Tax(); |
|
35
|
|
|
$this->tax = $tax; |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
$taxRules = new TaxRules(); |
|
38
|
|
|
$this->taxRules = $taxRules; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function ProductList(Request $request) { |
|
42
|
|
|
|
|
43
|
|
|
if (!$request->has('currency')) { |
|
44
|
|
|
$currency = "USD"; |
|
45
|
|
|
} else { |
|
46
|
|
|
$currency = $request->input('currency'); |
|
47
|
|
|
} |
|
48
|
|
|
\Session::put('currency', $currency); |
|
49
|
|
|
if (!\Session::has('currency')) { |
|
50
|
|
|
\Session::put('currency', 'USD'); |
|
51
|
|
|
//dd(\Session::get('currency')); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
return $this->templateController->show(1); |
|
57
|
|
|
} catch (\Exception $ex) { |
|
58
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function Cart(Request $request) { |
|
63
|
|
|
try { |
|
64
|
|
|
$id = $request->input('id'); |
|
65
|
|
|
if (!array_key_exists($id, Cart::getContent()->toArray())) { |
|
66
|
|
|
$items = $this->AddProduct($id); |
|
67
|
|
|
Cart::add($items); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$cartCollection = Cart::getContent(); |
|
71
|
|
|
foreach($cartCollection as $item){ |
|
72
|
|
|
$attributes[]=$item->attributes; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
return view('themes.default1.front.cart', compact('cartCollection','attributes')); |
|
75
|
|
|
} catch (\Exception $ex) { |
|
76
|
|
|
dd($ex); |
|
77
|
|
|
|
|
78
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function CheckTax($isTaxApply,$id) { |
|
83
|
|
|
try { |
|
84
|
|
|
$rate1 = 0; |
|
85
|
|
|
$rate2 = 0; |
|
86
|
|
|
$name1 = 'null'; |
|
87
|
|
|
$name2 = 'null'; |
|
88
|
|
|
$ruleEnabled = $this->taxRules->where('id', '1')->first(); |
|
89
|
|
|
if ($ruleEnabled) { |
|
90
|
|
|
$enabled = $ruleEnabled->status; |
|
91
|
|
|
$type = $ruleEnabled->type; |
|
92
|
|
|
$compound = $ruleEnabled->compound; |
|
93
|
|
|
if ($enabled == 1 && $type == 'exclusive') { |
|
94
|
|
|
if ($isTaxApply == 1) { |
|
95
|
|
|
$tax1 = $this->tax->where('level', 1)->first(); |
|
96
|
|
|
$tax2 = $this->tax->where('level', 2)->first(); |
|
97
|
|
View Code Duplication |
if ($tax1) { |
|
98
|
|
|
$name1 = $tax1->name; |
|
99
|
|
|
$rate1 = $tax1->rate; |
|
100
|
|
|
$taxCondition1 = new \Darryldecode\Cart\CartCondition([ |
|
101
|
|
|
'name' => $name1, |
|
102
|
|
|
'type' => 'tax', |
|
103
|
|
|
'target' => 'item', |
|
104
|
|
|
'value' => $rate1 . '%', |
|
105
|
|
|
]); |
|
106
|
|
|
} else { |
|
107
|
|
|
$taxCondition1 = new \Darryldecode\Cart\CartCondition([ |
|
108
|
|
|
'name' => $name1, |
|
109
|
|
|
'type' => 'tax', |
|
110
|
|
|
'target' => 'item', |
|
111
|
|
|
'value' => $rate1, |
|
112
|
|
|
]); |
|
113
|
|
|
} |
|
114
|
|
View Code Duplication |
if ($tax2) { |
|
115
|
|
|
$name2 = $tax2->name; |
|
116
|
|
|
$rate2 = $tax2->rate; |
|
117
|
|
|
$taxCondition2 = new \Darryldecode\Cart\CartCondition([ |
|
118
|
|
|
'name' => $name2, |
|
119
|
|
|
'type' => 'tax', |
|
120
|
|
|
'target' => 'item', |
|
121
|
|
|
'value' => $rate2 . '%', |
|
122
|
|
|
]); |
|
123
|
|
|
} else { |
|
124
|
|
|
$taxCondition2 = new \Darryldecode\Cart\CartCondition([ |
|
125
|
|
|
'name' => $name2, |
|
126
|
|
|
'type' => 'tax', |
|
127
|
|
|
'target' => 'item', |
|
128
|
|
|
'value' => $rate2, |
|
129
|
|
|
]); |
|
130
|
|
|
} |
|
131
|
|
|
} else { |
|
132
|
|
|
$taxCondition1 = new \Darryldecode\Cart\CartCondition([ |
|
133
|
|
|
'name' => $name1, |
|
134
|
|
|
'type' => 'tax', |
|
135
|
|
|
'target' => 'item', |
|
136
|
|
|
'value' => $rate1, |
|
137
|
|
|
]); |
|
138
|
|
|
$taxCondition2 = new \Darryldecode\Cart\CartCondition([ |
|
139
|
|
|
'name' => $name2, |
|
140
|
|
|
'type' => 'tax', |
|
141
|
|
|
'target' => 'item', |
|
142
|
|
|
'value' => $rate2, |
|
143
|
|
|
]); |
|
144
|
|
|
} |
|
145
|
|
|
$currency_attribute = $this->addCurrencyAttributes($id); |
|
146
|
|
|
//dd($currency_attribute); |
|
147
|
|
|
if ($compound == 1) { |
|
148
|
|
|
return ['conditions' => [$taxCondition1, $taxCondition2], 'attributes' => ['tax' => [['name' => $name1, 'rate' => $rate1], ['name' => $name2, 'rate' => $rate2]],'currency'=>$currency_attribute]]; |
|
149
|
|
|
} else { |
|
150
|
|
|
return ['conditions' => $taxCondition2, 'attributes' => ['tax' => [['name' => $name2, 'rate' => $rate2]],'currency'=>$currency_attribute]]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} catch (\Exception $ex) { |
|
155
|
|
|
dd($ex); |
|
156
|
|
|
throw new \Exception('Can not check the tax'); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function CartRemove(Request $request) { |
|
161
|
|
|
$id = $request->input('id'); |
|
162
|
|
|
//dd($id); |
|
163
|
|
|
Cart::remove($id); |
|
164
|
|
|
|
|
165
|
|
|
return 'success'; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
View Code Duplication |
public function ReduseQty(Request $request) { |
|
169
|
|
|
$id = $request->input('id'); |
|
170
|
|
|
Cart::update($id, [ |
|
171
|
|
|
'quantity' => -1, // so if the current product has a quantity of 4, it will subtract 1 and will result to 3 |
|
172
|
|
|
]); |
|
173
|
|
|
//dd(Cart::getContent()); |
|
|
|
|
|
|
174
|
|
|
return 'success'; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
View Code Duplication |
public function IncreaseQty(Request $request) { |
|
178
|
|
|
$id = $request->input('id'); |
|
179
|
|
|
Cart::update($id, [ |
|
180
|
|
|
'quantity' => +1, // so if the current product has a quantity of 4, it will add 1 and will result to 5 |
|
181
|
|
|
]); |
|
182
|
|
|
//dd(Cart::getContent()); |
|
|
|
|
|
|
183
|
|
|
return 'success'; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
public function AddAddons($id) { |
|
187
|
|
|
$addon = $this->addons->where('id', $id)->first(); |
|
188
|
|
|
|
|
189
|
|
|
$isTaxApply = $addon->tax_addon; |
|
190
|
|
|
|
|
191
|
|
|
$taxConditions = $this->CheckTax($isTaxApply); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
$items = ['id' => 'addon' . $addon->id, 'name' => $addon->name, 'price' => $addon->selling_price, 'quantity' => 1]; |
|
194
|
|
|
$items = array_merge($items, $taxConditions); |
|
195
|
|
|
|
|
196
|
|
|
//dd($items); |
|
197
|
|
|
|
|
198
|
|
|
return $items; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
public function GetProductAddons($productId) { |
|
202
|
|
|
$addons = []; |
|
203
|
|
|
if ($this->addonRelation->where('product_id', $productId)->count() > 0) { |
|
204
|
|
|
$addid = $this->addonRelation->where('product_id', $productId)->pluck('addon_id')->toArray(); |
|
205
|
|
|
$addons = $this->addons->whereIn('id', $addid)->get(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $addons; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function AddProduct($id) { |
|
212
|
|
|
$currency = \Session::get('currency'); |
|
213
|
|
|
// if (!$currency) { |
|
|
|
|
|
|
214
|
|
|
// $currency = 'USD'; |
|
215
|
|
|
// } |
|
216
|
|
|
$product = $this->product->where('id', $id)->first(); |
|
217
|
|
|
if ($product) { |
|
218
|
|
|
$productCurrency = $product->price()->where('currency', $currency)->first()->currency; |
|
219
|
|
|
$actualPrice = $product->price()->where('currency', $currency)->first()->sales_price; |
|
220
|
|
|
if (!$actualPrice) { |
|
221
|
|
|
$actualPrice = $product->price()->where('currency', $currency)->first()->price; |
|
222
|
|
|
} |
|
223
|
|
|
$currency = $this->currency->where('code', $productCurrency)->get()->toArray(); |
|
224
|
|
|
|
|
225
|
|
|
$productName = $product->name; |
|
226
|
|
|
|
|
227
|
|
|
/* |
|
228
|
|
|
* Check the Tax is On |
|
229
|
|
|
*/ |
|
230
|
|
|
$isTaxApply = $product->tax_apply; |
|
231
|
|
|
|
|
232
|
|
|
$taxConditions = $this->CheckTax($isTaxApply,$id); |
|
233
|
|
|
//dd($taxConditions); |
|
234
|
|
|
|
|
235
|
|
|
/* |
|
236
|
|
|
* Check if this product allow multiple qty |
|
237
|
|
|
*/ |
|
238
|
|
|
if ($product->multiple_qty == 1) { |
|
|
|
|
|
|
239
|
|
|
// Allow |
|
240
|
|
|
} else { |
|
241
|
|
|
$qty = 1; |
|
242
|
|
|
} |
|
243
|
|
|
$items = ['id' => $id, 'name' => $productName, 'price' => $actualPrice, 'quantity' => $qty, 'attributes' => ['currency' => [[$currency]]]]; |
|
|
|
|
|
|
244
|
|
|
$items = array_merge($items, $taxConditions); |
|
245
|
|
|
|
|
246
|
|
|
return $items; |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
public function ClearCart() { |
|
251
|
|
|
Cart::clear(); |
|
252
|
|
|
|
|
253
|
|
|
return redirect('home'); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function LicenceCart($id) { |
|
257
|
|
|
try { |
|
258
|
|
|
$licence = $this->licence->where('id', $id)->first(); |
|
259
|
|
|
|
|
260
|
|
|
$isTaxApply = 0; |
|
261
|
|
|
|
|
262
|
|
|
$taxConditions = $this->CheckTax($isTaxApply); |
|
|
|
|
|
|
263
|
|
|
|
|
264
|
|
|
$items = ['id' => $licence->id, 'name' => $licence->name, 'price' => $licence->price, 'quantity' => 1, 'attributes' => ['number_of_sla' => $licence->number_of_sla]]; |
|
265
|
|
|
$items = array_merge($items, $taxConditions); |
|
266
|
|
|
Cart::clear(); |
|
267
|
|
|
Cart::add($items); |
|
268
|
|
|
|
|
269
|
|
|
return view('themes.default1.front.cart', compact('cartCollection')); |
|
270
|
|
|
} catch (\Exception $ex) { |
|
271
|
|
|
dd($ex); |
|
272
|
|
|
throw new \Exception('Problem while adding licence to cart'); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
public function cartUpdate($id, $key, $value) { |
|
277
|
|
|
try { |
|
278
|
|
|
Cart::update($id, [ |
|
279
|
|
|
$key => $value, // new item name |
|
280
|
|
|
] |
|
281
|
|
|
); |
|
282
|
|
|
} catch (\Exception $ex) { |
|
283
|
|
|
|
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
public function addCurrencyAttributes($id) { |
|
288
|
|
|
try { |
|
289
|
|
|
$currency = \Session::get('currency'); |
|
290
|
|
|
$product = $this->product->where('id', $id)->first(); |
|
291
|
|
|
//dd($product); |
|
292
|
|
|
if ($product) { |
|
293
|
|
|
$productCurrency = $product->price()->where('currency', $currency)->first()->currency; |
|
294
|
|
|
$currency = $this->currency->where('code', $productCurrency)->get()->toArray(); |
|
295
|
|
|
}else{ |
|
296
|
|
|
$currency=[]; |
|
297
|
|
|
} |
|
298
|
|
|
return $currency; |
|
299
|
|
|
} catch (\Exception $ex) { |
|
|
|
|
|
|
300
|
|
|
|
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
} |
|
305
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: