Merge remote-tracking branch 'origin/new/bug_8209'
[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     if ($input->param('reopen')) {
83         ReopenInvoice($invoiceid);
84     } elsif ($input->param('close')) {
85         CloseInvoice($invoiceid);
86     }
87     $template->param( modified => 1 );
88 }
89
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;
101
102 foreach my $order (@$orders) {
103     my $line = get_infos( $order, $bookseller );
104
105     $total_quantity += $$line{quantity};
106     $total_rrp      += $order->{quantity} * $order->{rrp};
107     $total_est      += $order->{quantity} * $order->{'ecost'};
108
109     my %row = ( %$order, %$line );
110     push @orders_loop, \%row;
111 }
112
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) {
123
124     # if we have GST
125     if ( $bookseller->{'listincgst'} ) {
126
127         # if prices already includes GST
128
129         # we know $total_rrp_gsti
130         $total_rrp_gsti = $total_rrp;
131
132         # and can reverse compute other values
133         $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
134
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;
138     }
139     else {
140         # if prices does not include GST
141
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 );
148     }
149     $gist_est = $gist_rrp - ( $gist_rrp * $discount );
150 }
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;
155 }
156 my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
157
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 ),
168 );
169
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 )
178     {
179         $line{selected} = 1;
180     }
181     push @budgets_loop, \%line;
182 }
183
184 $template->param(
185     invoiceid        => $details->{'invoiceid'},
186     invoicenumber    => $details->{'invoicenumber'},
187     suppliername     => $details->{'suppliername'},
188     supplierid       => $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     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
199     budgets_loop             => \@budgets_loop,
200 );
201
202 sub get_infos {
203     my $order      = shift;
204     my $bookseller = shift;
205     my $qty        = $order->{'quantity'} || 0;
206     if ( !defined $order->{quantityreceived} ) {
207         $order->{quantityreceived} = 0;
208     }
209     my $budget = GetBudget( $order->{'budget_id'} );
210
211     my %line = %{$order};
212     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
213     $line{budget_name}    = $budget->{budget_name};
214     $line{total}          = $qty * $order->{ecost};
215
216     if ( $line{uncertainprice} ) {
217         $line{rrp} .= ' (Uncertain)';
218     }
219     if ( $line{'title'} ) {
220         my $volume      = $order->{'volume'};
221         my $seriestitle = $order->{'seriestitle'};
222         $line{'title'} .= " / $seriestitle" if $seriestitle;
223         $line{'title'} .= " / $volume"      if $volume;
224     }
225     else {
226         $line{'title'} = "Deleted bibliographic notice, can't find title.";
227     }
228
229     return \%line;
230 }
231
232 output_html_with_http_headers $input, $cookie, $template->output;