<link rel="stylesheet" href="css/new_mxerp.css">
<?php ob_start();
$PageSecurity = 4;
$PageName = 'Finished Material';
include('includes/DefinePOClass.php');
include('includes/stockcalculation.php');
global $s;

if(isset($_POST['Save']))
{
	 
   

         $sql="update mat_conversion set 
                      
                      store='".$_REQUEST['store']."',
                      transaction_time='".date("Y-m-d H:i:s")."',
                      modify_by='".$_SESSION['UserID']."',
                      modify_date='".date('Y-m-d')."',
                      remarks='".$_REQUEST['remarks']."',
                      item_from='".$_REQUEST['first_item']."',
                      item_to='".$_REQUEST['second_item']."' where id='".$_REQUEST['id']."'
                      ";
                    $insertResult = DB_query($sql,$db,$ErrMsg);
           $Last_id=$_REQUEST['id'];

			 
	    if($Last_id!='')
	   {
       DB_query("delete from mat_conversion_details where parent_id='".$_REQUEST['id']."'",$db);

      $sql_detl="insert into mat_conversion_details (parent_id,item_id,trnx_type,pre_qty,size_id,gread_id,batch_no) values('".$Last_id."','".$_REQUEST['first_item']."','issue','".$_REQUEST['convert_stock']."','".$_REQUEST['sizesp1']."','".$_REQUEST['gradesp1']."','".$_REQUEST['item_batch']."'),('".$Last_id."','".$_REQUEST['second_item']."','received','".$_REQUEST['new_item_stock_convert']."','".$_REQUEST['sizesp2']."','".$_REQUEST['gradesp2']."','".$_REQUEST['new_item_batch']."')";
       DB_query($sql_detl,$db);

      }
      prnMsg( _('Record saved Successfully'),'success');
                echo "<script>location.href='index.php?route=Inventory/transation/material_conversion_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
    

	 }
    
     $resultMat_con=DB_query("select * from mat_conversion where id='".$_REQUEST['id']."'",$db);
     $row=DB_fetch_array($resultMat_con);

     $function_filter[]=$row['site'];
$function_filter[]=$row['store'];
$function_filter[]=date('Y-m-d');
 $item_Arr=array(); 


    $sql_edit=DB_query("Select * from mat_conversion_details where parent_id='".$_REQUEST['id']."'",$db);
while($record_edit=DB_fetch_array($sql_edit))
{
   if($record_edit['trnx_type']=='issue')
  $item_Arr[0]=$record_edit['item_id'];
  else
   $item_Arr[1]=$record_edit['item_id'];
      
  $itemResult=DB_query("select * from item_master where id='".$record_edit['item_id']."'",$db);
  $row_item=DB_fetch_array($itemResult);

$op=openingBalance($function_filter,$record_edit['item_id']);
$in_1=receivedMRN($function_filter,$record_edit['item_id']);
$out_1=issueSlip($function_filter,$record_edit['item_id']);
$out_2=issueSiteToSite($function_filter,$record_edit['item_id']);    
$in_2=receivedSiteToSite($function_filter,$record_edit['item_id']);
$out_3=issueMatarialReturn($function_filter,$record_edit['item_id']);
$in_3=receivedReturn($function_filter,$record_edit['item_id']);  
$out_4=Rgpchallan($function_filter,$record_edit['item_id']);
$out_5=Nrgp($function_filter,$record_edit['item_id']);
$in_4=Rgpreturn($function_filter,$record_edit['item_id']);  
$in_5=FinishedMR($function_filter,$record_edit['item_id']);
$in_6=MaterialConReceived($function_filter,$record_edit['item_id']);
$out_6=MaterialConIssue($function_filter,$record_edit['item_id']);
$in_7=PhysicalStockPlus($function_filter,$record_edit['item_id']);
$out_7=PhysicalStockMinus($function_filter,$record_edit['item_id']);
$out_8=dispatchIssue($function_filter,$record_edit['item_id']);
$out_9=jobWorkChallan57Issue($function_filter,$record_edit['item_id']);
$in_8=jobWorkChallan57Received($function_filter,$record_edit['item_id']);
$invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5 + $in_6 + $in_7 + $in_8);
$outvalue=($out_1+$out_2+$out_3+$out_4+$out_5 + $out_6 + $out_7 + $out_8 + $out_9);
$tot=$invalue-$outvalue;

$stockbalance[$record_edit['item_id']]=$tot; 



