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>.
37 use Koha::Acquisition::Booksellers;
38 use Koha::Acquisition::Currencies;
40 use Koha::Misc::Files;
41 use Koha::Acquisition::Invoice::Adjustments;
44 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
46 template_name => 'acqui/invoice.tt',
49 flagsrequired => { 'acquisition' => '*' },
53 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
54 my $invoiceid = $input->param('invoiceid');
55 my $op = $input->param('op');
57 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
59 && ! $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } )
60 && ! $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } )
61 && ! $logged_in_patron->has_permission( { acquisition => 'merge_invoices' } )
62 && ! $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
65 if ( C4::Context->preference('AcqEnableFiles') ) {
66 $invoice_files = Koha::Misc::Files->new(
67 tabletag => 'aqinvoices', recordid => $invoiceid );
70 if ( $op && $op eq 'close' ) {
71 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
72 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
73 my @invoiceid = $input->multi_param('invoiceid');
74 foreach my $invoiceid ( @invoiceid ) {
75 CloseInvoice($invoiceid);
77 my $referer = $input->param('referer');
79 print $input->redirect($referer);
83 elsif ( $op && $op eq 'reopen' ) {
84 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
85 unless $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
86 my @invoiceid = $input->multi_param('invoiceid');
87 foreach my $invoiceid ( @invoiceid ) {
88 ReopenInvoice($invoiceid);
90 my $referer = $input->param('referer');
92 print $input->redirect($referer);
96 elsif ( $op && $op eq 'mod' ) {
97 my $shipmentcost = $input->param('shipmentcost');
98 my $shipment_budget_id = $input->param('shipment_budget_id');
99 my $invoicenumber = $input->param('invoicenumber');
101 invoiceid => $invoiceid,
102 invoicenumber => $invoicenumber,
103 shipmentdate => scalar output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
104 billingdate => scalar output_pref( { str => scalar $input->param('billingdate'), dateformat => 'iso', dateonly => 1 } ),
105 shipmentcost => $shipmentcost,
106 shipmentcost_budgetid => $shipment_budget_id
108 if ($input->param('reopen')) {
109 ReopenInvoice($invoiceid)
110 if $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
111 } elsif ($input->param('close')) {
113 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
114 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
116 CloseInvoice($invoiceid);
117 } elsif ($input->param('merge')) {
119 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
120 unless $logged_in_patron->has_permission( { acquisition => 'merge_invoices' } );
122 my @sources = $input->multi_param('merge');
123 MergeInvoices($invoiceid, \@sources);
124 defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
126 $template->param( modified => 1 );
128 elsif ( $op && $op eq 'delete' ) {
130 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
131 unless $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
133 DelInvoice($invoiceid);
134 defined($invoice_files) && $invoice_files->DelAllFiles();
135 my $referer = $input->param('referer') || 'invoices.pl';
137 print $input->redirect($referer);
141 elsif ( $op && $op eq 'del_adj' ) {
143 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
144 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
146 my $adjustment_id = $input->param('adjustment_id');
147 my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
148 $del_adj->delete() if ($del_adj);
150 elsif ( $op && $op eq 'mod_adj' ) {
152 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
153 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
155 my @adjustment_id = $input->multi_param('adjustment_id');
156 my @adjustment = $input->multi_param('adjustment');
157 my @reason = $input->multi_param('reason');
158 my @note = $input->multi_param('note');
159 my @budget_id = $input->multi_param('budget_id');
160 my @encumber_open = $input->multi_param('encumber_open');
161 my %e_open = map { $_ => 1 } @encumber_open;
163 for( my $i=0; $i < scalar @adjustment; $i++ ){
164 if( $adjustment_id[$i] eq 'new' ){
165 next unless ( $adjustment[$i] || $reason[$i] );
166 my $new_adj = Koha::Acquisition::Invoice::Adjustment->new({
167 invoiceid => $invoiceid,
168 adjustment => $adjustment[$i],
169 reason => $reason[$i],
171 budget_id => $budget_id[$i] || undef,
172 encumber_open => defined $e_open{ $adjustment_id[$i] } ? 1 : 0,
177 my $old_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id[$i] );
178 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] ){
179 $old_adj->timestamp(undef);
180 $old_adj->adjustment( $adjustment[$i] );
181 $old_adj->reason( $reason[$i] );
182 $old_adj->note( $note[$i] );
183 $old_adj->budget_id( $budget_id[$i] || undef );
184 $old_adj->encumber_open( $e_open{$adjustment_id[$i]} ? 1 : 0 );
191 my $details = GetInvoiceDetails($invoiceid);
192 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
193 my @orders_loop = ();
194 my $orders = $details->{'orders'};
197 my $shipmentcost = $details->{shipmentcost} || 0;
198 my $total_quantity = 0;
199 my $total_tax_excluded = 0;
200 my $total_tax_included = 0;
201 my $total_tax_value = 0;
202 foreach my $order (@$orders) {
203 my $line = get_infos( $order, $bookseller);
205 $line->{total_tax_excluded} = get_rounded_price($line->{unitprice_tax_excluded}) * $line->{quantity};
206 $line->{total_tax_included} = get_rounded_price($line->{unitprice_tax_included}) * $line->{quantity};
208 $line->{tax_value} = $line->{tax_value_on_receiving};
209 $line->{tax_rate} = $line->{tax_rate_on_receiving};
211 $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
212 $foot{$$line{tax_rate}}{tax_value} += get_rounded_price($$line{tax_value});
213 $total_tax_value += $$line{tax_value};
214 $foot{$$line{tax_rate}}{quantity} += $$line{quantity};
215 $total_quantity += $$line{quantity};
216 $foot{$$line{tax_rate}}{total_tax_excluded} += get_rounded_price($$line{total_tax_excluded});
217 $total_tax_excluded += get_rounded_price($$line{total_tax_excluded});
218 $foot{$$line{tax_rate}}{total_tax_included} += get_rounded_price($$line{total_tax_included});
219 $total_tax_included += get_rounded_price($$line{total_tax_included});
221 $line->{orderline} = $line->{parent_ordernumber};
222 push @orders_loop, $line;
225 push @foot_loop, map {$_} values %foot;
227 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
230 my $budget_loop = [];
231 my $budgets = GetBudgetHierarchy();
232 foreach my $r ( @{$budgets} ) {
233 next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) );
235 my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0;
237 push @{$budget_loop},
239 b_id => $r->{budget_id},
240 b_txt => $r->{budget_name},
241 b_active => $r->{budget_period_active},
242 selected => $selected,
247 sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
249 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
250 if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
253 invoiceid => $details->{'invoiceid'},
254 invoicenumber => $details->{'invoicenumber'},
255 suppliername => $details->{'suppliername'},
256 booksellerid => $details->{'booksellerid'},
257 shipmentdate => $details->{'shipmentdate'},
258 billingdate => $details->{'billingdate'},
259 invoiceclosedate => $details->{'closedate'},
260 shipmentcost => $shipmentcost,
261 orders_loop => \@orders_loop,
262 foot_loop => \@foot_loop,
263 total_quantity => $total_quantity,
264 total_tax_excluded => $total_tax_excluded,
265 total_tax_included => $total_tax_included,
266 total_tax_value => $total_tax_value,
267 total_tax_excluded_shipment => $total_tax_excluded + $shipmentcost,
268 total_tax_included_shipment => $total_tax_included + $shipmentcost,
269 invoiceincgst => $bookseller->invoiceincgst,
270 currency => Koha::Acquisition::Currencies->get_active,
271 budgets => $budget_loop,
272 budget => GetBudget( $shipmentcost_budgetid ),
275 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
278 # Fonction dupplicated from basket.pl
279 # Code must to be exported. Where ??
282 my $bookseller = shift;
283 my $qty = $order->{'quantity'} || 0;
284 if ( !defined $order->{quantityreceived} ) {
285 $order->{quantityreceived} = 0;
287 my $budget = GetBudget( $order->{'budget_id'} );
289 my %line = %{ $order };
290 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
291 $line{budget_name} = $budget->{budget_name};
293 if ( $line{'title'} ) {
294 my $volume = $order->{'volume'};
295 my $seriestitle = $order->{'seriestitle'};
296 $line{'title'} .= " / $seriestitle" if $seriestitle;
297 $line{'title'} .= " / $volume" if $volume;
303 output_html_with_http_headers $input, $cookie, $template->output;