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::Acquisition; #GetBasket() |
38 |
|
39 |
|
40 |
my $query = new CGI; |
41 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
42 |
{ |
43 |
template_name => "reports/orders_by_budget.tt", |
44 |
query => $query, |
45 |
type => "intranet", |
46 |
authnotrequired => 0, |
47 |
flagsrequired => { reports => '*' }, |
48 |
debug => 1, |
49 |
} |
50 |
); |
51 |
|
52 |
my $params = $query->Vars; |
53 |
my $get_orders = $params->{'get_orders'}; |
54 |
|
55 |
if ( $get_orders ) { |
56 |
my $budgetfilter = $params->{'budgetfilter'} || undef; |
57 |
my $total_quantity = 0; |
58 |
my $total_rrp = 0; |
59 |
my $total_ecost = 0; |
60 |
my %budget_name; |
61 |
|
62 |
# Fetch the orders |
63 |
my @orders; |
64 |
unless($budgetfilter) { |
65 |
# If no budget filter was selected, get the orders of all budgets |
66 |
my @budgets = C4::Budgets::GetBudgetsReport(); |
67 |
foreach my $budget (@budgets) { |
68 |
push(@orders, $budget); |
69 |
$budget_name{$budget->{'budget_id'}} = $budget->{'budget_name'}; |
70 |
} |
71 |
} |
72 |
else { |
73 |
if ($budgetfilter eq 'activebudgets') { |
74 |
# If all active budgets's option was selected, get the orders of all active budgets |
75 |
my @active_budgets = C4::Budgets::GetBudgetsReport(1); |
76 |
foreach my $active_budget (@active_budgets) |
77 |
{ |
78 |
push(@orders, $active_budget); |
79 |
$budget_name{$active_budget->{'budget_id'}} = $active_budget->{'budget_name'}; |
80 |
} |
81 |
} |
82 |
else { |
83 |
# A budget filter was selected, only get the orders for the selected budget |
84 |
my @filtered_budgets = C4::Budgets::GetBudgetReport($budgetfilter); |
85 |
foreach my $budget (@filtered_budgets) |
86 |
{ |
87 |
push(@orders, $budget); |
88 |
$budget_name{$budget->{'budget_id'}} = $budget->{'budget_name'}; |
89 |
} |
90 |
if ($filtered_budgets[0]) { |
91 |
$template->param( |
92 |
current_budget_name => $filtered_budgets[0]->{'budget_name'}, |
93 |
); |
94 |
} |
95 |
} |
96 |
} |
97 |
|
98 |
# Format the order's informations |
99 |
foreach my $order (@orders) { |
100 |
# Get the title of the ordered item |
101 |
my $biblio = C4::Biblio::GetBiblio($order->{'biblionumber'}); |
102 |
my $basket = C4::Acquisition::GetBasket($order->{'basketno'}); |
103 |
|
104 |
$order->{'basketname'} = $basket->{'basketname'}; |
105 |
$order->{'authorisedbyname'} = $basket->{'authorisedbyname'}; |
106 |
|
107 |
$order->{'title'} = $biblio->{'title'} || $order->{'biblionumber'}; |
108 |
|
109 |
$order->{'total_rrp'} = $order->{'quantity'} * $order->{'rrp'}; |
110 |
$order->{'total_ecost'} = $order->{'quantity'} * $order->{'ecost'}; |
111 |
|
112 |
# Format the dates and currencies correctly |
113 |
$order->{'datereceived'} = Koha::DateUtils::output_pref(Koha::DateUtils::dt_from_string($order->{'datereceived'})); |
114 |
$order->{'entrydate'} = Koha::DateUtils::output_pref(Koha::DateUtils::dt_from_string($order->{'entrydate'})); |
115 |
$order->{'listprice'} = sprintf( "%.2f", $order->{'listprice'} ); |
116 |
$order->{'rrp'} = sprintf( "%.2f", $order->{'rrp'} ); |
117 |
$order->{'ecost'} = sprintf( "%.2f", $order->{'ecost'}); |
118 |
$order->{'total_rrp'} = sprintf( "%.2f", $order->{'total_rrp'}); |
119 |
$order->{'total_ecost'} = sprintf( "%.2f", $order->{'total_ecost'}); |
120 |
|
121 |
$total_quantity += $order->{'quantity'}; |
122 |
$total_rrp += $order->{'total_rrp'}; |
123 |
$total_ecost += $order->{'total_ecost'}; |
124 |
|
125 |
# Get the budget's name |
126 |
$order->{'budget_name'} = $budget_name{$order->{'budget_id'}}; |
127 |
} |
128 |
|
129 |
# If we are outputting to screen, output to the template. |
130 |
if($params->{"output"} eq 'screen') { |
131 |
$template->param( |
132 |
total => scalar @orders, |
133 |
ordersloop => \@orders, |
134 |
get_orders => $get_orders, |
135 |
total_quantity => $total_quantity, |
136 |
total_rrp => sprintf( "%.2f", $total_rrp ), |
137 |
total_ecost => sprintf( "%.2f", $total_ecost ), |
138 |
); |
139 |
} |
140 |
# If we are outputting to a file, create it and exit. |
141 |
else { |
142 |
my $basename = $params->{"basename"}; |
143 |
my $sep = $params->{"sep"}; |
144 |
$sep = "\t" if ($sep eq 'tabulation'); |
145 |
|
146 |
print $query->header( |
147 |
-type => 'application/vnd.sun.xml.calc', |
148 |
-encoding => 'utf-8', |
149 |
-attachment => "$basename.csv", |
150 |
-name => "$basename.csv" |
151 |
); |
152 |
|
153 |
#binmode STDOUT, ":encoding(UTF-8)"; |
154 |
|
155 |
# Surrounds a string with double-quotes and escape the double-quotes inside |
156 |
sub _surround { |
157 |
my $string = shift || ""; |
158 |
$string =~ s/"/""/g; |
159 |
return "\"$string\""; |
160 |
} |
161 |
my @rows; |
162 |
foreach my $order (@orders) { |
163 |
my @row; |
164 |
my $sep=''; |
165 |
if ($order->{'order_vendornote'}){ |
166 |
$sep='. Note2:' |
167 |
} |
168 |
$order->{'order_internalnote'} .= $sep .$order->{'order_vendornote'}; |
169 |
push(@row, _surround($order->{'budget_name'})); |
170 |
push(@row, _surround($order->{'basketno'})); |
171 |
push(@row, _surround($order->{'basketname'})); |
172 |
push(@row, _surround($order->{'authorisedbyname'})); |
173 |
push(@row, _surround($order->{'biblionumber'})); |
174 |
push(@row, _surround($order->{'title'})); |
175 |
push(@row, _surround($order->{'currency'})); |
176 |
push(@row, _surround($order->{'listprice'})); |
177 |
push(@row, _surround($order->{'rrp'})); |
178 |
push(@row, _surround($order->{'ecost'})); |
179 |
push(@row, _surround($order->{'quantity'})); |
180 |
push(@row, _surround($order->{'total_rrp'})); |
181 |
push(@row, _surround($order->{'total_ecost'})); |
182 |
push(@row, _surround($order->{'entrydate'})); |
183 |
push(@row, _surround($order->{'datereceived'})); |
184 |
push(@row, _surround($order->{'order_internalnote'})); |
185 |
push(@rows, \@row); |
186 |
} |
187 |
|
188 |
my @totalrow; |
189 |
for(1..9){push(@totalrow, "")}; |
190 |
push(@totalrow, _surround($total_quantity)); |
191 |
push(@totalrow, _surround($total_rrp)); |
192 |
push(@totalrow, _surround($total_ecost)); |
193 |
|
194 |
my $csvTemplate = C4::Templates::gettemplate('reports/csv/orders_by_budget.tt', 'intranet', $query); |
195 |
$csvTemplate->param(sep => $sep, rows => \@rows, totalrow => \@totalrow); |
196 |
print $csvTemplate->output; |
197 |
|
198 |
exit(0); |
199 |
} |
200 |
} |
201 |
else { |
202 |
# Set file export choices |
203 |
my @outputFormats = ('CSV'); |
204 |
my @CSVdelimiters =(',','#',qw(; tabulation \\ /)); |
205 |
|
206 |
# getting all budgets |
207 |
# active budgets |
208 |
my @active_budgets = C4::Budgets::GetBudgetsByActivity(1); |
209 |
# non active budgets |
210 |
my @non_active_budgets = C4::Budgets::GetBudgetsByActivity(0); |
211 |
my @budgetsloop; |
212 |
foreach my $thisbudget ( sort {$a->{budget_name} cmp $b->{budget_name}} @active_budgets ) { |
213 |
my $budget_period_desc = C4::Budgets::GetBudgetPeriodDescription($thisbudget->{budget_id}); |
214 |
my %row = ( |
215 |
value => $thisbudget->{budget_id}, |
216 |
description => $thisbudget->{budget_name}, |
217 |
period => "(" . $budget_period_desc->{budget_period_description} . ")", |
218 |
); |
219 |
push @budgetsloop, \%row; |
220 |
} |
221 |
foreach my $thisbudget ( sort {$a->{budget_name} cmp $b->{budget_name}} @non_active_budgets ) { |
222 |
my $budget_period_desc = C4::Budgets::GetBudgetPeriodDescription($thisbudget->{budget_id}); |
223 |
my %row = ( |
224 |
value => $thisbudget->{budget_id}, |
225 |
description => "[i] ". $thisbudget->{budget_name}, |
226 |
period => "(" . $budget_period_desc->{budget_period_description} . ")", |
227 |
); |
228 |
push @budgetsloop, \%row; |
229 |
} |
230 |
$template->param( budgetsloop => \@budgetsloop, |
231 |
outputFormatloop => \@outputFormats, |
232 |
delimiterloop => \@CSVdelimiters, |
233 |
delimiterPreference => C4::Context->preference('delimiter') |
234 |
); |
235 |
} |
236 |
|
237 |
# writing the template |
238 |
output_html_with_http_headers $query, $cookie, $template->output; |