Bug 21467: Fix default sort
[koha.git] / serials / subscription-detail.pl
index 824ffc2..9ea681b 100755 (executable)
@@ -25,8 +25,9 @@ use C4::Serials;
 use C4::Output;
 use C4::Context;
 use C4::Search qw/enabled_staff_search_views/;
-use Koha::DateUtils;
 
+use Koha::AuthorisedValues;
+use Koha::DateUtils;
 use Koha::Acquisition::Bookseller;
 
 use Date::Calc qw/Today Day_of_Year Week_of_Year Add_Delta_Days/;
@@ -44,7 +45,7 @@ if ( $op and $op eq "close" ) {
     C4::Serials::ReopenSubscription( $subscriptionid );
 }
 
-# the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
+# the subscription must be deletable if there is NO issues for a reason or another (should not happened, but...)
 
 # Permission needed if it is a deletion (del) : delete_subscription
 # Permission needed otherwise : *
@@ -61,6 +62,10 @@ my ($template, $loggedinuser, $cookie)
 
 
 my $subs = GetSubscription($subscriptionid);
+
+output_and_exit( $query, $cookie, $template, 'unknown_subscription')
+    unless $subs;
+
 $subs->{enddate} ||= GetExpirationDate($subscriptionid);
 
 my ($totalissues,@serialslist) = GetSerials($subscriptionid);
@@ -103,7 +108,8 @@ for my $date ( qw(startdate enddate firstacquidate histstartdate histenddate) )
     $subs->{$date} = output_pref( { str => $subs->{$date}, dateonly => 1 } )
         if $subs->{$date};
 }
-$subs->{location} = GetKohaAuthorisedValueLib("LOC",$subs->{location});
+my $av = Koha::AuthorisedValues->search({ category => 'LOC', authorised_value => $subs->{location} });
+$subs->{location} = $av->count ? $av->next->lib : '';
 $subs->{abouttoexpire}  = abouttoexpire($subs->{subscriptionid});
 $template->param(%{ $subs });
 $template->param(biblionumber_for_new_subscription => $subs->{bibnum});
@@ -114,32 +120,6 @@ my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($su
 
 my $default_bib_view = get_default_view();
 
-my ( $order, $bookseller, $tmpl_infos );
-if ( defined $subscriptionid ) {
-    my $lastOrderNotReceived = GetLastOrderNotReceivedFromSubscriptionid $subscriptionid;
-    my $lastOrderReceived = GetLastOrderReceivedFromSubscriptionid $subscriptionid;
-    if ( defined $lastOrderNotReceived ) {
-        my $basket = GetBasket $lastOrderNotReceived->{basketno};
-        my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
-        ( $tmpl_infos->{valuegsti_ordered}, $tmpl_infos->{valuegste_ordered} ) = get_value_with_gst_params ( $lastOrderNotReceived->{ecost}, $lastOrderNotReceived->{gstrate}, $bookseller );
-        $tmpl_infos->{valuegsti_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegsti_ordered} );
-        $tmpl_infos->{valuegste_ordered} = sprintf( "%.2f", $tmpl_infos->{valuegste_ordered} );
-        $tmpl_infos->{budget_name_ordered} = GetBudgetName $lastOrderNotReceived->{budget_id};
-        $tmpl_infos->{basketno} = $lastOrderNotReceived->{basketno};
-        $tmpl_infos->{ordered_exists} = 1;
-    }
-    if ( defined $lastOrderReceived ) {
-        my $basket = GetBasket $lastOrderReceived->{basketno};
-        my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
-        ( $tmpl_infos->{valuegsti_spent}, $tmpl_infos->{valuegste_spent} ) = get_value_with_gst_params ( $lastOrderReceived->{unitprice}, $lastOrderReceived->{gstrate}, $bookseller );
-        $tmpl_infos->{valuegsti_spent} = sprintf( "%.2f", $tmpl_infos->{valuegsti_spent} );
-        $tmpl_infos->{valuegste_spent} = sprintf( "%.2f", $tmpl_infos->{valuegste_spent} );
-        $tmpl_infos->{budget_name_spent} = GetBudgetName $lastOrderReceived->{budget_id};
-        $tmpl_infos->{invoiceid} = $lastOrderReceived->{invoiceid};
-        $tmpl_infos->{spent_exists} = 1;
-    }
-}
-
 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
 for my $field ( @$additional_fields ) {
     if ( $field->{authorised_value_category} ) {
@@ -148,6 +128,9 @@ for my $field ( @$additional_fields ) {
 }
 $template->param( additional_fields_for_subscription => $additional_fields );
 
+# FIXME Do we want to hide canceled orders?
+my $orders = Koha::Acquisition::Orders->search( { subscriptionid => $subscriptionid }, { order_by => { -desc => 'parent_ordernumber' } } );
+
 $template->param(
     subscriptionid => $subscriptionid,
     serialslist => \@serialslist,
@@ -164,10 +147,8 @@ $template->param(
     intranetcolorstylesheet => C4::Context->preference('intranetcolorstylesheet'),
     irregular_issues => scalar @irregular_issues,
     default_bib_view => $default_bib_view,
+    orders => $orders,
     (uc(C4::Context->preference("marcflavour"))) => 1,
-    show_acquisition_details => defined $tmpl_infos->{ordered_exists} || defined $tmpl_infos->{spent_exists} ? 1 : 0,
-    basketno => $order->{basketno},
-    %$tmpl_infos,
 );
 
 output_html_with_http_headers $query, $cookie, $template->output;
@@ -186,36 +167,3 @@ sub get_default_view {
     }
     return 'detail';
 }
-
-sub get_value_with_gst_params {
-    my $value = shift;
-    my $gstrate = shift;
-    my $bookseller = shift;
-    if ( $bookseller->{listincgst} ) {
-        return ( $value, $value / ( 1 + $gstrate ) );
-    } else {
-        return ( $value * ( 1 + $gstrate ), $value );
-    }
-}
-
-sub get_gste {
-    my $value = shift;
-    my $gstrate = shift;
-    my $bookseller = shift;
-    if ( $bookseller->{invoiceincgst} ) {
-        return $value / ( 1 + $gstrate );
-    } else {
-        return $value;
-    }
-}
-
-sub get_gst {
-    my $value = shift;
-    my $gstrate = shift;
-    my $bookseller = shift;
-    if ( $bookseller->{invoiceincgst} ) {
-        return $value / ( 1 + $gstrate ) * $gstrate;
-    } else {
-        return $value * ( 1 + $gstrate ) - $value;
-    }
-}