Merge remote-tracking branch 'kc/new/bug_6521' into kcmaster
[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 my ($loggedinuser, $cookie, $sessionID) = checkauth($input, 0, $flagsrequired, 'intranet');
38 my $user=$input->remote_user;
39 my $biblionumber = $input->param('biblionumber');
40 my $biblioitemnumber=$input->param('biblioitemnumber');
41 my $ordernumber=$input->param('ordernumber');
42 my $origquantityrec=$input->param('origquantityrec');
43 my $quantityrec=$input->param('quantityrec');
44 my $quantity=$input->param('quantity');
45 my $unitprice=$input->param('cost');
46 my $invoiceno=$input->param('invoice');
47 my $datereceived=$input->param('datereceived');
48 my $replacement=$input->param('rrp');
49 my $gst=$input->param('gst');
50 my $freight=$input->param('freight');
51 my $supplierid = $input->param('supplierid');
52 my $cnt=0;
53 my $error_url_str;
54 my $ecost = $input->param('ecost');
55 my $note = $input->param("note");
56
57 my %tplorder = ( 'quantity'                  =>     $input->param('quantity') || '',
58                              'quantityreceived'   =>     $input->param('quantityrec') || '',
59                              'notes'                      =>      $input->param("note") || '',
60                              'rrp'                          =>      $input->param('rrp') || '',
61                              'ecost'                      =>      $input->param('ecost') || '',
62                              'unitprice'                =>      $input->param('cost') || '',
63                      );
64 my $order = GetOrder($ordernumber);
65 if ( any { $order->{$_} ne $tplorder{$_} } qw(quantity quantityreceived notes rrp ecost unitprice) ) {
66     $order->{quantity} = $tplorder{quantity} if $tplorder{quantity};
67     $order->{quantityreceived} = $tplorder{quantityreceived} if $tplorder{quantityreceived};
68     $order->{notes} = $tplorder{notes} if $tplorder{notes};
69     $order->{rrp} = $tplorder{rrp} if $tplorder{rrp};
70     $order->{ecost} = $tplorder{ecost} if $tplorder{ecost};
71     $order->{unitprice} = $tplorder{unitprice} if $tplorder{unitprice};
72     ModOrder($order);
73 }
74
75 #need old recievedate if we update the order, parcel.pl only shows the right parcel this way FIXME
76 if ($quantityrec > $origquantityrec ) {
77     # now, add items if applicable
78     if (C4::Context->preference('AcqCreateItem') eq 'receiving') {
79         my @tags         = $input->param('tag');
80         my @subfields    = $input->param('subfield');
81         my @field_values = $input->param('field_value');
82         my @serials      = $input->param('serial');
83         my @itemid       = $input->param('itemid');
84         my @ind_tag      = $input->param('ind_tag');
85         my @indicator    = $input->param('indicator');
86         #Rebuilding ALL the data for items into a hash
87         # parting them on $itemid.
88         my %itemhash;
89         my $countdistinct;
90         my $range=scalar(@itemid);
91         for (my $i=0; $i<$range; $i++){
92             unless ($itemhash{$itemid[$i]}){
93             $countdistinct++;
94             }
95             push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
96             push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
97             push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
98             push @{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
99             push @{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
100         }
101         foreach my $item (keys %itemhash){
102             my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
103                                     $itemhash{$item}->{'subfields'},
104                                     $itemhash{$item}->{'field_values'},
105                                     $itemhash{$item}->{'ind_tag'},
106                                     $itemhash{$item}->{'indicator'},'ITEM');
107             my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
108             my ($biblionumber,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
109         }
110     }
111     
112     # save the quantity received.
113         if( $quantityrec > 0 ) {
114         $datereceived = ModReceiveOrder($biblionumber,$ordernumber, $quantityrec ,$user,$unitprice,$invoiceno,$freight,$replacement,undef,$datereceived);
115         }
116 }
117     print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoice=$invoiceno&supplierid=$supplierid&freight=$freight&gst=$gst&datereceived=$datereceived$error_url_str");