DELIMITER $$
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`collection_addItem`(
	vBarccodeFk INT,
	vQuantity INT,
	vTicketFk INT
)
BEGIN
/**
 * Add a sales line to the ticket with the indicated amount.
 *
 * @param vBarcodeFk barcode on the item label
 * @param vQuantity quantity to be added to the ticket
 * @param vTicketFk ticket to which the sales line is added
 */
	DECLARE vConcept VARCHAR(50);
	DECLARE vItemFk INT;
	DECLARE vSaleFk INT;
	DECLARE vDescription VARCHAR(50);
	DECLARE vItemName VARCHAR(50);

	SELECT barcodeToItem(vBarccodeFk) INTO vItemFk;

	SELECT CONCAT(getUser(),
			' ',
			DATE_FORMAT(util.VN_NOW(), '%H:%i' ),
			' ',
			i.name
			) INTO vConcept 
		FROM item i
		WHERE i.id = vItemFk;

	SELECT name INTO vItemName FROM item WHERE id = vBarccodeFk;

	SELECT CONCAT('Añadido articulo ', 
			vItemName,
			' cantidad:', 
			vQuantity
			) INTO vDescription;

	INSERT INTO ticketLog
		SET originFk = vTicketFk,
			userFk = getUser(),
			`action` = 'update',
			`description` = vDescription;

	INSERT INTO sale
		SET itemFk = vItemFk,
			ticketFk = vTicketFk,
			concept = vConcept,
			quantity = vQuantity,
			isAdded = TRUE;

	SELECT LAST_INSERT_ID() INTO vSaleFk;

	UPDATE sale
		SET originalQuantity = 0
		WHERE id = vSaleFk;

	CALL sale_calculateComponent(vSaleFk, NULL);
END$$
DELIMITER ;
