<?php
function addColumns($chk_seq){
	
$columns = array();
$columns['Item_Code'] 				= 'Item Code';	
$columns['Item_Description'] 		= 'Item Description';
$columns['item_longdescription'] 	= 'Long Description';
$columns['Item_vat'] 				= 'VAT';
$columns['item_vattype'] 			= 'VAT Type';
$columns['Currency_code'] 			= 'Currency Code';
$columns['Group_code'] 				= 'Group Code';
$columns['SubGroup_Code'] 			= 'Sub-Group Code';
$columns['CostCent_Code'] 			= 'Costcenter Code';
$columns['Project_Code'] 			= 'Project Code';
$columns['Item_WarehouseCode'] 		= 'Warehouse Code';
$columns['Category_code'] 			= 'Category Code';
$columns['Item_Remark'] 			= 'Remark';
$columns['Item_SellingPrice1'] 		= 'Selling Price';
$columns['item_lastcostprice'] 		= 'Last Cost Price';
$columns['item_fixedcost'] 			= 'Fixed Cost Price';
$columns['Item_MinimumPrice'] 		= 'Minimum Price';
$columns['item_discountpercent'] 	= 'Discount';
$columns['Item_SubSequence'] 		= 'Sequence';
$columns['last_SubItemSeq'] 		= 'Sub-sequence';
$columns['Item_UnitSalesDesc'] 		= 'Selling Price Unit';
$columns['Item_UnitPurchaseDesc'] 	= 'Purchase Unit';

$Cols = explode(',', $_SESSION['Cols']);
$Cols = array_filter($Cols);
	foreach($Cols as $col){
		unset($columns["$col"]);
	}

echo "  <span class='style1'>Add Item Columns</span> 
		<select name='itemcol' id='itemcol' style='width:150px;border-radius: 4px;'>";
echo "		<option value=''></option>";		
foreach ($columns as $colK => $colV) {
	echo "	<option value='$colK'>$colV</option>";	
	
}

echo "	</select> <button type='button' id='addButton' onclick=\"addField();\">+</button>";
echo " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
echo "<input type='checkbox' name='chk_seq' id='chk_seq' value='$chk_seq' onclick=\"setSeq();\" ".($chk_seq=='1'?'checked':'')."> <span class='style1'>Item Code Sequence Used</span>";
}

function showColumns($tempCol, $removedCol){

$columns = array();
$columns['Item_Code'] 			= 'Item Code';	
$columns['Item_Description'] 		= 'Item Description';
$columns['item_longdescription'] 	= 'Long Description';
$columns['Item_vat'] 			= 'VAT';
$columns['item_vattype'] 		= 'VAT Type';
$columns['Currency_code'] 		= 'Currency Code';
$columns['Group_code'] 			= 'Group Code';
$columns['SubGroup_Code'] 		= 'Sub-Group Code';
$columns['CostCent_Code'] 		= 'Costcenter Code';
$columns['Project_Code'] 		= 'Project Code';
$columns['Item_WarehouseCode'] 		= 'Warehouse Code';
$columns['Category_code'] 		= 'Category Code';
$columns['Item_Remark'] 		= 'Remark';
$columns['Item_SellingPrice1'] 		= 'Selling Price';
$columns['item_lastcostprice'] 		= 'Last Cost Price';
$columns['item_fixedcost'] 		= 'Fixed Cost Price';
$columns['Item_MinimumPrice'] 		= 'Minimum Price';
$columns['item_discountpercent'] 	= 'Discount';
$columns['Item_SubSequence'] 		= 'Sequence';
$columns['last_SubItemSeq'] 		= 'Sub-sequence';
$columns['Item_UnitSalesDesc'] 		= 'Selling Price Unit';
$columns['Item_UnitPurchaseDesc'] 	= 'Purchase Unit';

if($tempCol != '')
$_SESSION['Cols'] = $_SESSION['Cols'].",".$tempCol;

$Cols = explode(',', $_SESSION['Cols']);
$Cols = array_filter($Cols);

//remove field from array and reset session

if($removedCol != ''){ 
	
	unset($Cols[$removedCol]);
	$_SESSION['Cols'] = '';
	foreach($Cols as $col){
		$_SESSION['Cols'] = $_SESSION['Cols'].",".$col;
	} 
	
}

echo "<table cellspacing=0 cellpading=0 width='1080px'>";
echo "<col width='180px'><col width='180px'><col width='180px'><col width='180px'><col width='180px'><col width='180px'>";
echo "<tr colspan='6'><hr></tr>";
$i = 0; 

	//var_dump($Cols);
	foreach($Cols as $cID => $col){
		if(!($i%6)) echo "</tr><tr height='30px'>";
		
		if($col != ''){
			if($i == 0) echo "<tr height='30px'>";
			$val = $columns["$col"];
			$colID = 'colID'.$cID;
			echo "<td align='center' onclick=\"removeCol('$cID');\"><span class='colStyle'>".$val."</span></td>";
			$i++;
		}
		
		
	}

	echo "</tr>";
	echo "<input type='hidden' name='colnumber' id='colnumber' value='$i'>";
echo "</table>";


}

