Bug 5339: Invoices management improvement
[koha_fer] / 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
39 my $input = new CGI;
40 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
41     template_name   => 'acqui/invoice.tmpl',
42     query           => $input,
43     type            => 'intranet',
44     authnotrequired => 0,
45     flagsrequired   => { 'acquisition' => '*' },
46     debug           => 1,
47 } );
48
49 my $invoiceid = $input->param('invoiceid');
50 my $op = $input->param('op');
51
52 if($op && $op eq 'close') {
53     CloseInvoice($invoiceid);
54     my $referer = $input->param('referer');
55     if($referer) {
56         print $input->redirect($referer);
57         exit 0;
58     }
59 }elsif($op && $op eq 'reopen') {
60     ReopenInvoice($invoiceid);
61     my $referer = $input->param('referer');
62     if($referer) {
63         print $input->redirect($referer);
64         exit 0;
65     }
66 }elsif($op && $op eq 'mod') {
67     my $shipmentdate = $input->param('shipmentdate');
68     my $billingdate = $input->param('billingdate');
69     my $shipmentcost = $input->param('shipmentcost');
70     my $shipment_budget_id = $input->param('shipment_budget_id');
71     ModInvoice(
72         invoiceid => $invoiceid,
73         shipmentdate   => C4::Dates->new($shipmentdate)->output("iso"),
74         billingdate   => C4::Dates->new($billingdate)->output("iso"),
75         shipmentcost  => $shipmentcost,
76         shipmentcost_budgetid => $shipment_budget_id
77     );
78     $template->param(modified => 1);
79 }
80
81 my $details = GetInvoiceDetails($invoiceid);
82 my $bookseller = GetBookSellerFromId($details->{booksellerid});
83 my @orders_loop = ();
84 my $orders = $details->{'orders'};
85 my $qty_total;
86 my @books_loop;
87 my @book_foot_loop;
88 my %foot;
89 my $total_quantity = 0;
90 my $total_rrp = 0;
91 my $total_est = 0;
92 foreach my $order (@$orders) {
93     my $line = get_infos( $order, $bookseller);
94
95     $total_quantity += $$line{quantity};
96     $total_rrp += $order->{quantity} * $order->{rrp};
97     $total_est += $order->{quantity} * $order->{'ecost'};
98
99     my %row = (%$order, %$line);
100     push @orders_loop, \%row;
101 }
102
103 my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
104 my $discount = $bookseller->{'discount'} ? ($bookseller->{discount} / 100) : 0;
105 my $total_est_gste;
106 my $total_est_gsti;
107 my $total_rrp_gsti; # RRP Total, GST included
108 my $total_rrp_gste; # RRP Total, GST excluded
109 my $gist_est;
110 my $gist_rrp;
111 if ($gist){
112     # if we have GST
113     if ( $bookseller->{'listincgst'} ) {
114         # if prices already includes GST
115
116         # we know $total_rrp_gsti
117         $total_rrp_gsti = $total_rrp;
118         # and can reverse compute other values
119         $total_rrp_gste = $total_rrp_gsti / ( $gist + 1 );
120
121         $gist_rrp       = $total_rrp_gsti - $total_rrp_gste;
122         $total_est_gste = $total_rrp_gste - ( $total_rrp_gste * $discount );
123         $total_est_gsti = $total_est;
124     } else {
125         # if prices does not include GST
126
127         # then we use the common way to compute other values
128         $total_rrp_gste = $total_rrp;
129         $gist_rrp       = $total_rrp_gste * $gist;
130         $total_rrp_gsti = $total_rrp_gste + $gist_rrp;
131         $total_est_gste = $total_est;
132         $total_est_gsti = $total_rrp_gsti - ( $total_rrp_gsti * $discount );
133    }
134    $gist_est = $gist_rrp - ( $gist_rrp * $discount );
135 } else {
136     $total_rrp_gste = $total_rrp_gsti = $total_rrp;
137     $total_est_gste = $total_est_gsti = $total_est;
138     $gist_rrp = $gist_est = 0;
139 }
140 my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
141
142 my $format = "%.2f";
143 $template->param(
144     total_rrp_gste => sprintf($format, $total_rrp_gste),
145     total_rrp_gsti => sprintf($format, $total_rrp_gsti),
146     total_est_gste => sprintf($format, $total_est_gste),
147     total_est_gsti => sprintf($format, $total_est_gsti),
148     gist_rrp => sprintf($format, $gist_rrp),
149     gist_est => sprintf($format, $gist_est),
150     total_gsti_shipment => sprintf($format, $total_gsti_shipment),
151     gist => sprintf($format, $gist * 100),
152 );
153
154 my $budgets = GetBudgets();
155 my @budgets_loop;
156 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
157 foreach my $budget (@$budgets) {
158     next unless CanUserUseBudget($loggedinuser, $budget, $flags);
159     my %line = %{ $budget };
160     if($shipmentcost_budgetid and $budget->{budget_id} == $shipmentcost_budgetid) {
161         $line{selected} = 1;
162     }
163     push @budgets_loop, \%line;
164 }
165
166 $template->param(
167     invoiceid        => $details->{'invoiceid'},
168     invoicenumber    => $details->{'invoicenumber'},
169     suppliername     => $details->{'suppliername'},
170     supplierid       => $details->{'booksellerid'},
171     datereceived     => $details->{'datereceived'},
172     shipmentdate     => $details->{'shipmentdate'},
173     billingdate      => $details->{'billingdate'},
174     invoiceclosedate => $details->{'closedate'},
175     shipmentcost     => sprintf($format, $details->{'shipmentcost'} || 0),
176     orders_loop      => \@orders_loop,
177     total_quantity   => $total_quantity,
178     invoiceincgst    => $bookseller->{invoiceincgst},
179     currency         => $bookseller->{listprice},
180     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
181     budgets_loop     => \@budgets_loop,
182 );
183
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     $line{total} = $qty * $order->{ecost};
197
198     if ( $line{uncertainprice} ) {
199         $line{rrp} .= ' (Uncertain)';
200     }
201     if ( $line{'title'} ) {
202         my $volume      = $order->{'volume'};
203         my $seriestitle = $order->{'seriestitle'};
204         $line{'title'} .= " / $seriestitle" if $seriestitle;
205         $line{'title'} .= " / $volume"      if $volume;
206     } else {
207         $line{'title'} = "Deleted bibliographic notice, can't find title.";
208     }
209
210     return \%line;
211 }
212
213 output_html_with_http_headers $input, $cookie, $template->output;