+ # Add items from MarcItemFieldsToOrder
+ my @homebranches = $input->multi_param('homebranch_' . $biblio_count);
+ my $count = scalar @homebranches;
+ my @holdingbranches = $input->multi_param('holdingbranch_' . $biblio_count);
+ my @itypes = $input->multi_param('itype_' . $biblio_count);
+ my @nonpublic_notes = $input->multi_param('nonpublic_note_' . $biblio_count);
+ my @public_notes = $input->multi_param('public_note_' . $biblio_count);
+ my @locs = $input->multi_param('loc_' . $biblio_count);
+ my @ccodes = $input->multi_param('ccode_' . $biblio_count);
+ my @notforloans = $input->multi_param('notforloan_' . $biblio_count);
+ my @uris = $input->multi_param('uri_' . $biblio_count);
+ my @copynos = $input->multi_param('copyno_' . $biblio_count);
+ my @budget_codes = $input->multi_param('budget_code_' . $biblio_count);
+ my @itemprices = $input->multi_param('itemprice_' . $biblio_count);
+ my @replacementprices = $input->multi_param('replacementprice_' . $biblio_count);
+ my @itemcallnumbers = $input->multi_param('itemcallnumber_' . $biblio_count);
+ my $itemcreation = 0;
+
+ my @itemnumbers;
+ for (my $i = 0; $i < $count; $i++) {
+ $itemcreation = 1;
+ my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({
+ homebranch => $homebranches[$i],
+ holdingbranch => $holdingbranches[$i],
+ itemnotes_nonpublic => $nonpublic_notes[$i],
+ itemnotes => $public_notes[$i],
+ location => $locs[$i],
+ ccode => $ccodes[$i],
+ itype => $itypes[$i],
+ notforloan => $notforloans[$i],
+ uri => $uris[$i],
+ copynumber => $copynos[$i],
+ price => $itemprices[$i],
+ replacementprice => $replacementprices[$i],
+ itemcallnumber => $itemcallnumbers[$i],
+ }, $biblionumber);
+ push( @itemnumbers, $itemnumber );
+ }
+ if ($itemcreation == 1) {
+ # Group orderlines from MarcItemFieldsToOrder
+ my $budget_hash;
+ for (my $i = 0; $i < $count; $i++) {
+ $budget_hash->{$budget_codes[$i]}->{quantity} += 1;
+ $budget_hash->{$budget_codes[$i]}->{price} = $itemprices[$i];
+ $budget_hash->{$budget_codes[$i]}->{itemnumbers} //= [];
+ push @{ $budget_hash->{$budget_codes[$i]}->{itemnumbers} }, $itemnumbers[$i];
+ }
+
+ # Create orderlines from MarcItemFieldsToOrder
+ while(my ($budget_id, $infos) = each %$budget_hash) {
+ if ($budget_id) {
+ my %orderinfo = (
+ biblionumber => $biblionumber,
+ basketno => $cgiparams->{'basketno'},
+ quantity => $infos->{quantity},
+ budget_id => $budget_id,
+ currency => $cgiparams->{'all_currency'},
+ );
+
+ my $price = $infos->{price};
+ my $replacementprice = $infos->{replacementprice};
+ if ($price){
+ # in France, the cents separator is the , but sometimes, ppl use a .
+ # in this case, the price will be x100 when unformatted ! Replace the . by a , to get a proper price calculation
+ $price =~ s/\./,/ if C4::Context->preference("CurrencyFormat") eq "FR";
+ $price = Koha::Number::Price->new($price)->unformat;
+ $orderinfo{tax_rate} = $bookseller->tax_rate;
+ my $c = $c_discount ? $c_discount : $bookseller->discount / 100;
+ $orderinfo{discount} = $c;
+ if ( $c_discount ) {
+ $orderinfo{ecost} = $price;
+ $orderinfo{rrp} = $orderinfo{ecost} / ( 1 - $c );
+ } else {
+ $orderinfo{ecost} = $price * ( 1 - $c );
+ $orderinfo{rrp} = $price;
+ }
+ $orderinfo{listprice} = $orderinfo{rrp} / $active_currency->rate;
+ $orderinfo{unitprice} = $orderinfo{ecost};
+ $orderinfo{total} = $orderinfo{ecost} * $infos->{quantity};
+ } else {
+ $orderinfo{listprice} = 0;
+ }
+ $orderinfo{replacementprice} = $replacementprice || 0;
+
+ # remove uncertainprice flag if we have found a price in the MARC record
+ $orderinfo{uncertainprice} = 0 if $orderinfo{listprice};
+
+ %orderinfo = %{
+ C4::Acquisition::populate_order_with_prices(
+ {
+ order => \%orderinfo,
+ booksellerid => $booksellerid,
+ ordering => 1,
+ receiving => 1,
+ }
+ )
+ };
+
+ my $order = Koha::Acquisition::Order->new( \%orderinfo )->store;
+ $order->add_item( $_ ) for @{ $budget_hash->{$budget_id}->{itemnumbers} };
+ }