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