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
24 use C4::Dates qw(format_date);
29 use vars qw($VERSION @ISA @EXPORT);
32 # set the version for version checking
37 &GetBasket &NewBasket &CloseBasket
38 &GetPendingOrders &GetOrder &GetOrders
39 &GetOrderNumber &GetLateOrders &NewOrder &DelOrder
40 &SearchOrder &GetHistory &GetRecentAcqui
41 &ModOrder &ModReceiveOrder &ModOrderBiblioNumber
42 &GetParcels &GetParcel
46 # used in receiveorder subroutine
47 # to provide library specific handling
48 my $library_name = C4::Context->preference("LibraryName");
52 C4::Acquisition - Koha functions for dealing with orders and acquisitions
60 The functions in this module deal with acquisitions, managing book
61 orders, basket and parcels.
67 =head2 FUNCTIONS ABOUT BASKETS
75 $aqbasket = &GetBasket($basketnumber);
77 get all basket informations in aqbasket for a given basket
80 informations for a given basket returned as a hashref.
90 my $dbh = C4::Context->dbh;
93 concat( b.firstname,' ',b.surname) AS authorisedbyname,
94 b.branchcode AS branch
96 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
99 my $sth=$dbh->prepare($query);
100 $sth->execute($basketno);
101 my $basket = $sth->fetchrow_hashref;
105 #------------------------------------------------------------#
111 $basket = &NewBasket();
113 Create a new basket in aqbasket table
119 # FIXME : this function seems to be unused.
122 my ( $booksellerid, $authorisedby ) = @_;
123 my $dbh = C4::Context->dbh;
126 (creationdate,booksellerid,authorisedby)
127 VALUES (now(),'$booksellerid','$authorisedby')
132 #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
133 my $basket = $dbh->{'mysql_insertid'};
137 #------------------------------------------------------------#
143 &CloseBasket($basketno);
145 close a basket (becomes unmodifiable,except for recieves)
153 my $dbh = C4::Context->dbh;
159 my $sth = $dbh->prepare($query);
160 $sth->execute($basketno);
163 #------------------------------------------------------------#
167 =head2 FUNCTIONS ABOUT ORDERS
173 #------------------------------------------------------------#
175 =head3 GetPendingOrders
179 $orders = &GetPendingOrders($booksellerid, $grouped);
181 Finds pending orders from the bookseller with the given ID. Ignores
182 completed and cancelled orders.
184 C<$orders> is a reference-to-array; each element is a
185 reference-to-hash with the following fields:
186 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
187 in a single result line
191 =item C<authorizedby>
197 These give the value of the corresponding field in the aqorders table
198 of the Koha database.
204 Results are ordered from most to least recent.
208 sub GetPendingOrders {
209 my ($supplierid,$grouped) = @_;
210 my $dbh = C4::Context->dbh;
212 SELECT ".($grouped?"count(*),":"")."aqbasket.basketno,
213 surname,firstname,aqorders.*,
214 aqbasket.closedate, aqbasket.creationdate
216 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
217 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
219 AND (quantity > quantityreceived OR quantityreceived is NULL)
220 AND datecancellationprinted IS NULL
221 AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
223 ## FIXME Why 180 days ???
224 my @query_params = ( $supplierid );
225 if ( C4::Context->preference("IndependantBranches") ) {
226 my $userenv = C4::Context->userenv;
227 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
228 $strsth .= " and (borrowers.branchcode = ?
229 or borrowers.branchcode = '')";
230 push @query_params, $userenv->{branch};
233 $strsth .= " group by aqbasket.basketno" if $grouped;
234 $strsth .= " order by aqbasket.basketno";
236 my $sth = $dbh->prepare($strsth);
237 $sth->execute( @query_params );
238 my $results = $sth->fetchall_arrayref({});
243 #------------------------------------------------------------#
249 @orders = &GetOrders($basketnumber, $orderby);
251 Looks up the pending (non-cancelled) orders with the given basket
252 number. If C<$booksellerID> is non-empty, only orders from that seller
256 C<&basket> returns a two-element array. C<@orders> is an array of
257 references-to-hash, whose keys are the fields from the aqorders,
258 biblio, and biblioitems tables in the Koha database.
265 my ( $basketno, $orderby ) = @_;
266 my $dbh = C4::Context->dbh;
268 SELECT aqorderbreakdown.*,
269 biblio.*,biblioitems.publishercode,
271 aqbookfund.bookfundname,
274 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
275 LEFT JOIN aqbookfund ON aqbookfund.bookfundid=aqorderbreakdown.bookfundid
276 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
277 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
279 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
282 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
283 $query .= " ORDER BY $orderby";
284 my $sth = $dbh->prepare($query);
285 $sth->execute($basketno);
288 while ( my $data = $sth->fetchrow_hashref ) {
289 push @results, $data;
295 #------------------------------------------------------------#
297 =head3 GetOrderNumber
301 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
303 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
305 Returns the number of this order.
307 =item C<$ordernumber> is the order number.
313 my ( $biblionumber,$biblioitemnumber ) = @_;
314 my $dbh = C4::Context->dbh;
319 AND biblioitemnumber=?
321 my $sth = $dbh->prepare($query);
322 $sth->execute( $biblionumber, $biblioitemnumber );
324 return $sth->fetchrow;
327 #------------------------------------------------------------#
333 $order = &GetOrder($ordernumber);
335 Looks up an order by order number.
337 Returns a reference-to-hash describing the order. The keys of
338 C<$order> are fields from the biblio, biblioitems, aqorders, and
339 aqorderbreakdown tables of the Koha database.
347 my $dbh = C4::Context->dbh;
349 SELECT biblioitems.*, biblio.*, aqorderbreakdown.*, aqorders.*
351 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
352 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
353 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
354 WHERE aqorders.ordernumber=?
357 my $sth= $dbh->prepare($query);
358 $sth->execute($ordnum);
359 my $data = $sth->fetchrow_hashref;
364 #------------------------------------------------------------#
370 &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
371 $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
372 $ecost, $gst, $budget, $unitprice, $subscription,
373 $booksellerinvoicenumber, $purchaseorder);
375 Adds a new order to the database. Any argument that isn't described
376 below is the new value of the field with the same name in the aqorders
377 table of the Koha database.
379 C<$ordnum> is a "minimum order number." After adding the new entry to
380 the aqorders table, C<&neworder> finds the first entry in aqorders
381 with order number greater than or equal to C<$ordnum>, and adds an
382 entry to the aqorderbreakdown table, with the order number just found,
383 and the book fund ID of the newly-added order.
385 C<$budget> is effectively ignored.
386 If it's undef (anything false) or the string 'now', the current day is used.
387 Else, the upcoming July 1st is used.
389 C<$subscription> may be either "yes", or anything else for "no".
397 $basketno, $bibnum, $title, $quantity,
398 $listprice, $booksellerid, $authorisedby, $notes,
399 $bookfund, $bibitemnum, $rrp, $ecost,
400 $gst, $budget, $cost, $sub,
401 $invoice, $sort1, $sort2, $purchaseorder
405 my $year = localtime->year() + 1900;
406 my $month = localtime->mon() + 1; # months starts at 0, add 1
408 if ( !$budget || $budget eq 'now' ) {
412 # if month is july or more, budget start is 1 jul, next year.
413 elsif ( $month >= '7' ) {
414 ++$year; # add 1 to year , coz its next year
415 $budget = "$year-07-01";
419 # START OF NEW BUDGET, 1ST OF JULY, THIS YEAR
420 $budget = "$year-07-01";
423 if ( $sub eq 'yes' ) {
430 # if $basket empty, it's also a new basket, create it
432 $basketno = NewBasket( $booksellerid, $authorisedby );
435 my $dbh = C4::Context->dbh;
438 ( biblionumber, title, basketno, quantity, listprice,
439 notes, biblioitemnumber, rrp, ecost, gst,
440 unitprice, subscription, sort1, sort2, budgetdate,
441 entrydate, purchaseordernumber)
442 VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,COALESCE(?,NOW()),NOW(),? )
444 my $sth = $dbh->prepare($query);
447 $bibnum, $title, $basketno, $quantity, $listprice,
448 $notes, $bibitemnum, $rrp, $ecost, $gst,
449 $cost, $sub, $sort1, $sort2, $budget,
454 #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
455 my $ordnum = $dbh->{'mysql_insertid'};
457 INSERT INTO aqorderbreakdown (ordernumber,bookfundid)
460 $sth = $dbh->prepare($query);
461 $sth->execute( $ordnum, $bookfund );
463 return ( $basketno, $ordnum );
466 #------------------------------------------------------------#
472 &ModOrder($title, $ordernumber, $quantity, $listprice,
473 $biblionumber, $basketno, $supplier, $who, $notes,
474 $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
475 $unitprice, $booksellerinvoicenumber, $branchcode);
477 Modifies an existing order. Updates the order with order number
478 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
479 update the fields with the same name in the aqorders table of the Koha
482 Entries with order number C<$ordernumber> in the aqorderbreakdown
483 table are also updated to the new book fund ID or branchcode.
491 $title, $ordnum, $quantity, $listprice, $bibnum,
492 $basketno, $supplier, $who, $notes, $bookfund,
493 $bibitemnum, $rrp, $ecost, $gst, $budget,
494 $cost, $invoice, $sort1, $sort2, $purchaseorder, $branchcode
497 # FIXME : Refactor to pass a hashref instead of fifty params.
498 my $dbh = C4::Context->dbh;
502 quantity=?,listprice=?,basketno=?,
503 rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
504 notes=?,sort1=?, sort2=?, purchaseordernumber=?
505 WHERE ordernumber=? AND biblionumber=?
507 my $sth = $dbh->prepare($query);
509 $title, $quantity, $listprice, $basketno, $rrp,
510 $ecost, $cost, $invoice, $notes, $sort1,
511 $sort2, $purchaseorder,
516 UPDATE aqorderbreakdown
517 SET bookfundid=?,branchcode=?
520 $sth = $dbh->prepare($query);
522 my $rv = $sth->execute( $bookfund,$branchcode, $ordnum );
523 unless($rv && ( $rv ne '0E0' )) { # zero rows affected [Bug 734]
525 INSERT INTO aqorderbreakdown
526 (ordernumber,branchcode,bookfundid)
529 $sth = $dbh->prepare($query);
530 $sth->execute( $ordnum,$branchcode, $bookfund );
535 #------------------------------------------------------------#
537 =head3 ModOrderBiblioNumber
541 &ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
543 Modifies the biblioitemnumber for an existing order.
544 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
550 sub ModOrderBiblioNumber {
551 my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
552 my $dbh = C4::Context->dbh;
555 SET biblioitemnumber = ?
556 WHERE ordernumber = ?
557 AND biblionumber = ?";
558 my $sth = $dbh->prepare($query);
559 $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
562 #------------------------------------------------------------#
564 =head3 ModReceiveOrder
568 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
569 $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
570 $freight, $bookfund, $rrp);
572 Updates an order, to reflect the fact that it was received, at least
573 in part. All arguments not mentioned below update the fields with the
574 same name in the aqorders table of the Koha database.
576 If a partial order is received, splits the order into two. The received
577 portion must have a booksellerinvoicenumber.
579 Updates the order with bibilionumber C<$biblionumber> and ordernumber
582 Also updates the book fund ID in the aqorderbreakdown table.
589 sub ModReceiveOrder {
591 $biblionumber, $ordnum, $quantrec, $user, $cost,
592 $invoiceno, $freight, $rrp, $bookfund, $datereceived
595 my $dbh = C4::Context->dbh;
596 # warn "DATE BEFORE : $daterecieved";
597 # $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
598 # warn "DATE REC : $daterecieved";
599 $datereceived = C4::Dates->output('iso') unless $datereceived;
600 my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
602 ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
604 # Allows libraries to change their bookfund during receiving orders
605 # allows them to adjust budgets
606 if ( C4::Context->preference("LooseBudgets") && $bookfund ) {
608 UPDATE aqorderbreakdown
612 my $sth = $dbh->prepare($query);
613 $sth->execute( $bookfund, $ordnum );
617 my $sth=$dbh->prepare("SELECT * FROM aqorders LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
618 WHERE biblionumber=? AND aqorders.ordernumber=?");
619 $sth->execute($biblionumber,$ordnum);
620 my $order = $sth->fetchrow_hashref();
623 if ( $order->{quantity} > $quantrec ) {
624 $sth=$dbh->prepare("update aqorders
625 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
626 unitprice=?,freight=?,rrp=?,quantity=?
627 where biblionumber=? and ordernumber=?");
628 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$quantrec,$biblionumber,$ordnum);
630 # create a new order for the remaining items, and set its bookfund.
631 my $newOrder = NewOrder($order->{'basketno'},$order->{'biblionumber'},$order->{'title'}, $order->{'quantity'} - $quantrec,
632 $order->{'listprice'},$order->{'booksellerid'},$order->{'authorisedby'},$order->{'notes'},
633 $order->{'bookfundid'},$order->{'biblioitemnumber'},$order->{'rrp'},$order->{'ecost'},$order->{'gst'},
634 $order->{'budget'},$order->{'unitcost'},$order->{'sub'},'',$order->{'sort1'},$order->{'sort2'},$order->{'purchaseordernumber'});
636 $sth=$dbh->prepare("update aqorders
637 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
638 unitprice=?,freight=?,rrp=?
639 where biblionumber=? and ordernumber=?");
640 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$biblionumber,$ordnum);
643 return $datereceived;
645 #------------------------------------------------------------#
649 @results = &SearchOrder($search, $biblionumber, $complete);
653 C<$search> may take one of several forms: if it is an ISBN,
654 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
655 order number, C<&ordersearch> returns orders with that order number
656 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
657 to be a space-separated list of search terms; in this case, all of the
658 terms must appear in the title (matching the beginning of title
661 If C<$complete> is C<yes>, the results will include only completed
662 orders. In any case, C<&ordersearch> ignores cancelled orders.
664 C<&ordersearch> returns an array.
665 C<@results> is an array of references-to-hash with the following keys:
682 my ( $search, $id, $biblionumber, $catview ) = @_;
683 my $dbh = C4::Context->dbh;
684 my @data = split( ' ', $search );
687 @searchterms = ($id);
689 map { push( @searchterms, "$_%", "%$_%" ) } @data;
690 push( @searchterms, $search, $search, $biblionumber );
692 ### FIXME THIS CAN raise a problem if more THAN ONE biblioitem is linked to one biblio
695 "SELECT *,biblio.title
697 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
698 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
699 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
700 WHERE aqbasket.booksellerid = ?
701 AND ((datecancellationprinted is NULL)
702 OR (datecancellationprinted = '0000-00-00'))
706 map { "(biblio.title like ? or biblio.title like ?)" } @data )
708 . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
713 " SELECT *,biblio.title
715 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
716 LEFT JOIN aqbasket on aqorders.basketno=aqbasket.basketno
717 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
718 WHERE ((datecancellationprinted is NULL)
719 OR (datecancellationprinted = '0000-00-00'))
720 AND (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
724 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
726 . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
728 $query .= " GROUP BY aqorders.ordernumber";
730 my $sth = $dbh->prepare($query);
731 $sth->execute(@searchterms);
738 my $sth2 = $dbh->prepare($query2);
741 FROM aqorderbreakdown
744 my $sth3 = $dbh->prepare($query3);
746 while ( my $data = $sth->fetchrow_hashref ) {
747 $sth2->execute( $data->{'biblionumber'} );
748 my $data2 = $sth2->fetchrow_hashref;
749 $data->{'author'} = $data2->{'author'};
750 $data->{'seriestitle'} = $data2->{'seriestitle'};
751 $sth3->execute( $data->{'ordernumber'} );
752 my $data3 = $sth3->fetchrow_hashref;
753 $data->{'branchcode'} = $data3->{'branchcode'};
754 $data->{'bookfundid'} = $data3->{'bookfundid'};
755 push( @results, $data );
764 #------------------------------------------------------------#
770 &DelOrder($biblionumber, $ordernumber);
772 Cancel the order with the given order and biblio numbers. It does not
773 delete any entries in the aqorders table, it merely marks them as
781 my ( $bibnum, $ordnum ) = @_;
782 my $dbh = C4::Context->dbh;
785 SET datecancellationprinted=now()
786 WHERE biblionumber=? AND ordernumber=?
788 my $sth = $dbh->prepare($query);
789 $sth->execute( $bibnum, $ordnum );
796 =head2 FUNCTIONS ABOUT PARCELS
802 #------------------------------------------------------------#
808 @results = &GetParcel($booksellerid, $code, $date);
810 Looks up all of the received items from the supplier with the given
811 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
813 C<@results> is an array of references-to-hash. The keys of each element are fields from
814 the aqorders, biblio, and biblioitems tables of the Koha database.
816 C<@results> is sorted alphabetically by book title.
823 #gets all orders from a certain supplier, orders them alphabetically
824 my ( $supplierid, $code, $datereceived ) = @_;
825 my $dbh = C4::Context->dbh;
828 if $code; # add % if we search on a given code (otherwise, let him empty)
835 aqorders.biblionumber,
837 aqorders.ordernumber,
839 aqorders.quantityreceived,
845 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
846 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
848 aqbasket.booksellerid = ?
849 AND aqorders.booksellerinvoicenumber LIKE ?
850 AND aqorders.datereceived = ? ";
852 my @query_params = ( $supplierid, $code, $datereceived );
853 if ( C4::Context->preference("IndependantBranches") ) {
854 my $userenv = C4::Context->userenv;
855 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
856 $strsth .= " and (borrowers.branchcode = ?
857 or borrowers.branchcode = '')";
858 push @query_params, $userenv->{branch};
861 $strsth .= " ORDER BY aqbasket.basketno";
862 ### parcelinformation : $strsth
863 my $sth = $dbh->prepare($strsth);
864 $sth->execute( @query_params );
865 while ( my $data = $sth->fetchrow_hashref ) {
866 push( @results, $data );
868 ### countparcelbiblio: scalar(@results)
874 #------------------------------------------------------------#
880 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
881 get a lists of parcels.
886 is the bookseller this function has to get parcels.
889 To know on what criteria the results list has to be ordered.
892 is the booksellerinvoicenumber.
894 =item $datefrom & $dateto
895 to know on what date this function has to filter its search.
898 a pointer on a hash list containing parcel informations as such :
904 =item Number of biblio
906 =item Number of items
913 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
914 my $dbh = C4::Context->dbh;
915 my @query_params = ();
917 SELECT aqorders.booksellerinvoicenumber,
918 datereceived,purchaseordernumber,
919 count(DISTINCT biblionumber) AS biblio,
920 sum(quantity) AS itemsexpected,
921 sum(quantityreceived) AS itemsreceived
922 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
923 WHERE aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
926 if ( defined $code ) {
927 $strsth .= ' and aqorders.booksellerinvoicenumber like ? ';
928 # add a % to the end of the code to allow stemming.
929 push @query_params, "$code%";
932 if ( defined $datefrom ) {
933 $strsth .= ' and datereceived >= ? ';
934 push @query_params, $datefrom;
937 if ( defined $dateto ) {
938 $strsth .= 'and datereceived <= ? ';
939 push @query_params, $dateto;
942 $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
944 # can't use a placeholder to place this column name.
945 # but, we could probably be checking to make sure it is a column that will be fetched.
946 $strsth .= "order by $order " if ($order);
948 my $sth = $dbh->prepare($strsth);
950 $sth->execute( @query_params );
951 my $results = $sth->fetchall_arrayref({});
956 #------------------------------------------------------------#
962 @results = &GetLateOrders;
964 Searches for bookseller with late orders.
967 the table of supplier with late issues. This table is full of hashref.
975 my $supplierid = shift;
978 my $dbh = C4::Context->dbh;
980 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
981 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
983 my @query_params = ($delay); # delay is the first argument regardless
985 SELECT aqbasket.basketno,
986 aqorders.ordernumber,
987 DATE(aqbasket.closedate) AS orderdate,
988 aqorders.rrp AS unitpricesupplier,
989 aqorders.ecost AS unitpricelib,
990 aqbookfund.bookfundname AS budget,
991 borrowers.branchcode AS branch,
992 aqbooksellers.name AS supplier,
995 biblioitems.publishercode AS publisher,
996 biblioitems.publicationyear,
1000 (aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber)
1001 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber)
1002 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber = aqorderbreakdown.ordernumber)
1003 LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
1004 (aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber)
1005 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1006 WHERE aqorders.basketno = aqbasket.basketno
1007 AND ( (datereceived = '' OR datereceived IS NULL)
1008 OR (aqorders.quantityreceived < aqorders.quantity)
1012 if ($dbdriver eq "mysql") {
1014 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
1015 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1016 DATEDIFF(CURDATE( ),closedate) AS latesince
1018 $from .= " AND (closedate <= DATE_SUB(CURDATE( ),INTERVAL ? DAY)) ";
1020 HAVING quantity <> 0
1021 AND unitpricesupplier <> 0
1022 AND unitpricelib <> 0
1025 # FIXME: account for IFNULL as above
1027 aqorders.quantity AS quantity,
1028 aqorders.quantity * aqorders.rrp AS subtotal,
1029 (CURDATE - closedate) AS latesince
1031 $from .= " AND (closedate <= (CURDATE -(INTERVAL ? DAY)) ";
1033 if (defined $supplierid) {
1034 $from .= ' AND aqbasket.booksellerid = ? ';
1035 push @query_params, $supplierid;
1037 if (defined $branch) {
1038 $from .= ' AND borrowers.branchcode LIKE ? ';
1039 push @query_params, $branch;
1041 if (C4::Context->preference("IndependantBranches")
1042 && C4::Context->userenv
1043 && C4::Context->userenv->{flags} != 1 ) {
1044 $from .= ' AND borrowers.branchcode LIKE ? ';
1045 push @query_params, C4::Context->userenv->{branch};
1047 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
1048 $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
1049 my $sth = $dbh->prepare($query);
1050 $sth->execute(@query_params);
1052 while (my $data = $sth->fetchrow_hashref) {
1053 $data->{orderdate} = format_date($data->{orderdate});
1054 push @results, $data;
1059 #------------------------------------------------------------#
1065 (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on );
1067 Retreives some acquisition history information
1070 $order_loop is a list of hashrefs that each look like this:
1072 'author' => 'Twain, Mark',
1074 'biblionumber' => '215',
1076 'creationdate' => 'MM/DD/YYYY',
1077 'datereceived' => undef,
1080 'invoicenumber' => undef,
1082 'ordernumber' => '1',
1084 'quantityreceived' => undef,
1085 'title' => 'The Adventures of Huckleberry Finn'
1087 $total_qty is the sum of all of the quantities in $order_loop
1088 $total_price is the cost of each in $order_loop times the quantity
1089 $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
1096 my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
1099 my $total_qtyreceived = 0;
1100 my $total_price = 0;
1102 # don't run the query if there are no parameters (list would be too long for sure !)
1103 if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
1104 my $dbh = C4::Context->dbh;
1110 name,aqbasket.creationdate,
1111 aqorders.datereceived,
1113 aqorders.quantityreceived,
1115 aqorders.ordernumber,
1116 aqorders.booksellerinvoicenumber as invoicenumber,
1117 aqbooksellers.id as id,
1118 aqorders.biblionumber
1120 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
1121 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
1122 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
1124 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
1125 if ( C4::Context->preference("IndependantBranches") );
1127 $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
1129 my @query_params = ();
1131 if ( defined $title ) {
1132 $query .= " AND biblio.title LIKE ? ";
1133 push @query_params, "%$title%";
1136 if ( defined $author ) {
1137 $query .= " AND biblio.author LIKE ? ";
1138 push @query_params, "%$author%";
1141 if ( defined $name ) {
1142 $query .= " AND name LIKE ? ";
1143 push @query_params, "%$name%";
1146 if ( defined $from_placed_on ) {
1147 $query .= " AND creationdate >= ? ";
1148 push @query_params, $from_placed_on;
1151 if ( defined $to_placed_on ) {
1152 $query .= " AND creationdate <= ? ";
1153 push @query_params, $to_placed_on;
1156 if ( C4::Context->preference("IndependantBranches") ) {
1157 my $userenv = C4::Context->userenv;
1158 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1159 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
1160 push @query_params, $userenv->{branch};
1163 $query .= " ORDER BY booksellerid";
1164 my $sth = $dbh->prepare($query);
1165 $sth->execute( @query_params );
1167 while ( my $line = $sth->fetchrow_hashref ) {
1168 $line->{count} = $cnt++;
1169 $line->{toggle} = 1 if $cnt % 2;
1170 push @order_loop, $line;
1171 $line->{creationdate} = format_date( $line->{creationdate} );
1172 $line->{datereceived} = format_date( $line->{datereceived} );
1173 $total_qty += $line->{'quantity'};
1174 $total_qtyreceived += $line->{'quantityreceived'};
1175 $total_price += $line->{'quantity'} * $line->{'ecost'};
1178 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
1181 =head2 GetRecentAcqui
1183 $results = GetRecentAcqui($days);
1185 C<$results> is a ref to a table which containts hashref
1189 sub GetRecentAcqui {
1191 my $dbh = C4::Context->dbh;
1195 ORDER BY timestamp DESC
1198 my $sth = $dbh->prepare($query);
1201 while(my $data = $sth->fetchrow_hashref){
1202 push @results,$data;
1214 Koha Developement team <info@koha.org>