var rotacion = $('.tablaTasa').DataTable({
    "autoWidth": false,
    "stateSave":  true,
    "deferRender":true,
    "retrieve":true,
    "processing": false,
    "processing": false,
    "scrollCollapse":true,
    "sScrollY": "70vh",
    "paging":false,
    "colReorder": true,
    "language": {
  
            "decimal": "",
            "sEmptyTable": "No hay información",
            "sInfo": "Mostrando _START_ a _END_ de _TOTAL_ Entradas",
            "sInfoEmpty": "Mostrando 0 to 0 of 0 Entradas",
            "sInfoFiltered": "(Filtrado de _MAX_ total entradas)",
            "sInfoPostFix": "",
            "sInfoThousands": ",",
            "sLengthMenu": "Mostrar _MENU_ Entradas",
            "sLoadingRecords": "Cargando...",
            "sProcessing": "Procesando...",
            "sSearch": "Buscar:",
            "sZeroRecords": "Sin resultados encontrados",
            "oPaginate": {
                "sFirst": "Primero",
                "sLast": "Ultimo",
                "sNext": "Siguiente",
                "sPrevious": "Anterior"
            },
            "oAria": {
                "sSortAscending": ": Activar para ordenar la columna de manera ascendete",
                "sSortDescending": ": Activar para ordenar la columna de manera descendete",
            }
        },
        "drawCallback": function() {
              var api = this.api();
              $(api.column(3).footer()).html(
                  'TOTAL: '+Math.round(api.column(3, {page: 'current'}).data().sum()*100)/100
              )
  
              $(api.column(4).footer()).html(
                  'TOTAL: '+(Math.round(api.column(4, {page: 'current'}).data().sum()*100)/100)
              )
              
              
            }
       
        
  });
  

/* data table*/

/*=============================================
MODIFICAR STOCK FISICO
=============================================*/

$(".formularioPrecision").on("change", "input.stockFisico", function(){

	var stockSistema = $(this).parent().parent().children(".StockSistema").children(".stockSistema");
	
	var precision= $(this).parent().parent().children(".Precision").children(".precision");

	var precision1 =  stockSistema.val() / $(this).val() ; 

    precision1 = Number((precision1 * 100).toFixed(2))

	precision.val(precision1);



	var nuevoStock = Number($(this).attr("stock")) - $(this).val();

	$(this).attr("nuevoStock", nuevoStock);


	if(Number($(this).val()) == 0 ){

		/*=============================================
		SI EL STOCK FISICO ES CERO 
		=============================================*/

		$(this).val(1);

		var precision1 =  stockSistema.val() / $(this).val() ; 
        precision1 = Number((precision1 * 100).toFixed(2))

		precision.val(precision1);


		swal({
	      title: "El stock Fisico de ser Mayor a 0",
	      text: "¡Sólo debe ser Mayor a 0",
	      type: "error",
	      confirmButtonText: "¡Cerrar!"
	    });

	    return;

	}

    listarProductos1()


})

/*=============================================
LISTAR TODOS LOS PRODUCTOS
=============================================*/

function listarProductos1(){

	var listaProductos = [];

	var descripcion = $(".descripcion");

	var stockSistema = $(".stockSistema");

	var stockFisico = $(".stockFisico");

	var precision = $(".precision");

	for(var i = 0; i < descripcion.length; i++){

		listaProductos.push({ "id" : $(stockSistema[i]).attr("idProducto"), 
							  "descripcion" : $(descripcion[i]).val(),
							  "stockSistema" : $(stockSistema[i]).val(),
							  "stockFisico" : $(stockFisico[i]).val(),
							  "precision" : $(precision[i]).val()})

	}

	$("#listaProductos1").val(JSON.stringify(listaProductos)); 

}

