<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts Multicart                     |
// +----------------------------------------------------------------------+
// | Authors: Programmer<programmer@armia.com>        		              |
// +----------------------------------------------------------------------+
// | Copyrights Armia Systems, Inc and iScripts.com � 2005                |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+
// | This script may not be distributed, sold, given away for free to     |
// | third party, or used as a part of any internet services such as      |
// | webdesign etc.                                                       |
// +----------------------------------------------------------------------+
?>
<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";
include "classes/cls_order.php";

if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE); 
} 

if(isset( $_SESSION["sess_userid"]) and $_SESSION["sess_userid"]!= ""){
	$userid = $_SESSION["sess_userid"];
}else{
	header("Location:login.php");
	exit;
}

$adminCurrency	= GetAdminCurrency();

/*if($begin == ""){
        $begin=0;
        $num=1;
        $numBegin=1;
}*/

$currency_attributes = displayCurrencyType();

if(isset($_GET["txtSearch"]) and $_GET["txtSearch"]!=""){
	$txtSearch = $_GET["txtSearch"];
	$cmbSearchType = $_GET["cmbSearchType"];
}else if(isset($_POST["txtSearch"]) and $_POST["txtSearch"]!=""){
	$txtSearch = $_POST["txtSearch"];
	$cmbSearchType = $_POST["cmbSearchType"];
}

if(isset($_GET["msg"]) and $_GET["msg"]!=""){
	$msg = $_GET["msg"];
	switch($msg){
		case "deleted": $message = FEEDBACK_DELETE_SUCCESS_TEXT;break;
		default:
			;
	} // switch
	
}

$qryopt="";
if($txtSearch != ""){
   if($cmbSearchType == "username"){
   		$qryopt .= "  AND  u.user_name like '" . addslashes($txtSearch) . "%'";
   }else if($cmbSearchType == "dateordered"){
   		$dt = $txtSearch;
		$arrdt = explode("/",$dt) ;
		$day = $arrdt[1];
		$month = $arrdt[0];
		$year = $arrdt[2];
		$dt = $year."-". $month ."-".$day;
		$qryopt .= "  AND  o.order_date like '" . addslashes($dt) . "%'";
   }
   else if($cmbSearchType == "email")
   {
          $qryopt .= "  AND  u.email like '" . addslashes($txtSearch) . "%'";
   }
   else if($cmbSearchType == "orderid")
   {
         $qryopt .= "  AND  o.order_id like '" . addslashes($txtSearch) . "%'";
   }
   
   
}
$qry="select * from ".$tableprefix."settings ";
$rs_q=mysql_query($qry) or die(mysql_error());
$rr=mysql_fetch_array($rs_q);
$sql=      "SELECT o.*,s.order_status,od.product_name,gm.ngift_product_id,od.user_refund_id as refund_id,od.artist_id,
			sum( (od.product_price-(od.product_price*product_discount/100)) * od.product_quantity) as total
			FROM ".$tableprefix."orders o 
			INNER JOIN ".$tableprefix."order_status s ON o.order_status = s.order_status_id 
			LEFT JOIN ".$tableprefix."order_details od ON od.order_id=o.order_id 
			LEFT JOIN ".$tableprefix."giftcoupon_master gm ON gm.ngift_product_id=od.product_id 
			WHERE o.user_id = '".addslashes($userid)."'
			AND o.vpayment_status != 'P'  " . $qryopt . " 
			GROUP BY od.order_id 
			ORDER BY o.order_id DESC";
			

if(!(isset($pagenum)))
{
	$pagenum = 1;
} 


$result=mysql_query($sql) or die(mysql_error());						
	
$totalrows = mysql_num_rows($result);

$limit = 10;

$page = isset($_GET['page'])? trim($_GET['page']):1;

if(empty($page))
{
	$page = 1;
}

//$limitvalue = ($page - 1) * $limit;

//$query_string = " LIMIT $limitvalue, $limit";

//$sql = $sql.$query_string;
//echo $sql;
$sql_qr=dopaging($sql,'',$limit);	

$result=mysql_query($sql_qr) or die(mysql_error());


/*display the active template*/

$active_template = displayTemplate();


include "includes/htmltop.php";

/* Top File Name */

include_once ("includes/".$active_template[0]);

?>
<link rel="stylesheet" href="<?php echo SITE_URL; ?>/style/jquery-ui.css">
<script src="<?php echo SITE_URL; ?>/js/jquery-ui.js"></script>
<script language="JavaScript" type="text/JavaScript">
    $(document).ready(function () {
        $("#datepicker").datepicker({
                dateFormat: 'mm/dd/yy',
                maxDate: '0'
       
         });
      
    $("#cmbSearchType").on("change",function(){
        if(this.value=='dateordered'){
             $("#textSelector").hide();
             $("#dateSelector").show();
             $("#txtSearch").attr("disabled","disabled");
             $("#datepicker").removeAttr("disabled");
            
        }else{
             $("#textSelector").show();
             $("#dateSelector").hide();
             $("#datepicker").attr("disabled","disabled");
             $("#txtSearch").removeAttr("disabled");
             
        }
        
        
        });
    });
