Bug 24412: DBIC schema changes
[koha.git] / acqui / booksellers.pl
blob9b15aac94cef6d907f044bb082ec52069c5bdec3
1 #!/usr/bin/perl
3 #script to show suppliers and orders
5 # Copyright 2000-2002 Katipo Communications
6 # Copyright 2008-2009 BibLibre SARL
7 # Copyright 2010 PTFS Europe
9 # This file is part of Koha.
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.
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.
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>.
24 =head1 NAME
26 booksellers.pl
28 =head1 DESCRIPTION
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.
34 =head1 CGI PARAMETERS
36 =over 4
38 =item supplier
40 C<$supplier> is the string with which we search for a supplier
42 =back
44 =over 4
46 =item id or booksellerid
48 The id of the supplier whose baskets we will display
50 =back
52 =cut
54 use Modern::Perl;
55 use C4::Auth;
56 use C4::Biblio;
57 use C4::Budgets;
58 use C4::Output;
59 use CGI qw ( -utf8 );
61 use C4::Acquisition qw/ GetBasketsInfosByBookseller CanUserManageBasket /;
62 use C4::Context;
64 use Koha::Acquisition::Booksellers;
65 use Koha::Patrons;
67 my $query = CGI->new;
68 my ( $template, $loggedinuser, $cookie, $userflags ) = get_template_and_user(
69 { template_name => 'acqui/booksellers.tt',
70 query => $query,
71 type => 'intranet',
72 flagsrequired => { acquisition => '*' },
73 debug => 1,
77 #parameters
78 my $supplier = $query->param('supplier');
79 my $booksellerid = $query->param('booksellerid');
80 my $allbaskets= $query->param('allbaskets')||0;
81 my @suppliers;
83 if ($booksellerid) {
84 push @suppliers, Koha::Acquisition::Booksellers->find( $booksellerid );
85 } else {
86 @suppliers = Koha::Acquisition::Booksellers->search(
87 { name => { -like => "%$supplier%" } },
88 { order_by => { -asc => 'name' } } );
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]->baskets->count,
97 active => $suppliers[0]->active,
101 my $uid;
102 # FIXME This script should only be accessed by a valid logged in patron
103 if ($loggedinuser) {
104 # FIXME Should not be needed, logged in patron should be cached
105 $uid = Koha::Patrons->find( $loggedinuser )->userid;
108 my $userenv = C4::Context::userenv;
109 my $viewbaskets = C4::Context->preference('AcqViewBaskets');
111 my $userbranch = $userenv->{branch};
113 my $budgets = GetBudgetHierarchy;
114 my $has_budgets = 0;
115 foreach my $r (@{$budgets}) {
116 next unless (CanUserUseBudget($loggedinuser, $r, $userflags));
118 $has_budgets = 1;
119 last;
122 #build result page
123 my $loop_suppliers = [];
125 for my $vendor (@suppliers) {
126 my $baskets = GetBasketsInfosByBookseller( $vendor->id, $allbaskets );
128 my $loop_basket = [];
130 for my $basket ( @{$baskets} ) {
131 if (CanUserManageBasket($loggedinuser, $basket, $userflags)) {
132 my $patron = Koha::Patrons->find( $basket->{authorisedby} );
133 foreach (qw(total_items total_biblios expected_items)) {
134 $basket->{$_} ||= 0;
136 if ( $patron ) {
137 $basket->{authorisedby} = $patron;
139 if ($basket->{basketgroupid}) {
140 my $basketgroup = C4::Acquisition::GetBasketgroup($basket->{basketgroupid});
141 if ($basketgroup) {
142 $basket->{basketgroup} = $basketgroup;
145 push @{$loop_basket}, $basket;
149 push @{$loop_suppliers},
150 { loop_basket => $loop_basket,
151 booksellerid => $vendor->id,
152 name => $vendor->name,
153 active => $vendor->active,
157 $template->param(
158 loop_suppliers => $loop_suppliers,
159 supplier => ( $booksellerid || $supplier ),
160 count => $supplier_count,
161 has_budgets => $has_budgets,
163 $template->{VARS}->{'allbaskets'} = $allbaskets;
165 output_html_with_http_headers $query, $cookie, $template->output;