DBRev 24.06.00.000: Start of a new release cycle
[koha.git] / acqui / booksellers.pl
1 #!/usr/bin/perl
2
3 #script to show suppliers and orders
4
5 # Copyright 2000-2002 Katipo Communications
6 # Copyright 2008-2009 BibLibre SARL
7 # Copyright 2010 PTFS Europe
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 =head1 NAME
25
26 booksellers.pl
27
28 =head1 DESCRIPTION
29
30 this script displays the list of suppliers & baskets like C<$supplier> given on input arg.
31 thus, this page brings differents features like to display supplier's details,
32 to add an order for a specific supplier or to just add a new supplier.
33
34 =head1 CGI PARAMETERS
35
36 =over 4
37
38 =item supplier
39
40 C<$supplier> is the string with which we search for a supplier
41
42 =back
43
44 =over 4
45
46 =item id or booksellerid
47
48 The id of the supplier whose baskets we will display
49
50 =back
51
52 =cut
53
54 use Modern::Perl;
55 use C4::Auth qw( get_template_and_user );
56 use C4::Budgets qw( GetBudgetHierarchy GetBudget CanUserUseBudget );
57 use C4::Output qw( output_html_with_http_headers );
58 use CGI qw ( -utf8 );
59
60 use C4::Acquisition qw( GetBasket GetBasketsInfosByBookseller CanUserManageBasket GetBasketgroup );
61 use C4::Context;
62
63 use Koha::Acquisition::Booksellers;
64 use Koha::Patrons;
65
66 my $query = CGI->new;
67 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
68     {   template_name   => 'acqui/booksellers.tt',
69         query           => $query,
70         type            => 'intranet',
71         flagsrequired   => { acquisition => '*' },
72     }
73 );
74
75 #parameters
76 my $supplier = $query->param('supplier');
77 my $booksellerid = $query->param('booksellerid');
78 my $allbaskets= $query->param('allbaskets')||0;
79 my @suppliers;
80
81 if ($booksellerid) {
82     push @suppliers, Koha::Acquisition::Booksellers->find( $booksellerid );
83 } else {
84     @suppliers = Koha::Acquisition::Booksellers->search(
85         [
86             { name => { -like => "%$supplier%" } },
87             { 'aqbookseller_aliases.alias' => { -like => "%$supplier%" } },
88         ],
89         {
90             order_by => { -asc => 'name' },
91             join     => 'aqbookseller_aliases',
92             distinct => 1,
93         }
94     )->as_list;
95 }
96
97 my $supplier_count = @suppliers;
98 if ( $supplier_count == 1 ) {
99     $template->param(
100         supplier_name => $suppliers[0]->name,
101         booksellerid  => $suppliers[0]->id,
102         basketcount   => $suppliers[0]->baskets->count,
103         subscriptionscount => $suppliers[0]->subscriptions->count,
104         active        => $suppliers[0]->active,
105     );
106 }
107
108 my $uid;
109 # FIXME This script should only be accessed by a valid logged in patron
110 if ($loggedinuser) {
111     # FIXME Should not be needed, logged in patron should be cached
112     $uid = Koha::Patrons->find( $loggedinuser )->userid;
113 }
114
115 my $userenv = C4::Context::userenv;
116 my $viewbaskets = C4::Context->preference('AcqViewBaskets');
117
118 my $userbranch = $userenv->{branch};
119
120 my $budgets = GetBudgetHierarchy;
121 my $has_budgets = 0;
122 foreach my $r (@{$budgets}) {
123     next unless (CanUserUseBudget($loggedinuser, $r, $userflags));
124
125     $has_budgets = 1;
126     last;
127 }
128
129 #build result page
130 my $loop_suppliers = [];
131
132 for my $vendor (@suppliers) {
133     my $baskets = GetBasketsInfosByBookseller( $vendor->id, $allbaskets );
134
135     my $loop_basket = [];
136
137     for my $basket ( @{$baskets} ) {
138         if (CanUserManageBasket($loggedinuser, $basket, $userflags)) {
139             my $patron = Koha::Patrons->find( $basket->{authorisedby} );
140             foreach (qw(total_items total_biblios expected_items)) {
141                 $basket->{$_} ||= 0;
142             }
143             if ( $patron ) {
144                 $basket->{authorisedby} = $patron;
145             }
146             if ($basket->{basketgroupid}) {
147                 my $basketgroup = C4::Acquisition::GetBasketgroup($basket->{basketgroupid});
148                 if ($basketgroup) {
149                     $basket->{basketgroup} = $basketgroup;
150                 }
151             }
152             push @{$loop_basket}, $basket; 
153         }
154     }
155
156     push @{$loop_suppliers},
157       { loop_basket => $loop_basket,
158         booksellerid  => $vendor->id,
159         name        => $vendor->name,
160         active      => $vendor->active,
161         vendor_type => $vendor->type,
162         basketcount   => $vendor->baskets->count,
163         subscriptioncount => $vendor->subscriptions->count,
164       };
165
166 }
167 $template->param(
168     loop_suppliers => $loop_suppliers,
169     supplier       => ( $booksellerid || $supplier ),
170     count          => $supplier_count,
171     has_budgets          => $has_budgets,
172 );
173 $template->{VARS}->{'allbaskets'} = $allbaskets;
174
175 output_html_with_http_headers $query, $cookie, $template->output;