CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn2008`.`Compres`
AS SELECT `c`.`id` AS `Id_Compra`,
	`c`.`entryFk` AS `Id_Entrada`,
	`c`.`itemFk` AS `Id_Article`,
	`c`.`buyingValue` AS `Costefijo`,
	`c`.`quantity` AS `Cantidad`,
	`c`.`packagingFk` AS `Id_Cubo`,
	`c`.`stickers` AS `Etiquetas`,
	`c`.`freightValue` AS `Portefijo`,
	`c`.`packageValue` AS `Embalajefijo`,
	`c`.`comissionValue` AS `Comisionfija`,
	`c`.`packing` AS `Packing`,
	`c`.`grouping` AS `grouping`,
	`c`.`groupingMode` AS `caja`,
	`c`.`location` AS `Nicho`,
	`c`.`price1` AS `Tarifa1`,
	`c`.`price2` AS `Tarifa2`,
	`c`.`price3` AS `Tarifa3`,
	`c`.`minPrice` AS `PVP`,
	`c`.`printedStickers` AS `Vida`,
	`c`.`isChecked` AS `punteo`,
	`c`.`ektFk` AS `buy_edi_id`,
	`c`.`created` AS `odbc_date`,
	`c`.`isIgnored` AS `Novincular`,
	`c`.`isPickedOff` AS `isPickedOff`,
	`c`.`workerFk` AS `Id_Trabajador`,
	`c`.`weight` AS `weight`,
	`c`.`dispatched` AS `dispatched`,
	`c`.`containerFk` AS `container_id`,
	`c`.`itemOriginalFk` AS `itemOriginalFk`
FROM `vn`.`buy` `c`;

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn2008`.`buySource`
AS SELECT `b`.`entryFk` AS `Id_Entrada`,
	`b`.`isPickedOff` AS `isPickedOff`,
	NULL AS `tarifa0`,
	`e`.`kop` AS `kop`,
	`b`.`id` AS `Id_Compra`,
	`i`.`typeFk` AS `tipo_id`,
	`b`.`itemFk` AS `Id_Article`,
	`i`.`size` AS `Medida`,
	`i`.`stems` AS `Tallos`,
	`b`.`stickers` AS `Etiquetas`,
	`b`.`packagingFk` AS `Id_Cubo`,
	`b`.`buyingValue` AS `Costefijo`,
	`b`.`packing` AS `Packing`,
	`b`.`grouping` AS `Grouping`,
	`b`.`quantity` AS `Cantidad`,
	`b`.`price2` AS `Tarifa2`,
	`b`.`price3` AS `Tarifa3`,
	`b`.`isChecked` AS `Punteo`,
	`b`.`groupingMode` AS `Caja`,
	`i`.`isToPrint` AS `Imprimir`,
	`i`.`name` AS `Article`,
	`vn`.`ink`.`picture` AS `Tinta`,
	`i`.`originFk` AS `id_origen`,
	`i`.`minPrice` AS `PVP`,
	NULL AS `Id_Accion`,
	`s`.`company_name` AS `pro`,
	`i`.`hasMinPrice` AS `Min`,
	`b`.`isIgnored` AS `Novincular`,
	`b`.`freightValue` AS `Portefijo`,
	round(`b`.`buyingValue` * `b`.`quantity`, 2) AS `Importe`,
	`b`.`printedStickers` AS `Vida`,
	`i`.`comment` AS `reference`,
	`b`.`workerFk` AS `Id_Trabajador`,
	`e`.`s1` AS `S1`,
	`e`.`s2` AS `S2`,
	`e`.`s3` AS `S3`,
	`e`.`s4` AS `S4`,
	`e`.`s5` AS `S5`,
	`e`.`s6` AS `S6`,
	0 AS `price_fixed`,
	`i`.`producerFk` AS `producer_id`,
	`i`.`subName` AS `tag1`,
	`i`.`value5` AS `tag2`,
	`i`.`value6` AS `tag3`,
	`i`.`value7` AS `tag4`,
	`i`.`value8` AS `tag5`,
	`i`.`value9` AS `tag6`,
	`s`.`company_name` AS `company_name`,
	`b`.`weight` AS `weightPacking`,
	`i`.`packingOut` AS `packingOut`,
	`b`.`itemOriginalFk` AS `itemOriginalFk`,
	`io`.`longName` AS `itemOriginalName`,
	`it`.`gramsMax` AS `gramsMax`
FROM (
		(
			(
				(
					(
						(
							`vn`.`item` `i`
							JOIN `vn`.`itemType` `it` ON(`it`.`id` = `i`.`typeFk`)
						)
						LEFT JOIN `vn`.`ink` ON(`vn`.`ink`.`id` = `i`.`inkFk`)
					)
					LEFT JOIN `vn`.`buy` `b` ON(`b`.`itemFk` = `i`.`id`)
				)
				LEFT JOIN `vn`.`item` `io` ON(`io`.`id` = `b`.`itemOriginalFk`)
			)
			LEFT JOIN `edi`.`ekt` `e` ON(`e`.`id` = `b`.`ektFk`)
		)
		LEFT JOIN `edi`.`supplier` `s` ON(`e`.`pro` = `s`.`supplier_id`)
	);

CREATE OR REPLACE DEFINER=`root`@`localhost`
	SQL SECURITY DEFINER
	VIEW `vn`.`awbVolume`
AS SELECT `d`.`awbFk` AS `awbFk`,
	`b`.`stickers` * `i`.`density` * IF(
		`p`.`volume` > 0,
		`p`.`volume`,
		`p`.`width` * `p`.`depth` * IF(`p`.`height` = 0, `i`.`size` + 10, `p`.`height`)
	) / (`vc`.`aerealVolumetricDensity` * 1000) AS `volume`,
	`b`.`id` AS `buyFk`
FROM (
		(
			(
				(
					(
						(
							(
								(
									`vn`.`buy` `b`
									JOIN `vn`.`item` `i` ON(`b`.`itemFk` = `i`.`id`)
								)
								JOIN `vn`.`itemType` `it` ON(`i`.`typeFk` = `it`.`id`)
							)
							JOIN `vn`.`packaging` `p` ON(`p`.`id` = `b`.`packagingFk`)
						)
						JOIN `vn`.`entry` `e` ON(`b`.`entryFk` = `e`.`id`)
					)
					JOIN `vn`.`travel` `t` ON(`t`.`id` = `e`.`travelFk`)
				)
				JOIN `vn`.`duaEntry` `de` ON(`de`.`entryFk` = `e`.`id`)
			)
			JOIN `vn`.`dua` `d` ON(`d`.`id` = `de`.`duaFk`)
		)
		JOIN `vn`.`volumeConfig` `vc`
	)
WHERE `t`.`shipped` > makedate(year(`util`.`VN_CURDATE`()) - 1, 1);