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
23 use C4::Dates qw(format_date);
28 use vars qw($VERSION @ISA @EXPORT);
31 # set the version for version checking
36 &GetBasket &NewBasket &CloseBasket
37 &GetPendingOrders &GetOrder &GetOrders
38 &GetOrderNumber &GetLateOrders &NewOrder &DelOrder
39 &SearchOrder &GetHistory &GetRecentAcqui
40 &ModOrder &ModReceiveOrder &ModOrderBiblioNumber
41 &GetParcels &GetParcel
45 # used in receiveorder subroutine
46 # to provide library specific handling
47 my $library_name = C4::Context->preference("LibraryName");
51 C4::Acquisition - Koha functions for dealing with orders and acquisitions
59 The functions in this module deal with acquisitions, managing book
60 orders, basket and parcels.
66 =head2 FUNCTIONS ABOUT BASKETS
74 $aqbasket = &GetBasket($basketnumber);
76 get all basket informations in aqbasket for a given basket
79 informations for a given basket returned as a hashref.
89 my $dbh = C4::Context->dbh;
92 concat( b.firstname,' ',b.surname) AS authorisedbyname,
93 b.branchcode AS branch
95 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
98 my $sth=$dbh->prepare($query);
99 $sth->execute($basketno);
100 my $basket = $sth->fetchrow_hashref;
104 #------------------------------------------------------------#
110 $basket = &NewBasket();
112 Create a new basket in aqbasket table
118 # FIXME : this function seems to be unused.
121 my ( $booksellerid, $authorisedby ) = @_;
122 my $dbh = C4::Context->dbh;
125 (creationdate,booksellerid,authorisedby)
126 VALUES (now(),'$booksellerid','$authorisedby')
131 #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
132 my $basket = $dbh->{'mysql_insertid'};
136 #------------------------------------------------------------#
142 &CloseBasket($basketno);
144 close a basket (becomes unmodifiable,except for recieves)
152 my $dbh = C4::Context->dbh;
158 my $sth = $dbh->prepare($query);
159 $sth->execute($basketno);
162 #------------------------------------------------------------#
166 =head2 FUNCTIONS ABOUT ORDERS
172 #------------------------------------------------------------#
174 =head3 GetPendingOrders
178 $orders = &GetPendingOrders($booksellerid, $grouped);
180 Finds pending orders from the bookseller with the given ID. Ignores
181 completed and cancelled orders.
183 C<$orders> is a reference-to-array; each element is a
184 reference-to-hash with the following fields:
185 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
186 in a single result line
190 =item C<authorizedby>
196 These give the value of the corresponding field in the aqorders table
197 of the Koha database.
203 Results are ordered from most to least recent.
207 sub GetPendingOrders {
208 my ($supplierid,$grouped) = @_;
209 my $dbh = C4::Context->dbh;
211 SELECT ".($grouped?"count(*),":"")."aqbasket.basketno,
212 surname,firstname,aqorders.*,
213 aqbasket.closedate, aqbasket.creationdate
215 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
216 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
218 AND (quantity > quantityreceived OR quantityreceived is NULL)
219 AND datecancellationprinted IS NULL
220 AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
222 ## FIXME Why 180 days ???
223 my @query_params = ( $supplierid );
224 if ( C4::Context->preference("IndependantBranches") ) {
225 my $userenv = C4::Context->userenv;
226 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
227 $strsth .= " and (borrowers.branchcode = ?
228 or borrowers.branchcode = '')";
229 push @query_params, $userenv->{branch};
232 $strsth .= " group by aqbasket.basketno" if $grouped;
233 $strsth .= " order by aqbasket.basketno";
235 my $sth = $dbh->prepare($strsth);
236 $sth->execute( @query_params );
237 my $results = $sth->fetchall_arrayref({});
242 #------------------------------------------------------------#
248 @orders = &GetOrders($basketnumber, $orderby);
250 Looks up the pending (non-cancelled) orders with the given basket
251 number. If C<$booksellerID> is non-empty, only orders from that seller
255 C<&basket> returns a two-element array. C<@orders> is an array of
256 references-to-hash, whose keys are the fields from the aqorders,
257 biblio, and biblioitems tables in the Koha database.
264 my ( $basketno, $orderby ) = @_;
265 my $dbh = C4::Context->dbh;
267 SELECT aqorderbreakdown.*,
268 biblio.*,biblioitems.publishercode,
270 aqbookfund.bookfundname,
273 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
274 LEFT JOIN aqbookfund ON aqbookfund.bookfundid=aqorderbreakdown.bookfundid
275 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
276 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
278 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
281 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
282 $query .= " ORDER BY $orderby";
283 my $sth = $dbh->prepare($query);
284 $sth->execute($basketno);
287 while ( my $data = $sth->fetchrow_hashref ) {
288 push @results, $data;
294 #------------------------------------------------------------#
296 =head3 GetOrderNumber
300 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
302 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
304 Returns the number of this order.
306 =item C<$ordernumber> is the order number.
312 my ( $biblionumber,$biblioitemnumber ) = @_;
313 my $dbh = C4::Context->dbh;
318 AND biblioitemnumber=?
320 my $sth = $dbh->prepare($query);
321 $sth->execute( $biblionumber, $biblioitemnumber );
323 return $sth->fetchrow;
326 #------------------------------------------------------------#
332 $order = &GetOrder($ordernumber);
334 Looks up an order by order number.
336 Returns a reference-to-hash describing the order. The keys of
337 C<$order> are fields from the biblio, biblioitems, aqorders, and
338 aqorderbreakdown tables of the Koha database.
346 my $dbh = C4::Context->dbh;
350 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
351 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
352 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
353 WHERE aqorders.ordernumber=?
356 my $sth= $dbh->prepare($query);
357 $sth->execute($ordnum);
358 my $data = $sth->fetchrow_hashref;
363 #------------------------------------------------------------#
369 &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
370 $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
371 $ecost, $gst, $budget, $unitprice, $subscription,
372 $booksellerinvoicenumber, $purchaseorder);
374 Adds a new order to the database. Any argument that isn't described
375 below is the new value of the field with the same name in the aqorders
376 table of the Koha database.
378 C<$ordnum> is a "minimum order number." After adding the new entry to
379 the aqorders table, C<&neworder> finds the first entry in aqorders
380 with order number greater than or equal to C<$ordnum>, and adds an
381 entry to the aqorderbreakdown table, with the order number just found,
382 and the book fund ID of the newly-added order.
384 C<$budget> is effectively ignored.
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
402 my $year = localtime->year() + 1900;
403 my $month = localtime->mon() + 1; # months starts at 0, add 1
405 if ( !$budget || $budget eq 'now' ) {
409 # if month is july or more, budget start is 1 jul, next year.
410 elsif ( $month >= '7' ) {
411 ++$year; # add 1 to year , coz its next year
412 $budget = "'$year-07-01'";
416 # START OF NEW BUDGET, 1ST OF JULY, THIS YEAR
417 $budget = "'$year-07-01'";
420 if ( $sub eq 'yes' ) {
427 # if $basket empty, it's also a new basket, create it
429 $basketno = NewBasket( $booksellerid, $authorisedby );
432 my $dbh = C4::Context->dbh;
435 ( biblionumber,title,basketno,quantity,listprice,notes,
436 biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate,purchaseordernumber)
437 VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,$budget,now(),? )
439 my $sth = $dbh->prepare($query);
442 $bibnum, $title, $basketno, $quantity, $listprice,
443 $notes, $bibitemnum, $rrp, $ecost, $gst,
444 $cost, $sub, $sort1, $sort2, $purchaseorder
448 #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
449 my $ordnum = $dbh->{'mysql_insertid'};
451 INSERT INTO aqorderbreakdown (ordernumber,bookfundid)
454 $sth = $dbh->prepare($query);
455 $sth->execute( $ordnum, $bookfund );
457 return ( $basketno, $ordnum );
460 #------------------------------------------------------------#
466 &ModOrder($title, $ordernumber, $quantity, $listprice,
467 $biblionumber, $basketno, $supplier, $who, $notes,
468 $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
469 $unitprice, $booksellerinvoicenumber);
471 Modifies an existing order. Updates the order with order number
472 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
473 update the fields with the same name in the aqorders table of the Koha
476 Entries with order number C<$ordernumber> in the aqorderbreakdown
477 table are also updated to the new book fund ID.
485 $title, $ordnum, $quantity, $listprice, $bibnum,
486 $basketno, $supplier, $who, $notes, $bookfund,
487 $bibitemnum, $rrp, $ecost, $gst, $budget,
488 $cost, $invoice, $sort1, $sort2, $purchaseorder
491 my $dbh = C4::Context->dbh;
495 quantity=?,listprice=?,basketno=?,
496 rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
497 notes=?,sort1=?, sort2=?, purchaseordernumber=?
498 WHERE ordernumber=? AND biblionumber=?
500 my $sth = $dbh->prepare($query);
502 $title, $quantity, $listprice, $basketno, $rrp,
503 $ecost, $cost, $invoice, $notes, $sort1,
504 $sort2, $purchaseorder,
510 UPDATE aqorderbreakdown
511 SET bookfundid=?,branchcode=?
514 $sth = $dbh->prepare($query);
516 unless ( $sth->execute( $bookfund,$branchcode, $ordnum ) )
517 { # zero rows affected [Bug 734]
519 INSERT INTO aqorderbreakdown
520 (ordernumber,branchcode,bookfundid)
523 $sth = $dbh->prepare($query);
524 $sth->execute( $ordnum,$branchcode, $bookfund );
529 #------------------------------------------------------------#
531 =head3 ModOrderBiblioNumber
535 &ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
537 Modifies the biblioitemnumber for an existing order.
538 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
544 sub ModOrderBiblioNumber {
545 my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
546 my $dbh = C4::Context->dbh;
549 SET biblioitemnumber = ?
550 WHERE ordernumber = ?
551 AND biblionumber = ?";
552 my $sth = $dbh->prepare($query);
553 $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
556 #------------------------------------------------------------#
558 =head3 ModReceiveOrder
562 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
563 $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
564 $freight, $bookfund, $rrp);
566 Updates an order, to reflect the fact that it was received, at least
567 in part. All arguments not mentioned below update the fields with the
568 same name in the aqorders table of the Koha database.
570 If a partial order is received, splits the order into two. The received
571 portion must have a booksellerinvoicenumber.
573 Updates the order with bibilionumber C<$biblionumber> and ordernumber
576 Also updates the book fund ID in the aqorderbreakdown table.
583 sub ModReceiveOrder {
585 $biblionumber, $ordnum, $quantrec, $user, $cost,
586 $invoiceno, $freight, $rrp, $bookfund, $datereceived
589 my $dbh = C4::Context->dbh;
590 # warn "DATE BEFORE : $daterecieved";
591 # $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
592 # warn "DATE REC : $daterecieved";
593 $datereceived = C4::Dates->output('iso') unless $datereceived;
594 my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
596 ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
598 # Allows libraries to change their bookfund during receiving orders
599 # allows them to adjust budgets
600 if ( C4::Context->preference("LooseBudgets") && $bookfund ) {
602 UPDATE aqorderbreakdown
606 my $sth = $dbh->prepare($query);
607 $sth->execute( $bookfund, $ordnum );
611 my $sth=$dbh->prepare("SELECT * FROM aqorders LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
612 WHERE biblionumber=? AND aqorders.ordernumber=?");
613 $sth->execute($biblionumber,$ordnum);
614 my $order = $sth->fetchrow_hashref();
617 if ( $order->{quantity} > $quantrec ) {
618 $sth=$dbh->prepare("update aqorders
619 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
620 unitprice=?,freight=?,rrp=?,quantity=?
621 where biblionumber=? and ordernumber=?");
622 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$quantrec,$biblionumber,$ordnum);
624 # create a new order for the remaining items, and set its bookfund.
625 my $newOrder = NewOrder($order->{'basketno'},$order->{'biblionumber'},$order->{'title'}, $order->{'quantity'} - $quantrec,
626 $order->{'listprice'},$order->{'booksellerid'},$order->{'authorisedby'},$order->{'notes'},
627 $order->{'bookfundid'},$order->{'biblioitemnumber'},$order->{'rrp'},$order->{'ecost'},$order->{'gst'},
628 $order->{'budget'},$order->{'unitcost'},$order->{'sub'},'',$order->{'sort1'},$order->{'sort2'},$order->{'purchaseordernumber'});
630 $sth=$dbh->prepare("update aqorders
631 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
632 unitprice=?,freight=?,rrp=?
633 where biblionumber=? and ordernumber=?");
634 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$biblionumber,$ordnum);
637 return $datereceived;
639 #------------------------------------------------------------#
643 @results = &SearchOrder($search, $biblionumber, $complete);
647 C<$search> may take one of several forms: if it is an ISBN,
648 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
649 order number, C<&ordersearch> returns orders with that order number
650 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
651 to be a space-separated list of search terms; in this case, all of the
652 terms must appear in the title (matching the beginning of title
655 If C<$complete> is C<yes>, the results will include only completed
656 orders. In any case, C<&ordersearch> ignores cancelled orders.
658 C<&ordersearch> returns an array.
659 C<@results> is an array of references-to-hash with the following keys:
676 my ( $search, $id, $biblionumber, $catview ) = @_;
677 my $dbh = C4::Context->dbh;
678 my @data = split( ' ', $search );
681 @searchterms = ($id);
683 map { push( @searchterms, "$_%", "%$_%" ) } @data;
684 push( @searchterms, $search, $search, $biblionumber );
686 ### FIXME THIS CAN raise a problem if more THAN ONE biblioitem is linked to one biblio
689 "SELECT *,biblio.title
691 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
692 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
693 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
694 WHERE aqbasket.booksellerid = ?
695 AND ((datecancellationprinted is NULL)
696 OR (datecancellationprinted = '0000-00-00'))
700 map { "(biblio.title like ? or biblio.title like ?)" } @data )
702 . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
707 " SELECT *,biblio.title
709 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
710 LEFT JOIN aqbasket on aqorders.basketno=aqbasket.basketno
711 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
712 WHERE ((datecancellationprinted is NULL)
713 OR (datecancellationprinted = '0000-00-00'))
714 AND (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
718 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
720 . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
722 $query .= " GROUP BY aqorders.ordernumber";
724 my $sth = $dbh->prepare($query);
725 $sth->execute(@searchterms);
732 my $sth2 = $dbh->prepare($query2);
735 FROM aqorderbreakdown
738 my $sth3 = $dbh->prepare($query3);
740 while ( my $data = $sth->fetchrow_hashref ) {
741 $sth2->execute( $data->{'biblionumber'} );
742 my $data2 = $sth2->fetchrow_hashref;
743 $data->{'author'} = $data2->{'author'};
744 $data->{'seriestitle'} = $data2->{'seriestitle'};
745 $sth3->execute( $data->{'ordernumber'} );
746 my $data3 = $sth3->fetchrow_hashref;
747 $data->{'branchcode'} = $data3->{'branchcode'};
748 $data->{'bookfundid'} = $data3->{'bookfundid'};
749 push( @results, $data );
758 #------------------------------------------------------------#
764 &DelOrder($biblionumber, $ordernumber);
766 Cancel the order with the given order and biblio numbers. It does not
767 delete any entries in the aqorders table, it merely marks them as
775 my ( $bibnum, $ordnum ) = @_;
776 my $dbh = C4::Context->dbh;
779 SET datecancellationprinted=now()
780 WHERE biblionumber=? AND ordernumber=?
782 my $sth = $dbh->prepare($query);
783 $sth->execute( $bibnum, $ordnum );
790 =head2 FUNCTIONS ABOUT PARCELS
796 #------------------------------------------------------------#
802 @results = &GetParcel($booksellerid, $code, $date);
804 Looks up all of the received items from the supplier with the given
805 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
807 C<@results> is an array of references-to-hash. The keys of each element are fields from
808 the aqorders, biblio, and biblioitems tables of the Koha database.
810 C<@results> is sorted alphabetically by book title.
817 #gets all orders from a certain supplier, orders them alphabetically
818 my ( $supplierid, $code, $datereceived ) = @_;
819 my $dbh = C4::Context->dbh;
822 if $code; # add % if we search on a given code (otherwise, let him empty)
829 aqorders.biblionumber,
831 aqorders.ordernumber,
833 aqorders.quantityreceived,
839 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
840 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
842 aqbasket.booksellerid = ?
843 AND aqorders.booksellerinvoicenumber LIKE ?
844 AND aqorders.datereceived = ? ";
846 my @query_params = ( $supplierid, $code, $datereceived );
847 if ( C4::Context->preference("IndependantBranches") ) {
848 my $userenv = C4::Context->userenv;
849 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
850 $strsth .= " and (borrowers.branchcode = ?
851 or borrowers.branchcode = '')";
852 push @query_params, $userenv->{branch};
855 $strsth .= " ORDER BY aqbasket.basketno";
856 ### parcelinformation : $strsth
857 my $sth = $dbh->prepare($strsth);
858 $sth->execute( @query_params );
859 while ( my $data = $sth->fetchrow_hashref ) {
860 push( @results, $data );
862 ### countparcelbiblio: scalar(@results)
868 #------------------------------------------------------------#
874 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
875 get a lists of parcels.
880 is the bookseller this function has to get parcels.
883 To know on what criteria the results list has to be ordered.
886 is the booksellerinvoicenumber.
888 =item $datefrom & $dateto
889 to know on what date this function has to filter its search.
892 a pointer on a hash list containing parcel informations as such :
898 =item Number of biblio
900 =item Number of items
907 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
908 my $dbh = C4::Context->dbh;
910 SELECT aqorders.booksellerinvoicenumber,
911 datereceived,purchaseordernumber,
912 count(DISTINCT biblionumber) AS biblio,
913 sum(quantity) AS itemsexpected,
914 sum(quantityreceived) AS itemsreceived
915 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
916 WHERE aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
919 $strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" " if ($code);
921 $strsth .= "and datereceived >=" . $dbh->quote($datefrom) . " " if ($datefrom);
923 $strsth .= "and datereceived <=" . $dbh->quote($dateto) . " " if ($dateto);
925 $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
926 $strsth .= "order by $order " if ($order);
928 my $sth = $dbh->prepare($strsth);
931 my $results = $sth->fetchall_arrayref({});
936 #------------------------------------------------------------#
942 @results = &GetLateOrders;
944 Searches for bookseller with late orders.
947 the table of supplier with late issues. This table is full of hashref.
955 my $supplierid = shift;
958 my $dbh = C4::Context->dbh;
960 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
962 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
965 if ( $dbdriver eq "mysql" ) {
967 SELECT aqbasket.basketno,aqorders.ordernumber,
968 DATE(aqbasket.closedate) AS orderdate,
969 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
970 aqorders.rrp AS unitpricesupplier,
971 aqorders.ecost AS unitpricelib,
972 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
973 aqbookfund.bookfundname AS budget,
974 borrowers.branchcode AS branch,
975 aqbooksellers.name AS supplier,
978 biblioitems.publishercode AS publisher,
979 biblioitems.publicationyear,
980 DATEDIFF(CURDATE( ),closedate) AS latesince
982 (aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber)
983 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber)
984 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber = aqorderbreakdown.ordernumber)
985 LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
986 (aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber)
987 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
988 WHERE aqorders.basketno = aqbasket.basketno
989 AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY))
990 AND ((datereceived = '' OR datereceived is null)
991 OR (aqorders.quantityreceived < aqorders.quantity) )
993 $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
994 $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'"
997 " AND borrowers.branchcode like \'"
998 . C4::Context->userenv->{branch} . "\'"
999 if ( C4::Context->preference("IndependantBranches")
1000 && C4::Context->userenv
1001 && C4::Context->userenv->{flags} != 1 );
1002 $strsth .=" HAVING quantity<>0
1003 AND unitpricesupplier<>0
1005 ORDER BY latesince,basketno,borrowers.branchcode, supplier
1010 SELECT aqbasket.basketno,
1011 DATE(aqbasket.closedate) AS orderdate,
1012 aqorders.quantity, aqorders.rrp AS unitpricesupplier,
1013 aqorders.ecost as unitpricelib,
1014 aqorders.quantity * aqorders.rrp AS subtotal
1015 aqbookfund.bookfundname AS budget,
1016 borrowers.branchcode AS branch,
1017 aqbooksellers.name AS supplier,
1020 biblioitems.publishercode AS publisher,
1021 biblioitems.publicationyear,
1022 (CURDATE - closedate) AS latesince
1024 (aqorders LEFT JOIN biblio on biblio.biblionumber = aqorders.biblionumber)
1025 LEFT JOIN biblioitems on biblioitems.biblionumber=biblio.biblionumber)
1026 LEFT JOIN aqorderbreakdown on aqorders.ordernumber = aqorderbreakdown.ordernumber)
1027 LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
1028 (aqbasket LEFT JOIN borrowers on aqbasket.authorisedby = borrowers.borrowernumber) LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1029 WHERE aqorders.basketno = aqbasket.basketno
1030 AND (closedate < (CURDATE -(INTERVAL $delay DAY))
1031 AND ((datereceived = '' OR datereceived is null)
1032 OR (aqorders.quantityreceived < aqorders.quantity) ) ";
1033 $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
1035 $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'" if ($branch);
1036 $strsth .=" AND borrowers.branchcode like \'". C4::Context->userenv->{branch} . "\'"
1037 if (C4::Context->preference("IndependantBranches") && C4::Context->userenv->{flags} != 1 );
1038 $strsth .=" ORDER BY latesince,basketno,borrowers.branchcode, supplier";
1040 my $sth = $dbh->prepare($strsth);
1044 while ( my $data = $sth->fetchrow_hashref ) {
1045 $data->{hilighted} = $hilighted if ( $hilighted > 0 );
1046 $data->{orderdate} = format_date( $data->{orderdate} );
1047 push @results, $data;
1048 $hilighted = -$hilighted;
1054 #------------------------------------------------------------#
1060 (\@order_loop, $total_qty, $total_price, $total_qtyreceived)=&GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on )
1062 this function get the search history.
1069 my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
1072 my $total_qtyreceived = 0;
1073 my $total_price = 0;
1075 # don't run the query if there are no parameters (list would be too long for sure !)
1076 if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
1077 my $dbh = C4::Context->dbh;
1083 name,aqbasket.creationdate,
1084 aqorders.datereceived,
1086 aqorders.quantityreceived,
1088 aqorders.ordernumber,
1089 aqorders.booksellerinvoicenumber as invoicenumber,
1090 aqbooksellers.id as id,
1091 aqorders.biblionumber
1093 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
1094 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
1095 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
1097 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
1098 if ( C4::Context->preference("IndependantBranches") );
1100 $query .= " WHERE 1 ";
1101 $query .= " AND biblio.title LIKE " . $dbh->quote( "%" . $title . "%" )
1105 " AND biblio.author LIKE " . $dbh->quote( "%" . $author . "%" )
1108 $query .= " AND name LIKE " . $dbh->quote( "%" . $name . "%" ) if $name;
1110 $query .= " AND creationdate >" . $dbh->quote($from_placed_on)
1113 $query .= " AND creationdate<" . $dbh->quote($to_placed_on)
1115 $query .= " AND (datecancellationprinted is NULL or datecancellationprinted='0000-00-00')";
1117 if ( C4::Context->preference("IndependantBranches") ) {
1118 my $userenv = C4::Context->userenv;
1119 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1121 " AND (borrowers.branchcode = '"
1122 . $userenv->{branch}
1123 . "' OR borrowers.branchcode ='')";
1126 $query .= " ORDER BY booksellerid";
1127 my $sth = $dbh->prepare($query);
1130 while ( my $line = $sth->fetchrow_hashref ) {
1131 $line->{count} = $cnt++;
1132 $line->{toggle} = 1 if $cnt % 2;
1133 push @order_loop, $line;
1134 $line->{creationdate} = format_date( $line->{creationdate} );
1135 $line->{datereceived} = format_date( $line->{datereceived} );
1136 $total_qty += $line->{'quantity'};
1137 $total_qtyreceived += $line->{'quantityreceived'};
1138 $total_price += $line->{'quantity'} * $line->{'ecost'};
1141 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
1144 =head2 GetRecentAcqui
1146 $results = GetRecentAcqui($days);
1148 C<$results> is a ref to a table which containts hashref
1152 sub GetRecentAcqui {
1154 my $dbh = C4::Context->dbh;
1158 ORDER BY timestamp DESC
1161 my $sth = $dbh->prepare($query);
1164 while(my $data = $sth->fetchrow_hashref){
1165 push @results,$data;
1177 Koha Developement team <info@koha.org>