<?php $this->load->view("common/header"); ?>
<!-- end: HEAD -->

<!-- start: CSS REQUIRED FOR THIS PAGE ONLY -->
<link rel="stylesheet" type="text/css" href="<?php echo asset_url(); ?>plugins/select2/select2.css" />
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/DataTables/media/css/DT_bootstrap.css" />
<link href="<?php echo asset_url(); ?>plugins/bootstrap-modal/css/bootstrap-modal-bs3patch.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo asset_url(); ?>plugins/bootstrap-modal/css/bootstrap-modal.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-fileupload/bootstrap-fileupload.min.css">
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/datepicker/css/datepicker.css">
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-timepicker/css/bootstrap-timepicker.min.css">
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-daterangepicker/daterangepicker-bs3.css">
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-colorpicker/css/bootstrap-colorpicker.css">
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/jQuery-Tags-Input/jquery.tagsinput.css">
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/bootstrap-fileupload/bootstrap-fileupload.min.css">
<link rel="stylesheet" href="<?php echo asset_url(); ?>plugins/summernote/build/summernote.css">
<!--  -->
<style type="text/css">
.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;
}
#s2id_srh_customer_id {
	border: 0 none;
	padding: 0;
}
button.dt-button, div.dt-button, a.dt-button {
	-moz-user-select: none;
	background-color: #e9e9e9;
	background-image: linear-gradient(to bottom, #fff 0%, #e9e9e9 100%);
	border: 1px solid #999;
	border-radius: 2px;
	box-sizing: border-box;
	color: black;
	cursor: pointer;
	display: inline-block;
	font-size: 0.88em;
	margin-right: 0.333em;
	outline: medium none;
	overflow: hidden;
	padding: 0.5em 1em;
	position: relative;
	text-decoration: none;
	white-space: nowrap;
}
.loader {
	color: #000000;
	font-size: 30px;
	text-indent: -9999em;
	overflow: hidden;
	width: 1em;
	height: 1em;
	border-radius: 50%;
	margin: 72px auto;
	position: relative;
	-webkit-transform: translateZ(0);
	-ms-transform: translateZ(0);
	transform: translateZ(0);
	-webkit-animation: load6 1.7s infinite ease, round 1.7s infinite ease;
	animation: load6 1.7s infinite ease, round 1.7s infinite ease;
}
@-webkit-keyframes load6 {
 0% {
 box-shadow: 0 -0.83em 0 -0.4em, 0 -0.83em 0 -0.42em, 0 -0.83em 0 -0.44em, 0 -0.83em 0 -0.46em, 0 -0.83em 0 -0.477em;
}
 5%,  95% {
 box-shadow: 0 -0.83em 0 -0.4em, 0 -0.83em 0 -0.42em, 0 -0.83em 0 -0.44em, 0 -0.83em 0 -0.46em, 0 -0.83em 0 -0.477em;
}
 10%,  59% {
 box-shadow: 0 -0.83em 0 -0.4em, -0.087em -0.825em 0 -0.42em, -0.173em -0.812em 0 -0.44em, -0.256em -0.789em 0 -0.46em, -0.297em -0.775em 0 -0.477em;
}
 20% {
 box-shadow: 0 -0.83em 0 -0.4em, -0.338em -0.758em 0 -0.42em, -0.555em -0.617em 0 -0.44em, -0.671em -0.488em 0 -0.46em, -0.749em -0.34em 0 -0.477em;
}
 38% {
 box-shadow: 0 -0.83em 0 -0.4em, -0.377em -0.74em 0 -0.42em, -0.645em -0.522em 0 -0.44em, -0.775em -0.297em 0 -0.46em, -0.82em -0.09em 0 -0.477em;
}
 100% {
 box-shadow: 0 -0.83em 0 -0.4em, 0 -0.83em 0 -0.42em, 0 -0.83em 0 -0.44em, 0 -0.83em 0 -0.46em, 0 -0.83em 0 -0.477em;
}
}
@keyframes load6 {
 0% {
 box-shadow: 0 -0.83em 0 -0.4em, 0 -0.83em 0 -0.42em, 0 -0.83em 0 -0.44em, 0 -0.83em 0 -0.46em, 0 -0.83em 0 -0.477em;
}
 5%,  95% {
 box-shadow: 0 -0.83em 0 -0.4em, 0 -0.83em 0 -0.42em, 0 -0.83em 0 -0.44em, 0 -0.83em 0 -0.46em, 0 -0.83em 0 -0.477em;
}
 10%,  59% {
 box-shadow: 0 -0.83em 0 -0.4em, -0.087em -0.825em 0 -0.42em, -0.173em -0.812em 0 -0.44em, -0.256em -0.789em 0 -0.46em, -0.297em -0.775em 0 -0.477em;
}
 20% {
 box-shadow: 0 -0.83em 0 -0.4em, -0.338em -0.758em 0 -0.42em, -0.555em -0.617em 0 -0.44em, -0.671em -0.488em 0 -0.46em, -0.749em -0.34em 0 -0.477em;
}
 38% {
 box-shadow: 0 -0.83em 0 -0.4em, -0.377em -0.74em 0 -0.42em, -0.645em -0.522em 0 -0.44em, -0.775em -0.297em 0 -0.46em, -0.82em -0.09em 0 -0.477em;
}
 100% {
 box-shadow: 0 -0.83em 0 -0.4em, 0 -0.83em 0 -0.42em, 0 -0.83em 0 -0.44em, 0 -0.83em 0 -0.46em, 0 -0.83em 0 -0.477em;
}
}
@-webkit-keyframes round {
 0% {
 -webkit-transform: rotate(0deg);
 transform: rotate(0deg);
}
 100% {
 -webkit-transform: rotate(360deg);
 transform: rotate(360deg);
}
}
@keyframes round {
 0% {
 -webkit-transform: rotate(0deg);
 transform: rotate(0deg);
}
 100% {
 -webkit-transform: rotate(360deg);
 transform: rotate(360deg);
}
}
</style>

<!-- end: CSS REQUIRED FOR THIS PAGE ONLY -->
<!-- end: HEAD -->
<!-- start: BODY -->
<body>
<!-- start: HEADER -->
<div 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 $this->load->view("common/logo"); ?>
      <!-- end: LOGO --> 
    </div>
    <div class="navbar-tools"> 
      <!-- start: TOP NAVIGATION MENU -->
      <?php $this->load->view("common/notifications.php"); ?>
      <!-- end: TOP NAVIGATION MENU --> 
    </div>
  </div>
  <!-- end: TOP NAVIGATION CONTAINER --> 
</div>
<!-- end: HEADER --> 
<!-- start: MAIN CONTAINER -->
<div class="main-container">
<div class="navbar-content"> 
  <!-- start: SIDEBAR -->
  <?php $this->load->view("common/navigation"); ?>
  <!-- end: SIDEBAR --> 
</div>
<!-- start: PAGE -->
<div class="main-content">
<!-- end: SPANEL CONFIGURATION MODAL FORM -->
<div class="container">
<!-- start: PAGE HEADER -->
<div 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="#"> Reports </a> </li>
      <li class="active"> Sales Report </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 class="page-header">
      <h1>Customer Balance</h1>
    </div>
    <p>Please use the table below to navigate or filter the results. </p>
  </div>
</div>

<!-- end: PAGE HEADER --> 
<!-- start: PAGE CONTENT 
                    <!-- start grid -->
<div class="row">
  <div class="col-md-12"> 
    <!-- start: DYNAMIC TABLE PANEL -->
    <div class="panel panel-default">
      <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Sales Report
        <div class="panel-tools" style="top:2px;">
          <button onClick="fbs_click('<?php echo base_url('reports/print_sale?srh_warehouse_id=1'); ?>');" class="btn btn-blue dropdown-toggle" data-toggle="dropdown"> <i class="fa fa-print"></i> </button>
        </div>
      </div>
    </div>
    <div class="panel-body">
      <div id="error"></div>
      <div class="col-md-12">
        <div class="panel panel-default">
          <div style="font-weight: 700;" class="panel-heading"></div>
          <div class="panel-body">
            <div class="col-sm-4">
              <div class="form-group">
                <label>Warehouse </label>
                <select id="srh_warehouse_id" class="form-control search-select" name="srh_warehouse_id">
                  <?php 
																 $ss_warehouse_id=$this->session->userdata('ss_warehouse_id'); 
                                                              foreach ($warehouse_list as $row)
                                                              {
																  $sel='';
																  if($ss_warehouse_id==$row->id)
																  {
																	  $sel=' selected="selected"';
																  }
                                                              ?>
                  <option value="<?php echo $row->id; ?>" <?php echo $sel; ?>> <?php echo $row->name; ?> </option>
                  <?php }?>
                </select>
              </div>
            </div>
            <div class="col-sm-4">
              <div class="form-group">
                <label>Customer </label>
                <select id="srh_customer_id" class="form-control search-select" name="srh_customer_id">
                  <option value="">-Select-</option>
                  <?php 
															
															foreach ($customer_list as $row)
															{
																
															?>
                  <option value="<?php echo $row['cus_id'];?>"><?php echo $row['cus_name']; if($row['cus_phone']) echo " / ".$row['cus_phone'];?> </option>
                  <?php }  ?>
                </select>
              </div>
            </div>
            <div class="col-sm-4">
              <div class="form-group">
                <label for="s2id_autogen1">To Date </label>
                <input id="srh_to_date" name="srh_to_date" type='text' class="form-control date-picker" value="" data-bv-field="date"/>
              </div>
            </div>
            <div class="col-sm-4 pull-right">
              <div class="form-group"><br>
                <br>
                <br>
                <br>
                <label for="s2id_autogen1">&nbsp;<br>
                  <br>
                </label>
                <input type="submit" name="add_category" id="add_category" value="Search" class="btn btn-primary" onClick="searchDetails()">
                &nbsp;&nbsp;
                <input type="submit" name="add_category" value="Reset" class="btn btn-danger" onClick="searchDetailsReset()">
              </div>
            </div>
          </div>
        </div>
        <div id="loader" class="loader"></div>
        <div>
          <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Customer Balance </div>
          <table id="" class="table table-bordered table-condensed table-hover table-striped dataTable">
            <tbody>
              <!--<tr>
    <td></td>
    <td class="text-right">From Date:  <span id="f-date-txt"></span>, To Date: <span id="t-date-txt"></span></td>
  </tr>-->
              <tr>
                <th width="50%">Total Sales</th>
                <td width="50%"><div id="total_sales_tbl">0.00</div>
                  <input id="total_sales_fld" type="hidden" value=""></td>
              </tr>
              <tr>
                <th width="50%">Return</th>
                <td width="50%"><div id="total_return_tbl">0.00</div>
                  <input id="total_return_fld" type="hidden" value=""></td>
              </tr>
              <tr>
                <th width="50%">Total Payment</th>
                <td width="50%"><div id="total_payment_tbl">0.00</div>
                  <input id="total_payment_fld" type="hidden" value=""></td>
              </tr>
              <tr style="">
                <th width="50%">Unrealized Cheque</th>
                <td width="50%"><div id="unrealized_cheque_tbl">0.00</div>
                  <input id="unrealized_cheque_fld" type="hidden" value=""></td>
              </tr>
              <!--<tr>
    <th>Sales Return </th>
    <td><div id="sales_return_tbl">0.00</div><input id="sales_return_fld" type="text" value=""></td>
</tr>-->
              
              <tr>
                <th>Customer Balance 
                  <!-- <div onClick="set()" style="cursor:pointer">(Reresh)</div>--></th>
                <td><div id="balance_tbl">0.00</div>
                  <input id="balance_fld" type="hidden" value=""></td>
              </tr>
              <?php /*?> <tr>
     <th>Customer Balance With Unrealized Cheque
      <!-- <div onClick="set()" style="cursor:pointer">(Reresh)</div>--></th>
     <td><div id="unrealized_balance_tbl">0.00</div><input id="unrealized_balance_fld" type="hidden" value=""></td>
   </tr><?php */?>
            </tbody>
          </table>
        </div>
        <div>
          <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Return Balance </div>
          <table id="" class="table table-bordered table-condensed table-hover table-striped dataTable">
            <tbody>
              <tr>
                <th width="50%">Total Return</th>
                <td width="50%"><div id="total_return_tbl">0.00</div>
                  <input id="total_return_fld" type="hidden" value=""></td>
              </tr>
              <tr>
                <th>Rrturn Cash</th>
                <td><div id="return_cash_tbl">0.00</div>
                  <input id="return_cash_fld" type="hidden" value="0"></td>
              </tr>
              <tr>
                <th>Return Balance </th>
                <td><div id="return_balance_tbl">0.00</div>
                  <input id="return_balance_fld" type="hidden" value=""></td>
              </tr>
            </tbody>
          </table>
        </div>
        <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Sales List </div>
        <table width="100%" class="table table-bordered table-condensed table-hover table-striped dataTable" id="warehouse_table">
          <thead>
            <tr>
              <th>ID#</th>
              <th class="col-sm-2">Date</th>
              <th>Invoice No</th>
              <th>Customer</th>
              <th>Payment Status</th>
              <th>Grand Total</th>
              <th>Return</th>
              <th>Paid</th>
              <th class="col-sm-1">Balance</th>
              <th class="col-sm-1">Note</th>
            </tr>
          </thead>
          <tfoot>
            <tr>
              <th>ID#</th>
              <th>Date</th>
              <th>Invoice No</th>
              <th>Customer</th>
              <th>Payment Status</th>
              <th>Grand Total</th>
              <th>Return</th>
              <th>Paid</th>
              <th>Balance</th>
              <th class="col-sm-1">Note</th>
            </tr>
        </table>
        <br>
        <br>
        <br>
        <br>
        <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Sales Return List </div>
        <table width="100%" class="table table-bordered table-condensed table-hover table-striped dataTable" id="sales_return_table">
          <thead>
            <tr>
              <th class="col-sm-2">Date</th>
              <th>Invoice No</th>
              <th>Customer</th>
              <th>Payment Status</th>
              <th class="col-sm-1">Grand Total</th>
            </tr>
          </thead>
          <tfoot>
            <tr>
              <th>Date</th>
              <th>Invoice No</th>
              <th>Customer</th>
              <th>Payment Status</th>
              <th>Grand Total</th>
            </tr>
        </table>
        <br>
        <br>
        <br>
        <div class="panel-heading"> <i class="fa fa-external-link-square"></i> Sales Payment List </div>
        <table width="100%" class="table table-bordered table-condensed table-hover table-striped dataTable" id="sales_payment_table">
          <thead>
            <tr>
              <th>Ref. No.</th>
              <th>Payment Date</th>
              <th>Invoice No</th>
              <th class="text-right" style="text-align:right !important;">Type</th>
              <th class="text-right" style="text-align:right !important;">Payment Term</th>
              <th class="text-right" style="text-align:right !important;">Paid Amount</th>
            </tr>
          </thead>
          <tfoot>
            <tr>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
              <th>&nbsp;</th>
            </tr>
        </table>
        
        <!-- end: DYNAMIC TABLE PANEL --> 
      </div>
    </div>
    
    <!-- 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"); ?>
<script src="<?php echo asset_url(); ?>js/jquery-ui.js" ></script> 
<script src="<?php echo asset_url(); ?>js/moment-with-locales.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootstrap-datetimepicker.js"></script> 
<script type="text/javascript" src="<?php echo asset_url(); ?>js/bootbox.min.js"></script> 
<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> 
<!-- end: MAIN JAVASCRIPTS --> <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(); ?>/plugins/jquery-inputlimiter/jquery.inputlimiter.1.3.1.min.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/autosize/jquery.autosize.min.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/select2/select2.min.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/jquery.maskedinput/src/jquery.maskedinput.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/jquery-maskmoney/jquery.maskMoney.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/bootstrap-datepicker/js/bootstrap-datepicker.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/bootstrap-timepicker/js/bootstrap-timepicker.min.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/bootstrap-daterangepicker/moment.min.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/bootstrap-daterangepicker/daterangepicker.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/bootstrap-colorpicker/js/bootstrap-colorpicker.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/bootstrap-colorpicker/js/commits.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/jQuery-Tags-Input/jquery.tagsinput.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/bootstrap-fileupload/bootstrap-fileupload.min.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/summernote/build/summernote.min.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/ckeditor/ckeditor.js"></script> 
<script src="<?php echo asset_url(); ?>/plugins/ckeditor/adapters/jquery.js"></script> 
<script src="<?php echo asset_url(); ?>/js/form-elements.js"></script> 
<script>
		
		function searchDetailsReset(){
			$('#srh_to_date').val('');
			$('#srh_from_date').val('');
		}
		function searchDetails(){
					$("#loader").fadeIn();
			
					$('#return_cash_tbl').text(accounting.formatMoney(0, "", 2, ",", "."));
					$('#return_cash_fld').val(accounting.formatMoney(0, "", 2, ",", "."));
					
					loadGridSalesReturn();
					loadGridSalesPayment();
					loadGrid();
					cheque();
		}
				
				
			jQuery(document).ready(function() {
				FormElements.init();
					//$("#srh_customer_id").select2();
					$('#loader').fadeOut();
				//TableData.init();
				//loadGrid();
				//loadGridSalesReturn();
				//loadGridSalesPayment();				
				//cheque();
				/*var currentDate = new Date();
				$('#srh_to_date').datetimepicker({
					defaultDate: new Date()
				});
				$('#srh_from_date').datetimepicker({
					defaultDate: new Date()
				});*/
			});

var total_sale_for_cal=0;
var total_payment_for_cal=0;
var total_return_for_cal=0;
var tot_ret =0;
var ret_cash =0;
var RPB=0;
var total_return_for_final=0;

				var sales_rtn_tot_cost=0;
				var sales_rtn_tot_val=0;

			
			function loadGrid() {
				$('#loader').fadeIn();
			var srh_from_date=$('#srh_from_date').val();
			var srh_to_date=$('#srh_to_date').val();
			var srh_warehouse_id=$('#srh_warehouse_id').val();
			var srh_customer_id=$('#srh_customer_id').val();
			var srh_payment_status=$('#srh_payment_status').val();
							
					    $('#warehouse_table').DataTable({
							dom: 'Blfrtip',
							
						buttons: [
								   { extend: 'print', text: 'Print',footer: true},
									{ extend: 'excel', text: 'Save as Excel',footer: true},
									 { extend: 'pdf', text: 'Save as Pdf',footer: true},
									
								],
							"ajax": {
							'type': 'POST',
							'url': '<?php echo base_url('reports/get_list_sales_for_print_balance');?>',
							'data': {
							   srh_from_date: srh_from_date,
							   srh_to_date: srh_to_date,
							   srh_warehouse_id: srh_warehouse_id,
							   srh_customer_id: srh_customer_id,
							   srh_payment_status: srh_payment_status,
							}
							},
							"fnCreatedRow": function (oSettings, json)
							{
								set();
								$("#loader").fadeOut();
							},
					        "bDestroy": true,
					        "iDisplayLength": 10,
							"order": [[ 0, "desc" ]],
							"fnFooterCallback": function (nRow, aaData, iStart, iEnd, aiDisplay) {
				
                var pq = 0, sq = 0, bq = 0, pa = 0, grand_tot = 0, tech_tot = 0, parts_tot=0 , ser_tot=0;
				//alert(aaData.length);
				var ser_tot3=0;
				var ser_tot2=0;
				var ser_tot1=0;
				var ser_tot4=0;
                for (var i = 0; i < aaData.length; i++) {
					//alert(aaData[[i]][5]);
                   // p = (aaData[aiDisplay[i]][2]).split('__');
					ser_tot1 += parseFloat(aaData[[i]][5]);
					//console.log(ser_tot1);
	 				total_sale_for_cal += parseFloat(aaData[[i]][5]);
					ser_tot2 += parseFloat(aaData[[i]][6]);
					ser_tot3 += parseFloat(aaData[[i]][7]);
					ser_tot4 += parseFloat(aaData[[i]][8]);
				}
                var nCells = nRow.getElementsByTagName('th');
				nCells[5].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot1, "", 2, ",", ".")+' </div>';
				nCells[6].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot2, "", 2, ",", ".")+' </div>';
				nCells[7].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot3, "", 2, ",", ".")+' </div>';
				nCells[8].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot4, "", 2, ",", ".")+' </div>';
            
		//	 total_sale_for_cal= ser_tot1;

//$('#balance_fld').val(ser_tot4);
//$('#balance_tbl').text(ser_tot4, "", 2, ",", ".");
			total_sale_for_cal=ser_tot1;
		 $('#total_sales_fld').val(ser_tot1);
		  $('#total_return_fld').val(ser_tot2);
			  $('#total_sales_tbl').text(accounting.formatMoney(ser_tot1, "", 2, ",", ".")); 
			  $('#total_return_tbl').text(accounting.formatMoney(ser_tot2, "", 2, ",", ".")); 
			  set();
            }
			});
			
	}
					



