예제 #1
0
 public function process()
 {
     $factura = new Factura();
     $factura->usuario_id = Auth::user()->id;
     $factura->total = Cart::total();
     foreach (Cart::content() as $item) {
         if (Item::find($item['id'])->stock == 0) {
             Session::flash('error', 'El item ' . $item['name'] . ' se ha agotado');
             return Redirect::back();
         }
         if (Item::find($item['id'])->stock - $item['qty'] < 0) {
             Session::flash('error', 'No hay suficiente stock del item ' . $item['name'] . ' para cubrir su pedido');
             return Redirect::back();
         }
     }
     if ($factura->save()) {
         foreach (Cart::content() as $item) {
             $detalle = new Detalle();
             $detalle->factura_id = $factura->id;
             $detalle->item_id = $item['id'];
             $detalle->cantidad = $item['qty'];
             if ($detalle->save()) {
                 $deduct = Item::find($item['id']);
                 $deduct->stock -= $item['qty'];
                 $deduct->save();
             } else {
                 Session::flash('error', 'Error al procesar');
                 return Redirect::back();
             }
         }
     } else {
         Session::flash('error', 'Error al procesar');
         return Redirect::back();
     }
     Cart::destroy();
     return Redirect::to('shop');
 }
예제 #2
0
 public function guardarPedido()
 {
     if (Request::ajax()) {
         $num_productos = count(Input::get('codigos'));
         $codigos = Input::get('codigos');
         $cantidades = Input::get('cantidades');
         $unidadManejo = Input::get('unidadManejo');
         $n_pedido = $this->tomarNumPedido();
         $pedido = new Pedido();
         $pedido->sede_id = Auth::user()->sede_id;
         $pedido->NumeroPedido = $n_pedido;
         $pedido->CodigoCliente = Auth::user()->Codigo_Cliente;
         $pedido->DescuentoNegociado = 0;
         $pedido->Status = 'PENDIENTE';
         $pedido->Enviado = 0;
         $pedido->save();
         /*$id_pedido = DB::table('pedidos')
                   ->insertGetId($nuevo_pedido);
           */
         $id_pedido = $pedido->id;
         if ($id_pedido) {
             for ($i = 0; $i < $num_productos; $i++) {
                 $producto = DB::table('productos')->where('Codigo', '=', $codigos[$i])->first();
                 $decremento = $cantidades[$i] * $unidadManejo[$i];
                 DB::table('inventarios')->where('user_id', '=', Auth::user()->id)->where('Codigo', '=', $codigos[$i])->decrement('Existencia', $decremento);
                 $detalle = new Detalle();
                 $detalle->pedido_id = $id_pedido;
                 $detalle->producto_id = $producto->id;
                 $detalle->Cantidad = $cantidades[$i];
                 $detalle->DescuentoNegociado = 0;
                 $detalle->save();
             }
             return Response::json(array("guardado" => true, "pedido_id" => $id_pedido));
         }
     }
 }