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
25 use C4
::Dates
qw(format_date format_date_in_iso);
29 use C4
::SQLHelper
qw(InsertInTable);
34 use vars
qw($VERSION @ISA @EXPORT);
37 # set the version for version checking
42 &GetBasket &NewBasket &CloseBasket &DelBasket &ModBasket
43 &GetBasketsByBookseller &GetBasketsByBasketgroup
47 &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
48 &GetBasketgroups &ReOpenBasketgroup
50 &NewOrder &DelOrder &ModOrder &GetPendingOrders &GetOrder &GetOrders
51 &GetOrderNumber &GetLateOrders &GetOrderFromItemnumber
52 &SearchOrder &GetHistory &GetRecentAcqui
53 &ModReceiveOrder &ModOrderBiblioitemNumber
55 &NewOrderItem &ModOrderItem
57 &GetParcels &GetParcel
58 &GetContracts &GetContract
60 &GetItemnumbersFromOrder
68 sub GetOrderFromItemnumber
{
69 my ($itemnumber) = @_;
70 my $dbh = C4
::Context
->dbh;
73 SELECT
* from aqorders LEFT JOIN aqorders_items
74 ON
( aqorders
.ordernumber
= aqorders_items
.ordernumber
)
75 WHERE itemnumber
= ?
|;
77 my $sth = $dbh->prepare($query);
81 $sth->execute($itemnumber);
83 my $order = $sth->fetchrow_hashref;
88 # Returns the itemnumber(s) associated with the ordernumber given in parameter
89 sub GetItemnumbersFromOrder
{
90 my ($ordernumber) = @_;
91 my $dbh = C4
::Context
->dbh;
92 my $query = "SELECT itemnumber FROM aqorders_items WHERE ordernumber=?";
93 my $sth = $dbh->prepare($query);
94 $sth->execute($ordernumber);
97 while (my $order = $sth->fetchrow_hashref) {
98 push @tab, $order->{'itemnumber'};
112 C4::Acquisition - Koha functions for dealing with orders and acquisitions
120 The functions in this module deal with acquisitions, managing book
121 orders, basket and parcels.
125 =head2 FUNCTIONS ABOUT BASKETS
131 $aqbasket = &GetBasket($basketnumber);
133 get all basket informations in aqbasket for a given basket
136 informations for a given basket returned as a hashref.
144 my $dbh = C4
::Context
->dbh;
147 concat( b.firstname,' ',b.surname) AS authorisedbyname,
148 b.branchcode AS branch
150 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
153 my $sth=$dbh->prepare($query);
154 $sth->execute($basketno);
155 my $basket = $sth->fetchrow_hashref;
159 #------------------------------------------------------------#
165 $basket = &NewBasket( $booksellerid, $authorizedby, $basketname, $basketnote, $basketbooksellernote, $basketcontractnumber );
167 Create a new basket in aqbasket table
169 =item C<$booksellerid> is a foreign key in the aqbasket table
171 =item C<$authorizedby> is the username of who created the basket
173 The other parameters are optional, see ModBasketHeader for more info on them.
179 # FIXME : this function seems to be unused.
182 my ( $booksellerid, $authorisedby, $basketname, $basketnote, $basketbooksellernote, $basketcontractnumber ) = @_;
183 my $dbh = C4
::Context
->dbh;
186 (creationdate,booksellerid,authorisedby)
187 VALUES (now(),'$booksellerid','$authorisedby')
191 #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
192 my $basket = $dbh->{'mysql_insertid'};
193 ModBasketHeader
($basket, $basketname || '', $basketnote || '', $basketbooksellernote || '', $basketcontractnumber || undef);
197 #------------------------------------------------------------#
203 &CloseBasket($basketno);
205 close a basket (becomes unmodifiable,except for recieves)
213 my $dbh = C4
::Context
->dbh;
219 my $sth = $dbh->prepare($query);
220 $sth->execute($basketno);
223 #------------------------------------------------------------#
225 =head3 CloseBasketgroup
229 &CloseBasketgroup($basketgroupno);
237 sub CloseBasketgroup
{
238 my ($basketgroupno) = @_;
239 my $dbh = C4
::Context
->dbh;
240 my $sth = $dbh->prepare("
241 UPDATE aqbasketgroups
245 $sth->execute($basketgroupno);
248 #------------------------------------------------------------#
250 =head3 ReOpenBaskergroup($basketgroupno)
254 &ReOpenBaskergroup($basketgroupno);
262 sub ReOpenBasketgroup
{
263 my ($basketgroupno) = @_;
264 my $dbh = C4
::Context
->dbh;
265 my $sth = $dbh->prepare("
266 UPDATE aqbasketgroups
270 $sth->execute($basketgroupno);
273 #------------------------------------------------------------#
280 &DelBasket($basketno);
282 Deletes the basket that has basketno field $basketno in the aqbasket table.
286 =item C<$basketno> is the primary key of the basket in the aqbasket table.
294 my ( $basketno ) = @_;
295 my $query = "DELETE FROM aqbasket WHERE basketno=?";
296 my $dbh = C4
::Context
->dbh;
297 my $sth = $dbh->prepare($query);
298 $sth->execute($basketno);
302 #------------------------------------------------------------#
308 &ModBasket($basketinfo);
310 Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
314 =item C<$basketno> is the primary key of the basket in the aqbasket table.
322 my $basketinfo = shift;
323 my $query = "UPDATE aqbasket SET ";
325 foreach my $key (keys %$basketinfo){
326 if ($key ne 'basketno'){
327 $query .= "$key=?, ";
328 push(@params, $basketinfo->{$key} || undef );
331 # get rid of the "," at the end of $query
332 if (substr($query, length($query)-2) eq ', '){
337 $query .= "WHERE basketno=?";
338 push(@params, $basketinfo->{'basketno'});
339 my $dbh = C4
::Context
->dbh;
340 my $sth = $dbh->prepare($query);
341 $sth->execute(@params);
345 #------------------------------------------------------------#
347 =head3 ModBasketHeader
351 &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber);
353 Modifies a basket's header.
357 =item C<$basketno> is the "basketno" field in the "aqbasket" table;
359 =item C<$basketname> is the "basketname" field in the "aqbasket" table;
361 =item C<$note> is the "note" field in the "aqbasket" table;
363 =item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
365 =item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
372 sub ModBasketHeader
{
373 my ($basketno, $basketname, $note, $booksellernote, $contractnumber) = @_;
374 my $query = "UPDATE aqbasket SET basketname=?, note=?, booksellernote=? WHERE basketno=?";
375 my $dbh = C4
::Context
->dbh;
376 my $sth = $dbh->prepare($query);
377 $sth->execute($basketname,$note,$booksellernote,$basketno);
378 if ( $contractnumber ) {
379 my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
380 my $sth2 = $dbh->prepare($query2);
381 $sth2->execute($contractnumber,$basketno);
387 #------------------------------------------------------------#
389 =head3 GetBasketsByBookseller
393 @results = &GetBasketsByBookseller($booksellerid, $extra);
395 Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
399 =item C<$booksellerid> is the 'id' field of the bookseller in the aqbooksellers table
401 =item C<$extra> is the extra sql parameters, can be
403 - $extra->{groupby}: group baskets by column
404 ex. $extra->{groupby} = aqbasket.basketgroupid
405 - $extra->{orderby}: order baskets by column
406 - $extra->{limit}: limit number of results (can be helpful for pagination)
414 sub GetBasketsByBookseller
{
415 my ($booksellerid, $extra) = @_;
416 my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
418 if ($extra->{groupby
}) {
419 $query .= " GROUP by $extra->{groupby}";
421 if ($extra->{orderby
}){
422 $query .= " ORDER by $extra->{orderby}";
424 if ($extra->{limit
}){
425 $query .= " LIMIT $extra->{limit}";
428 my $dbh = C4
::Context
->dbh;
429 my $sth = $dbh->prepare($query);
430 $sth->execute($booksellerid);
431 my $results = $sth->fetchall_arrayref({});
436 #------------------------------------------------------------#
438 =head3 GetBasketsByBasketgroup
442 $baskets = &GetBasketsByBasketgroup($basketgroupid);
446 Returns a reference to all baskets that belong to basketgroup $basketgroupid.
454 sub GetBasketsByBasketgroup
{
455 my $basketgroupid = shift;
456 my $query = "SELECT * FROM aqbasket
457 LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?";
458 my $dbh = C4
::Context
->dbh;
459 my $sth = $dbh->prepare($query);
460 $sth->execute($basketgroupid);
461 my $results = $sth->fetchall_arrayref({});
466 #------------------------------------------------------------#
468 =head3 NewBasketgroup
472 $basketgroupid = NewBasketgroup(\%hashref);
476 Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
478 $hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
480 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
482 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
484 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
486 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
488 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
497 my $basketgroupinfo = shift;
498 die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
499 my $query = "INSERT INTO aqbasketgroups (";
501 foreach my $field ('name', 'deliveryplace', 'deliverycomment', 'closed') {
502 if ( $basketgroupinfo->{$field} ) {
503 $query .= "$field, ";
504 push(@params, $basketgroupinfo->{$field});
507 $query .= "booksellerid) VALUES (";
512 push(@params, $basketgroupinfo->{'booksellerid'});
513 my $dbh = C4
::Context
->dbh;
514 my $sth = $dbh->prepare($query);
515 $sth->execute(@params);
516 my $basketgroupid = $dbh->{'mysql_insertid'};
517 if( $basketgroupinfo->{'basketlist'} ) {
518 foreach my $basketno (@
{$basketgroupinfo->{'basketlist'}}) {
519 my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
520 my $sth2 = $dbh->prepare($query2);
521 $sth2->execute($basketgroupid, $basketno);
524 return $basketgroupid;
527 #------------------------------------------------------------#
529 =head3 ModBasketgroup
533 ModBasketgroup(\%hashref);
537 Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
539 $hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
541 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
543 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
545 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
547 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
549 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
551 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
560 my $basketgroupinfo = shift;
561 die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
562 my $dbh = C4
::Context
->dbh;
563 my $query = "UPDATE aqbasketgroups SET ";
565 foreach my $field (qw(name billingplace deliveryplace deliverycomment closed)) {
566 if ( defined $basketgroupinfo->{$field} ) {
567 $query .= "$field=?, ";
568 push(@params, $basketgroupinfo->{$field});
573 $query .= " WHERE id=?";
574 push(@params, $basketgroupinfo->{'id'});
575 my $sth = $dbh->prepare($query);
576 $sth->execute(@params);
578 $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
579 $sth->execute($basketgroupinfo->{'id'});
581 if($basketgroupinfo->{'basketlist'} && @
{$basketgroupinfo->{'basketlist'}}){
582 $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
583 foreach my $basketno (@
{$basketgroupinfo->{'basketlist'}}) {
584 $sth->execute($basketgroupinfo->{'id'}, $basketno);
591 #------------------------------------------------------------#
593 =head3 DelBasketgroup
597 DelBasketgroup($basketgroupid);
601 Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
603 =item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
612 my $basketgroupid = shift;
613 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
614 my $query = "DELETE FROM aqbasketgroups WHERE id=?";
615 my $dbh = C4
::Context
->dbh;
616 my $sth = $dbh->prepare($query);
617 $sth->execute($basketgroupid);
621 #------------------------------------------------------------#
625 =head2 FUNCTIONS ABOUT ORDERS
631 =head3 GetBasketgroup
635 $basketgroup = &GetBasketgroup($basketgroupid);
639 Returns a reference to the hash containing all infermation about the basketgroup.
648 my $basketgroupid = shift;
649 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
650 my $query = "SELECT * FROM aqbasketgroups WHERE id=?";
651 my $dbh = C4
::Context
->dbh;
652 my $sth = $dbh->prepare($query);
653 $sth->execute($basketgroupid);
654 my $result = $sth->fetchrow_hashref;
659 #------------------------------------------------------------#
661 =head3 GetBasketgroups
665 $basketgroups = &GetBasketgroups($booksellerid);
669 Returns a reference to the array of all the basketgroups of bookseller $booksellerid.
677 sub GetBasketgroups
{
678 my $booksellerid = shift;
679 die "bookseller id is required to edit a basketgroup" unless $booksellerid;
680 my $query = "SELECT * FROM aqbasketgroups WHERE booksellerid=?";
681 my $dbh = C4
::Context
->dbh;
682 my $sth = $dbh->prepare($query);
683 $sth->execute($booksellerid);
684 my $results = $sth->fetchall_arrayref({});
689 #------------------------------------------------------------#
693 =head2 FUNCTIONS ABOUT ORDERS
699 #------------------------------------------------------------#
701 =head3 GetPendingOrders
705 $orders = &GetPendingOrders($booksellerid, $grouped, $owner);
707 Finds pending orders from the bookseller with the given ID. Ignores
708 completed and cancelled orders.
710 C<$booksellerid> contains the bookseller identifier
711 C<$grouped> contains 0 or 1. 0 means returns the list, 1 means return the total
712 C<$owner> contains 0 or 1. 0 means any owner. 1 means only the list of orders entered by the user itself.
714 C<$orders> is a reference-to-array; each element is a
715 reference-to-hash with the following fields:
716 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
717 in a single result line
721 =item C<authorizedby>
727 These give the value of the corresponding field in the aqorders table
728 of the Koha database.
734 Results are ordered from most to least recent.
738 sub GetPendingOrders
{
739 my ($supplierid,$grouped,$owner,$basketno) = @_;
740 my $dbh = C4
::Context
->dbh;
742 SELECT ".($grouped?
"count(*),":"")."aqbasket.basketno,
743 surname,firstname,aqorders.*,biblio.*,biblioitems.isbn,
744 aqbasket.closedate, aqbasket.creationdate, aqbasket.basketname
746 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
747 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
748 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
749 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
751 AND (quantity > quantityreceived OR quantityreceived is NULL)
752 AND datecancellationprinted IS NULL
753 AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
755 ## FIXME Why 180 days ???
756 my @query_params = ( $supplierid );
757 my $userenv = C4
::Context
->userenv;
758 if ( C4
::Context
->preference("IndependantBranches") ) {
759 if ( ($userenv) && ( $userenv->{flags
} != 1 ) ) {
760 $strsth .= " and (borrowers.branchcode = ?
761 or borrowers.branchcode = '')";
762 push @query_params, $userenv->{branch
};
766 $strsth .= " AND aqbasket.authorisedby=? ";
767 push @query_params, $userenv->{'number'};
770 $strsth .= " AND aqbasket.basketno=? ";
771 push @query_params, $basketno;
773 $strsth .= " group by aqbasket.basketno" if $grouped;
774 $strsth .= " order by aqbasket.basketno";
776 my $sth = $dbh->prepare($strsth);
777 $sth->execute( @query_params );
778 my $results = $sth->fetchall_arrayref({});
783 #------------------------------------------------------------#
789 @orders = &GetOrders($basketnumber, $orderby);
791 Looks up the pending (non-cancelled) orders with the given basket
792 number. If C<$booksellerID> is non-empty, only orders from that seller
796 C<&basket> returns a two-element array. C<@orders> is an array of
797 references-to-hash, whose keys are the fields from the aqorders,
798 biblio, and biblioitems tables in the Koha database.
805 my ( $basketno, $orderby ) = @_;
806 my $dbh = C4
::Context
->dbh;
808 SELECT biblio.*,biblioitems.*,
813 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
814 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
815 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
817 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
820 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
821 $query .= " ORDER BY $orderby";
822 my $sth = $dbh->prepare($query);
823 $sth->execute($basketno);
824 my $results = $sth->fetchall_arrayref({});
829 #------------------------------------------------------------#
831 =head3 GetOrderNumber
835 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
839 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
841 Returns the number of this order.
845 =item C<$ordernumber> is the order number.
851 my ( $biblionumber,$biblioitemnumber ) = @_;
852 my $dbh = C4
::Context
->dbh;
857 AND biblioitemnumber=?
859 my $sth = $dbh->prepare($query);
860 $sth->execute( $biblionumber, $biblioitemnumber );
862 return $sth->fetchrow;
865 #------------------------------------------------------------#
871 $order = &GetOrder($ordernumber);
873 Looks up an order by order number.
875 Returns a reference-to-hash describing the order. The keys of
876 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
883 my ($ordernumber) = @_;
884 my $dbh = C4
::Context
->dbh;
886 SELECT biblioitems.*, biblio.*, aqorders.*
888 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
889 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
890 WHERE aqorders.ordernumber=?
893 my $sth= $dbh->prepare($query);
894 $sth->execute($ordernumber);
895 my $data = $sth->fetchrow_hashref;
900 #------------------------------------------------------------#
906 &NewOrder(\%hashref);
908 Adds a new order to the database. Any argument that isn't described
909 below is the new value of the field with the same name in the aqorders
910 table of the Koha database.
914 =item $hashref->{'basketno'} is the basketno foreign key in aqorders, it is mandatory
917 =item $hashref->{'ordernumber'} is a "minimum order number."
919 =item $hashref->{'budgetdate'} is effectively ignored.
920 If it's undef (anything false) or the string 'now', the current day is used.
921 Else, the upcoming July 1st is used.
923 =item $hashref->{'subscription'} may be either "yes", or anything else for "no".
925 =item $hashref->{'uncertainprice'} may be 0 for "the price is known" or 1 for "the price is uncertain"
927 =item defaults entrydate to Now
929 The following keys are used: "biblionumber", "title", "basketno", "quantity", "notes", "biblioitemnumber", "rrp", "ecost", "gst", "unitprice", "subscription", "sort1", "sort2", "booksellerinvoicenumber", "listprice", "budgetdate", "purchaseordernumber", "branchcode", "booksellerinvoicenumber", "bookfundid".
938 my $orderinfo = shift;
939 #### ------------------------------
940 my $dbh = C4
::Context
->dbh;
944 # if these parameters are missing, we can't continue
945 for my $key (qw
/basketno quantity biblionumber budget_id/) {
946 die "Mandatory parameter $key missing" unless $orderinfo->{$key};
949 if ( $orderinfo->{'subscription'} eq 'yes' ) {
950 $orderinfo->{'subscription'} = 1;
952 $orderinfo->{'subscription'} = 0;
954 $orderinfo->{'entrydate'} ||= C4
::Dates
->new()->output("iso");
956 my $ordernumber=InsertInTable
("aqorders",$orderinfo);
957 return ( $orderinfo->{'basketno'}, $ordernumber );
962 #------------------------------------------------------------#
976 #my ($biblioitemnumber,$ordernumber, $biblionumber) = @_;
977 my ($itemnumber, $ordernumber) = @_;
978 my $dbh = C4
::Context
->dbh;
980 INSERT INTO aqorders_items
981 (itemnumber
, ordernumber
)
984 my $sth = $dbh->prepare($query);
985 $sth->execute( $itemnumber, $ordernumber);
988 #------------------------------------------------------------#
994 &ModOrder(\%hashref);
998 Modifies an existing order. Updates the order with order number
999 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All other keys of the hash
1000 update the fields with the same name in the aqorders table of the Koha database.
1009 my $orderinfo = shift;
1011 die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '' ;
1012 die "Biblionumber is required" if $orderinfo->{'biblionumber'} eq '';
1014 my $dbh = C4
::Context
->dbh;
1016 # delete($orderinfo->{'branchcode'});
1017 # the hash contains a lot of entries not in aqorders, so get the columns ...
1018 my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1020 my $colnames = $sth->{NAME
};
1021 my $query = "UPDATE aqorders SET ";
1023 foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1024 # ... and skip hash entries that are not in the aqorders table
1025 # FIXME : probably not the best way to do it (would be better to have a correct hash)
1026 next unless grep(/^$orderinfokey$/, @
$colnames);
1027 $query .= "$orderinfokey=?, ";
1028 push(@params, $orderinfo->{$orderinfokey});
1031 $query .= "timestamp=NOW() WHERE ordernumber=?";
1032 # push(@params, $specorderinfo{'ordernumber'});
1033 push(@params, $orderinfo->{'ordernumber'} );
1034 $sth = $dbh->prepare($query);
1035 $sth->execute(@params);
1039 #------------------------------------------------------------#
1045 &ModOrderItem(\%hashref);
1049 Modifies the itemnumber in the aqorders_items table. The input hash needs three entities:
1050 - itemnumber: the old itemnumber
1051 - ordernumber: the order this item is attached to
1052 - newitemnumber: the new itemnumber we want to attach the line to
1061 my $orderiteminfo = shift;
1062 if (! $orderiteminfo->{'ordernumber'} || ! $orderiteminfo->{'itemnumber'} || ! $orderiteminfo->{'newitemnumber'}){
1063 die "Ordernumber, itemnumber and newitemnumber is required";
1066 my $dbh = C4
::Context
->dbh;
1068 my $query = "UPDATE aqorders_items set itemnumber=? where itemnumber=? and ordernumber=?";
1069 my @params = ($orderiteminfo->{'newitemnumber'}, $orderiteminfo->{'itemnumber'}, $orderiteminfo->{'ordernumber'});
1071 warn Data
::Dumper
::Dumper
(@params);
1072 my $sth = $dbh->prepare($query);
1073 $sth->execute(@params);
1077 #------------------------------------------------------------#
1080 =head3 ModOrderBibliotemNumber
1084 &ModOrderBiblioitemNumber($biblioitemnumber,$ordernumber, $biblionumber);
1086 Modifies the biblioitemnumber for an existing order.
1087 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
1093 #FIXME: is this used at all?
1094 sub ModOrderBiblioitemNumber
{
1095 my ($biblioitemnumber,$ordernumber, $biblionumber) = @_;
1096 my $dbh = C4
::Context
->dbh;
1099 SET biblioitemnumber = ?
1100 WHERE ordernumber = ?
1101 AND biblionumber = ?";
1102 my $sth = $dbh->prepare($query);
1103 $sth->execute( $biblioitemnumber, $ordernumber, $biblionumber );
1106 #------------------------------------------------------------#
1108 =head3 ModReceiveOrder
1112 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
1113 $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
1114 $freight, $bookfund, $rrp);
1116 Updates an order, to reflect the fact that it was received, at least
1117 in part. All arguments not mentioned below update the fields with the
1118 same name in the aqorders table of the Koha database.
1120 If a partial order is received, splits the order into two. The received
1121 portion must have a booksellerinvoicenumber.
1123 Updates the order with bibilionumber C<$biblionumber> and ordernumber
1131 sub ModReceiveOrder
{
1133 $biblionumber, $ordernumber, $quantrec, $user, $cost,
1134 $invoiceno, $freight, $rrp, $budget_id, $datereceived
1137 my $dbh = C4
::Context
->dbh;
1138 # warn "DATE BEFORE : $daterecieved";
1139 # $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
1140 # warn "DATE REC : $daterecieved";
1141 $datereceived = C4
::Dates
->output('iso') unless $datereceived;
1142 my $suggestionid = GetSuggestionFromBiblionumber
( $dbh, $biblionumber );
1143 if ($suggestionid) {
1144 ModSuggestion
( {suggestionid
=>$suggestionid,
1145 STATUS
=>'AVAILABLE',
1146 biblionumber
=> $biblionumber}
1150 my $sth=$dbh->prepare("
1151 SELECT * FROM aqorders
1152 WHERE biblionumber=? AND aqorders.ordernumber=?");
1154 $sth->execute($biblionumber,$ordernumber);
1155 my $order = $sth->fetchrow_hashref();
1158 if ( $order->{quantity
} > $quantrec ) {
1159 $sth=$dbh->prepare("
1161 SET quantityreceived=?
1163 , booksellerinvoicenumber=?
1167 , quantityreceived=?
1168 WHERE biblionumber=? AND ordernumber=?");
1170 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$quantrec,$biblionumber,$ordernumber);
1173 # create a new order for the remaining items, and set its bookfund.
1174 foreach my $orderkey ( "linenumber", "allocation" ) {
1175 delete($order->{'$orderkey'});
1177 my $newOrder = NewOrder
($order);
1179 $sth=$dbh->prepare("update aqorders
1180 set quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
1181 unitprice=?,freight=?,rrp=?
1182 where biblionumber=? and ordernumber=?");
1183 $sth->execute($quantrec,$datereceived,$invoiceno,$cost,$freight,$rrp,$biblionumber,$ordernumber);
1186 return $datereceived;
1188 #------------------------------------------------------------#
1192 @results = &SearchOrder($search, $biblionumber, $complete);
1194 Searches for orders.
1196 C<$search> may take one of several forms: if it is an ISBN,
1197 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
1198 order number, C<&ordersearch> returns orders with that order number
1199 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
1200 to be a space-separated list of search terms; in this case, all of the
1201 terms must appear in the title (matching the beginning of title
1204 If C<$complete> is C<yes>, the results will include only completed
1205 orders. In any case, C<&ordersearch> ignores cancelled orders.
1207 C<&ordersearch> returns an array.
1208 C<@results> is an array of references-to-hash with the following keys:
1214 =item C<seriestitle>
1225 #### -------- SearchOrder-------------------------------
1226 my ($ordernumber, $search, $supplierid, $basket) = @_;
1228 my $dbh = C4
::Context
->dbh;
1233 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1234 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1235 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1236 WHERE (datecancellationprinted is NULL)";
1239 $query .= " AND (aqorders.ordernumber=?)";
1240 push @args, $ordernumber;
1243 $query .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
1244 push @args, ("%$search%","%$search%","%$search%");
1247 $query .= "AND aqbasket.booksellerid = ?";
1248 push @args, $supplierid;
1251 $query .= "AND aqorders.basketno = ?";
1252 push @args, $basket;
1255 my $sth = $dbh->prepare($query);
1256 $sth->execute(@args);
1257 my $results = $sth->fetchall_arrayref({});
1262 #------------------------------------------------------------#
1268 &DelOrder($biblionumber, $ordernumber);
1270 Cancel the order with the given order and biblio numbers. It does not
1271 delete any entries in the aqorders table, it merely marks them as
1279 my ( $bibnum, $ordernumber ) = @_;
1280 my $dbh = C4
::Context
->dbh;
1283 SET datecancellationprinted=now()
1284 WHERE biblionumber=? AND ordernumber=?
1286 my $sth = $dbh->prepare($query);
1287 $sth->execute( $bibnum, $ordernumber );
1291 =head2 FUNCTIONS ABOUT PARCELS
1295 #------------------------------------------------------------#
1301 @results = &GetParcel($booksellerid, $code, $date);
1303 Looks up all of the received items from the supplier with the given
1304 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
1306 C<@results> is an array of references-to-hash. The keys of each element are fields from
1307 the aqorders, biblio, and biblioitems tables of the Koha database.
1309 C<@results> is sorted alphabetically by book title.
1316 #gets all orders from a certain supplier, orders them alphabetically
1317 my ( $supplierid, $code, $datereceived ) = @_;
1318 my $dbh = C4
::Context
->dbh;
1321 if $code; # add % if we search on a given code (otherwise, let him empty)
1323 SELECT authorisedby,
1328 aqorders.biblionumber,
1329 aqorders.ordernumber,
1331 aqorders.quantityreceived,
1338 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
1339 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1340 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1342 aqbasket.booksellerid = ?
1343 AND aqorders.booksellerinvoicenumber LIKE ?
1344 AND aqorders.datereceived = ? ";
1346 my @query_params = ( $supplierid, $code, $datereceived );
1347 if ( C4
::Context
->preference("IndependantBranches") ) {
1348 my $userenv = C4
::Context
->userenv;
1349 if ( ($userenv) && ( $userenv->{flags
} != 1 ) ) {
1350 $strsth .= " and (borrowers.branchcode = ?
1351 or borrowers.branchcode = '')";
1352 push @query_params, $userenv->{branch
};
1355 $strsth .= " ORDER BY aqbasket.basketno";
1356 # ## parcelinformation : $strsth
1357 my $sth = $dbh->prepare($strsth);
1358 $sth->execute( @query_params );
1359 while ( my $data = $sth->fetchrow_hashref ) {
1360 push( @results, $data );
1362 # ## countparcelbiblio: scalar(@results)
1368 #------------------------------------------------------------#
1374 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1375 get a lists of parcels.
1384 is the bookseller this function has to get parcels.
1387 To know on what criteria the results list has to be ordered.
1390 is the booksellerinvoicenumber.
1392 =item $datefrom & $dateto
1393 to know on what date this function has to filter its search.
1396 a pointer on a hash list containing parcel informations as such :
1400 =item Last operation
1402 =item Number of biblio
1404 =item Number of items
1411 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
1412 my $dbh = C4
::Context
->dbh;
1413 my @query_params = ();
1415 SELECT aqorders.booksellerinvoicenumber,
1416 datereceived,purchaseordernumber,
1417 count(DISTINCT biblionumber) AS biblio,
1418 sum(quantity) AS itemsexpected,
1419 sum(quantityreceived) AS itemsreceived
1420 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
1421 WHERE aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
1424 if ( defined $code ) {
1425 $strsth .= ' and aqorders.booksellerinvoicenumber like ? ';
1426 # add a % to the end of the code to allow stemming.
1427 push @query_params, "$code%";
1430 if ( defined $datefrom ) {
1431 $strsth .= ' and datereceived >= ? ';
1432 push @query_params, $datefrom;
1435 if ( defined $dateto ) {
1436 $strsth .= 'and datereceived <= ? ';
1437 push @query_params, $dateto;
1440 $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
1442 # can't use a placeholder to place this column name.
1443 # but, we could probably be checking to make sure it is a column that will be fetched.
1444 $strsth .= "order by $order " if ($order);
1446 my $sth = $dbh->prepare($strsth);
1448 $sth->execute( @query_params );
1449 my $results = $sth->fetchall_arrayref({});
1454 #------------------------------------------------------------#
1456 =head3 GetLateOrders
1460 @results = &GetLateOrders;
1462 Searches for bookseller with late orders.
1465 the table of supplier with late issues. This table is full of hashref.
1473 my $supplierid = shift;
1476 my $dbh = C4
::Context
->dbh;
1478 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
1479 my $dbdriver = C4
::Context
->config("db_scheme") || "mysql";
1481 my @query_params = ($delay); # delay is the first argument regardless
1483 SELECT aqbasket.basketno,
1484 aqorders.ordernumber,
1485 DATE(aqbasket.closedate) AS orderdate,
1486 aqorders.rrp AS unitpricesupplier,
1487 aqorders.ecost AS unitpricelib,
1488 aqbudgets.budget_name AS budget,
1489 borrowers.branchcode AS branch,
1490 aqbooksellers.name AS supplier,
1492 biblioitems.publishercode AS publisher,
1493 biblioitems.publicationyear,
1497 aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1498 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1499 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
1500 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
1501 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1502 WHERE aqorders.basketno = aqbasket.basketno
1503 AND ( datereceived = ''
1504 OR datereceived IS NULL
1505 OR aqorders.quantityreceived < aqorders.quantity
1509 if ($dbdriver eq "mysql") {
1511 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
1512 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1513 DATEDIFF(CURDATE( ),closedate) AS latesince
1515 $from .= " AND (closedate <= DATE_SUB(CURDATE( ),INTERVAL ? DAY)) ";
1517 HAVING quantity <> 0
1518 AND unitpricesupplier <> 0
1519 AND unitpricelib <> 0
1522 # FIXME: account for IFNULL as above
1524 aqorders.quantity AS quantity,
1525 aqorders.quantity * aqorders.rrp AS subtotal,
1526 (CURDATE - closedate) AS latesince
1528 $from .= " AND (closedate <= (CURDATE -(INTERVAL ? DAY)) ";
1530 if (defined $supplierid) {
1531 $from .= ' AND aqbasket.booksellerid = ? ';
1532 push @query_params, $supplierid;
1534 if (defined $branch) {
1535 $from .= ' AND borrowers.branchcode LIKE ? ';
1536 push @query_params, $branch;
1538 if (C4
::Context
->preference("IndependantBranches")
1539 && C4
::Context
->userenv
1540 && C4
::Context
->userenv->{flags
} != 1 ) {
1541 $from .= ' AND borrowers.branchcode LIKE ? ';
1542 push @query_params, C4
::Context
->userenv->{branch
};
1544 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
1545 $debug and print STDERR
"GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
1546 my $sth = $dbh->prepare($query);
1547 $sth->execute(@query_params);
1549 while (my $data = $sth->fetchrow_hashref) {
1550 $data->{orderdate
} = format_date
($data->{orderdate
});
1551 push @results, $data;
1556 #------------------------------------------------------------#
1562 (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on );
1564 Retreives some acquisition history information
1567 $order_loop is a list of hashrefs that each look like this:
1569 'author' => 'Twain, Mark',
1571 'biblionumber' => '215',
1573 'creationdate' => 'MM/DD/YYYY',
1574 'datereceived' => undef,
1577 'invoicenumber' => undef,
1579 'ordernumber' => '1',
1581 'quantityreceived' => undef,
1582 'title' => 'The Adventures of Huckleberry Finn'
1584 $total_qty is the sum of all of the quantities in $order_loop
1585 $total_price is the cost of each in $order_loop times the quantity
1586 $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
1593 my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
1596 my $total_qtyreceived = 0;
1597 my $total_price = 0;
1599 # don't run the query if there are no parameters (list would be too long for sure !)
1600 if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
1601 my $dbh = C4
::Context
->dbh;
1607 name,aqbasket.creationdate,
1608 aqorders.datereceived,
1610 aqorders.quantityreceived,
1612 aqorders.ordernumber,
1613 aqorders.booksellerinvoicenumber as invoicenumber,
1614 aqbooksellers.id as id,
1615 aqorders.biblionumber
1617 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
1618 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
1619 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber";
1621 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
1622 if ( C4
::Context
->preference("IndependantBranches") );
1624 $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
1626 my @query_params = ();
1628 if ( defined $title ) {
1629 $query .= " AND biblio.title LIKE ? ";
1630 push @query_params, "%$title%";
1633 if ( defined $author ) {
1634 $query .= " AND biblio.author LIKE ? ";
1635 push @query_params, "%$author%";
1638 if ( defined $name ) {
1639 $query .= " AND name LIKE ? ";
1640 push @query_params, "%$name%";
1643 if ( defined $from_placed_on ) {
1644 $query .= " AND creationdate >= ? ";
1645 push @query_params, $from_placed_on;
1648 if ( defined $to_placed_on ) {
1649 $query .= " AND creationdate <= ? ";
1650 push @query_params, $to_placed_on;
1653 if ( C4
::Context
->preference("IndependantBranches") ) {
1654 my $userenv = C4
::Context
->userenv;
1655 if ( ($userenv) && ( $userenv->{flags
} != 1 ) ) {
1656 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
1657 push @query_params, $userenv->{branch
};
1660 $query .= " ORDER BY booksellerid";
1661 my $sth = $dbh->prepare($query);
1662 $sth->execute( @query_params );
1664 while ( my $line = $sth->fetchrow_hashref ) {
1665 $line->{count
} = $cnt++;
1666 $line->{toggle
} = 1 if $cnt % 2;
1667 push @order_loop, $line;
1668 $line->{creationdate
} = format_date
( $line->{creationdate
} );
1669 $line->{datereceived
} = format_date
( $line->{datereceived
} );
1670 $total_qty += $line->{'quantity'};
1671 $total_qtyreceived += $line->{'quantityreceived'};
1672 $total_price += $line->{'quantity'} * $line->{'ecost'};
1675 return \
@order_loop, $total_qty, $total_price, $total_qtyreceived;
1678 =head2 GetRecentAcqui
1680 $results = GetRecentAcqui($days);
1682 C<$results> is a ref to a table which containts hashref
1686 sub GetRecentAcqui
{
1688 my $dbh = C4
::Context
->dbh;
1692 ORDER BY timestamp DESC
1695 my $sth = $dbh->prepare($query);
1697 my $results = $sth->fetchall_arrayref({});
1705 $contractlist = &GetContracts($booksellerid, $activeonly);
1707 Looks up the contracts that belong to a bookseller
1709 Returns a list of contracts
1711 =item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
1713 =item C<$activeonly> if exists get only contracts that are still active.
1719 my ( $booksellerid, $activeonly ) = @_;
1720 my $dbh = C4
::Context
->dbh;
1722 if (! $activeonly) {
1726 WHERE booksellerid=?
1731 WHERE booksellerid=?
1732 AND contractenddate >= CURDATE( )";
1734 my $sth = $dbh->prepare($query);
1735 $sth->execute( $booksellerid );
1737 while (my $data = $sth->fetchrow_hashref ) {
1738 push(@results, $data);
1744 #------------------------------------------------------------#
1750 $contract = &GetContract($contractID);
1752 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
1760 my ( $contractno ) = @_;
1761 my $dbh = C4
::Context
->dbh;
1765 WHERE contractnumber=?
1768 my $sth = $dbh->prepare($query);
1769 $sth->execute( $contractno );
1770 my $result = $sth->fetchrow_hashref;
1779 Koha Developement team <info@koha.org>