Bug 32779: Add tests
[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 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_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);
105
106     $order_obj->populate_with_prices_for_receiving();
107
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
113         }
114         ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
115             {
116                 biblionumber     => $biblionumber,
117                 order            => $order_obj->unblessed,
118                 quantityreceived => $quantityrec,
119                 user             => $user,
120                 invoice          => $invoice,
121                 budget_id        => $bookfund,
122                 datereceived     => $datereceived,
123                 received_items   => \@received_items,
124             }
125         );
126     }
127
128     # now, add items if applicable
129     if ($basket->effective_create_items eq 'receiving') {
130
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.
138         my %itemhash;
139         my $countdistinct;
140         my $range=scalar(@itemid);
141         for (my $i=0; $i<$range; $i++){
142             unless ($itemhash{$itemid[$i]}){
143             $countdistinct++;
144             }
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];
148         }
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'},
154                                           undef,
155                                           undef,
156                                           'ITEM' );
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 );
160         }
161     }
162 }
163
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 )  {
167     $item->update({
168         booksellerid => $booksellerid,
169         dateaccessioned => $datereceived,
170         datelastseen => $datereceived,
171         price => $unitprice,
172         replacementprice => $replacementprice,
173         replacementpricedate => $datereceived,
174     });
175 }
176
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;
183 }
184
185 # Log the receipt
186 if (C4::Context->preference("AcquisitionLog")) {
187     my $infos = {
188         quantityrec      => $quantityrec,
189         bookfund         => $bookfund || 'unchanged',
190         tax_rate         => $input->param("tax_rate"),
191         replacementprice => $replacementprice,
192         unitprice        => $unitprice
193     };
194
195     logaction(
196         'ACQUISITIONS',
197         'RECEIVE_ORDER',
198         $ordernumber,
199         encode_json($infos)
200     );
201 }
202
203 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");