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