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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 use C4::Dates qw(format_date format_date_in_iso);
31 use C4::SQLHelper qw(InsertInTable);
32 use C4::Bookseller qw(GetBookSellerFromId);
33 use C4::Templates qw(gettemplate);
38 use vars qw($VERSION @ISA @EXPORT);
41 # set the version for version checking
42 $VERSION = 3.07.00.049;
46 &GetBasket &NewBasket &CloseBasket &DelBasket &ModBasket
47 &GetBasketAsCSV &GetBasketGroupAsCSV
48 &GetBasketsByBookseller &GetBasketsByBasketgroup
49 &GetBasketsInfosByBookseller
53 &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
54 &GetBasketgroups &ReOpenBasketgroup
56 &NewOrder &DelOrder &ModOrder &GetPendingOrders &GetOrder &GetOrders &GetOrdersByBiblionumber
57 &GetOrderNumber &GetLateOrders &GetOrderFromItemnumber
58 &SearchOrder &GetHistory &GetRecentAcqui
59 &ModReceiveOrder &CancelReceipt &ModOrderBiblioitemNumber
61 &GetLastOrderNotReceivedFromSubscriptionid &GetLastOrderReceivedFromSubscriptionid
62 &NewOrderItem &ModItemOrder
64 &GetParcels &GetParcel
65 &GetContracts &GetContract
75 &GetItemnumbersFromOrder
85 sub GetOrderFromItemnumber {
86 my ($itemnumber) = @_;
87 my $dbh = C4::Context->dbh;
90 SELECT * from aqorders LEFT JOIN aqorders_items
91 ON ( aqorders.ordernumber = aqorders_items.ordernumber )
92 WHERE itemnumber = ? |;
94 my $sth = $dbh->prepare($query);
98 $sth->execute($itemnumber);
100 my $order = $sth->fetchrow_hashref;
105 # Returns the itemnumber(s) associated with the ordernumber given in parameter
106 sub GetItemnumbersFromOrder {
107 my ($ordernumber) = @_;
108 my $dbh = C4::Context->dbh;
109 my $query = "SELECT itemnumber FROM aqorders_items WHERE ordernumber=?";
110 my $sth = $dbh->prepare($query);
111 $sth->execute($ordernumber);
114 while (my $order = $sth->fetchrow_hashref) {
115 push @tab, $order->{'itemnumber'};
129 C4::Acquisition - Koha functions for dealing with orders and acquisitions
137 The functions in this module deal with acquisitions, managing book
138 orders, basket and parcels.
142 =head2 FUNCTIONS ABOUT BASKETS
146 $aqbasket = &GetBasket($basketnumber);
148 get all basket informations in aqbasket for a given basket
150 B<returns:> informations for a given basket returned as a hashref.
156 my $dbh = C4::Context->dbh;
159 concat( b.firstname,' ',b.surname) AS authorisedbyname,
160 b.branchcode AS branch
162 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
165 my $sth=$dbh->prepare($query);
166 $sth->execute($basketno);
167 my $basket = $sth->fetchrow_hashref;
171 #------------------------------------------------------------#
175 $basket = &NewBasket( $booksellerid, $authorizedby, $basketname,
176 $basketnote, $basketbooksellernote, $basketcontractnumber, $deliveryplace, $billingplace );
178 Create a new basket in aqbasket table
182 =item C<$booksellerid> is a foreign key in the aqbasket table
184 =item C<$authorizedby> is the username of who created the basket
188 The other parameters are optional, see ModBasketHeader for more info on them.
193 my ( $booksellerid, $authorisedby, $basketname, $basketnote,
194 $basketbooksellernote, $basketcontractnumber, $deliveryplace,
195 $billingplace ) = @_;
196 my $dbh = C4::Context->dbh;
198 'INSERT INTO aqbasket (creationdate,booksellerid,authorisedby) '
199 . 'VALUES (now(),?,?)';
200 $dbh->do( $query, {}, $booksellerid, $authorisedby );
202 my $basket = $dbh->{mysql_insertid};
203 $basketname ||= q{}; # default to empty strings
205 $basketbooksellernote ||= q{};
206 ModBasketHeader( $basket, $basketname, $basketnote, $basketbooksellernote,
207 $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace );
211 #------------------------------------------------------------#
215 &CloseBasket($basketno);
217 close a basket (becomes unmodifiable,except for recieves)
223 my $dbh = C4::Context->dbh;
229 my $sth = $dbh->prepare($query);
230 $sth->execute($basketno);
233 #------------------------------------------------------------#
235 =head3 GetBasketAsCSV
237 &GetBasketAsCSV($basketno);
239 Export a basket as CSV
241 $cgi parameter is needed for column name translation
246 my ($basketno, $cgi) = @_;
247 my $basket = GetBasket($basketno);
248 my @orders = GetOrders($basketno);
249 my $contract = GetContract($basket->{'contractnumber'});
251 my $template = C4::Templates::gettemplate("acqui/csv/basket.tmpl", "intranet", $cgi);
254 foreach my $order (@orders) {
255 my $bd = GetBiblioData( $order->{'biblionumber'} );
257 contractname => $contract->{'contractname'},
258 ordernumber => $order->{'ordernumber'},
259 entrydate => $order->{'entrydate'},
260 isbn => $order->{'isbn'},
261 author => $bd->{'author'},
262 title => $bd->{'title'},
263 publicationyear => $bd->{'publicationyear'},
264 publishercode => $bd->{'publishercode'},
265 collectiontitle => $bd->{'collectiontitle'},
266 notes => $order->{'notes'},
267 quantity => $order->{'quantity'},
268 rrp => $order->{'rrp'},
269 deliveryplace => C4::Branch::GetBranchName( $basket->{'deliveryplace'} ),
270 billingplace => C4::Branch::GetBranchName( $basket->{'billingplace'} ),
273 contractname author title publishercode collectiontitle notes
274 deliveryplace billingplace
276 # Double the quotes to not be interpreted as a field end
277 $row->{$_} =~ s/"/""/g if $row->{$_};
283 if(defined $a->{publishercode} and defined $b->{publishercode}) {
284 $a->{publishercode} cmp $b->{publishercode};
288 $template->param(rows => \@rows);
290 return $template->output;
294 =head3 GetBasketGroupAsCSV
298 &GetBasketGroupAsCSV($basketgroupid);
300 Export a basket group as CSV
302 $cgi parameter is needed for column name translation
308 sub GetBasketGroupAsCSV {
309 my ($basketgroupid, $cgi) = @_;
310 my $baskets = GetBasketsByBasketgroup($basketgroupid);
312 my $template = C4::Templates::gettemplate('acqui/csv/basketgroup.tmpl', 'intranet', $cgi);
315 for my $basket (@$baskets) {
316 my @orders = GetOrders( $$basket{basketno} );
317 my $contract = GetContract( $$basket{contractnumber} );
318 my $bookseller = GetBookSellerFromId( $$basket{booksellerid} );
319 my $basketgroup = GetBasketgroup( $$basket{basketgroupid} );
321 foreach my $order (@orders) {
322 my $bd = GetBiblioData( $order->{'biblionumber'} );
324 clientnumber => $bookseller->{accountnumber},
325 basketname => $basket->{basketname},
326 ordernumber => $order->{ordernumber},
327 author => $bd->{author},
328 title => $bd->{title},
329 publishercode => $bd->{publishercode},
330 publicationyear => $bd->{publicationyear},
331 collectiontitle => $bd->{collectiontitle},
332 isbn => $order->{isbn},
333 quantity => $order->{quantity},
334 rrp => $order->{rrp},
335 discount => $bookseller->{discount},
336 ecost => $order->{ecost},
337 notes => $order->{notes},
338 entrydate => $order->{entrydate},
339 booksellername => $bookseller->{name},
340 bookselleraddress => $bookseller->{address1},
341 booksellerpostal => $bookseller->{postal},
342 contractnumber => $contract->{contractnumber},
343 contractname => $contract->{contractname},
344 basketgroupdeliveryplace => C4::Branch::GetBranchName( $basketgroup->{deliveryplace} ),
345 basketgroupbillingplace => C4::Branch::GetBranchName( $basketgroup->{billingplace} ),
346 basketdeliveryplace => C4::Branch::GetBranchName( $basket->{deliveryplace} ),
347 basketbillingplace => C4::Branch::GetBranchName( $basket->{billingplace} ),
350 basketname author title publishercode collectiontitle notes
351 booksellername bookselleraddress booksellerpostal contractname
352 basketgroupdeliveryplace basketgroupbillingplace
353 basketdeliveryplace basketbillingplace
355 # Double the quotes to not be interpreted as a field end
356 $row->{$_} =~ s/"/""/g if $row->{$_};
361 $template->param(rows => \@rows);
363 return $template->output;
367 =head3 CloseBasketgroup
369 &CloseBasketgroup($basketgroupno);
375 sub CloseBasketgroup {
376 my ($basketgroupno) = @_;
377 my $dbh = C4::Context->dbh;
378 my $sth = $dbh->prepare("
379 UPDATE aqbasketgroups
383 $sth->execute($basketgroupno);
386 #------------------------------------------------------------#
388 =head3 ReOpenBaskergroup($basketgroupno)
390 &ReOpenBaskergroup($basketgroupno);
396 sub ReOpenBasketgroup {
397 my ($basketgroupno) = @_;
398 my $dbh = C4::Context->dbh;
399 my $sth = $dbh->prepare("
400 UPDATE aqbasketgroups
404 $sth->execute($basketgroupno);
407 #------------------------------------------------------------#
412 &DelBasket($basketno);
414 Deletes the basket that has basketno field $basketno in the aqbasket table.
418 =item C<$basketno> is the primary key of the basket in the aqbasket table.
425 my ( $basketno ) = @_;
426 my $query = "DELETE FROM aqbasket WHERE basketno=?";
427 my $dbh = C4::Context->dbh;
428 my $sth = $dbh->prepare($query);
429 $sth->execute($basketno);
433 #------------------------------------------------------------#
437 &ModBasket($basketinfo);
439 Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
443 =item C<$basketno> is the primary key of the basket in the aqbasket table.
450 my $basketinfo = shift;
451 my $query = "UPDATE aqbasket SET ";
453 foreach my $key (keys %$basketinfo){
454 if ($key ne 'basketno'){
455 $query .= "$key=?, ";
456 push(@params, $basketinfo->{$key} || undef );
459 # get rid of the "," at the end of $query
460 if (substr($query, length($query)-2) eq ', '){
465 $query .= "WHERE basketno=?";
466 push(@params, $basketinfo->{'basketno'});
467 my $dbh = C4::Context->dbh;
468 my $sth = $dbh->prepare($query);
469 $sth->execute(@params);
473 #------------------------------------------------------------#
475 =head3 ModBasketHeader
477 &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid);
479 Modifies a basket's header.
483 =item C<$basketno> is the "basketno" field in the "aqbasket" table;
485 =item C<$basketname> is the "basketname" field in the "aqbasket" table;
487 =item C<$note> is the "note" field in the "aqbasket" table;
489 =item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
491 =item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
493 =item C<$booksellerid> is the id (foreign) key in the "aqbooksellers" table for the vendor.
495 =item C<$deliveryplace> is the "deliveryplace" field in the aqbasket table.
497 =item C<$billingplace> is the "billingplace" field in the aqbasket table.
503 sub ModBasketHeader {
504 my ($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid, $deliveryplace, $billingplace) = @_;
507 SET basketname=?, note=?, booksellernote=?, booksellerid=?, deliveryplace=?, billingplace=?
511 my $dbh = C4::Context->dbh;
512 my $sth = $dbh->prepare($query);
513 $sth->execute($basketname, $note, $booksellernote, $booksellerid, $deliveryplace, $billingplace, $basketno);
515 if ( $contractnumber ) {
516 my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
517 my $sth2 = $dbh->prepare($query2);
518 $sth2->execute($contractnumber,$basketno);
524 #------------------------------------------------------------#
526 =head3 GetBasketsByBookseller
528 @results = &GetBasketsByBookseller($booksellerid, $extra);
530 Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
534 =item C<$booksellerid> is the 'id' field of the bookseller in the aqbooksellers table
536 =item C<$extra> is the extra sql parameters, can be
538 $extra->{groupby}: group baskets by column
539 ex. $extra->{groupby} = aqbasket.basketgroupid
540 $extra->{orderby}: order baskets by column
541 $extra->{limit}: limit number of results (can be helpful for pagination)
547 sub GetBasketsByBookseller {
548 my ($booksellerid, $extra) = @_;
549 my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
551 if ($extra->{groupby}) {
552 $query .= " GROUP by $extra->{groupby}";
554 if ($extra->{orderby}){
555 $query .= " ORDER by $extra->{orderby}";
557 if ($extra->{limit}){
558 $query .= " LIMIT $extra->{limit}";
561 my $dbh = C4::Context->dbh;
562 my $sth = $dbh->prepare($query);
563 $sth->execute($booksellerid);
564 my $results = $sth->fetchall_arrayref({});
569 =head3 GetBasketsInfosByBookseller
571 my $baskets = GetBasketsInfosByBookseller($supplierid, $allbaskets);
573 The optional second parameter allbaskets is a boolean allowing you to
574 select all baskets from the supplier; by default only active baskets (open or
575 closed but still something to receive) are returned.
577 Returns in a arrayref of hashref all about booksellers baskets, plus:
578 total_biblios: Number of distinct biblios in basket
579 total_items: Number of items in basket
580 expected_items: Number of non-received items in basket
584 sub GetBasketsInfosByBookseller {
585 my ($supplierid, $allbaskets) = @_;
587 return unless $supplierid;
589 my $dbh = C4::Context->dbh;
592 SUM(aqorders.quantity) AS total_items,
593 COUNT(DISTINCT aqorders.biblionumber) AS total_biblios,
595 IF(aqorders.datereceived IS NULL
596 AND aqorders.datecancellationprinted IS NULL
601 LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
602 WHERE booksellerid = ?};
604 $query.=" AND (closedate IS NULL OR (aqorders.quantity > aqorders.quantityreceived AND datecancellationprinted IS NULL))";
606 $query.=" GROUP BY aqbasket.basketno";
608 my $sth = $dbh->prepare($query);
609 $sth->execute($supplierid);
610 return $sth->fetchall_arrayref({});
614 #------------------------------------------------------------#
616 =head3 GetBasketsByBasketgroup
618 $baskets = &GetBasketsByBasketgroup($basketgroupid);
620 Returns a reference to all baskets that belong to basketgroup $basketgroupid.
624 sub GetBasketsByBasketgroup {
625 my $basketgroupid = shift;
627 SELECT *, aqbasket.booksellerid as booksellerid
629 LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?
631 my $dbh = C4::Context->dbh;
632 my $sth = $dbh->prepare($query);
633 $sth->execute($basketgroupid);
634 my $results = $sth->fetchall_arrayref({});
639 #------------------------------------------------------------#
641 =head3 NewBasketgroup
643 $basketgroupid = NewBasketgroup(\%hashref);
645 Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
647 $hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
649 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
651 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
653 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
655 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
657 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
659 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
661 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
666 my $basketgroupinfo = shift;
667 die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
668 my $query = "INSERT INTO aqbasketgroups (";
670 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
671 if ( defined $basketgroupinfo->{$field} ) {
672 $query .= "$field, ";
673 push(@params, $basketgroupinfo->{$field});
676 $query .= "booksellerid) VALUES (";
681 push(@params, $basketgroupinfo->{'booksellerid'});
682 my $dbh = C4::Context->dbh;
683 my $sth = $dbh->prepare($query);
684 $sth->execute(@params);
685 my $basketgroupid = $dbh->{'mysql_insertid'};
686 if( $basketgroupinfo->{'basketlist'} ) {
687 foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
688 my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
689 my $sth2 = $dbh->prepare($query2);
690 $sth2->execute($basketgroupid, $basketno);
693 return $basketgroupid;
696 #------------------------------------------------------------#
698 =head3 ModBasketgroup
700 ModBasketgroup(\%hashref);
702 Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
704 $hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
706 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
708 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
710 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
712 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
714 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
716 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
718 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
723 my $basketgroupinfo = shift;
724 die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
725 my $dbh = C4::Context->dbh;
726 my $query = "UPDATE aqbasketgroups SET ";
728 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
729 if ( defined $basketgroupinfo->{$field} ) {
730 $query .= "$field=?, ";
731 push(@params, $basketgroupinfo->{$field});
736 $query .= " WHERE id=?";
737 push(@params, $basketgroupinfo->{'id'});
738 my $sth = $dbh->prepare($query);
739 $sth->execute(@params);
741 $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
742 $sth->execute($basketgroupinfo->{'id'});
744 if($basketgroupinfo->{'basketlist'} && @{$basketgroupinfo->{'basketlist'}}){
745 $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
746 foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
747 $sth->execute($basketgroupinfo->{'id'}, $basketno);
754 #------------------------------------------------------------#
756 =head3 DelBasketgroup
758 DelBasketgroup($basketgroupid);
760 Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
764 =item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
771 my $basketgroupid = shift;
772 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
773 my $query = "DELETE FROM aqbasketgroups WHERE id=?";
774 my $dbh = C4::Context->dbh;
775 my $sth = $dbh->prepare($query);
776 $sth->execute($basketgroupid);
780 #------------------------------------------------------------#
783 =head2 FUNCTIONS ABOUT ORDERS
785 =head3 GetBasketgroup
787 $basketgroup = &GetBasketgroup($basketgroupid);
789 Returns a reference to the hash containing all information about the basketgroup.
794 my $basketgroupid = shift;
795 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
796 my $query = "SELECT * FROM aqbasketgroups WHERE id=?";
797 my $dbh = C4::Context->dbh;
798 my $sth = $dbh->prepare($query);
799 $sth->execute($basketgroupid);
800 my $result = $sth->fetchrow_hashref;
805 #------------------------------------------------------------#
807 =head3 GetBasketgroups
809 $basketgroups = &GetBasketgroups($booksellerid);
811 Returns a reference to the array of all the basketgroups of bookseller $booksellerid.
815 sub GetBasketgroups {
816 my $booksellerid = shift;
817 die 'bookseller id is required to edit a basketgroup' unless $booksellerid;
818 my $query = 'SELECT * FROM aqbasketgroups WHERE booksellerid=? ORDER BY id DESC';
819 my $dbh = C4::Context->dbh;
820 my $sth = $dbh->prepare($query);
821 $sth->execute($booksellerid);
822 return $sth->fetchall_arrayref({});
825 #------------------------------------------------------------#
827 =head2 FUNCTIONS ABOUT ORDERS
831 #------------------------------------------------------------#
833 =head3 GetPendingOrders
835 $orders = &GetPendingOrders($supplierid,$grouped,$owner,$basketno,$ordernumber,$search,$ean);
837 Finds pending orders from the bookseller with the given ID. Ignores
838 completed and cancelled orders.
840 C<$booksellerid> contains the bookseller identifier
841 C<$owner> contains 0 or 1. 0 means any owner. 1 means only the list of orders entered by the user itself.
842 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
843 in a single result line
844 C<$orders> is a reference-to-array; each element is a reference-to-hash.
846 Used also by the filter in parcel.pl
853 These give the value of the corresponding field in the aqorders table
854 of the Koha database.
856 Results are ordered from most to least recent.
860 sub GetPendingOrders {
861 my ($supplierid,$grouped,$owner,$basketno,$ordernumber,$search,$ean) = @_;
862 my $dbh = C4::Context->dbh;
864 SELECT ".($grouped?"count(*),":"")."aqbasket.basketno,
865 surname,firstname,biblio.*,biblioitems.isbn,
866 aqbasket.closedate, aqbasket.creationdate, aqbasket.basketname,
869 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
870 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
871 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
872 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
873 WHERE (quantity > quantityreceived OR quantityreceived is NULL)
874 AND datecancellationprinted IS NULL";
876 my $userenv = C4::Context->userenv;
877 if ( C4::Context->preference("IndependantBranches") ) {
878 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
879 $strsth .= " AND (borrowers.branchcode = ?
880 or borrowers.branchcode = '')";
881 push @query_params, $userenv->{branch};
885 $strsth .= " AND aqbasket.booksellerid = ?";
886 push @query_params, $supplierid;
889 $strsth .= " AND (aqorders.ordernumber=?)";
890 push @query_params, $ordernumber;
893 $strsth .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
894 push @query_params, ("%$search%","%$search%","%$search%");
897 $strsth .= " AND biblioitems.ean = ?";
898 push @query_params, $ean;
901 $strsth .= " AND aqbasket.basketno=? ";
902 push @query_params, $basketno;
905 $strsth .= " AND aqbasket.authorisedby=? ";
906 push @query_params, $userenv->{'number'};
908 $strsth .= " group by aqbasket.basketno" if $grouped;
909 $strsth .= " order by aqbasket.basketno";
910 my $sth = $dbh->prepare($strsth);
911 $sth->execute( @query_params );
912 my $results = $sth->fetchall_arrayref({});
917 #------------------------------------------------------------#
921 @orders = &GetOrders($basketnumber, $orderby);
923 Looks up the pending (non-cancelled) orders with the given basket
924 number. If C<$booksellerID> is non-empty, only orders from that seller
928 C<&basket> returns a two-element array. C<@orders> is an array of
929 references-to-hash, whose keys are the fields from the aqorders,
930 biblio, and biblioitems tables in the Koha database.
935 my ( $basketno, $orderby ) = @_;
936 my $dbh = C4::Context->dbh;
938 SELECT biblio.*,biblioitems.*,
942 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
943 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
944 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
946 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
949 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
950 $query .= " ORDER BY $orderby";
951 my $sth = $dbh->prepare($query);
952 $sth->execute($basketno);
953 my $results = $sth->fetchall_arrayref({});
958 #------------------------------------------------------------#
959 =head3 GetOrdersByBiblionumber
961 @orders = &GetOrdersByBiblionumber($biblionumber);
963 Looks up the orders with linked to a specific $biblionumber, including
964 cancelled orders and received orders.
967 C<@orders> is an array of references-to-hash, whose keys are the
968 fields from the aqorders, biblio, and biblioitems tables in the Koha database.
972 sub GetOrdersByBiblionumber {
973 my $biblionumber = shift;
974 return unless $biblionumber;
975 my $dbh = C4::Context->dbh;
977 SELECT biblio.*,biblioitems.*,
981 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
982 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
983 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
984 WHERE aqorders.biblionumber=?
986 my $sth = $dbh->prepare($query);
987 $sth->execute($biblionumber);
988 my $results = $sth->fetchall_arrayref({});
993 #------------------------------------------------------------#
995 =head3 GetOrderNumber
997 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
999 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
1001 Returns the number of this order.
1005 =item C<$ordernumber> is the order number.
1011 sub GetOrderNumber {
1012 my ( $biblionumber,$biblioitemnumber ) = @_;
1013 my $dbh = C4::Context->dbh;
1017 WHERE biblionumber=?
1018 AND biblioitemnumber=?
1020 my $sth = $dbh->prepare($query);
1021 $sth->execute( $biblionumber, $biblioitemnumber );
1023 return $sth->fetchrow;
1026 #------------------------------------------------------------#
1030 $order = &GetOrder($ordernumber);
1032 Looks up an order by order number.
1034 Returns a reference-to-hash describing the order. The keys of
1035 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
1040 my ($ordernumber) = @_;
1041 my $dbh = C4::Context->dbh;
1043 SELECT biblioitems.*, biblio.*, aqorders.*
1045 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
1046 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
1047 WHERE aqorders.ordernumber=?
1050 my $sth= $dbh->prepare($query);
1051 $sth->execute($ordernumber);
1052 my $data = $sth->fetchrow_hashref;
1057 =head3 GetLastOrderNotReceivedFromSubscriptionid
1059 $order = &GetLastOrderNotReceivedFromSubscriptionid($subscriptionid);
1061 Returns a reference-to-hash describing the last order not received for a subscription.
1065 sub GetLastOrderNotReceivedFromSubscriptionid {
1066 my ( $subscriptionid ) = @_;
1067 my $dbh = C4::Context->dbh;
1069 SELECT * FROM aqorders
1070 LEFT JOIN subscription
1071 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1072 WHERE aqorders.subscriptionid = ?
1073 AND aqorders.datereceived IS NULL
1076 my $sth = $dbh->prepare( $query );
1077 $sth->execute( $subscriptionid );
1078 my $order = $sth->fetchrow_hashref;
1082 =head3 GetLastOrderReceivedFromSubscriptionid
1084 $order = &GetLastOrderReceivedFromSubscriptionid($subscriptionid);
1086 Returns a reference-to-hash describing the last order received for a subscription.
1090 sub GetLastOrderReceivedFromSubscriptionid {
1091 my ( $subscriptionid ) = @_;
1092 my $dbh = C4::Context->dbh;
1094 SELECT * FROM aqorders
1095 LEFT JOIN subscription
1096 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1097 WHERE aqorders.subscriptionid = ?
1098 AND aqorders.datereceived =
1100 SELECT MAX( aqorders.datereceived )
1102 LEFT JOIN subscription
1103 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1104 WHERE aqorders.subscriptionid = ?
1105 AND aqorders.datereceived IS NOT NULL
1107 ORDER BY ordernumber DESC
1110 my $sth = $dbh->prepare( $query );
1111 $sth->execute( $subscriptionid, $subscriptionid );
1112 my $order = $sth->fetchrow_hashref;
1118 #------------------------------------------------------------#
1122 &NewOrder(\%hashref);
1124 Adds a new order to the database. Any argument that isn't described
1125 below is the new value of the field with the same name in the aqorders
1126 table of the Koha database.
1130 =item $hashref->{'basketno'} is the basketno foreign key in aqorders, it is mandatory
1132 =item $hashref->{'ordernumber'} is a "minimum order number."
1134 =item $hashref->{'budgetdate'} is effectively ignored.
1135 If it's undef (anything false) or the string 'now', the current day is used.
1136 Else, the upcoming July 1st is used.
1138 =item $hashref->{'subscription'} may be either "yes", or anything else for "no".
1140 =item $hashref->{'uncertainprice'} may be 0 for "the price is known" or 1 for "the price is uncertain"
1142 =item defaults entrydate to Now
1144 The following keys are used: "biblionumber", "title", "basketno", "quantity", "notes", "biblioitemnumber", "rrp", "ecost", "gstrate", "unitprice", "subscription", "sort1", "sort2", "booksellerinvoicenumber", "listprice", "budgetdate", "purchaseordernumber", "branchcode", "booksellerinvoicenumber", "budget_id".
1151 my $orderinfo = shift;
1152 #### ------------------------------
1153 my $dbh = C4::Context->dbh;
1157 # if these parameters are missing, we can't continue
1158 for my $key (qw/basketno quantity biblionumber budget_id/) {
1159 croak "Mandatory parameter $key missing" unless $orderinfo->{$key};
1162 if ( defined $orderinfo->{subscription} && $orderinfo->{'subscription'} eq 'yes' ) {
1163 $orderinfo->{'subscription'} = 1;
1165 $orderinfo->{'subscription'} = 0;
1167 $orderinfo->{'entrydate'} ||= C4::Dates->new()->output("iso");
1168 if (!$orderinfo->{quantityreceived}) {
1169 $orderinfo->{quantityreceived} = 0;
1172 my $ordernumber=InsertInTable("aqorders",$orderinfo);
1173 if (not $orderinfo->{parent_ordernumber}) {
1174 my $sth = $dbh->prepare("
1176 SET parent_ordernumber = ordernumber
1177 WHERE ordernumber = ?
1179 $sth->execute($ordernumber);
1181 return ( $orderinfo->{'basketno'}, $ordernumber );
1186 #------------------------------------------------------------#
1195 my ($itemnumber, $ordernumber) = @_;
1196 my $dbh = C4::Context->dbh;
1198 INSERT INTO aqorders_items
1199 (itemnumber, ordernumber)
1202 my $sth = $dbh->prepare($query);
1203 $sth->execute( $itemnumber, $ordernumber);
1206 #------------------------------------------------------------#
1210 &ModOrder(\%hashref);
1212 Modifies an existing order. Updates the order with order number
1213 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All
1214 other keys of the hash update the fields with the same name in the aqorders
1215 table of the Koha database.
1220 my $orderinfo = shift;
1222 die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '' ;
1223 die "Biblionumber is required" if $orderinfo->{'biblionumber'} eq '';
1225 my $dbh = C4::Context->dbh;
1228 # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
1229 $orderinfo->{uncertainprice}=1 if $orderinfo->{uncertainprice};
1231 # delete($orderinfo->{'branchcode'});
1232 # the hash contains a lot of entries not in aqorders, so get the columns ...
1233 my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1235 my $colnames = $sth->{NAME};
1236 #FIXME Be careful. If aqorders would have columns with diacritics,
1237 #you should need to decode what you get back from NAME.
1238 #See report 10110 and guided_reports.pl
1239 my $query = "UPDATE aqorders SET ";
1241 foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1242 # ... and skip hash entries that are not in the aqorders table
1243 # FIXME : probably not the best way to do it (would be better to have a correct hash)
1244 next unless grep(/^$orderinfokey$/, @$colnames);
1245 $query .= "$orderinfokey=?, ";
1246 push(@params, $orderinfo->{$orderinfokey});
1249 $query .= "timestamp=NOW() WHERE ordernumber=?";
1250 # push(@params, $specorderinfo{'ordernumber'});
1251 push(@params, $orderinfo->{'ordernumber'} );
1252 $sth = $dbh->prepare($query);
1253 $sth->execute(@params);
1257 #------------------------------------------------------------#
1261 ModItemOrder($itemnumber, $ordernumber);
1263 Modifies the ordernumber of an item in aqorders_items.
1268 my ($itemnumber, $ordernumber) = @_;
1270 return unless ($itemnumber and $ordernumber);
1272 my $dbh = C4::Context->dbh;
1274 UPDATE aqorders_items
1276 WHERE itemnumber = ?
1278 my $sth = $dbh->prepare($query);
1279 return $sth->execute($ordernumber, $itemnumber);
1282 #------------------------------------------------------------#
1285 =head3 ModOrderBibliotemNumber
1287 &ModOrderBiblioitemNumber($biblioitemnumber,$ordernumber, $biblionumber);
1289 Modifies the biblioitemnumber for an existing order.
1290 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
1294 #FIXME: is this used at all?
1295 sub ModOrderBiblioitemNumber {
1296 my ($biblioitemnumber,$ordernumber, $biblionumber) = @_;
1297 my $dbh = C4::Context->dbh;
1300 SET biblioitemnumber = ?
1301 WHERE ordernumber = ?
1302 AND biblionumber = ?";
1303 my $sth = $dbh->prepare($query);
1304 $sth->execute( $biblioitemnumber, $ordernumber, $biblionumber );
1307 =head3 GetCancelledOrders
1309 my @orders = GetCancelledOrders($basketno, $orderby);
1311 Returns cancelled orders for a basket
1315 sub GetCancelledOrders {
1316 my ( $basketno, $orderby ) = @_;
1318 return () unless $basketno;
1320 my $dbh = C4::Context->dbh;
1322 SELECT biblio.*, biblioitems.*, aqorders.*, aqbudgets.*
1324 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1325 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1326 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1328 AND (datecancellationprinted IS NOT NULL
1329 AND datecancellationprinted <> '0000-00-00')
1332 $orderby = "aqorders.datecancellationprinted desc, aqorders.timestamp desc"
1334 $query .= " ORDER BY $orderby";
1335 my $sth = $dbh->prepare($query);
1336 $sth->execute($basketno);
1337 my $results = $sth->fetchall_arrayref( {} );
1343 #------------------------------------------------------------#
1345 =head3 ModReceiveOrder
1347 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
1348 $unitprice, $invoiceid, $biblioitemnumber,
1349 $bookfund, $rrp, \@received_itemnumbers);
1351 Updates an order, to reflect the fact that it was received, at least
1352 in part. All arguments not mentioned below update the fields with the
1353 same name in the aqorders table of the Koha database.
1355 If a partial order is received, splits the order into two.
1357 Updates the order with bibilionumber C<$biblionumber> and ordernumber
1363 sub ModReceiveOrder {
1365 $biblionumber, $ordernumber, $quantrec, $user, $cost, $ecost,
1366 $invoiceid, $rrp, $budget_id, $datereceived, $received_items
1370 my $dbh = C4::Context->dbh;
1371 $datereceived = C4::Dates->output('iso') unless $datereceived;
1372 my $suggestionid = GetSuggestionFromBiblionumber( $biblionumber );
1373 if ($suggestionid) {
1374 ModSuggestion( {suggestionid=>$suggestionid,
1375 STATUS=>'AVAILABLE',
1376 biblionumber=> $biblionumber}
1380 my $sth=$dbh->prepare("
1381 SELECT * FROM aqorders
1382 WHERE biblionumber=? AND aqorders.ordernumber=?");
1384 $sth->execute($biblionumber,$ordernumber);
1385 my $order = $sth->fetchrow_hashref();
1388 my $new_ordernumber = $ordernumber;
1389 if ( $order->{quantity} > $quantrec ) {
1390 # Split order line in two parts: the first is the original order line
1391 # without received items (the quantity is decreased),
1392 # the second part is a new order line with quantity=quantityrec
1393 # (entirely received)
1394 $sth=$dbh->prepare("
1397 WHERE ordernumber = ?
1400 $sth->execute($order->{quantity} - $quantrec, $ordernumber);
1404 delete $order->{'ordernumber'};
1405 $order->{'quantity'} = $quantrec;
1406 $order->{'quantityreceived'} = $quantrec;
1407 $order->{'datereceived'} = $datereceived;
1408 $order->{'invoiceid'} = $invoiceid;
1409 $order->{'unitprice'} = $cost;
1410 $order->{'rrp'} = $rrp;
1411 $order->{ecost} = $ecost;
1412 $order->{'orderstatus'} = 3; # totally received
1414 ( $basketno, $new_ordernumber ) = NewOrder($order);
1416 if ($received_items) {
1417 foreach my $itemnumber (@$received_items) {
1418 ModItemOrder($itemnumber, $new_ordernumber);
1422 $sth=$dbh->prepare("update aqorders
1423 set quantityreceived=?,datereceived=?,invoiceid=?,
1424 unitprice=?,rrp=?,ecost=?
1425 where biblionumber=? and ordernumber=?");
1426 $sth->execute($quantrec,$datereceived,$invoiceid,$cost,$rrp,$ecost,$biblionumber,$ordernumber);
1429 return ($datereceived, $new_ordernumber);
1432 =head3 CancelReceipt
1434 my $parent_ordernumber = CancelReceipt($ordernumber);
1436 Cancel an order line receipt and update the parent order line, as if no
1438 If items are created at receipt (AcqCreateItem = receiving) then delete
1444 my $ordernumber = shift;
1446 return unless $ordernumber;
1448 my $dbh = C4::Context->dbh;
1450 SELECT datereceived, parent_ordernumber, quantity
1452 WHERE ordernumber = ?
1454 my $sth = $dbh->prepare($query);
1455 $sth->execute($ordernumber);
1456 my $order = $sth->fetchrow_hashref;
1458 warn "CancelReceipt: order $ordernumber does not exist";
1461 unless($order->{'datereceived'}) {
1462 warn "CancelReceipt: order $ordernumber is not received";
1466 my $parent_ordernumber = $order->{'parent_ordernumber'};
1468 if($parent_ordernumber == $ordernumber || not $parent_ordernumber) {
1469 # The order line has no parent, just mark it as not received
1472 SET quantityreceived = ?,
1475 WHERE ordernumber = ?
1477 $sth = $dbh->prepare($query);
1478 $sth->execute(0, undef, undef, $ordernumber);
1480 # The order line has a parent, increase parent quantity and delete
1483 SELECT quantity, datereceived
1485 WHERE ordernumber = ?
1487 $sth = $dbh->prepare($query);
1488 $sth->execute($parent_ordernumber);
1489 my $parent_order = $sth->fetchrow_hashref;
1490 unless($parent_order) {
1491 warn "Parent order $parent_ordernumber does not exist.";
1494 if($parent_order->{'datereceived'}) {
1495 warn "CancelReceipt: parent order is received.".
1496 " Can't cancel receipt.";
1502 WHERE ordernumber = ?
1504 $sth = $dbh->prepare($query);
1505 my $rv = $sth->execute(
1506 $order->{'quantity'} + $parent_order->{'quantity'},
1510 warn "Cannot update parent order line, so do not cancel".
1514 if(C4::Context->preference('AcqCreateItem') eq 'receiving') {
1515 # Remove items that were created at receipt
1517 DELETE FROM items, aqorders_items
1518 USING items, aqorders_items
1519 WHERE items.itemnumber = ? AND aqorders_items.itemnumber = ?
1521 $sth = $dbh->prepare($query);
1522 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
1523 foreach my $itemnumber (@itemnumbers) {
1524 $sth->execute($itemnumber, $itemnumber);
1528 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
1529 foreach my $itemnumber (@itemnumbers) {
1530 ModItemOrder($itemnumber, $parent_ordernumber);
1535 DELETE FROM aqorders
1536 WHERE ordernumber = ?
1538 $sth = $dbh->prepare($query);
1539 $sth->execute($ordernumber);
1543 return $parent_ordernumber;
1546 #------------------------------------------------------------#
1550 @results = &SearchOrder($search, $biblionumber, $complete);
1552 Searches for orders.
1554 C<$search> may take one of several forms: if it is an ISBN,
1555 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
1556 order number, C<&ordersearch> returns orders with that order number
1557 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
1558 to be a space-separated list of search terms; in this case, all of the
1559 terms must appear in the title (matching the beginning of title
1562 If C<$complete> is C<yes>, the results will include only completed
1563 orders. In any case, C<&ordersearch> ignores cancelled orders.
1565 C<&ordersearch> returns an array.
1566 C<@results> is an array of references-to-hash with the following keys:
1572 =item C<seriestitle>
1583 #### -------- SearchOrder-------------------------------
1584 my ( $ordernumber, $search, $ean, $supplierid, $basket ) = @_;
1586 my $dbh = C4::Context->dbh;
1591 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1592 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1593 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1594 WHERE (datecancellationprinted is NULL)";
1597 $query .= " AND (aqorders.ordernumber=?)";
1598 push @args, $ordernumber;
1601 $query .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
1602 push @args, ("%$search%","%$search%","%$search%");
1605 $query .= " AND biblioitems.ean = ?";
1609 $query .= "AND aqbasket.booksellerid = ?";
1610 push @args, $supplierid;
1613 $query .= "AND aqorders.basketno = ?";
1614 push @args, $basket;
1617 my $sth = $dbh->prepare($query);
1618 $sth->execute(@args);
1619 my $results = $sth->fetchall_arrayref({});
1624 #------------------------------------------------------------#
1628 &DelOrder($biblionumber, $ordernumber);
1630 Cancel the order with the given order and biblio numbers. It does not
1631 delete any entries in the aqorders table, it merely marks them as
1637 my ( $bibnum, $ordernumber ) = @_;
1638 my $dbh = C4::Context->dbh;
1641 SET datecancellationprinted=now()
1642 WHERE biblionumber=? AND ordernumber=?
1644 my $sth = $dbh->prepare($query);
1645 $sth->execute( $bibnum, $ordernumber );
1647 my @itemnumbers = GetItemnumbersFromOrder( $ordernumber );
1648 foreach my $itemnumber (@itemnumbers){
1649 C4::Items::DelItem( $dbh, $bibnum, $itemnumber );
1654 =head2 FUNCTIONS ABOUT PARCELS
1658 #------------------------------------------------------------#
1662 @results = &GetParcel($booksellerid, $code, $date);
1664 Looks up all of the received items from the supplier with the given
1665 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
1667 C<@results> is an array of references-to-hash. The keys of each element are fields from
1668 the aqorders, biblio, and biblioitems tables of the Koha database.
1670 C<@results> is sorted alphabetically by book title.
1675 #gets all orders from a certain supplier, orders them alphabetically
1676 my ( $supplierid, $code, $datereceived ) = @_;
1677 my $dbh = C4::Context->dbh;
1680 if $code; # add % if we search on a given code (otherwise, let him empty)
1682 SELECT authorisedby,
1687 aqorders.biblionumber,
1688 aqorders.ordernumber,
1689 aqorders.parent_ordernumber,
1691 aqorders.quantityreceived,
1699 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
1700 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1701 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1702 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1704 aqbasket.booksellerid = ?
1705 AND aqinvoices.invoicenumber LIKE ?
1706 AND aqorders.datereceived = ? ";
1708 my @query_params = ( $supplierid, $code, $datereceived );
1709 if ( C4::Context->preference("IndependantBranches") ) {
1710 my $userenv = C4::Context->userenv;
1711 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1712 $strsth .= " and (borrowers.branchcode = ?
1713 or borrowers.branchcode = '')";
1714 push @query_params, $userenv->{branch};
1717 $strsth .= " ORDER BY aqbasket.basketno";
1718 # ## parcelinformation : $strsth
1719 my $sth = $dbh->prepare($strsth);
1720 $sth->execute( @query_params );
1721 while ( my $data = $sth->fetchrow_hashref ) {
1722 push( @results, $data );
1724 # ## countparcelbiblio: scalar(@results)
1730 #------------------------------------------------------------#
1734 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1736 get a lists of parcels.
1743 is the bookseller this function has to get parcels.
1746 To know on what criteria the results list has to be ordered.
1749 is the booksellerinvoicenumber.
1751 =item $datefrom & $dateto
1752 to know on what date this function has to filter its search.
1757 a pointer on a hash list containing parcel informations as such :
1763 =item Last operation
1765 =item Number of biblio
1767 =item Number of items
1774 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
1775 my $dbh = C4::Context->dbh;
1776 my @query_params = ();
1778 SELECT aqinvoices.invoicenumber,
1779 datereceived,purchaseordernumber,
1780 count(DISTINCT biblionumber) AS biblio,
1781 sum(quantity) AS itemsexpected,
1782 sum(quantityreceived) AS itemsreceived
1783 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
1784 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1785 WHERE aqbasket.booksellerid = ? and datereceived IS NOT NULL
1787 push @query_params, $bookseller;
1789 if ( defined $code ) {
1790 $strsth .= ' and aqinvoices.invoicenumber like ? ';
1791 # add a % to the end of the code to allow stemming.
1792 push @query_params, "$code%";
1795 if ( defined $datefrom ) {
1796 $strsth .= ' and datereceived >= ? ';
1797 push @query_params, $datefrom;
1800 if ( defined $dateto ) {
1801 $strsth .= 'and datereceived <= ? ';
1802 push @query_params, $dateto;
1805 $strsth .= "group by aqinvoices.invoicenumber,datereceived ";
1807 # can't use a placeholder to place this column name.
1808 # but, we could probably be checking to make sure it is a column that will be fetched.
1809 $strsth .= "order by $order " if ($order);
1811 my $sth = $dbh->prepare($strsth);
1813 $sth->execute( @query_params );
1814 my $results = $sth->fetchall_arrayref({});
1819 #------------------------------------------------------------#
1821 =head3 GetLateOrders
1823 @results = &GetLateOrders;
1825 Searches for bookseller with late orders.
1828 the table of supplier with late issues. This table is full of hashref.
1834 my $supplierid = shift;
1836 my $estimateddeliverydatefrom = shift;
1837 my $estimateddeliverydateto = shift;
1839 my $dbh = C4::Context->dbh;
1841 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
1842 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
1844 my @query_params = ();
1846 SELECT aqbasket.basketno,
1847 aqorders.ordernumber,
1848 DATE(aqbasket.closedate) AS orderdate,
1849 aqorders.rrp AS unitpricesupplier,
1850 aqorders.ecost AS unitpricelib,
1851 aqorders.claims_count AS claims_count,
1852 aqorders.claimed_date AS claimed_date,
1853 aqbudgets.budget_name AS budget,
1854 borrowers.branchcode AS branch,
1855 aqbooksellers.name AS supplier,
1856 aqbooksellers.id AS supplierid,
1857 biblio.author, biblio.title,
1858 biblioitems.publishercode AS publisher,
1859 biblioitems.publicationyear,
1860 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
1864 aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1865 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1866 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
1867 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
1868 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1869 WHERE aqorders.basketno = aqbasket.basketno
1870 AND ( datereceived = ''
1871 OR datereceived IS NULL
1872 OR aqorders.quantityreceived < aqorders.quantity
1874 AND aqbasket.closedate IS NOT NULL
1875 AND (aqorders.datecancellationprinted IS NULL OR aqorders.datecancellationprinted='0000-00-00')
1878 if ($dbdriver eq "mysql") {
1880 aqorders.quantity - COALESCE(aqorders.quantityreceived,0) AS quantity,
1881 (aqorders.quantity - COALESCE(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1882 DATEDIFF(CAST(now() AS date),closedate) AS latesince
1884 if ( defined $delay ) {
1885 $from .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) " ;
1886 push @query_params, $delay;
1889 HAVING quantity <> 0
1890 AND unitpricesupplier <> 0
1891 AND unitpricelib <> 0
1894 # FIXME: account for IFNULL as above
1896 aqorders.quantity AS quantity,
1897 aqorders.quantity * aqorders.rrp AS subtotal,
1898 (CAST(now() AS date) - closedate) AS latesince
1900 if ( defined $delay ) {
1901 $from .= " AND (closedate <= (CAST(now() AS date) -(INTERVAL ? DAY)) ";
1902 push @query_params, $delay;
1905 if (defined $supplierid) {
1906 $from .= ' AND aqbasket.booksellerid = ? ';
1907 push @query_params, $supplierid;
1909 if (defined $branch) {
1910 $from .= ' AND borrowers.branchcode LIKE ? ';
1911 push @query_params, $branch;
1914 if ( defined $estimateddeliverydatefrom or defined $estimateddeliverydateto ) {
1915 $from .= ' AND aqbooksellers.deliverytime IS NOT NULL ';
1917 if ( defined $estimateddeliverydatefrom ) {
1918 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
1919 push @query_params, $estimateddeliverydatefrom;
1921 if ( defined $estimateddeliverydateto ) {
1922 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
1923 push @query_params, $estimateddeliverydateto;
1925 if ( defined $estimateddeliverydatefrom and not defined $estimateddeliverydateto ) {
1926 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
1928 if (C4::Context->preference("IndependantBranches")
1929 && C4::Context->userenv
1930 && C4::Context->userenv->{flags} != 1 ) {
1931 $from .= ' AND borrowers.branchcode LIKE ? ';
1932 push @query_params, C4::Context->userenv->{branch};
1934 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
1935 $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
1936 my $sth = $dbh->prepare($query);
1937 $sth->execute(@query_params);
1939 while (my $data = $sth->fetchrow_hashref) {
1940 $data->{orderdate} = format_date($data->{orderdate});
1941 $data->{claimed_date} = format_date($data->{claimed_date});
1942 push @results, $data;
1947 #------------------------------------------------------------#
1951 (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( %params );
1953 Retreives some acquisition history information
1961 basket - search both basket name and number
1962 booksellerinvoicenumber
1965 $order_loop is a list of hashrefs that each look like this:
1967 'author' => 'Twain, Mark',
1969 'biblionumber' => '215',
1971 'creationdate' => 'MM/DD/YYYY',
1972 'datereceived' => undef,
1975 'invoicenumber' => undef,
1977 'ordernumber' => '1',
1979 'quantityreceived' => undef,
1980 'title' => 'The Adventures of Huckleberry Finn'
1982 $total_qty is the sum of all of the quantities in $order_loop
1983 $total_price is the cost of each in $order_loop times the quantity
1984 $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
1989 # don't run the query if there are no parameters (list would be too long for sure !)
1990 croak "No search params" unless @_;
1992 my $title = $params{title};
1993 my $author = $params{author};
1994 my $isbn = $params{isbn};
1995 my $ean = $params{ean};
1996 my $name = $params{name};
1997 my $from_placed_on = $params{from_placed_on};
1998 my $to_placed_on = $params{to_placed_on};
1999 my $basket = $params{basket};
2000 my $booksellerinvoicenumber = $params{booksellerinvoicenumber};
2001 my $basketgroupname = $params{basketgroupname};
2004 my $total_qtyreceived = 0;
2005 my $total_price = 0;
2007 my $dbh = C4::Context->dbh;
2015 aqbasket.basketname,
2016 aqbasket.basketgroupid,
2017 aqbasketgroups.name as groupname,
2019 aqbasket.creationdate,
2020 aqorders.datereceived,
2022 aqorders.quantityreceived,
2024 aqorders.ordernumber,
2026 aqinvoices.invoicenumber,
2027 aqbooksellers.id as id,
2028 aqorders.biblionumber
2030 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
2031 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
2032 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
2033 LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
2034 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
2035 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid";
2037 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
2038 if ( C4::Context->preference("IndependantBranches") );
2040 $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
2042 my @query_params = ();
2045 $query .= " AND biblio.title LIKE ? ";
2046 $title =~ s/\s+/%/g;
2047 push @query_params, "%$title%";
2051 $query .= " AND biblio.author LIKE ? ";
2052 push @query_params, "%$author%";
2056 $query .= " AND biblioitems.isbn LIKE ? ";
2057 push @query_params, "%$isbn%";
2059 if ( defined $ean and $ean ) {
2060 $query .= " AND biblioitems.ean = ? ";
2061 push @query_params, "$ean";
2064 $query .= " AND aqbooksellers.name LIKE ? ";
2065 push @query_params, "%$name%";
2068 if ( $from_placed_on ) {
2069 $query .= " AND creationdate >= ? ";
2070 push @query_params, $from_placed_on;
2073 if ( $to_placed_on ) {
2074 $query .= " AND creationdate <= ? ";
2075 push @query_params, $to_placed_on;
2079 if ($basket =~ m/^\d+$/) {
2080 $query .= " AND aqorders.basketno = ? ";
2081 push @query_params, $basket;
2083 $query .= " AND aqbasket.basketname LIKE ? ";
2084 push @query_params, "%$basket%";
2088 if ($booksellerinvoicenumber) {
2089 $query .= " AND aqinvoices.invoicenumber LIKE ? ";
2090 push @query_params, "%$booksellerinvoicenumber%";
2093 if ($basketgroupname) {
2094 $query .= " AND aqbasketgroups.name LIKE ? ";
2095 push @query_params, "%$basketgroupname%";
2098 if ( C4::Context->preference("IndependantBranches") ) {
2099 my $userenv = C4::Context->userenv;
2100 if ( $userenv && ($userenv->{flags} || 0) != 1 ) {
2101 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
2102 push @query_params, $userenv->{branch};
2105 $query .= " ORDER BY id";
2106 my $sth = $dbh->prepare($query);
2107 $sth->execute( @query_params );
2109 while ( my $line = $sth->fetchrow_hashref ) {
2110 $line->{count} = $cnt++;
2111 $line->{toggle} = 1 if $cnt % 2;
2112 push @order_loop, $line;
2113 $total_qty += $line->{'quantity'};
2114 $total_qtyreceived += $line->{'quantityreceived'};
2115 $total_price += $line->{'quantity'} * $line->{'ecost'};
2117 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
2120 =head2 GetRecentAcqui
2122 $results = GetRecentAcqui($days);
2124 C<$results> is a ref to a table which containts hashref
2128 sub GetRecentAcqui {
2130 my $dbh = C4::Context->dbh;
2134 ORDER BY timestamp DESC
2137 my $sth = $dbh->prepare($query);
2139 my $results = $sth->fetchall_arrayref({});
2145 $contractlist = &GetContracts($booksellerid, $activeonly);
2147 Looks up the contracts that belong to a bookseller
2149 Returns a list of contracts
2153 =item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
2155 =item C<$activeonly> if exists get only contracts that are still active.
2162 my ( $booksellerid, $activeonly ) = @_;
2163 my $dbh = C4::Context->dbh;
2165 if (! $activeonly) {
2169 WHERE booksellerid=?
2174 WHERE booksellerid=?
2175 AND contractenddate >= CURDATE( )";
2177 my $sth = $dbh->prepare($query);
2178 $sth->execute( $booksellerid );
2180 while (my $data = $sth->fetchrow_hashref ) {
2181 push(@results, $data);
2187 #------------------------------------------------------------#
2191 $contract = &GetContract($contractID);
2193 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
2200 my ( $contractno ) = @_;
2201 my $dbh = C4::Context->dbh;
2205 WHERE contractnumber=?
2208 my $sth = $dbh->prepare($query);
2209 $sth->execute( $contractno );
2210 my $result = $sth->fetchrow_hashref;
2218 &AddClaim($ordernumber);
2220 Add a claim for an order
2226 my ($ordernumber) = @_;
2227 my $dbh = C4::Context->dbh;
2230 claims_count = claims_count + 1,
2231 claimed_date = CURDATE()
2232 WHERE ordernumber = ?
2234 my $sth = $dbh->prepare($query);
2235 $sth->execute($ordernumber);
2240 my @invoices = GetInvoices(
2241 invoicenumber => $invoicenumber,
2242 suppliername => $suppliername,
2243 shipmentdatefrom => $shipmentdatefrom, # ISO format
2244 shipmentdateto => $shipmentdateto, # ISO format
2245 billingdatefrom => $billingdatefrom, # ISO format
2246 billingdateto => $billingdateto, # ISO format
2247 isbneanissn => $isbn_or_ean_or_issn,
2250 publisher => $publisher,
2251 publicationyear => $publicationyear,
2252 branchcode => $branchcode,
2253 order_by => $order_by
2256 Return a list of invoices that match all given criteria.
2258 $order_by is "column_name (asc|desc)", where column_name is any of
2259 'invoicenumber', 'booksellerid', 'shipmentdate', 'billingdate', 'closedate',
2260 'shipmentcost', 'shipmentcost_budgetid'.
2262 asc is the default if omitted
2269 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2270 closedate shipmentcost shipmentcost_budgetid);
2272 my $dbh = C4::Context->dbh;
2274 SELECT aqinvoices.*, aqbooksellers.name AS suppliername,
2277 aqorders.datereceived IS NOT NULL,
2278 aqorders.biblionumber,
2281 ) AS receivedbiblios,
2282 SUM(aqorders.quantityreceived) AS receiveditems
2284 LEFT JOIN aqbooksellers ON aqbooksellers.id = aqinvoices.booksellerid
2285 LEFT JOIN aqorders ON aqorders.invoiceid = aqinvoices.invoiceid
2286 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2287 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
2288 LEFT JOIN subscription ON biblio.biblionumber = subscription.biblionumber
2293 if($args{supplierid}) {
2294 push @bind_strs, " aqinvoices.booksellerid = ? ";
2295 push @bind_args, $args{supplierid};
2297 if($args{invoicenumber}) {
2298 push @bind_strs, " aqinvoices.invoicenumber LIKE ? ";
2299 push @bind_args, "%$args{invoicenumber}%";
2301 if($args{suppliername}) {
2302 push @bind_strs, " aqbooksellers.name LIKE ? ";
2303 push @bind_args, "%$args{suppliername}%";
2305 if($args{shipmentdatefrom}) {
2306 push @bind_strs, " aqinvoices.shipementdate >= ? ";
2307 push @bind_args, $args{shipmentdatefrom};
2309 if($args{shipmentdateto}) {
2310 push @bind_strs, " aqinvoices.shipementdate <= ? ";
2311 push @bind_args, $args{shipmentdateto};
2313 if($args{billingdatefrom}) {
2314 push @bind_strs, " aqinvoices.billingdate >= ? ";
2315 push @bind_args, $args{billingdatefrom};
2317 if($args{billingdateto}) {
2318 push @bind_strs, " aqinvoices.billingdate <= ? ";
2319 push @bind_args, $args{billingdateto};
2321 if($args{isbneanissn}) {
2322 push @bind_strs, " (biblioitems.isbn LIKE ? OR biblioitems.ean LIKE ? OR biblioitems.issn LIKE ? ) ";
2323 push @bind_args, $args{isbneanissn}, $args{isbneanissn}, $args{isbneanissn};
2326 push @bind_strs, " biblio.title LIKE ? ";
2327 push @bind_args, $args{title};
2330 push @bind_strs, " biblio.author LIKE ? ";
2331 push @bind_args, $args{author};
2333 if($args{publisher}) {
2334 push @bind_strs, " biblioitems.publishercode LIKE ? ";
2335 push @bind_args, $args{publisher};
2337 if($args{publicationyear}) {
2338 push @bind_strs, " biblioitems.publicationyear = ? ";
2339 push @bind_args, $args{publicationyear};
2341 if($args{branchcode}) {
2342 push @bind_strs, " aqorders.branchcode = ? ";
2343 push @bind_args, $args{branchcode};
2346 $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
2347 $query .= " GROUP BY aqinvoices.invoiceid ";
2349 if($args{order_by}) {
2350 my ($column, $direction) = split / /, $args{order_by};
2351 if(grep /^$column$/, @columns) {
2352 $direction ||= 'ASC';
2353 $query .= " ORDER BY $column $direction";
2357 my $sth = $dbh->prepare($query);
2358 $sth->execute(@bind_args);
2360 my $results = $sth->fetchall_arrayref({});
2366 my $invoice = GetInvoice($invoiceid);
2368 Get informations about invoice with given $invoiceid
2370 Return a hash filled with aqinvoices.* fields
2375 my ($invoiceid) = @_;
2378 return unless $invoiceid;
2380 my $dbh = C4::Context->dbh;
2386 my $sth = $dbh->prepare($query);
2387 $sth->execute($invoiceid);
2389 $invoice = $sth->fetchrow_hashref;
2393 =head3 GetInvoiceDetails
2395 my $invoice = GetInvoiceDetails($invoiceid)
2397 Return informations about an invoice + the list of related order lines
2399 Orders informations are in $invoice->{orders} (array ref)
2403 sub GetInvoiceDetails {
2404 my ($invoiceid) = @_;
2406 if ( !defined $invoiceid ) {
2407 carp 'GetInvoiceDetails called without an invoiceid';
2411 my $dbh = C4::Context->dbh;
2413 SELECT aqinvoices.*, aqbooksellers.name AS suppliername
2415 LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
2418 my $sth = $dbh->prepare($query);
2419 $sth->execute($invoiceid);
2421 my $invoice = $sth->fetchrow_hashref;
2424 SELECT aqorders.*, biblio.*
2426 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2429 $sth = $dbh->prepare($query);
2430 $sth->execute($invoiceid);
2431 $invoice->{orders} = $sth->fetchall_arrayref({});
2432 $invoice->{orders} ||= []; # force an empty arrayref if fetchall_arrayref fails
2439 my $invoiceid = AddInvoice(
2440 invoicenumber => $invoicenumber,
2441 booksellerid => $booksellerid,
2442 shipmentdate => $shipmentdate,
2443 billingdate => $billingdate,
2444 closedate => $closedate,
2445 shipmentcost => $shipmentcost,
2446 shipmentcost_budgetid => $shipmentcost_budgetid
2449 Create a new invoice and return its id or undef if it fails.
2456 return unless(%invoice and $invoice{invoicenumber});
2458 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2459 closedate shipmentcost shipmentcost_budgetid);
2463 foreach my $key (keys %invoice) {
2464 if(0 < grep(/^$key$/, @columns)) {
2465 push @set_strs, "$key = ?";
2466 push @set_args, ($invoice{$key} || undef);
2472 my $dbh = C4::Context->dbh;
2473 my $query = "INSERT INTO aqinvoices SET ";
2474 $query .= join (",", @set_strs);
2475 my $sth = $dbh->prepare($query);
2476 $rv = $sth->execute(@set_args);
2478 $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
2487 invoiceid => $invoiceid, # Mandatory
2488 invoicenumber => $invoicenumber,
2489 booksellerid => $booksellerid,
2490 shipmentdate => $shipmentdate,
2491 billingdate => $billingdate,
2492 closedate => $closedate,
2493 shipmentcost => $shipmentcost,
2494 shipmentcost_budgetid => $shipmentcost_budgetid
2497 Modify an invoice, invoiceid is mandatory.
2499 Return undef if it fails.
2506 return unless(%invoice and $invoice{invoiceid});
2508 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2509 closedate shipmentcost shipmentcost_budgetid);
2513 foreach my $key (keys %invoice) {
2514 if(0 < grep(/^$key$/, @columns)) {
2515 push @set_strs, "$key = ?";
2516 push @set_args, ($invoice{$key} || undef);
2520 my $dbh = C4::Context->dbh;
2521 my $query = "UPDATE aqinvoices SET ";
2522 $query .= join(",", @set_strs);
2523 $query .= " WHERE invoiceid = ?";
2525 my $sth = $dbh->prepare($query);
2526 $sth->execute(@set_args, $invoice{invoiceid});
2531 CloseInvoice($invoiceid);
2535 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
2540 my ($invoiceid) = @_;
2542 return unless $invoiceid;
2544 my $dbh = C4::Context->dbh;
2547 SET closedate = CAST(NOW() AS DATE)
2550 my $sth = $dbh->prepare($query);
2551 $sth->execute($invoiceid);
2554 =head3 ReopenInvoice
2556 ReopenInvoice($invoiceid);
2560 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => C4::Dates->new()->output('iso'))
2565 my ($invoiceid) = @_;
2567 return unless $invoiceid;
2569 my $dbh = C4::Context->dbh;
2572 SET closedate = NULL
2575 my $sth = $dbh->prepare($query);
2576 $sth->execute($invoiceid);
2584 Koha Development Team <http://koha-community.org/>