1 package C4::Acquisition;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
29 use vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
32 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
34 # used in reciveorder subroutine
35 # to provide library specific handling
36 my $library_name = C4::Context->preference("LibraryName");
40 C4::Acquisition - Koha functions for dealing with orders and acquisitions
48 The functions in this module deal with acquisitions, managing book
49 orders, converting money to different currencies, and so forth.
59 &getbasket &getbasketcontent &newbasket &closebasket
61 &getorders &getallorders &getrecorders
62 &getorder &neworder &delorder
63 &ordersearch &histsearch
64 &modorder &getsingleorder &invoice &receiveorder
65 &updaterecorder &newordernum
66 &getsupplierlistwithlateorders
68 &getparcels &getparcelinformation
69 &bookfunds &curconvert &getcurrencies &bookfundbreakdown
70 &updatecurrencies &getcurrency
72 &bookseller &breakdown
85 $aqbasket = &getbasket($basketnumber);
87 get all basket informations in aqbasket for a given basket
92 my $dbh = C4::Context->dbh;
95 "select aqbasket.*,borrowers.firstname+' '+borrowers.surname as authorisedbyname, borrowers.branchcode as branch from aqbasket left join borrowers on aqbasket.authorisedby=borrowers.borrowernumber where basketno=?"
97 $sth->execute($basketno);
98 return ( $sth->fetchrow_hashref );
102 =item getbasketcontent
104 ($count, @orders) = &getbasketcontent($basketnumber, $booksellerID);
106 Looks up the pending (non-cancelled) orders with the given basket
107 number. If C<$booksellerID> is non-empty, only orders from that seller
110 C<&basket> returns a two-element array. C<@orders> is an array of
111 references-to-hash, whose keys are the fields from the aqorders,
112 biblio, and biblioitems tables in the Koha database. C<$count> is the
113 number of elements in C<@orders>.
118 sub getbasketcontent {
119 my ( $basketno, $supplier, $orderby ) = @_;
120 my $dbh = C4::Context->dbh;
122 "SELECT aqorderbreakdown.*,biblio.*,biblioitems.*,aqorders.*,biblio.title FROM aqorders,biblio,biblioitems
123 LEFT JOIN aqorderbreakdown ON aqorderbreakdown.ordernumber=aqorders.ordernumber
125 AND biblio.biblionumber=aqorders.biblionumber AND biblioitems.biblioitemnumber
126 =aqorders.biblioitemnumber
127 AND (datecancellationprinted IS NULL OR datecancellationprinted =
129 if ( $supplier ne '' ) {
130 $query .= " AND aqorders.booksellerid=?";
133 $orderby = "biblioitems.publishercode" unless $orderby;
134 $query .= " ORDER BY $orderby";
135 my $sth = $dbh->prepare($query);
136 if ( $supplier ne '' ) {
137 $sth->execute( $basketno, $supplier );
140 $sth->execute($basketno);
146 while ( my $data = $sth->fetchrow_hashref ) {
147 $results[$i] = $data;
151 return ( $i, @results );
156 $basket = &newbasket();
158 Create a new basket in aqbasket table
162 my ( $booksellerid, $authorisedby ) = @_;
163 my $dbh = C4::Context->dbh;
166 "insert into aqbasket (creationdate,booksellerid,authorisedby) values(now(),'$booksellerid','$authorisedby')"
169 #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
170 my $basket = $dbh->{'mysql_insertid'};
176 &newbasket($basketno);
178 close a basket (becomes unmodifiable,except for recieves
183 my $dbh = C4::Context->dbh;
185 $dbh->prepare("update aqbasket set closedate=now() where basketno=?");
186 $sth->execute($basketno);
191 &neworder($basket, $biblionumber, $title, $quantity, $listprice,
192 $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
193 $ecost, $gst, $budget, $unitprice, $subscription,
194 $booksellerinvoicenumber);
196 Adds a new order to the database. Any argument that isn't described
197 below is the new value of the field with the same name in the aqorders
198 table of the Koha database.
200 C<$ordnum> is a "minimum order number." After adding the new entry to
201 the aqorders table, C<&neworder> finds the first entry in aqorders
202 with order number greater than or equal to C<$ordnum>, and adds an
203 entry to the aqorderbreakdown table, with the order number just found,
204 and the book fund ID of the newly-added order.
206 C<$budget> is effectively ignored.
208 C<$subscription> may be either "yes", or anything else for "no".
215 $basketno, $bibnum, $title, $quantity,
216 $listprice, $booksellerid, $authorisedby, $notes,
217 $bookfund, $bibitemnum, $rrp, $ecost,
218 $gst, $budget, $cost, $sub,
219 $invoice, $sort1, $sort2
224 if ( !$budget || $budget eq 'now' ) {
225 $sth = $dbh->prepare(
226 "INSERT INTO aqorders
227 (biblionumber,title,basketno,quantity,listprice,notes,
228 biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate)
229 VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,now(),now() )"
232 $bibnum, $title, $basketno, $quantity, $listprice,
233 $notes, $bibitemnum, $rrp, $ecost, $gst,
234 $cost, $sub, $sort1, $sort2
239 ##FIXME HARDCODED DATE.
240 $budget = "'2006-07-01'";
241 $sth = $dbh->prepare(
242 "INSERT INTO aqorders
243 (biblionumber,title,basketno,quantity,listprice,notes,
244 biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate)
245 VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,now() )"
248 $bibnum, $title, $basketno, $quantity, $listprice,
249 $notes, $bibitemnum, $rrp, $ecost, $gst,
250 $cost, $sub, $sort1, $sort2, $budget
256 #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
257 my $ordnum = $dbh->{'mysql_insertid'};
258 $sth = $dbh->prepare(
259 "INSERT INTO aqorderbreakdown (ordernumber,bookfundid) VALUES
262 $sth->execute( $ordnum, $bookfund );
269 &delorder($biblionumber, $ordernumber);
271 Cancel the order with the given order and biblio numbers. It does not
272 delete any entries in the aqorders table, it merely marks them as
279 my ( $bibnum, $ordnum ) = @_;
280 my $dbh = C4::Context->dbh;
281 my $sth = $dbh->prepare(
282 "update aqorders set datecancellationprinted=now()
283 where biblionumber=? and ordernumber=?"
285 $sth->execute( $bibnum, $ordnum );
291 &modorder($title, $ordernumber, $quantity, $listprice,
292 $biblionumber, $basketno, $supplier, $who, $notes,
293 $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
294 $unitprice, $booksellerinvoicenumber);
296 Modifies an existing order. Updates the order with order number
297 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
298 update the fields with the same name in the aqorders table of the Koha
301 Entries with order number C<$ordernumber> in the aqorderbreakdown
302 table are also updated to the new book fund ID.
309 $title, $ordnum, $quantity, $listprice, $bibnum,
310 $basketno, $supplier, $who, $notes, $bookfund,
311 $bibitemnum, $rrp, $ecost, $gst, $budget,
312 $cost, $invoice, $sort1, $sort2
315 my $dbh = C4::Context->dbh;
316 my $sth = $dbh->prepare(
317 "update aqorders set title=?,
318 quantity=?,listprice=?,basketno=?,
319 rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
320 notes=?,sort1=?, sort2=?
322 ordernumber=? and biblionumber=?"
325 $title, $quantity, $listprice, $basketno, $rrp,
326 $ecost, $cost, $invoice, $notes, $sort1,
327 $sort2, $ordnum, $bibnum
330 $sth = $dbh->prepare(
331 "update aqorderbreakdown set bookfundid=? where
335 unless ( $sth->execute( $bookfund, $ordnum ) )
336 { # zero rows affected [Bug 734]
338 "insert into aqorderbreakdown (ordernumber,bookfundid) values (?,?)";
339 $sth = $dbh->prepare($query);
340 $sth->execute( $ordnum, $bookfund );
347 $order = &newordernum();
349 Finds the next unused order number in the aqorders table of the Koha
350 database, and returns it.
355 # FIXME - Race condition
357 my $dbh = C4::Context->dbh;
358 my $sth = $dbh->prepare("Select max(ordernumber) from aqorders");
360 my $data = $sth->fetchrow_arrayref;
361 my $ordnum = $$data[0];
369 &receiveorder($biblionumber, $ordernumber, $quantityreceived, $user,
370 $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
371 $freight, $bookfund, $rrp);
373 Updates an order, to reflect the fact that it was received, at least
374 in part. All arguments not mentioned below update the fields with the
375 same name in the aqorders table of the Koha database.
377 Updates the order with bibilionumber C<$biblionumber> and ordernumber
380 Also updates the book fund ID in the aqorderbreakdown table.
386 my ( $biblio, $ordnum, $quantrec, $user, $cost, $invoiceno, $freight, $rrp, $bookfund)
388 my $dbh = C4::Context->dbh;
389 my $sth = $dbh->prepare(
390 "update aqorders set quantityreceived=?,datereceived=now(),booksellerinvoicenumber=?,
391 unitprice=?,freight=?,rrp=?
392 where biblionumber=? and ordernumber=?"
394 my $suggestionid = findsuggestion_from_biblionumber( $dbh, $biblio );
396 changestatus( $suggestionid, 'AVAILABLE', '', $biblio );
398 $sth->execute( $quantrec, $invoiceno, $cost, $freight, $rrp, $biblio,
402 # Allows libraries to change their bookfund during receiving orders
403 # allows them to adjust budgets
404 if ( C4::Context->preferene("LooseBudgets") ) {
405 my $sth = $dbh->prepare(
406 "UPDATE aqorderbreakdown SET bookfundid=?
409 $sth->execute( $bookfund, $ordnum );
416 &updaterecorder($biblionumber, $ordernumber, $user, $unitprice,
419 Updates the order with biblionumber C<$biblionumber> and order number
420 C<$ordernumber>. C<$bookfundid> is the new value for the book fund ID
421 in the aqorderbreakdown table of the Koha database. All other
422 arguments update the fields with the same name in the aqorders table.
430 my ( $biblio, $ordnum, $user, $cost, $bookfund, $rrp ) = @_;
431 my $dbh = C4::Context->dbh;
432 my $sth = $dbh->prepare(
435 where biblionumber=? and ordernumber=?
438 $sth->execute( $cost, $rrp, $biblio, $ordnum );
442 "update aqorderbreakdown set bookfundid=? where ordernumber=?");
443 $sth->execute( $bookfund, $ordnum );
455 ($count, $orders) = &getorders($booksellerid);
457 Finds pending orders from the bookseller with the given ID. Ignores
458 completed and cancelled orders.
460 C<$count> is the number of elements in C<@{$orders}>.
462 C<$orders> is a reference-to-array; each element is a
463 reference-to-hash with the following fields:
469 Gives the number of orders in with this basket number.
471 =item C<authorizedby>
477 These give the value of the corresponding field in the aqorders table
478 of the Koha database.
482 Results are ordered from most to least recent.
488 my ($supplierid) = @_;
489 my $dbh = C4::Context->dbh;
490 my $strsth = "Select count(*),authorisedby,creationdate,aqbasket.basketno,
491 closedate,surname,firstname,aqorders.title
493 left join aqbasket on aqbasket.basketno=aqorders.basketno
494 left join borrowers on aqbasket.authorisedby=borrowers.borrowernumber
495 where booksellerid=? and (quantity > quantityreceived or
496 quantityreceived is NULL) and datecancellationprinted is NULL and (to_days(now())-to_days(closedate) < 180 or closedate is null)";
497 if ( C4::Context->preference("IndependantBranches") ) {
498 my $userenv = C4::Context->userenv;
499 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
501 " and (borrowers.branchcode = '"
503 . "' or borrowers.branchcode ='')";
506 $strsth .= " group by basketno order by aqbasket.basketno";
507 my $sth = $dbh->prepare($strsth);
508 $sth->execute($supplierid);
510 while ( my $data = $sth->fetchrow_hashref ) {
511 push( @results, $data );
514 return ( scalar(@results), \@results );
519 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
521 Looks up the order with the given biblionumber and biblioitemnumber.
523 Returns a two-element array. C<$ordernumber> is the order number.
524 C<$order> is a reference-to-hash describing the order; its keys are
525 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
526 tables of the Koha database.
531 my ( $bi, $bib ) = @_;
532 my $dbh = C4::Context->dbh;
535 "Select ordernumber from aqorders where biblionumber=? and biblioitemnumber=?"
537 $sth->execute( $bib, $bi );
539 # FIXME - Use fetchrow_array(), since we're only interested in the one
541 my $ordnum = $sth->fetchrow_hashref;
543 my $order = getsingleorder( $ordnum->{'ordernumber'} );
544 return ( $order, $ordnum->{'ordernumber'} );
549 $order = &getsingleorder($ordernumber);
551 Looks up an order by order number.
553 Returns a reference-to-hash describing the order. The keys of
554 C<$order> are fields from the biblio, biblioitems, aqorders, and
555 aqorderbreakdown tables of the Koha database.
561 my $dbh = C4::Context->dbh;
562 my $sth = $dbh->prepare(
563 "Select * from biblio,biblioitems,aqorders left join aqorderbreakdown
564 on aqorders.ordernumber=aqorderbreakdown.ordernumber
565 where aqorders.ordernumber=?
566 and biblio.biblionumber=aqorders.biblionumber and
567 biblioitems.biblioitemnumber=aqorders.biblioitemnumber"
569 $sth->execute($ordnum);
570 my $data = $sth->fetchrow_hashref;
577 ($count, @results) = &getallorders($booksellerid);
579 Looks up all of the pending orders from the supplier with the given
580 bookseller ID. Ignores cancelled and completed orders.
582 C<$count> is the number of elements in C<@results>. C<@results> is an
583 array of references-to-hash. The keys of each element are fields from
584 the aqorders, biblio, and biblioitems tables of the Koha database.
586 C<@results> is sorted alphabetically by book title.
593 #gets all orders from a certain supplier, orders them alphabetically
594 my ($supplierid) = @_;
595 my $dbh = C4::Context->dbh;
597 my $strsth = "Select count(*),authorisedby,creationdate,aqbasket.basketno,
598 closedate,surname,firstname,aqorders.biblionumber,aqorders.title, aqorders.ordernumber
600 left join aqbasket on aqbasket.basketno=aqorders.basketno
601 left join borrowers on aqbasket.authorisedby=borrowers.borrowernumber
602 where booksellerid=? and (quantity > quantityreceived or
603 quantityreceived is NULL) and datecancellationprinted is NULL ";
605 if ( C4::Context->preference("IndependantBranches") ) {
606 my $userenv = C4::Context->userenv;
607 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
609 " and (borrowers.branchcode = '"
611 . "' or borrowers.branchcode ='')";
614 $strsth .= " group by basketno order by aqbasket.basketno";
615 my $sth = $dbh->prepare($strsth);
616 $sth->execute($supplierid);
617 while ( my $data = $sth->fetchrow_hashref ) {
618 push( @results, $data );
621 return ( scalar(@results), @results );
624 =item getparcelinformation
626 ($count, @results) = &getparcelinformation($booksellerid, $code, $date);
628 Looks up all of the received items from the supplier with the given
629 bookseller ID at the given date, for the given code. Ignores cancelled and completed orders.
631 C<$count> is the number of elements in C<@results>. C<@results> is an
632 array of references-to-hash. The keys of each element are fields from
633 the aqorders, biblio, and biblioitems tables of the Koha database.
635 C<@results> is sorted alphabetically by book title.
640 sub getparcelinformation {
642 #gets all orders from a certain supplier, orders them alphabetically
643 my ( $supplierid, $code, $datereceived ) = @_;
644 my $dbh = C4::Context->dbh;
647 if $code; # add % if we search on a given code (otherwise, let him empty)
649 "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\'";
651 if ( C4::Context->preference("IndependantBranches") ) {
652 my $userenv = C4::Context->userenv;
653 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
655 " and (borrowers.branchcode = '"
657 . "' or borrowers.branchcode ='')";
660 $strsth .= " order by aqbasket.basketno";
661 ### parcelinformation : $strsth
662 my $sth = $dbh->prepare($strsth);
663 $sth->execute($supplierid);
664 while ( my $data = $sth->fetchrow_hashref ) {
665 push( @results, $data );
667 my $count = scalar(@results);
668 ### countparcelbiblio: $count
671 return ( scalar(@results), @results );
674 =item getsupplierlistwithlateorders
676 %results = &getsupplierlistwithlateorders;
678 Searches for suppliers with late orders.
683 sub getsupplierlistwithlateorders {
685 my $dbh = C4::Context->dbh;
687 #FIXME NOT quite sure that this operation is valid for DBMs different from Mysql, HOPING so
688 #should be tested with other DBMs
691 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
692 if ( $dbdriver eq "mysql" ) {
693 $strsth = "SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
694 FROM aqorders, aqbasket
695 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
696 WHERE aqorders.basketno = aqbasket.basketno AND
697 (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY) AND (datereceived = '' or datereceived is null))
701 $strsth = "SELECT DISTINCT aqbasket.booksellerid, aqbooksellers.name
702 FROM aqorders, aqbasket
703 LEFT JOIN aqbooksellers ON aqbasket.aqbooksellerid = aqbooksellers.id
704 WHERE aqorders.basketno = aqbasket.basketno AND
705 (closedate < (CURDATE( )-(INTERVAL $delay DAY))) AND (datereceived = '' or datereceived is null))
709 # warn "C4::Acquisition getsupplierlistwithlateorders : ".$strsth;
710 my $sth = $dbh->prepare($strsth);
713 while ( my ( $id, $name ) = $sth->fetchrow ) {
714 $supplierlist{$id} = $name;
716 return %supplierlist;
721 %results = &getlateorders;
723 Searches for suppliers with late orders.
730 my $supplierid = shift;
733 my $dbh = C4::Context->dbh;
735 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
737 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
740 if ( $dbdriver eq "mysql" ) {
741 $strsth = "SELECT aqbasket.basketno,
742 DATE(aqbasket.closedate) as orderdate, aqorders.quantity - IFNULL(aqorders.quantityreceived,0) as quantity, aqorders.rrp as unitpricesupplier,aqorders.ecost as unitpricelib,
743 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp as subtotal, aqbookfund.bookfundname as budget, borrowers.branchcode as branch,
744 aqbooksellers.name as supplier,
745 aqorders.title, biblio.author, biblioitems.publishercode as publisher, biblioitems.publicationyear,
746 DATEDIFF(CURDATE( ),closedate) AS latesince
749 (aqorders LEFT JOIN biblio on biblio.biblionumber = aqorders.biblionumber) LEFT JOIN biblioitems on biblioitems.biblionumber=biblio.biblionumber
750 ) LEFT JOIN aqorderbreakdown on aqorders.ordernumber = aqorderbreakdown.ordernumber
751 ) LEFT JOIN aqbookfund on aqorderbreakdown.bookfundid = aqbookfund.bookfundid
752 ),(aqbasket LEFT JOIN borrowers on aqbasket.authorisedby = borrowers.borrowernumber) LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
753 WHERE aqorders.basketno = aqbasket.basketno AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY))
754 AND ((datereceived = '' OR datereceived is null) OR (aqorders.quantityreceived < aqorders.quantity) ) ";
755 $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
756 $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'"
759 " AND borrowers.branchcode like \'"
760 . C4::Context->userenv->{branch} . "\'"
761 if ( C4::Context->preference("IndependantBranches")
762 && C4::Context->userenv
763 && C4::Context->userenv->{flags} != 1 );
765 " HAVING quantity<>0 AND unitpricesupplier<>0 AND unitpricelib<>0 ORDER BY latesince,basketno,borrowers.branchcode, supplier ";
768 $strsth = "SELECT aqbasket.basketno,
769 DATE(aqbasket.closedate) as orderdate,
770 aqorders.quantity, aqorders.rrp as unitpricesupplier,aqorders.ecost as unitpricelib, aqorders.quantity * aqorders.rrp as subtotal
771 aqbookfund.bookfundname as budget, borrowers.branchcode as branch,
772 aqbooksellers.name as supplier,
773 biblio.title, biblio.author, biblioitems.publishercode as publisher, biblioitems.publicationyear,
774 (CURDATE - closedate) AS latesince
777 (aqorders LEFT JOIN biblio on biblio.biblionumber = aqorders.biblionumber) LEFT JOIN biblioitems on biblioitems.biblionumber=biblio.biblionumber
778 ) LEFT JOIN aqorderbreakdown on aqorders.ordernumber = aqorderbreakdown.ordernumber
779 ) LEFT JOIN aqbookfund on aqorderbreakdown.bookfundid = aqbookfund.bookfundid
780 ),(aqbasket LEFT JOIN borrowers on aqbasket.authorisedby = borrowers.borrowernumber) LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
781 WHERE aqorders.basketno = aqbasket.basketno AND (closedate < (CURDATE -(INTERVAL $delay DAY))
782 AND ((datereceived = '' OR datereceived is null) OR (aqorders.quantityreceived < aqorders.quantity) ) ";
783 $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
784 $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'"
787 " AND borrowers.branchcode like \'"
788 . C4::Context->userenv->{branch} . "\'"
789 if ( C4::Context->preference("IndependantBranches")
790 && C4::Context->userenv->{flags} != 1 );
792 " ORDER BY latesince,basketno,borrowers.branchcode, supplier";
794 warn "C4::Acquisition : getlateorders SQL:" . $strsth;
795 my $sth = $dbh->prepare($strsth);
799 while ( my $data = $sth->fetchrow_hashref ) {
800 $data->{hilighted} = $hilighted if ( $hilighted > 0 );
801 $data->{orderdate} = format_date( $data->{orderdate} );
802 push @results, $data;
803 $hilighted = -$hilighted;
806 return ( scalar(@results), @results );
812 #gets all orders from a certain supplier, orders them alphabetically
814 my $dbh = C4::Context->dbh;
816 my $sth = $dbh->prepare(
817 "Select * from aqorders,biblio,biblioitems where booksellerid=?
818 and (cancelledby is NULL or cancelledby = '')
819 and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
820 aqorders.biblioitemnumber and
821 aqorders.quantityreceived>0
822 and aqorders.datereceived >=now()
823 group by aqorders.biblioitemnumber
827 $sth->execute($supid);
828 while ( my $data = $sth->fetchrow_hashref ) {
829 push( @results, $data );
832 return ( scalar(@results), @results );
837 ($count, @results) = &ordersearch($search, $biblionumber, $complete);
841 C<$search> may take one of several forms: if it is an ISBN,
842 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
843 order number, C<&ordersearch> returns orders with that order number
844 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
845 to be a space-separated list of search terms; in this case, all of the
846 terms must appear in the title (matching the beginning of title
849 If C<$complete> is C<yes>, the results will include only completed
850 orders. In any case, C<&ordersearch> ignores cancelled orders.
852 C<&ordersearch> returns an array. C<$count> is the number of elements
853 in C<@results>. C<@results> is an array of references-to-hash with the
872 my ( $search, $id, $biblio, $catview ) = @_;
873 my $dbh = C4::Context->dbh;
874 my @data = split( ' ', $search );
877 @searchterms = ($id);
879 map { push( @searchterms, "$_%", "% $_%" ) } @data;
880 push( @searchterms, $search, $search, $biblio );
884 "SELECT *,biblio.title FROM aqorders,biblioitems,biblio,aqbasket
885 WHERE aqorders.biblioitemnumber = biblioitems.biblioitemnumber AND
886 aqorders.basketno = aqbasket.basketno
887 AND aqbasket.booksellerid = ?
888 AND biblio.biblionumber=aqorders.biblionumber
889 AND ((datecancellationprinted is NULL)
890 OR (datecancellationprinted = '0000-00-00'))
894 map { "(biblio.title like ? or biblio.title like ?)" } @data )
896 . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
901 "SELECT *,biblio.title FROM aqorders,biblioitems,biblio,aqbasket
902 WHERE aqorders.biblioitemnumber = biblioitems.biblioitemnumber AND
903 aqorders.basketno = aqbasket.basketno
904 AND biblio.biblionumber=aqorders.biblionumber
905 AND ((datecancellationprinted is NULL)
906 OR (datecancellationprinted = '0000-00-00'))
907 AND (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
911 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
913 . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
915 $query .= " GROUP BY aqorders.ordernumber";
916 my $sth = $dbh->prepare($query);
917 $sth->execute(@searchterms);
919 my $sth2 = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
921 $dbh->prepare("SELECT * FROM aqorderbreakdown WHERE ordernumber=?");
922 while ( my $data = $sth->fetchrow_hashref ) {
923 $sth2->execute( $data->{'biblionumber'} );
924 my $data2 = $sth2->fetchrow_hashref;
925 $data->{'author'} = $data2->{'author'};
926 $data->{'seriestitle'} = $data2->{'seriestitle'};
927 $sth3->execute( $data->{'ordernumber'} );
928 my $data3 = $sth3->fetchrow_hashref;
929 $data->{'branchcode'} = $data3->{'branchcode'};
930 $data->{'bookfundid'} = $data3->{'bookfundid'};
931 push( @results, $data );
936 return ( scalar(@results), @results );
940 my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
943 my $total_qtyreceived = 0;
946 # don't run the query if there are no parameters (list would be too long for sure !
947 if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
948 my $dbh = C4::Context->dbh;
950 "select biblio.title,biblio.author,aqorders.basketno,name,aqbasket.creationdate,aqorders.datereceived, aqorders.quantity, aqorders.quantityreceived, aqorders.ecost from aqorders,aqbasket,aqbooksellers,biblio";
951 $query .= ",borrowers "
952 if ( C4::Context->preference("IndependantBranches") );
954 " where aqorders.basketno=aqbasket.basketno and aqbasket.booksellerid=aqbooksellers.id and biblio.biblionumber=aqorders.biblionumber ";
955 $query .= " and aqbasket.authorisedby=borrowers.borrowernumber"
956 if ( C4::Context->preference("IndependantBranches") );
957 $query .= " and biblio.title like " . $dbh->quote( "%" . $title . "%" )
960 " and biblio.author like " . $dbh->quote( "%" . $author . "%" )
962 $query .= " and name like " . $dbh->quote( "%" . $name . "%" ) if $name;
963 $query .= " and creationdate >" . $dbh->quote($from_placed_on)
965 $query .= " and creationdate<" . $dbh->quote($to_placed_on)
968 if ( C4::Context->preference("IndependantBranches") ) {
969 my $userenv = C4::Context->userenv;
970 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
972 " and (borrowers.branchcode = '"
974 . "' or borrowers.branchcode ='')";
977 $query .= " order by booksellerid";
978 warn "query histearch: " . $query;
979 my $sth = $dbh->prepare($query);
982 while ( my $line = $sth->fetchrow_hashref ) {
983 $line->{count} = $cnt++;
984 $line->{toggle} = 1 if $cnt % 2;
985 push @order_loop, $line;
986 $line->{creationdate} = format_date( $line->{creationdate} );
987 $line->{datereceived} = format_date( $line->{datereceived} );
988 $total_qty += $line->{'quantity'};
989 $total_qtyreceived += $line->{'quantityreceived'};
990 $total_price += $line->{'quantity'} * $line->{'ecost'};
993 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
1004 ($count, @results) = &invoice($booksellerinvoicenumber);
1006 Looks up orders by invoice number.
1008 Returns an array. C<$count> is the number of elements in C<@results>.
1009 C<@results> is an array of references-to-hash; the keys of each
1010 elements are fields from the aqorders, biblio, and biblioitems tables
1011 of the Koha database.
1018 my $dbh = C4::Context->dbh;
1020 my $sth = $dbh->prepare(
1021 "Select * from aqorders,biblio,biblioitems where
1022 booksellerinvoicenumber=?
1023 and biblio.biblionumber=aqorders.biblionumber and biblioitems.biblioitemnumber=
1024 aqorders.biblioitemnumber group by aqorders.ordernumber,aqorders.biblioitemnumber"
1026 $sth->execute($invoice);
1027 while ( my $data = $sth->fetchrow_hashref ) {
1028 push( @results, $data );
1031 return ( scalar(@results), @results );
1036 ($count, @results) = &bookfunds();
1038 Returns a list of all book funds.
1040 C<$count> is the number of elements in C<@results>. C<@results> is an
1041 array of references-to-hash, whose keys are fields from the aqbookfund
1042 and aqbudget tables of the Koha database. Results are ordered
1043 alphabetically by book fund name.
1050 my $dbh = C4::Context->dbh;
1051 my $userenv = C4::Context->userenv;
1052 my $branch = $userenv->{branch};
1055 if ( $branch ne '' ) {
1056 $strsth = "SELECT * FROM aqbookfund,aqbudget WHERE aqbookfund.bookfundid
1057 =aqbudget.bookfundid AND startdate<now() AND enddate>now() AND (aqbookfund.branchcode is null or aqbookfund.branchcode='' or aqbookfund.branchcode= ? )
1058 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
1061 $strsth = "SELECT * FROM aqbookfund,aqbudget WHERE aqbookfund.bookfundid
1062 =aqbudget.bookfundid AND startdate<now() AND enddate>now()
1063 GROUP BY aqbookfund.bookfundid ORDER BY bookfundname";
1065 my $sth = $dbh->prepare($strsth);
1066 if ( $branch ne '' ) {
1067 $sth->execute($branch);
1073 while ( my $data = $sth->fetchrow_hashref ) {
1074 push( @results, $data );
1077 return ( scalar(@results), @results );
1080 =item bookfundbreakdown
1082 returns the total comtd & spent for a given bookfund, and a given year
1083 used in acqui-home.pl
1088 sub bookfundbreakdown {
1089 my ( $id, $year ,$start, $end) = @_;
1090 my $dbh = C4::Context->dbh;
1091 my $sth = $dbh->prepare(
1092 "SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
1093 quantityreceived,subscription
1094 FROM aqorders,aqorderbreakdown WHERE bookfundid=? AND
1095 aqorders.ordernumber=aqorderbreakdown.ordernumber
1096 AND (datecancellationprinted is NULL OR
1097 datecancellationprinted='0000-00-00')"
1100 $sth = $dbh->prepare(
1101 "SELECT quantity,datereceived,freight,unitprice,listprice,ecost,
1102 quantityreceived,subscription
1103 FROM aqorders,aqorderbreakdown
1104 WHERE bookfundid=? AND
1105 aqorders.ordernumber=aqorderbreakdown.ordernumber
1106 AND (datecancellationprinted is NULL OR
1107 datecancellationprinted='0000-00-00')
1108 AND ((datereceived >= ? AND datereceived < ?) OR
1109 (budgetdate >= ? AND budgetdate < ?))"
1111 $sth->execute( $id, $start, $end, $start, $end );
1119 while ( my $data = $sth->fetchrow_hashref ) {
1121 if ( $data->{'subscription'} == 1 ) {
1122 $spent += $data->{'quantity'} * $data->{'unitprice'};
1125 my $leftover = $data->{'quantity'} - $data->{'quantityreceived'};
1126 $comtd += ( $data->{'ecost'} ) * $leftover;
1127 $spent += ( $data->{'unitprice'} ) * $data->{'quantityreceived'};
1131 return ( $spent, $comtd );
1136 $foreignprice = &curconvert($currency, $localprice);
1138 Converts the price C<$localprice> to foreign currency C<$currency> by
1139 dividing by the exchange rate, and returns the result.
1141 If no exchange rate is found, C<&curconvert> assumes the rate is one
1148 my ( $currency, $price ) = @_;
1149 my $dbh = C4::Context->dbh;
1150 my $sth = $dbh->prepare("Select rate from currency where currency=?");
1151 $sth->execute($currency);
1152 my $cur = ( $sth->fetchrow_array() )[0];
1157 return ( $price / $cur );
1162 ($count, $currencies) = &getcurrencies();
1164 Returns the list of all known currencies.
1166 C<$count> is the number of elements in C<$currencies>. C<$currencies>
1167 is a reference-to-array; its elements are references-to-hash, whose
1168 keys are the fields from the currency table in the Koha database.
1174 my $dbh = C4::Context->dbh;
1175 my $sth = $dbh->prepare("Select * from currency");
1178 while ( my $data = $sth->fetchrow_hashref ) {
1179 push( @results, $data );
1182 return ( scalar(@results), \@results );
1185 =item updatecurrencies
1187 &updatecurrencies($currency, $newrate);
1189 Sets the exchange rate for C<$currency> to be C<$newrate>.
1194 sub updatecurrencies {
1195 my ( $currency, $rate ) = @_;
1196 my $dbh = C4::Context->dbh;
1197 my $sth = $dbh->prepare("update currency set rate=? where currency=?");
1198 $sth->execute( $rate, $currency );
1210 ($count, @results) = &bookseller($searchstring);
1212 Looks up a book seller. C<$searchstring> may be either a book seller
1213 ID, or a string to look for in the book seller's name.
1215 C<$count> is the number of elements in C<@results>. C<@results> is an
1216 array of references-to-hash, whose keys are the fields of of the
1217 aqbooksellers table in the Koha database.
1223 my ($searchstring) = @_;
1224 my $dbh = C4::Context->dbh;
1226 $dbh->prepare("Select * from aqbooksellers where name like ? or id = ?");
1227 $sth->execute( "$searchstring%", $searchstring );
1229 while ( my $data = $sth->fetchrow_hashref ) {
1230 push( @results, $data );
1233 return ( scalar(@results), @results );
1238 ($count, $results) = &breakdown($ordernumber);
1240 Looks up an order by order ID, and returns its breakdown.
1242 C<$count> is the number of elements in C<$results>. C<$results> is a
1243 reference-to-array; its elements are references-to-hash, whose keys
1244 are the fields of the aqorderbreakdown table in the Koha database.
1251 my $dbh = C4::Context->dbh;
1253 $dbh->prepare("Select * from aqorderbreakdown where ordernumber=?");
1256 while ( my $data = $sth->fetchrow_hashref ) {
1257 push( @results, $data );
1260 return ( scalar(@results), \@results );
1265 ($count, @results) = &branches();
1267 Returns a list of all library branches.
1269 C<$count> is the number of elements in C<@results>. C<@results> is an
1270 array of references-to-hash, whose keys are the fields of the branches
1271 table of the Koha database.
1277 my $dbh = C4::Context->dbh;
1279 if ( C4::Context->preference("IndependantBranches")
1280 && ( C4::Context->userenv )
1281 && ( C4::Context->userenv->{flags} != 1 ) )
1283 my $strsth = "Select * from branches ";
1285 " WHERE branchcode = "
1286 . $dbh->quote( C4::Context->userenv->{branch} );
1287 $strsth .= " order by branchname";
1288 warn "C4::Acquisition->branches : " . $strsth;
1289 $sth = $dbh->prepare($strsth);
1292 $sth = $dbh->prepare("Select * from branches order by branchname");
1297 while ( my $data = $sth->fetchrow_hashref ) {
1298 push( @results, $data );
1302 return ( scalar(@results), @results );
1307 &updatesup($bookseller);
1309 Updates the information for a given bookseller. C<$bookseller> is a
1310 reference-to-hash whose keys are the fields of the aqbooksellers table
1311 in the Koha database. It must contain entries for all of the fields.
1312 The entry to modify is determined by C<$bookseller-E<gt>{id}>.
1314 The easiest way to get all of the necessary fields is to look up a
1315 book seller with C<&booksellers>, modify what's necessary, then call
1316 C<&updatesup> with the result.
1323 my $dbh = C4::Context->dbh;
1324 my $sth = $dbh->prepare(
1325 "Update aqbooksellers set
1326 name=?,address1=?,address2=?,address3=?,address4=?,postal=?,
1327 phone=?,fax=?,url=?,contact=?,contpos=?,contphone=?,contfax=?,contaltphone=?,
1328 contemail=?,contnotes=?,active=?,
1329 listprice=?, invoiceprice=?,gstreg=?, listincgst=?,
1330 invoiceincgst=?, specialty=?,discount=?,invoicedisc=?,
1335 $data->{'name'}, $data->{'address1'},
1336 $data->{'address2'}, $data->{'address3'},
1337 $data->{'address4'}, $data->{'postal'},
1338 $data->{'phone'}, $data->{'fax'},
1339 $data->{'url'}, $data->{'contact'},
1340 $data->{'contpos'}, $data->{'contphone'},
1341 $data->{'contfax'}, $data->{'contaltphone'},
1342 $data->{'contemail'}, $data->{'contnote'},
1343 $data->{'active'}, $data->{'listprice'},
1344 $data->{'invoiceprice'}, $data->{'gstreg'},
1345 $data->{'listincgst'}, $data->{'invoiceincgst'},
1346 $data->{'specialty'}, $data->{'discount'},
1347 $data->{'invoicedisc'}, $data->{'nocalc'},
1355 $id = &insertsup($bookseller);
1357 Creates a new bookseller. C<$bookseller> is a reference-to-hash whose
1358 keys are the fields of the aqbooksellers table in the Koha database.
1359 All fields must be present.
1361 Returns the ID of the newly-created bookseller.
1368 my $dbh = C4::Context->dbh;
1369 my $sth = $dbh->prepare("Select max(id) from aqbooksellers");
1371 my $data2 = $sth->fetchrow_hashref;
1373 $data2->{'max(id)'}++;
1374 $sth = $dbh->prepare("Insert into aqbooksellers (id) values (?)");
1375 $sth->execute( $data2->{'max(id)'} );
1377 $data->{'id'} = $data2->{'max(id)'};
1379 return ( $data->{'id'} );
1384 ($count, $results) = &getparcels($dbh, $bookseller, $order, $limit);
1386 get a lists of parcels
1387 Returns the count of parcels returned and a pointer on a hash list containing parcel informations as such :
1398 my ( $bookseller, $order, $code, $datefrom, $dateto, $limit ) = @_;
1399 my $dbh = C4::Context->dbh;
1401 "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 ";
1402 $strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" "
1404 $strsth .= "and datereceived >=" . $dbh->quote($datefrom) . " "
1406 $strsth .= "and datereceived <=" . $dbh->quote($dateto) . " " if ($dateto);
1407 $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
1408 $strsth .= "order by $order " if ($order);
1409 $strsth .= " LIMIT 0,$limit" if ($limit);
1410 my $sth = $dbh->prepare($strsth);
1411 ### getparcels: $strsth
1415 while ( my $data2 = $sth->fetchrow_hashref ) {
1416 push @results, $data2;
1420 return ( scalar(@results), @results );
1423 END { } # module clean-up code here (global destructor)
1432 Koha Developement team <info@koha.org>