<?php 
if($_REQUEST['Save'])
{
      date_default_timezone_set('Asia/Kolkata');
	$PreSql=DB_query("select prefix from locations where loccode='".$_REQUEST['branch']."'",$db);
	$Prefix=DB_fetch_array($PreSql);
	$chkLC=DB_query("select * from faberication_main where site='".$_REQUEST['branch']."'",$db);
	if(DB_num_rows($chkLC)>0)
	{
			$GetMxSQL=DB_query("select sheet_no AS MAXLC from faberication_main 
							 where site='".$_REQUEST['branch']."' order  by id desc limit 0,1",$db);
			$GetMAXID=DB_fetch_array($GetMxSQL);
			$Explode=explode("/",$GetMAXID['MAXLC']);
			$FinalMaxId=$Explode[2]+1;
			$FinalCode=$Prefix['prefix']."/"."FAB"."/".str_pad($FinalMaxId,5,0,STR_PAD_LEFT);
	}
	else
	{
	$FinalCode=$Prefix['prefix']."/"."FAB"."/"."00001";
	}
	
	$data['site']                 =$_REQUEST['branch'];
	$data['sheet_date']           =FormatDateForSQL($_REQUEST['sheet_date']);
	$data['sheet_no']             =$FinalCode;
	$data['type_of_sheet']        =$_REQUEST['type_sheet'];
	$data['type']                 =$_REQUEST['typ_id'];
	$data['client']               =$_REQUEST['client'];
	$data['against_indent']       =$_REQUEST['against_indent'];	
	$data['consignee']            =$_REQUEST['consignee_id'];
	$data['checked_by']           =$_REQUEST['checked_by'];	
	$data['prepared_by']          =$_REQUEST['prepared_by'];
	$data['authorized_by']        =$_REQUEST['authorized_by'];
	$data['created_by']           =$_SESSION['UserID'];
	$data['created_date']         =date("Y-m-d H:i:s");
	$data['CompanyID']            =$_SESSION['ACGCompanyId'];
	$data['remarks']              =$_REQUEST['remarks1'];
	$row= $s->RecordInsert('faberication_main', $data);
	$lastid=DB_Last_Insert_ID($db,'faberication_main', 'id');
	
	
	$data_arr1=array();
	
	for($k1=0;$k1<count($_REQUEST['itemid']);$k1++)
	{
			
		$data_arr1['parent_id']=$lastid;
		$data_arr1['itemid']=$_REQUEST['itemid'][$k1];
		$data_arr1['size']=$_REQUEST['sizeid'][$k1];
		$data_arr1['qty']=$_REQUEST['qty'][$k1];
		$rowinsert= $s->RecordInsert('faberication_main_child_item', $data_arr1);
		
	}
	
	
	
	
	$data_arr=array();
	
	for($k=0;$k<count($_REQUEST['parameter']);$k++)
	{
		if($_REQUEST['specification'][$k]!='')
		{	
		$data_arr['parentid']=$lastid;
		$data_arr['groupid']=$_REQUEST['group'][$k];
		$data_arr['parameter']=$_REQUEST['parameter'][$k];
		$data_arr['specificatin']=$_REQUEST['specification'][$k];
		$data_arr['remarks']=$_REQUEST['remarks'][$k];
		$rowinsert= $s->RecordInsert('faberication_main_child', $data_arr);
		}
	}
	
	
	$update=DB_query("update measurment_sheet_main set stauts='Done' where id='".$_REQUEST['against_indent']."'",$db);
	
	$update=DB_query("update salesorder_details_approv set fabrication_id='".$lastid."' where measurement_id='".$_REQUEST['against_indent']."'",$db);
	
	
	
	$s->pageLocation('index.php?route=CRM/measuement_sheet_view&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
	//$s->pageLocation('index.php?route=CRM/fabrication_index&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
	
		
	
}

$sql_fetch=DB_query("select * from measurment_sheet_main where id='".$_REQUEST['id']."'",$db);
$sql_fetch_data=DB_fetch_array($sql_fetch);
extract($sql_fetch_data);


?>
<center> <h2>Fabrication Sheet</h2>
</center>
<form action="" method="post">
<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>

<?php $sql_branch=DB_fetch_array(DB_query("select * from locations where loccode='".$site."' and  CompanyId='".$_SESSION['ACGCompanyId']."' ",$db)); ?>
    <td width="20%">
	<input type="text" name="branch_name"  style="width:100%;" value=" <?php echo $sql_branch['locationname'];  ?>" readonly="true" />
	<input type="hidden"  name="branch" value="<?php echo $sql_branch['loccode'];  ?>" />
	   </td>
    <th width="13%"><strong>Sheet No.</strong></th>
    <td width="20%"><input name="sheet_no" id="sheet_no" type="text" class="" style="width:100%;" value="<?php  echo "Auto Generate" ?>" />    </td>
    <th><strong>Sheet Date</strong></th>
    <td width="20%"><input name="sheet_date" id="sheet_date" type="text" class="datepicker" style="width:100%;"
	value="<?php echo date('d/m/Y') ; ?>" /></td>
  </tr>
  <tr>
    <th><strong>Client</strong></th>
    <td><input type="text" style="width:100%;" name="party1" id="party1" readonly 
	value="<?php echo $s->getField('clientmaster','id',$client,'c_name'); ?>"   />
	<input type="hidden" name="client" value="<?php echo $client; ?>" />
	
	</td>
    <th><strong>Against MMS</strong></th>
    <td><input type="text" style="width:100%;" name="against_indent1" id="against_indent1" value="<?php echo $mms_no?>">
	<input type="hidden" style="width:100%;" name="against_indent" id="against_indent" value="<?php echo $_REQUEST['id']; ?>">
       </td>
    <th><strong>Date</strong></th>
    <td><input name="date" id="date" type="text" class="datepicker" style="width:100%;"
	 value="<?php echo ConvertSQLDate($mms_date)?>"  readonly="readonly"/></td>
  </tr>
  
  <tr>
    <th><strong>Consignee</strong></th>
    <td>
       	<?php
	$consignee_sql=DB_fetch_array(DB_query(" select * from  clientmaster_consignee_address where id='".$consignee."' ",$db)); ?>
    <input type="text"  style="width:100%;"  value="<?php echo $consignee_sql['consignee_name']; ?>" name="consignee_name" id="consignee_name"  readonly="true"/>
	<input type="hidden" name="consignee_id" value="<?php echo $consignee_sql['id'];  ?>" />
	
	</td>
    <th><strong>Type of Sheet</strong></th>
    <td><input type="text" style="width:100%;" name="type_sheet" id="type_sheet" 
	value="<?php echo $mms_type  ;?> ">
    </td>
    <th><strong>Type</strong></th>
	<?php $ty_nm=DB_fetch_array(DB_query("SELECT name FROM type_master WHERE id='".$type."'",$db)); ?>
    <td><input type="text" style="width:100%;"  readonly name="type1" id="type" value="<?php echo $ty_nm['name'] ?> ">
	<input type="hidden" name="typ_id" value="<?php echo $type; ?>" />
</td>
  </tr>
</table>
</fieldset>
<br />

<fieldset><legend style="font-size:13px;"><strong>Item Details

</strong></legend>
<table width="100%" border="1" cellpadding="2" cellspacing="1">
  <tr>
    <th width="5%">Sr No</th>
    <th width="20%">Item Name</th>
    <th width="33%" align="left">Size</th>
    <th align="left">Qty</th>
	<th align="left">Unit</th>
	
  </tr>
<?php
$k1=1;
$sql2=DB_query("select DISTINCT(itemid) from measurment_sheet_details where parent_id='".$_REQUEST['id']."'
                ",$db);  
$arr12='';
while($row=DB_fetch_array($sql2))
{
$arr12[]=$row['itemid'];
}



$sql21=DB_query("select DISTINCT(size) from measurment_sheet_details where parent_id='".$_REQUEST['id']."' ",$db);  
$arr22='';
while($row2=DB_fetch_array($sql21))
{
$arr22[]=$row2['size'];
}

for($i=0;$i<count($arr12);$i++)
{



$sql=DB_query("select sum(qty) as qty from measurment_sheet_details where   parent_id='".$_REQUEST['id']."'
              and itemid='".$arr12[$i]."'",$db);
$row2=DB_fetch_array($sql);

$unitid=$s->getField('item_master','id',$arr12[$i],'mst_unitid');
$unit=$s->getField('unitsofmeasure','unitid',$unitid,'unitname');

?>
 <tr>
 <td><?php  echo $k1;    ?></td>
 <td><?php  echo $s->getField('item_master','id',$arr12[$i],'item_name');   ?>
 <input type="hidden" name="itemid[]" value="<?php echo $arr12[$i] ?>" />
 </td>
 <td><?php  echo $s->getField('size_master','id',$arr22[$i],'size');   ?>
 <input type="hidden" name="sizeid[]" value="<?php echo $arr22[$i]?>" />
 </td>
 <td><input type="text" name="qty[]" value="<?php  echo $row2['qty'];   ?>" readonly="readonly" /> </td>
 <td><?php  echo $unit;   ?></td> 
  
  </tr>
 <?php $k1++; } ?> 
  
  </table>
  </fieldset>
  



<?php
$sql=DB_query("select * from fabrication_group where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 
while($row=DB_fetch_array($sql))
{
if($s->getField('fabrication_group_parameter','frm_id',$row['id'],'frm_id')==$row['id']) 
{ 

?>
<fieldset><legend style="font-size:13px;"><strong><?php echo $row['group_name']; ?>

</strong></legend>
<table width="100%" border="1" cellpadding="2" cellspacing="1">
  <tr>
    <th width="5%">Sr No</th>
    <th width="20%">Parameter</th>
    <th width="33%" align="left">Specification</th>
    <th align="left">Remarks</th>
  </tr>
  
  <?php 
  $i=1;
  $sql1=DB_query("select * from fabrication_group_parameter where frm_id='".$row['id']."' and parant_id=0",$db);
  while($row2=DB_fetch_array($sql1))
  {
  ?>
  <tr>
    <td><?php echo $i; ?></td>
    <td><?php  echo $row2['terms_conditions']; ?>
	<input type="hidden" name="parameter[]" value="<?php echo  $row2['id']; ?>" />
	<input type="hidden" name="group[]" value="<?php echo $row['id']; ?> " />
	</td>
    <td><select style="width:100%;" name="specification[]" id="specification">
      <option value="">--Select--</option>
     <?php
	 $sql3=DB_query("select * from  fabrication_group_parameter where parant_id='".$row2['id']."' ",$db);
	 while($row1=DB_fetch_array($sql3))
	 {
	 
	  ?>
	 <option value="<?php echo $row1['id']; ?>"><?php echo $row1['terms_conditions']; ?></option>
	 <?php } ?>
    </select></td>
    <td><input name="remarks[]" id="" type="text" class="" style="width:100%;" /></td>
  </tr>
  <?php $i++; }?>
  
  
</table>


</fieldset>
<?php  } } ?>
<br />

<br />
<table width="100%" align="center" border="1" cellpadding="2" cellspacing="1">
 <tr>
    <th>Remarks</th>
    <td colspan="5">
	<textarea rows="3" name="remarks1" style="width:100%"></textarea>
	</td>
  </tr>
 
  <tr>
    <th width="13%"><strong>Prepared By </strong></th>
    <td width="20%"><input type="text" name="prepared_by1" style="width:100%;"  
	value="<?php echo 	 $s->getField('www_users','id',$_SESSION['UserID'],'realname'); ?>"  readonly="readonly"/>
	<input type="hidden" name="prepared_by" value="<?php echo $_SESSION['UserID']; ?>" />
         </td>
    <th width="13%"><strong>Checked By </strong></th>
    <td width="20%"><input name="checked_by" id="checked_by" type="text"  style="width:100%;" />    </td>
    <th><strong>Authorized By</strong></th>
    <td width="20%"><input type="text" name="authorized_by" value=""  style="width:100%;"/></td>
  </tr>
  
</table>

<p align="center"><input type="submit" name="Save" value="Save" onclick="return validate()" class="search" />&nbsp;<input type="submit" name="Cancel" value="Cancel" class="search" /></p>
</form>