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