2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-08-20 08:06:10 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`invoiceOut_newFromClient`(
|
2024-01-15 11:31:03 +00:00
|
|
|
IN vClientFk INT,
|
|
|
|
IN vSerial CHAR(2),
|
|
|
|
IN vMaxShipped DATE,
|
|
|
|
IN vCompanyFk INT,
|
|
|
|
IN vTaxArea VARCHAR(25),
|
|
|
|
IN vRef VARCHAR(25),
|
|
|
|
OUT vInvoiceId INT)
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Factura los tickets de un cliente hasta una fecha dada
|
|
|
|
* @param vClientFk Id del cliente a facturar
|
|
|
|
* @param vSerial Serie de factura
|
|
|
|
* @param vMaxShipped Fecha hasta la cual cogera tickets para facturar
|
|
|
|
* @param vCompanyFk Id de la empresa desde la que se factura
|
|
|
|
* @param vTaxArea Tipo de iva en relacion a la empresa y al cliente, NULL por defecto
|
|
|
|
* @param vRef Referencia de la factura en caso que se quiera forzar, NULL por defecto
|
|
|
|
* @return vInvoiceId factura
|
|
|
|
*/
|
|
|
|
DECLARE vIsRefEditable BOOLEAN;
|
|
|
|
DECLARE vIsExportation BOOLEAN;
|
|
|
|
|
|
|
|
IF vRef IS NOT NULL THEN
|
|
|
|
SELECT isRefEditable INTO vIsRefEditable
|
|
|
|
FROM invoiceOutSerial
|
|
|
|
WHERE code = vSerial;
|
|
|
|
|
|
|
|
IF NOT vIsRefEditable THEN
|
|
|
|
CALL util.throw('serial non editable');
|
|
|
|
END IF;
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
SELECT COUNT(*) INTO vIsExportation
|
|
|
|
FROM vn.invoiceOutSerial
|
|
|
|
WHERE taxAreaFk = 'WORLD'
|
|
|
|
AND `code` = vSerial;
|
|
|
|
|
|
|
|
IF vIsExportation THEN
|
|
|
|
CALL invoiceOut_exportationFromClient(vMaxShipped, vClientFk, vCompanyFk);
|
|
|
|
ELSE
|
|
|
|
CALL invoiceFromClient(vMaxShipped, vClientFk, vCompanyFk);
|
|
|
|
END IF;
|
|
|
|
|
|
|
|
CALL invoiceOut_new(vSerial, util.VN_CURDATE(), vTaxArea, vInvoiceId);
|
|
|
|
|
|
|
|
UPDATE invoiceOut
|
|
|
|
SET `ref` = vRef
|
|
|
|
WHERE id = vInvoiceId
|
|
|
|
AND vRef IS NOT NULL;
|
|
|
|
|
|
|
|
IF vSerial <> 'R' AND NOT ISNULL(vInvoiceId) AND vInvoiceId <> 0 THEN
|
|
|
|
CALL invoiceOutBooking(vInvoiceId);
|
|
|
|
END IF;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|