function clickSearch()
{
	document.frmFeedbacks.submit();
}
function changeStatus(id){
	var frm = document.frmFeedbacks;
	if(Confirm('<?php echo CONFIRM_CHANGE_STATUS_TEXT;?>')){
		document.frmFeedbacks.submit();
	}
}

//-->
</script>


<!--mainbody area start -->
<div class="main_body">
	<div class="container">
		<div class="row">			

		<!--center content area start-->		

		<form action="<?php echo $_SERVER["PHP_SELF"]?>" method="post" name="frmFeedbacks">
		
		<section class="sec_marg">
		<div class="col-sm-12 col-lg-12 col-md-12 ">
			<h3><?php echo ORDERS_TEXT;?></h3>
		</div>
		</section>
		<div class="clear"></div>
		
		<div class="col-sm-12 col-lg-12 col-md-12 ">
		<!--items display area start -->
		
		<div>&nbsp;</div>
		<div class="text_information" align="center"><?php echo $message?></div>
		<div>
                     <?php
                                $dateblock = "none";
                                if($cmbSearchType == "orderid"){
                                           $txtBlock = "display";
                                           $dateblock = "none";
                                           $txtSearch1 = $txtSearch; 
                                           $disabled1   = "";
                                           $disabled2   = "disabled=disabled";
                                 } else{
                                           $txtBlock = "none";
                                           $dateblock = "display";
                                           $txtSearch2 = $txtSearch;
                                           $disabled1   = "disabled=disabled";
                                           $disabled2   = "";
                                }
                                                           
                             ?>
			
			<div class="order_listings">
			<section class="sec_marg">
				<div class="row">
				<div class="col-sm-12 col-lg-12 col-md-12 ">
					<div class="order_serch_sec">
						<?php echo SEARCH_LABEL;?>
							<select name="cmbSearchType"  id="cmbSearchType" class="textarea bord_radius">
								<option value="dateordered"  <?php if($cmbSearchType == "dateordered"){ echo("selected"); } ?>><?php echo ORDER_DATE_TEXT;?></option>
								<option value="orderid"  <?php if($cmbSearchType == "orderid"){ echo("selected"); } ?>><?php echo ORDER_ID_TEXT;?></option>
							</select> &nbsp;
							 <span id="textSelector" style="display:<?php echo $txtBlock; ?>"  >
                                        <input type="text" <?php echo $disabled1; ?> name="txtSearch" id="txtSearch" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch1)); ?>"  onKeyPress="if(window.event.keyCode == '13'){ return false; }"  class="textarea">
                                   </span>
                                    <span id="dateSelector" style="display:<?php echo $dateblock; ?>" >
                                        <input type="text" <?php echo $disabled2; ?> name="txtSearch" id="datepicker" size="20" maxlength="50" value="<?php echo(htmlentities($txtSearch2)); ?>"   class="textarea">
                                    </span>
                                                        <input name="Submit" type="submit" class="gobtn button" value="<?php echo GO_BUTTON;?> "/>
						
						<p>
							<?php echo GIFT_NOTE_TEXT;?>
						</p>
						<p>
							<?php echo REFUND_NOTE_TEXT;?> 
						</p>
					</div>
				</div>
				</div>
			</section>
			
			<table width="100%"  border="0" cellpadding="0" cellspacing="1" align="center" class="responsive">
	
				<tr class="ordheads_heads">
					  <td width="5%"><b>#</b></td>
					  <td width="15%"><strong><?php echo ORDER_ID_TEXT1;?> </strong></td>
					  <td width="22%" align="left" ><b><?php echo DATE_ADD_TEXT;?></b></td>				     
					  <td width="17%" align="left" ><b><?php echo ORDER_TOTAL_TEXT;?></b></td>
					  <td width="18%"><b><?php echo STATUS_TEXT;?></b></td>
					  <td width="18%"><b><?php echo ACTION_TEXT;?></b></td>
				</tr>
						<?php
								if(mysql_num_rows($result) > 0 )
								{
									
									$i=1;
                                                                        $couponCodeDiscountAmount = 0;
                                                                        
                                                                        $couponCodeDiscount = 0;
                                                                        
                                                                        $TotalDiscount  = 0;
                                                                        $totalshippingcost = 0;
									while($row = mysql_fetch_array($result))
									{
									
										
										$currencySymbol=getCurrencySybol($row['vorder_currency']);
										$link = "<a href='vieworder.php?orderid=".$row["order_id"]."'>";
										$check = "<input type='checkbox' name='chkproducts[]' value='" . $row["product_id"] . "' > ";
													
									
										
									 $sql_Shipping="SELECT sum(nshipping_cost)as nShippingcost from ".$tableprefix."shipping_details where norder_id=".$row['order_id']." AND nshp_status='C' ";
									 $rs_shipping=mysql_query($sql_Shipping);
									 $rsShippingObj=mysql_fetch_object($rs_shipping);
                                                                            $totalshippingcost =number_format($rsShippingObj->nShippingcost,2,".","");
                                                                            $orderprice=number_format($row["total"],2,".","");
                                                                            $couponCodeDiscount=$row["couponCodeDiscount"];
                                                                            
                                                                            $discountFlag   =   0;
									  if($row["nused_gift_amount"] >0 || $couponCodeDiscount>0 ) {
                                                                                                    
                                                                                                   $giftDiscountAmount           = $row["nused_gift_amount"];
                                                                                                   
                                                                                                   //$couponCodeDiscountAmount = ($row["total"]*$row["couponCodeDiscount"])/100;
                                                                                                   $couponCodeDiscountAmount = $row["couponCodeDiscount"];
                                                                                                    
                                                                                                   $TotalDiscount   = $giftDiscountAmount + $couponCodeDiscountAmount;
                                                                                                   $orderprice      =   $orderprice - $TotalDiscount;
                                                                                                   
                                                                                                    $discountFlag   =   1;
                                                                                                   $orderprice=number_format($orderprice,2,".","");                               
                                                                                                }
       
?>
										<tr> 
											<td><?php echo $link ?> <?php echo (($page*$limit)+$i-$limit)?></a></td>
                                                                                        <td> <?php if($discountFlag==1) { ?> <span class="required">* </span> <?php } ?><?php echo $link .$row["order_id"]."</a>";?></td>
											<td align="left"><?php echo $link .dateFormat($row["order_date"], "Y-m-d", "m/d/Y")."</a>";?></td>
											<td align="left"><?php
												
                                                                                               
                                                                                                $taxrate                =      Order::getorderTaxTotal($row['order_id']);
                                                                                                
                                                                                                $orderTotal             =      $orderprice+$totalshippingcost+$taxrate;
                                                                    
                                                                                                //echo "<br>total =>".$orderprice."--Shipping=>".$totalshippingcost."====tax=>".$taxrate;
												$i++;
												
                                                                                                
                                                                                                $taxrate
											?>
											<?php  echo $adminCurrency['currency_symbol'];?> <?php echo $link.number_format($orderTotal,2,".","");"</a>";?>&nbsp; <?php if($row['vgiftcertificate_code']!='') { echo "*" ; } ?></td>
											<td align="left" class="text_information alignleftext">
											<?php 
											switch (getOrderStatus($row["order_id"]))
											{
												case 'Delivered': $status = DELIVERED_STATUS_TEXT;
																	break;
												case 'Pending': $status = PENDING_STATUS_TEXT;
																	break;
												case 'Shipped': $status = SHIPPED_STATUS_TEXT;
																	break;
												case 'Cancelled': $status = CANCELLED_STATUS_TEXT;
																	break;
												case 'Returned to Seller': $status = RETURNED_STATUS_TEXT;
																	break;
												case 'Seller Received Back': $status = RECEIVED_STATUS_TEXT;
																	break;
											}	
											echo $link .$status."</a>";?>
											</td>
											<td align="left" class="required">
											<?php if($rr['enable_rma']=='Y' &&   $row['ngift_product_id']=='' && (substr_count( getOrderStatus($row["order_id"]),"Delivered")>0) && $row["refund_id"]=='0'){ echo " <I><a href='refund_request.php?orderid=".$row["order_id"]."' class='required'> ".REQUEST_REFUND_TEXT."  </a><I>";}?>
											<?php if($rr['enable_rma']=='Y' &&  htmlentities(getOrderStatus($row["order_id"]))=="Delivered" && $row["refund_id"]!='0'){ echo " ( ".REQUESTED_REFUND_TEXT.")";}?>
											</td>
										</tr>
										
<?php									
									
									
									}
								}
								else
								{
?>					
										<tr>
											<td colspan="7" class="text_information" align="center"><?php echo NO_RESULT_TEXT;?></td>
										</tr>
										
<?php					
								}		
								
								
														
?>
								
			  </table>
			<div class="clear"></div>
			</div>
			<div class="row">
			<div class="col-sm-12 col-lg-6 col-md-6">
				<input type="button" value="<?php echo ACCOUNT_BACK_BUTTON;?>" name="btnBack"  class="button" onClick="window.location.href='myaccount.php'">
			</div>  	
			<div class="col-sm-12 col-lg-6 col-md-6">
				<?php 
				if($totalrows>10)
				{
				
				?>	
										<div  class="pagination"> <?php echo rightpaging();?></div>
				<?php } ?>					
			</div>
			</div>
			<div class="clear"></div>
		  
							
							
						
																										
		</div>	
	
	
		<div align="left" class="float_clear1"></div>
					
					
		 <!--items display area end -->
		  </div>
		
			</form>
		<!--center content area end-->	

	</div>
	</div>
</div>
<!--mainbody area end -->
<div class="clear"></div>
<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>																										
																										
																										
																									