<?php
/* $Id: InputSerialItems.php 4830 2012-01-22 02:57:49Z vvs2012 $*/
/*Input Serial Items - used for inputing serial numbers or batch/roll/bundle references
for controlled items - used in:
- ConfirmDispatchControlledInvoice.php
- GoodsReceivedControlled.php
- StockAdjustments.php
- StockTransfers.php
- CreditItemsControlled.php
*/

include ('includes/Add_SerialItems.php');

/*Setup the Data Entry Types */
if (isset($_GET['LineNo'])){
	$LineNo = $_GET['LineNo'];
} elseif (isset($_POST['LineNo'])){
	$LineNo = $_POST['LineNo'];
} else {
	$LineNo=0;
}
/*
        Entry Types:
             Keyed Mode: 'Qty' Rows of Input Fields. Upto X shown per page (100 max)
             Barcode Mode: Part Keyed, part not. 1st, 'Qty' of barcodes entered. Then extra data as/if
             necessary
             FileUpload Mode: File Uploaded must fulfill item requirements when parsed... no form based data
                 entry. 1-upload, 2-parse&validate, 3-bad>1 good>4, 4-import.
        switch the type we are updating from, w/ some rules...
                Qty < X   - Default to keyed
                X < Qty < Y - Default to barcode
                Y < Qty - Default to upload

        possibly override setting elsewhere.
*/
if (!isset($RecvQty)) {
	$RecvQty=0;
}
if (!isset($_POST['EntryType']) OR trim($_POST['EntryType']) == ''){
	if ($RecvQty <= 50) {
		$_POST['EntryType'] = 'KEYED';
	} //elseif ($RecvQty <= 50) { $EntryType = "BARCODE"; }
	else {
		$_POST['EntryType'] = 'FILE';
	}
}

$invalid_imports = 0;
$valid = true;

echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF']) .'?identifier='.$identifier.'" enctype="multipart/form-data" >';
echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<input type="hidden" name="LineNo" value="' . $LineNo . '">';
echo '<input type="hidden" name="StockID" value="'. $StockID. '">';
echo '<table class=selection><tr><td>';
echo '<input type="radio" name=EntryType onClick="submit();" ';
if ($_POST['EntryType']=='KEYED') {
	echo ' checked ';
}
echo 'value="KEYED">'. _('Keyed Entry');
echo '</TD>';

if ($LineItem->Serialised==1){
	echo '<td>';
	echo '<input type="radio" name=EntryType onClick="submit();" ';
	if ($_POST['EntryType']=='SEQUENCE') {
		echo ' checked ';
	}
	echo ' value="SEQUENCE">'. _('Sequential');
	echo '</td>';
}

echo '<td valign=bottom>';
echo '<input type="radio" id="FileEntry" name="EntryType" onClick="submit();" ';
if ($_POST['EntryType']=='FILE') {
	echo ' checked ';
}
echo ' value="FILE">'. _('File Upload');
echo '&nbsp; <input type="file" name="ImportFile" onClick="document.getElementById(\'FileEntry\').checked=true;" >';
echo '</td></tr><tr><td colspan=3>';
echo '<div class="centre"><input type="submit" value="'. _('Set Entry Type'). ':"></div>';
echo '</td></tr></table>';
echo '</form>';

global $tableheader;
/* Link to clear the list and start from scratch */
$EditLink =  '<br /><div class="centre"><a href="' . htmlspecialchars($_SERVER['PHP_SELF']) . '?identifier='.$identifier.'&EditControlled=true&StockID=' . $LineItem->StockID .
	'&LineNo=' . $LineNo .'">'. _('Edit'). '</a> | ';
$RemoveLink = '<a href="' . htmlspecialchars($_SERVER['PHP_SELF']) . '?identifier='.$identifier.'&DELETEALL=YES&StockID=' . $LineItem->StockID .
	'&LineNo=' . $LineNo .'">'. _('Remove All'). '</a><br /></div>';
$sql="SELECT perishable
		FROM stockmaster
		WHERE stockid='".$StockID."'";
$result=DB_query($sql, $db);
$myrow=DB_fetch_array($result);
$Perishable=$myrow['perishable'];
if ($LineItem->Serialised==1){
	if ($Perishable==0) {
		$tableheader .= '<tr>
				<th>'. _('Serial No'). '</th>
				</tr>';
	} else {
		$tableheader .= '<tr>
				<th>'. _('Serial No'). '</th>
				<th>'. _('Expiry Date'). '<th>
				</tr>';
	}
} else if ($LineItem->Serialised==0 AND $Perishable==1){
	$tableheader = '<tr>
			<th>'. _('Batch/Roll/Bundle'). ' #</th>
			<th>'. _('Quantity'). '</th>
			<th>'. _('Expiry Date'). '</th>
		</tr>';
} else {
	$tableheader = '<tr>
			<th>'. _('Batch/Roll/Bundle'). ' #</th>
			<th>'. _('Quantity'). '</th>
		</tr>';
}

echo $EditLink . $RemoveLink;
if ($_POST['EntryType'] == 'FILE'){
	include('includes/InputSerialItemsFile.php');
} elseif ($_POST['EntryType'] == 'SEQUENCE'){
        include('includes/InputSerialItemsSequential.php');
} else { /*KEYED or BARCODE */
	include('includes/InputSerialItemsKeyed.php');
}
?>