/* data table*/
$('.tabla').DataTable({
    "autoWidth": false,
    "deferRender":true,
    "retrieve":true,
    "scrollCollapse": true,
    "paging": false,
    "processing": false,
    "sScrollY": "70vh",
    "scrollCollapse":true,
    "paging":false,
    "language": {

            "decimal": "",
            "sEmptyTable": "No hay información",
            "sInfo": "Mostrando _START_ a _END_ de _TOTAL_ Entradas",
            "sInfoEmpty": "Mostrando 0 to 0 of 0 Entradas",
            "sInfoFiltered": "(Filtrado de _MAX_ total entradas)",
            "sInfoPostFix": "",
            "sInfoThousands": ",",
            "sLengthMenu": "Mostrar _MENU_ Entradas",
            "sLoadingRecords": "Cargando...",
            "sProcessing": "Procesando...",
            "sSearch": "Buscar:",
            "sZeroRecords": "Sin resultados encontrados",
            "oPaginate": {
                "sFirst": "Primero",
                "sLast": "Ultimo",
                "sNext": "Siguiente",
                "sPrevious": "Anterior"
            },
            "oAria": {
                "sSortAscending": ": Activar para ordenar la columna de manera ascendete",
                "sSortDescending": ": Activar para ordenar la columna de manera descendete",
            }
        },
        "dom": "Bfrtilp",
                "buttons": [
                    {
                        "extend": "excel",
                        "text": "<i class='fas fa-file-excel'></i>",
                        "titleAttr" : "Exportar a Excel",
                        "className": 'btn btn-success',
                        "filename": function() {
                            return "rotacion"      
                          },
                        "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"]
                        }
                    },
                    {
                        "extend": "pdfHtml5",
                        "download": "open",
                        "text": "<i class='fas fa-file-pdf'></i>",
                        "titleAttr" : "Exportar a pdf",
                        "className": 'btn btn-danger',
                        "filename" : function() {
                            return "Rotacion"      
                          },      
                        "customize": function(doc) {
                            doc.defaultStyle.fontSize = 8; 
                            doc.defaultStyle.alignment = 'justify'
                            
                         
                        },
                         "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"],
                            "page": 'current'
                        },
                        "footer": true

                    },
                    {
                        "extend": "print",
                        "text": "<i class='fa fa-print'></i>",
                        "titleAttr" : "Imprimir",
                        "className": 'btn btn-info',
                        "customize": function (win) {
                            $(win.document.body)
                                .css( "font-size", "8pt" )
                            $(win.document.body).find( "table" )
                                .addClass( "compact" )
                                .css( "font-size", "8pt" );
                        },
                        "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"]
                        }
                    }
                ]
        
});

$('.tabla1').DataTable({
    "autoWidth": false,
    "deferRender":true,
    "retrieve":true,
    "scrollCollapse": true,
    "paging": false,
    "processing": false,
    "sScrollY": "70vh",
    "scrollCollapse":true,
    "paging":false,
    "language": {

            "decimal": "",
            "sEmptyTable": "No hay información",
            "sInfo": "Mostrando _START_ a _END_ de _TOTAL_ Entradas",
            "sInfoEmpty": "Mostrando 0 to 0 of 0 Entradas",
            "sInfoFiltered": "(Filtrado de _MAX_ total entradas)",
            "sInfoPostFix": "",
            "sInfoThousands": ",",
            "sLengthMenu": "Mostrar _MENU_ Entradas",
            "sLoadingRecords": "Cargando...",
            "sProcessing": "Procesando...",
            "sSearch": "Buscar:",
            "sZeroRecords": "Sin resultados encontrados",
            "oPaginate": {
                "sFirst": "Primero",
                "sLast": "Ultimo",
                "sNext": "Siguiente",
                "sPrevious": "Anterior"
            },
            "oAria": {
                "sSortAscending": ": Activar para ordenar la columna de manera ascendete",
                "sSortDescending": ": Activar para ordenar la columna de manera descendete",
            }
        },
        "dom": "Bfrtilp",
                "buttons": [
                    {
                        "extend": "excel",
                        "text": "<i class='fas fa-file-excel'></i>",
                        "titleAttr" : "Exportar a Excel",
                        "className": 'btn btn-success',
                        "filename": function() {
                            return "PRECISION"      
                          },
                        "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"]
                        }
                    },
                    {
                        "extend": "pdfHtml5",
                        "download": "open",
                        "text": "<i class='fas fa-file-pdf'></i>",
                        "titleAttr" : "Exportar a pdf",
                        "className": 'btn btn-danger',
                        "filename" : function() {
                            return "PRECISION"      
                          },      
                        "customize": function(doc) {
                            doc.defaultStyle.fontSize = 8; 
                            doc.defaultStyle.alignment = 'justify'
                            
                         
                        },
                         "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"],
                            "page": 'current'
                        },
                        "footer": true

                    },
                    {
                        "extend": "print",
                        "text": "<i class='fa fa-print'></i>",
                        "titleAttr" : "Imprimir",
                        "className": 'btn btn-info',
                        "customize": function (win) {
                            $(win.document.body)
                                .css( "font-size", "8pt" )
                            $(win.document.body).find( "table" )
                                .addClass( "compact" )
                                .css( "font-size", "8pt" );
                        },
                        "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"]
                        }
                    }
                ]
        
});

