DROP PROCEDURE IF EXISTS `sp_FixCreditDebitNoteForHistoricalInvoice`;

DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_FixCreditDebitNoteForHistoricalInvoice`()
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
BEGIN
	/* UPDATE TABLE comp_sales_creditnote */
	IF NOT EXISTS(SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'comp_sales_creditnote' AND COLUMN_NAME = 'isHistoricalInvoice') THEN
		ALTER TABLE comp_sales_creditnote ADD `isHistoricalInvoice` INT NOT NULL DEFAULT '0' AFTER `salesInvoiceID`;
	END IF;
	
	/* UPDATE TABLE comp_supplier_debitnote */
	IF NOT EXISTS(SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'comp_supplier_debitnote' AND COLUMN_NAME = 'isHistoricalInvoice') THEN
		ALTER TABLE comp_supplier_debitnote ADD `isHistoricalInvoice` INT NOT NULL DEFAULT '0' AFTER `supplierInvoiceID`;
	END IF;
END
//

CALL sp_FixCreditDebitNoteForHistoricalInvoice();;