<?php

if($_REQUEST['Save'] == "Save" and $_REQUEST['action'] == "AddNew"){
	

	$sitename=$s->GetFieldValue('locations',"loccode='".$_REQUEST['branchid']."'",'prefix');

	$SQLMax=DB_query("select MAX(advice_no) as ADVNO from manufacturing_advice where branchid='".$_REQUEST['branchid']."'",$db);
	if(DB_num_rows($SQLMax)>0)
	{
		$MaxQut=DB_fetch_array($SQLMax);
		$MXNo=substr($MaxQut['ADVNO'],-2);
		$FinMx=$MXNo+1;
		$FinalCode=$sitename."/"."ADV"."/".str_pad($FinMx,5,0,STR_PAD_LEFT);
		$costcenter="CC"."/".$sitename."/".str_pad($FinMx,5,0,STR_PAD_LEFT);		
	}
	else
	{
		$FinalCode=$sitename."/"."ADV"."/".'00001';
		$costcenter="CC"."/".$sitename."/"."00001";
	}
	
	$data['advice_no'] = $FinalCode;
	$data['costcenter'] = $costcenter;
	$data['branchid'] = $_REQUEST['branchid'];
	$data['partyid'] = $_REQUEST['partyid'];
	$data['partyCode'] = $_REQUEST['partyCode'];
	$data['advice_type'] = $_REQUEST['advice_type'];
	$data['advice_date'] = FormatDateForSQL($_REQUEST['advice_date']);
	$data['prepared_by'] = $_SESSION['UserID'];
	$data['CompanyId'] = $_SESSION['ACGCompanyId'];
	$data['approved_by'] = $_REQUEST['approved_by'];
	$data['remarks'] = $_REQUEST['remarks'];
	$data['oreder_delevery_date'] = FormatDateForSQL($_REQUEST['oreder_delevery_date']);
	
	
	$s->RecordInsert('manufacturing_advice', $data);
	$masterId=DB_Last_Insert_ID($db,'manufacturing_advice','id');
		
	for($cnt=0;$cnt<count($_REQUEST['item_code']);$cnt++)
	{	
		if($_REQUEST['item_code'][$cnt]!=''){
		$dataArr['advice_id']=$masterId;
		$dataArr['item_code']=$_REQUEST['item_code'][$cnt];
		$dataArr['size']=$_REQUEST['size'][$cnt];
		//$dataArr['grade']=$_REQUEST['grade'][$cnt];
		$dataArr['itemUnit']=$_REQUEST['itemUnit'][$cnt];
		$dataArr['Qty']=$_REQUEST['priQty'][$cnt];
		//$dataArr['secQty']=$_REQUEST['secQty'][$cnt];
		//$dataArr['requisitionDate']=FormatDateForSQL(date('Y-m-d'));
			
		$s->RecordInsert('manufacturing_advice_items',$dataArr);
		}
	}
	
	$s->pageLocation("index.php?route=Production/manufacturing_adviceList".$rurl);

}
if($_REQUEST['MID'] != '')
{
	$sql = "SELECT * FROM manufacturing_advice where id='".$_REQUEST['MID']."'";
	$result = DB_query($sql,$db);
	$adviceRow = DB_fetch_array($result);
	
	$advice_no = $adviceRow['advice_no'];
	$costcenter= $adviceRow['costcenter'];
	$branchid = $adviceRow['branchid'];
	$partyid = $adviceRow['partyid'];
	$partyCode = $adviceRow['partyCode'];
	$advice_type = $adviceRow['advice_type'];
	$advice_date = ($adviceRow['advice_date'] != '')?date('d/m/Y',strtotime($adviceRow['advice_date'])):'';
	$prepared_by = $adviceRow['prepared_by'];
	$approved_by = $adviceRow['approved_by'];
	$remarks = $adviceRow['remarks'];
	$oreder_delevery_date = $adviceRow['oreder_delevery_date'];

}


