<?php
$PageSecurity = 15;
$title = _('Color Master');
if ( isset($_GET['SelectedMeasureID']) )
	$SelectedMeasureID = $_GET['SelectedMeasureID'];
elseif (isset($_POST['SelectedMeasureID']))
	$SelectedMeasureID = $_POST['SelectedMeasureID'];
if (isset($_POST['submit']))
{
	$InputError = 0;
	if (strpos($_POST['MeasureName'],'&')>0 OR strpos($_POST['MeasureName'],"'")>0) {
		$InputError = 1;
		prnMsg( _('The Color Master cannot contain the character') . " '&' " . _('or the character') ." '",'error');
	}
	if (trim($_POST['MeasureName']) == '') {
		$InputError = 1;
		prnMsg( _('The Color Master may not be empty'), 'error');
	}

	if ($_POST['SelectedMeasureID']!='' AND $InputError !=1) {

		/*SelectedMeasureID could also exist if submit had not been clicked this code would not run in this case cos submit is false of course  see the delete code below*/
		// Check the name does not clash
		$sql = "SELECT count(*) FROM product_color
				WHERE id <> " . $SelectedMeasureID ."
				AND color ".LIKE." '" . $_POST['MeasureName'] . "'";
		$result = DB_query($sql,$db);
		$myrow = DB_fetch_row($result);
		if ( $myrow[0] > 0 ) {
			$InputError = 1;
			prnMsg( _('The Color Master can not be renamed because another with the same name already exist.'),'error');
		} else {
			// Get the old name and check that the record still exist neet to be very carefull here
			// idealy this is one of those sets that should be in a stored procedure simce even the checks are
			// relavant
			$sql = "SELECT id, color FROM product_color
				WHERE id = " . $SelectedMeasureID;
			$result = DB_query($sql,$db);
			if ( DB_num_rows($result) != 0 ) {
				// This is probably the safest way there is
				$myrow = DB_fetch_row($result);
				$OldMeasureName = $myrow[0];
				$sql = array();
				$sql[] = "UPDATE product_color
					SET color='" . $_POST['MeasureName'] . "'
					WHERE id ".LIKE." '".$OldMeasureName."'";
				$sql[] = "UPDATE stockmaster
					SET units='" . $_POST['MeasureName'] . "'
					WHERE units ".LIKE." '" . $OldMeasureName . "'";
				$sql[] = "UPDATE contracts
					SET units='" . $_POST['MeasureName'] . "'
					WHERE units ".LIKE." '" . $OldMeasureName . "'";
			} else {
				$InputError = 1;
				prnMsg( _('The Color Master no longer exist.'),'error');
			}
		}
		$msg = _('Color Master changed');
	} elseif ($InputError !=1) {
		/*SelectedMeasureID is null cos no item selected on first time round so must be adding a record*/
		$sql = "SELECT count(*) FROM product_color
				WHERE id " .LIKE. " '".$_POST['MeasureName'] ."'";
		$result = DB_query($sql,$db);
		$myrow = DB_fetch_row($result);
		if ( $myrow[0] > 0 ) {
			$InputError = 1;
			prnMsg( _('The Color Master can not be created because another with the same name already exists.'),'error');
		} else {
			$sql = "INSERT INTO product_color (
						color )
				VALUES (
					'" . $_POST['MeasureName'] ."'
					)";
		}
		$msg = _('New Color Master added');
	}

	if ($InputError!=1){
		//run the SQL from either of the above possibilites
		if (is_array($sql)) {
			$result = DB_Txn_Begin($db);
			$tmpErr = _('Could not update Color Master');
			$tmpDbg = _('The sql that failed was') . ':';
			foreach ($sql as $stmt ) {
				$result = DB_query($stmt,$db, $tmpErr,$tmpDbg,true);
				if(!$result) {
					$InputError = 1;
					break;
				}
			}
			if ($InputError!=1){
				$result = DB_Txn_Commit($db);
			} else {
				$result = DB_Txn_Rollback($db);
			}
		} else {
			$result = DB_query($sql,$db);
		}
		prnMsg($msg,'success');
	}
	unset ($SelectedMeasureID);
	unset ($_POST['SelectedMeasureID']);
	unset ($_POST['MeasureName']);