function itemcostcentprice($costcenterprice_id,  $item_code,$connection){
	if($costcenterprice_id != ''){
		$query = "select costcenter_code, costcent_name, min_price from costcenterPrice
				inner join costcent on costcent.costcent_code = costcenter_code
				where costcenterPrice_id = $costcenterprice_id";
		$res = mysql_query($query,$connection);
		$rw = mysql_fetch_row($res);
		$costcent_codep = $rw[0];
		$costcent_namep = $rw[1];
		$min_price = $rw[2];
	}
	echo"<table border='0' width=700><col width=30px><col width=30px><col width=400px><col width=150px><col width=90px>";
echo "<tr><td></td><td></td><td><span class='style1'>Costcenter</span></td><td><span class='style1'>Price</span></td><td></td></tr>";
echo "<tr><td></td><td></td><td><input type='text' name='costcent_codep' value='$costcent_codep' readonly style='width:150px'>
		<label><span class='style1'><img src='img/btnopen.jpg' title=' Select CostCenter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" 
		onClick='showlistcostcentercode(9); '  /></span></label>
		<input type='text' name='costcent_namep' value='$costcent_namep' readonly style='width:200px'></td>
		<td><input type='text' name='min_price' value='$min_price'  style='width:140px'></td>
		<td><button type='button' id='btnsub' name='btnsub' onclick=\"submitCostcentPrice('$costcenterprice_id'); return false;\" title='Submit Price'><span><span>SubmitPrice</span></span></button></td></tr>";
echo "</table>";
}

function showitemcostcentprice($costcenterprice_id,  $item_code,$connection){
	$query = "select costcenterPrice_id, concat(costcenter_code, '-', costcent_name), min_price from costcenterPrice
			inner join costcent on costcent_code = costcenter_code where costcenterPrice.item_code='$item_code' order by costcent_code";
		
echo"<table border='0' width=700><col width=30px><col width=30px><col width=400px><col width=150px><col width=90px>";
			
echo"<tr bgcolor='#D0D0D0' height='25px'>
				<td align='left'><span class='style1'></span></td>
				<td><span class='style1'></span></td>
				<td><span class='style1'>Costcenter</span></td>			
				<td><span class='style1'>Minimum Price</span></td>
				<td><span class='style1'></span></td>
								
			</tr>";
 
	

	
	$res = mysql_query($query,$connection);		
	$ct=0;
	while($rw = mysql_fetch_array($res)){
		$ct+=1;

		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
					
		echo "<tr $Color>";
		echo "	<td align='center' valign='center'><span class='style15'><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"UpdateCostcentPrice('".$rw[0]."');\" title='Edit Record'  ></span></td>";
		echo "	<td align='center' valign='center'><span class='style15'><img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"DeleteCostcentPrice('".$rw[0]."');\" title='Delete Record'  ></span></td>";
		echo "<td>$rw[1]</td>";
		echo"<td>$rw[2]</td>";
		echo"</tr>";
	}
	echo"</table>";		
}

