CREATE OR REPLACE DEFINER=`vn-admin`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`ticketNotInvoiced`
AS SELECT `t`.`companyFk` AS `companyFk`,
	`cm`.`code` AS `companyCode`,
	`t`.`clientFk` AS `clientFk`,
	`c`.`name` AS `clientName`,
	`t`.`shipped` AS `shipped`,
	cast(
		sum(
			`s`.`quantity` * `s`.`price` * (100 - `s`.`discount`) / 100
		) AS decimal(10, 2)
	) AS `value`
FROM (
		(
			(
				`vn`.`ticket` `t`
				JOIN `vn`.`sale` `s` ON(`s`.`ticketFk` = `t`.`id`)
			)
			JOIN `vn`.`client` `c` ON(`c`.`id` = `t`.`clientFk`)
		)
		JOIN `vn`.`company` `cm` ON(`cm`.`id` = `t`.`companyFk`)
	)
WHERE `t`.`refFk` IS NULL
	AND `t`.`shipped` > '2017-01-01'
GROUP BY `t`.`id`
