Bug 19036: Add ability to enable credit number for only some credit types
[koha.git] / acqui / invoice.pl
blob0a9f38f31a38d54455e158880fe9d451c1c0edff
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;
41 use Koha::Acquisition::Invoice::Adjustments;
43 my $input = new CGI;
44 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
46 template_name => 'acqui/invoice.tt',
47 query => $input,
48 type => 'intranet',
49 authnotrequired => 0,
50 flagsrequired => { 'acquisition' => '*' },
51 debug => 1,
55 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
56 my $invoiceid = $input->param('invoiceid');
57 my $op = $input->param('op');
59 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
60 if $op
61 && ! $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } )
62 && ! $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } )
63 && ! $logged_in_patron->has_permission( { acquisition => 'merge_invoices' } )
64 && ! $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
66 my $invoice_files;
67 if ( C4::Context->preference('AcqEnableFiles') ) {
68 $invoice_files = Koha::Misc::Files->new(
69 tabletag => 'aqinvoices', recordid => $invoiceid );
72 if ( $op && $op eq 'close' ) {
73 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
74 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
75 CloseInvoice($invoiceid);
76 my $referer = $input->param('referer');
77 if ($referer) {
78 print $input->redirect($referer);
79 exit 0;
82 elsif ( $op && $op eq 'reopen' ) {
83 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
84 unless $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
86 ReopenInvoice($invoiceid);
87 my $referer = $input->param('referer');
88 if ($referer) {
89 print $input->redirect($referer);
90 exit 0;
93 elsif ( $op && $op eq 'mod' ) {
94 my $shipmentcost = $input->param('shipmentcost');
95 my $shipment_budget_id = $input->param('shipment_budget_id');
96 my $invoicenumber = $input->param('invoicenumber');
97 ModInvoice(
98 invoiceid => $invoiceid,
99 invoicenumber => $invoicenumber,
100 shipmentdate => scalar output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
101 billingdate => scalar output_pref( { str => scalar $input->param('billingdate'), dateformat => 'iso', dateonly => 1 } ),
102 shipmentcost => $shipmentcost,
103 shipmentcost_budgetid => $shipment_budget_id
105 if ($input->param('reopen')) {
106 ReopenInvoice($invoiceid)
107 if $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
108 } elsif ($input->param('close')) {
110 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
111 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
113 CloseInvoice($invoiceid);
114 } elsif ($input->param('merge')) {
116 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
117 unless $logged_in_patron->has_permission( { acquisition => 'merge_invoices' } );
119 my @sources = $input->multi_param('merge');
120 MergeInvoices($invoiceid, \@sources);
121 defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
123 $template->param( modified => 1 );
125 elsif ( $op && $op eq 'delete' ) {
127 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
128 unless $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
130 DelInvoice($invoiceid);
131 defined($invoice_files) && $invoice_files->DelAllFiles();
132 my $referer = $input->param('referer') || 'invoices.pl';
133 if ($referer) {
134 print $input->redirect($referer);
135 exit 0;
138 elsif ( $op && $op eq 'del_adj' ) {
140 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
141 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
143 my $adjustment_id = $input->param('adjustment_id');
144 my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
145 $del_adj->delete() if ($del_adj);
147 elsif ( $op && $op eq 'mod_adj' ) {
149 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
150 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
152 my @adjustment_id = $input->multi_param('adjustment_id');
153 my @adjustment = $input->multi_param('adjustment');
154 my @reason = $input->multi_param('reason');
155 my @note = $input->multi_param('note');
156 my @budget_id = $input->multi_param('budget_id');
157 my @encumber_open = $input->multi_param('encumber_open');
158 my %e_open = map { $_ => 1 } @encumber_open;
160 for( my $i=0; $i < scalar @adjustment; $i++ ){
161 if( $adjustment_id[$i] eq 'new' ){
162 next unless ( $adjustment[$i] || $reason[$i] );
163 my $new_adj = Koha::Acquisition::Invoice::Adjustment->new({
164 invoiceid => $invoiceid,
165 adjustment => $adjustment[$i],
166 reason => $reason[$i],
167 note => $note[$i],
168 budget_id => $budget_id[$i] || undef,
169 encumber_open => defined $e_open{ $adjustment_id[$i] } ? 1 : 0,
171 $new_adj->store();
173 else {
174 my $old_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id[$i] );
175 unless ( $old_adj->adjustment == $adjustment[$i] && $old_adj->reason eq $reason[$i] && $old_adj->budget_id == $budget_id[$i] && $old_adj->encumber_open == $e_open{$adjustment_id[$i]} && $old_adj->note eq $note[$i] ){
176 $old_adj->timestamp(undef);
177 $old_adj->adjustment( $adjustment[$i] );
178 $old_adj->reason( $reason[$i] );
179 $old_adj->note( $note[$i] );
180 $old_adj->budget_id( $budget_id[$i] || undef );
181 $old_adj->encumber_open( $e_open{$adjustment_id[$i]} ? 1 : 0 );
182 $old_adj->update();
188 my $details = GetInvoiceDetails($invoiceid);
189 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
190 my @orders_loop = ();
191 my $orders = $details->{'orders'};
192 my @foot_loop;
193 my %foot;
194 my $shipmentcost = $details->{shipmentcost} || 0;
195 my $total_quantity = 0;
196 my $total_tax_excluded = 0;
197 my $total_tax_included = 0;
198 my $total_tax_value = 0;
199 foreach my $order (@$orders) {
200 my $line = get_infos( $order, $bookseller);
202 $line->{total_tax_excluded} = get_rounded_price($line->{unitprice_tax_excluded}) * $line->{quantity};
203 $line->{total_tax_included} = get_rounded_price($line->{unitprice_tax_included}) * $line->{quantity};
205 $line->{tax_value} = $line->{tax_value_on_receiving};
206 $line->{tax_rate} = $line->{tax_rate_on_receiving};
208 $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
209 $foot{$$line{tax_rate}}{tax_value} += get_rounded_price($$line{tax_value});
210 $total_tax_value += $$line{tax_value};
211 $foot{$$line{tax_rate}}{quantity} += $$line{quantity};
212 $total_quantity += $$line{quantity};
213 $foot{$$line{tax_rate}}{total_tax_excluded} += get_rounded_price($$line{total_tax_excluded});
214 $total_tax_excluded += get_rounded_price($$line{total_tax_excluded});
215 $foot{$$line{tax_rate}}{total_tax_included} += get_rounded_price($$line{total_tax_included});
216 $total_tax_included += get_rounded_price($$line{total_tax_included});
218 $line->{orderline} = $line->{parent_ordernumber};
219 push @orders_loop, $line;
222 push @foot_loop, map {$_} values %foot;
224 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
226 # build budget list
227 my $budget_loop = [];
228 my $budgets = GetBudgetHierarchy();
229 foreach my $r ( @{$budgets} ) {
230 next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) );
232 my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0;
234 push @{$budget_loop},
236 b_id => $r->{budget_id},
237 b_txt => $r->{budget_name},
238 b_active => $r->{budget_period_active},
239 selected => $selected,
243 @{$budget_loop} =
244 sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
246 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
247 if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
249 $template->param(
250 invoiceid => $details->{'invoiceid'},
251 invoicenumber => $details->{'invoicenumber'},
252 suppliername => $details->{'suppliername'},
253 booksellerid => $details->{'booksellerid'},
254 shipmentdate => $details->{'shipmentdate'},
255 billingdate => $details->{'billingdate'},
256 invoiceclosedate => $details->{'closedate'},
257 shipmentcost => $shipmentcost,
258 orders_loop => \@orders_loop,
259 foot_loop => \@foot_loop,
260 total_quantity => $total_quantity,
261 total_tax_excluded => $total_tax_excluded,
262 total_tax_included => $total_tax_included,
263 total_tax_value => $total_tax_value,
264 total_tax_excluded_shipment => $total_tax_excluded + $shipmentcost,
265 total_tax_included_shipment => $total_tax_included + $shipmentcost,
266 invoiceincgst => $bookseller->invoiceincgst,
267 currency => Koha::Acquisition::Currencies->get_active,
268 budgets => $budget_loop,
269 budget => GetBudget( $shipmentcost_budgetid ),
272 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
274 # FIXME
275 # Fonction dupplicated from basket.pl
276 # Code must to be exported. Where ??
277 sub get_infos {
278 my $order = shift;
279 my $bookseller = shift;
280 my $qty = $order->{'quantity'} || 0;
281 if ( !defined $order->{quantityreceived} ) {
282 $order->{quantityreceived} = 0;
284 my $budget = GetBudget( $order->{'budget_id'} );
286 my %line = %{ $order };
287 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
288 $line{budget_name} = $budget->{budget_name};
290 if ( $line{'title'} ) {
291 my $volume = $order->{'volume'};
292 my $seriestitle = $order->{'seriestitle'};
293 $line{'title'} .= " / $seriestitle" if $seriestitle;
294 $line{'title'} .= " / $volume" if $volume;
297 return \%line;
300 output_html_with_http_headers $input, $cookie, $template->output;