<?php
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
?>
<link rel="stylesheet" href="css/purchase_requision_print.css">
<div class="body_contain">
<div class="tab_div">
<div>
<form action="" method="post">
<table width="100%" border="0">
  <tr>
    <td align="center"><strong>Reason</strong></td>
  </tr>
  <tr>
    <td><textarea name="reason" id="reason" style="width:100%"></textarea></td>
  </tr>
  <tr>
    <td align="center"><input type="submit" name="Cancel" value="Proceed" onclick="return validate();" /></td>
  </tr>
</table>
</form>
    

  
</div>
<?php
//echo $_REQUEST['id'];

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

       $sql_get_trans_id="select total_cramount,trans_type_id,VPrefix from account_book where id='".$_REQUEST['id']."' ";
        $result_get_trans_id=DB_query($sql_get_trans_id,$db);
        $rs_get_trans_id=DB_fetch_array($result_get_trans_id);
       $voucher_id=$rs_get_trans_id[2];
       

    
    if($voucher_id=='3' ||$voucher_id=='5')
    {
       $sql_account_invoice_billbook="select * from account_invoice_billbook where account_book_id='".$_REQUEST['id']."' ";
       $result_account_invoice_billbook=DB_query($sql_account_invoice_billbook,$db); 
         while($rs_account_invoice_billbook=DB_fetch_array($result_account_invoice_billbook))
         {
            

               if($rs_account_invoice_billbook['Billtype']=='INV')
               {
              
                 
                 DB_query("update invoice set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',invoice_status='Pending' where invoice_id='".$rs_account_invoice_billbook['invoice_billbookid']."'",$db);
               }
               else
               {
               
                 DB_query("update clientmaster_bill_information set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',invoice_status='Pending' where id='".$rs_account_invoice_billbook['invoice_billbookid']."'",$db);
               }
         }        
	  }
    else if($voucher_id=='4' ||$voucher_id=='6')
    {
      $sql_account_invoice_billbook="select * from account_purchase_billbook where account_book_id='".$_REQUEST['id']."' ";
       $result_account_invoice_billbook=DB_query($sql_account_invoice_billbook,$db); 
         while($rs_account_invoice_billbook=DB_fetch_array($result_account_invoice_billbook))
         {
            

               if($rs_account_invoice_billbook['Billtype']=='PUR')
               {
              

                 DB_query("update purchase_billbook set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',bill_status='Pending' where id='".$rs_account_invoice_billbook['purchase_billbookid']."'",$db);
               }
              
         }  
    }     
    else if($voucher_id=='8')
        {
         
               $sql_account_invoice_billbook="select * from account_purchase_billbook where account_book_id='".$_REQUEST['id']."' ";
              $result_account_invoice_billbook=DB_query($sql_account_invoice_billbook,$db);
              $count=DB_num_rows($result_account_invoice_billbook);
              
                 
             if($count>0)
             {    
                   while($rs_account_invoice_billbook=DB_fetch_array($result_account_invoice_billbook))
                   {
                      
                      if($rs_account_invoice_billbook['Billtype']=='PUR')
                         {
                        
                          DB_query("update purchase_billbook set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',bill_status='Pending' where id='".$rs_account_invoice_billbook['purchase_billbookid']."'",$db);
                         }
                        
                    }
              }
              else
              {
                 $sql_account_invoice_billbook="select * from account_invoice_billbook where account_book_id='".$_REQUEST['id']."' ";
                 $result_account_invoice_billbook=DB_query($sql_account_invoice_billbook,$db); 
                     while($rs_account_invoice_billbook=DB_fetch_array($result_account_invoice_billbook))
                     {
                        

                           if($rs_account_invoice_billbook['Billtype']=='INV')
                           {
                          

                             DB_query("update invoice set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',invoice_status='Pending' where invoice_id='".$rs_account_invoice_billbook['invoice_billbookid']."'",$db);
                           }
                           else
                           {
                             DB_query("update clientmaster_bill_information set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',invoice_status='Pending' where id='".$rs_account_invoice_billbook['invoice_billbookid']."'",$db);
                           }
                     }        

            }    
    }
     else if($voucher_id=='11')
    {
       $sql_account_invoice_billbook="select * from account_invoice_billbook where account_book_id='".$_REQUEST['id']."' ";
       $result_account_invoice_billbook=DB_query($sql_account_invoice_billbook,$db); 
         while($rs_account_invoice_billbook=DB_fetch_array($result_account_invoice_billbook))
         {
            

               if($rs_account_invoice_billbook['Billtype']=='INV')
               {
              
                 
                 DB_query("update invoice set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',invoice_status='Pending' where invoice_id='".$rs_account_invoice_billbook['invoice_billbookid']."'",$db);
               }
               else
               {
               
                 DB_query("update clientmaster_bill_information set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',invoice_status='Pending' where id='".$rs_account_invoice_billbook['invoice_billbookid']."'",$db);
               }
         }        
    }  
    else if($voucher_id=='12')
    {
       $sql_account_invoice_billbook="select * from account_purchase_billbook where account_book_id='".$_REQUEST['id']."' ";
       $result_account_invoice_billbook=DB_query($sql_account_invoice_billbook,$db); 
         while($rs_account_invoice_billbook=DB_fetch_array($result_account_invoice_billbook))
         {
            

               if($rs_account_invoice_billbook['Billtype']=='PUR')
               {
              

                 DB_query("update purchase_billbook set pending_amount=pending_amount+'".$rs_account_invoice_billbook['adjustment']."',bill_status='Pending' where id='".$rs_account_invoice_billbook['purchase_billbookid']."'",$db);
               }
        }  
    }           
   
	$data['cancel_status']='1';
	$data['cancel_reason']=$_REQUEST['reason'];
	$cond="id='".$_REQUEST['id']."'";
	$s->RecordEdit('account_book',$data,$cond);
echo "<script>parent.window.close();
parent.window.opener.location.href='index.php?route=Account/ACBillbookPayment&modid=".$_REQUEST['modid'].                      "&frmid=".$_REQUEST['frmid']."&VPrefix=".$_REQUEST['VPrefix']."&daction=view'</script>";			
				}
?>
<script type="text/javascript">
  function validate()
  {
    if(document.getElementById("reason").value=='')
    {
       alert("Please Enter Reason");
       return false;
    }   
  }

</script>