public function executeDetalle(sfWebRequest $request) { $Factura = FacturaQuery::create()->findOneById($request->getParameter('id')); if (!$Factura->getActivo()) { $this->getUser()->setFlash('error', 'Factura cerrada'); $this->redirect('inicio/index'); } $this->form = new VentaDetalleForm(); if ($request->isMethod('POST')) { $this->form->bind($request->getParameter('venta_detalle')); if ($this->form->isValid()) { $valores = $this->form->getValues(); $FacturaDetalle = FacturaDetalleQuery::create()->filterByProductoId($valores['Producto'])->filterByFacturaId($request->getParameter('id'))->findOne(); $Factura = FacturaQuery::create()->findOneById($request->getParameter('id')); $cantidad = 0; $Producto = InventarioQuery::create()->filterByProductoId($valores['Producto'])->findOne(); $fecha = date('d/m/Y'); $Promocion = PromocionQuery::create()->filterByProductoId($valores['Producto'])->where("fecha_inicio <= '{$fecha}' and fecha_fin >= '{$fecha}'")->findOne(); $descuento = 1; if ($Promocion) { $descuento = 1 - $Promocion->getDescuento() / 100; } if ($FacturaDetalle) { $FacturaDetalle->setCantidad($valores['Cantidad'] + $FacturaDetalle->getCantidad()); $cantidad = $valores['Cantidad']; } else { $FacturaDetalle = new FacturaDetalle(); $FacturaDetalle->setProveedorId($Producto->getProveedorId()); $FacturaDetalle->setFacturaId($request->getParameter('id')); $FacturaDetalle->setProductoId($valores['Producto']); $FacturaDetalle->setCantidad($valores['Cantidad']); $cantidad = $valores['Cantidad']; $FacturaDetalle->setPrecioUnitario($Producto->getPrecioCompra() * 1.2 * $descuento); } if ($cantidad <= $Producto->getCantidad()) { $Producto->setCantidad($Producto->getCantidad() - $cantidad); $Producto->save(); $FacturaDetalle->save(); } else { $this->getUser()->setFlash('error', 'Cantidad solicitada supera a la cantidad en existencia'); } $this->redirect('venta/detalle?id=' . $request->getParameter('id')); } } $this->id = $request->getParameter('id'); $this->detalles = FacturaDetalleQuery::create()->findByFacturaId($this->id); $this->factura = FacturaQuery::create()->findOneById($this->id); }