@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> Pembelian</a></li>
			<li class="active">Faktur Pembelian</li>
		</ul>
	</div>
</div>

<div class="content">
	<div class="panel panel-flat">
		<div class="panel-heading">
			<h5 class="panel-title">Faktur Pembelian</h5>
			<div class="heading-elements">
				<ul class="icons-list">
		      		<li><a href="{{ url('pembelian') }}" class="text-warning"><i class="icon-circle-left2 warning"></i> Kembali</a></li>
		      	</ul>
	       	</div>
	    </div>
	    <div class="panel-body">
	    	<form class="form-horizontal" method="post" action="{{ url('pembelian/simpan') }}" autocomplete="off" >
	    	<input type="hidden" name="_token" value="{{ csrf_token() }}">
	    	<input type="hidden" name="id_pembelian" value="{{ $data['data']['id_pembelian']}}">
	    	<!--div class="col-sm-12">
	    		<div class="form-group">
	    			{{ Form::hidden("id_orderbeli") }}
	    			{{ Form::label("Order", "", ['class'=>'control-label col-sm-2']) }}
	    			<div class="col-sm-4">
		    			{{ Form::text("no_order", "", ['class'=>'form-control', 'readonly'=>'true']) }}
		    			<span class="help-block text-warning" style="font-size: 10px; font-style: italic;">*)Optional</span>
		    		</div>
		    		<div class="col-sm-4">
		    			{{ Form::button("Pilih Order", ['class'=>'btn btn-sm btn-warning', 'onclick'=>'pilih_order()']) }}
		    			{{ Form::button("Reset Order", ['class'=>'btn btn-sm btn-default', 'onclick'=>'reset_order()']) }}
		    		</div>
	    		</div>
	    	</div-->
	    	
	    	<div class="col-sm-12 col-md-12">
	    		<div class="row col-sm-6 col-md-6">
		    		<div class="form-group">
		    			{{ Form::label("Lokasi", "", ['class'=>'control-label col-sm-4']) }}
		    			<div class="col-sm-8">
		    				<select name="lokasi" class="form-control set_selected">
		    					@foreach($data['lokasi'] AS $key => $value)
		    					<option value="{{ $value->id_lokasi }}" {{ ($data['data']['lokasi']==$value->id_lokasi) ? "selected=''" : "" }}>{{ $value->nama_lokasi }}</option>
		    					@endforeach
		    				</select>
		    			</div>
		    		</div>
		    	</div>
	    	</div>

	    	<div class="col-sm-12 col-md-12">
	    		<div class="row col-sm-6 col-md-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>
	    	</div>
        	<div class="col-sm-6">
        		<div class="form-group">
        			<label class="control-label col-sm-4">Penyedia</label>
        			<div class="col-sm-8">
        				<select class="form-control select-search" name="penyedia">
        					<option value="">--- Pilih Penyedia ---</option>
        					@php foreach($data['supplier'] as $s){ 
        					$sel = $data['data']['id_penyedia'];
        					@endphp

        					<option value="@php echo $s->id_penyedia @endphp" {{ ($s->id_penyedia == $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 readonly />
        			</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 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-2 col-md-2">
        				{{ Form::text('no_faktur', $data['data']['no_faktur'], ['class'=>'form-control text-right']) }}
        			</div>
        			<div class="col-sm-6 col-md-6">
        				{{ 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>

        	<div class="col-sm-6">
        		<div class="form-group">
    				{{ Form::label("Syarat Pembayaran", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::textarea("syarat_bayar", $data['data']['syarat_bayar'], ['class'=>'form-control', 'rows'=>2]) }}
    				</div>
    			</div>

    			<div class="form-group">
    				{{ Form::label("Tanggal Penyerahan", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::text("tanggal_serah", $data['data']['tanggal_serah'], ['class'=>'form-control set_datepicker', 'readonly'=>'']) }}
    				</div>
    			</div>

    			<div class="form-group">
    				{{ Form::label("Tempat Penyerahan", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::select("tempat_serah", $data['tempat_penyerahan'], $data['data']['tempat_serah'], ['class'=>'form-control set_selected', 'onchange'=>'get_alamat_tempat_penyerahan($(this))']) }}
    				</div>
    			</div>

    			<div class="form-group">
    				{{ Form::label("Alamat Penyerahan", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::textarea("alamat_serah", "-", ['class'=>'form-control', 'rows'=>4, 'readonly'=>'']) }}
    				</div>
    			</div>

    			<div class="form-group">
    				{{ Form::label("Penerima", "", ['class'=>'control-label col-sm-4']) }}
    				<div class="col-sm-8">
    					{{ Form::text("penerima", $data['data']['penerima'], ['class'=>'form-control']) }}
    				</div>
    			</div>
        	</div>

        	
        	<div class="col-sm-12 form-group">
        	<h5 class="panel-title">Detail Barang</h5>
        		<br>
        		<button href="#" class="btn btn-success" style="margin-bottom: 1%;" id="btn_tambah_item" type="button">Tambah Barang</button> 
        		<button href="#" class="btn btn-warning" style="margin-bottom: 1%;" id="btn_ambil_barang" type="button">Ambil Barang</button>
        	</div>
        	<div class="col-ms-12">
        	<table class="table table-stripped" id="table_barang">
	    	<thead>
				<tr>
					<th>No</th>
					<th>Kode Barang</th>
					<th>Nama Barang</th>
					<th>Harga Satuan</th>
					<th>Jumlah</th>
					<th>Satuan</th>
                    <th>Nominal</th>
					<th class="text-center">Aksi</th>
				</tr>
			</thead>
			<tbody>
				
			</tbody>
	    </table>
        	</div>
             <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>Total Pembelian</td>
             		<td class="text-right">
         				<label class="checkbox-inline checkbox-right">
         					<input type="checkbox" name="termasuk_pajak" value="1" class="styled" onclick="termasuk_pajaks($(this))" {{ $data['data']['termasuk_pajak']==1 ? "checked='checked'" : "" }}> Sudah Termasuk PPN
         				</label>
             		</td>
             	</tr>
             	<tr>
					<td width="30%">Sub Total</td>
                    <td><input type="hidden" name="td_total" value="0" readonly="">
                    	<p class="help-block 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="{{ $data['data']['nominal_potongan'] }}">
                    	<p class="help-block 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 pull-right" id="help_popup_pajak">Rp 0</p>
                    </td>
				</tr>
               
				<tr class="row_no_faktur_pajak">
					<td>No Faktur Pajak</td>
                    <td>
                    	{{ Form::text("no_faktur_pajak", $data['data']['no_faktur_pajak'], ['class'=>'form-control', 'data-mask'=>'999.999-99.00000000']) }}
                    </td>
				</tr>
                <tr>
					<td>Ongkos Kirim</td>
                    <td>
                    	<input type="number" step="0.01" min="0" name="td_ongkir" class="form-control text-right" value="0">
                    	<p class="help-block pull-right" id="help_popup_ongkir">Rp 0</p>
                    </td>
				</tr>
                <tr>
					<td>Tagihan</td>
                    <td><p class="help-block 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">
             		<table class="table table-bordered table-form">
             			<tbody>
             				<tr class="active">
             					<td colspan="2">Pembayaran</td>
             				</tr>
             				<tr>
             					<td>Cara Bayar</td>
             					<td>
			        				<select class="form-control select-search" name="cara_bayar" onchange="ubah_carabayar($(this))">
			        					@php foreach($data['cara'] as $s){ 
			        					// $sel = $data['data']['id_carabayar'];
			        					@endphp
			        					<option jenis_bayar="@php echo $s->jenis_bayar @endphp"  value="@php echo $s->id_carabayar @endphp">@php echo  $s->cara.' - '.$s->tipe; @endphp</option>
			        					@php } @endphp
			        				</select>
             					</td>
             				</tr>
             				<tr class="akun">
             					<td>Akun</td>
             					<td>
             						{{-- Form::select("id_akun_carabayar", $data['perkiraan'], [$data['data']['id_akun_carabayar']], ['class'=>'form-control select-search']) --}}
             					</td>
             				</tr>
             				<tr>
             					<td class="nominal_text">Nominal Bayar</td>
             					<td>
             						<input type="text" name="td_nominal_bayar" class="form-control number-only text-right" value="" onkeyup="setFormatNumber($(this), 'td')" required="">
             						<p class="help-block pull-right">Rp 0</p>
             					</td>
             				</tr>
             			</tbody>
             		</table>
             	</div>
            </div> -->

	    </div>

	    <div class="panel-footer">
	    	<div class="form-group col-md-12 col-sm-12">
				<a href="{{url('pembelian')}}" class="btn bg-warning-400">Kembali</a>
				<div class="form-group pull-right">
					<button class="btn btn-primary" type="submit">Simpan</button>
				</div>
			</div>
	    </div>


		
		<input type="hidden" name="td_netto" class="form-control">
	    </form>
	</div>
</div>

<!---Modal--->
<div class="modal fade" id="exampleModal"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      	<div class="modal-body">
	      		<form class="form-horizontal" autocomplete="off">
	      			<input type="hidden" name="popup_id_table">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Barang</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_barang">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah {{-- <b id="popup_satuanawal"></b> --}}</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlah" class="form-control number-only">
	      					<p class="help-block text-right" id="help_popup_jumlah"></p>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="col-sm-4 control-label">Satuan</label>
	      				<div class="col-sm-8">
	      					<select name="popup_satuan" class="form-control select-search">
	      						<option value="">-- Pilih --</option>
	      						@php
	      						foreach ($data['satuan'] as $s) { @endphp
	      						<option value="{{ $s->id_satuan }}" name="{{ $s->nama_satuan }}">{{ $s->nama_satuan }}</option>
	      						@php
	      						}
	      						@endphp
	      					</select>
	      				</div>
	      			</div>
	      			{{-- <div class="form-group" id="div_konversi">
	      				<label class="control-label col-sm-4">Jumlah <b id="popup_satuanakhir"></b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jumlahakhir" class="form-control number-only">
	      				</div>
	      			</div> --}}
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Harga Satuan</label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_harga" class="form-control text-right number-only">
	      					<p class="help-block pull-right" id="help_popup_harga"></p>
	      				</div>
	      			</div>
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_simpan">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<div class="modal fade" id="exampleModal2"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data</h5>
	    		<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span></button>
	      	</div>
	      	<div class="modal-body">
	      		<form class="form-horizontal" autocomplete="off">
	      			<input type="hidden" name="popup_id_tableorder">
	      			<input type="hidden" name="popup_idlist">
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Order Pembelian</label>
	      				<div class="col-sm-8">
	      					<select class="form-control select-search" name="popup_order">
	      						
	      					</select>
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah </label>
	      				<div class="col-sm-8">
	      					<label class="control-label" name="popup_jum_pesan"></label>

	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Jumlah Diterima <b id="popup_satuanawal2"></b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jum_terima" class="form-control number-only" min="" max="" onkeyup="maksimal($(this))">
	      					<p class="help-block text-right" id="help_popup_terima"></p>
	      				</div>
	      			</div>
	      			<div class="form-group" id="div_konversiorder">
	      				<label class="control-label col-sm-4">Jumlah <b id="popup_satuanakhir2"></b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_jum_terimaakhir" class="form-control number-only" onkeyup="maksimal($(this))">
	      				</div>
	      			</div>
	      			<div class="form-group">
	      				<label class="control-label col-sm-4">Harga Satuan Order Pembalian</label>
	      				<div class="col-sm-8">
	      					<P class="form-control-static" name="popup_harga_pesan"></p>

	      				</div>
	      			</div>
	      			<div class="form-group" id="">
	      				<label class="control-label col-sm-4">Harga Pembelian</b></label>
	      				<div class="col-sm-8">
	      					<input type="text" name="popup_harga_beli" class="form-control text-right number-only">
	      					<p class="help-block pull-right" id="help_popup_harga_beli"></p>
	      				</div>
	      			</div>
	      		</form>
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-primary" id="btn_popup_ambil">Simpan</button>
	      	</div>
	    </div>
	</div>
</div>

<div class="modal fade" id="modal-order-pembelian"  role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
	    <div class="modal-content">
	    	<div class="modal-header">
	    		<h5 class="modal-title" id="exampleModalLabel">Data Order Pembelian</h5>
	    		<button type="button" class="close" aria-label="Close" onclick="btnBatalModalOrder()">
	    			<span aria-hidden="true">&times;</span>
	    		</button>
	      	</div>
	      	<div class="modal-body">
	      		
	      	</div>
	      	<div class="modal-footer">
	        	<button type="button" class="btn btn-default" onclick="btnBatalModalOrder()">Batal</button>
	      	</div>
	    </div>
	</div>
</div>

<input type="hidden" name="store_konversi_awal" class="form-control">
<input type="hidden" name="store_konversi_akhir" class="form-control">
<input type="hidden" name="store_konversi" class="form-control">
<input type="hidden" name="store_konversiorder" class="form-control">

<!----Modal---->
<script type="text/javascript">
	var summary_sub_total = $("input[name=td_total]");
	var summary_potongan = $("input[name=td_potongan]");
	var summary_pajak = $("input[name=td_pajak]");
	var summary_tagihan = $("input[name=td_netto]");

	$("#btn_tambah_item").click(function(){
		get_barang($(this));
		$("[name=popup_id_table]").val('');
		$("[name=popup_barang]").val('').trigger('change');
		$("[name=popup_jumlah]").val('');
		$("[name=popup_harga]").val('');
		$("#help_popup_harga").text('');
		$("#div_konversi").addClass('hide');
		$("[name=popup_jumlahakhir]").val('');
		$("#popup_satuanawal").text('');
		$("#popup_satuanakhir").text('');
		$("#help_popup_jumlah").text('');
		$("#exampleModal").modal('show');

	})

	var table_barang;
	var penyedia;
	var tb_no = parseInt(10000);
	var id_pembelian;
	var tr_akun = $("tr.akun");
	$(document).ready(function(){
		table_barang = $("#table_barang").DataTable({
			"paging": false
		});
		table_barang.on( 'order.dt search.dt', function () {
            table_barang.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
                cell.innerHTML = i+1;
            } );
        } ).draw();

        $(".set_selected").select2();

		// get_barang($("select[name=transaksi]"));
		tr_akun.hide();

		id_pembelian = '{{ $data['data']['id_pembelian']}}';
		if(id_pembelian != ''){
			get_edit(id_pembelian);
			get_editorder(id_pembelian);
			$("[name=penyedia]").attr('disabled', true);
		}

		// id_transaksi = '{{-- $data['data']['id_transaksi'] --}}';
		// if(id_transaksi != ''){
		// 	$("select[name=transaksi]").val(id_transaksi).change().attr('disabled', true);
		// }

		transaksi = $("[name=transaksi]").val();
		penyedia = $("[name=penyedia]").val();
		get_order(transaksi, penyedia);

		// $("input[name=td_nominal_bayar]").val("{{-- $data['data']['nominal_bayar'] --}}").trigger("keyup");
		$(".row_no_faktur_pajak").hide();
		$("input[name=no_faktur_pajak]").val("{{ $data['data']['no_faktur_pajak'] }}");
		$("select[name=pajak]").change();
		$(document).on("change","select[name=pajak]",function(e){
            if($(this).val()==1){
            	$(".row_no_faktur_pajak").show();
        		$("input[name=td_pajak]").val(0);    	
        		$("input[name=td_pajak]").removeAttr('readonly');
            }else{
				$(".row_no_faktur_pajak").hide();
            	$("input[name=td_pajak]").val(0);
            	$("input[name=td_pajak]").attr('readonly','readonly');
            }
            netto();
        });
        $(document).on("change","select[name=cara_bayar]",function(e){
        	jenis_bayar=$('option:selected', this).attr('jenis_bayar');
            if(jenis_bayar==1){
            	$("td.nominal_text").html('Nominal Bayar');
            	$("input[name=td_nominal_bayar]").val($("input[name=td_netto]").val()).keyup();
        		$("input[name=td_nominal_bayar]").removeAttr('readonly');
            }else if(jenis_bayar==2){
            	$("td.nominal_text").html('Nominal Bayar');
            	$("input[name=td_nominal_bayar]").val(0).keyup();
            	$("input[name=td_nominal_bayar]").attr('readonly','readonly');
            }else if(jenis_bayar==3){
            	$("td.nominal_text").html('Nominal Uang Muka');
            	// $("input[name=td_nominal_bayar]").val("{{-- $data['data']['nominal_bayar'] --}}").keyup();
            	$("input[name=td_nominal_bayar]").removeAttr('readonly');
            }
        });

        $("select[name=pajak]").val("{{$data['data']['pajak']}}").change();
        $("input[name=td_ongkir]").val("{{$data['data']['nominal_ongkir']}}").trigger("keyup");// $("select[name=cara_bayar]").val("{{-- $data['data']['id_carabayar'] --}}").change();
        if(id_pembelian == ''){
        	$("input[name=tanggal_faktur]").trigger("change");
        	
        }

        setTimeout(function(){
        	// console.log($("input[name=termasuk_pajak]").is(":checked"));
        	if($("input[name=termasuk_pajak]").is(":checked")){
	        	termasuk_pajaks($("input[name=termasuk_pajak]"));
	        }
        }, 2000);

        get_alamat_tempat_penyerahan($("select[name=tempat_serah]"));
        
	})

	function get_alamat_tempat_penyerahan(this_) {
		var id = this_.val();
		$.ajax({
			url : "{{ url('orderbeli/get_tempat_penyerahan') }}",
			type : "GET",
			data : {id : id},
			dataType : "json",
			success : function(result){
				$("textarea[name=alamat_serah]").val(result.alamat);
			}
		})
	}

	function get_nomor(this_){
		var id_pembelian = $("input[name=id_pembelian]").val();
		var date = this_.val();
		var s_date = date.split("/");
		var sufix_no_faktur = "/"+s_date[1]+"/"+s_date[2];
		$.ajax({
			url : "{{ url('pembelian/get_nomor') }}",
			type : "get",
			data : {id_pembelian:id_pembelian, tanggal:date},
			dataType : "json",
			success : function(result){
				if(id_pembelian!=""){
					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_pembelian = $("input[name=id_pembelian]").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 get_barang(this_){
		// var transaksi = this_.find("option:selected").val();
		$.ajax({
			type : 'get',
			url: '{{ url('pembelian/get_barang') }}',
			// data : {id_transaksi : transaksi},
			dataType : 'json',
			success: function(respon){
				$("[name=popup_barang]").html('');
				$("[name=popup_barang]").append("<option value=''>--- Pilih Barang ---</option>");
				if(respon.length > 0){
					for(i in respon){
						var r_opt_name = respon[i].kategori+' - '+respon[i].nama_sub+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang;
						if(respon[i].id_kelompok==4){
							var r_opt_name = respon[i].kode_barang+' - '+respon[i].nama_barang;
						}

						$("[name=popup_barang]").append('<option value="'+respon[i].id_barang+'" barang="'+respon[i].nama_barang+'" kode="'+respon[i].kode_barang+'" satuan="'+respon[i].satuan+'">'+r_opt_name+'</option>');
					}
				}
			}
		})
	}

	$("#btn_popup_simpan").click(function(){
		var id_data = $("[name=popup_id_table]").val();
		var id_barang = $("[name=popup_barang]").val();
		var nama_barang = $("[name=popup_barang] :selected").attr('barang');
		var kode = $("[name=popup_barang] :selected").attr('kode');
		var termasuk_pajak = $("[name=popup_order] :selected").attr('termasuk_pajak');
		var jumlah = $("[name=popup_jumlah]").val();
		// var jumlah_konv = $("[name=popup_jumlahakhir]").val();
		// if(isNaN(jumlah_konv) || jumlah_konv == ''){
		// 	jumlah_konv = jumlah;
		// }
		var id_satuan = $("[name=popup_satuan]").val();
		var satuan = $("[name=popup_satuan] :selected").attr('name');

		var harga = $("[name=popup_harga]").val();
		var total = parseFloat(jumlah)*parseFloat(harga);
		if(id_barang != '' && jumlah != '' && harga != '' && id_satuan != ''){
			var label_ppn = "";
			if(termasuk_pajak==1){
				label_ppn = "<span class='badge badge-success'><i class='fa fa-plus'></i> PPN</span>";
			}
			if(id_data == ''){
				table_barang.row.add(['<div><center></center></div>',
					'<div id="list_kode'+tb_no+'">'+kode+'</div>',
					'<div id="list_nama'+tb_no+'">'+nama_barang+'</div>',
					'<div id="list_harga'+tb_no+'">'+accounting.formatMoney(harga)+" "+label_ppn+'</div>',
					'<div id="list_jumlah'+tb_no+'">'+format_angka(jumlah)+'</div>',
					'<div id="list_satuan'+tb_no+'">'+satuan+'</div>',
					'<div id="list_total'+tb_no+'">'+accounting.formatMoney(total)+'</div>',
					'<div><button type="button" class="btn btn-xs bg-warning-400" onclick="edit_table('+tb_no+')"><i class="icon-pencil"></i> </button> <button class="btn btn-xs bg-danger-800" type="button" onclick="hapus($(this))"><i class="icon-eraser"></i></button></div>'+
					'<input type="hidden" name="tabel_id[]" id="tabel_id'+tb_no+'" value="">'+
					'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+tb_no+'" value="'+id_barang+'">'+
					'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+tb_no+'" value="'+jumlah+'">'+
					'<input type="hidden" name="tabel_jumlah_konv[]" id="tabel_jum_konv'+tb_no+'" value="'+jumlah+'">'+
					'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+tb_no+'" value="'+harga+'">'+
					'<input type="hidden" name="tabel_total[]" id="tabel_total'+tb_no+'" value="'+total+'">'+
					'<input type="hidden" name="tabel_idorderbarang[]" id="tabel_id_orderbarang'+tb_no+'" value="">'+
					'<input type="hidden" name="tabel_idsatuan[]" id="tabel_idsatuan'+tb_no+'" value="'+id_satuan+'">'
					]).draw(false);
			}else{
				$("#list_kode"+id_data).text(kode);
				$("#list_nama"+id_data).text(nama_barang);
				$("#list_harga"+id_data).html(accounting.formatMoney(harga)+" "+label_ppn);
				$("#list_jumlah"+id_data).text(format_angka(jumlah));
				$("#list_satuan"+id_data).text(satuan);
				$("#list_total"+id_data).text(accounting.formatMoney(total));

				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_idbarang"+id_data).val(id_barang);				
				$("#tabel_jumlah"+id_data).val(jumlah);
				$("#tabel_jumlah_konv"+id_data).val(jumlah);
				$("#tabel_idsatuan"+id_data).val(id_satuan);
				$("#tabel_harga"+id_data).val(harga);
				$("#tabel_total"+id_data).val(total);
			}

			tb_no++;
			// get_jum();
			if(termasuk_pajak==1 && $("input[name=termasuk_pajak]").is(":checked")==false){
				$("input[name=termasuk_pajak]").attr("checked", "checked").trigger("click");
			}else if(termasuk_pajak==1 && $("input[name=termasuk_pajak]").is(":checked")==true){
				termasuk_pajaks($("input[name=termasuk_pajak]"));
			}else{
				get_jum();
			}
			
			$("#exampleModal").modal('hide');
		}
	//	console.log(id_barang+"\n"+nama_barang+"\n"+jumlah+"\n"+harga);
	})

	function edit_table(id){
		get_barang($(this));
		var id_barang = $("#tabel_idbarang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();
		var id_satuan = $("#tabel_idsatuan"+id).val();
		var harga = $("#tabel_harga"+id).val();
		setTimeout(function (argument) {
			// body...
		$("[name=popup_id_table]").val(id);
		$("[name=popup_barang]").val(id_barang).trigger('change');
		$("[name=popup_jumlah]").val(jumlah).trigger("keyup");
		$("[name=popup_satuan]").val(id_satuan).trigger("change");
		$("[name=popup_harga]").val(harga);
		$("#help_popup_harga").text(accounting.formatMoney(harga));
		$("[name=popup_satuan]").val(id_satuan).trigger('change');
		$("#help_popup_jumlah").text(format_angka(jumlah));

		console.log(id_satuan);

		$("#exampleModal").modal('show');
		}, 2500);
	}

	function get_jum(){
		var jum = parseFloat(0);
		$('input[id^="tabel_total"]').each(function() {
              // alert($(this).val());
              var val = ($(this).val());
              jum += parseFloat(val);
              
            });
		$("#td_total").text(accounting.formatMoney(jum));
		$("[name=td_total]").val(jum);
		netto();
	}


	$("[name=td_potongan]").keyup(function(){
		// var rp = accounting.formatMoney($("[name=td_potongan]").val());
		// $("#help_popup_potongan").text(rp);
		netto();
	});

	$("[name=td_pajak]").keyup(function(){
		// var rp = accounting.formatMoney($("[name=td_pajak]").val());
		// $("#help_popup_pajak").text(rp);
		netto();
	})

	$("[name=td_ongkir]").keyup(function(){
	// 	var rp = accounting.formatMoney($("[name=td_ongkir]").val());
	// 	$("#help_popup_ongkir").text(rp);
		netto();
	})

	// $("[name=td_bunga]").keyup(function(){
		// netto();
	// })

	$("[name=popup_harga]").keyup(function(){
		var rp = accounting.formatMoney($("[name=popup_harga]").val());
		$("#help_popup_harga").text(rp);
	})

	$("[name=popup_harga_beli]").keyup(function(){
		var rp = accounting.formatMoney($("[name=popup_harga_beli]").val());
		$("#help_popup_harga_beli").text(rp);
	})

	$("[name=popup_jum_terima]").keyup(function(){
		$("#help_popup_terima").text(format_angka($("[name=popup_jum_terima]").val()));
	})

	function setFormatNumber(this_, p) {
		var parentz = this_.parents(p);
		var data = this_.val();
		parentz.find(".help-block").text(accounting.formatMoney(data));
	}

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

		// if(potongan == '' || potongan == 0 || isNaN(potongan)){
		// 	potongan = parseFloat(0);
		// }

		if($('select[name=pajak]').val()==1){
			ppn = parseFloat((total-potongan)*(10/100));
		}

		// if(total == '' || total == 0 || isNaN(total)){
		// 	total = parseFloat(0);
		// }

		// if(ppn == '' || ppn == 0 || isNaN(ppn)){
		// 	ppn = parseFloat(0);
		// }
		// if(ongkir == '' || ongkir == 0 || isNaN(ongkir)){
		// 	ongkir = parseFloat(0);
		// }

		netto = total-potongan+ppn+ongkir; //+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));

		$("[name=td_ongkir]").val(ongkir);
		$("#help_popup_ongkir").text(accounting.formatMoney(ongkir));

		$("#td_netto").text(accounting.formatMoney(netto));
		$("[name=td_netto]").val(netto);
		// $("select[name=cara_bayar]").change();
		
        // $("select[name=id_akun_carabayar]").val("{{-- $data['data']['id_akun_carabayar'] --}}").change();
	//	console.log(total+"\n"+pot+"\n"+bunga+"\n"+netto);
	}

	function termasuk_pajaks(this_) {
		var is_checked = this_.is(":checked");
		var	temp_tagihan = $("input[name=td_total]").val()
		var temp_sub_total = 0;

		if(is_checked){
			// temp_sub_total = (100/110)*temp_tagihan;
			temp_sub_total = 0;
			$('input[name^="tabel_jumlah[]"]').each(function(id){
				var input_jumlah = $(this);
				var input_harga = $('input[name^="tabel_harga[]"]');

				var jumlah = input_jumlah[0].value;
				var harga = (input_harga[id]).value;
				var temp_harga = (100/110)*harga;
				temp_sub_total += (parseFloat(temp_harga)*jumlah);
			});

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

			$("select[name=pajak]").val(1).change();
			netto();
		}else{
			// temp_sub_total = $("input[name=td_netto]").val();
			// $("#td_total").text(accounting.formatMoney(temp_sub_total));
			// $("input[name=td_total]").val(temp_sub_total);

			// $("select[name=pajak]").val("{{ $data['data']['pajak'] }}").change();

			get_jum();

		}
	}

	function get_edit(id){
		$.ajax({
			url: "{{ url('pembelian/get_edit')}} ",
			type: 'post',
			data: {id : id,},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					for(i in respon){
						table_barang.row.add(['<div><center></center></div>',
							'<div id="list_kode'+respon[i].id_beli_barang+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_beli_barang+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_harga'+respon[i].id_beli_barang+'">'+accounting.formatMoney(respon[i].harga_satuan)+'</div>',
							'<div id="list_jumlah'+respon[i].id_beli_barang+'">'+format_angka(respon[i].jml_barang)+'</div>',
							'<div id="list_satuan'+respon[i].id_beli_barang+'">'+respon[i].nama_satuan+'</div>',
							'<div id="list_total'+respon[i].id_beli_barang+'">'+accounting.formatMoney(respon[i].total)+'</div>',
							'<div><button type="button" class="btn btn-xs bg-warning-400" onclick="edit_table('+respon[i].id_beli_barang+')"><i class="icon-pencil"></i> </button> <button class="btn btn-xs bg-danger-800" type="button" onclick="hapus($(this))"><i class="icon-eraser"></i></button></div>'+
							'<input type="hidden" name="tabel_id[]" id="tabel_id'+respon[i].id_beli_barang+'" value="'+respon[i].id_beli_barang+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+respon[i].id_beli_barang+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang+'">'+
							'<input type="hidden" name="tabel_jumlah_konv[]" id="tabel_jum_konv'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang+'">'+
							'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+respon[i].id_beli_barang+'" value="'+respon[i].harga_satuan+'">'+
							'<input type="hidden" name="tabel_total[]" id="tabel_total'+respon[i].id_beli_barang+'" value="'+respon[i].total+'">'+
							'<input type="hidden" name="tabel_idorderbarang[]" id="tabel_id_orderbarang'+respon[i].id_beli_barang+'" value="">'+
							'<input type="hidden" name="tabel_idsatuan[]" id="tabel_idsatuan'+respon[i].id_beli_barang+'" value="'+respon[i].id_satuan+'">'
							]).draw(false);
					}

					get_jum();
					// netto();
				}
				
			}
		})
	}

	function ubah_carabayar(this_) {
		var opt = this_.find("option:selected");
		var jenis_bayar = opt.attr("jenis_bayar");
		tr_akun.find("select[name=id_akun_carabayar]").val('').change();
		if(jenis_bayar!=2){
			tr_akun.show();
		}else{
			tr_akun.hide();
		}
	}

	function hapus(d){
		table_barang.row(d.parents('tr')).remove().draw();
		get_jum();
		// netto();
		//  alert(d);
	}

	$("[name=popup_barang]").change(function(){
		var id_barang = $("[name=popup_barang]").val();
		
		$.ajax({
			url: "{{ url('pembelian/get_konversi')}} ",
			type: 'post',
			data: {id : id_barang},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					$("#div_konversi").removeClass('hide');

					$("#popup_satuanawal").text(respon[0].satuan_awal);
					$("#popup_satuanakhir").text(respon[0].satuan_akhir);
					var nilai = parseFloat(respon[0].nilai_akhir)/parseFloat(respon[0].nilai_awal);
					$("[name=store_konversi]").val(nilai);
				}else{
					$("#div_konversi").addClass('hide');
					$("[name=popup_jumlahakhir]").val('');
					$("#popup_satuanawal").text(respon.satuan_awal);
					$("#popup_satuanakhir").text('');				
				}
			}
		})
	})

	$("[name=popup_jumlah]").keyup(function(){
		var jum = parseFloat($("[name=popup_jumlah]").val());
		if(isNaN(jum) || jum == 0 || jum == ''){
			jum = parseFloat(0);
		}
		var nilai = parseFloat($("[name=store_konversi]").val());

		var tot = jum*nilai;
		$("[name=popup_jumlahakhir]").val(tot);
	})

	//Ambil Barang
	$("#btn_ambil_barang").click(function(){
		$("[name=popup_id_tableorder]").val('');
		$("[name=popup_order]").val(null).trigger('change');
		$("[name=popup_jum_pesan]").text('');
		$("[name=popup_jum_terima]").attr('min', 0);
		$("[name=popup_jum_terima]").attr('max', 0);
		$("[name=popup_jum_terima]").val(1).trigger("keyup");
		$("[name=popup_harga_pesan]").text('');
		$("[name=popup_harga_beli]").val(0).trigger("keyup");
		// $("#help_popup_harga_beli").text('');
		$("#help_popup_terima").text('');
		
		$("#popup_satuanawal2").text('');
		$("#popup_satuanakhir2").text('');
		$("[name=popup_jum_terimaakhir]").val('');

		$("#div_konversiorder").addClass('hide');
		$("#exampleModal2").modal('show');
	})

	$("[name=penyedia]").change(function(){
		var transaksi = $("[name=transaksi]").val();
		var id_penyedia = $("[name=penyedia]").val();
		table_barang.clear().draw();
		get_order(transaksi, id_penyedia);
		var rp = parseFloat(0);
		$("#td_total").text(accounting.formatMoney(rp));
		$('[name=td_potongan]').val(rp);
		$('[name=td_ppn]').val(rp);
		$("#td_netto").text(accounting.formatMoney(rp));
		$("#help_popup_potongan").text(accounting.formatMoney(rp));
		$("#help_popup_pajak").text(accounting.formatMoney(rp));
		$("select[name=cara_bayar]").change();
	})

	function get_order(transaksi, penyedia){
		$.ajax({
			url: "{{ url('pembelian/get_order')}} ",
			type: 'post',
			data: {id_transaksi : transaksi, id_penyedia : penyedia},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				$("[name=popup_order]").html('');
				if(respon.length > 0){
					$("[name=popup_order]").append('<option value="">-- Pilih Barang --</option>');
					for(i in respon){
						$("[name=popup_order]").append('<option value="'+respon[i].id_orderbeli_barang+'" id_barang="'+respon[i].id_barang+'" nama_barang="'+respon[i].nama_barang+'" kode_barang="'+respon[i].kode_barang+'" harga_satuan="'+respon[i].harga_satuan+'" termasuk_pajak="'+respon[i].termasuk_pajak+'" id_satuan="'+respon[i].id_satuan+'" nama_satuan="'+respon[i].nama_satuan+'">( '+respon[i].no_order+' ) '+respon[i].nama_penyedia+' - '+respon[i].kode_barang+' - '+respon[i].nama_barang+'</option>');
					}
				}
			}
		})
	}

	$("[name=popup_order]").change(function(){
		var id_order = $(this).val();
		var id_penyedia = $("[name=penyedia]").val();
		var id_barang = $("[name=popup_order]:selected").attr('id_barang');
		var nilai = 0;
		var id;
		if(id_penyedia != '' && id_order != ''){
			$.ajax({
				url: "{{ url('pembelian/get_order')}} ",
				type: 'post',
				data: {id_penyedia : id_penyedia, id_orderbeli : id_order},
				headers : {
					'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				},
				success: function(respon){

				    console.log(respon);
				    var label_ppn = "";
					if(respon[0].termasuk_pajak==1){
						label_ppn = "<span class='badge badge-success'><i class='fa fa-plus'></i> PPN</span>";
					}
					// $('input[id^=tabel_id]').each(function(){
					// //	id_order;
					// 	id = [($(this).val())];
					// })

					$.ajax({
						url: "{{ url('pembelian/get_konversi')}} ",
						type: 'post',
						data: {id : id_barang},
						headers : {
							'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
						},
						success: function(result){
							if(result.length > 0){
								$("#div_konversiorder").removeClass('hide');

								$("#popup_satuanawal2").text(result[0].satuan_awal);
								$("#popup_satuanakhir2").text(result[0].satuan_akhir);
								var nilai = parseFloat(result[0].nilai_akhir)/parseFloat(result[0].nilai_awal);
								$("[name=store_konversiorder]").val(nilai);
							}else{
								$("#div_konversiorder").addClass('hide');
								$("[name=popup_jumlahakhir2]").val('');
								$("#popup_satuanawal2").text(result.satuan_awal);
								$("#popup_satuanakhir2").text('');				
							}

							var total = parseFloat(0);
							$('input[id^="tabel_jumlah"]').each(function() {
				              
				              var val = ($(this).attr('id'));
				              val = val.replace('tabel_jumlah', '');
				              if($("#tabel_idorderbarang"+val).val() == id_order){
					              var jum = $("#tabel_jumlah"+val).val();
					              	total += parseFloat(jum);
				              }
				              
				            });

				        //    console.log(total);
							if(respon.length > 0){
								if($("[name=popup_id_tableorder]").val() == ''){
									nilai = parseFloat(respon[0].sisa)-total;
								}else{
									if($("[name=popup_idlist]").val() == ''){
										nilai = (parseFloat(respon[0].sisa)-total)+parseFloat($("[name=popup_jum_terima]").val());
									}else{
										nilai = (parseFloat(respon[0].sisa)-total)+parseFloat($("[name=popup_jum_terima]").val());
									}
								}

								// console.log("Sisa "+respon[0].sisa+"\n total "+total+"\n nilai "+$("[name=popup_jum_terima]").val());

								$("[name=popup_jum_pesan]").text(nilai+' '+respon[0].nama_satuan);
								$("[name=popup_jum_terima]").val(nilai).trigger("keyup");
								$("[name=popup_jum_terima]").attr('min', 1);
								$("[name=popup_jum_terima]").attr('max', nilai);

								$("[name=popup_jum_terimaakhir]").attr('min', 1);
								$("[name=popup_jum_terimaakhir]").attr('max', nilai*$("[name=store_konversiorder]").val());

								$("[name=popup_harga_pesan]").html(accounting.formatMoney(respon[0].harga_satuan)+" "+label_ppn);
								$("[name=popup_harga_beli]").val(respon[0].harga_satuan).trigger("keyup");
								// $("[name=popup_harga_beli]").val(respon[0].harga_satuan);
								// $("#help_popup_harga_beli").text(accounting.formatMoney(respon[0].harga_satuan));
							}
						}
					})

					


				}
			})

		}else{
			$("[name=popup_jum_pesan]").text('');
			$("[name=popup_jum_terima]").attr('min', 0);
			$("[name=popup_jum_terima]").attr('max', 0);
			$("[name=popup_jum_terima]").val('');
		}
	})

	$("[name=popup_jum_terima]").keyup(function(){
		var jum = parseFloat($("[name=popup_jum_terima]").val());
		if(isNaN(jum) || jum == 0 || jum == ''){
			jum = parseFloat(0);
		}
		var nilai = parseFloat($("[name=store_konversiorder]").val());

		var tot = jum*nilai;
		$("[name=popup_jum_terimaakhir]").val(tot);
	})

	$("[name=popup_satuanakhir]").keyup(function(){
		var jum = parseFloat($("[name=popup_satuanakhir]").val());
		if(isNaN(jum) || jum == 0 || jum == ''){
			jum = parseFloat(0);
		}
		var nilai = parseFloat($("[name=store_konversiorder]").val());

		var tot = jum/nilai;
		$("[name=popup_jum_terima]").val(tot);
	})

	$("[name=popup_jumlah]").keyup(function(){
		$("#help_popup_jumlah").text(format_angka($("[name=popup_jumlah]").val()));
	})

	function maksimal(x) {
    	var min = parseFloat(x.attr('min'));
    	var max = parseFloat(x.attr('max'));

    	if(x.val() > max) {
    		x.val(max);
    	}else if(x.val() < min){
    		x.val(min);
    	}
	}

	$("#btn_popup_ambil").click(function(){
		var id_data = $("[name=popup_id_tableorder]").val();
		var id_barang = $("[name=popup_order] :selected").attr('id_barang');
		var nama_barang = $("[name=popup_order] :selected").attr('nama_barang');
		var kode = $("[name=popup_order] :selected").attr('kode_barang');
		var termasuk_pajak = $("[name=popup_order] :selected").attr('termasuk_pajak');
		var jumlah = $("[name=popup_jum_terima]").val();
		// var jumlah_konv = $("[name=popup_jum_terimaakhir]").val();
		// if(isNaN(jumlah_konv) || jumlah_konv == ''){
		// 	jumlah_konv = jumlah;
		// }

		var id_satuan = $("[name=popup_order] :selected").attr('id_satuan');
		var satuan = $("[name=popup_order] :selected").attr('nama_satuan');

		// var harga = $("[name=popup_order] :selected").attr('harga_satuan');
		// var total = parseFloat(jumlah)*parseFloat(harga);

		var harga_beli = $("[name=popup_harga_beli]").val();
		var jumlah_beli = $("[name=popup_jum_terima]").val();
		var total = parseFloat(jumlah_beli)*parseFloat(harga_beli);

		var id_orderbarang = $("[name=popup_order]").val();

		if(id_orderbarang != '' && jumlah != '' && jumlah != 0){
			var label_ppn = "";
			if(termasuk_pajak==1){
				label_ppn = "<span class='badge badge-success'><i class='fa fa-plus'></i> PPN</span>";
			}
			if(id_data == ''){
				table_barang.row.add(['<div><center></center></div>',
						'<div id="list_kode'+tb_no+'">'+kode+'</div>',
						'<div id="list_nama'+tb_no+'">'+nama_barang+'</div>',
						'<div id="list_harga'+tb_no+'">'+accounting.formatMoney(harga_beli)+" "+label_ppn+'</div>',
						'<div id="list_jumlah'+tb_no+'">'+format_angka(jumlah)+'</div>',
						'<div id="list_satuan'+tb_no+'">'+satuan+'</div>',
						'<div id="list_total'+tb_no+'">'+accounting.formatMoney(total)+'</div>',
						'<div><button type="button" class="btn btn-xs bg-warning-400" onclick="edit_tableorder('+tb_no+')"><i class="icon-pencil"></i> </button><button class="btn btn-xs bg-danger-800" type="button" onclick="hapus_order($(this))"><i class="icon-eraser"></i></button></div>'+
						'<input type="hidden" name="tabel_id[]" id="tabel_id'+tb_no+'" value="">'+
						'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+tb_no+'" value="'+id_barang+'">'+
						'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+tb_no+'" value="'+jumlah+'">'+
						'<input type="hidden" name="tabel_jumlah_konv[]" id="tabel_jum_konv'+tb_no+'" value="'+jumlah+'">'+
						'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+tb_no+'" value="'+harga_beli+'">'+
						'<input type="hidden" name="tabel_total[]" id="tabel_total'+tb_no+'" value="'+total+'">'+
						'<input type="hidden" name="tabel_idorderbarang[]" id="tabel_idorderbarang'+tb_no+'" value="'+id_orderbarang+'">'+
						'<input type="hidden" name="tabel_idsatuan[]" id="tabel_idsatuan'+tb_no+'" value="'+id_satuan+'">'
						]).draw(false);
			}else{
				$("#list_kode"+id_data).text(kode);
				$("#list_nama"+id_data).text(nama_barang);
				$("#list_harga"+id_data).html(accounting.formatMoney(harga_beli)+" "+label_ppn);
				$("#list_jumlah"+id_data).text(format_angka(jumlah));
				$("#list_satuan"+id_data).text(satuan);
				$("#list_total"+id_data).text(accounting.formatMoney(total));

				$("#tabel_id"+id_data).val(id_data);
				$("#tabel_idbarang"+id_data).val(id_barang);				
				$("#tabel_jumlah"+id_data).val(jumlah);
				$("#tabel_jumlah_konv"+id_data).val(jumlah);
				$("#tabel_harga"+id_data).val(harga_beli);
				$("#tabel_total"+id_data).val(total);
				$("#tabel_idorderbarang"+id_data).val(id_orderbarang);
				$("#tabel_idsatuan"+id_data).val(id_satuan);
			}		

			tb_no++;
			// get_jum();
			if(termasuk_pajak==1 && $("input[name=termasuk_pajak]").is(":checked")==false){
				$("input[name=termasuk_pajak]").attr("checked", "checked").trigger("click");
			}else if(termasuk_pajak==1 && $("input[name=termasuk_pajak]").is(":checked")==true){
				termasuk_pajaks($("input[name=termasuk_pajak]"));
			}else{
				get_jum();
			}

			$("#exampleModal2").modal('hide');
		}else{

		}

	})

	function edit_tableorder(id){

		var id_orderbarang = $("#tabel_idorderbarang"+id).val();
		var jumlah = $("#tabel_jumlah"+id).val();
		var jum_kon = $("#tabel_jum_konv"+id).val();
		var harga_beli = $("#tabel_harga"+id).val();

		$("[name=popup_id_tableorder]").val(id);
		$("[name=popup_order]").val(id_orderbarang).trigger('change');
		$("[name=popup_jum_terima]").val(jumlah);
		$("[name=popup_jum_terimaakhir]").val(jum_kon);
		
		$("[name=popup_harga_beli]").val(harga_beli);
		$("#help_popup_harga_beli").text(accounting.formatMoney(harga_beli));
		$("#help_popup_terima").text(format_angka(jumlah));


		$("#exampleModal2").modal('show');
	}

	function get_editorder(id){
		$.ajax({
			url: "{{ url('pembelian/get_editorder')}} ",
			type: 'post',
			data: {id : id,},
			headers : {
				'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
			},
			success: function(respon){
				if(respon.length > 0){
					for(i in respon){		
						var label_ppn = "";
						if(respon[i].termasuk_pajak==1){
							label_ppn = "<span class='badge badge-success'><i class='fa fa-plus'></i> PPN</span>";
						}
						table_barang.row.add(['<div><center></center></div>',
							'<div id="list_kode'+respon[i].id_beli_barang+'">'+respon[i].kode_barang+'</div>',
							'<div id="list_nama'+respon[i].id_beli_barang+'">'+respon[i].nama_barang+'</div>',
							'<div id="list_harga'+respon[i].id_beli_barang+'">'+accounting.formatMoney(respon[i].harga_satuan)+" "+label_ppn+'</div>',
							'<div id="list_jumlah'+respon[i].id_beli_barang+'">'+format_angka(respon[i].jml_barang)+'</div>',
							'<div id="list_satuan'+respon[i].id_beli_barang+'">'+respon[i].nama_satuan+'</div>',
							'<div id="list_total'+respon[i].id_beli_barang+'">'+accounting.formatMoney(respon[i].total)+'</div>',
							'<div><button type="button" class="btn btn-xs bg-warning-400" onclick="edit_tableorder('+respon[i].id_beli_barang+')"><i class="icon-pencil"></i> </button> <button class="btn btn-xs bg-danger-800" type="button" onclick="hapus_order($(this))"><i class="icon-eraser"></i></button></div>'+
							'<input type="hidden" name="tabel_id[]" id="tabel_id'+respon[i].id_beli_barang+'" value="'+respon[i].id_beli_barang+'">'+
							'<input type="hidden" name="tabel_idbarang[]" id="tabel_idbarang'+respon[i].id_beli_barang+'" value="'+respon[i].id_barang+'">'+
							'<input type="hidden" name="tabel_jumlah[]" id="tabel_jumlah'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang+'">'+
							'<input type="hidden" name="tabel_jumlah_konv[]" id="tabel_jum_konv'+respon[i].id_beli_barang+'" value="'+respon[i].jml_barang_konv+'">'+
							'<input type="hidden" name="tabel_harga[]" id="tabel_harga'+respon[i].id_beli_barang+'" value="'+respon[i].harga_satuan+'">'+
							'<input type="hidden" name="tabel_total[]" id="tabel_total'+respon[i].id_beli_barang+'" value="'+respon[i].total+'">'+
							'<input type="hidden" name="tabel_idorderbarang[]" id="tabel_idorderbarang'+respon[i].id_beli_barang+'" value="'+respon[i].id_obb+'">'+
							'<input type="hidden" name="tabel_idsatuan[]" id="tabel_idsatuan'+respon[i].id_beli_barang+'" value="'+respon[i].id_satuan+'">'
							]).draw(false);
					}

					get_jum();
					// netto();
				}
				
			}
		})
	}

	function hapus_order(d){
		table_barang.row(d.parents('tr')).remove().draw();
		get_jum();
		// netto();
		
	}

</script>

@endsection