
--
-- Estructura para la vista `v_caja_aper`
--
DROP VIEW IF EXISTS `v_caja_aper`;

CREATE VIEW `v_caja_aper`  AS  select `apc`.`id_apc` AS `id_apc`,`apc`.`id_usu` AS `id_usu`,`apc`.`id_caja` AS `id_caja`,`apc`.`id_turno` AS `id_turno`,`apc`.`fecha_aper` AS `fecha_aper`,`apc`.`monto_aper` AS `monto_aper`,`apc`.`fecha_cierre` AS `fecha_cierre`,`apc`.`monto_cierre` AS `monto_cierre`,`apc`.`monto_sistema` AS `monto_sistema`,`apc`.`stock_pollo` AS `stock_pollo`,`apc`.`estado` AS `estado`,concat(`tp`.`nombres`,' ',`tp`.`ape_paterno`,' ',`tp`.`ape_materno`) AS `desc_per`,`tc`.`descripcion` AS `desc_caja`,`tt`.`descripcion` AS `desc_turno` from (((`tm_aper_cierre` `apc` join `tm_usuario` `tp` on(`apc`.`id_usu` = `tp`.`id_usu`)) join `tm_caja` `tc` on(`apc`.`id_caja` = `tc`.`id_caja`)) join `tm_turno` `tt` on(`apc`.`id_turno` = `tt`.`id_turno`)) order by `apc`.`id_apc` desc ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_clientes`
--
DROP VIEW IF EXISTS `v_clientes`;

CREATE VIEW `v_clientes`  AS  select `tm_cliente`.`id_cliente` AS `id_cliente`,`tm_cliente`.`tipo_cliente` AS `tipo_cliente`,`tm_cliente`.`dni` AS `dni`,`tm_cliente`.`ruc` AS `ruc`,`tm_cliente`.`ce` AS `ce`,concat(ifnull(`tm_cliente`.`razon_social`,''),'',`tm_cliente`.`nombres`) AS `nombre`,`tm_cliente`.`telefono` AS `telefono`,`tm_cliente`.`fecha_nac` AS `fecha_nac`,`tm_cliente`.`direccion` AS `direccion`,`tm_cliente`.`referencia` AS `referencia`,`tm_cliente`.`estado` AS `estado` from `tm_cliente` order by `tm_cliente`.`id_cliente` desc ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_productos`
--
DROP VIEW IF EXISTS `v_productos`;

CREATE VIEW `v_productos`  AS  select `pp`.`id_pres` AS `id_pres`,`pp`.`id_prod` AS `id_prod`,`pp`.`impuesto_icbper`,`pp`.`impuesto_compra`,`pp`.`precio_compra`,`p`.`id_tipo` AS `id_tipo`,`p`.`id_catg` AS `id_catg`,`p`.`id_areap` AS `id_areap`,`cp`.`descripcion` AS `pro_cat`,`pp`.`cod_prod` AS `pro_cod`,`p`.`nombre` AS `pro_nom`,`pp`.`presentacion` AS `pro_pre`,ifnull(`pp`.`descripcion`,'') AS `pro_des`,`pp`.`precio` AS `pro_cos`,`pp`.`precio2` AS `pro_cos2`,`pp`.`ordenins` AS `ordenins`,`pp`.`precio_delivery` AS `pro_cos_del`,`pp`.`receta` AS `pro_rec`,`pp`.`stock_min` AS `pro_sto`,`pp`.`impuesto` AS `pro_imp`,`pp`.`margen` AS `pro_mar`,`pp`.`favorito` AS `favorito`,`pp`.`precios` AS `precios`,`pp`.`igv` AS `pro_igv`,`pp`.`impresoras` AS `impresoras`,`pp`.`activar_impresoras` AS `activar_impresoras`,`pp`.`imagen` AS `pro_img`,`cp`.`delivery` AS `del_a`,`p`.`delivery` AS `del_b`,`pp`.`delivery` AS `del_c`,`cp`.`estado` AS `est_a`,`p`.`estado` AS `est_b`,`pp`.`estado` AS `est_c`,`pp`.`crt_stock` AS `crt_stock` from ((`tm_producto_pres` `pp` join `tm_producto` `p` on(`pp`.`id_prod` = `p`.`id_prod`)) join `tm_producto_catg` `cp` on(`p`.`id_catg` = `cp`.`id_catg`)) where `pp`.`id_pres` <> 0 order by `pp`.`id_pres` desc ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_prod_favorito`
--
DROP VIEW IF EXISTS `v_prod_favorito`;

CREATE VIEW `v_prod_favorito`  AS  select `pp`.`id_pres` AS `id_pres`,`pp`.`id_prod` AS `id_prod`,`pp`.`impuesto_icbper`,`pp`.`impuesto_compra`,`pp`.`precio_compra`,`p`.`id_tipo` AS `id_tipo`,`p`.`id_catg` AS `id_catg`,`p`.`id_areap` AS `id_areap`,`cp`.`descripcion` AS `pro_cat`,`pp`.`cod_prod` AS `pro_cod`,`p`.`nombre` AS `pro_nom`,`pp`.`presentacion` AS `pro_pre`,ifnull(`pp`.`descripcion`,'') AS `pro_des`,`pp`.`precio` AS `pro_cos`,`pp`.`precio2` AS `pro_cos2`,`pp`.`ordenins` AS `ordenins`,`pp`.`precio_delivery` AS `pro_cos_del`,`pp`.`receta` AS `pro_rec`,`pp`.`stock_min` AS `pro_sto`,`pp`.`impuesto` AS `pro_imp`,`pp`.`margen` AS `pro_mar`,`pp`.`favorito` AS `favorito`,`pp`.`precios` AS `precios`,`pp`.`igv` AS `pro_igv`,`pp`.`impresoras` AS `impresoras`,`pp`.`activar_impresoras` AS `activar_impresoras`,`pp`.`imagen` AS `pro_img`,`cp`.`delivery` AS `del_a`,`p`.`delivery` AS `del_b`,`pp`.`delivery` AS `del_c`,`cp`.`estado` AS `est_a`,`p`.`estado` AS `est_b`,`pp`.`estado` AS `est_c`,`pp`.`crt_stock` AS `crt_stock` from ((`tm_producto_pres` `pp` join `tm_producto` `p` on(`pp`.`id_prod` = `p`.`id_prod`)) join `tm_producto_catg` `cp` on(`p`.`id_catg` = `cp`.`id_catg`)) where `pp`.`id_pres` <> 0  and `pp`.`favorito` = 1 order by `pp`.`id_pres` desc ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_usuarios`
--
DROP VIEW IF EXISTS `v_usuarios`;

