Bug 21467: Allow several receipts for a given subscription
[koha.git] / acqui / invoices.pl
index 4909b78..8ab7a32 100755 (executable)
@@ -26,16 +26,16 @@ Search for invoices
 
 =cut
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 use CGI qw ( -utf8 );
 use C4::Auth;
 use C4::Output;
 
 use C4::Acquisition qw/GetInvoices/;
-use C4::Branch qw/GetBranches/;
 use C4::Budgets;
+use Koha::DateUtils;
+use Koha::Acquisition::Booksellers;
 
 my $input = CGI->new;
 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
@@ -61,66 +61,51 @@ my $author           = $input->param('author');
 my $publisher        = $input->param('publisher');
 my $publicationyear  = $input->param('publicationyear');
 my $branch           = $input->param('branch');
+my $message_id       = $input->param('message_id');
 my $op               = $input->param('op');
 
+$shipmentdatefrom and $shipmentdatefrom = eval { dt_from_string( $shipmentdatefrom ) };
+$shipmentdateto   and $shipmentdateto   = eval { dt_from_string( $shipmentdateto ) };
+$billingdatefrom  and $billingdatefrom  = eval { dt_from_string( $billingdatefrom ) };
+$billingdateto    and $billingdateto    = eval { dt_from_string( $billingdateto ) };
+
 my $invoices = [];
 if ( $op and $op eq 'do_search' ) {
-    my $shipmentdatefrom_iso = C4::Dates->new($shipmentdatefrom)->output('iso');
-    my $shipmentdateto_iso   = C4::Dates->new($shipmentdateto)->output('iso');
-    my $billingdatefrom_iso  = C4::Dates->new($billingdatefrom)->output('iso');
-    my $billingdateto_iso    = C4::Dates->new($billingdateto)->output('iso');
     @{$invoices} = GetInvoices(
         invoicenumber    => $invoicenumber,
         supplierid       => $supplierid,
-        shipmentdatefrom => $shipmentdatefrom_iso,
-        shipmentdateto   => $shipmentdateto_iso,
-        billingdatefrom  => $billingdatefrom_iso,
-        billingdateto    => $billingdateto_iso,
+        shipmentdatefrom => $shipmentdatefrom ? output_pref( { str => $shipmentdatefrom, dateformat => 'iso' } ) : undef,
+        shipmentdateto   => $shipmentdateto   ? output_pref( { str => $shipmentdateto,   dateformat => 'iso' } ) : undef,
+        billingdatefrom  => $billingdatefrom  ? output_pref( { str => $billingdatefrom,  dateformat => 'iso' } ) : undef,
+        billingdateto    => $billingdateto    ? output_pref( { str => $billingdateto,    dateformat => 'iso' } ) : undef,
         isbneanissn      => $isbneanissn,
         title            => $title,
         author           => $author,
         publisher        => $publisher,
         publicationyear  => $publicationyear,
-        branchcode       => $branch
+        branchcode       => $branch,
+        message_id       => $message_id,
     );
 }
 
 # Build suppliers list
-my @suppliers      = Koha::Acquisition::Bookseller->search;
+my @suppliers      = Koha::Acquisition::Booksellers->search( undef, { order_by => { -asc => 'name' } } );
 my $suppliers_loop = [];
 my $suppliername;
 foreach (@suppliers) {
     my $selected = 0;
-    if ($supplierid && $supplierid == $_->{id} ) {
+    if ($supplierid && $supplierid == $_->id ) {
         $selected = 1;
-        $suppliername = $_->{name};
+        $suppliername = $_->name;
     }
     push @{$suppliers_loop},
       {
-        suppliername => $_->{name},
-        booksellerid   => $_->{id},
+        suppliername => $_->name,
+        booksellerid   => $_->id,
         selected     => $selected,
       };
 }
 
-# Build branches list
-my $branches      = GetBranches();
-my $branches_loop = [];
-my $branchname;
-foreach ( sort keys %$branches ) {
-    my $selected = 0;
-    if ( $branch && $branch eq $_ ) {
-        $selected   = 1;
-        $branchname = $branches->{$_}->{'branchname'};
-    }
-    push @{$branches_loop},
-      {
-        branchcode => $_,
-        branchname => $branches->{$_}->{branchname},
-        selected   => $selected,
-      };
-}
-
 my $budgets = GetBudgets();
 my @budgets_loop;
 foreach my $budget (@$budgets) {
@@ -135,6 +120,8 @@ $template->param(
     invoicenumber   => $invoicenumber,
     booksellerid    => $supplierid,
     suppliername    => $suppliername,
+    shipmentdatefrom => $shipmentdatefrom,
+    shipmentdateto   => $shipmentdateto,
     billingdatefrom => $billingdatefrom,
     billingdateto   => $billingdateto,
     isbneanissn     => $isbneanissn,
@@ -143,9 +130,7 @@ $template->param(
     publisher       => $publisher,
     publicationyear => $publicationyear,
     branch          => $branch,
-    branchname      => $branchname,
     suppliers_loop  => $suppliers_loop,
-    branches_loop   => $branches_loop,
 );
 
 output_html_with_http_headers $input, $cookie, $template->output;