Bug 13321: Rename variables
[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
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 qw ( -utf8 );
33 use C4::Auth;
34 use C4::Output;
35 use C4::Acquisition;
36 use C4::Budgets;
37
38 use Koha::Acquisition::Bookseller;
39 use Koha::Acquisition::Currencies;
40 use Koha::DateUtils;
41 use Koha::Misc::Files;
42
43 my $input = new CGI;
44 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
45     {
46         template_name   => 'acqui/invoice.tt',
47         query           => $input,
48         type            => 'intranet',
49         authnotrequired => 0,
50         flagsrequired   => { 'acquisition' => '*' },
51         debug           => 1,
52     }
53 );
54
55 my $invoiceid = $input->param('invoiceid');
56 my $op        = $input->param('op');
57
58 my $invoice_files;
59 if ( C4::Context->preference('AcqEnableFiles') ) {
60     $invoice_files = Koha::Misc::Files->new(
61         tabletag => 'aqinvoices', recordid => $invoiceid );
62 }
63
64 if ( $op && $op eq 'close' ) {
65     CloseInvoice($invoiceid);
66     my $referer = $input->param('referer');
67     if ($referer) {
68         print $input->redirect($referer);
69         exit 0;
70     }
71 }
72 elsif ( $op && $op eq 'reopen' ) {
73     ReopenInvoice($invoiceid);
74     my $referer = $input->param('referer');
75     if ($referer) {
76         print $input->redirect($referer);
77         exit 0;
78     }
79 }
80 elsif ( $op && $op eq 'mod' ) {
81     my $shipmentcost       = $input->param('shipmentcost');
82     my $shipment_budget_id = $input->param('shipment_budget_id');
83     my $invoicenumber      = $input->param('invoicenumber');
84     ModInvoice(
85         invoiceid             => $invoiceid,
86         invoicenumber         => $invoicenumber,
87         shipmentdate          => output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
88         billingdate           => output_pref( { str => scalar $input->param('billingdate'),  dateformat => 'iso', dateonly => 1 } ),
89         shipmentcost          => $shipmentcost,
90         shipmentcost_budgetid => $shipment_budget_id
91     );
92     if ($input->param('reopen')) {
93         ReopenInvoice($invoiceid);
94     } elsif ($input->param('close')) {
95         CloseInvoice($invoiceid);
96     } elsif ($input->param('merge')) {
97         my @sources = $input->multi_param('merge');
98         MergeInvoices($invoiceid, \@sources);
99         defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
100     }
101     $template->param( modified => 1 );
102 }
103 elsif ( $op && $op eq 'delete' ) {
104     DelInvoice($invoiceid);
105     defined($invoice_files) && $invoice_files->DelAllFiles();
106     my $referer = $input->param('referer') || 'invoices.pl';
107     if ($referer) {
108         print $input->redirect($referer);
109         exit 0;
110     }
111 }
112
113
114 my $details = GetInvoiceDetails($invoiceid);
115 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $details->{booksellerid} });
116 my @orders_loop = ();
117 my $orders = $details->{'orders'};
118 my @foot_loop;
119 my %foot;
120 my $total_quantity = 0;
121 my $total_tax_excluded = 0;
122 my $total_tax_included = 0;
123 my $total_tax_value = 0;
124 foreach my $order (@$orders) {
125     my $line = get_infos( $order, $bookseller);
126
127     $line->{total_tax_excluded} = Koha::Number::Price->new( $line->{unitprice_tax_excluded} * $line->{quantity} )->format;
128     $line->{total_tax_included} = Koha::Number::Price->new( $line->{unitprice_tax_included} * $line->{quantity} )->format;
129
130     $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
131     $foot{$$line{tax_rate}}{tax_value} += $$line{tax_value};
132     $total_tax_value += $$line{tax_value};
133     $foot{$$line{tax_rate}}{quantity}  += $$line{quantity};
134     $total_quantity += $$line{quantity};
135     $foot{$$line{tax_rate}}{total_tax_excluded} += $$line{total_tax_excluded};
136     $total_tax_excluded += $$line{total_tax_excluded};
137     $foot{$$line{tax_rate}}{total_tax_included} += $$line{total_tax_included};
138     $total_tax_included += $$line{total_tax_included};
139
140     $line->{orderline} = $line->{parent_ordernumber};
141     push @orders_loop, $line;
142 }
143
144 push @foot_loop, map {$_} values %foot;
145
146 my $format = "%.2f";
147 my $budgets = GetBudgets();
148 my @budgets_loop;
149 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
150 foreach my $budget (@$budgets) {
151     next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
152     my %line = %{$budget};
153     if (    $shipmentcost_budgetid
154         and $budget->{budget_id} == $shipmentcost_budgetid )
155     {
156         $line{selected} = 1;
157     }
158     push @budgets_loop, \%line;
159 }
160
161 $template->param(
162     invoiceid        => $details->{'invoiceid'},
163     invoicenumber    => $details->{'invoicenumber'},
164     suppliername     => $details->{'suppliername'},
165     booksellerid     => $details->{'booksellerid'},
166     shipmentdate     => $details->{'shipmentdate'},
167     billingdate      => $details->{'billingdate'},
168     invoiceclosedate => $details->{'closedate'},
169     shipmentcost     => $details->{'shipmentcost'},
170     orders_loop      => \@orders_loop,
171     foot_loop        => \@foot_loop,
172     total_quantity   => $total_quantity,
173     total_tax_excluded       => sprintf( $format, $total_tax_excluded ),
174     total_tax_included       => sprintf( $format, $total_tax_included ),
175     total_tax_value   => sprintf( $format, $total_tax_value ),
176     total_tax_excluded_shipment => sprintf( $format, $total_tax_excluded + $details->{shipmentcost}),
177     total_tax_included_shipment => sprintf( $format, $total_tax_included + $details->{shipmentcost}),
178     invoiceincgst    => $bookseller->{invoiceincgst},
179     currency         => Koha::Acquisition::Currencies->get_active,
180     budgets_loop     => \@budgets_loop,
181 );
182
183 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
184
185 # FIXME
186 # Fonction dupplicated from basket.pl
187 # Code must to be exported. Where ??
188 sub get_infos {
189     my $order = shift;
190     my $bookseller = shift;
191     my $qty = $order->{'quantity'} || 0;
192     if ( !defined $order->{quantityreceived} ) {
193         $order->{quantityreceived} = 0;
194     }
195     my $budget = GetBudget( $order->{'budget_id'} );
196
197     my %line = %{ $order };
198     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
199     $line{budget_name}    = $budget->{budget_name};
200
201     if ( $line{uncertainprice} ) {
202         $template->param( uncertainprices => 1 );
203         $line{rrp} .= ' (Uncertain)';
204     }
205     if ( $line{'title'} ) {
206         my $volume      = $order->{'volume'};
207         my $seriestitle = $order->{'seriestitle'};
208         $line{'title'} .= " / $seriestitle" if $seriestitle;
209         $line{'title'} .= " / $volume"      if $volume;
210     }
211
212     return \%line;
213 }
214
215 output_html_with_http_headers $input, $cookie, $template->output;