<head>
    <style>
        #tblpenjualan th, tr, td {
            border-bottom:1px black solid; border-top:1px black solid;
        }        
    </style>
</head>



<?php $__env->startSection('title'); ?>
	
	<tr><td width="80">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php echo e(tanggal_indonesia(date('Y-m-d'),false)); ?></td></tr>	
<?php $__env->stopSection(); ?>

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

<!------------------------ PENJUALAN KANVAS ------------->

<?php $__env->startSection('content'); ?>   
<div class="row">
	<div class="container-fluid">
		<div class="card">
			<div class="card-block">  
			
    <a onclick="addForm()" class="btn btn-success btn-sm"><i class="fa fa-plus-circle"> </i> New Kanvas </a> 			
    <h6 style="text-align:right; margin-top:-20px"><?php echo e(tanggal_indonesia(date('Y-m-d'),false)); ?></h6>

<form method="post" id="form-penjualan">
<?php echo e(csrf_field()); ?>


<input type="hidden" id="mgr" name ="mgr" value="<?php echo e(Auth::user()->group); ?>"> 

<table class="table table-padding table-striped table-responsive" id="tblpenjualan" width="100%">
<thead>
   <tr class="tblheader">
   
        <th width="100px">No.Faktur</th>
        <th width="30">GDG</th>
        <th width="70px">Tgl.Ftr</th>
        <th width="50px">Jt.Tempo</th>
        <th width="200">Customer</th>
        <th width="75px">Sales</th>
        <th width="25px">Disc</th>
        <th width="70px">Total</th>
        <th width="5px">Act</th>
 
	</tr>
</thead>
<tbody></tbody>

</table>
</form>

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

