@extends('layout')


@section('content')

<style type="text/css">
	 .table-form>tbody>tr>th, .table-form>tfoot>tr>th, .table-form>thead>tr>td, .table-form>tbody>tr>td, .table-form>tfoot>tr>td{
		    padding: 5px;
		   -- vertical-align: middle;
		}
		table.table-form tbody>tr.selected, table.table-form tbody>tr.selected td, table.table-form tbody>tr>.selected{
		    color: white !important;
		    background-color: #49b6d6 !important;
		}

	 table, th, td, input, textarea, select {
            color:  black !important; 
        }
        .help-block {
 
    margin-top: 0px!important;
    margin-bottom: 0px!important;
}
</style>

<div class="page-header page-header-default">
	<div class="breadcrumb-line">
		<ul class="breadcrumb">
			<li><a href="#"><i class="icon-home2 position-left"></i> Penjualan</a></li>
			<li class="active">Retur Penjualan (Non Faktur)</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Retur Penjualan  (Non Faktur)</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('penjualan/retur-penjualan-nonfaktur') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<form class="form-horizontal" id="form_retur_penjualan" method="POST" action="{{ $url_submit }}" autocomplete="off" >
	    		{{ csrf_field() }}
	    		{{ method_field($method) }}
	    		@if(isset($data))
	    		{{ Form::hidden('id', $data->id) }}
	    		@endif

        	<div class="col-sm-6">
	    		<div class="form-group">
	    			{{ Form::label("Label CV", "", ['class'=>'control-label col-sm-4']) }}
	    			<div class="col-sm-8">
	    				<div class="checkbox">
	    					<label>
		    					{{ Form::checkbox("cv", '1', true, ['class'=>'styled']) }}
	    					</label>
	    				</div>
	    			</div>
	    		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Lokasi</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="lokasi">
        					@foreach($lokasi as $key=>$value)
        					<option value="{{ $value->id_lokasi }}">{{ $value->nama_lokasi}}</option>
        					@endforeach
        				</select>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Pelanggan</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="pelanggan">
        					@foreach($pelanggan as $value)

        					<option value="{{ $value->id_pelanggan }}">{{ $value->pic.' - '.$value->nama }}</option>

        					@endforeach
        				</select>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Input</label>
        			<div class="col-sm-8">
        				<input class="form-control datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ date('d-m-Y') }}" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Retur</label>
        			<div class="col-sm-8">
        				<input class="form-control datepicker" type="text" name="tanggal_retur" placeholder="Tanggal Retur" value="{{ date('d-m-Y') }}" onchange="get_nomor($(this))" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No. Retur</label>
        			<div class="col-sm-3 col-md-3">
        				{{ Form::text('no_retur', 0, ['class'=>'form-control text-right', 'placeholder'=>'Nomor']) }}
        			</div>
        			<div class="col-sm-5 col-md-5">
        				{{ Form::text("sufix_no_retur", 0, ['class'=>'form-control text-uppercase']) }}
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Keterangan</label>
        			<div class="col-sm-8">
        				<textarea class="form-control" name="keterangan" rows="2"></textarea>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Mata Uang</label>
        			<div class="col-sm-6 col-md-6">
        				<select class="form-control select-search" name="currency" id="currency">
        					@foreach($mata_uang as $key => $value)
        						<option value="{{ $value->id_matauang }}" data-inisial="{{ $value->inisial }}">{{ $value->nama_matauang }} - {{ $value->inisial }}</option>
        					@endforeach
        				</select>
        			</div>
        		</div>
        		<div class="form-group" id="field_kurs">
        			<label class="control-label col-sm-4">Kurs (IDR/USD)</label>
        			<div class="col-sm-8">
        				{{ Form::label("Kurs IDR/USD", "", ["class"=>"control-label"]) }}
	    				{{ Form::number("kurs", 1, ["class"=>"form-control","min"=>"0","id"=>"kurs","step"=>0.01]) }}
	    				<span class="help-block text-right" style="font-weight: bold;" id="nominal_kurs">Rp0</span>
        			</div>
        		</div>
        	</div>

        	<div class="col-sm-6">
        	</div>

        	<div class="row col-sm-12">
			    <h5 class="panel-title">Detail Barang</h5>
	        	<div class="col-sm-6">
		        	<div class="form-group">
		        		<button type="button" class="btn btn-primary" id="btn_add_brg"><i class="fa fa-plus"></i> Tambah Barang</button>
		        	</div>
		        </div>
	        </div>

        	<div class="col-ms-12">

        	<div class="responsive">
	        	<table class="table table-stripped" id="table_barang">
			    	<thead>
						<tr>
							<th width="25%">Nama Barang</th>
							<th width="15%">Jml. Retur</th>
							<th width="15%">Satuan</th>
							<th width="15%">Harga Satuan</th>
							<th width="15%">Potongan</th>
							<th width="15%">Nominal</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td>
								<select name="barang[id_barang][]" class="form-control select-search">
									@php
										print_r($barang);
									@endphp
								</select>
							</td>
							<td>
								<input type="number" class="form-control text-right" step="1" min="1" name="barang[jml_retur][]" class="jml_retur" value="0" onkeyup="setNominal($(this))">
							</td>
							<td>
								<select name="barang[id_satuan][]" class="form-control select-search">
									@php
										print_r($satuan);
									@endphp
								</select>
							</td>
							<td>
								<input type="number" class="form-control text-right" step="1" min="0" name="barang[hrg_satuan][]" class="hrg_satuan" value="0" onkeyup="setFormatMoney($(this), 'td'), setNominal($(this))">
								<span class="help-block currency text-right">Rp.0</span>
							</td>
							<td>
								<input type="number" class="form-control text-right" step="1" min="0" name="barang[potongan][]" class="potongan" value="0" onkeyup="setFormatMoney($(this), 'td'), setNominal($(this))">
								<span class="help-block currency text-right">Rp.0</span>
							</td>
							<td class="nominal text-right">
								<input type="hidden" name="barang[total][]" class="nominal" value="0" onkeyup="setFormatMoney($(this), 'td')">
								<span class="help-block currency text-right">Rp.0</span>
							</td>
							<td></td>
						</tr>
					</tbody>
					<tfoot>

					</tfoot>
			    </table>
			</div>

        	</div>

        	<hr>

            <div class=" col-md-6 col-sm-12 m-l-5">
             
              <div class="table-responsive">
	    	
		           <table class="table table-bordered table-form">
			    	<tbody>
			    		<tr class="active">
		             		<td colspan="2">Total Penjualan</td>
		             	</tr>
		             	<tr>
							<td>Sub Total</td>
		                    <td><input type="hidden" name="td_total" value="0" readonly="">
		                    	<p class="help-block currency pull-right" id="td_total">Rp 0</p>
		                    </td>
						</tr>
						
		                {{-- <tr>
							<td>Potongan</td>
		                    <td><input type="text" name="td_potongan" class="form-control number-only text-right" value="0">
		                    	<p class="help-block currency pull-right" id="help_popup_potongan">Rp 0</p>
		                    </td>
						</tr> --}}
		               
						<tr>
							<td>PPN</td>
		                    <td>
		                    	<select class="form-control select-search" name="pajak">
		        					<option value="0" >Tidak</option>
		        					<option value="1" >Iya</option>
		        				</select>
		                    	<input type="hidden" name="td_pajak" class="form-control number-only text-right" value="0">
		                    	<p class="help-block currency pull-right" id="help_popup_pajak">Rp 0</p>
		                    </td>
						</tr>
		                <tr>
							<td>Nilai Retur</td>
		                    <td><p class="help-block currency pull-right" id="td_netto">Rp 0</p></td>
						</tr>
					</tbody>
			    </table>
             </div>
             </div>


             <div class="col-md-6 col-sm-12">
             	<div class="table-responsive">
             		
             	</div>
             </div>

	    </div>

	    <div class="panel-footer">
	    	<div class="col-sm-12 col-md-12">
	            <div class="form-group pull-right">
	            	<a href="{{ url('penjualan/retur-penjualan-nonfaktur') }}" class="btn btn-default">Batal</a>
	             	<button class="hidden" type="submit" id="btn-submit">Simpan</button>
	             	<button class="btn btn-success" type="submit" id="simpan_retur_penjualan">Simpan</button>
	            </div>

	    	</div>

	    </div>

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


