Bug 8179: Make partial receive work correctly
[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 $invoice_unitprice = $input->param('invoice_unitprice');
55 my $invoice_currency = $input->param('invoice_currency');
56 my $invoiceid        = $input->param('invoiceid');
57 my $invoice          = GetInvoice($invoiceid);
58 my $invoiceno        = $invoice->{invoicenumber};
59 my $booksellerid     = $input->param('booksellerid');
60 my $cnt              = 0;
61 my $bookfund         = $input->param("bookfund");
62 my $suggestion_id    = $input->param("suggestionid");
63 my $order            = GetOrder($ordernumber);
64 my $new_ordernumber  = $ordernumber;
65
66 #bug18723 regression fix
67 if (C4::Context->preference("CurrencyFormat") eq 'FR') {
68     if (rindex($unitprice, '.') ge 0) {
69         substr($unitprice, rindex($unitprice, '.'), 1, ',');
70     }
71     if (rindex($replacementprice,'.') ge 0) {
72         substr($replacementprice, rindex($replacementprice, '.'), 1, ',');
73     }
74 }
75
76 $unitprice = Koha::Number::Price->new( $unitprice )->unformat();
77 $replacementprice = Koha::Number::Price->new( $replacementprice )->unformat();
78 my $order_obj = Koha::Acquisition::Orders->find( $ordernumber );
79 my $basket = $order_obj->basket;
80
81 #need old receivedate if we update the order, parcel.pl only shows the right parcel this way FIXME
82 if ($quantityrec > $origquantityrec ) {
83     my @received_items = ();
84     if ($basket->effective_create_items eq 'ordering') {
85         @received_items = $input->multi_param('items_to_receive[]');
86         my @affects = split q{\|}, C4::Context->preference("AcqItemSetSubfieldsWhenReceived");
87         if ( @affects ) {
88             my $frameworkcode = GetFrameworkCode($biblionumber);
89             my ( $itemfield ) = GetMarcFromKohaField( 'items.itemnumber' );
90             for my $in ( @received_items ) {
91                 my $item = C4::Items::GetMarcItem( $biblionumber, $in );
92                 for my $affect ( @affects ) {
93                     my ( $sf, $v ) = split q{=}, $affect, 2;
94                     foreach ( $item->field($itemfield) ) {
95                         $_->update( $sf => $v );
96                     }
97                 }
98                 C4::Items::ModItemFromMarc( $item, $biblionumber, $in );
99             }
100         }
101     }
102
103     $order_obj->set(
104         {
105             order_internalnote    => scalar $input->param("order_internalnote"),
106             tax_rate_on_receiving => scalar $input->param("tax_rate"),
107             replacementprice      => $replacementprice,
108             unitprice             => $unitprice,
109             (
110                 $invoice_unitprice && $invoice_unitprice ne ''
111                 ? (
112                     invoice_unitprice => $invoice_unitprice,
113                     invoice_currency  => $invoice_currency,
114                   )
115                 : (
116                     invoice_unitprice => undef,
117                     invoice_currency  => undef,
118                 )
119             ),
120         }
121     );
122
123     $order_obj->populate_with_prices_for_receiving();
124
125     # save the quantity received.
126     if ( $quantityrec > 0 ) {
127         if ( $order_obj->subscriptionid ) {
128             # Quantity can only be modified if linked to a subscription
129             $order_obj->quantity($quantity); # quantityrec will be deduced from this value in ModReceiveOrder
130         }
131         ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
132             {
133                 biblionumber     => $biblionumber,
134                 order            => $order_obj->unblessed,
135                 quantityreceived => $quantityrec,
136                 user             => $user,
137                 invoice          => $invoice,
138                 budget_id        => $bookfund,
139                 datereceived     => $datereceived,
140                 received_items   => \@received_items,
141             }
142         );
143     }
144
145     # now, add items if applicable
146     if ($basket->effective_create_items eq 'receiving') {
147
148         my @tags         = $input->multi_param('tag');
149         my @subfields    = $input->multi_param('subfield');
150         my @field_values = $input->multi_param('field_value');
151         my @serials      = $input->multi_param('serial');
152         my @itemid       = $input->multi_param('itemid');
153         #Rebuilding ALL the data for items into a hash
154         # parting them on $itemid.
155         my %itemhash;
156         my $countdistinct;
157         my $range=scalar(@itemid);
158         for (my $i=0; $i<$range; $i++){
159             unless ($itemhash{$itemid[$i]}){
160             $countdistinct++;
161             }
162             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
163             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
164             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
165         }
166         my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
167         foreach my $item (keys %itemhash){
168             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
169                                           $itemhash{$item}->{'subfields'},
170                                           $itemhash{$item}->{'field_values'},
171                                           undef,
172                                           undef,
173                                           'ITEM' );
174             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
175             my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
176             $new_order->add_item( $itemnumber );
177         }
178     }
179 }
180
181 my $new_order_object = Koha::Acquisition::Orders->find( $new_ordernumber ); # FIXME we should not need to refetch it
182 my $items = $new_order_object->items;
183 while ( my $item = $items->next )  {
184     $item->update({
185         booksellerid => $booksellerid,
186         dateaccessioned => $datereceived,
187         datelastseen => $datereceived,
188         price => $unitprice,
189         replacementprice => $replacementprice,
190         replacementpricedate => $datereceived,
191     });
192 }
193
194 if ($suggestion_id) {
195     my $reason = $input->param("reason") || '';
196     my $other_reason = $input->param("other_reason");
197     $reason = $other_reason if $reason eq 'other';
198     my $suggestion = Koha::Suggestions->find($suggestion_id);
199     $suggestion->update( { reason => $reason } ) if $suggestion;
200 }
201
202 # Log the receipt
203 if (C4::Context->preference("AcquisitionLog")) {
204     my $infos = {
205         quantityrec      => $quantityrec,
206         bookfund         => $bookfund || 'unchanged',
207         tax_rate         => $input->param("tax_rate"),
208         replacementprice => $replacementprice,
209         unitprice        => $unitprice,
210         (
211             defined $invoice_unitprice && $invoice_unitprice ne ''
212             ? (
213                 invoice_unitprice => $invoice_unitprice,
214                 invoice_currency  => $invoice_currency,
215               )
216             : ()
217         ),
218     };
219
220     logaction(
221         'ACQUISITIONS',
222         'RECEIVE_ORDER',
223         $ordernumber,
224         encode_json($infos)
225     );
226 }
227
228 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");