fixup of bookfundbreakdown(), totals now add up correctly.

This commit is contained in:
sushi 2006-07-12 13:52:28 +00:00
parent d81cd891ce
commit 567c744d07

View file

@ -383,7 +383,10 @@ Also updates the book fund ID in the aqorderbreakdown table.
#' #'
sub receiveorder { sub receiveorder {
my ( $biblio, $ordnum, $quantrec, $user, $cost, $invoiceno, $freight, $rrp, $bookfund) my (
$biblio, $ordnum, $quantrec, $user, $cost,
$invoiceno, $freight, $rrp, $bookfund
)
= @_; = @_;
my $dbh = C4::Context->dbh; my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare( my $sth = $dbh->prepare(
@ -670,6 +673,7 @@ sub getparcelinformation {
return ( scalar(@results), @results ); return ( scalar(@results), @results );
} }
=item getparcelinformation =item getparcelinformation
($count, @results) = &getparcelinformation($booksellerid, $code, $date); ($count, @results) = &getparcelinformation($booksellerid, $code, $date);
@ -684,19 +688,26 @@ the aqorders, biblio, and biblioitems tables of the Koha database.
C<@results> is sorted alphabetically by book title. C<@results> is sorted alphabetically by book title.
=cut =cut
#' #'
sub getparcelinformation { sub getparcelinformation {
#gets all orders from a certain supplier, orders them alphabetically #gets all orders from a certain supplier, orders them alphabetically
my ( $supplierid, $code, $datereceived ) = @_; my ( $supplierid, $code, $datereceived ) = @_;
my $dbh = C4::Context->dbh; my $dbh = C4::Context->dbh;
my @results = (); my @results = ();
$code .='%' if $code; # add % if we search on a given code (otherwise, let him empty) $code .= '%'
my $strsth ="Select authorisedby,creationdate,aqbasket.basketno,closedate,surname,firstname,aqorders.biblionumber,aqorders.title,aqorders.ordernumber, aqorders.quantity, aqorders.quantityreceived, aqorders.unitprice, aqorders.listprice, aqorders.rrp, aqorders.ecost from aqorders,aqbasket left join borrowers on aqbasket.authorisedby=borrowers.borrowernumber where aqbasket.basketno=aqorders.basketno and aqbasket.booksellerid=? and aqorders.booksellerinvoicenumber like \"$code\" and aqorders.datereceived= \'$datereceived\'"; if $code; # add % if we search on a given code (otherwise, let him empty)
my $strsth =
"Select authorisedby,creationdate,aqbasket.basketno,closedate,surname,firstname,aqorders.biblionumber,aqorders.title,aqorders.ordernumber, aqorders.quantity, aqorders.quantityreceived, aqorders.unitprice, aqorders.listprice, aqorders.rrp, aqorders.ecost from aqorders,aqbasket left join borrowers on aqbasket.authorisedby=borrowers.borrowernumber where aqbasket.basketno=aqorders.basketno and aqbasket.booksellerid=? and aqorders.booksellerinvoicenumber like \"$code\" and aqorders.datereceived= \'$datereceived\'";
if ( C4::Context->preference("IndependantBranches") ) { if ( C4::Context->preference("IndependantBranches") ) {
my $userenv = C4::Context->userenv; my $userenv = C4::Context->userenv;
if ( ($userenv) && ( $userenv->{flags} != 1 ) ) { if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
$strsth .= " and (borrowers.branchcode = '".$userenv->{branch}."' or borrowers.branchcode ='')"; $strsth .=
" and (borrowers.branchcode = '"
. $userenv->{branch}
. "' or borrowers.branchcode ='')";
} }
} }
$strsth .= " order by aqbasket.basketno"; $strsth .= " order by aqbasket.basketno";
@ -712,6 +723,7 @@ sub getparcelinformation {
return ( scalar(@results), @results ); return ( scalar(@results), @results );
} }
=item getsupplierlistwithlateorders =item getsupplierlistwithlateorders
%results = &getsupplierlistwithlateorders; %results = &getsupplierlistwithlateorders;
@ -1129,49 +1141,82 @@ sub bookfunds {
sub bookfundbreakdown { sub bookfundbreakdown {
my ( $id, $year, $start, $end ) = @_; my ( $id, $year, $start, $end ) = @_;
my $dbh = C4::Context->dbh; 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( my $sth = $dbh->prepare(
"SELECT quantity,datereceived,freight,unitprice,listprice,ecost, "Select quantity,datereceived,freight,unitprice,listprice,ecost,
quantityreceived,subscription quantityreceived,subscription
FROM aqorders,aqorderbreakdown WHERE bookfundid=? AND from aqorders left join aqorderbreakdown on
aqorders.ordernumber=aqorderbreakdown.ordernumber aqorders.ordernumber=aqorderbreakdown.ordernumber
AND (datecancellationprinted is NULL OR where bookfundid=? and (datecancellationprinted is NULL or
datecancellationprinted='0000-00-00')" datecancellationprinted='0000-00-00') and
); ((datereceived >= ? and datereceived < ?) or
if ($start) { (budgetdate >= ? and budgetdate < ?))"
$sth = $dbh->prepare(
"SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
quantityreceived,subscription
FROM aqorders,aqorderbreakdown
WHERE bookfundid=? AND
aqorders.ordernumber=aqorderbreakdown.ordernumber
AND (datecancellationprinted is NULL OR
datecancellationprinted='0000-00-00')
AND ((datereceived >= ? AND datereceived < ?) OR
(budgetdate >= ? AND budgetdate < ?))"
); );
$sth->execute( $id, $start, $end, $start, $end ); $sth->execute( $id, $start, $end, $start, $end );
}
else {
$sth->execute($id);
}
my $comtd = 0;
my $spent = 0; my $spent = 0;
while ( my $data = $sth->fetchrow_hashref ) { while ( my $data = $sth->fetchrow_hashref ) {
if ( $data->{'subscription'} == 1 ) { if ( $data->{'subscription'} == 1 ) {
$spent += $data->{'quantity'} * $data->{'unitprice'}; $spent += $data->{'quantity'} * $data->{'unitprice'};
} }
else { else {
my $leftover = $data->{'quantity'} - $data->{'quantityreceived'}; my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
$comtd += ( $data->{'ecost'} ) * $leftover;
$spent += ( $data->{'unitprice'} ) * $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; $sth->finish;
return ( $spent, $comtd ); return ( $spent, $comtd );
} }
=item curconvert =item curconvert
$foreignprice = &curconvert($currency, $localprice); $foreignprice = &curconvert($currency, $localprice);
@ -1370,16 +1415,24 @@ sub updatesup {
listprice=?, invoiceprice=?,gstreg=?, listincgst=?, listprice=?, invoiceprice=?,gstreg=?, listincgst=?,
invoiceincgst=?, specialty=?,discount=?,invoicedisc=?, invoiceincgst=?, specialty=?,discount=?,invoicedisc=?,
nocalc=?, notes=? nocalc=?, notes=?
where id=?"); where id=?"
$sth->execute($data->{'name'},$data->{'address1'},$data->{'address2'}, );
$data->{'address3'},$data->{'address4'},$data->{'postal'},$data->{'phone'}, $sth->execute(
$data->{'fax'},$data->{'url'},$data->{'contact'},$data->{'contpos'}, $data->{'name'}, $data->{'address1'},
$data->{'contphone'},$data->{'contfax'},$data->{'contaltphone'}, $data->{'address2'}, $data->{'address3'},
$data->{'contemail'}, $data->{'address4'}, $data->{'postal'},
$data->{'contnotes'},$data->{'active'},$data->{'listprice'}, $data->{'phone'}, $data->{'fax'},
$data->{'invoiceprice'},$data->{'gstreg'},$data->{'listincgst'}, $data->{'url'}, $data->{'contact'},
$data->{'invoiceincgst'},$data->{'specialty'},$data->{'discount'}, $data->{'contpos'}, $data->{'contphone'},
$data->{'invoicedisc'},$data->{'nocalc'},$data->{'notes'},$data->{'id'}); $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; $sth->finish;
} }
@ -1466,13 +1519,17 @@ Returns the count of parcels returned and a pointer on a hash list containing pa
=cut =cut
#' #'
sub getparcels { sub getparcels {
my ( $bookseller, $order, $code, $datefrom, $dateto, $limit ) = @_; my ( $bookseller, $order, $code, $datefrom, $dateto, $limit ) = @_;
my $dbh = C4::Context->dbh; my $dbh = C4::Context->dbh;
my $strsth = "SELECT aqorders.booksellerinvoicenumber, datereceived, count(DISTINCT biblionumber) as biblio, sum(quantity) as itemsexpected, sum(quantityreceived) as itemsreceived from aqorders, aqbasket where aqbasket.basketno = aqorders.basketno and aqbasket.booksellerid = $bookseller and datereceived is not null "; my $strsth =
$strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" " if ($code); "SELECT aqorders.booksellerinvoicenumber, datereceived, count(DISTINCT biblionumber) as biblio, sum(quantity) as itemsexpected, sum(quantityreceived) as itemsreceived from aqorders, aqbasket where aqbasket.basketno = aqorders.basketno and aqbasket.booksellerid = $bookseller and datereceived is not null ";
$strsth .= "and datereceived >=".$dbh->quote($datefrom)." " if ($datefrom); $strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" "
if ($code);
$strsth .= "and datereceived >=" . $dbh->quote($datefrom) . " "
if ($datefrom);
$strsth .= "and datereceived <=" . $dbh->quote($dateto) . " " if ($dateto); $strsth .= "and datereceived <=" . $dbh->quote($dateto) . " " if ($dateto);
$strsth .= "group by aqorders.booksellerinvoicenumber,datereceived "; $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
$strsth .= "order by $order " if ($order); $strsth .= "order by $order " if ($order);
@ -1481,6 +1538,7 @@ sub getparcels {
### getparcels: $strsth ### getparcels: $strsth
$sth->execute; $sth->execute;
my @results; my @results;
while ( my $data2 = $sth->fetchrow_hashref ) { while ( my $data2 = $sth->fetchrow_hashref ) {
push @results, $data2; push @results, $data2;
} }