<script type="text/javascript">
var stok_barang_selected = 0;
var tabel;

$(document).ready(function(){
	// init element
	$(".set_select2").select2();
	tabel = $("#table_barang");

	// action element
	$("select[name=pajak]").change(function(){
        if($(this).val()==1){
    		$("input[name=td_pajak]").val(0);    	
    		// $("input[name=td_pajak]").removeAttr('readonly');
        }else{
        	$("input[name=td_pajak]").val(0);    	
        	// $("input[name=td_pajak]").attr('readonly','readonly');
        }
        netto();
    });


	$("#simpan_retur_penjualan").on("click", function(evt){
		if ($("#currency").val() != "1") {
			if ($("#kurs").val() == "" || $("#kurs").val() == "0" || $("#kurs").val() == 0 || $("#kurs").val() == "1" || $("#kurs").val() == 1) {
				$("#kurs").focus();
				alert('Masukkan jumlah kurs (IDR/USD) saat ini');
			} else {
				$("#form_retur_penjualan#btn-submit").trigger("click");
			}
		} else {
			$("#form_retur_penjualan#btn-submit").trigger("click");
		}
	})

	$("#form_retur_penjualan").submit(function(){
		$("#simpan_retur_penjualan").addClass("disabled");
	})

	// trigger data
	$("#form_retur_penjualan input[name=tanggal_retur]").trigger("change");
	
	@if(isset($data))
	edit_data_retur();
	@endif

    setCurrency($("#currency").val());
	checkKurs();
	if ($("#currency").val() == "1") {
		$("#field_kurs").hide('slow');
		$("#kurs").val(1);
	} else {
		$("#field_kurs").show('slow');
		$("#kurs").val(1);
	}

	$("#btn_add_brg").click(function(evt){
		evt.preventDefault();
		var row = '<tr>'+
							'<td>'+
								'<select name="barang[id_barang][]" class="form-control select-search">'+
									@php
										foreach ($barang as $key => $value){
											echo "'".$value."'+";
										}
									@endphp
								'</select>'+
							'</td>'+
							'<td>'+
								'<input type="number" class="form-control text-right" step="1" min="1" name="barang[jml_retur][]" class="jml_retur" value="0" onkeyup="setNominal($(this))">'+
							'</td>'+
							'<td>'+
								'<select name="barang[id_satuan][]" class="form-control select-search">'+
									@php
										foreach ($satuan as $key => $value){
											echo "'".$value."'+";
										}
									@endphp
								'</select>'+
							'</td>'+
							'<td>'+
								'<input type="number" class="form-control text-right" step="1" min="0" name="barang[hrg_satuan][]" class="hrg_satuan" value="0" onkeyup="setFormatMoney($(this), \'td\'), setNominal($(this))">'+
								'<span class="help-block currency text-right">Rp.0</span>'+
							'</td>'+
							'<td>'+
								'<input type="number" class="form-control text-right" step="1" min="0" name="barang[potongan][]" class="potongan" value="0" onkeyup="setFormatMoney($(this), \'td\'), setNominal($(this))">'+
								'<span class="help-block currency text-right">Rp.0</span>'+
							'</td>'+
							'<td class="nominal text-right">'+
								'<input type="hidden" name="barang[total][]" class="nominal" value="0" onkeyup="setFormatMoney($(this), \'td\')">'+
								'<span class="help-block currency text-right">Rp.0</span>'+
							'</td>'+
							'<td><button type="button" onclick="removeRow(this)" class="btn btn-xs btn-danger"><i class="fa fa-times"></i></button></td>'+
						'</tr>';
		$("#table_barang tbody").append(row);
		$(".select-search").select2();
	})

});

