PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/minicart2.php

https://bitbucket.org/chadsaun/ifrogz-panel
PHP | 688 lines | 604 code | 18 blank | 66 comment | 148 complexity | 081ef788fefc91f38624088230fece11 MD5 | raw file
  1. <?php
  2. session_start();
  3. include('init.php');
  4. include(APPPATH.'views/partials/admin/dbconnection.php');
  5. include(APPPATH.'views/partials/admin/includes.php');
  6. include(APPPATH.'views/partials/admin/languagefile.php');
  7. include(APPPATH.'views/partials/admin/functions.php');
  8. $Btestdiscount=$_REQUEST['Btestdiscount'];
  9. $autodiscount=$_REQUEST['autodiscount'];
  10. //echo '='.$Btestdiscount;
  11. if($_POST['show']=="FALSE"){
  12. $sSQL = "SELECT * FROM cart WHERE cartCompleted=0 AND cartSessionID='" . session_id() . "'";
  13. $result = mysql_query($sSQL) or print(mysql_error());
  14. $num_rows=mysql_num_rows($result);
  15. if($num_rows==1) $item="item";
  16. else $item="items";
  17. echo $num_rows." ".$item;
  18. } else { ?>
  19. <?php
  20. //echo 'id= '.$_POST["id"];
  21. //exit();
  22. $isInStock=TRUE;
  23. $WSP = "";
  24. $OWSP = "";
  25. $theid = mysql_real_escape_string(trim(@$_POST["id"]));
  26. if(@$dateadjust=="") $dateadjust=0;
  27. $alreadygotadmin = getadminsettings();
  28. if(@$_SESSION["clientUser"] != ""){
  29. if(($_SESSION["clientActions"] & 8) == 8){
  30. $WSP = "pWholesalePrice AS ";
  31. if(@$wholesaleoptionpricediff==TRUE) $OWSP = 'optWholesalePriceDiff AS ';
  32. if(@$nowholesalediscounts==TRUE) $nodiscounts=TRUE;
  33. }
  34. if(($_SESSION["clientActions"] & 16) == 16){
  35. $WSP = $_SESSION["clientPercentDiscount"] . "*pPrice AS ";
  36. if(@$wholesaleoptionpricediff==TRUE) $OWSP = $_SESSION["clientPercentDiscount"] . '*optPriceDiff AS ';
  37. if(@$nowholesalediscounts==TRUE) $nodiscounts=TRUE;
  38. }
  39. }
  40. if(@$_SESSION["clientLoginLevel"] != "") $minloglevel=$_SESSION["clientLoginLevel"]; else $minloglevel=0;
  41. /*if($stockManage != 0){
  42. $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)) . "')";
  43. if($delAfter != 0)
  44. $sSQL .= " OR (cartCompleted=0 AND cartDateAdded<'" . date("Y-m-d H:i:s", time()-($delAfter*60*60*24)) . "')";
  45. $result = mysql_query($sSQL) or print(mysql_error());
  46. if(mysql_num_rows($result)>0){
  47. $addcomma = "";
  48. $delstr="";
  49. $delcart="";
  50. while($rs = mysql_fetch_assoc($result)){
  51. $delcart .= $addcomma . $rs["cartOrderID"];
  52. $delstr .= $addcomma . $rs["cartID"];
  53. $addcomma = ",";
  54. }
  55. if($delAfter != 0) mysql_query("DELETE FROM orders WHERE ordID IN (" . $delcart . ")") or print(mysql_error());
  56. mysql_query("DELETE FROM cart WHERE cartID IN (" . $delstr . ")") or print(mysql_error());
  57. mysql_query("DELETE FROM cartoptions WHERE coCartID IN (" . $delstr . ")") or print(mysql_error());
  58. }
  59. mysql_free_result($result);
  60. }*/
  61. $thesessionid=session_id();
  62. function addadiscount($resset, $groupdiscount, $dscamount, $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, $theexemptions, $thetax){
  63. global $totaldiscounts, $cpnmessage, $statetaxfree, $countrytaxfree, $gotcpncode, $perproducttaxrate, $countryTax , $cpnIDs , $appliedcouponname , $appliedcouponamount,$thesessionid;
  64. $totaldiscounts += $dscamount;
  65. if($groupdiscount){
  66. $statetaxfree -= ($dscamount * $statetaxhandback);
  67. $countrytaxfree -= ($dscamount * $countrytaxhandback);
  68. }else{
  69. subtaxesfordiscounts($theexemptions, $dscamount);
  70. if(@$perproducttaxrate) $countryTax -= (($dscamount * $thetax) / 100.0);
  71. }
  72. $usecoupon=TRUE;
  73. //echo '$cdcpncode='.$cdcpncode;
  74. $cpnDiscount=substr($resset['cpnWorkingName'],0,10);
  75. //echo '$cpnDiscount='.$cpnDiscount;
  76. $sql_dd="SELECT * FROM daily_discounts WHERE starttime LIKE '".$cpnDiscount."%'";
  77. //echo $sql_dd;
  78. $result_dd=mysql_query($sql_dd);
  79. if(mysql_num_rows($result_dd)>0){
  80. $row_dd=mysql_fetch_assoc($result_dd);
  81. //echo 'starttime='.$row_dd['starttime'];
  82. $sql_d="SELECT cartDateAdded FROM cart WHERE cartSessionID='".$thesessionid."' AND cartDateAdded<'".$row_dd['starttime']."'";
  83. //echo $sql_d;
  84. $result_d=mysql_query($sql_d);
  85. if(mysql_num_rows($result_d)>0){
  86. 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>';
  87. $dscamount=0;
  88. $totaldiscounts=0;
  89. $usecoupon=FALSE;
  90. }
  91. }
  92. //echo '$dscamount='.$dscamount;
  93. //echo '$totaldiscounts='.$totaldiscounts;
  94. //$dscamount=0;
  95. //$totaldiscounts=0;
  96. if($usecoupon){
  97. //echo 'used';
  98. if(stristr($cpnmessage,"<br />" . $resset[getlangid("cpnName",1024)] . "<br />") == FALSE) $cpnmessage .= $resset[getlangid("cpnName",1024)] . "<br />";
  99. // added Blake 1/2/07
  100. // add discount ids to order
  101. $comma=",";
  102. if(empty($cpnIDs)) $comma=" ";
  103. $cpnIDs .= $comma.$resset["cpnID"];
  104. // end
  105. if($subcpns){
  106. $theres = mysql_query("SELECT cpnID FROM coupons WHERE cpnNumAvail>0 AND cpnNumAvail<30000000 AND cpnID=" . $resset["cpnID"]) or print(mysql_error());
  107. if($theresset = mysql_fetch_assoc($theres)) @$_SESSION["couponapply"] .= "," . $resset["cpnID"];
  108. mysql_query("UPDATE coupons SET cpnNumAvail=cpnNumAvail-1 WHERE cpnNumAvail>0 AND cpnNumAvail<30000000 AND cpnID=" . $resset["cpnID"]) or print(mysql_error());
  109. }
  110. if($cdcpncode!="" && strtolower(trim($resset["cpnNumber"]))==strtolower($cdcpncode)) {$gotcpncode=TRUE;$appliedcouponname = $resset['cpnName']; $appliedcouponamount = $dscamount; }
  111. }
  112. }
  113. function subtaxesfordiscounts($theExemptions, $discAmount){
  114. global $statetaxfree,$countrytaxfree,$shipfreegoods;
  115. if(($theExemptions & 1)==1) $statetaxfree -= $discAmount;
  116. if(($theExemptions & 2)==2) $countrytaxfree -= $discAmount;
  117. if(($theExemptions & 4)==4) $shipfreegoods -= $discAmount;
  118. }
  119. function timesapply($taquant,$tathresh,$tamaxquant,$tamaxthresh,$taquantrepeat,$tathreshrepeat){
  120. if($taquantrepeat==0 && $tathreshrepeat==0)
  121. $tatimesapply = 1.0;
  122. elseif($tamaxquant==0)
  123. $tatimesapply = (int)(($tathresh - $tamaxthresh) / $tathreshrepeat)+1;
  124. elseif($tamaxthresh==0)
  125. $tatimesapply = (int)(($taquant - $tamaxquant) / $taquantrepeat)+1;
  126. else{
  127. $ta1 = (int)(($taquant - $tamaxquant) / $taquantrepeat)+1;
  128. $ta2 = (int)(($tathresh - $tamaxthresh) / $tathreshrepeat)+1;
  129. if($ta2 < $ta1) $tatimesapply = $ta2; else $tatimesapply = $ta1;
  130. }
  131. return($tatimesapply);
  132. }
  133. function calculatediscounts($cdgndtot, $subcpns, $cdcpncode){
  134. global $totaldiscounts, $cpnmessage, $statetaxfree, $countrytaxfree, $nodiscounts, $WSP, $cpncode, $gotcpncode, $thesessionid, $countryTaxRate, $countryTax;
  135. $totaldiscounts = 0;
  136. $cpnmessage = "<br />";
  137. $cdtotquant=0;
  138. if($cdgndtot==0){
  139. $statetaxhandback = 0.0;
  140. $countrytaxhandback = 0.0;
  141. }else{
  142. $statetaxhandback = 1.0 - (($cdgndtot - $statetaxfree) / $cdgndtot);
  143. $countrytaxhandback = 1.0 - (($cdgndtot - $countrytaxfree) / $cdgndtot);
  144. }
  145. if(! $nodiscounts){
  146. $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";
  147. $cdresult = mysql_query($sSQL) or print(mysql_error());
  148. $cdadindex=0;
  149. while($cdrs = mysql_fetch_assoc($cdresult)){
  150. $cdalldata[$cdadindex++]=$cdrs;
  151. }
  152. for($index=0; $index<$cdadindex; $index++){
  153. $cdrs = $cdalldata[$index];
  154. $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"] . "'";
  155. $cdresult2 = mysql_query($sSQL) or print(mysql_error());
  156. $cdrs2 = mysql_fetch_assoc($cdresult2);
  157. if(! is_null($cdrs2["totOpts"])) $cdrs["thePrice"] += $cdrs2["totOpts"];
  158. $cdtotquant += $cdrs["sumQuant"];
  159. $topcpnids = $cdrs["pSection"];
  160. $thetopts = $cdrs["pSection"];
  161. if(is_null($cdrs["pTax"])) $cdrs["pTax"] = $countryTaxRate;
  162. for($cpnindex=0; $cpnindex<= 10; $cpnindex++){
  163. if($thetopts==0)
  164. break;
  165. else{
  166. $sSQL = "SELECT topSection FROM sections WHERE sectionID=" . $thetopts;
  167. $result2 = mysql_query($sSQL) or print(mysql_error());
  168. if($rs2 = mysql_fetch_assoc($result2)){
  169. $thetopts = $rs2["topSection"];
  170. $topcpnids .= "," . $thetopts;
  171. }else
  172. break;
  173. }
  174. }
  175. $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";
  176. if($cdcpncode != "") $sSQL .= " OR (cpnIsCoupon=1 AND cpnNumber='" . $cdcpncode . "')";
  177. $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 ";
  178. $sSQL .= "(cpnSitewide=2 OR (cpaType=2 AND cpaAssignment='" . $cdrs["cartProdID"] . "') ";
  179. $sSQL .= "OR (cpaType=1 AND cpaAssignment IN ('" . str_replace(",","','",$topcpnids) . "')))";
  180. if(!empty($WSP)) $sSQL .= " AND (cpnIsWholesale=1)";
  181. else $sSQL .= " AND (cpnIsWholesale=0)";
  182. $result2 = mysql_query($sSQL) or print(mysql_error());
  183. //echo $sSQL;
  184. while($rs2 = mysql_fetch_assoc($result2)){
  185. if($rs2["cpnType"]==1){ // Flat Rate Discount
  186. $thedisc = (double)$rs2["cpnDiscount"] * timesapply($cdrs["sumQuant"], $cdrs["thePrice"], $rs2["cpnQuantity"], $rs2["cpnThreshold"], $rs2["cpnQuantityRepeat"], $rs2["cpnThresholdRepeat"]);
  187. if($cdrs["thePrice"] < $thedisc) $thedisc = $cdrs["thePrice"];
  188. addadiscount($rs2, FALSE, $thedisc, $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, $cdrs["pExemptions"], $cdrs["pTax"]);
  189. }elseif($rs2["cpnType"]==2){ // Percentage Discount
  190. addadiscount($rs2, FALSE, (((double)$rs2["cpnDiscount"] * (double)$cdrs["thePrice"]) / 100.0), $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, $cdrs["pExemptions"], $cdrs["pTax"]);
  191. }
  192. }
  193. }
  194. $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";
  195. if($cdcpncode != "") $sSQL .= " OR (cpnIsCoupon=1 AND cpnNumber='" . $cdcpncode . "')";
  196. $sSQL .= ") AND cpnThreshold<=" . $cdgndtot . " AND cpnQuantity<=" . $cdtotquant . " AND (cpnSitewide=1 OR cpnSitewide=3) AND (cpnType=1 OR cpnType=2)";
  197. if(!empty($WSP)) $sSQL .= " AND (cpnIsWholesale=1)";
  198. else $sSQL .= " AND (cpnIsWholesale=0)";
  199. $result2 = mysql_query($sSQL) or print(mysql_error());
  200. while($rs2 = mysql_fetch_assoc($result2)){
  201. $totquant = 0;
  202. $totprice = 0;
  203. if($rs2["cpnSitewide"]==3){
  204. $sSQL = "SELECT cpaAssignment FROM cpnassign WHERE cpaType=1 AND cpacpnID=" . $rs2["cpnID"];
  205. $result3 = mysql_query($sSQL) or print(mysql_error());
  206. $secids = "";
  207. $addcomma = "";
  208. while($rs3 = mysql_fetch_assoc($result3)){
  209. $secids .= $addcomma . $rs3["cpaAssignment"];
  210. $addcomma = ",";
  211. }
  212. if($secids != ""){
  213. $secids = getsectionids($secids, FALSE);
  214. $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 . ")";
  215. $result3 = mysql_query($sSQL) or print(mysql_error());
  216. $rs3 = mysql_fetch_assoc($result3);
  217. if(is_null($rs3["totPrice"])) $totprice = 0; else $totprice = $rs3["totPrice"];
  218. if(is_null($rs3["totQuant"])) $totquant=0; else $totquant = $rs3["totQuant"];
  219. $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 . ")";
  220. $result3 = mysql_query($sSQL) or print(mysql_error());
  221. $rs3 = mysql_fetch_assoc($result3);
  222. if(! is_null($rs3["optPrDiff"])) $totprice = $totprice+$rs3["optPrDiff"];
  223. }
  224. }else{ // cpnSitewide==1
  225. $totquant = $cdtotquant;
  226. $totprice = $cdgndtot;
  227. }
  228. if($totquant > 0 && $rs2["cpnThreshold"] <= $totprice && ($rs2["cpnThresholdMax"] > $totprice || $rs2["cpnThresholdMax"]==0) && $rs2["cpnQuantity"] <= $totquant && ($rs2["cpnQuantityMax"] > $totquant || $rs2["cpnQuantityMax"]==0)){
  229. if($rs2["cpnType"]==1){ // Flat Rate Discount
  230. $thedisc = (double)$rs2["cpnDiscount"] * timesapply($totquant, $totprice, $rs2["cpnQuantity"], $rs2["cpnThreshold"], $rs2["cpnQuantityRepeat"], $rs2["cpnThresholdRepeat"]);
  231. if($totprice < $thedisc) $thedisc = $totprice;
  232. }elseif($rs2["cpnType"]==2){ // Percentage Discount
  233. $thedisc = ((double)$rs2["cpnDiscount"] * (double)$totprice) / 100.0;
  234. }
  235. addadiscount($rs2, TRUE, $thedisc, $subcpns, $cdcpncode, $statetaxhandback, $countrytaxhandback, 3, 0);
  236. if(@$perproducttaxrate && $cdgndtot > 0){
  237. for($index=0; $index<$cdadindex; $index++){
  238. $cdrs = $cdalldata[$index];
  239. if($rs2["cpnType"]==1) // Flat Rate Discount
  240. $applicdisc = $thedisc / ($cdtotquant / $cdrs["sumQuant"]);
  241. elseif($rs2["cpnType"]==2) // Percentage Discount
  242. $applicdisc = $thedisc / ($cdgndtot / $cdrs["thePrice"]);
  243. if(($cdrs["pExemptions"] & 2) != 2) $countryTax -= (($applicdisc * $cdrs["pTax"]) / 100.0);
  244. }
  245. }
  246. }
  247. }
  248. }
  249. if($statetaxfree < 0) $statetaxfree = 0;
  250. if($countrytaxfree < 0) $countrytaxfree = 0;
  251. $totaldiscounts = round($totaldiscounts, 2);
  252. //echo '$totaldiscounts='.$totaldiscounts;
  253. }
  254. function checkpricebreaks($cpbpid,$origprice){
  255. global $WSP;
  256. $newprice="";
  257. $sSQL = "SELECT SUM(cartQuantity) AS totquant FROM cart WHERE cartCompleted=0 AND cartSessionID='" . session_id() . "' AND cartProdID='".mysql_real_escape_string($cpbpid)."'";
  258. $result = mysql_query($sSQL) or print(mysql_error());
  259. $rs=mysql_fetch_assoc($result);
  260. if(is_null($rs["totquant"])) $thetotquant=0; else $thetotquant = $rs["totquant"];
  261. $sSQL="SELECT ".$WSP."pPrice FROM pricebreaks WHERE ".$thetotquant.">=pbQuantity AND pbProdID='".mysql_real_escape_string($cpbpid)."' ORDER BY " . ($WSP==""?"pPrice":str_replace(' AS ','',$WSP));
  262. $result = mysql_query($sSQL) or print(mysql_error());
  263. if($rs=mysql_fetch_assoc($result))
  264. $thepricebreak = $rs["pPrice"];
  265. else
  266. $thepricebreak = $origprice;
  267. //$sSQL = "UPDATE cart SET cartProdPrice=".$thepricebreak." WHERE cartAltPrice=-1 AND cartCompleted=0 AND cartSessionID='" . session_id() . "' AND cartProdID='".mysql_real_escape_string($cpbpid)."'";
  268. //echo $sSQL;
  269. mysql_query($sSQL) or print(mysql_error().$sSQL);
  270. }
  271. $mode=$_POST["mode"];
  272. $viewall=$_POST['show'];
  273. if(@$_POST["mode"]=="delete")
  274. {
  275. foreach(@$_POST as $objItem => $objValue)
  276. {
  277. if(substr($objItem,0,5)=="delet")
  278. {
  279. $sSQL="DELETE FROM cart WHERE cartID='" . (int)substr($objItem, 5) . "'";
  280. mysql_query($sSQL) or print(mysql_error());
  281. $sSQL="DELETE FROM cartoptions WHERE coCartID='" . (int)substr($objItem, 5) . "'";
  282. mysql_query($sSQL) or print(mysql_error());
  283. }
  284. }
  285. }
  286. if(@$_POST["mode"]=="add")
  287. {
  288. if(@$estimateshipping==TRUE) $_SESSION["xsshipping"] = "";
  289. if(@isset($_SESSION["discounts"])) $_SESSION["discounts"] = "";
  290. 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());
  291. $bExists = FALSE;
  292. if(trim(@$_POST["frompage"])!="") $_SESSION["frompage"]=$_POST["frompage"]; else $_SESSION["frompage"]="";
  293. if(@$_POST["quant"]=="" || ! is_numeric(@$_POST["quant"]))
  294. $quantity=1;
  295. else
  296. $quantity=abs((int)@$_POST["quant"]);
  297. foreach(@$_POST as $objItem => $objValue){ // Check if the product id is modified
  298. if(substr($objItem,0,4)=="optn"){
  299. $sSQL="SELECT optRegExp FROM options WHERE optID='" . mysql_real_escape_string($objValue) . "'";
  300. // echo $sSQL;
  301. // exit();
  302. $result2 = mysql_query($sSQL) or print(mysql_error());
  303. $rs=mysql_fetch_assoc($result2);
  304. $theexp = trim($rs["optRegExp"]);
  305. if($theexp != "" && substr($theexp, 0, 1) != "!"){
  306. $theexp = str_replace('%s', $theid, $theexp);
  307. if(strpos($theexp, " ") !== FALSE){ // Search and replace
  308. $exparr = split(" ", $theexp, 2);
  309. $theid = str_replace($exparr[0], $exparr[1], $theid);
  310. }else
  311. $theid = $theexp;
  312. }
  313. mysql_free_result($result2);
  314. }
  315. if(! $bExists) break;
  316. }
  317. $sSQL = "SELECT cartID,cartProdID, cartAltPrice FROM cart WHERE cartCompleted=0 AND cartSessionID='" . session_id() . "' AND cartProdID='" . $theid . "' AND cartAddProd=0 AND cartAltPrice=-1";
  318. //echo $sSQL;
  319. $result = mysql_query($sSQL) or print(mysql_error());
  320. while($rs = mysql_fetch_assoc($result)){
  321. $bExists = TRUE;
  322. $cartID = $rs["cartID"];
  323. foreach(@$_POST as $objItem => $objValue){ // We have the product. Check we have all the same options
  324. if(substr($objItem,0,4)=="optn"){
  325. if(@$_POST["v" . $objItem] != ""){
  326. $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]))) . "'";
  327. $result2 = mysql_query($sSQL) or print(mysql_error());
  328. if(mysql_num_rows($result2)==0) $bExists=FALSE;
  329. //if($rs["cartAltPrice"]>0 && mysql_num_rows($result2)>0 && $Btestdiscount==0)$bExists = FALSE;
  330. mysql_free_result($result2);
  331. }else{
  332. $sSQL="SELECT coID FROM cartoptions WHERE coCartID=" . $cartID . " AND coOptID='" . mysql_real_escape_string($objValue) . "'";
  333. $result2 = mysql_query($sSQL) or print(mysql_error());
  334. if(mysql_num_rows($result2)==0) $bExists=FALSE;
  335. /*else{
  336. if($rs["cartAltPrice"]>0 && $Btestdiscount==0)$bExists = FALSE;
  337. //else $bExists = TRUE;
  338. if($rs["cartAltPrice"]==0 && $Btestdiscount>0)$bExists = FALSE;
  339. }*/
  340. mysql_free_result($result2);
  341. }
  342. }
  343. if(! $bExists) break;
  344. }
  345. if($bExists) break;
  346. }
  347. mysql_free_result($result);
  348. $sSQL = "SELECT ".getlangid("pName",1).",".$WSP."pPrice,pInStock,pWeight,pSell,pPricing_group FROM products WHERE pID='" . $theid . "'";
  349. $result = mysql_query($sSQL) or print(mysql_error());
  350. if(! ($rsStock = mysql_fetch_array($result))){
  351. $rsStock[getlangid("pName",1)]=$theid;
  352. $stockManage=0;
  353. $isInStock=FALSE;
  354. $outofstockreason=2;
  355. }
  356. mysql_free_result($result);
  357. if($stockManage != 0){
  358. if(($rsStock["pSell"] & 2)==2){
  359. $isInStock = true;
  360. foreach(@$_POST as $objItem => $objValue){
  361. if(substr($objItem,0,4)=="optn"){
  362. $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) . "'";
  363. $result = mysql_query($sSQL) or print(mysql_error());
  364. if($rs = mysql_fetch_array($result))
  365. $isInStock = ($isInStock && ($rs["optStock"]+1000 >= $quantity));
  366. mysql_free_result($result);
  367. }
  368. }
  369. if($isInStock){ // Check cart
  370. $bestDate = time()+(60*60*24*62);
  371. foreach(@$_POST as $objItem => $objValue){
  372. $totQuant = 0;
  373. $stockQuant = 0;
  374. $actualstockQuant=0;
  375. if(substr($objItem,0,4)=="optn"){
  376. $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) . "'";
  377. //echo $sSQL;
  378. $result = mysql_query($sSQL) or print(mysql_error());
  379. $extend_shipping_out='';
  380. if(mysql_num_rows($result)>0){
  381. $rs = mysql_fetch_array($result);
  382. $stockQuant = $rs["optStock"]+1000;//adds 1000 to make it never out of stock
  383. do{
  384. $totQuant += $rs["cartQuantity"];
  385. if((int)$rs["cartOrderID"]==0 && strtotime($rs["cartDateAdded"]) < $bestDate) $bestDate = strtotime($rs["cartDateAdded"]);
  386. }while($rs = mysql_fetch_array($result));
  387. if(($totQuant+$quantity) > $stockQuant){
  388. $isInStock=false;
  389. $outofstockreason=1;
  390. }
  391. }
  392. mysql_free_result($result);
  393. }
  394. }
  395. }
  396. }else{
  397. if($isInStock = (($rsStock["pInStock"]-$quantity) >= 0)){ // Check cart
  398. $totQuant = 0;
  399. $bestDate = time()+(60*60*24*62);
  400. $sSQL = "SELECT cartQuantity,cartDateAdded,cartOrderID FROM cart WHERE cartCompleted=0 AND cartProdID='" . $theid . "'";
  401. $result = mysql_query($sSQL) or print(mysql_error());
  402. while($rs = mysql_fetch_array($result)){
  403. $totQuant += $rs["cartQuantity"];
  404. if((int)$rs["cartOrderID"]==0 && strtotime($rs["cartDateAdded"]) < $bestDate) $bestDate = strtotime($rs["cartDateAdded"]);
  405. }
  406. mysql_free_result($result);
  407. if(($rsStock["pInStock"]-($totQuant+$quantity)) < 0){
  408. $isInStock = FALSE;
  409. $outofstockreason=1;
  410. }
  411. }
  412. }
  413. }
  414. //check to see if quanity is less than min level added by Blake April 3, 2006
  415. $extend_shipping_out='';
  416. foreach(@$_POST as $objItem => $objValue){
  417. if(substr($objItem,0,4)=="optn"){
  418. if(trim(@$_POST["v" . $objItem])==""){
  419. $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) . "'";
  420. //echo '<div id="asdt" style="display: none;">'.$sSQL.'</div>';
  421. $result = mysql_query($sSQL) or print(mysql_error());
  422. if(mysql_num_rows($result)>0){
  423. $rs = mysql_fetch_array($result);
  424. $stockQuant = $rs["optStock"]+1000;//adds 100 to make it never out of stock
  425. $actualstockQuant = $rs["optStock"];//this is the actual stock available
  426. $extend_shipping = $rs["optExtend_shipping"];//extends shipping time, displayed in the cart
  427. $min = $rs["optMin"];//sets how many in stock above zero the extend_shipping is displayed in the cart
  428. $coID = $rs["coID"];
  429. $optname = $rs["optName"];
  430. do{
  431. $totQuant += $rs["cartQuantity"];
  432. //echo '<div id="asdg" style="display: none;">'.$totQuant.'</div>';
  433. if((int)$rs["cartOrderID"]==0 && strtotime($rs["cartDateAdded"]) < $bestDate) $bestDate = strtotime($rs["cartDateAdded"]);
  434. }while($rs = mysql_fetch_array($result));
  435. // blake
  436. //echo '<div id="asdf" style="display: none;">total qty='.$totQuant.' new qty='.$quantity.' min='.$min.' ='.$actualstockQuant.' name='.$optname."</div>";
  437. if(($totQuant+$quantity+$min)> $actualstockQuant){
  438. $sql_co="UPDATE cartoptions SET coExtendShipping='$extend_shipping' WHERE coCartID=".$cartID." AND coOptID=".$objValue;
  439. $extend_shipping_out[$objValue]=$extend_shipping;
  440. //echo '<br />Update='.$sql_co;
  441. mysql_query($sql_co);
  442. }
  443. } else {
  444. $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) . "'";
  445. $result = mysql_query($sSQL) or print(mysql_error());
  446. if($rs3 = mysql_fetch_array($result)) {
  447. if($quantity+$rs3["optMin"] > $rs3["optStock"]) {
  448. //echo $rs3["optName"].' qty='.$quantity.' min='.$rs3["optMin"].'='.$rs3["optStock"];
  449. $extend_shipping_out[$objValue]=$rs3["optExtend_shipping"];
  450. //echo '<br />first option in cart. Shipping='.$extend_shipping_out[$objValue];
  451. }
  452. }
  453. }
  454. mysql_free_result($result);
  455. }
  456. }
  457. }
  458. //end added
  459. if($isInStock){
  460. //wholesale prices Added by Blake 6-6-06
  461. $pPrice_adj=1;
  462. if(!empty($WSP)) $pPrice_adj=getPricingAdj($_SESSION['custID'],1,$rsStock["pPricing_group"]);//(customer ID,tier,pricing group)
  463. /*if($autodiscount>=0 && $autodiscount!='') {$final_price=$autodiscount;$final_price_alt=$autodiscount;}
  464. else*/if($Btestdiscount>0) {$final_price=$Btestdiscount;$final_price_alt=$Btestdiscount;}
  465. else {$final_price=$rsStock["pPrice"]*$pPrice_adj;$final_price_alt=-1;}
  466. if($bExists){
  467. $sSQL = "UPDATE cart SET cartQuantity=cartQuantity+" . $quantity . " WHERE cartID=" . $cartID;
  468. mysql_query($sSQL) or print(mysql_error());
  469. //$showthisID=$cartID;
  470. }else{
  471. $sSQL = "INSERT INTO cart (cartSessionID,cartProdID,cartQuantity,cartCompleted,cartProdName,cartProdPrice,cartAltPrice,cartOrderID,cartWish,cartDateAdded) VALUES (";
  472. $sSQL .= "'" . session_id() . "',";
  473. $sSQL .= "'" . $theid . "',";
  474. $sSQL .= $quantity . ",";
  475. $sSQL .= "0,";
  476. $sSQL .= "'" . mysql_real_escape_string($rsStock[getlangid("pName",1)]) . "',";
  477. $sSQL .= "'" . $final_price . "',";
  478. $sSQL .= "'" . $final_price_alt . "',";
  479. $sSQL .= "0,";
  480. $sSQL .= "'" . $_POST['wish'] . "',";
  481. $sSQL .= "'" . date("Y-m-d H:i:s", time() + ($dateadjust*60*60)) . "')";
  482. //echo $sSQL;
  483. mysql_query($sSQL) or print(mysql_error());
  484. $cartID = mysql_insert_id();
  485. foreach(@$_POST as $objItem => $objValue){
  486. if(substr($objItem,0,4)=="optn"){
  487. if(trim(@$_POST["v" . $objItem])==""){
  488. $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) . "'";
  489. $result = mysql_query($sSQL) or print(mysql_error());
  490. if($rs = mysql_fetch_array($result)){
  491. if(abs($rs["optType"]) != 3){
  492. $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] . "',";
  493. if(($rs["optFlags"]&1)==0) $sSQL .= $rs["optPriceDiff"] . ","; else $sSQL .= round(($rs["optPriceDiff"] * $final_price)/100.0, 2) . ",";
  494. if(($rs["optFlags"]&2)==0) $sSQL .= $rs["optWeightDiff"] . ")"; else $sSQL .= multShipWeight($rsStock["pWeight"],$rs["optWeightDiff"]) . ")";
  495. //echo $sSQL;
  496. }else
  497. $sSQL = "INSERT INTO cartoptions (coCartID,coOptID,coOptGroup,coCartOption,coPriceDiff,coWeightDiff) VALUES (" . $cartID . "," . $rs["optID"] . ",'" . mysql_real_escape_string($rs[getlangid("optGrpName",16)]) . "','',0,0)";
  498. mysql_query($sSQL) or print(mysql_error());
  499. }
  500. mysql_free_result($result);
  501. }else{
  502. //echo 'in2';
  503. $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) . "'";
  504. $result = mysql_query($sSQL) or print(mysql_error());
  505. $rs = mysql_fetch_array($result);
  506. $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)";
  507. mysql_query($sSQL) or print(mysql_error());
  508. mysql_free_result($result);
  509. }
  510. }
  511. }
  512. }
  513. //echo $sSQL;
  514. checkpricebreaks($theid,$final_price);
  515. }else{
  516. ?>
  517. <!-- If it's not in stock -->
  518. <div class="error">That item is not in stock</div>
  519. <?php
  520. }
  521. }
  522. ?>
  523. <?php
  524. $totaldiscounts = 0;
  525. calculatediscounts(0, true, '');
  526. //$_SESSION["discounts"]=$totaldiscounts;
  527. checkRelatedDiscounts();
  528. ?>
  529. <?php
  530. $sql_gc = "SELECT * FROM products WHERE p_iscert > 0";
  531. $res_gc = mysql_query($sql_gc) or print(mysql_error());
  532. $aGC = array();
  533. while($row_gc = mysql_fetch_assoc($res_gc)) {
  534. array_push($aGC,$row_gc['pName']);
  535. }
  536. $alldata="";
  537. $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() . "'";
  538. $sSQL .= " ORDER BY cartDateAdded DESC";
  539. $result = mysql_query($sSQL) or print(mysql_error());
  540. ?>
  541. <?php
  542. if(!empty($_SESSION['os'])){
  543. $sSQL1 = "SELECT * FROM coupons WHERE ";
  544. $sSQL1 .="cpnNumber='".$_SESSION['os']."'";
  545. $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";
  546. if(!empty($WSP)) $sSQL1 .= " AND (cpnIsWholesale=1)";
  547. else $sSQL1 .= " AND (cpnIsWholesale=0)";
  548. $sSQL1 .= " ORDER BY cpnID";
  549. //echo $sSQL1;
  550. $result21 = mysql_query($sSQL1) or print(mysql_error());
  551. if(mysql_num_rows($result21) > 0){ ?>
  552. <div style="padding:2px; font-size:10px;"><strong>Discounts:</strong><?php //print $xxDsProd?><br /><font color="#FF0000" size="1">
  553. <?php while($rs2=mysql_fetch_assoc($result21)){
  554. print $rs2['cpnName']." <br />";
  555. } ?></font></div>
  556. <?php
  557. }
  558. mysql_free_result($result21);
  559. }
  560. $changechecker = "";
  561. $index = 0;?>
  562. <div id="minicart_bg">
  563. <div style="float:right; margin-right:178px;">
  564. <a href="javascript:void(0);" onclick="Element.hide('<?=$_POST['addcartdiv']?>');"><img src="/lib/images/closelabel.gif" alt="Close" /></a>
  565. </div>
  566. <?php $number_rows=mysql_num_rows($result);
  567. if($number_rows<=0) {
  568. ?>
  569. <h3 style="margin-left:0px;">Your Cart Is Empty!</h3>
  570. <?php
  571. } else { ?>
  572. <? if(mysql_num_rows($result)>1 && $mode!="delete" && !$viewall){?>
  573. <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>
  574. <!-- <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>
  575. --> <? }?>
  576. <? if($mode=='delete'){?>
  577. <h3 style="margin-left:0px;">Your Item's Been Deleted!</h3>
  578. <? } elseif($viewall) {?>
  579. <h3 style="margin-left:0px;">All Items In Cart!</h3>
  580. <? } else { ?>
  581. <h3 style="margin-left:0px;">Your Item's Been Added!</h3>
  582. <div class="just_added_prod">Last item added...</div>
  583. <? }?>
  584. <table width="340" border="0" cellspacing="0" cellpadding="0" align="left">
  585. <?php } ?>
  586. <?php
  587. $cnt_products=0;
  588. $total_in_cart=0;
  589. while($alldata=mysql_fetch_assoc($result))
  590. {
  591. $allProducts[$alldata["cartProdID"]]+=$alldata["cartQuantity"];
  592. $index++;
  593. $changechecker .= 'if(document.checkoutform.quant' . $alldata["cartID"] . ".value!=" . $alldata["cartQuantity"] . ") dowarning=true;\n";
  594. $theoptions = "";
  595. $theoptionspricediff = 0;
  596. $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";
  597. $opts = mysql_query($sSQL) or print(mysql_error());
  598. $optPriceDiff=0;
  599. if($cntr > 3) {
  600. $cntr = 0;
  601. }
  602. if($alldata['isSet']=='yes') {
  603. $isSet = true;
  604. $cntr++;
  605. }else{
  606. $isSet = false;
  607. }
  608. $num_options=mysql_num_rows($opts);
  609. $options='';
  610. $comma='';
  611. if($num_options> 0) {
  612. while($rs=mysql_fetch_assoc($opts))
  613. {
  614. $options.='<div class="options_cart">'.$rs["coOptGroup"].' - '.$rs["coCartOption"].'</div> ';
  615. $comma=', ';
  616. }
  617. $cntr=0;
  618. }
  619. mysql_free_result($opts);
  620. if($alldata['cartID']==$cartID) $class='just_added';
  621. else if($mode=='delete' || $viewall)$class='all_cart_show';
  622. else $class='all_cart';
  623. $row_style="";
  624. if ($cnt_products % 2 == 0) {
  625. $row_style='style="background-color:#F4F4F4;"';
  626. }
  627. ?>
  628. <tr class="<?=$class?>" <?=$row_style?> >
  629. <td width="80%"align="left" valign="middle"><span class="prod_name_cart"><?=$alldata['cartProdName']?></span><?= $options!=""?': '.$options:''?></td>
  630. <td width="10%" valign="middle"><?=$alldata['cartQuantity']?></td>
  631. <td width="10%" valign="middle">
  632. <input type="image" src="/lib/images/trash_can.gif" name="delet<?=$alldata['cartID']?>" onclick="delItem(this.name, '<?=$_POST['addcartdiv']?>');" />
  633. </td>
  634. </tr>
  635. <?
  636. $total_in_cart+=($alldata['cartProdPrice']*$alldata['cartQuantity']);
  637. $cnt_products++;
  638. }
  639. if($number_rows>0) { ?>
  640. </table>
  641. <div style="clear:both;"></div>
  642. <? }
  643. $total_in_cart-=$totaldiscounts;
  644. include(APPPATH.'views/pages/admin/productbodyminicart.php');
  645. ?>
  646. <div style="margin:10px 0px; <? if($number_rows>0) echo 'border-top:1px solid #7089B0;';?> padding:10px 0px;">
  647. <div class="num_items">
  648. <?= $number_rows==1 ? $number_rows.' item' : $number_rows.' items'?>
  649. </div>
  650. <a href="/cart.php" title="View Cart" rel="nofollow"><img src="/lib/images/design/checkout_sm.gif" alt="Checkout" /></a>
  651. <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>
  652. <!--<div id="minicart_total">Total: <?=sprintf("$%01.2f",$total_in_cart)?></div>
  653. -->
  654. </div>
  655. </div>
  656. <? } ?>