4196b52815168fd8245b2721a51d717024b8a2be
[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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23 use strict;
24 use warnings;
25 use CGI;
26 use C4::Auth;
27 use C4::Output;
28 use C4::Context;
29 use C4::Acquisition;
30 use C4::Biblio;
31 use C4::Bookseller;
32 use C4::Items;
33 use C4::Search;
34 use List::MoreUtils qw/any/;
35
36 my $input=new CGI;
37 my $flagsrequired = {acquisition => 'order_receive'};
38
39 checkauth($input, 0, $flagsrequired, 'intranet');
40
41 my $user             = $input->remote_user;
42 my $biblionumber     = $input->param('biblionumber');
43 my $ordernumber      = $input->param('ordernumber');
44 my $origquantityrec  = $input->param('origquantityrec');
45 my $quantityrec      = $input->param('quantityrec');
46 my $quantity         = $input->param('quantity');
47 my $unitprice        = $input->param('cost');
48 my $invoiceid        = $input->param('invoiceid');
49 my $invoice          = GetInvoice($invoiceid);
50 my $invoiceno        = $invoice->{invoicenumber};
51 my $datereceived     = $invoice->{shipmentdate};
52 my $booksellerid     = $input->param('booksellerid');
53 my $cnt              = 0;
54 my $ecost            = $input->param('ecost');
55 my $rrp              = $input->param('rrp');
56 my $note             = $input->param("note");
57 my $bookfund         = $input->param("bookfund");
58 my $order            = GetOrder($ordernumber);
59
60 #need old recievedate if we update the order, parcel.pl only shows the right parcel this way FIXME
61 if ($quantityrec > $origquantityrec ) {
62     my @received_items = ();
63     if(C4::Context->preference('AcqCreateItem') eq 'ordering') {
64         @received_items = $input->param('items_to_receive');
65     }
66
67     $order->{rrp} = $rrp;
68     $order->{ecost} = $ecost;
69     $order->{unitprice} = $unitprice;
70     my $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
71     if ( $bookseller->{listincgst} ) {
72         if ( not $bookseller->{invoiceincgst} ) {
73             $order->{rrp} = $order->{rrp} * ( 1 + $order->{gstrate} );
74             $order->{ecost} = $order->{ecost} * ( 1 + $order->{gstrate} );
75             $order->{unitprice} = $order->{unitprice} * ( 1 + $order->{gstrate} );
76         }
77     } else {
78         if ( $bookseller->{invoiceincgst} ) {
79             $order->{rrp} = $order->{rrp} / ( 1 + $order->{gstrate} );
80             $order->{ecost} = $order->{ecost} / ( 1 + $order->{gstrate} );
81             $order->{unitprice} = $order->{unitprice} / ( 1 + $order->{gstrate} );
82         }
83     }
84
85     my $new_ordernumber = $ordernumber;
86     # save the quantity received.
87     if ( $quantityrec > 0 ) {
88         ($datereceived, $new_ordernumber) = ModReceiveOrder(
89             $biblionumber,
90             $ordernumber,
91             $quantityrec,
92             $user,
93             $order->{unitprice},
94             $order->{ecost},
95             $invoiceid,
96             $order->{rrp},
97             $bookfund,
98             $datereceived,
99             \@received_items,
100         );
101     }
102
103     # now, add items if applicable
104     if (C4::Context->preference('AcqCreateItem') eq 'receiving') {
105
106         my @tags         = $input->param('tag');
107         my @subfields    = $input->param('subfield');
108         my @field_values = $input->param('field_value');
109         my @serials      = $input->param('serial');
110         my @itemid       = $input->param('itemid');
111         my @ind_tag      = $input->param('ind_tag');
112         my @indicator    = $input->param('indicator');
113         #Rebuilding ALL the data for items into a hash
114         # parting them on $itemid.
115         my %itemhash;
116         my $countdistinct;
117         my $range=scalar(@itemid);
118         for (my $i=0; $i<$range; $i++){
119             unless ($itemhash{$itemid[$i]}){
120             $countdistinct++;
121             }
122             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
123             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
124             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
125             push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
126             push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
127         }
128         foreach my $item (keys %itemhash){
129             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
130                                           $itemhash{$item}->{'subfields'},
131                                           $itemhash{$item}->{'field_values'},
132                                           $itemhash{$item}->{'ind_tag'},
133                                           $itemhash{$item}->{'indicator'},'ITEM');
134             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
135             my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
136             NewOrderItem($itemnumber, $new_ordernumber);
137         }
138     }
139
140 }
141
142 update_item( $_ ) foreach GetItemnumbersFromOrder( $ordernumber );
143
144 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");
145
146 ################################ End of script ################################
147
148 sub update_item {
149     my ( $itemnumber ) = @_;
150
151     ModItem( {
152         booksellerid         => $booksellerid,
153         dateaccessioned      => $datereceived,
154         price                => $unitprice,
155         replacementprice     => $rrp,
156         replacementpricedate => $datereceived,
157     }, $biblionumber, $itemnumber );
158 }