<?php  error_reporting(0);
$PageSecurity = 3;
  $modid=$_REQUEST['modid'];
  $frmid=$_REQUEST['frmid'];
  $rurl="&modid=".$modid."&frmid=".$frmid;
     $cnp=$_REQUEST['cnp'];
     $site=$_REQUEST['site'];
  //echo $_SERVER['PHP_SELF'];
 include ('includes/session.inc');
  include ('includes/function_lib.php');
 global $db;
   echo '<link href="'.$rootpath. '/css/'. $_SESSION['Theme'] .'/default.css" rel="stylesheet" type="text/css" />';
 if($_REQUEST['id']=='')
    $_SESSION['ACGCurrentREF']=$_SERVER['HTTP_REFERER'];
   
   //echo  $_SESSION['ACGCurrentREF'];
   $GETAccountLedgerDetails="SELECT AccountsID,AccountName ,  UnderGroup ,ledgertype,vender_client
     FROM accountledger  where AccountsID='".$cnp."' ";
    $Result_Ledtails=DB_query($GETAccountLedgerDetails,$db);
  $RowLedtails=DB_fetch_array($Result_Ledtails);
  $AccountName=$RowLedtails['AccountName'];
  $UnderGroup=$RowLedtails['UnderGroup'];
  $ledgertype=$RowLedtails['ledgertype']; 
  $vendorid=$RowLedtails['vender_client']; 
   if($ledgertype=='client')
   $vendercode=$s->getField('clientmaster','id',$RowLedtails['vender_client'],'short_name');
   else
   $vendercode=$s->getField('suppliers','id',$RowLedtails['vender_client'],'short_name') ;  
    
    
 ?>
 <link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1><?php echo $fromtitle=  $s->getField('voucher_master','VoucherType',$_REQUEST['VPrefix'],'GroupName');?></h1>
 <?php 
   if($_REQUEST['id']!='')
   {
   // if($_REQUEST['table']=='purchase_billbook')
   //  $query1="select  bill_book_no,bill_no,bill_book_date,GrandTotal,bill_status,pending_amount,'purchase_billbook' as tablename 
 // from purchase_billbook where id='".$_REQUEST['id']."'";
    if($_REQUEST['table']=='account_book')
    {
      $query1="select A.pending_amount as pending_amount,A.bill_status as bill_status,A.Voucher_NO AS bill_book_no,A.Dates AS bill_book_date,B.amount as GrandTotal,'account_book' as tablename ,A.Ref_no as bill_no,A.trans_type_id as trans_type_id 
        FROM  account_book A,account_book_details B
  WHERE A.id=B.account_bookid and  A.id='".$_REQUEST['id']."' AND B.ledgerid='".$_REQUEST['cnp']."' and B.amount_type='DR'";  
    }
    else if($_REQUEST['table']=='account_fund_receipt')
    {
       $query1="select A.pending_amount as pending_amount,A.bill_status as bill_status,A.voucher_no AS bill_book_no,A.voucher_date AS bill_book_date,A.funding_amount as GrandTotal,'account_fund_receipt' as tablename ,A.budget_id as bill_no,null as trans_type_id 
        FROM  account_fund_receipt A
  WHERE   A.id='".$_REQUEST['id']."' AND A.revenue_head='".$_REQUEST['cnp']."' and type='Receipt' ";

    }
   // echo $query1;
      $result1=DB_query($query1,$db);
     $myrow1=DB_fetch_array($result1) ;
      $billno=$myrow1['bill_book_no'];
    // $bill_no1=$myrow1['bill_no'];
      $bill_date=$myrow1['bill_book_date'];
       $grand_total=$myrow1['GrandTotal'];
       
       if($myrow1['bill_status']=='Pending' && $myrow1['pending_amount']=='0.00')
       {$pending_amount=$myrow1['GrandTotal'];}
       else{
       $pending_amount=$myrow1['pending_amount'];}
       
      //  $billno=$myrow1['bill_book_no'];
        $tablename=$myrow1['tablename'];
        if($myrow1['trans_type_id']!='')
        $bill_no1=$s->getField('invoice','invoice_id',$myrow1['trans_type_id'],'partyPO');
        else
          $bill_no1=$myrow1['bill_no'];
     
   }
   if($_REQUEST['seq']==0)
   {
    unset($_SESSION['adjamt1']);
    unset($_SESSION['adjamt']);
   }

   if($_REQUEST['Submit'])
   {
    $table=$_REQUEST['tablename'];  
   // $table="purchase_billbook";  
    $adjustment=$_REQUEST['adjust'];
    $_SESSION['adjamt1'][]=$adjustment;
  $dataArray['pending_amount']=$_REQUEST['grand_total']-$adjustment;
   if($dataArray['pending_amount']=='0.00')
    $dataArray['bill_status']='Done';
   
  
  $cond="id='".$_REQUEST['id']."'";
  
  //$s->RecordEdit('invoice', $dataArray, $cond);
  
    $_SESSION['adjamt'][$_REQUEST['seq']]=$_REQUEST['adjust'];
      
    $sql1="#update $table set ";
    $i=0;
    foreach($dataArray as $key => $val)
      {
      if($i==0)
      $sql1.="".$key."='".$val."'";
      else
      $sql1.=",".$key."='".$val."'";      
      $i++;
      }
        $sql1 .=" where  $cond" ;
   
    $_SESSION['sql1']=$_SESSION['sql1'].$sql1;
   //$s->RecordEdit('account_book_details', $dataArray, $cond);
  
   
   $data['Billtype']='INV';
   $data['invoice_billbookid']=$_REQUEST['id'];
   $data['grand_total']=$pending_amount;
   $data['adjustment']=$_REQUEST['adjust'];
   $data['pending_amount']=$grandTotal-$adjustment;
   $data['posted_date']=date('Y-m-d');   
   $data['status']=0;
   
    $sql="#Insert into account_invoice_billbook set ";
    $i=0;
    foreach($data as $key => $val)
      {
      if($i==0)
      $sql.="".$key."='".$val."'";
      else
      $sql.=",".$key."='".$val."'";     
      $i++;
      }
  
   $_SESSION['sql2']=$_SESSION['sql2'].$sql;
   //$_SESSION['sql2']=$_SESSION['sql2'].$sql;
    
   ?>
    <script language='javascript'>
  //self.close ();
  //opener.window.location="<?php  //echo $_SESSION['ACGCurrentREF'];?>&amount=<?php //echo $adjustment;?>&cnp=<?php  //echo $cnp?>";self.close(); 
    //opener.window.location="<?php  //echo $_SESSION['ACGCurrentREF'];?>&amount=<?php //echo $adjustment;?>&cnp=<?php  //echo $cnp?>";self.close(); 
  </script>

   <?php 
   
   }
 ?>
 <script type="text/javascript">
 function show()
 {  

var id=document.getElementById("rowid").value;

if(id==0){ var docid="totalcrconvPrice";} else {var docid="totalcrconvPrice"+'_'+id;}
//window.opener.document.getElementById(docid).value=document.getElementById("adjust").value; 
//window.opener.document.getElementById(docid).focus();
var vendor=document.getElementById("vendorcode").value;

var billno=window.opener.document.getElementById("tot_bil_no").value;
if(billno=='')
{
  var billno1=document.getElementById("bill_no1").value;
}
else
{
  var billno1=billno+','+document.getElementById("bill_no1").value;
}

var billno2="Payment Made against bill No:"+vendor+'-'+billno1;
window.opener.document.getElementById("tot_bil_no").value=billno1;
window.opener.document.getElementById("narration").value=billno2;
if(document.getElementById("tablename").value=='account_fund_receipt')
{
  var cnpid=document.getElementById("tranid").value;
window.opener.document.getElementById("transfer_type_id").value=cnpid;
window.opener.document.getElementById("transfer_type").value='Fund';
}
}
function show1()
 {
  var id=document.getElementById("rowid").value;

if(id==0){ var docid="totalcrconvPrice";} else {var docid="totalcrconvPrice"+'_'+id;}
if(document.getElementById("totaladjustamt").value!="")
{
 window.opener.document.getElementById(docid).value=document.getElementById("totaladjustamt").value; 
window.opener.document.getElementById(docid).focus();
}
self.close();
 }


