salix/db/routines/vn2008/views/v_xsubcuentas.sql

208 lines
5.6 KiB
SQL

CREATE OR REPLACE DEFINER=`root`@`localhost`
SQL SECURITY DEFINER
VIEW `vn2008`.`v_xsubcuentas`
AS SELECT `Clientes`.`Cuenta` AS `COD`,
`Clientes`.`razonSocial` AS `TITULO`,
concat(
IF(
`p`.`CEE` = 1
AND ascii(left(`Clientes`.`if`, 1)) < 58,
`p`.`Codigo`,
''
),
`Clientes`.`if`
) AS `NIF`,
`Clientes`.`domicilio` AS `DOMICILIO`,
`Clientes`.`poblacion` AS `POBLACION`,
`province`.`name` AS `PROVINCIA`,
`Clientes`.`codPostal` AS `CODPOSTAL`,
`p`.`Codigo` AS `country_code`,
sub.`empresa_id` AS `empresa_id`,
substr(
`Clientes`.`e-mail`,
1,
coalesce(nullif(locate(',', `Clientes`.`e-mail`), 0), 99) - 1
) AS `EMAIL`,
IF(
`p`.`CEE` = 0
OR `p`.`CEE` = 1
AND `Clientes`.`vies` = 0,
1,
IF(
`p`.`CEE` = 1
AND `Clientes`.`vies` <> 0,
2,
4
)
) AS `IDNIF`
FROM (
(
(
`vn2008`.`Clientes`
JOIN (
SELECT DISTINCT `Facturas`.`Id_Cliente` AS `Id_Cliente`,
`Facturas`.`empresa_id` AS `empresa_id`
FROM `vn2008`.`Facturas`
WHERE `Facturas`.`Fecha` > `util`.`VN_CURDATE`() + INTERVAL -2 MONTH
UNION
SELECT `Recibos`.`Id_Cliente` AS `Id_Cliente`,
`Recibos`.`empresa_id` AS `empresa_id`
FROM `vn2008`.`Recibos`
WHERE `Recibos`.`Fechacobro` > `util`.`VN_CURDATE`() + INTERVAL -2 MONTH
) sub ON(
`Clientes`.`id_cliente` = sub.`Id_Cliente`
)
)
LEFT JOIN `vn2008`.`Paises` `p` ON(`p`.`Id` = `Clientes`.`Id_Pais`)
)
JOIN `vn2008`.`province` ON(
`Clientes`.`province_id` = `province`.`province_id`
)
)
GROUP BY `Clientes`.`id_cliente`,
sub.`empresa_id`
UNION ALL
SELECT `Proveedores`.`cuenta` AS `Cuenta`,
`Proveedores`.`Proveedor` AS `Proveedor`,
concat(
IF(
`p`.`CEE` = 1
AND ascii(left(`Proveedores`.`NIF`, 1)) < 58,
`p`.`Codigo`,
''
),
`Proveedores`.`NIF`
) AS `NIF`,
`Proveedores`.`Domicilio` AS `Domicilio`,
`Proveedores`.`Localidad` AS `Localidad`,
`prov`.`name` AS `Provincia`,
`Proveedores`.`codpos` AS `CP`,
`p`.`Codigo` AS `country_code`,
sub.`empresa_id` AS `empresa_id`,
substr(
`c`.`email`,
1,
coalesce(nullif(locate(',', `c`.`email`), 0), 99) - 1
) AS `EMAIL`,
IF(`p`.`CEE` = 0, 1, IF(`p`.`CEE` = 1, 2, 4)) AS `IDNIF`
FROM (
(
(
(
`vn2008`.`Proveedores`
JOIN (
SELECT `recibida`.`proveedor_id` AS `proveedor_id`,
`recibida`.`empresa_id` AS `empresa_id`
FROM `vn2008`.`recibida`
WHERE `recibida`.`fecha` > `util`.`VN_CURDATE`() + INTERVAL -3 MONTH
GROUP BY `recibida`.`proveedor_id`,
`recibida`.`empresa_id`
UNION ALL
SELECT `pago`.`id_proveedor` AS `id_proveedor`,
`pago`.`empresa_id` AS `empresa_id`
FROM `vn2008`.`pago`
WHERE `pago`.`fecha` > `util`.`VN_CURDATE`() + INTERVAL -3 MONTH
GROUP BY `pago`.`id_proveedor`,
`pago`.`empresa_id`
) sub ON(
`Proveedores`.`Id_Proveedor` = sub.`proveedor_id`
)
)
LEFT JOIN `vn2008`.`Paises` `p` ON(`p`.`Id` = `Proveedores`.`pais_id`)
)
LEFT JOIN `vn2008`.`province` `prov` ON(
`prov`.`province_id` = `Proveedores`.`province_id`
)
)
LEFT JOIN `vn`.`supplierContact` `c` ON(`c`.`supplierFk` = `Proveedores`.`Id_Proveedor`)
)
WHERE `Proveedores`.`oficial` <> 0
GROUP BY sub.`proveedor_id`,
sub.`empresa_id`
UNION ALL
SELECT `Gastos`.`Id_Gasto` COLLATE utf8mb3_unicode_ci AS `Id_Gasto`,
`Gastos`.`Gasto` COLLATE utf8mb3_unicode_ci AS `Gasto`,
NULL AS `NULL`,
NULL AS `My_exp_NULL`,
NULL AS `My_exp_1_NULL`,
NULL AS `My_exp_2_NULL`,
NULL AS `My_exp_3_NULL`,
NULL AS `country_code`,
`e`.`id` AS `id`,
NULL AS `EMAIL`,
1 AS `IDNIF`
FROM (
`vn2008`.`Gastos`
JOIN `vn2008`.`empresa` `e` ON(`e`.`id` = 442)
)
UNION ALL
SELECT `Bancos`.`Cuenta` AS `Cuenta`,
`Bancos`.`Banco` AS `Banco`,
NULL AS `NULL`,
NULL AS `My_exp_NULL`,
NULL AS `My_exp_1_NULL`,
NULL AS `My_exp_2_NULL`,
NULL AS `My_exp_3_NULL`,
NULL AS `country_code`,
`e`.`id` AS `id`,
NULL AS `EMAIL`,
1 AS `IDNIF`
FROM (
`vn2008`.`Bancos`
JOIN `vn2008`.`empresa` `e` ON(`e`.`id` = 442)
)
UNION ALL
SELECT lpad(right(`Proveedores`.`cuenta`, 5), 10, '47510000') AS `Cuenta`,
`Proveedores`.`Proveedor` AS `Proveedor`,
`Proveedores`.`NIF` AS `NIF`,
`Proveedores`.`Domicilio` AS `Domicilio`,
`Proveedores`.`Localidad` AS `Localidad`,
`prov`.`name` AS `Provincia`,
`Proveedores`.`codpos` AS `CP`,
`p`.`Codigo` AS `country_code`,
sub.`empresa_id` AS `empresa_id`,
substr(
`c`.`email`,
1,
coalesce(nullif(locate(',', `c`.`email`), 0), 99) - 1
) AS `EMAIL`,
IF(`p`.`CEE` = 0, 1, IF(`p`.`CEE` = 1, 2, 4)) AS `IDNIF`
FROM (
(
(
(
`vn2008`.`Proveedores`
JOIN (
SELECT `recibida`.`proveedor_id` AS `proveedor_id`,
`recibida`.`empresa_id` AS `empresa_id`
FROM `vn2008`.`recibida`
WHERE `recibida`.`fecha` > `util`.`VN_CURDATE`() + INTERVAL -3 MONTH
GROUP BY `recibida`.`proveedor_id`,
`recibida`.`empresa_id`
UNION ALL
SELECT `pago`.`id_proveedor` AS `id_proveedor`,
`pago`.`empresa_id` AS `empresa_id`
FROM `vn2008`.`pago`
WHERE `pago`.`fecha` > `util`.`VN_CURDATE`() + INTERVAL -3 MONTH
GROUP BY `pago`.`id_proveedor`,
`pago`.`empresa_id`
) sub ON(
`Proveedores`.`Id_Proveedor` = sub.`proveedor_id`
)
)
LEFT JOIN `vn2008`.`Paises` `p` ON(`p`.`Id` = `Proveedores`.`pais_id`)
)
LEFT JOIN `vn2008`.`province` `prov` ON(
`prov`.`province_id` = `Proveedores`.`province_id`
)
)
LEFT JOIN `vn`.`supplierContact` `c` ON(`c`.`supplierFk` = `Proveedores`.`Id_Proveedor`)
)
WHERE (
`Proveedores`.`cuenta` LIKE '_____3____'
OR `Proveedores`.`cuenta` LIKE '_____2____'
)
AND `Proveedores`.`oficial` = 1
GROUP BY sub.`proveedor_id`,
sub.`empresa_id`