CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn2008`.`v_ventes`
AS SELECT `Agencias`.`Agencia` AS `Agencia`,
	`A`.`Categoria` AS `Categoria`,
	`A`.`tipo_id` AS `tipo_id`,
	`A`.`Medida` AS `Medida`,
	`A`.`Article` AS `Article`,
	`A`.`Color` AS `Color`,
	`CS`.`Id_cliente` AS `Id_Cliente`,
	`TP`.`Id_Tipo` AS `Tipo`,
	`T`.`Factura` AS `Factura`,
	`T`.`warehouse_id` AS `warehouse_id`,
	`M`.`Id_Movimiento` AS `Id_Movimiento`,
	`M`.`Id_Article` AS `Id_Article`,
	`TP`.`Id_Tipo` AS `Familia`,
	`M`.`Id_Ticket` AS `Id_Ticket`,
	`M`.`Concepte` AS `Concepte`,
	`M`.`Cantidad` AS `Cantidad`,
	`M`.`quantity` AS `quantity`,
	`M`.`Preu` AS `Preu`,
	`M`.`Descuento` AS `Descuento`,
	IF(
		`T`.`Fecha` >= '2015-10-01',
		`M`.`CostFixat`,
		`M`.`Preu` * (100 - `M`.`Descuento`) / 100
	) AS `CostFixat`,
	`M`.`Reservado` AS `Reservado`,
	`M`.`OK` AS `OK`,
	`M`.`PrecioFijado` AS `PrecioFijado`,
	`M`.`odbc_date` AS `odbc_date`,
	cast(`T`.`Fecha` AS date) AS `Fecha`,
	`T`.`Fecha` AS `FechaCompleta`,
	`CS`.`consignatario` AS `Alias`,
	`T`.`Id_Consigna` AS `Id_Consigna`,
	`M`.`Cantidad` * `M`.`Preu` * (100 - `M`.`Descuento`) / 100 AS `Importe`,
	`O`.`Origen` AS `Origen`,
	`TP`.`reino_id` AS `reino_id`,
	`C`.`invoice` AS `invoice`,
	`A`.`producer_id` AS `producer_id`
FROM (
		(
			(
				(
					(
						(
							(
								(
									`vn2008`.`Movimientos` `M`
									JOIN `vn2008`.`Tickets` `T` ON(`M`.`Id_Ticket` = `T`.`Id_Ticket`)
								)
								JOIN `vn2008`.`Consignatarios` `CS` ON(`CS`.`id_consigna` = `T`.`Id_Consigna`)
							)
							JOIN `vn2008`.`Clientes` `C` ON(`CS`.`Id_cliente` = `C`.`id_cliente`)
						)
						JOIN `vn2008`.`Articles` `A` ON(`M`.`Id_Article` = `A`.`Id_Article`)
					)
					JOIN `vn2008`.`Origen` `O` ON(`O`.`id` = `A`.`id_origen`)
				)
				JOIN `vn2008`.`Tipos` `TP` ON(`A`.`tipo_id` = `TP`.`tipo_id`)
			)
			JOIN `vn2008`.`reinos` `r` ON(`TP`.`reino_id` = `r`.`id`)
		)
		JOIN `vn2008`.`Agencias` ON(`Agencias`.`Id_Agencia` = `T`.`Id_Agencia`)
	)
WHERE `T`.`Fecha` >= '2013-01-01'
	AND `C`.`real` > 0
	AND `r`.`id` <> 6
