-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCartInfo.php
143 lines (104 loc) · 3.49 KB
/
CartInfo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
require_once "./utilities.php";
class CartInfo
{
private array $productList = [];
private array $aisleListFromFile;
private int $productCount = 0;
private array $currentCart = [];
private array $totalArr = [];
public function __construct()
{
$this->fetchCart();
}
public function getTotalPriceAfterTax()
{
return $this->getTotalPriceBeforeTax() + $this->getQST() + $this->getGST();
}
public function getTotalPriceBeforeTax()
{
return $this->totalArr['total_price_before_tax'];
}
public function getGST()
{
return $this->totalArr['gst'];
}
public function getQST()
{
return $this->totalArr['qst'];
}
public function getElementTotalCost(int $id)
{
return $this->productList[$id]['totalCost'];
}
public function getElementCount($item_name): int
{
return $this->productList[$item_name]['count'];
}
public function getTotalAmount()
{
return $this->totalArr['total_amount'] ?? 0;
}
/**
* @return array
*/
public function getProductList(): array
{
return $this->productList;
}
public function addCart(int $id, int $amount, bool $overrideAmount = false)
{
if (!is_null(Utilities::findItem($id))) {
if (isset($this->currentCart[$id])) {
if ($overrideAmount) $this->currentCart[$id] = min($amount, Utilities::findItem($id)->Amount); else {
$this->currentCart[$id] = min(($this->currentCart[$id] + $amount), Utilities::findItem($id)->Amount);
}
} else {
$this->currentCart[$id] = min($amount, Utilities::findItem($id)->Amount);
}
$this->fetchCart();
return true;
}
return false;
}
public function removeCart($id): bool
{
$success = false;
$id = intval($id);
if (isset($this->productList[$id])) {
$success = true;
unset($this->currentCart[$id]);
}
$this->fetchCart();
return $success;
}
//todo fix cart stuff
public function fetchCart()
{
$productCount = 0.00;
$totalPriceBeforeTax = 0.00;
$totalAmount = 0.00;
$this->productList = [];
if ($this->currentCart != []) {
foreach ($this->currentCart as $cart_product_id => $amount) {
$contentArr = Utilities::findItem($cart_product_id);
if (!is_null($contentArr)) {
$contentArr = (array)$contentArr;
$productCount++;
$contentArr['Amount'] = (int)$amount;
$this->productList[$cart_product_id] = $contentArr;
$this->productList[$cart_product_id]['count'] = $productCount;
$this->productList[$cart_product_id]['totalCost'] = (float)number_format((float)$amount * (float)$contentArr['Price'], 2);
$totalPriceBeforeTax += $this->productList[$cart_product_id]['totalCost'];
$totalAmount += (int)$amount;
}
}
}
$totalGST = (float)$totalPriceBeforeTax * 0.05;
$totalQST = (float)$totalPriceBeforeTax * 0.0975;
$this->totalArr['total_price_before_tax'] = $totalPriceBeforeTax;
$this->totalArr['gst'] = $totalGST;
$this->totalArr['qst'] = $totalQST;
$this->totalArr['total_amount'] = $totalAmount;
}
}