<?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";
include "classes/cls_store.php";
include_once('api/pemfile/iphonepush.php');
if(isset( $_SESSION["sess_userid"]) and $_SESSION["sess_userid"]!= ""){
	$userid = $_SESSION["sess_userid"];
}
else
{
	header("Location:".SITE_URL."/login.php");
	exit;
}

$orderid					=   $_REQUEST['orderid'];
$userid                                         =   $_REQUEST['userid'];
$digital_flag                                   =   $_REQUEST['digitalflag'];
$gift_flag                                      =   $_REQUEST['giftflag'];
$usertype                                       =   $_REQUEST['usertype'];
$_SESSION["sess_guest_password"]                =   $_REQUEST['guestpassword'];

             if($orderid!='')
                        {

                      // Update Order Table
                        $sql_update="UPDATE " .$tableprefix. "orders SET vpayment_status='C' where order_id=".$orderid." ";

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

                      // Update Artists Payemt Table

                        $sql_update_payment="UPDATE " .$tableprefix. "artist_payments SET payment_status='C' where order_id=".$orderid." ";

                        $re_up=mysql_query($sql_update_payment) or die(mysql_error());

                         // Settle seller , Admin Amounts

                        // End

                        $sql_select_pay = "SELECT payment_completed_status  FROM  " .$tableprefix. "orders where order_id=".$orderid." ";

                        $rs_select_pay  =mysql_query($sql_select_pay) or die(mysql_error());

                        $rowPay         =mysql_fetch_array($rs_select_pay);

                        $paymentComplete_Status =$rowPay['payment_completed_status'];

                     if($paymentComplete_Status=='N'){

                        if($gift_flag==1)
                            {
                            sendgiftCertificateMail_Buyer($orderid,$userid );
			    sendgiftCertificateMail_Seller($orderid,$userid );
			    sendgiftCertificateMail_Admin($orderid,$userid );
                        }
                      
                        else{
                            sendBuyerEmail($orderid,$userid ,$usertype);
                            sendSellerEmail($orderid,$userid );
                            sendAdminEmail($orderid,$userid );
                        }

                        $sql_update_order_payment="UPDATE " .$tableprefix. "orders SET payment_completed_status='Y' where order_id=".$orderid." ";

                        $res_update=mysql_query($sql_update_order_payment) or die(mysql_error());

               }
                        adjustStock($userid ,"-" );
                        emptyCart($userid);

                $_SESSION["sess_addresses_collected"] = false;
		$_SESSION["sess_stock_adjusted"] = false;
                $_SESSION['couponCode_det']     = array();
                $_SESSION['couponCode_det']='';
		if(isset($_SESSION['guest_login']) && $_SESSION['guest_login']!='')
		{
                    if(function_exists('session_unregister')){
			session_unregister($_SESSION['guest_login']);
			session_unregister($_SESSION['sess_userid']);
                        session_unregister($_SESSION['SHIPPING_TOTAL']);
                    }else{
                        unset($_SESSION['guest_login']);
			unset($_SESSION['sess_userid']);
                        unset($_SESSION['SHIPPING_TOTAL']);
                    }
			$_SESSION['guest_login']='';
			$_SESSION["sess_userid"]='';
		}
		$_SESSION["sess_guest_username"]='';
		$_SESSION["sess_guest_password"]='';
		 $_SESSION["sessShippingDisplayName"]='';
		  $_SESSION['sess_giftcertificate_code']='';
		 $_SESSION['sess_gift_amount_used']='';
		 $_SESSION['amount_paid']='';
		 $_SESSION['sess_giftcertificate_available_amt']='';
                 $_SESSION["SHIPPING_DETAILS"] = '';
                  $_SESSION["TAX_DETAILS"] = '';
                  $_SESSION["TAX_TOTAL"] = '';
                  $_SESSION["SHIPPING_TOTAL"] = '';
                  $_SESSION["SESS_BUYER_COMMENTS"] ='';
            unset($_SESSION['SHIPPING_TOTAL']);
            $_SESSION["SHIPPING_DETAILS"]   =array();
             unset($_SESSION["SHIPPING_DETAILS"]);
             unset($_SESSION["TAX_DETAILS"]);
             unset($_SESSION['couponCode_det']);
           $payment_completed =true;

           header("Location:parallel_pay_success.php");
           exit;
   }


/*display the active template*/

$active_template = displayTemplate();


include "includes/htmltop.php";

/* Top File Name */

include_once ("includes/".$active_template[0]);
    $qry1="select max(order_id) as ord_id from " . $tableprefix . "orders";
    $excqry1=mysql_query($qry1);
    $row1=mysql_fetch_array($excqry1);
    $orderid=$row1['ord_id'];
    //--- Get Artist Id----//
    $sql_artist ="SELECT * FROM ".$tableprefix."orders o INNER JOIN ".$tableprefix."order_details od ON o.order_id = od.order_id where o.order_id='".$orderid."' AND vpayment_status='C' ";        
    $result=mysql_query($sql_artist) or die(mysql_error());			
    $artist_row = mysql_fetch_assoc($result);  
    $artistId = $artist_row['artist_id'];
    $productName = $artist_row['product_name'];
    // Send push notification
    $devicesql = "Select device_id from " . $tableprefix . "artists where artist_id ='$artistId'"; //Get seller device ID           
    $result = mysql_query($devicesql);
    $device_row = mysql_fetch_assoc($result);
    $msg = "New order placed at ".SITE_NAME. ", please check.";
    send_push_nofication($msg,SITE_NAME,"armia",$device_row['device_id'],"Order","order");//Message,From,passphrase,device token,type-->product,order...
    //-------------------------//
?>
<!--mainbody area start -->
<div class="main_body">
	<div class="container">
		<div class="row">

		<!--center content area start-->

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


		<!--items display area start -->

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

				<div align="left" id="items_top_area_ttl"><?php echo PP_PAY_SUCCESS_TEXT;?></div>
		</div>

		<div>&nbsp;</div>

		<div class="text_information" align="center"><?php echo $message?></div>

		<div align="center" id="items_display_area">

		<table cellpadding="0" cellspacing="0" width="100%">
			<tr>
				<td class="text_information" align="center">
					<b style="background-color:#000000; margin-bottom:15px;  font-family:Arial, Helvetica, sans-serif; font-size:12px; padding:10px 10px 10px 10px; color:#67CB1A;"><?php echo ORDER_PLACED_SUCESS_TEXT;?></b>
				</td>
			</tr>
			<tr>
			  <td height="8" align="center" ></td>
		  </tr>


			<tr>
				<td>&nbsp;</td>
			</tr>
			<tr>
				<td align="center">
					<input type='button' value='<?php echo HOME_LINK;?>'  class='button' onClick="window.location.href='<?php echo SITE_URL?>/index.php'">
					&nbsp;&nbsp;<input type='button' value='<?php echo CONTINUE_SHOPPING_BUTTON;?>'  class='button' onClick="window.location.href='<?php echo SITE_URL?>/categorydetail.php'">
				</td>
			</tr>
	  </table>
	</div>

	<div align="left" class="float_clear1"></div>
	 <!--items display area end -->
	  </div>
	<!--center content area end-->
	<div align="left" class="clear"></div>
	</div>
	</div>
</div>		
<!--mainbody area end -->
	<div align="left" class="clear"></div>


<?php
include "includes/".$active_template[2];	/* Bottom File Name */
?>															