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