<?php
ob_start();
/* 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";


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;
}


include "../includes/htmltop.php";


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


	/*check whether gift coupon with the same name exists in the database*/
		
	$select_coupon = "SELECT * FROM ".$tableprefix."products WHERE LOWER(product_name) = ".GetSQLValueString(strtolower($_POST['txtBannerName']),"text");
	$result_coupon = mysql_query($select_coupon) or die(mysql_error());
	
	if(mysql_num_rows($result_coupon)>0)
	{
	
		$message = MSG_CERT_EXIST;
	}
	else
	{	
	
		$couponfiletype = $_FILES['txtCouponImage']['type'];
		$couponfilename = $_FILES['txtCouponImage']['name'];
		$coupontempname	= $_FILES['txtCouponImage']['tmp_name'];

		if ($_FILES['txtCouponImage']['name'][0] != "")
		{
			if (!isValidWebImageType($couponfiletype,$couponfilename,$coupontempname))
			{
				
				$message .= MSG_INVAL_IMG_UPLD . "<br>";
				
			}
			else
			{
		
				
				$imagewidth_height_type_array = explode(":", ImageTypeBanner($_FILES['txtCouponImage']['tmp_name']));
				$imagetype = $imagewidth_height_type_array[0];
	
								
				$assignedname_small = "gift_coupon_small" . time() . "." . $imagetype;
				
				$assignedname_large = "gift_coupon_large" . time() . "." . $imagetype;
				
				if (move_uploaded_file($_FILES['txtCouponImage']['tmp_name'], "../products/" . $assignedname_large))
				{
				
					chmod("../products/$assignedname_large", 0777);
					
					/*taken from the general site settings table*/
					
					$gift_image_size = '400,400';
					$gift_size  = split(',',$gift_image_size);
					
					//generateThumbNail("../products/".$assignedname_large,"../products/".$assignedname_large,400,400);
					
					resizeImg("../products/".$assignedname_large, 283 ,269, false, 100, 0,"_thumb");
					
					@copy("../products/$assignedname_large","../products/$assignedname_small");
					
					//generateThumbNail("../products/".$assignedname_small,"../products/".$assignedname_small,120,150);
					
					resizeImg("../products/".$assignedname_small, 191 ,183, false, 100, 0,"_thumb");
															
					$select_coupon_category = "SELECT category_id FROM ".$tableprefix."categories WHERE gift_certificate_flag = 'Y'";
					$result_coupon_category = mysql_query($select_coupon_category) or die(mysql_error());
					
					$coupon_row = mysql_fetch_assoc($result_coupon_category);
		
                                        $insert_gift_coupon = "INSERT INTO ".$tableprefix."products(product_name,product_code,product_description,product_price,
										   product_artist_id,product_category,date_added,deleted,vapproved)
										   VALUES(".GetSQLValueString($_POST['txtGiftCouponName'],"text").","
                                                                                            .GetSQLValueString($_POST['txtGiftCouponCode'],"text").","
                                                                                            .GetSQLValueString($_POST['txtCouponDescription'],"text").","
                                                                                            .GetSQLValueString($_POST['txtCouponAmount'],"text").","
                                                                                            .GetSQLValueString($artistid,"text").","
                                                                                            .$coupon_row['category_id'].",now(),'N','Y')";

					mysql_query($insert_gift_coupon) or die(mysql_error());

					$insert_product_id = mysql_insert_id();

                                        $insert_product_options = "INSERT INTO ".$tableprefix."product_options (product_id,product_stock,reorder_level,image_small,image_big,default_option)
                                                                    VALUES
                                                                    (".$insert_product_id.","
                                                                      .GetSQLValueString($_POST['txtCouponStock'],"text").","
                                                                      .GetSQLValueString($_POST['txtStockReorder'],"text").",'"
                                                                      .$assignedname_small."','".$assignedname_large."','YES')";

					mysql_query($insert_product_options) or die(mysql_error());

					
					
					//=================================================Send Mail to Admin =============================================================
		if(!empty($insert_product_id))
		{ // check if product added to data base 
		
		
			 $select_seller_name = "SELECT p .*, c.category_name, a.artist_name,po.product_stock,po.image_small,po.image_big
                                                    FROM ".$tableprefix."products p
                                                    INNER JOIN ".$tableprefix."product_options po ON po.product_id = p.product_id AND po.default_option = 'YES'
                                                    INNER JOIN ".$tableprefix."artists a ON p.product_artist_id = a.artist_id
                                                    INNER JOIN ".$tableprefix."categories c ON c.category_id = p.product_category
                                                    WHERE p.product_artist_id =".$artistid."
                                                    AND p.product_id = ".$insert_product_id."
                                                    LIMIT 0 , 1";
										
			$result_seller_name = mysql_query($select_seller_name) or die(mysql_error());
			$numrows=mysql_num_rows($result_seller_name);
			if($numrows>0)
				{ // check number of rows
					$rs=mysql_fetch_array($result_seller_name);
					$seller_username=$rs['artist_name'];
					$seller_name=SITE_EMAIL;
					$category_name=$rs['category_name'];
					$product_code=$rs['product_code'];
					$product_name=$rs['product_name'];
					$product_price=$adminCurrency['currency_symbol']."&nbsp;".number_format($rs['product_price'],2,".","");;
					$stock_available=$rs['product_stock'];
					$final_image_small=$rs['image_small']; 
					$final_image_big=$rs['image_big']; 
				} // end if 
				
				$to=SITE_EMAIL;
				$date=date("F d,Y");
				
				$shipping_weight=$txtShippingWeight ." lbs";
			 //===========================For showing product images====================================
				if($final_image_small=="")
		   			{ // check if thumnail image is not avilabe select images from 
						if(!empty($final_image_big))
						{ // check if big image is uploaded or not 
						
						$product_image=SITE_URL."/products/$final_image_big";
						}	
						  else
						  { // if images not in the table 
							$product_image=SITE_URL."/products/noimage.jpg";
							
						  }	 // end if 
						
				  }  // end if product image null check 
		   else
		   {
		   		$product_image=SITE_URL."/products/$final_image_small";
		   }		
		  $imageurl='<img src="'.$product_image.'" width="120" height="150" border="0" >';
			//==================================End display product image===============================
				
			$link="<a href=".SITE_URL."/admin/editproduct.php?productid=$insert_product_id>Click here to view the details</a>" ;
			/*---------Multicart Seller Account Acivation Email Confirmation to seller------------*/
			$subject = "A New Gift Certificate been added in - ".stripslashes(SITE_NAME);
	/*-------Email Template Reading Fuction Call--------*/				
			$mailcontent = readEmailTemplate('new_gift_certificate_seller','products');
			$mailcontent = str_replace('[seller_username]',$seller_username,$mailcontent);	
			$mailcontent = str_replace('[seller_name]',$seller_name,$mailcontent);						
			$mailcontent = str_replace('[date]',$date,$mailcontent);
			$mailcontent = str_replace('[certificate_name]',stripslashes($_POST["txtGiftCouponName"]),$mailcontent);		
			$mailcontent = str_replace('[certificate_code]',stripslashes($_POST["txtGiftCouponCode"]),$mailcontent);				
			$mailcontent = str_replace('[certificate_amount]',$product_price,$mailcontent);	
			$mailcontent = str_replace('[stock_reorder]',stripslashes($_POST["txtStockReorder"]),$mailcontent);	
			$mailcontent = str_replace('[imglink]',$link,$mailcontent);	
			$mailcontent = str_replace('[stock_available]',stripslashes($_POST["txtCouponStock"]),$mailcontent);	

			$headers  = "MIME-Version: 1.0" . "\r\n";
			$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
			$headers .= "From: ".SITE_EMAIL."<".SITE_EMAIL.">" . "\r\n";
			
			$mailcontent = nl2br($mailcontent);
					
		$mail_send_details = '<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   <title>Gift Coupon Adding</title>
  <link href="./themes/'. $active_template[4].' rel="stylesheet" type="text/css">
</head>
<body>

<table width="103%" cellspacing="10" cellpadding="0" bgcolor="#EAEAEA">
   <tr>
     <td align="center" valign="top">
         
         <table width="600" border="0" cellpadding="0" cellspacing="0" bordercolor="#EAEAEA" bgcolor="#FFFFFF" class="manage_table">
            <tr>
               <td align="right" >
              </td>
            </tr>
            <tr>
               <td valign="middle" align="left"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                 <tr>
                   <td  bgcolor="#EAEAEA" > <img src='.SITE_URL.'/banners/'.getSettingsValue('site_logo').' width="125" height="60"/></td>
                 </tr>
               </table></td>
            </tr>
            <tr>
              <td align="center" valign="top"><table width="100%"  border="0" cellspacing="0" cellpadding="10">
                <tr>
                  <td align="left"><p>'.$mailcontent.'</p>
                  </td>
                </tr>
              </table></td>
            </tr>
            <tr>
               <td align="center" bgcolor="#EAEAEA" class="footer">
                  <p>&nbsp;</p>
              </td>
            </tr>
       </table>

     </td>
   </tr>
</table>


</body>
</html>';

			
			$mailsent = mail($to, $subject, $mail_send_details, $headers);
			
		
 } // end if 
		
		
//==============================================End MAil To Admin=================================================================		
		
					
					/*$insert_gift_coupon = "INSERT INTO ".$tableprefix."giftcoupon_master(ngift_product_id,vgift_coupon_code,
											ngift_coupon_amount,vgift_currency,vgift_delete_status)
										   VALUES(".$insert_product_id.","
													.GetSQLValueString($gift_coupon_code,"text").","
													.GetSQLValueString($_POST['txtCouponAmount'],"text").",
													'USD','N')";*/
													
					 $insert_gift_coupon = "INSERT INTO ".$tableprefix."giftcoupon_master(ngift_product_id,
											ngift_coupon_amount,vgift_currency,vgift_delete_status)
										   VALUES(".$insert_product_id.","
													.GetSQLValueString($_POST['txtCouponAmount'],"text").",
													'USD','N')";													
																				
					
                                        mysql_query($insert_gift_coupon) or die(mysql_error());
										
					$update_category_count = "UPDATE ".$tableprefix."categories SET count = count + 1 WHERE category_id = ".$coupon_row['category_id'];
					mysql_query($update_category_count) or die(mysql_error());
										
										
				header('Location:gift_coupon_artist.php?msg='.MSG_GIFT_CERTIFICATE_ADDED_SUCCESSFULLY);
				}					  
			}					  
		}						  
	}						  
}
?>
<script>
function checkAddCouponForm()
{

  var form_fields = new Array('txtGiftCouponName','txtGiftCouponCode','txtCouponImage','txtCouponAmount');
  var display_name = new Array('<?php echo TEXT_GIFT_CERT_NAME; ?>','<?php echo TEXT_GIFT_CERT_CODE; ?>','<?php echo TEXT_GIFT_CERT_IMG; ?>','<?php echo TEXT_GIFT_CERT_AMOUNT; ?>');
  var count = 4;	
  
  if(check_fields('frmAddGiftCoupon',form_fields,display_name,count))
  {
  	 if(!check_numeric(trimSpaces(document.frmAddGiftCoupon.txtCouponAmount.value)))
	 {
	 
	 	alert('<?php echo VAL_MSG_NUMERIC;?>');
		document.frmAddGiftCoupon.txtCouponAmount.focus();
		return false;		 
	 
	 }
   
  }
  else
  {
  
  		return false;
  
  }
   
}