if($_REQUEST['Save'] == "Save" and $_REQUEST['action'] == "Edit"){

	$data['branchid'] = $_REQUEST['branchid'];
	$data['partyid'] = $_REQUEST['partyid'];
	$data['partyCode'] = $_REQUEST['partyCode'];
	$data['advice_type'] = $_REQUEST['advice_type'];
	$data['advice_date'] = FormatDateForSQL($_REQUEST['advice_date']);
	$data['approved_by'] = $_REQUEST['approved_by'];
	$data['remarks'] = $_REQUEST['remarks'];
	$data['oreder_delevery_date'] = FormatDateForSQL($_REQUEST['oreder_delevery_date']);

	
	//print_r($_REQUEST);die;
	$condition = "id='".$_REQUEST['MID']."'";
	$s->RecordEdit('manufacturing_advice', $data,$condition);
	
	for($cnt=0;$cnt<count($_REQUEST['item_code']);$cnt++)
	{	
		if($_REQUEST['item_code'][$cnt]!=''){
			if($_REQUEST['itemRowId'][$cnt] !=''){
			$dataArr['item_code']=$_REQUEST['item_code'][$cnt];
			$dataArr['size']=$_REQUEST['size'][$cnt];
			$dataArr['grade']=$_REQUEST['grade'][$cnt];
			$dataArr['itemUnit']=$_REQUEST['itemUnit'][$cnt];
			$dataArr['priQty']=$_REQUEST['priQty'][$cnt];
			$dataArr['secQty']=$_REQUEST['secQty'][$cnt];
			
			$cond = "id='".$_REQUEST['itemRowId'][$cnt]."'";
			$s->RecordEdit('manufacturing_advice_items',$dataArr,$cond);
			}else{
				$dataArr['advice_id']=$_REQUEST['MID'];
				$dataArr['item_code']=$_REQUEST['item_code'][$cnt];
				$dataArr['size']=$_REQUEST['size'][$cnt];
				$dataArr['grade']=$_REQUEST['grade'][$cnt];
				$dataArr['itemUnit']=$_REQUEST['itemUnit'][$cnt];
				$dataArr['priQty']=$_REQUEST['priQty'][$cnt];
				$dataArr['secQty']=$_REQUEST['secQty'][$cnt];
				$dataArr['requisitionDate']=FormatDateForSQL(date('Y-m-d'));
					
				$s->RecordInsert('manufacturing_advice_items',$dataArr);
			}
		}
	}
	$s->pageLocation("index.php?route=Production/manufacturing_adviceList".$rurl);

}


?>


