Bug 21180: Allow Talking Tech outbound script to limit based on patron home library...
[koha.git] / reports / orders_by_fund.pl
blob4e797c2d25ae543f488c38a2adf9815300c30210
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 = new CGI;
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 template_name => "reports/orders_by_budget.tt",
44 query => $query,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { reports => '*' },
48 debug => 1,
52 my $params = $query->Vars;
53 my $get_orders = $params->{'get_orders'};
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;
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'};
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)
78 push(@orders, $active_budget);
79 $budget_name{$active_budget->{'budget_id'}} = $active_budget->{'budget_name'};
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)
87 push(@orders, $budget);
88 $budget_name{$budget->{'budget_id'}} = $budget->{'budget_name'};
90 if ($filtered_budgets[0]) {
91 $template->param(
92 current_budget_name => $filtered_budgets[0]->{'budget_name'},
98 # Format the order's informations
99 foreach my $order (@orders) {
100 # Get the title of the ordered item
101 my $biblio = Koha::Biblios->find( $order->{biblionumber} );
102 my $basket = C4::Acquisition::GetBasket($order->{'basketno'});
104 $order->{'basketname'} = $basket->{'basketname'};
105 $order->{'authorisedbyname'} = $basket->{'authorisedbyname'};
107 $order->{title} = $biblio ? $biblio->title : '';
108 $order->{title} ||= $order->{biblionumber};
110 $order->{'total_rrp'} = get_rounded_price($order->{'quantity'}) * $order->{'rrp'};
111 $order->{'total_ecost'} = get_rounded_price($order->{'quantity'}) * $order->{'ecost'};
113 # Format the dates and currencies correctly
114 $order->{'datereceived'} = output_pref(dt_from_string($order->{'datereceived'}));
115 $order->{'entrydate'} = output_pref(dt_from_string($order->{'entrydate'}));
116 $total_quantity += $order->{'quantity'};
117 $total_rrp += $order->{'total_rrp'};
118 $total_ecost += $order->{'total_ecost'};
120 # Get the budget's name
121 $order->{'budget_name'} = $budget_name{$order->{'budget_id'}};
124 # If we are outputting to screen, output to the template.
125 if($params->{"output"} eq 'screen') {
126 $template->param(
127 total => scalar @orders,
128 ordersloop => \@orders,
129 get_orders => $get_orders,
130 total_quantity => $total_quantity,
131 total_rrp => $total_rrp,
132 total_ecost => $total_ecost,
135 # If we are outputting to a file, create it and exit.
136 else {
137 my $basename = $params->{"basename"};
138 my $sep = $params->{"sep"};
139 $sep = "\t" if ($sep eq 'tabulation');
141 # TODO Use Text::CSV to generate the CSV file
142 print $query->header(
143 -type => 'text/csv',
144 -encoding => 'utf-8',
145 -attachment => "$basename.csv",
146 -name => "$basename.csv"
149 #binmode STDOUT, ":encoding(UTF-8)";
151 # Surrounds a string with double-quotes and escape the double-quotes inside
152 sub _surround {
153 my $string = shift || "";
154 $string =~ s/"/""/g;
155 return "\"$string\"";
157 my @rows;
158 foreach my $order (@orders) {
159 my @row;
160 push(@row, _surround($order->{'budget_name'}));
161 push(@row, _surround($order->{'basketno'}));
162 push(@row, _surround($order->{'basketname'}));
163 push(@row, _surround($order->{'authorisedbyname'}));
164 push(@row, _surround($order->{'biblionumber'}));
165 push(@row, _surround($order->{'title'}));
166 push(@row, _surround($order->{'currency'}));
167 push(@row, _surround($order->{'listprice'}));
168 push(@row, _surround($order->{'rrp'}));
169 push(@row, _surround($order->{'ecost'}));
170 push(@row, _surround($order->{'quantity'}));
171 push(@row, _surround($order->{'total_rrp'}));
172 push(@row, _surround($order->{'total_ecost'}));
173 push(@row, _surround($order->{'entrydate'}));
174 push(@row, _surround($order->{'datereceived'}));
175 push(@row, _surround($order->{'order_internalnote'}));
176 push(@row, _surround($order->{'order_vendornote'}));
177 push(@rows, \@row);
180 my @totalrow;
181 for(1..9){push(@totalrow, "")};
182 push(@totalrow, _surround($total_quantity));
183 push(@totalrow, _surround($total_rrp));
184 push(@totalrow, _surround($total_ecost));
186 my $csvTemplate = C4::Templates::gettemplate('reports/csv/orders_by_budget.tt', 'intranet', $query);
187 $csvTemplate->param(sep => $sep, rows => \@rows, totalrow => \@totalrow);
188 print $csvTemplate->output;
190 exit(0);
193 else {
194 # Set file export choices
195 my @outputFormats = ('CSV');
196 my @CSVdelimiters =(',','#',qw(; tabulation \\ /));
198 # getting all budgets
199 my $budgets = GetBudgetHierarchy;
200 my $budgetloop = [];
201 foreach my $budget (@{$budgets}) {
202 push @{$budgetloop},{
203 value => $budget->{budget_id},
204 description => $budget->{budget_name},
205 period => $budget->{budget_period_description},
206 active => $budget->{budget_period_active},
209 @{$budgetloop} =sort { uc( $a->{description}) cmp uc( $b->{description}) } @{$budgetloop};
210 $template->param( budgetsloop => \@{$budgetloop},
211 outputFormatloop => \@outputFormats,
212 delimiterloop => \@CSVdelimiters,
213 delimiterPreference => C4::Context->preference('delimiter')
217 # writing the template
218 output_html_with_http_headers $query, $cookie, $template->output;