//$s->getField('item_master','id',$record_edit['item_id'],'item_name');
$item_desArr[$record_edit['item_id']]=$row_item['item_name'];
$item_unitArr[$record_edit['item_id']]=$s->getField('unitsofmeasure','unitid',$row_item['mst_unitid'],'unitname');
$item_codeArr[$record_edit['item_id']]=$row_item['StockID'];
$item_sizeArr[$record_edit['item_id']]=$record_edit['size_id'];
$item_gradeArr[$record_edit['item_id']]=$record_edit['gread_id'];
    $sunit=((integer)$row_item['conv_unit']==0)?$row_item['mst_unitid']:$row_item['conv_unit'];
$item_sunitArr[$record_edit['item_id']]=$s->getField('unitsofmeasure','unitid',$sunit,'unitname');
$item_confArr[$record_edit['item_id']]=$row_item['conv_factor'];
$item_batch[$record_edit['item_id']]=$record_edit['batch_no'];
$item_Stock[$record_edit['item_id']]=$record_edit['pre_qty'] + $stockbalance[$record_edit['item_id']];
$item_qty[$record_edit['item_id']]=$record_edit['pre_qty'];
  $conf=((float)$row_item['conv_factor']==0)?1:$row_item['conv_factor'];
$item_con_fctor[$record_edit['item_id']]=$conf;


 }
 // /print_r($item_unitArr);
 ?>
<div class="body_contain">
<div class="tab_div">
 <h1>Material Conversion</h1>
<form name="form1" onsubmit="return chkValidate();" method="post">
<div align="center">
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
<tr>
    <th class="tabledata1"><strong>Branch</strong></th>
    <td>

 
	<select name="site" id="site" style="width:100%;" >
    
      <option value="<?php echo $row['site']; ?>"><?php echo $s->getField('locations','loccode',$row['site'],'locationname'); ?></option>
     
                                
    </select></td>
    <th class="tabledata1"><strong>MTC Slip No.</strong></th>
    <td id="ISSUE"><input name="MCT_slip" type="text" id="MCT_slip" value="<?php echo $row['mat_con_slip_no']; ?>" size="15" style="width:100%;" readonly /> 
      </td>
    <th class="tabledata1"><strong> Date</strong></th>
    <td id="ISSUE"><input name="MTC_date" type="text"  id="MTC_date" value="<?php echo date('d/m/Y',strtotime($row['mat_con_date'])); ?>"  size="15" style="width:100%;" readonly /> 
    </tr>
  <tr>
    <th class="tabledata1"><strong>Godown</strong></th>
    <td><div id='hid-Godown-div'>
	<?php
	     $sql_store ='select distinct id,StoreCode, StoreName from storetype';
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store" id="store" style="width:100%;">
        <option value="0">Select</option>
        <?php 

		while ($myrow=DB_fetch_array($resultStkLocs))
		{ ?>
        <option value="<?php echo $myrow['id'];?>"<?php echo($row['store']==$myrow['id']?'selected':'')?>><?php echo $myrow['StoreName'];?></option>
        <?php 
		}
		?>
      </select></div></td>
    <td class="tabledata1"><strong></strong>  </td>
    <td>
	</td>
	  <td class="tabledata1"><strong> </strong> </td>
    <td id="work_or_date"></td>
  </tr>
</table>
</div>
<br /><br />

<div style="overflow:auto; width:1237px; height:auto">
  