<center> <h2>Manufacturing Advice</h2>
</center>
<form name="frm_advice" id="frm_advice" method="post" action="" onsubmit="return validate();">
<fieldset><legend style="font-size:13px;"><strong>General Details</strong></legend>
<table width="100%" align="center" border="1" cellpadding="2" cellspacing="1">
  <tr>
    <th width="13%"><strong>Branch</strong></th>
    <td width="20%">
		<select name="branchid" id="branchid" style="width:150px;">
			<option value="">--Select--</option>	
			<?php
			$Sitesql="select * from locations where  CompanyId='".$_SESSION['ACGCompanyId']."' and loccode IN ('".$_SESSION['AssignedBranch']."') ";
			$SiteRes=DB_query($Sitesql,$db);
			while($SiteRow=DB_fetch_array($SiteRes))
			{?>
			<option value="<?php  echo $SiteRow['loccode'];?>" <?php  echo $branchid==$SiteRow['loccode']?'selected':''?> ><?php  echo $SiteRow['locationname'];?></option>
		<?php 
		}
		?>
		</select>
    </td>
    <th width="13%"><strong>Advice No.</strong></th>
    <td width="20%"><input name="advice_no" id="advice_no" type="text" class="" value="<?php echo ($advice_no != '')?$advice_no:"Auto Generate";?>" readonly="" style="width:100%;" />
    </td>
    <th><strong>Advice Date</strong></th>
    <td width="20%"><input name="advice_date" id="advice_date" type="text" value="<?php echo $advice_date;?>" class="datepicker" style="width:100%;" /></td>
  </tr>
  
  <tr>
    <th><strong>Party</strong></th>
    <td>
		<select name="partyid" style="width:150px;" id="partyid" onchange="GetClientDetails(this.value);">
			<option value="000">Select</option>
			<?php
			//echo  $_SESSION['UserSites'];
			$client_sql=DB_query("Select id, c_name from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
			  while($client_row=DB_fetch_array($client_sql)){ ?>
			<option value="<?php echo $client_row['id'];?>"<?php echo($partyid==$client_row['id']?'selected':'')?>>
			<?php  echo $client_row['c_name'];?></option> <?php } ?>
		</select> 
	</td>
    <th><strong>Party Code</strong></th>
    <td><input name="partyCode" id="partyCode" value="<?php echo $partyCode;?>" type="text" class="" style="width:100%;" readonly="" />
    </td>
    <th><strong>Type</strong></th>
    <td>
		<select name="advice_type" id="advicde_type" style="width:100%;">
		<option>- Select -</option>
		<?php
			$mainCat = array();
		$advicetype_sql = "SELECT id, stockmaincategoryid FROM stockmaincategory WHERE CompanyId='".$_SESSION['ACGCompanyId']."' and (stockmaincategoryname LIKE '%finish%' or stockmaincategoryname LIKE '%semifinish%') ";
			
			$catResult = DB_query($advicetype_sql,$db);
			while($catRow = DB_fetch_array($catResult)){
				$mainCat [] = $catRow['id'];
			}
			if(count($mainCat)>0){
			$subcat_Sql = "SELECT id, categorydescription FROM stockcategory WHERE stockmaincategory in(".implode(",",$mainCat).")";
			$subcat_result = DB_query($subcat_Sql,$db);
			while($subcatRow = DB_fetch_array($subcat_result)){
		?>
      		<option value="<?php echo $subcatRow['id']?>" <?php echo ($subcatRow['id'] == $advice_type)? "selected" : "";?>>
				<?php echo $subcatRow['categorydescription']?></option>
			<?php } 
			}
			?>
			
   	 	</select>
	 </td>
  </tr>
   <tr>
    
    <th><strong>Delevery Date</strong></th>
    <td width="20%" ><input  name="oreder_delevery_date"  style="width:150px;"  id="oreder_delevery_date" type="text" value="<?php echo $oreder_delevery_date;?>" class="datepicker" /></td>

<th><strong>Cost Center</strong></th>
    <td width="20%" ><input  name="cost_center"  style="width:100%;" readonly="true" id="cost_center" type="text" value="<?php echo ($costcenter != '')?$costcenter:"Auto Generate"; ?>"  class="" /></td>

<td colspan="2"></td>
  </tr>
  
</table>
</fieldset>
<br />

<fieldset><legend style="font-size:13px;"><strong>Product Information</strong></legend>



<table align='center' width='100%;' border='1' cellpadding='3' cellspacing='0'>
<tr class="row1">
	<td class="tabledata1" colspan="6" style="text-align:left;">
	<input type="button" onClick="addRow('dataTable')" value="Add More" class="search" />&nbsp;
	<input type="button" name="profile_popup" value="Pick Items" id="profile_popup"  onclick="open_items()" class="search" />
	<input type="button" class="search"  onclick="deleteRow('dataTable')" value="Delete" />
	<!--<input type="button" class="search" name="UndoRecords" value="UndoRecords" onclick="undo()" />-->
	</td>
</tr>
	<tr class='row1'>
	<th width="5%" align="left">Sr. No.</th>
	<th>&nbsp;</th>
    <th width="10%" align="left">Part Code</th>
    <th width="20%" align="left">Part Name</th>
    <th width="20%" align="left">Size</th>
   <!-- <th width="20%" align="left">Grade</th>-->
    <th align="left">Unit</th>
<!--    <th align="left">Qty.</th>-->  
  <th align="left">Qty </th>
  </tr>
<?php
if($_REQUEST['MID'] !='' and $_REQUEST['action'] == 'Edit'){
	$item_query = "SELECT MI.id, MI.item_code,MI.size, MI.grade, MI.itemUnit, MI.priQty, MI.secQty, I.StockID, I.item_name 
				   FROM manufacturing_advice_items MI 
				   INNER JOIN item_master I ON I.id=MI.item_code 
				   WHERE MI.advice_id='".$_REQUEST['MID']."'";
	$result = DB_query($item_query,$db);
	$i = 0;
	while($itemRow = DB_fetch_array($result)){?>
	<tr>
	<td><input name="delitem[]" type="checkbox" value="" /></td>
	<td><input type='hidden' name="item_code[]" id="itemid_<?php echo $i;?>" value="<?php echo $itemRow['item_code'];?>" /></td>
    <td><input type="text" name="partCode[]" id="partCode_<?php echo $i;?>" value="<?php echo $itemRow['StockID'];?>" style="width:100%;" /></td>
    <td><input type="text" name="partName[]" value="<?php echo $itemRow['item_name'];?>" id="partName_<?php echo $i;?>" style="width:100%;"/></td>
    <td><select style="width:100px;" name="size[]" id="size_<?php echo $i;?>">
			<option value="0">--Select--</option>
			<?php $SQLSize=DB_query("SELECT id,size FROM size_master WHERE CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
			while($sizeid=DB_fetch_array($SQLSize)){?>
			<option value="<?php echo $sizeid['id']; ?>" <?php echo ($itemRow['size']==$sizeid['id'])? "selected":""?>><?php echo $sizeid['size']; ?></option>
			<?php } ?>
		</select>
	</td>
   <?php /*?> <td><select style="width:100px;" name="grade[]" id="grade_<?php echo $i;?>">
			<option value="0">--Select--</option>
			<?php $SQLgrade=DB_query("SELECT id, grade FROM product_grade",$db); 
			while($gradeid=DB_fetch_array($SQLgrade)){?>
			<option value="<?php echo $gradeid['id']; ?>" <?php echo ($itemRow['grade']==$gradeid['id'])? "selected":""?>><?php echo $gradeid['grade']; ?></option>
			<?php } ?>
		</select>
	</td><?php */?>
    <td><input type="text" name="itemUnit[]" value="<?php echo $itemRow['itemUnit'];?>"  id="itemUnit_<?php echo $i;?>" style="width:100%;" /></td>
   <?php /*?> <td><input type="text" name="priQty[]" value="<?php echo $itemRow['priQty'];?>"  id="priQty_<?php echo $i;?>" style="width:100%;" /></td><?php */?>
    <td><input type="text" name="secQty[]" value="<?php echo $itemRow['secQty'];?>"  id="secQty_<?php echo $i;?>" style="width:100%;" /></td>
	</tr><input type="hidden" name="itemRowId[]" value="<?php echo $itemRow['id'];?>" />
<?php }
}
?>
  <tbody id='dataTable'>
	<tr>
    <td><input name="delitem[]" type="checkbox" value="" /></td>
	<td><input type='hidden' name="item_code[]" id="itemid" value="" /></td>
    <td><input type="text" name="partCode[]" value="" id="partCode" style="width:100%;" /></td>
    <td><input type="text" name="partName[]" value="" id="partName" style="width:100%;"/></td>
    <td><select style="width:100px;" name="size[]" id="size">
			<option>--Select--</option>
			<?php $SQLSize=DB_query("SELECT id,size FROM size_master WHERE CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
			while($sizeid=DB_fetch_array($SQLSize)){?>
			<option value="<?php echo $sizeid['id']; ?>"><?php echo $sizeid['size']; ?></option>
			<?php } ?>
		</select>
	</td>
   <?php /*?> <td><select style="width:100px;" name="grade[]" id="grade">
			<option>--Select--</option>
			<?php $SQLgrade=DB_query("SELECT id, grade FROM product_grade",$db); 
			while($gradeid=DB_fetch_array($SQLgrade)){?>
			<option value="<?php echo $gradeid['id']; ?>"><?php echo $gradeid['grade']; ?></option>
			<?php } ?>
		</select>
	</td><?php */?>
    <td><input type="text" name="itemUnit[]" id="itemUnit" style="width:100%;" /></td>
    <?php /*?><td><input type="text" name="priQty[]" value="" id="priQty" style="width:100%;" /></td><?php */?>
    <td><input type="text" name="secQty[]" value="" id="secQty" style="width:100%;" /></td>
  </tr>
 </tbody>   
</table>
<input type='hidden' name='count1' id='count1' value=''>
<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
 <input type="hidden" name="" value="<?php echo $i;?>" id="edit_cnt" />

</fieldset>

<br />
<table width="100%" align="center" border="1" cellpadding="2" cellspacing="1">
  
  <tr>
    <th width="15%"><strong>Remarks</strong></th>
    <td width="35%" colspan="3">
		<textarea name="remarks" id="remarks" class="" style="width:100%;"><?php echo $remarks;?></textarea></td>
  </tr>

  <tr>
    <th width="15%"><strong>Prepared By:</strong></th>
    <td width="35%">
		<input name="prepared_by" id="prepared_by" type="text" class="" style="width:100%;" 
		value="<?php echo ($prepared_by !='')?$s->GetFieldValue('www_users'," id='".$prepared_by."'",'userid'):$s->GetFieldValue('www_users'," id='".$_SESSION['UserID']."'",'userid');?>"  /></td>
    <th width="15%"><strong>Approved By:</strong></th>
    <td width="35%"><input name="approved_by" id="approved_by" value="<?php echo $approved_by;?>" type="text" class="" style="width:100%;" /></td>
  </tr>
</table>
<br />
<p align="center"><input type="submit" name="Save" value="Save" class="search" />&nbsp;<input type="submit" name="Cancel" value="Cancel" class="search" /></p>

</form>

<script type="text/javascript">

function addRow(tableID) 
{
	if(document)
	if(document.getElementById("count1").value!='')
	{
		var edit_cnt=parseInt(document.getElementById("count1").value);
	}
	else
	{
	edit_cnt=1;
	}
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	
	var newCount=parseInt(rowCount)+parseInt(edit_cnt)-1;
	var newcell = row.insertCell(i);
	//newcell.style.background='#e7e7e7';
	newcell.className='tabledata';
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+newCount;
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id_hidden = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+newCount;
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+newCount;
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id_chk = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+newCount;
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id;
	newcell.childNodes[0].id = newcell.childNodes[0].id+'_'+newCount;
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	//document.getElementById("itemUnit_"+newCount).value='1';
	}
	
function deleteRowOrder(tableID) 
{
		try 
		{
			var table = document.getElementById(tableID);
			//alert(table);
			var rowCount = table.rows.length;
			//alert(rowCount);
			for(var i=0; i<rowCount; i++) 
				{
					var row = table.rows[i];
					var chkbox = row.cells[0].childNodes[0];
					if(null != chkbox && true == chkbox.checked) 
					{
						if(rowCount <= 1) 
						{
							alert("Cannot delete all the rows.");
							break;
						}
					table.deleteRow(i);
					rowCount--;
					i--;
					}
					
				}
		document.getElementById("hiditemcount").value=parseInt(rowCount)-1;		
		}
		catch(e) 
		{
			alert(e);
		}

	}	
	
	function open_items()
	{
	
	var site=document.getElementById("branchid").value;
	
	var store='';
	var date=document.getElementById("advice_date").value;
	
	window.open("mnfAdviceItem.php?site="+site+"&store="+store+"&date="+date+"&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'] ?>&action=<?php echo $_REQUEST['action']?>","itempopup_window","height=500,width=600,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
	}
	
function GetClientDetails(str)
{
	
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("partyCode").value=xmlhttp.responseText;
	
	}
	}
	xmlhttp.open("GET","mnfAdviceClientDetail.php?cid="+str,true);
	xmlhttp.send();
}

function validate(){

var itemcount = document.getElementsByName('item_code[]');
	
	if(itemcount[0].value == '')
	{
		alert("Pick Items from Item List");
		return false;
	}
	if(document.getElementById("branchid").value=='')
	{
		alert("Branch must be filled out");
		document.getElementById("branchid").focus();
		return false;
	}

}
	
</script>
