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>.
25 use C4::Auth qw( checkauth );
26 use JSON qw( encode_json );
29 use C4::Acquisition qw( GetInvoice GetOrder ModReceiveOrder );
30 use C4::Biblio qw( GetFrameworkCode GetMarcFromKohaField TransformHtmlToXml );
31 use C4::Items qw( GetMarcItem ModItemFromMarc AddItemFromMarc );
32 use C4::Log qw(logaction);
35 use Koha::Number::Price;
36 use Koha::Acquisition::Booksellers;
37 use Koha::Acquisition::Orders;
41 my $flagsrequired = {acquisition => 'order_receive'};
43 checkauth($input, 0, $flagsrequired, 'intranet');
45 my $user = $input->remote_user;
46 my $biblionumber = $input->param('biblionumber');
47 my $ordernumber = $input->param('ordernumber');
48 my $origquantityrec = $input->param('origquantityrec');
49 my $quantityrec = $input->param('quantityrec');
50 my $quantity = $input->param('quantity');
51 my $unitprice = $input->param('unitprice');
52 my $replacementprice = $input->param('replacementprice');
53 my $datereceived = $input->param('datereceived');
54 my $invoice_unitprice = $input->param('invoice_unitprice');
55 my $invoice_currency = $input->param('invoice_currency');
56 my $invoiceid = $input->param('invoiceid');
57 my $invoice = GetInvoice($invoiceid);
58 my $invoiceno = $invoice->{invoicenumber};
59 my $booksellerid = $input->param('booksellerid');
61 my $bookfund = $input->param("bookfund");
62 my $suggestion_id = $input->param("suggestionid");
63 my $order = GetOrder($ordernumber);
64 my $new_ordernumber = $ordernumber;
66 #bug18723 regression fix
67 if (C4::Context->preference("CurrencyFormat") eq 'FR') {
68 if (rindex($unitprice, '.') ge 0) {
69 substr($unitprice, rindex($unitprice, '.'), 1, ',');
71 if (rindex($replacementprice,'.') ge 0) {
72 substr($replacementprice, rindex($replacementprice, '.'), 1, ',');
76 $unitprice = Koha::Number::Price->new( $unitprice )->unformat();
77 $replacementprice = Koha::Number::Price->new( $replacementprice )->unformat();
78 my $order_obj = Koha::Acquisition::Orders->find( $ordernumber );
79 my $basket = $order_obj->basket;
81 #need old receivedate if we update the order, parcel.pl only shows the right parcel this way FIXME
82 if ($quantityrec > $origquantityrec ) {
83 my @received_items = ();
84 if ($basket->effective_create_items eq 'ordering') {
85 @received_items = $input->multi_param('items_to_receive[]');
86 my @affects = split q{\|}, C4::Context->preference("AcqItemSetSubfieldsWhenReceived");
88 my $frameworkcode = GetFrameworkCode($biblionumber);
89 my ( $itemfield ) = GetMarcFromKohaField( 'items.itemnumber' );
90 for my $in ( @received_items ) {
91 my $item = C4::Items::GetMarcItem( $biblionumber, $in );
92 for my $affect ( @affects ) {
93 my ( $sf, $v ) = split q{=}, $affect, 2;
94 foreach ( $item->field($itemfield) ) {
95 $_->update( $sf => $v );
98 C4::Items::ModItemFromMarc( $item, $biblionumber, $in );
105 order_internalnote => scalar $input->param("order_internalnote"),
106 tax_rate_on_receiving => scalar $input->param("tax_rate"),
107 replacementprice => $replacementprice,
108 unitprice => $unitprice,
110 $invoice_unitprice && $invoice_unitprice ne ''
112 invoice_unitprice => $invoice_unitprice,
113 invoice_currency => $invoice_currency,
116 invoice_unitprice => undef,
117 invoice_currency => undef,
123 $order_obj->populate_with_prices_for_receiving();
125 # save the quantity received.
126 if ( $quantityrec > 0 ) {
127 if ( $order_obj->subscriptionid ) {
128 # Quantity can only be modified if linked to a subscription
129 $order_obj->quantity($quantity); # quantityrec will be deduced from this value in ModReceiveOrder
131 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
133 biblionumber => $biblionumber,
134 order => $order_obj->unblessed,
135 quantityreceived => $quantityrec,
138 budget_id => $bookfund,
139 datereceived => $datereceived,
140 received_items => \@received_items,
145 # now, add items if applicable
146 if ($basket->effective_create_items eq 'receiving') {
148 my @tags = $input->multi_param('tag[]');
149 my @subfields = $input->multi_param('subfield[]');
150 my @field_values = $input->multi_param('field_value[]');
151 my @serials = $input->multi_param('serial[]');
152 my @itemid = $input->multi_param('itemid[]');
153 #Rebuilding ALL the data for items into a hash
154 # parting them on $itemid.
157 my $range=scalar(@itemid);
158 for (my $i=0; $i<$range; $i++){
159 unless ($itemhash{$itemid[$i]}){
162 push @{$itemhash{$itemid[$i]}->{'tags'}},$tags[$i];
163 push @{$itemhash{$itemid[$i]}->{'subfields'}},$subfields[$i];
164 push @{$itemhash{$itemid[$i]}->{'field_values'}},$field_values[$i];
166 my $new_order = Koha::Acquisition::Orders->find( $new_ordernumber );
167 foreach my $item (keys %itemhash){
168 my $xml = TransformHtmlToXml( $itemhash{$item}->{'tags'},
169 $itemhash{$item}->{'subfields'},
170 $itemhash{$item}->{'field_values'},
174 my $record=MARC::Record::new_from_xml($xml, 'UTF-8');
175 my (undef,$bibitemnum,$itemnumber) = AddItemFromMarc($record,$biblionumber);
176 $new_order->add_item( $itemnumber );
181 my $new_order_object = Koha::Acquisition::Orders->find( $new_ordernumber ); # FIXME we should not need to refetch it
182 my $items = $new_order_object->items;
183 while ( my $item = $items->next ) {
185 booksellerid => $booksellerid,
186 dateaccessioned => $datereceived,
187 datelastseen => $datereceived,
189 replacementprice => $replacementprice,
190 replacementpricedate => $datereceived,
194 if ($suggestion_id) {
195 my $reason = $input->param("reason") || '';
196 my $other_reason = $input->param("other_reason");
197 $reason = $other_reason if $reason eq 'other';
198 my $suggestion = Koha::Suggestions->find($suggestion_id);
199 $suggestion->update( { reason => $reason } ) if $suggestion;
203 if (C4::Context->preference("AcquisitionLog")) {
205 quantityrec => $quantityrec,
206 bookfund => $bookfund || 'unchanged',
207 tax_rate => $input->param("tax_rate"),
208 replacementprice => $replacementprice,
209 unitprice => $unitprice,
211 defined $invoice_unitprice && $invoice_unitprice ne ''
213 invoice_unitprice => $invoice_unitprice,
214 invoice_currency => $invoice_currency,
228 print $input->redirect("/cgi-bin/koha/acqui/parcel.pl?invoiceid=$invoiceid");