Bug 11369 - Updating the jquery.cookie.js-plugin
[koha.git] / acqui / invoice.pl
blob5971ed161a2e5465cf3195e0cdc2d71aec9df5e2
1 #!/usr/bin/perl
3 # Copyright 2011 BibLibre SARL
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 =head1 NAME
21 invoice.pl
23 =head1 DESCRIPTION
25 Invoice details
27 =cut
29 use strict;
30 use warnings;
32 use CGI;
33 use C4::Auth;
34 use C4::Output;
35 use C4::Acquisition;
36 use C4::Bookseller qw/GetBookSellerFromId/;
37 use C4::Budgets;
39 my $input = new CGI;
40 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
42 template_name => 'acqui/invoice.tmpl',
43 query => $input,
44 type => 'intranet',
45 authnotrequired => 0,
46 flagsrequired => { 'acquisition' => '*' },
47 debug => 1,
51 my $invoiceid = $input->param('invoiceid');
52 my $op = $input->param('op');
54 if ( $op && $op eq 'close' ) {
55 CloseInvoice($invoiceid);
56 my $referer = $input->param('referer');
57 if ($referer) {
58 print $input->redirect($referer);
59 exit 0;
62 elsif ( $op && $op eq 'reopen' ) {
63 ReopenInvoice($invoiceid);
64 my $referer = $input->param('referer');
65 if ($referer) {
66 print $input->redirect($referer);
67 exit 0;
70 elsif ( $op && $op eq 'mod' ) {
71 my $shipmentdate = $input->param('shipmentdate');
72 my $billingdate = $input->param('billingdate');
73 my $shipmentcost = $input->param('shipmentcost');
74 my $shipment_budget_id = $input->param('shipment_budget_id');
75 ModInvoice(
76 invoiceid => $invoiceid,
77 shipmentdate => C4::Dates->new($shipmentdate)->output("iso"),
78 billingdate => C4::Dates->new($billingdate)->output("iso"),
79 shipmentcost => $shipmentcost,
80 shipmentcost_budgetid => $shipment_budget_id
82 if ($input->param('reopen')) {
83 ReopenInvoice($invoiceid);
84 } elsif ($input->param('close')) {
85 CloseInvoice($invoiceid);
86 } elsif ($input->param('merge')) {
87 my @sources = $input->param('merge');
88 MergeInvoices($invoiceid, \@sources);
90 $template->param( modified => 1 );
92 elsif ( $op && $op eq 'delete' ) {
93 DelInvoice($invoiceid);
94 my $referer = $input->param('referer') || 'invoices.pl';
95 if ($referer) {
96 print $input->redirect($referer);
97 exit 0;
101 my $details = GetInvoiceDetails($invoiceid);
102 my $bookseller = GetBookSellerFromId( $details->{booksellerid} );
103 my @orders_loop = ();
104 my $orders = $details->{'orders'};
105 my $qty_total;
106 my @books_loop;
107 my @book_foot_loop;
108 my %foot;
109 my $total_quantity = 0;
110 my $total_rrp = 0;
111 my $total_est = 0;
113 foreach my $order (@$orders) {
114 my $line = get_infos( $order, $bookseller );
116 $total_quantity += $$line{quantity};
117 $total_rrp += $order->{quantity} * $order->{rrp};
118 $total_est += $order->{quantity} * $order->{'ecost'};
120 my %row = ( %$order, %$line );
121 push @orders_loop, \%row;
124 my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
125 my $discount =
126 $bookseller->{'discount'} ? ( $bookseller->{discount} / 100 ) : 0;
127 my $total_est_gste;
128 my $total_est_gsti;
129 my $total_rrp_gsti; # RRP Total, GST included
130 my $total_rrp_gste; # RRP Total, GST excluded
131 my $gist_est;
132 my $gist_rrp;
133 if ($gist) {
135 # if we have GST
136 if ( $bookseller->{'listincgst'} ) {
138 # if prices already includes GST
140 # we know $total_rrp_gsti
141 $total_rrp_gsti = $total_rrp;
143 # and can reverse compute other values
144 $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
146 $gist_rrp = $total_rrp_gsti - $total_rrp_gste;
147 $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
148 $total_est_gsti = $total_est;
150 else {
151 # if prices does not include GST
153 # then we use the common way to compute other values
154 $total_rrp_gste = $total_rrp;
155 $gist_rrp = $total_rrp_gste * $gist;
156 $total_rrp_gsti = $total_rrp_gste + $gist_rrp;
157 $total_est_gste = $total_est;
158 $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
160 $gist_est = $gist_rrp - ( $gist_rrp * $discount );
162 else {
163 $total_rrp_gste = $total_rrp_gsti = $total_rrp;
164 $total_est_gste = $total_est_gsti = $total_est;
165 $gist_rrp = $gist_est = 0;
167 my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
169 my $format = "%.2f";
170 $template->param(
171 total_rrp_gste => sprintf( $format, $total_rrp_gste ),
172 total_rrp_gsti => sprintf( $format, $total_rrp_gsti ),
173 total_est_gste => sprintf( $format, $total_est_gste ),
174 total_est_gsti => sprintf( $format, $total_est_gsti ),
175 gist_rrp => sprintf( $format, $gist_rrp ),
176 gist_est => sprintf( $format, $gist_est ),
177 total_gsti_shipment => sprintf( $format, $total_gsti_shipment ),
178 gist => sprintf( $format, $gist * 100 ),
181 my $budgets = GetBudgets();
182 my @budgets_loop;
183 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
184 foreach my $budget (@$budgets) {
185 next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
186 my %line = %{$budget};
187 if ( $shipmentcost_budgetid
188 and $budget->{budget_id} == $shipmentcost_budgetid )
190 $line{selected} = 1;
192 push @budgets_loop, \%line;
195 $template->param(
196 invoiceid => $details->{'invoiceid'},
197 invoicenumber => $details->{'invoicenumber'},
198 suppliername => $details->{'suppliername'},
199 booksellerid => $details->{'booksellerid'},
200 datereceived => $details->{'datereceived'},
201 shipmentdate => $details->{'shipmentdate'},
202 billingdate => $details->{'billingdate'},
203 invoiceclosedate => $details->{'closedate'},
204 shipmentcost => sprintf( $format, $details->{'shipmentcost'} || 0 ),
205 orders_loop => \@orders_loop,
206 total_quantity => $total_quantity,
207 invoiceincgst => $bookseller->{invoiceincgst},
208 currency => $bookseller->{listprice},
209 budgets_loop => \@budgets_loop,
212 sub get_infos {
213 my $order = shift;
214 my $bookseller = shift;
215 my $qty = $order->{'quantity'} || 0;
216 if ( !defined $order->{quantityreceived} ) {
217 $order->{quantityreceived} = 0;
219 my $budget = GetBudget( $order->{'budget_id'} );
221 my %line = %{$order};
222 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
223 $line{budget_name} = $budget->{budget_name};
224 $line{total} = $qty * $order->{ecost};
226 if ( $line{uncertainprice} ) {
227 $line{rrp} .= ' (Uncertain)';
229 if ( $line{'title'} ) {
230 my $volume = $order->{'volume'};
231 my $seriestitle = $order->{'seriestitle'};
232 $line{'title'} .= " / $seriestitle" if $seriestitle;
233 $line{'title'} .= " / $volume" if $volume;
235 else {
236 $line{'title'} = "Deleted bibliographic notice, can't find title.";
239 return \%line;
242 output_html_with_http_headers $input, $cookie, $template->output;