<?php 
$pgMod="party_summary";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php 
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a> 
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}

?>
<form method="get" action="">
    <input type="hidden" name="src" value="Account" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
    <div class="row-fluid">
        <div class="span12">
            <div class="well no-padding">
                <div class="controls controls-row">

                    <div class="control-group span4">
                        <label class="control-label" for="class">Select Party</label>
                        <div class="controls">
                        	<select name="pmode">
                                <option value="">All Type</option>
                                 <?php 
            					$Expensemode=$db->getRows("select * from mode where school_id='".$_SESSION["admin"]["school_id"]."' and type='Expense' and status=1");
            					if(is_array($Expensemode) && count($Expensemode)>0){
            					?>
                                  <?php foreach($Expensemode as $Expense){
            					  	?><option value="<?php echo $Expense["mode"] ?>"><?php echo $Expense["mode"] ?></option><?php
            					  }?>
                                <?php } ?>
                            </select>
                        </div>
                    </div>
                    <div class="control-group span4">
                        <label class="control-label" for="class">From Date</label>
                        <div class="controls">
                            <input required type="date" id="tfrom" name="tfrom" class="input-block-level" value="<?php if($_GET)echo $_GET["tfrom"];?>">
                        </div>
                    </div>
                    <div class="control-group span4">
                        <label class="control-label" for="class">To Date</label>
                        <div class="controls">
                            <input required type="date" id="tto" name="tto" class="input-block-level" value="<?php if($_GET)echo $_GET["tto"];?>">
                        </div>
                    </div>

                    <div class="" style="margin-top:10px; margin-left:-200px;float:left;">
                        <button type="submit" name="party_ledger" class="btn-classic">Party Ledger</button>
                        <button type="submit" name="overall_ledger" class="btn-classic">Overall Ledger</button>
                    </div>
                </div>
                <br>
            </div>
        </div>
    </div>

</form>

<div class="row-fluid"><div class="span12"><div class="top-bar">
<h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
<div style="float:right; margin-top:5px">
    <button class="btn btn-success" onclick="exportTableToExcel('tblData', 'Expenses Summary')" style="margin-left:35px;">Excel</button>
        <a onclick="printDiv('printableDiv')" class="btn btn-primary" target="_blank">Print</a>
        <button onclick="SMSComplete('completeFeeShw','EXPENSESMS')" style="float:right" type="button" class="btn btn-primary">SMS</button>
    </div>
</div></div></div>

<?php if(isset($_GET["party_ledger"])){ ?>

<div class="well no-padding" id="printableDiv">

<?php
$aryPgAct=array("src"=>"Account", "page_id"=>$pgMod,"action"=>"add");
$between="";
if(isset($_GET["tfrom"]) && isset($_GET["tto"])){
$between=" and udate between '".$_GET["tfrom"]."' and '".$_GET["tto"]." 23:59:59' ";

    $aryList=$db->getRows("SELECT * FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and type='mode_income' and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' $between"); 

if($_GET["pmode"]!=""){
	$aryList=$db->getRows("SELECT * FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and (type='mode_income' OR type='withdraw') and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' and tfor = '".$_GET["pmode"]."' $between");
}

$i=0;$total=0; 
if(is_array($aryList) && count($aryList)>0)
{
			$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_GET["tto"])));

			?>
            <center><h5><p style="float:right">Expense Date: 
			<?php if(strtotime($tndate)==strtotime($_GET["tfrom"])){echo date("d-M-Y",strtotime($tndate));?> - <?php echo date("l",strtotime($tndate));}
			else echo date("d-M-Y",strtotime($_GET["tfrom"])) ." to ".date("d-M-Y",strtotime($tndate)); ?></p> <p><?php echo $_GET["pmode"] ;?></p>	</h5></center>

	<table class="table" id="tblData" border="1">
		<thead>
			<tr>
				<th>S. No.</th>
                <th>Date</th>
				<th>Detail</th>
				<th>Borrow</th>
                <th>Expense</th>
                <th>Balance</th>
			</tr>
		</thead>
		<tbody>
		<?php foreach($aryList as $clist){ $i++; 
		$aryPgAct["id"]=$clist['id'];
		?>
			<tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
				<td><?php echo $i; ?></td>
				<!--<td><?php echo  $clist["exp_id"]; ?></td>-->
                <td><?php echo date("d-M-Y",strtotime($clist["udate"])); ?></td>
				<td><?php echo $clist["detail"] ?></td>
				
				<td><?php if($clist["type"]=='mode_income')echo $clist["amount"]; ?></td>
				<td><?php if($clist["type"]=='withdraw')echo $clist["amount"]; ?></td>
				<td><?php if($clist["type"]=='mode_income')echo $total+=$clist["amount"];
				else if($clist["type"]=='withdraw')echo $total-=$clist["amount"]; ?></td>
			</tr>
		<?php } ?>
		</tbody>
		<tfoot>
			<tr>
				<td colspan="5" style="color:red">Total</td>
                <!--<td></td>-->
                <!--<td></td>-->
                <td style="color:red" id="completeFeeShw"><?php echo $total; ?></td>
				<!--<td></td>-->
			</tr>
		</tfoot>
	</table>  Print Date : <?php echo date('d-M-Y'); ?>
	<?php
}
else{
	?>
	<div class="alert alert-info">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
	</div>
	<?php
}
}
?>
</div>

