Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- defined('BASEPATH') or exit('No direct script access allowed');
- class Fachada_pedido extends CI_Controller {
- public function mostrar_pedidos() {
- $nombre_sucursal = $this->input->post('sucursal');
- $pedido = new Pedido();
- $sucursal = new Sucursal();
- $venta = new Venta();
- $sucursal->where('nombre_sucursal', $nombre_sucursal)->get();
- $pedido->where_related($sucursal);
- $pedido->get();
- foreach ( $pedido as $row ) {
- $url = base_url() . 'Fachada_pedido/ver_pedido/' . $row->id;
- echo '<tr>';
- echo '<td>' . $row->id . "</td>";
- echo '<td class="text-center">' . $row->fecha_hora . "</td>";
- echo '<td class="text-center">' . $row->cliente->get()->dni."</td>";
- echo '<td class="text-center">' . $row->cliente->get()->apellido . " " . $row->cliente->get()->nombre . "</td>";
- echo '<td class="text-right">' . $row->importe . "</td>";
- echo '<td><a role="button" id="ver_pedido"class="btn btn-info pull-right" href="' . $url . '">Ver Pedido</a></td>';
- echo '</tr>';
- }
- }
- // Función para mientras no tengamos forma de obtener el id del cliente actual
- public function _obtener_id_cliente() {
- $rol = $this->session->userdata('rol');
- $mail = $this->session->userdata('mail');
- if ($rol === 'cliente') {
- $c = new Cliente();
- return $c->where_related('usuario','mail', $mail)->get()->id;
- } else {
- return 1;
- }
- }
- public function realizar_pedido() {
- $rol = $this->session->userdata('rol');
- $roles = array('vendedor', 'cliente', 'administrador');
- if (in_array($rol, $roles)) {
- $this->load->view('includes/Header');
- $this->load->view('Botones');
- $this->load->view('sucursales');
- $this->load->view('realizar_pedido');
- $this->load->view('includes/Footer');
- } else {
- redirect('login');
- }
- }
- public function crear_pedido() {
- $cliente = $this->input->post('cliente');
- $sucursal = $this->input->post('sucursal');
- $rol = $this->session->userdata('rol');
- $mail = $this->session->userdata('mail');
- $s = new Sucursal();
- $sucursal_id = $s->where('nombre_sucursal', $sucursal)->get()->id;
- if($rol === 'vendedor') {
- $cliente_id = 1;
- } else {
- $c = new Cliente();
- $cliente_id = $c->where_related_usuario('mail', $mail)->get()->id;
- }
- $pedido = new Pedido();
- $nuevo_pedido = $pedido->crear_pedido($cliente_id, $sucursal_id);
- if ($rol === 'vendedor') {
- echo $nuevo_pedido;
- } else {
- echo "cliente";
- }
- }
- public function mostrar_carrito() {
- $sucursal = $this->input->post('sucursal');
- $id_cliente = $this->_obtener_id_cliente();
- $carritos = new Carrito();
- $carritos->where('cliente_id', $id_cliente);
- $carritos->where_related('inventario/sucursal', 'nombre_sucursal', $sucursal);
- $p = new Pedido();
- $carritos->get();
- foreach ($carritos as $carrito) {
- $cantidad = $carrito->cantidad;
- $producto = $carrito->inventario->get()->producto->get();
- $precio = $p->obtener_precio($producto->precio, $cantidad);
- $boton_cancelar = '<button type="button" class="btn btn-danger" id="cancelar">' .
- 'Eliminar</button>';
- echo '<tr id="tr">';
- echo '<td width="25%" class="text-center">' . $producto->nombre_producto . '</td>';
- echo '<td width="25%" class="text-center">' . $cantidad . '</td>';
- echo '<td width="25%" class="text-center"> $' . $precio . '</td>';
- echo '<td width="25%" class="text-center">' . $boton_cancelar . '</td>';
- echo '</tr>';
- }
- }
- public function agregar_producto_carrito() {
- // Recibe parámetros por POST
- $producto = $this->input->post('producto');
- $cantidad = $this->input->post('cantidad');
- $sucursal = $this->input->post('sucursal');
- // echo "Ingresamos al método agregar_producto_carrito de Fachada_pedido \n";
- // Obtiene producto asociado al nombre obtenido por parámetro
- $p = new Producto();
- $producto = $p->where('nombre_producto', $producto)->get();
- // echo "Producto: " . $producto->nombre_producto . "\n";
- // Obtiene inventario correspondiente al producto y a la sucursal
- $inventario = new Inventario();
- $inventario->where_related($producto);
- $inventario->where_related('sucursal', 'nombre_sucursal', $sucursal);
- $inventario->get();
- $stock = $inventario->stock;
- $inventario_id = $inventario->id;
- // echo "Id de Inventario: " . $inventario_id . "\n";
- // echo "Stock de Inventario: " . $stock . "\n";
- // Obtiene carrito si existe.
- $carrito = new Carrito();
- $id_cliente = $this->_obtener_id_cliente();
- $carrito->where('cliente_id', $id_cliente);
- $carrito->where('inventario_id', $inventario_id);
- $carrito->get();
- $inventario->trans_begin();
- // Actualizo stock de inventario
- if ($stock >= $cantidad) {
- $nuevo_stock = $stock - $cantidad;
- $inventario->stock = $nuevo_stock;
- $inventario->save();
- if($inventario->trans_status() === FALSE) {
- $inventario->trans_rollback();
- }
- }
- $carrito->trans_begin();
- if($carrito->exists()) {
- // Si ya existe el carrito para este inventario y cliente actualizo la cantidad
- $cantidad = $cantidad + $carrito->cantidad;
- $carrito->cantidad = $cantidad;
- $carrito->save();
- } else {
- // Si el carrito es nuevo lo guardo
- $carrito->guardar($id_cliente, $inventario_id, $cantidad);
- }
- if($carrito->trans_status() === TRUE) {
- $inventario->trans_commit();
- $carrito->trans_commit();
- } else {
- "No tiene elementos suficientes en el inventario";
- }
- }
- public function quitar_producto_carrito() {
- // Recibe parámetros por POST
- $producto = $this->input->post('producto');
- $sucursal = $this->input->post('sucursal');
- // Obtiene el inventario asociado al producto y la sucursal
- $inventario = new Inventario();
- $inventario->where_related('sucursal', 'nombre_sucursal', $sucursal);
- $inventario->where_related('producto', 'nombre_producto', $producto)->get();
- // Elimina el carrito del cliente asociado al inventario
- $carrito = new Carrito();
- $carrito->where_related($inventario);
- $id_cliente = $this->_obtener_id_cliente();
- $carrito->where('cliente_id', $id_cliente)->get();
- $inventario->stock = $inventario->stock + $carrito->cantidad;
- $inventario->save();
- $carrito->delete();
- }
- public function consultar_pedidos(){
- $rol = $this->session->userdata('rol');
- $roles = array('vendedor', 'administrador');
- if(in_array($rol, $roles)) {
- $this->load->view('includes/Header');
- $this->load->view('Botones');
- $this->load->view('sucursales');
- $this->load->view('Consultar_Pedidos');
- $this->load->view('includes/Footer');
- } else {
- redirect('login');
- };
- }
- public function ver_pedido($id_pedido){
- $data = array(
- 'id_pedido' => $id_pedido
- );
- $rol = $this->session->userdata('rol');
- $roles = array('vendedor', 'cliente', 'administrador');
- if(in_array($rol, $roles)){
- $this->load->view('includes/Header');
- $this->load->view('Botones');
- $this->load->view('ver_pedido', $data);
- $this->load->view('includes/Footer');
- } else {
- redirect('login');
- }
- }
- public function asentar_venta($id_pedido) {
- $venta = new Venta();
- echo $venta->crear_venta($id_pedido);
- }
- public function mostrar_productos() {
- $productos = $this->input->post('productos');
- $sucursal = $this->input->post('sucursal');
- $producto = new Producto();
- $inventario = new Inventario();
- foreach ( $productos as $p ) {
- $producto->from_json($p);
- $inventario->where('stock >', 0);
- $inventario->where_related('sucursal', 'nombre_sucursal', $sucursal);
- $inventario->where_related($producto)->get();
- echo '
- <div class="col-md-4">
- <div class="thumbnail">
- <img src="' . base_url() . 'assets/imagenes/' . $producto->path_img . '" style="width:300px; height:300px" />
- <h3>' . $producto->nombre_producto . '</h3>
- <p>
- <label for="cant1">Cantidad:</label>
- <input class="form-control" type="number" name="cantidad" min="1" value="1" max="' . $inventario->stock . '">
- </p>
- <button class="btn btn-primary" id="agregar">
- <span class="glyphicon glyphicon-plus-sign"></span>
- Agregar
- </button>
- </div>
- </div>';
- }
- }
- public function cargar_pedido(){
- $id_pedido = $this->input->post('id_pedido');
- $linea= new Linea_pedido();
- $linea->where('pedido_id', $id_pedido)->get();
- foreach ( $linea as $row ) {
- $cantidad = $row->cantidad;
- $precio = $row->precio_producto;
- $subtotal = $cantidad * $precio;
- $nombre=$row->producto->get()->nombre_producto;
- echo '<tr>';
- echo '<td width="25%" class="text-center">' .$nombre.'</td>';
- echo '<td width="25%" class="text-center">' .$precio. '</td>';
- echo '<td width="25%" class="text-center">'.$cantidad.'</td>';
- echo '<td width="25%" class="text-right">' . $subtotal .'</td>';
- echo '</tr>';
- }
- }
- public function contar_productos_carrito() {
- $sucursal = $this->input->post('sucursal');
- $cliente_id = $this->_obtener_id_cliente();
- $carrito = new Carrito();
- $carrito->where_related('inventario/sucursal', 'nombre_sucursal', $sucursal);
- $carrito->where_related('cliente', 'id', $cliente_id);
- echo $carrito->count();
- }
- public function obtener_max_inventario() {
- $this->input->post('sucursal');
- $this->input->post('');
- $inventario = new Inventario();
- $inventario->
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement