-#
-#
-# MONEY
-#
-#
-
-=item invoice
-
- ($count, @results) = &invoice($booksellerinvoicenumber);
-
-Looks up orders by invoice number.
-
-Returns an array. C<$count> is the number of elements in C<@results>.
-C<@results> is an array of references-to-hash; the keys of each
-elements are fields from the aqorders, biblio, and biblioitems tables
-of the Koha database.
-
-=cut
-
-#'
-sub invoice {
- my ($invoice) = @_;
- my $dbh = C4::Context->dbh;
- my @results = ();
- my $sth = $dbh->prepare(
- "Select * from aqorders,biblio,biblioitems where
- booksellerinvoicenumber=?
- and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
- aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber"
- );
- $sth->execute($invoice);
- while ( my $data = $sth->fetchrow_hashref ) {
- push( @results, $data );
- }
- $sth->finish;
- return ( scalar(@results), @results );
-}
-
-=item bookfunds
-
- ($count, @results) = &bookfunds();
-
-Returns a list of all book funds.
-
-C<$count> is the number of elements in C<@results>. C<@results> is an
-array of references-to-hash, whose keys are fields from the aqbookfund
-and aqbudget tables of the Koha database. Results are ordered
-alphabetically by book fund name.
-
-=cut
-
-#'
-sub bookfunds {
- my ($branch) = @_;
- my $dbh = C4::Context->dbh;
- my $userenv = C4::Context->userenv;
- my $branch = $userenv->{branch};
- my $strsth;
-
- if ( $branch ne '' ) {
- $strsth = "SELECT * FROM aqbookfund,aqbudget WHERE aqbookfund.bookfundid
- =aqbudget.bookfundid AND startdate<now() AND enddate>now() AND (aqbookfund.branchcode is null or aqbookfund.branchcode='' or aqbookfund.branchcode= ? )
- GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
- }
- else {
- $strsth = "SELECT * FROM aqbookfund,aqbudget WHERE aqbookfund.bookfundid
- =aqbudget.bookfundid AND startdate<now() AND enddate>now()
- GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
- }
- my $sth = $dbh->prepare($strsth);
- if ( $branch ne '' ) {
- $sth->execute($branch);
- }
- else {
- $sth->execute;
- }
- my @results = ();
- while ( my $data = $sth->fetchrow_hashref ) {
- push( @results, $data );
- }
- $sth->finish;
- return ( scalar(@results), @results );
-}
-
-=item bookfundbreakdown
-
- returns the total comtd & spent for a given bookfund, and a given year
- used in acqui-home.pl
-=cut
-
-#'
-
-sub bookfundbreakdown {
- my ( $id, $year, $start, $end ) = @_;
- my $dbh = C4::Context->dbh;
-
- # if no start/end dates given defaut to everything
- if ( !$start ) {
- $start = '0000-00-00';
- $end = 'now()';
- }
-
- # do a query for spent totals.
- my $sth = $dbh->prepare(
- "Select quantity,datereceived,freight,unitprice,listprice,ecost,
- quantityreceived,subscription
- from aqorders left join aqorderbreakdown on
- aqorders.ordernumber=aqorderbreakdown.ordernumber
- where bookfundid=? and (datecancellationprinted is NULL or
- datecancellationprinted='0000-00-00') and
- ((datereceived >= ? and datereceived < ?) or
- (budgetdate >= ? and budgetdate < ?))"
- );
- $sth->execute( $id, $start, $end, $start, $end );
-
- my $spent = 0;
- while ( my $data = $sth->fetchrow_hashref ) {
- if ( $data->{'subscription'} == 1 ) {
- $spent += $data->{'quantity'} * $data->{'unitprice'};
- }
- else {
-
- my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
- $spent += ( $data->{'unitprice'} ) * $data->{'quantityreceived'};
-
- }
- }
-
- # then do a seperate query for commited totals, (pervious single query was
- # returning incorrect comitted results.
-
- my $query = "Select quantity,datereceived,freight,unitprice,
- listprice,ecost,quantityreceived as qrev,
- subscription,title,itemtype,aqorders.biblionumber,
- aqorders.booksellerinvoicenumber,
- quantity-quantityreceived as tleft,
- aqorders.ordernumber as ordnum,entrydate,budgetdate,
- booksellerid,aqbasket.basketno
- from aqorderbreakdown,aqbasket,aqorders
- left join biblioitems on
- biblioitems.biblioitemnumber=aqorders.biblioitemnumber
- where bookfundid=? and aqorders.ordernumber=aqorderbreakdown.ordernumber and
- aqorders.basketno=aqbasket.basketno and
- (budgetdate >= ? and budgetdate < ?) and
- (datecancellationprinted is NULL or datecancellationprinted='0000-00-00')";
- #warn $query;
- my $sth = $dbh->prepare($query);
- $sth->execute( $id, $start, $end );
-
- my $comtd;
-
- my $total = 0;
- while ( my $data = $sth->fetchrow_hashref ) {
- my $left = $data->{'tleft'};
- if ( !$left || $left eq '' ) {
- $left = $data->{'quantity'};
- }
- if ( $left && $left > 0 ) {
- my $subtotal = $left * $data->{'ecost'};
- $data->{subtotal} = $subtotal;
- $data->{'left'} = $left;
- $comtd += $subtotal;
- }
- }
-
- #warn " spent=$spent, comtd=$comtd\n";
- $sth->finish;
- return ( $spent, $comtd );
-}
-
-
-=item curconvert
-
- $foreignprice = &curconvert($currency, $localprice);
-
-Converts the price C<$localprice> to foreign currency C<$currency> by
-dividing by the exchange rate, and returns the result.
-
-If no exchange rate is found, C<&curconvert> assumes the rate is one
-to one.
-
-=cut
-
-#'
-sub curconvert {
- my ( $currency, $price ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("Select rate from currency where currency=?");
- $sth->execute($currency);
- my $cur = ( $sth->fetchrow_array() )[0];
- $sth->finish;
- if ( $cur == 0 ) {
- $cur = 1;
- }
- return ( $price / $cur );
-}
-
-=item getcurrencies
-
- ($count, $currencies) = &getcurrencies();
-
-Returns the list of all known currencies.
-
-C<$count> is the number of elements in C<$currencies>. C<$currencies>
-is a reference-to-array; its elements are references-to-hash, whose
-keys are the fields from the currency table in the Koha database.
-
-=cut
-
-#'
-sub getcurrencies {
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("Select * from currency");
- $sth->execute;
- my @results = ();
- while ( my $data = $sth->fetchrow_hashref ) {
- push( @results, $data );
- }
- $sth->finish;
- return ( scalar(@results), \@results );
-}
-
-=item updatecurrencies
-
- &updatecurrencies($currency, $newrate);
-
-Sets the exchange rate for C<$currency> to be C<$newrate>.
-
-=cut
-
-#'
-sub updatecurrencies {
- my ( $currency, $rate ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("update currency set rate=? where currency=?");
- $sth->execute( $rate, $currency );
- $sth->finish;
-}
-
-#
-#
-# OTHERS
-#
-#
-
-=item bookseller
-
- ($count, @results) = &bookseller($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<$count> is the number of elements in C<@results>. C<@results> is an
-array of references-to-hash, whose keys are the fields of of the
-aqbooksellers table in the Koha database.
-
-=cut
-
-#'
-sub bookseller {
- my ($searchstring) = @_;
- my $dbh = C4::Context->dbh;
- my $sth =
- $dbh->prepare("Select * from aqbooksellers where name like ? or id = ?");
- $sth->execute( "$searchstring%", $searchstring );
- my @results;
- while ( my $data = $sth->fetchrow_hashref ) {
- push( @results, $data );
- }
- $sth->finish;
- return ( scalar(@results), @results );
-}
-
-=item breakdown
-
- ($count, $results) = &breakdown($ordernumber);
-
-Looks up an order by order ID, and returns its breakdown.
-
-C<$count> is the number of elements in C<$results>. C<$results> is a
-reference-to-array; its elements are references-to-hash, whose keys
-are the fields of the aqorderbreakdown table in the Koha database.
-
-=cut
-
-#'
-sub breakdown {
- my ($id) = @_;
- my $dbh = C4::Context->dbh;
- my $sth =
- $dbh->prepare("Select * from aqorderbreakdown where ordernumber=?");
- $sth->execute($id);
- my @results = ();
- while ( my $data = $sth->fetchrow_hashref ) {
- push( @results, $data );
- }
- $sth->finish;
- return ( scalar(@results), \@results );
-}
-
-=item branches
-
- ($count, @results) = &branches();
-
-Returns a list of all library branches.
-
-C<$count> is the number of elements in C<@results>. C<@results> is an
-array of references-to-hash, whose keys are the fields of the branches
-table of the Koha database.
-
-=cut
-
-#'
-sub branches {
- my $dbh = C4::Context->dbh;
- my $sth;
- if ( C4::Context->preference("IndependantBranches")
- && ( C4::Context->userenv )
- && ( C4::Context->userenv->{flags} != 1 ) )
- {
- my $strsth = "Select * from branches ";
- $strsth .=
- " WHERE branchcode = "
- . $dbh->quote( C4::Context->userenv->{branch} );
- $strsth .= " order by branchname";
- warn "C4::Acquisition->branches : " . $strsth;
- $sth = $dbh->prepare($strsth);
- }
- else {
- $sth = $dbh->prepare("Select * from branches order by branchname");
- }
- my @results = ();
-
- $sth->execute();
- while ( my $data = $sth->fetchrow_hashref ) {
- push( @results, $data );
- } # while
-
- $sth->finish;
- return ( scalar(@results), @results );
-} # sub branches
-
-=item updatesup
-
- &updatesup($bookseller);
-
-Updates the information for a given bookseller. C<$bookseller> is a
-reference-to-hash whose keys are the fields of the aqbooksellers table
-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<&updatesup> with the result.
-
-=cut
-
-#'
-sub updatesup {
- my ($data) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare(
- "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=?"
- );
- $sth->execute(
- $data->{'name'}, $data->{'address1'},
- $data->{'address2'}, $data->{'address3'},
- $data->{'address4'}, $data->{'postal'},
- $data->{'phone'}, $data->{'fax'},
- $data->{'url'}, $data->{'contact'},
- $data->{'contpos'}, $data->{'contphone'},
- $data->{'contfax'}, $data->{'contaltphone'},
- $data->{'contemail'}, $data->{'contnotes'},
- $data->{'active'}, $data->{'listprice'},
- $data->{'invoiceprice'}, $data->{'gstreg'},
- $data->{'listincgst'}, $data->{'invoiceincgst'},
- $data->{'specialty'}, $data->{'discount'},
- $data->{'invoicedisc'}, $data->{'nocalc'},
- $data->{'notes'}, $data->{'id'}
- );
- $sth->finish;
-}
-
-=item insertsup
-
- $id = &insertsup($bookseller);
-
-Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
-keys are the fields of the aqbooksellers table in the Koha database.
-All fields must be present.
-
-Returns the ID of the newly-created bookseller.
-
-=cut
-
-#'
-sub insertsup {
- my ($data) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("Select max(id) from aqbooksellers");
- $sth->execute;
- my $data2 = $sth->fetchrow_hashref;
- $sth->finish;
- $data2->{'max(id)'}++;
- $sth = $dbh->prepare("Insert into aqbooksellers (id) values (?)");
- $sth->execute( $data2->{'max(id)'} );
- $sth->finish;
- $data->{'id'} = $data2->{'max(id)'};
- updatesup($data);
- return ( $data->{'id'} );
-}
-
-=item getparcels