function removeRow(_this){
	$(_this).closest ('tr').remove();
	subtotal();
}

$("#currency").on("change", function(evt){
	evt.preventDefault();
	if ($(this).val() == 1) {
		$("#field_kurs").hide('slow');
		$("#kurs").val(1);
		$("#kurs").trigger('keyup');
	} else {
		$("#field_kurs").show('slow');
		$("#kurs").val(1);
		$("#kurs").trigger('keyup');
	}
	setCurrency($(this).val());
})

$("#kurs").on("keyup", function(evt){
	evt.preventDefault();
	setKurs($(this));
})

function checkKurs(){
	if ($("#kurs").val() != "") {
		setKurs($("#kurs"));
	}
}

function setKurs(this_){
	accounting.settings = {
		currency: {
			symbol : "Rp",   // default currency symbol is '$'
			format: { pos : "%s%v", neg : "( %s%v )", zero : "%s%v"}, // controls output: %s = symbol, %v = value/number (can be object: see below)
			decimal : ",",  // decimal point separator
			thousand: ".",  // thousands separator
			precision : 2   // decimal places
		},
		number: {
			precision : 2,  // default precision on numbers is 0
			thousand: ".",
			decimal : ","
		}
	}

	var data = this_.val();
	if(data==""){
		data = 0;
	}

	$("#nominal_kurs").html(accounting.formatMoney(data));
}

function setCurrency(curr){
	accounting.settings = {
		currency: {
			symbol : (curr == "1") ? "Rp" : "$",   // default currency symbol is '$'
			format: { pos : "%s%v", neg : "( %s%v )", zero : "%s%v"}, // controls output: %s = symbol, %v = value/number (can be object: see below)
			decimal : ",",  // decimal point separator
			thousand: ".",  // thousands separator
			precision : 2   // decimal places
		},
		number: {
			precision : 2,  // default precision on numbers is 0
			thousand: ".",
			decimal : ","
		}
	}

	$(".currency").html(accounting.formatMoney());
	$("input[name='barang[hrg_satuan][]'], input[name='barang[potongan][]']").trigger("keyup");
}