CREATE VIEW `v_usuarios` AS 
SELECT 
    `u`.*, 
    `r`.`descripcion` AS `desc_r`, 
    `p`.`nombre` AS `desc_ap`, 
    -- `imp`.`id_imp` AS `imp_id`,  -- Alias para evitar duplicación
    `imp`.`nombre` AS `nombre_imp`
FROM 
    (`tm_usuario` `u`
    JOIN `tm_rol` `r` ON `u`.`id_rol` = `r`.`id_rol`)
    LEFT JOIN `tm_area_prod` `p` ON `u`.`id_areap` = `p`.`id_areap`
    LEFT JOIN `tm_impresora` `imp` ON `u`.`id_imp` = `imp`.`id_imp`
WHERE 
    `u`.`id_usu` <> 0 
ORDER BY 
    `u`.`id_usu` DESC;

-- CREATE VIEW `v_usuarios`  AS  select `u`.`id_usu` AS `id_usu`,`u`.`id_rol` AS `id_rol`,`u`.`id_areap` AS `id_areap`,`u`.`dni` AS `dni`,`u`.`editarprecio` AS `editarprecio`,`u`.`ventarapida` AS `ventarapida`,`u`.`ape_paterno` AS `ape_paterno`,`u`.`ape_materno` AS `ape_materno`,`u`.`nombres` AS `nombres`,`u`.`email` AS `email`,`u`.`precuenta` AS `precuenta`,`u`.`usuario` AS `usuario`,`u`.`contrasena` AS `contrasena`,`u`.`estado` AS `estado`,`u`.`imagen` AS `imagen`,`r`.`descripcion` AS `desc_r`,`p`.`nombre` AS `desc_ap`,`imp`.`id_imp` AS `id_imp`,`imp`.`nombre` AS `nombre_imp` from (((`tm_usuario` `u` join `tm_rol` `r` on(`u`.`id_rol` = `r`.`id_rol`)) left join `tm_area_prod` `p` on(`u`.`id_areap` = `p`.`id_areap`)) left join `tm_impresora` `imp` on(`u`.`id_imp` = `imp`.`id_imp`)) where `u`.`id_usu` <> 0 order by `u`.`id_usu` desc ;


-- --------------------------------------------------------
--
-- Estructura para la vista `v_cocina_de`
--
DROP VIEW IF EXISTS `v_cocina_de`;

CREATE VIEW `v_cocina_de`  AS  select `dp`.`id_pedido` AS `id_pedido`,`vp`.`id_areap` AS `id_areap`,`vp`.`id_tipo` AS `id_tipo`,`dp`.`id_pres` AS `id_pres`,if(`dp`.`cantidad` < `dp`.`cant`,`dp`.`cant`,`dp`.`cantidad`) AS `cantidad`,`dp`.`comentario` AS `comentario`,`dp`.`fecha_pedido` AS `fecha_pedido`,`dp`.`fecha_envio` AS `fecha_envio`,`dp`.`estado` AS `estado`,`pd`.`nro_pedido` AS `nro_pedido`,`pd`.`num_pedido` AS `num_pedido`,`tp`.`id_usu` AS `id_usu`,`vp`.`pro_nom` AS `nombre_prod`,`vp`.`pro_pre` AS `pres_prod`,`vu`.`ape_paterno` AS `ape_paterno`,`vu`.`ape_materno` AS `ape_materno`,`vu`.`nombres` AS `nombres`,`tp`.`estado` AS `estado_pedido` from ((((`tm_detalle_pedido` `dp` join `tm_pedido_delivery` `pd` on(`dp`.`id_pedido` = `pd`.`id_pedido`)) join `tm_pedido` `tp` on(`dp`.`id_pedido` = `tp`.`id_pedido`)) join `v_productos` `vp` on(`dp`.`id_pres` = `vp`.`id_pres`)) join `v_usuarios` `vu` on(`tp`.`id_usu` = `vu`.`id_usu`)) ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_mesas`
--
DROP VIEW IF EXISTS `v_mesas`;

CREATE VIEW `v_mesas`  AS  select `m`.`id_mesa` AS `id_mesa`,`cm`.`configuracion_json` AS `configuracion_json`,`cm`.`disenio` AS `disenio`,`m`.`pos_x` AS `pos_x`,`m`.`pos_y` AS `pos_y`,`m`.`width` AS `width`,`m`.`height` AS `height`,`m`.`id_salon` AS `id_salon`,`m`.`forma` AS `forma`,`m`.`orden` AS `orden`,`m`.`nro_mesa` AS `nro_mesa`,`m`.`estado` AS `estado`,`cm`.`descripcion` AS `desc_salon` from (`tm_mesa` `m` join `tm_salon` `cm` on(`m`.`id_salon` = `cm`.`id_salon`)) where `m`.`id_mesa` <> 0 and `cm`.`estado` <> 'i' order by `m`.`id_mesa` ;

-- --------------------------------------------------------
--
-- Estructura para la vista `v_cocina_me`
--
DROP VIEW IF EXISTS `v_cocina_me`;

CREATE VIEW `v_cocina_me`  AS  select `dp`.`id_pedido` AS `id_pedido`,`vp`.`id_areap` AS `id_areap`,`vp`.`id_tipo` AS `id_tipo`,`dp`.`id_pres` AS `id_pres`,`dp`.`cantidad` AS `cantidad`,`dp`.`comentario` AS `comentario`,`dp`.`fecha_pedido` AS `fecha_pedido`,`dp`.`fecha_envio` AS `fecha_envio`,`dp`.`estado` AS `estado`,`pm`.`num_pedido` AS `num_pedido`,`pm`.`id_mesa` AS `id_mesa`,`pm`.`id_mozo` AS `id_mozo`,`vp`.`pro_nom` AS `nombre_prod`,`vp`.`pro_pre` AS `pres_prod`,`vm`.`nro_mesa` AS `nro_mesa`,`vm`.`desc_salon` AS `desc_salon`,`vu`.`ape_paterno` AS `ape_paterno`,`vu`.`ape_materno` AS `ape_materno`,`vu`.`nombres` AS `nombres`,`vu`.`id_usu` AS `id_usu`,`tp`.`estado` AS `estado_pedido` from (((((`tm_detalle_pedido` `dp` join `tm_pedido_mesa` `pm` on(`dp`.`id_pedido` = `pm`.`id_pedido`)) join `tm_pedido` `tp` on(`dp`.`id_pedido` = `tp`.`id_pedido`)) join `v_productos` `vp` on(`dp`.`id_pres` = `vp`.`id_pres`)) join `v_mesas` `vm` on(`pm`.`id_mesa` = `vm`.`id_mesa`)) join `v_usuarios` `vu` on(`pm`.`id_mozo` = `vu`.`id_usu`)) ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_cocina_mo`
--
DROP VIEW IF EXISTS `v_cocina_mo`;