function loadGridSalesReturn() {
	test1=2;
			var srh_from_date=$('#srh_from_date').val();
			var srh_to_date=$('#srh_to_date').val();
			var srh_warehouse_id=$('#srh_warehouse_id').val();
			var srh_customer_id=$('#srh_customer_id').val();
							
					    $('#sales_return_table').DataTable({
							dom: 'Blfrtip',
							
						buttons: [
       { extend: 'print', text: 'Print',footer: true},
        { extend: 'excel', text: 'Save as Excel',footer: true},
		 { extend: 'pdf', text: 'Save as Pdf',footer: true},
		
    ],
							"ajax": {
							'type': 'POST',
							'url': '<?php echo base_url('reports/get_list_sales_return_for_balance_report');?>',
							'data':{
							srh_customer_id:srh_customer_id,
							   srh_from_date: srh_from_date,
							   srh_to_date: srh_to_date,
							   srh_warehouse_id: srh_warehouse_id,
							}
							},
					        "bDestroy": true,
					        "bDestroy": true,
					        "iDisplayLength": 5,
							"order": [[ 1, "desc" ]], "fnFooterCallback": function (nRow, aaData, iStart, iEnd, aiDisplay) {
				
                var pq = 0, sq = 0, bq = 0, pa = 0, grand_tot = 0, tech_tot = 0, parts_tot=0 , ser_tot=0;
				//alert(aaData.length);
				var ser_tot3=0;
				var ser_tot2=0;
				var ser_tot1=0;
				var ser_tot4=0;
		//		var ser_tot5=0;
				//var sales_rtn=0;
                for (var i = 0; i < aaData.length; i++) {
					//alert(aaData[[i]][5]);
                   // p = (aaData[aiDisplay[i]][2]).split('__');
					ser_tot1 += parseFloat(aaData[[i]][3]);
					ser_tot2 += parseFloat(aaData[[i]][4]);
					ser_tot3 += parseFloat(aaData[[i]][4]);
		total_return_for_cal += parseFloat(aaData[[i]][4]);
		
	//				ser_tot5 += parseFloat(aaData[[i]][6]);
						
//						window.alert(ser_tot5);
                }
                var nCells = nRow.getElementsByTagName('th');
				//nCells[3].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot1, "", 2, ",", ".")+' </div>';
				
				
				//nCells[4].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot2, "", 2, ",", ".")+' </div>';
				nCells[4].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot3, "", 2, ",", ".")+' </div>';
				 
				// total_return_for_cal=ser_tot2;
				
				 sales_rtn_tot_cost=ser_tot1;
				 sales_rtn_tot_val=ser_tot3;
	//			$('#sales-rtn-cost-fld').val(sales_rtn_tot_cost);
//				$('#sales-rtn-val-fld').val(sales_rtn_tot_val);
				$('#total_return_tbl').text(accounting.formatMoney(sales_rtn_tot_val, "", 2, ",", "."));
total_return_for_final= ser_tot3;

		$('#total_return_fld').val(sales_rtn_tot_val);			//	$('#sales-rtn-val-tbl').text(accounting.formatMoney(sales_rtn_tot_val, "", 2, ",", "."));
				
				
				var balance=0;


var tempBalance = 0;
balance = total_sale_for_cal;
tempBalance = total_payment_for_cal + total_return_for_cal;
balance = balance - tempBalance;
//grand_rtn_tot = grand_rtn_tot - total_return_payments;
//window.alert(grand_rtn_tot);

/*$('#balance_tbl').text(accounting.formatMoney(balance, "", 2, ",", "."));*/

					set();
              
            }
					    });
					}

