 
 <?php

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Sales.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$actionmode 	= $_POST["actionmode"]; 				if ($actionmode == ""){				$actionmode 	= $_GET["actionmode"];}   
$message 		= $_POST["message"]; 					if ($message == ""){				$message 		= $_GET["message"];}   
$frm	   		= $_POST['frm']; 						if ($frm == ""){					$frm 			= $_GET["frm"];}
$item 	   		= $_POST['item'];						if ($item == ""){					$item 			= $_GET["item"];}
$startrange 	= $_POST['startrange'];					if ($startrange == ""){				$startrange 	= $_GET["startrange"];}
$PTabIndex 		= $_POST['PTabIndex'];					if ($PTabIndex == ""){				$PTabIndex 		= $_GET["PTabIndex"];}


$perpage   		= $_POST["perpage"]; 					if ($perpage == ""){				$perpage 		= $_GET["perpage"];}
$p_name    		= $_POST["p_name"]; 					if ($p_name == ""){					$p_name 		= $_GET["p_name"];}
$p_cashregister = $_POST["p_cashregister"]; 			if ($p_cashregister == ""){			$p_cashregister = $_GET["p_cashregister"];}
$p_subgroup  	= $_POST["p_subgroup"]; 				if ($p_subgroup == ""){				$p_subgroup 	= $_GET["p_subgroup"];}
$p_category  	= $_POST["p_category"]; 				if ($p_category == ""){				$p_category 	= $_GET["p_category"];}

$Reason_Id  	= $_POST["Reason_Id"]; 					if ($Reason_Id == ""){				$Reason_Id 		= $_GET["Reason_Id"];}
$Reason_Remark 	= $_POST["Reason_Remark"]; 				if ($Reason_Remark == ""){			$Reason_Remark 	= $_GET["Reason_Remark"];}

$Payment_Id  		 = $_POST["Payment_Id"]; 			if ($Payment_Id == ""){				$Payment_Id 	 	 = $_GET["Payment_Id"];}
$Payment_Account 	 = $_POST["Payment_Account"]; 		if ($Payment_Account == ""){		$Payment_Account 	 = $_GET["Payment_Account"];}
$Payment_Description = $_POST["Payment_Description"]; 	if ($Payment_Description == ""){	$Payment_Description = $_GET["Payment_Description"];}


$User_Account  		= $_POST["User_Account"]; 		if ($User_Account == ""){		$User_Account 		= $_GET["User_Account"];}
$User_Name 	   		= $_POST["User_Name"];		 	if ($User_Name == ""){			$User_Name 			= $_GET["User_Name"];}
$User_Password 		= $_POST["User_Password"]; 		if ($User_Password == ""){		$User_Password 		= $_GET["User_Password"];}

$CashRegister_Name 	= $_GET["CashRegister_Name"]; 	if ($CashRegister_Name == ""){	$CashRegister_Name 	= $_POST["CashRegister_Name"];} 
$txtRetailPointId 	= $_GET["txtRetailPointId"]; 	if ($txtRetailPointId == ""){	$txtRetailPointId 	= $_POST["txtRetailPointId"];} 
//Read From Setup Table							
$querysetup= " Select DecimalBase, DecimalBase1, DecimalBase2, Base1, Base2, grid_color1, grid_color2, ifNull(ShowSlipNumber,0) As ShowSlipNumber, ".
             " Setup_VatNo, Date_Format(Setup_VatStartDate,'%d/%m/%Y') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage, Company_Name As CompanyName, ".
			 " CashReg_DispLedMsg, ifNull(CashReg_Weight,0) As CashReg_Weight, CashReg_StartBarcode, CashReg_EndBarcode, CashReg_StartQty, CashReg_EndQty, ".
			 " ifNull(CashReg_Price,0) As CashReg_Price, CashReg_StartPrice, CashReg_EndPrice, ifNull(CashReg_RoundDivisor,1) As CashReg_RoundDivisor, ".
			 " ifNull(CashReg_ColorTheme,'default') as CashReg_ColorTheme, CashReg_BgColorOffer, CashReg_BgColorQty, CashReg_BgColorOrderQty, Company_memo as CompanyMemo, ".
			 " CashReg_BgColorZeroPrice, CashReg_BgColorZeroQty, CashReg_BgColorHasDiscount, ifNull(CashReg_ApplyColorOffer,0) As CashReg_ApplyColorOffer, ".
			 " ifNull(CashReg_ApplyColorQty,0) As CashReg_ApplyColorQty, ifNull(CashReg_ApplyColorOrderQty,0) As CashReg_ApplyColorOrderQty, ifNull(CashReg_ApplyColorZeroPrice,0) As CashReg_ApplyColorZeroPrice,  ".
			 " ifNull(CashReg_ApplyColorZeroQty,0) As CashReg_ApplyColorZeroQty, ifNull(CashReg_ApplyColorHasDiscount,0) As CashReg_ApplyColorHasDiscount,  ".
			 " Retaildt_height,shortcutgrid_height, subgroupgrid_height,shortcutgrid_width,subgroupgrid_width, ifnull(RestrictZeroPrice,0) as RestrictZeroPrice, project_no, costcent_no, REMARKINV,".
			 " ifnull(TabletScreenChecked,0) as chkTabletScreen, ifnull(POS_RedeemPoints,0) as RedeemPoints ".
			 " From Setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