CREATE VIEW `v_cocina_mo`  AS  select `dp`.`id_pedido` AS `id_pedido`,`vp`.`id_areap` AS `id_areap`,`vp`.`id_tipo` AS `id_tipo`,`dp`.`id_pres` AS `id_pres`,if(`dp`.`cantidad` < `dp`.`cant`,`dp`.`cant`,`dp`.`cantidad`) AS `cantidad`,`dp`.`comentario` AS `comentario`,`dp`.`fecha_pedido` AS `fecha_pedido`,`dp`.`fecha_envio` AS `fecha_envio`,`dp`.`estado` AS `estado`,`pm`.`num_pedido` AS `num_pedido`,`pm`.`nro_pedido` AS `nro_pedido`,`tp`.`id_usu` AS `id_usu`,`vp`.`pro_nom` AS `nombre_prod`,`vp`.`pro_pre` AS `pres_prod`,`vu`.`ape_paterno` AS `ape_paterno`,`vu`.`ape_materno` AS `ape_materno`,`vu`.`nombres` AS `nombres`,`tp`.`estado` AS `estado_pedido` from ((((`tm_detalle_pedido` `dp` join `tm_pedido_llevar` `pm` on(`dp`.`id_pedido` = `pm`.`id_pedido`)) join `tm_pedido` `tp` on(`dp`.`id_pedido` = `tp`.`id_pedido`)) join `v_productos` `vp` on(`dp`.`id_pres` = `vp`.`id_pres`)) join `v_usuarios` `vu` on(`tp`.`id_usu` = `vu`.`id_usu`)) ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_compras`
--
DROP VIEW IF EXISTS `v_compras`;

CREATE VIEW `v_compras`  AS  select `c`.`id_compra` AS `id_compra`,`c`.`id_prov` AS `id_prov`,`c`.`id_tipo_compra` AS `id_tipo_compra`,`c`.`id_tipo_doc` AS `id_tipo_doc`,`c`.`fecha_c` AS `fecha_c`,`c`.`fecha_reg` AS `fecha_r`,`c`.`hora_c` AS `hora_c`,`c`.`serie_doc` AS `serie_doc`,`c`.`num_doc` AS `num_doc`,`c`.`igv` AS `igv`,`c`.`total` AS `total`,`c`.`estado` AS `estado`,`tc`.`descripcion` AS `desc_tc`,`td`.`descripcion` AS `desc_td`,`tp`.`razon_social` AS `desc_prov` from (((`tm_compra` `c` join `tm_tipo_compra` `tc` on(`c`.`id_tipo_compra` = `tc`.`id_tipo_compra`)) join `tm_tipo_doc` `td` on(`c`.`id_tipo_doc` = `td`.`id_tipo_doc`)) join `tm_proveedor` `tp` on(`c`.`id_prov` = `tp`.`id_prov`)) where `c`.`id_compra` <> 0 order by `c`.`id_compra` desc ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_det_delivery`
--
DROP VIEW IF EXISTS `v_det_delivery`;

CREATE VIEW `v_det_delivery`  AS  select `dp`.`id_pedido` AS `id_pedido`,`dp`.`id_pres` AS `id_pres`,if(`dp`.`cantidad` < `dp`.`cant`,`dp`.`cant`,`dp`.`cantidad`) AS `cantidad`,`dp`.`precio` AS `precio`,`dp`.`estado` AS `estado`,`vp`.`pro_nom` AS `nombre_prod`,`vp`.`pro_pre` AS `pres_prod` from (((`tm_detalle_pedido` `dp` join `tm_pedido_delivery` `pd` on(`dp`.`id_pedido` = `pd`.`id_pedido`)) join `tm_pedido` `tp` on(`dp`.`id_pedido` = `tp`.`id_pedido`)) join `v_productos` `vp` on(`dp`.`id_pres` = `vp`.`id_pres`)) where `dp`.`estado` <> 'z' ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_det_llevar`
--
DROP VIEW IF EXISTS `v_det_llevar`;

CREATE VIEW `v_det_llevar`  AS  select `dp`.`id_pedido` AS `id_pedido`,`dp`.`id_pres` AS `id_pres`,if(`dp`.`cantidad` < `dp`.`cant`,`dp`.`cant`,`dp`.`cantidad`) AS `cantidad`,`dp`.`precio` AS `precio`,`dp`.`estado` AS `estado`,`vp`.`pro_nom` AS `nombre_prod`,`vp`.`pro_pre` AS `pres_prod` from (((`tm_detalle_pedido` `dp` join `tm_pedido_llevar` `pm` on(`dp`.`id_pedido` = `pm`.`id_pedido`)) join `tm_pedido` `tp` on(`dp`.`id_pedido` = `tp`.`id_pedido`)) join `v_productos` `vp` on(`dp`.`id_pres` = `vp`.`id_pres`)) where `dp`.`estado` <> 'z' ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_gastosadm`
--
DROP VIEW IF EXISTS `v_gastosadm`;

