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.
65 =head2 FUNCTIONS ABOUT BASKETS
71 $aqbasket = &GetBasket($basketnumber);
73 get all basket informations in aqbasket for a given basket
76 informations for a given basket returned as a hashref.
84 my $dbh = C4::Context->dbh;
87 concat( b.firstname,' ',b.surname) AS authorisedbyname,
88 b.branchcode AS branch
90 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
93 my $sth=$dbh->prepare($query);
94 $sth->execute($basketno);
95 my $basket = $sth->fetchrow_hashref;
99 #------------------------------------------------------------#
105 $basket = &NewBasket();
107 Create a new basket in aqbasket table
113 # FIXME : this function seems to be unused.
116 my ( $booksellerid, $authorisedby ) = @_;
117 my $dbh = C4::Context->dbh;
120 (creationdate,booksellerid,authorisedby)
121 VALUES (now(),'$booksellerid','$authorisedby')
126 #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
127 my $basket = $dbh->{'mysql_insertid'};
131 #------------------------------------------------------------#
137 &CloseBasket($basketno);
139 close a basket (becomes unmodifiable,except for recieves)
147 my $dbh = C4::Context->dbh;
153 my $sth = $dbh->prepare($query);
154 $sth->execute($basketno);
157 #------------------------------------------------------------#
159 =head2 FUNCTIONS ABOUT ORDERS
163 #------------------------------------------------------------#
165 =head3 GetPendingOrders
169 $orders = &GetPendingOrders($booksellerid, $grouped);
171 Finds pending orders from the bookseller with the given ID. Ignores
172 completed and cancelled orders.
174 C<$orders> is a reference-to-array; each element is a
175 reference-to-hash with the following fields:
176 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
177 in a single result line
181 =item C<authorizedby>
187 These give the value of the corresponding field in the aqorders table
188 of the Koha database.
194 Results are ordered from most to least recent.
198 sub GetPendingOrders {
199 my ($supplierid,$grouped) = @_;
200 my $dbh = C4::Context->dbh;
202 SELECT ".($grouped?"count(*),":"")."aqbasket.basketno,
203 surname,firstname,aqorders.*,
204 aqbasket.closedate, aqbasket.creationdate
206 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
207 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
209 AND (quantity > quantityreceived OR quantityreceived is NULL)
210 AND datecancellationprinted IS NULL
211 AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
213 ## FIXME Why 180 days ???
214 my @query_params = ( $supplierid );
215 if ( C4::Context->preference("IndependantBranches") ) {
216 my $userenv = C4::Context->userenv;
217 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
218 $strsth .= " and (borrowers.branchcode = ?
219 or borrowers.branchcode = '')";
220 push @query_params, $userenv->{branch};
223 $strsth .= " group by aqbasket.basketno" if $grouped;
224 $strsth .= " order by aqbasket.basketno";
226 my $sth = $dbh->prepare($strsth);
227 $sth->execute( @query_params );
228 my $results = $sth->fetchall_arrayref({});
233 #------------------------------------------------------------#
239 @orders = &GetOrders($basketnumber, $orderby);
241 Looks up the pending (non-cancelled) orders with the given basket
242 number. If C<$booksellerID> is non-empty, only orders from that seller
246 C<&basket> returns a two-element array. C<@orders> is an array of
247 references-to-hash, whose keys are the fields from the aqorders,
248 biblio, and biblioitems tables in the Koha database.
255 my ( $basketno, $orderby ) = @_;
256 my $dbh = C4::Context->dbh;
258 SELECT aqorderbreakdown.*,
259 biblio.*,biblioitems.*,
261 aqbookfund.bookfundname,
264 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
265 LEFT JOIN aqbookfund ON aqbookfund.bookfundid=aqorderbreakdown.bookfundid
266 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
267 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
269 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
272 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
273 $query .= " ORDER BY $orderby";
274 my $sth = $dbh->prepare($query);
275 $sth->execute($basketno);
278 while ( my $data = $sth->fetchrow_hashref ) {
279 push @results, $data;
285 #------------------------------------------------------------#
287 =head3 GetOrderNumber
291 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
295 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
297 Returns the number of this order.
301 =item C<$ordernumber> is the order number.
307 my ( $biblionumber,$biblioitemnumber ) = @_;
308 my $dbh = C4::Context->dbh;
313 AND biblioitemnumber=?
315 my $sth = $dbh->prepare($query);
316 $sth->execute( $biblionumber, $biblioitemnumber );
318 return $sth->fetchrow;
321 #------------------------------------------------------------#
327 $order = &GetOrder($ordernumber);
329 Looks up an order by order number.
331 Returns a reference-to-hash describing the order. The keys of
332 C<$order> are fields from the biblio, biblioitems, aqorders, and
333 aqorderbreakdown tables of the Koha database.
341 my $dbh = C4::Context->dbh;
343 SELECT biblioitems.*, biblio.*, aqorderbreakdown.*, aqorders.*
345 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
346 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
347 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
348 WHERE aqorders.ordernumber=?
351 my $sth= $dbh->prepare($query);
352 $sth->execute($ordnum);
353 my $data = $sth->fetchrow_hashref;
358 #------------------------------------------------------------#
364 &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
365 $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
366 $ecost, $gst, $budget, $unitprice, $subscription,
367 $booksellerinvoicenumber, $purchaseorder, $branchcode);
369 Adds a new order to the database. Any argument that isn't described
370 below is the new value of the field with the same name in the aqorders
371 table of the Koha database.
373 C<$ordnum> is a "minimum order number." After adding the new entry to
374 the aqorders table, C<&neworder> finds the first entry in aqorders
375 with order number greater than or equal to C<$ordnum>, and adds an
376 entry to the aqorderbreakdown table, with the order number just found,
377 and the book fund ID of the newly-added order.
379 C<$budget> is effectively ignored.
380 If it's undef (anything false) or the string 'now', the current day is used.
381 Else, the upcoming July 1st is used.
383 C<$subscription> may be either "yes", or anything else for "no".
391 $basketno, $bibnum, $title, $quantity,
392 $listprice, $booksellerid, $authorisedby, $notes,
393 $bookfund, $bibitemnum, $rrp, $ecost,
394 $gst, $budget, $cost, $sub,
395 $invoice, $sort1, $sort2, $purchaseorder,
400 my $year = localtime->year() + 1900;
401 my $month = localtime->mon() + 1; # months starts at 0, add 1
403 if ( !$budget || $budget eq 'now' ) {
407 # if month is july or more, budget start is 1 jul, next year.
408 elsif ( $month >= '7' ) {
409 ++$year; # add 1 to year , coz its next year
410 $budget = "$year-07-01";
414 # START OF NEW BUDGET, 1ST OF JULY, THIS YEAR
415 $budget = "$year-07-01";
418 if ( $sub eq 'yes' ) {
425 # if $basket empty, it's also a new basket, create it
427 $basketno = NewBasket( $booksellerid, $authorisedby );
430 my $dbh = C4::Context->dbh;
433 ( biblionumber, title, basketno, quantity, listprice,
434 notes, biblioitemnumber, rrp, ecost, gst,
435 unitprice, subscription, sort1, sort2, budgetdate,
436 entrydate, purchaseordernumber)
437 VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,COALESCE(?,NOW()),NOW(),? )
439 my $sth = $dbh->prepare($query);
442 $bibnum, $title, $basketno, $quantity, $listprice,
443 $notes, $bibitemnum, $rrp, $ecost, $gst,
444 $cost, $sub, $sort1, $sort2, $budget,
449 #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
450 my $ordnum = $dbh->{'mysql_insertid'};
452 INSERT INTO aqorderbreakdown (ordernumber,bookfundid, branchcode)
455 $sth = $dbh->prepare($query);
456 $sth->execute( $ordnum, $bookfund, $branchcode );
458 return ( $basketno, $ordnum );
461 #------------------------------------------------------------#
467 &ModOrder($title, $ordernumber, $quantity, $listprice,
468 $biblionumber, $basketno, $supplier, $who, $notes,
469 $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
470 $unitprice, $booksellerinvoicenumber, $branchcode);
472 Modifies an existing order. Updates the order with order number
473 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
474 update the fields with the same name in the aqorders table of the Koha
477 Entries with order number C<$ordernumber> in the aqorderbreakdown
478 table are also updated to the new book fund ID or branchcode.
486 $title, $ordnum, $quantity, $listprice, $bibnum,
487 $basketno, $supplier, $who, $notes, $bookfund,
488 $bibitemnum, $rrp, $ecost, $gst, $budget,
489 $cost, $invoice, $sort1, $sort2, $purchaseorder, $branchcode
492 # FIXME : Refactor to pass a hashref instead of fifty params.
493 my $dbh = C4::Context->dbh;
497 quantity=?,listprice=?,basketno=?,
498 rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
499 notes=?,sort1=?, sort2=?, purchaseordernumber=?
500 WHERE ordernumber=? AND biblionumber=?
502 my $sth = $dbh->prepare($query);
504 $title, $quantity, $listprice, $basketno, $rrp,
505 $ecost, $cost, $invoice, $notes, $sort1,
506 $sort2, $purchaseorder,
511 UPDATE aqorderbreakdown
512 SET bookfundid=?,branchcode=?
515 $sth = $dbh->prepare($query);
517 my $rv = $sth->execute( $bookfund,$branchcode, $ordnum );
518 unless($rv && ( $rv ne '0E0' )) { # zero rows affected [Bug 734]
520 INSERT INTO aqorderbreakdown
521 (ordernumber,branchcode,bookfundid)
524 $sth = $dbh->prepare($query);
525 $sth->execute( $ordnum,$branchcode, $bookfund );
530 #------------------------------------------------------------#
532 =head3 ModOrderBiblioNumber
536 &ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
538 Modifies the biblioitemnumber for an existing order.
539 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
545 sub ModOrderBiblioNumber {
546 my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
547 my $dbh = C4::Context->dbh;
550 SET biblioitemnumber = ?
551 WHERE ordernumber = ?
552 AND biblionumber = ?";
553 my $sth = $dbh->prepare($query);
554 $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
557 #------------------------------------------------------------#
559 =head3 ModReceiveOrder
563 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
564 $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
565 $freight, $bookfund, $rrp);
567 Updates an order, to reflect the fact that it was received, at least
568 in part. All arguments not mentioned below update the fields with the
569 same name in the aqorders table of the Koha database.
571 If a partial order is received, splits the order into two. The received
572 portion must have a booksellerinvoicenumber.
574 Updates the order with bibilionumber C<$biblionumber> and ordernumber
577 Also updates the book fund ID in the aqorderbreakdown table.
584 sub ModReceiveOrder {
586 $biblionumber, $ordnum, $quantrec, $user, $cost,
587 $invoiceno, $freight, $rrp, $bookfund, $datereceived
590 my $dbh = C4::Context->dbh;
591 # warn "DATE BEFORE : $daterecieved";
592 # $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
593 # warn "DATE REC : $daterecieved";
594 $datereceived = C4::Dates->output('iso') unless $datereceived;
595 my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
597 ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
599 # Allows libraries to change their bookfund during receiving orders
600 # allows them to adjust budgets
601 if ( C4::Context->preference("LooseBudgets") && $bookfund ) {
603 UPDATE aqorderbreakdown
607 my $sth = $dbh->prepare($query);
608 $sth->execute( $bookfund, $ordnum );
612 my $sth=$dbh->prepare("SELECT * FROM aqorders LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
613 WHERE biblionumber=? AND aqorders.ordernumber=?");
614 $sth->execute($biblionumber,$ordnum);
615 my $order = $sth->fetchrow_hashref();
618 if ( $order->{quantity} > $quantrec ) {
619 $sth=$dbh->prepare("update aqorders
620 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
621 unitprice=?,freight=?,rrp=?,quantity=?
622 where biblionumber=? and ordernumber=?");
623 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$quantrec,$biblionumber,$ordnum);
625 # create a new order for the remaining items, and set its bookfund.
626 my $newOrder = NewOrder($order->{'basketno'},$order->{'biblionumber'},$order->{'title'}, $order->{'quantity'} - $quantrec,
627 $order->{'listprice'},$order->{'booksellerid'},$order->{'authorisedby'},$order->{'notes'},
628 $order->{'bookfundid'},$order->{'biblioitemnumber'},$order->{'rrp'},$order->{'ecost'},$order->{'gst'},
629 $order->{'budget'},$order->{'unitcost'},$order->{'sub'},'',$order->{'sort1'},$order->{'sort2'},$order->{'purchaseordernumber'});
631 $sth=$dbh->prepare("update aqorders
632 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
633 unitprice=?,freight=?,rrp=?
634 where biblionumber=? and ordernumber=?");
635 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$biblionumber,$ordnum);
638 return $datereceived;
640 #------------------------------------------------------------#
644 @results = &SearchOrder($search, $biblionumber, $complete);
648 C<$search> may take one of several forms: if it is an ISBN,
649 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
650 order number, C<&ordersearch> returns orders with that order number
651 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
652 to be a space-separated list of search terms; in this case, all of the
653 terms must appear in the title (matching the beginning of title
656 If C<$complete> is C<yes>, the results will include only completed
657 orders. In any case, C<&ordersearch> ignores cancelled orders.
659 C<&ordersearch> returns an array.
660 C<@results> is an array of references-to-hash with the following keys:
677 my ( $search, $id, $biblionumber, $catview ) = @_;
678 my $dbh = C4::Context->dbh;
679 my @data = split( ' ', $search );
682 @searchterms = ($id);
684 map { push( @searchterms, "$_%", "%$_%" ) } @data;
685 push( @searchterms, $search, $search, $biblionumber );
687 ### FIXME THIS CAN raise a problem if more THAN ONE biblioitem is linked to one biblio
690 "SELECT *,biblio.title
692 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
693 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
694 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
695 WHERE aqbasket.booksellerid = ?
696 AND ((datecancellationprinted is NULL)
697 OR (datecancellationprinted = '0000-00-00'))
701 map { "(biblio.title like ? or biblio.title like ?)" } @data )
703 . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
708 " SELECT *,biblio.title
710 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
711 LEFT JOIN aqbasket on aqorders.basketno=aqbasket.basketno
712 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
713 WHERE ((datecancellationprinted is NULL)
714 OR (datecancellationprinted = '0000-00-00'))
715 AND (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
719 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
721 . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
724 if( $biblionumber and $biblionumber ne "" ){
725 $query .= "AND aqorders.biblionumber = ? ";
726 push (@searchterms, $biblionumber);
729 $query .= " GROUP BY aqorders.ordernumber";
731 my $sth = $dbh->prepare($query);
732 $sth->execute(@searchterms);
739 my $sth2 = $dbh->prepare($query2);
742 FROM aqorderbreakdown
745 my $sth3 = $dbh->prepare($query3);
747 while ( my $data = $sth->fetchrow_hashref ) {
748 $sth2->execute( $data->{'biblionumber'} );
749 my $data2 = $sth2->fetchrow_hashref;
750 $data->{'author'} = $data2->{'author'};
751 $data->{'seriestitle'} = $data2->{'seriestitle'};
752 $sth3->execute( $data->{'ordernumber'} );
753 my $data3 = $sth3->fetchrow_hashref;
754 $data->{'branchcode'} = $data3->{'branchcode'};
755 $data->{'bookfundid'} = $data3->{'bookfundid'};
756 push( @results, $data );
765 #------------------------------------------------------------#
771 &DelOrder($biblionumber, $ordernumber);
773 Cancel the order with the given order and biblio numbers. It does not
774 delete any entries in the aqorders table, it merely marks them as
782 my ( $bibnum, $ordnum ) = @_;
783 my $dbh = C4::Context->dbh;
786 SET datecancellationprinted=now()
787 WHERE biblionumber=? AND ordernumber=?
789 my $sth = $dbh->prepare($query);
790 $sth->execute( $bibnum, $ordnum );
794 =head2 FUNCTIONS ABOUT PARCELS
798 #------------------------------------------------------------#
804 @results = &GetParcel($booksellerid, $code, $date);
806 Looks up all of the received items from the supplier with the given
807 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
809 C<@results> is an array of references-to-hash. The keys of each element are fields from
810 the aqorders, biblio, and biblioitems tables of the Koha database.
812 C<@results> is sorted alphabetically by book title.
819 #gets all orders from a certain supplier, orders them alphabetically
820 my ( $supplierid, $code, $datereceived ) = @_;
821 my $dbh = C4::Context->dbh;
824 if $code; # add % if we search on a given code (otherwise, let him empty)
831 aqorders.biblionumber,
833 aqorders.ordernumber,
835 aqorders.quantityreceived,
841 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
842 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
844 aqbasket.booksellerid = ?
845 AND aqorders.booksellerinvoicenumber LIKE ?
846 AND aqorders.datereceived = ? ";
848 my @query_params = ( $supplierid, $code, $datereceived );
849 if ( C4::Context->preference("IndependantBranches") ) {
850 my $userenv = C4::Context->userenv;
851 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
852 $strsth .= " and (borrowers.branchcode = ?
853 or borrowers.branchcode = '')";
854 push @query_params, $userenv->{branch};
857 $strsth .= " ORDER BY aqbasket.basketno";
858 ### parcelinformation : $strsth
859 my $sth = $dbh->prepare($strsth);
860 $sth->execute( @query_params );
861 while ( my $data = $sth->fetchrow_hashref ) {
862 push( @results, $data );
864 ### countparcelbiblio: scalar(@results)
870 #------------------------------------------------------------#
876 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
877 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;
1212 Koha Developement team <info@koha.org>