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