<?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_artistid"]) and $_SESSION["sess_artistid"] != "") {
    $artistid = $_SESSION["sess_artistid"];
} else {
    header("Location:login.php");
    exit;
}
if(checkArtistExpired($artistid)){
    header("Location:seller_plan_upgrade.php?sellerid=".$artistid);
    exit;
}

$orderStatus = $_GET["status"];

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 deleted successfully!";
            break;
        default:
            ;
    } // switch
}

if (isset($_POST['gotoPage'])) {

    header("Location:" . $_SERVER['PHP_SELF'] . "?page=" . $_POST['gotoPage']);
}

$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) . "%'";
    }
}

$currencycode = getSellerCurrencySybol($artistid);
$currencySymbol = getCurrencySybol($currencycode);



//$qryopt .= " AND o.vorder_currency = cm.vcurrency_code";

$condition = "";
if (!empty($status) && $status == 1) {
    $condition = " AND os.order_status_id=$status";
}


$sql = "SELECT o.*, u.user_name,  u.email, od.product_discount,sum( (od.product_price-(od.product_price*product_discount/100)) * od.product_quantity) as total,sum(od.product_tax)as taxtotal
		FROM " . $tableprefix . "orders o 
		INNER JOIN " . $tableprefix . "users u ON o.user_id = u.user_id 
		INNER JOIN " . $tableprefix . "order_details od ON o.order_id = od.order_id 
                INNER JOIN " . $tableprefix . "order_status os ON od.item_status = os.order_status_id " . $condition . " 
		WHERE od.artist_id = '" . addslashes($artistid) . "'
		AND o.vpayment_status != 'P'" . $qryopt;
$sql.=" GROUP BY o.order_id ORDER BY o.order_id DESC ";

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

$limit = getSettingsValue('npagination_limit');

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


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

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

$totalrows = mysql_num_rows($result);

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

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

$sql = $sql . $query_string;

$rs = mysql_query($sql) or die(mysql_error());

$numofpages = ceil($totalrows / $limit);

include "../includes/htmltop.php";
?>
<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 QTN_CHNG_ST; ?>")){
            document.frmFeedbacks.submit();
        }
    }

    function addValues(order_value)
    {

        var checkbox_values = "";

        for(i=1;i<=document.frmFeedbacks1.checkCount.value;i++)
        {

            var checkbox_name = "Order_"+i;

            if(document.getElementById(checkbox_name).checked)
            {
                checkbox_values += document.getElementById(checkbox_name).value + ',';
            }

            document.frmFeedbacks1.mailOrder.value = checkbox_values;


        }
    }

    function checkAllOrders()
    {

        var checkbox_values = "";

        if(document.frmFeedbacks1.checkAllBox.checked)
        {

            for(i=1;i<=document.frmFeedbacks1.checkCount.value;i++)
            {

                var checkbox_name = "Order_"+i;
                document.getElementById(checkbox_name).checked = true;
                checkbox_values += document.getElementById(checkbox_name).value + ',';

            }

            document.frmFeedbacks1.mailOrder.value = checkbox_values;

        }
        else
        {
            for(i=1;i<=document.frmFeedbacks1.checkCount.value;i++)
            {

                var checkbox_name = "Order_"+i;
                document.getElementById(checkbox_name).checked = false;

            }

            document.frmFeedbacks1.mailOrder.value = "";
        }


    }

    function OrderMailPopup(orders)
    {


        if(document.frmFeedbacks1.mailOrder.value == "")
        {
            alert('<?php echo MSG_SELECT_ORDER; ?>');
        }
        else
        {
            Javascript:window.open('sales_order_acknowledgement.php?order_id='+orders, 'SalesOrder','menubar=0,resizable=0,width=1000,height=600')
        }


    }

</script>

<body>