@if(isset($data))
function edit_data_retur(){
	$("#form_retur_penjualan [name=lokasi]").val('{{$data->lokasi}}').attr("disabled", "").trigger("change");
	@if($data->cv==1)
		$("#form_retur_penjualan [name=cv]").prop("checked", true).attr("disabled", "").trigger("change");
	@else
		$("#form_retur_penjualan [name=cv]").prop("checked", false).attr("disabled", "").trigger("change");
	@endif
	$("#form_retur_penjualan [name=pelanggan]").val('{{$data->id_pelanggan}}').attr("disabled", "").trigger("change");
	$("#form_retur_penjualan [name=tanggal]").val('{{date_db($data->tanggal, true, 2)}}').attr("disabled", "").removeClass("datepicker").trigger("change");
	$("#form_retur_penjualan [name=tanggal_retur]").val('{{date_db($data->tanggal_retur, true, 2)}}').trigger("change");
	$("#form_retur_penjualan [name=keterangan]").val('{{$data->keterangan}}');
	$("#form_retur_penjualan #currency").val('{{$data->mata_uang}}').trigger("change");
}
@endif

function get_nomor(this_){
	var id_retur = $("#form_retur_penjualan [name=id]").val()||'';
	var date = this_.val();
	var s_date = date.split("-");
	var sufix_no_retur = "/RTR";
	$.ajax({
		url : "{{ url('penjualan/retur-penjualan-nonfaktur/get_nomor') }}",
		type : "get",
		data : {id_retur:id_retur, tanggal:date},
		dataType : "json",
		success : function(result){
			if(id_retur!=""){
				sufix_no_retur = result.sufix_no_retur;
			}
			
			$("input[name=no_retur]").val(result.no_retur);
			$("input[name=sufix_no_retur]").val(sufix_no_retur);
		}
	});
}

function subtotal() {
	var jumlah = 0;
	$("input[name^='barang[total][]']").each(function(id, item) {
		jumlah += parseFloat(item.value);
	});

	$("#td_total").text(accounting.formatMoney(jumlah));
	$("[name=td_total]").val(jumlah);

	netto();
}

function netto(){
	var total = parseFloat($("[name=td_total]").val());
	// var potongan = parseFloat($("[name=td_potongan]").val());
	var potongan = 0;
	var ppn = parseFloat($("[name=td_pajak]").val());

	total = isNaN(total) ? 0 : total;
	potongan = isNaN(potongan) ? 0 : potongan;
	ppn = isNaN(ppn) ? 0 : ppn;

	var netto = parseFloat(0);
	var ppn = parseFloat(0);

	if($('select[name=pajak]').val()==1){
		ppn = parseFloat((total-potongan)*(10/100));
	}
		
	netto = total-potongan+ppn; //+bunga
	// $("[name=td_potongan]").val(potongan);
	// $("#help_popup_potongan").text(accounting.formatMoney(potongan));

	$("[name=td_pajak]").val(ppn);
	$("#help_popup_pajak").text(accounting.formatMoney(ppn));

	$("#td_netto").text(accounting.formatMoney(netto));
	$("[name=td_netto]").val(netto);
}

function setNominal(this_) {
	var row = this_.parents("tr");
	var jml_retur = row.find($("input[name='barang[jml_retur][]']")).val();
	var harga_satuan = row.find($("input[name='barang[hrg_satuan][]']")).val();
	var potongan = row.find($("input[name='barang[potongan][]']")).val();
	var inputTotal = row.find($("input[name='barang[total][]']"));

	jml_retur = isNaN(jml_retur) ? 0 : jml_retur;
	harga_satuan = isNaN(harga_satuan) ? 0 : harga_satuan;
	potongan = isNaN(potongan) ? 0 : potongan;
	var totalvalue = (jml_retur*harga_satuan)-(jml_retur*potongan);
	inputTotal.val(totalvalue).trigger("keyup");

	subtotal();

}

function setFormatMoney(this_, p) {
	var div = this_.parents(p);
	var data = this_.val();
	if(data==""){
		data = 0;
	}

	accounting.settings = {
		currency: {
			symbol : ($("#currency").val() == "1") ? "Rp" : "$",   // default currency symbol is '$'
			format: { pos : "%s%v", neg : "( %s%v )", zero : "%s%v"}, // controls output: %s = symbol, %v = value/number (can be object: see below)
			decimal : ",",  // decimal point separator
			thousand: ".",  // thousands separator
			precision : 2   // decimal places
		},
		number: {
			precision : 2,  // default precision on numbers is 0
			thousand: ".",
			decimal : ","
		}
	}

	div.find(".currency").html(accounting.formatMoney(data));
}

</script>

@endsection