//function loadGridSalesPayment()
function loadGridSalesPayment() {
			var srh_from_date=$('#srh_from_date').val();
			var srh_to_date=$('#srh_to_date').val();
			var srh_customer_id=$('#srh_customer_id').val();
			$('#f-date-txt').text(srh_from_date);
			$('#t-date-txt').text(srh_to_date);
			var srh_warehouse_id=$('#srh_warehouse_id').val();
				var srh_type=	'sale';//$('#srh_type').val();
	var srh_payment_term=$('#srh_payment_term').val();
	var ss_user_id=$('#srh_user_id').val();
		//	alert(ss_user_id);
							
					    $('#sales_payment_table').DataTable({
							
							"ajax": {
							'type': 'POST',
							'url': '<?php echo base_url('reports/get_list_payments_for_balance_report');?>',
							'data': {
							   srh_customer_id : srh_customer_id,
							   srh_from_date: srh_from_date,
							   srh_to_date: srh_to_date,
							   srh_warehouse_id: srh_warehouse_id,
							   srh_type: srh_type,
							   srh_payment_term: srh_payment_term,
							   ss_user_id: ss_user_id,
							}
							},
					        "bDestroy": true,
					        "iDisplayLength": 20,
							"order": [[ 1, "desc" ]], "fnFooterCallback": function (nRow, aaData, iStart, iEnd, aiDisplay) {
				
                var pq = 0, sq = 0, bq = 0, pa = 0, grand_tot = 0, tech_tot = 0, parts_tot=0 , ser_tot=0;
				//alert(aaData.length);
				var ser_tot3=0;
				var ser_tot2=0;
				var ser_tot1=0;
				var ser_tot4=0;
				var pay_type='';
				//var tot_payment=0;
				var sales_t =0;
				var sales_return_tot=0;
				var cash_return_tot=0;				
				var sales_grn_tot=0;
				var return_cash_tot=0;
				
				
				//var sales_rtn=0;
				for (var i = 0; i < aaData.length; i++) {
					//alert(aaData[[i]][5]);
					// p = (aaData[aiDisplay[i]][2]).split('__');
					ser_tot1 += parseFloat(aaData[[i]][3]);
					//pay_type=aaData[[i]][4];
					if(aaData[[i]][3]=='sale'){
						//tot_payment += parseFloat(aaData[[i]][5]);
						if(aaData[[i]][4]=='Return_Cash'){
							//alert(aaData[[i]][4]);
							return_cash_tot += parseFloat(aaData[[i]][5]);
							$('#return_cash_fld').val(return_cash_tot);
							$('#return_cash_tbl').text(accounting.formatMoney(return_cash_tot, "", 2, ",", "."));
							}else{
								ser_tot2 += parseFloat(aaData[[i]][5]);
								//sales_t += parseFloat(aaData[[i]][5]);
								}
					//var temp=0;
					}else{
					//ser_tot2 -= parseFloat(aaData[[i]][6]);
					if(aaData[[i]][3]=='grn'){
					sales_grn_tot += parseFloat(aaData[[i]][5]);
					}
					if(aaData[[i]][3]=='sales_return'){
						//alert(aaData[[i]][4]);
						sales_return_tot += parseFloat(aaData[[i]][5]);
						if(aaData[[i]][4]=='Return Payment'){
						//alert(aaData[[i]][4]);
						sales_return_tot += parseFloat(aaData[[i]][5]);
						$('#total_return_payment_tbl').text(accounting.formatMoney(sales_return_tot, "", 2, ",", "."));
					}
					}}//$('#return_cash_tbl').text(accounting.formatMoney(return_cash_tot, "", 2, ",", "."));
					}
					var nCells = nRow.getElementsByTagName('th');
					nCells[5].innerHTML = '<div class="text-right">'+accounting.formatMoney(ser_tot2, "", 2, ",", ".")+' </div>';
					total_payment_for_cal = ser_tot2 - return_cash_tot;
					var sales_rtn_tot_cost=ser_tot1;
					var sales_rtn_tot_val=ser_tot2;
					//alert(ser_tot2);
					$('#total_payment_tbl').text(accounting.formatMoney(ser_tot2, "", 2, ",", "."));
					$('#total_payment_fld').val(ser_tot2);
					$('#total_return_payments_tbl').text(accounting.formatMoney(sales_return_tot, "", 2, ",", "."));
					set();			
	}});	
	
}


