Bug 19074: Fix category display in Batch patron modification.
[koha.git] / C4 / Bookseller.pm
1 package C4::Bookseller;
2
3 # Copyright 2000-2002 Katipo Communications
4 # Copyright 2010 PTFS Europe
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23
24 use base qw( Exporter );
25
26 our @EXPORT_OK = qw(
27   GetBooksellersWithLateOrders
28 );
29
30 =head1 NAME
31
32 C4::Bookseller - Koha functions for dealing with booksellers.
33
34 =head1 SYNOPSIS
35
36 use C4::Bookseller;
37
38 =head1 DESCRIPTION
39
40 The functions in this module deal with booksellers. They allow to
41 add a new bookseller, to modify it or to get some informations around
42 a bookseller.
43
44 =head1 FUNCTIONS
45
46 =head2 GetBooksellersWithLateOrders
47
48 %results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto );
49
50 Searches for suppliers with late orders.
51
52 =cut
53
54 sub GetBooksellersWithLateOrders {
55     my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_;
56     my $dbh = C4::Context->dbh;
57
58     # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
59     # should be tested with other DBMs
60
61     my $query;
62     my @query_params = ();
63     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
64     $query = "
65         SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
66         FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
67         LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
68         WHERE
69             ( datereceived = ''
70             OR datereceived IS NULL
71             OR aqorders.quantityreceived < aqorders.quantity
72             )
73             AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0
74             AND aqbasket.closedate IS NOT NULL
75     ";
76     if ( defined $delay && $delay >= 0 ) {
77         $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime,0) DAY)) ";
78         push @query_params, $delay;
79     } elsif ( $delay && $delay < 0 ){
80         warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value';
81         return;
82     }
83     if ( defined $estimateddeliverydatefrom ) {
84         $query .= '
85             AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) >= ?';
86             push @query_params, $estimateddeliverydatefrom;
87             if ( defined $estimateddeliverydateto ) {
88                 $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?';
89                 push @query_params, $estimateddeliverydateto;
90             } else {
91                     $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)';
92             }
93     }
94     if ( defined $estimateddeliverydateto ) {
95         $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?';
96         push @query_params, $estimateddeliverydateto;
97     }
98
99     my $sth = $dbh->prepare($query);
100     $sth->execute( @query_params );
101     my %supplierlist;
102     while ( my ( $id, $name ) = $sth->fetchrow ) {
103         $supplierlist{$id} = $name;
104     }
105
106     return %supplierlist;
107 }
108
109 1;
110
111 __END__
112
113 =head1 AUTHOR
114
115 Koha Development Team <http://koha-community.org/>
116
117 =cut