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 (quantity > quantityreceived OR quantityreceived is NULL)
210 AND datecancellationprinted IS NULL
211 AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
214 $strsth .= " AND closedate IS NOT NULL ";
216 ## FIXME Why 180 days ???
217 my @query_params = ( $supplierid );
218 if ( C4::Context->preference("IndependantBranches") ) {
219 my $userenv = C4::Context->userenv;
220 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
221 $strsth .= " and (borrowers.branchcode = ?
222 or borrowers.branchcode = '')";
223 push @query_params, $userenv->{branch};
226 $strsth .= " group by aqbasket.basketno" if $grouped;
227 $strsth .= " order by aqbasket.basketno";
229 my $sth = $dbh->prepare($strsth);
230 $sth->execute( @query_params );
231 my $results = $sth->fetchall_arrayref({});
236 #------------------------------------------------------------#
242 @orders = &GetOrders($basketnumber, $orderby);
244 Looks up the pending (non-cancelled) orders with the given basket
245 number. If C<$booksellerID> is non-empty, only orders from that seller
249 C<&basket> returns a two-element array. C<@orders> is an array of
250 references-to-hash, whose keys are the fields from the aqorders,
251 biblio, and biblioitems tables in the Koha database.
258 my ( $basketno, $orderby ) = @_;
259 my $dbh = C4::Context->dbh;
261 SELECT aqorderbreakdown.*,
262 biblio.*,biblioitems.*,
264 aqbookfund.bookfundname,
267 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
268 LEFT JOIN aqbookfund ON aqbookfund.bookfundid=aqorderbreakdown.bookfundid
269 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
270 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
272 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
275 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
276 $query .= " ORDER BY $orderby";
277 my $sth = $dbh->prepare($query);
278 $sth->execute($basketno);
281 while ( my $data = $sth->fetchrow_hashref ) {
282 push @results, $data;
288 #------------------------------------------------------------#
290 =head3 GetOrderNumber
294 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
298 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
300 Returns the number of this order.
304 =item C<$ordernumber> is the order number.
310 my ( $biblionumber,$biblioitemnumber ) = @_;
311 my $dbh = C4::Context->dbh;
316 AND biblioitemnumber=?
318 my $sth = $dbh->prepare($query);
319 $sth->execute( $biblionumber, $biblioitemnumber );
321 return $sth->fetchrow;
324 #------------------------------------------------------------#
330 $order = &GetOrder($ordernumber);
332 Looks up an order by order number.
334 Returns a reference-to-hash describing the order. The keys of
335 C<$order> are fields from the biblio, biblioitems, aqorders, and
336 aqorderbreakdown tables of the Koha database.
344 my $dbh = C4::Context->dbh;
346 SELECT biblioitems.*, biblio.*, aqorderbreakdown.*, aqorders.*
348 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
349 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
350 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
351 WHERE aqorders.ordernumber=?
354 my $sth= $dbh->prepare($query);
355 $sth->execute($ordnum);
356 my $data = $sth->fetchrow_hashref;
361 #------------------------------------------------------------#
367 &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
368 $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
369 $ecost, $gst, $budget, $unitprice, $subscription,
370 $booksellerinvoicenumber, $purchaseorder, $branchcode);
372 Adds a new order to the database. Any argument that isn't described
373 below is the new value of the field with the same name in the aqorders
374 table of the Koha database.
376 C<$ordnum> is a "minimum order number." After adding the new entry to
377 the aqorders table, C<&neworder> finds the first entry in aqorders
378 with order number greater than or equal to C<$ordnum>, and adds an
379 entry to the aqorderbreakdown table, with the order number just found,
380 and the book fund ID of the newly-added order.
382 C<$budget> is effectively ignored.
383 If it's undef (anything false) or the string 'now', the current day is used.
384 Else, the upcoming July 1st is used.
386 C<$subscription> may be either "yes", or anything else for "no".
394 $basketno, $bibnum, $title, $quantity,
395 $listprice, $booksellerid, $authorisedby, $notes,
396 $bookfund, $bibitemnum, $rrp, $ecost,
397 $gst, $budget, $cost, $sub,
398 $invoice, $sort1, $sort2, $purchaseorder,
403 my $year = localtime->year() + 1900;
404 my $month = localtime->mon() + 1; # months starts at 0, add 1
406 if ( !$budget || $budget eq 'now' ) {
410 # if month is july or more, budget start is 1 jul, next year.
411 elsif ( $month >= '7' ) {
412 ++$year; # add 1 to year , coz its next year
413 $budget = "$year-07-01";
417 # START OF NEW BUDGET, 1ST OF JULY, THIS YEAR
418 $budget = "$year-07-01";
421 if ( $sub eq 'yes' ) {
428 # if $basket empty, it's also a new basket, create it
430 $basketno = NewBasket( $booksellerid, $authorisedby );
433 my $dbh = C4::Context->dbh;
436 ( biblionumber, title, basketno, quantity, listprice,
437 notes, biblioitemnumber, rrp, ecost, gst,
438 unitprice, subscription, sort1, sort2, budgetdate,
439 entrydate, purchaseordernumber)
440 VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,COALESCE(?,NOW()),NOW(),? )
442 my $sth = $dbh->prepare($query);
445 $bibnum, $title, $basketno, $quantity, $listprice,
446 $notes, $bibitemnum, $rrp, $ecost, $gst,
447 $cost, $sub, $sort1, $sort2, $budget,
452 #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
453 my $ordnum = $dbh->{'mysql_insertid'};
455 INSERT INTO aqorderbreakdown (ordernumber,bookfundid, branchcode)
458 $sth = $dbh->prepare($query);
459 $sth->execute( $ordnum, $bookfund, $branchcode );
461 return ( $basketno, $ordnum );
464 #------------------------------------------------------------#
470 &ModOrder($title, $ordernumber, $quantity, $listprice,
471 $biblionumber, $basketno, $supplier, $who, $notes,
472 $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
473 $unitprice, $booksellerinvoicenumber, $branchcode);
475 Modifies an existing order. Updates the order with order number
476 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
477 update the fields with the same name in the aqorders table of the Koha
480 Entries with order number C<$ordernumber> in the aqorderbreakdown
481 table are also updated to the new book fund ID or branchcode.
489 $title, $ordnum, $quantity, $listprice, $bibnum,
490 $basketno, $supplier, $who, $notes, $bookfund,
491 $bibitemnum, $rrp, $ecost, $gst, $budget,
492 $cost, $invoice, $sort1, $sort2, $purchaseorder, $branchcode
495 # FIXME : Refactor to pass a hashref instead of fifty params.
496 my $dbh = C4::Context->dbh;
500 quantity=?,listprice=?,basketno=?,
501 rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
502 notes=?,sort1=?, sort2=?, purchaseordernumber=?
503 WHERE ordernumber=? AND biblionumber=?
505 my $sth = $dbh->prepare($query);
507 $title, $quantity, $listprice, $basketno, $rrp,
508 $ecost, $cost, $invoice, $notes, $sort1,
509 $sort2, $purchaseorder,
514 UPDATE aqorderbreakdown
515 SET bookfundid=?,branchcode=?
518 $sth = $dbh->prepare($query);
520 my $rv = $sth->execute( $bookfund,$branchcode, $ordnum );
521 unless($rv && ( $rv ne '0E0' )) { # zero rows affected [Bug 734]
523 INSERT INTO aqorderbreakdown
524 (ordernumber,branchcode,bookfundid)
527 $sth = $dbh->prepare($query);
528 $sth->execute( $ordnum,$branchcode, $bookfund );
533 #------------------------------------------------------------#
535 =head3 ModOrderBiblioNumber
539 &ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
541 Modifies the biblioitemnumber for an existing order.
542 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
548 sub ModOrderBiblioNumber {
549 my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
550 my $dbh = C4::Context->dbh;
553 SET biblioitemnumber = ?
554 WHERE ordernumber = ?
555 AND biblionumber = ?";
556 my $sth = $dbh->prepare($query);
557 $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
560 #------------------------------------------------------------#
562 =head3 ModReceiveOrder
566 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
567 $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
568 $freight, $bookfund, $rrp);
570 Updates an order, to reflect the fact that it was received, at least
571 in part. All arguments not mentioned below update the fields with the
572 same name in the aqorders table of the Koha database.
574 If a partial order is received, splits the order into two. The received
575 portion must have a booksellerinvoicenumber.
577 Updates the order with bibilionumber C<$biblionumber> and ordernumber
580 Also updates the book fund ID in the aqorderbreakdown table.
587 sub ModReceiveOrder {
589 $biblionumber, $ordnum, $quantrec, $user, $cost,
590 $invoiceno, $freight, $rrp, $bookfund, $datereceived
593 my $dbh = C4::Context->dbh;
594 # warn "DATE BEFORE : $daterecieved";
595 # $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
596 # warn "DATE REC : $daterecieved";
597 $datereceived = C4::Dates->output('iso') unless $datereceived;
598 my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
600 ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
602 # Allows libraries to change their bookfund during receiving orders
603 # allows them to adjust budgets
604 if ( C4::Context->preference("LooseBudgets") && $bookfund ) {
606 UPDATE aqorderbreakdown
610 my $sth = $dbh->prepare($query);
611 $sth->execute( $bookfund, $ordnum );
615 my $sth=$dbh->prepare("SELECT * FROM aqorders LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
616 WHERE biblionumber=? AND aqorders.ordernumber=?");
617 $sth->execute($biblionumber,$ordnum);
618 my $order = $sth->fetchrow_hashref();
621 if ( $order->{quantity} > $quantrec ) {
622 $sth=$dbh->prepare("update aqorders
623 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
624 unitprice=?,freight=?,rrp=?,quantity=?
625 where biblionumber=? and ordernumber=?");
626 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$quantrec,$biblionumber,$ordnum);
628 # create a new order for the remaining items, and set its bookfund.
629 my $newOrder = NewOrder($order->{'basketno'},$order->{'biblionumber'},$order->{'title'}, $order->{'quantity'} - $quantrec,
630 $order->{'listprice'},$order->{'booksellerid'},$order->{'authorisedby'},$order->{'notes'},
631 $order->{'bookfundid'},$order->{'biblioitemnumber'},$order->{'rrp'},$order->{'ecost'},$order->{'gst'},
632 $order->{'budget'},$order->{'unitcost'},$order->{'sub'},'',$order->{'sort1'},$order->{'sort2'},$order->{'purchaseordernumber'});
634 $sth=$dbh->prepare("update aqorders
635 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
636 unitprice=?,freight=?,rrp=?
637 where biblionumber=? and ordernumber=?");
638 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$biblionumber,$ordnum);
641 return $datereceived;
643 #------------------------------------------------------------#
647 @results = &SearchOrder($search, $biblionumber, $complete);
651 C<$search> may take one of several forms: if it is an ISBN,
652 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
653 order number, C<&ordersearch> returns orders with that order number
654 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
655 to be a space-separated list of search terms; in this case, all of the
656 terms must appear in the title (matching the beginning of title
659 If C<$complete> is C<yes>, the results will include only completed
660 orders. In any case, C<&ordersearch> ignores cancelled orders.
662 C<&ordersearch> returns an array.
663 C<@results> is an array of references-to-hash with the following keys:
680 my ( $search, $id, $biblionumber, $catview ) = @_;
681 my $dbh = C4::Context->dbh;
682 my @data = split( ' ', $search );
685 @searchterms = ($id);
687 map { push( @searchterms, "$_%", "%$_%" ) } @data;
688 push( @searchterms, $search, $search, $biblionumber );
690 ### FIXME THIS CAN raise a problem if more THAN ONE biblioitem is linked to one biblio
692 @searchterms = ($id, $search);
694 "SELECT *,biblio.title
696 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
697 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
698 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
699 WHERE aqbasket.booksellerid = ? AND aqorders.ordernumber = ?
703 "SELECT *,biblio.title
705 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
706 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
707 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
708 WHERE aqbasket.booksellerid = ?
709 AND ((datecancellationprinted is NULL)
710 OR (datecancellationprinted = '0000-00-00'))
714 map { "(biblio.title like ? or biblio.title like ?)" } @data )
716 . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
721 " SELECT *,biblio.title
723 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
724 LEFT JOIN aqbasket on aqorders.basketno=aqbasket.basketno
725 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
726 WHERE ((datecancellationprinted is NULL)
727 OR (datecancellationprinted = '0000-00-00'))
728 AND (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
732 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
734 . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
737 if ( $biblionumber ) {
738 $query .= "AND biblio.biblionumber = ? ";
739 push (@searchterms, $biblionumber);
742 $query .= " GROUP BY aqorders.ordernumber";
744 my $sth = $dbh->prepare($query);
745 $sth->execute(@searchterms);
752 my $sth2 = $dbh->prepare($query2);
755 FROM aqorderbreakdown
758 my $sth3 = $dbh->prepare($query3);
760 while ( my $data = $sth->fetchrow_hashref ) {
761 $sth2->execute( $data->{'biblionumber'} );
762 my $data2 = $sth2->fetchrow_hashref;
763 $data->{'author'} = $data2->{'author'};
764 $data->{'seriestitle'} = $data2->{'seriestitle'};
765 $sth3->execute( $data->{'ordernumber'} );
766 my $data3 = $sth3->fetchrow_hashref;
767 $data->{'branchcode'} = $data3->{'branchcode'};
768 $data->{'bookfundid'} = $data3->{'bookfundid'};
769 push( @results, $data );
778 #------------------------------------------------------------#
784 &DelOrder($biblionumber, $ordernumber);
786 Cancel the order with the given order and biblio numbers. It does not
787 delete any entries in the aqorders table, it merely marks them as
795 my ( $bibnum, $ordnum ) = @_;
796 my $dbh = C4::Context->dbh;
799 SET datecancellationprinted=now()
800 WHERE biblionumber=? AND ordernumber=?
802 my $sth = $dbh->prepare($query);
803 $sth->execute( $bibnum, $ordnum );
807 =head2 FUNCTIONS ABOUT PARCELS
811 #------------------------------------------------------------#
817 @results = &GetParcel($booksellerid, $code, $date);
819 Looks up all of the received items from the supplier with the given
820 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
822 C<@results> is an array of references-to-hash. The keys of each element are fields from
823 the aqorders, biblio, and biblioitems tables of the Koha database.
825 C<@results> is sorted alphabetically by book title.
832 #gets all orders from a certain supplier, orders them alphabetically
833 my ( $supplierid, $code, $datereceived ) = @_;
834 my $dbh = C4::Context->dbh;
837 if $code; # add % if we search on a given code (otherwise, let him empty)
844 aqorders.biblionumber,
846 aqorders.ordernumber,
848 aqorders.quantityreceived,
854 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
855 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
857 aqbasket.booksellerid = ?
858 AND aqorders.booksellerinvoicenumber LIKE ?
859 AND aqorders.datereceived = ? ";
861 my @query_params = ( $supplierid, $code, $datereceived );
862 if ( C4::Context->preference("IndependantBranches") ) {
863 my $userenv = C4::Context->userenv;
864 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
865 $strsth .= " and (borrowers.branchcode = ?
866 or borrowers.branchcode = '')";
867 push @query_params, $userenv->{branch};
870 $strsth .= " ORDER BY aqbasket.basketno";
871 ### parcelinformation : $strsth
872 my $sth = $dbh->prepare($strsth);
873 $sth->execute( @query_params );
874 while ( my $data = $sth->fetchrow_hashref ) {
875 push( @results, $data );
877 ### countparcelbiblio: scalar(@results)
883 #------------------------------------------------------------#
889 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
890 get a lists of parcels.
899 is the bookseller this function has to get parcels.
902 To know on what criteria the results list has to be ordered.
905 is the booksellerinvoicenumber.
907 =item $datefrom & $dateto
908 to know on what date this function has to filter its search.
911 a pointer on a hash list containing parcel informations as such :
917 =item Number of biblio
919 =item Number of items
926 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
927 my $dbh = C4::Context->dbh;
928 my @query_params = ();
930 SELECT aqorders.booksellerinvoicenumber,
931 datereceived,purchaseordernumber,
932 count(DISTINCT biblionumber) AS biblio,
933 sum(quantity) AS itemsexpected,
934 sum(quantityreceived) AS itemsreceived
935 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
936 WHERE aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
939 if ( defined $code ) {
940 $strsth .= ' and aqorders.booksellerinvoicenumber like ? ';
941 # add a % to the end of the code to allow stemming.
942 push @query_params, "$code%";
945 if ( defined $datefrom ) {
946 $strsth .= ' and datereceived >= ? ';
947 push @query_params, $datefrom;
950 if ( defined $dateto ) {
951 $strsth .= 'and datereceived <= ? ';
952 push @query_params, $dateto;
955 $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
957 # can't use a placeholder to place this column name.
958 # but, we could probably be checking to make sure it is a column that will be fetched.
959 $strsth .= "order by $order " if ($order);
961 my $sth = $dbh->prepare($strsth);
963 $sth->execute( @query_params );
964 my $results = $sth->fetchall_arrayref({});
969 #------------------------------------------------------------#
975 @results = &GetLateOrders;
977 Searches for bookseller with late orders.
980 the table of supplier with late issues. This table is full of hashref.
988 my $supplierid = shift;
991 my $dbh = C4::Context->dbh;
993 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
994 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
996 my @query_params = ($delay); # delay is the first argument regardless
998 SELECT aqbasket.basketno,
999 aqorders.ordernumber,
1000 DATE(aqbasket.closedate) AS orderdate,
1001 aqorders.rrp AS unitpricesupplier,
1002 aqorders.ecost AS unitpricelib,
1003 aqbookfund.bookfundname AS budget,
1004 borrowers.branchcode AS branch,
1005 aqbooksellers.name AS supplier,
1008 biblioitems.publishercode AS publisher,
1009 biblioitems.publicationyear,
1013 (aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber)
1014 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber)
1015 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber = aqorderbreakdown.ordernumber)
1016 LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
1017 (aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber)
1018 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1019 WHERE aqorders.basketno = aqbasket.basketno
1020 AND ( (datereceived = '' OR datereceived IS NULL)
1021 OR (aqorders.quantityreceived < aqorders.quantity)
1025 if ($dbdriver eq "mysql") {
1027 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
1028 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1029 DATEDIFF(CURDATE( ),closedate) AS latesince
1031 $from .= " AND (closedate <= DATE_SUB(CURDATE( ),INTERVAL ? DAY)) ";
1033 HAVING quantity <> 0
1034 AND unitpricesupplier <> 0
1035 AND unitpricelib <> 0
1038 # FIXME: account for IFNULL as above
1040 aqorders.quantity AS quantity,
1041 aqorders.quantity * aqorders.rrp AS subtotal,
1042 (CURDATE - closedate) AS latesince
1044 $from .= " AND (closedate <= (CURDATE -(INTERVAL ? DAY)) ";
1046 if (defined $supplierid) {
1047 $from .= ' AND aqbasket.booksellerid = ? ';
1048 push @query_params, $supplierid;
1050 if (defined $branch) {
1051 $from .= ' AND borrowers.branchcode LIKE ? ';
1052 push @query_params, $branch;
1054 if (C4::Context->preference("IndependantBranches")
1055 && C4::Context->userenv
1056 && C4::Context->userenv->{flags} != 1 ) {
1057 $from .= ' AND borrowers.branchcode LIKE ? ';
1058 push @query_params, C4::Context->userenv->{branch};
1060 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
1061 $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
1062 my $sth = $dbh->prepare($query);
1063 $sth->execute(@query_params);
1065 while (my $data = $sth->fetchrow_hashref) {
1066 $data->{orderdate} = format_date($data->{orderdate});
1067 push @results, $data;
1072 #------------------------------------------------------------#
1078 (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on );
1080 Retreives some acquisition history information
1083 $order_loop is a list of hashrefs that each look like this:
1085 'author' => 'Twain, Mark',
1087 'biblionumber' => '215',
1089 'creationdate' => 'MM/DD/YYYY',
1090 'datereceived' => undef,
1093 'invoicenumber' => undef,
1095 'ordernumber' => '1',
1097 'quantityreceived' => undef,
1098 'title' => 'The Adventures of Huckleberry Finn'
1100 $total_qty is the sum of all of the quantities in $order_loop
1101 $total_price is the cost of each in $order_loop times the quantity
1102 $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
1109 my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
1112 my $total_qtyreceived = 0;
1113 my $total_price = 0;
1115 # don't run the query if there are no parameters (list would be too long for sure !)
1116 if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
1117 my $dbh = C4::Context->dbh;
1123 name,aqbasket.creationdate,
1124 aqorders.datereceived,
1126 aqorders.quantityreceived,
1128 aqorders.ordernumber,
1129 aqorders.booksellerinvoicenumber as invoicenumber,
1130 aqbooksellers.id as id,
1131 aqorders.biblionumber
1133 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
1134 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
1135 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
1137 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
1138 if ( C4::Context->preference("IndependantBranches") );
1140 $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
1142 my @query_params = ();
1144 if ( defined $title ) {
1145 $query .= " AND biblio.title LIKE ? ";
1146 push @query_params, "%$title%";
1149 if ( defined $author ) {
1150 $query .= " AND biblio.author LIKE ? ";
1151 push @query_params, "%$author%";
1154 if ( defined $name ) {
1155 $query .= " AND name LIKE ? ";
1156 push @query_params, "%$name%";
1159 if ( defined $from_placed_on ) {
1160 $query .= " AND creationdate >= ? ";
1161 push @query_params, $from_placed_on;
1164 if ( defined $to_placed_on ) {
1165 $query .= " AND creationdate <= ? ";
1166 push @query_params, $to_placed_on;
1169 if ( C4::Context->preference("IndependantBranches") ) {
1170 my $userenv = C4::Context->userenv;
1171 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1172 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
1173 push @query_params, $userenv->{branch};
1176 $query .= " ORDER BY booksellerid";
1177 my $sth = $dbh->prepare($query);
1178 $sth->execute( @query_params );
1180 while ( my $line = $sth->fetchrow_hashref ) {
1181 $line->{count} = $cnt++;
1182 $line->{toggle} = 1 if $cnt % 2;
1183 push @order_loop, $line;
1184 $line->{creationdate} = format_date( $line->{creationdate} );
1185 $line->{datereceived} = format_date( $line->{datereceived} );
1186 $total_qty += $line->{'quantity'};
1187 $total_qtyreceived += $line->{'quantityreceived'};
1188 $total_price += $line->{'quantity'} * $line->{'ecost'};
1191 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
1194 =head2 GetRecentAcqui
1196 $results = GetRecentAcqui($days);
1198 C<$results> is a ref to a table which containts hashref
1202 sub GetRecentAcqui {
1204 my $dbh = C4::Context->dbh;
1208 ORDER BY timestamp DESC
1211 my $sth = $dbh->prepare($query);
1214 while(my $data = $sth->fetchrow_hashref){
1215 push @results,$data;
1225 Koha Developement team <info@koha.org>