<?php
}

 if(isset($_GET["overall_ledger"])){
      ?>

<div class="well no-padding" id="printableDiv">

<?php
$aryPgAct=array("src"=>"Account", "page_id"=>$pgMod,"action"=>"add");
$between="";
if(isset($_GET["tfrom"]) && isset($_GET["tto"])){
$between=" and udate between '".$_GET["tfrom"]."' and '".$_GET["tto"]." 23:59:59' ";

    $aryList=$db->getRows("SELECT * FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and (type='mode_income' OR type='withdraw') and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' $between group by tfor");

if($_GET["pmode"]!=""){
	$aryList=$db->getRows("SELECT * FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and (type='mode_income' OR type='withdraw') and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' and tfor = '".$_GET["pmode"]."' $between group by tfor");
}

$i=0;$total=0;
if(is_array($aryList) && count($aryList)>0)
{
			$tndate = date('Y/m/d', strtotime('-1 days',strtotime($_GET["tto"])));

			?>
            <center><h5><p style="float:right">Expense Date: 
			<?php if(strtotime($tndate)==strtotime($_GET["tfrom"])){echo date("d-M-Y",strtotime($tndate));?> - <?php echo date("l",strtotime($tndate));}
			else echo date("d-M-Y",strtotime($_GET["tfrom"])) ." to ".date("d-M-Y",strtotime($tndate)); ?></p> <p>Expense Detail</p>	</h5></center>

	<table class="table" id="tblData" border="1">
		<thead>
			<tr>
				<th>S. No.</th>
                <th>Date</th>
                <th>Party Name</th>
				<!--<th>Detail</th>-->
				<th>Borrow</th>
                <th>Expense</th>
                <th>Balance</th>
			</tr>
		</thead>
		<tbody>
		<?php foreach($aryList as $clist){ $i++; 
		$aryPgAct["id"]=$clist['id'];
		?>
			<tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
				<td><?php echo $i; ?></td>
                <td><?php echo date("d-M-Y",strtotime($clist["udate"])); ?></td>
				<td><?php echo $clist["tfor"] ?></td>
				<!--<td><?php echo $clist["detail"] ?></td>-->
				<td><?php
				    echo $borrow = $db->getVal("SELECT sum(amount) FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and type='mode_income' and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' and tfor = '".$clist["tfor"]."' $between ");
				?></td>
				<td><?php
				echo $expense = $db->getVal("SELECT sum(amount) FROM transaction where school_id='".$_SESSION["admin"]["school_id"]."' and type='withdraw' and status=1 and amount!=0 and session='".$_SESSION["admin"]["session"]."' and tfor = '".$clist["tfor"]."' $between ");
				?></td>
				<td><?php echo $borrow-$expense; ?></td>
			</tr>
		<?php } ?>
		</tbody>
		<!--<tfoot>-->
		<!--	<tr>-->
		<!--		<td colspan="5" style="color:red">Total</td>-->
  <!--              <td style="color:red" id="completeFeeShw"><?php echo $total; ?></td>-->
		<!--	</tr>-->
		<!--</tfoot>-->
	</table>  Print Date : <?php echo date('d-M-Y'); ?>
	<?php
}
else{
	?>
	<div class="alert alert-info">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> Record not found </i>
	</div>
	<?php
}
}
?>
</div>

<?php

 }

?>
<script type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": true,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"fnRowCallback" : function(nRow, aData, iDisplayIndex){
                $("td:first", nRow).html(iDisplayIndex +1);
               return nRow;
            },
			"bAutoWidth": false
		});
	});
	
	function SMSComplete(valueid,act){
	$(this).hide();
	var Fee=$("#"+valueid).html();
	var fdate='<?php if($_GET["tfrom"]!="")echo date("d-M-Y",strtotime($_GET["tfrom"]));else echo "0" ?>';
	var tdate='<?php if($_GET["tto"]!="")echo date("d-M-Y",strtotime($_GET["tto"]));else echo "0" ?>';
	var mode='<?php if($_GET["pmode"]!="")echo $_GET["pmode"];else echo "0" ?>';
	$.post( "ajax.php", { type: act ,fee: Fee, fdate:fdate, tdate:tdate, pmode: mode  } , function( data ) {
	  	//$("#ins").html(data);getTotal();
		alert(data);
		$(this).show();
	});
}


    function exportTableToExcel(tableID, filename = '',e){
    
    var tableType = document.getElementById('tblData').getAttribute("data-tblname");

    var downloadLink;
    var dataType = 'application/vnd.ms-excel';
    var tableSelect = document.getElementById(tableID);
    var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

    // Specify file name
    filename = filename?filename+'.xls':'excel_data.xls';
    
    // Create download link element
    downloadLink = document.createElement("a");
    
    document.body.appendChild(downloadLink);
    
    if(navigator.msSaveOrOpenBlob){
        var blob = new Blob(['\ufeff', tableHTML], {
            type: dataType
        });
        navigator.msSaveOrOpenBlob( blob, filename);
    }else{
        // Create a link to the file
        downloadLink.href = 'data:' + dataType + ', ' + tableHTML;
    
        // Setting the file name
        downloadLink.download = filename;
        
        //triggering the function
        downloadLink.click();
    }
    e.preventDefault();
}

	</script>
<!-- / Forms -->