@extends('layout')

@section('js_script')
@endsection

@section('content')

@php
	function isNegative($number) {
		if ($number < 0) {
			return "(".currency(abs($number)).")";
		} else {
			return currency($number);
		}
	}

	function currency($number) {
		return number_format($number, 2, ',', '.');
	}
@endphp
<style type="text/css">
	.details-control{
		cursor: pointer;
	}
</style>

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="index.html"><i class="icon-home2 position-left"></i> Laporan</a></li>
			<li class="active">Laba Rugi</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Laba Rugi</h5>
	    </div>
	    <div class="panel-body">
	    	<div class="row">
	    		<div class="col-sm-8 col-md-8">
			    	<form class="form" method="get" action="" id="form-filter-neraca-saldo">
			    		<div class="form-group">
			    			{{ Form::label("Tanggal", "", ['class'=>'control-label col-sm-2 col-md-2']) }}
			    			<div class="col-sm-3 col-md-3" >
				    			{{ Form::text("tanggal_awal", date("d-m-Y"), ['class'=>'form-control set_datepicker','id'=>'tanggal_awal']) }}
				    		</div>
			    			<div class="col-sm-3 col-md-3" >
				    			{{ Form::text("tanggal_akhir", date("d-m-Y"), ['class'=>'form-control set_datepicker','id'=>'tanggal_akhir']) }}
				    		</div>
			    			<div class="col-sm-3 col-md-3 btn-group">
				    			<button type="submit" class="btn btn-sm btn-info">
				    				<i class="fa fa-filter"></i> Filter
				    			</button>
				    			<a href="javascript:void(0)" id="btn_excel" class="btn btn-sm btn-success"><i class="fa fa-file-excel-o"></i> Excel</a>
				    		</div>
			    		</div>
			    		<!-- <div class="form-group">
			    			{{ Form::label("", "", ['class'=>'control-label col-sm-2 col-md-2']) }}
    			    		<div class="col-sm-3 col-md-3 btn-group">
    					    	<a href="javascript:void(0)" id="btn_excel" class="btn btn-sm btn-success"><i class="fa fa-file-excel-o"></i> Excel</a>
    			    		</div>
			    		</div> -->
			    	</form>	
	    		</div>

	    		<div class="col-sm-4 col-md-4">
			    	<form class="form" action="#">
			    		<div class="form-group">
			    			<div class="col-sm-12 col-md-12 text-right">
							
							</div>
						</div>
					</form>
	    		</div>
	    		
	    		<div class="col-sm-12 col-md-12" style="margin-top: 2%;" id="div-tabel-laba-rugi">
	    		
	    		<div class="table-responsive">
	    			<table class="table table-stripped">
		    		<?php
		    		$total_pendapatan = 0;
		    		if(sizeof($pendapatan) > 0){
		    			foreach ($pendapatan as $key => $value) {
		    				if(sizeof($value['child'])==0) continue;
		    				echo "<tr>";
		    				
			    				echo "<th colspan='3' style='text-align: left;'>".$value['nama_perkiraan']."</th>";

		    				echo "</tr>";

		    				if(sizeof($value['child']) > 0){
		    					foreach ($value['child'] as $keyC => $valueC) {
		    						if($valueC['total_saldo']==0) continue;

		    						echo "<tr>";
		    						echo "<td>$valueC[nama_perkiraan]</td>";
		    						echo "<td style='text-align : right;'>".isNegative($valueC['total_saldo'])."</td>";
		    						echo "<td>&nbsp;</td>";
		    						echo "</tr>";

		    						if($valueC['saldo_normal']==$value['saldo_normal']){
		    							$total_pendapatan += $valueC['total_saldo'];
		    						}else{
		    							$total_pendapatan -= $valueC['total_saldo'];
		    						}
		    					}

	    					echo "<tr>";
	    					echo "<th colspan='2' style='text-align: left;'>Total $value[nama_perkiraan]</th>";
	    					echo "<th style='text-align : right;'>".isNegative($total_pendapatan)."</th>";
	    					echo "</tr>";

		    				}

		    			}
		    		}
		    		?>

		    		<?php
		    		$total_hpp = 0;
		    		
		    		if(sizeof($hpp) > 0){

	    				echo "<tr>";
		    				echo "<th colspan='3' style='text-align: left;'>Harga Pokok Penjualan (HPP)</th>";
	    				echo "</tr>";

		    			foreach ($hpp as $key => $value) {
		    				if(sizeof($value['child'])==0) continue;
		    				$sub_total_hpp = 0;
		    				if(sizeof($value['child']) > 0){

		    					foreach ($value['child'] as $keyC => $valueC) {
		    						if($valueC['total_saldo']==0) continue;

		    						if($valueC['saldo_normal']==$value['saldo_normal']){
		    							$sub_total_hpp += $valueC['total_saldo'];
		    							$saldo_hpp = $valueC['total_saldo'];
		    						}else{
		    							$sub_total_hpp -= $valueC['total_saldo'];
		    							$saldo_hpp = $valueC['total_saldo']*-1;
		    						}

		    					echo "<tr>";
		    					echo "<td style='text-align: left;'>$valueC[nama_perkiraan]</td>";
		    					echo "<td style='text-align : right;'>".isNegative($saldo_hpp)."</td>";
		    					echo "<td style='text-align : right;'>&nbsp;</td>";
		    					echo "</tr>";

		    					}
		    				
			    				if($sub_total_hpp==0) continue;

		    					// echo "<tr>";
		    					// echo "<td style='text-align: left;'>$value[nama_perkiraan]</td>";
		    					// echo "<td style='text-align : right;'>".rupiah2($sub_total_hpp)."</td>";
		    					// echo "<td style='text-align : right;'>&nbsp;</td>";
		    					// echo "</tr>";

		    					$total_hpp += $sub_total_hpp;
		    				}

		    			}
    					echo "<tr>";
    					echo "<th colspan='2' style='text-align: left;'>Total Harga Pokok Penjualan (HPP)</th>";
    					echo "<th style='text-align : right;'>".isNegative($total_hpp)."</th>";
    					echo "</tr>";

		    		}
		    		?>
		    		
		    		<?php
		    		$total_beban_usaha = 0;
		    		if(sizeof($beban) > 0){
		    			$total_beban = 0;

	    				echo "<tr>";
		    				echo "<th colspan='3' style='text-align: left;'>Beban Usaha</th>";
	    				echo "</tr>";

		    			foreach ($beban as $key => $value) {
		    				if(sizeof($value['child'])==0) continue;

		    				if(sizeof($value['child']) > 0){
		    					$total_beban = 0;

		    					foreach ($value['child'] as $keyC => $valueC) {
		    						if($valueC['total_saldo']==0) continue;

		    						if($valueC['saldo_normal']==$value['saldo_normal']){
		    							$total_beban += $valueC['total_saldo'];
		    						}else{
		    							$total_beban -= $valueC['total_saldo'];
		    						}
		    					}
		    				
			    				if($total_beban==0) continue;

		    					echo "<tr>";
		    					echo "<td style='text-align: left;'>$value[nama_perkiraan]</td>";
		    					echo "<td style='text-align : right;'>".isNegative($total_beban)."</td>";
		    					echo "<td style='text-align : right;'>&nbsp;</td>";
		    					echo "</tr>";

		    					$total_beban_usaha += $total_beban;

		    				}

		    			}
    					echo "<tr>";
    					echo "<th colspan='2' style='text-align: left;'>Total Beban Usaha</th>";
    					echo "<th style='text-align : right;'>".isNegative($total_beban_usaha)."</th>";
    					echo "</tr>";

		    		}
		    		?>
		    		</table>

		    		<table class="table">
		    		<?php
		    		$labaRugi = $total_pendapatan-$total_hpp-$total_beban_usaha;
		    		echo "<tr>";
		    			echo "<th colspan='2' style='text-align : left;'>LABA (RUGI)</th>";
		    			echo "<th style='text-align : right;'>".isNegative($labaRugi)."</th>";
		    		echo "</tr>";
		    		?>
		    		</table>
		    	</div>

	    		</div>

	    	</div> <!-- end : row -->

	    </div>
	</div>
