Bug 10613: calculate the GST rate correctly on the invoice page.
[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->{supplierid});
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     my %row = %{ $order, $line };
127     $row{'orderline'} = $row{'parent_ordernumber'};
128     push @orders_loop, \%row;
129 }
130
131 push @foot_loop, map {$_} values %foot;
132
133 my $format = "%.2f";
134 my $budgets = GetBudgets();
135 my @budgets_loop;
136 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
137 foreach my $budget (@$budgets) {
138     next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
139     my %line = %{$budget};
140     if (    $shipmentcost_budgetid
141         and $budget->{budget_id} == $shipmentcost_budgetid )
142     {
143         $line{selected} = 1;
144     }
145     push @budgets_loop, \%line;
146 }
147
148 $template->param(
149     invoiceid        => $details->{'invoiceid'},
150     invoicenumber    => $details->{'invoicenumber'},
151     suppliername     => $details->{'suppliername'},
152     booksellerid     => $details->{'booksellerid'},
153     datereceived     => $details->{'datereceived'},
154     billingdate      => C4::Dates->new($details->{'billingdate'}, "iso")->output(),
155     invoiceclosedate => $details->{'invoiceclosedate'},
156     shipmentcost     => $details->{'shipmentcost'},
157     orders_loop      => \@orders_loop,
158     foot_loop        => \@foot_loop,
159     total_quantity   => $total_quantity,
160     total_gste       => sprintf( $format, $total_gste ),
161     total_gsti       => sprintf( $format, $total_gsti ),
162     total_gstvalue   => sprintf( $format, $total_gstvalue ),
163     total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
164     total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
165     invoiceincgst    => $bookseller->{invoiceincgst},
166     currency         => GetCurrency()->{currency},
167     budgets_loop     => \@budgets_loop,
168 );
169
170 # FIXME
171 # Fonction dupplicated from basket.pl
172 # Code must to be exported. Where ??
173 sub get_infos {
174     my $order = shift;
175     my $bookseller = shift;
176     my $qty = $order->{'quantity'} || 0;
177     if ( !defined $order->{quantityreceived} ) {
178         $order->{quantityreceived} = 0;
179     }
180     my $budget = GetBudget( $order->{'budget_id'} );
181
182     my %line = %{ $order };
183     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
184     $line{budget_name}    = $budget->{budget_name};
185     if ( $bookseller->{'listincgst'} ) {
186         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
187         $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
188         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} );
189         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) );
190         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
191         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
192         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
193     } else {
194         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
195         $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
196         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) );
197         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} );
198         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
199         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
200         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
201     }
202
203     if ( $line{uncertainprice} ) {
204         $template->param( uncertainprices => 1 );
205         $line{rrp} .= ' (Uncertain)';
206     }
207     if ( $line{'title'} ) {
208         my $volume      = $order->{'volume'};
209         my $seriestitle = $order->{'seriestitle'};
210         $line{'title'} .= " / $seriestitle" if $seriestitle;
211         $line{'title'} .= " / $volume"      if $volume;
212     } else {
213         $line{'title'} = "Deleted bibliographic notice, can't find title.";
214     }
215
216     return \%line;
217 }
218
219 output_html_with_http_headers $input, $cookie, $template->output;