CREATE VIEW `v_gastosadm`  AS  select `ga`.`id_ga` AS `id_ga`,`ga`.`id_tipo_gasto` AS `id_tg`,`ga`.`id_per` AS `id_per`,`ga`.`id_usu` AS `id_usu`,`ga`.`id_apc` AS `id_apc`,`ga`.`importe` AS `importe`,`ga`.`responsable` AS `responsable`,`ga`.`motivo` AS `motivo`,`ga`.`fecha_registro` AS `fecha_re`,`ga`.`estado` AS `estado`,`tg`.`descripcion` AS `des_tg`,concat(`tu`.`nombres`,' ',`tu`.`ape_paterno`,' ',`tu`.`ape_materno`) AS `desc_usu`,if(`ga`.`id_per` = '0','',concat(`tus`.`nombres`,' ',`tus`.`ape_paterno`,' ',`tus`.`ape_materno`)) AS `desc_per` from (((`tm_gastos_adm` `ga` join `tm_tipo_gasto` `tg` on(`ga`.`id_tipo_gasto` = `tg`.`id_tipo_gasto`)) join `tm_usuario` `tu` on(`ga`.`id_usu` = `tu`.`id_usu`)) left join `tm_usuario` `tus` on(`ga`.`id_per` = `tus`.`id_usu`)) where `ga`.`id_ga` <> 0 order by `ga`.`id_ga` desc ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_insumos`
--
DROP VIEW IF EXISTS `v_insumos`;

CREATE VIEW `v_insumos`  AS  select `i`.`id_ins` AS `id_ins`,`i`.`id_catg` AS `id_catg`,`i`.`id_med` AS `id_med`,`i`.`cos_uni`,`m`.`grupo` AS `id_gru`,`i`.`cod_ins` AS `ins_cod`,`i`.`nomb_ins` AS `ins_nom`,`i`.`stock_min` AS `ins_sto`,`i`.`cos_uni` AS `ins_cos`,`i`.`estado` AS `ins_est`,`ic`.`descripcion` AS `ins_cat`,`m`.`descripcion` AS `ins_med` from ((`tm_insumo` `i` join `tm_insumo_catg` `ic` on(`i`.`id_catg` = `ic`.`id_catg`)) join `tm_tipo_medida` `m` on(`i`.`id_med` = `m`.`id_med`)) ;

--
-- Estructura para la vista `v_toppings`
--
DROP VIEW IF EXISTS `v_toppings`;

CREATE VIEW `v_toppings` AS
SELECT 
  t.`id_top` AS `id_top`,
  t.`id_cattop` AS `id_cattop`,
  t.`id_med` AS `id_med`,
  t.`cos_uni`,
  m.`grupo` AS `id_gru`,
  t.`cod_top` AS `top_cod`,
  t.`nomb_top` AS `top_nom`,
  t.`stock_min` AS `top_sto`,
  t.`cos_uni` AS `top_cos`,
  t.`estado` AS `top_est`,
  tc.`descripcion` AS `top_cat`,
  m.`descripcion` AS `top_med`
FROM 
  (`tm_topping` t
  JOIN `tm_topping_catg` tc ON (t.`id_cattop` = tc.`id_cattop`)
  JOIN `tm_tipo_medida` m ON (t.`id_med` = m.`id_med`));


-- --------------------------------------------------------

--
-- Estructura para la vista `v_insprod`
--
DROP VIEW IF EXISTS `v_insprod`;

CREATE VIEW `v_insprod`  AS  select 1 AS `id_tipo_ins`,`i`.`id_ins` AS `id_ins`,`i`.`id_med` AS `id_med`,`i`.`id_gru` AS `id_gru`,`i`.`cos_uni` as `precio_compra`,`i`.`ins_cod` AS `ins_cod`,`i`.`ins_nom` AS `ins_nom`,`i`.`ins_cat` AS `ins_cat`,`i`.`ins_med` AS `ins_med`,1 AS `ins_rec`,`i`.`ins_cos` AS `ins_cos`,`i`.`ins_sto` AS `ins_sto`,`i`.`ins_est` AS `est_a`,'a' AS `est_b`,'a' AS `est_c`,'' AS `crt_stock` from `v_insumos` `i` union select 2 AS `id_tipo_ins`,`p`.`id_pres` AS `id_pres`,'1' AS `1`,'1' AS `1`,`p`.`precio_compra`,`p`.`pro_cod` AS `pro_cod`,concat(`p`.`pro_nom`,' ',`p`.`pro_pre`) AS `pro_nom`,`p`.`pro_cat` AS `pro_cat`,'UNIDAD' AS `UNIDAD`,`p`.`pro_rec` AS `pro_rec`,`p`.`pro_cos` AS `pro_cos`,`p`.`pro_sto` AS `pro_sto`,`p`.`est_a` AS `est_a`,`p`.`est_b` AS `est_b`,`p`.`est_c` AS `est_c`,`p`.`crt_stock` AS `crt_stock` from `v_productos` `p` where `p`.`id_tipo` = 2 and `p`.`id_catg` <> 1 union select if(`p`.`crt_stock` = 1,2,3) AS `id_tipo_ins`,`p`.`id_pres` AS `id_pres`,'1' AS `1`,'1' AS `1`,`p`.`precio_compra`,`p`.`pro_cod` AS `pro_cod`,concat(`p`.`pro_nom`,' ',`p`.`pro_pre`) AS `pro_nom`,`p`.`pro_cat` AS `pro_cat`,'UNIDAD' AS `UNIDAD`,`p`.`pro_rec` AS `pro_rec`,`p`.`pro_cos` AS `pro_cos`,`p`.`pro_sto` AS `pro_sto`,`p`.`est_a` AS `est_a`,`p`.`est_b` AS `est_b`,`p`.`est_c` AS `est_c`,`p`.`crt_stock` AS `crt_stock` from `v_productos` `p` where `p`.`id_tipo` = 1 and `p`.`id_catg` <> 1 ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_inventario_ent`
--
DROP VIEW IF EXISTS `v_inventario_ent`;

CREATE VIEW `v_inventario_ent`  AS  select `tm_inventario`.`id_tipo_ins` AS `id_tipo_ins`,`tm_inventario`.`id_ins` AS `id_ins`,`tm_inventario`.`fecha_r` AS `fecha_r`,if(`tm_inventario`.`id_tipo_ope` = 1 or `tm_inventario`.`id_tipo_ope` = 3,sum(`tm_inventario`.`cant`),0) AS `total` from `tm_inventario` where `tm_inventario`.`id_tipo_ope` <> 2 and `tm_inventario`.`id_tipo_ope` <> 4 and `tm_inventario`.`estado` <> 'i' group by `tm_inventario`.`id_tipo_ins`,`tm_inventario`.`id_ins` ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_inventario_sal`
--
DROP VIEW IF EXISTS `v_inventario_sal`;

CREATE VIEW `v_inventario_sal`  AS  select `tm_inventario`.`id_tipo_ins` AS `id_tipo_ins`,`tm_inventario`.`id_ins` AS `id_ins`,`tm_inventario`.`fecha_r` AS `fecha_r`,if(`tm_inventario`.`id_tipo_ope` = 2 or `tm_inventario`.`id_tipo_ope` = 4,sum(`tm_inventario`.`cant`),0) AS `total` from `tm_inventario` where `tm_inventario`.`id_tipo_ope` <> 1 and `tm_inventario`.`id_tipo_ope` <> 3 and `tm_inventario`.`estado` <> 'i' group by `tm_inventario`.`id_tipo_ins`,`tm_inventario`.`id_ins` ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_inventario`
--
DROP VIEW IF EXISTS `v_inventario`;

