Followup : fb4366cdad0ad96d0427810581763dc2fc189af1
[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 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along with
20 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
21 # Suite 330, Boston, MA  02111-1307 USA
22
23
24 =head1 NAME
25
26 booksellers.pl
27
28 =head1 DESCRIPTION
29
30 this script displays the list of suppliers & orders 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 suplier we have to search order.
41 =back
42
43 =item op
44
45 C<OP> can be equals to 'close' if we have to close a basket before building the page.
46
47 =item basket
48
49 the C<basket> we have to close if op is equal to 'close'.
50
51 =back
52
53 =cut
54
55 use strict;
56 use C4::Auth;
57 use C4::Biblio;
58 use C4::Output;
59 use CGI;
60
61
62 use C4::Acquisition;
63 use C4::Dates qw/format_date/;
64 use C4::Bookseller;
65 use C4::Members qw/GetMember/;
66
67 my $query = new CGI;
68 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
69     {
70         template_name   => "acqui/booksellers.tmpl",
71         query           => $query,
72         type            => "intranet",
73         authnotrequired => 0,
74         flagsrequired   => { acquisition => 'vendors_manage' },
75         debug           => 1,
76     }
77 );
78
79 #parameters
80 my $supplier = $query->param('supplier');
81 my $id       = $query->param('id') || $query->param('supplierid');
82 my @suppliers;
83
84 if ($id) {
85         push @suppliers, GetBookSellerFromId($id);
86 } else {
87         @suppliers = GetBookSeller($supplier);
88 }
89 my $count = scalar @suppliers;
90 if ($count == 1){
91         $template->param( supplier_name => $suppliers[0]->{'name'},
92                 id => $suppliers[0]->{'id'}
93         );
94 }
95
96 #build result page
97 my @loop_suppliers;
98 for ( my $i = 0 ; $i < $count ; $i++ ) {
99     my $orders  = GetBasketsByBookseller( $suppliers[$i]->{'id'}, {groupby => "aqbasket.basketno", orderby => "aqbasket.basketname"} );
100     my $ordcount = scalar @$orders;
101     my %line;
102
103     $line{supplierid} = $suppliers[$i]->{'id'};
104     $line{name}       = $suppliers[$i]->{'name'};
105     $line{active}     = $suppliers[$i]->{'active'};
106     my @loop_basket;
107     my $uid = GetMember(borrowernumber => $loggedinuser)->{userid} if $loggedinuser;
108     for ( my $i2 = 0 ; $i2 < $ordcount ; $i2++ ) {
109         if ( $orders->[$i2]{'authorisedby'} eq $loggedinuser || haspermission($uid, { flagsrequired   => { 'acquisition' => '*' } } ) ) {
110             my %inner_line;
111             $inner_line{basketno}     = $orders->[$i2]{'basketno'};
112             $inner_line{basketname}     = $orders->[$i2]{'basketname'};
113             $inner_line{total}        = scalar GetOrders($orders->[$i2]{'basketno'});
114             $inner_line{authorisedby} = $orders->[$i2]{'authorisedby'};
115             my $authby = GetMember( $orders->[$i2]{'authorisedby'});
116             $inner_line{surname}      = $authby->{'firstname'};
117             $inner_line{firstname}    = $authby->{'surname'};
118             $inner_line{creationdate} = format_date( $orders->[$i2]{'creationdate'} );
119             $inner_line{closedate}    = format_date( $orders->[$i2]{'closedate'}    );
120             $inner_line{uncertainprice} = $orders->[$i2]{'uncertainprice'};
121             push @loop_basket, \%inner_line;
122         }
123     }
124     $line{loop_basket} = \@loop_basket;
125     push @loop_suppliers, \%line;
126 }
127 $template->param(
128     loop_suppliers          => \@loop_suppliers,
129     supplier                => ($id || $supplier),
130     count                   => $count,
131 );
132
133 output_html_with_http_headers $query, $cookie, $template->output;