X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FBookseller.pm;h=4ad42cb6f24b05d3a7df29b5b96dee6c00e7b2f7;hb=902d19dfa93d5709848afb26ab474b3353efcd4a;hp=279f39bd1bca999c826085f3c50846e96b19dfaf;hpb=2ffd5b7228f4e638583162d483e1dd2febeafe1b;p=koha.git diff --git a/C4/Bookseller.pm b/C4/Bookseller.pm old mode 100755 new mode 100644 index 279f39bd1b..4ad42cb6f2 --- a/C4/Bookseller.pm +++ b/C4/Bookseller.pm @@ -1,6 +1,7 @@ package C4::Bookseller; # Copyright 2000-2002 Katipo Communications +# Copyright 2010 PTFS Europe # # This file is part of Koha. # @@ -13,27 +14,24 @@ 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 warnings; -use vars qw($VERSION @ISA @EXPORT); +use base qw( Exporter ); # 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 +our $VERSION = 4.01; +our @EXPORT_OK = qw( + GetBookSeller GetBooksellersWithLateOrders GetBookSellerFromId + ModBookseller + DelBookseller + AddBookseller ); - =head1 NAME C4::Bookseller - Koha functions for dealing with booksellers. @@ -50,82 +48,69 @@ a bookseller. =head1 FUNCTIONS -=cut - -#-------------------------------------------------------------------# - =head2 GetBookSeller -@results = &GetBookSeller($searchstring); +@results = GetBookSeller($searchstring); Looks up a book seller. C<$searchstring> may be either a book seller 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 +C<@results> is an array of hash_refs whose keys are the fields of of the aqbooksellers table in the Koha database. =cut sub GetBookSeller { - my ($searchstring) = @_; + my $searchstring = shift; + $searchstring = q{%} . $searchstring . q{%}; + my $query = +'select aqbooksellers.*, count(*) as basketcount from aqbooksellers left join aqbasket ' + . 'on aqbasket.booksellerid = aqbooksellers.id where name lIke ? group by aqbooksellers.id order by name'; + + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare($query); + $sth->execute($searchstring); + my $resultset_ref = $sth->fetchall_arrayref( {} ); + return @{$resultset_ref}; +} + +sub GetBookSellerFromId { + my $id = shift or return; my $dbh = C4::Context->dbh; - my $query = " - SELECT * - FROM aqbooksellers - WHERE name LIKE ? OR id = ? - "; - my $sth =$dbh->prepare($query); - $sth->execute("$searchstring%", $searchstring ); - my @results; - while ( my $data = $sth->fetchrow_hashref ) { - push( @results, $data ); + my $vendor = + $dbh->selectrow_hashref( 'SELECT * FROM aqbooksellers WHERE id = ?', + {}, $id ); + if ($vendor) { + ( $vendor->{basketcount} ) = $dbh->selectrow_array( + 'SELECT count(*) FROM aqbasket where booksellerid = ?', + {}, $id ); } - $sth->finish; - return @results ; + return $vendor; } - #-----------------------------------------------------------------# =head2 GetBooksellersWithLateOrders -%results = &GetBooksellersWithLateOrders; +%results = GetBooksellersWithLateOrders($delay); Searches for suppliers with late orders. =cut sub GetBooksellersWithLateOrders { - my ($delay,$branch) = @_; + my $delay = shift; my $dbh = C4::Context->dbh; -# FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so -# should be tested with other DBMs - - my $strsth; - my $dbdriver = C4::Context->config("db_scheme") || "mysql"; - if ( $dbdriver eq "mysql" ) { - $strsth = " - SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name - FROM aqorders, aqbasket - LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id - WHERE aqorders.basketno = aqbasket.basketno - AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY) - AND (datereceived = '' OR datereceived IS NULL)) - "; - } - else { - $strsth = " - SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name - FROM aqorders, aqbasket - LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id - WHERE aqorders.basketno = aqbasket.basketno - AND (closedate < (CURDATE( )-(INTERVAL $delay DAY))) - AND (datereceived = '' OR datereceived IS NULL)) - "; - } + # TODO delay should be verified + my $query_string = + "SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name + FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno + LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id + WHERE (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY) + AND (datereceived = '' OR datereceived IS NULL))"; - my $sth = $dbh->prepare($strsth); + my $sth = $dbh->prepare($query_string); $sth->execute; my %supplierlist; while ( my ( $id, $name ) = $sth->fetchrow ) { @@ -151,19 +136,19 @@ Returns the ID of the newly-created bookseller. sub AddBookseller { my ($data) = @_; - my $dbh = C4::Context->dbh; - my $query = " + my $dbh = C4::Context->dbh; + my $query = q| INSERT INTO aqbooksellers ( name, address1, address2, address3, address4, postal, phone, fax, url, contact, contpos, contphone, contfax, contaltphone, contemail, contnotes, active, listprice, invoiceprice, gstreg, - listincgst,invoiceincgst, specialty, discount, invoicedisc, - nocalc, notes + listincgst,invoiceincgst, gstrate, discount, + notes ) - VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) - "; + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) | + ; my $sth = $dbh->prepare($query); $sth->execute( $data->{'name'}, $data->{'address1'}, @@ -177,26 +162,19 @@ 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->{'gstrate'}, + $data->{'discount'}, $data->{'notes'} ); # return the id of this new supplier - $query = " - SELECT max(id) - FROM aqbooksellers - "; - $sth = $dbh->prepare($query); - $sth->execute; - return scalar($sth->fetchrow); + return $dbh->{'mysql_insertid'}; } #-----------------------------------------------------------------# -=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 @@ -204,25 +182,23 @@ in the Koha database. It must contain entries for all of the fields. The entry to modify is determined by C<$bookseller-E{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 sub ModBookseller { my ($data) = @_; my $dbh = C4::Context->dbh; - my $query = " - UPDATE aqbooksellers + my $query = 'UPDATE aqbooksellers SET name=?,address1=?,address2=?,address3=?,address4=?, postal=?,phone=?,fax=?,url=?,contact=?,contpos=?, contphone=?,contfax=?,contaltphone=?,contemail=?, contnotes=?,active=?,listprice=?, invoiceprice=?, - gstreg=?, listincgst=?,invoiceincgst=?, - specialty=?,discount=?,invoicedisc=?,nocalc=?, notes=? - WHERE id=? - "; - my $sth = $dbh->prepare($query); + gstreg=?,listincgst=?,invoiceincgst=?, + discount=?,notes=?,gstrate=? + WHERE id=?'; + my $sth = $dbh->prepare($query); $sth->execute( $data->{'name'}, $data->{'address1'}, $data->{'address2'}, $data->{'address3'}, @@ -235,14 +211,28 @@ 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; + return; } -END { } # module clean-up code here (global destructor) +=head2 DelBookseller + +DelBookseller($booksellerid); + +delete the supplier record identified by $booksellerid +This sub assumes it is called only if the supplier has no order. + +=cut + +sub DelBookseller { + my $id = shift; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?'); + $sth->execute($id); + return; +} 1; @@ -250,6 +240,6 @@ __END__ =head1 AUTHOR -Koha Developement team +Koha Development Team =cut