Bug 10390: Add ability to delete empty invoices
[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 elsif ( $op && $op eq 'delete' ) {
90     DelInvoice($invoiceid);
91     my $referer = $input->param('referer') || 'invoices.pl';
92     if ($referer) {
93         print $input->redirect($referer);
94         exit 0;
95     }
96 }
97
98 my $details     = GetInvoiceDetails($invoiceid);
99 my $bookseller  = GetBookSellerFromId( $details->{booksellerid} );
100 my @orders_loop = ();
101 my $orders      = $details->{'orders'};
102 my $qty_total;
103 my @books_loop;
104 my @book_foot_loop;
105 my %foot;
106 my $total_quantity = 0;
107 my $total_rrp      = 0;
108 my $total_est      = 0;
109
110 foreach my $order (@$orders) {
111     my $line = get_infos( $order, $bookseller );
112
113     $total_quantity += $$line{quantity};
114     $total_rrp      += $order->{quantity} * $order->{rrp};
115     $total_est      += $order->{quantity} * $order->{'ecost'};
116
117     my %row = ( %$order, %$line );
118     push @orders_loop, \%row;
119 }
120
121 my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
122 my $discount =
123   $bookseller->{'discount'} ? ( $bookseller->{discount} / 100 ) : 0;
124 my $total_est_gste;
125 my $total_est_gsti;
126 my $total_rrp_gsti;    # RRP Total, GST included
127 my $total_rrp_gste;    # RRP Total, GST excluded
128 my $gist_est;
129 my $gist_rrp;
130 if ($gist) {
131
132     # if we have GST
133     if ( $bookseller->{'listincgst'} ) {
134
135         # if prices already includes GST
136
137         # we know $total_rrp_gsti
138         $total_rrp_gsti = $total_rrp;
139
140         # and can reverse compute other values
141         $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
142
143         $gist_rrp       = $total_rrp_gsti - $total_rrp_gste;
144         $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
145         $total_est_gsti = $total_est;
146     }
147     else {
148         # if prices does not include GST
149
150         # then we use the common way to compute other values
151         $total_rrp_gste = $total_rrp;
152         $gist_rrp       = $total_rrp_gste * $gist;
153         $total_rrp_gsti = $total_rrp_gste + $gist_rrp;
154         $total_est_gste = $total_est;
155         $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
156     }
157     $gist_est = $gist_rrp - ( $gist_rrp * $discount );
158 }
159 else {
160     $total_rrp_gste = $total_rrp_gsti = $total_rrp;
161     $total_est_gste = $total_est_gsti = $total_est;
162     $gist_rrp       = $gist_est       = 0;
163 }
164 my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
165
166 my $format = "%.2f";
167 $template->param(
168     total_rrp_gste      => sprintf( $format, $total_rrp_gste ),
169     total_rrp_gsti      => sprintf( $format, $total_rrp_gsti ),
170     total_est_gste      => sprintf( $format, $total_est_gste ),
171     total_est_gsti      => sprintf( $format, $total_est_gsti ),
172     gist_rrp            => sprintf( $format, $gist_rrp ),
173     gist_est            => sprintf( $format, $gist_est ),
174     total_gsti_shipment => sprintf( $format, $total_gsti_shipment ),
175     gist                => sprintf( $format, $gist * 100 ),
176 );
177
178 my $budgets = GetBudgets();
179 my @budgets_loop;
180 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
181 foreach my $budget (@$budgets) {
182     next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
183     my %line = %{$budget};
184     if (    $shipmentcost_budgetid
185         and $budget->{budget_id} == $shipmentcost_budgetid )
186     {
187         $line{selected} = 1;
188     }
189     push @budgets_loop, \%line;
190 }
191
192 $template->param(
193     invoiceid        => $details->{'invoiceid'},
194     invoicenumber    => $details->{'invoicenumber'},
195     suppliername     => $details->{'suppliername'},
196     booksellerid       => $details->{'booksellerid'},
197     datereceived     => $details->{'datereceived'},
198     shipmentdate     => $details->{'shipmentdate'},
199     billingdate      => $details->{'billingdate'},
200     invoiceclosedate => $details->{'closedate'},
201     shipmentcost     => sprintf( $format, $details->{'shipmentcost'} || 0 ),
202     orders_loop      => \@orders_loop,
203     total_quantity   => $total_quantity,
204     invoiceincgst    => $bookseller->{invoiceincgst},
205     currency         => $bookseller->{listprice},
206     budgets_loop             => \@budgets_loop,
207 );
208
209 sub get_infos {
210     my $order      = shift;
211     my $bookseller = shift;
212     my $qty        = $order->{'quantity'} || 0;
213     if ( !defined $order->{quantityreceived} ) {
214         $order->{quantityreceived} = 0;
215     }
216     my $budget = GetBudget( $order->{'budget_id'} );
217
218     my %line = %{$order};
219     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
220     $line{budget_name}    = $budget->{budget_name};
221     $line{total}          = $qty * $order->{ecost};
222
223     if ( $line{uncertainprice} ) {
224         $line{rrp} .= ' (Uncertain)';
225     }
226     if ( $line{'title'} ) {
227         my $volume      = $order->{'volume'};
228         my $seriestitle = $order->{'seriestitle'};
229         $line{'title'} .= " / $seriestitle" if $seriestitle;
230         $line{'title'} .= " / $volume"      if $volume;
231     }
232     else {
233         $line{'title'} = "Deleted bibliographic notice, can't find title.";
234     }
235
236     return \%line;
237 }
238
239 output_html_with_http_headers $input, $cookie, $template->output;