Merge remote-tracking branch 'origin/new/bug_6448'
[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::Items;
32 use C4::Search;
33 use List::MoreUtils qw/any/;
34
35 my $input=new CGI;
36 my $flagsrequired = {acquisition => 'order_receive'};
37
38 checkauth($input, 0, $flagsrequired, 'intranet');
39
40 my $user=$input->remote_user;
41 my $biblionumber = $input->param('biblionumber');
42 my $biblioitemnumber=$input->param('biblioitemnumber');
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 $replacement=$input->param('rrp');
53 my $gst=$input->param('gst');
54 my $booksellerid = $input->param('booksellerid');
55 my $cnt=0;
56 my $ecost = $input->param('ecost');
57 my $note = $input->param("note");
58
59 #need old recievedate if we update the order, parcel.pl only shows the right parcel this way FIXME
60 if ($quantityrec > $origquantityrec ) {
61     my @received_items = ();
62     if(C4::Context->preference('AcqCreateItem') eq 'ordering') {
63         @received_items = $input->param('items_to_receive');
64     }
65
66     my $new_ordernumber = $ordernumber;
67     # save the quantity received.
68     if ( $quantityrec > 0 ) {
69         ($datereceived, $new_ordernumber) = ModReceiveOrder(
70             $biblionumber, $ordernumber, $quantityrec, $user, $unitprice,
71             $invoiceid, $replacement, undef, $datereceived, \@received_items);
72     }
73
74     # now, add items if applicable
75     if (C4::Context->preference('AcqCreateItem') eq 'receiving') {
76
77         my @tags         = $input->param('tag');
78         my @subfields    = $input->param('subfield');
79         my @field_values = $input->param('field_value');
80         my @serials      = $input->param('serial');
81         my @itemid       = $input->param('itemid');
82         my @ind_tag      = $input->param('ind_tag');
83         my @indicator    = $input->param('indicator');
84         #Rebuilding ALL the data for items into a hash
85         # parting them on $itemid.
86         my %itemhash;
87         my $countdistinct;
88         my $range=scalar(@itemid);
89         for (my $i=0; $i<$range; $i++){
90             unless ($itemhash{$itemid[$i]}){
91             $countdistinct++;
92             }
93             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
94             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
95             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
96             push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
97             push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
98         }
99         foreach my $item (keys %itemhash){
100             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
101                                           $itemhash{$item}->{'subfields'},
102                                           $itemhash{$item}->{'field_values'},
103                                           $itemhash{$item}->{'ind_tag'},
104                                           $itemhash{$item}->{'indicator'},'ITEM');
105             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
106             my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
107             NewOrderItem($itemnumber, $new_ordernumber);
108         }
109     }
110 }
111
112 update_item( $_ ) foreach GetItemnumbersFromOrder( $ordernumber );
113
114 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");
115
116 ################################ End of script ################################
117
118 sub update_item {
119     my ( $itemnumber ) = @_;
120
121     ModItem( {
122         booksellerid         => $booksellerid,
123         dateaccessioned      => $datereceived,
124         price                => $unitprice,
125         replacementprice     => $replacement,
126         replacementpricedate => $datereceived,
127     }, $biblionumber, $itemnumber );
128 }