CREATE VIEW `v_inventario`  AS  select `e`.`id_tipo_ins` AS `id_tipo_ins`,`e`.`id_ins` AS `id_ins`,`e`.`fecha_r` AS `fecha_r`,ifnull(`e`.`total`,0) AS `ent`,'0' AS `sal` from `v_inventario_ent` `e` union select `s`.`id_tipo_ins` AS `id_tipo_ins`,`s`.`id_ins` AS `id_ins`,`s`.`fecha_r` AS `fecha_r`,'0' AS `ent`,ifnull(`s`.`total`,0) AS `sal` from `v_inventario_sal` `s` ;

-- --------------------------------------------------------

--
-- Estructura para la vista `v_pedido_mesa`
--

DROP VIEW IF EXISTS `v_pedido_mesa`;

CREATE VIEW `v_pedido_mesa`  AS  SELECT 
    p.id_pedido AS id_pedido,
    p.id_tipo_pedido AS id_tipo_pedido,
    p.id_usu AS id_usu,
    pm.id_mesa AS id_mesa,
    p.fecha_pedido AS fecha_pedido,
    p.estado AS estado_pedido,
    pm.num_pedido AS num_pedido,
    pm.nomb_cliente AS nombre_cliente,
    pm.nro_personas AS nro_personas,
    vm.nro_mesa AS nro_mesa,
    vm.desc_salon AS desc_salon,
    vm.estado AS estado_mesa,
    vm.id_salon AS id_salon,
    vm.orden AS orden,
    u.id_usu AS id_mozo,
    CONCAT(u.nombres, ' ', u.ape_paterno) AS nombre_mozo
FROM tm_pedido p
JOIN tm_pedido_mesa pm ON p.id_pedido = pm.id_pedido
JOIN v_mesas vm ON pm.id_mesa = vm.id_mesa
JOIN tm_usuario u ON pm.id_mozo = u.id_usu
-- Subconsulta para obtener el último id_pedido por id_mesa
WHERE p.id_pedido IN (
    SELECT MAX(p1.id_pedido)
    FROM tm_pedido p1
    JOIN tm_pedido_mesa pm1 ON p1.id_pedido = pm1.id_pedido
    WHERE p1.estado IN ('a', 'c')
    GROUP BY pm1.id_mesa
)
ORDER BY p.id_pedido DESC;


-- CREATE VIEW `v_pedido_mesa`  AS  select `p`.`id_pedido` AS `id_pedido`,`p`.`id_tipo_pedido` AS `id_tipo_pedido`,`p`.`id_usu` AS `id_usu`,`pm`.`id_mesa` AS `id_mesa`,`p`.`fecha_pedido` AS `fecha_pedido`,`p`.`estado` AS `estado_pedido`,`pm`.`num_pedido` AS `num_pedido`,`pm`.`nomb_cliente` AS `nombre_cliente`,`pm`.`nro_personas` AS `nro_personas`,`vm`.`nro_mesa` AS `nro_mesa`,`vm`.`desc_salon` AS `desc_salon`,`vm`.`estado` AS `estado_mesa`,`vm`.`id_salon` AS `id_salon`,`vm`.`orden` AS `orden`,`u`.`id_usu` AS `id_mozo`,concat(`u`.`nombres`,' ',`u`.`ape_paterno`) AS `nombre_mozo` from (((`tm_pedido` `p` join `tm_pedido_mesa` `pm` on(`p`.`id_pedido` = `pm`.`id_pedido`)) join `v_mesas` `vm` on(`pm`.`id_mesa` = `vm`.`id_mesa`)) join `tm_usuario` `u` on(`pm`.`id_mozo` = `u`.`id_usu`)) where `p`.`id_pedido` <> 0 and (`p`.`estado` = 'a' OR `p`.`estado` = 'c') order by `p`.`id_pedido` desc ;

-- DROP VIEW IF EXISTS `v_pedido_mesa`;

-- CREATE VIEW `v_pedido_mesa` AS
-- WITH LatestPedidos AS (
--     SELECT 
--         pm.id_mesa,
--         MAX(p.fecha_pedido) AS max_fecha_pedido
--     FROM
--         tm_pedido p
--     JOIN
--         tm_pedido_mesa pm ON p.id_pedido = pm.id_pedido
--     WHERE
--         p.id_pedido <> 0 AND p.estado = 'a'
--     GROUP BY pm.id_mesa
-- )
-- SELECT 
--     p.id_pedido AS id_pedido,
--     p.id_tipo_pedido AS id_tipo_pedido,
--     p.id_usu AS id_usu,
--     pm.id_mesa AS id_mesa,
--     p.fecha_pedido AS fecha_pedido,
--     p.estado AS estado_pedido,
--     pm.nomb_cliente AS nombre_cliente,
--     pm.num_pedido,
--     pm.nro_personas AS nro_personas,
--     vm.nro_mesa AS nro_mesa,
--     vm.desc_salon AS desc_salon,
--     vm.estado AS estado_mesa,
--     vm.orden AS orden,
--     vm.id_salon AS id_salon,
--     CONCAT(u.nombres, ' ', u.ape_paterno) AS nombre_mozo
-- FROM
--     LatestPedidos lp
-- JOIN
--     tm_pedido p ON lp.max_fecha_pedido = p.fecha_pedido
-- JOIN
--     tm_pedido_mesa pm ON p.id_pedido = pm.id_pedido AND lp.id_mesa = pm.id_mesa
-- JOIN
--     v_mesas vm ON pm.id_mesa = vm.id_mesa
-- JOIN
--     tm_usuario u ON pm.id_mozo = u.id_usu
-- WHERE
--     p.id_pedido <> 0 AND p.estado = 'a'
-- ORDER BY
--     p.id_pedido DESC;


-- --------------------------------------------------------

-- 
-- Estructura para la vista `v_listar_mesas`
--

