Bug 20538: Remove the need of writing [% KOHA_VERSION %] everywhere
[koha.git] / acqui / invoice.pl
blob0f2933aa0ca87129fb1def341550dda4f038c2ad
1 #!/usr/bin/perl
3 # Copyright 2011 BibLibre SARL
4 # This file is part of Koha.
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.
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.
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>.
19 =head1 NAME
21 invoice.pl
23 =head1 DESCRIPTION
25 Invoice details
27 =cut
29 use Modern::Perl;
31 use CGI qw ( -utf8 );
32 use C4::Auth;
33 use C4::Output;
34 use C4::Acquisition;
35 use C4::Budgets;
37 use Koha::Acquisition::Booksellers;
38 use Koha::Acquisition::Currencies;
39 use Koha::DateUtils;
40 use Koha::Misc::Files;
42 my $input = new CGI;
43 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
45 template_name => 'acqui/invoice.tt',
46 query => $input,
47 type => 'intranet',
48 authnotrequired => 0,
49 flagsrequired => { 'acquisition' => '*' },
50 debug => 1,
54 my $invoiceid = $input->param('invoiceid');
55 my $op = $input->param('op');
57 my $invoice_files;
58 if ( C4::Context->preference('AcqEnableFiles') ) {
59 $invoice_files = Koha::Misc::Files->new(
60 tabletag => 'aqinvoices', recordid => $invoiceid );
63 if ( $op && $op eq 'close' ) {
64 CloseInvoice($invoiceid);
65 my $referer = $input->param('referer');
66 if ($referer) {
67 print $input->redirect($referer);
68 exit 0;
71 elsif ( $op && $op eq 'reopen' ) {
72 ReopenInvoice($invoiceid);
73 my $referer = $input->param('referer');
74 if ($referer) {
75 print $input->redirect($referer);
76 exit 0;
79 elsif ( $op && $op eq 'mod' ) {
80 my $shipmentcost = $input->param('shipmentcost');
81 my $shipment_budget_id = $input->param('shipment_budget_id');
82 my $invoicenumber = $input->param('invoicenumber');
83 ModInvoice(
84 invoiceid => $invoiceid,
85 invoicenumber => $invoicenumber,
86 shipmentdate => scalar output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
87 billingdate => scalar output_pref( { str => scalar $input->param('billingdate'), dateformat => 'iso', dateonly => 1 } ),
88 shipmentcost => $shipmentcost,
89 shipmentcost_budgetid => $shipment_budget_id
91 if ($input->param('reopen')) {
92 ReopenInvoice($invoiceid);
93 } elsif ($input->param('close')) {
94 CloseInvoice($invoiceid);
95 } elsif ($input->param('merge')) {
96 my @sources = $input->multi_param('merge');
97 MergeInvoices($invoiceid, \@sources);
98 defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
100 $template->param( modified => 1 );
102 elsif ( $op && $op eq 'delete' ) {
103 DelInvoice($invoiceid);
104 defined($invoice_files) && $invoice_files->DelAllFiles();
105 my $referer = $input->param('referer') || 'invoices.pl';
106 if ($referer) {
107 print $input->redirect($referer);
108 exit 0;
113 my $details = GetInvoiceDetails($invoiceid);
114 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
115 my @orders_loop = ();
116 my $orders = $details->{'orders'};
117 my @foot_loop;
118 my %foot;
119 my $total_quantity = 0;
120 my $total_tax_excluded = 0;
121 my $total_tax_included = 0;
122 my $total_tax_value = 0;
123 foreach my $order (@$orders) {
124 my $line = get_infos( $order, $bookseller);
126 $line->{total_tax_excluded} = $line->{unitprice_tax_excluded} * $line->{quantity};
127 $line->{total_tax_included} = $line->{unitprice_tax_included} * $line->{quantity};
129 $line->{tax_value} = $line->{tax_value_on_receiving};
130 $line->{tax_rate} = $line->{tax_rate_on_receiving};
132 $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
133 $foot{$$line{tax_rate}}{tax_value} += $$line{tax_value};
134 $total_tax_value += $$line{tax_value};
135 $foot{$$line{tax_rate}}{quantity} += $$line{quantity};
136 $total_quantity += $$line{quantity};
137 $foot{$$line{tax_rate}}{total_tax_excluded} += $$line{total_tax_excluded};
138 $total_tax_excluded += $$line{total_tax_excluded};
139 $foot{$$line{tax_rate}}{total_tax_included} += $$line{total_tax_included};
140 $total_tax_included += $$line{total_tax_included};
142 $line->{orderline} = $line->{parent_ordernumber};
143 push @orders_loop, $line;
146 push @foot_loop, map {$_} values %foot;
148 my $budgets = GetBudgets();
149 my @budgets_loop;
150 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
151 foreach my $budget (@$budgets) {
152 next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
153 my %line = %{$budget};
154 if ( $shipmentcost_budgetid
155 and $budget->{budget_id} == $shipmentcost_budgetid )
157 $line{selected} = 1;
159 push @budgets_loop, \%line;
162 $template->param(
163 invoiceid => $details->{'invoiceid'},
164 invoicenumber => $details->{'invoicenumber'},
165 suppliername => $details->{'suppliername'},
166 booksellerid => $details->{'booksellerid'},
167 shipmentdate => $details->{'shipmentdate'},
168 billingdate => $details->{'billingdate'},
169 invoiceclosedate => $details->{'closedate'},
170 shipmentcost => $details->{'shipmentcost'},
171 orders_loop => \@orders_loop,
172 foot_loop => \@foot_loop,
173 total_quantity => $total_quantity,
174 total_tax_excluded => $total_tax_excluded,
175 total_tax_included => $total_tax_included,
176 total_tax_value => $total_tax_value,
177 total_tax_excluded_shipment => $total_tax_excluded + $details->{shipmentcost},
178 total_tax_included_shipment => $total_tax_included + $details->{shipmentcost},
179 invoiceincgst => $bookseller->invoiceincgst,
180 currency => Koha::Acquisition::Currencies->get_active,
181 budgets_loop => \@budgets_loop,
184 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
186 # FIXME
187 # Fonction dupplicated from basket.pl
188 # Code must to be exported. Where ??
189 sub get_infos {
190 my $order = shift;
191 my $bookseller = shift;
192 my $qty = $order->{'quantity'} || 0;
193 if ( !defined $order->{quantityreceived} ) {
194 $order->{quantityreceived} = 0;
196 my $budget = GetBudget( $order->{'budget_id'} );
198 my %line = %{ $order };
199 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
200 $line{budget_name} = $budget->{budget_name};
202 if ( $line{uncertainprice} ) {
203 $line{rrp} .= ' (Uncertain)';
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;
212 return \%line;
215 output_html_with_http_headers $input, $cookie, $template->output;