Bug 26569: Use gender neutral pronouns in system prefernece explanations
[koha.git] / reports / orders_by_fund.pl
blobd5fa1070f52f5390e7a700c69164614bc63236e0
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Author : Frédérick Capovilla, 2011 - SYS-TECH
6 # Modified by : Élyse Morin, 2012 - Libéo
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.
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.
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
22 =head1 orders_by_budget
24 This script displays all orders associated to a selected budget.
26 =cut
28 use Modern::Perl;
30 use CGI qw( -utf8 );
31 use C4::Auth;
32 use C4::Output;
33 use C4::Budgets;
34 use C4::Biblio;
35 use C4::Reports;
36 use C4::Acquisition; #GetBasket()
37 use Koha::Biblios;
38 use Koha::DateUtils;
40 my $query = CGI->new;
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 template_name => "reports/orders_by_budget.tt",
44 query => $query,
45 type => "intranet",
46 flagsrequired => { reports => '*' },
47 debug => 1,
51 my $params = $query->Vars;
52 my $get_orders = $params->{'get_orders'};
54 if ( $get_orders ) {
55 my $budgetfilter = $params->{'budgetfilter'} || undef;
56 my $total_quantity = 0;
57 my $total_rrp = 0;
58 my $total_ecost = 0;
59 my %budget_name;
61 # Fetch the orders
62 my @orders;
63 unless($budgetfilter) {
64 # If no budget filter was selected, get the orders of all budgets
65 my @budgets = C4::Budgets::GetBudgetsReport();
66 foreach my $budget (@budgets) {
67 push(@orders, $budget);
68 $budget_name{$budget->{'budget_id'}} = $budget->{'budget_name'};
71 else {
72 if ($budgetfilter eq 'activebudgets') {
73 # If all active budgets's option was selected, get the orders of all active budgets
74 my @active_budgets = C4::Budgets::GetBudgetsReport(1);
75 foreach my $active_budget (@active_budgets)
77 push(@orders, $active_budget);
78 $budget_name{$active_budget->{'budget_id'}} = $active_budget->{'budget_name'};
81 else {
82 # A budget filter was selected, only get the orders for the selected budget
83 my @filtered_budgets = C4::Budgets::GetBudgetReport($budgetfilter);
84 foreach my $budget (@filtered_budgets)
86 push(@orders, $budget);
87 $budget_name{$budget->{'budget_id'}} = $budget->{'budget_name'};
89 if ($filtered_budgets[0]) {
90 $template->param(
91 current_budget_name => $filtered_budgets[0]->{'budget_name'},
97 # Format the order's informations
98 foreach my $order (@orders) {
99 # Get the title of the ordered item
100 my $biblio = Koha::Biblios->find( $order->{biblionumber} );
101 my $basket = C4::Acquisition::GetBasket($order->{'basketno'});
103 $order->{'basketname'} = $basket->{'basketname'};
104 $order->{'authorisedbyname'} = $basket->{'authorisedbyname'};
106 $order->{title} = $biblio ? $biblio->title : '';
107 $order->{title} ||= $order->{biblionumber};
109 $order->{'total_rrp'} = get_rounded_price($order->{'quantity'}) * $order->{'rrp'};
110 $order->{'total_ecost'} = get_rounded_price($order->{'quantity'}) * $order->{'ecost'};
112 # Format the dates and currencies correctly
113 $order->{'datereceived'} = output_pref(dt_from_string($order->{'datereceived'}));
114 $order->{'entrydate'} = output_pref(dt_from_string($order->{'entrydate'}));
115 $total_quantity += $order->{'quantity'};
116 $total_rrp += $order->{'total_rrp'};
117 $total_ecost += $order->{'total_ecost'};
119 # Get the budget's name
120 $order->{'budget_name'} = $budget_name{$order->{'budget_id'}};
123 # If we are outputting to screen, output to the template.
124 if($params->{"output"} eq 'screen') {
125 $template->param(
126 total => scalar @orders,
127 ordersloop => \@orders,
128 get_orders => $get_orders,
129 total_quantity => $total_quantity,
130 total_rrp => $total_rrp,
131 total_ecost => $total_ecost,
134 # If we are outputting to a file, create it and exit.
135 else {
136 my $basename = $params->{"basename"};
137 my $sep = $params->{"sep"};
138 $sep = "\t" if ($sep eq 'tabulation');
140 # TODO Use Text::CSV to generate the CSV file
141 print $query->header(
142 -type => 'text/csv',
143 -encoding => 'utf-8',
144 -attachment => "$basename.csv",
145 -name => "$basename.csv"
148 #binmode STDOUT, ":encoding(UTF-8)";
150 # Surrounds a string with double-quotes and escape the double-quotes inside
151 sub _surround {
152 my $string = shift || "";
153 $string =~ s/"/""/g;
154 return "\"$string\"";
156 my @rows;
157 foreach my $order (@orders) {
158 my @row;
159 push(@row, _surround($order->{'budget_name'}));
160 push(@row, _surround($order->{'basketno'}));
161 push(@row, _surround($order->{'basketname'}));
162 push(@row, _surround($order->{'authorisedbyname'}));
163 push(@row, _surround($order->{'biblionumber'}));
164 push(@row, _surround($order->{'title'}));
165 push(@row, _surround($order->{'currency'}));
166 push(@row, _surround($order->{'listprice'}));
167 push(@row, _surround($order->{'rrp'}));
168 push(@row, _surround($order->{'ecost'}));
169 push(@row, _surround($order->{'quantity'}));
170 push(@row, _surround($order->{'total_rrp'}));
171 push(@row, _surround($order->{'total_ecost'}));
172 push(@row, _surround($order->{'entrydate'}));
173 push(@row, _surround($order->{'datereceived'}));
174 push(@row, _surround($order->{'order_internalnote'}));
175 push(@row, _surround($order->{'order_vendornote'}));
176 push(@rows, \@row);
179 my @totalrow;
180 for(1..9){push(@totalrow, "")};
181 push(@totalrow, _surround($total_quantity));
182 push(@totalrow, _surround($total_rrp));
183 push(@totalrow, _surround($total_ecost));
185 my $csvTemplate = C4::Templates::gettemplate('reports/csv/orders_by_budget.tt', 'intranet', $query);
186 $csvTemplate->param(sep => $sep, rows => \@rows, totalrow => \@totalrow);
187 print $csvTemplate->output;
189 exit(0);
192 else {
193 # Set file export choices
194 my @outputFormats = ('CSV');
195 my @CSVdelimiters =(',','#',qw(; tabulation \\ /));
197 # getting all budgets
198 my $budgets = GetBudgetHierarchy;
199 my $budgetloop = [];
200 foreach my $budget (@{$budgets}) {
201 push @{$budgetloop},{
202 value => $budget->{budget_id},
203 description => $budget->{budget_name},
204 period => $budget->{budget_period_description},
205 active => $budget->{budget_period_active},
208 @{$budgetloop} =sort { uc( $a->{description}) cmp uc( $b->{description}) } @{$budgetloop};
209 $template->param( budgetsloop => \@{$budgetloop},
210 outputFormatloop => \@outputFormats,
211 delimiterloop => \@CSVdelimiters,
212 delimiterPreference => C4::Context->preference('delimiter')
216 # writing the template
217 output_html_with_http_headers $query, $cookie, $template->output;