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, $closed) = @_;
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 datecancellationprinted IS NULL
210 AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
214 AND (quantity > quantityreceived OR quantityreceived is NULL)
215 AND closedate IS NOT NULL ";
217 ## FIXME Why 180 days ???
218 my @query_params = ( $supplierid );
219 if ( C4::Context->preference("IndependantBranches") ) {
220 my $userenv = C4::Context->userenv;
221 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
222 $strsth .= " and (borrowers.branchcode = ?
223 or borrowers.branchcode = '')";
224 push @query_params, $userenv->{branch};
227 $strsth .= " group by aqbasket.basketno" if $grouped;
228 $strsth .= " order by aqbasket.basketno";
230 my $sth = $dbh->prepare($strsth);
231 $sth->execute( @query_params );
232 my $results = $sth->fetchall_arrayref({});
237 #------------------------------------------------------------#
243 @orders = &GetOrders($basketnumber, $orderby);
245 Looks up the pending (non-cancelled) orders with the given basket
246 number. If C<$booksellerID> is non-empty, only orders from that seller
250 C<&basket> returns a two-element array. C<@orders> is an array of
251 references-to-hash, whose keys are the fields from the aqorders,
252 biblio, and biblioitems tables in the Koha database.
259 my ( $basketno, $orderby ) = @_;
260 my $dbh = C4::Context->dbh;
262 SELECT aqorderbreakdown.*,
263 biblio.*,biblioitems.*,
265 aqbookfund.bookfundname,
268 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
269 LEFT JOIN aqbookfund ON aqbookfund.bookfundid=aqorderbreakdown.bookfundid
270 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
271 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
273 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
276 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
277 $query .= " ORDER BY $orderby";
278 my $sth = $dbh->prepare($query);
279 $sth->execute($basketno);
282 while ( my $data = $sth->fetchrow_hashref ) {
283 push @results, $data;
289 #------------------------------------------------------------#
291 =head3 GetOrderNumber
295 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
299 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
301 Returns the number of this order.
305 =item C<$ordernumber> is the order number.
311 my ( $biblionumber,$biblioitemnumber ) = @_;
312 my $dbh = C4::Context->dbh;
317 AND biblioitemnumber=?
319 my $sth = $dbh->prepare($query);
320 $sth->execute( $biblionumber, $biblioitemnumber );
322 return $sth->fetchrow;
325 #------------------------------------------------------------#
331 $order = &GetOrder($ordernumber);
333 Looks up an order by order number.
335 Returns a reference-to-hash describing the order. The keys of
336 C<$order> are fields from the biblio, biblioitems, aqorders, and
337 aqorderbreakdown tables of the Koha database.
345 my $dbh = C4::Context->dbh;
347 SELECT biblioitems.*, biblio.*, aqorderbreakdown.*, aqorders.*
349 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
350 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
351 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
352 WHERE aqorders.ordernumber=?
355 my $sth= $dbh->prepare($query);
356 $sth->execute($ordnum);
357 my $data = $sth->fetchrow_hashref;
362 #------------------------------------------------------------#
368 &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
369 $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
370 $ecost, $gst, $budget, $unitprice, $subscription,
371 $booksellerinvoicenumber, $purchaseorder, $branchcode);
373 Adds a new order to the database. Any argument that isn't described
374 below is the new value of the field with the same name in the aqorders
375 table of the Koha database.
377 C<$ordnum> is a "minimum order number." After adding the new entry to
378 the aqorders table, C<&neworder> finds the first entry in aqorders
379 with order number greater than or equal to C<$ordnum>, and adds an
380 entry to the aqorderbreakdown table, with the order number just found,
381 and the book fund ID of the newly-added order.
383 C<$budget> is effectively ignored.
384 If it's undef (anything false) or the string 'now', the current day is used.
385 Else, the upcoming July 1st is used.
387 C<$subscription> may be either "yes", or anything else for "no".
395 $basketno, $bibnum, $title, $quantity,
396 $listprice, $booksellerid, $authorisedby, $notes,
397 $bookfund, $bibitemnum, $rrp, $ecost,
398 $gst, $budget, $cost, $sub,
399 $invoice, $sort1, $sort2, $purchaseorder,
404 my $year = localtime->year() + 1900;
405 my $month = localtime->mon() + 1; # months starts at 0, add 1
407 if ( !$budget || $budget eq 'now' ) {
411 # if month is july or more, budget start is 1 jul, next year.
412 elsif ( $month >= '7' ) {
413 ++$year; # add 1 to year , coz its next year
414 $budget = "$year-07-01";
418 # START OF NEW BUDGET, 1ST OF JULY, THIS YEAR
419 $budget = "$year-07-01";
422 if ( $sub eq 'yes' ) {
429 # if $basket empty, it's also a new basket, create it
431 $basketno = NewBasket( $booksellerid, $authorisedby );
434 my $dbh = C4::Context->dbh;
437 ( biblionumber, title, basketno, quantity, listprice,
438 notes, biblioitemnumber, rrp, ecost, gst,
439 unitprice, subscription, sort1, sort2, budgetdate,
440 entrydate, purchaseordernumber)
441 VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,COALESCE(?,NOW()),NOW(),? )
443 my $sth = $dbh->prepare($query);
446 $bibnum, $title, $basketno, $quantity, $listprice,
447 $notes, $bibitemnum, $rrp, $ecost, $gst,
448 $cost, $sub, $sort1, $sort2, $budget,
453 #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
454 my $ordnum = $dbh->{'mysql_insertid'};
456 INSERT INTO aqorderbreakdown (ordernumber,bookfundid, branchcode)
459 $sth = $dbh->prepare($query);
460 $sth->execute( $ordnum, $bookfund, $branchcode );
462 return ( $basketno, $ordnum );
465 #------------------------------------------------------------#
471 &ModOrder($title, $ordernumber, $quantity, $listprice,
472 $biblionumber, $basketno, $supplier, $who, $notes,
473 $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
474 $unitprice, $booksellerinvoicenumber, $branchcode);
476 Modifies an existing order. Updates the order with order number
477 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
478 update the fields with the same name in the aqorders table of the Koha
481 Entries with order number C<$ordernumber> in the aqorderbreakdown
482 table are also updated to the new book fund ID or branchcode.
490 $title, $ordnum, $quantity, $listprice, $bibnum,
491 $basketno, $supplier, $who, $notes, $bookfund,
492 $bibitemnum, $rrp, $ecost, $gst, $budget,
493 $cost, $invoice, $sort1, $sort2, $purchaseorder, $branchcode
496 # FIXME : Refactor to pass a hashref instead of fifty params.
497 my $dbh = C4::Context->dbh;
501 quantity=?,listprice=?,basketno=?,
502 rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
503 notes=?,sort1=?, sort2=?, purchaseordernumber=?
504 WHERE ordernumber=? AND biblionumber=?
506 my $sth = $dbh->prepare($query);
508 $title, $quantity, $listprice, $basketno, $rrp,
509 $ecost, $cost, $invoice, $notes, $sort1,
510 $sort2, $purchaseorder,
515 UPDATE aqorderbreakdown
516 SET bookfundid=?,branchcode=?
519 $sth = $dbh->prepare($query);
521 my $rv = $sth->execute( $bookfund,$branchcode, $ordnum );
522 unless($rv && ( $rv ne '0E0' )) { # zero rows affected [Bug 734]
524 INSERT INTO aqorderbreakdown
525 (ordernumber,branchcode,bookfundid)
528 $sth = $dbh->prepare($query);
529 $sth->execute( $ordnum,$branchcode, $bookfund );
534 #------------------------------------------------------------#
536 =head3 ModOrderBiblioNumber
540 &ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
542 Modifies the biblioitemnumber for an existing order.
543 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
549 sub ModOrderBiblioNumber {
550 my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
551 my $dbh = C4::Context->dbh;
554 SET biblioitemnumber = ?
555 WHERE ordernumber = ?
556 AND biblionumber = ?";
557 my $sth = $dbh->prepare($query);
558 $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
561 #------------------------------------------------------------#
563 =head3 ModReceiveOrder
567 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
568 $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
569 $freight, $bookfund, $rrp);
571 Updates an order, to reflect the fact that it was received, at least
572 in part. All arguments not mentioned below update the fields with the
573 same name in the aqorders table of the Koha database.
575 If a partial order is received, splits the order into two. The received
576 portion must have a booksellerinvoicenumber.
578 Updates the order with bibilionumber C<$biblionumber> and ordernumber
581 Also updates the book fund ID in the aqorderbreakdown table.
588 sub ModReceiveOrder {
590 $biblionumber, $ordnum, $quantrec, $user, $cost,
591 $invoiceno, $freight, $rrp, $bookfund, $datereceived
594 my $dbh = C4::Context->dbh;
595 # warn "DATE BEFORE : $daterecieved";
596 # $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
597 # warn "DATE REC : $daterecieved";
598 $datereceived = C4::Dates->output('iso') unless $datereceived;
599 my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
601 ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
603 # Allows libraries to change their bookfund during receiving orders
604 # allows them to adjust budgets
605 if ( C4::Context->preference("LooseBudgets") && $bookfund ) {
607 UPDATE aqorderbreakdown
611 my $sth = $dbh->prepare($query);
612 $sth->execute( $bookfund, $ordnum );
616 my $sth=$dbh->prepare("SELECT * FROM aqorders LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
617 WHERE biblionumber=? AND aqorders.ordernumber=?");
618 $sth->execute($biblionumber,$ordnum);
619 my $order = $sth->fetchrow_hashref();
622 if ( $order->{quantity} > $quantrec ) {
623 $sth=$dbh->prepare("update aqorders
624 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
625 unitprice=?,freight=?,rrp=?,quantity=?
626 where biblionumber=? and ordernumber=?");
627 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$quantrec,$biblionumber,$ordnum);
629 # create a new order for the remaining items, and set its bookfund.
630 my $newOrder = NewOrder($order->{'basketno'},$order->{'biblionumber'},$order->{'title'}, $order->{'quantity'} - $quantrec,
631 $order->{'listprice'},$order->{'booksellerid'},$order->{'authorisedby'},$order->{'notes'},
632 $order->{'bookfundid'},$order->{'biblioitemnumber'},$order->{'rrp'},$order->{'ecost'},$order->{'gst'},
633 $order->{'budget'},$order->{'unitcost'},$order->{'sub'},'',$order->{'sort1'},$order->{'sort2'},$order->{'purchaseordernumber'});
635 $sth=$dbh->prepare("update aqorders
636 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
637 unitprice=?,freight=?,rrp=?
638 where biblionumber=? and ordernumber=?");
639 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$biblionumber,$ordnum);
642 return $datereceived;
644 #------------------------------------------------------------#
648 @results = &SearchOrder($search, $biblionumber, $complete);
652 C<$search> may take one of several forms: if it is an ISBN,
653 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
654 order number, C<&ordersearch> returns orders with that order number
655 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
656 to be a space-separated list of search terms; in this case, all of the
657 terms must appear in the title (matching the beginning of title
660 If C<$complete> is C<yes>, the results will include only completed
661 orders. In any case, C<&ordersearch> ignores cancelled orders.
663 C<&ordersearch> returns an array.
664 C<@results> is an array of references-to-hash with the following keys:
681 my ( $search, $id, $biblionumber ) = @_;
682 my $dbh = C4::Context->dbh;
683 my @data = split( ' ', $search );
686 @searchterms = ($id);
688 map { push( @searchterms, "$_%", "%$_%" ) } @data;
689 push( @searchterms, $search, $search, $biblionumber );
691 ### FIXME THIS CAN raise a problem if more THAN ONE biblioitem is linked to one biblio
692 if(not $id and $biblionumber and $search){
693 $query = "SELECT *,biblio.title
695 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
696 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
697 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
698 WHERE ((datecancellationprinted is NULL)
699 OR (datecancellationprinted = '0000-00-00'))
700 AND aqorders.biblionumber = ?
701 AND aqorders.ordernumber = ?
703 @searchterms = ($biblionumber, $search);
707 "SELECT *,biblio.title
709 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
710 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
711 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
712 WHERE aqbasket.booksellerid = ?
713 AND ((datecancellationprinted is NULL)
714 OR (datecancellationprinted = '0000-00-00'))
718 map { "(biblio.title like ? or biblio.title like ?)" } @data )
720 . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
724 " SELECT *,biblio.title
726 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
727 LEFT JOIN aqbasket on aqorders.basketno=aqbasket.basketno
728 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
729 WHERE ((datecancellationprinted is NULL)
730 OR (datecancellationprinted = '0000-00-00'))
731 AND (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
735 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
737 . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
740 $query .= " GROUP BY aqorders.ordernumber";
742 my $sth = $dbh->prepare($query);
743 $sth->execute(@searchterms);
750 my $sth2 = $dbh->prepare($query2);
753 FROM aqorderbreakdown
756 my $sth3 = $dbh->prepare($query3);
758 while ( my $data = $sth->fetchrow_hashref ) {
759 $sth2->execute( $data->{'biblionumber'} );
760 my $data2 = $sth2->fetchrow_hashref;
761 $data->{'author'} = $data2->{'author'};
762 $data->{'seriestitle'} = $data2->{'seriestitle'};
763 $sth3->execute( $data->{'ordernumber'} );
764 my $data3 = $sth3->fetchrow_hashref;
765 $data->{'branchcode'} = $data3->{'branchcode'};
766 $data->{'bookfundid'} = $data3->{'bookfundid'};
767 push( @results, $data );
776 #------------------------------------------------------------#
782 &DelOrder($biblionumber, $ordernumber);
784 Cancel the order with the given order and biblio numbers. It does not
785 delete any entries in the aqorders table, it merely marks them as
793 my ( $bibnum, $ordnum ) = @_;
794 my $dbh = C4::Context->dbh;
797 SET datecancellationprinted=now()
798 WHERE biblionumber=? AND ordernumber=?
800 my $sth = $dbh->prepare($query);
801 $sth->execute( $bibnum, $ordnum );
805 =head2 FUNCTIONS ABOUT PARCELS
809 #------------------------------------------------------------#
815 @results = &GetParcel($booksellerid, $code, $date);
817 Looks up all of the received items from the supplier with the given
818 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
820 C<@results> is an array of references-to-hash. The keys of each element are fields from
821 the aqorders, biblio, and biblioitems tables of the Koha database.
823 C<@results> is sorted alphabetically by book title.
830 #gets all orders from a certain supplier, orders them alphabetically
831 my ( $supplierid, $code, $datereceived ) = @_;
832 my $dbh = C4::Context->dbh;
835 if $code; # add % if we search on a given code (otherwise, let him empty)
842 aqorders.biblionumber,
844 aqorders.ordernumber,
846 aqorders.quantityreceived,
852 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
853 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
855 aqbasket.booksellerid = ?
856 AND aqorders.booksellerinvoicenumber LIKE ?
857 AND aqorders.datereceived = ? ";
859 my @query_params = ( $supplierid, $code, $datereceived );
860 if ( C4::Context->preference("IndependantBranches") ) {
861 my $userenv = C4::Context->userenv;
862 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
863 $strsth .= " and (borrowers.branchcode = ?
864 or borrowers.branchcode = '')";
865 push @query_params, $userenv->{branch};
868 $strsth .= " ORDER BY aqbasket.basketno";
869 ### parcelinformation : $strsth
870 my $sth = $dbh->prepare($strsth);
871 $sth->execute( @query_params );
872 while ( my $data = $sth->fetchrow_hashref ) {
873 push( @results, $data );
875 ### countparcelbiblio: scalar(@results)
881 #------------------------------------------------------------#
887 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
888 get a lists of parcels.
897 is the bookseller this function has to get parcels.
900 To know on what criteria the results list has to be ordered.
903 is the booksellerinvoicenumber.
905 =item $datefrom & $dateto
906 to know on what date this function has to filter its search.
909 a pointer on a hash list containing parcel informations as such :
915 =item Number of biblio
917 =item Number of items
924 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
925 my $dbh = C4::Context->dbh;
926 my @query_params = ();
928 SELECT aqorders.booksellerinvoicenumber,
929 datereceived,purchaseordernumber,
930 count(DISTINCT biblionumber) AS biblio,
931 sum(quantity) AS itemsexpected,
932 sum(quantityreceived) AS itemsreceived
933 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
934 WHERE aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
937 if ( defined $code ) {
938 $strsth .= ' and aqorders.booksellerinvoicenumber like ? ';
939 # add a % to the end of the code to allow stemming.
940 push @query_params, "$code%";
943 if ( defined $datefrom ) {
944 $strsth .= ' and datereceived >= ? ';
945 push @query_params, $datefrom;
948 if ( defined $dateto ) {
949 $strsth .= 'and datereceived <= ? ';
950 push @query_params, $dateto;
953 $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
955 # can't use a placeholder to place this column name.
956 # but, we could probably be checking to make sure it is a column that will be fetched.
957 $strsth .= "order by $order " if ($order);
959 my $sth = $dbh->prepare($strsth);
961 $sth->execute( @query_params );
962 my $results = $sth->fetchall_arrayref({});
967 #------------------------------------------------------------#
973 @results = &GetLateOrders;
975 Searches for bookseller with late orders.
978 the table of supplier with late issues. This table is full of hashref.
986 my $supplierid = shift;
989 my $dbh = C4::Context->dbh;
991 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
992 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
994 my @query_params = ($delay); # delay is the first argument regardless
996 SELECT aqbasket.basketno,
997 aqorders.ordernumber,
998 DATE(aqbasket.closedate) AS orderdate,
999 aqorders.rrp AS unitpricesupplier,
1000 aqorders.ecost AS unitpricelib,
1001 aqbookfund.bookfundname AS budget,
1002 borrowers.branchcode AS branch,
1003 aqbooksellers.name AS supplier,
1006 biblioitems.publishercode AS publisher,
1007 biblioitems.publicationyear,
1011 (aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber)
1012 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber)
1013 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber = aqorderbreakdown.ordernumber)
1014 LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
1015 (aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber)
1016 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1017 WHERE aqorders.basketno = aqbasket.basketno
1018 AND ( (datereceived = '' OR datereceived IS NULL)
1019 OR (aqorders.quantityreceived < aqorders.quantity)
1023 if ($dbdriver eq "mysql") {
1025 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
1026 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1027 DATEDIFF(CURDATE( ),closedate) AS latesince
1029 $from .= " AND (closedate <= DATE_SUB(CURDATE( ),INTERVAL ? DAY)) ";
1031 HAVING quantity <> 0
1032 AND unitpricesupplier <> 0
1033 AND unitpricelib <> 0
1036 # FIXME: account for IFNULL as above
1038 aqorders.quantity AS quantity,
1039 aqorders.quantity * aqorders.rrp AS subtotal,
1040 (CURDATE - closedate) AS latesince
1042 $from .= " AND (closedate <= (CURDATE -(INTERVAL ? DAY)) ";
1044 if (defined $supplierid) {
1045 $from .= ' AND aqbasket.booksellerid = ? ';
1046 push @query_params, $supplierid;
1048 if (defined $branch) {
1049 $from .= ' AND borrowers.branchcode LIKE ? ';
1050 push @query_params, $branch;
1052 if (C4::Context->preference("IndependantBranches")
1053 && C4::Context->userenv
1054 && C4::Context->userenv->{flags} != 1 ) {
1055 $from .= ' AND borrowers.branchcode LIKE ? ';
1056 push @query_params, C4::Context->userenv->{branch};
1058 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
1059 $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
1060 my $sth = $dbh->prepare($query);
1061 $sth->execute(@query_params);
1063 while (my $data = $sth->fetchrow_hashref) {
1064 $data->{orderdate} = format_date($data->{orderdate});
1065 push @results, $data;
1070 #------------------------------------------------------------#
1076 (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on );
1078 Retreives some acquisition history information
1081 $order_loop is a list of hashrefs that each look like this:
1083 'author' => 'Twain, Mark',
1085 'biblionumber' => '215',
1087 'creationdate' => 'MM/DD/YYYY',
1088 'datereceived' => undef,
1091 'invoicenumber' => undef,
1093 'ordernumber' => '1',
1095 'quantityreceived' => undef,
1096 'title' => 'The Adventures of Huckleberry Finn'
1098 $total_qty is the sum of all of the quantities in $order_loop
1099 $total_price is the cost of each in $order_loop times the quantity
1100 $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
1107 my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
1110 my $total_qtyreceived = 0;
1111 my $total_price = 0;
1113 # don't run the query if there are no parameters (list would be too long for sure !)
1114 if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
1115 my $dbh = C4::Context->dbh;
1121 name,aqbasket.creationdate,
1122 aqorders.datereceived,
1124 aqorders.quantityreceived,
1126 aqorders.ordernumber,
1127 aqorders.booksellerinvoicenumber as invoicenumber,
1128 aqbooksellers.id as id,
1129 aqorders.biblionumber
1131 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
1132 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
1133 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
1135 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
1136 if ( C4::Context->preference("IndependantBranches") );
1138 $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
1140 my @query_params = ();
1142 if ( defined $title ) {
1143 $query .= " AND biblio.title LIKE ? ";
1144 push @query_params, "%$title%";
1147 if ( defined $author ) {
1148 $query .= " AND biblio.author LIKE ? ";
1149 push @query_params, "%$author%";
1152 if ( defined $name ) {
1153 $query .= " AND name LIKE ? ";
1154 push @query_params, "%$name%";
1157 if ( defined $from_placed_on ) {
1158 $query .= " AND creationdate >= ? ";
1159 push @query_params, $from_placed_on;
1162 if ( defined $to_placed_on ) {
1163 $query .= " AND creationdate <= ? ";
1164 push @query_params, $to_placed_on;
1167 if ( C4::Context->preference("IndependantBranches") ) {
1168 my $userenv = C4::Context->userenv;
1169 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1170 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
1171 push @query_params, $userenv->{branch};
1174 $query .= " ORDER BY booksellerid";
1175 my $sth = $dbh->prepare($query);
1176 $sth->execute( @query_params );
1178 while ( my $line = $sth->fetchrow_hashref ) {
1179 $line->{count} = $cnt++;
1180 $line->{toggle} = 1 if $cnt % 2;
1181 push @order_loop, $line;
1182 $line->{creationdate} = format_date( $line->{creationdate} );
1183 $line->{datereceived} = format_date( $line->{datereceived} );
1184 $total_qty += $line->{'quantity'};
1185 $total_qtyreceived += $line->{'quantityreceived'};
1186 $total_price += $line->{'quantity'} * $line->{'ecost'};
1189 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
1192 =head2 GetRecentAcqui
1194 $results = GetRecentAcqui($days);
1196 C<$results> is a ref to a table which containts hashref
1200 sub GetRecentAcqui {
1202 my $dbh = C4::Context->dbh;
1206 ORDER BY timestamp DESC
1209 my $sth = $dbh->prepare($query);
1212 while(my $data = $sth->fetchrow_hashref){
1213 push @results,$data;
1223 Koha Developement team <info@koha.org>