MT 1816: Granular permissions for the serials module
[koha.git] / C4 / Bookseller.pm
old mode 100755 (executable)
new mode 100644 (file)
index e5893e5..0153f2b
@@ -17,21 +17,22 @@ package C4::Bookseller;
 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
 # Suite 330, Boston, MA  02111-1307 USA
 
-# $Id$
-
 use strict;
 
 use vars qw($VERSION @ISA @EXPORT);
 
-# set the version for version checking
-$VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
-
-@ISA    = qw(Exporter);
-@EXPORT = qw(
-    &GetBookSeller &GetBooksellersWithLateOrders
-    &ModBookseller
-    &AddBookseller
-);
+BEGIN {
+       # set the version for version checking
+       $VERSION = 3.01;
+    require Exporter;
+       @ISA    = qw(Exporter);
+       @EXPORT = qw(
+               &GetBookSeller &GetBooksellersWithLateOrders &GetBookSellerFromId
+               &ModBookseller
+               &DelBookseller
+               &AddBookseller
+       );
+}
 
 
 =head1 NAME
@@ -50,15 +51,7 @@ a bookseller.
 
 =head1 FUNCTIONS
 
-=over 2
-
-=cut
-
-#-------------------------------------------------------------------#
-
-=head3 GetBookSeller
-
-=over 4
+=head2 GetBookSeller
 
 @results = &GetBookSeller($searchstring);
 
@@ -68,22 +61,24 @@ ID, or a string to look for in the book seller's name.
 C<@results> is an array of references-to-hash, whose keys are the fields of of the
 aqbooksellers table in the Koha database.
 
-=back
-
 =cut
 
-sub GetBookSeller {
+# FIXME: This function is badly named.  It should be something like 
+# SearchBookSellersByName.  It is NOT a singular return value.
+
+sub GetBookSeller($) {
     my ($searchstring) = @_;
     my $dbh = C4::Context->dbh;
-    my $query = "
-        SELECT *
-        FROM   aqbooksellers
-        WHERE  name LIKE ? OR id = ?
-    ";
+    my $query = "SELECT * FROM aqbooksellers WHERE name LIKE ? ";
     my $sth =$dbh->prepare($query);
-    $sth->execute("$searchstring%", $searchstring );
+    $sth->execute( "%$searchstring%");
     my @results;
+    # count how many baskets this bookseller has.
+    # if it has none, the bookseller can be deleted
+    my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
     while ( my $data = $sth->fetchrow_hashref ) {
+        $sth2->execute($data->{id});
+        $data->{basketcount} = $sth2->fetchrow();
         push( @results, $data );
     }
     $sth->finish;
@@ -91,22 +86,32 @@ sub GetBookSeller {
 }
 
 
+sub GetBookSellerFromId($) {
+       my ($id) = shift or return undef;
+       my $dbh = C4::Context->dbh();
+       my $query = "SELECT * FROM aqbooksellers WHERE id = ?";
+       my $sth =$dbh->prepare($query);
+       $sth->execute( $id );
+       if (my $data = $sth->fetchrow_hashref()){
+               my $sth2 = $dbh->prepare("SELECT count(*) FROM aqbasket WHERE booksellerid=?");
+               $sth2->execute($id);
+               $data->{basketcount}=$sth2->fetchrow();
+               return ($data);
+       }
+       return 0;
+}
 #-----------------------------------------------------------------#
 
-=head3 GetBooksellersWithLateOrders
-
-=over 4
+=head2 GetBooksellersWithLateOrders
 
 %results = &GetBooksellersWithLateOrders;
 
 Searches for suppliers with late orders.
 
-=back
-
 =cut
 
 sub GetBooksellersWithLateOrders {
-    my $delay = shift;
+    my ($delay,$branch) = @_;  # FIXME: Branch argument unused.
     my $dbh   = C4::Context->dbh;
 
 # FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
@@ -117,20 +122,18 @@ sub GetBooksellersWithLateOrders {
     if ( $dbdriver eq "mysql" ) {
         $strsth = "
             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
-            FROM aqorders, aqbasket
+            FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
             LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
-            WHERE aqorders.basketno = aqbasket.basketno
-                AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
+            WHERE (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY)
                 AND (datereceived = '' OR datereceived IS NULL))
         ";
     }
     else {
         $strsth = "
             SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
-            FROM aqorders, aqbasket
+            FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
             LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id
-            WHERE aqorders.basketno = aqbasket.basketno
-                AND (closedate < (CURDATE( )-(INTERVAL $delay DAY)))
+            WHERE (closedate < (CURDATE( )-(INTERVAL $delay DAY)))
                 AND (datereceived = '' OR datereceived IS NULL))
         ";
     }
@@ -147,9 +150,7 @@ sub GetBooksellersWithLateOrders {
 
 #--------------------------------------------------------------------#
 
-=head3 AddBookseller
-
-=over 4
+=head2 AddBookseller
 
 $id = &AddBookseller($bookseller);
 
@@ -159,8 +160,6 @@ All fields must be present.
 
 Returns the ID of the newly-created bookseller.
 
-=back
-
 =cut
 
 sub AddBookseller {
@@ -197,22 +196,21 @@ sub AddBookseller {
     );
 
     # return the id of this new supplier
-    my $query = "
+    # FIXME: no protection against simultaneous addition: max(id) might be wrong!
+    $query = "
         SELECT max(id)
         FROM   aqbooksellers
     ";
-    my $sth = $dbh->prepare($query);
+    $sth = $dbh->prepare($query);
     $sth->execute;
     return scalar($sth->fetchrow);
 }
 
 #-----------------------------------------------------------------#
 
-=head3 ModSupplier
-
-=over 4
+=head2 ModBookseller
 
-&ModSupplier($bookseller);
+&ModBookseller($bookseller);
 
 Updates the information for a given bookseller. C<$bookseller> is a
 reference-to-hash whose keys are the fields of the aqbooksellers table
@@ -223,8 +221,6 @@ The easiest way to get all of the necessary fields is to look up a
 book seller with C<&booksellers>, modify what's necessary, then call
 C<&ModSupplier> with the result.
 
-=back
-
 =cut
 
 sub ModBookseller {
@@ -260,15 +256,25 @@ sub ModBookseller {
     $sth->finish;
 }
 
+=head2 DelBookseller
+
+&DelBookseller($booksellerid);
+
+delete the supplier identified by $booksellerid
+This sub can be called only if the supplier has no order.
 
-END { }    # module clean-up code here (global destructor)
+=cut
+sub DelBookseller {
+    my ($id) = @_;
+    my $dbh=C4::Context->dbh;
+    my $sth=$dbh->prepare("DELETE FROM aqbooksellers WHERE id=?");
+    $sth->execute($id);
+}
 
 1;
 
 __END__
 
-=back
-
 =head1 AUTHOR
 
 Koha Developement team <info@koha.org>