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