<style type="text/css">
.fa-3x {
	font-size: 2em !important;
}
.report_view_th {
	background-color: #428bca;
	color: #fff !important;
	font-size: 14px;
}
.table-responsive td {
	font-size: 14px;
}
h4 {
	font-size: 13px;
}
.table > thead:first-child > tr:first-child > th, .table > thead:first-child > tr:first-child > td, .table-striped thead tr.primary:nth-child(2n+1) th {
	background-color: #428bca;
	border-color: #357ebd;
	border-top: 1px solid #357ebd;
	color: white;
	text-align: center;
}
/**/
.ml12 {
	font-weight: 200;
	font-size: 1.8em;
	text-transform: uppercase;
	letter-spacing: 0.5em;
}
.ml12 .letter {
	display: inline-block;
	line-height: 1em;
}
/**/
</style>
<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->

<!-- start: CSS REQUIRED FOR THIS PAGE ONLY -->

<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->
<body onLoad="add_row()">
<!-- start: HEADER -->
<div style="display:none" class="navbar navbar-inverse navbar-fixed-top"> 
  <!-- start: TOP NAVIGATION CONTAINER -->
  <div class="container">
    <div class="navbar-header"> 
      <!-- start: RESPONSIVE MENU TOGGLER -->
      <button data-target=".navbar-collapse" data-toggle="collapse" class="navbar-toggle" type="button"> <span class="fa fa-list"></span> </button>
      <!-- end: RESPONSIVE MENU TOGGLER --> 
      <!-- start: LOGO -->
      <?php /*?><?php $this->load->view("common/logo"); ?><?php */?>
      <!-- end: LOGO --> 
    </div>
    <div class="navbar-tools"> 
      <!-- start: TOP NAVIGATION MENU -->
      <?php /*?><?php $this->load->view("common/notifications.php"); ?><?php */?>
      <!-- end: TOP NAVIGATION MENU --> 
    </div>
  </div>
  <!-- end: TOP NAVIGATION CONTAINER --> 
</div>
<!-- end: HEADER --> 
<!-- start: MAIN CONTAINER --> 
<!--<div class="main-container">-->
<div style="display:none" class="navbar-content"> 
  <!-- start: SIDEBAR -->
  <?php /*?><?php $this->load->view("common/navigation"); ?><?php */?>
  <!-- end: SIDEBAR --> 
</div>
<!-- start: PAGE --> 
<!--	<div class="main-content">
		--> <!-- end: SPANEL CONFIGURATION MODAL FORM --> 
<!--<div class="container">--> 
<!-- start: PAGE HEADER -->
<div style="display:none" class="row">
  <div class="col-sm-12"> 
    <!-- start: PAGE TITLE & BREADCRUMB -->
    <ol class="breadcrumb">
      <li> <a href="<?php echo base_url('dashboard'); ?>"> Dashboard </a> </li>
      <li> <a href="<?php echo base_url('sales'); ?>"> Sales </a> </li>
      <li class="active"> View </li>
      <li class="search-box">
        <form class="sidebar-search">
          <div class="form-group">
            <input type="text" placeholder="Start Searching...">
            <button class="submit"> <i class="fa fa-search"></i> </button>
          </div>
        </form>
      </li>
    </ol>
    <div style="display:none" class="page-header">
      <?php  
							   $message = $this->session->flashdata('message');
							   if($message){ ?>
      <div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert">×</button>
        <?php echo $message ?> </div>
      <?php
                               }
							    ?>
    </div>
  </div>
</div>
<!-- end: PAGE HEADER --> 
<!-- start: PAGE CONTENT 
                    <!-- start grid -->

<div class="panel panel-default">
  <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Balance Summary Report
    <div class="panel-tools" style="top:2px;">
      <button id="fa_table_btn" onClick="init_table()" class="btn btn-blue dropdown-toggle"> <i class="fa fa-table"></i> </button>
      <button class="btn btn-blue " id="excel"> <i class="fa fa-fw" aria-hidden="true" title="Copy to use file-excel-o"></i> </button>
      <button onClick="printThis()" class="btn btn-blue dropdown-toggle"> <i class="fa fa-print"></i> </button>
    </div>
  </div>
