-
Notifications
You must be signed in to change notification settings - Fork 0
/
viewcart.php
283 lines (248 loc) · 14.6 KB
/
viewcart.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php require_once('header.php'); ?>
<?php require_once('javascripts.php'); ?>
<?php
$_SESSION["vatabletax"]="";
$_SESSION["grandtotal"]="";
?>
<?php
function getstorename() {
$query = mysql_query("SELECT * FROM storeinfo WHERE storeid = ". $_SESSION['storeid']);
$numrows = mysql_num_rows($query);
if ( $numrows != 0 ) {
$row = mysql_fetch_assoc($query);
echo $row['storename'];
}
}
?>
<div class="container">
<div class="block-header">
<h2>
<b id="storename">
<?php getstorename(); ?>
</b>
Products
<small>Click Add to Cart for your order</small>
</h2>
</div>
<div class="card">
<div class="listview lv-bordered lv-lg">
<div class="lv-body">
<?php viewcart(); ?>
</div>
</div>
</div>
</div>
<?php require_once('footer.php'); ?>
<?php
function viewcart() {
$getitem = mysql_query("SELECT i.item_num, i.item_desc, i.item_itemonhand, i.item_price, c.item_qty,
(i.item_price * c.item_qty) AS subtotal
FROM item i, cart c
WHERE i.item_num = c.item_num
AND c.customer_num = ". $_SESSION['user_id']);
$num_rows = mysql_num_rows($getitem);
$gtotal = 0;
$vatTax = 0;
$vaTable = 0;
$num = 1;
$update = "btnUpdate";
$remove = "btnDelete";
$oqty = "txtoqty";
$originalqty = "txtoriginal";
$newtotal = "total";
$subhidden = "subtot";
while($res = mysql_fetch_assoc($getitem)) {
$holdersaupdate = $num . $update;
$holdersadelete = $num . $remove;
$holdersatxtbox = $num . $oqty;
$holderoriginalqty = $num . $originalqty;
$holdersanewtotal = $num . $newtotal;
$holdersasubtotalhidden = $num . $subhidden;
echo '<div class="lv-item media">
<div class="media-body">
<div class="lv-title"><h5><b>'. $res['item_desc'] .'</b></h5></div>
<div style="padding-top: 10px;">
<form method="post" action="viewcart.php?item_num='. $res['item_num'] .'">
<small>Price: '. $res['item_price'] .'
<input type="text" name="item_num" value="'. $res['item_num'] .'" hidden>
<span style="padding-left: 5px; padding-right: 5px;color:red"> x </span>
<input type="number" style="width:50px" min="1" max="'. $res['item_itemonhand'] .'" name="'.$holdersatxtbox.'" id="'.$holdersatxtbox.'" value="'. $res['item_qty'] .'"><span style="padding-left: 5px; padding-right: 5px;color:red"> = </span>
<span style="padding-right: 10px; font-weight: bold"><label id= "'.$holdersanewtotal.'">'. number_format($res['subtotal'], 2) .'</label><input type="text" id= "'.$holdersasubtotalhidden.'" value="'. number_format($res['subtotal'], 2) .'" hidden></span>
</small><br>
<input type="text" name="'.$holderoriginalqty.'" value="'.$res['item_qty'].'" style="display:none;">
<span style="padding-top: 10px">
<button type="submit" name="' .$holdersaupdate.'" class="btn bgm-amber waves-effect"><i class="zmdi zmdi-edit zmdi-hc-fw"></i></button>
<button type="submit" name="' .$holdersadelete.'" class="btn bgm-amber waves-effect"><i class="zmdi zmdi-delete zmdi-hc-fw"></i></button>
</span>
</div>
</div>
</div>';
$num++;
if(isset($_POST["$holdersaupdate"]) && isset($_POST["$holdersatxtbox"]))
{
$newqty = $_POST["$holdersatxtbox"];
$rqty = $_POST["$holdersatxtbox"];
$originalquantity = $_POST["$holderoriginalqty"];
$desc = $res['item_desc'];
$itemnumber = $res['item_num'];
if($rqty < $originalquantity)
{
$result = $originalquantity - $rqty;
$rqty = mysql_query("UPDATE cart SET item_qty = '$rqty'
WHERE item_num = '$itemnumber' and customer_num ='".$_SESSION['user_id']."'")
or die (mysql_error());
$update_stocks = mysql_query("UPDATE item
SET item_itemonhand = (item_itemonhand + $result) WHERE item_num = '$itemnumber'")
or die (mysql_error());
?>
<script type="text/javascript">
var itemprice = "<?php echo $res['item_price'];?>";
var newqty = "<?php echo $newqty;?>";
var qty_textbox = "<?php echo $holdersatxtbox;?>";
var newtotal_txtbox = "<?php echo $holdersanewtotal;?>";
var subtotal_txtbox = "<?php echo $holdersasubtotalhidden?>";
var newtotal = itemprice * newqty;
var oldsub = document.getElementById(subtotal_txtbox).value;
$(window).load(function(){
swal("Done", "A quantity on your cart has been deducted"+oldsub, "success");
var gtotal = document.getElementById('gtotal').value;
var comptotal = Number(oldsub) - Number(newtotal);
var newgtotal = Number(gtotal) - Number(comptotal);
var vatTax = Number(newgtotal) / 1.12;
var vatableTax = Number(vatTax) * .12;
document.getElementById(qty_textbox).value = newqty;
document.getElementById(newtotal_txtbox).innerHTML = newtotal.toFixed(2);
document.getElementById('gtotal').value = newgtotal.toFixed(2);
document.getElementById('vattax').innerHTML = vatableTax.toFixed(2);
document.getElementById('vatabletax').innerHTML = vatTax.toFixed(2);
});
</script>
<?php
}
elseif($rqty > $originalquantity)
{
$results = $rqty - $originalquantity;
$update_cart = mysql_query("UPDATE cart SET item_qty = '$rqty'
WHERE item_num = '$itemnumber' and customer_num ='".$_SESSION['user_id']."'")
or die (mysql_error());
$add_stocks = mysql_query("UPDATE item
SET item_itemonhand = (item_itemonhand - $results) WHERE item_num = '$itemnumber'")
or die (mysql_error());
?>
<script type="text/javascript">
var itemprice = "<?php echo $res['item_price'];?>";
var newqty = "<?php echo $newqty;?>";
var qty_textbox = "<?php echo $holdersatxtbox;?>";
var newtotal_txtbox = "<?php echo $holdersanewtotal;?>";
var subtotal_txtbox = "<?php echo $holdersasubtotalhidden?>";
var newtotal = itemprice * newqty;
var oldsub = document.getElementById(subtotal_txtbox).value;
$(window).load(function(){
swal("Done", "You added a quantity to order", "success");
var gtotal = document.getElementById('gtotal').value;
var comptotal = Number(newtotal) - Number(oldsub);
var newgtotal = Number(gtotal) + Number(comptotal);
var vatTax = Number(newgtotal) / 1.12;
var vatableTax = Number(vatTax) * .12;
document.getElementById(qty_textbox).value = newqty;
document.getElementById(newtotal_txtbox).innerHTML = newtotal.toFixed(2);
document.getElementById('gtotal').value = newgtotal.toFixed(2);
document.getElementById('vattax').innerHTML = vatableTax.toFixed(2);
document.getElementById('vatabletax').innerHTML = vatTax.toFixed(2);
});
</script>
<?php
}
}
//****************************************************************************************
//**** DELETE STARTS HERE ***
//****************************************************************************************
if(isset($_POST["$holdersadelete"]))
{
?>
<script type="text/javascript">
var itemnum = "<?php echo $res['item_num'];?>";
var cus_num = "<?php echo $_SESSION['user_id'];?>";
$(window).load(function(){
swal({
title: "Are you sure?",
text: "You will not be able to recover this item!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel pls!",
closeOnConfirm: false,
closeOnCancel: false
}, function(isConfirm){
if (isConfirm) {
$.post("delcartitem.php",{itemnum : itemnum, cusnum : cus_num}, function(data){
if(data == "success")
{
swal("Deleted!", "Your selected item has been deleted.", "success");
window.location.href='viewcart.php';
}
else if(data == "error")
{
swal("Cancelled", "Error Deleting item on cart :(", "error");
}
else
{
swal("Cancelled", "Error Deleting item on cart :((", "error");
}
});
} else {
swal("Cancelled", "Your Item is not deleted :)", "error");
}
});
});
</script>
<?php
}
$gtotal += $res['subtotal'];
$vatTax = $gtotal / 1.12 * 0.12;
$vaTable = $gtotal - $vatTax;
}
echo '<div class="lv-item media">
<div class="media-body" style="float: right">
<table>
<tr>
<td><small style="padding-right: 10px;">Total</small></td>
<td><small style="font-weight: bold"><input type="text" style="border-color:#fff;border-style:solid;" id="gtotal" value="'. number_format($gtotal, 2) .'"></small></td>
</tr>
<tr>
<td><small style="padding-right: 10px;">Vat Tax</small></td>
<td><small style="font-weight: bold"><label id="vattax">'. number_format($vatTax, 2) .'</label></small></td>
</tr>
<tr>
<td><small style="padding-right: 10px;">VATable</small></td>
<td><small style="font-weight: bold"><label id="vatabletax">'. number_format($vaTable, 2) .'</label></small></td>
</tr>
</table><br>
<button type="submit" name="proceed" class="btn bgm-deeporange waves-effect">
Proceed
<i class="zmdi zmdi-arrow-forward"></i>
</button>
</div>
</div>';
if(isset($_POST["proceed"]))
{
$_SESSION["vatabletax"] = $vatTax;
$_SESSION["grandtotal"]= $gtotal;
if($gtotal <= 1000)
{
?>
<script type="text/javascript">
$(window).load(function(){
swal("Total amount must be greater than 1000 pesos")
});
</script>
<?php
}
else
{
echo "<script>window.location='rtype.php'</script>";
}
}
}
?>