function validate(str)
{
var grdamt=document.getElementById("grand_total").value;
if(parseFloat(grdamt)<parseFloat(str))
{
  alert("Adjustment amt should be less or equal then Pending Amt");
  document.getElementById("adjust").value=document.getElementById("grand_total").value;
  document.getElementById("adjust").focus();
  return false;
}
return true;


}
 </script>
 <form action="" method="post">
 
 <table width="100%" border="1" cellpadding="3" cellspacing="0" style="border:#808080 1px solid; border-collapse:collapse;">
  <tr>
    <th>Bill Book No.</th>
    <th width="18%">Bill Date</th>  
    <th width="18%">Pend. Amount</th>
    <th width="18%">Adjust. Amount</th>
    <th width="8%">&nbsp;</th>
  </tr>
  <tr bgcolor="#fff">
        <input type="hidden" name="vendorcode" id="vendorcode" value="<?php echo $AccountName; ?>">
        <input type="hidden" name="tablename" id="tablename" value="<?php echo $tablename; ?>">
    <td><input type="hidden" name="rowid" id="rowid" value="<?php echo $_REQUEST['rowid']; ?>">  
        <input type="hidden" name="tranid" id="tranid" value="<?php echo $_REQUEST['id']; ?>">  
        <input name="bill_no" type="text" id="bill_no1"  value="<?php echo $billno; ?>"style="width:98%;" />
    </td>
    <td><input name="bill_date" type="text" value="<?php echo $bill_date ?>"style="width:98%;" /></td>
        <input type="hidden" name="seq" id="seq" value="<?php echo $_REQUEST['seq'];?>">
    <td><input name="grand_total" type="text" id="grand_total" value="<?php echo $pending_amount ?>"style="width:98%;" /></td>
    <td><input name="adjust" id="adjust" type="text" value="<?php echo $pending_amount ?>" onkeyup="return validate(this.value);"style="width:98%;" /></td>
    <td><input name="Submit" type="submit" value="Go" class="search" onclick="show();" /></td>
  </tr>
