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