3 #script to add a new item and to mark orders as received
4 #written 1/3/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>.
25 use C4::Auth qw( checkauth );
26 use JSON qw( encode_json );
29 use C4::Acquisition qw( GetInvoice GetOrder ModReceiveOrder );
30 use C4::Biblio qw( GetFrameworkCode GetMarcFromKohaField TransformHtmlToXml );
31 use C4::Items qw( GetMarcItem ModItemFromMarc AddItemFromMarc );
32 use C4::Log qw(logaction);
35 use Koha::Number::Price;
36 use Koha::Acquisition::Booksellers;
37 use Koha::Acquisition::Orders;
41 my $flagsrequired = {acquisition => 'order_receive'};
43 checkauth($input, 0, $flagsrequired, 'intranet');
45 my $user = $input->remote_user;
46 my $biblionumber = $input->param('biblionumber');
47 my $ordernumber = $input->param('ordernumber');
48 my $origquantityrec = $input->param('origquantityrec');
49 my $quantityrec = $input->param('quantityrec');
50 my $quantity = $input->param('quantity');
51 my $unitprice = $input->param('unitprice');
52 my $replacementprice = $input->param('replacementprice');
53 my $datereceived = $input->param('datereceived');
54 my $invoiceid = $input->param('invoiceid');
55 my $invoice = GetInvoice($invoiceid);
56 my $invoiceno = $invoice->{invoicenumber};
57 my $booksellerid = $input->param('booksellerid');
59 my $bookfund = $input->param("bookfund");
60 my $suggestion_id = $input->param("suggestionid");
61 my $order = GetOrder($ordernumber);
62 my $new_ordernumber = $ordernumber;
64 #bug18723 regression fix
65 if (C4::Context->preference("CurrencyFormat") eq 'FR') {
66 if (rindex($unitprice, '.') ge 0) {
67 substr($unitprice, rindex($unitprice, '.'), 1, ',');
69 if (rindex($replacementprice,'.') ge 0) {
70 substr($replacementprice, rindex($replacementprice, '.'), 1, ',');
74 $unitprice = Koha::Number::Price->new( $unitprice )->unformat();
75 $replacementprice = Koha::Number::Price->new( $replacementprice )->unformat();
76 my $order_obj = Koha::Acquisition::Orders->find( $ordernumber );
77 my $basket = $order_obj->basket;
79 #need old receivedate if we update the order, parcel.pl only shows the right parcel this way FIXME
80 if ($quantityrec > $origquantityrec ) {
81 my @received_items = ();
82 if ($basket->effective_create_items eq 'ordering') {
83 @received_items = $input->multi_param('items_to_receive');
84 my @affects = split q{\|}, C4::Context->preference("AcqItemSetSubfieldsWhenReceived");
86 my $frameworkcode = GetFrameworkCode($biblionumber);
87 my ( $itemfield ) = GetMarcFromKohaField( 'items.itemnumber' );
88 for my $in ( @received_items ) {
89 my $item = C4::Items::GetMarcItem( $biblionumber, $in );
90 for my $affect ( @affects ) {
91 my ( $sf, $v ) = split q{=}, $affect, 2;
92 foreach ( $item->field($itemfield) ) {
93 $_->update( $sf => $v );
96 C4::Items::ModItemFromMarc( $item, $biblionumber, $in );
101 $order_obj->order_internalnote(scalar $input->param("order_internalnote"));
102 $order_obj->tax_rate_on_receiving(scalar $input->param("tax_rate"));
103 $order_obj->replacementprice($replacementprice);
104 $order_obj->unitprice($unitprice);
106 $order_obj->populate_with_prices_for_receiving();
108 # save the quantity received.
109 if ( $quantityrec > 0 ) {
110 if ( $order_obj->subscriptionid ) {
111 # Quantity can only be modified if linked to a subscription
112 $order_obj->quantity($quantity); # quantityrec will be deduced from this value in ModReceiveOrder
114 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
116 biblionumber => $biblionumber,
117 order => $order_obj->unblessed,
118 quantityreceived => $quantityrec,
121 budget_id => $bookfund,
122 datereceived => $datereceived,
123 received_items => \@received_items,
128 # now, add items if applicable
129 if ($basket->effective_create_items eq 'receiving') {
131 my @tags = $input->multi_param('tag');
132 my @subfields = $input->multi_param('subfield');
133 my @field_values = $input->multi_param('field_value');
134 my @serials = $input->multi_param('serial');
135 my @itemid = $input->multi_param('itemid');
136 #Rebuilding ALL the data for items into a hash
137 # parting them on $itemid.
140 my $range=scalar(@itemid);
141 for (my $i=0; $i<$range; $i++){
142 unless ($itemhash{$itemid[$i]}){
145 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
146 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
147 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
149 my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
150 foreach my $item (keys %itemhash){
151 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
152 $itemhash{$item}->{'subfields'},
153 $itemhash{$item}->{'field_values'},
157 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
158 my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
159 $new_order->add_item( $itemnumber );
164 my $new_order_object = Koha::Acquisition::Orders->find( $new_ordernumber ); # FIXME we should not need to refetch it
165 my $items = $new_order_object->items;
166 while ( my $item = $items->next ) {
168 booksellerid => $booksellerid,
169 dateaccessioned => $datereceived,
170 datelastseen => $datereceived,
172 replacementprice => $replacementprice,
173 replacementpricedate => $datereceived,
177 if ($suggestion_id) {
178 my $reason = $input->param("reason") || '';
179 my $other_reason = $input->param("other_reason");
180 $reason = $other_reason if $reason eq 'other';
181 my $suggestion = Koha::Suggestions->find($suggestion_id);
182 $suggestion->update( { reason => $reason } ) if $suggestion;
186 if (C4::Context->preference("AcquisitionLog")) {
188 quantityrec => $quantityrec,
189 bookfund => $bookfund || 'unchanged',
190 tax_rate => $input->param("tax_rate"),
191 replacementprice => $replacementprice,
192 unitprice => $unitprice
203 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");