<?php 
$PageSecurity = 1;
include('includes/DefinePaymentClass.php');
$title = _('Invoice');
include('includes/Class_Client.php');
include('includes/SQL_CommonFunctions.inc');
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmAdd']=$frmpermission[0];
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];
$_SESSION['frmView']=$frmpermission[3];

?>
<div style="width:800px; margin-left:auto; margin-right:auto">
<fieldset>
<legend>View Trading Invoice </legend>
<?php if($_SESSION['frmAdd']=='1'){?>
<div align="center"><a href="index.php?route=trading_invoice&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=addnew">ADD NEW INVOICE</a></div><?php } ?> 
<br />
<?php
echo $_REQUEST['id'];
if($_REQUEST['action']=='delete')
{
   //$SqlDis=DB_query("select partyPO,formNo from invoice where invoice_id='".$_REQUEST['id']."'",$db);
  // $SqlDisRow=DB_fetch_array($SqlDis);
   //$updateDispatchStat=DB_query("Update dispatch set status='0' where id='".$SqlDisRow['partyPO']."'",$db);
//$updateFormStatus=DB_query("Update party_form_master set invoice_status='0' where id='".$_REQUEST['form_no']."'",$db);



   $delsql = "delete from invoice where invoice_id='".$_REQUEST['id']."'";
   $delete_result = DB_query($delsql,$db);
   $delsql22 = "delete from  invoice_details where invoiceid='".$_REQUEST['id']."'";
   $delete_result22 = DB_query($delsql22,$db);
    if(!$delete_result)
  {
    die("error".DB_error_msg($db));
   }
   $s->pageLocation('index.php?route=ViewInvoice&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
 }

 if($_REQUEST['daction'])
{?>
<div class="success">Record <?php echo $_REQUEST['daction'] ?> Successfully </div><br>
<?php }?>
<form action="" method="Post">
<table width="60%" border="0" cellpadding="2" cellspacing="2" align="center" style="background-color: #FFFFFF; border: solid 1px #CCC">
<tr><th nowrap="nowrap">Branch Wise</th>
	<td width="25%"><select name="branch_id" style="width: 100%; height: 100%">
	<option value="000">Select Branch</option>
    	<?php $site_sql=DB_query("Select prefix, loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$_SESSION['AssignedBranch']."'",$db);
    	while($site_row=DB_fetch_array($site_sql))
		{?>
			<option value="<?php echo $site_row['prefix'];?>"<?php echo($_REQUEST['branch_id']==$site_row['prefix']?'selected':'')?>><?php echo $site_row['locationname'];?></option>
		<?php 	
		}
    	?></select></td>
    	<th nowrap="nowrap">Party Wise</th>
    	<td>
	<?php
			$arr_con_acc=array();
$con_accountSql=DB_query("Select Convert_AccountId from crm_leads where deleted_status='N' and Convert_IntoAccountStatus='Y'",$db);
if(DB_num_rows($con_accountSql)>0)
{
while($row_Conacc=DB_fetch_array($con_accountSql))
{
$arr_con_acc[]=$row_Conacc['Convert_AccountId'];
}
$x="'";
if(count($arr_con_acc)>0)
{
$x.=implode("', '",$arr_con_acc)."'";
}

$AccountCompany= "SELECT * FROM accountledger WHERE  ledgertype IN('client','leads') or AccountsID IN ($x)";
$Account_sql=DB_query($AccountCompany,$db);
}
?>
<select name="party_id" id="party_id" style="width: 100%">
<option value="0">Select</option>
<?php

while($Account_row=DB_fetch_array($Account_sql))
{
 if($Account_row['ledgertype']=='client'){
	  	 $ClientSQL="Select Site,Zone from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site='".$_SESSION['AssignedBranch']."' 
		 and id='".$Account_row['vender_client']."' and  Zone in(".$_SESSION['AssignedSites'].") ";
		 }
		 else  if($Account_row['ledgertype']=='leads')
		 {
		 $ClientSQL="Select Site,Zone from clientmaster where CompanyId='".$_SESSION['ACGCompanyId']."' and  Site='".$_SESSION['AssignedBranch']."' 
		 and id='".$Account_row['AccountName']."' and  Zone in(".$_SESSION['AssignedSites'].") ";
 		 }

?>
<option value="<?php echo $Account_row['vender_client']?>"<?php echo($_REQUEST['party']==$Account_row['AccountsID'] ? 'selected':'');?>><?php 

if($Account_row['ledgertype']=='leads')
		echo $s->GetFieldValue('clientmaster',"id='".$Account_row['AccountName']."'",'c_name');
		else 
	 

echo $Account_row['AccountName']?></option>

<?php 	
}
?>
</select>

		
		
		</td>
    	<th><input type="submit" name="Search" value="Search" /></th>	
</tr></table>	
</form>
<?php 
if(isset($_POST['Search']))
{
 if($_REQUEST['branch_id']!='000')
{
 $cond="branch_name='".$_REQUEST['branch_id']."' and CompanyId='".$_SESSION['ACGCompanyId']."'  and invoiceType='Trading'";
$GetRecord=$s->GetRecord("invoice",$cond);	
}
if($_REQUEST['party_id']!='000') 
{
$cond="client_id='".$_REQUEST['party_id']."' and CompanyId='".$_SESSION['ACGCompanyId']."' and invoiceType='Trading'";
$GetRecord=$s->GetRecord("invoice",$cond);		
}
if($_REQUEST['branch_id']!='000' and $_REQUEST['party_id']!='000')
{
  $cond="client_id='".$_REQUEST['party_id']."' and  branch_name='".$_REQUEST['branch_id']."' and CompanyId='".$_SESSION['ACGCompanyId']."' and invoiceType='Trading'";
$GetRecord=$s->GetRecord("invoice",$cond);	
}



   $result=DB_query($GetRecord,$db);
  ?>
  
 <table width="100%" border="0" cellspacing="2" cellpadding="2" >
  <tr >
    <th>SNo.</th>
	 <th>Invoice Number</th>
    <th>Invoice Date</th>
   <th>Party</th><th>Branch Name</th>
   <th>View</th>
    <th>Edit</th>
    <th>Delete</th>
    <th>Print</th>
  </tr>
  <?php $i=0;
  $num=DB_num_rows($result);

if($num>0)
{
 while( $row=DB_fetch_array($result))
 {
   ?>
  <tr bgcolor="#FFFFFF">
    <td><?php echo $i+1;?></td>
     <td><?php echo $row['invoiceNo'];?></td>
	<td><?php echo  ConvertSQLDate($row['invoiceDate']); ?></td>
    <td><?php 
	echo $s->getField('clientmaster','id', $row['client_id'],'c_name')
	 ?></td>
	<td><?php echo  $s->getField('locations','prefix', $row['branch_name'],'locationname');
	 ?></td>
	  <td><?php if($_SESSION['frmView']=='1'){?>	  <!--<a href="#"  onclick="popup('InvoiceDetails.php?InvoiceId=<?php// echo $row['invoice_id']?>',700,500)" >-->
	  <a href="index.php?route=InvoiceDetails&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=view&InvoiceId=<?php echo $row['invoice_id']?>">
	 <div align="center"> <img src="images/search.gif"  width="10" height="10"  ></div></a><?php }else{echo "No Permission";}?></td>
     <td><?php if($_SESSION['frmEdit']=='1'){?><a href="index.php?route=invoice&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&id=<?php echo $row['invoice_id']?>"><img src="images/icon-edit.png"  width="10" height="10"  ></a><?php }else{echo "No Permission";}?></td>
	<td><?php if($_SESSION['frmDelete']=='1'){?><a href="index.php?route=ViewInvoice&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=delete&id=<?php echo $row['invoice_id']?>" onclick="return confirm('Do You Want To Delete This Record')" width="10" height="10"><img src="images/Icon_Delete.png" width="10" height="10"></a><?php } else{echo "No Permission";}?></td>
  <td>
	<a href="#" onclick="popup('InvoiceDetails-print.php?InvoiceId=<?php echo $row['invoice_id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&print=Buyer',1200,600)">1. Original For Buyer</a><br/>
	
	
  	<a href="#" onclick="popup('InvoiceDetails-print.php?InvoiceId=<?php echo $row['invoice_id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&print=Transporter',1200,600)">2. Duplicate For Transporter</a><br>
	
	
  	<a href="#" onclick="popup('InvoiceDetails-print.php?InvoiceId=<?php echo $row['invoice_id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&print=Assessee',1200,600)">3. Triplicate For Assessee</a><br>
	
	
  <a href="#" onclick="popup('InvoiceDetails-print.php?InvoiceId=<?php echo $row['invoice_id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&print=Quarduplicate',1200,600)">4. Quarduplicate</a>
  	</td>
  
  </tr>
 <?php $i++;} } else { ?>
 	  <tr bgcolor="#FFFFFF"><td colspan="9">Sorry No records</td></tr><?php } }?> 
</table>

<?php
include('includes/footer.inc');
?>
</fieldset>
</div>