Bug 16047: Making category code a required field on creating a group
[koha.git] / acqui / invoice.pl
blob026fd13b9d5471bf224ae8ee0b44fa756d17b4a5
1 #!/usr/bin/perl
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
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 =head1 NAME
21 invoice.pl
23 =head1 DESCRIPTION
25 Invoice details
27 =cut
29 use strict;
30 use warnings;
32 use CGI qw ( -utf8 );
33 use C4::Auth;
34 use C4::Output;
35 use C4::Acquisition;
36 use C4::Budgets;
38 use Koha::Acquisition::Bookseller;
39 use Koha::Misc::Files;
41 my $input = new CGI;
42 my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user(
44 template_name => 'acqui/invoice.tt',
45 query => $input,
46 type => 'intranet',
47 authnotrequired => 0,
48 flagsrequired => { 'acquisition' => '*' },
49 debug => 1,
53 my $invoiceid = $input->param('invoiceid');
54 my $op = $input->param('op');
56 my $invoice_files;
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');
65 if ($referer) {
66 print $input->redirect($referer);
67 exit 0;
70 elsif ( $op && $op eq 'reopen' ) {
71 ReopenInvoice($invoiceid);
72 my $referer = $input->param('referer');
73 if ($referer) {
74 print $input->redirect($referer);
75 exit 0;
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');
83 ModInvoice(
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';
105 if ($referer) {
106 print $input->redirect($referer);
107 exit 0;
112 my $details = GetInvoiceDetails($invoiceid);
113 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $details->{booksellerid} });
114 my @orders_loop = ();
115 my $orders = $details->{'orders'};
116 my $qty_total;
117 my @foot_loop;
118 my %foot;
119 my $total_quantity = 0;
120 my $total_gste = 0;
121 my $total_gsti = 0;
122 my $total_gstvalue = 0;
123 foreach my $order (@$orders) {
124 $order = C4::Acquisition::populate_order_with_prices(
126 order => $order,
127 booksellerid => $bookseller->{id},
128 receiving => 1,
131 my $line = get_infos( $order, $bookseller);
133 $foot{$$line{gstrate}}{gstrate} = $$line{gstrate};
134 $foot{$$line{gstrate}}{gstvalue} += $$line{gstvalue};
135 $total_gstvalue += $$line{gstvalue};
136 $foot{$$line{gstrate}}{quantity} += $$line{quantity};
137 $total_quantity += $$line{quantity};
138 $foot{$$line{gstrate}}{totalgste} += $$line{totalgste};
139 $total_gste += $$line{totalgste};
140 $foot{$$line{gstrate}}{totalgsti} += $$line{totalgsti};
141 $total_gsti += $$line{totalgsti};
143 $line->{orderline} = $line->{parent_ordernumber};
144 push @orders_loop, $line;
147 push @foot_loop, map {$_} values %foot;
149 my $format = "%.2f";
150 my $budgets = GetBudgets();
151 my @budgets_loop;
152 my $shipmentcost_budgetid = $details->{shipmentcost_budgetid};
153 foreach my $budget (@$budgets) {
154 next unless CanUserUseBudget( $loggedinuser, $budget, $flags );
155 my %line = %{$budget};
156 if ( $shipmentcost_budgetid
157 and $budget->{budget_id} == $shipmentcost_budgetid )
159 $line{selected} = 1;
161 push @budgets_loop, \%line;
164 $template->param(
165 invoiceid => $details->{'invoiceid'},
166 invoicenumber => $details->{'invoicenumber'},
167 suppliername => $details->{'suppliername'},
168 booksellerid => $details->{'booksellerid'},
169 shipmentdate => $details->{'shipmentdate'},
170 billingdate => $details->{'billingdate'},
171 invoiceclosedate => $details->{'closedate'},
172 shipmentcost => $details->{'shipmentcost'},
173 orders_loop => \@orders_loop,
174 foot_loop => \@foot_loop,
175 total_quantity => $total_quantity,
176 total_gste => sprintf( $format, $total_gste ),
177 total_gsti => sprintf( $format, $total_gsti ),
178 total_gstvalue => sprintf( $format, $total_gstvalue ),
179 total_gste_shipment => sprintf( $format, $total_gste + $details->{shipmentcost}),
180 total_gsti_shipment => sprintf( $format, $total_gsti + $details->{shipmentcost}),
181 invoiceincgst => $bookseller->{invoiceincgst},
182 currency => GetCurrency()->{currency},
183 budgets_loop => \@budgets_loop,
186 defined( $invoice_files ) && $template->param( files => $invoice_files->GetFilesInfo() );
188 # FIXME
189 # Fonction dupplicated from basket.pl
190 # Code must to be exported. Where ??
191 sub get_infos {
192 my $order = shift;
193 my $bookseller = shift;
194 my $qty = $order->{'quantity'} || 0;
195 if ( !defined $order->{quantityreceived} ) {
196 $order->{quantityreceived} = 0;
198 my $budget = GetBudget( $order->{'budget_id'} );
200 my %line = %{ $order };
201 $line{order_received} = ( $qty == $order->{'quantityreceived'} );
202 $line{budget_name} = $budget->{budget_name};
204 if ( $line{uncertainprice} ) {
205 $template->param( uncertainprices => 1 );
206 $line{rrp} .= ' (Uncertain)';
208 if ( $line{'title'} ) {
209 my $volume = $order->{'volume'};
210 my $seriestitle = $order->{'seriestitle'};
211 $line{'title'} .= " / $seriestitle" if $seriestitle;
212 $line{'title'} .= " / $volume" if $volume;
215 return \%line;
218 output_html_with_http_headers $input, $cookie, $template->output;