</div>
<div id="printableArea" class="print-start">
  <div class="row">
    <div class="col-md-12"> 
      <!-- start: DYNAMIC TABLE PANEL -->
      <div class="panel panel-default">
        <div style="display:none" class="panel-heading"> <i class="fa fa-external-link-square"></i>
          <div class="panel-tools" style="top:2px;">
            <button class="btn btn-blue dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-list"></i> </button>
            <ul class="dropdown-menu dropdown-light pull-right">
              <li> </li>
            </ul>
          </div>
          <!--panel-tools--> 
        </div>
        <!--panel-heading--> 
        
        <br>
        <div class="panel-body">
          <div>
            <h1 align="center">Customer Balance Summary Report</h1>
          </div>
          <div class="well well-sm">
            <div  class="col-xs-4 border-right">
              <div class="col-xs-2"><i class="fa fa-3x fa-file-text-o padding010 text-muted"></i></div>
              <div class="col-xs-10">
                <h4 class="">Date :
                  <?php //echo date('Y-m-d H:i:s') ?>
                </h4>
                <?php if($srh_from_date){ ?>
                <p>From :
                  <?php 
$srh_from_date = date('Y-m-d',strtotime($srh_from_date));
echo $srh_from_date;
 }
?>
                </p>
                <?php if($srh_to_date){
?>
                <p>To   :
                  <?php
$srh_to_date = date('Y-m-d',strtotime($srh_to_date));
 echo $srh_to_date;
}?>
                </p>
              </div>
              <div class="clearfix"></div>
            </div>
            
            <!-- -->
            <div  style="" class="col-xs-4 border-right">
              <?php if($srh_customer_id){ ?>
              <div class="col-xs-2"><i class="fa fa-3x fa-user padding010 text-muted" style=""></i></div>
              <div class="col-xs-10" style="">
                <h4 class="">
                  <?php //echo $summery[0]['cus_name']; ?>
                </h4>
                <br>
                <p></p>
                <?php /*?>Tel:  <?php echo $summery[0]['cus_phone']; ?><br>
Email: <?php echo $summery[0]['cus_email']; ?><?php */?>
              </div>
              <div class="clearfix"></div>
              <?php } ?>
            </div>
            <div class="col-xs-4">
              <div class="col-xs-2"><i class="fa fa-3x fa-building-o padding010 text-muted"></i></div>
              <div class="col-xs-10"  style="">
                <h4 class=""><?php echo $warehouse_details['name']; ?></h4>
                <?php echo $warehouse_details['address']; ?>
                <p></p>
                Tel: <?php echo $warehouse_details['phone']; ?><br>
                Email: <?php echo $warehouse_details['email']; ?> </div>
              <div class="clearfix"></div>
            </div>
            <div class="clearfix"></div>
          </div>
          <!--col-xs-4-->
          
          <center>
            <h1 class="ml12">Processing... <i style="letter-spacing:normal" class="fa fa-spinner fa-spin"></i></h1>
          </center>
          <!--<div class="spinner">
  <div class="double-bounce1"></div>
  <div class="double-bounce2"></div>
</div>-->
          
          <div style="display:none" class="col-xs-5">
            <input name="sale_id" type="hidden" id="sale_id" value="">
            <input type="hidden" id="sale_type" name="sale_type" value="sale">
            <div class="clearfix"></div>
          </div>
          <!--col-xs-4"-->
          <div class="clearfix"></div>
          <div class="table-responsive">
            <table id="warehouse_table" class="table table-bordered table-hover table-striped print-table order-table" width="100%">
              <thead>
                <tr class="report_view_th">
                  <th>No</th>
                  <th>Customer Name</th>
                  <th>Total Sales</th>
                  <th>Total Return</th>
                  <th>Return Cash</th>
                  <th>Total Paid</th>
                  <th>Unrealized Cheque</th>
                  <th>Balance</th>
                </tr>
              </thead>
              <tbody>
              </tbody>
              <tfoot>
              </tfoot>
            </table>
          </div>
          <!--table-responsive-->
          
          <div class="clearfix"></div>
          <p></p>
          <div style="display:none" class="well well-sm col-xs-6 pull-right">
            <div class="clearfix"></div>
            <div class="clearfix"></div>
          </div>
          <!--well well-sm col-xs-6 pull-right--> 
          
          <!-- payment list -->
          
          <div style="display:none" class="clearfix"></div>
          <br>
          <div style="display:none" class="clearfix"></div>
          <!-- end payment list--><!--buttons--> 
          
        </div>
        <!--panel-body--> 
      </div>
      <!--panel--> 
      
    </div>
    <!--col-md-12--> 
  </div>
  <!--row--> 
  
  <!-- end grid --> 
  
