Bug 30477: Add new UNIMARC installer translation files
[koha.git] / acqui / parcel.pl
1 #!/usr/bin/perl
2
3 #script to receive orders
4
5
6 # Copyright 2000-2002 Katipo Communications
7 # Copyright 2008-2009 BibLibre SARL
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 =head1 NAME
25
26 parcel.pl
27
28 =head1 DESCRIPTION
29
30 This script shows all orders receipt or pending for a given supplier.
31 It allows to write an order as 'received' when it arrives.
32
33 =head1 CGI PARAMETERS
34
35 =over 4
36
37 =item booksellerid
38
39 To know the supplier this script has to show orders.
40
41 =item code
42
43 is the bookseller invoice number.
44
45
46 =item gst
47
48
49 =item datereceived
50
51 To filter the results list on this given date.
52
53 =back
54
55 =cut
56
57 use Modern::Perl;
58
59 use C4::Auth qw( get_template_and_user );
60 use C4::Acquisition qw( CancelReceipt GetInvoice GetInvoiceDetails get_rounded_price );
61 use C4::Budgets qw( GetBudget GetBudgetByOrderNumber GetBudgetName );
62 use CGI qw ( -utf8 );
63 use C4::Output qw( output_html_with_http_headers );
64 use C4::Suggestions qw( GetSuggestion GetSuggestionInfoFromBiblionumber GetSuggestionInfo );
65
66 use Koha::Acquisition::Baskets;
67 use Koha::Acquisition::Bookseller;
68 use Koha::Acquisition::Orders;
69 use Koha::Biblios;
70
71
72 my $input = CGI->new;
73
74 my ($template, $loggedinuser, $cookie)
75     = get_template_and_user({template_name => "acqui/parcel.tt",
76                  query => $input,
77                  type => "intranet",
78                  flagsrequired => {acquisition => 'order_receive'},
79 });
80
81 my $op = $input->param('op') // '';
82
83 # process cancellation first so that list of
84 # orders to display is calculated after
85 if ($op eq 'cancelreceipt') {
86     my $ordernumber = $input->param('ordernumber');
87     my $parent_ordernumber = CancelReceipt($ordernumber);
88     unless($parent_ordernumber) {
89         $template->param(error_cancelling_receipt => 1);
90     }
91 }
92
93 my $invoiceid = $input->param('invoiceid');
94 my $invoice;
95 $invoice = GetInvoiceDetails($invoiceid) if $invoiceid;
96
97 unless( $invoiceid and $invoice->{invoiceid} ) {
98     $template->param(
99         error_invoice_not_known => 1,
100         no_orders_to_display    => 1
101     );
102     output_html_with_http_headers $input, $cookie, $template->output;
103     exit;
104 }
105
106 my $booksellerid = $invoice->{booksellerid};
107 my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
108
109 my @orders        = @{ $invoice->{orders} };
110 my $countlines    = scalar @orders;
111 my @loop_received = ();
112 my @book_foot_loop;
113 my %foot;
114 my $total_tax_excluded = 0;
115 my $total_tax_included = 0;
116
117 my $subtotal_for_funds;
118 for my $order ( @orders ) {
119     $order->{'unitprice'} += 0;
120
121     my $order_object = Koha::Acquisition::Orders->find($order->{ordernumber});
122     if ( $bookseller->invoiceincgst ) {
123         $order->{ecost}     = $order->{ecost_tax_included};
124         $order->{unitprice} = $order->{unitprice_tax_included};
125     }
126     else {
127         $order->{ecost}     = $order->{ecost_tax_excluded};
128         $order->{unitprice} = $order->{unitprice_tax_excluded};
129     }
130
131     $order->{total} = get_rounded_price($order->{unitprice}) * $order->{quantity};
132
133     my %line = %{ $order };
134     $line{invoice} = $invoice->{invoicenumber};
135     my @itemnumbers = $order_object->items->get_column('itemnumber');
136     my $biblio = Koha::Biblios->find( $line{biblionumber} );
137     $line{total_holds} = $biblio ? $biblio->holds->count : 0;
138     $line{item_holds} = $biblio ? $biblio->current_holds->search(
139         {
140             itemnumber => { -in => \@itemnumbers },
141         }
142     )->count : 0;
143     $line{budget} = GetBudgetByOrderNumber( $line{ordernumber} );
144
145     $line{tax_value} = $line{tax_value_on_receiving};
146     $line{tax_rate} = $line{tax_rate_on_receiving};
147     $foot{$line{tax_rate}}{tax_rate} = $line{tax_rate};
148     $foot{$line{tax_rate}}{tax_value} += $line{tax_value};
149     $total_tax_excluded += get_rounded_price($line{unitprice_tax_excluded}) * $line{quantity};
150     $total_tax_included += get_rounded_price($line{unitprice_tax_included}) * $line{quantity};
151
152     my $suggestion   = GetSuggestionInfoFromBiblionumber($line{biblionumber});
153     $line{suggestionid}         = $suggestion->{suggestionid};
154     $line{surnamesuggestedby}   = $suggestion->{surnamesuggestedby};
155     $line{firstnamesuggestedby} = $suggestion->{firstnamesuggestedby};
156
157     if ( $line{parent_ordernumber} != $line{ordernumber} ) {
158         if ( grep { $_->{ordernumber} == $line{parent_ordernumber} }
159             @orders
160             )
161         {
162             $line{cannot_cancel} = 1;
163         }
164     }
165
166     my $budget_name = GetBudgetName( $line{budget_id} );
167     $line{budget_name} = $budget_name;
168
169     $subtotal_for_funds->{ $line{budget_name} }{ecost} += get_rounded_price($order->{ecost}) * $order->{quantity};
170     $subtotal_for_funds->{ $line{budget_name} }{unitprice} += $order->{total};
171
172     push @loop_received, \%line;
173 }
174 push @book_foot_loop, map { $_ } values %foot;
175
176 $template->param(
177     invoiceid             => $invoice->{invoiceid},
178     invoice               => $invoice->{invoicenumber},
179     invoiceclosedate      => $invoice->{closedate},
180     shipmentdate         => $invoice->{shipmentdate},
181     name                  => $bookseller->name,
182     booksellerid          => $bookseller->id,
183     loop_received         => \@loop_received,
184     book_foot_loop        => \@book_foot_loop,
185     (uc(C4::Context->preference("marcflavour"))) => 1,
186     total_tax_excluded    => $total_tax_excluded,
187     total_tax_included    => $total_tax_included,
188     subtotal_for_funds    => $subtotal_for_funds,
189 );
190 output_html_with_http_headers $input, $cookie, $template->output;