//Read From Module Table
$querymodule=" Select ifNull(Module_PostRetailOneToOne,0) As Module_PostRetailOneToOne, ifNull(Module_PostingByCashRegister,0) As Module_PostingByCashRegister, ".
             " ifNull(Module_CashRegItemQtyInCode,0) As Module_CashRegItemQtyInCode, ifNull(Module_CashRegItemSeperate,0) As Module_CashRegItemSeperate, ".
			 " ifNull(Module_CashRegisterBase2,0) As Module_CashRegisterBase2, ifNull(Module_PriceOffer,0) As Module_PriceOffer, ".
			 " ifNull(Module_sizes,0) As Module_sizes, ifNull(Module_Color,0) As Module_Color ".
			 " From Module";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

if (empty($CashRegister_Name) && !isset($CashRegister_Name)) {
	$queryCashRegisterDft=" Select CashRegister_Name From CashRegister Order By CashRegister_Name Limit 1 ";
	$resultCashRegisterDft = selectData($connection, $queryCashRegisterDft);
	
	$CashRegister_Name = $resultCashRegisterDft[0];
}

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<script type="text/javascript" src="js/grid/jquery-1.9.1.js"></script>
<link type="text/css" href="js/calendar/jquery.datetimepicker.css" rel="stylesheet">
<script type="text/javascript" src="js/calendar/jquery.datetimepicker.js"></script>
<script type="text/javascript" src="js/jscolor/jscolor.js"></script>
<script type="text/javascript" src="tabsClass/tabber.js"></script>
<link rel="stylesheet" href="tabsClass/tabber.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>
HERE;
 ?>
<script type="text/javascript" language="javascript">
	$(document).on('click', '#txtDateFrom', function() {
			$('#txtDateFrom').datetimepicker({
				timepicker:false,
				formatDate:'Y/m/d',
				scrollMonth:false
			});
		});
</script>
		
<script type="text/javascript" language="javascript">

