Bug 28464: Remove useless check that gives the incorrect error message
[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 Modern::Perl;
30
31 use CGI qw ( -utf8 );
32 use C4::Auth qw( get_template_and_user );
33 use C4::Output qw( output_and_exit output_html_with_http_headers );
34 use C4::Acquisition qw( CloseInvoice ReopenInvoice ModInvoice MergeInvoices DelInvoice GetInvoice GetInvoiceDetails get_rounded_price );
35 use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget );
36
37 use Koha::Acquisition::Booksellers;
38 use Koha::Acquisition::Currencies qw( get_active );
39 use Koha::DateUtils qw( output_pref );
40 use Koha::Misc::Files;
41 use Koha::Acquisition::Invoice::Adjustments;
42
43 my $input = CGI->new;
44 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
45     {
46         template_name   => 'acqui/invoice.tt',
47         query           => $input,
48         type            => 'intranet',
49         flagsrequired   => { 'acquisition' => '*' },
50     }
51 );
52
53 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
54 my $invoiceid = $input->param('invoiceid');
55 my $op        = $input->param('op');
56
57 output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
58   if $op
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' } );
63
64 my $invoice_files;
65 if ( C4::Context->preference('AcqEnableFiles') ) {
66     $invoice_files = Koha::Misc::Files->new(
67         tabletag => 'aqinvoices', recordid => $invoiceid );
68 }
69
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);
76     }
77     my $referer = $input->param('referer');
78     if ($referer) {
79         print $input->redirect($referer);
80         exit 0;
81     }
82 }
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);
89     }
90     my $referer = $input->param('referer');
91     if ($referer) {
92         print $input->redirect($referer);
93         exit 0;
94     }
95 }
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');
100     ModInvoice(
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
107     );
108     if ($input->param('reopen')) {
109         ReopenInvoice($invoiceid)
110             if $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
111     } elsif ($input->param('close')) {
112
113         output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
114             unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
115
116         CloseInvoice($invoiceid);
117     } elsif ($input->param('merge')) {
118
119         output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
120             unless $logged_in_patron->has_permission( { acquisition => 'merge_invoices' } );
121
122         my @sources = $input->multi_param('merge');
123         MergeInvoices($invoiceid, \@sources);
124         defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
125     }
126     $template->param( modified => 1 );
127 }
128 elsif ( $op && $op eq 'delete' ) {
129
130     output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
131         unless $logged_in_patron->has_permission( { acquisition => 'delete_invoices' } );
132
133     DelInvoice($invoiceid);
134     defined($invoice_files) && $invoice_files->DelAllFiles();
135     my $referer = $input->param('referer') || 'invoices.pl';
136     if ($referer) {
137         print $input->redirect($referer);
138         exit 0;
139     }
140 }
141 elsif ( $op && $op eq 'del_adj' ) {
142
143     output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
144         unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
145
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);
149 }
150 elsif ( $op && $op eq 'mod_adj' ) {
151
152     output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
153         unless $logged_in_patron->has_permission( { acquisition => 'edit_invoices' } );
154
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;
162
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],
170                 note => $note[$i],
171                 budget_id => $budget_id[$i] || undef,
172                 encumber_open => defined $e_open{ $adjustment_id[$i] } ? 1 : 0,
173             });
174             $new_adj->store();
175         }
176         else {
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 );
185                 $old_adj->update();
186             }
187         }
188     }
189 }
190
191 my $details = GetInvoiceDetails($invoiceid);
192 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
193 my @orders_loop = ();
194 my $orders = $details->{'orders'};
195 my @foot_loop;
196 my %foot;
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);
204
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};
207
208     $line->{tax_value} = $line->{tax_value_on_receiving};
209     $line->{tax_rate} = $line->{tax_rate_on_receiving};
210
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});
220
221     $line->{orderline} = $line->{parent_ordernumber};
222     push @orders_loop, $line;
223 }
224
225 push @foot_loop, map {$_} values %foot;
226
227 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
228
229 # build budget list
230 my $budget_loop = [];
231 my $budgets     = GetBudgetHierarchy();
232 foreach my $r ( @{$budgets} ) {
233     next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) );
234
235     my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0;
236
237     push @{$budget_loop},
238       {
239         b_id     => $r->{budget_id},
240         b_txt    => $r->{budget_name},
241         b_active => $r->{budget_period_active},
242         selected => $selected,
243       };
244 }
245
246 @{$budget_loop} =
247   sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
248
249 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
250 if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
251
252 $template->param(
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 ),
273 );
274
275 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
276
277 # FIXME
278 # Fonction dupplicated from basket.pl
279 # Code must to be exported. Where ??
280 sub get_infos {
281     my $order = shift;
282     my $bookseller = shift;
283     my $qty = $order->{'quantity'} || 0;
284     if ( !defined $order->{quantityreceived} ) {
285         $order->{quantityreceived} = 0;
286     }
287     my $budget = GetBudget( $order->{'budget_id'} );
288
289     my %line = %{ $order };
290     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
291     $line{budget_name}    = $budget->{budget_name};
292
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;
298     }
299
300     return \%line;
301 }
302
303 output_html_with_http_headers $input, $cookie, $template->output;