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

170 lines
4.2 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`,
`v_xsubclien`.`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 `vn2008`.`v_xsubclien` ON(
`Clientes`.`id_cliente` = `v_xsubclien`.`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`,
`v_xsubclien`.`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`,
`v_xsubprov`.`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 `vn2008`.`v_xsubprov` ON(
`Proveedores`.`Id_Proveedor` = `v_xsubprov`.`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 `v_xsubprov`.`proveedor_id`,
`v_xsubprov`.`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`,
`v_xsubprov`.`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 `vn2008`.`v_xsubprov` ON(
`Proveedores`.`Id_Proveedor` = `v_xsubprov`.`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 `v_xsubprov`.`proveedor_id`,
`v_xsubprov`.`empresa_id`