</div>

<script type="text/javascript">
	var table;
	var urlParam;
	var urlData;

	var arr_key = [];
	var arr_detail = [];
	var base_url = "{{ url('/') }}";

$(document).ready(function(){
	$(".set_datepicker").datepicker({
		format : "dd-mm-yyyy",
		autoclose : true,
      	zIndexOffset : 9999,
	});

	urlParam = getUrlParams();
	urlData = '{{ url("laporan/get_data_neraca_saldo") }}';

	if(Object.keys(urlParam).length > 1){
		urlData += '?tanggal_awal='+urlParam['tanggal_awal']+'&tanggal_akhir='+urlParam['tanggal_akhir'];
	} else {
		urlData += '?tanggal_awal='+$("#tanggal_awal").val()+'&tanggal_akhir='+$("#tanggal_akhir").val();
	}
	
	table = $('#tabel-neraca-saldo').DataTable({
		serverSide : true,
		processing : true,
		deferRender : true,
		searchDelay: 1500,
		ajax : urlData,
		columns : [
			{data:'kode_perkiraan', name:'kode_perkiraan'},
			{data:'nama_perkiraan', name:'nama_perkiraan'},
			{data:'D', name:'D'},
			{data:'K', name:'K'},
		],
		order : false,
		scrollX : true,
		createdRow : function(row, data, index){
			console.log(data);
			arr_key.push(data.kode_perkiraan);
			arr_detail.push(data.detail);

			// $("td", row).addClass('details-control');
			$("td", row).eq(2).addClass('text-right').html(accounting.formatMoney(data['D']));
			$("td", row).eq(3).addClass('text-right').html(accounting.formatMoney(data['K']));
		}
	});

});

	$("#btn_excel").click(function(evt){
		window.open(base_url+'/laporan/laba_rugi/excel?tanggal_awal='+$("#tanggal_awal").val()+'&tanggal_akhir='+$("#tanggal_akhir").val());
	})

</script>

@endsection