echo "<script>location.href='index.php?route=ColorMaster&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";} 
if(isset($_GET['delete'])) 
{
	$sql = "SELECT id, color FROM product_color	WHERE id = " . $SelectedMeasureID;
	$result = DB_query($sql,$db);
	if ( DB_num_rows($result) == 0 ) {
		// This is probably the safest way there is
		prnMsg( _('Cannot delete this Color Master because it no longer exist'),'warn');
	} else {
		$myrow = DB_fetch_row($result);
		$OldMeasureName = $myrow[0];
		$OldColorName = $myrow[1];
		$sql= "SELECT COUNT(*) FROM stockmaster WHERE units ".LIKE." '" . $OldMeasureName . "'";
		$result = DB_query($sql,$db);
		$myrow = DB_fetch_row($result);
		if ($myrow[0]>0) {
			prnMsg( _('Cannot delete this Color Master because inventory items have been created using this Color Master'),'warn');
			echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('inventory items that refer to this Color Master') . '</font>';
		} else {
			$sql= "SELECT COUNT(*) FROM contracts WHERE units ".LIKE." '" . $OldMeasureName . "'";
			$result = DB_query($sql,$db);
			$myrow = DB_fetch_row($result);
			if ($myrow[0]>0) {
				prnMsg( _('Cannot delete this Color Master because contracts have been created using this Color Master'),'warn');
				echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('contracts that refer to this Color Master') . '</font>';
			} else {
				$sql="DELETE FROM product_color WHERE id ".LIKE."'" . $OldMeasureName . "'";
				$result = DB_query($sql,$db);
				prnMsg( $OldColorName . ' ' . _('Color Master has been deleted') . '!','success');
			}
		}

	} //end if account group used in GL accounts
	unset ($SelectedMeasureID);
	unset ($_GET['SelectedMeasureID']);
	unset($_GET['delete']);
	unset ($_POST['SelectedMeasureID']);
	unset ($_POST['MeasureID']);
	unset ($_POST['MeasureName']);
	echo "<script>location.href='index.php?route=ColorMaster&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";}
if (! isset($_GET['delete'])) 
{
?>
	<form method="post" action="">
<?php 
	if (isset($SelectedMeasureID)) 
	{
		$sql = "SELECT id, color FROM product_color	WHERE id=" . $SelectedMeasureID;
		$result = DB_query($sql, $db);
		if ( DB_num_rows($result) == 0 ) {
			prnMsg( _('Could not retrieve the requested Color Master, please try again.'),'warn');
			unset($SelectedMeasureID);
		} else {
			$myrow = DB_fetch_array($result);

			$_POST['MeasureID'] = $myrow['id'];
			$_POST['MeasureName']  = $myrow['color'];
?>
			<input type="hidden" name="SelectedMeasureID" VALUE="<?php echo $_POST['MeasureID'];?>">
<?php 	}

	}  else {
		$_POST['MeasureName']='';
		
	}?>
	<table>
	<tr>
		<td><strong>Color</strong></td>
		<td><input type="Text" name="MeasureName" size="30" maxlength="30" value="<?php echo $_POST['MeasureName'];?>"></td>
	  </tr>
	  </table>
	  <br />
		<div class="centre"><input type="Submit" name="submit" value="Save"></div>
</form>
<?php }	
if (!isset($SelectedMeasureID)) 
{
	$sql = "SELECT id, color FROM product_color ORDER BY id ASC";
	$ErrMsg = _('Could not get Color Masters because');
	$result = DB_query($sql,$db,$ErrMsg);
?>
	<table width="50%" cellpadding="2" cellspacing="2">
		<tr >
		<th><strong>Color</strong></th>
		<th><strong>Edit</strong></th>
		<th><strong>Delete</strong></th>
		</tr>
<?php 
	$k=0; //row colour counter
	while ($myrow = DB_fetch_row($result)) 
	{?>
		<tr bgcolor="#FFFFFF">
		<td><?php echo $myrow[1];?></td>
		<td><a href="index.php?route=ColorMaster&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedMeasureID=<?php echo $myrow[0];?>">Edit</a></td>
		<td><a href="index.php?route=ColorMaster&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&SelectedMeasureID=<?php echo $myrow[0];?>&delete=1" onclick="return confirm('This color will be deleted. Are you sure?')">Delete</a></td>
		</tr>
<?php 
	} ?>
	</table>
	<?php } 
include('includes/footer.inc');
?>