Merge remote-tracking branch 'origin/new/bug_8597'
[koha.git] / acqui / invoice.pl
1 #!/usr/bin/perl
2
3 # Copyright 2011 BibLibre SARL
4 # This file is part of Koha.
5 #
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.
10 #
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.
14 #
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.
18
19 =head1 NAME
20
21 invoice.pl
22
23 =head1 DESCRIPTION
24
25 Invoice details
26
27 =cut
28
29 use strict;
30 use warnings;
31
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;
38
39 my $input = new CGI;
40 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
41     {
42         template_name   => 'acqui/invoice.tmpl',
43         query           => $input,
44         type            => 'intranet',
45         authnotrequired => 0,
46         flagsrequired   => { 'acquisition' => '*' },
47         debug           => 1,
48     }
49 );
50
51 my $invoiceid = $input->param('invoiceid');
52 my $op        = $input->param('op');
53
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;
60     }
61 }
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;
68     }
69 }
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
81     );
82     $template->param( modified => 1 );
83 }
84
85 my $details     = GetInvoiceDetails($invoiceid);
86 my $bookseller  = GetBookSellerFromId( $details->{booksellerid} );
87 my @orders_loop = ();
88 my $orders      = $details->{'orders'};
89 my $qty_total;
90 my @books_loop;
91 my @book_foot_loop;
92 my %foot;
93 my $total_quantity = 0;
94 my $total_rrp      = 0;
95 my $total_est      = 0;
96
97 foreach my $order (@$orders) {
98     my $line = get_infos( $order, $bookseller );
99
100     $total_quantity += $$line{quantity};
101     $total_rrp      += $order->{quantity} * $order->{rrp};
102     $total_est      += $order->{quantity} * $order->{'ecost'};
103
104     my %row = ( %$order, %$line );
105     push @orders_loop, \%row;
106 }
107
108 my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
109 my $discount =
110   $bookseller->{'discount'} ? ( $bookseller->{discount} / 100 ) : 0;
111 my $total_est_gste;
112 my $total_est_gsti;
113 my $total_rrp_gsti;    # RRP Total, GST included
114 my $total_rrp_gste;    # RRP Total, GST excluded
115 my $gist_est;
116 my $gist_rrp;
117 if ($gist) {
118
119     # if we have GST
120     if ( $bookseller->{'listincgst'} ) {
121
122         # if prices already includes GST
123
124         # we know $total_rrp_gsti
125         $total_rrp_gsti = $total_rrp;
126
127         # and can reverse compute other values
128         $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
129
130         $gist_rrp       = $total_rrp_gsti - $total_rrp_gste;
131         $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
132         $total_est_gsti = $total_est;
133     }
134     else {
135         # if prices does not include GST
136
137         # then we use the common way to compute other values
138         $total_rrp_gste = $total_rrp;
139         $gist_rrp       = $total_rrp_gste * $gist;
140         $total_rrp_gsti = $total_rrp_gste + $gist_rrp;
141         $total_est_gste = $total_est;
142         $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
143     }
144     $gist_est = $gist_rrp - ( $gist_rrp * $discount );
145 }
146 else {
147     $total_rrp_gste = $total_rrp_gsti = $total_rrp;
148     $total_est_gste = $total_est_gsti = $total_est;
149     $gist_rrp       = $gist_est       = 0;
150 }
151 my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
152
153 my $format = "%.2f";
154 $template->param(
155     total_rrp_gste      => sprintf( $format, $total_rrp_gste ),
156     total_rrp_gsti      => sprintf( $format, $total_rrp_gsti ),
157     total_est_gste      => sprintf( $format, $total_est_gste ),
158     total_est_gsti      => sprintf( $format, $total_est_gsti ),
159     gist_rrp            => sprintf( $format, $gist_rrp ),
160     gist_est            => sprintf( $format, $gist_est ),
161     total_gsti_shipment => sprintf( $format, $total_gsti_shipment ),
162     gist                => sprintf( $format, $gist * 100 ),
163 );
164
165 my $budgets = GetBudgets();
166 my @budgets_loop;
167 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
168 foreach my $budget (@$budgets) {
169     next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
170     my %line = %{$budget};
171     if (    $shipmentcost_budgetid
172         and $budget->{budget_id} == $shipmentcost_budgetid )
173     {
174         $line{selected} = 1;
175     }
176     push @budgets_loop, \%line;
177 }
178
179 $template->param(
180     invoiceid        => $details->{'invoiceid'},
181     invoicenumber    => $details->{'invoicenumber'},
182     suppliername     => $details->{'suppliername'},
183     supplierid       => $details->{'booksellerid'},
184     datereceived     => $details->{'datereceived'},
185     shipmentdate     => $details->{'shipmentdate'},
186     billingdate      => $details->{'billingdate'},
187     invoiceclosedate => $details->{'closedate'},
188     shipmentcost     => sprintf( $format, $details->{'shipmentcost'} || 0 ),
189     orders_loop      => \@orders_loop,
190     total_quantity   => $total_quantity,
191     invoiceincgst    => $bookseller->{invoiceincgst},
192     currency         => $bookseller->{listprice},
193     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
194     budgets_loop             => \@budgets_loop,
195 );
196
197 sub get_infos {
198     my $order      = shift;
199     my $bookseller = shift;
200     my $qty        = $order->{'quantity'} || 0;
201     if ( !defined $order->{quantityreceived} ) {
202         $order->{quantityreceived} = 0;
203     }
204     my $budget = GetBudget( $order->{'budget_id'} );
205
206     my %line = %{$order};
207     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
208     $line{budget_name}    = $budget->{budget_name};
209     $line{total}          = $qty * $order->{ecost};
210
211     if ( $line{uncertainprice} ) {
212         $line{rrp} .= ' (Uncertain)';
213     }
214     if ( $line{'title'} ) {
215         my $volume      = $order->{'volume'};
216         my $seriestitle = $order->{'seriestitle'};
217         $line{'title'} .= " / $seriestitle" if $seriestitle;
218         $line{'title'} .= " / $volume"      if $volume;
219     }
220     else {
221         $line{'title'} = "Deleted bibliographic notice, can't find title.";
222     }
223
224     return \%line;
225 }
226
227 output_html_with_http_headers $input, $cookie, $template->output;