Line 0
Link Here
|
|
|
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 3 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::GetBudgetsReport(1); |
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 $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 eq '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, ":encoding(UTF-8)"; |
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 |
my @rows; |
163 |
foreach my $order (@orders) { |
164 |
my @row; |
165 |
push(@row, _surround($order->{'budget_name'})); |
166 |
push(@row, _surround($order->{'basketno'})); |
167 |
push(@row, _surround($order->{'authorisedbyname'})); |
168 |
push(@row, _surround($order->{'biblionumber'})); |
169 |
push(@row, _surround($order->{'title'})); |
170 |
push(@row, _surround($order->{'currency'})); |
171 |
push(@row, _surround($order->{'listprice'})); |
172 |
push(@row, _surround($order->{'rrp'})); |
173 |
push(@row, _surround($order->{'ecost'})); |
174 |
push(@row, _surround($order->{'quantity'})); |
175 |
push(@row, _surround($order->{'total_rrp'})); |
176 |
push(@row, _surround($order->{'total_ecost'})); |
177 |
push(@row, _surround($order->{'entrydate'})); |
178 |
push(@row, _surround($order->{'datereceived'})); |
179 |
push(@row, _surround($order->{'notes'})); |
180 |
push(@rows, \@row); |
181 |
} |
182 |
|
183 |
my @totalrow; |
184 |
for(1..9){push(@totalrow, "")}; |
185 |
push(@totalrow, _surround($total_quantity)); |
186 |
push(@totalrow, _surround($total_rrp)); |
187 |
push(@totalrow, _surround($total_ecost)); |
188 |
|
189 |
my $csvTemplate = C4::Templates::gettemplate('reports/csv/orders_by_budget.tt', 'intranet', $query); |
190 |
$csvTemplate->param(sep => $sep, rows => \@rows, totalrow => \@totalrow); |
191 |
print $csvTemplate->output; |
192 |
|
193 |
exit(0); |
194 |
} |
195 |
} |
196 |
else { |
197 |
# Set file export choices |
198 |
my @outputFormats = ('CSV'); |
199 |
my @CSVdelimiters = qw(; tabulation , \\ / #); |
200 |
|
201 |
# getting all budgets |
202 |
# active budgets |
203 |
my @active_budgets = C4::Budgets::GetBudgetsByActivity(1); |
204 |
# non active budgets |
205 |
my @non_active_budgets = C4::Budgets::GetBudgetsByActivity(0); |
206 |
my @budgetsloop; |
207 |
foreach my $thisbudget ( sort {$a->{budget_name} cmp $b->{budget_name}} @active_budgets ) { |
208 |
my $budget_period_desc = C4::Budgets::GetBudgetPeriodDescription($thisbudget->{budget_id}); |
209 |
my %row = ( |
210 |
value => $thisbudget->{budget_id}, |
211 |
description => $thisbudget->{budget_name}, |
212 |
period => "(" . $budget_period_desc->{budget_period_description} . ")", |
213 |
); |
214 |
push @budgetsloop, \%row; |
215 |
} |
216 |
foreach my $thisbudget ( sort {$a->{budget_name} cmp $b->{budget_name}} @non_active_budgets ) { |
217 |
my $budget_period_desc = C4::Budgets::GetBudgetPeriodDescription($thisbudget->{budget_id}); |
218 |
my %row = ( |
219 |
value => $thisbudget->{budget_id}, |
220 |
description => "[i] ". $thisbudget->{budget_name}, |
221 |
period => "(" . $budget_period_desc->{budget_period_description} . ")", |
222 |
); |
223 |
push @budgetsloop, \%row; |
224 |
} |
225 |
|
226 |
$template->param( budgetsloop => \@budgetsloop, |
227 |
outputFormatloop => \@outputFormats, |
228 |
delimiterloop => \@CSVdelimiters |
229 |
); |
230 |
} |
231 |
|
232 |
# writing the template |
233 |
output_html_with_http_headers $query, $cookie, $template->output; |