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(
42 template_name => 'acqui/invoice.tmpl',
46 flagsrequired => { 'acquisition' => '*' },
51 my $invoiceid = $input->param('invoiceid');
52 my $op = $input->param('op');
54 if ( $op && $op eq 'close' ) {
55 CloseInvoice($invoiceid);
56 my $referer = $input->param('referer');
58 print $input->redirect($referer);
62 elsif ( $op && $op eq 'reopen' ) {
63 ReopenInvoice($invoiceid);
64 my $referer = $input->param('referer');
66 print $input->redirect($referer);
70 elsif ( $op && $op eq 'mod' ) {
71 my $shipmentdate = $input->param('shipmentdate');
72 my $billingdate = $input->param('billingdate');
73 my $shipmentcost = $input->param('shipmentcost');
74 my $shipment_budget_id = $input->param('shipment_budget_id');
76 invoiceid => $invoiceid,
77 shipmentdate => C4::Dates->new($shipmentdate)->output("iso"),
78 billingdate => C4::Dates->new($billingdate)->output("iso"),
79 shipmentcost => $shipmentcost,
80 shipmentcost_budgetid => $shipment_budget_id
82 if ($input->param('reopen')) {
83 ReopenInvoice($invoiceid);
84 } elsif ($input->param('close')) {
85 CloseInvoice($invoiceid);
86 } elsif ($input->param('merge')) {
87 my @sources = $input->param('merge');
88 MergeInvoices($invoiceid, \@sources);
90 $template->param( modified => 1 );
92 elsif ( $op && $op eq 'delete' ) {
93 DelInvoice($invoiceid);
94 my $referer = $input->param('referer') || 'invoices.pl';
96 print $input->redirect($referer);
102 my $details = GetInvoiceDetails($invoiceid);
103 my $bookseller = GetBookSellerFromId($details->{booksellerid});
104 my @orders_loop = ();
105 my $orders = $details->{'orders'};
109 my $total_quantity = 0;
112 my $total_gstvalue = 0;
113 foreach my $order (@$orders) {
114 my $line = get_infos( $order, $bookseller);
116 $foot{$$line{gstgsti}}{gstgsti} = $$line{gstgsti};
117 $foot{$$line{gstgsti}}{gstvalue} += $$line{gstvalue};
118 $total_gstvalue += $$line{gstvalue};
119 $foot{$$line{gstgsti}}{quantity} += $$line{quantity};
120 $total_quantity += $$line{quantity};
121 $foot{$$line{gstgsti}}{totalgste} += $$line{totalgste};
122 $total_gste += $$line{totalgste};
123 $foot{$$line{gstgsti}}{totalgsti} += $$line{totalgsti};
124 $total_gsti += $$line{totalgsti};
126 my %row = %{ $order, $line };
127 $row{'orderline'} = $row{'parent_ordernumber'};
128 push @orders_loop, \%row;
131 push @foot_loop, map {$_} values %foot;
134 my $budgets = GetBudgets();
136 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
137 foreach my $budget (@$budgets) {
138 next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
139 my %line = %{$budget};
140 if ( $shipmentcost_budgetid
141 and $budget->{budget_id} == $shipmentcost_budgetid )
145 push @budgets_loop, \%line;
149 invoiceid => $details->{'invoiceid'},
150 invoicenumber => $details->{'invoicenumber'},
151 suppliername => $details->{'suppliername'},
152 booksellerid => $details->{'booksellerid'},
153 datereceived => $details->{'datereceived'},
154 billingdate => C4::Dates->new($details->{'billingdate'}, "iso")->output(),
155 invoiceclosedate => $details->{'invoiceclosedate'},
156 shipmentcost => $details->{'shipmentcost'},
157 orders_loop => \@orders_loop,
158 foot_loop => \@foot_loop,
159 total_quantity => $total_quantity,
160 total_gste => sprintf( $format, $total_gste ),
161 total_gsti => sprintf( $format, $total_gsti ),
162 total_gstvalue => sprintf( $format, $total_gstvalue ),
163 total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
164 total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
165 invoiceincgst => $bookseller->{invoiceincgst},
166 currency => GetCurrency()->{currency},
167 budgets_loop => \@budgets_loop,
171 # Fonction dupplicated from basket.pl
172 # Code must to be exported. Where ??
175 my $bookseller = shift;
176 my $qty = $order->{'quantity'} || 0;
177 if ( !defined $order->{quantityreceived} ) {
178 $order->{quantityreceived} = 0;
180 my $budget = GetBudget( $order->{'budget_id'} );
182 my %line = %{ $order };
183 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
184 $line{budget_name} = $budget->{budget_name};
185 if ( $bookseller->{'listincgst'} ) {
186 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
187 $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
188 $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} );
189 $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) );
190 $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
191 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
192 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
194 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
195 $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
196 $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) );
197 $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} );
198 $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
199 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
200 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
203 if ( $line{uncertainprice} ) {
204 $template->param( uncertainprices => 1 );
205 $line{rrp} .= ' (Uncertain)';
207 if ( $line{'title'} ) {
208 my $volume = $order->{'volume'};
209 my $seriestitle = $order->{'seriestitle'};
210 $line{'title'} .= " / $seriestitle" if $seriestitle;
211 $line{'title'} .= " / $volume" if $volume;
213 $line{'title'} = "Deleted bibliographic notice, can't find title.";
219 output_html_with_http_headers $input, $cookie, $template->output;