3 #script to add a new item and to mark orders as received
4 #written 1/3/00 by chris@katipo.co.nz
6 # Copyright 2000-2002 Katipo Communications
8 # This file is part of Koha.
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.
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.
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>.
34 use List
::MoreUtils qw
/any/;
37 my $flagsrequired = {acquisition
=> 'order_receive'};
39 checkauth
($input, 0, $flagsrequired, 'intranet');
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');
54 my $ecost = $input->param('ecost');
55 my $rrp = $input->param('rrp');
56 my $order_internalnote = $input->param("order_internalnote");
57 my $bookfund = $input->param("bookfund");
58 my $order = GetOrder
($ordernumber);
59 my $new_ordernumber = $ordernumber;
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->param('items_to_receive');
66 my @affects = split q{\|}, C4
::Context
->preference("AcqItemSetSubfieldsWhenReceived");
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 );
78 C4
::Items
::ModItemFromMarc
( $item, $biblionumber, $in );
84 $order->{ecost
} = $ecost;
85 $order->{unitprice
} = $unitprice;
86 my $bookseller = C4
::Bookseller
::GetBookSellerFromId
($booksellerid);
87 if ( $bookseller->{listincgst
} ) {
88 if ( not $bookseller->{invoiceincgst
} ) {
89 $order->{rrp
} = $order->{rrp
} * ( 1 + $order->{gstrate
} );
90 $order->{ecost
} = $order->{ecost
} * ( 1 + $order->{gstrate
} );
91 $order->{unitprice
} = $order->{unitprice
} * ( 1 + $order->{gstrate
} );
94 if ( $bookseller->{invoiceincgst
} ) {
95 $order->{rrp
} = $order->{rrp
} / ( 1 + $order->{gstrate
} );
96 $order->{ecost
} = $order->{ecost
} / ( 1 + $order->{gstrate
} );
97 $order->{unitprice
} = $order->{unitprice
} / ( 1 + $order->{gstrate
} );
101 # save the quantity received.
102 if ( $quantityrec > 0 ) {
103 ($datereceived, $new_ordernumber) = ModReceiveOrder
({
104 biblionumber
=> $biblionumber,
105 ordernumber
=> $ordernumber,
106 quantityreceived
=> $quantityrec,
108 cost
=> $order->{unitprice
},
109 ecost
=> $order->{ecost
},
110 invoiceid
=> $invoiceid,
111 rrp
=> $order->{rrp
},
112 budget_id
=> $bookfund,
113 datereceived
=> $datereceived,
114 received_items
=> \
@received_items,
115 order_internalnote
=> $order_internalnote,
119 # now, add items if applicable
120 if (C4
::Context
->preference('AcqCreateItem') eq 'receiving') {
122 my @tags = $input->param('tag');
123 my @subfields = $input->param('subfield');
124 my @field_values = $input->param('field_value');
125 my @serials = $input->param('serial');
126 my @itemid = $input->param('itemid');
127 my @ind_tag = $input->param('ind_tag');
128 my @indicator = $input->param('indicator');
129 #Rebuilding ALL the data for items into a hash
130 # parting them on $itemid.
133 my $range=scalar(@itemid);
134 for (my $i=0; $i<$range; $i++){
135 unless ($itemhash{$itemid[$i]}){
138 push @
{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
139 push @
{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
140 push @
{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
141 push @
{$itemhash{$itemid[$i]}->{'ind_tag'}},$ind_tag[$i];
142 push @
{$itemhash{$itemid[$i]}->{'indicator'}},$indicator[$i];
144 my $order = Koha
::Acquisition
::Order
->fetch({ ordernumber
=> $new_ordernumber });
145 foreach my $item (keys %itemhash){
146 my $xml = TransformHtmlToXml
( $itemhash{$item}->{'tags'},
147 $itemhash{$item}->{'subfields'},
148 $itemhash{$item}->{'field_values'},
149 $itemhash{$item}->{'ind_tag'},
150 $itemhash{$item}->{'indicator'},'ITEM');
151 my $record=MARC
::Record
::new_from_xml
($xml, 'UTF-8');
152 my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc
($record,$biblionumber);
153 $order->add_item( $itemnumber );
160 booksellerid
=> $booksellerid,
161 dateaccessioned
=> $datereceived,
163 replacementprice
=> $rrp,
164 replacementpricedate
=> $datereceived,
168 ) foreach GetItemnumbersFromOrder
($new_ordernumber);
170 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid&sticky_filters=1");