Bug 29955: (QA follow-up) Tidy code block
[srvgit] / acqui / basket.pl
index ae918b9..c028dc0 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;
 
@@ -86,7 +82,6 @@ our ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
         query           => $query,
         type            => "intranet",
         flagsrequired   => { acquisition => 'order_manage' },
-        debug           => 1,
     }
 );
 
@@ -132,14 +127,14 @@ if ( $op eq 'delete_confirm' ) {
     my $delbiblio  = $query->param('delbiblio');
     my $basket_obj = Koha::Acquisition::Baskets->find($basketno);
 
-    my $orders = $basket_obj->orders;
+    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->messages };
+        my @messages = @{ $order->object_messages };
 
         if ( scalar @messages > 0 ) {
 
@@ -177,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 => $@ };
@@ -191,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;
@@ -311,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;
@@ -383,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,
@@ -394,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),
@@ -426,8 +424,8 @@ if ( $op eq 'list' ) {
         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 },
@@ -524,17 +522,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') ) {
@@ -555,8 +553,8 @@ sub edi_close_and_order {
             );
             ModBasket(
                 {
-                    basketno      => $basketno,
-                    basketgroupid => $basketgroupid
+                    basketno       => $basketno,
+                    basketgroupid  => $basketgroupid
                 }
             );
             print $query->redirect(