function item_option($itemCode, $optionId, $submit_button, $clear, $connection){
	
		
	if ($optionId != ''){
		$queryget = "select itemOption_code, item_description, itemOption_qty from item_option
					inner join items on items.item_code = itemOption_code
					where itemOption_id='".$optionId."'";//echo $queryget;
		$resget = mysql_query($queryget,$connection);
		$rowget = mysql_fetch_row($resget);
		$opt_itemcode = $rowget[0];
		$opt_itemdesc = $rowget[1];
		$opt_itemqty = $rowget[2];
	}
	echo"	<table border='0'>
			<col width=20>
			<col width=20>
			<col width=300>
			<col width=100>
			<col width=150>
			<tr bgcolor='#D0D0D0' height='25px'>
				<td align='center'><span class='style1'>&nbsp;</span></td>
				<td align='center'><span class='style1'>&nbsp;</span></td>
				<td align='center'><span class='style1'>Code/Desc<span class='style26'>*</span></span></td>
				<td align='center'><span class='style1'>Quantity<span class='style26'>*</span></span></span></td>
				<td align='center'><span class='style1'>&nbsp;</span></td>
			</tr>";

	echo"	<tr>
				<td align='center'><span class='style1'>&nbsp;</span></td>
				<td align='center'><span class='style1'>&nbsp;</span></td>
				<td><input type='text' name='opt_itemcode' id='opt_itemcode' value='$opt_itemcode'  style='width:120px;' readonly>
					<input type='text' name='opt_itemdesc' id='opt_itemdesc' value='$opt_itemdesc'  style='width:150px;' readonly>
					<img src='img/btnopen.jpg' height='15' width='20' title='Select Item' onclick=\"showlistitem('4');\">
				</td>
				<td><input type='text' name='opt_itemqty' id='opt_itemqty' value='$opt_itemqty'  style='width:90px;text-align:right;' maxlength='10'></td>
				<td align=center>
					<button type='button' id='submitrecord' name='submitrecord' onclick=\"SaveOpt('$optionId'); return false;\" title='Submit Record'><span><span>$submit_button</span></span></button>
					<button type='button' id='clearbutton' name='clearbutton' onclick=\"clearOpt(); return false;\" title='Clear '><span><span>$clear </span></span></button>
				</td>
			</tr>";

	$queryrebate = " select itemOption_id, itemOption_code, item_description, itemOption_qty from item_option
					inner join items on items.item_code = itemOption_code
					where item_option.item_code='".$itemCode."' ";
	//echo $queryrebate; exit;
	$resultrebate = mysql_query($queryrebate, $connection) or die(mysql_error());
	while($rowsrebate=mysql_fetch_array($resultrebate)){
		
		$ct+=1;

		if($ct%2==0)
			$Color="style='background-color:#F7F7F7'";
		else			
			$Color="style='background-color:#E6E6E6'";	
		$class = "style15";
		
		if ($rowsrebate[0] == $optionId) {
			$Color="style='background-color:#1e87a5'";	
			$class = "style28";
		}
		echo "<tr ".$Color." >";
		echo "	<td align='center' valign='center'><span class='$class'><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"UpdateOpt('".$rowsrebate[0]."');\" title='Edit Record'  ></span></td>";
		echo "	<td align='center' valign='center'><span class='$class'><img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=\"DeleteOpt('".$rowsrebate[0]."');\" title='Delete Record'  ></span></td>";
		echo "	<td align='left'><span class='$class'>".$rowsrebate[1]." - ".$rowsrebate[2]."</span></td>";
		echo "	<td align='right'><span class='$class'>".$rowsrebate[3]."</span></td>";
		echo"	<td align='center' colspan='2'><span class='style1'>&nbsp;</span></td>";
		echo "</tr>";
							
	}

	echo"</table>";
}

