Able to call haspermission w/o $dbh, and add error msg on deletemember.
[koha.git] / C4 / Bookseller.pm
old mode 100755 (executable)
new mode 100644 (file)
index 279f39b..72f7896
@@ -17,19 +17,19 @@ 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 ); };
+$VERSION = 3.00;
 
 @ISA    = qw(Exporter);
 @EXPORT = qw(
     &GetBookSeller &GetBooksellersWithLateOrders
     &ModBookseller
+    &DelBookseller
     &AddBookseller
 );
 
@@ -77,7 +77,12 @@ sub GetBookSeller {
     my $sth =$dbh->prepare($query);
     $sth->execute("$searchstring%", $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;
@@ -107,20 +112,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))
         ";
     }
@@ -242,6 +245,20 @@ 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.
+
+=cut
+sub DelBookseller {
+    my ($id) = @_;
+    my $dbh=C4::Context->dbh;
+    my $sth=$dbh->prepare("DELETE FROM aqbooksellers WHERE id=?");
+    $sth->execute($id);
+}
 END { }    # module clean-up code here (global destructor)
 
 1;