Bug 15685: (QA follow-up) Fix typo
[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 strict;
24 use warnings;
25 use CGI qw ( -utf8 );
26 use C4::Auth;
27 use C4::Output;
28 use C4::Context;
29 use C4::Acquisition;
30 use C4::Biblio;
31 use C4::Items;
32 use C4::Search;
33
34 use Koha::Number::Price;
35 use Koha::Acquisition::Booksellers;
36 use Koha::Acquisition::Orders;
37
38 use List::MoreUtils qw/any/;
39
40 my $input=new CGI;
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 $datereceived     = $input->param('datereceived'),
53 my $invoiceid        = $input->param('invoiceid');
54 my $invoice          = GetInvoice($invoiceid);
55 my $invoiceno        = $invoice->{invoicenumber};
56 my $booksellerid     = $input->param('booksellerid');
57 my $cnt              = 0;
58 my $bookfund         = $input->param("bookfund");
59 my $order            = GetOrder($ordernumber);
60 my $new_ordernumber  = $ordernumber;
61
62 $unitprice = Koha::Number::Price->new( $unitprice )->unformat();
63 my $basket = Koha::Acquisition::Orders->find( $ordernumber )->basket;
64
65 #need old receivedate if we update the order, parcel.pl only shows the right parcel this way FIXME
66 if ($quantityrec > $origquantityrec ) {
67     my @received_items = ();
68     if ($basket->effective_create_items eq 'ordering') {
69         @received_items = $input->param('items_to_receive');
70         my @affects = split q{\|}, C4::Context->preference("AcqItemSetSubfieldsWhenReceived");
71         if ( @affects ) {
72             my $frameworkcode = GetFrameworkCode($biblionumber);
73             my ( $itemfield ) = GetMarcFromKohaField( 'items.itemnumber', $frameworkcode );
74             for my $in ( @received_items ) {
75                 my $item = C4::Items::GetMarcItem( $biblionumber, $in );
76                 for my $affect ( @affects ) {
77                     my ( $sf, $v ) = split q{=}, $affect, 2;
78                     foreach ( $item->field($itemfield) ) {
79                         $_->update( $sf => $v );
80                     }
81                 }
82                 C4::Items::ModItemFromMarc( $item, $biblionumber, $in );
83             }
84         }
85     }
86
87     $order->{order_internalnote} = $input->param("order_internalnote");
88     $order->{tax_rate_on_receiving} = $input->param("tax_rate");
89     $order->{unitprice} = $unitprice;
90
91     $order = C4::Acquisition::populate_order_with_prices(
92         {
93             order => $order,
94             booksellerid => $booksellerid,
95             receiving => 1
96         }
97     );
98
99     # save the quantity received.
100     if ( $quantityrec > 0 ) {
101         ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
102             {
103                 biblionumber     => $biblionumber,
104                 order            => $order,
105                 quantityreceived => $quantityrec,
106                 user             => $user,
107                 invoice          => $invoice,
108                 budget_id        => $bookfund,
109                 received_items   => \@received_items,
110             }
111         );
112     }
113
114     # now, add items if applicable
115     if ($basket->effective_create_items eq 'receiving') {
116
117         my @tags         = $input->multi_param('tag');
118         my @subfields    = $input->multi_param('subfield');
119         my @field_values = $input->multi_param('field_value');
120         my @serials      = $input->multi_param('serial');
121         my @itemid       = $input->multi_param('itemid');
122         my @ind_tag      = $input->multi_param('ind_tag');
123         my @indicator    = $input->multi_param('indicator');
124         #Rebuilding ALL the data for items into a hash
125         # parting them on $itemid.
126         my %itemhash;
127         my $countdistinct;
128         my $range=scalar(@itemid);
129         for (my $i=0; $i<$range; $i++){
130             unless ($itemhash{$itemid[$i]}){
131             $countdistinct++;
132             }
133             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
134             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
135             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
136             push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
137             push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
138         }
139         my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
140         foreach my $item (keys %itemhash){
141             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
142                                           $itemhash{$item}->{'subfields'},
143                                           $itemhash{$item}->{'field_values'},
144                                           $itemhash{$item}->{'indicator'},
145                                           $itemhash{$item}->{'ind_tag'},
146                                           'ITEM' );
147             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
148             my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
149             $new_order->add_item( $itemnumber );
150         }
151     }
152 }
153
154 ModItem(
155     {
156         booksellerid         => $booksellerid,
157         dateaccessioned      => $datereceived,
158         datelastseen         => $datereceived,
159         price                => $unitprice,
160         replacementprice     => $order->{rrp},
161         replacementpricedate => $datereceived,
162     },
163     $biblionumber,
164     $_
165 ) foreach GetItemnumbersFromOrder($new_ordernumber);
166
167 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid&sticky_filters=1");