<?php
$connect = mysqli_connect("localhost", "root", "", "mhw_inventariocsm2");//Configurar los datos de conexion
$columns = array('codigo_transaccion','codigo', 'numero', 'tipo_transaccion', 'idenc', 'fecha');

 
$query = "SELECT codigo_transaccion,codigo,numero,tipo_transaccion,idenc,fecha FROM transaccion_medicamentos WHERE ";
 
if($_POST["is_date_search"] == "yes")
{
 $query .= 'fecha BETWEEN "'.$_POST["start_date"].'" AND "'.$_POST["end_date"].'" AND ';
}
 
if(isset($_POST["search"]["value"]))
{
 $query .= '
  (codigo_transaccion LIKE "%'.$_POST["search"]["value"].'%" 
  OR codigo LIKE "%'.$_POST["search"]["value"].'%" 
  OR numero LIKE "%'.$_POST["search"]["value"].'%" 
  OR tipo_transaccion LIKE "%'.$_POST["search"]["value"].'%"
  OR idenc LIKE "%'.$_POST["search"]["value"].'%")
 ';
}
 
if(isset($_POST["order"]))
{
 $query .= 'ORDER BY '.$columns[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' 
 ';
}
else
{
 $query .= 'ORDER BY tipo_transaccion DESC ';
}
 
$query1 = '';
 
if($_POST["length"] != -1)
{
 $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}
 
$number_filter_row = mysqli_num_rows(mysqli_query($connect, $query));
 
$result = mysqli_query($connect, $query . $query1);
 
$data = array();
 
while($row = mysqli_fetch_array($result))
{
 $fecha=date("d/m/Y", strtotime($row["fecha"]));			
 $sub_array = array();
 $sub_array[] = $row["codigo_transaccion"];
  $sub_array[] = $row["codigo"];
 $sub_array[] = $row["numero"];
 $sub_array[] = $row["tipo_transaccion"];
 $sub_array[] = $row["idenc"];

 $sub_array[] = $fecha;
 
 $data[] = $sub_array;
}
 
function get_all_data($connect)
{
 $query = "SELECT codigo_transaccion,codigo,numero,tipo_transaccion,idenc,fecha FROM transaccion_medicamentos";
 $result = mysqli_query($connect, $query);
 return mysqli_num_rows($result);
}
 
$output = array(
 "draw"    => intval($_POST["draw"]),
 "recordsTotal"  =>  get_all_data($connect),
 "recordsFiltered" => $number_filter_row,
 "data"    => $data
);
 
echo json_encode($output);
 
?>