Bug 12274: Invoice detail: don't crash on certain billing dates
[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     } elsif ($input->param('merge')) {
87         my @sources = $input->param('merge');
88         MergeInvoices($invoiceid, \@sources);
89     }
90     $template->param( modified => 1 );
91 }
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;
98     }
99 }
100
101
102 my $details = GetInvoiceDetails($invoiceid);
103 my $bookseller = GetBookSellerFromId($details->{booksellerid});
104 my @orders_loop = ();
105 my $orders = $details->{'orders'};
106 my $qty_total;
107 my @foot_loop;
108 my %foot;
109 my $total_quantity = 0;
110 my $total_gste = 0;
111 my $total_gsti = 0;
112 my $total_gstvalue = 0;
113 foreach my $order (@$orders) {
114     my $line = get_infos( $order, $bookseller);
115
116     $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
117     $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
118     $total_gstvalue += $$line{gstvalue};
119     $foot{$$line{gstgsti}}{quantity}  += $$line{quantity};
120     $total_quantity += $$line{quantity};
121     $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
122     $total_gste += $$line{totalgste};
123     $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
124     $total_gsti += $$line{totalgsti};
125
126     $line->{orderline} = $line->{parent_ordernumber};
127     push @orders_loop, $line;
128 }
129
130 push @foot_loop, map {$_} values %foot;
131
132 my $format = "%.2f";
133 my $budgets = GetBudgets();
134 my @budgets_loop;
135 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
136 foreach my $budget (@$budgets) {
137     next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
138     my %line = %{$budget};
139     if (    $shipmentcost_budgetid
140         and $budget->{budget_id} == $shipmentcost_budgetid )
141     {
142         $line{selected} = 1;
143     }
144     push @budgets_loop, \%line;
145 }
146
147 $template->param(
148     invoiceid        => $details->{'invoiceid'},
149     invoicenumber    => $details->{'invoicenumber'},
150     suppliername     => $details->{'suppliername'},
151     booksellerid     => $details->{'booksellerid'},
152     datereceived     => $details->{'datereceived'},
153     billingdate      => $details->{'billingdate'},
154     invoiceclosedate => $details->{'closedate'},
155     shipmentcost     => $details->{'shipmentcost'},
156     orders_loop      => \@orders_loop,
157     foot_loop        => \@foot_loop,
158     total_quantity   => $total_quantity,
159     total_gste       => sprintf( $format, $total_gste ),
160     total_gsti       => sprintf( $format, $total_gsti ),
161     total_gstvalue   => sprintf( $format, $total_gstvalue ),
162     total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
163     total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
164     invoiceincgst    => $bookseller->{invoiceincgst},
165     currency         => GetCurrency()->{currency},
166     budgets_loop     => \@budgets_loop,
167 );
168
169 # FIXME
170 # Fonction dupplicated from basket.pl
171 # Code must to be exported. Where ??
172 sub get_infos {
173     my $order = shift;
174     my $bookseller = shift;
175     my $qty = $order->{'quantity'} || 0;
176     if ( !defined $order->{quantityreceived} ) {
177         $order->{quantityreceived} = 0;
178     }
179     my $budget = GetBudget( $order->{'budget_id'} );
180
181     my %line = %{ $order };
182     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
183     $line{budget_name}    = $budget->{budget_name};
184     if ( $bookseller->{'listincgst'} ) {
185         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
186         $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
187         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} );
188         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) );
189         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
190         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
191         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
192     } else {
193         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
194         $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
195         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) );
196         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} );
197         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
198         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
199         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
200     }
201
202     if ( $line{uncertainprice} ) {
203         $template->param( uncertainprices => 1 );
204         $line{rrp} .= ' (Uncertain)';
205     }
206     if ( $line{'title'} ) {
207         my $volume      = $order->{'volume'};
208         my $seriestitle = $order->{'seriestitle'};
209         $line{'title'} .= " / $seriestitle" if $seriestitle;
210         $line{'title'} .= " / $volume"      if $volume;
211     } else {
212         $line{'title'} = "Deleted bibliographic notice, can't find title.";
213     }
214
215     return \%line;
216 }
217
218 output_html_with_http_headers $input, $cookie, $template->output;