From c8f33af96fddd66cafc74894305855079989d3ee Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Thu, 17 Oct 2019 13:19:34 +0100
Subject: [PATCH] Bug 23354: (follow-up) Make display of debit types
 configurable

This patch adds a the ability to define where a debit type will be
available as a option for use.
---
 Koha/Schema/Result/AccountDebitType.pm        | 16 ++++++++---
 admin/debit_types.pl                          |  6 ++--
 installer/data/mysql/account_debit_types.sql  | 28 +++++++++----------
 .../data/mysql/atomicupdate/bug_23354.perl    | 22 +++++++++++++++
 installer/data/mysql/kohastructure.sql        |  3 +-
 .../prog/en/modules/admin/debit_types.tt      | 27 ++++++++++++++----
 members/maninvoice.pl                         |  2 +-
 pos/pay.pl                                    |  2 +-
 8 files changed, 77 insertions(+), 29 deletions(-)

diff --git a/Koha/Schema/Result/AccountDebitType.pm b/Koha/Schema/Result/AccountDebitType.pm
index 146e97199b..5a858d8256 100644
--- a/Koha/Schema/Result/AccountDebitType.pm
+++ b/Koha/Schema/Result/AccountDebitType.pm
@@ -35,12 +35,18 @@ __PACKAGE__->table("account_debit_types");
   is_nullable: 1
   size: 200
 
-=head2 can_be_added_manually
+=head2 can_be_invoiced
 
   data_type: 'tinyint'
   default_value: 1
   is_nullable: 0
 
+=head2 can_be_sold
+
+  data_type: 'tinyint'
+  default_value: 0
+  is_nullable: 0
+
 =head2 default_amount
 
   data_type: 'decimal'
@@ -66,8 +72,10 @@ __PACKAGE__->add_columns(
   { data_type => "varchar", is_nullable => 0, size => 80 },
   "description",
   { data_type => "varchar", is_nullable => 1, size => 200 },
-  "can_be_added_manually",
+  "can_be_invoiced",
   { data_type => "tinyint", default_value => 1, is_nullable => 0 },
+  "can_be_sold",
+  { data_type => "tinyint", default_value => 0, is_nullable => 0 },
   "default_amount",
   { data_type => "decimal", is_nullable => 1, size => [28, 6] },
   "is_system",
@@ -121,8 +129,8 @@ __PACKAGE__->has_many(
 );
 
 
-# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-10-23 13:48:17
-# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:uDKsz1QUF6zY+haOVVQZNw
+# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-10-17 12:22:04
+# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:8FIMJZ+JAmqa+Dx7oymBjw
 
 __PACKAGE__->add_columns(
     '+is_system' => { is_boolean => 1 }
diff --git a/admin/debit_types.pl b/admin/debit_types.pl
index 89decf1160..fe09d0b839 100755
--- a/admin/debit_types.pl
+++ b/admin/debit_types.pl
@@ -76,7 +76,8 @@ if ( $op eq 'add_form' ) {
 }
 elsif ( $op eq 'add_validate' ) {
     my $description           = $input->param('description');
-    my $can_be_added_manually = $input->param('can_be_added_manually') || 0;
+    my $can_be_invoiced = $input->param('can_be_invoiced') || 0;
+    my $can_be_sold = $input->param('can_be_sold') || 0;
     my $default_amount        = $input->param('default_amount') || undef;
     my @branches = grep { $_ ne q{} } $input->multi_param('branches');
 
@@ -84,7 +85,8 @@ elsif ( $op eq 'add_validate' ) {
         $debit_type = Koha::Account::DebitType->new( { code => $code } );
     }
     $debit_type->description($description);
-    $debit_type->can_be_added_manually($can_be_added_manually);
+    $debit_type->can_be_invoiced($can_be_invoiced);
+    $debit_type->can_be_sold($can_be_sold);
     $debit_type->default_amount($default_amount);
 
     try {
diff --git a/installer/data/mysql/account_debit_types.sql b/installer/data/mysql/account_debit_types.sql
index 731c461e80..18a499e95e 100644
--- a/installer/data/mysql/account_debit_types.sql
+++ b/installer/data/mysql/account_debit_types.sql
@@ -1,14 +1,14 @@
-INSERT INTO account_debit_types ( code, description, can_be_added_manually, default_amount, is_system ) VALUES
-('ACCOUNT', 'Account creation fee', 0, NULL, 1),
-('ACCOUNT_RENEW', 'Account renewal fee', 0, NULL, 1),
-('LOST', 'Lost item', 1, NULL, 1),
-('MANUAL', 'Manual fee', 1, NULL, 0),
-('NEW_CARD', 'New card fee', 1, NULL, 1),
-('OVERDUE', 'Overdue fine', 0, NULL, 1),
-('PROCESSING', 'Lost item processing fee', 0, NULL, 1),
-('RENT', 'Rental fee', 0, NULL, 1),
-('RENT_DAILY', 'Daily rental fee', 0, NULL, 1),
-('RENT_RENEW', 'Renewal of rental item', 0, NULL, 1),
-('RENT_DAILY_RENEW', 'Rewewal of daily rental item', 0, NULL, 1),
-('RESERVE', 'Hold fee', 0, NULL, 1),
-('RESERVE_EXPIRED', 'Hold waiting too long', 0, NULL, 1);
+INSERT INTO account_debit_types ( code, description, can_be_invoiced, can_be_sold, default_amount, is_system ) VALUES
+('ACCOUNT', 'Account creation fee', 0, 0, NULL, 1),
+('ACCOUNT_RENEW', 'Account renewal fee', 0, 0, NULL, 1),
+('LOST', 'Lost item', 1, 0, NULL, 1),
+('MANUAL', 'Manual fee', 1, 0, NULL, 0),
+('NEW_CARD', 'New card fee', 1, 0, NULL, 1),
+('OVERDUE', 'Overdue fine', 0, 0, NULL, 1),
+('PROCESSING', 'Lost item processing fee', 0, 0, NULL, 1),
+('RENT', 'Rental fee', 0, 0, NULL, 1),
+('RENT_DAILY', 'Daily rental fee', 0, 0, NULL, 1),
+('RENT_RENEW', 'Renewal of rental item', 0, 0, NULL, 1),
+('RENT_DAILY_RENEW', 'Rewewal of daily rental item', 0, 0, NULL, 1),
+('RESERVE', 'Hold fee', 0, 0, NULL, 1),
+('RESERVE_EXPIRED', 'Hold waiting too long', 0, 0, NULL, 1);
diff --git a/installer/data/mysql/atomicupdate/bug_23354.perl b/installer/data/mysql/atomicupdate/bug_23354.perl
index edf3525626..9e7d269303 100644
--- a/installer/data/mysql/atomicupdate/bug_23354.perl
+++ b/installer/data/mysql/atomicupdate/bug_23354.perl
@@ -5,6 +5,28 @@ if( CheckVersion( $DBversion ) ) {
         INSERT IGNORE INTO account_offset_types ( type ) VALUES ( 'Purchase' );
     });
 
+    # Updating field in account_debit_types
+    unless ( column_exists('account_debit_types', 'can_be_invoiced') ) {
+        $dbh->do(
+            qq{
+                ALTER TABLE account_debit_types
+                CHANGE COLUMN
+                  can_be_added_manually can_be_invoiced tinyint(1) NOT NULL DEFAULT 1
+              }
+        );
+    }
+    unless ( column_exists('account_debit_types', 'can_be_sold') ) {
+        $dbh->do(
+            qq{
+                ALTER IGNORE TABLE account_debit_types
+                ADD
+                  can_be_sold tinyint(1) DEFAULT 0
+                AFTER
+                  can_be_invoiced
+              }
+        );
+    }
+
     $dbh->do(q{
 INSERT IGNORE INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`, `title`, `content`, `message_transport_type`, `lang`) VALUES
 ('pos', 'RECEIPT', '', 'Point of sale receipt', 0, 'Receipt', '<table>
diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql
index 41f1ee37bb..7a49696d5e 100644
--- a/installer/data/mysql/kohastructure.sql
+++ b/installer/data/mysql/kohastructure.sql
@@ -2654,7 +2654,8 @@ DROP TABLE IF EXISTS `account_debit_types`;
 CREATE TABLE `account_debit_types` (
   `code` varchar(80) NOT NULL,
   `description` varchar(200) DEFAULT NULL,
-  `can_be_added_manually` tinyint(4) NOT NULL DEFAULT 1,
+  `can_be_invoiced` tinyint(1) NOT NULL DEFAULT 1, -- boolean flag to denote if this debit type is available for manual invoicing
+  `can_be_sold` tinyint(1) NOT NULL DEFAULT 0, -- boolean flag to denote if this debit type is available at point of sale
   `default_amount` decimal(28,6) DEFAULT NULL,
   `is_system` tinyint(1) NOT NULL DEFAULT 0,
   `archived` tinyint(1) NOT NULL DEFAULT 0, -- boolean flag to denote if this till is archived or not
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/debit_types.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/debit_types.tt
index 2fdf187ff2..736a1815be 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/debit_types.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/debit_types.tt
@@ -83,11 +83,19 @@
                                     <input type="text" name="description" id="description" required="required" class="required" size="80" maxlength="100" value="[% debit_type.description | html %]" /> <span class="required">Required</span>
                                 </li>
                                 <li>
-                                    <label for="can_be_added_manually">Can be added manually? </label>
-                                    [% IF debit_type.can_be_added_manually %]
-                                        <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" checked="checked" value="1" />
+                                    <label for="can_be_invoiced">Can be manually invoiced? </label>
+                                    [% IF debit_type.can_be_invoiced %]
+                                        <input type="checkbox" name="can_be_invoiced" id="can_be_invoiced" checked="checked" value="1" />
                                     [% ELSE %]
-                                        <input type="checkbox" name="can_be_added_manually" id="can_be_added_manually" value="1" />
+                                        <input type="checkbox" name="can_be_invoiced" id="can_be_invoiced" value="1" />
+                                    [% END %]
+                                </li>
+                                <li>
+                                    <label for="can_be_sold">Can be sold? </label>
+                                    [% IF debit_type.can_be_sold %]
+                                        <input type="checkbox" name="can_be_sold" id="can_be_sold" checked="checked" value="1" />
+                                    [% ELSE %]
+                                        <input type="checkbox" name="can_be_sold" id="can_be_sold" value="1" />
                                     [% END %]
                                 </li>
                                 <li>
@@ -128,7 +136,7 @@
                                 <th>Code</th>
                                 <th>Description</th>
                                 <th>Default amount</th>
-                                <th>Can be added manually</th>
+                                <th>Available for</th>
                                 <th>Library limitations</th>
                                 <th>Actions</th>
                             </thead>
@@ -140,7 +148,14 @@
                                     <td>[% debit_type.code | html %]</td>
                                     <td>[% debit_type.description | html %]</td>
                                     <td>[% debit_type.default_amount | $Price %]</td>
-                                    <td>[% IF debit_type.can_be_added_manually %]Yes[% ELSE %]No[% END %]</td>
+                                    <td>[% IF debit_type.can_be_invoiced && debit_type.can_be_sold %]
+                                        <i class="fa fa-id-card-o"></i> Invoicing, <i class="fa fa-shopping-cart"></i> Sale
+                                        [% ELSIF debit_type.can_be_invoiced %]
+                                        <i class="fa fa-id-card-o"></i> Invoicing
+                                        [% ELSIF debit_type.can_be_sold %]
+                                        <i class="fa fa-shopping-cart"></i> Sale
+                                        [% END %]
+                                    </td>
                                     <td>
                                         [% IF debit_type.library_limits.count > 0 %]
                                             [% library_limits_str = "" %]
diff --git a/members/maninvoice.pl b/members/maninvoice.pl
index a54206fb74..f271d1f791 100755
--- a/members/maninvoice.pl
+++ b/members/maninvoice.pl
@@ -126,7 +126,7 @@ else {
     );
 
     my @debit_types = Koha::Account::DebitTypes->search_with_library_limits(
-        { can_be_added_manually => 1, archived => 0 },
+        { can_be_invoiced => 1, archived => 0 },
         {}, $library_id );
     $template->param( debit_types => \@debit_types );
     $template->param(
diff --git a/pos/pay.pl b/pos/pay.pl
index 3406f24461..3d9956f8a5 100755
--- a/pos/pay.pl
+++ b/pos/pay.pl
@@ -56,7 +56,7 @@ else {
 
 my $invoice_types =
   Koha::Account::DebitTypes->search_with_library_limits(
-    { can_be_added_manually => 1 },
+    { can_be_sold => 1 },
     {}, $library_id );
 $template->param( invoice_types => $invoice_types );
 
-- 
2.20.1