DROP VIEW IF EXISTS `v_listar_mesas`;

CREATE VIEW `v_listar_mesas` AS  
SELECT 
    vm.id_mesa AS id_mesa,
    vm.forma AS forma,
    vm.orden AS orden,
    vm.id_salon AS id_salon,
    vm.nro_mesa AS nro_mesa,
    vm.estado AS estado,
    vm.desc_salon AS desc_salon,
    vo.id_pedido AS id_pedido,
    vm.configuracion_json AS configuracion_json,
    vm.disenio AS disenio,
    vm.pos_x AS pos_x,
    vm.pos_y AS pos_y,
    vm.width AS width,
    vm.height AS height,
    vo.nombre_cliente AS nombre_cliente,
    vo.fecha_pedido AS fecha_pedido,
    vo.nombre_mozo AS nombre_mozo,
    vo.id_mozo AS id_mozo,
    vo.nro_personas AS nro_personas,
    
    -- ✅ Conteo de mesas disponibles, ocupadas y en proceso
    (SELECT COUNT(*) FROM v_mesas WHERE estado = 'a') AS disponibles,
    (SELECT COUNT(*) FROM v_mesas WHERE estado = 'p') AS enproceso,
    (SELECT COUNT(*) FROM v_mesas WHERE estado = 'i') AS ocupados

FROM v_mesas vm
LEFT JOIN v_pedido_mesa vo ON vm.id_mesa = vo.id_mesa
ORDER BY vm.nro_mesa;

-- DROP VIEW IF EXISTS `v_listar_mesas`;

-- CREATE VIEW `v_listar_mesas`  AS  select `vm`.`id_mesa` AS `id_mesa`,`vm`.`forma` AS `forma`,`vm`.`orden` AS `orden`,`vm`.`id_salon` AS `id_salon`,`vm`.`nro_mesa` AS `nro_mesa`,`vm`.`estado` AS `estado`,`vm`.`desc_salon` AS `desc_salon`,`vo`.`id_pedido` AS `id_pedido`,`vo`.`nombre_cliente` AS `nombre_cliente`,`vo`.`nombre_mozo` AS `nombre_mozo`,`vo`.`fecha_pedido` AS `fecha_pedido`,`vo`.`nro_personas` AS `nro_personas` from (`v_mesas` `vm` left join `v_pedido_mesa` `vo` on(`vm`.`id_mesa` = `vo`.`id_mesa`)) order by `vm`.`orden` asc ;

-- --------------------------------------------------------


--
-- Estructura para la vista `v_pedidos_agrupados`
--
DROP VIEW IF EXISTS `v_pedidos_agrupados`;

CREATE VIEW `v_pedidos_agrupados`  AS  select 1 AS `tipo_atencion`,`v_cocina_me`.`id_pedido` AS `id_pedido`,`v_cocina_me`.`id_areap` AS `id_areap`,`v_cocina_me`.`id_tipo` AS `id_tipo`,`v_cocina_me`.`id_pres` AS `id_pres`,sum(`v_cocina_me`.`cantidad`) AS `cantidad`,`v_cocina_me`.`comentario` AS `comentario`,`v_cocina_me`.`fecha_pedido` AS `fecha_pedido`,`v_cocina_me`.`fecha_envio` AS `fecha_envio`,`v_cocina_me`.`estado` AS `estado`,`v_cocina_me`.`nombre_prod` AS `nombre_prod`,`v_cocina_me`.`pres_prod` AS `pres_prod`,`v_cocina_me`.`nro_mesa` AS `nro_mesa`,`v_cocina_me`.`num_pedido` AS `num_pedido`,`v_cocina_me`.`desc_salon` AS `desc_salon`,`v_cocina_me`.`ape_paterno` AS `ape_paterno`,`v_cocina_me`.`ape_materno` AS `ape_materno`,`v_cocina_me`.`nombres` AS `nombres`,`v_cocina_me`.`id_mozo` AS `id_usu`,`v_cocina_me`.`estado_pedido` AS `estado_pedido` from `v_cocina_me` group by `v_cocina_me`.`id_pedido`,`v_cocina_me`.`id_pres`,`v_cocina_me`.`fecha_pedido`,`v_cocina_me`.`comentario` union select 2 AS `tipo_atencion`,`v_cocina_mo`.`id_pedido` AS `id_pedido`,`v_cocina_mo`.`id_areap` AS `id_areap`,`v_cocina_mo`.`id_tipo` AS `id_tipo`,`v_cocina_mo`.`id_pres` AS `id_pres`,sum(`v_cocina_mo`.`cantidad`) AS `cantidad`,`v_cocina_mo`.`comentario` AS `comentario`,`v_cocina_mo`.`fecha_pedido` AS `fecha_pedido`,`v_cocina_mo`.`fecha_envio` AS `fecha_envio`,`v_cocina_mo`.`estado` AS `estado`,`v_cocina_mo`.`nombre_prod` AS `nombre_prod`,`v_cocina_mo`.`pres_prod` AS `pres_prod`,`v_cocina_mo`.`nro_pedido` AS `nro_pedido`,`v_cocina_mo`.`num_pedido` AS `num_pedido`,'MOSTRADOR' AS `MOSTRADOR`,`v_cocina_mo`.`ape_paterno` AS `ape_paterno`,`v_cocina_mo`.`ape_materno` AS `ape_materno`,`v_cocina_mo`.`nombres` AS `nombres`,`v_cocina_mo`.`id_usu` AS `id_usu`,`v_cocina_mo`.`estado_pedido` AS `estado_pedido` from `v_cocina_mo` group by `v_cocina_mo`.`id_pedido`,`v_cocina_mo`.`id_pres`,`v_cocina_mo`.`fecha_pedido`,`v_cocina_mo`.`comentario` union select 3 AS `tipo_atencion`,`v_cocina_de`.`id_pedido` AS `id_pedido`,`v_cocina_de`.`id_areap` AS `id_areap`,`v_cocina_de`.`id_tipo` AS `id_tipo`,`v_cocina_de`.`id_pres` AS `id_pres`,sum(`v_cocina_de`.`cantidad`) AS `cantidad`,`v_cocina_de`.`comentario` AS `comentario`,`v_cocina_de`.`fecha_pedido` AS `fecha_pedido`,`v_cocina_de`.`fecha_envio` AS `fecha_envio`,`v_cocina_de`.`estado` AS `estado`,`v_cocina_de`.`nombre_prod` AS `nombre_prod`,`v_cocina_de`.`pres_prod` AS `pres_prod`,`v_cocina_de`.`nro_pedido` AS `nro_pedido`,`v_cocina_de`.`num_pedido` AS `num_pedido`,'DELIVERY' AS `DELIVERY`,`v_cocina_de`.`ape_paterno` AS `ape_paterno`,`v_cocina_de`.`ape_materno` AS `ape_materno`,`v_cocina_de`.`nombres` AS `nombres`,`v_cocina_de`.`id_usu` AS `id_usu`,`v_cocina_de`.`estado_pedido` AS `estado_pedido` from `v_cocina_de` group by `v_cocina_de`.`id_pedido`,`v_cocina_de`.`id_pres`,`v_cocina_de`.`fecha_pedido`,`v_cocina_de`.`comentario` ;

