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 $line->{orderline} = $line->{parent_ordernumber};
127 push @orders_loop, $line;
130 push @foot_loop, map {$_} values %foot;
133 my $budgets = GetBudgets();
135 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
136 foreach my $budget (@$budgets) {
137 next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
138 my %line = %{$budget};
139 if ( $shipmentcost_budgetid
140 and $budget->{budget_id} == $shipmentcost_budgetid )
144 push @budgets_loop, \%line;
148 invoiceid => $details->{'invoiceid'},
149 invoicenumber => $details->{'invoicenumber'},
150 suppliername => $details->{'suppliername'},
151 booksellerid => $details->{'booksellerid'},
152 datereceived => $details->{'datereceived'},
153 billingdate => C4::Dates->new($details->{'billingdate'}, "iso")->output(),
154 invoiceclosedate => $details->{'invoiceclosedate'},
155 shipmentcost => $details->{'shipmentcost'},
156 orders_loop => \@orders_loop,
157 foot_loop => \@foot_loop,
158 total_quantity => $total_quantity,
159 total_gste => sprintf( $format, $total_gste ),
160 total_gsti => sprintf( $format, $total_gsti ),
161 total_gstvalue => sprintf( $format, $total_gstvalue ),
162 total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
163 total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
164 invoiceincgst => $bookseller->{invoiceincgst},
165 currency => GetCurrency()->{currency},
166 budgets_loop => \@budgets_loop,
170 # Fonction dupplicated from basket.pl
171 # Code must to be exported. Where ??
174 my $bookseller = shift;
175 my $qty = $order->{'quantity'} || 0;
176 if ( !defined $order->{quantityreceived} ) {
177 $order->{quantityreceived} = 0;
179 my $budget = GetBudget( $order->{'budget_id'} );
181 my %line = %{ $order };
182 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
183 $line{budget_name} = $budget->{budget_name};
184 if ( $bookseller->{'listincgst'} ) {
185 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
186 $line{gstgste} = sprintf( "%.2f", $line{gstgsti} / ( 1 + ( $line{gstgsti} / 100 ) ) );
187 $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} );
188 $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} / ( 1 + ( $line{gstgsti} / 100 ) ) );
189 $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
190 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
191 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
193 $line{gstgsti} = sprintf( "%.2f", $line{gstrate} * 100 );
194 $line{gstgste} = sprintf( "%.2f", $line{gstrate} * 100 );
195 $line{actualcostgsti} = sprintf( "%.2f", $line{unitprice} * ( 1 + ( $line{gstrate} ) ) );
196 $line{actualcostgste} = sprintf( "%.2f", $line{unitprice} );
197 $line{gstvalue} = sprintf( "%.2f", ( $line{actualcostgsti} - $line{actualcostgste} ) * $line{quantity});
198 $line{totalgste} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgste} );
199 $line{totalgsti} = sprintf( "%.2f", $order->{quantity} * $line{actualcostgsti} );
202 if ( $line{uncertainprice} ) {
203 $template->param( uncertainprices => 1 );
204 $line{rrp} .= ' (Uncertain)';
206 if ( $line{'title'} ) {
207 my $volume = $order->{'volume'};
208 my $seriestitle = $order->{'seriestitle'};
209 $line{'title'} .= " / $seriestitle" if $seriestitle;
210 $line{'title'} .= " / $volume" if $volume;
212 $line{'title'} = "Deleted bibliographic notice, can't find title.";
218 output_html_with_http_headers $input, $cookie, $template->output;