Bug 16225 - Extra closing quote in circulation home page template
[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 strict;
55 use warnings;
56 use C4::Auth;
57 use C4::Biblio;
58 use C4::Budgets;
59 use C4::Output;
60 use CGI qw ( -utf8 );
61
62 use C4::Acquisition qw/ GetBasketsInfosByBookseller CanUserManageBasket /;
63 use C4::Members qw/GetMember/;
64 use C4::Context;
65
66 use Koha::Acquisition::Bookseller;
67
68 my $query = CGI->new;
69 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
70     {   template_name   => 'acqui/booksellers.tt',
71         query           => $query,
72         type            => 'intranet',
73         authnotrequired => 0,
74         flagsrequired   => { acquisition => '*' },
75         debug           => 1,
76     }
77 );
78
79 #parameters
80 my $supplier = $query->param('supplier');
81 my $booksellerid = $query->param('booksellerid');
82 my $allbaskets= $query->param('allbaskets')||0;
83 my @suppliers;
84
85 if ($booksellerid) {
86     push @suppliers, Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
87 } else {
88     @suppliers = Koha::Acquisition::Bookseller->search({ name => $supplier });
89 }
90
91 my $supplier_count = @suppliers;
92 if ( $supplier_count == 1 ) {
93     $template->param(
94         supplier_name => $suppliers[0]->{'name'},
95         booksellerid  => $suppliers[0]->{'id'},
96         basketcount   => $suppliers[0]->{'basketcount'},
97         active        => $suppliers[0]->{active},
98     );
99 }
100
101 my $uid;
102 if ($loggedinuser) {
103     $uid = GetMember( borrowernumber => $loggedinuser )->{userid};
104 }
105
106 my $userenv = C4::Context::userenv;
107 my $viewbaskets = C4::Context->preference('AcqViewBaskets');
108
109 my $userbranch = $userenv->{branch};
110
111 my $budgets = GetBudgetHierarchy;
112 my $has_budgets = 0;
113 foreach my $r (@{$budgets}) {
114     if (!defined $r->{budget_amount} || $r->{budget_amount} == 0) {
115         next;
116     }
117     next unless (CanUserUseBudget($loggedinuser, $r, $userflags));
118
119     $has_budgets = 1;
120     last;
121 }
122
123 #build result page
124 my $loop_suppliers = [];
125
126 for my $vendor (@suppliers) {
127     my $baskets = GetBasketsInfosByBookseller( $vendor->{id}, $allbaskets );
128
129     my $loop_basket = [];
130
131     for my $basket ( @{$baskets} ) {
132         if (CanUserManageBasket($loggedinuser, $basket, $userflags)) {
133             my $member = GetMember( borrowernumber => $basket->{authorisedby} );
134             foreach (qw(total_items total_biblios expected_items)) {
135                 $basket->{$_} ||= 0;
136             }
137             if($member) {
138                 $basket->{authorisedby_firstname} = $member->{firstname};
139                 $basket->{authorisedby_surname} = $member->{surname};
140             }
141             if ($basket->{basketgroupid}) {
142                 my $basketgroup = C4::Acquisition::GetBasketgroup($basket->{basketgroupid});
143                 if ($basketgroup) {
144                     $basket->{basketgroup} = $basketgroup;
145                 }
146             }
147             push @{$loop_basket}, $basket; 
148         }
149     }
150
151     push @{$loop_suppliers},
152       { loop_basket => $loop_basket,
153         booksellerid  => $vendor->{id},
154         name        => $vendor->{name},
155         active      => $vendor->{active},
156       };
157
158 }
159 $template->param(
160     loop_suppliers => $loop_suppliers,
161     supplier       => ( $booksellerid || $supplier ),
162     count          => $supplier_count,
163     has_budgets          => $has_budgets,
164 );
165 $template->{VARS}->{'allbaskets'} = $allbaskets;
166
167 output_html_with_http_headers $query, $cookie, $template->output;