Merge remote-tracking branch 'kc/new/bug_6755' into kcmaster
[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 supplierid
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::Bookseller qw/ GetBookSellerFromId GetBookSeller /;
61 use C4::Members qw/GetMember/;
62
63 my $query = CGI->new;
64 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
65     {   template_name   => 'acqui/booksellers.tmpl',
66         query           => $query,
67         type            => 'intranet',
68         authnotrequired => 0,
69         flagsrequired   => { acquisition => '*' },
70         debug           => 1,
71     }
72 );
73
74 #parameters
75 my $supplier = $query->param('supplier');
76 my $id = $query->param('id') || $query->param('supplierid');
77 my @suppliers;
78
79 if ($id) {
80     push @suppliers, GetBookSellerFromId($id);
81 } else {
82     @suppliers = GetBookSeller($supplier);
83 }
84
85 my $supplier_count = @suppliers;
86 if ( $supplier_count == 1 ) {
87     $template->param(
88         supplier_name => $suppliers[0]->{'name'},
89         id            => $suppliers[0]->{'id'}
90     );
91 }
92
93 my $uid;
94 if ($loggedinuser) {
95     $uid = GetMember( borrowernumber => $loggedinuser )->{userid};
96 }
97
98 #build result page
99 my $loop_suppliers = [];
100
101 for my $vendor (@suppliers) {
102     my $baskets = get_vendors_baskets( $vendor->{id} );
103
104     my $loop_basket = [];
105     for my $basket ( @{$baskets} ) {
106         if ((      $basket->{authorisedby}
107                 && $basket->{authorisedby} eq $loggedinuser
108             )
109             || haspermission( $uid, { acquisition => q{*} } )
110           ) {
111             for my $date_field (qw( creationdate closedate)) {
112                 if ( $basket->{$date_field} ) {
113                     $basket->{$date_field} =
114                       format_date( $basket->{$date_field} );
115                 }
116             }
117             push @{$loop_basket}, $basket;
118         }
119     }
120
121     push @{$loop_suppliers},
122       { loop_basket => $loop_basket,
123         supplierid  => $vendor->{id},
124         name        => $vendor->{name},
125         active      => $vendor->{active},
126       };
127
128 }
129 $template->param(
130     loop_suppliers => $loop_suppliers,
131     supplier       => ( $id || $supplier ),
132     count          => $supplier_count,
133 );
134
135 output_html_with_http_headers $query, $cookie, $template->output;
136
137 sub get_vendors_baskets {
138     my $supplier_id = shift;
139     my $dbh         = C4::Context->dbh;
140     my $sql         = <<'ENDSQL';
141 select aqbasket.*, count(*) as total,  borrowers.firstname, borrowers.surname
142 from aqbasket left join aqorders on aqorders.basketno = aqbasket.basketno
143 left join borrowers on aqbasket.authorisedby = borrowers.borrowernumber
144 where booksellerid = ?
145 AND ( aqorders.quantity > aqorders.quantityreceived OR quantityreceived IS NULL)
146 AND datecancellationprinted IS NULL
147 group by basketno
148 ENDSQL
149     return $dbh->selectall_arrayref( $sql, { Slice => {} }, $supplier_id );
150 }