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
57 &GetOrderNumber &GetLateOrders &GetOrderFromItemnumber
58 &SearchOrder &GetHistory &GetRecentAcqui
59 &ModReceiveOrder &CancelReceipt &ModOrderBiblioitemNumber
62 &NewOrderItem &ModOrderItem &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);
573 Returns in a arrayref of hashref all about booksellers baskets, plus:
574 total_biblios: Number of distinct biblios in basket
575 total_items: Number of items in basket
576 expected_items: Number of non-received items in basket
580 sub GetBasketsInfosByBookseller {
581 my ($supplierid) = @_;
583 return unless $supplierid;
585 my $dbh = C4::Context->dbh;
588 SUM(aqorders.quantity) AS total_items,
589 COUNT(DISTINCT aqorders.biblionumber) AS total_biblios,
591 IF(aqorders.datereceived IS NULL
592 AND aqorders.datecancellationprinted IS NULL
597 LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
598 WHERE booksellerid = ?
599 GROUP BY aqbasket.basketno
601 my $sth = $dbh->prepare($query);
602 $sth->execute($supplierid);
603 return $sth->fetchall_arrayref({});
607 #------------------------------------------------------------#
609 =head3 GetBasketsByBasketgroup
611 $baskets = &GetBasketsByBasketgroup($basketgroupid);
613 Returns a reference to all baskets that belong to basketgroup $basketgroupid.
617 sub GetBasketsByBasketgroup {
618 my $basketgroupid = shift;
620 SELECT *, aqbasket.booksellerid as booksellerid
622 LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?
624 my $dbh = C4::Context->dbh;
625 my $sth = $dbh->prepare($query);
626 $sth->execute($basketgroupid);
627 my $results = $sth->fetchall_arrayref({});
632 #------------------------------------------------------------#
634 =head3 NewBasketgroup
636 $basketgroupid = NewBasketgroup(\%hashref);
638 Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
640 $hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
642 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
644 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
646 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
648 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
650 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
652 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
654 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
659 my $basketgroupinfo = shift;
660 die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
661 my $query = "INSERT INTO aqbasketgroups (";
663 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
664 if ( defined $basketgroupinfo->{$field} ) {
665 $query .= "$field, ";
666 push(@params, $basketgroupinfo->{$field});
669 $query .= "booksellerid) VALUES (";
674 push(@params, $basketgroupinfo->{'booksellerid'});
675 my $dbh = C4::Context->dbh;
676 my $sth = $dbh->prepare($query);
677 $sth->execute(@params);
678 my $basketgroupid = $dbh->{'mysql_insertid'};
679 if( $basketgroupinfo->{'basketlist'} ) {
680 foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
681 my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
682 my $sth2 = $dbh->prepare($query2);
683 $sth2->execute($basketgroupid, $basketno);
686 return $basketgroupid;
689 #------------------------------------------------------------#
691 =head3 ModBasketgroup
693 ModBasketgroup(\%hashref);
695 Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
697 $hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
699 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
701 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
703 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
705 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
707 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
709 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
711 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
716 my $basketgroupinfo = shift;
717 die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
718 my $dbh = C4::Context->dbh;
719 my $query = "UPDATE aqbasketgroups SET ";
721 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
722 if ( defined $basketgroupinfo->{$field} ) {
723 $query .= "$field=?, ";
724 push(@params, $basketgroupinfo->{$field});
729 $query .= " WHERE id=?";
730 push(@params, $basketgroupinfo->{'id'});
731 my $sth = $dbh->prepare($query);
732 $sth->execute(@params);
734 $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
735 $sth->execute($basketgroupinfo->{'id'});
737 if($basketgroupinfo->{'basketlist'} && @{$basketgroupinfo->{'basketlist'}}){
738 $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
739 foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
740 $sth->execute($basketgroupinfo->{'id'}, $basketno);
747 #------------------------------------------------------------#
749 =head3 DelBasketgroup
751 DelBasketgroup($basketgroupid);
753 Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
757 =item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
764 my $basketgroupid = shift;
765 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
766 my $query = "DELETE FROM aqbasketgroups WHERE id=?";
767 my $dbh = C4::Context->dbh;
768 my $sth = $dbh->prepare($query);
769 $sth->execute($basketgroupid);
773 #------------------------------------------------------------#
776 =head2 FUNCTIONS ABOUT ORDERS
778 =head3 GetBasketgroup
780 $basketgroup = &GetBasketgroup($basketgroupid);
782 Returns a reference to the hash containing all infermation about the basketgroup.
787 my $basketgroupid = shift;
788 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
789 my $query = "SELECT * FROM aqbasketgroups WHERE id=?";
790 my $dbh = C4::Context->dbh;
791 my $sth = $dbh->prepare($query);
792 $sth->execute($basketgroupid);
793 my $result = $sth->fetchrow_hashref;
798 #------------------------------------------------------------#
800 =head3 GetBasketgroups
802 $basketgroups = &GetBasketgroups($booksellerid);
804 Returns a reference to the array of all the basketgroups of bookseller $booksellerid.
808 sub GetBasketgroups {
809 my $booksellerid = shift;
810 die "bookseller id is required to edit a basketgroup" unless $booksellerid;
811 my $query = "SELECT * FROM aqbasketgroups WHERE booksellerid=? ORDER BY `id` DESC";
812 my $dbh = C4::Context->dbh;
813 my $sth = $dbh->prepare($query);
814 $sth->execute($booksellerid);
815 my $results = $sth->fetchall_arrayref({});
820 #------------------------------------------------------------#
822 =head2 FUNCTIONS ABOUT ORDERS
826 #------------------------------------------------------------#
828 =head3 GetPendingOrders
830 $orders = &GetPendingOrders($supplierid,$grouped,$owner,$basketno,$ordernumber,$search,$ean);
832 Finds pending orders from the bookseller with the given ID. Ignores
833 completed and cancelled orders.
835 C<$booksellerid> contains the bookseller identifier
836 C<$owner> contains 0 or 1. 0 means any owner. 1 means only the list of orders entered by the user itself.
837 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
838 in a single result line
839 C<$orders> is a reference-to-array; each element is a reference-to-hash.
841 Used also by the filter in parcel.pl
848 These give the value of the corresponding field in the aqorders table
849 of the Koha database.
851 Results are ordered from most to least recent.
855 sub GetPendingOrders {
856 my ($supplierid,$grouped,$owner,$basketno,$ordernumber,$search,$ean) = @_;
857 my $dbh = C4::Context->dbh;
859 SELECT ".($grouped?"count(*),":"")."aqbasket.basketno,
860 surname,firstname,biblio.*,biblioitems.isbn,
861 aqbasket.closedate, aqbasket.creationdate, aqbasket.basketname,
864 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
865 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
866 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
867 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
868 WHERE (quantity > quantityreceived OR quantityreceived is NULL)
869 AND datecancellationprinted IS NULL";
871 my $userenv = C4::Context->userenv;
872 if ( C4::Context->preference("IndependantBranches") ) {
873 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
874 $strsth .= " AND (borrowers.branchcode = ?
875 or borrowers.branchcode = '')";
876 push @query_params, $userenv->{branch};
880 $strsth .= " AND aqbasket.booksellerid = ?";
881 push @query_params, $supplierid;
884 $strsth .= " AND (aqorders.ordernumber=?)";
885 push @query_params, $ordernumber;
888 $strsth .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
889 push @query_params, ("%$search%","%$search%","%$search%");
892 $strsth .= " AND biblioitems.ean = ?";
893 push @query_params, $ean;
896 $strsth .= " AND aqbasket.basketno=? ";
897 push @query_params, $basketno;
900 $strsth .= " AND aqbasket.authorisedby=? ";
901 push @query_params, $userenv->{'number'};
903 $strsth .= " group by aqbasket.basketno" if $grouped;
904 $strsth .= " order by aqbasket.basketno";
905 my $sth = $dbh->prepare($strsth);
906 $sth->execute( @query_params );
907 my $results = $sth->fetchall_arrayref({});
912 #------------------------------------------------------------#
916 @orders = &GetOrders($basketnumber, $orderby);
918 Looks up the pending (non-cancelled) orders with the given basket
919 number. If C<$booksellerID> is non-empty, only orders from that seller
923 C<&basket> returns a two-element array. C<@orders> is an array of
924 references-to-hash, whose keys are the fields from the aqorders,
925 biblio, and biblioitems tables in the Koha database.
930 my ( $basketno, $orderby ) = @_;
931 my $dbh = C4::Context->dbh;
933 SELECT biblio.*,biblioitems.*,
938 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
939 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
940 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
942 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
945 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
946 $query .= " ORDER BY $orderby";
947 my $sth = $dbh->prepare($query);
948 $sth->execute($basketno);
949 my $results = $sth->fetchall_arrayref({});
954 #------------------------------------------------------------#
956 =head3 GetOrderNumber
958 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
960 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
962 Returns the number of this order.
966 =item C<$ordernumber> is the order number.
973 my ( $biblionumber,$biblioitemnumber ) = @_;
974 my $dbh = C4::Context->dbh;
979 AND biblioitemnumber=?
981 my $sth = $dbh->prepare($query);
982 $sth->execute( $biblionumber, $biblioitemnumber );
984 return $sth->fetchrow;
987 #------------------------------------------------------------#
991 $order = &GetOrder($ordernumber);
993 Looks up an order by order number.
995 Returns a reference-to-hash describing the order. The keys of
996 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
1001 my ($ordernumber) = @_;
1002 my $dbh = C4::Context->dbh;
1004 SELECT biblioitems.*, biblio.*, aqorders.*
1006 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
1007 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
1008 WHERE aqorders.ordernumber=?
1011 my $sth= $dbh->prepare($query);
1012 $sth->execute($ordernumber);
1013 my $data = $sth->fetchrow_hashref;
1018 #------------------------------------------------------------#
1022 &NewOrder(\%hashref);
1024 Adds a new order to the database. Any argument that isn't described
1025 below is the new value of the field with the same name in the aqorders
1026 table of the Koha database.
1030 =item $hashref->{'basketno'} is the basketno foreign key in aqorders, it is mandatory
1032 =item $hashref->{'ordernumber'} is a "minimum order number."
1034 =item $hashref->{'budgetdate'} is effectively ignored.
1035 If it's undef (anything false) or the string 'now', the current day is used.
1036 Else, the upcoming July 1st is used.
1038 =item $hashref->{'subscription'} may be either "yes", or anything else for "no".
1040 =item $hashref->{'uncertainprice'} may be 0 for "the price is known" or 1 for "the price is uncertain"
1042 =item defaults entrydate to Now
1044 The following keys are used: "biblionumber", "title", "basketno", "quantity", "notes", "biblioitemnumber", "rrp", "ecost", "gstrate", "unitprice", "subscription", "sort1", "sort2", "booksellerinvoicenumber", "listprice", "budgetdate", "purchaseordernumber", "branchcode", "booksellerinvoicenumber", "bookfundid".
1051 my $orderinfo = shift;
1052 #### ------------------------------
1053 my $dbh = C4::Context->dbh;
1057 # if these parameters are missing, we can't continue
1058 for my $key (qw/basketno quantity biblionumber budget_id/) {
1059 croak "Mandatory parameter $key missing" unless $orderinfo->{$key};
1062 if ( defined $orderinfo->{subscription} && $orderinfo->{'subscription'} eq 'yes' ) {
1063 $orderinfo->{'subscription'} = 1;
1065 $orderinfo->{'subscription'} = 0;
1067 $orderinfo->{'entrydate'} ||= C4::Dates->new()->output("iso");
1068 if (!$orderinfo->{quantityreceived}) {
1069 $orderinfo->{quantityreceived} = 0;
1072 my $ordernumber=InsertInTable("aqorders",$orderinfo);
1073 if (not $orderinfo->{parent_ordernumber}) {
1074 my $sth = $dbh->prepare("
1076 SET parent_ordernumber = ordernumber
1077 WHERE ordernumber = ?
1079 $sth->execute($ordernumber);
1081 return ( $orderinfo->{'basketno'}, $ordernumber );
1086 #------------------------------------------------------------#
1095 my ($itemnumber, $ordernumber) = @_;
1096 my $dbh = C4::Context->dbh;
1098 INSERT INTO aqorders_items
1099 (itemnumber, ordernumber)
1102 my $sth = $dbh->prepare($query);
1103 $sth->execute( $itemnumber, $ordernumber);
1106 #------------------------------------------------------------#
1110 &ModOrder(\%hashref);
1112 Modifies an existing order. Updates the order with order number
1113 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All
1114 other keys of the hash update the fields with the same name in the aqorders
1115 table of the Koha database.
1120 my $orderinfo = shift;
1122 die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '' ;
1123 die "Biblionumber is required" if $orderinfo->{'biblionumber'} eq '';
1125 my $dbh = C4::Context->dbh;
1128 # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
1129 $orderinfo->{uncertainprice}=1 if $orderinfo->{uncertainprice};
1131 # delete($orderinfo->{'branchcode'});
1132 # the hash contains a lot of entries not in aqorders, so get the columns ...
1133 my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1135 my $colnames = $sth->{NAME};
1136 my $query = "UPDATE aqorders SET ";
1138 foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1139 # ... and skip hash entries that are not in the aqorders table
1140 # FIXME : probably not the best way to do it (would be better to have a correct hash)
1141 next unless grep(/^$orderinfokey$/, @$colnames);
1142 $query .= "$orderinfokey=?, ";
1143 push(@params, $orderinfo->{$orderinfokey});
1146 $query .= "timestamp=NOW() WHERE ordernumber=?";
1147 # push(@params, $specorderinfo{'ordernumber'});
1148 push(@params, $orderinfo->{'ordernumber'} );
1149 $sth = $dbh->prepare($query);
1150 $sth->execute(@params);
1154 #------------------------------------------------------------#
1158 &ModOrderItem(\%hashref);
1160 Modifies the itemnumber in the aqorders_items table. The input hash needs three entities:
1164 =item - itemnumber: the old itemnumber
1165 =item - ordernumber: the order this item is attached to
1166 =item - newitemnumber: the new itemnumber we want to attach the line to
1173 my $orderiteminfo = shift;
1174 if (! $orderiteminfo->{'ordernumber'} || ! $orderiteminfo->{'itemnumber'} || ! $orderiteminfo->{'newitemnumber'}){
1175 die "Ordernumber, itemnumber and newitemnumber is required";
1178 my $dbh = C4::Context->dbh;
1180 my $query = "UPDATE aqorders_items set itemnumber=? where itemnumber=? and ordernumber=?";
1181 my @params = ($orderiteminfo->{'newitemnumber'}, $orderiteminfo->{'itemnumber'}, $orderiteminfo->{'ordernumber'});
1182 my $sth = $dbh->prepare($query);
1183 $sth->execute(@params);
1189 ModItemOrder($itemnumber, $ordernumber);
1191 Modifies the ordernumber of an item in aqorders_items.
1196 my ($itemnumber, $ordernumber) = @_;
1198 return unless ($itemnumber and $ordernumber);
1200 my $dbh = C4::Context->dbh;
1202 UPDATE aqorders_items
1204 WHERE itemnumber = ?
1206 my $sth = $dbh->prepare($query);
1207 return $sth->execute($ordernumber, $itemnumber);
1210 #------------------------------------------------------------#
1213 =head3 ModOrderBibliotemNumber
1215 &ModOrderBiblioitemNumber($biblioitemnumber,$ordernumber, $biblionumber);
1217 Modifies the biblioitemnumber for an existing order.
1218 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
1222 #FIXME: is this used at all?
1223 sub ModOrderBiblioitemNumber {
1224 my ($biblioitemnumber,$ordernumber, $biblionumber) = @_;
1225 my $dbh = C4::Context->dbh;
1228 SET biblioitemnumber = ?
1229 WHERE ordernumber = ?
1230 AND biblionumber = ?";
1231 my $sth = $dbh->prepare($query);
1232 $sth->execute( $biblioitemnumber, $ordernumber, $biblionumber );
1235 =head3 GetCancelledOrders
1237 my @orders = GetCancelledOrders($basketno, $orderby);
1239 Returns cancelled orders for a basket
1243 sub GetCancelledOrders {
1244 my ( $basketno, $orderby ) = @_;
1246 return () unless $basketno;
1248 my $dbh = C4::Context->dbh;
1250 SELECT biblio.*, biblioitems.*, aqorders.*, aqbudgets.*
1252 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1253 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1254 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1256 AND (datecancellationprinted IS NOT NULL
1257 AND datecancellationprinted <> '0000-00-00')
1260 $orderby = "aqorders.datecancellationprinted desc, aqorders.timestamp desc"
1262 $query .= " ORDER BY $orderby";
1263 my $sth = $dbh->prepare($query);
1264 $sth->execute($basketno);
1265 my $results = $sth->fetchall_arrayref( {} );
1271 #------------------------------------------------------------#
1273 =head3 ModReceiveOrder
1275 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
1276 $unitprice, $invoiceid, $biblioitemnumber,
1277 $bookfund, $rrp, \@received_itemnumbers);
1279 Updates an order, to reflect the fact that it was received, at least
1280 in part. All arguments not mentioned below update the fields with the
1281 same name in the aqorders table of the Koha database.
1283 If a partial order is received, splits the order into two.
1285 Updates the order with bibilionumber C<$biblionumber> and ordernumber
1291 sub ModReceiveOrder {
1293 $biblionumber, $ordernumber, $quantrec, $user, $cost, $ecost,
1294 $invoiceid, $rrp, $budget_id, $datereceived, $received_items
1298 my $dbh = C4::Context->dbh;
1299 $datereceived = C4::Dates->output('iso') unless $datereceived;
1300 my $suggestionid = GetSuggestionFromBiblionumber( $biblionumber );
1301 if ($suggestionid) {
1302 ModSuggestion( {suggestionid=>$suggestionid,
1303 STATUS=>'AVAILABLE',
1304 biblionumber=> $biblionumber}
1308 my $sth=$dbh->prepare("
1309 SELECT * FROM aqorders
1310 WHERE biblionumber=? AND aqorders.ordernumber=?");
1312 $sth->execute($biblionumber,$ordernumber);
1313 my $order = $sth->fetchrow_hashref();
1316 my $new_ordernumber = $ordernumber;
1317 if ( $order->{quantity} > $quantrec ) {
1318 # Split order line in two parts: the first is the original order line
1319 # without received items (the quantity is decreased),
1320 # the second part is a new order line with quantity=quantityrec
1321 # (entirely received)
1322 $sth=$dbh->prepare("
1325 WHERE ordernumber = ?
1328 $sth->execute($order->{quantity} - $quantrec, $ordernumber);
1332 delete $order->{'ordernumber'};
1333 $order->{'quantity'} = $quantrec;
1334 $order->{'quantityreceived'} = $quantrec;
1335 $order->{'datereceived'} = $datereceived;
1336 $order->{'invoiceid'} = $invoiceid;
1337 $order->{'unitprice'} = $cost;
1338 $order->{'rrp'} = $rrp;
1339 $order->{ecost} = $ecost;
1340 $order->{'orderstatus'} = 3; # totally received
1341 $new_ordernumber = NewOrder($order);
1343 if ($received_items) {
1344 foreach my $itemnumber (@$received_items) {
1345 ModItemOrder($itemnumber, $new_ordernumber);
1349 $sth=$dbh->prepare("update aqorders
1350 set quantityreceived=?,datereceived=?,invoiceid=?,
1351 unitprice=?,rrp=?,ecost=?
1352 where biblionumber=? and ordernumber=?");
1353 $sth->execute($quantrec,$datereceived,$invoiceid,$cost,$rrp,$ecost,$biblionumber,$ordernumber);
1356 return ($datereceived, $new_ordernumber);
1359 =head3 CancelReceipt
1361 my $parent_ordernumber = CancelReceipt($ordernumber);
1363 Cancel an order line receipt and update the parent order line, as if no
1365 If items are created at receipt (AcqCreateItem = receiving) then delete
1371 my $ordernumber = shift;
1373 return unless $ordernumber;
1375 my $dbh = C4::Context->dbh;
1377 SELECT datereceived, parent_ordernumber, quantity
1379 WHERE ordernumber = ?
1381 my $sth = $dbh->prepare($query);
1382 $sth->execute($ordernumber);
1383 my $order = $sth->fetchrow_hashref;
1385 warn "CancelReceipt: order $ordernumber does not exist";
1388 unless($order->{'datereceived'}) {
1389 warn "CancelReceipt: order $ordernumber is not received";
1393 my $parent_ordernumber = $order->{'parent_ordernumber'};
1395 if($parent_ordernumber == $ordernumber || not $parent_ordernumber) {
1396 # The order line has no parent, just mark it as not received
1399 SET quantityreceived = ?,
1402 WHERE ordernumber = ?
1404 $sth = $dbh->prepare($query);
1405 $sth->execute(0, undef, undef, $ordernumber);
1407 # The order line has a parent, increase parent quantity and delete
1410 SELECT quantity, datereceived
1412 WHERE ordernumber = ?
1414 $sth = $dbh->prepare($query);
1415 $sth->execute($parent_ordernumber);
1416 my $parent_order = $sth->fetchrow_hashref;
1417 unless($parent_order) {
1418 warn "Parent order $parent_ordernumber does not exist.";
1421 if($parent_order->{'datereceived'}) {
1422 warn "CancelReceipt: parent order is received.".
1423 " Can't cancel receipt.";
1429 WHERE ordernumber = ?
1431 $sth = $dbh->prepare($query);
1432 my $rv = $sth->execute(
1433 $order->{'quantity'} + $parent_order->{'quantity'},
1437 warn "Cannot update parent order line, so do not cancel".
1441 if(C4::Context->preference('AcqCreateItem') eq 'receiving') {
1442 # Remove items that were created at receipt
1444 DELETE FROM items, aqorders_items
1445 USING items, aqorders_items
1446 WHERE items.itemnumber = ? AND aqorders_items.itemnumber = ?
1448 $sth = $dbh->prepare($query);
1449 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
1450 foreach my $itemnumber (@itemnumbers) {
1451 $sth->execute($itemnumber, $itemnumber);
1455 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
1456 foreach my $itemnumber (@itemnumbers) {
1457 ModItemOrder($itemnumber, $parent_ordernumber);
1462 DELETE FROM aqorders
1463 WHERE ordernumber = ?
1465 $sth = $dbh->prepare($query);
1466 $sth->execute($ordernumber);
1470 return $parent_ordernumber;
1473 #------------------------------------------------------------#
1477 @results = &SearchOrder($search, $biblionumber, $complete);
1479 Searches for orders.
1481 C<$search> may take one of several forms: if it is an ISBN,
1482 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
1483 order number, C<&ordersearch> returns orders with that order number
1484 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
1485 to be a space-separated list of search terms; in this case, all of the
1486 terms must appear in the title (matching the beginning of title
1489 If C<$complete> is C<yes>, the results will include only completed
1490 orders. In any case, C<&ordersearch> ignores cancelled orders.
1492 C<&ordersearch> returns an array.
1493 C<@results> is an array of references-to-hash with the following keys:
1499 =item C<seriestitle>
1510 #### -------- SearchOrder-------------------------------
1511 my ( $ordernumber, $search, $ean, $supplierid, $basket ) = @_;
1513 my $dbh = C4::Context->dbh;
1518 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1519 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1520 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1521 WHERE (datecancellationprinted is NULL)";
1524 $query .= " AND (aqorders.ordernumber=?)";
1525 push @args, $ordernumber;
1528 $query .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
1529 push @args, ("%$search%","%$search%","%$search%");
1532 $query .= " AND biblioitems.ean = ?";
1536 $query .= "AND aqbasket.booksellerid = ?";
1537 push @args, $supplierid;
1540 $query .= "AND aqorders.basketno = ?";
1541 push @args, $basket;
1544 my $sth = $dbh->prepare($query);
1545 $sth->execute(@args);
1546 my $results = $sth->fetchall_arrayref({});
1551 #------------------------------------------------------------#
1555 &DelOrder($biblionumber, $ordernumber);
1557 Cancel the order with the given order and biblio numbers. It does not
1558 delete any entries in the aqorders table, it merely marks them as
1564 my ( $bibnum, $ordernumber ) = @_;
1565 my $dbh = C4::Context->dbh;
1568 SET datecancellationprinted=now()
1569 WHERE biblionumber=? AND ordernumber=?
1571 my $sth = $dbh->prepare($query);
1572 $sth->execute( $bibnum, $ordernumber );
1574 my @itemnumbers = GetItemnumbersFromOrder( $ordernumber );
1575 foreach my $itemnumber (@itemnumbers){
1576 C4::Items::DelItem( $dbh, $bibnum, $itemnumber );
1581 =head2 FUNCTIONS ABOUT PARCELS
1585 #------------------------------------------------------------#
1589 @results = &GetParcel($booksellerid, $code, $date);
1591 Looks up all of the received items from the supplier with the given
1592 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
1594 C<@results> is an array of references-to-hash. The keys of each element are fields from
1595 the aqorders, biblio, and biblioitems tables of the Koha database.
1597 C<@results> is sorted alphabetically by book title.
1602 #gets all orders from a certain supplier, orders them alphabetically
1603 my ( $supplierid, $code, $datereceived ) = @_;
1604 my $dbh = C4::Context->dbh;
1607 if $code; # add % if we search on a given code (otherwise, let him empty)
1609 SELECT authorisedby,
1614 aqorders.biblionumber,
1615 aqorders.ordernumber,
1616 aqorders.parent_ordernumber,
1618 aqorders.quantityreceived,
1626 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
1627 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1628 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1629 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1631 aqbasket.booksellerid = ?
1632 AND aqinvoices.invoicenumber LIKE ?
1633 AND aqorders.datereceived = ? ";
1635 my @query_params = ( $supplierid, $code, $datereceived );
1636 if ( C4::Context->preference("IndependantBranches") ) {
1637 my $userenv = C4::Context->userenv;
1638 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1639 $strsth .= " and (borrowers.branchcode = ?
1640 or borrowers.branchcode = '')";
1641 push @query_params, $userenv->{branch};
1644 $strsth .= " ORDER BY aqbasket.basketno";
1645 # ## parcelinformation : $strsth
1646 my $sth = $dbh->prepare($strsth);
1647 $sth->execute( @query_params );
1648 while ( my $data = $sth->fetchrow_hashref ) {
1649 push( @results, $data );
1651 # ## countparcelbiblio: scalar(@results)
1657 #------------------------------------------------------------#
1661 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1663 get a lists of parcels.
1670 is the bookseller this function has to get parcels.
1673 To know on what criteria the results list has to be ordered.
1676 is the booksellerinvoicenumber.
1678 =item $datefrom & $dateto
1679 to know on what date this function has to filter its search.
1684 a pointer on a hash list containing parcel informations as such :
1690 =item Last operation
1692 =item Number of biblio
1694 =item Number of items
1701 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
1702 my $dbh = C4::Context->dbh;
1703 my @query_params = ();
1705 SELECT aqinvoices.invoicenumber,
1706 datereceived,purchaseordernumber,
1707 count(DISTINCT biblionumber) AS biblio,
1708 sum(quantity) AS itemsexpected,
1709 sum(quantityreceived) AS itemsreceived
1710 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
1711 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1712 WHERE aqbasket.booksellerid = ? and datereceived IS NOT NULL
1714 push @query_params, $bookseller;
1716 if ( defined $code ) {
1717 $strsth .= ' and aqinvoices.invoicenumber like ? ';
1718 # add a % to the end of the code to allow stemming.
1719 push @query_params, "$code%";
1722 if ( defined $datefrom ) {
1723 $strsth .= ' and datereceived >= ? ';
1724 push @query_params, $datefrom;
1727 if ( defined $dateto ) {
1728 $strsth .= 'and datereceived <= ? ';
1729 push @query_params, $dateto;
1732 $strsth .= "group by aqinvoices.invoicenumber,datereceived ";
1734 # can't use a placeholder to place this column name.
1735 # but, we could probably be checking to make sure it is a column that will be fetched.
1736 $strsth .= "order by $order " if ($order);
1738 my $sth = $dbh->prepare($strsth);
1740 $sth->execute( @query_params );
1741 my $results = $sth->fetchall_arrayref({});
1746 #------------------------------------------------------------#
1748 =head3 GetLateOrders
1750 @results = &GetLateOrders;
1752 Searches for bookseller with late orders.
1755 the table of supplier with late issues. This table is full of hashref.
1761 my $supplierid = shift;
1763 my $estimateddeliverydatefrom = shift;
1764 my $estimateddeliverydateto = shift;
1766 my $dbh = C4::Context->dbh;
1768 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
1769 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
1771 my @query_params = ();
1773 SELECT aqbasket.basketno,
1774 aqorders.ordernumber,
1775 DATE(aqbasket.closedate) AS orderdate,
1776 aqorders.rrp AS unitpricesupplier,
1777 aqorders.ecost AS unitpricelib,
1778 aqorders.claims_count AS claims_count,
1779 aqorders.claimed_date AS claimed_date,
1780 aqbudgets.budget_name AS budget,
1781 borrowers.branchcode AS branch,
1782 aqbooksellers.name AS supplier,
1783 aqbooksellers.id AS supplierid,
1784 biblio.author, biblio.title,
1785 biblioitems.publishercode AS publisher,
1786 biblioitems.publicationyear,
1787 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
1791 aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1792 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1793 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
1794 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
1795 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1796 WHERE aqorders.basketno = aqbasket.basketno
1797 AND ( datereceived = ''
1798 OR datereceived IS NULL
1799 OR aqorders.quantityreceived < aqorders.quantity
1801 AND aqbasket.closedate IS NOT NULL
1802 AND (aqorders.datecancellationprinted IS NULL OR aqorders.datecancellationprinted='0000-00-00')
1805 if ($dbdriver eq "mysql") {
1807 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
1808 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1809 DATEDIFF(CAST(now() AS date),closedate) AS latesince
1811 if ( defined $delay ) {
1812 $from .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) " ;
1813 push @query_params, $delay;
1816 HAVING quantity <> 0
1817 AND unitpricesupplier <> 0
1818 AND unitpricelib <> 0
1821 # FIXME: account for IFNULL as above
1823 aqorders.quantity AS quantity,
1824 aqorders.quantity * aqorders.rrp AS subtotal,
1825 (CAST(now() AS date) - closedate) AS latesince
1827 if ( defined $delay ) {
1828 $from .= " AND (closedate <= (CAST(now() AS date) -(INTERVAL ? DAY)) ";
1829 push @query_params, $delay;
1832 if (defined $supplierid) {
1833 $from .= ' AND aqbasket.booksellerid = ? ';
1834 push @query_params, $supplierid;
1836 if (defined $branch) {
1837 $from .= ' AND borrowers.branchcode LIKE ? ';
1838 push @query_params, $branch;
1841 if ( defined $estimateddeliverydatefrom or defined $estimateddeliverydateto ) {
1842 $from .= ' AND aqbooksellers.deliverytime IS NOT NULL ';
1844 if ( defined $estimateddeliverydatefrom ) {
1845 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
1846 push @query_params, $estimateddeliverydatefrom;
1848 if ( defined $estimateddeliverydateto ) {
1849 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
1850 push @query_params, $estimateddeliverydateto;
1852 if ( defined $estimateddeliverydatefrom and not defined $estimateddeliverydateto ) {
1853 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
1855 if (C4::Context->preference("IndependantBranches")
1856 && C4::Context->userenv
1857 && C4::Context->userenv->{flags} != 1 ) {
1858 $from .= ' AND borrowers.branchcode LIKE ? ';
1859 push @query_params, C4::Context->userenv->{branch};
1861 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
1862 $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
1863 my $sth = $dbh->prepare($query);
1864 $sth->execute(@query_params);
1866 while (my $data = $sth->fetchrow_hashref) {
1867 $data->{orderdate} = format_date($data->{orderdate});
1868 $data->{claimed_date} = format_date($data->{claimed_date});
1869 push @results, $data;
1874 #------------------------------------------------------------#
1878 (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( %params );
1880 Retreives some acquisition history information
1888 basket - search both basket name and number
1889 booksellerinvoicenumber
1892 $order_loop is a list of hashrefs that each look like this:
1894 'author' => 'Twain, Mark',
1896 'biblionumber' => '215',
1898 'creationdate' => 'MM/DD/YYYY',
1899 'datereceived' => undef,
1902 'invoicenumber' => undef,
1904 'ordernumber' => '1',
1906 'quantityreceived' => undef,
1907 'title' => 'The Adventures of Huckleberry Finn'
1909 $total_qty is the sum of all of the quantities in $order_loop
1910 $total_price is the cost of each in $order_loop times the quantity
1911 $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
1916 # don't run the query if there are no parameters (list would be too long for sure !)
1917 croak "No search params" unless @_;
1919 my $title = $params{title};
1920 my $author = $params{author};
1921 my $isbn = $params{isbn};
1922 my $ean = $params{ean};
1923 my $name = $params{name};
1924 my $from_placed_on = $params{from_placed_on};
1925 my $to_placed_on = $params{to_placed_on};
1926 my $basket = $params{basket};
1927 my $booksellerinvoicenumber = $params{booksellerinvoicenumber};
1928 my $basketgroupname = $params{basketgroupname};
1931 my $total_qtyreceived = 0;
1932 my $total_price = 0;
1934 my $dbh = C4::Context->dbh;
1942 aqbasket.basketname,
1943 aqbasket.basketgroupid,
1944 aqbasketgroups.name as groupname,
1946 aqbasket.creationdate,
1947 aqorders.datereceived,
1949 aqorders.quantityreceived,
1951 aqorders.ordernumber,
1952 aqinvoices.invoicenumber,
1953 aqbooksellers.id as id,
1954 aqorders.biblionumber
1956 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
1957 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
1958 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
1959 LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
1960 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
1961 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid";
1963 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
1964 if ( C4::Context->preference("IndependantBranches") );
1966 $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
1968 my @query_params = ();
1971 $query .= " AND biblio.title LIKE ? ";
1972 $title =~ s/\s+/%/g;
1973 push @query_params, "%$title%";
1977 $query .= " AND biblio.author LIKE ? ";
1978 push @query_params, "%$author%";
1982 $query .= " AND biblioitems.isbn LIKE ? ";
1983 push @query_params, "%$isbn%";
1985 if ( defined $ean and $ean ) {
1986 $query .= " AND biblioitems.ean = ? ";
1987 push @query_params, "$ean";
1990 $query .= " AND aqbooksellers.name LIKE ? ";
1991 push @query_params, "%$name%";
1994 if ( $from_placed_on ) {
1995 $query .= " AND creationdate >= ? ";
1996 push @query_params, $from_placed_on;
1999 if ( $to_placed_on ) {
2000 $query .= " AND creationdate <= ? ";
2001 push @query_params, $to_placed_on;
2005 if ($basket =~ m/^\d+$/) {
2006 $query .= " AND aqorders.basketno = ? ";
2007 push @query_params, $basket;
2009 $query .= " AND aqbasket.basketname LIKE ? ";
2010 push @query_params, "%$basket%";
2014 if ($booksellerinvoicenumber) {
2015 $query .= " AND aqinvoices.invoicenumber LIKE ? ";
2016 push @query_params, "%$booksellerinvoicenumber%";
2019 if ($basketgroupname) {
2020 $query .= " AND aqbasketgroups.name LIKE ? ";
2021 push @query_params, "%$basketgroupname%";
2024 if ( C4::Context->preference("IndependantBranches") ) {
2025 my $userenv = C4::Context->userenv;
2026 if ( $userenv && ($userenv->{flags} || 0) != 1 ) {
2027 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
2028 push @query_params, $userenv->{branch};
2031 $query .= " ORDER BY id";
2032 my $sth = $dbh->prepare($query);
2033 $sth->execute( @query_params );
2035 while ( my $line = $sth->fetchrow_hashref ) {
2036 $line->{count} = $cnt++;
2037 $line->{toggle} = 1 if $cnt % 2;
2038 push @order_loop, $line;
2039 $total_qty += $line->{'quantity'};
2040 $total_qtyreceived += $line->{'quantityreceived'};
2041 $total_price += $line->{'quantity'} * $line->{'ecost'};
2043 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
2046 =head2 GetRecentAcqui
2048 $results = GetRecentAcqui($days);
2050 C<$results> is a ref to a table which containts hashref
2054 sub GetRecentAcqui {
2056 my $dbh = C4::Context->dbh;
2060 ORDER BY timestamp DESC
2063 my $sth = $dbh->prepare($query);
2065 my $results = $sth->fetchall_arrayref({});
2071 $contractlist = &GetContracts($booksellerid, $activeonly);
2073 Looks up the contracts that belong to a bookseller
2075 Returns a list of contracts
2079 =item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
2081 =item C<$activeonly> if exists get only contracts that are still active.
2088 my ( $booksellerid, $activeonly ) = @_;
2089 my $dbh = C4::Context->dbh;
2091 if (! $activeonly) {
2095 WHERE booksellerid=?
2100 WHERE booksellerid=?
2101 AND contractenddate >= CURDATE( )";
2103 my $sth = $dbh->prepare($query);
2104 $sth->execute( $booksellerid );
2106 while (my $data = $sth->fetchrow_hashref ) {
2107 push(@results, $data);
2113 #------------------------------------------------------------#
2117 $contract = &GetContract($contractID);
2119 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
2126 my ( $contractno ) = @_;
2127 my $dbh = C4::Context->dbh;
2131 WHERE contractnumber=?
2134 my $sth = $dbh->prepare($query);
2135 $sth->execute( $contractno );
2136 my $result = $sth->fetchrow_hashref;
2144 &AddClaim($ordernumber);
2146 Add a claim for an order
2152 my ($ordernumber) = @_;
2153 my $dbh = C4::Context->dbh;
2156 claims_count = claims_count + 1,
2157 claimed_date = CURDATE()
2158 WHERE ordernumber = ?
2160 my $sth = $dbh->prepare($query);
2161 $sth->execute($ordernumber);
2166 my @invoices = GetInvoices(
2167 invoicenumber => $invoicenumber,
2168 suppliername => $suppliername,
2169 shipmentdatefrom => $shipmentdatefrom, # ISO format
2170 shipmentdateto => $shipmentdateto, # ISO format
2171 billingdatefrom => $billingdatefrom, # ISO format
2172 billingdateto => $billingdateto, # ISO format
2173 isbneanissn => $isbn_or_ean_or_issn,
2176 publisher => $publisher,
2177 publicationyear => $publicationyear,
2178 branchcode => $branchcode,
2179 order_by => $order_by
2182 Return a list of invoices that match all given criteria.
2184 $order_by is "column_name (asc|desc)", where column_name is any of
2185 'invoicenumber', 'booksellerid', 'shipmentdate', 'billingdate', 'closedate',
2186 'shipmentcost', 'shipmentcost_budgetid'.
2188 asc is the default if omitted
2195 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2196 closedate shipmentcost shipmentcost_budgetid);
2198 my $dbh = C4::Context->dbh;
2200 SELECT aqinvoices.*, aqbooksellers.name AS suppliername,
2203 aqorders.datereceived IS NOT NULL,
2204 aqorders.biblionumber,
2207 ) AS receivedbiblios,
2208 SUM(aqorders.quantityreceived) AS receiveditems
2210 LEFT JOIN aqbooksellers ON aqbooksellers.id = aqinvoices.booksellerid
2211 LEFT JOIN aqorders ON aqorders.invoiceid = aqinvoices.invoiceid
2212 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2213 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
2214 LEFT JOIN subscription ON biblio.biblionumber = subscription.biblionumber
2219 if($args{supplierid}) {
2220 push @bind_strs, " aqinvoices.booksellerid = ? ";
2221 push @bind_args, $args{supplierid};
2223 if($args{invoicenumber}) {
2224 push @bind_strs, " aqinvoices.invoicenumber LIKE ? ";
2225 push @bind_args, "%$args{invoicenumber}%";
2227 if($args{suppliername}) {
2228 push @bind_strs, " aqbooksellers.name LIKE ? ";
2229 push @bind_args, "%$args{suppliername}%";
2231 if($args{shipmentdatefrom}) {
2232 push @bind_strs, " aqinvoices.shipementdate >= ? ";
2233 push @bind_args, $args{shipmentdatefrom};
2235 if($args{shipmentdateto}) {
2236 push @bind_strs, " aqinvoices.shipementdate <= ? ";
2237 push @bind_args, $args{shipmentdateto};
2239 if($args{billingdatefrom}) {
2240 push @bind_strs, " aqinvoices.billingdate >= ? ";
2241 push @bind_args, $args{billingdatefrom};
2243 if($args{billingdateto}) {
2244 push @bind_strs, " aqinvoices.billingdate <= ? ";
2245 push @bind_args, $args{billingdateto};
2247 if($args{isbneanissn}) {
2248 push @bind_strs, " (biblioitems.isbn LIKE ? OR biblioitems.ean LIKE ? OR biblioitems.issn LIKE ? ) ";
2249 push @bind_args, $args{isbneanissn}, $args{isbneanissn}, $args{isbneanissn};
2252 push @bind_strs, " biblio.title LIKE ? ";
2253 push @bind_args, $args{title};
2256 push @bind_strs, " biblio.author LIKE ? ";
2257 push @bind_args, $args{author};
2259 if($args{publisher}) {
2260 push @bind_strs, " biblioitems.publishercode LIKE ? ";
2261 push @bind_args, $args{publisher};
2263 if($args{publicationyear}) {
2264 push @bind_strs, " biblioitems.publicationyear = ? ";
2265 push @bind_args, $args{publicationyear};
2267 if($args{branchcode}) {
2268 push @bind_strs, " aqorders.branchcode = ? ";
2269 push @bind_args, $args{branchcode};
2272 $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
2273 $query .= " GROUP BY aqinvoices.invoiceid ";
2275 if($args{order_by}) {
2276 my ($column, $direction) = split / /, $args{order_by};
2277 if(grep /^$column$/, @columns) {
2278 $direction ||= 'ASC';
2279 $query .= " ORDER BY $column $direction";
2283 my $sth = $dbh->prepare($query);
2284 $sth->execute(@bind_args);
2286 my $results = $sth->fetchall_arrayref({});
2292 my $invoice = GetInvoice($invoiceid);
2294 Get informations about invoice with given $invoiceid
2296 Return a hash filled with aqinvoices.* fields
2301 my ($invoiceid) = @_;
2304 return unless $invoiceid;
2306 my $dbh = C4::Context->dbh;
2312 my $sth = $dbh->prepare($query);
2313 $sth->execute($invoiceid);
2315 $invoice = $sth->fetchrow_hashref;
2319 =head3 GetInvoiceDetails
2321 my $invoice = GetInvoiceDetails($invoiceid)
2323 Return informations about an invoice + the list of related order lines
2325 Orders informations are in $invoice->{orders} (array ref)
2329 sub GetInvoiceDetails {
2330 my ($invoiceid) = @_;
2332 if ( !defined $invoiceid ) {
2333 carp 'GetInvoiceDetails called without an invoiceid';
2337 my $dbh = C4::Context->dbh;
2339 SELECT aqinvoices.*, aqbooksellers.name AS suppliername
2341 LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
2344 my $sth = $dbh->prepare($query);
2345 $sth->execute($invoiceid);
2347 my $invoice = $sth->fetchrow_hashref;
2350 SELECT aqorders.*, biblio.*
2352 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2355 $sth = $dbh->prepare($query);
2356 $sth->execute($invoiceid);
2357 $invoice->{orders} = $sth->fetchall_arrayref({});
2358 $invoice->{orders} ||= []; # force an empty arrayref if fetchall_arrayref fails
2365 my $invoiceid = AddInvoice(
2366 invoicenumber => $invoicenumber,
2367 booksellerid => $booksellerid,
2368 shipmentdate => $shipmentdate,
2369 billingdate => $billingdate,
2370 closedate => $closedate,
2371 shipmentcost => $shipmentcost,
2372 shipmentcost_budgetid => $shipmentcost_budgetid
2375 Create a new invoice and return its id or undef if it fails.
2382 return unless(%invoice and $invoice{invoicenumber});
2384 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2385 closedate shipmentcost shipmentcost_budgetid);
2389 foreach my $key (keys %invoice) {
2390 if(0 < grep(/^$key$/, @columns)) {
2391 push @set_strs, "$key = ?";
2392 push @set_args, ($invoice{$key} || undef);
2398 my $dbh = C4::Context->dbh;
2399 my $query = "INSERT INTO aqinvoices SET ";
2400 $query .= join (",", @set_strs);
2401 my $sth = $dbh->prepare($query);
2402 $rv = $sth->execute(@set_args);
2404 $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
2413 invoiceid => $invoiceid, # Mandatory
2414 invoicenumber => $invoicenumber,
2415 booksellerid => $booksellerid,
2416 shipmentdate => $shipmentdate,
2417 billingdate => $billingdate,
2418 closedate => $closedate,
2419 shipmentcost => $shipmentcost,
2420 shipmentcost_budgetid => $shipmentcost_budgetid
2423 Modify an invoice, invoiceid is mandatory.
2425 Return undef if it fails.
2432 return unless(%invoice and $invoice{invoiceid});
2434 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2435 closedate shipmentcost shipmentcost_budgetid);
2439 foreach my $key (keys %invoice) {
2440 if(0 < grep(/^$key$/, @columns)) {
2441 push @set_strs, "$key = ?";
2442 push @set_args, ($invoice{$key} || undef);
2446 my $dbh = C4::Context->dbh;
2447 my $query = "UPDATE aqinvoices SET ";
2448 $query .= join(",", @set_strs);
2449 $query .= " WHERE invoiceid = ?";
2451 my $sth = $dbh->prepare($query);
2452 $sth->execute(@set_args, $invoice{invoiceid});
2457 CloseInvoice($invoiceid);
2461 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
2466 my ($invoiceid) = @_;
2468 return unless $invoiceid;
2470 my $dbh = C4::Context->dbh;
2473 SET closedate = CAST(NOW() AS DATE)
2476 my $sth = $dbh->prepare($query);
2477 $sth->execute($invoiceid);
2480 =head3 ReopenInvoice
2482 ReopenInvoice($invoiceid);
2486 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => C4::Dates->new()->output('iso'))
2491 my ($invoiceid) = @_;
2493 return unless $invoiceid;
2495 my $dbh = C4::Context->dbh;
2498 SET closedate = NULL
2501 my $sth = $dbh->prepare($query);
2502 $sth->execute($invoiceid);
2510 Koha Development Team <http://koha-community.org/>