+ 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} };
+ }
+ }
+ } else {
+ # 3rd add order
+ my $patron = Koha::Patrons->find( $loggedinuser );
+ # get quantity in the MARC record (1 if none)
+ my $quantity = GetMarcQuantity($marcrecord, C4::Context->preference('marcflavour')) || 1;
+ my %orderinfo = (
+ biblionumber => $biblionumber,
+ basketno => $cgiparams->{'basketno'},
+ quantity => $c_quantity,
+ branchcode => $patron->branchcode,
+ budget_id => $c_budget_id,
+ uncertainprice => 1,
+ sort1 => $c_sort1,
+ sort2 => $c_sort2,
+ order_internalnote => $cgiparams->{'all_order_internalnote'},
+ order_vendornote => $cgiparams->{'all_order_vendornote'},
+ currency => $cgiparams->{'all_currency'},
+ );
+ # get the price if there is one.
+ my $price= shift( @prices ) || GetMarcPrice($marcrecord, C4::Context->preference('marcflavour'));
+ 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} * $c_quantity;
+ } else {
+ $orderinfo{listprice} = 0;
+ }