</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_ADD_GIFT_CERT; ?></div>
					<div align="center" class="text_information grey_tr"><?php echo $message?></div>
            		<br>
											
					<div align="left">		
					<form name="frmAddGiftCoupon" action="" method="POST" enctype="multipart/form-data" onSubmit="return checkAddCouponForm();">
					<table width="100%" border="0" cellpadding="0" cellspacing="0" class="manage_table">
						<tr><td colspan="3">&nbsp;</td></tr>
						<tr><td colspan="3" align="right"><span class="required">*</span>&nbsp;<?php echo MSG_FIELD_MANDATORY; ?></td></tr>
						<tr><td width="41%" align="right" valign=top><?php echo TEXT_GIFT_CERT_NAME; ?>&nbsp;<span class="required">*</span></td><td width="3%" >&nbsp;</td>
							<td width="56%" align="left" valign=top><input class="textarea" type="text" name="txtGiftCouponName" value="" size="25" maxlength="150"></td>
						</tr>
						<tr><td width="41%" align="right" valign=top><?php echo TEXT_GIFT_CERT_CODE; ?>&nbsp;<span class="required">*</span></td><td width="3%" >&nbsp;</td>
							<td width="56%" align="left" valign=top><input class="textarea" type="text" name="txtGiftCouponCode" value="" size="25" maxlength="20"></td>
						</tr>
						<tr>
						  <td align="right" valign=top><?php echo TEXT_DESC; ?>&nbsp;</td>
						  <td >&nbsp;</td>
							<td valign=top align="left">
								<textarea name="txtCouponDescription" rows="8" cols="30" class="textarea"></textarea>
							</td>
						</tr>
						<tr><td align="right" valign=top><?php echo TEXT_GIFT_CERT_IMG; ?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
							<td valign=top align="left"><input type="file" class="textarea" name="txtCouponImage">
							&nbsp;[jpg,gif,png]&nbsp;[283 * 269]</td>
						</tr>
						<tr><td align="right" valign=top><?php echo TEXT_AMT; ?>&nbsp;<span class="required">*</span></td><td >&nbsp;</td>
							<td valign=top align="left"><input type="text" name="txtCouponAmount" class="textarea">&nbsp;<b><?php echo $adminCurrency['currency_code'];?> &nbsp;</b></td>
						</tr>
						<tr><td align="right" valign=top><?php echo MSG_STOCK_AVAIL; ?>&nbsp;<span class="required">*</span></td>
							<td>&nbsp;</td>
							<td valign="top" align="left">
								<select name="txtCouponStock" class="textarea2">