<table width="100%" id="" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td>Pick Item</td>
    <td class="tabledata">Item Description </td>
    <td class="tabledata">Item Code</td>
    <td class="tabledata">Batch No.</td>
    
     <td class="tabledata" colspan="2">Specification</td>
       <td class="tabledata">UOM</td>
	  <td class="tabledata">Stock Qty</td>
	 
    <td class="tabledata">UOM</td>
	<td class="tabledata">Stock </td>

  </tr>
  <tbody id="dataTable">
   
    <tr>
    <td><?php if($_REQUEST['action']=='Edit'){ ?><input type="button" style="float:left; margin-left;20px; margin-bottom:5px;" name="profile_popup" value="Pick Items" id="profile_popup"  onclick="open_items('1')" accesskey="I" class="search" /><?php } ?></td>
    <td><input type="hidden" value="<?php echo $item_Arr[0]; ?>" name="first_item" id="first_item"><input type="text" id="item_desc" name="item_desc" value="<?php echo $item_desArr[$item_Arr[0]]; ?>" readonly /></td>
    <td><input type="text" id="item_code" value="<?php echo $item_codeArr[$item_Arr[0]]; ?>" name="item_code" readonly ></td>
    <td><input type="text" id="item_batch" value="<?php echo $item_batch[$item_Arr[0]]; ?>" name="item_batch"  ></td>
    <td> <select  name="sizesp1" id="sizesp1"   >
    <option value="0">select</option>
    <?php
     $resultSize=DB_query("select * from size_master",$db);
     while($rowSize=DB_fetch_array($resultSize))
     { ?>
    <option value="<?php echo $rowSize['id']; ?>" <?php echo ($item_sizeArr[$item_Arr[0]]==$rowSize['id'])?'selected="selected"':''; ?>><?php echo $rowSize['size']; ?></option>
     <?php }
     ?>
   </select>  </td>
   <td><select  id="gradesp1" name="gradesp1"  >
    <option value="0">select</option>
    <?php
      $resultGrade=DB_query("select * from product_grade",$db);
      while($rowGrade=DB_fetch_array($resultGrade))
      { ?>
     <option value="<?php echo $rowGrade['id']; ?>" <?php echo ($item_gradeArr[$item_Arr[0]]==$rowGrade['id'])?'selected="selected"':''; ?> ><?php echo $rowGrade['grade']; ?></option>
       <?php }
     ?>
   
   </select></td>
   
   <td><input type="text" id="item_unit" value="<?php echo $item_unitArr[$item_Arr[0]]; ?>" name="item_unit" readonly ></td>
   <td><input type="text"  id="item_stock" value="<?php echo $item_Stock[$item_Arr[0]]; ?>" name="item_stock" readonly ></td>
   <td><input type="text" id="item_unit1" name="item_unit1" value="<?php echo $item_sunitArr[$item_Arr[0]]; ?>" readonly ></td>
   <td><input type="text" id="item_stock1" value="<?php echo ($item_con_fctor[$item_Arr[0]] * $item_Stock[$item_Arr[0]]); ?>" name="item_stock1" readonly ></td> 
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td><td></td>
     <td colspan="2"><strong>Convert into </strong></td>
      <td><input type="text" id="convert_unit" value="<?php echo $item_unitArr[$item_Arr[0]]; ?>" name="convert_unit"></td>
      <td><input type="text" id="convert_stock" value="<?php echo $item_qty[$item_Arr[0]]; ?>" onkeyup="convert_s_unit()" name="convert_stock"></td>
      <td><input type="text" id="convert_unit1" value="<?php echo $item_sunitArr[$item_Arr[0]]; ?>" name="convert_unit1"></td>
      <td><input type="text" id="convert_stock1" value="<?php echo ($item_con_fctor[$item_Arr[0]] * $item_qty[$item_Arr[0]]) ?>"  name="convert_stock1"><input type="hidden"  name="con_factor" value="<?php echo $item_con_fctor[$item_Arr[0]]; ?>" id="con_factor"></td>
    </tr>
    <tr>
      <td><?php if($_REQUEST['action']=='Edit'){ ?><input type="button" style="" name="profile_popup" value="Pick Items" id="profile_popup"  onclick="open_items('2')" accesskey="I" class="search" /><?php  } ?></td>
    <td><input type="hidden" name="second_item" value="<?php echo $item_Arr[1]; ?>" id="second_item"><input type="text" id="new_item_name" value="<?php echo $item_desArr[$item_Arr[1]]; ?>" name="new_item_name"></td>
    <td><input type="text" id="new_item_code" value="<?php echo $item_codeArr[$item_Arr[1]]; ?>" name="new_item_code"></td>
     <td><input type="text" id="new_item_batch" value="<?php echo $item_batch[$item_Arr[1]]; ?>" name="new_item_batch"></td>
     <td> <select  name="sizesp2" id="sizesp2"   >
    <option value="0">select</option>
    <?php
     $resultSize=DB_query("select * from size_master",$db);
     while($rowSize=DB_fetch_array($resultSize))
     { ?>
    <option value="<?php echo $rowSize['id']; ?>" <?php echo ($item_sizeArr[$item_Arr[1]]==$rowSize['id'])?'selected="selected"':''; ?> ><?php echo $rowSize['size']; ?></option>
     <?php }
     ?>
   </select>  </td>
   <td><select  name="gradesp2"  id="gradesp2" >
    <option value="0">select</option>
    <?php
      $resultGrade=DB_query("select * from product_grade",$db);
      while($rowGrade=DB_fetch_array($resultGrade))
      { ?>
     <option value="<?php echo $rowGrade['id']; ?>" <?php echo ($item_gradeArr[$item_Arr[1]]==$rowGrade['id'])?'selected="selected"':''; ?> ><?php echo $rowGrade['grade']; ?></option>
       <?php }
     ?>
   
   </select></td>
   <td><input type="hidden" value="<?php echo $item_con_fctor[$item_Arr[1]]; ?>" id="con_factor_second"><input type="text" value="<?php echo $item_unitArr[$item_Arr[1]]; ?>"  id="new_item_unit" name="new_item_unit"><input type="hidden" id="second_item_old_stock" value="<?php echo $stockbalance[$item_Arr[1]]; ?>" ></td>
   <td><input type="text" value="<?php echo $item_Stock[$item_Arr[1]]; ?>" size="8" id="new_item_stock" readonly name="new_item_stock">&nbsp;<input type="text"  value="<?php echo $item_qty[$item_Arr[1]]; ?>" size="8" id="new_item_stock_convert" onkeyup="set_new_stock();"  name="new_item_stock_convert"></td>
   <td><input type="text" id="new_item_unit1" value="<?php echo $item_sunitArr[$item_Arr[1]]; ?>"  name="new_item_unit1"></td>
   <td><input type="text" id="new_item_stock1" value="<?php echo ($item_con_fctor[$item_Arr[1]] * $item_Stock[$item_Arr[1]]) ?>" name="new_item_stock1"></td> 
    </tr>
   </tbody>
