function generarFactura($reserva, $hotel, $negativo = false) { try { global $template_dir; global $template_url; global $app_dir; global $smarty; $campania = getCampania($hotel->campaniaId); $reserva->id = decryptId($reserva->id); if ($negativo) { $factura = getFacturaByIdReserva($reserva->id); if ($factura) { $negativo = $factura->id; $numeroFactura = 'R-' . $factura->numero; } else { return false; } } else { $count = DAOFactory::getFacturaDAO()->countByYear(intval(date('Y')), $campania->localizador); $count = '' . $count[0][0]; $count++; while (strlen($count) < 4) { $count = '0' . $count; } $numeroFactura = $count . '-' . $campania->localizador . '-' . date('Y'); } insertFactura($hotel->id, array($reserva->id), "cobrado", $negativo); $smarty->assign('numero', $numeroFactura); $smarty->assign('reserva', $reserva); $smarty->assign('hotel', $hotel); $smarty->assign('negativo', $negativo); $content = $smarty->fetch('reserva_factura.tpl'); $filename = $app_dir . '/cache/factura_' . time() . '.pdf'; Html2PdfWriter::write($content, $filename); return $filename; } catch (Exception $e) { print_r($e); return false; } }
<?php $reservas = getAllReservas(); $reservas_hotel = array(); $reservas_cliente_positivas = array(); $reservas_cliente_negativas = array(); foreach ($reservas as $reserva) { $facturar = true; switch ($reserva->estado) { case 'Stayed': $reservas_hotel[$reserva->hotelId][] = $reserva->id; $facturar = false; break; case 'No show': $facturar = false; break; case 'Cancelada por el administrador': $facturar = false; $id_factura = insertFactura($reserva->hotelId, array($reserva->id), "cobrada", false, date('Y-m-d H:i:s', strtotime($reserva->tiempoCreacion)), 'usuario'); insertFactura($reserva->hotelId, array($reserva->id), "cobrada", $id_factura, date('Y-m-d H:i:s', strtotime($reserva->tiempoCreacion)), 'usuario'); break; default: break; } if ($facturar) { insertFactura($reserva->hotelId, array($reserva->id), "cobrada", false, date('Y-m-d H:i:s', strtotime($reserva->tiempoCreacion)), 'usuario'); } } foreach ($reservas_hotel as $h_k => $r_h) { insertFactura($h_k, $r_h, "pendiente de cobro", false, null, 'hotel'); }