X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FBookseller.pm;h=8a5f43e10ef5e4c913eb8543d515c43cfb30a9aa;hb=20d9ed618fbe3cdcb9c04444a1f8a584b0364069;hp=5874a936819650305a9da467a15eaf7145754385;hpb=9dc42e9ec4bb6ab0511000018adaa0f7883c905f;p=koha.git diff --git a/C4/Bookseller.pm b/C4/Bookseller.pm index 5874a93681..8a5f43e10e 100644 --- a/C4/Bookseller.pm +++ b/C4/Bookseller.pm @@ -5,28 +5,28 @@ package C4::Bookseller; # # This file is part of Koha. # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY 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., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use strict; use warnings; use base qw( Exporter ); -# set the version for version checking -our $VERSION = 3.07.00.049; +use C4::Bookseller::Contact; + our @EXPORT_OK = qw( - GetBookSeller GetBooksellersWithLateOrders GetBookSellerFromId + GetBooksellersWithLateOrders ModBookseller DelBookseller AddBookseller @@ -48,73 +48,25 @@ a bookseller. =head1 FUNCTIONS -=head2 GetBookSeller - -@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 hash_refs whose keys are the fields of of the -aqbooksellers table in the Koha database. - -=cut - -sub GetBookSeller { - 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 $vendor = - $dbh->selectrow_hashref( 'SELECT * FROM aqbooksellers WHERE id = ?', - {}, $id ); - if ($vendor) { - ( $vendor->{basketcount} ) = $dbh->selectrow_array( - 'SELECT count(*) FROM aqbasket where booksellerid = ?', - {}, $id ); - ( $vendor->{subscriptioncount} ) = $dbh->selectrow_array( - 'SELECT count(*) FROM subscription WHERE aqbooksellerid = ?', - {}, $id ); - } - return $vendor; -} - -#-----------------------------------------------------------------# - =head2 GetBooksellersWithLateOrders -%results = GetBooksellersWithLateOrders($delay); +%results = GetBooksellersWithLateOrders( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ); Searches for suppliers with late orders. =cut sub GetBooksellersWithLateOrders { - my ( $delay, $branch, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_; # FIXME: Branch argument unused. + my ( $delay, $estimateddeliverydatefrom, $estimateddeliverydateto ) = @_; 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 $query; my @query_params = (); my $dbdriver = C4::Context->config("db_scheme") || "mysql"; - $strsth = " + $query = " SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name FROM aqorders LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id @@ -125,27 +77,33 @@ sub GetBooksellersWithLateOrders { ) AND aqorders.rrp <> 0 AND aqorders.ecost <> 0 - AND aqorders.quantity - IFNULL(aqorders.quantityreceived,0) <> 0 + AND aqorders.quantity - COALESCE(aqorders.quantityreceived,0) <> 0 AND aqbasket.closedate IS NOT NULL "; - if ( defined $delay ) { - $strsth .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) "; + if ( defined $delay && $delay >= 0 ) { + $query .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? + COALESCE(aqbooksellers.deliverytime,0) DAY)) "; push @query_params, $delay; + } elsif ( $delay && $delay < 0 ){ + warn 'WARNING: GetBooksellerWithLateOrders is called with a negative value'; + return; } if ( defined $estimateddeliverydatefrom ) { - $strsth .= ' - AND aqbooksellers.deliverytime IS NOT NULL - AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?'; - push @query_params, $estimateddeliverydatefrom; + $query .= ' + AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) >= ?'; + push @query_params, $estimateddeliverydatefrom; + if ( defined $estimateddeliverydateto ) { + $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= ?'; + push @query_params, $estimateddeliverydateto; + } else { + $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime, 0) DAY) <= CAST(now() AS date)'; + } } - if ( defined $estimateddeliverydatefrom and defined $estimateddeliverydateto ) { - $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?'; + if ( defined $estimateddeliverydateto ) { + $query .= ' AND ADDDATE(aqbasket.closedate, INTERVAL COALESCE(aqbooksellers.deliverytime,0) DAY) <= ?'; push @query_params, $estimateddeliverydateto; - } elsif ( defined $estimateddeliverydatefrom ) { - $strsth .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)'; } - my $sth = $dbh->prepare($strsth); + my $sth = $dbh->prepare($query); $sth->execute( @query_params ); my %supplierlist; while ( my ( $id, $name ) = $sth->fetchrow ) { @@ -170,20 +128,18 @@ Returns the ID of the newly-created bookseller. =cut sub AddBookseller { - my ($data) = @_; + my ($data, $contacts) = @_; my $dbh = C4::Context->dbh; - my $query = q| + my $query = q| INSERT INTO aqbooksellers ( - name, address1, address2, address3, address4, - postal, phone, accountnumber, fax, url, - contact, - contpos, contphone, contfax, contaltphone, contemail, - contnotes, active, listprice, invoiceprice, gstreg, - listincgst,invoiceincgst, gstrate, discount, - notes + name, address1, address2, address3, address4, + postal, phone, accountnumber,fax, url, + active, listprice, invoiceprice, gstreg, + listincgst,invoiceincgst, tax_rate, discount, notes, + deliverytime ) - VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) | + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) | ; my $sth = $dbh->prepare($query); $sth->execute( @@ -191,20 +147,25 @@ sub AddBookseller { $data->{'address2'}, $data->{'address3'}, $data->{'address4'}, $data->{'postal'}, $data->{'phone'}, $data->{'accountnumber'}, - $data->{'fax'}, - $data->{'url'}, $data->{'contact'}, - $data->{'contpos'}, $data->{'contphone'}, - $data->{'contfax'}, $data->{'contaltphone'}, - $data->{'contemail'}, $data->{'contnotes'}, + $data->{'fax'}, $data->{'url'}, $data->{'active'}, $data->{'listprice'}, $data->{'invoiceprice'}, $data->{'gstreg'}, $data->{'listincgst'}, $data->{'invoiceincgst'}, - $data->{'gstrate'}, - $data->{'discount'}, $data->{'notes'} + $data->{'tax_rate'}, $data->{'discount'}, + $data->{notes}, $data->{deliverytime}, ); # return the id of this new supplier - return $dbh->{'mysql_insertid'}; + my $id = $dbh->{'mysql_insertid'}; + if ($id && $contacts) { + foreach my $contact (@$contacts) { + $contact = C4::Bookseller::Contact->new( $contact ) + unless ref $contacts eq 'C4::Bookseller::Contact'; + $contact->bookseller($id); + $contact->save(); + } + } + return $id; } #-----------------------------------------------------------------# @@ -219,42 +180,54 @@ 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<&GetBookseller>, modify what's necessary, then call +book seller with C, modify what's necessary, then call C<&ModBookseller> with the result. =cut sub ModBookseller { - my ($data) = @_; + my ($data, $contacts) = @_; my $dbh = C4::Context->dbh; + return unless $data->{'id'}; my $query = 'UPDATE aqbooksellers SET name=?,address1=?,address2=?,address3=?,address4=?, - postal=?,phone=?,accountnumber=?,fax=?,url=?,contact=?,contpos=?, - contphone=?,contfax=?,contaltphone=?,contemail=?, - contnotes=?,active=?,listprice=?, invoiceprice=?, + postal=?,phone=?,accountnumber=?,fax=?,url=?, + active=?,listprice=?, invoiceprice=?, gstreg=?,listincgst=?,invoiceincgst=?, - discount=?,notes=?,gstrate=?,deliverytime=? + discount=?,notes=?,tax_rate=?,deliverytime=? WHERE id=?'; my $sth = $dbh->prepare($query); - $sth->execute( + my $cnt = $sth->execute( $data->{'name'}, $data->{'address1'}, $data->{'address2'}, $data->{'address3'}, $data->{'address4'}, $data->{'postal'}, $data->{'phone'}, $data->{'accountnumber'}, - $data->{'fax'}, - $data->{'url'}, $data->{'contact'}, - $data->{'contpos'}, $data->{'contphone'}, - $data->{'contfax'}, $data->{'contaltphone'}, - $data->{'contemail'}, $data->{'contnotes'}, + $data->{'fax'}, $data->{'url'}, $data->{'active'}, $data->{'listprice'}, $data->{'invoiceprice'}, $data->{'gstreg'}, $data->{'listincgst'}, $data->{'invoiceincgst'}, $data->{'discount'}, $data->{'notes'}, - $data->{'gstrate'}, - $data->{deliverytime}, + $data->{'tax_rate'}, $data->{deliverytime}, $data->{'id'} ); - return; + $contacts ||= $data->{'contacts'}; + my $contactquery = "DELETE FROM aqcontacts WHERE booksellerid = ?"; + my @contactparams = ($data->{'id'}); + if ($contacts) { + foreach my $contact (@$contacts) { + $contact = C4::Bookseller::Contact->new( $contact ) + unless ref $contacts eq 'C4::Bookseller::Contact'; + $contact->bookseller($data->{'id'}); + $contact->save(); + push @contactparams, $contact->id if $contact->id; + } + if ($#contactparams > 0) { + $contactquery .= ' AND id NOT IN (' . ('?, ' x ($#contactparams - 1)) . '?);'; + } + } + $sth = $dbh->prepare($contactquery); + $sth->execute(@contactparams); + return $cnt; } =head2 DelBookseller @@ -270,8 +243,7 @@ sub DelBookseller { my $id = shift; my $dbh = C4::Context->dbh; my $sth = $dbh->prepare('DELETE FROM aqbooksellers WHERE id=?'); - $sth->execute($id); - return; + return $sth->execute($id); } 1;