function cheque(){
	
	
//$('#add_category').click(function(){
		var customer_id=$('#srh_customer_id').val();
		var date = $('#srh_to_date').val();
		//var base_url='http:127.0.0.1/akaki/';//
		//$("#base_url").val();
		
		var warehouse_id = $('#srh_warehouse_id').val();
$.get("<?php echo base_url() ?>reports/get_unrealized_cheques?cus_id="+customer_id+"&warehouse_id="+warehouse_id+"&date="+date)
	  .done(function( data ) {
		  var obj = jQuery.parseJSON(data);
		  var old_credit_amount=obj.old_credit_amount;

		  $('#unrealized_cheque_tbl').text(accounting.formatMoney(old_credit_amount, "", 2, ",", "."));
		  
		  $('#unrealized_cheque_fld').val(old_credit_amount);
	  set();
	  });

	//});
	
	
	}

//setInterval( function(){
	
	//set();
	//window.alert("INT");
	//},3000)



function set(){
	
	
	//alert(balance);	
/*		  
tot_ret  = document.getElementById('total_return_fld').value;
ret_cash  = document.getElementById('return_cash_fld').value;
*/
//alert(tot_ret);

//$('#return_balance_tbl').text(tot_ret- ret_cash, "", 2, ",", ".");

	var total_sales_fld=parseFloat($('#total_sales_fld').val());
	var total_return_fld=parseFloat($('#total_return_fld').val());
	
	
	var total_payment_fld=parseFloat($('#total_payment_fld').val());
	var return_cash_fld=parseFloat($('#return_cash_fld').val());
	var total_return_fld=parseFloat($('#total_return_fld').val());
	
	var unrealized_cheque_fld=parseFloat($('#unrealized_cheque_fld').val());

	
	var balance=total_sales_fld-total_return_fld-total_payment_fld;
	
	var unrealized_balance = total_sales_fld-total_return_fld-total_payment_fld - unrealized_cheque_fld;
	
//	alert(total_return_fld);

$('#return_balance_fld').val(total_return_fld- return_cash_fld);
$('#return_balance_tbl').text(accounting.formatMoney(total_return_fld- return_cash_fld, "", 2, ",", "."));

	//+return_cash_fld;

//alert(balance);

$('#balance_fld').val(balance);
$('#balance_tbl').text(accounting.formatMoney(balance, "", 2, ",", "."));


$('#unrealized_balance_fld').val(unrealized_balance);
$('#unrealized_balance_tbl').text(accounting.formatMoney(unrealized_balance, "", 2, ",", "."));



//var balance2 = total_return_fld - 	return_cash_fld;
//	$('#return_balance_fld').val(balance);

/*
tot_sale_=0;
tot_return_=0;
value_ =0;

tot_sale_    = total_sale_for_cal;
tot_return_  = total_return_for_final;*/
//alert(tot_return_);
   
   
//tot_payment_= sales_rtn_tot_val; //document.getElementById('total_payment_fld').value;


//value_ = tot_return_ +tot_payment_ ;
//alert(sales_rtn_tot_val);
//value_ = tot_sale_ - value_;

//$('#balance_fld').val(value_);
	
	}

function fbs_click(url) {
//	var srh_from_date=$('#srh_from_date').val();
	var srh_to_date=$('#srh_to_date').val();
	var srh_warehouse_id=$('#srh_warehouse_id').val();
	var srh_customer_id=$('#srh_customer_id').val();
	var csn = $('#srh_customer_id option:selected').text();
//var d=new Date(srh_to_date);
//var srh_to_date = d.getDate()  + "-" + (d.getMonth()+1) + "-" + d.getFullYear();
			
	u=location.href;
	t=document.title;
	url='<?php echo base_url();?>'+'reports/customer_balance?type=e&wh='+srh_warehouse_id+'&dt='+srh_to_date+'&cs='+srh_customer_id+'&csn='+csn;
	window.open(url,'sharer','toolbar=0,status=0,width=750,height=436, left=10, top=10,scrollbars=yes');return false;
}

$(window).load(function () {    
    //"use strict";
   // $("#loader").fadeOut();
   // $("#preloader").delay(350).fadeOut("slow");
});

$('#add_category').click(function(){
	$('#loader').fadeIn();
	//alert('s');
	});

</script>
</body>
<!-- end: BODY -->
</html>