function showPackdt ($itemcode,$connection){
	echo "<table border=0 cellpadding=0 cellspacing=0 width=1040px>
			<col widh=30px><col width=450px><col width=150px><col width=100px><col width=140px><col width=140px><col width=30px>";
	
	echo "<tr height='30px' bgcolor='darkgray'><td></td>
			<td align='center'><span class='style1'>Pack Item</span></td>
			<td  align='center'><span class='style1'>Warehouse</span></td>
			<td  align='center'><span class='style1'>Qty.</span></td>
			<td  align='center'><span class='style1'>From date</span></td>
			<td  align='center'><span class='style1'>To Date</span></td>
			<td></td>
		</tr>";	
	
	$i=0;
	echo "<tr height='20px'><td></td>
			<td align='center'><input type='text' name='packcode$i' id='packcode$i' style='width:200px;border-radius:5px;'>
					<input type='text' name='packdesc$i' id='packdesc$i' style='width:200px;border-radius:5px;'>
					<img src='img/btnopen.jpg' width=15 height=15 onmouseover=\"style.cursor='hand'\" onclick=\"showlistitem('$i');\"></td>
			<td  align='center'><input type='text' name='warehousecode$i' id='warehousecode$i' style='width:120px;border-radius:5px;'>
					<img src='img/btnopen.jpg' width=15 height=15 onmouseover=\"style.cursor='hand'\" onclick=\"showlistwarehouse('$i');\"></td>
			<td  align='center'><input type='number' name='itemqty$i' id='itemqty$i' style='width:90px;border-radius:5px;'></td>
			<td  align='center'><input type='date' name='fromdate$i' id='fromdate$i' style='width:130px;border-radius:5px;'></td>
			<td  align='center'><input type='date' name='todate$i' id='todate$i' style='width:130px;border-radius:5px;'></td>
			<td align='center'><img src='img/confirm-1.png' onmouseover=\"style.cursor='hand'\" width=20px height=20px onclick=\"submitrecord('$i','','');\"></td>
		</tr>";	
	$query = "select Item_CodePackDt, item_description, packdt.Warehouse_Code, PackDt_Quantity, date_format(PackDt_FromDate,'%Y-%m-%d'), date_format(PackDt_ToDate,'%Y-%m-%d')
			from packdt
			inner join items on items.item_code = packdt.Item_CodePackDt
			inner join warehouse on warehouse.warehouse_code = packdt.Warehouse_Code
			where packdt.Item_Code = '$itemcode' ";	
			
	$result = mysql_query($query, $connection);
	while ($rows = mysql_fetch_array($result)){
		$i++;
		echo "<tr height='20px'><td align='center'><img src='img/delete.png' onmouseover=\"style.cursor='hand'\" width=20px height=20px onclick=\"deleterecord('$i','$rows[0]','$rows[2]');\"></td>
			<td align='center'><input type='text' name='packcode$i' id='packcode$i' value='".htmlspecialchars($rows[0], ENT_QUOTES)."' style='width:200px;border-radius:5px;'>
					<input type='text' name='packdesc$i' id='packdesc$i' value='".htmlspecialchars($rows[1], ENT_QUOTES)."' style='width:200px;border-radius:5px;'>
					<img src='img/btnopen.jpg' width=15 height=15 onmouseover=\"style.cursor='hand'\" onclick=\"showlistitem('$i');\"></td>
			<td  align='center'><input type='text' name='warehousecode$i' id='warehousecode$i' value='".htmlspecialchars($rows[2], ENT_QUOTES)."' style='width:120px;border-radius:5px;'>
					<img src='img/btnopen.jpg' width=15 height=15 onmouseover=\"style.cursor='hand'\" onclick=\"showlistwarehouse('$i');\"></td>
			<td  align='center'><input type='number' name='itemqty$i' id='itemqty$i' value='".htmlspecialchars($rows[3], ENT_QUOTES)."' style='width:90px;border-radius:5px;'></td>
			<td  align='center'><input type='date' name='fromdate$i' id='fromdate$i' value='".htmlspecialchars($rows[4], ENT_QUOTES)."' style='width:130px;border-radius:5px;'></td>
			<td  align='center'><input type='date' name='todate$i' id='todate$i' value='".htmlspecialchars($rows[5], ENT_QUOTES)."' style='width:130px;border-radius:5px;'></td>
			<td align='center'><img src='img/confirm-1.png' onmouseover=\"style.cursor='hand'\" width=20px height=20px onclick=\"submitrecord('$i','$rows[0]','$rows[2]');\"></td>
		</tr>";
		
	}	
	echo "</table>";
	
	
	
}

