@extends('layout')

@section('content')
<style type="text/css">
	.table_td_align_top > tbody > tr > td{
		vertical-align : top;
	}
</style>
<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			@if($sbg == "piutang_awal")
			<li><a href="{{ url('piutangawal') }}"><i class="icon-home2 position-left"></i> Piutang Awal</a></li>
			@else
			<li><a href="{{ url('piutang') }}"><i class="icon-home2 position-left"></i> Piutang</a></li>
			@endif
			<li class="active"></li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">{{ (isset($penjualanBayar)) ? "Edit " : "" }}Penerimaan Tagihan</h5>
			<div class="heading-elements">
				<ul class="icons-list">
					@if($sbg == "piutang_awal")
					<li><a href="{{ url('piutangawal') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
					@else
					<li><a href="{{ url('piutang') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
					@endif
				</ul>
			</div>
		</div>

		<form class="" method="post" action="{{ url('penjualan/simpan_terima_tagihan') }}">
			{{ csrf_field() }}

			<input type="hidden" name="id_penjualan_transaksi_bayar" value="{{ (isset($penjualanBayar)) ? $penjualanBayar->id : ((isset($piutangAwalBayar)) ? $piutangAwalBayar->id : '') }}">
			<input type="hidden" name="id_jual_bayar" value="{{ (isset($penjualanBayar)) ? $penjualanBayar->id_jualbayar : '' }}">
			<input type="hidden" name="id_piutangawal_bayar" value="{{ (isset($piutangAwalBayar)) ? $piutangAwalBayar->id_piutangawal_bayar : '' }}">
			<input type="hidden" name="sbg" value="{{$sbg}}">

			<div class="panel-body">
				<div class="row">
					<div class="col-sm-3 col-md-3">
						<div class="form-group">
							{{ Form::label("Pelanggan", "", ["class"=>"control-label"]) }}
							{{ Form::hidden("id_pelanggan", $penjualanById->id_pelanggan) }}
							{{ Form::hidden("mata_uang", $penjualanById->mata_uang) }}
							{{ Form::text("nama_pelanggan", $penjualanById->nama_pelanggan, ["class"=>"form-control", "disabled"=>"disabled"]) }}
						</div>
					</div>
					<div class="col-sm-4 col-md-4">
						<div class="form-group">
							{{ Form::label("Akun Terima", "", ["class"=>"control-label"]) }}
							{{ Form::select("id_akun", $akunTerima, (isset($penjualanBayar->id_akun)) ? $penjualanBayar->id_akun : ((isset($piutangAwalBayar->id_akun)) ? $piutangAwalBayar->id_akun : null), ["class"=>"form-control set_selected"]) }}
						</div>
					</div>
				</div>

				<div class="row">
					<div class="col-sm-3 col-md-3">
						<div class="form-group">
							{{ Form::label("No. Penerimaan", "", ["class"=>"control-label"]) }}
							{{ Form::text("no_transaksi_bayar", (isset($penjualanBayar)) ? $penjualanBayar->no_transaksi_bayar : ((isset($piutangAwalBayar)) ? $piutangAwalBayar->no_transaksi_bayar : 0), ["class"=>"form-control", (isset($penjualanBayar)) ? "" : ((isset($piutangAwalBayar)) ? "" : "")]) }}
						</div>
					</div>
					<div class="col-sm-3 col-md-3">
						<div class="form-group">
							{{ Form::label("Tanggal Terima", "", ["class"=>"control-label"]) }}
							{{ Form::text("tgl_bayar", (isset($penjualanBayar)) ? date("d/m/Y", strtotime($penjualanBayar->tgl_bayar)) : ((isset($piutangAwalBayar)) ? date("d/m/Y", strtotime($piutangAwalBayar->tgl_bayar)) : date("d/m/Y")), ["class"=>"form-control set_datepicker"]) }}
						</div>
					</div>
				</div>

			</div>

			<table class="table table_td_align_top">
				<thead>
					<tr>
						<th>No. Tagihan</th>
						<th>Keterangan</th>
						<th>Total</th>
						<th>Sisa Tagihan</th>
						<th>Pembayaran</th>
					</tr>
				</thead>
				<tbody>
					@if(isset($penjualanBayar))
						@foreach($penjualanByPelanggan AS $key=>$value)
						@php 
						//$sisa_tagihan = $value->nominal_netto-$value->total_bayar+$value->jumlah_bayar; 
						// if($sisa_tagihan==0) continue;
						$retur = DB::table('a_pen_penjualan_retur_groupby_penjualan')->where('id_penjualan', $value->id_penjualan);
						if($retur->count() > 0) {
							$sisa_tagihan = ($value->nominal_netto-$value->total_bayar+$value->jumlah_bayar)-$retur->first()->nominal_total;
						} else {
							$sisa_tagihan = $value->nominal_netto-$value->total_bayar+$value->jumlah_bayar;
						}
						@endphp
						<tr>
							<td>{{ nomor_penjualan($value) }}</td>
							<td>{{ $value->keterangan }}</td>
							<td class="text-right">{{ $value->simbol.rupiah2($value->nominal_netto, 2) }}</td>
							<td class="text-right">{{ $value->simbol.rupiah2($sisa_tagihan, 2) }}</td>
							<td>
								{{ Form::number("jml_bayar[$value->id_penjualan]", $value->jumlah_bayar, ["class"=>"form-control text-right", "min"=>0, "step"=>0.01, "onkeyup"=>"setFormatMoney($(this), 'td'), set_total_bayar($(this))", ($sisa_tagihan == 0) ? "readonly" : ""]) }}
								{{ Form::hidden("sebagai[$value->id_penjualan]", "piutang_penjualan") }}
								<span class="help-block text-nominal text-right" style="font-weight: bold;">Rp 0</span>
							</td>
						</tr>
						@endforeach
					@elseif(isset($piutangAwalBayar))
						@foreach($piutangAwalByPelanggan AS $key=>$value)
						@php 
						$sisa_tagihan = $value->nominal_netto-$value->total_bayar+$value->jumlah_bayar; 
						// if($sisa_tagihan==0) continue;
						@endphp
						<tr>
							<td>{{ nomor_penjualan($value) }}</td>
							<td>{{ $value->keterangan }}</td>
							<td class="text-right">Rp.{{ rupiah2($value->nominal_netto, 2) }}</td>
							<td class="text-right">Rp.{{ rupiah2($sisa_tagihan, 2) }}</td>
							<td>
								{{ Form::number("jml_bayar[$value->id]", $value->jumlah_bayar, ["class"=>"form-control text-right", "min"=>0, "step"=>0.01, "onkeyup"=>"setFormatMoney($(this), 'td'), set_total_bayar($(this))", ($sisa_tagihan == 0) ? "readonly" : ""]) }}
								{{ Form::hidden("sebagai[$value->id]", "piutang_awal") }}
								<span class="help-block text-nominal text-right" style="font-weight: bold;">Rp 0</span>
							</td>
						</tr>
						@endforeach
					@else
						@php 
						$retur = DB::table('a_pen_penjualan_retur_groupby_penjualan')->where('id_penjualan', $penjualanById->id_penjualan);
						if($retur->count() > 0) {
							$sisa_tagihan = ($penjualanById->nominal_netto-$penjualanById->total_bayar)-$retur->first()->nominal_total;
						} else {
							$sisa_tagihan = $penjualanById->nominal_netto-$penjualanById->total_bayar;
						}
						@endphp
						<tr class="info">
							<td>{{ nomor_penjualan($penjualanById) }}</td>
							<td>{{ $penjualanById->keterangan }}</td>
							<td class="text-right">{{ $penjualanById->simbol.rupiah2($penjualanById->nominal_netto, 2) }}</td>
							<td class="text-right">{{ $penjualanById->simbol.rupiah2($sisa_tagihan, 2) }}</td>
							<td>
								{{ Form::number("jml_bayar[$penjualanById->id_penjualan]", $sisa_tagihan, ["class"=>"form-control text-right", "min"=>0, "step"=>0.01, "onkeyup"=>"setFormatMoney($(this), 'td'), set_total_bayar($(this))"]) }}
								{{ Form::hidden("sebagai[$penjualanById->id_penjualan]", $penjualanById->sebagai) }}
								<span class="help-block text-nominal text-right" style="font-weight: bold;">Rp 0</span>
							</td>
						</tr>
						@foreach($penjualanByPelanggan AS $key=>$value)
						@if(checkPembayaran($value->id_penjualan) == 0 || checkPembayaran($value->id_penjualan) == 1)
						@php 
						$retur = DB::table('a_pen_penjualan_retur_groupby_penjualan')->where('id_penjualan', $value->id_penjualan);
						if($retur->count() > 0) {
							$sisa_tagihan = ($value->nominal_netto-$value->total_bayar)-$retur->first()->nominal_total;
						} else {
							$sisa_tagihan = $value->nominal_netto-$value->total_bayar;
						}
						@endphp
						<tr>
							<td>{{ nomor_penjualan($value) }}</td>
							<td>{{ $value->keterangan }}</td>
							<td class="text-right">{{ $value->simbol.rupiah2($value->nominal_netto, 2) }}</td>
							<td class="text-right">{{ $value->simbol.rupiah2($sisa_tagihan, 2) }}</td>
							<td>
								{{ Form::number("jml_bayar[$value->id_penjualan]", 0, ["class"=>"form-control text-right", "min"=>0, "step"=>0.01, "onkeyup"=>"setFormatMoney($(this), 'td'), set_total_bayar($(this))"]) }}
								{{ Form::hidden("sebagai[$value->id_penjualan]", $value->sebagai) }}
								<span class="help-block text-nominal text-right" style="font-weight: bold;">Rp 0</span>
							</td>
						</tr>
						@endif
						@endforeach
					@endif
				</tbody>
			</table>

			<div class="panel-footer">
				<div class="col-sm-12 col-md-12 text-right">
					<div class="form-group">
						<p class="form-control-static" style="font-size: 25px" id="total_bayar">Rp 0</p>
					</div>
				</div>
				<div class="col-sm-12 col-md-12 text-right">
					@if($sbg == "piutang_awal")
					<a href="{{ url('piutangawal') }}" class="btn btn-md btn-default">Batal</a>
					@else
					<a href="{{ url('piutang') }}" class="btn btn-md btn-default">Batal</a>
					@endif
					<button type="submit" class="btn btn-md btn-success">Simpan</button>
				</div>
			</div>

		</form>
	</div>
</div> <!-- end : .content -->

<!----Modal---->
<script type="text/javascript">
	$(document).ready(function () {
		$(".set_selected").select2();

		$("input[name^='jml_bayar']").trigger("keyup");
	});

	function set_total_bayar(this_) {
		var temp_jml_bayar = 0;
		var input_jml_bayar = $("input[name^='jml_bayar']");
		var temp_item;
		$.each(input_jml_bayar, function (id, item) {
			temp_item = (item.value==null)||(item.value==NaN)||(item.value=="") ? 0 : item.value;
			temp_jml_bayar += parseFloat(temp_item);
		});

		$("#total_bayar").html(accounting.formatMoney(temp_jml_bayar));
	}


	function setFormatMoney(this_, p) {
		var div = this_.parents(p);
		var data = this_.val();
		if(data==""){
			data = 0;
		}
		div.find(".help-block").html(accounting.formatMoney(data));
	}
</script>

@endsection