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