+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);
+ }
+}