function showlistitem(str){
	window.open('select_item.php?item='+str,'select_item','width=900,height=550,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistcategory(){
	window.open('List_Categories.php?str=15','categorywindow','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistsubgroups(str){
	window.open('List_SubGroup.php?str='+str,'List_SubGroup','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}
function editItems(){
	window.open('Form_RetailEditPromo.php','List_SubGroup','width=1050,height=750,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function showlistgroups(str){
	window.open('List_Grouping.php?str='+str,'List_SubGroup','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}

function loadItems(){
	let x = document.form1;
	if(x.fromdate.value == '' || x.todate.value == ''){
		alert('From/To Date are Empty!');
	} 
	else {
		x.promotionId.value = '';
		x.actionmode.value = 'loaditems';
		x.submit();
	}
	
}

function SubmitPromo() {
	let x = document.form1;
	let result = 1;
		
	if (x.actionmode.value != 'loaditems' && x.promotionId.value == '') {
		result = 0;
		alert('No item was loaded');
	}
	else if(x.fromdate.value == '' || x.todate.value == ''){
		result = 0;
		alert('From/To Date are Empty!');
	}
	
	if (result == 1) {
		x.actionmode.value='SubmitPromo';
		x.PTabIndex.value=7;
		x.submit();
	}
}

function SubmitPoints() {
	let x = document.form1;
	x.actionmode.value='SubmitPoints';
	//x.PTabIndex.value=7;
	x.submit();
	
}

function ClearItems(){
	let x = document.form1;
	x.fromdate.value = '';
	x.todate.value = '';
	x.fromtime.value = '';
	x.totime.value = '';
	x.promotionId.value = '';
	x.actionmode.value = '';
	x.group_code.value = '';
	x.subgroup_code.value = '';
	x.category_code.value = '';
	x.item_code.value = '';
	x.promoDisc.value = '';
	x.submit();
}

</script>

<?php

if($actionmode == 'SubmitPromo'){
	$ndate = explode('/', $fromdate);
	$fromdate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$fromdate .= " ".$fromtime;
	$ndate = explode('/', $todate);
	$todate = $ndate[2]."-".$ndate[1]."-".$ndate[0];
	$todate .= " ".$totime;
	
	if($promotionId == ''){
		$query_getPromo = "select item_code,item_sellingprice1base1
					from items 
					where if( '$group_code'='', 1=1, Group_code = '$group_code')
					and  if( '$subgroup_code'='', 1=1, subgroup_code = '$subgroup_code') and  if( '$item_code'='', 1=1, item_code = '$item_code') and  if( '$category_code'='', 1=1, category_code = '$category_code')
					order by items.item_code	"; 
		$res_getPromo = mysql_query($query_getPromo, $connection);
		while($rw_getPromo = mysql_fetch_row($res_getPromo)){
			$itemDisc = 'itemDisc'.$rw_getPromo[0];
			
			if($$itemDisc == '') $$itemDisc = 0;
			if($$itemDisc != 0){
				$queryins = "insert into item_promotion (promotion_fromdate, promotion_todate, promotion_itemCode, promotion_discount) values
							('".$fromdate."','".$todate."', '".$rw_getPromo[0]."', ".$$itemDisc." );";
				$resins = mysql_query($queryins,$connection);
			}
		}
	}
	else if($promotionId != ''){
		$query_getPromo = "select promotion_itemCode
					from item_promotion 
					where promotion_id = $promotionId	"; 
		$res_getPromo = mysql_query($query_getPromo, $connection);
		$rw_getPromo = mysql_fetch_row($res_getPromo);
		$itemDisc = 'itemDisc'.$rw_getPromo[0];;
		if($$itemDisc == '') $$itemDisc = 0;
		if($$itemDisc != 0){
			$updatepromo = "update item_promotion set promotion_fromdate = '".$fromdate."', promotion_todate = '".$todate."', promotion_discount = ".$$itemDisc." where promotion_id = $promotionId ";
			 $respromo = mysql_query($updatepromo,$connection);
		}
		
	}
	
}
echo" <body>";
    echo" <form id='form1' name='form1'> ";
    
echo"	<div style='text-align: center;' >
		<h2 title='Users'>Item Promotion</h2>";
echo"  		<table border='0' cellpadding='0' cellspacing='0' align='center' valign='center' style='width:620px;' >
			<col width=100>
			<col width=100><col width=150>";
echo "<tr>";
echo "<td align='left'>";
echo "<button type='button' id='btnSubmit7' name='btnSubmit7' onclick=\"SubmitPromo(); return false;\"  title='Save'><span><span>Submit</span></span></button>";
echo "<button type='button' id='btnLoad7' name='btnLoad7' onclick=\"editItems(); return false;\"  title='Edit Promoted Items'><span><span>Edit Promoted Items</span></span></button>";
echo "<button type='button' id='btnLoad7' name='btnLoad7' onclick=\"loadItems(); return false;\"  title='Load Items'><span><span>Load Items</span></span></button>";
echo"<button type='button' id='btnClear7' name='btnClear7' onclick=\"ClearItems(); return false;\"  title='Clear'><span><span>Clear</span></span></button>
	</td></tr><tr><td>&nbsp;</td></tr>";
if($promotionId != '') {	
	$query_getPromo = "select date_format(promotion_fromdate,'%d/%m/%Y') ,  date_format(promotion_todate,'%d/%m/%Y'), 
						date_format(promotion_fromdate,'%H:%i') ,  date_format(promotion_todate,'%H:%i') 
						from item_promotion  
						where promotion_id = '$promotionId' ";//echo $query_getPromo;
	$res_getPromo = mysql_query($query_getPromo, $connection);
	$rw_getPromo = mysql_fetch_row($res_getPromo);	
	$fromdate = $rw_getPromo[0];
	$todate = $rw_getPromo[1];
	$fromtime = $rw_getPromo[2];
	$totime = $rw_getPromo[3];
}
	if($fromtime == '') $fromtime = "00:00";
	if($totime == '')  $totime = "23:59";
echo "<tr><td><table border='0' cellpadding='0' cellspacing='0' width=620px><col width=100px><col width=240px><col width=100px><col width=180px>
		<tr height=25px>
			<td><span class='style4'>From Date</span></td><td><input type='text' name='fromdate' id='fromdate' value='$fromdate' style='width:80px;'>
				<a href='#' onclick=\"displayCalendar(document.form1.fromdate,'dd/mm/yyyy',this); return false;\" name='anchor1' id='anchor1'><img src='./img/cal.gif' border=0 ></a>
				<input type='time' name='fromtime' id='fromtime' value='$fromtime' style='height: 20px; font-size: 12px; width: 90px;'>
			</td>
			<td><span class='style4'>Group </span></td><td><input type='text' name='group_code' id='group_code' value='$group_code' style='width:150px;'>
				<img src='img/btnopen.jpg' width='15' height=20' onclick=\"showlistgroups('16');\" onmouseover=\"style.cursor='pointer'\" title='Select Group'>
			</td>
		</tr>
		<tr height=25px>
			<td><span class='style4'>To Date</span></td><td><input type='text' name='todate' id='todate' value='$todate' style='width:80px;'>
				<a href='#' onclick=\"displayCalendar(document.form1.todate,'dd/mm/yyyy',this); return false;\" name='anchor1' id='anchor1'><img src='./img/cal.gif' border=0 ></a>
				<input type='time' name='totime' id='totime' value='$totime' style='height: 20px; font-size: 12px; width: 90px;'>
			</td>
			<td><span class='style4'>Sub Group</span></td><td><input type='text' name='subgroup_code' id='subgroup_code' value='$subgroup_code' style='width:150px;'>
				<img src='img/btnopen.jpg' width='15' height=20' onclick=\"showlistsubgroups('11');\" onmouseover=\"style.cursor='pointer'\" title='Select SubGroup'>
			</td>
		</tr>
		<tr height=25px>
			<td><span class='style4'>Item</span></td><td><input type='text' name='item_code' id='item_code' value='$item_code' style='width:130px;'>
				<img src='img/btnopen.jpg' width='15' height=20' onclick=\"showlistitem('21');\" onmouseover=\"style.cursor='pointer'\" title='Select Item'>
			</td>
			<td><span class='style4'>Category</span></td><td><input type='text' name='category_code' id='category_code' value='$category_code' style='width:130px;'>
			<img src='img/btnopen.jpg' width='15' height=20' onclick=\"showlistcategory();\" onmouseover=\"style.cursor='pointer'\" title='Select Category'>
		</td>
		</tr>
		<tr height=25px>
			<td><span class='style4'>Discount %</span></td><td><input type='text' name='promoDisc' id='promoDisc' value='$promoDisc' style='width:150px;' ></td>
		</tr>
	</table></td></tr>";
	
echo "<tr><td><table border='0' cellpadding='0' cellspacing='0' width=600px><col width=100px><col width=280px><col width=120px><col width=100px><col width=100px>
	<tr><td bgcolor='#D8D8D8' align='center'><span class='style4'>Item Code</span></td>
		<td bgcolor='#D8D8D8' align='center'><span class='style4'>Description</span></td>
		<td bgcolor='#D8D8D8' align='center'><span class='style4'>Cost</span></td>
		<td bgcolor='#D8D8D8' align='center'><span class='style4'>Selling Price</span></td>
		<td bgcolor='#D8D8D8' align='center'><span class='style4'>existing</span></td>
		<td bgcolor='#D8D8D8' align='center'><span class='style4'>Discount %</span></td>
	</tr>";

echo"</table></td></tr>";
	
echo"<tr><td>
		<div style='overflow-y:scroll;height:400px;width:620px;border:1px solid #D8D8D8;padding-left:5px;'>
		<table border='0' cellpadding='0' cellspacing='0' width=600px><col width=100px><col width=280px><col width=120px><col width=100px><col width=100px><col width=100px>";
if($actionmode == 'loaditems')
	$query_getPromo = "select item_code, item_description, '' , QtyExists(item_code,'',now(),0,'','',''),item_sellingprice1base1 , meancostcur(item_code ,now(),1,'NGN','NGN')
					from items 
					where if( '$group_code'='', 1=1, Group_code = '$group_code')
					and  if( '$subgroup_code'='', 1=1, subgroup_code = '$subgroup_code') and  if( '$item_code'='', 1=1, item_code = '$item_code') and  if( '$category_code'='', 1=1, category_code = '$category_code') 
					order by items.item_code	";
else
	$query_getPromo = "select promotion_itemCode, item_description, promotion_discount , QtyExists(item_code,'',now(),0,'','',''),items.item_sellingprice1base1, meancostcur(item_code ,now(),1,'NGN','NGN')
					from item_promotion 
					inner join items on items.item_code = promotion_itemCode 
					where promotion_id = '$promotionId' ";
					//echo $query_getPromo;
$res_getPromo = mysql_query($query_getPromo, $connection);

while($rw_getPromo = mysql_fetch_row($res_getPromo)){
	$itemDisc = 'itemDisc'.$rw_getPromo[0];
	if($promotionId != '') $$itemDisc = $rw_getPromo[2];
	else $$itemDisc = $promoDisc;
	
	echo "<tr height=25px>
			<td align='left'><span class='style15'>$rw_getPromo[0]</span></td>
			<td align='center'><span class='style15'>$rw_getPromo[1]</span></td>
			<td align='center'><span class='style15'>$rw_getPromo[5]</span></td>
			<td align='center'><span class='style15'>$rw_getPromo[4]</span></td>
			<td align='center'><span class='style15'>$rw_getPromo[3]</span></td>
			<td align='left'><span class='style15'><input type='text' name='$itemDisc' id='$itemDisc' value='".$$itemDisc."' style='width:80px;'></span></td>
		<tr>";
}
echo "<input type='hidden' name='promotionId' id='promotionId' value='$promotionId'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='PTabIndex' id='PTabIndex' value='$PTabIndex'>

<input type='hidden' name='p_id' id='p_id'  value='$p_id' >
<input type='hidden' name='item' id='item' value='$item' />
<input type='hidden' name='frm' id='frm' value='$frm' />
<input type='hidden' name='startrange' value='$startrange'>
";

echo "</table></td></tr>";			
echo "</table></div>";


if($RedeemPoints == 1){
	if( $actionmode == 'SubmitPoints'){
		$querychk = "select 1 from retailpoint";
		$reschk = mysql_query($querychk,$connection);
		$numchk = mysql_num_rows($reschk);
		if($numchk > 0){
			$up = "update retailpoint set retail_salespoint = '$salept', retail_redeemPoint = '$redeempt', retail_MinRedeemPoint = '$minredeempt' ";
			$res = mysql_query($up,$connection);
		} else {
			$ins = "insert into retailpoint (retail_salespoint, retail_redeemPoint, retail_MinRedeemPoint ) values ('$salept', '$redeempt', '$minredeempt');";
			$res = mysql_query($ins,$connection);
		}
	}
	$query = "select retail_salespoint, retail_redeemPoint, retail_MinRedeemPoint from retailpoint limit 1;";
	$res = mysql_query($query,$connection);
	$row = mysql_fetch_row($res);
	$salept = $row[0];
	$redeempt = $row[1];
	$minredeempt = $row[2];
	
	echo"	<div class='tabbertab' tabberIndex='7'>
		<h2 title='Users'>Points Setup</h2>";
	echo"  		<table border='0' cellpadding='0' cellspacing='0' align='center' valign='center' style='width:620px;' >
				<col width=120><col width=190>
				<col width=120><col width=190>";
	echo "<tr>";
	echo "<td align='left'>";
	echo "<button type='button' id='btnSubmit7' name='btnSubmit7' onclick=\"SubmitPoints(); return false;\"  title='Save'><span><span>Submit</span></span></button>";
	echo "</td></tr><tr><td>&nbsp;</td></tr>";
	
	echo "<tr><td><b>Sale Points</b></td><td><input type='text' name='salept' id='salept' value = '$salept' style='width:150px;'></td>
			<td><b>Redeem Points</b></td><td><input type='text' name='redeempt' id='redeempt' value = '$redeempt' style='width:150px;'></td>
		</tr>";
	echo "<tr><td><b>Min. Redeem Points</b></td><td><input type='text' name='minredeempt' id='minredeempt' value = '$minredeempt' style='width:150px;'></td>
			<td></td><td></td>
		</tr>";
	echo "</table></div>";
echo"</div>";
	
}

echo"</div>";



echo "</td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";	
if($actionmode == 'SubmitPromo')
	echo "<script>ClearItems();</script>";		
if($actionmode == 'SubmitPoints')
	echo "<script>ClearPoints();</script>";	
echo"<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:638px;' valign='top'></td>
		<td id='search-bottom3' style='height:34px; width:31px;' valign='top'></td>";
echo"</tr>";	

 

function fbegin()
{
	mysql_query("START TRANSACTION");
}
function fcommit(){
	mysql_query("COMMIT");
}
function frollback()
{
	mysql_query("ROLLBACK");
}
print <<<HERE
</form>
</body>
HERE;

?>