<?php 
if($_REQUEST['Save']!='')
{
	if($_REQUEST['type']=='sales')
	{
		for($i=0;$i<$_REQUEST['count'];$i++)
		{
			if($_REQUEST['inv_check'][$i]=='1')
			{
				  $invoice_Sql = "Update invoice set form_c_status='received',receive_date = '".ConvertSQLDate($_REQUEST['indate'][$i])."', form_c_no='".$_REQUEST['form_c_no'][$i]."' where invoice_id='".$_REQUEST['inv_id'][$i]."' ";
			 	 
				$update=DB_query($invoice_Sql,$db);
			}
		}
	
	}
	else
	if($_REQUEST['type']=='purchase')
	{
		for($i=0;$i<$_REQUEST['count'];$i++)
		{
			if($_REQUEST['inv_check'][$i]=='1')
			{
				  $issuequery =  "Update purchase_billbook set form_c_status='received', form_c_no='".$_REQUEST['form_c_no'][$i]."',issue_date='".ConvertSQLDate($_REQUEST['indate'][$i])."' where id='".$_REQUEST['bill_id'][$i]."'";
				 
				$update=DB_query($issuequery,$db);
			}
		}
		
	}
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php if($_REQUEST['type']=='sales'){?>
<h1>Form C Recievable</h1><?php }elseif($_REQUEST['type']=='purchase'){?>
<h1>Form C Issuable</h1>
<?php }?>
<form action="" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right"><strong>Branch:</strong></td>
    <td width="20%"><select name="branch" id="branch" style="width:98%">
	<option value="0">Select</option>
	<?php 
	$loc=DB_query("Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
	while($loc_row=DB_fetch_array($loc))
	{?>
		<option value="<?php echo $loc_row['loccode'];?>" <?php if($_REQUEST['branch']==$loc_row['loccode']) echo "selected";?>><?php echo $loc_row['locationname'];?></option>
	<?php 	
	}
	?>
	</select>
	</td>
    <td width="13%" align="right"><strong>Party:</strong></td>
    <td width="20%"><?php if($_REQUEST['type']=='sales'){?>
	<select name="client" id="client" style="width:98%">
	<option value="">Select</option>
	<?php 
	$party=DB_query("Select id,client_code,c_name from clientmaster",$db);
	while($party_row=DB_fetch_array($party))
	{?>
		<option value="<?php echo $party_row['id'];?>" <?php if($_REQUEST['client']==$party_row['id']) echo "selected";?>><?php echo $party_row['c_name'];?></option>
	<?php 	
	}
	?>
	</select><?php }else if($_REQUEST['type']=='purchase')
	{?> 
	<select name="vendor" id="vendor" style="width:98%">
	<option value="">Select</option>
	<?php 
	$party=DB_query("Select id,vendor_code,vendor_name from suppliers",$db);
	while($party_row=DB_fetch_array($party))
	{?>
		<option value="<?php echo $party_row['id'];?>" <?php if($_REQUEST['vendor']==$party_row['id']) echo "selected";?>><?php echo $party_row['vendor_name'];?></option>
	<?php 	
	}
	?>
	</select>
	<?php
	}?>
	</td>
	<td width="13%" align="right"><strong>Form Type:</strong></td>
    <td><select  name="form_type" id="form_type" style="width:98%;">
		<option value="">Select</option>
		<?php
			$formSql=DB_query("Select * from form_type_master",$db);
			while($formRow=DB_fetch_array($formSql))
			{?>
				<option value="<?php  echo $formRow['id']?>"<?php echo ($_REQUEST['form_type']==$formRow['id']?'selected':'')?>><?php  echo $formRow['form_name']?></option>
	  <?php }?>
		</select> 
	</td>
	</tr>
	<tr class="row1">
    <td align="right"><strong>From Date:</strong></td>
    <td><input type="text" name="from" id="from" value="<?php echo $_REQUEST['from'];?>" readonly class="datepicker" style="width:98%" /></td>
    <td align="right"><strong>To Date:</strong></td>
    <td><input type="text" name="to" id="to" value="<?php echo $_REQUEST['to'];?>" readonly class="datepicker" style="width:98%" /></td>
    <td colspan="2"></td>
  </tr>
  <tr class="row1">
    <td colspan="6" style="text-align:center"><input type="submit" name="Show" value="Show" class="search" /></td>
  </tr>
</table>
</form>
<form action="" method="post" id="form_id" name="myform">
<?php if($_REQUEST['Show']!='')
{
	  
?>
<table width="100%" cellspacing="0" cellpadding="3" border="0">
	<tr class="row1">
		<td width="35%">Branch: <strong><?php 	if($_REQUEST['branch']) echo $s->getField('locations','loccode',$_REQUEST['branch'],'locationname');?></strong></td>
		<td width="25%">Party: <strong>
			<?php 
				if($_REQUEST['type']=='sales')
				 {
				 	echo $s->getField('clientmaster','id',$_REQUEST['client'],'c_name');
				 }
				 else
				 {
				 	echo $s->getField('suppliers','id',$_REQUEST['vendor'],'vendor_name');
				 }?>
				</strong>
		</td>
		<td width="20%">From : <strong><?php 	if($_REQUEST['from']) echo $_REQUEST['from'];?></strong></td>
		<td>To : <strong><?php if($_REQUEST['to']) echo $_REQUEST['to'];?></strong></td>
</tr>
</table>
<br />
<table width="100%" cellspacing="0" cellpadding="3" border="1" style="border:solid 1px #808080; border-collapse:collapse;">
<tr>
	<th>Branch</th>
	<th width="7%">Date</th>
	<th width="9%">Vch. No./Ref. No.</th>
	<th width="9%">Assessable value</th>
	<th width="9%">CST Amount</th>
	<th width="9%">Invoive Amount</th>
	<th width="10%">Form Type</th>
	<th width="7%"><?php if($_REQUEST['type']=='sales'){echo "Receive Date ";} else {echo "Issue Date";}?> </th>
	<th width="8%">Form No.</th>
	<th width="6%"><?php if($_REQUEST['type']=='sales'){echo "Receive";} else {echo "Issue";}?></th>
</tr>
<?php 

if($_REQUEST['type']=='sales')
{	
	if($_REQUEST['branch'])
			{
				$filter[]="branch_name='".$_REQUEST['branch']."'";
			}
			if($_REQUEST['form_type'])
			{
				$filter[]="formType='".$_REQUEST['form_type']."'";
			}
			if($_REQUEST['client'])
			{
				$filter[]="client_id='".$_REQUEST['client']."'";
			}
			if($_REQUEST['from']!='' && $_REQUEST['to']!='')
			{
				$filter[]="invoiceDate between '".FormatDateFORSQL($_REQUEST['from'])."' and '".FormatDateFORSQL($_REQUEST['to'])."'";
			}
			
			if($filter!='')
			{
				$filter1=implode(" and ",$filter);
				 $invoice="select * from invoice where $filter1 and form_c_status='pending' ";
			}
			else
				$invoice="select * from invoice where form_c_status='pending'  ";

				 
	$invoice_res=DB_query($invoice,$db);
	$assess_array='';
	$cst_array='';
	$total_array='';
	$i=0;
	if(DB_num_rows($invoice_res)>0)
	{
		while($invoice_row=DB_fetch_array($invoice_res))
		{?>
		<tr bgcolor="#FFFFFF">
			<td><?php echo $s->getField('locations','loccode',$invoice_row['branch_name'],'locationname'); ;?></td>
			<td><?php echo ConvertSQLDate($invoice_row['invoiceDate']);?></td>
			<td><?php echo $invoice_row['invoiceNo'];?></td>
			<td style="text-align:right"><?php  $t=$invoice_row['total_assessable_amt'];
			echo ($t!="")?$t:"0.00";
			 $assess_array[]=$t;?> DR</td>
			<td style="text-align:right"><?php $u=$invoice_row['exciseValue']; 
			echo ($u!="")?$u:"0.00";
			$cst_array[]=$u;?> DR</td>
			<td style="text-align:right"><?php  $v=$invoice_row['grandTotal'];
			echo ($v!="")?$v:"0.00";
			 $total_array[]=$v;?> DR</td>
			<td><?php echo $s->getField('form_type_master','id',$invoice_row['formType'],'form_name'); ?></td>
			<td><input style="width:98%" readonly="" type="text" name="indate[<?php echo $i;?>]" value="<?php echo date('d/m/Y') ?>" class="datepicker"></td>
			<td><input style="width:98%" type="text" name="form_c_no[]" id="form_c_no" value="" placeholder="Form No"></td>		
			<td><input type="checkbox" name="checked[<?php echo $i;?>]" id="checked_<?php echo $i;?>" value="" onClick="check(this.value,<?php echo $i;?>)"><input type="hidden" name="inv_check[<?php echo $i;?>]" id="inv_check_<?php echo $i;?>" value="0"><input type="hidden" name="inv_id[<?php echo $i;?>]" value="<?php echo $invoice_row['invoice_id'];?>">
			</td>
		</tr>
<?php $i++;	} 
?>
<input type="hidden" name="count" id="count" value="<?php echo $i;?>">
<tr bgcolor="#fff">
	<td></td>
	<td></td>
	<td style="text-align:right"><strong>Total</strong></td>
	<td style="text-align:right"><?php if(isset($assess_array)){echo round(array_sum($assess_array));}?> DR</td>
	<td style="text-align:right"><?php if(isset($cst_array)){echo round(array_sum($cst_array));}?> DR</td>
	<td style="text-align:right"><?php if(isset($total_array)){echo round(array_sum($total_array));}?> DR</td>
	<td style="text-align:center" colspan="4"></td>
</tr>
<?php 
	} 
	else
	{echo "<tr><td colspan='10' style='ext-align:center'><b>No Record Found</b></td></tr>";}
}
elseif($_REQUEST['type']=='purchase')
{
	if($_REQUEST['branch'])
			{
				$filter[]="site='".$_REQUEST['branch']."'";
			}
			if($_REQUEST['form_type'])
			{
				$filter[]="form_type='".$_REQUEST['form_type']."'";
			}
			if($_REQUEST['vendor'])
			{
				$filter[]="vendor='".$_REQUEST['vendor']."'";
			}
			if($_REQUEST['from']!='' && $_REQUEST['to']!='')
			{
				$filter[]="bill_date between '".FormatDateFORSQL($_REQUEST['from'])."' and '".FormatDateFORSQL($_REQUEST['to'])."'";
			}
			
			if($filter!='')
			{
				$filter1=implode(" and ",$filter);
				 $purchase_bill="select * from purchase_billbook where $filter1 and form_c_status='pending' ";
			}
			else
				$purchase_bill="select * from purchase_billbook where form_c_status='pending'  ";
             // echo $purchase_bill;


	//$purchase_bill="Select * from purchase_billbook where form_type='".$form_id['id']."' and bill_date between '".$from_Sqlformat."' and '".$to_Sqlformat."' and vendor='".$_REQUEST['vendor']."' and form_c_status='pending'";


	$purchase_res=DB_query($purchase_bill,$db);
		$assess_array='';
	$cst_array='';
	$total_array='';
	$i=0;
	if(DB_num_rows($purchase_res)>0)
	{
		while($purchase_row=DB_fetch_array($purchase_res))
		{
		?>
		<tr bgcolor="#FFFFFF">
			<td><?php echo $s->getField('locations','loccode',$purchase_row['site'],'locationname'); ;?></td>
			<td><?php echo ConvertSQLDate($purchase_row['bill_date']);?></td>
			<td><?php echo $purchase_row['bill_book_no'];?></td>
			<td style="text-align:right"><?php $t=$purchase_row['total_item_amount']+$purchase_row['pkg_charges']+$purchase_row['excise_value']+$purchase_row['educess_value']+$purchase_row['she_ecess_value']+$purchase_row['freight'];
			echo ($t!="")?$t:"0.00";
			 $assess_array[]=$t;?> DR</td>
			<td style="text-align:right"><?php $u=$purchase_row['taxvalue']; 
			echo ($u!="")?$u:"0.00";
			$cst_array[]=$u;?> DR</td>
			<td style="text-align:right"><?php $v=$purchase_row['GrandTotal']; 
			echo ($v!="")?$v:"0.00";
			$total_array[]=$v;?> DR</td>
			<td><?php echo $s->getField('form_type_master','id',$purchase_row['form_type'],'form_name'); ?></td>
			<td><input style="width:98%" readonly="" type="text" name="indate[<?php echo $i;?>]" value="<?php echo date('d/m/Y') ?>" class="datepicker"></td>
			<td><input style="width:98%;" type="text" name="form_c_no[]"  placeholder="Form No" id="form_c_no" value=""></td>
			<td><input type="checkbox" name="" id="checked_<?php echo $i;?>" value="" onClick="check(this.value,<?php echo $i;?>)"><input type="hidden" name="inv_check[<?php echo $i;?>]" id="inv_check_<?php echo $i;?>" value="0"><input type="hidden" name="bill_id[<?php echo $i;?>]" value="<?php echo $purchase_row['id'];?>"></td>
		</tr>
<?php 	$i++;} 
?>
<input type="hidden" name="count" id="count" value="<?php echo $i;?>">
<tr bgcolor="#fff">
<td></td>
<td style="text-align:center"></td>
<td style="text-align:right"><strong>Total</strong></td>
<td style="text-align:right"><strong><?php if(isset($assess_array)){echo round(array_sum($assess_array));}?> DR</strong></td>
<td style="text-align:right"><strong><?php if(isset($cst_array)){echo round(array_sum($cst_array));}?> DR</strong></td>
<td style="text-align:right"><strong><?php if(isset($total_array)){echo round(array_sum($total_array));}?> DR</strong></td>
<td colspan="4"></td>

</tr>
<?php 
	} 
	else
	{echo "<tr><td colspan='10' style='text-align:center'><b>No Record Found</b></td></tr>";}
}
?>
</table> 	
<p align="center"><input type="submit" name="Save" value="Save" class="search" /></p>
<?php
}
?>

</form>
</div>
</div>
<script language="javascript" type="text/javascript">
function check(str,i)
{

	var check_stat=document.getElementById("checked_"+i).checked;
	if(check_stat==true)
	{
		document.getElementById("inv_check_"+i).value='1';
	}
	else
	{
		document.getElementById("inv_check_"+i).value='0';
	}
}
</script>