Bug 30477: Add new UNIMARC installer translation files
[koha.git] / acqui / finishreceive.pl
1 #!/usr/bin/perl
2
3 #script to add a new item and to mark orders as received
4 #written 1/3/00 by chris@katipo.co.nz
5
6 # Copyright 2000-2002 Katipo Communications
7 #
8 # This file is part of Koha.
9 #
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.
14 #
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.
19 #
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>.
22
23 use Modern::Perl;
24 use CGI qw ( -utf8 );
25 use C4::Auth qw( checkauth );
26 use JSON qw( encode_json );
27 use C4::Output;
28 use C4::Context;
29 use C4::Acquisition qw( GetInvoice GetOrder populate_order_with_prices ModReceiveOrder );
30 use C4::Biblio qw( GetFrameworkCode GetMarcFromKohaField TransformHtmlToXml );
31 use C4::Items qw( GetMarcItem ModItemFromMarc AddItemFromMarc );
32 use C4::Log qw(logaction);
33 use C4::Search;
34
35 use Koha::Number::Price;
36 use Koha::Acquisition::Booksellers;
37 use Koha::Acquisition::Orders;
38
39
40 my $input=CGI->new;
41 my $flagsrequired = {acquisition => 'order_receive'};
42
43 checkauth($input, 0, $flagsrequired, 'intranet');
44
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');
58 my $cnt              = 0;
59 my $bookfund         = $input->param("bookfund");
60 my $suggestion_id    = $input->param("suggestionid");
61 my $order            = GetOrder($ordernumber);
62 my $new_ordernumber  = $ordernumber;
63
64 #bug18723 regression fix
65 if (C4::Context->preference("CurrencyFormat") eq 'FR') {
66     if (rindex($unitprice, '.') ge 0) {
67         substr($unitprice, rindex($unitprice, '.'), 1, ',');
68     }
69     if (rindex($replacementprice,'.') ge 0) {
70         substr($replacementprice, rindex($replacementprice, '.'), 1, ',');
71     }
72 }
73
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;
78
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");
85         if ( @affects ) {
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 );
94                     }
95                 }
96                 C4::Items::ModItemFromMarc( $item, $biblionumber, $in );
97             }
98         }
99     }
100
101     $order->{order_internalnote} = $input->param("order_internalnote");
102     $order->{tax_rate_on_receiving} = $input->param("tax_rate");
103     $order->{replacementprice} = $replacementprice;
104     $order->{unitprice} = $unitprice;
105
106     $order = C4::Acquisition::populate_order_with_prices(
107         {
108             order => $order,
109             booksellerid => $booksellerid,
110             receiving => 1
111         }
112     );
113
114     # save the quantity received.
115     if ( $quantityrec > 0 ) {
116         if ( $order_obj->subscriptionid ) {
117             # Quantity can only be modified if linked to a subscription
118             $order->{quantity} = $quantity; # quantityrec will be deduced from this value in ModReceiveOrder
119         }
120         ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
121             {
122                 biblionumber     => $biblionumber,
123                 order            => $order,
124                 quantityreceived => $quantityrec,
125                 user             => $user,
126                 invoice          => $invoice,
127                 budget_id        => $bookfund,
128                 datereceived     => $datereceived,
129                 received_items   => \@received_items,
130             }
131         );
132     }
133
134     # now, add items if applicable
135     if ($basket->effective_create_items eq 'receiving') {
136
137         my @tags         = $input->multi_param('tag');
138         my @subfields    = $input->multi_param('subfield');
139         my @field_values = $input->multi_param('field_value');
140         my @serials      = $input->multi_param('serial');
141         my @itemid       = $input->multi_param('itemid');
142         #Rebuilding ALL the data for items into a hash
143         # parting them on $itemid.
144         my %itemhash;
145         my $countdistinct;
146         my $range=scalar(@itemid);
147         for (my $i=0; $i<$range; $i++){
148             unless ($itemhash{$itemid[$i]}){
149             $countdistinct++;
150             }
151             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
152             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
153             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
154         }
155         my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
156         foreach my $item (keys %itemhash){
157             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
158                                           $itemhash{$item}->{'subfields'},
159                                           $itemhash{$item}->{'field_values'},
160                                           undef,
161                                           undef,
162                                           'ITEM' );
163             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
164             my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
165             $new_order->add_item( $itemnumber );
166         }
167     }
168 }
169
170 my $new_order_object = Koha::Acquisition::Orders->find( $new_ordernumber ); # FIXME we should not need to refetch it
171 my $items = $new_order_object->items;
172 while ( my $item = $items->next )  {
173     $item->update({
174         booksellerid => $booksellerid,
175         dateaccessioned => $datereceived,
176         datelastseen => $datereceived,
177         price => $unitprice,
178         replacementprice => $replacementprice,
179         replacementpricedate => $datereceived,
180     });
181 }
182
183 if ($suggestion_id) {
184     my $reason = $input->param("reason") || '';
185     my $other_reason = $input->param("other_reason");
186     $reason = $other_reason if $reason eq 'other';
187     my $suggestion = Koha::Suggestions->find($suggestion_id);
188     $suggestion->update( { reason => $reason } ) if $suggestion;
189 }
190
191 # Log the receipt
192 if (C4::Context->preference("AcquisitionLog")) {
193     my $infos = {
194         quantityrec      => $quantityrec,
195         bookfund         => $bookfund || 'unchanged',
196         tax_rate         => $input->param("tax_rate"),
197         replacementprice => $replacementprice,
198         unitprice        => $unitprice
199     };
200
201     logaction(
202         'ACQUISITIONS',
203         'RECEIVE_ORDER',
204         $ordernumber,
205         encode_json($infos)
206     );
207 }
208
209 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");