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' => '*' },
54 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
55 my $invoiceid = $input->param('invoiceid');
56 my $op = $input->param('op');
58 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
60 && ! $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } )
61 && ! $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } )
62 && ! $logged_in_patron->has_permission( { acquisition => 'merge_invoices' } )
63 && ! $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
66 if ( C4::Context->preference('AcqEnableFiles') ) {
67 $invoice_files = Koha::Misc::Files->new(
68 tabletag => 'aqinvoices', recordid => $invoiceid );
71 if ( $op && $op eq 'close' ) {
72 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
73 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
74 my @invoiceid = $input->multi_param('invoiceid');
75 foreach my $invoiceid ( @invoiceid ) {
76 CloseInvoice($invoiceid);
78 my $referer = $input->param('referer');
80 print $input->redirect($referer);
84 elsif ( $op && $op eq 'reopen' ) {
85 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
86 unless $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
87 my @invoiceid = $input->multi_param('invoiceid');
88 foreach my $invoiceid ( @invoiceid ) {
89 ReopenInvoice($invoiceid);
91 my $referer = $input->param('referer');
93 print $input->redirect($referer);
97 elsif ( $op && $op eq 'mod' ) {
98 my $shipmentcost = $input->param('shipmentcost');
99 my $shipment_budget_id = $input->param('shipment_budget_id');
100 my $invoicenumber = $input->param('invoicenumber');
102 invoiceid => $invoiceid,
103 invoicenumber => $invoicenumber,
104 shipmentdate => scalar output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
105 billingdate => scalar output_pref( { str => scalar $input->param('billingdate'), dateformat => 'iso', dateonly => 1 } ),
106 shipmentcost => $shipmentcost,
107 shipmentcost_budgetid => $shipment_budget_id
109 if ($input->param('reopen')) {
110 ReopenInvoice($invoiceid)
111 if $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
112 } elsif ($input->param('close')) {
114 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
115 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
117 CloseInvoice($invoiceid);
118 } elsif ($input->param('merge')) {
120 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
121 unless $logged_in_patron->has_permission( { acquisition => 'merge_invoices' } );
123 my @sources = $input->multi_param('merge');
124 MergeInvoices($invoiceid, \@sources);
125 defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
127 $template->param( modified => 1 );
129 elsif ( $op && $op eq 'delete' ) {
131 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
132 unless $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
134 DelInvoice($invoiceid);
135 defined($invoice_files) && $invoice_files->DelAllFiles();
136 my $referer = $input->param('referer') || 'invoices.pl';
138 print $input->redirect($referer);
142 elsif ( $op && $op eq 'del_adj' ) {
144 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
145 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
147 my $adjustment_id = $input->param('adjustment_id');
148 my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
149 $del_adj->delete() if ($del_adj);
151 elsif ( $op && $op eq 'mod_adj' ) {
153 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
154 unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
156 my @adjustment_id = $input->multi_param('adjustment_id');
157 my @adjustment = $input->multi_param('adjustment');
158 my @reason = $input->multi_param('reason');
159 my @note = $input->multi_param('note');
160 my @budget_id = $input->multi_param('budget_id');
161 my @encumber_open = $input->multi_param('encumber_open');
162 my %e_open = map { $_ => 1 } @encumber_open;
164 for( my $i=0; $i < scalar @adjustment; $i++ ){
165 if( $adjustment_id[$i] eq 'new' ){
166 next unless ( $adjustment[$i] || $reason[$i] );
167 my $new_adj = Koha::Acquisition::Invoice::Adjustment->new({
168 invoiceid => $invoiceid,
169 adjustment => $adjustment[$i],
170 reason => $reason[$i],
172 budget_id => $budget_id[$i] || undef,
173 encumber_open => defined $e_open{ $adjustment_id[$i] } ? 1 : 0,
178 my $old_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id[$i] );
179 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] ){
180 $old_adj->timestamp(undef);
181 $old_adj->adjustment( $adjustment[$i] );
182 $old_adj->reason( $reason[$i] );
183 $old_adj->note( $note[$i] );
184 $old_adj->budget_id( $budget_id[$i] || undef );
185 $old_adj->encumber_open( $e_open{$adjustment_id[$i]} ? 1 : 0 );
192 my $details = GetInvoiceDetails($invoiceid);
193 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
194 my @orders_loop = ();
195 my $orders = $details->{'orders'};
198 my $shipmentcost = $details->{shipmentcost} || 0;
199 my $total_quantity = 0;
200 my $total_tax_excluded = 0;
201 my $total_tax_included = 0;
202 my $total_tax_value = 0;
203 foreach my $order (@$orders) {
204 my $line = get_infos( $order, $bookseller);
206 $line->{total_tax_excluded} = get_rounded_price($line->{unitprice_tax_excluded}) * $line->{quantity};
207 $line->{total_tax_included} = get_rounded_price($line->{unitprice_tax_included}) * $line->{quantity};
209 $line->{tax_value} = $line->{tax_value_on_receiving};
210 $line->{tax_rate} = $line->{tax_rate_on_receiving};
212 $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
213 $foot{$$line{tax_rate}}{tax_value} += get_rounded_price($$line{tax_value});
214 $total_tax_value += $$line{tax_value};
215 $foot{$$line{tax_rate}}{quantity} += $$line{quantity};
216 $total_quantity += $$line{quantity};
217 $foot{$$line{tax_rate}}{total_tax_excluded} += get_rounded_price($$line{total_tax_excluded});
218 $total_tax_excluded += get_rounded_price($$line{total_tax_excluded});
219 $foot{$$line{tax_rate}}{total_tax_included} += get_rounded_price($$line{total_tax_included});
220 $total_tax_included += get_rounded_price($$line{total_tax_included});
222 $line->{orderline} = $line->{parent_ordernumber};
223 push @orders_loop, $line;
226 push @foot_loop, map {$_} values %foot;
228 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
231 my $budget_loop = [];
232 my $budgets = GetBudgetHierarchy();
233 foreach my $r ( @{$budgets} ) {
234 next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) );
236 my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0;
238 push @{$budget_loop},
240 b_id => $r->{budget_id},
241 b_txt => $r->{budget_name},
242 b_active => $r->{budget_period_active},
243 selected => $selected,
248 sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
250 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
251 if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
254 invoiceid => $details->{'invoiceid'},
255 invoicenumber => $details->{'invoicenumber'},
256 suppliername => $details->{'suppliername'},
257 booksellerid => $details->{'booksellerid'},
258 shipmentdate => $details->{'shipmentdate'},
259 billingdate => $details->{'billingdate'},
260 invoiceclosedate => $details->{'closedate'},
261 shipmentcost => $shipmentcost,
262 orders_loop => \@orders_loop,
263 foot_loop => \@foot_loop,
264 total_quantity => $total_quantity,
265 total_tax_excluded => $total_tax_excluded,
266 total_tax_included => $total_tax_included,
267 total_tax_value => $total_tax_value,
268 total_tax_excluded_shipment => $total_tax_excluded + $shipmentcost,
269 total_tax_included_shipment => $total_tax_included + $shipmentcost,
270 invoiceincgst => $bookseller->invoiceincgst,
271 currency => Koha::Acquisition::Currencies->get_active,
272 budgets => $budget_loop,
273 budget => GetBudget( $shipmentcost_budgetid ),
276 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
279 # Fonction dupplicated from basket.pl
280 # Code must to be exported. Where ??
283 my $bookseller = shift;
284 my $qty = $order->{'quantity'} || 0;
285 if ( !defined $order->{quantityreceived} ) {
286 $order->{quantityreceived} = 0;
288 my $budget = GetBudget( $order->{'budget_id'} );
290 my %line = %{ $order };
291 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
292 $line{budget_name} = $budget->{budget_name};
294 if ( $line{'title'} ) {
295 my $volume = $order->{'volume'};
296 my $seriestitle = $order->{'seriestitle'};
297 $line{'title'} .= " / $seriestitle" if $seriestitle;
298 $line{'title'} .= " / $volume" if $volume;
304 output_html_with_http_headers $input, $cookie, $template->output;