Bug 33290: Fix incorrect variable in http-client.js
[koha-ffzg.git] / acqui / basket.pl
index 7d16879..80c633c 100755 (executable)
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use C4::Auth;
-use C4::Koha;
-use C4::Output;
+use C4::Auth qw( get_template_and_user haspermission );
+use C4::Output qw( output_html_with_http_headers output_and_exit );
 use CGI qw ( -utf8 );
-use C4::Acquisition;
-use C4::Budgets;
-use C4::Contract;
-use C4::Debug;
-use C4::Biblio;
-use C4::Items;
-use C4::Suggestions;
+use C4::Acquisition qw( GetBasket CanUserManageBasket GetBasketAsCSV NewBasket NewBasketgroup ModBasket ReopenBasket ModBasketUsers GetBasketgroup GetBasketgroups GetBasketUsers GetOrders GetOrder get_rounded_price );
+use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget );
+use C4::Contract qw( GetContract );
+use C4::Suggestions qw( GetSuggestion GetSuggestionInfoFromBiblionumber GetSuggestionInfo );
 use Koha::Biblios;
 use Koha::Acquisition::Baskets;
 use Koha::Acquisition::Booksellers;
 use Koha::Acquisition::Orders;
 use Koha::Libraries;
-use C4::Letters qw/SendAlerts/;
-use Date::Calc qw/Add_Delta_Days/;
+use C4::Letters qw( SendAlerts );
+use Date::Calc qw( Add_Delta_Days );
 use Koha::Database;
-use Koha::EDI qw( create_edi_order get_edifact_ean );
+use Koha::EDI qw( create_edi_order );
 use Koha::CsvProfiles;
 use Koha::Patrons;
 
@@ -74,7 +70,7 @@ the supplier this script have to display the basket.
 
 =cut
 
-our $query        = new CGI;
+our $query        = CGI->new;
 our $basketno     = $query->param('basketno');
 our $ean          = $query->param('ean');
 our $booksellerid = $query->param('booksellerid');
@@ -86,7 +82,6 @@ our ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
         query           => $query,
         type            => "intranet",
         flagsrequired   => { acquisition => 'order_manage' },
-        debug           => 1,
     }
 );
 
