Bug 30891: (QA follow-up) Add check to patron details page
[koha-ffzg.git] / C4 / Acquisition.pm
index 143058d..76b6e64 100644 (file)
@@ -19,14 +19,13 @@ package C4::Acquisition;
 
 
 use Modern::Perl;
-use Carp;
+use Carp qw( carp croak );
 use Text::CSV_XS;
 use C4::Context;
-use C4::Debug;
-use C4::Suggestions;
-use C4::Biblio;
-use C4::Contract;
-use C4::Debug;
+use C4::Suggestions qw( GetSuggestion GetSuggestionFromBiblionumber ModSuggestion );
+use C4::Biblio qw( GetMarcFromKohaField GetMarcStructure IsMarcStructureInternal );
+use C4::Contract qw( GetContract );
+use C4::Log qw( logaction );
 use C4::Templates qw(gettemplate);
 use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Acquisition::Baskets;
@@ -43,59 +42,58 @@ use Koha::Patrons;
 use C4::Koha;
 
 use MARC::Field;
-use MARC::Record;
+use JSON qw( to_json );
 
-use Time::localtime;
-
-use vars qw(@ISA @EXPORT);
 
+our (@ISA, @EXPORT_OK);
 BEGIN {
     require Exporter;
-    @ISA    = qw(Exporter);
-    @EXPORT = qw(
-        &GetBasket &NewBasket &CloseBasket &ReopenBasket &DelBasket &ModBasket
-        &GetBasketAsCSV &GetBasketGroupAsCSV
-        &GetBasketsByBookseller &GetBasketsByBasketgroup
-        &GetBasketsInfosByBookseller
-
-        &GetBasketUsers &ModBasketUsers
-        &CanUserManageBasket
-
-        &ModBasketHeader
-
-        &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
-        &GetBasketgroups &ReOpenBasketgroup
-
-        &ModOrder &GetOrder &GetOrders &GetOrdersByBiblionumber
-        &GetOrderFromItemnumber
-        &SearchOrders &GetHistory &GetRecentAcqui
-        &ModReceiveOrder &CancelReceipt
-        &TransferOrder
-        &ModItemOrder
-
-        &GetParcels
-
-        &GetInvoices
-        &GetInvoice
-        &GetInvoiceDetails
-        &AddInvoice
-        &ModInvoice
-        &CloseInvoice
-        &ReopenInvoice
-        &DelInvoice
-        &MergeInvoices
-
-        &AddClaim
-        &GetBiblioCountByBasketno
-
-        &GetOrderUsers
-        &ModOrderUsers
-        &NotifyOrderUsers
-
-        &FillWithDefaultValues
-
-        &get_rounded_price
-        &get_rounding_sql
+    @ISA       = qw(Exporter);
+    @EXPORT_OK = qw(
+      GetBasket NewBasket ReopenBasket ModBasket
+      GetBasketAsCSV GetBasketGroupAsCSV
+      GetBasketsByBookseller GetBasketsByBasketgroup
+      GetBasketsInfosByBookseller
+
+      GetBasketUsers ModBasketUsers
+      CanUserManageBasket
+
+      ModBasketHeader
+
+      ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroup CloseBasketgroup
+      GetBasketgroups ReOpenBasketgroup
+
+      ModOrder GetOrder GetOrders GetOrdersByBiblionumber
+      GetOrderFromItemnumber
+      SearchOrders GetHistory GetRecentAcqui
+      ModReceiveOrder CancelReceipt
+      populate_order_with_prices
+      TransferOrder
+      ModItemOrder
+
+      GetParcels
+
+      GetInvoices
+      GetInvoice
+      GetInvoiceDetails
+      AddInvoice
+      ModInvoice
+      CloseInvoice
+      ReopenInvoice
+      DelInvoice
+      MergeInvoices
+
+      AddClaim
+      GetBiblioCountByBasketno
+
+      GetOrderUsers
+      ModOrderUsers
+      NotifyOrderUsers
+
+      FillWithDefaultValues
+
+      get_rounded_price
+      get_rounding_sql
     );
 }
 
@@ -201,31 +199,22 @@ sub NewBasket {
     $basketname           ||= q{}; # default to empty strings
     $basketnote           ||= q{};
     $basketbooksellernote ||= q{};
-    ModBasketHeader( $basket, $basketname, $basketnote, $basketbooksellernote,
-        $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items );
-    return $basket;
-}
-
-#------------------------------------------------------------#
-
-=head3 CloseBasket
 
-  &CloseBasket($basketno);
-
-close a basket (becomes unmodifiable, except for receives)
-
-=cut
+    # Log the basket creation
+    if (C4::Context->preference("AcquisitionLog")) {
+        my $created = Koha::Acquisition::Baskets->find( $basket );
+        logaction(
+            'ACQUISITIONS',
+            'ADD_BASKET',
+            $basket,
+            to_json($created->unblessed)
+        );
+    }
 
-sub CloseBasket {
-    my ($basketno) = @_;
-    my $dbh        = C4::Context->dbh;
-    $dbh->do('UPDATE aqbasket SET closedate=now() WHERE basketno=?', {}, $basketno );
+    ModBasketHeader( $basket, $basketname, $basketnote, $basketbooksellernote,
+        $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items );
 
-    $dbh->do(
-q{UPDATE aqorders SET orderstatus = 'ordered' WHERE basketno = ? AND orderstatus NOT IN ( 'complete', 'cancelled')},
-        {}, $basketno
-    );
-    return;
+    return $basket;
 }
 
 =head3 ReopenBasket
@@ -247,6 +236,17 @@ sub ReopenBasket {
         WHERE basketno = ?
         AND orderstatus NOT IN ( 'complete', 'cancelled' )
         }, {}, $basketno);