-- --------------------------------------------------------

--

--
-- Estructura para la vista `v_repartidores`
--
DROP VIEW IF EXISTS `v_repartidores`;

CREATE VIEW `v_repartidores`  AS  select `tm_usuario`.`id_usu` AS `id_repartidor`,concat(`tm_usuario`.`nombres`,' ',`tm_usuario`.`ape_paterno`,' ',`tm_usuario`.`ape_materno`) AS `desc_repartidor` from `tm_usuario` where `tm_usuario`.`id_rol` = 6 union select `tm_repartidor`.`id_repartidor` AS `id_repartidor`,`tm_repartidor`.`descripcion` AS `desc_repartidor` from `tm_repartidor` ;

-- --------------------------------------------------------

-- Estructura para la vista `v_pedido_delivery`
--
DROP VIEW IF EXISTS `v_pedido_delivery`;

CREATE VIEW `v_pedido_delivery`  AS  select `p`.`id_pedido` AS `id_pedido`,`p`.`id_tipo_pedido` AS `id_tipo_pedido`,`p`.`id_usu` AS `id_usu`,`tp`.`descripcion` AS `desc_tp`,`pd`.`id_repartidor` AS `id_repartidor`,`p`.`fecha_pedido` AS `fecha_pedido`,`p`.`estado` AS `estado_pedido`,`pd`.`tipo_entrega` AS `tipo_entrega`,`pd`.`pedido_programado` AS `pedido_programado`,`pd`.`num_pedido`,`pd`.`hora_entrega` AS `hora_entrega`,`pd`.`amortizacion` AS `amortizacion`,`pd`.`tipo_pago` AS `tipo_pago`,`pd`.`tipo_pago` AS `id_tpag`,`pd`.`paga_con` AS `paga_con`,`pd`.`comision_delivery` AS `comision_delivery`,`pd`.`nro_pedido` AS `nro_pedido`,`pd`.`id_cliente` AS `id_cliente`,`c`.`tipo_cliente` AS `tipo_cliente`,`c`.`dni` AS `dni_cliente`,`c`.`ruc` AS `ruc_cliente`,`pd`.`nombre_cliente` AS `nombre_cliente`,`pd`.`telefono_cliente` AS `telefono_cliente`,`pd`.`direccion_cliente` AS `direccion_cliente`,`pd`.`referencia_cliente` AS `referencia_cliente`,`pd`.`email_cliente` AS `email_cliente`,`r`.`desc_repartidor` AS `desc_repartidor` from (((`tm_pedido` `p` join `tm_pedido_delivery` `pd` on(`p`.`id_pedido` = `pd`.`id_pedido`)) join `v_repartidores` `r` on(`pd`.`id_repartidor` = `r`.`id_repartidor`)) join `tm_tipo_pago` `tp` on(`pd`.`tipo_pago` = `tp`.`id_tipo_pago`) join `tm_cliente` `c` on(`pd`.`id_cliente` = `c`.`id_cliente`)) where `p`.`id_pedido` <> 0 order by `p`.`id_pedido` desc ;
 
-- --------------------------------------------------------

--
-- Estructura para la vista `v_pedido_llevar`
--
DROP VIEW IF EXISTS `v_pedido_llevar`;

CREATE VIEW `v_pedido_llevar`  AS  select `p`.`id_pedido` AS `id_pedido`,`p`.`id_tipo_pedido` AS `id_tipo_pedido`,`p`.`id_usu` AS `id_usu`,`p`.`fecha_pedido` AS `fecha_pedido`,`p`.`estado` AS `estado_pedido`,`pl`.`nro_pedido` AS `nro_pedido`,`pl`.`num_pedido`,`pl`.`nomb_cliente` AS `nombre_cliente` from (`tm_pedido` `p` join `tm_pedido_llevar` `pl` on(`p`.`id_pedido` = `pl`.`id_pedido`)) where `p`.`id_pedido` <> 0 order by `p`.`id_pedido` desc ;

-- --------------------------------------------------------



--
-- Estructura para la vista `v_stock`
--
DROP VIEW IF EXISTS `v_stock`;

CREATE VIEW `v_stock`  AS  (select `a`.`id_tipo_ins` AS `id_tipo_ins`,`a`.`id_ins` AS `id_ins`,sum(`a`.`ent`) AS `ent`,sum(`a`.`sal`) AS `sal`,`b`.`est_a` AS `est_a`,`b`.`est_b` AS `est_b`,if(`a`.`ent` - `a`.`sal` > `b`.`ins_sto`,1,0) AS `debajo_stock`,`a`.`fecha_r` AS `fecha_r` from (`v_inventario` `a` join `v_insprod` `b` on(`a`.`id_tipo_ins` = `b`.`id_tipo_ins` and `a`.`id_ins` = `b`.`id_ins`)) where `b`.`est_a` = 'a' and `b`.`est_b` = 'a' group by `a`.`id_tipo_ins`,`a`.`id_ins`) ;

-- --------------------------------------------------------

--
DROP VIEW IF EXISTS `v_stock_pedido`;