<?php echo $__env->make('kanvas.penjualan_create', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('kanvas.form_detail', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('kanvas.add_detail', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('kanvas.listcustomer', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('sales.listkemasan', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>
<?php echo $__env->make('sales.add_bonus', \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 = $('#tblpenjualan').DataTable({
        "processing" : true,
        "lengthMenu": [[8, 25, 50, -1], [8, 25, 50, "All"]],       //pilihan jumlah record show
        "order" : [0,'DESC'],
        "ajax" : {
            "url" : "<?php echo e(route('kanvas.datasales')); ?>",
            "type" : "GET"
        }
    }); 
});   

//submit
$('#form-faktur').on("submit", function(e) {

    if(!e.isDefaultPrevented()){
		 
		$('#submit-control').html(' Simpan Create ... ');
		$('#submit-control').attr('disabled','disabled');
		  
        var id = $('#id').val();
        if(save_method == "add") url = "<?php echo e(route('kanvas.store')); ?>";
        else url = "kanvas/"+id;

        $.ajax({
            url : url,
            type : "POST",
            data : $('#form-faktur').serialize(),
            dataType: 'JSON',		   
            success : function(data){
			    if(data.msg=="error"){
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');
					swal("WARNING", " Gagal menyimpan data !","error");
					$('#cus').focus().select();
				}else{
					$('#modal-form_create').modal('hide');
					//swal("SUCCESS", " Transfer baru berhasil dicreate ! ","success");
					$('#submit-control').html('Save');
					$('#submit-control').removeAttr('disabled');		
					$('#tblpenjualan').DataTable().ajax.reload(null, false);
				}   
 		    },
            error : function(){
			    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 batal(id){
    
    if($('#mgr').val() != "MANAGER"){ swal("WARNING", " Anda tidak berhak membatalkan !","info"); return false; }
    
    if(confirm("  Apakah yakin Invoice akan dibatalkan ?  ")){
        $.ajax({
            url : "<?php echo e(DIR_APP); ?>sales/"+id+"/batal",
            type : "GET",
            dataType : "JSON",
            success : function(data){
				 if(data.msg=="error-1"){
					swal("WARNING", " Invoice Gagal dibatalkan karena masih ada transaksi !","error");
				 }else{
					swal("SUCCESS", " Pembatalan Invoice berhasil ! ","success");
					table.ajax.reload();	
				 }
            },
            error : function(){
			    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
			}   
        });
        return false;
	}
}

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

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

            $('#cus').val(data.sale_cust).attr('disabled', 'disabled');
            $('#sls').val(data.sales_id);
            if(data.sale_ppn=="0")
	            $('#ppn').val("0"); else $('#ppn').val("1");
	            
	        $('#dis').val(data.sale_disc);
	        $('#ket').val(data.notes);
	 
	    },
        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){
    $('#idsale').val(id);
    $('#idsale2').val(id);    
    $('#idsale3').val(id);
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>sales/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){
            
            $('#cust').text(data.cus_name+" - "+data.cus_address);
            $('#nofaktur').text("NO.Faktur: "+data.sale_no);
            $('#keterangan').text("Disc Faktur: "+data.sale_disc+"%"+'\xa0\xa0\xa0\xa0\xa0\xa0\xa0'+"PPN: "+data.sale_ppn+"%");

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

function tampilCustomer(){
    
    var id=$('#idsale').val();
   
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>sales/"+id+"/edit",
        type : "GET",
        dataType : "JSON",
        success : function(data){
            
            $('#idcust').val(data.id_cust);
            $('#idsale2').val(data.id);

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


function loadSummary(){
    
    var id=$('#idsale').val();
    
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>sales/"+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); ?>sales/"+id+"/datadetail");
	tbdetail.ajax.reload();
}    

function tampil_Detail(id){
    tbldetail = $('#tbldetail').DataTable({
        "processing" : true,
        "lengthMenu": [[8, 25, 50, -1], [8, 25, 50, "All"]],       //pilihan jumlah record show
        "ajax" : {
            "url" : "<?php echo e(DIR_APP); ?>sales/"+id+"/datadetail",
            "type" : "GET"
        }
    });     
}

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

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

    if(!e.isDefaultPrevented()){
		 
		$('#submit-add').html(' Simpan data ... ');
		$('#submit-add').attr('disabled','disabled');
		  
        var id = $('#iddetail').val();
        if(save_method == "add") { url = "<?php echo e(DIR_APP); ?>sales/adddetail"; tipe="POST"; }
        else { url = "<?php echo e(DIR_APP); ?>sales/"+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=="error2"){
					swal("WARNING", " Invalid Kemasan  !","error");
					$('#prd').focus().select();
					return false;				    
			    }
			    
				if(data.msg=="error1"){
					swal("WARNING", " Stock tidak ada/ Tidak mencukupi !","error");
					$('#prd').focus().select();
					return false;				    
			
				}else{

					$('#add_detail').modal('hide');
					$('#submit-detail').html('Save');
					$('#submit-detail').removeAttr('disabled');		
					loadSummary();					
					$("#tbldetail").DataTable().ajax.reload(null, false);
					$('#tblpenjualan').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); ?>sales/"+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);
	        $('#dis1').val(data.disc_1);
	        $('#pot').val(data.potongan);
	        $('#harga').val(data.harga);
	        $('#qty').val(data.qty);
	        $('#kemasan').val(data.kemasan.concat(' / ', data.isi));
	 
	    },
        error : function(){
            alert("Tidak dapat menampilkan data!");
        }
    });   
}

function posting(){

    swal({
        title: "Anda Yakin ingin memposting Faktur ?",
        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() {
                postingFtr();
            });
        }else {
            swal("Batal", "Invoice masih bisa diedit :)", "error");
        }
    })
}    

function postingFtr(){
    
    $.ajax({
        url : "<?php echo e(DIR_APP); ?>sales/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", "Faktur berhasil diposting !", "success");
                location.reload();
			}   
	    },
        error : function(){
		    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
        }   
    });
    return false;
}


$('#tgltrx').on('change', function(){

    if($('#tgltrx').val() > $('#sekarang').val()){
        alert('Tanggal Invoice Salah');
        return false;
    }
})


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


function printDO(){
    $('#modal-form_do').modal('show');
    $('#modal-form_do form')[0].reset();            
    //$('.modal-title').text('Print DO Keluar');
}

/* bonus */
function bonus(){
    save_method = "add";
    $('input[name=_method]').val('POST');
    $('#add_bonus').modal('show');
    $('#add_bonus form')[0].reset();   
}

//submit add bonus
$('#form-add_bonus').on("submit", function(e) {
    
    //alert('simpan bonus'); return false;

    if(!e.isDefaultPrevented()){
		 
		$('#submit-bonus').html(' Simpan data ... ');
		$('#submit-bonus').attr('disabled','disabled');

        url = "<?php echo e(DIR_APP); ?>sales/addbonus"; 
        

        $.ajax({
            url : url,
            type : 'POST',
            data : $('#form-add_bonus').serialize(),
            dataType: 'JSON',		   
            success : function(data){
			    if(data.msg=="error"){
					$('#submit-bonus').html('Save');
					$('#submit-bonus').removeAttr('disabled');
					swal("WARNING", " Gagal menyimpan bonus !","error");
					
					return false;
			
				}else{

					$('#add_bonus').modal('hide');
					$('#submit-bonus').html('Save');
					$('#submit-bonus').removeAttr('disabled');		
			
					$("#tbldetail").DataTable().ajax.reload(null, false);

				}   
 		    },
            error : function(){
			    swal("WARNING"," DATA tidak dapat disimpan, harap hubungi IT !","error");
			    $('#submit-bonus').html('Save');
			    $('#submit-bonus').removeAttr('disabled');			 
            }   
        });
        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/kanvas/index.blade.php ENDPATH**/ ?>