<?php //error_reporting(0);

echo '<link href="'.$rootpath. '/css/'. $_SESSION['Theme'] .'/default.css" rel="stylesheet" type="text/css" />';
//echo "Select * from companies where CompanyId='".$_SESSION['ACGCompanyId']."'";
$get_financial_yr=DB_query("Select * from companies where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
$result=DB_fetch_array($get_financial_yr);

$fin_year_start=$result['AccountingYearStart'];
$fin_year_end=$result['AccountingYearEnd'];

$fin_year_start_brk=explode('-',$fin_year_start);
$year1=$fin_year_start_brk[0];
$fin_year_end_brk=explode('-',$fin_year_end);
$year2=$fin_year_end_brk[0];

$month_array=array(4=>'April',5=>'May',6=>'June',7=>'July',8=>'August',9=>'September',10=>'October',11=>'November',12=>'December',1=>'January',2=>'February',3=>'March',);
//print_r($month_array);
?>
<?php if(empty($_REQUEST['m']) && empty($_REQUEST['y'])){?>
<h2 align="center"><strong>Sale Register Month Wise</strong></h2>
<table width="100%" border="0" cellspacing="2" cellpadding="2" style="border:solid #666666">
<tr bgcolor="#CCCCCC">
<td>&nbsp;</td>
<td><div align="right"><strong>Debit</strong></div></td>
<td><div align="right"><strong>Credit</strong></div></td>
<td><div align="right"><strong>Closing</strong></div></td>
</tr>
<?php 
$array_CR=array();
$array_DR=array();
$array_CR_closing=array();
$array_DR_closing=array();
foreach($month_array as $key=>$val)
{
	if($key>=4 && $key<=12){$yr=$year1;}elseif($key>=1 && $key<=3){$yr=$year2;}
	if($key>=1 && $key<=9){$month="0".$key;}else{$month=$key;}
	
	$total_CR=0;
	$total_CR1=0;
	$total_DR=0;
	
	$get_month_total="Select * from invoice where  MONTH(invoiceDate)='".$month."' and YEAR(invoiceDate)='".$yr."'";
	$get_total_result=DB_query($get_month_total,$db);
	while($get_total=DB_fetch_array($get_total_result))
	{
		
	$sql=DB_query("select * from invoice_details where invoiceid='".$get_total['invoice_id']."'",$db);
     while($row=DB_fetch_array($sql))
     {	
	 $total_CR1=$total_CR1+$row['Amount'];
			
	}
	
	}
	$total_CR=$total_CR+$total_CR1;; 
?> 
  <tr bgcolor="#CCCCCC">
    <td><a href="index.php?route=Account/saleregister_report&modid=<?php echo $_REQUEST['modid']; ?>&frmid=<?php echo $_REQUEST['frmid']; ?>&m=<?php echo $key;?>&y=<?php echo $yr;?>&mwords=<?php echo $val;?>"><strong><?php echo $val; echo ", ".$yr;?></strong></a></td>
    <td><div align="right"><strong><?php echo $total_DR; $array_DR[]=$total_DR;?></strong></div></td>
    <td><div align="right"><strong><?php echo $total_CR; $array_CR[]=$total_CR;?></strong></div></td>
    <td><div align="right"><strong>
      <?php if($total_DR>$total_CR){echo $closing_DR=$total_DR-$total_CR." DR"; $array_DR_closing[]=$closing_DR;}elseif($total_CR>$total_DR){echo $closing_CR=$total_CR-$total_DR." CR"; $array_CR_closing[]=$closing_CR;}?>
    </strong></div></td>
  </tr>
  <?php } ?>
  <tr bgcolor="#CCCCCC">
    <td><div align="right"><strong>Grand Total</strong></div></td>
    <td><div align="right"><strong><?php echo array_sum($array_DR);//print_r($array_DR); ?></strong></div></td>
    <td><div align="right"><strong><?php echo array_sum($array_CR);//print_r($array_CR);?></strong></div></td>
    <td><div align="right"><strong><?php 
	//print_r($array_DR_closing); print_r($array_CR_closing);
	$sum_DR=array_sum($array_DR_closing); 
	$sum_CR=array_sum($array_CR_closing);
	if($sum_DR>$sum_CR){echo $sum_DR-$sum_CR." DR";}elseif($sum_CR>$sum_DR){echo $sum_CR-$sum_DR." CR";}
	{
	?></strong></div></td>
  </tr>
</table>

<?php }	
	
	 } ?>

<?php
if($_REQUEST['m']!='' && $_REQUEST['y']!='')
{
	
	 
	 
	if($_REQUEST['m']>=1 && $_REQUEST['m']<=9){$month="0".$_REQUEST['m'];}else{$month=$_REQUEST['m'];}
	$yr=$_REQUEST['y'];
	
	//echo $get_transactions;
?>
<p align="right"><a href="javascript:history.go(-1);"><input type="button" value="Go Back" /></a></p>
<h2 align="center"><strong>Sale Register Report for the month of <?php echo $_REQUEST['mwords'];?></strong></h2>
<table width="98%" border="0" cellspacing="1" cellpadding="1" align="center" style="border:solid 1px #999999">
<tr bgcolor="#999999">
<td width="15%"><strong>Date</strong></td>
<td width="20%"><strong>Particulars</strong></td>
<td width="20%"><strong>Voucher No </strong></td>
<td width="10%"><strong>Voucher Type </strong></td>
<td width="20%"><strong>Debit Amount(INR)</strong></td>
<td width="20%"><strong>Credit Amount(INR).</strong></td>
<td  width="5%"><strong>View</strong></td>
</tr>
<?php 
$DR_arr='';
$CR_arr='';
 $get_transactions="Select *  from invoice  where  MONTH(invoiceDate)='".$month."' and YEAR(invoiceDate)='".$yr."' order by invoiceDate asc ";
	$get_transactions_res=DB_query($get_transactions,$db);
	while($row2=DB_fetch_array($get_transactions_res))
	{

$amount=0;
$totalamount=0;
$sql=DB_query("select * from invoice_details where invoiceid='".$row2['invoice_id']."'",$db);
while($row=DB_fetch_array($sql)){

//$sql="Select ledgerid from account_book_details where account_bookid='".$account_book_id."' and amount_type='CR'";
//$ledger_res=DB_query($sql,$db);
//$ledger_row=DB_fetch_array($ledger_res);




 $amount=$amount+$row['Amount'];
 
}
$DR_amount=$amount;
 $Ledger_name=$s->GetFieldValue('clientmaster','id="'.$row2['client_id'].'"','c_name');
 if($DR_amount=='')
 {
 $DR_amount=0;	
 }
 $CR_amount=0;
 
 //else{$DR_amount='';}
?>
<tr bgcolor="#CCCCCC">
<td width="10%"><strong><?php echo ConvertSQLDate($row2['invoiceDate']);?></strong></td>
<td width="20%"><strong><?php echo $Ledger_name;?></strong></td>
<td width="20%"><strong><?php echo $row2['invoiceNo'];?></strong></td>
<td width="10%"><strong>Sales</strong></td>
<td width="20%"><strong><?php echo $DR_amount; $DR_arr[]=$DR_amount;?></strong></td>
<td width="20%"><strong><?php echo $CR_amount; $CR_arr[]=$CR_amount;?></strong></td>
<td  width="5%"><a  href="#" onclick="opendata(<?php echo $row2['invoice_id'];?>);"><img src="images/search.gif"  width="15" height="15"/></a></td>
</tr>
<?php }  ?>

	    <tr bgcolor="#999999" style="color:#FFFFFF">
 		  <td>&nbsp;</td>
 		  <td>&nbsp;</td>
 		  <td nowrap="nowrap">&nbsp;</td>
 		  <td>&nbsp;<strong>Current Total:</strong></td>
 		<td><strong><?php echo array_sum($DR_arr);?></strong></td> 
		<td><strong><?php echo array_sum($CR_arr);?></strong></td>
		<td>&nbsp;</td>
	    
</table>

<?php 	
}
?>
<script language="JavaScript">
function opendata(str)
{
	
	var invoicedata=str;
	
	window.open("Saleregisterpopup.php?id="+str,"","height=1100,width=1100,toolbar=no,menubar=no,dependent=yes,resizable=yes,scrollbars=yes");
}	
	
</script>