ifrogz-panel /admin/minicart2.php

Language PHP Lines 688
MD5 Hash 081ef788fefc91f38624088230fece11 Estimated Cost $14,312 (why?)
Repository https://bitbucket.org/chadsaun/ifrogz-panel.git View Raw File View Project SPDX
  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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<?php
session_start();
include('init.php');
include(APPPATH.'views/partials/admin/dbconnection.php');
include(APPPATH.'views/partials/admin/includes.php');
include(APPPATH.'views/partials/admin/languagefile.php');
include(APPPATH.'views/partials/admin/functions.php');

$Btestdiscount=$_REQUEST['Btestdiscount'];
$autodiscount=$_REQUEST['autodiscount'];
//echo '='.$Btestdiscount;
if($_POST['show']=="FALSE"){
	$sSQL = "SELECT * FROM cart WHERE cartCompleted=0 AND cartSessionID='" .  session_id() . "'";
	$result = mysql_query($sSQL) or print(mysql_error());
	$num_rows=mysql_num_rows($result);
	if($num_rows==1) $item="item";
	else $item="items";
	echo $num_rows." ".$item;
} else { ?>	
	
<?php
	//echo 'id= '.$_POST["id"];
	//exit();
	$isInStock=TRUE;
	$WSP = "";
	$OWSP = "";
	$theid = mysql_real_escape_string(trim(@$_POST["id"]));
	if(@$dateadjust=="") $dateadjust=0;
	$alreadygotadmin = getadminsettings();
	if(@$_SESSION["clientUser"] != ""){
		if(($_SESSION["clientActions"] & 8) == 8){
			$WSP = "pWholesalePrice AS ";
			if(@$wholesaleoptionpricediff==TRUE) $OWSP = 'optWholesalePriceDiff AS ';
			if(@$nowholesalediscounts==TRUE) $nodiscounts=TRUE;
		}
		if(($_SESSION["clientActions"] & 16) == 16){
			$WSP = $_SESSION["clientPercentDiscount"] . "*pPrice AS ";
			if(@$wholesaleoptionpricediff==TRUE) $OWSP = $_SESSION["clientPercentDiscount"] . '*optPriceDiff AS ';
			if(@$nowholesalediscounts==TRUE) $nodiscounts=TRUE;
		}
	}
	if(@$_SESSION["clientLoginLevel"] != "") $minloglevel=$_SESSION["clientLoginLevel"]; else $minloglevel=0;

	/*if($stockManage != 0){
		$sSQL = "SELECT cartOrderID,cartID FROM cart WHERE (cartCompleted=0 AND cartOrderID=0 AND cartDateAdded<'" . date("Y-m-d H:i:s", time()+(($dateadjust-$stockManage)*60*60)) . "')";
		if($delAfter != 0)
			$sSQL .= " OR (cartCompleted=0 AND cartDateAdded<'" . date("Y-m-d H:i:s", time()-($delAfter*60*60*24)) . "')";
		$result = mysql_query($sSQL) or print(mysql_error());
		if(mysql_num_rows($result)>0){
			$addcomma = "";
			$delstr="";
			$delcart="";
			while($rs = mysql_fetch_assoc($result)){
				$delcart .= $addcomma . $rs["cartOrderID"];
				$delstr .= $addcomma . $rs["cartID"];
				$addcomma = ",";
			}
			if($delAfter != 0) mysql_query("DELETE FROM orders WHERE ordID IN (" . $delcart . ")") or print(mysql_error());
			mysql_query("DELETE FROM cart WHERE cartID IN (" . $delstr . ")") or print(mysql_error());
			mysql_query("DELETE FROM cartoptions WHERE coCartID IN (" . $delstr . ")") or print(mysql_error());
		}
		mysql_free_result($result);
	}*/
	
	
$thesessionid=session_id();	
function addadiscount($resset, $groupdiscount, $dscamount, $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, $theexemptions, $thetax){
	global $totaldiscounts, $cpnmessage, $statetaxfree, $countrytaxfree, $gotcpncode, $perproducttaxrate, $countryTax , $cpnIDs , $appliedcouponname , $appliedcouponamount,$thesessionid;
	$totaldiscounts += $dscamount;
	if($groupdiscount){
		$statetaxfree -= ($dscamount * $statetaxhandback);
		$countrytaxfree -= ($dscamount * $countrytaxhandback);
	}else{
		subtaxesfordiscounts($theexemptions, $dscamount);
		if(@$perproducttaxrate) $countryTax -= (($dscamount * $thetax) / 100.0);
	}
	$usecoupon=TRUE;
	//echo '$cdcpncode='.$cdcpncode;
	$cpnDiscount=substr($resset['cpnWorkingName'],0,10);
	//echo '$cpnDiscount='.$cpnDiscount;
	$sql_dd="SELECT * FROM daily_discounts WHERE starttime LIKE '".$cpnDiscount."%'";
	//echo $sql_dd;
	$result_dd=mysql_query($sql_dd);
	if(mysql_num_rows($result_dd)>0){
		$row_dd=mysql_fetch_assoc($result_dd);
		//echo 'starttime='.$row_dd['starttime'];
		$sql_d="SELECT cartDateAdded FROM cart WHERE cartSessionID='".$thesessionid."' AND cartDateAdded<'".$row_dd['starttime']."'";
		//echo $sql_d;
		$result_d=mysql_query($sql_d);
		if(mysql_num_rows($result_d)>0){
			echo '<div style="color:#FF0000; font-weight:bold; margin:4px; font-size:14px;"> You must add the Snatch It product to the cart after the green light flashes to get it for free.</div>';			
			$dscamount=0;
			$totaldiscounts=0;			
			$usecoupon=FALSE;
		}		
	}
	//echo '$dscamount='.$dscamount;
	//echo '$totaldiscounts='.$totaldiscounts;
	
	//$dscamount=0;
	//$totaldiscounts=0;
	if($usecoupon){
		//echo 'used';
		if(stristr($cpnmessage,"<br />" . $resset[getlangid("cpnName",1024)] . "<br />") == FALSE) $cpnmessage .= $resset[getlangid("cpnName",1024)] . "<br />";
		// added Blake 1/2/07
		// add discount ids to order
		$comma=",";
		if(empty($cpnIDs)) $comma=" ";
		$cpnIDs .= $comma.$resset["cpnID"];
		// end
		if($subcpns){
			$theres = mysql_query("SELECT cpnID FROM coupons WHERE cpnNumAvail>0 AND cpnNumAvail<30000000 AND cpnID=" . $resset["cpnID"]) or print(mysql_error());
			if($theresset = mysql_fetch_assoc($theres)) @$_SESSION["couponapply"] .= "," . $resset["cpnID"];
			mysql_query("UPDATE coupons SET cpnNumAvail=cpnNumAvail-1 WHERE cpnNumAvail>0 AND cpnNumAvail<30000000 AND cpnID=" . $resset["cpnID"]) or print(mysql_error());
		}
		if($cdcpncode!="" && strtolower(trim($resset["cpnNumber"]))==strtolower($cdcpncode)) {$gotcpncode=TRUE;$appliedcouponname = $resset['cpnName']; $appliedcouponamount = $dscamount; }
	}
}
function subtaxesfordiscounts($theExemptions, $discAmount){
	global $statetaxfree,$countrytaxfree,$shipfreegoods;
	if(($theExemptions & 1)==1) $statetaxfree -= $discAmount;
	if(($theExemptions & 2)==2) $countrytaxfree -= $discAmount;
	if(($theExemptions & 4)==4) $shipfreegoods -= $discAmount;
}
function timesapply($taquant,$tathresh,$tamaxquant,$tamaxthresh,$taquantrepeat,$tathreshrepeat){
	if($taquantrepeat==0 && $tathreshrepeat==0)
		$tatimesapply = 1.0;
	elseif($tamaxquant==0)
		$tatimesapply = (int)(($tathresh - $tamaxthresh) / $tathreshrepeat)+1;
	elseif($tamaxthresh==0)
		$tatimesapply = (int)(($taquant - $tamaxquant) / $taquantrepeat)+1;
	else{
		$ta1 = (int)(($taquant - $tamaxquant) / $taquantrepeat)+1;
		$ta2 = (int)(($tathresh - $tamaxthresh) / $tathreshrepeat)+1;
		if($ta2 < $ta1) $tatimesapply = $ta2; else $tatimesapply = $ta1;
	}
	return($tatimesapply);
}	
function calculatediscounts($cdgndtot, $subcpns, $cdcpncode){
	global $totaldiscounts, $cpnmessage, $statetaxfree, $countrytaxfree, $nodiscounts, $WSP, $cpncode, $gotcpncode, $thesessionid, $countryTaxRate, $countryTax;
	$totaldiscounts = 0;
	$cpnmessage = "<br />";
	$cdtotquant=0;
	if($cdgndtot==0){
		$statetaxhandback = 0.0;
		$countrytaxhandback = 0.0;
	}else{
		$statetaxhandback = 1.0 - (($cdgndtot - $statetaxfree) / $cdgndtot);
		$countrytaxhandback = 1.0 - (($cdgndtot - $countrytaxfree) / $cdgndtot);
	}
	if(! $nodiscounts){
		$sSQL = "SELECT cartProdID,SUM(cartProdPrice*cartQuantity) AS thePrice,SUM(cartQuantity) AS sumQuant,pSection,COUNT(cartProdID),pExemptions,pTax FROM products INNER JOIN cart ON cart.cartProdID=products.pID WHERE cartCompleted=0 AND cartSessionID='" . $thesessionid . "' GROUP BY cartProdID,pSection,pExemptions,pTax";
		$cdresult = mysql_query($sSQL) or print(mysql_error());
		$cdadindex=0;
		while($cdrs = mysql_fetch_assoc($cdresult)){
			$cdalldata[$cdadindex++]=$cdrs;
		}
		for($index=0; $index<$cdadindex; $index++){
			$cdrs = $cdalldata[$index];
			$sSQL = "SELECT SUM(coPriceDiff*cartQuantity) AS totOpts FROM cart LEFT OUTER JOIN cartoptions ON cart.cartID=cartoptions.coCartID WHERE cartCompleted=0 AND cartSessionID='" . $thesessionid . "' AND cartProdID='" . $cdrs["cartProdID"] . "'";
			$cdresult2 = mysql_query($sSQL) or print(mysql_error());
			$cdrs2 = mysql_fetch_assoc($cdresult2);
			if(! is_null($cdrs2["totOpts"])) $cdrs["thePrice"] += $cdrs2["totOpts"];
			$cdtotquant += $cdrs["sumQuant"];
			$topcpnids = $cdrs["pSection"];
			$thetopts = $cdrs["pSection"];
			if(is_null($cdrs["pTax"])) $cdrs["pTax"] = $countryTaxRate;
			for($cpnindex=0; $cpnindex<= 10; $cpnindex++){
				if($thetopts==0)
					break;
				else{
					$sSQL = "SELECT topSection FROM sections WHERE sectionID=" . $thetopts;
					$result2 = mysql_query($sSQL) or print(mysql_error());
					if($rs2 = mysql_fetch_assoc($result2)){
						$thetopts = $rs2["topSection"];
						$topcpnids .= "," . $thetopts;
					}else
						break;
				}
			}
			$sSQL = "SELECT DISTINCT cpnID,cpnDiscount,cpnWorkingName,cpnType,cpnNumber,".getlangid("cpnName",1024).",cpnThreshold,cpnQuantity,cpnSitewide,cpnThresholdRepeat,cpnQuantityRepeat FROM coupons LEFT OUTER JOIN cpnassign ON coupons.cpnID=cpnassign.cpaCpnID WHERE cpnNumAvail>0 AND cpnEndDate>='" . date("Y-m-d H:i:s",time()) ."' AND cpnBeginDate <= '" . date("Y-m-d H:i:s",time()) ."' AND (cpnIsCoupon=0";
			if($cdcpncode != "") $sSQL .= " OR (cpnIsCoupon=1 AND cpnNumber='" . $cdcpncode . "')";
			$sSQL .= ") AND cpnThreshold<=" . $cdrs["thePrice"] . " AND (cpnThresholdMax>" . $cdrs["thePrice"] . " OR cpnThresholdMax=0) AND cpnQuantity<=" . $cdrs["sumQuant"] . " AND (cpnQuantityMax>" . $cdrs["sumQuant"] . " OR cpnQuantityMax=0) AND (cpnSitewide=0 OR cpnSitewide=2) AND ";
			$sSQL .= "(cpnSitewide=2 OR (cpaType=2 AND cpaAssignment='" . $cdrs["cartProdID"] . "') ";
			$sSQL .= "OR (cpaType=1 AND cpaAssignment IN ('" . str_replace(",","','",$topcpnids) . "')))";
			if(!empty($WSP)) $sSQL .= " AND (cpnIsWholesale=1)";
			else $sSQL .= " AND (cpnIsWholesale=0)";
			$result2 = mysql_query($sSQL) or print(mysql_error());
			//echo $sSQL;
			while($rs2 = mysql_fetch_assoc($result2)){
				if($rs2["cpnType"]==1){ // Flat Rate Discount
					$thedisc = (double)$rs2["cpnDiscount"] * timesapply($cdrs["sumQuant"], $cdrs["thePrice"], $rs2["cpnQuantity"], $rs2["cpnThreshold"], $rs2["cpnQuantityRepeat"], $rs2["cpnThresholdRepeat"]);
					if($cdrs["thePrice"] < $thedisc) $thedisc = $cdrs["thePrice"];
					addadiscount($rs2, FALSE, $thedisc, $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, $cdrs["pExemptions"], $cdrs["pTax"]);
				}elseif($rs2["cpnType"]==2){ // Percentage Discount
					addadiscount($rs2, FALSE, (((double)$rs2["cpnDiscount"] * (double)$cdrs["thePrice"]) / 100.0), $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, $cdrs["pExemptions"], $cdrs["pTax"]);
				}
			}
		}
		$sSQL = "SELECT DISTINCT cpnID,cpnDiscount,cpnWorkingName,cpnType,cpnNumber,".getlangid("cpnName",1024).",cpnSitewide,cpnThreshold,cpnThresholdMax,cpnQuantity,cpnQuantityMax,cpnThresholdRepeat,cpnQuantityRepeat FROM coupons WHERE cpnNumAvail>0 AND cpnEndDate>='" . date("Y-m-d H:i:s",time()) ."' AND cpnBeginDate <= '" . date("Y-m-d H:i:s",time()) ."' AND (cpnIsCoupon=0";
		if($cdcpncode != "") $sSQL .= " OR (cpnIsCoupon=1 AND cpnNumber='" . $cdcpncode . "')";
		$sSQL .= ") AND cpnThreshold<=" . $cdgndtot . " AND cpnQuantity<=" . $cdtotquant . " AND (cpnSitewide=1 OR cpnSitewide=3) AND (cpnType=1 OR cpnType=2)";
		if(!empty($WSP)) $sSQL .= " AND (cpnIsWholesale=1)";
		else $sSQL .= " AND (cpnIsWholesale=0)";
		$result2 = mysql_query($sSQL) or print(mysql_error());
		while($rs2 = mysql_fetch_assoc($result2)){
			$totquant = 0;
			$totprice = 0;
			if($rs2["cpnSitewide"]==3){
				$sSQL = "SELECT cpaAssignment FROM cpnassign WHERE cpaType=1 AND cpacpnID=" . $rs2["cpnID"];
				$result3 = mysql_query($sSQL) or print(mysql_error());
				$secids = "";
				$addcomma = "";
				while($rs3 = mysql_fetch_assoc($result3)){
					$secids .= $addcomma . $rs3["cpaAssignment"];
					$addcomma = ",";
				}
				if($secids != ""){
					$secids = getsectionids($secids, FALSE);
					$sSQL = "SELECT SUM(cartProdPrice*cartQuantity) AS totPrice,SUM(cartQuantity) AS totQuant FROM products INNER JOIN cart ON cart.cartProdID=products.pID WHERE cartCompleted=0 AND cartSessionID='" . $thesessionid . "' AND products.pSection IN (" . $secids . ")";
					$result3 = mysql_query($sSQL) or print(mysql_error());
					$rs3 = mysql_fetch_assoc($result3);
					if(is_null($rs3["totPrice"])) $totprice = 0; else $totprice = $rs3["totPrice"];
					if(is_null($rs3["totQuant"])) $totquant=0; else $totquant = $rs3["totQuant"];
					$sSQL = "SELECT SUM(coPriceDiff*cartQuantity) AS optPrDiff FROM products INNER JOIN cart ON cart.cartProdID=products.pID LEFT OUTER JOIN cartoptions ON cart.cartID=cartoptions.coCartID WHERE cartCompleted=0 AND cartSessionID='" . $thesessionid . "' AND products.pSection IN (" . $secids . ")";
					$result3 = mysql_query($sSQL) or print(mysql_error());
					$rs3 = mysql_fetch_assoc($result3);
					if(! is_null($rs3["optPrDiff"])) $totprice = $totprice+$rs3["optPrDiff"];
				}
			}else{ // cpnSitewide==1
				$totquant = $cdtotquant;
				$totprice = $cdgndtot;
			}
			if($totquant > 0 && $rs2["cpnThreshold"] <= $totprice && ($rs2["cpnThresholdMax"] > $totprice || $rs2["cpnThresholdMax"]==0) && $rs2["cpnQuantity"] <= $totquant && ($rs2["cpnQuantityMax"] > $totquant || $rs2["cpnQuantityMax"]==0)){
				if($rs2["cpnType"]==1){ // Flat Rate Discount
					$thedisc = (double)$rs2["cpnDiscount"] * timesapply($totquant, $totprice, $rs2["cpnQuantity"], $rs2["cpnThreshold"], $rs2["cpnQuantityRepeat"], $rs2["cpnThresholdRepeat"]);
					if($totprice < $thedisc) $thedisc = $totprice;
				}elseif($rs2["cpnType"]==2){ // Percentage Discount
					$thedisc = ((double)$rs2["cpnDiscount"] * (double)$totprice) / 100.0;
				}
				addadiscount($rs2, TRUE, $thedisc, $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, 3, 0);
				if(@$perproducttaxrate && $cdgndtot > 0){
					for($index=0; $index<$cdadindex; $index++){
						$cdrs = $cdalldata[$index];
						if($rs2["cpnType"]==1) // Flat Rate Discount
							$applicdisc = $thedisc / ($cdtotquant / $cdrs["sumQuant"]);
						elseif($rs2["cpnType"]==2) // Percentage Discount
							$applicdisc = $thedisc / ($cdgndtot / $cdrs["thePrice"]);
						if(($cdrs["pExemptions"] & 2) != 2) $countryTax -= (($applicdisc * $cdrs["pTax"]) / 100.0);
					}
				}
			}
		}
	}
	if($statetaxfree < 0) $statetaxfree = 0;
	if($countrytaxfree < 0) $countrytaxfree = 0;
	$totaldiscounts = round($totaldiscounts, 2);
	
	//echo '$totaldiscounts='.$totaldiscounts;
}

function checkpricebreaks($cpbpid,$origprice){
	global $WSP;
	$newprice="";
	$sSQL = "SELECT SUM(cartQuantity) AS totquant FROM cart WHERE cartCompleted=0 AND cartSessionID='" . session_id() . "' AND cartProdID='".mysql_real_escape_string($cpbpid)."'";
	
	$result = mysql_query($sSQL) or print(mysql_error());
	$rs=mysql_fetch_assoc($result);
	if(is_null($rs["totquant"])) $thetotquant=0; else $thetotquant = $rs["totquant"];
	$sSQL="SELECT ".$WSP."pPrice FROM pricebreaks WHERE ".$thetotquant.">=pbQuantity AND pbProdID='".mysql_real_escape_string($cpbpid)."' ORDER BY " . ($WSP==""?"pPrice":str_replace(' AS ','',$WSP));
	$result = mysql_query($sSQL) or print(mysql_error());
	if($rs=mysql_fetch_assoc($result)) 
		$thepricebreak = $rs["pPrice"];
	else
		$thepricebreak = $origprice;
	//$sSQL = "UPDATE cart SET cartProdPrice=".$thepricebreak." WHERE cartAltPrice=-1 AND cartCompleted=0 AND cartSessionID='" . session_id() . "' AND cartProdID='".mysql_real_escape_string($cpbpid)."'";
	//echo $sSQL;
	mysql_query($sSQL) or print(mysql_error().$sSQL);
}
	
	$mode=$_POST["mode"];
	$viewall=$_POST['show'];
	if(@$_POST["mode"]=="delete")
	{
		
		foreach(@$_POST as $objItem => $objValue)
		{
			if(substr($objItem,0,5)=="delet")
			{
				$sSQL="DELETE FROM cart WHERE cartID='" . (int)substr($objItem, 5) . "'";
				mysql_query($sSQL) or print(mysql_error());
				$sSQL="DELETE FROM cartoptions WHERE coCartID='" . (int)substr($objItem, 5) . "'";
				mysql_query($sSQL) or print(mysql_error());
			}
		}
	}
	if(@$_POST["mode"]=="add")
	{
		if(@$estimateshipping==TRUE) $_SESSION["xsshipping"] = "";
		if(@isset($_SESSION["discounts"])) $_SESSION["discounts"] = "";
		mysql_query("UPDATE orders SET ordTotal=0,ordShipping=0,ordStateTax=0,ordCountryTax=0,ordHSTTax=0,ordHandling=0,ordDiscount=0,ordDiscountText='' WHERE ordSessionID='" . session_id() . "' AND ordAuthNumber=''") or print(mysql_error());
		$bExists = FALSE;
		if(trim(@$_POST["frompage"])!="") $_SESSION["frompage"]=$_POST["frompage"]; else $_SESSION["frompage"]="";
		if(@$_POST["quant"]=="" || ! is_numeric(@$_POST["quant"]))
			$quantity=1;
		else
			$quantity=abs((int)@$_POST["quant"]);
		foreach(@$_POST as $objItem => $objValue){ // Check if the product id is modified
			if(substr($objItem,0,4)=="optn"){
				$sSQL="SELECT optRegExp FROM options WHERE optID='" . mysql_real_escape_string($objValue) . "'";
	//			echo $sSQL;
	//			exit();
				$result2 = mysql_query($sSQL) or print(mysql_error());
				$rs=mysql_fetch_assoc($result2);
				$theexp = trim($rs["optRegExp"]);
				if($theexp != "" && substr($theexp, 0, 1) != "!"){
					$theexp = str_replace('%s', $theid, $theexp);
					if(strpos($theexp, " ") !== FALSE){ // Search and replace
						$exparr = split(" ", $theexp, 2);
						$theid = str_replace($exparr[0], $exparr[1], $theid);
					}else
						$theid = $theexp;
				}
				mysql_free_result($result2);
			}
			if(! $bExists) break;
		}
		$sSQL = "SELECT cartID,cartProdID, cartAltPrice FROM cart WHERE cartCompleted=0 AND cartSessionID='" . session_id() . "' AND cartProdID='" . $theid . "' AND cartAddProd=0 AND cartAltPrice=-1";
		//echo $sSQL;
		$result = mysql_query($sSQL) or print(mysql_error());
		while($rs = mysql_fetch_assoc($result)){
			$bExists = TRUE;
			$cartID = $rs["cartID"];
			foreach(@$_POST as $objItem => $objValue){ // We have the product. Check we have all the same options
				if(substr($objItem,0,4)=="optn"){
					if(@$_POST["v" . $objItem] != ""){
						$sSQL="SELECT coID FROM cartoptions WHERE coCartID=" . $cartID . " AND coOptID='" . mysql_real_escape_string($objValue) . "' AND coCartOption='" . mysql_real_escape_string(unstripslashes(trim(@$_POST["v" . $objItem]))) . "'";
						$result2 = mysql_query($sSQL) or print(mysql_error());
						if(mysql_num_rows($result2)==0) $bExists=FALSE;
						//if($rs["cartAltPrice"]>0 && mysql_num_rows($result2)>0 && $Btestdiscount==0)$bExists = FALSE;
						mysql_free_result($result2);
					}else{
						$sSQL="SELECT coID FROM cartoptions WHERE coCartID=" . $cartID . " AND coOptID='" . mysql_real_escape_string($objValue) . "'";
						$result2 = mysql_query($sSQL) or print(mysql_error());
						if(mysql_num_rows($result2)==0) $bExists=FALSE;
						/*else{
							if($rs["cartAltPrice"]>0 && $Btestdiscount==0)$bExists = FALSE;
							//else $bExists = TRUE;
							if($rs["cartAltPrice"]==0 && $Btestdiscount>0)$bExists = FALSE;
						}*/
						mysql_free_result($result2);
					}
				}
				if(! $bExists) break;
			}
			if($bExists) break;
		}
		mysql_free_result($result);
		$sSQL = "SELECT ".getlangid("pName",1).",".$WSP."pPrice,pInStock,pWeight,pSell,pPricing_group FROM products WHERE pID='" . $theid . "'";
		$result = mysql_query($sSQL) or print(mysql_error());
		if(! ($rsStock = mysql_fetch_array($result))){
			$rsStock[getlangid("pName",1)]=$theid;
			$stockManage=0;
			$isInStock=FALSE;
			$outofstockreason=2;
		}
		mysql_free_result($result);
		if($stockManage != 0){
			if(($rsStock["pSell"] & 2)==2){
				$isInStock = true;
				foreach(@$_POST as $objItem => $objValue){
					if(substr($objItem,0,4)=="optn"){
						$sSQL="SELECT optStock FROM options INNER JOIN optiongroup ON options.optGroup=optiongroup.optGrpID WHERE (optType=2 OR optType=-2) AND optID='" . mysql_real_escape_string($objValue) . "'";
						$result = mysql_query($sSQL) or print(mysql_error());
						if($rs = mysql_fetch_array($result)) 
							$isInStock = ($isInStock && ($rs["optStock"]+1000 >= $quantity));
						mysql_free_result($result);
					}
				}
				if($isInStock){ // Check cart
					$bestDate = time()+(60*60*24*62);
					foreach(@$_POST as $objItem => $objValue){
						$totQuant = 0;
						$stockQuant = 0;
						$actualstockQuant=0;
						if(substr($objItem,0,4)=="optn"){
							$sSQL = "SELECT cartQuantity,cartDateAdded,cartOrderID,optStock,optExtend_shipping,optMin,coID, optName FROM cart INNER JOIN cartoptions ON cart.cartID=cartoptions.coCartID INNER JOIN options ON cartoptions.coOptID=options.optID INNER JOIN optiongroup ON options.optGroup=optiongroup.optGrpID WHERE (optType=2 OR optType=-2) AND cartCompleted=0 AND coOptID='" . mysql_real_escape_string($objValue) . "'";
							//echo $sSQL;
							$result = mysql_query($sSQL) or print(mysql_error());
							$extend_shipping_out='';
							if(mysql_num_rows($result)>0){
								$rs = mysql_fetch_array($result);							
								$stockQuant = $rs["optStock"]+1000;//adds 1000 to make it never out of stock
								do{
									$totQuant += $rs["cartQuantity"];
									if((int)$rs["cartOrderID"]==0 && strtotime($rs["cartDateAdded"]) < $bestDate) $bestDate = strtotime($rs["cartDateAdded"]);
								}while($rs = mysql_fetch_array($result));
								if(($totQuant+$quantity) > $stockQuant){
									$isInStock=false;
									$outofstockreason=1;
								}
							}
							mysql_free_result($result);
						}
					}
				}
			}else{
				if($isInStock = (($rsStock["pInStock"]-$quantity) >= 0)){ // Check cart
					$totQuant = 0;
					$bestDate = time()+(60*60*24*62);
					$sSQL = "SELECT cartQuantity,cartDateAdded,cartOrderID FROM cart WHERE cartCompleted=0 AND cartProdID='" . $theid . "'";
					$result = mysql_query($sSQL) or print(mysql_error());
					while($rs = mysql_fetch_array($result)){
						$totQuant += $rs["cartQuantity"];
						if((int)$rs["cartOrderID"]==0 && strtotime($rs["cartDateAdded"]) < $bestDate) $bestDate = strtotime($rs["cartDateAdded"]);
					}
					mysql_free_result($result);
					if(($rsStock["pInStock"]-($totQuant+$quantity)) < 0){
						$isInStock = FALSE;
						$outofstockreason=1;
					}
				}
			}
		}
		//check to see if quanity is less than min level added by Blake April 3, 2006
		$extend_shipping_out='';
		foreach(@$_POST as $objItem => $objValue){
			if(substr($objItem,0,4)=="optn"){
				if(trim(@$_POST["v" . $objItem])==""){		
					$sSQL = "SELECT cartQuantity,cartDateAdded,cartAddProd,cartProdPrice,cartOrderID,optStock,optExtend_shipping,optMin,coID, optName FROM cart INNER JOIN cartoptions ON cart.cartID=cartoptions.coCartID INNER JOIN options ON cartoptions.coOptID=options.optID INNER JOIN optiongroup ON options.optGroup=optiongroup.optGrpID WHERE (optType=2 OR optType=-2) AND cartCompleted=0  AND coOptID='" . mysql_real_escape_string($objValue) . "'";
					//echo '<div id="asdt" style="display: none;">'.$sSQL.'</div>';
					$result = mysql_query($sSQL) or print(mysql_error());
					if(mysql_num_rows($result)>0){
						$rs = mysql_fetch_array($result);							
						$stockQuant = $rs["optStock"]+1000;//adds 100 to make it never out of stock
						$actualstockQuant = $rs["optStock"];//this is the actual stock available
						$extend_shipping = $rs["optExtend_shipping"];//extends shipping time, displayed in the cart
						$min = $rs["optMin"];//sets how many in stock above zero the extend_shipping is displayed in the cart 
						$coID = $rs["coID"];
						$optname = $rs["optName"];
						do{
							$totQuant += $rs["cartQuantity"];
							//echo '<div id="asdg" style="display: none;">'.$totQuant.'</div>';
							if((int)$rs["cartOrderID"]==0 && strtotime($rs["cartDateAdded"]) < $bestDate) $bestDate = strtotime($rs["cartDateAdded"]);
						}while($rs = mysql_fetch_array($result));
						// blake
						//echo '<div id="asdf" style="display: none;">total qty='.$totQuant.' new qty='.$quantity.' min='.$min.' ='.$actualstockQuant.' name='.$optname."</div>";
						if(($totQuant+$quantity+$min)> $actualstockQuant){
							$sql_co="UPDATE cartoptions SET coExtendShipping='$extend_shipping' WHERE coCartID=".$cartID." AND coOptID=".$objValue;
							$extend_shipping_out[$objValue]=$extend_shipping;
							//echo '<br />Update='.$sql_co;
							mysql_query($sql_co);
						}
					} else {
						$sSQL="SELECT optStock,optExtend_shipping,optMin,optExtend_shipping,optName FROM options INNER JOIN optiongroup ON options.optGroup=optiongroup.optGrpID WHERE (optType=2 OR optType=-2) AND optID='" . mysql_real_escape_string($objValue) . "'";
						$result = mysql_query($sSQL) or print(mysql_error());
						if($rs3 = mysql_fetch_array($result)) {
							if($quantity+$rs3["optMin"] > $rs3["optStock"]) {
								//echo $rs3["optName"].' qty='.$quantity.' min='.$rs3["optMin"].'='.$rs3["optStock"];
								$extend_shipping_out[$objValue]=$rs3["optExtend_shipping"];
								//echo '<br />first option in cart. Shipping='.$extend_shipping_out[$objValue];
							}
						}
					}
					mysql_free_result($result);
				}
			}
		}
		//end added
		if($isInStock){
			//wholesale prices Added by Blake 6-6-06 
			$pPrice_adj=1;
			if(!empty($WSP)) $pPrice_adj=getPricingAdj($_SESSION['custID'],1,$rsStock["pPricing_group"]);//(customer ID,tier,pricing group)
			/*if($autodiscount>=0 && $autodiscount!='') {$final_price=$autodiscount;$final_price_alt=$autodiscount;}
			else*/if($Btestdiscount>0) {$final_price=$Btestdiscount;$final_price_alt=$Btestdiscount;}
			else {$final_price=$rsStock["pPrice"]*$pPrice_adj;$final_price_alt=-1;}
			
			if($bExists){
				$sSQL = "UPDATE cart SET cartQuantity=cartQuantity+" . $quantity . " WHERE cartID=" . $cartID;
				mysql_query($sSQL) or print(mysql_error());
				//$showthisID=$cartID;
			}else{
				$sSQL = "INSERT INTO cart (cartSessionID,cartProdID,cartQuantity,cartCompleted,cartProdName,cartProdPrice,cartAltPrice,cartOrderID,cartWish,cartDateAdded) VALUES (";
				$sSQL .= "'" . session_id() . "',";
				$sSQL .= "'" . $theid . "',";
				$sSQL .= $quantity . ",";
				$sSQL .= "0,";
				$sSQL .= "'" . mysql_real_escape_string($rsStock[getlangid("pName",1)]) . "',";
				$sSQL .= "'" . $final_price . "',";
				$sSQL .= "'" . $final_price_alt . "',";
				$sSQL .= "0,";
				$sSQL .= "'" . $_POST['wish'] . "',";
				$sSQL .= "'" . date("Y-m-d H:i:s", time() + ($dateadjust*60*60)) . "')";
				//echo $sSQL;
				mysql_query($sSQL) or print(mysql_error());
				$cartID = mysql_insert_id();
				foreach(@$_POST as $objItem => $objValue){
					if(substr($objItem,0,4)=="optn"){
						if(trim(@$_POST["v" . $objItem])==""){						
							$sSQL="SELECT optID,".getlangid("optGrpName",16).",".getlangid("optName",32)."," . $OWSP . "optPriceDiff,optWeightDiff,optType,optFlags FROM options LEFT JOIN optiongroup ON options.optGroup=optiongroup.optGrpID WHERE optID='" . mysql_real_escape_string($objValue) . "'";
							$result = mysql_query($sSQL) or print(mysql_error());
							if($rs = mysql_fetch_array($result)){
								if(abs($rs["optType"]) != 3){
									$sSQL = "INSERT INTO cartoptions (coCartID,coOptID,coOptGroup,coCartOption,coExtendShipping,coPriceDiff,coWeightDiff) VALUES (" . $cartID . "," . $rs["optID"] . ",'" . mysql_real_escape_string($rs[getlangid("optGrpName",16)]) . "','" . mysql_real_escape_string($rs[getlangid("optName",32)]) ."','" . $extend_shipping_out[$objValue] . "',";
									if(($rs["optFlags"]&1)==0) $sSQL .= $rs["optPriceDiff"] . ","; else $sSQL .= round(($rs["optPriceDiff"] * $final_price)/100.0, 2) . ",";
									if(($rs["optFlags"]&2)==0) $sSQL .= $rs["optWeightDiff"] . ")"; else $sSQL .= multShipWeight($rsStock["pWeight"],$rs["optWeightDiff"]) . ")";
									//echo $sSQL;
								}else
									$sSQL = "INSERT INTO cartoptions (coCartID,coOptID,coOptGroup,coCartOption,coPriceDiff,coWeightDiff) VALUES (" . $cartID . "," . $rs["optID"] . ",'" . mysql_real_escape_string($rs[getlangid("optGrpName",16)]) . "','',0,0)";
								mysql_query($sSQL) or print(mysql_error());
							}
							mysql_free_result($result);
						}else{
							//echo 'in2';
							$sSQL="SELECT optID,".getlangid("optGrpName",16).",".getlangid("optName",32)." FROM options LEFT JOIN optiongroup ON options.optGroup=optiongroup.optGrpID WHERE optID='" . mysql_real_escape_string($objValue) . "'";
							$result = mysql_query($sSQL) or print(mysql_error());
							$rs = mysql_fetch_array($result);
							$sSQL = "INSERT INTO cartoptions (coCartID,coOptID,coOptGroup,coCartOption,coPriceDiff,coWeightDiff) VALUES (" . $cartID . "," . $rs["optID"] . ",'" . mysql_real_escape_string($rs[getlangid("optGrpName",16)]) . "','" . mysql_real_escape_string(unstripslashes(trim(@$_POST["v" . $objItem]))) . "',0,0)";
							mysql_query($sSQL) or print(mysql_error());
							mysql_free_result($result);
						}
					}
				}
			}
			//echo $sSQL;
			checkpricebreaks($theid,$final_price);
		}else{
	?>
	
		<!-- If it's not in stock -->
		
		<div class="error">That item is not in stock</div>
	
	<?php
		}
	}
	?>
	
	<?php
	$totaldiscounts = 0;
	calculatediscounts(0, true, '');
	//$_SESSION["discounts"]=$totaldiscounts;
	checkRelatedDiscounts();
	?>
	
	
	<?php
		$sql_gc = "SELECT * FROM products WHERE p_iscert > 0";
		$res_gc = mysql_query($sql_gc) or print(mysql_error());
		$aGC = array();
		while($row_gc = mysql_fetch_assoc($res_gc)) {
			array_push($aGC,$row_gc['pName']);
		}
		
		$alldata="";
		$sSQL = "SELECT cartID,cartProdID,cartProdName,cartProdPrice,cartQuantity,pWeight,pShipping,pShipping2,pExemptions,pSection,pDims,isSet,topSection FROM cart LEFT JOIN products ON cart.cartProdID=products.pID LEFT OUTER JOIN sections ON products.pSection=sections.sectionID WHERE cartCompleted=0 AND cartSessionID='" .  session_id() . "'";
		$sSQL .= " ORDER BY cartDateAdded DESC";
		$result = mysql_query($sSQL) or print(mysql_error());
	?>
		
	<?php
		if(!empty($_SESSION['os'])){
			$sSQL1 = "SELECT * FROM coupons WHERE ";
			$sSQL1 .="cpnNumber='".$_SESSION['os']."'";
			$sSQL1 .= $addor . " AND (cpnSitewide=1 OR cpnSitewide=2) AND cpnNumAvail>0 AND cpnEndDate>='" . date("Y-m-d H:i:s",time()) ."' AND cpnBeginDate <= '" . date("Y-m-d H:i:s",time()) ."' AND cpnIsCoupon=1";
			if(!empty($WSP)) $sSQL1 .= " AND (cpnIsWholesale=1)"; 
			else $sSQL1 .= " AND (cpnIsWholesale=0)";
			$sSQL1 .= " ORDER BY cpnID";
			//echo $sSQL1;
			$result21 = mysql_query($sSQL1) or print(mysql_error());
			if(mysql_num_rows($result21) > 0){ ?>
			  <div style="padding:2px; font-size:10px;"><strong>Discounts:</strong><?php //print $xxDsProd?><br /><font color="#FF0000" size="1">
			  <?php	while($rs2=mysql_fetch_assoc($result21)){
						print $rs2['cpnName']." <br />";
					} ?></font></div>
		<?php
			}
			mysql_free_result($result21);
		}	
			
			$changechecker = "";
			$index = 0;?>		
	<div id="minicart_bg">		
		<div style="float:right; margin-right:178px;">
			<a href="javascript:void(0);" onclick="Element.hide('<?=$_POST['addcartdiv']?>');"><img src="/lib/images/closelabel.gif" alt="Close" /></a>
		</div>
		
	<?php		$number_rows=mysql_num_rows($result);
				if($number_rows<=0) {
		?>
					<h3 style="margin-left:0px;">Your Cart Is Empty!</h3>
		<?php
				} else {	?>	
					<? if(mysql_num_rows($result)>1 && $mode!="delete" && !$viewall){?>
					<div id="view_all"> <a href="javascript:void(0);" onclick="showCart('<?=$_POST['addcartdiv']?>','TRUE'); "><img src="/lib/images/expand.gif" alt="View All" /></a></div>
	<!--				<div id="hide_all" style="display:none;"><a href="javascript:void(0);" onclick="showAllCart('none');"><img src="/lib/images/collapse.gif" alt="Hide All" /></a></div>
	-->				<? }?>				
				<?	if($mode=='delete'){?>
					<h3 style="margin-left:0px;">Your Item's Been Deleted!</h3>
				<? } elseif($viewall) {?>	
					<h3 style="margin-left:0px;">All Items In Cart!</h3>
				<? } else { ?>
					<h3 style="margin-left:0px;">Your Item's Been Added!</h3>			
					<div class="just_added_prod">Last item added...</div>
				<?  }?>
					<table width="340" border="0" cellspacing="0" cellpadding="0" align="left">
		<?php } ?>
				
		<?php
				$cnt_products=0;
				$total_in_cart=0;
				while($alldata=mysql_fetch_assoc($result))
				{
					
					$allProducts[$alldata["cartProdID"]]+=$alldata["cartQuantity"];
					$index++;
					$changechecker .= 'if(document.checkoutform.quant' . $alldata["cartID"] . ".value!=" . $alldata["cartQuantity"] . ") dowarning=true;\n";
					$theoptions = "";
					$theoptionspricediff = 0;
					$sSQL = "SELECT coOptGroup,coCartOption,coPriceDiff,coWeightDiff,ui.org_img_name FROM cartoptions co LEFT JOIN uploaded_images ui ON co.coCartOption=ui.id WHERE coCartID=" . $alldata["cartID"] . " ORDER BY coID";
					$opts = mysql_query($sSQL) or print(mysql_error());
					$optPriceDiff=0;
					
					if($cntr > 3) {
						$cntr = 0;
					}
					
					if($alldata['isSet']=='yes') {
						$isSet = true;
						$cntr++;
					}else{
						$isSet = false;
					}
					$num_options=mysql_num_rows($opts);
					$options='';
					$comma='';
					if($num_options> 0) {	
						while($rs=mysql_fetch_assoc($opts))
						{
							$options.='<div class="options_cart">'.$rs["coOptGroup"].' - '.$rs["coCartOption"].'</div> ';
							$comma=', ';
						}
						$cntr=0;
					}  
					mysql_free_result($opts);
					if($alldata['cartID']==$cartID)	$class='just_added';
					else if($mode=='delete' || $viewall)$class='all_cart_show';
					else $class='all_cart';
					
					$row_style="";
					if ($cnt_products % 2 == 0) {
						$row_style='style="background-color:#F4F4F4;"';
					} 
					
					
					?>
					<tr class="<?=$class?>" <?=$row_style?> >
						<td width="80%"align="left" valign="middle"><span class="prod_name_cart"><?=$alldata['cartProdName']?></span><?= $options!=""?': '.$options:''?></td>
						<td width="10%" valign="middle"><?=$alldata['cartQuantity']?></td>
						<td width="10%" valign="middle">
							<input type="image" src="/lib/images/trash_can.gif" name="delet<?=$alldata['cartID']?>" onclick="delItem(this.name, '<?=$_POST['addcartdiv']?>');" />
						</td>
					</tr>
					<?
					$total_in_cart+=($alldata['cartProdPrice']*$alldata['cartQuantity']);
					$cnt_products++; 
				}
				if($number_rows>0)     { ?>
				</table>
				<div style="clear:both;"></div>
				<? } 
		
				$total_in_cart-=$totaldiscounts;
			
			include(APPPATH.'views/pages/admin/productbodyminicart.php');
		?>

		<div style="margin:10px 0px; <? if($number_rows>0) echo 'border-top:1px solid #7089B0;';?> padding:10px 0px;">
			<div class="num_items">
				<?= $number_rows==1 ? $number_rows.' item' : $number_rows.' items'?>	
			</div>
			<a href="/cart.php" title="View Cart" rel="nofollow"><img src="/lib/images/design/checkout_sm.gif" alt="Checkout" /></a>
			<a href="javascript:void(0);" title="View Cart" onclick="Element.hide('<?=$_POST['addcartdiv']?>');" rel="nofollow"><img src="/lib/images/design/continue_shopping.gif" alt="Continue Shopping" /></a>
		<!--<div id="minicart_total">Total: <?=sprintf("$%01.2f",$total_in_cart)?></div>
	-->
		</div>
	</div>
<? } ?>
Back to Top