<?php

	for($i=1;$i<=50;$i++)
	{
?>										
								<option value="<?php echo $i?>"><?php echo $i?></option>								
								
<?php

	}
	
?>									
								</select>			
							</td>
						</tr>
						<tr><td align="right" valign=top><?php echo MSG_STOCK_RECORDER; ?>&nbsp;<span class="required">*</span></td>
							<td>&nbsp;</td>
							<td valign="top" align="left">
								<select name="txtStockReorder" class="textarea2">
<?php

	for($i=1;$i<=50;$i++)
	{
?>										
								<option value="<?php echo $i?>"><?php echo $i?></option>								
								
<?php

	}
	
?>									
								</select>
								<b><?php echo MSG_STOCK_LEVEL; ?></b>			
							</td>
						</tr>
						<tr>
						  <td colspan="3" align="center">&nbsp;</td>
					  </tr>
						<tr>
							<td colspan="3" align="center">
<<<<<<< HEAD
								<input type="button" name="back" value="<?php echo BTN_BACK;?>" class="button" onClick="Javascript:history.go(-1)">
								<input type="submit" value="<?php echo BTN_ADD;?>" name="add" class="button">
=======
								
								<input type="button" name="back" value="<?php echo BTN_BACK;?>" class="button" onClick="Javascript:history.go(-1)">
                                                                <input type="submit" value="<?php echo BTN_ADD;?>" name="add" class="button">
>>>>>>> cb8869b740ef346af41d2b966718cd5cd2b857c8
							</td>
						</tr>
						<tr><td colspan="3">&nbsp;</td></tr>
					</table>
				</form>
				
				</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";
	
	ob_end_flush();
?>
