Bug 12896: Move the bookseller-related code into Koha::Acquisition::Bookseller
[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::Budgets;
37
38 use Koha::Acquisition::Bookseller;
39 use Koha::Misc::Files;
40
41 my $input = new CGI;
42 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
43     {
44         template_name   => 'acqui/invoice.tt',
45         query           => $input,
46         type            => 'intranet',
47         authnotrequired => 0,
48         flagsrequired   => { 'acquisition' => '*' },
49         debug           => 1,
50     }
51 );
52
53 my $invoiceid = $input->param('invoiceid');
54 my $op        = $input->param('op');
55
56 my $invoice_files;
57 if ( C4::Context->preference('AcqEnableFiles') ) {
58     $invoice_files = Koha::Misc::Files->new(
59         tabletag => 'aqinvoices', recordid => $invoiceid );
60 }
61
62 if ( $op && $op eq 'close' ) {
63     CloseInvoice($invoiceid);
64     my $referer = $input->param('referer');
65     if ($referer) {
66         print $input->redirect($referer);
67         exit 0;
68     }
69 }
70 elsif ( $op && $op eq 'reopen' ) {
71     ReopenInvoice($invoiceid);
72     my $referer = $input->param('referer');
73     if ($referer) {
74         print $input->redirect($referer);
75         exit 0;
76     }
77 }
78 elsif ( $op && $op eq 'mod' ) {
79     my $shipmentdate       = $input->param('shipmentdate');
80     my $billingdate        = $input->param('billingdate');
81     my $shipmentcost       = $input->param('shipmentcost');
82     my $shipment_budget_id = $input->param('shipment_budget_id');
83     ModInvoice(
84         invoiceid             => $invoiceid,
85         shipmentdate          => C4::Dates->new($shipmentdate)->output("iso"),
86         billingdate           => C4::Dates->new($billingdate)->output("iso"),
87         shipmentcost          => $shipmentcost,
88         shipmentcost_budgetid => $shipment_budget_id
89     );
90     if ($input->param('reopen')) {
91         ReopenInvoice($invoiceid);
92     } elsif ($input->param('close')) {
93         CloseInvoice($invoiceid);
94     } elsif ($input->param('merge')) {
95         my @sources = $input->param('merge');
96         MergeInvoices($invoiceid, \@sources);
97         defined($invoice_files) && $invoice_files->MergeFileRecIds(@sources);
98     }
99     $template->param( modified => 1 );
100 }
101 elsif ( $op && $op eq 'delete' ) {
102     DelInvoice($invoiceid);
103     defined($invoice_files) && $invoice_files->DelAllFiles();
104     my $referer = $input->param('referer') || 'invoices.pl';
105     if ($referer) {
106         print $input->redirect($referer);
107         exit 0;
108     }
109 }
110
111
112 my $details = GetInvoiceDetails($invoiceid);
113 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $details->{booksellerid} });
114 my @orders_loop = ();
115 my $orders = $details->{'orders'};
116 my $qty_total;
117 my @foot_loop;
118 my %foot;
119 my $total_quantity = 0;
120 my $total_gste = 0;
121 my $total_gsti = 0;
122 my $total_gstvalue = 0;
123 foreach my $order (@$orders) {
124     my $line = get_infos( $order, $bookseller);
125
126     $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
127     $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
128     $total_gstvalue += $$line{gstvalue};
129     $foot{$$line{gstgsti}}{quantity}  += $$line{quantity};
130     $total_quantity += $$line{quantity};
131     $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
132     $total_gste += $$line{totalgste};
133     $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
134     $total_gsti += $$line{totalgsti};
135
136     $line->{orderline} = $line->{parent_ordernumber};
137     push @orders_loop, $line;
138 }
139
140 push @foot_loop, map {$_} values %foot;
141
142 my $format = "%.2f";
143 my $budgets = GetBudgets();
144 my @budgets_loop;
145 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
146 foreach my $budget (@$budgets) {
147     next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
148     my %line = %{$budget};
149     if (    $shipmentcost_budgetid
150         and $budget->{budget_id} == $shipmentcost_budgetid )
151     {
152         $line{selected} = 1;
153     }
154     push @budgets_loop, \%line;
155 }
156
157 $template->param(
158     invoiceid        => $details->{'invoiceid'},
159     invoicenumber    => $details->{'invoicenumber'},
160     suppliername     => $details->{'suppliername'},
161     booksellerid     => $details->{'booksellerid'},
162     shipmentdate     => $details->{'shipmentdate'},
163     billingdate      => $details->{'billingdate'},
164     invoiceclosedate => $details->{'closedate'},
165     shipmentcost     => $details->{'shipmentcost'},
166     orders_loop      => \@orders_loop,
167     foot_loop        => \@foot_loop,
168     total_quantity   => $total_quantity,
169     total_gste       => sprintf( $format, $total_gste ),
170     total_gsti       => sprintf( $format, $total_gsti ),
171     total_gstvalue   => sprintf( $format, $total_gstvalue ),
172     total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
173     total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
174     invoiceincgst    => $bookseller->{invoiceincgst},
175     currency         => GetCurrency()->{currency},
176     budgets_loop     => \@budgets_loop,
177 );
178
179 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
180
181 # FIXME
182 # Fonction dupplicated from basket.pl
183 # Code must to be exported. Where ??
184 sub get_infos {
185     my $order = shift;
186     my $bookseller = shift;
187     my $qty = $order->{'quantity'} || 0;
188     if ( !defined $order->{quantityreceived} ) {
189         $order->{quantityreceived} = 0;
190     }
191     my $budget = GetBudget( $order->{'budget_id'} );
192
193     my %line = %{ $order };
194     $line{order_received} = ( $qty == $order->{'quantityreceived'} );
195     $line{budget_name}    = $budget->{budget_name};
196     if ( $bookseller->{'listincgst'} ) {
197         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
198         $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
199         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} );
200         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) );
201         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
202         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
203         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
204     } else {
205         $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
206         $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
207         $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) );
208         $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} );
209         $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
210         $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
211         $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
212     }
213
214     if ( $line{uncertainprice} ) {
215         $template->param( uncertainprices => 1 );
216         $line{rrp} .= ' (Uncertain)';
217     }
218     if ( $line{'title'} ) {
219         my $volume      = $order->{'volume'};
220         my $seriestitle = $order->{'seriestitle'};
221         $line{'title'} .= " / $seriestitle" if $seriestitle;
222         $line{'title'} .= " / $volume"      if $volume;
223     } else {
224         $line{'title'} = "Deleted bibliographic notice, can't find title.";
225     }
226
227     return \%line;
228 }
229
230 output_html_with_http_headers $input, $cookie, $template->output;