Bug 24663: [19.11.x] Remove authnotrequired if set to 0
[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;
33 use C4::Output;
34 use C4::Acquisition;
35 use C4::Budgets;
36
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;
42
43 my $input = new CGI;
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         debug           => 1,
51     }
52 );
53
54 my $invoiceid = $input->param('invoiceid');
55 my $op        = $input->param('op');
56
57 my $invoice_files;
58 if ( C4::Context->preference('AcqEnableFiles') ) {
59     $invoice_files = Koha::Misc::Files->new(
60         tabletag => 'aqinvoices', recordid => $invoiceid );
61 }
62
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;
69     }
70 }
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;
77     }
78 }
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
90     );
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);
99     }
100     $template->param( modified => 1 );
101 }
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;
109     }
110 }
111 elsif ( $op && $op eq 'del_adj' ) {
112     my $adjustment_id  = $input->param('adjustment_id');
113     my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
114     $del_adj->delete() if ($del_adj);
115 }
116 elsif ( $op && $op eq 'mod_adj' ) {
117     my @adjustment_id  = $input->multi_param('adjustment_id');
118     my @adjustment     = $input->multi_param('adjustment');
119     my @reason         = $input->multi_param('reason');
120     my @note           = $input->multi_param('note');
121     my @budget_id      = $input->multi_param('budget_id');
122     my @encumber_open  = $input->multi_param('encumber_open');
123     my %e_open = map { $_ => 1 } @encumber_open;
124
125     for( my $i=0; $i < scalar @adjustment; $i++ ){
126         if( $adjustment_id[$i] eq 'new' ){
127             next unless ( $adjustment[$i] || $reason[$i] );
128             my $new_adj = Koha::Acquisition::Invoice::Adjustment->new({
129                 invoiceid => $invoiceid,
130                 adjustment => $adjustment[$i],
131                 reason => $reason[$i],
132                 note => $note[$i],
133                 budget_id => $budget_id[$i] || undef,
134                 encumber_open => defined $e_open{ $adjustment_id[$i] } ? 1 : 0,
135             });
136             $new_adj->store();
137         }
138         else {
139             my $old_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id[$i] );
140             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] ){
141                 $old_adj->timestamp(undef);
142                 $old_adj->adjustment( $adjustment[$i] );
143                 $old_adj->reason( $reason[$i] );
144                 $old_adj->note( $note[$i] );
145                 $old_adj->budget_id( $budget_id[$i] || undef );
146                 $old_adj->encumber_open( $e_open{$adjustment_id[$i]} ? 1 : 0 );
147                 $old_adj->update();
148             }
149         }
150     }
151 }
152
153 my $details = GetInvoiceDetails($invoiceid);
154 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
155 my @orders_loop = ();
156 my $orders = $details->{'orders'};
157 my @foot_loop;
158 my %foot;
159 my $shipmentcost = $details->{shipmentcost} || 0;
160 my $total_quantity = 0;
161 my $total_tax_excluded = 0;
162 my $total_tax_included = 0;
163 my $total_tax_value = 0;
164 foreach my $order (@$orders) {
165     my $line = get_infos( $order, $bookseller);
166
167     $line->{total_tax_excluded} = get_rounded_price($line->{unitprice_tax_excluded}) * $line->{quantity};
168     $line->{total_tax_included} = get_rounded_price($line->{unitprice_tax_included}) * $line->{quantity};
169
170     $line->{tax_value} = $line->{tax_value_on_receiving};
171     $line->{tax_rate} = $line->{tax_rate_on_receiving};
172
173     $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
174     $foot{$$line{tax_rate}}{tax_value} += get_rounded_price($$line{tax_value});
175     $total_tax_value += $$line{tax_value};
176     $foot{$$line{tax_rate}}{quantity}  += $$line{quantity};
177     $total_quantity += $$line{quantity};
178     $foot{$$line{tax_rate}}{total_tax_excluded} += get_rounded_price($$line{total_tax_excluded});
179     $total_tax_excluded += get_rounded_price($$line{total_tax_excluded});
180     $foot{$$line{tax_rate}}{total_tax_included} += get_rounded_price($$line{total_tax_included});
181     $total_tax_included += get_rounded_price($$line{total_tax_included});
182
183     $line->{orderline} = $line->{parent_ordernumber};
184     push @orders_loop, $line;
185 }
186
187 push @foot_loop, map {$_} values %foot;
188
189 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
190
191 # build budget list
192 my $budget_loop = [];
193 my $budgets     = GetBudgetHierarchy();
194 foreach my $r ( @{$budgets} ) {
195     next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) );
196
197     if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
198         next;
199     }
200
201     my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0;
202
203     push @{$budget_loop},
204       {
205         b_id     => $r->{budget_id},
206         b_txt    => $r->{budget_name},
207         b_active => $r->{budget_period_active},
208         selected => $selected,
209       };
210 }
211
212 @{$budget_loop} =
213   sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
214
215 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
216 if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
217
218 $template->param(
219     invoiceid                   => $details->{'invoiceid'},
220     invoicenumber               => $details->{'invoicenumber'},
221     suppliername                => $details->{'suppliername'},
222     booksellerid                => $details->{'booksellerid'},
223     shipmentdate                => $details->{'shipmentdate'},
224     billingdate                 => $details->{'billingdate'},
225     invoiceclosedate            => $details->{'closedate'},
226     shipmentcost                => $shipmentcost,
227     orders_loop                 => \@orders_loop,
228     foot_loop                   => \@foot_loop,
229     total_quantity              => $total_quantity,
230     total_tax_excluded          => $total_tax_excluded,
231     total_tax_included          => $total_tax_included,
232     total_tax_value             => $total_tax_value,
233     total_tax_excluded_shipment => $total_tax_excluded + $shipmentcost,
234     total_tax_included_shipment => $total_tax_included + $shipmentcost,
235     invoiceincgst               => $bookseller->invoiceincgst,
236     currency                    => Koha::Acquisition::Currencies->get_active,
237     budgets                     => $budget_loop,
238 );
239
240 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
241
242 # FIXME
243 # Fonction dupplicated from basket.pl
244 # Code must to be exported. Where ??
245 sub get_infos {
246     my $order = shift;
247     my $bookseller = shift;
248     my $qty = $order->{'quantity'} || 0;
249     if ( !defined $order->{quantityreceived} ) {
250         $order->{quantityreceived} = 0;
251     }
252     my $budget = GetBudget( $order->{'budget_id'} );
253
254     my %line = %{ $order };
255     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
256     $line{budget_name}    = $budget->{budget_name};
257
258     if ( $line{'title'} ) {
259         my $volume      = $order->{'volume'};
260         my $seriestitle = $order->{'seriestitle'};
261         $line{'title'} .= " / $seriestitle" if $seriestitle;
262         $line{'title'} .= " / $volume"      if $volume;
263     }
264
265     return \%line;
266 }
267
268 output_html_with_http_headers $input, $cookie, $template->output;