if(isset($_POST["albarancomprafactura"])){
$conexion = new Conexion();
$lineasalbaran = $_POST["albarancomprafactura"];
if($lineasalbaran === ''){
echo '<div id="alert" class="alert alert-danger">
Error!!! no has seleccionado ninguna linea del albaran
</div>';
}else{
$array = implode("','",$lineasalbaran);
$sql = "SELECT * FROM pos_lineasalbarancompra WHERE id_lineaalbarancompra IN ($array)";
$stm = $conexion->prepare($sql);
$stm->execute();
$data = $stm->fetchAll();
$stmt = $conexion->prepare("SELECT id_facturacompra FROM pos_facturascompras ORDER BY id_facturacompra DESC LIMIT 1");
$stmt->execute();
$row = $stmt->fetch();
$facturacompra = $row['id_facturacompra'] + 1;
$stmt = $conexion->prepare("INSERT INTO pos_facturascompras (id_facturacompra) VALUES (:id_facturacompra)");
$stmt->bindParam(':id_facturacompra', $facturacompra);
$stmt->execute();
$array = implode(',', $lineasalbaran);
$consulta = "SELECT * FROM pos_lineasalbarancompra WHERE id_lineaalbarancompra IN ($array)";
$result = $conexion->prepare($consulta);
$result->execute();
$data = $result->fetchAll();
foreach($data as $row){
$fecha = date("Y-m-d");
$sql = "INSERT INTO pos_lineasfacturacompra (codarticulo, descripcionarticulo, subtotallinea, cantiva, cantrecargo, precio, cantidad, id_factura, tipoiva, tiporecargo, fecha, usuario, tienda, id_iva) VALUES (:codarticulo, :descripcionarticulo, :subtotallineacompra, :ivalinea, :cantrecargo, :precio, :unidades, :id_facturacompra, :tipoiva, :tiporecargo, :fechalineacompra, :usuario, :tienda, :id_iva)";
$statement = $conexion->prepare($sql);
$statement->bindParam(':codarticulo', $row[1]);
$statement->bindParam(':descripcionarticulo', $row[2]);
$statement->bindParam(':subtotallineacompra', $row[7]);
$statement->bindParam(':ivalinea', $row[8]);
$statement->bindParam(':cantrecargo', $row[9]);
$statement->bindParam(':precio', $row[4]);
$statement->bindParam(':tipoiva', $row[5]);
$statement->bindParam(':tiporecargo', $row[6]);
$statement->bindParam(':unidades', $row[3]);
$statement->bindParam(':id_facturacompra', $facturacompra);
$statement->bindParam(':fechalineacompra', $fecha);
$statement->bindParam(':usuario', $row[12]);
$statement->bindParam(':tienda', $row[13]);
$statement->bindParam(':id_iva', $row[14]);
if(!$statement){
$mensaje = "Error, no se ha podido dar de alta la factura";
}else{
$statement->execute();
}
}
$conexion = new Conexion();
$stmt = $conexion ->prepare("SELECT SUM(subtotallinea) AS Subtotal, SUM(cantiva) AS Totaliva, ROUND(SUM(cantrecargo), 2) AS Totalrecargo FROM pos_lineasfacturacompra WHERE id_factura= '". $facturacompra . "'");
$stmt->execute();
$resultados = $stmt->fetchAll();
foreach($resultados as $fila){
$total = $fila['Subtotal']+$fila['Totaliva']+$fila['Totalrecargo'];
$subtotal = $fila['Subtotal'];
$totaliva = $fila['Totaliva'];
$totalrecargo = $fila['Totalrecargo'];
$sql = "UPDATE pos_facturascompras SET subtotalfactcompra=:subtotal, ivafactcompra=:totaliva, recargofactcompra=:totalrecargo, totalfactura=:Sumatotal WHERE id_facturacompra=:id_factura";
$statement = $conexion->prepare($sql);
$statement->bindParam(':subtotal', $subtotal);
$statement->bindParam(':totaliva', $totaliva);
$statement->bindParam('totalrecargo', $totalrecargo);
$statement->bindParam(':Sumatotal', $total);
$statement->bindParam(':id_factura', $facturacompra);
$statement->execute();
}
echo '<div id="alert" class="alert alert-success">
Perfecto!!! Linea o lineas facturadas
</div>';
$lineafacturada = 'SI';
$sql = "UPDATE pos_lineasalbarancompra SET lineafacturada=:lineafacturada, factura=:factura WHERE id_lineaalbarancompra IN ($array)";
$statement = $conexion->prepare($sql);
$statement->bindParam(':lineafacturada', $lineafacturada);
$statement->bindParam(':factura', $facturacompra);
$statement->execute();
}
$statement = null;
$stmt = null;
$sql2 = null;
$sql = null;
}
?>