Bug 24157: New permission - reopen_closed_invoices
[srvgit] / 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         authnotrequired => 0,
50         flagsrequired   => { 'acquisition' => '*' },
51         debug           => 1,
52     }
53 );
54
55 my $logged_in_patron = Koha::Patrons->find( $loggedinuser );
56
57 my $invoiceid = $input->param('invoiceid');
58 my $op        = $input->param('op');
59
60 my $invoice_files;
61 if ( C4::Context->preference('AcqEnableFiles') ) {
62     $invoice_files = Koha::Misc::Files->new(
63         tabletag => 'aqinvoices', recordid => $invoiceid );
64 }
65
66 if ( $op && $op eq 'close' ) {
67     CloseInvoice($invoiceid);
68     my $referer = $input->param('referer');
69     if ($referer) {
70         print $input->redirect($referer);
71         exit 0;
72     }
73 }
74 elsif ( $op && $op eq 'reopen' ) {
75     output_and_exit( $input, $cookie, $template, 'insufficient_permission' )
76         unless $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
77
78     ReopenInvoice($invoiceid);
79     my $referer = $input->param('referer');
80     if ($referer) {
81         print $input->redirect($referer);
82         exit 0;
83     }
84 }
85 elsif ( $op && $op eq 'mod' ) {
86     my $shipmentcost       = $input->param('shipmentcost');
87     my $shipment_budget_id = $input->param('shipment_budget_id');
88     my $invoicenumber      = $input->param('invoicenumber');
89     ModInvoice(
90         invoiceid             => $invoiceid,
91         invoicenumber         => $invoicenumber,
92         shipmentdate          => scalar output_pref( { str => scalar $input->param('shipmentdate'), dateformat => 'iso', dateonly => 1 } ),
93         billingdate           => scalar output_pref( { str => scalar $input->param('billingdate'),  dateformat => 'iso', dateonly => 1 } ),
94         shipmentcost          => $shipmentcost,
95         shipmentcost_budgetid => $shipment_budget_id
96     );
97     if ($input->param('reopen')) {
98         ReopenInvoice($invoiceid)
99             if $logged_in_patron->has_permission( { acquisition => 'reopen_closed_invoices' } );
100     } elsif ($input->param('close')) {
101         CloseInvoice($invoiceid);
102     } elsif ($input->param('merge')) {
103         my @sources = $input->multi_param('merge');
104         MergeInvoices($invoiceid, \@sources);
105         defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
106     }
107     $template->param( modified => 1 );
108 }
109 elsif ( $op && $op eq 'delete' ) {
110     DelInvoice($invoiceid);
111     defined($invoice_files) && $invoice_files->DelAllFiles();
112     my $referer = $input->param('referer') || 'invoices.pl';
113     if ($referer) {
114         print $input->redirect($referer);
115         exit 0;
116     }
117 }
118 elsif ( $op && $op eq 'del_adj' ) {
119     my $adjustment_id  = $input->param('adjustment_id');
120     my $del_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id );
121     $del_adj->delete() if ($del_adj);
122 }
123 elsif ( $op && $op eq 'mod_adj' ) {
124     my @adjustment_id  = $input->multi_param('adjustment_id');
125     my @adjustment     = $input->multi_param('adjustment');
126     my @reason         = $input->multi_param('reason');
127     my @note           = $input->multi_param('note');
128     my @budget_id      = $input->multi_param('budget_id');
129     my @encumber_open  = $input->multi_param('encumber_open');
130     my %e_open = map { $_ => 1 } @encumber_open;
131
132     for( my $i=0; $i < scalar @adjustment; $i++ ){
133         if( $adjustment_id[$i] eq 'new' ){
134             next unless ( $adjustment[$i] || $reason[$i] );
135             my $new_adj = Koha::Acquisition::Invoice::Adjustment->new({
136                 invoiceid => $invoiceid,
137                 adjustment => $adjustment[$i],
138                 reason => $reason[$i],
139                 note => $note[$i],
140                 budget_id => $budget_id[$i] || undef,
141                 encumber_open => defined $e_open{ $adjustment_id[$i] } ? 1 : 0,
142             });
143             $new_adj->store();
144         }
145         else {
146             my $old_adj = Koha::Acquisition::Invoice::Adjustments->find( $adjustment_id[$i] );
147             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] ){
148                 $old_adj->timestamp(undef);
149                 $old_adj->adjustment( $adjustment[$i] );
150                 $old_adj->reason( $reason[$i] );
151                 $old_adj->note( $note[$i] );
152                 $old_adj->budget_id( $budget_id[$i] || undef );
153                 $old_adj->encumber_open( $e_open{$adjustment_id[$i]} ? 1 : 0 );
154                 $old_adj->update();
155             }
156         }
157     }
158 }
159
160 my $details = GetInvoiceDetails($invoiceid);
161 my $bookseller = Koha::Acquisition::Booksellers->find( $details->{booksellerid} );
162 my @orders_loop = ();
163 my $orders = $details->{'orders'};
164 my @foot_loop;
165 my %foot;
166 my $shipmentcost = $details->{shipmentcost} || 0;
167 my $total_quantity = 0;
168 my $total_tax_excluded = 0;
169 my $total_tax_included = 0;
170 my $total_tax_value = 0;
171 foreach my $order (@$orders) {
172     my $line = get_infos( $order, $bookseller);
173
174     $line->{total_tax_excluded} = get_rounded_price($line->{unitprice_tax_excluded}) * $line->{quantity};
175     $line->{total_tax_included} = get_rounded_price($line->{unitprice_tax_included}) * $line->{quantity};
176
177     $line->{tax_value} = $line->{tax_value_on_receiving};
178     $line->{tax_rate} = $line->{tax_rate_on_receiving};
179
180     $foot{$$line{tax_rate}}{tax_rate} = $$line{tax_rate};
181     $foot{$$line{tax_rate}}{tax_value} += get_rounded_price($$line{tax_value});
182     $total_tax_value += $$line{tax_value};
183     $foot{$$line{tax_rate}}{quantity}  += $$line{quantity};
184     $total_quantity += $$line{quantity};
185     $foot{$$line{tax_rate}}{total_tax_excluded} += get_rounded_price($$line{total_tax_excluded});
186     $total_tax_excluded += get_rounded_price($$line{total_tax_excluded});
187     $foot{$$line{tax_rate}}{total_tax_included} += get_rounded_price($$line{total_tax_included});
188     $total_tax_included += get_rounded_price($$line{total_tax_included});
189
190     $line->{orderline} = $line->{parent_ordernumber};
191     push @orders_loop, $line;
192 }
193
194 push @foot_loop, map {$_} values %foot;
195
196 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
197
198 # build budget list
199 my $budget_loop = [];
200 my $budgets     = GetBudgetHierarchy();
201 foreach my $r ( @{$budgets} ) {
202     next unless ( CanUserUseBudget( $loggedinuser, $r, $flags ) );
203
204     if ( !defined $r->{budget_amount} || $r->{budget_amount} == 0 ) {
205         next;
206     }
207
208     my $selected = $shipmentcost_budgetid ? $r->{budget_id} eq $shipmentcost_budgetid : 0;
209
210     push @{$budget_loop},
211       {
212         b_id     => $r->{budget_id},
213         b_txt    => $r->{budget_name},
214         b_active => $r->{budget_period_active},
215         selected => $selected,
216       };
217 }
218
219 @{$budget_loop} =
220   sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$budget_loop};
221
222 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({ invoiceid => $details->{'invoiceid'} });
223 if ( $adjustments ) { $template->param( adjustments => $adjustments ); }
224
225 $template->param(
226     invoiceid                   => $details->{'invoiceid'},
227     invoicenumber               => $details->{'invoicenumber'},
228     suppliername                => $details->{'suppliername'},
229     booksellerid                => $details->{'booksellerid'},
230     shipmentdate                => $details->{'shipmentdate'},
231     billingdate                 => $details->{'billingdate'},
232     invoiceclosedate            => $details->{'closedate'},
233     shipmentcost                => $shipmentcost,
234     orders_loop                 => \@orders_loop,
235     foot_loop                   => \@foot_loop,
236     total_quantity              => $total_quantity,
237     total_tax_excluded          => $total_tax_excluded,
238     total_tax_included          => $total_tax_included,
239     total_tax_value             => $total_tax_value,
240     total_tax_excluded_shipment => $total_tax_excluded + $shipmentcost,
241     total_tax_included_shipment => $total_tax_included + $shipmentcost,
242     invoiceincgst               => $bookseller->invoiceincgst,
243     currency                    => Koha::Acquisition::Currencies->get_active,
244     budgets                     => $budget_loop,
245 );
246
247 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
248
249 # FIXME
250 # Fonction dupplicated from basket.pl
251 # Code must to be exported. Where ??
252 sub get_infos {
253     my $order = shift;
254     my $bookseller = shift;
255     my $qty = $order->{'quantity'} || 0;
256     if ( !defined $order->{quantityreceived} ) {
257         $order->{quantityreceived} = 0;
258     }
259     my $budget = GetBudget( $order->{'budget_id'} );
260
261     my %line = %{ $order };
262     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
263     $line{budget_name}    = $budget->{budget_name};
264
265     if ( $line{'title'} ) {
266         my $volume      = $order->{'volume'};
267         my $seriestitle = $order->{'seriestitle'};
268         $line{'title'} .= " / $seriestitle" if $seriestitle;
269         $line{'title'} .= " / $volume"      if $volume;
270     }
271
272     return \%line;
273 }
274
275 output_html_with_http_headers $input, $cookie, $template->output;