Bug 8435: DBRev 3.13.00.038
[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 under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 strict;
55 use warnings;
56 use C4::Auth;
57 use C4::Biblio;
58 use C4::Budgets;
59 use C4::Output;
60 use CGI;
61
62 use C4::Acquisition qw/ GetBasketsInfosByBookseller /;
63 use C4::Bookseller qw/ GetBookSellerFromId GetBookSeller /;
64 use C4::Members qw/GetMember/;
65 use C4::Context;
66
67 my $query = CGI->new;
68 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
69     {   template_name   => 'acqui/booksellers.tmpl',
70         query           => $query,
71         type            => 'intranet',
72         authnotrequired => 0,
73         flagsrequired   => { acquisition => '*' },
74         debug           => 1,
75     }
76 );
77
78 #parameters
79 my $supplier = $query->param('supplier');
80 my $booksellerid = $query->param('booksellerid');
81 my $allbaskets= $query->param('allbaskets')||0;
82 my @suppliers;
83
84 if ($booksellerid) {
85     push @suppliers, GetBookSellerFromId($booksellerid);
86 } else {
87     @suppliers = GetBookSeller($supplier);
88 }
89
90 my $supplier_count = @suppliers;
91 if ( $supplier_count == 1 ) {
92     $template->param(
93         supplier_name => $suppliers[0]->{'name'},
94         booksellerid  => $suppliers[0]->{'id'},
95         basketcount   => $suppliers[0]->{'basketcount'}
96     );
97 }
98
99 my $uid;
100 if ($loggedinuser) {
101     $uid = GetMember( borrowernumber => $loggedinuser )->{userid};
102 }
103
104 my $userenv = C4::Context::userenv;
105 my $viewbaskets = C4::Context->preference('AcqViewBaskets');
106
107 my $userbranch = $userenv->{branch};
108
109 my $budgets = GetBudgetHierarchy;
110 my $has_budgets = 0;
111 foreach my $r (@{$budgets}) {
112     if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
113         next;
114     }
115     next unless (CanUserUseBudget($loggedinuser, $r, $userflags));
116
117     $has_budgets = 1;
118     last;
119 }
120
121 #build result page
122 my $loop_suppliers = [];
123
124 for my $vendor (@suppliers) {
125     my $baskets = GetBasketsInfosByBookseller( $vendor->{id}, $allbaskets );
126
127     my $loop_basket = [];
128
129     for my $basket ( @{$baskets} ) {
130         my $authorisedby = $basket->{authorisedby};
131         my $basketbranch = ''; # set a blank branch to start with
132         my $member = GetMember( borrowernumber => $authorisedby );
133         if ( $member ) {
134            $basketbranch = $member->{branchcode};
135         }
136
137         if ($userenv->{'flags'} & 1 || #user is superlibrarian
138                (haspermission( $uid, { acquisition => q{*} } ) && #user has acq permissions and
139                    ($viewbaskets eq 'all' || #user is allowed to see all baskets
140                    ($viewbaskets eq 'branch' && $authorisedby && $userbranch eq $basketbranch) || #basket belongs to user's branch
141                    ($basket->{authorisedby} &&  $viewbaskets eq 'user' && $authorisedby == $loggedinuser) #user created this basket
142                    ) 
143                 ) 
144            ) { 
145             foreach (qw(total_items total_biblios expected_items)) {
146                 $basket->{$_} ||= 0;
147             }
148             if($member) {
149                 $basket->{authorisedby_firstname} = $member->{firstname};
150                 $basket->{authorisedby_surname} = $member->{surname};
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       };
162
163 }
164 $template->param(
165     loop_suppliers => $loop_suppliers,
166     supplier       => ( $booksellerid || $supplier ),
167     count          => $supplier_count,
168     has_budgets          => $has_budgets,
169 );
170 $template->{VARS}->{'allbaskets'} = $allbaskets;
171
172 output_html_with_http_headers $query, $cookie, $template->output;