Bug 10080: (followup) change two last instances of the old syspref name
[koha.git] / acqui / invoice.pl
blob1b6d97c72fe8b6692a977f8581d08d95a5b9e8df
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);
87 $template->param( modified => 1 );
90 my $details = GetInvoiceDetails($invoiceid);
91 my $bookseller = GetBookSellerFromId( $details->{booksellerid} );
92 my @orders_loop = ();
93 my $orders = $details->{'orders'};
94 my $qty_total;
95 my @books_loop;
96 my @book_foot_loop;
97 my %foot;
98 my $total_quantity = 0;
99 my $total_rrp = 0;
100 my $total_est = 0;
102 foreach my $order (@$orders) {
103 my $line = get_infos( $order, $bookseller );
105 $total_quantity += $$line{quantity};
106 $total_rrp += $order->{quantity} * $order->{rrp};
107 $total_est += $order->{quantity} * $order->{'ecost'};
109 my %row = ( %$order, %$line );
110 push @orders_loop, \%row;
113 my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
114 my $discount =
115 $bookseller->{'discount'} ? ( $bookseller->{discount} / 100 ) : 0;
116 my $total_est_gste;
117 my $total_est_gsti;
118 my $total_rrp_gsti; # RRP Total, GST included
119 my $total_rrp_gste; # RRP Total, GST excluded
120 my $gist_est;
121 my $gist_rrp;
122 if ($gist) {
124 # if we have GST
125 if ( $bookseller->{'listincgst'} ) {
127 # if prices already includes GST
129 # we know $total_rrp_gsti
130 $total_rrp_gsti = $total_rrp;
132 # and can reverse compute other values
133 $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
135 $gist_rrp = $total_rrp_gsti - $total_rrp_gste;
136 $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
137 $total_est_gsti = $total_est;
139 else {
140 # if prices does not include GST
142 # then we use the common way to compute other values
143 $total_rrp_gste = $total_rrp;
144 $gist_rrp = $total_rrp_gste * $gist;
145 $total_rrp_gsti = $total_rrp_gste + $gist_rrp;
146 $total_est_gste = $total_est;
147 $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
149 $gist_est = $gist_rrp - ( $gist_rrp * $discount );
151 else {
152 $total_rrp_gste = $total_rrp_gsti = $total_rrp;
153 $total_est_gste = $total_est_gsti = $total_est;
154 $gist_rrp = $gist_est = 0;
156 my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
158 my $format = "%.2f";
159 $template->param(
160 total_rrp_gste => sprintf( $format, $total_rrp_gste ),
161 total_rrp_gsti => sprintf( $format, $total_rrp_gsti ),
162 total_est_gste => sprintf( $format, $total_est_gste ),
163 total_est_gsti => sprintf( $format, $total_est_gsti ),
164 gist_rrp => sprintf( $format, $gist_rrp ),
165 gist_est => sprintf( $format, $gist_est ),
166 total_gsti_shipment => sprintf( $format, $total_gsti_shipment ),
167 gist => sprintf( $format, $gist * 100 ),
170 my $budgets = GetBudgets();
171 my @budgets_loop;
172 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
173 foreach my $budget (@$budgets) {
174 next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
175 my %line = %{$budget};
176 if ( $shipmentcost_budgetid
177 and $budget->{budget_id} == $shipmentcost_budgetid )
179 $line{selected} = 1;
181 push @budgets_loop, \%line;
184 $template->param(
185 invoiceid => $details->{'invoiceid'},
186 invoicenumber => $details->{'invoicenumber'},
187 suppliername => $details->{'suppliername'},
188 booksellerid => $details->{'booksellerid'},
189 datereceived => $details->{'datereceived'},
190 shipmentdate => $details->{'shipmentdate'},
191 billingdate => $details->{'billingdate'},
192 invoiceclosedate => $details->{'closedate'},
193 shipmentcost => sprintf( $format, $details->{'shipmentcost'} || 0 ),
194 orders_loop => \@orders_loop,
195 total_quantity => $total_quantity,
196 invoiceincgst => $bookseller->{invoiceincgst},
197 currency => $bookseller->{listprice},
198 budgets_loop => \@budgets_loop,
201 sub get_infos {
202 my $order = shift;
203 my $bookseller = shift;
204 my $qty = $order->{'quantity'} || 0;
205 if ( !defined $order->{quantityreceived} ) {
206 $order->{quantityreceived} = 0;
208 my $budget = GetBudget( $order->{'budget_id'} );
210 my %line = %{$order};
211 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
212 $line{budget_name} = $budget->{budget_name};
213 $line{total} = $qty * $order->{ecost};
215 if ( $line{uncertainprice} ) {
216 $line{rrp} .= ' (Uncertain)';
218 if ( $line{'title'} ) {
219 my $volume = $order->{'volume'};
220 my $seriestitle = $order->{'seriestitle'};
221 $line{'title'} .= " / $seriestitle" if $seriestitle;
222 $line{'title'} .= " / $volume" if $volume;
224 else {
225 $line{'title'} = "Deleted bibliographic notice, can't find title.";
228 return \%line;
231 output_html_with_http_headers $input, $cookie, $template->output;