</div>
<!-- end: PAGE -->
</div>
<!-- end: MAIN CONTAINER --> 
<!-- start: FOOTER --><!-- end: FOOTER --> 
<!-- start: RIGHT SIDEBAR --> 
<!-- end: RIGHT SIDEBAR -->
<div id="event-management" class="modal fade" tabindex="-1" data-width="760" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times; </button>
        <h4 class="modal-title">Event Management</h4>
      </div>
      <div class="modal-body"></div>
      <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn btn-light-grey"> Close </button>
        <button type="button" class="btn btn-danger remove-event no-display"> <i class='fa fa-trash-o'></i> Delete Event </button>
        <button type='submit' class='btn btn-success save-event'> <i class='fa fa-check'></i> Save </button>
      </div>
    </div>
  </div>
</div>

<!-- start ajax model -->
<div id="ajax-modal" class="modal fade" data-backdrop="static" data-keyboard="false" tabindex="-1" style="display: none;"></div>
<!-- end ajax model --> 

<!-- start: MAIN JAVASCRIPTS -->
<?php $this->load->view("common/footer"); ?>
<!-- end: MAIN JAVASCRIPTS --> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.js"></script>
        <script type="text/javascript" src="<?php echo asset_url(); ?>js/accounting.min.js"></script>
        <script type="text/javascript" src="<?php echo asset_url(); ?>js/jquery.dataTables.min.js"></script>
		<script type="text/javascript" src="<?php echo asset_url(); ?>js/dataTables.bootstrap.min.js"></script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/dataTables.buttons.min.js">
        </script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.print.min.js">
        </script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.flash.min.js">
        </script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/jszip.min.js">
        </script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/pdfmake.min.js">
        </script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/vfs_fonts.js">
        </script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.html5.min.js">
        </script>
        <script type="text/javascript" language="javascript" src="<?php echo asset_url(); ?>js/buttons.print.min.js"></script>
<script src="<?php echo asset_url(); ?>js/anime.min.js"></script>
<input type="hidden" value="<?php echo $srh_customer_id ?>" id="srh_customer_id">
<input type="hidden" value="<?php echo $warehouse_details['id']; ?>" id="warehouse_id">
<input type="hidden" value="<?php echo $srh_from_date ?>" id="srh_from_date">
<input type="hidden" value="<?php echo $srh_to_date ?>" id="srh_to_date">

<!-- end: JAVASCRIPTS REQUIRED FOR THIS PAGE ONLY --> 
<script>
			/*jQuery(document).ready(function() {
				TableData.init();
				
				$('#warehouse_table').DataTable({
					"bFilter": false,
					"bPaginate": false,
					dom: 'Bfrtip',
					buttons: [
       { extend: 'print', text: '<i class="fa fa-print fa-2x"></i>',footer: true},
        { extend: 'excel', text: '<i class="fa fa-file-excel-o fa-2x"></i>',footer: true},
		 { extend: 'pdf', text: '<i class="fa fa-file-pdf-o fa-2x"></i>',footer: true},
		
    ]
				});
				//Main.init();
				//TableData.init();
				//UIModals.init();

			});*/
		</script> 
