Fix FSF address in directory C4/
[koha.git] / C4 / Bookseller.pm
old mode 100755 (executable)
new mode 100644 (file)
index fe0ce8b..361039f
@@ -13,25 +13,26 @@ package C4::Bookseller;
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
-
-# $Id$
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 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,10 +51,6 @@ a bookseller.
 
 =head1 FUNCTIONS
 
-=cut
-
-#-------------------------------------------------------------------#
-
 =head2 GetBookSeller
 
 @results = &GetBookSeller($searchstring);
@@ -66,18 +63,22 @@ aqbooksellers table in the Koha database.
 
 =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;
@@ -85,6 +86,20 @@ sub GetBookSeller {
 }
 
 
+sub GetBookSellerFromId($) {
+       my $id = shift or return;
+       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;
+}
 #-----------------------------------------------------------------#
 
 =head2 GetBooksellersWithLateOrders
@@ -96,7 +111,7 @@ Searches for suppliers with late orders.
 =cut
 
 sub GetBooksellersWithLateOrders {
-    my ($delay,$branch) = @_;
+    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
@@ -157,10 +172,10 @@ sub AddBookseller {
                 postal,    phone,         fax,        url,           contact,
                 contpos,   contphone,     contfax,    contaltphone,  contemail,
                 contnotes, active,        listprice,  invoiceprice,  gstreg,
-                listincgst,invoiceincgst, specialty,  discount,      invoicedisc,
-                nocalc,    notes
+                listincgst,invoiceincgst,   discount,
+                notes
             )
-        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
+        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
     ";
     my $sth = $dbh->prepare($query);
     $sth->execute(
@@ -175,12 +190,11 @@ sub AddBookseller {
         $data->{'active'},       $data->{'listprice'},
         $data->{'invoiceprice'}, $data->{'gstreg'},
         $data->{'listincgst'},   $data->{'invoiceincgst'},
-        $data->{'specialty'},    $data->{'discount'},
-        $data->{'invoicedisc'},  $data->{'nocalc'},
-        $data->{'notes'}
+        $data->{'discount'},     $data->{'notes'}
     );
 
     # return the id of this new supplier
+    # FIXME: no protection against simultaneous addition: max(id) might be wrong!
     $query = "
         SELECT max(id)
         FROM   aqbooksellers
@@ -192,9 +206,9 @@ sub AddBookseller {
 
 #-----------------------------------------------------------------#
 
-=head2 ModSupplier
+=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
@@ -202,8 +216,8 @@ in the Koha database. It must contain entries for all of the fields.
 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
 
 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.
+book seller with C<&GetBookseller>, modify what's necessary, then call
+C<&ModBookseller> with the result.
 
 =cut
 
@@ -216,8 +230,8 @@ sub ModBookseller {
             postal=?,phone=?,fax=?,url=?,contact=?,contpos=?,
             contphone=?,contfax=?,contaltphone=?,contemail=?,
             contnotes=?,active=?,listprice=?, invoiceprice=?,
-            gstreg=?, listincgst=?,invoiceincgst=?,
-            specialty=?,discount=?,invoicedisc=?,nocalc=?, notes=?
+            gstreg=?,listincgst=?,invoiceincgst=?,
+            discount=?, notes=?, gstrate=?
         WHERE id=?
     ";
     my $sth    = $dbh->prepare($query);
@@ -233,14 +247,27 @@ sub ModBookseller {
         $data->{'active'},       $data->{'listprice'},
         $data->{'invoiceprice'}, $data->{'gstreg'},
         $data->{'listincgst'},   $data->{'invoiceincgst'},
-        $data->{'specialty'},    $data->{'discount'},
-        $data->{'invoicedisc'},  $data->{'nocalc'},
-        $data->{'notes'},        $data->{'id'}
+        $data->{'discount'},
+        $data->{'notes'},        $data->{'gstrate'},
+        $data->{'id'}
     );
     $sth->finish;
 }
 
-END { }    # module clean-up code here (global destructor)
+=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);
+}
 
 1;