+
+    # Log the basket reopening
+    if (C4::Context->preference("AcquisitionLog")) {
+        my $reopened = Koha::Acquisition::Baskets->find( $basketno );
+        logaction(
+            'ACQUISITIONS',
+            'REOPEN_BASKET',
+            $basketno,
+            to_json($reopened->unblessed)
+        );
+    }
     return;
 }
 
@@ -486,32 +486,6 @@ sub ReOpenBasketgroup {
 
 #------------------------------------------------------------#
 
-
-=head3 DelBasket
-
-  &DelBasket($basketno);
-
-Deletes the basket that has basketno field $basketno in the aqbasket table.
-
-=over
-
-=item C<$basketno> is the primary key of the basket in the aqbasket table.
-
-=back
-
-=cut
-
-sub DelBasket {
-    my ( $basketno ) = @_;
-    my $query = "DELETE FROM aqbasket WHERE basketno=?";
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare($query);
-    $sth->execute($basketno);
-    return;
-}
-
-#------------------------------------------------------------#
-
 =head3 ModBasket
 
   &ModBasket($basketinfo);
@@ -548,6 +522,19 @@ sub ModBasket {
     my $sth = $dbh->prepare($query);
     $sth->execute(@params);
 
+    # Log the basket update
+    if (C4::Context->preference("AcquisitionLog")) {
+        my $modified = Koha::Acquisition::Baskets->find(
+            $basketinfo->{basketno}
+        );
+        logaction(
+            'ACQUISITIONS',
+            'MODIFY_BASKET',
+            $basketinfo->{basketno},
+            to_json($modified->unblessed)
+        );
+    }
+
     return;
 }
 
@@ -605,6 +592,20 @@ sub ModBasketHeader {
         my $sth2 = $dbh->prepare($query2);
         $sth2->execute($contractnumber,$basketno);
     }
+
+    # Log the basket update
+    if (C4::Context->preference("AcquisitionLog")) {
+        my $modified = Koha::Acquisition::Baskets->find(
+            $basketno
+        );
+        logaction(
+            'ACQUISITIONS',
+            'MODIFY_BASKET_HEADER',
+            $basketno,
+            to_json($modified->unblessed)
+        );
+    }
+
     return;
 }
 