<script type="text/javascript" language="javascript" >
var g_tot			= 0;
function add_row(){
var srh_customer_id = $('#srh_customer_id').val();
var warehouse_id	= $('#warehouse_id').val();
var srh_from_date	= $('#srh_from_date').val();
var srh_to_date		= $('#srh_to_date').val();
var c				= 0;
var l				= 0;
var ln 				= 0;
//Get Customers
	$.get("../customers/get_customers?srh_customer_id="+srh_customer_id)
	  .done(function( data ) {
		var obj = jQuery.parseJSON(data);
			l   = obj.length;
					//break;
		$.each(obj, function(e) {
			ln++;
			var cus_name		= this.cus_name;
			var cus_id			= this.cus_id;
			var total_sales		= 0;
			var total_return	= 0;
			var return_cash		= 0;
			var total_paid		= 0;
			var unrealized_chq	= 0;
				/*-------------
					get sales
				---------------*/
				//console.log(this);
				$.get("../sales/get_all_sum_sales_for_summery_report?srh_customer_id="+this.cus_id+"&warehouse_id="+warehouse_id+"&srh_from_date="+srh_from_date+"&srh_to_date="+srh_to_date)
				.done(function( data ) {
				var sales_obj = jQuery.parseJSON(data);
				//console.log(sales_obj[0].sale_total);
					total_sales = sales_obj[0].sale_total;//-----------------------total_sales
					if(total_sales){
						/*-------------
							get returns
						---------------*/
					//console.log(this);
					$.get("../sales/get_sum_sales_return?srh_customer_id="+cus_id+"&warehouse_id="+warehouse_id+"&srh_from_date="+srh_from_date+"&srh_to_date="+srh_to_date)
					.done(function( data ) {
					var sales_return_obj = jQuery.parseJSON(data);
					//console.log(sales_return_obj);
						//total_return = sales_return_obj.sale_return_total;
									/*-------------
									get return cash
									---------------*/
							//console.log(this);
							$.get("../sales/getSumPaymentsForBalanceRep?srh_customer_id="+cus_id+"&warehouse_id="+warehouse_id+"&srh_from_date="+srh_from_date+"&srh_to_date="+srh_to_date+"&srh_type=sale&srh_payment_term=Return_Cash&return_name=Return_Cash")
							.done(function( data ) {
							var return_cash_obj = jQuery.parseJSON(data);
							//console.log(return_cash_obj[0].Return_Cash);
								//return_cash = return_cash_obj[0].Return_Cash;
													/*-------------
												get Total Paid
												---------------*/
										//console.log(this);
						//$srh_warehouse_id='',$srh_to_date='',$srh_from_date='',$srh_type='',$srh_payment_term='',$ss_user_id='',$srh_customer_id='',$return_name=''
										$.get("../sales/getSumPaymentsForBalanceRep?srh_customer_id="+cus_id+"&warehouse_id="+warehouse_id+"&srh_from_date="+srh_from_date+"&srh_to_date="+srh_to_date+"&srh_type=sale&srh_payment_term=&return_name=tot_sp")
										.done(function( data ) {
										var total_paid_obj = jQuery.parseJSON(data);
										//console.log(return_cash_obj[0].Return_Cash);
											//total_paid = total_paid_obj[0].tot_sp;
															/*-------------
															get UnRealized Chq
															---------------*/
													//console.log(this);
									//$srh_warehouse_id='',$srh_to_date='',$srh_from_date='',$srh_type='',$srh_payment_term='',$ss_user_id='',$srh_customer_id='',$return_name=''
													$.get("../sales/getSumPaymentsForBalanceRep?srh_customer_id="+cus_id+"&warehouse_id="+warehouse_id+"&srh_from_date="+srh_from_date+"&srh_to_date="+srh_to_date+"&srh_type=sale&srh_payment_term=Cheque&return_name=unrealized_chq")
													.done(function( data ) {
													var unrealized_chq_obj = jQuery.parseJSON(data);
													//console.log(return_cash_obj[0].Return_Cash);
														//unrealized_chq = unrealized_chq_obj[0].unrealized_chq;
														
				total_sales		= sales_obj[0].sale_total;
				total_return	= sales_return_obj.sale_return_total;
				return_cash		= return_cash_obj[0].Return_Cash;
				total_paid		= total_paid_obj[0].tot_sp;
				unrealized_chq	= unrealized_chq_obj[0].unrealized_chq;	
														
														
														
	var fin = total_sales - total_return - (total_paid-return_cash) ;
	
													if(fin > 0 || -1 > fin){
														c++;
														g_tot += fin;
														$('#warehouse_table > tbody:last').append('<tr><td>'+c+'</td><td>'+cus_name+'</td><td style="text-align:right;">'+total_sales+'</td><td style="text-align:right;">'+accounting.formatMoney(total_return, "", 2, "", ".")+'</td><td style="text-align:right;">'+accounting.formatMoney(return_cash, "", 2, "", ".")+'</td><td style="text-align:right;">'+accounting.formatMoney((total_paid-return_cash), "", 2, "", ".")+'</td><td style="text-align:right;">'+accounting.formatMoney(unrealized_chq, "", 2, "", ".")+'</td><td style="text-align:right;">'+accounting.formatMoney(fin, "", 2, "", ".")+'</td></tr>');
	//													console.log("l :"+l);
	//													console.log("ln :"+ln);
														if(l==ln){
															++ln;
															console.log('inner if');
															$('.ml12').fadeOut();$('.ml12').html("&nbsp;");init_table();
															}
													
													}else {
														if(l==ln){
															++ln;
															console.log('Outer If');
														$('.ml12').fadeOut();$('.ml12').html("&nbsp;");init_table();
														}
													}
													
												});
														/*-------------
															END UnRealized Chq
														---------------*/
										});
											/*-------------
												END Total Paid
											---------------*/
							});
								/*-------------
									END return cash
								---------------*/
					});
						/*-------------
							END get returns
						---------------*/
					}//else {$('.ml12').fadeOut();$('.ml12').html("&nbsp;"); init_table();}
				});
				/*-------------
					END get sales
				---------------*/
		});// end each
		
	  });
	//init_table();
	}

