CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn2008`.`v_descuadre_bionic`
AS SELECT `t`.`Id_Ticket` AS `Id_Ticket`,
	`t`.`Alias` AS `Alias`,
	`m`.`Concepte` AS `Concepte`,
	sum(`mc`.`Valor`) AS `suma_componente`,
	`m`.`Preu` AS `Preu`,
	`m`.`Descuento` AS `Descuento`,
	`m`.`Preu` * (100 - `m`.`Descuento`) / 100 - sum(`mc`.`Valor`) AS `diferencia`,
	`t`.`Fecha` AS `Fecha`,
	`m`.`Preu` > 0
	AND `tp`.`reino_id` <> 6
	AND `a`.`tipo_id` NOT IN (7, 115) AS `benvenut`
FROM (
		(
			(
				(
					(
						(
							`vn2008`.`Movimientos` `m`
							JOIN `vn2008`.`Tickets` `t` ON(`m`.`Id_Ticket` = `t`.`Id_Ticket`)
						)
						JOIN `vn2008`.`Clientes` `c` ON(`t`.`Id_Cliente` = `c`.`id_cliente`)
					)
					JOIN `vn2008`.`warehouse` `w` ON(`w`.`id` = `t`.`warehouse_id`)
				)
				JOIN `vn2008`.`Articles` `a` ON(`m`.`Id_Article` = `a`.`Id_Article`)
			)
			JOIN `vn2008`.`Tipos` `tp` ON(`a`.`tipo_id` = `tp`.`tipo_id`)
		)
		LEFT JOIN `vn2008`.`Movimientos_componentes` `mc` ON(`m`.`Id_Movimiento` = `mc`.`Id_Movimiento`)
	)
WHERE `t`.`Fecha` >= '2015-09-01'
	AND `t`.`empresa_id` IN (442, 791, 567)
	AND `w`.`reserve` <> 0
	AND `c`.`real` <> 0
	AND `tp`.`reino_id` <> 6
GROUP BY `m`.`Id_Movimiento`
HAVING abs(`diferencia`) > 0.01
	OR `diferencia` IS NULL