@@ -128,45 +123,38 @@ if ( $op eq 'delete_confirm' ) {
     output_and_exit( $query, $cookie, $template, 'insufficient_permission' )
       unless $logged_in_patron->has_permission( { acquisition => 'delete_baskets' } );
 
-    my $basketno = $query->param('basketno');
-    my $delbiblio = $query->param('delbiblio');
-    my @orders = GetOrders($basketno);
-#Delete all orders included in that basket, and all items received.
-    foreach my $myorder (@orders){
-        DelOrder($myorder->{biblionumber},$myorder->{ordernumber});
-    }
-# if $delbiblio = 1, delete the records if possible
-    if ((defined $delbiblio)and ($delbiblio ==1)){
-        my @cannotdelbiblios ;
-        foreach my $myorder (@orders){
-            my $biblionumber = $myorder->{'biblionumber'};
-            my $biblio = Koha::Biblios->find( $biblionumber );
-            my $countbiblio = $biblio->active_orders->count;
-            my $ordernumber = $myorder->{'ordernumber'};
-            my $cnt_subscriptions = $biblio->subscriptions->count;
-            my $itemcount = $biblio->items->count;
-            my $error;
-            if ($countbiblio == 0 && $itemcount == 0 && not $cnt_subscriptions ) {
-                $error = DelBiblio($myorder->{biblionumber}) }
-            else {
-                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
-                                         title=> $myorder->{'title'},
-                                         author=> $myorder->{'author'},
-                                         countbiblio=> $countbiblio,
-                                         itemcount=>$itemcount,
-                                         subscriptions => $cnt_subscriptions};
-            }
-            if ($error) {
-                push @cannotdelbiblios, {biblionumber=> ($myorder->{biblionumber}),
-                                         title=> $myorder->{'title'},
-                                         author=> $myorder->{'author'},
-                                         othererror=> $error};
-            }
+    my $basketno   = $query->param('basketno');
+    my $delbiblio  = $query->param('delbiblio');
+    my $basket_obj = Koha::Acquisition::Baskets->find($basketno);
+
+    my $orders = $basket_obj->orders->filter_by_current;
+
+    my @cannotdelbiblios;
+
+    while ( my $order = $orders->next ) {
+        # cancel the order
+        $order->cancel({ delete_biblio => $delbiblio });
+        my @messages = @{ $order->object_messages };
+
+        if ( scalar @messages > 0 ) {
+
+            my $biblio = $order->biblio;
+
+            push @cannotdelbiblios, {
+                biblionumber  => $biblio->id,
+                title         => $biblio->title // '',
+                author        => $biblio->author // '',
+                countbiblio   => $biblio->active_orders->count,
+                itemcount     => $biblio->items->count,
+                subscriptions => $biblio->subscriptions->count,
+            };
         }
-        $template->param( cannotdelbiblios => \@cannotdelbiblios );
     }
- # delete the basket
-    DelBasket($basketno,);
+
+    $template->param( cannotdelbiblios => \@cannotdelbiblios );
+
+    # delete the basket
+    $basket_obj->delete;
     $template->param(
         delete_confirmed => 1,
         booksellername => $bookseller->name,
@@ -184,7 +172,7 @@ if ( $op eq 'delete_confirm' ) {
     exit;
 } elsif ($op eq 'email') {
     my $err = eval {
-        SendAlerts( 'orderacquisition', $query->param('basketno'), 'ACQORDER' );
+        SendAlerts( 'orderacquisition', scalar $query->param('basketno'), 'ACQORDER' );
     };
     if ( $@ ) {
         push @messages, { type => 'error', code => $@ };
@@ -198,9 +186,12 @@ if ( $op eq 'delete_confirm' ) {
 } elsif ($op eq 'close') {
     my $confirm = $query->param('confirm') || $confirm_pref eq '2';
     if ($confirm) {
-        my $basketno = $query->param('basketno');
-        my $booksellerid = $query->param('booksellerid');
-        $basketno =~ /^\d+$/ and CloseBasket($basketno);
+
+        # close the basket
+        # FIXME: we should fetch the object at the beginning of this script
+        #        and get rid of the hash that is passed around
+        Koha::Acquisition::Baskets->find($basketno)->close;
+
         # if requested, create basket group, close it and attach the basket
         if ($query->param('createbasketgroup')) {
             my $branchcode;
@@ -318,10 +309,6 @@ if ( $op eq 'list' ) {
     # if new basket, pre-fill infos
     $basket->{creationdate} = ""            unless ( $basket->{creationdate} );
     $basket->{authorisedby} = $loggedinuser unless ( $basket->{authorisedby} );
-    $debug
-      and warn sprintf
-      "loggedinuser: $loggedinuser; creationdate: %s; authorisedby: %s",
-      $basket->{creationdate}, $basket->{authorisedby};
 
     my @basketusers_ids = GetBasketUsers($basketno);
     my @basketusers;
@@ -390,6 +377,9 @@ if ( $op eq 'list' ) {
         last;
     }
 
+    my $basket_obj = Koha::Acquisition::Baskets->find($basketno);
+    my $edi_order = $basket_obj->edi_order;
+
     $template->param(
         basketno             => $basketno,
         basket               => $basket,
@@ -401,6 +391,7 @@ if ( $op eq 'list' ) {
         basketcontractname   => $contract->{contractname},
         branches_loop        => \@branches_loop,
         creationdate         => $basket->{creationdate},
+        edi_order            => $edi_order,
         authorisedby         => $basket->{authorisedby},
         authorisedbyname     => $basket->{authorisedbyname},
         users_ids            => join(':', @basketusers_ids),
@@ -430,11 +421,11 @@ if ( $op eq 'list' ) {
         #
         # (The template has another implicit restriction that the order cannot be closed if there
         # are any orders with uncertain prices.)
-        unclosable           => @orders ? $basket->{is_standing} : 1,
+        unclosable           => @orders || @cancelledorders ? $basket->{is_standing} : 1,
         has_budgets          => $has_budgets,
         duplinbatch          => $duplinbatch,
-        csv_profiles         => [ Koha::CsvProfiles->search({ type => 'sql', used_for => 'export_basket' }) ],
-        available_additional_fields => [ Koha::AdditionalFields->search( { tablename => 'aqbasket' } ) ],
+        csv_profiles         => Koha::CsvProfiles->search({ type => 'sql', used_for => 'export_basket' }),
+        available_additional_fields => Koha::AdditionalFields->search( { tablename => 'aqbasket' } ),
         additional_field_values => { map {
             $_->field->name => $_->value
         } Koha::Acquisition::Baskets->find($basketno)->additional_field_values->as_list },
@@ -505,12 +496,13 @@ sub get_order_infos {
         $line{order_object}         = $order;
     }
 
-
     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
     $line{suggestionid}         = $$suggestion{suggestionid};
     $line{surnamesuggestedby}   = $$suggestion{surnamesuggestedby};
     $line{firstnamesuggestedby} = $$suggestion{firstnamesuggestedby};
 
+    $line{estimated_delivery_date} = $order->{estimated_delivery_date};
+
     foreach my $key (qw(transferred_from transferred_to)) {
         if ($line{$key}) {
             my $order = GetOrder($line{$key});
@@ -531,17 +523,17 @@ sub get_order_infos {
 sub edi_close_and_order {
     my $confirm = $query->param('confirm') || $confirm_pref eq '2';
     if ($confirm) {
-            my $edi_params = {
-                basketno => $basketno,
-                ean    => $ean,
-            };
-            if ( $basket->{branch} ) {
-                $edi_params->{branchcode} = $basket->{branch};
-            }
-            if ( create_edi_order($edi_params) ) {
-                #$template->param( edifile => 1 );
-            }
-        CloseBasket($basketno);
+        my $edi_params = {
+            basketno => $basketno,
+            ean    => $ean,
+        };
+        if ( $basket->{branch} ) {
+            $edi_params->{branchcode} = $basket->{branch};
+        }
+        if ( create_edi_order($edi_params) ) {
+            #$template->param( edifile => 1 );
+        }
+        Koha::Acquisition::Baskets->find($basketno)->close;
 
         # if requested, create basket group, close it and attach the basket
         if ( $query->param('createbasketgroup') ) {
@@ -562,8 +554,8 @@ sub edi_close_and_order {
             );
             ModBasket(
                 {
-                    basketno      => $basketno,
-                    basketgroupid => $basketgroupid
+                    basketno       => $basketno,
+                    basketgroupid  => $basketgroupid
                 }
             );
             print $query->redirect(