function showPriceByQty ($itemcode,$priceByQty,$currency, $base1,$base2,$connection){
	echo "<table border=0 cellpadding=0 cellspacing=0 width=660px>
			<col widh=30px><col width=120px><col width=120px><col width=120px><col width=120px><col width=120px><col width=30px>";
	
	echo "<tr height='30px' ><td colspan='7'>
			<span class='style1'><input type='checkbox' name='priceByQty' id='priceByQty' value='$priceByQty' ".($priceByQty==1?'checked':'')." onclick=\"setChkd(this.id)\">
			Item Price By Qty</span></td></tr>";
	
	echo "<tr height='30px' bgcolor='darkgray'><td></td>
			<td align='center'><span class='style1'>From Qty</span></td>
			<td  align='center'><span class='style1'>To Qty</span></td>
			<td  align='center'><span class='style1'>Price $currency</span></td>
			<td  align='center'><span class='style1'>Price $base1</span></td>
			<td  align='center'><span class='style1'>Price $base2</span></td>
			<td></td>
		</tr>";	
	
	$i=0;
	echo "<tr height='25px'><td></td>
			<td align='center'><input type='number' name='fromqty$i' id='fromqty$i' style='width:120px;border-radius:5px;'> </td>
			<td  align='center'><input type='number' name='toqty$i' id='toqty$i' style='width:120px;border-radius:5px;'> </td>
			<td  align='center'><input type='number' name='qtyprice$i' id='qtyprice$i' onkeyup=\"SetpricesQty('$i');\" style='width:120px;border-radius:5px;'></td>
			<td  align='center'><input type='number' name='qtyprice1b$i' id='qtyprice1b$i' readonly style='width:120px;border-radius:5px;'></td>
			<td  align='center'><input type='number' name='qtyprice2b$i' id='qtyprice2b$i' readonly style='width:120px;border-radius:5px;'></td>
			<td align='center'><img src='img/confirm-1.png' onmouseover=\"style.cursor='hand'\" width=20px height=20px onclick=\"submitpricebyqty('$i','','$itemcode');\"></td>
		</tr>";	
	$query = "select priceByQty_id, fromQty, toQty, qtyPrice, qtyPriceb1, qtyPriceb2  
			from item_priceByQty
			where item_code = '$itemcode' 
			order by fromQty, toQty ";	
			
	$result = mysql_query($query, $connection);
	while ($rows = mysql_fetch_array($result)){
		$i++;
		echo "<tr height='25px'><td align='center'><img src='img/delete.png' onmouseover=\"style.cursor='hand'\" width=20px height=20px onclick=\"deletepricebyqty('$i','$rows[0]','$itemcode');\"></td>
			<td align='center'><input type='number' name='fromqty$i' id='fromqty$i' value='".htmlspecialchars($rows[1], ENT_QUOTES)."' style='width:120px;border-radius:5px;'></td>
			<td  align='center'><input type='number' name='toqty$i' id='toqty$i' value='".htmlspecialchars($rows[2], ENT_QUOTES)."' style='width:120px;border-radius:5px;'> </td>
			<td  align='center'><input type='number' name='qtyprice$i' id='qtyprice$i' onkeyup=\"SetpricesQty('$i');\" value='".htmlspecialchars($rows[3], ENT_QUOTES)."' style='width:120px;border-radius:5px;'></td>
			<td  align='center'><input type='number' name='qtyprice1b$i' id='qtyprice1b$i' value='".htmlspecialchars($rows[4], ENT_QUOTES)."' readonly style='width:120px;border-radius:5px;'></td>
			<td  align='center'><input type='number' name='qtyprice2b$i' id='qtyprice2b$i' value='".htmlspecialchars($rows[5], ENT_QUOTES)."' readonly style='width:120px;border-radius:5px;'></td>
			<td align='center'><img src='img/confirm-1.png' onmouseover=\"style.cursor='hand'\" width=20px height=20px onclick=\"submitpricebyqty('$i','$rows[0]','$itemcode');\"></td>
		</tr>";
		
	}	
	echo "</table>";
	
	
	
}
?>


