<?php

/* FIRESHOP: Advanced Content Market System                         */

/* ============================================                         */

/*                                                                      */

/* Copyright (c) 2012 by Mohamad Porhaji                                */

/* http://Firedesign.ir                                                   */

/*                                                                      */

/* This program is free software.  */

/************************************************************************/

/* $Id: buy.php,v 1.0.0.1 2012/02/18 By Mohamad Porhaji $ */
@session_start();
define('BUY_FILE', true);
require_once("mainfile.php");
global $db;


$title_page_over="سبد خريد";
$page_title = "خريد محصول";


function content()

{
if(isset($_POST['ref_num'])){
global $db;
$id_i=$_POST['id'];
$num_pro_i = $_POST['num_pro_i'];
$query_i=$db->sql_query("UPDATE fireshop_buy_session SET  `num_pro` = '$num_pro_i' WHERE `id` = '$id_i' LIMIT 1");
    Header("Location: buy.php");
}

if(isset($_POST['del_pro'])){
global $db;
$id_i=$_POST['id'];
$query = $db->sql_query("DELETE FROM `fireshop_buy_session` WHERE `id`='$id_i' LIMIT 1");
    Header("Location: buy.php");
}





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

if(empty($_POST['id']))
{
echo"شماره محصول يافت نشد";
}else{



$id =$_POST['id'];
global $db;
$query = $db->sql_query("SELECT * FROM `fireshop_product` WHERE id='$id'") or die(mysql_error());
if($db->sql_numrows($query) == 0 ){
echo"چنين محصولي در سايت وجود ندارد";
}else{

$row = $db->sql_fetchrow($query);

$pro_id= $row['id'];
$product_title=$row['product_title'];
$product_price = $row['product_price'];
$product_price_s = number_format($product_price, 0);
$ip = @$_SERVER['REMOTE_ADDR'];
$time_stamp= time();
include("includes/jdate.php");
$today = jdate(" d /m /13y");
$dater = $today;


if(@$_SESSION['BUY_AGAINE'] == true)
{
$order_id=$_SESSION['order_id'];
}else{
$query = $db->sql_query("INSERT INTO fireshop_order (`ip`,`date`,`time_stamp`) VALUES ('$ip','$dater','$time_stamp')");
$query_s = $db->sql_query("SELECT * FROM `fireshop_order` WHERE time_stamp='$time_stamp'") or die(mysql_error());
$row = $db->sql_fetchrow($query_s);
$order_id= $row['id'];
$_SESSION['BUY_AGAINE'] = true;
$_SESSION['order_id']=$order_id;
}

$query = mysql_query("UPDATE `fireshop_product` SET `total_sale`=total_sale+1 WHERE id='$id' LIMIT 1");
$query = $db->sql_query("INSERT INTO fireshop_buy_session (`order_id`,`pro_id`, `ip`, `time_stamp`, `date`, `pro_title`, `pro_price`) VALUES ('$order_id','$pro_id', '$ip', '$time_stamp', '$dater','$product_title','$product_price')");

echo"<div align='center' dir='rtl' style='padding:8px;'>
<table style='background-color: #333333; font-family: tahoma; font-size: 11px; text-decoration: none; color: #CCCCCC'>
<tr>
<td style='width: 290px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
عنوان</td>
<td style='width: 75px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
مبلغ واحد</td>
<td style='width: 37px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

تعداد</td>
<td style='width: 76px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

قابل پرداخت</td>

<td style='width: 82px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
بروز رساني</td>
<td style='width: 36px'>
حذف</td></tr>
</table>";
$query = $db->sql_query("SELECT * FROM `fireshop_buy_session` WHERE ip='$ip' AND  order_id='$order_id'");
while($row=$db->sql_fetchrow($query)){

$id_i= $row['id'];
$pro_id= $row['pro_id'];
$num_pro= $row['num_pro'];
$ip= $row['ip'];
$pro_title= $row['pro_title'];
$pro_price= $row['pro_price'];
$pro_price_s= number_format($pro_price, 0);
$total_price_num = ($pro_price*$num_pro);
$total_price_num_s  = number_format($total_price_num, 0);
$subtotal+=$total_price_num;
$subtotal_s= number_format($subtotal, 0);
echo"
<form style='margin:0px' action='buy.php' method='post'>
<input type='hidden' name='id' value='$id_i'/>
<table style='background-color: #CCCCCC; font-family: tahoma; font-size: 11px; text-decoration: none; color: #333333'>
<tr>
<td style='width: 290px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

$pro_title</td>
<td style='width: 75px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

$pro_price_s</td>
<td style='width: 37px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
 <input type='text' size='3' name='num_pro_i' value='$num_pro'/>
</td>
<td style='width: 76px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

$total_price_num_s</td>


<td style='width: 74px'>

<input type='submit' name='ref_num' value='بروز رساني'/></td>


<td style='width: 36px'>

<input type='submit' name='del_pro' value='حذف'/></td>



</tr>
</table></form>


";
}
echo"
<br/>
<div  style='margin-left:155px;'  align='left'>
<table  style='background-color: #333333; font-family: tahoma; font-size: 11px; text-decoration: none; color: #cccccc' align='left' >
<tr>
<td valign='top'>

جمع كل :
</td>
<td valign='top'>

$subtotal_s  تومان
</td>
</tr>
</table></div>
<br/><br/>
<form style='float:left' action='order.php' method='post'><div style='margin-left:20px;'  align='left'><input type='submit' name='END_BUY' value='اتمام خريد'/></div></form>
<form style='float:left' action='buy.php' method='post'><div align='left'><input type='submit' name='refrash' value='بروز رساني سبد خريد'/></div></form>

<div class='clear'></div>
</div>";

}

}
}else{
global $db;
$ip = @$_SERVER['REMOTE_ADDR'];
if(@$_SESSION['BUY_AGAINE'] == true)
{
$order_id=$_SESSION['order_id'];
}
$query = $db->sql_query("SELECT * FROM `fireshop_buy_session` WHERE ip='$ip' AND  order_id='$order_id'") or die(mysql_error());
if($db->sql_numrows($query) == 0 ){
echo"سبد خريد شما خالي است";
}else{


echo"<div align='center' dir='rtl' style='padding:8px;'>
<table style='background-color: #333333; font-family: tahoma; font-size: 11px; text-decoration: none; color: #CCCCCC'>
<tr>
<td style='width: 290px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
عنوان</td>
<td style='width: 75px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
مبلغ واحد</td>
<td style='width: 47px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

تعداد</td>
<td style='width: 76px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

قابل پرداخت</td>

<td style='width: 82px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
بروز رساني</td>
<td style='width: 36px'>
حذف</td></tr>
</table>";
$query = $db->sql_query("SELECT * FROM `fireshop_buy_session` WHERE ip='$ip' AND  order_id='$order_id'");
while($row=$db->sql_fetchrow($query)){

$id_i= $row['id'];
$pro_id= $row['pro_id'];
$num_pro= $row['num_pro'];
$ip= $row['ip'];
$pro_title= $row['pro_title'];
$pro_price= $row['pro_price'];
$pro_price_s= number_format($pro_price, 0);
$total_price_num = ($pro_price*$num_pro);
$total_price_num_s  = number_format($total_price_num, 0);
$subtotal+=$total_price_num;
$subtotal_s= number_format($subtotal, 0);

echo"
<form style='margin:0px' action='buy.php' method='post'>
<input type='hidden' name='id' value='$id_i'/>
<table style='background-color: #CCCCCC; font-family: tahoma; font-size: 11px; text-decoration: none; color: #333333'>
<tr>
<td style='width: 290px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

$pro_title</td>
<td style='width: 75px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

$pro_price_s</td>
<td style='width: 47px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>
 <input type='text' size='3' name='num_pro_i' value='$num_pro' />
</td>
<td style='width: 76px; border-left-style: solid; border-left-width: 1px; border-left-color: #CCCCCC;'>

$total_price_num_s</td>


<td style='width: 74px'>

<input type='submit' name='ref_num' value='بروز رساني'/></td>


<td style='width: 36px'>

<input type='submit' name='del_pro' value='حذف'/></td>



</tr>
</table></form>


";
}



echo"
<br/>
<div  style='margin-left:155px;'  align='left'>
<table  style='background-color: #333333; font-family: tahoma; font-size: 11px; text-decoration: none; color: #cccccc' align='left' >
<tr>
<td valign='top'>

جمع كل :
</td>
<td valign='top'>

$subtotal_s  تومان
</td>
</tr>
</table></div>
<br/><br/>
<form style='float:left' action='order.php' method='post'><div style='margin-left:20px;'  align='left'><input type='submit' name='END_BUY' value='اتمام خريد'/></div></form>
<form style='float:left' action='buy.php' method='post'><div align='left'><input type='submit' name='refrash' value='بروز رساني سبد خريد'/></div></form>

<div class='clear'></div>
</div>";
}
}





}
theme();
?>