3 # Copyright 2011 BibLibre SARL
4 # This file is part of Koha.
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
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.
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.
36 use C4::Bookseller qw/GetBookSellerFromId/;
40 my ($template, $loggedinuser, $cookie, $flags) = get_template_and_user( {
41 template_name => 'acqui/invoice.tmpl',
45 flagsrequired => { 'acquisition' => '*' },
49 my $invoiceid = $input->param('invoiceid');
50 my $op = $input->param('op');
52 if($op && $op eq 'close') {
53 CloseInvoice($invoiceid);
54 my $referer = $input->param('referer');
56 print $input->redirect($referer);
59 }elsif($op && $op eq 'reopen') {
60 ReopenInvoice($invoiceid);
61 my $referer = $input->param('referer');
63 print $input->redirect($referer);
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');
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
78 $template->param(modified => 1);
81 my $details = GetInvoiceDetails($invoiceid);
82 my $bookseller = GetBookSellerFromId($details->{booksellerid});
84 my $orders = $details->{'orders'};
89 my $total_quantity = 0;
92 foreach my $order (@$orders) {
93 my $line = get_infos( $order, $bookseller);
95 $total_quantity += $$line{quantity};
96 $total_rrp += $order->{quantity} * $order->{rrp};
97 $total_est += $order->{quantity} * $order->{'ecost'};
99 my %row = (%$order, %$line);
100 push @orders_loop, \%row;
103 my $gist = $bookseller->{gstrate} // C4::Context->preference("gist") // 0;
104 my $discount = $bookseller->{'discount'} ? ($bookseller->{discount} / 100) : 0;
107 my $total_rrp_gsti; # RRP Total, GST included
108 my $total_rrp_gste; # RRP Total, GST excluded
113 if ( $bookseller->{'listincgst'} ) {
114 # if prices already includes GST
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 );
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;
125 # if prices does not include GST
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 );
134 $gist_est = $gist_rrp - ( $gist_rrp * $discount );
136 $total_rrp_gste = $total_rrp_gsti = $total_rrp;
137 $total_est_gste = $total_est_gsti = $total_est;
138 $gist_rrp = $gist_est = 0;
140 my $total_gsti_shipment = $total_est_gsti + $details->{shipmentcost};
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),
154 my $budgets = GetBudgets();
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) {
163 push @budgets_loop, \%line;
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,
186 my $bookseller = shift;
187 my $qty = $order->{'quantity'} || 0;
188 if ( !defined $order->{quantityreceived} ) {
189 $order->{quantityreceived} = 0;
191 my $budget = GetBudget( $order->{'budget_id'} );
193 my %line = %{ $order };
194 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
195 $line{budget_name} = $budget->{budget_name};
196 $line{total} = $qty * $order->{ecost};
198 if ( $line{uncertainprice} ) {
199 $line{rrp} .= ' (Uncertain)';
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;
207 $line{'title'} = "Deleted bibliographic notice, can't find title.";
213 output_html_with_http_headers $input, $cookie, $template->output;