@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="index.html"><i class="icon-home2 position-left"></i> Penjualan</a></li>
			<li class="active"></li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Form Input Penjualan</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('penjualan') }}" 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_penjualan" method="post" action="{{ url('penjualan/simpan_faktur') }}" autocomplete="off" >
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_penjualan" value="{{ $data['data']['id_penjualan']}}">
        	<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', $data['data']['cv'], ['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($data['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">
        					@php foreach($data['pelanggan'] as $s){ 
        					$sel = $data['data']['id_pelanggan'];
        					@endphp

        					<option value="@php echo $s->id_pelanggan @endphp" {{ ($s->id_pelanggan == $sel) ? "selected":"" }}>@php echo  $s->pic.' - '.$s->nama @endphp</option>

        					@php } @endphp
        				</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 set_datepicker" type="text" name="tanggal" placeholder="Tanggal" value="{{ ($data['data']['tanggal'] == '') ? date('d-m-Y'):$data['data']['tanggal'] }}" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Faktur</label>
        			<div class="col-sm-8">
        				<input class="form-control set_datepicker" type="text" name="tanggal_faktur" placeholder="Tanggal Faktur" value="{{ $data['data']['tanggal_faktur'] }}" onchange="get_nomor($(this)), set_jatuh_tempo($(this))" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">Tanggal Jatuh Tempo</label>
        			<div class="col-sm-8">
        				<input class="form-control set_datepicker" type="text" name="tanggal_tempo" placeholder="Tanggal Jatuh Tempo" value="{{ $data['data']['tanggal_tempo'] }}" required/>
        			</div>
        		</div>
        		<div class="form-group">
        			<label class="control-label col-sm-4">No. Faktur</label>
        			<div class="col-sm-3 col-md-3">
        				{{ Form::text('no_faktur', $data['data']['no_faktur'], ['class'=>'form-control text-right', 'placeholder'=>'Nomor']) }}
        			</div>
        			<div class="col-sm-5 col-md-5">
        				{{ Form::text("sufix_no_faktur", $data['data']['sufix_no_faktur'], ['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">{{ $data['data']['keterangan'] }}</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" name="currency" id="currency">
        					@foreach($data['mata_uang'] as $key => $val)
        						<option value="{{ $val->id_matauang }}" data-inisial="{{ $val->inisial }}" @if($data['data']['mata_uang'] == $val->id_matauang) selected @endif>{{ $val->nama_matauang }} - {{ $val->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", $data['data']['kurs_jual'], ["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="col-sm-12 form-group">
	        	<h5 class="panel-title">Detail Barang</h5>
	        </div>

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

        	<div class="responsive">
	        	<table class="table table-stripped" id="table_barang">
			    	<thead>
						<tr>
							<th width="25%">Kode Barang</th>
							<th>Nama Barang</th>
							<th width="5%">Jumlah</th>
							<th width="15%">Harga Satuan</th>
							<th width="15%">Potongan</th>
							<th width="15%">Nominal</th>
						</tr>
					</thead>
					<tbody>
						@foreach($data['penjualanBarang'] AS $key=>$value)
						<tr class="row-barang">
							<td style="vertical-align: top;">
								{{-- $value->kode_barang --}}
								<select name="id_barang" class="form-control select-search" disabled="">
									<option value="{{ $value->id_barang }}" data-gudang="{{ $value->id_gudang }}" data-satuan="{{ $value->id_satuan}}">{{ format_tampil_data_barang($value) }}</option>
								</select>
								<input type="hidden" name="barang[id_jual_barang][]" value="{{ $value->id_jual_barang }}">
							</td>
							<td style="vertical-align: top;">
								<input type="text" name="barang[nama_barang][]" class="form-control" value="{{ $value->nama_barang }}">
							</td>
							<td class="text-right" style="vertical-align: top;">
								{{ $value->jml_barang }}
								<input type="hidden" name="barang[jml_barang][]" value="{{ $value->jml_barang }}">
							</td>
							<td class="text-right" style="vertical-align: top;">
								<input type="number" step="0.01" min="0" name="barang[harga_satuan][]" class="form-control text-right" onkeyup="setFormatMoney($(this), 'td'), setNominal($(this))" value="{{ $value->harga_satuan }}">
								<span class="help-block currency text-right" style="font-weight: bold;">Rp0,00</span>
							</td>
							<td class="text-right" style="vertical-align: top;">
								<input type="number" step="0.01" min="0" name="barang[potongan][]" class="form-control text-right" onkeyup="setFormatMoney($(this), 'td'), setNominal($(this))" value="{{ $value->potongan }}">
								<span class="help-block currency text-right" style="font-weight: bold;">Rp0,00</span>
							</td>
							<td class="text-right" style="vertical-align: top;">
								<input type="hidden" name="barang[total][]" value="0" onkeyup="setFormatMoney($(this), 'td')">
								<span class="help-block currency text-right" style="font-weight: bold;">Rp0,00</span>
							</td>
						</tr>
						@endforeach
					</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>Tagihan</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') }}" class="btn btn-default">Batal</a>
	             	<button class="btn btn-success" type="button" id="simpan_penjualan">Simpan</button>
	            </div>

	    	</div>

	    </div>

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


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

$(document).ready(function(){
	$(".set_select2").select2();

	$("select[name='barang[id_barang][]']").val("").trigger("change");
	get_nomor($("input[name=tanggal_faktur]"));

	$("input[name=td_potongan], input[name=td_pajak]").keyup(function(){
		netto();
	});

	$("select[name=lokasi]").val("{{ $data['data']['lokasi'] }}").trigger("change");
	$("select[name=pajak]").val("{{ $data['data']['pajak'] }}").trigger("change");
	$("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();
    });

    $("input[name='barang[harga_satuan][]'], input[name='barang[potongan][]']").trigger("keyup");

    setCurrency($("#currency").val());


	$("#simpan_penjualan").on("click", function(evt){
		evt.preventDefault();
		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_penjualan").submit();
			}
		} else {
			$("#form_penjualan").submit();
		}
	})

	checkKurs();

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

});

$("#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[harga_satuan][]'], input[name='barang[potongan][]']").trigger("keyup");
}

function get_nomor(this_){
	var id_penjualan = "{{ $data['data']['id_penjualan'] }}";
	var date = this_.val();
	var s_date = date.split("/");
	var sufix_no_faktur = "/KTJB";
	$.ajax({
		url : "{{ url('penjualan/get_nomor') }}",
		type : "get",
		data : {id_penjualan:id_penjualan, tanggal:date},
		dataType : "json",
		success : function(result){
			if(id_penjualan!=""){
				sufix_no_faktur = result.sufix_no_faktur;
			}
			
			$("input[name=no_faktur]").val(result.no_faktur);
			$("input[name=sufix_no_faktur]").val(sufix_no_faktur);
		}
	});
}

function set_jatuh_tempo(this_) {
	var id_penjualan = $("input[name=id_penjualan]").val();
	var date = this_.val();
	var s_date = date.split("/");
	var _date = new Date(s_date[2], s_date[1], s_date[0]);
	_date.setMonth(_date.getMonth()+1);

	var dd = _date.getDate();
	var mm = _date.getMonth()+1;
	var yyyy = _date.getFullYear();
	if(mm < 10) mm = '0'+mm;
	if(dd < 10) dd = '0'+dd;

	var set = [dd, mm, yyyy];
	$("input[name=tanggal_tempo]").val(set.join("/"));
}

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 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_barang = row.find($("input[name='barang[jml_barang][]']")).val();
	var harga_satuan = row.find($("input[name='barang[harga_satuan][]']")).val();
	var potongan = row.find($("input[name='barang[potongan][]']")).val();
	var inputTotal = row.find($("input[name='barang[total][]']"));

	jml_barang = isNaN(jml_barang) ? 0 : jml_barang;
	harga_satuan = isNaN(harga_satuan) ? 0 : harga_satuan;
	potongan = isNaN(potongan) ? 0 : potongan;
	var totalvalue = (jml_barang*harga_satuan)-(jml_barang*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