Bug 5335 - More granular VAT
[koha-equinox.git] / acqui / orderreceive.pl
1 #!/usr/bin/perl
2
3
4 #script to recieve orders
5 #written by chris@katipo.co.nz 24/2/2000
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 =head1 NAME
25
26 orderreceive.pl
27
28 =head1 DESCRIPTION
29
30 This script shows all order already receive and all pendings orders.
31 It permit to write a new order as 'received'.
32
33 =head1 CGI PARAMETERS
34
35 =over 4
36
37 =item booksellerid
38
39 to know on what supplier this script has to display receive order.
40
41 =item receive
42
43 =item invoiceid
44
45 the id of this invoice.
46
47 =item freight
48
49 =item biblio
50
51 The biblionumber of this order.
52
53 =item datereceived
54
55 =item catview
56
57 =item gst
58
59 =back
60
61 =cut
62
63 use strict;
64 use warnings;
65
66 use CGI;
67 use C4::Context;
68 use C4::Koha;   # GetKohaAuthorisedValues GetItemTypes
69 use C4::Acquisition;
70 use C4::Auth;
71 use C4::Output;
72 use C4::Dates qw/format_date/;
73 use C4::Bookseller qw/ GetBookSellerFromId /;
74 use C4::Budgets qw/ GetBudget /;
75 use C4::Members;
76 use C4::Branch;    # GetBranches
77 use C4::Items;
78 use C4::Biblio;
79 use C4::Suggestions;
80
81
82 my $input      = new CGI;
83
84 my $dbh          = C4::Context->dbh;
85 my $invoiceid    = $input->param('invoiceid');
86 my $invoice      = GetInvoice($invoiceid);
87 my $booksellerid   = $invoice->{booksellerid};
88 my $freight      = $invoice->{shipmentcost};
89 my $datereceived = $invoice->{shipmentdate};
90 my $ordernumber  = $input->param('ordernumber');
91 my $search       = $input->param('receive');
92
93 $datereceived = $datereceived ? C4::Dates->new($datereceived, 'iso') : C4::Dates->new();
94
95 my $bookseller = GetBookSellerFromId($booksellerid);
96 my $results = SearchOrder($ordernumber,$search);
97
98 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
99     {
100         template_name   => "acqui/orderreceive.tmpl",
101         query           => $input,
102         type            => "intranet",
103         authnotrequired => 0,
104         flagsrequired   => {acquisition => 'order_receive'},
105         debug           => 1,
106     }
107 );
108
109 my $count = scalar @$results;
110 # prepare the form for receiving
111 if ( $count == 1 ) {
112     my $order = $results->[0];
113
114     # Check if ACQ framework exists
115     my $acq_fw = GetMarcStructure(1, 'ACQ');
116     unless($acq_fw) {
117         $template->param('NoACQframework' => 1);
118     }
119
120     my $AcqCreateItem = C4::Context->preference('AcqCreateItem');
121     if ($AcqCreateItem eq 'receiving') {
122         $template->param(
123             AcqCreateItemReceiving => 1,
124             UniqueItemFields => C4::Context->preference('UniqueItemFields'),
125         );
126     } elsif ($AcqCreateItem eq 'ordering') {
127         my $fw = ($acq_fw) ? 'ACQ' : '';
128         my @itemnumbers = GetItemnumbersFromOrder($order->{ordernumber});
129         my @items;
130         foreach (@itemnumbers) {
131             my $item = GetItem($_);
132             if($item->{homebranch}) {
133                 $item->{homebranchname} = GetBranchName($item->{homebranch});
134             }
135             if($item->{holdingbranch}) {
136                 $item->{holdingbranchname} = GetBranchName($item->{holdingbranch});
137             }
138             if(my $code = GetAuthValCode("items.notforloan", $fw)) {
139                 $item->{notforloan} = GetKohaAuthorisedValueLib($code, $item->{notforloan});
140             }
141             if(my $code = GetAuthValCode("items.restricted", $fw)) {
142                 $item->{restricted} = GetKohaAuthorisedValueLib($code, $item->{restricted});
143             }
144             if(my $code = GetAuthValCode("items.location", $fw)) {
145                 $item->{location} = GetKohaAuthorisedValueLib($code, $item->{location});
146             }
147             if(my $code = GetAuthValCode("items.ccode", $fw)) {
148                 $item->{collection} = GetKohaAuthorisedValueLib($code, $item->{ccode});
149             }
150             if(my $code = GetAuthValCode("items.materials", $fw)) {
151                 $item->{materials} = GetKohaAuthorisedValueLib($code, $item->{materials});
152             }
153             my $itemtype = getitemtypeinfo($item->{itype});
154             $item->{itemtype} = $itemtype->{description};
155             push @items, $item;
156         }
157         $template->param(items => \@items);
158     }
159
160     $order->{quantityreceived} = '' if $order->{quantityreceived} == 0;
161     $order->{unitprice} = '' if $order->{unitprice} == 0;
162
163     my $rrp;
164     my $ecost;
165     my $unitprice;
166     if ( $bookseller->{listincgst} ) {
167         if ( $bookseller->{invoiceincgst} ) {
168             $rrp = $order->{rrp};
169             $ecost = $order->{ecost};
170             $unitprice = $order->{unitprice};
171         } else {
172             $rrp = $order->{rrp} / ( 1 + $order->{gstrate} );
173             $ecost = $order->{ecost} / ( 1 + $order->{gstrate} );
174             $unitprice = $order->{unitprice} / ( 1 + $order->{gstrate} );
175         }
176     } else {
177         if ( $bookseller->{invoiceincgst} ) {
178             $rrp = $order->{rrp} * ( 1 + $order->{gstrate} );
179             $ecost = $order->{ecost} * ( 1 + $order->{gstrate} );
180             $unitprice = $order->{unitprice} * ( 1 + $order->{gstrate} );
181         } else {
182             $rrp = $order->{rrp};
183             $ecost = $order->{ecost};
184             $unitprice = $order->{unitprice};
185         }
186      }
187
188     my $suggestion = GetSuggestionInfoFromBiblionumber($order->{biblionumber});
189
190     my $authorisedby = $order->{authorisedby};
191     my $member = GetMember( borrowernumber => $authorisedby );
192
193     my $budget = GetBudget( $order->{budget_id} );
194
195     $template->param(
196         AcqCreateItem         => $AcqCreateItem,
197         count                 => 1,
198         biblionumber          => $order->{'biblionumber'},
199         ordernumber           => $order->{'ordernumber'},
200         biblioitemnumber      => $order->{'biblioitemnumber'},
201         booksellerid          => $order->{'booksellerid'},
202         freight               => $freight,
203         gstrate               => $order->{gstrate} || $bookseller->{gstrate} || C4::Context->preference("gist") || 0,
204         name                  => $bookseller->{'name'},
205         date                  => format_date($order->{entrydate}),
206         title                 => $order->{'title'},
207         author                => $order->{'author'},
208         copyrightdate         => $order->{'copyrightdate'},
209         isbn                  => $order->{'isbn'},
210         seriestitle           => $order->{'seriestitle'},
211         bookfund              => $budget->{budget_name},
212         quantity              => $order->{'quantity'},
213         quantityreceivedplus1 => $order->{'quantityreceived'} + 1,
214         quantityreceived      => $order->{'quantityreceived'},
215         rrp                   => sprintf( "%.2f", $rrp ),
216         ecost                 => sprintf( "%.2f", $ecost ),
217         unitprice             => sprintf( "%.2f", $unitprice),
218         memberfirstname       => $member->{firstname} || "",
219         membersurname         => $member->{surname} || "",
220         invoiceid             => $invoice->{invoiceid},
221         invoice               => $invoice->{invoicenumber},
222         datereceived          => $datereceived->output(),
223         datereceived_iso      => $datereceived->output('iso'),
224         notes                 => $order->{notes},
225         suggestionid          => $suggestion->{suggestionid},
226         surnamesuggestedby    => $suggestion->{surnamesuggestedby},
227         firstnamesuggestedby  => $suggestion->{firstnamesuggestedby},
228     );
229 }
230 else {
231     my @loop;
232     for ( my $i = 0 ; $i < $count ; $i++ ) {
233         my %line = %{ @$results[$i] };
234
235         $line{invoice}      = $invoice->{invoicenumber};
236         $line{datereceived} = $datereceived->output();
237         $line{freight}      = $freight;
238         $line{gstrate}      = @$results[$i]->{'gstrate'} || $bookseller->{gstrate} || C4::Context->preference("gist") || 0;
239         $line{title}        = @$results[$i]->{'title'};
240         $line{author}       = @$results[$i]->{'author'};
241         $line{booksellerid} = $booksellerid;
242         push @loop, \%line;
243     }
244
245     $template->param(
246         loop         => \@loop,
247         booksellerid => $booksellerid,
248         invoiceid    => $invoice->{invoiceid},
249     );
250 }
251 my $op = $input->param('op');
252 if ($op and $op eq 'edit'){
253     $template->param(edit   =>   1);
254 }
255 output_html_with_http_headers $input, $cookie, $template->output;