CREATE VIEW `v_stock_pedido`  AS 
(select 
	`c`.`id_ins`
	AS `id_ins`,

	ifnull((select sum(`b`.`ent`)
	from `v_inventario` `b` 
	where `b`.`id_ins` = `c`.`id_ins`  AND `b`.`id_tipo_ins` = `c`.`id_tipo_ins`
	group by `c`.`id_ins`),0) 
	AS `ent`,

	(select `z`.`id_tipo_ins` 
		from `v_inventario` `z` 
		where `z`.`id_ins` = `c`.`id_ins` AND `z`.`id_tipo_ins` = `c`.`id_tipo_ins`
		group by `c`.`id_ins`) 
	AS `id_tipo_ins`,

	ifnull((select sum(`a`.`sal`)
		from `v_inventario` `a` 
		where `a`.`id_ins` = `c`.`id_ins` AND `a`.`id_tipo_ins` = `c`.`id_tipo_ins`
		and `c`.`crt_stock` = '1' 
		group by `c`.`id_ins`),0) 
	AS `sal`,

	(select `e`.`stock_limit` 
		from `tm_producto_pres` `e` 
		where `e`.`id_pres` = `c`.`id_ins` 
		group by `c`.`id_ins`) 
	AS `limite`,

	(select `d`.`crt_stock` 
		from `tm_producto_pres` `d` 
		where `d`.`id_pres` = `c`.`id_ins` 
		group by `c`.`id_ins`) 
	AS `control`
    
from `v_insprod` `c` 
where `c`.`crt_stock` = 1 
group by `c`.`id_ins`, `c`.`id_tipo_ins`) ;

-- --------------------------------------------------------


--
-- Estructura para la vista `v_ventas_con`
--
DROP VIEW IF EXISTS `v_ventas_con`;

CREATE VIEW `v_ventas_con`  AS  select `v`.`id_venta` AS `id_ven`,`v`.`id_pedido` AS `id_ped`,`v`.`nvoriginal`,`v`.`nvfecha`,`v`.`id_tipo_pedido` AS `id_tped`,`v`.`id_cliente` AS `id_cli`,`v`.`id_tipo_doc` AS `id_tdoc`,`v`.`id_tipo_pago` AS `id_tpag`,`v`.`id_usu` AS `id_usu`,`v`.`id_apc` AS `id_apc`,`v`.`serie_doc` AS `ser_doc`,`v`.`nro_doc` AS `nro_doc`,`v`.`cambiomanual` AS `cambiomanual`,`v`.`pago_efe` AS `pago_efe`,`v`.`pago_efe_none` AS `pago_efe_none`,`v`.`pago_tar` AS `pago_tar`,`v`.`pago_yape` AS `pago_yape`,`v`.`pago_plin` AS `pago_plin`,`v`.`pago_tran` AS `pago_tran`,`v`.`descuento_monto` AS `desc_monto`,`v`.`descuento_tipo` AS `desc_tipo`,`v`.`descuento_personal` AS `desc_personal`,`v`.`descuento_motivo` AS `desc_motivo`,`v`.`comision_tarjeta` AS `comis_tar`,`v`.`comision_delivery` AS `comis_del`,`v`.`igv` AS `igv`,`v`.`total` AS `total`,`v`.`codigo_operacion` AS `codigo_operacion`,`v`.`fecha_venta` AS `fec_ven`,`v`.`estado` AS `estado`,`v`.`enviado_sunat` AS `enviado_sunat`,`v`.`code_respuesta_sunat` AS `code_respuesta_sunat`,`v`.`descripcion_sunat_cdr` AS `descripcion_sunat_cdr`,`v`.`name_file_sunat` AS `name_file_sunat`,`v`.`hash_cdr` AS `hash_cdr`,`v`.`hash_cpe` AS `hash_cpe`,`v`.`fecha_vencimiento` AS `fecha_vencimiento`,`td`.`descripcion` AS `desc_td`,`tp`.`descripcion` AS `desc_tp`,`v`.`consumo` AS `consumo`,`v`.`consumo_desc` AS `consumo_desc`,`v`.`observacion` AS `observacion`,concat(`tu`.`ape_paterno`,' ',`tu`.`ape_materno`,' ',`tu`.`nombres`) AS `desc_usu` from (((`tm_venta` `v` join `tm_tipo_doc` `td` on(`v`.`id_tipo_doc` = `td`.`id_tipo_doc`)) join `tm_tipo_pago` `tp` on(`v`.`id_tipo_pago` = `tp`.`id_tipo_pago`)) join `tm_usuario` `tu` on(`v`.`id_usu` = `tu`.`id_usu`)) where `v`.`id_venta` <> 0 order by `v`.`id_venta` desc ;


--
-- Estructura para la vista `v_estadistica`
--
DROP VIEW IF EXISTS `v_estadistica`;

CREATE VIEW `v_estadistica`  AS SELECT
	`v`.`id_venta`,
	`v`.`id_pedido`,
	`v`.`id_tipo_pedido`,
	`v`.`id_apc`, 
	`v`.`total`, 
	`p`.`id_mozo` AS `id_usu`, 
	CONCAT(`u`.`nombres`, ' ', SUBSTR(`u`.`ape_paterno`, 1, 1), '.') AS `nombres`,
	`u`.`ape_materno`, 
	`u`.`ape_paterno`, 
	`v`.`fecha_venta`,
	`d`.`tipo_entrega`,
	`d`.`id_repartidor`,
	CONCAT(`r`.`nombres`, ' ', SUBSTR(`r`.`ape_paterno`, 1, 1), '.') AS `repartidor_abrv`,
	CONCAT(`r`.`nombres`, ' ', `r`.`ape_paterno`, ' ', `r`.`ape_materno`) AS `repartidor`,
	`v`.`estado`
FROM
	`tm_venta` `v`
	LEFT JOIN
	`tm_pedido_mesa` `p`
	ON 
		`v`.`id_pedido` = `p`.`id_pedido`
	LEFT JOIN
	`tm_usuario` `u`
	ON 
		`p`.`id_mozo` = `u`.`id_usu`
	LEFT JOIN
	`tm_pedido_delivery` `d`
	ON 
		`d`.`id_pedido` = `v`.`id_pedido`
	LEFT JOIN
	`tm_usuario` `r`
	ON 
		`d`.`id_repartidor` = `r`.`id_usu`
WHERE
	`v`.`estado` = 'a';
--
-- Estructura para la vista `v_estadistica`
--

DROP VIEW IF EXISTS `v_topping_venta`;

CREATE VIEW v_topping_venta AS
SELECT 
    d.id_det_ped, 
    d.id_top, 
    d.precio, 
    d.cantidad, 
    d.estado, 
    t.top_nom, 
    t.top_cat, 
    t.top_cod, 
    t.top_sto, 
    t.top_cos 
FROM 
    tm_detalle_pedido_topping d
JOIN 
    v_toppings t 
    ON d.id_top = t.id_top;