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.
38 use Koha
::Acquisition
::Bookseller
;
39 use Koha
::Misc
::Files
;
42 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user
(
44 template_name
=> 'acqui/invoice.tt',
48 flagsrequired
=> { 'acquisition' => '*' },
53 my $invoiceid = $input->param('invoiceid');
54 my $op = $input->param('op');
57 if ( C4
::Context
->preference('AcqEnableFiles') ) {
58 $invoice_files = Koha
::Misc
::Files
->new(
59 tabletag
=> 'aqinvoices', recordid
=> $invoiceid );
62 if ( $op && $op eq 'close' ) {
63 CloseInvoice
($invoiceid);
64 my $referer = $input->param('referer');
66 print $input->redirect($referer);
70 elsif ( $op && $op eq 'reopen' ) {
71 ReopenInvoice
($invoiceid);
72 my $referer = $input->param('referer');
74 print $input->redirect($referer);
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');
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
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);
99 $template->param( modified
=> 1 );
101 elsif ( $op && $op eq 'delete' ) {
102 DelInvoice
($invoiceid);
103 defined($invoice_files) && $invoice_files->DelAllFiles();
104 my $referer = $input->param('referer') || 'invoices.pl';
106 print $input->redirect($referer);
112 my $details = GetInvoiceDetails
($invoiceid);
113 my $bookseller = Koha
::Acquisition
::Bookseller
->fetch({ id
=> $details->{booksellerid
} });
114 my @orders_loop = ();
115 my $orders = $details->{'orders'};
119 my $total_quantity = 0;
122 my $total_gstvalue = 0;
123 foreach my $order (@
$orders) {
124 my $line = get_infos
( $order, $bookseller);
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
};
136 $line->{orderline
} = $line->{parent_ordernumber
};
137 push @orders_loop, $line;
140 push @foot_loop, map {$_} values %foot;
143 my $budgets = GetBudgets
();
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 )
154 push @budgets_loop, \
%line;
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,
179 defined( $invoice_files ) && $template->param( files
=> $invoice_files->GetFilesInfo() );
182 # Fonction dupplicated from basket.pl
183 # Code must to be exported. Where ??
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 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
} );
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
} );
214 if ( $line{uncertainprice
} ) {
215 $template->param( uncertainprices
=> 1 );
216 $line{rrp
} .= ' (Uncertain)';
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;
224 $line{'title'} = "Deleted bibliographic notice, can't find title.";
230 output_html_with_http_headers
$input, $cookie, $template->output;