<?php $__env->startSection('title'); ?>
	
<?php $__env->stopSection(); ?>

<?php $__env->startSection('breadcrumb'); ?>
   ##parent-placeholder-6e5ce570b4af9c70279294e1a958333ab1037c86##
   <li>mutasi stock</li>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>   

<div class="row">
    <div class="container-fluid">
        <div class="card">
            <div class="card-block"> 

    <div class="text-left">
    	<a onclick="addForm()" class="btn btn-outline-success btn-sm" style="margin-bottom:-15px;"><i class="fa fa-plus-circle"> </i> New Mutasi </a> 
    </div>	
    
    <table class="table table-striped table-padding" id="tbl-mutasi" width="100%">
        <thead>
            <tr class="tblheader">
                <th>No.Mutasi</th>
        	    <th width="100px">Tanggal</th>
        	    <th>Asal</th>
                <th>Tujuan</th>
                <th>Keterangan</th>
                <th width="35px">Act</th> 	  
        	</tr>
        
        </thead>
        <tbody></tbody>
    
    </table>

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

<?php echo $__env->make('mutasi.mutasi_create', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('mutasi.form_detail', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('mutasi.add_detail', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('mutasi.listkemasan', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('script'); ?>
<script type="text/javascript">
var table, save_method;

$(function(){
   table = $('#tbl-mutasi').DataTable({
     "processing" : true,
     "ajax" : {
       "url" : "<?php echo e(route('mutasi.header')); ?>",
       "type" : "GET"
     }
   }); 
   
//submit
$(document).on("submit", function(e) {
    if(!e.isDefaultPrevented()){
		 
		$('#submit-control').html(' Simapan Invoice ... ');
		$('#submit-control').attr('disabled','disabled');
		  
        var id = $('#id').val();
        if(save_method == "add") url = "<?php echo e(route('mutasi.store')); ?>";
        else url = "mutasi/"+id;

        $.ajax({
            url : url,
            type : "POST",
            data : $('#modal-form_create form').serialize(),
            dataType: 'JSON',		   
            success : function(data){
			if(data.msg=="discerror")
			{
				swal("WARNING", " Discount Tambahan invalid !","error");
					$('#dis').focus().select();
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');
			}else{
				if(data.msg=="error"){
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');
					swal("WARNING", " Gagal menyimpan data !","error");
					$('#supl').focus().select();
				}else{
					$('#modal-form_create').modal('hide');
					//swal("SUCCESS", " PO baru berhasil dicreate ! ","success");
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');					
					table.ajax.reload(null, false);
				}   
		    }
 		    },
            error : function(){
                //alert("Tidak dapat menyimpan data!");
			    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
			    $('#submit-control').html('Save');
			    $('#submit-control').removeAttr('disabled');			 
            }   
        });
        return false;
	    }
    });
});

function addForm(){
   save_method = "add";
   $('input[name=_method]').val('POST');
   $('#modal-form_create').modal('show');
   $('#modal-form_create form')[0].reset();            
}
   
function editForm(id){
   save_method = "edit";
   $('input[name=_method]').val('PATCH');
   $('#modal-form form')[0].reset();
   $.ajax({
     url : "mutasi/"+id+"/edit",
     type : "GET",
     dataType : "JSON",
     success : function(data){
       $('#modal-form').modal('show');

       $('#id').val(data.id);
	   $('#kode').val(data.sup_id).attr('readonly', true);
       $('#nama').val(data.sup_nama);
       $('#alamat').val(data.sup_alamat);
       $('#telpon').val(data.sup_telp);
	   $('#kota').val(data.sup_kota);
       
     },
     error : function(){
       alert("Tidak dapat menampilkan data!");
     }
   });
}
	 
$('#tuju').on('change', function(){
    if($('#asal').val()==$('#tuju').val()){
        swal('Error', "Gudang Tujuan tidak boleh sama dengan Gudang Asal", 'error');
        return false;
    }
})

function showData(id){
   save_method = "edit";

   $('input[name=_method]').val('PATCH');
   $('#modal-form form')[0].reset();
   $.ajax({
     url : "invoice/"+id+"/lihat",
     type : "GET",
     dataType : "JSON",
     success : function(data){
       $('#modal-form').modal('show');
       $('.modal-title').text('Data Detail');
       
       $('#id').val(data.id_plist);
	   $('#supp').val(data.sup_kode.concat(' - ',data.sup_nama));
  	   $('#nama').val(data.list_sku.concat(' - ',data.sku_name));
	   $('#box').val(data.prc_box).attr('readonly', true);
       $('#pcs').val(data.prc_pcs).attr('readonly', true);
       $('#gross').val(data.prc_hrggross).attr('readonly', true);
	   $('#disc1').val(data.prc_disc1).attr('readonly', true);
	   $('#disc2').val(data.prc_disc2).attr('readonly', true);
	   $('#disc3').val(data.prc_disc3).attr('readonly', true);
	   $('#ppn').val(data.prc_ppn).attr('readonly', true);
	   $('#status').val(data.prc_status).attr('readonly', true);
	   $('#expd').val(data.list_exp).attr('readonly', true);
	                 
     },
     error : function(){
       alert("Tidak dapat menampilkan data!");
     }
   });   
   
}

    
function editHeader(id){
    save_method = "edit";

    $('input[name=_method]').val('PATCH');
    $('#modal-form_create form')[0].reset();
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>invoice/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){
            $('#modal-form_create').modal('show');
            $('.modal-title').text('Edit Header');
       
            $('#id').val(data.id);

            $('#supl').val(data.supp_id).attr('disabled', 'disabled');
            $('#inv').val(data.inv_no);
            $('#tglinv').val(data.inv_tgl);
            $('#tgljt').val(data.inv_jt);
	        $('#ppn').val(data.inv_ppn);
	        $('#dis').val(data.inv_disc);
	 
	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}

function addDetail(id){
   save_method = "add";
   $('input[name=_method]').val('POST');
   $('#form-detail').modal('show');
   $('#form-detail form')[0].reset();   
   tampilHeader(id);
   tampilDetail(id);
   //loadSummary();
}

function tampilHeader(id){
    
    $('#idmts').val(id);
    
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>mutasi/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){
            $('#nofaktur').text("NO.Mutasi: "+data.nomutasi);
            $('#ketr').text("dari "+data.asal+" ke "+data.tuju);

	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}

function loadSummary(){
    
    var id=$('#idinv').val();
    
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>invoice/"+id+"/loadsummary",
        type : "GET",
        dataType : "JSON",
        success : function(data){

            $('#totalftr').text("Total incl PPN : \xa0\xa0\xa0"+data.totalftr);

	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}

    tbdetail = $('#tbldetail').DataTable({
        "scrollY": '55vh',
        "scrollCollapse": true,
        "bSort": false,
        "destroy": true,
        "processing" : true,
	    "paging" : false,
	    "dom" : 'bti',
    });
    
function tampilDetail(id){

    tbdetail.ajax.url("<?php echo e(DIR_APP); ?>mutasi/"+id+"/datadetail");
	tbdetail.ajax.reload();
}    


function addItem(){
    save_method = "add";
    $('input[name=_method]').val('POST');
    $('#add_detail').modal('show');
    $('#add_detail form')[0].reset();   
    
    $('#idmts2').val($('#idmts').val());
}

 	tblproduk = $('.tabel-produk').DataTable({
        "bSort": false,
        "destroy": true,
        "processing" : true,
	    "paging" : false,
	    "dom" : 'bt',
    });

 	tblkemasan = $('.tabel-kemasan').DataTable({
        "bSort": false,
        "destroy": true,
        "processing" : true,
	    "paging" : false,
	    "dom" : 'bt',
    });    
    
//submit add detail
$('#form-add_detail').on("submit", function(e) {

    if(!e.isDefaultPrevented()){
		 
		$('#submit-add').html(' Simpan detail ... ');
		$('#submit-add').attr('disabled','disabled');
		  
        var id = $('#iddetail').val();
        if(save_method == "add") { url = "<?php echo e(DIR_APP); ?>mutasi/adddetail"; tipe="POST"; }
        else { url = "<?php echo e(DIR_APP); ?>mutasi/"+id+"/simpanedit" ; tipe="GET"; }

        $.ajax({
            url : url,
            type : tipe,
            data : $('#form-add_detail').serialize(),
            dataType: 'JSON',		   
            success : function(data){
			    if(data.msg=="error"){
					$('#submit-detail').html('Save');
					$('#submit-detail').removeAttr('disabled');
					swal("WARNING", " Gagal menyimpan data !","error");
					$('#prd').focus().select();
					return false;
			    }
				if(data.msg=="error1"){
					swal("WARNING", " Stock tidak ada/ Tidak mencukupi !","error");
					$('#prd').focus().select();
					return false;				    
			
				}else{
				    //swal("success", 'success', "success");
					$('#add_detail').modal('hide');
					$('#submit-detail').html('Save');
					$('#submit-detail').removeAttr('disabled');		
										
					$("#tbldetail").DataTable().ajax.reload(null, false);
					$('#tbl-mutasi').DataTable().ajax.reload(null, false);

				}   
 		    },
            error : function(){
			    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
			    $('#submit-detail').html('Save');
			    $('#submit-detail').removeAttr('disabled');			 
            }   
        });
        return false;
	}
});
   
function editDetail(id){
    save_method = "edit";

    $('input[name=_method]').val('PATCH');
    $('#add_detail').modal('show');
    $('#add_detail form')[0].reset();       
    
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>mutasi/"+id+"/editdetail",
        type : "GET",
        dataType : "JSON",
        success : function(data){

            $('#iddetail').val(data.id);
	   
            $('#prd2').val(data.product_id).attr('disabled', 'disabled');
            $('#kms').val(data.sub_id);
	        $('#harga').val(data.harga);
	        $('#qty').val(data.qty);
	        $('#kemasan').val(data.satuan.concat(' / ', data.isi));
	 
	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}

function printFaktur(){
    var id = $('#idmts').val();
    window.location="<?php echo e(DIR_APP); ?>mutasi/"+id+"/printfaktur";
}

/* posting: data tidak bisa diedit lagi   */

function posting(){

    swal({
        title: "Anda Yakin ingin memposting Mutasi ?",
        text: "Data tidak bisa diedit lagi jika sudah diposting !",
        icon: "info",
        buttons: [
            'Batal',
            'Posting'
            ],
        dangerMode: true,
        }).then(function(isConfirm) {
        if (isConfirm) {
            swal({
                title: 'Confirm!',
                text: 'Klik OK untuk melanjutkan Posting !',
                icon: 'info'
            }).then(function() {
                //$('.form-rekapinv').submit(); // -- submit form programmatically
                //alert('posting');
                postingMts();
            });
        }else {
            swal("Batal", "Mutasi masih bisa diedit :)", "error");
        }
    })
}    

function postingMts(){
    
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>mutasi/posting",
        type : "POST",
        data : $('#form-detailfaktur').serialize(),
        dataType: 'JSON',		   
        success : function(data){
		    if(data.msg=="error"){
				swal("WARNING", " Gagal posting data !","error");
				
			}else{
                swal("Success", "Mutasi berhasil diposting !", "success");
                location.reload();
			}   
	    },
        error : function(){
		    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
        }   
    });
    return false;
}



</script>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /home/myprojeq/public_html/sriayu/resources/views/mutasi/index.blade.php ENDPATH**/ ?>