</table>
</form>
 
 <table width="100%" border="1" cellpadding="3" cellspacing="0" style="border:#808080 1px solid; border-collapse:collapse;">
  <tr>
    <th>Bill Book No.</th>
    <th width="13%">Bill Date</th>   
    <th width="13%">Amount</th>
    <th width="13%">Pending Amount</th>
    <th width="13%">Adjust Amt</th>
    <th width="8%">Action</th>
  </tr>
<?php    
//select S1.id as vendor_code,S2.pending_amount as  pending_amount,S2.bill_status as bill_status,S2.bill_book_no as bill_book_no,S2.bill_book_date as bill_book_date,S2.bill_no as bill_no,
        //   S2.GrandTotal as GrandTotal,S2.id as id,'purchase_billbook' as table1   from suppliers S1,purchase_billbook S2  where 
   //S1.id=S2.vendor  and S1.id='".$vendorid."' 
   //and S1.account_group='".$UnderGroup."' and FIND_IN_SET('".$site."',S1.site) and S2.bill_status='Pending'
     //union

  $query="SELECT B.ledgerid as vendor_code ,A.pending_amount as pending_amount,A.bill_status as bill_status,A.Voucher_NO AS bill_book_no
    ,A.Dates AS bill_book_date,A.Ref_no AS bill_no,B.amount as GrandTotal,A.id AS id,'account_book' as table1,A.trans_type_id 
    as trans_type_id FROM  account_book A,account_book_details B   WHERE A.id=B.account_bookid AND A.Branch='".$site."'
     AND B.ledgerid='".$cnp."' AND  A.trans_type='INV'  and B.amount_type='DR' and A.bill_status='Pending' 
     union
     select funding_by as vendor_code,pending_amount as pending_amount,bill_status as bill_status,voucher_no as
      bill_book_no,voucher_date as bill_book_date,budget_id as bill_no,funding_amount as GrandTotal,id 
      as id,'account_fund_receipt' as table1,null as trans_type_id from account_fund_receipt where bill_status='Pending' 
      and branch='".$site."' and type='Receipt' and revenue_head='".$cnp."' ";

      $result=DB_query($query,$db);
    $i=1;
    // echo (DB_num_rows($result));
    if(DB_num_rows($result)>0)
    {
      while($myrow=DB_fetch_array($result))
      {
       // if($myrow['pending_amount']!=0){
      ?>
    <tr bgcolor="#fff">
    <td><?php  echo $myrow['bill_book_no']?></td>
    <td><?php  echo $myrow['bill_book_date']?></td>  
    <td><?php  echo $myrow['GrandTotal']?></td>
  <td><?php  
  if($myrow['bill_status']=='Pending' && $myrow['pending_amount']=='0.00')
  {echo $myrow['GrandTotal'];}
  else
  {echo $myrow['pending_amount'];}?></td>
    <td><?php echo $_SESSION['adjamt'][$i]; ?></td>
  <td><a href="<?php echo 'ACInvoiceDetails.php?cnp='.$cnp.'&seq='.$i.'&VPrefix='.$_REQUEST['VPrefix'].'&id='.$myrow['id'].'&table='.$myrow['table1'].'&rowid='.$_REQUEST['rowid'].'&site='.$_REQUEST['site']?>">PayAmount</a></td>  
  </tr> <!-- ACBillBookDetails.php?cnp=14&VPrefix=6-->
  <?php //}
 $i++; } }  ?>  <?php //echo $_SERVER['REQUEST_URI'].'&id='.$myrow['id']?>
 <tr bgcolor="#fff">
 <td colspan="3">&nbsp;</td>
 <td><strong>Total Amt.</strong></td>
 <td><input type="text" name="" readonly id="totaladjustamt" value="<?php if(!empty($_SESSION['adjamt1'])) echo array_sum($_SESSION['adjamt1']);?>"style="width:98%;" /></td>
 <td><input name="button" type="submit" value="Exit" class="search" onclick="show1();" /></td>
 </tr>
</table>

