Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Author : Frédérick Capovilla, 2011 - SYS-TECH |
6 |
# Modified by : Élyse Morin, 2012 - Libéo |
7 |
# |
8 |
# Koha is free software; you can redistribute it and/or modify it under the |
9 |
# terms of the GNU General Public License as published by the Free Software |
10 |
# Foundation; either version 2 of the License, or (at your option) any later |
11 |
# version. |
12 |
# |
13 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
14 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
15 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License along with |
18 |
# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
19 |
# Suite 330, Boston, MA 02111-1307 USA |
20 |
|
21 |
|
22 |
=head1 orders_by_budget |
23 |
|
24 |
This script displays all orders associated to a selected budget. |
25 |
|
26 |
=cut |
27 |
|
28 |
use strict; |
29 |
use warnings; |
30 |
|
31 |
use CGI; |
32 |
use C4::Auth; |
33 |
use C4::Output; |
34 |
use C4::Budgets; |
35 |
use C4::Biblio; |
36 |
use C4::Reports; |
37 |
use C4::Dates qw/format_date/; |
38 |
use C4::SQLHelper qw<:all>; |
39 |
use C4::Acquisition; #GetBasket() |
40 |
|
41 |
|
42 |
my $query = new CGI; |
43 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
44 |
{ |
45 |
template_name => "reports/orders_by_budget.tt", |
46 |
query => $query, |
47 |
type => "intranet", |
48 |
authnotrequired => 0, |
49 |
flagsrequired => { reports => '*' }, |
50 |
debug => 1, |
51 |
} |
52 |
); |
53 |
|
54 |
my $params = $query->Vars; |
55 |
my $get_orders = $params->{'get_orders'}; |
56 |
|
57 |
if ( $get_orders ) { |
58 |
my $budgetfilter = $params->{'budgetfilter'} || undef; |
59 |
my $total_quantity = 0; |
60 |
my $total_rrp = 0; |
61 |
my $total_ecost = 0; |
62 |
my %budget_name; |
63 |
|
64 |
# Fetch the orders |
65 |
my @orders; |
66 |
unless($budgetfilter) { |
67 |
# If no budget filter was selected, get the orders of all budgets |
68 |
my @budgets = C4::Budgets::GetBudgetsReport(); |
69 |
foreach my $budget (@budgets) { |
70 |
push(@orders, $budget); |
71 |
$budget_name{$budget->{'budget_id'}} = $budget->{'budget_name'}; |
72 |
} |
73 |
} |
74 |
else { |
75 |
if ($budgetfilter eq 'activebudgets') { |
76 |
# If all active budgets's option was selected, get the orders of all active budgets |
77 |
my @active_budgets = C4::Budgets::GetActiveBudgetsReport(); |
78 |
foreach my $active_budget (@active_budgets) |
79 |
{ |
80 |
push(@orders, $active_budget); |
81 |
$budget_name{$active_budget->{'budget_id'}} = $active_budget->{'budget_name'}; |
82 |
} |
83 |
} |
84 |
else { |
85 |
# A budget filter was selected, only get the orders for the selected budget |
86 |
my @filtered_budgets = C4::Budgets::GetBudgetReport($budgetfilter); |
87 |
foreach my $budget (@filtered_budgets) |
88 |
{ |
89 |
push(@orders, $budget); |
90 |
$budget_name{$budget->{'budget_id'}} = $budget->{'budget_name'}; |
91 |
} |
92 |
if (@filtered_budgets[0]) { |
93 |
$template->param( |
94 |
current_budget_name => @filtered_budgets[0]->{'budget_name'}, |
95 |
); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
# Format the order's informations |
101 |
foreach my $order (@orders) { |
102 |
# Get the title of the ordered item |
103 |
my (undef, $biblio) = C4::Biblio::GetBiblio($order->{'biblionumber'}); |
104 |
my $basket = C4::Acquisition::GetBasket($order->{'basketno'}); |
105 |
|
106 |
$order->{'authorisedbyname'} = $basket->{'authorisedbyname'}; |
107 |
|
108 |
$order->{'title'} = $biblio->{'title'} || $order->{'biblionumber'}; |
109 |
|
110 |
$order->{'total_rrp'} = $order->{'quantity'} * $order->{'rrp'}; |
111 |
$order->{'total_ecost'} = $order->{'quantity'} * $order->{'ecost'}; |
112 |
|
113 |
# Format the dates and currencies correctly |
114 |
$order->{'datereceived'} = format_date($order->{'datereceived'}); |
115 |
$order->{'entrydate'} = format_date($order->{'entrydate'}); |
116 |
$order->{'listprice'} = sprintf( "%.2f", $order->{'listprice'} ); |
117 |
$order->{'rrp'} = sprintf( "%.2f", $order->{'rrp'} ); |
118 |
$order->{'ecost'} = sprintf( "%.2f", $order->{'ecost'}); |
119 |
$order->{'total_rrp'} = sprintf( "%.2f", $order->{'total_rrp'}); |
120 |
$order->{'total_ecost'} = sprintf( "%.2f", $order->{'total_ecost'}); |
121 |
|
122 |
$total_quantity += $order->{'quantity'}; |
123 |
$total_rrp += $order->{'total_rrp'}; |
124 |
$total_ecost += $order->{'total_ecost'}; |
125 |
|
126 |
# Get the budget's name |
127 |
$order->{'budget_name'} = $budget_name{$order->{'budget_id'}}; |
128 |
} |
129 |
|
130 |
# If we are outputting to screen, output to the template. |
131 |
if($params->{"output"} eq 'screen') { |
132 |
$template->param( |
133 |
total => scalar @orders, |
134 |
ordersloop => \@orders, |
135 |
get_orders => $get_orders, |
136 |
total_quantity => $total_quantity, |
137 |
total_rrp => sprintf( "%.2f", $total_rrp ), |
138 |
total_ecost => sprintf( "%.2f", $total_ecost ), |
139 |
); |
140 |
} |
141 |
# If we are outputting to a file, create it and exit. |
142 |
else { |
143 |
my $basename = $params->{"basename"}; |
144 |
my $sep = $params->{"sep"}; |
145 |
$sep = "\t" if ($sep == 'tabulation'); |
146 |
|
147 |
print $query->header( |
148 |
-type => 'application/vnd.sun.xml.calc', |
149 |
-encoding => 'utf-8', |
150 |
-attachment => "$basename.csv", |
151 |
-name => "$basename.csv" |
152 |
); |
153 |
|
154 |
binmode STDOUT, ":utf8"; |
155 |
|
156 |
# Surrounds a string with double-quotes and escape the double-quotes inside |
157 |
sub _surround { |
158 |
my $string = shift || ""; |
159 |
$string =~ s/"/""/g; |
160 |
return "\"$string\""; |
161 |
} |
162 |
|
163 |
# Create the CSV file |
164 |
print '"Budget"' . $sep; |
165 |
print '"Basket"' . $sep; |
166 |
print '"Basket by"' . $sep; |
167 |
print '"biblionumber"' . $sep; |
168 |
print '"Title"' . $sep; |
169 |
print '"Currency"' . $sep; |
170 |
print '"Vendor Price"' . $sep; |
171 |
print '"RRP"' . $sep; |
172 |
print '"Budgeted cost"' . $sep; |
173 |
print '"Quantity"' . $sep; |
174 |
print '"Total RRP"' . $sep; |
175 |
print '"Total cost"' . $sep; |
176 |
print '"Entry date"' . $sep; |
177 |
print '"Date received"' . $sep; |
178 |
print '"Notes"' . "\n"; |
179 |
|
180 |
foreach my $order (@orders) { |
181 |
print _surround($order->{'budget_name'}) . $sep; |
182 |
print _surround($order->{'basketno'}) . $sep; |
183 |
print _surround($order->{'authorisedbyname'}) . $sep; |
184 |
print _surround($order->{'biblionumber'}) . $sep; |
185 |
print _surround($order->{'title'}) . $sep; |
186 |
print _surround($order->{'currency'}) . $sep; |
187 |
print _surround($order->{'listprice'}) . $sep; |
188 |
print _surround($order->{'rrp'}) . $sep; |
189 |
print _surround($order->{'ecost'}) . $sep; |
190 |
print _surround($order->{'quantity'}) . $sep; |
191 |
print _surround($order->{'total_rrp'}) . $sep; |
192 |
print _surround($order->{'total_ecost'}) . $sep; |
193 |
print _surround($order->{'entrydate'}) . $sep; |
194 |
print _surround($order->{'datereceived'}) . $sep; |
195 |
print _surround($order->{'notes'}) . "\n"; |
196 |
} |
197 |
|
198 |
print '"TOTAL"'. ($sep x 8); |
199 |
print _surround($total_quantity) . $sep; |
200 |
print _surround($total_rrp) . $sep; |
201 |
print _surround($total_ecost); |
202 |
|
203 |
exit(0); |
204 |
} |
205 |
} |
206 |
else { |
207 |
# Set file export choices |
208 |
my $CGIextChoice = CGI::scrolling_list( |
209 |
-name => 'MIME', |
210 |
-id => 'MIME', |
211 |
-values => ['CSV'], # FIXME translation |
212 |
-size => 1, |
213 |
-multiple => 0 |
214 |
); |
215 |
|
216 |
my $CGIsepChoice = GetDelimiterChoices; |
217 |
|
218 |
# getting all budgets |
219 |
# active budgets |
220 |
my @active_budgets = C4::Budgets::GetActiveBudgets(1); |
221 |
# non active budgets |
222 |
my @non_active_budgets = C4::Budgets::GetActiveBudgets(0); |
223 |
my @budgetsloop; |
224 |
foreach my $thisbudget ( sort {$a->{budget_name} cmp $b->{budget_name}} @active_budgets ) { |
225 |
my $budget_period_desc = C4::Budgets::GetBudgetPeriodDescription($thisbudget->{budget_id}); |
226 |
my %row = ( |
227 |
value => $thisbudget->{budget_id}, |
228 |
description => $thisbudget->{budget_name}, |
229 |
period => "(" . $budget_period_desc->{budget_period_description} . ")", |
230 |
); |
231 |
push @budgetsloop, \%row; |
232 |
} |
233 |
foreach my $thisbudget ( sort {$a->{budget_name} cmp $b->{budget_name}} @non_active_budgets ) { |
234 |
my $budget_period_desc = C4::Budgets::GetBudgetPeriodDescription($thisbudget->{budget_id}); |
235 |
my %row = ( |
236 |
value => $thisbudget->{budget_id}, |
237 |
description => "[i] ". $thisbudget->{budget_name}, |
238 |
period => "(" . $budget_period_desc->{budget_period_description} . ")", |
239 |
); |
240 |
push @budgetsloop, \%row; |
241 |
} |
242 |
|
243 |
$template->param( budgetsloop => \@budgetsloop, |
244 |
CGIextChoice => $CGIextChoice, |
245 |
CGIsepChoice => $CGIsepChoice, |
246 |
); |
247 |
} |
248 |
|
249 |
# writing the template |
250 |
output_html_with_http_headers $query, $cookie, $template->output; |