Advertisement
ganryu

realizar_pedido php

Nov 15th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.37 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class Fachada_pedido extends CI_Controller {
  4.    
  5.     public function mostrar_pedidos() {
  6.         $nombre_sucursal = $this->input->post('sucursal');
  7.         $pedido = new Pedido();
  8.         $sucursal = new Sucursal();
  9.         $venta = new Venta();
  10.        
  11.         $sucursal->where('nombre_sucursal', $nombre_sucursal)->get();
  12.        
  13.         $pedido->where_related($sucursal);
  14.        
  15.         $pedido->get();
  16.         foreach ( $pedido as $row ) {
  17.             $url = base_url() . 'Fachada_pedido/ver_pedido/' . $row->id;
  18.             echo '<tr>';
  19.             echo '<td>' . $row->id . "</td>";
  20.             echo '<td class="text-center">' . $row->fecha_hora . "</td>";
  21.             echo '<td class="text-center">' . $row->cliente->get()->dni."</td>";
  22.             echo '<td class="text-center">' . $row->cliente->get()->apellido . " " . $row->cliente->get()->nombre . "</td>";
  23.             echo '<td class="text-right">' . $row->importe . "</td>";
  24.             echo '<td><a role="button" id="ver_pedido"class="btn btn-info pull-right" href="' . $url . '">Ver Pedido</a></td>';
  25.             echo '</tr>';
  26.         }
  27.     }
  28.    
  29.     // Función para mientras no tengamos forma de obtener el id del cliente actual
  30.     public function _obtener_id_cliente() {
  31.         $rol = $this->session->userdata('rol');
  32.         $mail = $this->session->userdata('mail');
  33.        
  34.         if ($rol === 'cliente') {
  35.             $c = new Cliente();
  36.             return $c->where_related('usuario','mail', $mail)->get()->id;
  37.         } else {
  38.             return 1;
  39.         }
  40.     }
  41.  
  42.     public function realizar_pedido() {
  43.         $rol = $this->session->userdata('rol');
  44.         $roles = array('vendedor', 'cliente', 'administrador');
  45.         if (in_array($rol, $roles)) {
  46.             $this->load->view('includes/Header');
  47.             $this->load->view('Botones');
  48.             $this->load->view('sucursales');
  49.             $this->load->view('realizar_pedido');
  50.             $this->load->view('includes/Footer');
  51.         } else {
  52.             redirect('login');
  53.         }
  54.     }
  55.    
  56.     public function crear_pedido() {
  57.         $cliente = $this->input->post('cliente');
  58.         $sucursal = $this->input->post('sucursal');
  59.        
  60.         $rol = $this->session->userdata('rol');
  61.         $mail = $this->session->userdata('mail');
  62.        
  63.         $s = new Sucursal();
  64.         $sucursal_id = $s->where('nombre_sucursal', $sucursal)->get()->id;
  65.        
  66.        
  67.         if($rol === 'vendedor') {
  68.             $cliente_id = 1;
  69.         } else {
  70.             $c = new Cliente();
  71.             $cliente_id = $c->where_related_usuario('mail', $mail)->get()->id;
  72.         }
  73.        
  74.         $pedido = new Pedido();
  75.         $nuevo_pedido = $pedido->crear_pedido($cliente_id, $sucursal_id);
  76.        
  77.         if ($rol === 'vendedor') {
  78.             echo $nuevo_pedido;
  79.         } else {
  80.             echo "cliente";
  81.         }
  82.     }
  83.    
  84.     public function mostrar_carrito() {
  85.         $sucursal = $this->input->post('sucursal');
  86.        
  87.         $id_cliente = $this->_obtener_id_cliente();
  88.        
  89.         $carritos = new Carrito();
  90.         $carritos->where('cliente_id', $id_cliente);
  91.         $carritos->where_related('inventario/sucursal', 'nombre_sucursal', $sucursal);
  92.        
  93.         $p = new Pedido();
  94.        
  95.         $carritos->get();
  96.         foreach ($carritos as $carrito) {
  97.             $cantidad = $carrito->cantidad;
  98.             $producto = $carrito->inventario->get()->producto->get();
  99.             $precio = $p->obtener_precio($producto->precio, $cantidad);
  100.            
  101.             $boton_cancelar = '<button type="button" class="btn btn-danger" id="cancelar">' .
  102.                                 'Eliminar</button>';
  103.            
  104.             echo '<tr id="tr">';
  105.             echo '<td width="25%" class="text-center">' . $producto->nombre_producto . '</td>';
  106.             echo '<td width="25%" class="text-center">' . $cantidad . '</td>';
  107.             echo '<td width="25%" class="text-center"> $' . $precio . '</td>';
  108.             echo '<td width="25%" class="text-center">' . $boton_cancelar . '</td>';
  109.             echo '</tr>';
  110.         }
  111.     }
  112.    
  113.     public function agregar_producto_carrito() {
  114.         // Recibe parámetros por POST
  115.         $producto = $this->input->post('producto');
  116.         $cantidad = $this->input->post('cantidad');
  117.         $sucursal = $this->input->post('sucursal');
  118.        
  119. //      echo "Ingresamos al método agregar_producto_carrito de Fachada_pedido \n";
  120.        
  121.         // Obtiene producto asociado al nombre obtenido por parámetro
  122.         $p = new Producto();
  123.         $producto = $p->where('nombre_producto', $producto)->get();
  124.        
  125. //      echo "Producto: " . $producto->nombre_producto . "\n";
  126.    
  127.         // Obtiene inventario correspondiente al producto y a la sucursal
  128.         $inventario = new Inventario();
  129.         $inventario->where_related($producto);
  130.         $inventario->where_related('sucursal', 'nombre_sucursal', $sucursal);
  131.         $inventario->get();
  132.        
  133.         $stock = $inventario->stock;
  134.         $inventario_id = $inventario->id;
  135.        
  136. //      echo "Id de Inventario: " . $inventario_id . "\n";
  137. //      echo "Stock de Inventario: " . $stock . "\n";
  138.  
  139.         // Obtiene carrito si existe.
  140.         $carrito = new Carrito();
  141.         $id_cliente = $this->_obtener_id_cliente();
  142.         $carrito->where('cliente_id', $id_cliente);
  143.         $carrito->where('inventario_id', $inventario_id);
  144.         $carrito->get();
  145.        
  146.         $inventario->trans_begin();
  147.        
  148.         // Actualizo stock de inventario
  149.         if ($stock >= $cantidad) {
  150.             $nuevo_stock = $stock - $cantidad;
  151.             $inventario->stock = $nuevo_stock;
  152.             $inventario->save();
  153.             if($inventario->trans_status() === FALSE) {
  154.                 $inventario->trans_rollback();
  155.             }
  156.         }
  157.        
  158.         $carrito->trans_begin();
  159.         if($carrito->exists()) {
  160.             // Si ya existe el carrito para este inventario y cliente actualizo la cantidad
  161.             $cantidad = $cantidad + $carrito->cantidad;
  162.             $carrito->cantidad = $cantidad;
  163.             $carrito->save();
  164.         } else {
  165.             // Si el carrito es nuevo lo guardo
  166.             $carrito->guardar($id_cliente, $inventario_id, $cantidad);
  167.         }
  168.        
  169.         if($carrito->trans_status() === TRUE) {
  170.             $inventario->trans_commit();
  171.             $carrito->trans_commit();
  172.         } else {
  173.             "No tiene elementos suficientes en el inventario";
  174.         }
  175.     }
  176.  
  177.     public function quitar_producto_carrito() {
  178.         // Recibe parámetros por POST
  179.         $producto = $this->input->post('producto');
  180.         $sucursal = $this->input->post('sucursal');
  181.    
  182.         // Obtiene el inventario asociado al producto y la sucursal
  183.         $inventario = new Inventario();
  184.         $inventario->where_related('sucursal', 'nombre_sucursal', $sucursal);
  185.         $inventario->where_related('producto', 'nombre_producto', $producto)->get();
  186.    
  187.         // Elimina el carrito del cliente asociado al inventario
  188.         $carrito = new Carrito();
  189.         $carrito->where_related($inventario);
  190.         $id_cliente = $this->_obtener_id_cliente();
  191.         $carrito->where('cliente_id', $id_cliente)->get();
  192.        
  193.         $inventario->stock = $inventario->stock + $carrito->cantidad;
  194.         $inventario->save();
  195.         $carrito->delete();
  196.     }
  197.    
  198.     public function consultar_pedidos(){
  199.         $rol = $this->session->userdata('rol');
  200.         $roles = array('vendedor', 'administrador');
  201.         if(in_array($rol, $roles)) {
  202.             $this->load->view('includes/Header');
  203.             $this->load->view('Botones');
  204.             $this->load->view('sucursales');
  205.             $this->load->view('Consultar_Pedidos');
  206.             $this->load->view('includes/Footer');
  207.         } else {
  208.             redirect('login');
  209.         };
  210.     }
  211.    
  212.     public function ver_pedido($id_pedido){
  213.         $data = array(
  214.                 'id_pedido' => $id_pedido
  215.         );
  216.         $rol = $this->session->userdata('rol');
  217.         $roles = array('vendedor', 'cliente', 'administrador');
  218.         if(in_array($rol, $roles)){
  219.             $this->load->view('includes/Header');
  220.             $this->load->view('Botones');
  221.             $this->load->view('ver_pedido', $data);
  222.             $this->load->view('includes/Footer');
  223.         } else {
  224.             redirect('login');
  225.         }
  226.        
  227.     }
  228.    
  229.     public function asentar_venta($id_pedido) {
  230.         $venta = new Venta();
  231.         echo $venta->crear_venta($id_pedido);
  232.     }
  233.    
  234.     public function mostrar_productos() {
  235.         $productos = $this->input->post('productos');
  236.         $sucursal = $this->input->post('sucursal');
  237.         $producto = new Producto();
  238.         $inventario = new Inventario();
  239.         foreach ( $productos as $p ) {
  240.             $producto->from_json($p);
  241.             $inventario->where('stock >', 0);
  242.             $inventario->where_related('sucursal', 'nombre_sucursal', $sucursal);
  243.             $inventario->where_related($producto)->get();
  244.             echo '
  245.             <div class="col-md-4">
  246.                 <div class="thumbnail">
  247.                     <img src="' . base_url() . 'assets/imagenes/' . $producto->path_img . '" style="width:300px; height:300px" />
  248.                     <h3>' . $producto->nombre_producto . '</h3>
  249.                     <p>
  250.                     <label for="cant1">Cantidad:</label>
  251.                     <input class="form-control" type="number" name="cantidad" min="1" value="1" max="' . $inventario->stock . '">
  252.                     </p>
  253.                     <button class="btn btn-primary" id="agregar">
  254.                         <span class="glyphicon glyphicon-plus-sign"></span>
  255.                         Agregar
  256.                     </button>
  257.                 </div>
  258.             </div>';
  259.         }
  260.     }
  261.    
  262.     public function cargar_pedido(){
  263.         $id_pedido = $this->input->post('id_pedido');
  264.         $linea= new Linea_pedido();
  265.         $linea->where('pedido_id', $id_pedido)->get();
  266.         foreach ( $linea as $row ) {
  267.             $cantidad = $row->cantidad;
  268.             $precio = $row->precio_producto;
  269.             $subtotal = $cantidad * $precio;
  270.             $nombre=$row->producto->get()->nombre_producto;
  271.             echo '<tr>';
  272.             echo '<td width="25%" class="text-center">' .$nombre.'</td>';
  273.             echo '<td width="25%" class="text-center">' .$precio. '</td>';
  274.             echo '<td width="25%" class="text-center">'.$cantidad.'</td>';
  275.             echo '<td width="25%" class="text-right">' . $subtotal .'</td>';
  276.             echo '</tr>';
  277.         }
  278.     }
  279.    
  280.     public function contar_productos_carrito() {
  281.         $sucursal = $this->input->post('sucursal');
  282.         $cliente_id = $this->_obtener_id_cliente();
  283.        
  284.         $carrito = new Carrito();
  285.         $carrito->where_related('inventario/sucursal', 'nombre_sucursal', $sucursal);
  286.         $carrito->where_related('cliente', 'id', $cliente_id);
  287.        
  288.         echo $carrito->count();
  289.     }
  290.    
  291.     public function obtener_max_inventario() {
  292.         $this->input->post('sucursal');
  293.         $this->input->post('');
  294.         $inventario = new Inventario();
  295.         $inventario->
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement