From 9ccd3337982d3a409190fbe5848ee5c11ecbda04 Mon Sep 17 00:00:00 2001
From: Katrin Fischer <katrin.fischer@bsz-bw.de>
Date: Fri, 8 Sep 2023 15:34:33 +0000
Subject: [PATCH] Bug 28449: Add column with invoice number to basket summary
 page

At the moment it's quite a lot of clicks to get from the basket
summary to the invoice something was received in. This adds a nice
shortcut by creating a new column for the invoicenumber in the basket
summary table.

To test:
* Make sure you have a basket with some received and unreceived
  order lines
* Verify that the (received) shows for your received lines
* Verify no invoice number or link to it in sight
* Apply patch
* Verify that there is now a column for the Invoice containing
  the invoice number as a link to the invoice
* Test with a staff patron, that doesn't have edit_invoices permission
* Verify that now instead of a link you see the invoice number as text

Be happy, sign off ;)

Note: this doesn't change the CSV export. The way the CSV is built is
quite different to the summary table, so it would be better handled
in a separate bug/patch. The columns are already quite different!

Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu>
---
 acqui/basket.pl                               | 39 +++++++++++--------
 admin/columns_settings.yml                    |  2 +
 .../prog/en/modules/acqui/basket.tt           | 10 +++++
 3 files changed, 34 insertions(+), 17 deletions(-)

diff --git a/acqui/basket.pl b/acqui/basket.pl
index 80c633cf56..d7f2d76a99 100755
--- a/acqui/basket.pl
+++ b/acqui/basket.pl
@@ -473,27 +473,32 @@ sub get_order_infos {
         my $biblio = Koha::Biblios->find( $biblionumber );
         my $countbiblio = $biblio->active_orders->count;
 
-        my $ordernumber = $order->{'ordernumber'};
+        my $ordernumber       = $order->{'ordernumber'};
         my $cnt_subscriptions = $biblio->subscriptions->count;
-        my $itemcount   = $biblio->items->count;
-        my $holds_count = $biblio->holds->count;
-        my $order = Koha::Acquisition::Orders->find($ordernumber); # FIXME We should certainly do that at the beginning of this sub
-        my $items = $order->items;
+        my $itemcount         = $biblio->items->count;
+        my $holds_count       = $biblio->holds->count;
+        my $order             = Koha::Acquisition::Orders->find($ordernumber);    # FIXME We should certainly do that at the beginning of this sub
+        my $items   = $order->items;
+        my $invoice = $order->invoice;
+
         my $itemholds  = $biblio->holds->search({ itemnumber => { -in => [ $items->get_column('itemnumber') ] } })->count;
 
         # if the biblio is not in other orders and if there is no items elsewhere and no subscriptions and no holds we can then show the link "Delete order and Biblio" see bug 5680
-        $line{can_del_bib}          = 1 if $countbiblio <= 1 && $itemcount == $items->count && !($cnt_subscriptions) && !($holds_count);
-        $line{items}                = $itemcount - $items->count;
-        $line{left_item}            = 1 if $line{items} >= 1;
-        $line{left_biblio}          = 1 if $countbiblio > 1;
-        $line{biblios}              = $countbiblio - 1;
-        $line{left_subscription}    = 1 if $cnt_subscriptions;
-        $line{subscriptions}        = $cnt_subscriptions;
-        ($holds_count >= 1) ? $line{left_holds} = 1 : $line{left_holds} = 0;
-        $line{left_holds_on_order}  = 1 if $line{left_holds}==1 && ($line{items} == 0 || $itemholds );
-        $line{holds}                = $holds_count;
-        $line{holds_on_order}       = $itemholds?$itemholds:$holds_count if $line{left_holds_on_order};
-        $line{order_object}         = $order;
+        $line{can_del_bib} = 1
+            if $countbiblio <= 1 && $itemcount == $items->count && !($cnt_subscriptions) && !($holds_count);
+        $line{items}             = $itemcount - $items->count;
+        $line{left_item}         = 1 if $line{items} >= 1;
+        $line{left_biblio}       = 1 if $countbiblio > 1;
+        $line{biblios}           = $countbiblio - 1;
+        $line{left_subscription} = 1 if $cnt_subscriptions;
+        $line{subscriptions}     = $cnt_subscriptions;
+        ( $holds_count >= 1 ) ? $line{left_holds} = 1 : $line{left_holds} = 0;
+        $line{left_holds_on_order} = 1 if $line{left_holds} == 1 && ( $line{items} == 0 || $itemholds );
+        $line{holds}               = $holds_count;
+        $line{holds_on_order}      = $itemholds ? $itemholds : $holds_count if $line{left_holds_on_order};
+        $line{order_object}        = $order;
+        $line{invoice_object}      = $invoice;
+
     }
 
     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
diff --git a/admin/columns_settings.yml b/admin/columns_settings.yml
index 90fb535472..ae3226a215 100644
--- a/admin/columns_settings.yml
+++ b/admin/columns_settings.yml
@@ -152,6 +152,8 @@ modules:
               columnname: fund
             -
               columnname: estimated_delivery_date
+            -
+              columnname: invoice
             -
               columnname: supplier_report
             -
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
index 69acd9f1cc..62cd8dde80 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/basket.tt
@@ -476,6 +476,7 @@
                                                     <th>GST</th>
                                                     <th>Fund</th>
                                                     <th>Estimated delivery date</th>
+                                                    <th>Invoice</th>
                                                     [% IF Koha.Preference('EDIFACT') && ediaccount %]
                                                         <th>Supplier report</th>
                                                     [% END %]
@@ -506,6 +507,7 @@
                                                         <th>[% foot_loo.tax_value | $Price %]</th>
                                                         <th>&nbsp;</th>
                                                         <th>&nbsp;</th>
+                                                        <th>&nbsp;</th>
                                                         [% IF Koha.Preference('EDIFACT') && ediaccount %]
                                                             <th>&nbsp;</th>
                                                         [% END %]
@@ -534,6 +536,7 @@
                                                     <th>[% total_tax_value | $Price %]</th>
                                                     <th>&nbsp;</th>
                                                     <th>&nbsp;</th>
+                                                    <th>&nbsp;</th>
                                                     [% IF Koha.Preference('EDIFACT') && ediaccount %]
                                                         <th>&nbsp;</th>
                                                     [% END %]
@@ -654,6 +657,13 @@
                                                                     <i class="fa-solid fa-pencil" aria-hidden="true"></i> Edit</a>
                                                             [% END %]
                                                         </td>
+                                                        <td>
+                                                            [% IF CAN_user_acquisition_edit_invoices %]
+                                                                <a href="/cgi-bin/koha/acqui/invoice.pl?invoiceid=[% books_loo.invoice_object.invoiceid | url %]" class="invoice">[% books_loo.invoice_object.invoicenumber | html %]</a>
+                                                            [% ELSE %]
+                                                                [% books_loo.invoice_object.invoicenumber | html %]
+                                                            [% END %]
+                                                        </td>
                                                         [% IF Koha.Preference('EDIFACT') && ediaccount %]
                                                             <td>[% books_loo.suppliers_report | html %]</td>
                                                         [% END %]
-- 
2.30.2