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