From f7fb28f8302008c0fef4fb2350e00a5a32a905ec Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 9 Jan 2020 14:13:25 +0000
Subject: [PATCH] Bug 24081: Add credit_types, offset_types and permissions

This patch adds the credit_type, offset_type and permissions required
for the addition of a discounting workflow in the accounts system.

Signed-off-by: Michal Denar <black23@gmail.com>
---
 installer/data/mysql/account_credit_types.sql |  1 +
 installer/data/mysql/account_offset_types.sql |  1 +
 .../data/mysql/atomicupdate/bug_24081.perl    | 26 +++++++++++++++++++
 installer/data/mysql/userpermissions.sql      |  1 +
 4 files changed, 29 insertions(+)
 create mode 100644 installer/data/mysql/atomicupdate/bug_24081.perl

diff --git a/installer/data/mysql/account_credit_types.sql b/installer/data/mysql/account_credit_types.sql
index 70a51629d8..178b98b1c6 100644
--- a/installer/data/mysql/account_credit_types.sql
+++ b/installer/data/mysql/account_credit_types.sql
@@ -3,6 +3,7 @@ INSERT INTO account_credit_types ( code, description, can_be_added_manually, is_
 ('WRITEOFF', 'Writeoff', 0, 1),
 ('FORGIVEN', 'Forgiven', 1, 1),
 ('CREDIT', 'Credit', 1, 1),
+('DISCOUNT', 'A discount applied to a patrons fine', 0, 1),
 ('REFUND', 'A refund applied to a patrons fine', 0, 1),
 ('LOST_FOUND', 'Lost item fee refund', 0, 1),
 ('PURCHASE', 'Purchase', 0, 1);
diff --git a/installer/data/mysql/account_offset_types.sql b/installer/data/mysql/account_offset_types.sql
index f0cb80ece9..4b6ce8311d 100644
--- a/installer/data/mysql/account_offset_types.sql
+++ b/installer/data/mysql/account_offset_types.sql
@@ -20,4 +20,5 @@ INSERT INTO account_offset_types ( type ) VALUES
 ('Void Payment'),
 ('Credit Applied'),
 ('PAYOUT'),
+('DISOUNT'),
 ('REFUND');
diff --git a/installer/data/mysql/atomicupdate/bug_24081.perl b/installer/data/mysql/atomicupdate/bug_24081.perl
new file mode 100644
index 0000000000..a81fdc6a19
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_24081.perl
@@ -0,0 +1,26 @@
+$DBversion = 'XXX';    # will be replaced by the RM
+if ( CheckVersion($DBversion) ) {
+
+    $dbh->do(
+        qq{
+            INSERT IGNORE INTO account_credit_types (code, description, can_be_added_manually, is_system)
+            VALUES
+              ('DISCOUNT', 'A discount applied to a patrons fine', 0, 1)
+        }
+    );
+
+    $dbh->do(qq{
+        INSERT IGNORE INTO account_offset_types ( type ) VALUES ('DISCOUNT');
+    });
+
+    $dbh->do(qq{
+        INSERT IGNORE permissions (module_bit, code, description)
+        VALUES
+        (10, 'discount', 'Perform account discount action')
+    });
+
+    SetVersion($DBversion);
+    print "Upgrade to $DBversion done (Bug 24081 - Add DISCOUNT to account_credit_types)\n";
+    print "Upgrade to $DBversion done (Bug 24081 - Add DISCOUNT to account_offset_types)\n";
+    print "Upgrade to $DBversion done (Bug 24081 - Add accounts discount permission)\n";
+}
diff --git a/installer/data/mysql/userpermissions.sql b/installer/data/mysql/userpermissions.sql
index bad1b9ad44..75126f131a 100644
--- a/installer/data/mysql/userpermissions.sql
+++ b/installer/data/mysql/userpermissions.sql
@@ -46,6 +46,7 @@ INSERT INTO permissions (module_bit, code, description) VALUES
    ( 9, 'delete_all_items', 'Delete all items at once'),
    (10, 'payout', 'Perform account payout action'),
    (10, 'refund', 'Perform account refund action'),
+   (10, 'discount', 'Perform account discount action'),
    (10, 'writeoff', 'Write off fines and fees'),
    (10, 'remaining_permissions', 'Remaining permissions for managing fines and fees'),
    (11, 'currencies_manage', 'Manage currencies and exchange rates'),
-- 
2.20.1