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
61 &GetLastOrderNotReceivedFromSubscriptionid &GetLastOrderReceivedFromSubscriptionid
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 return $sth->fetchall_arrayref({});
818 #------------------------------------------------------------#
820 =head2 FUNCTIONS ABOUT ORDERS
824 #------------------------------------------------------------#
826 =head3 GetPendingOrders
828 $orders = &GetPendingOrders($supplierid,$grouped,$owner,$basketno,$ordernumber,$search,$ean);
830 Finds pending orders from the bookseller with the given ID. Ignores
831 completed and cancelled orders.
833 C<$booksellerid> contains the bookseller identifier
834 C<$owner> contains 0 or 1. 0 means any owner. 1 means only the list of orders entered by the user itself.
835 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
836 in a single result line
837 C<$orders> is a reference-to-array; each element is a reference-to-hash.
839 Used also by the filter in parcel.pl
846 These give the value of the corresponding field in the aqorders table
847 of the Koha database.
849 Results are ordered from most to least recent.
853 sub GetPendingOrders {
854 my ($supplierid,$grouped,$owner,$basketno,$ordernumber,$search,$ean) = @_;
855 my $dbh = C4::Context->dbh;
857 SELECT ".($grouped?"count(*),":"")."aqbasket.basketno,
858 surname,firstname,biblio.*,biblioitems.isbn,
859 aqbasket.closedate, aqbasket.creationdate, aqbasket.basketname,
862 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
863 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
864 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
865 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
866 WHERE (quantity > quantityreceived OR quantityreceived is NULL)
867 AND datecancellationprinted IS NULL";
869 my $userenv = C4::Context->userenv;
870 if ( C4::Context->preference("IndependantBranches") ) {
871 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
872 $strsth .= " AND (borrowers.branchcode = ?
873 or borrowers.branchcode = '')";
874 push @query_params, $userenv->{branch};
878 $strsth .= " AND aqbasket.booksellerid = ?";
879 push @query_params, $supplierid;
882 $strsth .= " AND (aqorders.ordernumber=?)";
883 push @query_params, $ordernumber;
886 $strsth .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
887 push @query_params, ("%$search%","%$search%","%$search%");
890 $strsth .= " AND biblioitems.ean = ?";
891 push @query_params, $ean;
894 $strsth .= " AND aqbasket.basketno=? ";
895 push @query_params, $basketno;
898 $strsth .= " AND aqbasket.authorisedby=? ";
899 push @query_params, $userenv->{'number'};
901 $strsth .= " group by aqbasket.basketno" if $grouped;
902 $strsth .= " order by aqbasket.basketno";
903 my $sth = $dbh->prepare($strsth);
904 $sth->execute( @query_params );
905 my $results = $sth->fetchall_arrayref({});
910 #------------------------------------------------------------#
914 @orders = &GetOrders($basketnumber, $orderby);
916 Looks up the pending (non-cancelled) orders with the given basket
917 number. If C<$booksellerID> is non-empty, only orders from that seller
921 C<&basket> returns a two-element array. C<@orders> is an array of
922 references-to-hash, whose keys are the fields from the aqorders,
923 biblio, and biblioitems tables in the Koha database.
928 my ( $basketno, $orderby ) = @_;
929 my $dbh = C4::Context->dbh;
931 SELECT biblio.*,biblioitems.*,
936 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
937 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
938 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
940 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
943 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
944 $query .= " ORDER BY $orderby";
945 my $sth = $dbh->prepare($query);
946 $sth->execute($basketno);
947 my $results = $sth->fetchall_arrayref({});
952 #------------------------------------------------------------#
954 =head3 GetOrderNumber
956 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
958 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
960 Returns the number of this order.
964 =item C<$ordernumber> is the order number.
971 my ( $biblionumber,$biblioitemnumber ) = @_;
972 my $dbh = C4::Context->dbh;
977 AND biblioitemnumber=?
979 my $sth = $dbh->prepare($query);
980 $sth->execute( $biblionumber, $biblioitemnumber );
982 return $sth->fetchrow;
985 #------------------------------------------------------------#
989 $order = &GetOrder($ordernumber);
991 Looks up an order by order number.
993 Returns a reference-to-hash describing the order. The keys of
994 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
999 my ($ordernumber) = @_;
1000 my $dbh = C4::Context->dbh;
1002 SELECT biblioitems.*, biblio.*, aqorders.*
1004 LEFT JOIN biblio on biblio.biblionumber=aqorders.biblionumber
1005 LEFT JOIN biblioitems on biblioitems.biblionumber=aqorders.biblionumber
1006 WHERE aqorders.ordernumber=?
1009 my $sth= $dbh->prepare($query);
1010 $sth->execute($ordernumber);
1011 my $data = $sth->fetchrow_hashref;
1016 =head3 GetLastOrderNotReceivedFromSubscriptionid
1018 $order = &GetLastOrderNotReceivedFromSubscriptionid($subscriptionid);
1020 Returns a reference-to-hash describing the last order not received for a subscription.
1024 sub GetLastOrderNotReceivedFromSubscriptionid {
1025 my ( $subscriptionid ) = @_;
1026 my $dbh = C4::Context->dbh;
1028 SELECT * FROM aqorders
1029 LEFT JOIN subscription
1030 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1031 WHERE aqorders.subscriptionid = ?
1032 AND aqorders.datereceived IS NULL
1035 my $sth = $dbh->prepare( $query );
1036 $sth->execute( $subscriptionid );
1037 my $order = $sth->fetchrow_hashref;
1041 =head3 GetLastOrderReceivedFromSubscriptionid
1043 $order = &GetLastOrderReceivedFromSubscriptionid($subscriptionid);
1045 Returns a reference-to-hash describing the last order received for a subscription.
1049 sub GetLastOrderReceivedFromSubscriptionid {
1050 my ( $subscriptionid ) = @_;
1051 my $dbh = C4::Context->dbh;
1053 SELECT * FROM aqorders
1054 LEFT JOIN subscription
1055 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1056 WHERE aqorders.subscriptionid = ?
1057 AND aqorders.datereceived =
1059 SELECT MAX( aqorders.datereceived )
1061 LEFT JOIN subscription
1062 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1063 WHERE aqorders.subscriptionid = ?
1064 AND aqorders.datereceived IS NOT NULL
1066 ORDER BY ordernumber DESC
1069 my $sth = $dbh->prepare( $query );
1070 $sth->execute( $subscriptionid, $subscriptionid );
1071 my $order = $sth->fetchrow_hashref;
1077 #------------------------------------------------------------#
1081 &NewOrder(\%hashref);
1083 Adds a new order to the database. Any argument that isn't described
1084 below is the new value of the field with the same name in the aqorders
1085 table of the Koha database.
1089 =item $hashref->{'basketno'} is the basketno foreign key in aqorders, it is mandatory
1091 =item $hashref->{'ordernumber'} is a "minimum order number."
1093 =item $hashref->{'budgetdate'} is effectively ignored.
1094 If it's undef (anything false) or the string 'now', the current day is used.
1095 Else, the upcoming July 1st is used.
1097 =item $hashref->{'subscription'} may be either "yes", or anything else for "no".
1099 =item $hashref->{'uncertainprice'} may be 0 for "the price is known" or 1 for "the price is uncertain"
1101 =item defaults entrydate to Now
1103 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".
1110 my $orderinfo = shift;
1111 #### ------------------------------
1112 my $dbh = C4::Context->dbh;
1116 # if these parameters are missing, we can't continue
1117 for my $key (qw/basketno quantity biblionumber budget_id/) {
1118 croak "Mandatory parameter $key missing" unless $orderinfo->{$key};
1121 if ( defined $orderinfo->{subscription} && $orderinfo->{'subscription'} eq 'yes' ) {
1122 $orderinfo->{'subscription'} = 1;
1124 $orderinfo->{'subscription'} = 0;
1126 $orderinfo->{'entrydate'} ||= C4::Dates->new()->output("iso");
1127 if (!$orderinfo->{quantityreceived}) {
1128 $orderinfo->{quantityreceived} = 0;
1131 my $ordernumber=InsertInTable("aqorders",$orderinfo);
1132 if (not $orderinfo->{parent_ordernumber}) {
1133 my $sth = $dbh->prepare("
1135 SET parent_ordernumber = ordernumber
1136 WHERE ordernumber = ?
1138 $sth->execute($ordernumber);
1140 return ( $orderinfo->{'basketno'}, $ordernumber );
1145 #------------------------------------------------------------#
1154 my ($itemnumber, $ordernumber) = @_;
1155 my $dbh = C4::Context->dbh;
1157 INSERT INTO aqorders_items
1158 (itemnumber, ordernumber)
1161 my $sth = $dbh->prepare($query);
1162 $sth->execute( $itemnumber, $ordernumber);
1165 #------------------------------------------------------------#
1169 &ModOrder(\%hashref);
1171 Modifies an existing order. Updates the order with order number
1172 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All
1173 other keys of the hash update the fields with the same name in the aqorders
1174 table of the Koha database.
1179 my $orderinfo = shift;
1181 die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '' ;
1182 die "Biblionumber is required" if $orderinfo->{'biblionumber'} eq '';
1184 my $dbh = C4::Context->dbh;
1187 # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
1188 $orderinfo->{uncertainprice}=1 if $orderinfo->{uncertainprice};
1190 # delete($orderinfo->{'branchcode'});
1191 # the hash contains a lot of entries not in aqorders, so get the columns ...
1192 my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1194 my $colnames = $sth->{NAME};
1195 my $query = "UPDATE aqorders SET ";
1197 foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1198 # ... and skip hash entries that are not in the aqorders table
1199 # FIXME : probably not the best way to do it (would be better to have a correct hash)
1200 next unless grep(/^$orderinfokey$/, @$colnames);
1201 $query .= "$orderinfokey=?, ";
1202 push(@params, $orderinfo->{$orderinfokey});
1205 $query .= "timestamp=NOW() WHERE ordernumber=?";
1206 # push(@params, $specorderinfo{'ordernumber'});
1207 push(@params, $orderinfo->{'ordernumber'} );
1208 $sth = $dbh->prepare($query);
1209 $sth->execute(@params);
1213 #------------------------------------------------------------#
1217 &ModOrderItem(\%hashref);
1219 Modifies the itemnumber in the aqorders_items table. The input hash needs three entities:
1223 =item - itemnumber: the old itemnumber
1224 =item - ordernumber: the order this item is attached to
1225 =item - newitemnumber: the new itemnumber we want to attach the line to
1232 my $orderiteminfo = shift;
1233 if (! $orderiteminfo->{'ordernumber'} || ! $orderiteminfo->{'itemnumber'} || ! $orderiteminfo->{'newitemnumber'}){
1234 die "Ordernumber, itemnumber and newitemnumber is required";
1237 my $dbh = C4::Context->dbh;
1239 my $query = "UPDATE aqorders_items set itemnumber=? where itemnumber=? and ordernumber=?";
1240 my @params = ($orderiteminfo->{'newitemnumber'}, $orderiteminfo->{'itemnumber'}, $orderiteminfo->{'ordernumber'});
1241 my $sth = $dbh->prepare($query);
1242 $sth->execute(@params);
1248 ModItemOrder($itemnumber, $ordernumber);
1250 Modifies the ordernumber of an item in aqorders_items.
1255 my ($itemnumber, $ordernumber) = @_;
1257 return unless ($itemnumber and $ordernumber);
1259 my $dbh = C4::Context->dbh;
1261 UPDATE aqorders_items
1263 WHERE itemnumber = ?
1265 my $sth = $dbh->prepare($query);
1266 return $sth->execute($ordernumber, $itemnumber);
1269 #------------------------------------------------------------#
1272 =head3 ModOrderBibliotemNumber
1274 &ModOrderBiblioitemNumber($biblioitemnumber,$ordernumber, $biblionumber);
1276 Modifies the biblioitemnumber for an existing order.
1277 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
1281 #FIXME: is this used at all?
1282 sub ModOrderBiblioitemNumber {
1283 my ($biblioitemnumber,$ordernumber, $biblionumber) = @_;
1284 my $dbh = C4::Context->dbh;
1287 SET biblioitemnumber = ?
1288 WHERE ordernumber = ?
1289 AND biblionumber = ?";
1290 my $sth = $dbh->prepare($query);
1291 $sth->execute( $biblioitemnumber, $ordernumber, $biblionumber );
1294 =head3 GetCancelledOrders
1296 my @orders = GetCancelledOrders($basketno, $orderby);
1298 Returns cancelled orders for a basket
1302 sub GetCancelledOrders {
1303 my ( $basketno, $orderby ) = @_;
1305 return () unless $basketno;
1307 my $dbh = C4::Context->dbh;
1309 SELECT biblio.*, biblioitems.*, aqorders.*, aqbudgets.*
1311 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1312 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1313 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1315 AND (datecancellationprinted IS NOT NULL
1316 AND datecancellationprinted <> '0000-00-00')
1319 $orderby = "aqorders.datecancellationprinted desc, aqorders.timestamp desc"
1321 $query .= " ORDER BY $orderby";
1322 my $sth = $dbh->prepare($query);
1323 $sth->execute($basketno);
1324 my $results = $sth->fetchall_arrayref( {} );
1330 #------------------------------------------------------------#
1332 =head3 ModReceiveOrder
1334 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
1335 $unitprice, $invoiceid, $biblioitemnumber,
1336 $bookfund, $rrp, \@received_itemnumbers);
1338 Updates an order, to reflect the fact that it was received, at least
1339 in part. All arguments not mentioned below update the fields with the
1340 same name in the aqorders table of the Koha database.
1342 If a partial order is received, splits the order into two.
1344 Updates the order with bibilionumber C<$biblionumber> and ordernumber
1350 sub ModReceiveOrder {
1352 $biblionumber, $ordernumber, $quantrec, $user, $cost, $ecost,
1353 $invoiceid, $rrp, $budget_id, $datereceived, $received_items
1357 my $dbh = C4::Context->dbh;
1358 $datereceived = C4::Dates->output('iso') unless $datereceived;
1359 my $suggestionid = GetSuggestionFromBiblionumber( $biblionumber );
1360 if ($suggestionid) {
1361 ModSuggestion( {suggestionid=>$suggestionid,
1362 STATUS=>'AVAILABLE',
1363 biblionumber=> $biblionumber}
1367 my $sth=$dbh->prepare("
1368 SELECT * FROM aqorders
1369 WHERE biblionumber=? AND aqorders.ordernumber=?");
1371 $sth->execute($biblionumber,$ordernumber);
1372 my $order = $sth->fetchrow_hashref();
1375 my $new_ordernumber = $ordernumber;
1376 if ( $order->{quantity} > $quantrec ) {
1377 # Split order line in two parts: the first is the original order line
1378 # without received items (the quantity is decreased),
1379 # the second part is a new order line with quantity=quantityrec
1380 # (entirely received)
1381 $sth=$dbh->prepare("
1384 WHERE ordernumber = ?
1387 $sth->execute($order->{quantity} - $quantrec, $ordernumber);
1391 delete $order->{'ordernumber'};
1392 $order->{'quantity'} = $quantrec;
1393 $order->{'quantityreceived'} = $quantrec;
1394 $order->{'datereceived'} = $datereceived;
1395 $order->{'invoiceid'} = $invoiceid;
1396 $order->{'unitprice'} = $cost;
1397 $order->{'rrp'} = $rrp;
1398 $order->{ecost} = $ecost;
1399 $order->{'orderstatus'} = 3; # totally received
1400 $new_ordernumber = NewOrder($order);
1402 if ($received_items) {
1403 foreach my $itemnumber (@$received_items) {
1404 ModItemOrder($itemnumber, $new_ordernumber);
1408 $sth=$dbh->prepare("update aqorders
1409 set quantityreceived=?,datereceived=?,invoiceid=?,
1410 unitprice=?,rrp=?,ecost=?
1411 where biblionumber=? and ordernumber=?");
1412 $sth->execute($quantrec,$datereceived,$invoiceid,$cost,$rrp,$ecost,$biblionumber,$ordernumber);
1415 return ($datereceived, $new_ordernumber);
1418 =head3 CancelReceipt
1420 my $parent_ordernumber = CancelReceipt($ordernumber);
1422 Cancel an order line receipt and update the parent order line, as if no
1424 If items are created at receipt (AcqCreateItem = receiving) then delete
1430 my $ordernumber = shift;
1432 return unless $ordernumber;
1434 my $dbh = C4::Context->dbh;
1436 SELECT datereceived, parent_ordernumber, quantity
1438 WHERE ordernumber = ?
1440 my $sth = $dbh->prepare($query);
1441 $sth->execute($ordernumber);
1442 my $order = $sth->fetchrow_hashref;
1444 warn "CancelReceipt: order $ordernumber does not exist";
1447 unless($order->{'datereceived'}) {
1448 warn "CancelReceipt: order $ordernumber is not received";
1452 my $parent_ordernumber = $order->{'parent_ordernumber'};
1454 if($parent_ordernumber == $ordernumber || not $parent_ordernumber) {
1455 # The order line has no parent, just mark it as not received
1458 SET quantityreceived = ?,
1461 WHERE ordernumber = ?
1463 $sth = $dbh->prepare($query);
1464 $sth->execute(0, undef, undef, $ordernumber);
1466 # The order line has a parent, increase parent quantity and delete
1469 SELECT quantity, datereceived
1471 WHERE ordernumber = ?
1473 $sth = $dbh->prepare($query);
1474 $sth->execute($parent_ordernumber);
1475 my $parent_order = $sth->fetchrow_hashref;
1476 unless($parent_order) {
1477 warn "Parent order $parent_ordernumber does not exist.";
1480 if($parent_order->{'datereceived'}) {
1481 warn "CancelReceipt: parent order is received.".
1482 " Can't cancel receipt.";
1488 WHERE ordernumber = ?
1490 $sth = $dbh->prepare($query);
1491 my $rv = $sth->execute(
1492 $order->{'quantity'} + $parent_order->{'quantity'},
1496 warn "Cannot update parent order line, so do not cancel".
1500 if(C4::Context->preference('AcqCreateItem') eq 'receiving') {
1501 # Remove items that were created at receipt
1503 DELETE FROM items, aqorders_items
1504 USING items, aqorders_items
1505 WHERE items.itemnumber = ? AND aqorders_items.itemnumber = ?
1507 $sth = $dbh->prepare($query);
1508 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
1509 foreach my $itemnumber (@itemnumbers) {
1510 $sth->execute($itemnumber, $itemnumber);
1514 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
1515 foreach my $itemnumber (@itemnumbers) {
1516 ModItemOrder($itemnumber, $parent_ordernumber);
1521 DELETE FROM aqorders
1522 WHERE ordernumber = ?
1524 $sth = $dbh->prepare($query);
1525 $sth->execute($ordernumber);
1529 return $parent_ordernumber;
1532 #------------------------------------------------------------#
1536 @results = &SearchOrder($search, $biblionumber, $complete);
1538 Searches for orders.
1540 C<$search> may take one of several forms: if it is an ISBN,
1541 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
1542 order number, C<&ordersearch> returns orders with that order number
1543 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
1544 to be a space-separated list of search terms; in this case, all of the
1545 terms must appear in the title (matching the beginning of title
1548 If C<$complete> is C<yes>, the results will include only completed
1549 orders. In any case, C<&ordersearch> ignores cancelled orders.
1551 C<&ordersearch> returns an array.
1552 C<@results> is an array of references-to-hash with the following keys:
1558 =item C<seriestitle>
1569 #### -------- SearchOrder-------------------------------
1570 my ( $ordernumber, $search, $ean, $supplierid, $basket ) = @_;
1572 my $dbh = C4::Context->dbh;
1577 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1578 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1579 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1580 WHERE (datecancellationprinted is NULL)";
1583 $query .= " AND (aqorders.ordernumber=?)";
1584 push @args, $ordernumber;
1587 $query .= " AND (biblio.title like ? OR biblio.author LIKE ? OR biblioitems.isbn like ?)";
1588 push @args, ("%$search%","%$search%","%$search%");
1591 $query .= " AND biblioitems.ean = ?";
1595 $query .= "AND aqbasket.booksellerid = ?";
1596 push @args, $supplierid;
1599 $query .= "AND aqorders.basketno = ?";
1600 push @args, $basket;
1603 my $sth = $dbh->prepare($query);
1604 $sth->execute(@args);
1605 my $results = $sth->fetchall_arrayref({});
1610 #------------------------------------------------------------#
1614 &DelOrder($biblionumber, $ordernumber);
1616 Cancel the order with the given order and biblio numbers. It does not
1617 delete any entries in the aqorders table, it merely marks them as
1623 my ( $bibnum, $ordernumber ) = @_;
1624 my $dbh = C4::Context->dbh;
1627 SET datecancellationprinted=now()
1628 WHERE biblionumber=? AND ordernumber=?
1630 my $sth = $dbh->prepare($query);
1631 $sth->execute( $bibnum, $ordernumber );
1633 my @itemnumbers = GetItemnumbersFromOrder( $ordernumber );
1634 foreach my $itemnumber (@itemnumbers){
1635 C4::Items::DelItem( $dbh, $bibnum, $itemnumber );
1640 =head2 FUNCTIONS ABOUT PARCELS
1644 #------------------------------------------------------------#
1648 @results = &GetParcel($booksellerid, $code, $date);
1650 Looks up all of the received items from the supplier with the given
1651 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
1653 C<@results> is an array of references-to-hash. The keys of each element are fields from
1654 the aqorders, biblio, and biblioitems tables of the Koha database.
1656 C<@results> is sorted alphabetically by book title.
1661 #gets all orders from a certain supplier, orders them alphabetically
1662 my ( $supplierid, $code, $datereceived ) = @_;
1663 my $dbh = C4::Context->dbh;
1666 if $code; # add % if we search on a given code (otherwise, let him empty)
1668 SELECT authorisedby,
1673 aqorders.biblionumber,
1674 aqorders.ordernumber,
1675 aqorders.parent_ordernumber,
1677 aqorders.quantityreceived,
1685 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
1686 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1687 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1688 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1690 aqbasket.booksellerid = ?
1691 AND aqinvoices.invoicenumber LIKE ?
1692 AND aqorders.datereceived = ? ";
1694 my @query_params = ( $supplierid, $code, $datereceived );
1695 if ( C4::Context->preference("IndependantBranches") ) {
1696 my $userenv = C4::Context->userenv;
1697 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1698 $strsth .= " and (borrowers.branchcode = ?
1699 or borrowers.branchcode = '')";
1700 push @query_params, $userenv->{branch};
1703 $strsth .= " ORDER BY aqbasket.basketno";
1704 # ## parcelinformation : $strsth
1705 my $sth = $dbh->prepare($strsth);
1706 $sth->execute( @query_params );
1707 while ( my $data = $sth->fetchrow_hashref ) {
1708 push( @results, $data );
1710 # ## countparcelbiblio: scalar(@results)
1716 #------------------------------------------------------------#
1720 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1722 get a lists of parcels.
1729 is the bookseller this function has to get parcels.
1732 To know on what criteria the results list has to be ordered.
1735 is the booksellerinvoicenumber.
1737 =item $datefrom & $dateto
1738 to know on what date this function has to filter its search.
1743 a pointer on a hash list containing parcel informations as such :
1749 =item Last operation
1751 =item Number of biblio
1753 =item Number of items
1760 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
1761 my $dbh = C4::Context->dbh;
1762 my @query_params = ();
1764 SELECT aqinvoices.invoicenumber,
1765 datereceived,purchaseordernumber,
1766 count(DISTINCT biblionumber) AS biblio,
1767 sum(quantity) AS itemsexpected,
1768 sum(quantityreceived) AS itemsreceived
1769 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
1770 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1771 WHERE aqbasket.booksellerid = ? and datereceived IS NOT NULL
1773 push @query_params, $bookseller;
1775 if ( defined $code ) {
1776 $strsth .= ' and aqinvoices.invoicenumber like ? ';
1777 # add a % to the end of the code to allow stemming.
1778 push @query_params, "$code%";
1781 if ( defined $datefrom ) {
1782 $strsth .= ' and datereceived >= ? ';
1783 push @query_params, $datefrom;
1786 if ( defined $dateto ) {
1787 $strsth .= 'and datereceived <= ? ';
1788 push @query_params, $dateto;
1791 $strsth .= "group by aqinvoices.invoicenumber,datereceived ";
1793 # can't use a placeholder to place this column name.
1794 # but, we could probably be checking to make sure it is a column that will be fetched.
1795 $strsth .= "order by $order " if ($order);
1797 my $sth = $dbh->prepare($strsth);
1799 $sth->execute( @query_params );
1800 my $results = $sth->fetchall_arrayref({});
1805 #------------------------------------------------------------#
1807 =head3 GetLateOrders
1809 @results = &GetLateOrders;
1811 Searches for bookseller with late orders.
1814 the table of supplier with late issues. This table is full of hashref.
1820 my $supplierid = shift;
1822 my $estimateddeliverydatefrom = shift;
1823 my $estimateddeliverydateto = shift;
1825 my $dbh = C4::Context->dbh;
1827 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
1828 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
1830 my @query_params = ();
1832 SELECT aqbasket.basketno,
1833 aqorders.ordernumber,
1834 DATE(aqbasket.closedate) AS orderdate,
1835 aqorders.rrp AS unitpricesupplier,
1836 aqorders.ecost AS unitpricelib,
1837 aqorders.claims_count AS claims_count,
1838 aqorders.claimed_date AS claimed_date,
1839 aqbudgets.budget_name AS budget,
1840 borrowers.branchcode AS branch,
1841 aqbooksellers.name AS supplier,
1842 aqbooksellers.id AS supplierid,
1843 biblio.author, biblio.title,
1844 biblioitems.publishercode AS publisher,
1845 biblioitems.publicationyear,
1846 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
1850 aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1851 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1852 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
1853 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
1854 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1855 WHERE aqorders.basketno = aqbasket.basketno
1856 AND ( datereceived = ''
1857 OR datereceived IS NULL
1858 OR aqorders.quantityreceived < aqorders.quantity
1860 AND aqbasket.closedate IS NOT NULL
1861 AND (aqorders.datecancellationprinted IS NULL OR aqorders.datecancellationprinted='0000-00-00')
1864 if ($dbdriver eq "mysql") {
1866 aqorders.quantity - COALESCE(aqorders.quantityreceived,0) AS quantity,
1867 (aqorders.quantity - COALESCE(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1868 DATEDIFF(CAST(now() AS date),closedate) AS latesince
1870 if ( defined $delay ) {
1871 $from .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) " ;
1872 push @query_params, $delay;
1875 HAVING quantity <> 0
1876 AND unitpricesupplier <> 0
1877 AND unitpricelib <> 0
1880 # FIXME: account for IFNULL as above
1882 aqorders.quantity AS quantity,
1883 aqorders.quantity * aqorders.rrp AS subtotal,
1884 (CAST(now() AS date) - closedate) AS latesince
1886 if ( defined $delay ) {
1887 $from .= " AND (closedate <= (CAST(now() AS date) -(INTERVAL ? DAY)) ";
1888 push @query_params, $delay;
1891 if (defined $supplierid) {
1892 $from .= ' AND aqbasket.booksellerid = ? ';
1893 push @query_params, $supplierid;
1895 if (defined $branch) {
1896 $from .= ' AND borrowers.branchcode LIKE ? ';
1897 push @query_params, $branch;
1900 if ( defined $estimateddeliverydatefrom or defined $estimateddeliverydateto ) {
1901 $from .= ' AND aqbooksellers.deliverytime IS NOT NULL ';
1903 if ( defined $estimateddeliverydatefrom ) {
1904 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
1905 push @query_params, $estimateddeliverydatefrom;
1907 if ( defined $estimateddeliverydateto ) {
1908 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
1909 push @query_params, $estimateddeliverydateto;
1911 if ( defined $estimateddeliverydatefrom and not defined $estimateddeliverydateto ) {
1912 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
1914 if (C4::Context->preference("IndependantBranches")
1915 && C4::Context->userenv
1916 && C4::Context->userenv->{flags} != 1 ) {
1917 $from .= ' AND borrowers.branchcode LIKE ? ';
1918 push @query_params, C4::Context->userenv->{branch};
1920 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
1921 $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
1922 my $sth = $dbh->prepare($query);
1923 $sth->execute(@query_params);
1925 while (my $data = $sth->fetchrow_hashref) {
1926 $data->{orderdate} = format_date($data->{orderdate});
1927 $data->{claimed_date} = format_date($data->{claimed_date});
1928 push @results, $data;
1933 #------------------------------------------------------------#
1937 (\@order_loop, $total_qty, $total_price, $total_qtyreceived) = GetHistory( %params );
1939 Retreives some acquisition history information
1947 basket - search both basket name and number
1948 booksellerinvoicenumber
1951 $order_loop is a list of hashrefs that each look like this:
1953 'author' => 'Twain, Mark',
1955 'biblionumber' => '215',
1957 'creationdate' => 'MM/DD/YYYY',
1958 'datereceived' => undef,
1961 'invoicenumber' => undef,
1963 'ordernumber' => '1',
1965 'quantityreceived' => undef,
1966 'title' => 'The Adventures of Huckleberry Finn'
1968 $total_qty is the sum of all of the quantities in $order_loop
1969 $total_price is the cost of each in $order_loop times the quantity
1970 $total_qtyreceived is the sum of all of the quantityreceived entries in $order_loop
1975 # don't run the query if there are no parameters (list would be too long for sure !)
1976 croak "No search params" unless @_;
1978 my $title = $params{title};
1979 my $author = $params{author};
1980 my $isbn = $params{isbn};
1981 my $ean = $params{ean};
1982 my $name = $params{name};
1983 my $from_placed_on = $params{from_placed_on};
1984 my $to_placed_on = $params{to_placed_on};
1985 my $basket = $params{basket};
1986 my $booksellerinvoicenumber = $params{booksellerinvoicenumber};
1987 my $basketgroupname = $params{basketgroupname};
1990 my $total_qtyreceived = 0;
1991 my $total_price = 0;
1993 my $dbh = C4::Context->dbh;
2001 aqbasket.basketname,
2002 aqbasket.basketgroupid,
2003 aqbasketgroups.name as groupname,
2005 aqbasket.creationdate,
2006 aqorders.datereceived,
2008 aqorders.quantityreceived,
2010 aqorders.ordernumber,
2011 aqinvoices.invoicenumber,
2012 aqbooksellers.id as id,
2013 aqorders.biblionumber
2015 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
2016 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
2017 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
2018 LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
2019 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
2020 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid";
2022 $query .= " LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber"
2023 if ( C4::Context->preference("IndependantBranches") );
2025 $query .= " WHERE (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
2027 my @query_params = ();
2030 $query .= " AND biblio.title LIKE ? ";
2031 $title =~ s/\s+/%/g;
2032 push @query_params, "%$title%";
2036 $query .= " AND biblio.author LIKE ? ";
2037 push @query_params, "%$author%";
2041 $query .= " AND biblioitems.isbn LIKE ? ";
2042 push @query_params, "%$isbn%";
2044 if ( defined $ean and $ean ) {
2045 $query .= " AND biblioitems.ean = ? ";
2046 push @query_params, "$ean";
2049 $query .= " AND aqbooksellers.name LIKE ? ";
2050 push @query_params, "%$name%";
2053 if ( $from_placed_on ) {
2054 $query .= " AND creationdate >= ? ";
2055 push @query_params, $from_placed_on;
2058 if ( $to_placed_on ) {
2059 $query .= " AND creationdate <= ? ";
2060 push @query_params, $to_placed_on;
2064 if ($basket =~ m/^\d+$/) {
2065 $query .= " AND aqorders.basketno = ? ";
2066 push @query_params, $basket;
2068 $query .= " AND aqbasket.basketname LIKE ? ";
2069 push @query_params, "%$basket%";
2073 if ($booksellerinvoicenumber) {
2074 $query .= " AND aqinvoices.invoicenumber LIKE ? ";
2075 push @query_params, "%$booksellerinvoicenumber%";
2078 if ($basketgroupname) {
2079 $query .= " AND aqbasketgroups.name LIKE ? ";
2080 push @query_params, "%$basketgroupname%";
2083 if ( C4::Context->preference("IndependantBranches") ) {
2084 my $userenv = C4::Context->userenv;
2085 if ( $userenv && ($userenv->{flags} || 0) != 1 ) {
2086 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
2087 push @query_params, $userenv->{branch};
2090 $query .= " ORDER BY id";
2091 my $sth = $dbh->prepare($query);
2092 $sth->execute( @query_params );
2094 while ( my $line = $sth->fetchrow_hashref ) {
2095 $line->{count} = $cnt++;
2096 $line->{toggle} = 1 if $cnt % 2;
2097 push @order_loop, $line;
2098 $total_qty += $line->{'quantity'};
2099 $total_qtyreceived += $line->{'quantityreceived'};
2100 $total_price += $line->{'quantity'} * $line->{'ecost'};
2102 return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
2105 =head2 GetRecentAcqui
2107 $results = GetRecentAcqui($days);
2109 C<$results> is a ref to a table which containts hashref
2113 sub GetRecentAcqui {
2115 my $dbh = C4::Context->dbh;
2119 ORDER BY timestamp DESC
2122 my $sth = $dbh->prepare($query);
2124 my $results = $sth->fetchall_arrayref({});
2130 $contractlist = &GetContracts($booksellerid, $activeonly);
2132 Looks up the contracts that belong to a bookseller
2134 Returns a list of contracts
2138 =item C<$booksellerid> is the "id" field in the "aqbooksellers" table.
2140 =item C<$activeonly> if exists get only contracts that are still active.
2147 my ( $booksellerid, $activeonly ) = @_;
2148 my $dbh = C4::Context->dbh;
2150 if (! $activeonly) {
2154 WHERE booksellerid=?
2159 WHERE booksellerid=?
2160 AND contractenddate >= CURDATE( )";
2162 my $sth = $dbh->prepare($query);
2163 $sth->execute( $booksellerid );
2165 while (my $data = $sth->fetchrow_hashref ) {
2166 push(@results, $data);
2172 #------------------------------------------------------------#
2176 $contract = &GetContract($contractID);
2178 Looks up the contract that has PRIMKEY (contractnumber) value $contractID
2185 my ( $contractno ) = @_;
2186 my $dbh = C4::Context->dbh;
2190 WHERE contractnumber=?
2193 my $sth = $dbh->prepare($query);
2194 $sth->execute( $contractno );
2195 my $result = $sth->fetchrow_hashref;
2203 &AddClaim($ordernumber);
2205 Add a claim for an order
2211 my ($ordernumber) = @_;
2212 my $dbh = C4::Context->dbh;
2215 claims_count = claims_count + 1,
2216 claimed_date = CURDATE()
2217 WHERE ordernumber = ?
2219 my $sth = $dbh->prepare($query);
2220 $sth->execute($ordernumber);
2225 my @invoices = GetInvoices(
2226 invoicenumber => $invoicenumber,
2227 suppliername => $suppliername,
2228 shipmentdatefrom => $shipmentdatefrom, # ISO format
2229 shipmentdateto => $shipmentdateto, # ISO format
2230 billingdatefrom => $billingdatefrom, # ISO format
2231 billingdateto => $billingdateto, # ISO format
2232 isbneanissn => $isbn_or_ean_or_issn,
2235 publisher => $publisher,
2236 publicationyear => $publicationyear,
2237 branchcode => $branchcode,
2238 order_by => $order_by
2241 Return a list of invoices that match all given criteria.
2243 $order_by is "column_name (asc|desc)", where column_name is any of
2244 'invoicenumber', 'booksellerid', 'shipmentdate', 'billingdate', 'closedate',
2245 'shipmentcost', 'shipmentcost_budgetid'.
2247 asc is the default if omitted
2254 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2255 closedate shipmentcost shipmentcost_budgetid);
2257 my $dbh = C4::Context->dbh;
2259 SELECT aqinvoices.*, aqbooksellers.name AS suppliername,
2262 aqorders.datereceived IS NOT NULL,
2263 aqorders.biblionumber,
2266 ) AS receivedbiblios,
2267 SUM(aqorders.quantityreceived) AS receiveditems
2269 LEFT JOIN aqbooksellers ON aqbooksellers.id = aqinvoices.booksellerid
2270 LEFT JOIN aqorders ON aqorders.invoiceid = aqinvoices.invoiceid
2271 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2272 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
2273 LEFT JOIN subscription ON biblio.biblionumber = subscription.biblionumber
2278 if($args{supplierid}) {
2279 push @bind_strs, " aqinvoices.booksellerid = ? ";
2280 push @bind_args, $args{supplierid};
2282 if($args{invoicenumber}) {
2283 push @bind_strs, " aqinvoices.invoicenumber LIKE ? ";
2284 push @bind_args, "%$args{invoicenumber}%";
2286 if($args{suppliername}) {
2287 push @bind_strs, " aqbooksellers.name LIKE ? ";
2288 push @bind_args, "%$args{suppliername}%";
2290 if($args{shipmentdatefrom}) {
2291 push @bind_strs, " aqinvoices.shipementdate >= ? ";
2292 push @bind_args, $args{shipmentdatefrom};
2294 if($args{shipmentdateto}) {
2295 push @bind_strs, " aqinvoices.shipementdate <= ? ";
2296 push @bind_args, $args{shipmentdateto};
2298 if($args{billingdatefrom}) {
2299 push @bind_strs, " aqinvoices.billingdate >= ? ";
2300 push @bind_args, $args{billingdatefrom};
2302 if($args{billingdateto}) {
2303 push @bind_strs, " aqinvoices.billingdate <= ? ";
2304 push @bind_args, $args{billingdateto};
2306 if($args{isbneanissn}) {
2307 push @bind_strs, " (biblioitems.isbn LIKE ? OR biblioitems.ean LIKE ? OR biblioitems.issn LIKE ? ) ";
2308 push @bind_args, $args{isbneanissn}, $args{isbneanissn}, $args{isbneanissn};
2311 push @bind_strs, " biblio.title LIKE ? ";
2312 push @bind_args, $args{title};
2315 push @bind_strs, " biblio.author LIKE ? ";
2316 push @bind_args, $args{author};
2318 if($args{publisher}) {
2319 push @bind_strs, " biblioitems.publishercode LIKE ? ";
2320 push @bind_args, $args{publisher};
2322 if($args{publicationyear}) {
2323 push @bind_strs, " biblioitems.publicationyear = ? ";
2324 push @bind_args, $args{publicationyear};
2326 if($args{branchcode}) {
2327 push @bind_strs, " aqorders.branchcode = ? ";
2328 push @bind_args, $args{branchcode};
2331 $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
2332 $query .= " GROUP BY aqinvoices.invoiceid ";
2334 if($args{order_by}) {
2335 my ($column, $direction) = split / /, $args{order_by};
2336 if(grep /^$column$/, @columns) {
2337 $direction ||= 'ASC';
2338 $query .= " ORDER BY $column $direction";
2342 my $sth = $dbh->prepare($query);
2343 $sth->execute(@bind_args);
2345 my $results = $sth->fetchall_arrayref({});
2351 my $invoice = GetInvoice($invoiceid);
2353 Get informations about invoice with given $invoiceid
2355 Return a hash filled with aqinvoices.* fields
2360 my ($invoiceid) = @_;
2363 return unless $invoiceid;
2365 my $dbh = C4::Context->dbh;
2371 my $sth = $dbh->prepare($query);
2372 $sth->execute($invoiceid);
2374 $invoice = $sth->fetchrow_hashref;
2378 =head3 GetInvoiceDetails
2380 my $invoice = GetInvoiceDetails($invoiceid)
2382 Return informations about an invoice + the list of related order lines
2384 Orders informations are in $invoice->{orders} (array ref)
2388 sub GetInvoiceDetails {
2389 my ($invoiceid) = @_;
2391 if ( !defined $invoiceid ) {
2392 carp 'GetInvoiceDetails called without an invoiceid';
2396 my $dbh = C4::Context->dbh;
2398 SELECT aqinvoices.*, aqbooksellers.name AS suppliername
2400 LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
2403 my $sth = $dbh->prepare($query);
2404 $sth->execute($invoiceid);
2406 my $invoice = $sth->fetchrow_hashref;
2409 SELECT aqorders.*, biblio.*
2411 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2414 $sth = $dbh->prepare($query);
2415 $sth->execute($invoiceid);
2416 $invoice->{orders} = $sth->fetchall_arrayref({});
2417 $invoice->{orders} ||= []; # force an empty arrayref if fetchall_arrayref fails
2424 my $invoiceid = AddInvoice(
2425 invoicenumber => $invoicenumber,
2426 booksellerid => $booksellerid,
2427 shipmentdate => $shipmentdate,
2428 billingdate => $billingdate,
2429 closedate => $closedate,
2430 shipmentcost => $shipmentcost,
2431 shipmentcost_budgetid => $shipmentcost_budgetid
2434 Create a new invoice and return its id or undef if it fails.
2441 return unless(%invoice and $invoice{invoicenumber});
2443 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2444 closedate shipmentcost shipmentcost_budgetid);
2448 foreach my $key (keys %invoice) {
2449 if(0 < grep(/^$key$/, @columns)) {
2450 push @set_strs, "$key = ?";
2451 push @set_args, ($invoice{$key} || undef);
2457 my $dbh = C4::Context->dbh;
2458 my $query = "INSERT INTO aqinvoices SET ";
2459 $query .= join (",", @set_strs);
2460 my $sth = $dbh->prepare($query);
2461 $rv = $sth->execute(@set_args);
2463 $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
2472 invoiceid => $invoiceid, # Mandatory
2473 invoicenumber => $invoicenumber,
2474 booksellerid => $booksellerid,
2475 shipmentdate => $shipmentdate,
2476 billingdate => $billingdate,
2477 closedate => $closedate,
2478 shipmentcost => $shipmentcost,
2479 shipmentcost_budgetid => $shipmentcost_budgetid
2482 Modify an invoice, invoiceid is mandatory.
2484 Return undef if it fails.
2491 return unless(%invoice and $invoice{invoiceid});
2493 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2494 closedate shipmentcost shipmentcost_budgetid);
2498 foreach my $key (keys %invoice) {
2499 if(0 < grep(/^$key$/, @columns)) {
2500 push @set_strs, "$key = ?";
2501 push @set_args, ($invoice{$key} || undef);
2505 my $dbh = C4::Context->dbh;
2506 my $query = "UPDATE aqinvoices SET ";
2507 $query .= join(",", @set_strs);
2508 $query .= " WHERE invoiceid = ?";
2510 my $sth = $dbh->prepare($query);
2511 $sth->execute(@set_args, $invoice{invoiceid});
2516 CloseInvoice($invoiceid);
2520 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
2525 my ($invoiceid) = @_;
2527 return unless $invoiceid;
2529 my $dbh = C4::Context->dbh;
2532 SET closedate = CAST(NOW() AS DATE)
2535 my $sth = $dbh->prepare($query);
2536 $sth->execute($invoiceid);
2539 =head3 ReopenInvoice
2541 ReopenInvoice($invoiceid);
2545 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => C4::Dates->new()->output('iso'))
2550 my ($invoiceid) = @_;
2552 return unless $invoiceid;
2554 my $dbh = C4::Context->dbh;
2557 SET closedate = NULL
2560 my $sth = $dbh->prepare($query);
2561 $sth->execute($invoiceid);
2569 Koha Development Team <http://koha-community.org/>