From df82dadaef8c4d9d2a6edc9380f2fb73a8a3c420 Mon Sep 17 00:00:00 2001 From: Jonathan Niles Date: Thu, 30 May 2019 10:47:38 +0100 Subject: [PATCH] fix(invoice): primary key collision invoicing_fees The invoice creation threw errors when two invoicing_fees of the same _value_ were applied to the same invoice. This is obvious incorrect. This change allows you to apply any value any number of times to the same invoice, provided you are not reusing the same invoicing fee. --- server/models/migrations/next/migrations.sql | 10 ++++++-- server/models/procedures/invoicing.sql | 2 +- server/models/schema.sql | 27 ++++++++++---------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/server/models/migrations/next/migrations.sql b/server/models/migrations/next/migrations.sql index 52331e6d99..6f6a3303ad 100644 --- a/server/models/migrations/next/migrations.sql +++ b/server/models/migrations/next/migrations.sql @@ -109,7 +109,7 @@ CREATE PROCEDURE UnbalancedInvoicePaymentsTable( DROP TEMPORARY TABLE IF EXISTS tmp_invoices_2; CREATE TEMPORARY TABLE tmp_invoices_2 AS SELECT * FROM tmp_invoices_1; - + DROP TEMPORARY TABLE IF EXISTS tmp_records; -- This holds the invoices from the PJ/GL CREATE TEMPORARY TABLE tmp_records AS @@ -187,4 +187,10 @@ CREATE PROCEDURE UnbalancedInvoicePaymentsTable( ); END$$ -DELIMITER ; \ No newline at end of file +-- author: jniles +-- date: 30/05/2019 +-- Fix primary key collisions at HEV. +ALTER TABLE invoice_invoicing_fee DROP PRIMARY KEY; +ALTER TABLE invoice_invoicing_fee ADD PRIMARY KEY (invoice_uuid, invoicing_fee_id); + +DELIMITER ; diff --git a/server/models/procedures/invoicing.sql b/server/models/procedures/invoicing.sql index 713db57d43..9704d2ebd9 100644 --- a/server/models/procedures/invoicing.sql +++ b/server/models/procedures/invoicing.sql @@ -898,7 +898,7 @@ CREATE PROCEDURE UnbalancedInvoicePaymentsTable( DROP TEMPORARY TABLE IF EXISTS tmp_invoices_2; CREATE TEMPORARY TABLE tmp_invoices_2 AS SELECT * FROM tmp_invoices_1; - + DROP TEMPORARY TABLE IF EXISTS tmp_records; -- This holds the invoices from the PJ/GL CREATE TEMPORARY TABLE tmp_records AS diff --git a/server/models/schema.sql b/server/models/schema.sql index baf809ee53..cca2557bb3 100644 --- a/server/models/schema.sql +++ b/server/models/schema.sql @@ -1614,11 +1614,10 @@ CREATE TABLE `invoice` ( DROP TABLE IF EXISTS invoice_invoicing_fee; CREATE TABLE invoice_invoicing_fee ( - `invoice_uuid` BINARY(16) NOT NULL, - `value` DECIMAL(10,4) NOT NULL, + `invoice_uuid` BINARY(16) NOT NULL, + `value` DECIMAL(10,4) NOT NULL, `invoicing_fee_id` SMALLINT UNSIGNED NOT NULL, - PRIMARY KEY (`invoice_uuid`, `value`), - UNIQUE KEY `invoice_invoicing_fee_1` (`invoice_uuid`, `invoicing_fee_id`), + PRIMARY KEY (`invoice_uuid`, `invoicing_fee_id`), KEY `invoice_uuid` (`invoice_uuid`), KEY `invoicing_fee_id` (`invoicing_fee_id`), FOREIGN KEY (`invoice_uuid`) REFERENCES `invoice` (`uuid`) ON DELETE CASCADE, @@ -2332,7 +2331,7 @@ CREATE TABLE `hospitalization_indicator` ( `total_beds` INT DEFAULT 0, `total_hospitalized_patient` INT DEFAULT 0, `total_external_patient` INT DEFAULT 0, - `total_death` INT DEFAULT 0, + `total_death` INT DEFAULT 0, `indicator_uuid` BINARY(16) NOT NULL, PRIMARY KEY (`uuid`), FOREIGN KEY (`indicator_uuid`) REFERENCES `indicator` (`uuid`) ON UPDATE CASCADE @@ -2374,19 +2373,19 @@ CREATE TABLE `finance_indicator` ( `indicator_uuid` BINARY(16) NOT NULL, PRIMARY KEY (`uuid`), FOREIGN KEY (`indicator_uuid`) REFERENCES `indicator` (`uuid`) ON UPDATE CASCADE -) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci; +) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci; DROP TABLE IF EXISTS `break_even_reference`; CREATE TABLE `break_even_reference` ( - `id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, - `label` VARCHAR(100) NOT NULL, - `is_cost` tinyint(1) DEFAULT 0, + `id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT, + `label` VARCHAR(100) NOT NULL, + `is_cost` tinyint(1) DEFAULT 0, `is_variable` tinyint(1) DEFAULT 0, - `is_turnover` tinyint(1) DEFAULT 0, - `account_reference_id` MEDIUMINT(8) UNSIGNED NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `break_even_reference_1` (`label`), - KEY `account_reference_id` (`account_reference_id`), + `is_turnover` tinyint(1) DEFAULT 0, + `account_reference_id` MEDIUMINT(8) UNSIGNED NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `break_even_reference_1` (`label`), + KEY `account_reference_id` (`account_reference_id`), FOREIGN KEY (`account_reference_id`) REFERENCES `account_reference` (`id`) ) ENGINE=InnoDB DEFAULT CHARACTER SET = utf8mb4 DEFAULT COLLATE = utf8mb4_unicode_ci;