Bug 10402: Add ability to enter multiple contacts
[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 under the
7 # terms of the GNU General Public License as published by the Free Software
8 # Foundation; either version 2 of the License, or (at your option) any later
9 # version.
10 #
11 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Koha; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 =head1 NAME
20
21 invoice.pl
22
23 =head1 DESCRIPTION
24
25 Invoice details
26
27 =cut
28
29 use strict;
30 use warnings;
31
32 use CGI;
33 use C4::Auth;
34 use C4::Output;
35 use C4::Acquisition;
36 use C4::Bookseller qw/GetBookSellerFromId/;
37 use C4::Budgets;
38 use Koha::Misc::Files;
39
40 my $input = new CGI;
41 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
42     {
43         template_name   => 'acqui/invoice.tt',
44         query           => $input,
45         type            => 'intranet',
46         authnotrequired => 0,
47         flagsrequired   => { 'acquisition' => '*' },
48         debug           => 1,
49     }
50 );
51
52 my $invoiceid = $input->param('invoiceid');
53 my $op        = $input->param('op');
54
55 my $invoice_files;
56 if ( C4::Context->preference('AcqEnableFiles') ) {
57     $invoice_files = Koha::Misc::Files->new(
58         tabletag => 'aqinvoices', recordid => $invoiceid );
59 }
60
61 if ( $op && $op eq 'close' ) {
62     CloseInvoice($invoiceid);
63     my $referer = $input->param('referer');
64     if ($referer) {
65         print $input->redirect($referer);
66         exit 0;
67     }
68 }
69 elsif ( $op && $op eq 'reopen' ) {
70     ReopenInvoice($invoiceid);
71     my $referer = $input->param('referer');
72     if ($referer) {
73         print $input->redirect($referer);
74         exit 0;
75     }
76 }
77 elsif ( $op && $op eq 'mod' ) {
78     my $shipmentdate       = $input->param('shipmentdate');
79     my $billingdate        = $input->param('billingdate');
80     my $shipmentcost       = $input->param('shipmentcost');
81     my $shipment_budget_id = $input->param('shipment_budget_id');
82     ModInvoice(
83         invoiceid             => $invoiceid,
84         shipmentdate          => C4::Dates->new($shipmentdate)->output("iso"),
85         billingdate           => C4::Dates->new($billingdate)->output("iso"),
86         shipmentcost          => $shipmentcost,
87         shipmentcost_budgetid => $shipment_budget_id
88     );
89     if ($input->param('reopen')) {
90         ReopenInvoice($invoiceid);
91     } elsif ($input->param('close')) {
92         CloseInvoice($invoiceid);
93     } elsif ($input->param('merge')) {
94         my @sources = $input->param('merge');
95         MergeInvoices($invoiceid, \@sources);
96         defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
97     }
98     $template->param( modified => 1 );
99 }
100 elsif ( $op && $op eq 'delete' ) {
101     DelInvoice($invoiceid);
102     defined($invoice_files) && $invoice_files->DelAllFiles();
103     my $referer = $input->param('referer') || 'invoices.pl';
104     if ($referer) {
105         print $input->redirect($referer);
106         exit 0;
107     }
108 }
109
110
111 my $details = GetInvoiceDetails($invoiceid);
112 my $bookseller = GetBookSellerFromId($details->{booksellerid});
113 my @orders_loop = ();
114 my $orders = $details->{'orders'};
115 my $qty_total;
116 my @foot_loop;
117 my %foot;
118 my $total_quantity = 0;
119 my $total_gste = 0;
120 my $total_gsti = 0;
121 my $total_gstvalue = 0;
122 foreach my $order (@$orders) {
123     my $line = get_infos( $order, $bookseller);
124
125     $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
126     $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
127     $total_gstvalue += $$line{gstvalue};
128     $foot{$$line{gstgsti}}{quantity}  += $$line{quantity};
129     $total_quantity += $$line{quantity};
130     $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
131     $total_gste += $$line{totalgste};
132     $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
133     $total_gsti += $$line{totalgsti};
134
135     $line->{orderline} = $line->{parent_ordernumber};
136     push @orders_loop, $line;
137 }
138
139 push @foot_loop, map {$_} values %foot;
140
141 my $format = "%.2f";
142 my $budgets = GetBudgets();
143 my @budgets_loop;
144 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
145 foreach my $budget (@$budgets) {
146     next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
147     my %line = %{$budget};
148     if (    $shipmentcost_budgetid
149         and $budget->{budget_id} == $shipmentcost_budgetid )
150     {
151         $line{selected} = 1;
152     }
153     push @budgets_loop, \%line;
154 }
155
156 $template->param(
157     invoiceid        => $details->{'invoiceid'},
158     invoicenumber    => $details->{'invoicenumber'},
159     suppliername     => $details->{'suppliername'},
160     booksellerid     => $details->{'booksellerid'},
161     shipmentdate     => $details->{'shipmentdate'},
162     billingdate      => $details->{'billingdate'},
163     invoiceclosedate => $details->{'closedate'},
164     shipmentcost     => $details->{'shipmentcost'},
165     orders_loop      => \@orders_loop,
166     foot_loop        => \@foot_loop,
167     total_quantity   => $total_quantity,
168     total_gste       => sprintf( $format, $total_gste ),
169     total_gsti       => sprintf( $format, $total_gsti ),
170     total_gstvalue   => sprintf( $format, $total_gstvalue ),
171     total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
172     total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
173     invoiceincgst    => $bookseller->{invoiceincgst},
174     currency         => GetCurrency()->{currency},
175     budgets_loop     => \@budgets_loop,
176 );
177
178 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
179
180 # FIXME
181 # Fonction dupplicated from basket.pl
182 # Code must to be exported. Where ??
183 sub get_infos {
184     my $order = shift;
185     my $bookseller = shift;
186     my $qty = $order->{'quantity'} || 0;
187     if ( !defined $order->{quantityreceived} ) {
188         $order->{quantityreceived} = 0;
189     }
190     my $budget = GetBudget( $order->{'budget_id'} );
191
192     my %line = %{ $order };
193     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
194     $line{budget_name}    = $budget->{budget_name};
195     if ( $bookseller->{'listincgst'} ) {
196         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
197         $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
198         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} );
199         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) );
200         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
201         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
202         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
203     } else {
204         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
205         $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
206         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) );
207         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} );
208         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
209         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
210         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
211     }
212
213     if ( $line{uncertainprice} ) {
214         $template->param( uncertainprices => 1 );
215         $line{rrp} .= ' (Uncertain)';
216     }
217     if ( $line{'title'} ) {
218         my $volume      = $order->{'volume'};
219         my $seriestitle = $order->{'seriestitle'};
220         $line{'title'} .= " / $seriestitle" if $seriestitle;
221         $line{'title'} .= " / $volume"      if $volume;
222     } else {
223         $line{'title'} = "Deleted bibliographic notice, can't find title.";
224     }
225
226     return \%line;
227 }
228
229 output_html_with_http_headers $input, $cookie, $template->output;