</table>
</div>
<div class="body_lightbox1" id="lightbox1" style="overflow:auto; width:600px; height:250px; display:none;"></div>
<input type="hidden" name="hiditemcount" id="hiditemcount" value="<?php echo $i; ?>" />
  <input type="hidden" name="" value="<?php echo $i; ?>" id="edit_cnt" />
  <input type="hidden" name="" value="<?php echo $i; ?>" id="rowcount1" />

  <br/><br />
<table width="100%" border="1" cellpadding="3" cellspacing="0" style="border-collapse:collapse; border:#666666 solid 1px;">
  <tr>
    <td class="tabledata">Remarks:</td>
    <td><textarea name="remarks" id="remarks" cols="" rows="" style="width:100%; height:60px;"><?php echo $row['remarks']; ?></textarea></td>
  </tr>
</table>


<?php
   if($_REQUEST['action']=='Edit')
   {
 ?>
<p align="center"><input type="submit" name="Save" value="Save" /></p>
<?php } ?>
</form>
</div>
</div>
<script type="text/javascript">
function open_items(num)
{
window.open("popup_conversion_items.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&num="+num,"item_popup_window","height=500,width=500,left=1111px,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}

function set_new_stock()
{ var num_convert=0;
   if($('#new_item_stock_convert').val()=='')
    num_convert=0;
  else
     num_convert=$('#new_item_stock_convert').val();

      $('#new_item_stock').val(parseFloat($('#second_item_old_stock').val()) + parseFloat(num_convert));
   $('#new_item_stock1').val(parseFloat($('#new_item_stock').val()) * parseFloat($('#con_factor_second').val()));

}

function convert_s_unit()
{ //con_factor 
    if( parseFloat($('#convert_stock').val()) > parseFloat($('#item_stock').val()))
    {
       alert('To convert Quantity should be less current stock.');
       $('#convert_stock').val($('#item_stock').val());
    }

    var con_stock=0;
   if($('#convert_stock').val()=='')
    con_stock=0;
   else
    con_stock=$('#convert_stock').val();

     $('#convert_stock1').val(parseFloat(con_stock) * parseFloat($('#con_factor').val()));
    
    
     
}

function chkValidate()
{
    if($('#site').val()==0)
    {
      alert('Please select branch.');
      return false;
    }
     if($('#MTC_date').val()=='')
     {
      alert('Please select date');
      return false;
     }
       if($('#store').val()==0)
       {
        alert('Please select godown.');
        return false;
       }
        if($('#first_item').val()=='')
        {
          alert('Please select first item');
          return false;
        }
        if($('#second_item').val()=='')
        {
          alert('Please select second item');
          return false;
        }
        if($('#convert_stock').val()=='' || $('#convert_stock').val()==0)
        {
           alert('Please enter convert quantity.');
           return false;
        }
        if(($('#new_item_stock_convert').val()=='') || ($('#new_item_stock_convert').val()==0))
        {
          alert('Please enter converted quantity of second item.');
          return false;
        }
         if(($('sizesp1').val()==0) || ($('#gradesp1').val()==0))
         {
          alert('Please select Specification of first item.');
          return false;
         }
         if(($('sizesp2').val()==0) || ($('#gradesp2').val()==0))
         {
          alert('Please select Specification of second item.');
          return false;
         }

         return true;

} 
</script>
<style type="text/css">
.body_lightbox1 {
width:100%;
background-color:#e7e7e7;
position:absolute;
left:280px;
top:95px;
padding-left:5px;
padding-right:5px;
border:#666666 solid 3px;
}
</style>
