3 #script to add an order into the system
4 #written 29/2/00 by chris@katipo.co.nz
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
30 this script allows to add an order.
35 =item neworderbiblio.pl to add an order from nothing.
37 =item neworderempty.pl to add an order from an existing biblio.
39 =item newordersuggestion.pl to add an order from an existing suggestion.
45 All of the cgi parameters below are related to the new order.
50 the number of this new order.
53 the number of this new basket
56 the bookseller the librarian has to pay.
61 the title of the record ordered.
64 the author of the record ordered.
66 =item C<copyrightdate>
67 the copyrightdate of the record ordered.
70 the ISBN of the record ordered.
75 the quantity to order.
78 the price of this order.
80 =item C<uncertainprice>
81 uncertain price, can't close basket until prices of all orders are known.
84 the branch where this order will be received.
92 budget_id used to pay this order.
94 =item C<sort1> & C<sort2>
109 the number of the invoice for this order.
111 =item C<publishercode>
113 =item C<suggestionid>
114 if it is an order from an existing suggestion : the id of this suggestion.
124 use CGI qw
( -utf8
);
125 use C4
::Auth
; # get_template_and_user
126 use C4
::Acquisition
; # ModOrder
127 use C4
::Suggestions
; # ModStatus
128 use C4
::Biblio
; # AddBiblio TransformKohaToMarc
132 use Koha
::Acquisition
::Currencies
;
134 ### "-------------------- addorder.pl ----------"
136 # FIXME: This needs to do actual error checking and possibly return user to the same form,
137 # not just blindly call C4 functions and print a redirect.
141 # Check if order total amount exceed allowed budget
142 my $confirm_budget_exceeding = $input->param('confirm_budget_exceeding');
143 unless($confirm_budget_exceeding) {
144 my $budget_id = $input->param('budget_id');
145 my $total = $input->param('total');
146 my $budget = GetBudget
($budget_id);
147 my $budget_spent = GetBudgetSpent
($budget_id);
148 my $budget_ordered = GetBudgetOrdered
($budget_id);
149 my $budget_used = $budget_spent + $budget_ordered;
150 my $budget_remaining = $budget->{budget_amount
} - $budget_used;
151 my $budget_encumbrance = $budget->{budget_amount
} * $budget->{budget_encumb
} / 100;
152 my $budget_expenditure = $budget->{budget_expend
};
154 if ( $total > $budget_remaining
155 || ( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance)
156 || ( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure) )
158 my ($template, $loggedinuser, $cookie) = get_template_and_user
({
159 template_name
=> "acqui/addorder.tt",
162 authnotrequired
=> 0,
163 flagsrequired
=> {acquisition
=> 'order_manage'},
166 my $url = $input->referer();
167 unless ( defined $url ) {
168 my $basketno = $input->param('basketno');
169 $url = "/cgi-bin/koha/acqui/basket.pl?basketno=$basketno";
172 my $vars = $input->Vars;
174 foreach (keys %$vars) {
177 values => [$input->param($_)],
181 if( ($budget_encumbrance+0) && ($budget_used + $total) > $budget_encumbrance
182 && $total <= $budget_remaining)
185 encumbrance_exceeded
=> 1,
186 encumbrance
=> sprintf("%.2f", $budget->{'budget_encumb'}),
189 if( ($budget_expenditure+0) && ($budget_used + $total) > $budget_expenditure
190 && $total <= $budget_remaining )
192 my $currency = Koha
::Acquisition
::Currencies
->get_active;
194 expenditure_exceeded
=> 1,
195 expenditure
=> sprintf("%.2f", $budget_expenditure),
196 currency
=> ($currency) ?
$currency->symbol : '',
199 if($total > $budget_remaining){
200 $template->param(budget_exceeded
=> 1);
204 not_enough_budget
=> 1,
206 vars_loop
=> \
@vars_loop,
208 output_html_with_http_headers
$input, $cookie, $template->output;
213 # get_template_and_user used only to check auth & get user id
214 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
216 template_name
=> "acqui/booksellers.tt",
219 authnotrequired
=> 0,
220 flagsrequired
=> { acquisition
=> 'order_manage' },
226 my $orderinfo = $input->Vars;
227 $orderinfo->{'list_price'} ||= 0;
228 $orderinfo->{'uncertainprice'} ||= 0;
229 $orderinfo->{subscriptionid
} ||= undef;
231 my $user = $input->remote_user;
233 # create, modify or delete biblio
234 # create if $quantity>0 and $existing='no'
235 # modify if $quantity>0 and $existing='yes'
236 if ( $orderinfo->{quantity
} ne '0' ) {
237 #TODO:check to see if biblio exists
238 unless ( $$orderinfo{biblionumber
} ) {
239 #if it doesn't create it
240 my $record = TransformKohaToMarc
(
242 "biblio.title" => "$$orderinfo{title}",
243 "biblio.author" => $$orderinfo{author
} ?
$$orderinfo{author
} : "",
244 "biblio.seriestitle" => $$orderinfo{series
} ?
$$orderinfo{series
} : "",
245 "biblioitems.isbn" => $$orderinfo{isbn
} ?
$$orderinfo{isbn
} : "",
246 "biblioitems.ean" => $$orderinfo{ean
} ?
$$orderinfo{ean
} : "",
247 "biblioitems.publishercode" => $$orderinfo{publishercode
} ?
$$orderinfo{publishercode
} : "",
248 "biblioitems.publicationyear" => $$orderinfo{publicationyear
} ?
$$orderinfo{publicationyear
}: "",
249 "biblio.copyrightdate" => $$orderinfo{publicationyear
} ?
$$orderinfo{publicationyear
}: "",
250 "biblioitems.itemtype" => $$orderinfo{itemtype
} ?
$$orderinfo{itemtype
} : "",
251 "biblioitems.editionstatement"=> $$orderinfo{editionstatement
} ?
$$orderinfo{editionstatement
} : "",
254 C4
::Acquisition
::FillWithDefaultValues
( $record );
256 # create the record in catalogue, with framework ''
257 my ($biblionumber,$bibitemnum) = AddBiblio
($record,'');
258 # change suggestion status if applicable
259 if ($$orderinfo{suggestionid
}) {
260 ModSuggestion
( {suggestionid
=>$$orderinfo{suggestionid
}, STATUS
=>'ORDERED', biblionumber
=>$biblionumber} );
262 $orderinfo->{biblionumber
}=$biblionumber;
265 $orderinfo->{unitprice
} = $orderinfo->{ecost
} if not defined $orderinfo->{unitprice
} or $orderinfo->{unitprice
} eq '';
267 # if we already have $ordernumber, then it's an ordermodif
268 my $order = Koha
::Acquisition
::Order
->new($orderinfo);
269 if ( $orderinfo->{ordernumber
} ) {
270 ModOrder
($orderinfo);
271 my $order_users_ids = $input->param('users_ids');
272 my @order_users = split( /:/, $order_users_ids );
274 ModOrderUsers
( $orderinfo->{ordernumber
}, @order_users );
276 else { # else, it's a new line
280 # now, add items if applicable
281 if (C4
::Context
->preference('AcqCreateItem') eq 'ordering') {
283 my @tags = $input->param('tag');
284 my @subfields = $input->param('subfield');
285 my @field_values = $input->param('field_value');
286 my @serials = $input->param('serial');
287 my @itemid = $input->param('itemid');
288 my @ind_tag = $input->param('ind_tag');
289 my @indicator = $input->param('indicator');
290 #Rebuilding ALL the data for items into a hash
291 # parting them on $itemid.
295 my $range=scalar(@itemid);
296 for (my $i=0; $i<$range; $i++){
297 unless ($itemhash{$itemid[$i]}){
300 push @
{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
301 push @
{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
302 push @
{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
303 push @
{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
304 push @
{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
306 foreach my $item (keys %itemhash){
308 my $xml = TransformHtmlToXml
( $itemhash{$item}->{'tags'},
309 $itemhash{$item}->{'subfields'},
310 $itemhash{$item}->{'field_values'},
311 $itemhash{$item}->{'indicator'},
312 $itemhash{$item}->{'ind_tag'},
314 my $record=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
315 my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc
($record,$$orderinfo{biblionumber
});
316 $order->add_item($itemnumber);
322 my $basketno=$$orderinfo{basketno
};
323 my $booksellerid=$$orderinfo{booksellerid
};
324 if (my $import_batch_id=$$orderinfo{import_batch_id
}) {
325 print $input->redirect("/cgi-bin/koha/acqui/addorderiso2709.pl?import_batch_id=$import_batch_id&basketno=$basketno&booksellerid=$booksellerid");
326 } elsif ( defined $orderinfo->{invoiceid
} ) {
327 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=" . $orderinfo->{invoiceid
});
329 print $input->redirect("/cgi-bin/koha/acqui/basket.pl?basketno=$basketno");