@@ -785,6 +786,20 @@ sub ModBasketUsers {
     foreach my $basketuser_id (@basketusers_ids) {
         $sth->execute($basketno, $basketuser_id);
     }
+
+    # Log the basket update
+    if (C4::Context->preference("AcquisitionLog")) {
+        logaction(
+            'ACQUISITIONS',
+            'MODIFY_BASKET_USERS',
+            $basketno,
+            to_json({
+                basketno    => $basketno,
+                basketusers => @basketusers_ids
+            })
+        );
+    }
+
     return;
 }
 
@@ -2070,6 +2085,8 @@ sub GetHistory {
     my $isbn   = $params{isbn};
     my $ean    = $params{ean};
     my $name = $params{name};
+    my $internalnote = $params{internalnote};
+    my $vendornote = $params{vendornote};
     my $from_placed_on = $params{from_placed_on};
     my $to_placed_on = $params{to_placed_on};
     my $basket = $params{basket};
@@ -2135,6 +2152,8 @@ sub GetHistory {
             aqorders.biblionumber,
             aqorders.orderstatus,
             aqorders.parent_ordernumber,
+            aqorders.order_internalnote,
+            aqorders.order_vendornote,
             aqbudgets.budget_name
             ";
     $query .= ", aqbudgets.budget_id AS budget" if defined $budget;
@@ -2227,6 +2246,16 @@ sub GetHistory {
         }
     }
 
+    if ( $internalnote ) {
+        $query .= " AND aqorders.order_internalnote LIKE ? ";
+        push @query_params, "%$internalnote%";
+    }
+
+    if ( $vendornote ) {
+        $query .= " AND aqorders.order_vendornote LIKE ?";
+        push @query_params, "%$vendornote%";
+    }
+
     if ($booksellerinvoicenumber) {
         $query .= " AND aqinvoices.invoicenumber LIKE ? ";
         push @query_params, "%$booksellerinvoicenumber%";
@@ -2262,7 +2291,7 @@ sub GetHistory {
         push @query_params, @$ordernumbers;
     }
     if ( @$additional_fields ) {
-        my @baskets = Koha::Acquisition::Baskets->filter_by_additional_fields($additional_fields);
+        my @baskets = Koha::Acquisition::Baskets->filter_by_additional_fields($additional_fields)->as_list;
 
         return [] unless @baskets;
 
@@ -2846,8 +2875,8 @@ sub populate_order_with_prices {
 
             # tax value = quantity * ecost tax excluded * tax rate
             # we should use the unitprice if included
-            my $cost_tax_included = $order->{unitprice_tax_included} || $order->{ecost_tax_included};
-            my $cost_tax_excluded = $order->{unitprice_tax_excluded} || $order->{ecost_tax_excluded};
+            my $cost_tax_included = $order->{unitprice_tax_included} == 0 ? $order->{ecost_tax_included} : $order->{unitprice_tax_included};
+            my $cost_tax_excluded = $order->{unitprice_tax_excluded} == 0 ? $order->{ecost_tax_excluded} : $order->{unitprice_tax_excluded};
             $order->{tax_value_on_ordering} = ( get_rounded_price($cost_tax_included) - get_rounded_price($cost_tax_excluded) ) * $order->{quantity};
 
         }
@@ -2868,7 +2897,7 @@ sub populate_order_with_prices {
 
             # tax value = quantity * ecost tax included * tax rate
             # we should use the unitprice if included
-            my $cost_tax_excluded = $order->{unitprice_tax_excluded} || $order->{ecost_tax_excluded};
+            my $cost_tax_excluded = $order->{unitprice_tax_excluded} == 0 ?  $order->{ecost_tax_excluded} : $order->{unitprice_tax_excluded};
             $order->{tax_value_on_ordering} = $order->{quantity} * get_rounded_price($cost_tax_excluded) * $order->{tax_rate_on_ordering};
         }
     }