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;
348 SELECT biblioitems.*, biblio.*, aqorderbreakdown.*, aqorders.*
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.
385 If it's undef (anything false) or the string 'now', the current day is used.
386 Else, the upcoming July 1st is used.
388 C<$subscription> may be either "yes", or anything else for "no".
396 $basketno, $bibnum, $title, $quantity,
397 $listprice, $booksellerid, $authorisedby, $notes,
398 $bookfund, $bibitemnum, $rrp, $ecost,
399 $gst, $budget, $cost, $sub,
400 $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)
459 $sth = $dbh->prepare($query);
460 $sth->execute( $ordnum, $bookfund );
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);
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.
490 $title, $ordnum, $quantity, $listprice, $bibnum,
491 $basketno, $supplier, $who, $notes, $bookfund,
492 $bibitemnum, $rrp, $ecost, $gst, $budget,
493 $cost, $invoice, $sort1, $sort2, $purchaseorder
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,
515 UPDATE aqorderbreakdown
516 SET bookfundid=?,branchcode=?
519 $sth = $dbh->prepare($query);
521 unless ( $sth->execute( $bookfund,$branchcode, $ordnum ) )
522 { # 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, $catview ) = @_;
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
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 = ?
700 AND ((datecancellationprinted is NULL)
701 OR (datecancellationprinted = '0000-00-00'))
705 map { "(biblio.title like ? or biblio.title like ?)" } @data )
707 . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
712 " SELECT *,biblio.title
714 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
715 LEFT JOIN aqbasket on aqorders.basketno=aqbasket.basketno
716 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
717 WHERE ((datecancellationprinted is NULL)
718 OR (datecancellationprinted = '0000-00-00'))
719 AND (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
723 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
725 . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
727 $query .= " GROUP BY aqorders.ordernumber";
729 my $sth = $dbh->prepare($query);
730 $sth->execute(@searchterms);
737 my $sth2 = $dbh->prepare($query2);
740 FROM aqorderbreakdown
743 my $sth3 = $dbh->prepare($query3);
745 while ( my $data = $sth->fetchrow_hashref ) {
746 $sth2->execute( $data->{'biblionumber'} );
747 my $data2 = $sth2->fetchrow_hashref;
748 $data->{'author'} = $data2->{'author'};
749 $data->{'seriestitle'} = $data2->{'seriestitle'};
750 $sth3->execute( $data->{'ordernumber'} );
751 my $data3 = $sth3->fetchrow_hashref;
752 $data->{'branchcode'} = $data3->{'branchcode'};
753 $data->{'bookfundid'} = $data3->{'bookfundid'};
754 push( @results, $data );
763 #------------------------------------------------------------#
769 &DelOrder($biblionumber, $ordernumber);
771 Cancel the order with the given order and biblio numbers. It does not
772 delete any entries in the aqorders table, it merely marks them as
780 my ( $bibnum, $ordnum ) = @_;
781 my $dbh = C4
::Context
->dbh;
784 SET datecancellationprinted=now()
785 WHERE biblionumber=? AND ordernumber=?
787 my $sth = $dbh->prepare($query);
788 $sth->execute( $bibnum, $ordnum );
795 =head2 FUNCTIONS ABOUT PARCELS
801 #------------------------------------------------------------#
807 @results = &GetParcel($booksellerid, $code, $date);
809 Looks up all of the received items from the supplier with the given
810 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
812 C<@results> is an array of references-to-hash. The keys of each element are fields from
813 the aqorders, biblio, and biblioitems tables of the Koha database.
815 C<@results> is sorted alphabetically by book title.
822 #gets all orders from a certain supplier, orders them alphabetically
823 my ( $supplierid, $code, $datereceived ) = @_;
824 my $dbh = C4
::Context
->dbh;
827 if $code; # add % if we search on a given code (otherwise, let him empty)
834 aqorders.biblionumber,
836 aqorders.ordernumber,
838 aqorders.quantityreceived,
844 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
845 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
847 aqbasket.booksellerid = ?
848 AND aqorders.booksellerinvoicenumber LIKE ?
849 AND aqorders.datereceived = ? ";
851 my @query_params = ( $supplierid, $code, $datereceived );
852 if ( C4
::Context
->preference("IndependantBranches") ) {
853 my $userenv = C4
::Context
->userenv;
854 if ( ($userenv) && ( $userenv->{flags
} != 1 ) ) {
855 $strsth .= " and (borrowers.branchcode = ?
856 or borrowers.branchcode = '')";
857 push @query_params, $userenv->{branch
};
860 $strsth .= " ORDER BY aqbasket.basketno";
861 ### parcelinformation : $strsth
862 my $sth = $dbh->prepare($strsth);
863 $sth->execute( @query_params );
864 while ( my $data = $sth->fetchrow_hashref ) {
865 push( @results, $data );
867 ### countparcelbiblio: scalar(@results)
873 #------------------------------------------------------------#
879 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
880 get a lists of parcels.
885 is the bookseller this function has to get parcels.
888 To know on what criteria the results list has to be ordered.
891 is the booksellerinvoicenumber.
893 =item $datefrom & $dateto
894 to know on what date this function has to filter its search.
897 a pointer on a hash list containing parcel informations as such :
903 =item Number of biblio
905 =item Number of items
912 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
913 my $dbh = C4
::Context
->dbh;
915 SELECT aqorders.booksellerinvoicenumber,
916 datereceived,purchaseordernumber,
917 count(DISTINCT biblionumber) AS biblio,
918 sum(quantity) AS itemsexpected,
919 sum(quantityreceived) AS itemsreceived
920 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
921 WHERE aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
924 $strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" " if ($code);
926 $strsth .= "and datereceived >=" . $dbh->quote($datefrom) . " " if ($datefrom);
928 $strsth .= "and datereceived <=" . $dbh->quote($dateto) . " " if ($dateto);
930 $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
931 $strsth .= "order by $order " if ($order);
933 my $sth = $dbh->prepare($strsth);
936 my $results = $sth->fetchall_arrayref({});
941 #------------------------------------------------------------#
947 @results = &GetLateOrders;
949 Searches for bookseller with late orders.
952 the table of supplier with late issues. This table is full of hashref.
960 my $supplierid = shift;
963 my $dbh = C4
::Context
->dbh;
965 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
967 my $dbdriver = C4
::Context
->config("db_scheme") || "mysql";
970 if ( $dbdriver eq "mysql" ) {
972 SELECT aqbasket.basketno,aqorders.ordernumber,
973 DATE(aqbasket.closedate) AS orderdate,
974 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
975 aqorders.rrp AS unitpricesupplier,
976 aqorders.ecost AS unitpricelib,
977 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
978 aqbookfund.bookfundname AS budget,
979 borrowers.branchcode AS branch,
980 aqbooksellers.name AS supplier,
983 biblioitems.publishercode AS publisher,
984 biblioitems.publicationyear,
985 DATEDIFF(CURDATE( ),closedate) AS latesince
987 (aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber)
988 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber)
989 LEFT JOIN aqorderbreakdown ON aqorders.ordernumber = aqorderbreakdown.ordernumber)
990 LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
991 (aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber)
992 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
993 WHERE aqorders.basketno = aqbasket.basketno
994 AND (closedate <= DATE_SUB(CURDATE( ),INTERVAL $delay DAY))
995 AND ((datereceived = '' OR datereceived is null)
996 OR (aqorders.quantityreceived < aqorders.quantity) )
998 $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
999 $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'"
1002 " AND borrowers.branchcode like \'"
1003 . C4
::Context
->userenv->{branch
} . "\'"
1004 if ( C4
::Context
->preference("IndependantBranches")
1005 && C4
::Context
->userenv
1006 && C4
::Context
->userenv->{flags
} != 1 );
1007 $strsth .=" HAVING quantity<>0
1008 AND unitpricesupplier<>0
1010 ORDER BY latesince,basketno,borrowers.branchcode, supplier
1015 SELECT aqbasket.basketno,
1016 DATE(aqbasket.closedate) AS orderdate,
1017 aqorders.quantity, aqorders.rrp AS unitpricesupplier,
1018 aqorders.ecost as unitpricelib,
1019 aqorders.quantity * aqorders.rrp AS subtotal
1020 aqbookfund.bookfundname AS budget,
1021 borrowers.branchcode AS branch,
1022 aqbooksellers.name AS supplier,
1025 biblioitems.publishercode AS publisher,
1026 biblioitems.publicationyear,
1027 (CURDATE - closedate) AS latesince
1029 (aqorders LEFT JOIN biblio on biblio.biblionumber = aqorders.biblionumber)
1030 LEFT JOIN biblioitems on biblioitems.biblionumber=biblio.biblionumber)
1031 LEFT JOIN aqorderbreakdown on aqorders.ordernumber = aqorderbreakdown.ordernumber)
1032 LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
1033 (aqbasket LEFT JOIN borrowers on aqbasket.authorisedby = borrowers.borrowernumber) LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1034 WHERE aqorders.basketno = aqbasket.basketno
1035 AND (closedate <= (CURDATE -(INTERVAL $delay DAY))
1036 AND ((datereceived = '' OR datereceived is null)
1037 OR (aqorders.quantityreceived < aqorders.quantity) ) ";
1038 $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
1040 $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'" if ($branch);
1041 $strsth .=" AND borrowers.branchcode like \'". C4
::Context
->userenv->{branch
} . "\'"
1042 if (C4
::Context
->preference("IndependantBranches") && C4
::Context
->userenv->{flags
} != 1 );
1043 $strsth .=" ORDER BY latesince,basketno,borrowers.branchcode, supplier";
1045 my $sth = $dbh->prepare($strsth);
1049 while ( my $data = $sth->fetchrow_hashref ) {
1050 $data->{hilighted
} = $hilighted if ( $hilighted > 0 );
1051 $data->{orderdate
} = format_date
( $data->{orderdate
} );
1052 push @results, $data;
1053 $hilighted = -$hilighted;
1059 #------------------------------------------------------------#
1065 (\@order_loop, $total_qty, $total_price, $total_qtyreceived)=&GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on )
1067 this function get the search history.
1074 my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
1077 my $total_qtyreceived = 0;
1078 my $total_price = 0;
1080 # don't run the query if there are no parameters (list would be too long for sure !)
1081 if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
1082 my $dbh = C4
::Context
->dbh;
1088 name,aqbasket.creationdate,
1089 aqorders.datereceived,
1091 aqorders.quantityreceived,
1093 aqorders.ordernumber,
1094 aqorders.booksellerinvoicenumber as invoicenumber,
1095 aqbooksellers.id as id,
1096 aqorders.biblionumber
1098 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
1099 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
1100 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
1102 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
1103 if ( C4
::Context
->preference("IndependantBranches") );
1105 $query .= " WHERE 1 ";
1106 $query .= " AND biblio.title LIKE " . $dbh->quote( "%" . $title . "%" )
1110 " AND biblio.author LIKE " . $dbh->quote( "%" . $author . "%" )
1113 $query .= " AND name LIKE " . $dbh->quote( "%" . $name . "%" ) if $name;
1115 $query .= " AND creationdate >" . $dbh->quote($from_placed_on)
1118 $query .= " AND creationdate<" . $dbh->quote($to_placed_on)
1120 $query .= " AND (datecancellationprinted is NULL or datecancellationprinted='0000-00-00')";
1122 if ( C4
::Context
->preference("IndependantBranches") ) {
1123 my $userenv = C4
::Context
->userenv;
1124 if ( ($userenv) && ( $userenv->{flags
} != 1 ) ) {
1126 " AND (borrowers.branchcode = '"
1127 . $userenv->{branch
}
1128 . "' OR borrowers.branchcode ='')";
1131 $query .= " ORDER BY booksellerid";
1132 my $sth = $dbh->prepare($query);
1135 while ( my $line = $sth->fetchrow_hashref ) {
1136 $line->{count
} = $cnt++;
1137 $line->{toggle
} = 1 if $cnt % 2;
1138 push @order_loop, $line;
1139 $line->{creationdate
} = format_date
( $line->{creationdate
} );
1140 $line->{datereceived
} = format_date
( $line->{datereceived
} );
1141 $total_qty += $line->{'quantity'};
1142 $total_qtyreceived += $line->{'quantityreceived'};
1143 $total_price += $line->{'quantity'} * $line->{'ecost'};
1146 return \
@order_loop, $total_qty, $total_price, $total_qtyreceived;
1149 =head2 GetRecentAcqui
1151 $results = GetRecentAcqui($days);
1153 C<$results> is a ref to a table which containts hashref
1157 sub GetRecentAcqui
{
1159 my $dbh = C4
::Context
->dbh;
1163 ORDER BY timestamp DESC
1166 my $sth = $dbh->prepare($query);
1169 while(my $data = $sth->fetchrow_hashref){
1170 push @results,$data;
1182 Koha Developement team <info@koha.org>