Merge remote-tracking branch 'kc/master' into merged_5549
[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 =item id or booksellerid
45
46 The id of the supplier whose baskets we will display
47
48 =back
49
50 =cut
51
52 use strict;
53 use warnings;
54 use C4::Auth;
55 use C4::Biblio;
56 use C4::Output;
57 use CGI;
58
59 use C4::Dates qw/format_date/;
60 use C4::Acquisition qw/ GetBasketsInfosByBookseller /;
61 use C4::Bookseller qw/ GetBookSellerFromId GetBookSeller /;
62 use C4::Members qw/GetMember/;
63 use C4::Context;
64
65 my $query = CGI->new;
66 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
67     {   template_name   => 'acqui/booksellers.tmpl',
68         query           => $query,
69         type            => 'intranet',
70         authnotrequired => 0,
71         flagsrequired   => { acquisition => '*' },
72         debug           => 1,
73     }
74 );
75
76 #parameters
77 my $supplier = $query->param('supplier');
78 my $booksellerid = $query->param('booksellerid');
79 my @suppliers;
80
81 if ($booksellerid) {
82     push @suppliers, GetBookSellerFromId($booksellerid);
83 } else {
84     @suppliers = GetBookSeller($supplier);
85 }
86
87 my $supplier_count = @suppliers;
88 if ( $supplier_count == 1 ) {
89     $template->param(
90         supplier_name => $suppliers[0]->{'name'},
91         booksellerid  => $suppliers[0]->{'booksellerid'}
92     );
93 }
94
95 my $uid;
96 if ($loggedinuser) {
97     $uid = GetMember( borrowernumber => $loggedinuser )->{userid};
98 }
99
100 my $userenv = C4::Context::userenv;
101 my $viewbaskets = C4::Context->preference('AcqViewBaskets');
102
103 my $userbranch = $userenv->{branch};
104
105 #build result page
106 my $loop_suppliers = [];
107
108 for my $vendor (@suppliers) {
109     my $baskets = GetBasketsInfosByBookseller( $vendor->{id} );
110
111     my $loop_basket = [];
112
113     for my $basket ( @{$baskets} ) {
114         my $authorisedby = $basket->{authorisedby};
115         my $basketbranch = ''; # set a blank branch to start with
116         my $member = GetMember( borrowernumber => $authorisedby );
117         if ( $member ) {
118            $basketbranch = $member->{branchcode};
119         }
120
121         if ($userenv->{'flags'} & 1 || #user is superlibrarian
122                (haspermission( $uid, { acquisition => q{*} } ) && #user has acq permissions and
123                    ($viewbaskets eq 'all' || #user is allowed to see all baskets
124                    ($viewbaskets eq 'branch' && $authorisedby && $userbranch eq $basketbranch) || #basket belongs to user's branch
125                    ($basket->{authorisedby} &&  $viewbaskets eq 'user' && $authorisedby == $loggedinuser) #user created this basket
126                    ) 
127                 ) 
128            ) { 
129             for my $date_field (qw( creationdate closedate)) {
130                 if ( $basket->{$date_field} ) {
131                     $basket->{$date_field} = format_date( $basket->{$date_field} );
132                 }
133             }
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             push @{$loop_basket}, $basket; 
142         }
143     }
144
145     push @{$loop_suppliers},
146       { loop_basket => $loop_basket,
147         booksellerid  => $vendor->{id},
148         name        => $vendor->{name},
149         active      => $vendor->{active},
150       };
151
152 }
153 $template->param(
154     loop_suppliers => $loop_suppliers,
155     supplier       => ( $booksellerid || $supplier ),
156     count          => $supplier_count,
157     dateformat     => C4::Context->preference('dateformat'),
158 );
159
160 output_html_with_http_headers $query, $cookie, $template->output;