<center>

    <div align="left" id="layout">




        <!--header area start -->
        <?php
        include_once("../includes/topheader_seller.php")
        ?>

        <!--header area end -->




        <div align="left" class="main_body">
            <!--main body start -->



            <?php
            include_once("../includes/artisttopmenu.php")
            ?>


            <div align="left" class="content_area">

                <!--content area start -->

                <div align="left" class="page_title"><?php echo TEXT_ORDERS;?></div>
                <div align="center" class="text_information grey_tr"><?php echo $message ?></div>
                <br>

                <div align="left">

                    <form action="<?php echo $_SERVER["PHP_SELF"] ?>" method="post" name="frmFeedbacks">
                         <?php
                                $dateblock = "none";
                                if($cmbSearchType!= "dateordered"){
                                           $txtBlock = "display";
                                           $dateblock = "none";
                                           $txtSearch1 = $txtSearch; 
                                           $disabled1   = "";
                                           $disabled2   = "disabled=disabled";
                                 } else{
                                           $txtBlock = "none";
                                           $dateblock = "display";
                                           $txtSearch2 = $txtSearch;
                                           $disabled1   = "disabled=disabled";
                                           $disabled2   = "";
                                }
                                                           
                          ?>

                        <table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_top">
                            <tr>
                                <td width="90%" align="left" valign="middle"><?php echo TEXT_SEARCH;?>
                                    <select name="cmbSearchType" id="cmbSearchType" class="textarea">
                                        <option value="username"  <?php
            if ($cmbSearchType == "username" || $cmbSearchType == "") {
                echo("selected");
            }
            ?>><?php echo TEXT_USER_NAME;?></option>
                                        <option value="email"  <?php
                                                if ($cmbSearchType == "email") {
                                                    echo("selected");
                                                }
            ?>><?php echo TEXT_EMAIL;?></option>
                                        <option value="dateordered"  <?php
                                                if ($cmbSearchType == "dateordered") {
                                                    echo("selected");
                                                }
            ?>><?php echo TEXT_ORDER_DATE;?></option>
                                        <option value="orderid"  <?php
                                                if ($cmbSearchType == "orderid") {
                                                    echo("selected");
                                                }
            ?>><?php echo TEXT_ORDER_ID;?></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(stripslashes($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>
                                    &nbsp;<a href="javascript:clickSearch();" class="go_button"><?php echo BTN_SEARCH; ?></a>&nbsp;
                                </td>

                            </tr>
                        </table>


                    </form>

                </div>

                <div align="left">


                    <table border="0" width="100%" cellpadding="0" cellspacing="0" class="manage_table">
                        <form action="" method="post" name="frmFeedbacks1">
                            <tr>
                                <td height="21" colspan="8"> <b><?php echo TEXT_BUYERS_SENTENCE;?></b><br></td>
                            </tr>
                            <tr class="form_header">
                                <th width="4%"><?php echo TEXT_SL_NO;?></th>
                                <th width="12%"><?php echo TEXT_ORDER_ID;?></th>
                                <th width="14%" align="left"><?php echo TEXT_USER_NAME;?></th>
                                <th width="19%" align="left"><?php echo TEXT_EMAIL;?></th>
                                <th width="13%" align="center"><?php echo TEXT_ORDER_DATE;?><br>
                                    </th>
                                <th width="13%" align="left"><?php echo TEXT_ORDER_TOTAL;?></th>
                                <th width="13%" align="left"><?php echo TEXT_STATUS;?></th>
                                <th width="12%" align="left"><input type="checkbox" name="checkAllBox" value="1" onClick="checkAllOrders();">
                                    <?php echo TEXT_SELECT_ALL ;?> &nbsp;/&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo TEXT_DESELECT;?></th>
                            </tr>


                            <?php
                            $itemTotal = 0;
                            
                            $orderTotal = 0;
                            
                            if (mysql_num_rows($result) > 0) {
                                $i = 1;
                                $slNo = $limit * ($page - 1);
                                while ($row = mysql_fetch_array($rs)) {
                                //--- Update seller view status ---//
                                $sql = "UPDATE ".$tableprefix."orders SET seller_viewed_status = 1 WHERE order_id = '".addslashes($row['order_id'])."'";			
                                mysql_query($sql) or die (mysql_error());
                                
                                    $link = "<a class=links href='vieworder.php?orderid=" . $row["order_id"] . "' class='smalllink'>";
                                    
                                    $itemTotal  =  $row["total"]; 
                                    
                                     $couponCodeDiscountAmount           =   0;
        
                                    if(($artistid==$row['couponCodeSeller_Id']) && ($row['couponCodeDiscount']>0))
                                        {
                                            $couponCodeDiscountAmount       =   $row['couponCodeDiscount'];
                                            $itemTotal                      =   $itemTotal - $couponCodeDiscountAmount;
                                        }
                                    
                                     if($row['nused_gift_amount']!=0 )
                                         {
                                                    
                                                    $giftDiscountAmount         =   $row['nused_gift_amount'];
                                                    
                                                    $itemTotal                  =   $itemTotal - $giftDiscountAmount;
                                                    
                                         }
                                         
                                         
                                    // added for including the shipping total
                                    $totalshippingcost = 0;

                                    $totalshippingcost = Order::orderShippingCost($row["order_id"],$artistid);

                                    $totalshippingcost = number_format(($totalshippingcost), 2, '.', '');
                                   
                                    
                                    $orderTotal = $itemTotal + $row["taxtotal"] + $totalshippingcost;
                                    
                                    $ordrst=getArtistOrderStatus($row["order_id"], $artistid);
                                                             
                                                                        switch ($ordrst){
																			
                                    										case 'Pending':$st=TEXT_PENDING;
                                    														break;
                                    										case 'Cancelled' :$st=TEXT_CANCELLED;
                                    														break;
                                    										case 'Shipped':$st=TEXT_SHIPPED;
                                    														break;
                                    										case 'Delivered':$st=TEXT_DELIVERED;
                                    														break;
                                    										case 'Returned to Seller':$st=TEXT_RET_SELLER;
                                    														break;
                                    										case 'Seller Received Back':$st=TEXT_SELLER_RECVD;
                                    														break;
                                    										}
                                    										
                                    ?>
                                    <tr>
                                        <td align="left"><?php echo++$slNo; ?></td>
                                        <td align="left"><?php echo $link . htmlentities($row["order_id"]) . "</a>"; ?></td>
                                        <td align="left"><?php echo $link . ($row["user_name"]) . "</a>"; ?></td>
                                        <td><?php echo $link . htmlentities($row["email"]) . "</a>"; ?></td>
                                        <td align="center"><?php echo $link . dateFormat($row["order_date"], "Y-m-d", "m/d/Y") . "</a>"; ?></td>
                                        <td align="left"><?php echo $adminCurrency['currency_symbol']; ?> &nbsp; <?php echo $link . htmlentities(number_format(($orderTotal), 2, '.', '')) . "</a>"; ?></td>
                                        <td align="left"><?php echo $link .$st."</a>"; ?></td>
                                        <td align="left">
                                            <?php
                                            $order_checkbox = "Order_" . $i;
                                            ?>
                                            <input type="checkbox" name="<?php echo $order_checkbox ?>" id="<?php echo $order_checkbox ?>" value="<?php echo $row['order_id'] ?>" onClick="addValues('<?php echo $row['order_id'] ?>');">
                                        </td>

                                    </tr>
                                    <?php
                                    $i++;
                                }
                                ?>

                                <input type="hidden" name="checkCount" value="<?php echo $i - 1 ?>">
                                <?php
                            } else {
                                ?>
                                <tr>
                                    <td align="center" valign="middle" colspan="7"  class="text_information"><?php echo TEXT_NO_RECORD;?></td>
                                </tr>
                                <?php
                            }

                            if ($page > 1) {
                                $pageprev = $page - 1;

                                $previous_page = "<a href=" . $_SERVER['PHP_SELF'] . "?page=$pageprev alt='Previous' border='0' class='small_gray_button'>".TEXT_PAGES_PREV."</a>";
                            }


                            if ($page < $numofpages) {
                                $pagenext = ($page + 1);

                                $next = "<a href=" . $_SERVER['PHP_SELF'] . "?page=$pagenext alt='Next' border='0' class='small_gray_button'>".TEXT_PAGES_NEXT."</a>";
                            }
                            ?>


                            <tr>
                                <td class="page_nos"  align="center" colspan="2">&nbsp;</td>
                                <td align="right" valign="middle" class="page_nos" colspan="4">&nbsp;</td>
                                <td align="left" class="btn_gotbl" valign="middle">&nbsp;</td>
                            </tr>
                            <?php
                            if ($page < $numofpages || $page > 1) {
                                ?>
                                <tr>
                                    <td  align="center" colspan="7">
										<table width="100%" border="0" cellspacing="10" cellpadding="0">
											<tr align="left" valign="middle">
												<td width="21%" align="right"><?php echo $previous_page ?></td>
												<td width="23%" align="right">
													<?php echo TEXT_PAGES; ?> <?php echo $page ?>/<?php echo $numofpages ?>&nbsp;&nbsp;<?php echo TEXT_GOTO;?>
												</td>
												<td width="8%" align="right">
													<select class="textarea2" name="gotoPage" style="padding:4px 5px; margin-bottom:1px; ">
                                            <?php
                                            for ($i = 1; $i <= $numofpages; $i++) {


                                                if ($i == $_GET['page']) {
                                                    $page_selected = "selected";
                                                } else {
                                                    $page_selected = "";
                                                }
                                                ?>
                                                <option value="<?php echo $i ?>" <?php echo $page_selected ?>><?php echo $i ?></option>

                                                <?php
                                            }
                                            ?>
                                        </select>
												</td>
												<td width="7%">
													<a onClick="Javascript:document.frmFeedbacks1.submit();" class="go_button_2"><?php echo BTN_GO; ?></a>
												</td>
												<td width="41%" align="center"><?php echo $next ?></td>
											</tr>
										</table>
									  </td>
                                </tr>
                                <?php
                            }
                            ?>
                            <input type="hidden" name="mailOrder" value="<?php echo $checked_value ?>">
                            <tr>
                                <td colspan="7" align="center">
                                    <a style="cursor:pointer" onClick="OrderMailPopup(document.frmFeedbacks1.mailOrder.value);" class="add_new_button">Email</a>
                                </td>
                            </tr>
                        </form>
                    </table>

                </div>


                <!--content area end-->

            </div>


            <div align="left" class="clear_float"><img src="<?php echo $imagefolder; ?>/clear.gif" alt="" width="1" height="1" /></div>
            <!--main body end -->
        </div>


        <?php
        include "../includes/footer.php";
        ?>