//var j = jQuery.noConflict();

//$('.ml12').each(function(){
//  $(this).html($(this).text().replace(/([^\x00-\x80]|\w)/g, "<span class='letter'>$&</span>"));
//});
//
//anime.timeline({loop: true})
//  .add({
//    targets: '.ml12 .letter',
//    translateX: [40,0],
//    translateZ: 0,
//    opacity: [0,1],
//    easing: "easeOutExpo",
//    duration: 1200,
//    delay: function(el, i) {
//      return 500 + 30 * i;
//    }
//  }).add({
//    targets: '.ml12 .letter',
//    translateX: [0,-30],
//    opacity: [1,0],
//    easing: "easeInExpo",
//    duration: 1100,
//    delay: function(el, i) {
//      return 100 + 30 * i;
//    }
//  });
//$('#warehouse_table tr').has('td:last-child:empty').hide();


$(function(){
    $('#excel').click(function(){
        var url='data:application/vnd.ms-excel,' + encodeURIComponent($('#printableArea').html()) 
        location.href=url
        return false
    })
})

function init_table(){
	//alert();
	setTimeout(function(){
		
		$('#warehouse_table > tfoot:last').append
	('<tr><td colspan="7" style="text-align:right">TOTAL</td>TOTAL<td>'+accounting.formatMoney(g_tot, "", 2, ",", ".")+'</td></tr>');
	
	$('#fa_table_btn').fadeOut();
	 //TableData.init();
		  $('#warehouse_table').DataTable({
					"bDestroy": true,
					"bPaginate": false,
					dom: 'Bfrtip',
				buttons: [
						{ extend: 'copy', text: '<i class="fa fa-copy fa-2x"></i>',footer: true, exportOptions:
				 [ 0 ]},
						   { extend: 'print', text: '<i class="fa fa-print fa-2x"></i>',title:'<center>CUSTOMER BALANCE SUMMARY REPORT<h2><?php echo $warehouse_details['name']; ?></br>From : '+$('#srh_from_date').val()+' To : '+$('#srh_to_date').val()+'<br/></h2><?php if($warehouse_details['phone'])echo '<h3 style="margin-top:-6px">'.$warehouse_details['address'].'</h3>'; ?><?php if($warehouse_details['phone'])echo '<h3 style="margin-top:-7px">'.$warehouse_details['phone']."</h3>"; ?></center>',footer: true},
							{ extend: 'excel', text: '<i class="fa fa-file-excel-o fa-2x"></i>',footer: true},
							 { extend: 'pdf', text: '<i class="fa fa-file-pdf-o fa-2x"></i>',footer: true},
							 
						]
				});
		
		},1000);
	}
	
//
//function toggleRow(e){
//   var subRow = e.parentNode.parentNode.nextElementSibling;
//   subRow.style.display = subRow.style.display === "none" ? "table-row" : "none";    
//	}
//
//function hide(c){
//	alert(c);
//	}
function printThis(){
	window.print();
	}
</script>
</body>
<!-- end: BODY -->
</html>