$('.tabla2').DataTable({
    "autoWidth": false,
    "deferRender":true,
    "retrieve":true,
    "scrollCollapse": true,
    "paging": false,
    "processing": false,
    "sScrollY": "70vh",
    "scrollCollapse":true,
    "paging":false,
    "language": {

            "decimal": "",
            "sEmptyTable": "No hay información",
            "sInfo": "Mostrando _START_ a _END_ de _TOTAL_ Entradas",
            "sInfoEmpty": "Mostrando 0 to 0 of 0 Entradas",
            "sInfoFiltered": "(Filtrado de _MAX_ total entradas)",
            "sInfoPostFix": "",
            "sInfoThousands": ",",
            "sLengthMenu": "Mostrar _MENU_ Entradas",
            "sLoadingRecords": "Cargando...",
            "sProcessing": "Procesando...",
            "sSearch": "Buscar:",
            "sZeroRecords": "Sin resultados encontrados",
            "oPaginate": {
                "sFirst": "Primero",
                "sLast": "Ultimo",
                "sNext": "Siguiente",
                "sPrevious": "Anterior"
            },
            "oAria": {
                "sSortAscending": ": Activar para ordenar la columna de manera ascendete",
                "sSortDescending": ": Activar para ordenar la columna de manera descendete",
            }
        },
        "dom": "Bfrtilp",
                "buttons": [
                    {
                        "extend": "excel",
                        "text": "<i class='fas fa-file-excel'></i>",
                        "titleAttr" : "Exportar a Excel",
                        "className": 'btn btn-success',
                        "filename": function() {
                            return "ABASTECIMIENTO"      
                          },
                        "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"]
                        }
                    },
                    {
                        "extend": "pdfHtml5",
                        "download": "open",
                        "text": "<i class='fas fa-file-pdf'></i>",
                        "titleAttr" : "Exportar a pdf",
                        "className": 'btn btn-danger',
                        "filename" : function() {
                            return "ABASTECIMIENTO"      
                          },      
                        "customize": function(doc) {
                            doc.defaultStyle.fontSize = 8; 
                            doc.defaultStyle.alignment = 'justify'
                            
                         
                        },
                         "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"],
                            "page": 'current'
                        },
                        "footer": true

                    },
                    {
                        "extend": "print",
                        "text": "<i class='fa fa-print'></i>",
                        "titleAttr" : "Imprimir",
                        "className": 'btn btn-info',
                        "customize": function (win) {
                            $(win.document.body)
                                .css( "font-size", "8pt" )
                            $(win.document.body).find( "table" )
                                .addClass( "compact" )
                                .css( "font-size", "8pt" );
                        },
                        "exportOptions": {
                            "columns": ["0","1","2","3", "4", "5"]
                        }
                    }
                ]
        
});

