+my $op = $input->param('op');
+$resultsperpage ||= 20;
+
+our ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
+ { template_name => 'acqui/parcels.tt',
+ query => $input,
+ type => 'intranet',
+ authnotrequired => 0,
+ flagsrequired => { acquisition => 'order_receive' },
+ debug => 1,
+ }
+);
+
+my $invoicenumber = $input->param('invoice');
+my $shipmentcost = $input->param('shipmentcost');
+my $shipmentcost_budgetid = $input->param('shipmentcost_budgetid');
+my $shipmentdate = $input->param('shipmentdate');
+$shipmentdate and $shipmentdate = output_pref({ str => $shipmentdate, dateformat => 'iso', dateonly => 1 });
+
+if ( $op and $op eq 'new' ) {
+ if ( C4::Context->preference('AcqWarnOnDuplicateInvoice') ) {
+ my @invoices = GetInvoices(
+ supplierid => $booksellerid,
+ invoicenumber => $invoicenumber,
+ );
+ if ( scalar @invoices > 0 ) {
+ $template->{'VARS'}->{'duplicate_invoices'} = \@invoices;
+ $template->{'VARS'}->{'invoicenumber'} = $invoicenumber;
+ $template->{'VARS'}->{'shipmentdate'} = $shipmentdate;
+ $template->{'VARS'}->{'shipmentcost'} = $shipmentcost;
+ $template->{'VARS'}->{'shipmentcost_budgetid'} =
+ $shipmentcost_budgetid;
+ }
+ }
+ $op = 'confirm' unless $template->{'VARS'}->{'duplicate_invoices'};
+}
+if ($op and $op eq 'confirm') {
+ my $invoiceid = AddInvoice(
+ invoicenumber => $invoicenumber,
+ booksellerid => $booksellerid,
+ shipmentdate => $shipmentdate,
+ shipmentcost => $shipmentcost,
+ shipmentcost_budgetid => $shipmentcost_budgetid,
+ );
+ if(defined $invoiceid) {
+ # Successful 'Add'
+ print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");
+ exit 0;
+ } else {
+ $template->param(error_failed_to_create_invoice => 1);
+ }
+}
+
+my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+my @parcels = GetInvoices(
+ supplierid => $booksellerid,
+ invoicenumber => $code,
+ ( $datefrom ? ( shipmentdatefrom => output_pref({ dt => dt_from_string($datefrom), dateformat => 'iso' }) ) : () ),
+ ( $dateto ? ( shipmentdateto => output_pref({ dt => dt_from_string($dateto), dateformat => 'iso' }) ) : () ),
+ order_by => $order
+);
+my $count_parcels = @parcels;
+
+# multi page display gestion
+$startfrom ||= 0;
+if ( $count_parcels > $resultsperpage ) {
+ set_page_navigation( $count_parcels, $startfrom, $resultsperpage );
+}
+my $loopres = [];
+
+my $next_page_start = $startfrom + $resultsperpage;
+my $last_row = ( $next_page_start < $count_parcels ) ? $next_page_start - 1 : $count_parcels - 1;
+for my $i ( $startfrom .. $last_row) {
+ my $p = $parcels[$i];
+
+ push @{$loopres},
+ { number => $i + 1,
+ invoiceid => $p->{invoiceid},
+ code => $p->{invoicenumber},
+ nullcode => $p->{invoicenumber} eq 'NULL',
+ emptycode => $p->{invoicenumber} eq q{},
+ raw_datereceived => $p->{shipmentdate},
+ datereceived => $p->{shipmentdate},
+ bibcount => $p->{receivedbiblios} || 0,
+ reccount => $p->{receiveditems} || 0,
+ itemcount => $p->{itemsexpected} || 0,
+ };
+}
+if ($count_parcels) {
+ $template->param( searchresults => $loopres, count => $count_parcels );
+}