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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Carp qw( carp croak );
25 use C4::Suggestions qw( GetSuggestion GetSuggestionFromBiblionumber ModSuggestion );
26 use C4::Biblio qw( GetMarcFromKohaField GetMarcStructure IsMarcStructureInternal );
27 use C4::Contract qw( GetContract );
28 use C4::Log qw( logaction );
29 use C4::Templates qw(gettemplate);
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::Acquisition::Baskets;
32 use Koha::Acquisition::Booksellers;
33 use Koha::Acquisition::Invoices;
34 use Koha::Acquisition::Orders;
38 use Koha::Number::Price;
40 use Koha::CsvProfiles;
46 use JSON qw( to_json );
49 our (@ISA, @EXPORT_OK);
54 GetBasket NewBasket ReopenBasket ModBasket
55 GetBasketAsCSV GetBasketGroupAsCSV
56 GetBasketsByBookseller GetBasketsByBasketgroup
57 GetBasketsInfosByBookseller
59 GetBasketUsers ModBasketUsers
64 ModBasketgroup NewBasketgroup DelBasketgroup GetBasketgroup CloseBasketgroup
65 GetBasketgroups ReOpenBasketgroup
67 ModOrder GetOrder GetOrders GetOrdersByBiblionumber
68 GetOrderFromItemnumber
69 SearchOrders GetHistory GetRecentAcqui
70 ModReceiveOrder CancelReceipt
87 GetBiblioCountByBasketno
104 sub GetOrderFromItemnumber {
105 my ($itemnumber) = @_;
106 my $dbh = C4::Context->dbh;
109 SELECT * from aqorders LEFT JOIN aqorders_items
110 ON ( aqorders.ordernumber = aqorders_items.ordernumber )
111 WHERE itemnumber = ? |;
113 my $sth = $dbh->prepare($query);
117 $sth->execute($itemnumber);
119 my $order = $sth->fetchrow_hashref;
126 C4::Acquisition - Koha functions for dealing with orders and acquisitions
134 The functions in this module deal with acquisitions, managing book
135 orders, basket and parcels.
139 =head2 FUNCTIONS ABOUT BASKETS
143 $aqbasket = &GetBasket($basketnumber);
145 get all basket informations in aqbasket for a given basket
147 B<returns:> informations for a given basket returned as a hashref.
153 my $dbh = C4::Context->dbh;
156 concat( b.firstname,' ',b.surname) AS authorisedbyname
158 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
161 my $sth=$dbh->prepare($query);
162 $sth->execute($basketno);
163 my $basket = $sth->fetchrow_hashref;
167 #------------------------------------------------------------#
171 $basket = &NewBasket( $booksellerid, $authorizedby, $basketname,
172 $basketnote, $basketbooksellernote, $basketcontractnumber, $deliveryplace, $billingplace, $is_standing, $create_items );
174 Create a new basket in aqbasket table
178 =item C<$booksellerid> is a foreign key in the aqbasket table
180 =item C<$authorizedby> is the username of who created the basket
184 The other parameters are optional, see ModBasketHeader for more info on them.
189 my ( $booksellerid, $authorisedby, $basketname, $basketnote,
190 $basketbooksellernote, $basketcontractnumber, $deliveryplace,
191 $billingplace, $is_standing, $create_items ) = @_;
192 my $dbh = C4::Context->dbh;
194 'INSERT INTO aqbasket (creationdate,booksellerid,authorisedby) '
195 . 'VALUES (now(),?,?)';
196 $dbh->do( $query, {}, $booksellerid, $authorisedby );
198 my $basket = $dbh->{mysql_insertid};
199 $basketname ||= q{}; # default to empty strings
201 $basketbooksellernote ||= q{};
203 # Log the basket creation
204 if (C4::Context->preference("AcquisitionLog")) {
205 my $created = Koha::Acquisition::Baskets->find( $basket );
210 to_json($created->unblessed)
214 ModBasketHeader( $basket, $basketname, $basketnote, $basketbooksellernote,
215 $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items );
222 &ReopenBasket($basketno);
230 my $dbh = C4::Context->dbh;
231 $dbh->do( q{UPDATE aqbasket SET closedate=NULL WHERE basketno=?}, {}, $basketno );
235 SET orderstatus = 'new'
237 AND orderstatus NOT IN ( 'complete', 'cancelled' )
240 # Log the basket reopening
241 if (C4::Context->preference("AcquisitionLog")) {
242 my $reopened = Koha::Acquisition::Baskets->find( $basketno );
247 to_json($reopened->unblessed)
253 #------------------------------------------------------------#
255 =head3 GetBasketAsCSV
257 &GetBasketAsCSV($basketno);
259 Export a basket as CSV
261 $cgi parameter is needed for column name translation
266 my ($basketno, $cgi, $csv_profile_id) = @_;
267 my $basket = GetBasket($basketno);
268 my @orders = GetOrders($basketno);
269 my $contract = GetContract({
270 contractnumber => $basket->{'contractnumber'}
273 my $template = C4::Templates::gettemplate("acqui/csv/basket.tt", "intranet", $cgi);
275 if ($csv_profile_id) {
276 my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
277 Koha::Exceptions::ObjectNotFound->throw( 'There is no valid csv profile given') unless $csv_profile;
279 my $delimiter = $csv_profile->csv_separator;
280 $delimiter = "\t" if $delimiter eq "\\t";
281 my $csv = Text::CSV_XS->new({'quote_char'=>'"','escape_char'=>'"','sep_char'=>$delimiter,'binary'=>1});
282 my $csv_profile_content = $csv_profile->content;
283 my ( @headers, @fields );
284 while ( $csv_profile_content =~ /
287 ([^\|]*) # fieldname (table.row or row)
291 my $field = ($2 eq '') ? $1 : $2;
293 $header =~ s/^\s+|\s+$//g; # Trim whitespaces
294 push @headers, $header;
296 $field =~ s/[^\.]*\.{1}//; # Remove the table name if exists.
297 $field =~ s/^\s+|\s+$//g; # Trim whitespaces
298 push @fields, $field;
300 for my $order (@orders) {
302 my $biblio = Koha::Biblios->find( $order->{biblionumber} );
303 my $biblioitem = $biblio->biblioitem;
304 $order = { %$order, %{ $biblioitem->unblessed } };
306 $order = {%$order, %$contract};
308 $order = {%$order, %$basket, %{ $biblio->unblessed }};
309 for my $field (@fields) {
310 push @row, $order->{$field};
314 my $content = join( $delimiter, @headers ) . "\n";
315 for my $row ( @rows ) {
316 $csv->combine(@$row);
317 my $string = $csv->string;
318 $content .= $string . "\n";
323 foreach my $order (@orders) {
324 my $biblio = Koha::Biblios->find( $order->{biblionumber} );
327 $biblioitem = $biblio->biblioitem;
330 contractname => $contract->{'contractname'},
331 ordernumber => $order->{'ordernumber'},
332 entrydate => $order->{'entrydate'},
333 isbn => $order->{'isbn'},
334 author => $biblio ? $biblio->author : q{},
335 title => $biblio ? $biblio->title : q{},
336 publicationyear => $biblioitem ? $biblioitem->publicationyear : q{},
337 publishercode => $biblioitem ? $biblioitem->publishercode : q{},
338 collectiontitle => $biblioitem ? $biblioitem->collectiontitle : q{},
339 notes => $order->{'order_vendornote'},
340 quantity => $order->{'quantity'},
341 rrp => $order->{'rrp'},
343 for my $place ( qw( deliveryplace billingplace ) ) {
344 if ( my $library = Koha::Libraries->find( $row->{deliveryplace} ) ) {
345 $row->{$place} = $library->branchname
349 contractname author title publishercode collectiontitle notes
350 deliveryplace billingplace
352 # Double the quotes to not be interpreted as a field end
353 $row->{$_} =~ s/"/""/g if $row->{$_};
359 if(defined $a->{publishercode} and defined $b->{publishercode}) {
360 $a->{publishercode} cmp $b->{publishercode};
364 $template->param(rows => \@rows);
366 return $template->output;
371 =head3 GetBasketGroupAsCSV
373 &GetBasketGroupAsCSV($basketgroupid);
375 Export a basket group as CSV
377 $cgi parameter is needed for column name translation
381 sub GetBasketGroupAsCSV {
382 my ($basketgroupid, $cgi) = @_;
383 my $baskets = GetBasketsByBasketgroup($basketgroupid);
385 my $template = C4::Templates::gettemplate('acqui/csv/basketgroup.tt', 'intranet', $cgi);
388 for my $basket (@$baskets) {
389 my @orders = GetOrders( $basket->{basketno} );
390 my $contract = GetContract({
391 contractnumber => $basket->{contractnumber}
393 my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
394 my $basketgroup = GetBasketgroup( $$basket{basketgroupid} );
396 foreach my $order (@orders) {
397 my $biblio = Koha::Biblios->find( $order->{biblionumber} );
400 $biblioitem = $biblio->biblioitem;
403 clientnumber => $bookseller->accountnumber,
404 basketname => $basket->{basketname},
405 ordernumber => $order->{ordernumber},
406 author => $biblio ? $biblio->author : q{},
407 title => $biblio ? $biblio->title : q{},
408 publishercode => $biblioitem ? $biblioitem->publishercode : q{},
409 publicationyear => $biblioitem ? $biblioitem->publicationyear : q{},
410 collectiontitle => $biblioitem ? $biblioitem->collectiontitle : q{},
411 isbn => $order->{isbn},
412 quantity => $order->{quantity},
413 rrp_tax_included => $order->{rrp_tax_included},
414 rrp_tax_excluded => $order->{rrp_tax_excluded},
415 discount => $bookseller->discount,
416 ecost_tax_included => $order->{ecost_tax_included},
417 ecost_tax_excluded => $order->{ecost_tax_excluded},
418 notes => $order->{order_vendornote},
419 entrydate => $order->{entrydate},
420 booksellername => $bookseller->name,
421 bookselleraddress => $bookseller->address1,
422 booksellerpostal => $bookseller->postal,
423 contractnumber => $contract->{contractnumber},
424 contractname => $contract->{contractname},
427 basketgroupdeliveryplace => $basketgroup->{deliveryplace},
428 basketgroupbillingplace => $basketgroup->{billingplace},
429 basketdeliveryplace => $basket->{deliveryplace},
430 basketbillingplace => $basket->{billingplace},
432 for my $place (qw( basketgroupdeliveryplace basketgroupbillingplace basketdeliveryplace basketbillingplace )) {
433 if ( my $library = Koha::Libraries->find( $temp->{$place} ) ) {
434 $row->{$place} = $library->branchname;
438 basketname author title publishercode collectiontitle notes
439 booksellername bookselleraddress booksellerpostal contractname
440 basketgroupdeliveryplace basketgroupbillingplace
441 basketdeliveryplace basketbillingplace
443 # Double the quotes to not be interpreted as a field end
444 $row->{$_} =~ s/"/""/g if $row->{$_};
449 $template->param(rows => \@rows);
451 return $template->output;
455 =head3 CloseBasketgroup
457 &CloseBasketgroup($basketgroupno);
463 sub CloseBasketgroup {
464 my ($basketgroupno) = @_;
465 my $dbh = C4::Context->dbh;
466 my $sth = $dbh->prepare("
467 UPDATE aqbasketgroups
471 $sth->execute($basketgroupno);
474 #------------------------------------------------------------#
476 =head3 ReOpenBaskergroup($basketgroupno)
478 &ReOpenBaskergroup($basketgroupno);
484 sub ReOpenBasketgroup {
485 my ($basketgroupno) = @_;
486 my $dbh = C4::Context->dbh;
487 my $sth = $dbh->prepare("
488 UPDATE aqbasketgroups
492 $sth->execute($basketgroupno);
495 #------------------------------------------------------------#
499 &ModBasket($basketinfo);
501 Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
505 =item C<$basketno> is the primary key of the basket in the aqbasket table.
512 my $basketinfo = shift;
513 my $query = "UPDATE aqbasket SET ";
515 foreach my $key (keys %$basketinfo){
516 if ($key ne 'basketno'){
517 $query .= "$key=?, ";
518 push(@params, $basketinfo->{$key} || undef );
521 # get rid of the "," at the end of $query
522 if (substr($query, length($query)-2) eq ', '){
527 $query .= "WHERE basketno=?";
528 push(@params, $basketinfo->{'basketno'});
529 my $dbh = C4::Context->dbh;
530 my $sth = $dbh->prepare($query);
531 $sth->execute(@params);
533 # Log the basket update
534 if (C4::Context->preference("AcquisitionLog")) {
535 my $modified = Koha::Acquisition::Baskets->find(
536 $basketinfo->{basketno}
541 $basketinfo->{basketno},
542 to_json($modified->unblessed)
549 #------------------------------------------------------------#
551 =head3 ModBasketHeader
553 &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid);
555 Modifies a basket's header.
559 =item C<$basketno> is the "basketno" field in the "aqbasket" table;
561 =item C<$basketname> is the "basketname" field in the "aqbasket" table;
563 =item C<$note> is the "note" field in the "aqbasket" table;
565 =item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
567 =item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
569 =item C<$booksellerid> is the id (foreign) key in the "aqbooksellers" table for the vendor.
571 =item C<$deliveryplace> is the "deliveryplace" field in the aqbasket table.
573 =item C<$billingplace> is the "billingplace" field in the aqbasket table.
575 =item C<$is_standing> is the "is_standing" field in the aqbasket table.
577 =item C<$create_items> should be set to 'ordering', 'receiving' or 'cataloguing' (or undef, in which
578 case the AcqCreateItem syspref takes precedence).
584 sub ModBasketHeader {
585 my ($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items) = @_;
590 SET basketname=?, note=?, booksellernote=?, booksellerid=?, deliveryplace=?, billingplace=?, is_standing=?, create_items=?
594 my $dbh = C4::Context->dbh;
595 my $sth = $dbh->prepare($query);
596 $sth->execute($basketname, $note, $booksellernote, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items || undef, $basketno);
598 if ( $contractnumber ) {
599 my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
600 my $sth2 = $dbh->prepare($query2);
601 $sth2->execute($contractnumber,$basketno);
604 # Log the basket update
605 if (C4::Context->preference("AcquisitionLog")) {
606 my $modified = Koha::Acquisition::Baskets->find(
611 'MODIFY_BASKET_HEADER',
613 to_json($modified->unblessed)
620 #------------------------------------------------------------#
622 =head3 GetBasketsByBookseller
624 @results = &GetBasketsByBookseller($booksellerid, $extra);
626 Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
630 =item C<$booksellerid> is the 'id' field of the bookseller in the aqbooksellers table
632 =item C<$extra> is the extra sql parameters, can be
634 $extra->{groupby}: group baskets by column
635 ex. $extra->{groupby} = aqbasket.basketgroupid
636 $extra->{orderby}: order baskets by column
637 $extra->{limit}: limit number of results (can be helpful for pagination)
643 sub GetBasketsByBookseller {
644 my ($booksellerid, $extra) = @_;
645 my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
647 if ($extra->{groupby}) {
648 $query .= " GROUP by $extra->{groupby}";
650 if ($extra->{orderby}){
651 $query .= " ORDER by $extra->{orderby}";
653 if ($extra->{limit}){
654 $query .= " LIMIT $extra->{limit}";
657 my $dbh = C4::Context->dbh;
658 my $sth = $dbh->prepare($query);
659 $sth->execute($booksellerid);
660 return $sth->fetchall_arrayref({});
663 =head3 GetBasketsInfosByBookseller
665 my $baskets = GetBasketsInfosByBookseller($supplierid, $allbaskets);
667 The optional second parameter allbaskets is a boolean allowing you to
668 select all baskets from the supplier; by default only active baskets (open or
669 closed but still something to receive) are returned.
671 Returns in a arrayref of hashref all about booksellers baskets, plus:
672 total_biblios: Number of distinct biblios in basket
673 total_items: Number of items in basket
674 expected_items: Number of non-received items in basket
678 sub GetBasketsInfosByBookseller {
679 my ($supplierid, $allbaskets) = @_;
681 return unless $supplierid;
683 my $dbh = C4::Context->dbh;
685 SELECT aqbasket.basketno, aqbasket.basketname, aqbasket.note, aqbasket.booksellernote, aqbasket.contractnumber, aqbasket.creationdate, aqbasket.closedate, aqbasket.booksellerid, aqbasket.authorisedby, aqbasket.booksellerinvoicenumber, aqbasket.basketgroupid, aqbasket.deliveryplace, aqbasket.billingplace, aqbasket.branch, aqbasket.is_standing, aqbasket.create_items,
686 SUM(aqorders.quantity) AS total_items,
688 IF ( aqorders.orderstatus = 'cancelled', aqorders.quantity, 0 )
689 ) AS total_items_cancelled,
690 COUNT(DISTINCT aqorders.biblionumber) AS total_biblios,
692 IF(aqorders.datereceived IS NULL
693 AND aqorders.datecancellationprinted IS NULL
697 SUM( aqorders.uncertainprice ) AS uncertainprices
699 LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
700 WHERE booksellerid = ?};
702 $query.=" GROUP BY aqbasket.basketno, aqbasket.basketname, aqbasket.note, aqbasket.booksellernote, aqbasket.contractnumber, aqbasket.creationdate, aqbasket.closedate, aqbasket.booksellerid, aqbasket.authorisedby, aqbasket.booksellerinvoicenumber, aqbasket.basketgroupid, aqbasket.deliveryplace, aqbasket.billingplace, aqbasket.branch, aqbasket.is_standing, aqbasket.create_items";
704 unless ( $allbaskets ) {
705 # Don't show the basket if it's NOT CLOSED or is FULLY RECEIVED
706 $query.=" HAVING (closedate IS NULL OR (
708 IF(aqorders.datereceived IS NULL
709 AND aqorders.datecancellationprinted IS NULL
715 my $sth = $dbh->prepare($query);
716 $sth->execute($supplierid);
717 my $baskets = $sth->fetchall_arrayref({});
719 # Retrieve the number of biblios cancelled
720 my $cancelled_biblios = $dbh->selectall_hashref( q|
721 SELECT COUNT(DISTINCT(biblionumber)) AS total_biblios_cancelled, aqbasket.basketno
723 LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
724 WHERE booksellerid = ?
725 AND aqorders.orderstatus = 'cancelled'
726 GROUP BY aqbasket.basketno
727 |, 'basketno', {}, $supplierid );
729 $_->{total_biblios_cancelled} = $cancelled_biblios->{$_->{basketno}}{total_biblios_cancelled} || 0
735 =head3 GetBasketUsers
737 $basketusers_ids = &GetBasketUsers($basketno);
739 Returns a list of all borrowernumbers that are in basket users list
744 my $basketno = shift;
746 return unless $basketno;
749 SELECT borrowernumber
753 my $dbh = C4::Context->dbh;
754 my $sth = $dbh->prepare($query);
755 $sth->execute($basketno);
756 my $results = $sth->fetchall_arrayref( {} );
759 foreach (@$results) {
760 push @borrowernumbers, $_->{'borrowernumber'};
763 return @borrowernumbers;
766 =head3 ModBasketUsers
768 my @basketusers_ids = (1, 2, 3);
769 &ModBasketUsers($basketno, @basketusers_ids);
771 Delete all users from basket users list, and add users in C<@basketusers_ids>
777 my ($basketno, @basketusers_ids) = @_;
779 return unless $basketno;
781 my $dbh = C4::Context->dbh;
783 DELETE FROM aqbasketusers
786 my $sth = $dbh->prepare($query);
787 $sth->execute($basketno);
790 INSERT INTO aqbasketusers (basketno, borrowernumber)
793 $sth = $dbh->prepare($query);
794 foreach my $basketuser_id (@basketusers_ids) {
795 $sth->execute($basketno, $basketuser_id);
798 # Log the basket update
799 if (C4::Context->preference("AcquisitionLog")) {
802 'MODIFY_BASKET_USERS',
805 basketno => $basketno,
806 basketusers => @basketusers_ids
814 =head3 CanUserManageBasket
816 my $bool = CanUserManageBasket($borrower, $basket[, $userflags]);
817 my $bool = CanUserManageBasket($borrowernumber, $basketno[, $userflags]);
819 Check if a borrower can manage a basket, according to system preference
820 AcqViewBaskets, user permissions and basket properties (creator, users list,
823 First parameter can be either a borrowernumber or a hashref as returned by
824 Koha::Patron->unblessed
826 Second parameter can be either a basketno or a hashref as returned by
827 C4::Acquisition::GetBasket.
829 The third parameter is optional. If given, it should be a hashref as returned
830 by C4::Auth::getuserflags. If not, getuserflags is called.
832 If user is authorised to manage basket, returns 1.
837 sub CanUserManageBasket {
838 my ($borrower, $basket, $userflags) = @_;
840 if (!ref $borrower) {
841 # FIXME This needs to be replaced
842 # We should not accept both scalar and array
843 # Tests need to be updated
844 $borrower = Koha::Patrons->find( $borrower )->unblessed;
847 $basket = GetBasket($basket);
850 return 0 unless ($basket and $borrower);
852 my $borrowernumber = $borrower->{borrowernumber};
853 my $basketno = $basket->{basketno};
855 my $AcqViewBaskets = C4::Context->preference('AcqViewBaskets');
857 if (!defined $userflags) {
858 my $dbh = C4::Context->dbh;
859 my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE borrowernumber = ?");
860 $sth->execute($borrowernumber);
861 my ($flags) = $sth->fetchrow_array;
864 $userflags = C4::Auth::getuserflags($flags, $borrower->{userid}, $dbh);
867 unless ($userflags->{superlibrarian}
868 || (ref $userflags->{acquisition} && $userflags->{acquisition}->{order_manage_all})
869 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
871 if (not exists $userflags->{acquisition}) {
875 if ( (ref $userflags->{acquisition} && !$userflags->{acquisition}->{order_manage})
876 || (!ref $userflags->{acquisition} && !$userflags->{acquisition}) ) {
880 if ($AcqViewBaskets eq 'user'
881 && $basket->{authorisedby} != $borrowernumber
882 && ! grep { $borrowernumber eq $_ } GetBasketUsers($basketno)) {
886 if ($AcqViewBaskets eq 'branch' && defined $basket->{branch}
887 && $basket->{branch} ne $borrower->{branchcode}) {
895 #------------------------------------------------------------#
897 =head3 GetBasketsByBasketgroup
899 $baskets = &GetBasketsByBasketgroup($basketgroupid);
901 Returns a reference to all baskets that belong to basketgroup $basketgroupid.
905 sub GetBasketsByBasketgroup {
906 my $basketgroupid = shift;
908 SELECT *, aqbasket.booksellerid as booksellerid
910 LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?
912 my $dbh = C4::Context->dbh;
913 my $sth = $dbh->prepare($query);
914 $sth->execute($basketgroupid);
915 return $sth->fetchall_arrayref({});
918 #------------------------------------------------------------#
920 =head3 NewBasketgroup
922 $basketgroupid = NewBasketgroup(\%hashref);
924 Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
926 $hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
928 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
930 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
932 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
934 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
936 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
938 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
940 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
945 my $basketgroupinfo = shift;
946 die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
947 my $query = "INSERT INTO aqbasketgroups (";
949 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
950 if ( defined $basketgroupinfo->{$field} ) {
951 $query .= "$field, ";
952 push(@params, $basketgroupinfo->{$field});
955 $query .= "booksellerid) VALUES (";
960 push(@params, $basketgroupinfo->{'booksellerid'});
961 my $dbh = C4::Context->dbh;
962 my $sth = $dbh->prepare($query);
963 $sth->execute(@params);
964 my $basketgroupid = $dbh->{'mysql_insertid'};
965 if( $basketgroupinfo->{'basketlist'} ) {
966 foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
967 my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
968 my $sth2 = $dbh->prepare($query2);
969 $sth2->execute($basketgroupid, $basketno);
972 return $basketgroupid;
975 #------------------------------------------------------------#
977 =head3 ModBasketgroup
979 ModBasketgroup(\%hashref);
981 Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
983 $hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
985 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
987 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
989 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
991 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
993 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
995 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
997 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
1001 sub ModBasketgroup {
1002 my $basketgroupinfo = shift;
1003 die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
1004 my $dbh = C4::Context->dbh;
1005 my $query = "UPDATE aqbasketgroups SET ";
1007 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
1008 if ( defined $basketgroupinfo->{$field} ) {
1009 $query .= "$field=?, ";
1010 push(@params, $basketgroupinfo->{$field});
1015 $query .= " WHERE id=?";
1016 push(@params, $basketgroupinfo->{'id'});
1017 my $sth = $dbh->prepare($query);
1018 $sth->execute(@params);
1020 $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
1021 $sth->execute($basketgroupinfo->{'id'});
1023 if($basketgroupinfo->{'basketlist'} && @{$basketgroupinfo->{'basketlist'}}){
1024 $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
1025 foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
1026 $sth->execute($basketgroupinfo->{'id'}, $basketno);
1032 #------------------------------------------------------------#
1034 =head3 DelBasketgroup
1036 DelBasketgroup($basketgroupid);
1038 Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
1042 =item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
1048 sub DelBasketgroup {
1049 my $basketgroupid = shift;
1050 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
1051 my $query = "DELETE FROM aqbasketgroups WHERE id=?";
1052 my $dbh = C4::Context->dbh;
1053 my $sth = $dbh->prepare($query);
1054 $sth->execute($basketgroupid);
1058 #------------------------------------------------------------#
1061 =head2 FUNCTIONS ABOUT ORDERS
1063 =head3 GetBasketgroup
1065 $basketgroup = &GetBasketgroup($basketgroupid);
1067 Returns a reference to the hash containing all information about the basketgroup.
1071 sub GetBasketgroup {
1072 my $basketgroupid = shift;
1073 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
1074 my $dbh = C4::Context->dbh;
1075 my $result_set = $dbh->selectall_arrayref(
1076 'SELECT * FROM aqbasketgroups WHERE id=?',
1080 return $result_set->[0]; # id is unique
1083 #------------------------------------------------------------#
1085 =head3 GetBasketgroups
1087 $basketgroups = &GetBasketgroups($booksellerid);
1089 Returns a reference to the array of all the basketgroups of bookseller $booksellerid.
1093 sub GetBasketgroups {
1094 my $booksellerid = shift;
1095 die 'bookseller id is required to edit a basketgroup' unless $booksellerid;
1096 my $query = 'SELECT * FROM aqbasketgroups WHERE booksellerid=? ORDER BY id DESC';
1097 my $dbh = C4::Context->dbh;
1098 my $sth = $dbh->prepare($query);
1099 $sth->execute($booksellerid);
1100 return $sth->fetchall_arrayref({});
1103 #------------------------------------------------------------#
1105 =head2 FUNCTIONS ABOUT ORDERS
1109 @orders = &GetOrders( $basketno, { orderby => 'biblio.title', cancelled => 0|1 } );
1111 Looks up the pending (non-cancelled) orders with the given basket
1114 If cancelled is set, only cancelled orders will be returned.
1119 my ( $basketno, $params ) = @_;
1121 return () unless $basketno;
1123 my $orderby = $params->{orderby};
1124 my $cancelled = $params->{cancelled} || 0;
1126 my $dbh = C4::Context->dbh;
1128 SELECT biblio.*,biblioitems.*,
1132 $query .= $cancelled
1134 aqorders_transfers.ordernumber_to AS transferred_to,
1135 aqorders_transfers.timestamp AS transferred_to_timestamp
1138 aqorders_transfers.ordernumber_from AS transferred_from,
1139 aqorders_transfers.timestamp AS transferred_from_timestamp
1143 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1144 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1145 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
1147 $query .= $cancelled
1149 LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_from = aqorders.ordernumber
1152 LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_to = aqorders.ordernumber
1160 $orderby ||= q|biblioitems.publishercode, biblio.title|;
1162 AND datecancellationprinted IS NOT NULL
1167 q|aqorders.datecancellationprinted desc, aqorders.timestamp desc|;
1169 AND datecancellationprinted IS NULL
1173 $query .= " ORDER BY $orderby";
1175 $dbh->selectall_arrayref( $query, { Slice => {} }, $basketno );
1180 #------------------------------------------------------------#
1182 =head3 GetOrdersByBiblionumber
1184 @orders = &GetOrdersByBiblionumber($biblionumber);
1186 Looks up the orders with linked to a specific $biblionumber, including
1187 cancelled orders and received orders.
1190 C<@orders> is an array of references-to-hash, whose keys are the
1191 fields from the aqorders, biblio, and biblioitems tables in the Koha database.
1195 sub GetOrdersByBiblionumber {
1196 my $biblionumber = shift;
1197 return unless $biblionumber;
1198 my $dbh = C4::Context->dbh;
1200 SELECT biblio.*,biblioitems.*,
1204 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1205 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1206 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
1207 WHERE aqorders.biblionumber=?
1210 $dbh->selectall_arrayref( $query, { Slice => {} }, $biblionumber );
1211 return @{$result_set};
1215 #------------------------------------------------------------#
1219 $order = &GetOrder($ordernumber);
1221 Looks up an order by order number.
1223 Returns a reference-to-hash describing the order. The keys of
1224 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
1229 my ($ordernumber) = @_;
1230 return unless $ordernumber;
1232 my $dbh = C4::Context->dbh;
1233 my $query = qq{SELECT
1237 aqbasket.basketname,
1238 borrowers.branchcode,
1239 biblioitems.publicationyear,
1240 biblio.copyrightdate,
1241 biblioitems.editionstatement,
1245 biblioitems.publishercode,
1246 aqorders.rrp AS unitpricesupplier,
1247 aqorders.ecost AS unitpricelib,
1248 aqbudgets.budget_name AS budget,
1249 aqbooksellers.name AS supplier,
1250 aqbooksellers.id AS supplierid,
1251 biblioitems.publishercode AS publisher,
1252 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS calculateddeliverydate,
1253 DATE(aqbasket.closedate) AS orderdate,
1254 aqorders.quantity - COALESCE(aqorders.quantityreceived,0) AS quantity_to_receive,
1255 (aqorders.quantity - COALESCE(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1256 DATEDIFF(CURDATE( ),closedate) AS latesince
1257 FROM aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1258 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1259 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
1260 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
1261 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1262 WHERE aqorders.basketno = aqbasket.basketno
1265 $dbh->selectall_arrayref( $query, { Slice => {} }, $ordernumber );
1267 # result_set assumed to contain 1 match
1268 return $result_set->[0];
1273 &ModOrder(\%hashref);
1275 Modifies an existing order. Updates the order with order number
1276 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All
1277 other keys of the hash update the fields with the same name in the aqorders
1278 table of the Koha database.
1283 my $orderinfo = shift;
1285 die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '';
1287 my $dbh = C4::Context->dbh;
1290 # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
1291 $orderinfo->{uncertainprice}=1 if $orderinfo->{uncertainprice};
1293 # delete($orderinfo->{'branchcode'});
1294 # the hash contains a lot of entries not in aqorders, so get the columns ...
1295 my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1297 my $colnames = $sth->{NAME};
1298 #FIXME Be careful. If aqorders would have columns with diacritics,
1299 #you should need to decode what you get back from NAME.
1300 #See report 10110 and guided_reports.pl
1301 my $query = "UPDATE aqorders SET ";
1303 foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1304 # ... and skip hash entries that are not in the aqorders table
1305 # FIXME : probably not the best way to do it (would be better to have a correct hash)
1306 next unless grep { $_ eq $orderinfokey } @$colnames;
1307 $query .= "$orderinfokey=?, ";
1308 push(@params, $orderinfo->{$orderinfokey});
1311 $query .= "timestamp=NOW() WHERE ordernumber=?";
1312 push(@params, $orderinfo->{'ordernumber'} );
1313 $sth = $dbh->prepare($query);
1314 $sth->execute(@params);
1318 #------------------------------------------------------------#
1322 ModItemOrder($itemnumber, $ordernumber);
1324 Modifies the ordernumber of an item in aqorders_items.
1329 my ($itemnumber, $ordernumber) = @_;
1331 return unless ($itemnumber and $ordernumber);
1333 my $dbh = C4::Context->dbh;
1335 UPDATE aqorders_items
1337 WHERE itemnumber = ?
1339 my $sth = $dbh->prepare($query);
1340 return $sth->execute($ordernumber, $itemnumber);
1343 #------------------------------------------------------------#
1345 =head3 ModReceiveOrder
1347 my ( $date_received, $new_ordernumber ) = ModReceiveOrder(
1349 biblionumber => $biblionumber,
1351 quantityreceived => $quantityreceived,
1353 invoice => $invoice,
1354 budget_id => $budget_id,
1355 datereceived => $datereceived,
1356 received_itemnumbers => \@received_itemnumbers,
1360 Updates an order, to reflect the fact that it was received, at least
1363 If a partial order is received, splits the order into two.
1365 Updates the order with biblionumber C<$biblionumber> and ordernumber
1366 C<$order->{ordernumber}>.
1371 sub ModReceiveOrder {
1373 my $biblionumber = $params->{biblionumber};
1374 my $order = { %{ $params->{order} } }; # Copy the order, we don't want to modify it
1375 my $invoice = $params->{invoice};
1376 my $quantrec = $params->{quantityreceived};
1377 my $user = $params->{user};
1378 my $budget_id = $params->{budget_id};
1379 my $datereceived = $params->{datereceived};
1380 my $received_items = $params->{received_items};
1382 my $dbh = C4::Context->dbh;
1383 $datereceived = $datereceived ? dt_from_string( $datereceived ) : dt_from_string;
1384 $datereceived = $datereceived->ymd;
1386 $order->{invoice_unitprice} ||= $order->{unitprice};
1387 $order->{invoice_currency} ||= Koha::Acquisition::Currencies->get_active->currency;
1389 my $suggestionid = GetSuggestionFromBiblionumber( $biblionumber );
1390 if ($suggestionid) {
1391 ModSuggestion( {suggestionid=>$suggestionid,
1392 STATUS=>'AVAILABLE',
1393 biblionumber=> $biblionumber}
1397 my $result_set = $dbh->selectrow_arrayref(
1398 q{SELECT aqbasket.is_standing
1400 WHERE basketno=?},{ Slice => {} }, $order->{basketno});
1401 my $is_standing = $result_set->[0]; # we assume we have a unique basket
1403 my $new_ordernumber = $order->{ordernumber};
1404 if ( $is_standing || $order->{quantity} > $quantrec ) {
1405 # Split order line in two parts: the first is the original order line
1406 # without received items (the quantity is decreased),
1407 # the second part is a new order line with quantity=quantityrec
1408 # (entirely received)
1412 orderstatus = 'partial'|;
1413 $query .= q| WHERE ordernumber = ?|;
1414 my $sth = $dbh->prepare($query);
1417 ( $is_standing ? 1 : ($order->{quantity} - $quantrec) ),
1418 $order->{ordernumber}
1421 # Recalculate tax_value
1425 tax_value_on_ordering = quantity * | . get_rounding_sql(q|ecost_tax_excluded|) . q| * tax_rate_on_ordering,
1426 tax_value_on_receiving = quantity * | . get_rounding_sql(q|unitprice_tax_excluded|) . q| * tax_rate_on_receiving
1430 if ( not $order->{subscriptionid} && defined $order->{order_internalnote} )
1432 $query .= q|, order_internalnote = ?|;
1433 push @params, $order->{order_internalnote};
1436 $query .= q|, invoice_unitprice = ?, invoice_currency = ?|;
1437 push @params, $order->{invoice_unitprice}, $order->{invoice_currency};
1440 WHERE ordernumber = ?
1443 $dbh->do($query, undef, @params, $order->{ordernumber});
1445 delete $order->{ordernumber};
1446 $order->{budget_id} = ( $budget_id || $order->{budget_id} );
1447 $order->{quantity} = $quantrec;
1448 $order->{quantityreceived} = $quantrec;
1449 $order->{ecost_tax_excluded} //= 0;
1450 $order->{tax_rate_on_ordering} //= 0;
1451 $order->{unitprice_tax_excluded} //= 0;
1452 $order->{tax_rate_on_receiving} //= 0;
1453 $order->{tax_value_on_ordering} = $order->{quantity} * get_rounded_price($order->{ecost_tax_excluded}) * $order->{tax_rate_on_ordering};
1454 $order->{tax_value_on_receiving} = $order->{quantity} * get_rounded_price($order->{unitprice_tax_excluded}) * $order->{tax_rate_on_receiving};
1455 $order->{datereceived} = $datereceived;
1456 $order->{invoiceid} = $invoice->{invoiceid};
1457 $order->{orderstatus} = 'complete';
1458 $new_ordernumber = Koha::Acquisition::Order->new($order)->store->ordernumber; # TODO What if the store fails?
1460 if ($received_items) {
1461 foreach my $itemnumber (@$received_items) {
1462 ModItemOrder($itemnumber, $new_ordernumber);
1468 SET quantityreceived = ?,
1472 orderstatus = 'complete'
1476 , replacementprice = ?
1477 | if defined $order->{replacementprice};
1480 , unitprice = ?, unitprice_tax_included = ?, unitprice_tax_excluded = ?
1481 | if defined $order->{unitprice};
1484 ,tax_value_on_receiving = ?
1485 | if defined $order->{tax_value_on_receiving};
1488 ,tax_rate_on_receiving = ?
1489 | if defined $order->{tax_rate_on_receiving};
1492 , order_internalnote = ?
1493 | if defined $order->{order_internalnote};
1496 , invoice_unitprice = ?, invoice_currency = ?
1499 $query .= q| where biblionumber=? and ordernumber=?|;
1501 my $sth = $dbh->prepare( $query );
1502 my @params = ( $quantrec, $datereceived, $invoice->{invoiceid}, ( $budget_id ? $budget_id : $order->{budget_id} ) );
1504 if ( defined $order->{replacementprice} ) {
1505 push @params, $order->{replacementprice};
1508 if ( defined $order->{unitprice} ) {
1509 push @params, $order->{unitprice}, $order->{unitprice_tax_included}, $order->{unitprice_tax_excluded};
1512 if ( defined $order->{tax_value_on_receiving} ) {
1513 push @params, $order->{tax_value_on_receiving};
1516 if ( defined $order->{tax_rate_on_receiving} ) {
1517 push @params, $order->{tax_rate_on_receiving};
1520 if ( defined $order->{order_internalnote} ) {
1521 push @params, $order->{order_internalnote};
1524 push @params, $order->{invoice_unitprice}, $order->{invoice_currency};
1526 push @params, ( $biblionumber, $order->{ordernumber} );
1528 $sth->execute( @params );
1530 # All items have been received, sent a notification to users
1531 NotifyOrderUsers( $order->{ordernumber} );
1534 return ($datereceived, $new_ordernumber);
1537 =head3 CancelReceipt
1539 my $parent_ordernumber = CancelReceipt($ordernumber);
1541 Cancel an order line receipt and update the parent order line, as if no
1543 If items are created at receipt (AcqCreateItem = receiving) then delete
1549 my $ordernumber = shift;
1551 return unless $ordernumber;
1553 my $dbh = C4::Context->dbh;
1555 SELECT datereceived, parent_ordernumber, quantity
1557 WHERE ordernumber = ?
1559 my $sth = $dbh->prepare($query);
1560 $sth->execute($ordernumber);
1561 my $order = $sth->fetchrow_hashref;
1563 warn "CancelReceipt: order $ordernumber does not exist";
1566 unless($order->{'datereceived'}) {
1567 warn "CancelReceipt: order $ordernumber is not received";
1571 my $parent_ordernumber = $order->{'parent_ordernumber'};
1573 my $order_obj = Koha::Acquisition::Orders->find( $ordernumber ); # FIXME rewrite all this subroutine using this object
1574 my @itemnumbers = $order_obj->items->get_column('itemnumber');
1576 if($parent_ordernumber == $ordernumber || not $parent_ordernumber) {
1577 # The order line has no parent, just mark it as not received
1580 SET quantityreceived = ?,
1583 orderstatus = 'ordered'
1584 WHERE ordernumber = ?
1586 $sth = $dbh->prepare($query);
1587 $sth->execute(0, undef, undef, $ordernumber);
1588 _cancel_items_receipt( $order_obj );
1590 # The order line has a parent, increase parent quantity and delete
1592 unless ( $order_obj->basket->is_standing ) {
1594 SELECT quantity, datereceived
1596 WHERE ordernumber = ?
1598 $sth = $dbh->prepare($query);
1599 $sth->execute($parent_ordernumber);
1600 my $parent_order = $sth->fetchrow_hashref;
1601 unless($parent_order) {
1602 warn "Parent order $parent_ordernumber does not exist.";
1605 if($parent_order->{'datereceived'}) {
1606 warn "CancelReceipt: parent order is received.".
1607 " Can't cancel receipt.";
1613 orderstatus = 'ordered'
1614 WHERE ordernumber = ?
1616 $sth = $dbh->prepare($query);
1617 my $rv = $sth->execute(
1618 $order->{'quantity'} + $parent_order->{'quantity'},
1622 warn "Cannot update parent order line, so do not cancel".
1627 # Recalculate tax_value
1631 tax_value_on_ordering = quantity * | . get_rounding_sql(q|ecost_tax_excluded|) . q| * tax_rate_on_ordering,
1632 tax_value_on_receiving = quantity * | . get_rounding_sql(q|unitprice_tax_excluded|) . q| * tax_rate_on_receiving
1633 WHERE ordernumber = ?
1634 |, undef, $parent_ordernumber);
1637 _cancel_items_receipt( $order_obj, $parent_ordernumber );
1640 DELETE FROM aqorders
1641 WHERE ordernumber = ?
1643 $sth = $dbh->prepare($query);
1644 $sth->execute($ordernumber);
1648 if( $order_obj->basket->effective_create_items eq 'ordering' ) {
1649 my @affects = split q{\|}, C4::Context->preference("AcqItemSetSubfieldsWhenReceiptIsCancelled");
1651 for my $in ( @itemnumbers ) {
1652 my $item = Koha::Items->find( $in ); # FIXME We do not need that, we already have Koha::Items from $order_obj->items
1653 my $biblio = $item->biblio;
1654 my ( $itemfield ) = GetMarcFromKohaField( 'items.itemnumber' );
1655 my $item_marc = C4::Items::GetMarcItem( $biblio->biblionumber, $in );
1656 for my $affect ( @affects ) {
1657 my ( $sf, $v ) = split q{=}, $affect, 2;
1658 foreach ( $item_marc->field($itemfield) ) {
1659 $_->update( $sf => $v );
1662 C4::Items::ModItemFromMarc( $item_marc, $biblio->biblionumber, $in );
1667 return $parent_ordernumber;
1670 sub _cancel_items_receipt {
1671 my ( $order, $parent_ordernumber ) = @_;
1672 $parent_ordernumber ||= $order->ordernumber;
1674 my $items = $order->items;
1675 if ( $order->basket->effective_create_items eq 'receiving' ) {
1676 # Remove items that were created at receipt
1678 DELETE FROM items, aqorders_items
1679 USING items, aqorders_items
1680 WHERE items.itemnumber = ? AND aqorders_items.itemnumber = ?
1682 my $dbh = C4::Context->dbh;
1683 my $sth = $dbh->prepare($query);
1684 while ( my $item = $items->next ) {
1685 $sth->execute($item->itemnumber, $item->itemnumber);
1689 while ( my $item = $items->next ) {
1690 ModItemOrder($item->itemnumber, $parent_ordernumber);
1695 #------------------------------------------------------------#
1699 @results = &SearchOrders({
1700 ordernumber => $ordernumber,
1703 booksellerid => $booksellerid,
1704 basketno => $basketno,
1705 basketname => $basketname,
1706 basketgroupname => $basketgroupname,
1710 biblionumber => $biblionumber,
1711 budget_id => $budget_id
1714 Searches for orders filtered by criteria.
1716 C<$ordernumber> Finds matching orders or transferred orders by ordernumber.
1717 C<$search> Finds orders matching %$search% in title, author, or isbn.
1718 C<$owner> Finds order for the logged in user.
1719 C<$pending> Finds pending orders. Ignores completed and cancelled orders.
1720 C<$ordered> Finds orders to receive only (status 'ordered' or 'partial').
1723 C<@results> is an array of references-to-hash with the keys are fields
1724 from aqorders, biblio, biblioitems and aqbasket tables.
1729 my ( $params ) = @_;
1730 my $ordernumber = $params->{ordernumber};
1731 my $search = $params->{search};
1732 my $ean = $params->{ean};
1733 my $booksellerid = $params->{booksellerid};
1734 my $basketno = $params->{basketno};
1735 my $basketname = $params->{basketname};
1736 my $basketgroupname = $params->{basketgroupname};
1737 my $owner = $params->{owner};
1738 my $pending = $params->{pending};
1739 my $ordered = $params->{ordered};
1740 my $biblionumber = $params->{biblionumber};
1741 my $budget_id = $params->{budget_id};
1743 my $dbh = C4::Context->dbh;
1746 SELECT aqbasket.basketno,
1748 borrowers.firstname,
1751 biblioitems.biblioitemnumber,
1752 biblioitems.publishercode,
1753 biblioitems.publicationyear,
1754 aqbasket.authorisedby,
1755 aqbasket.booksellerid,
1757 aqbasket.creationdate,
1758 aqbasket.basketname,
1759 aqbasketgroups.id as basketgroupid,
1760 aqbasketgroups.name as basketgroupname,
1763 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1764 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
1765 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1766 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1767 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1770 # If we search on ordernumber, we retrieve the transferred order if a transfer has been done.
1772 LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_to = aqorders.ordernumber
1776 WHERE (datecancellationprinted is NULL)
1779 if ( $pending or $ordered ) {
1782 ( aqbasket.is_standing AND aqorders.orderstatus IN ( "new", "ordered", "partial" ) )
1784 ( quantity > quantityreceived OR quantityreceived is NULL )
1788 $query .= q{ AND aqorders.orderstatus IN ( "ordered", "partial" )};
1796 my $userenv = C4::Context->userenv;
1797 if ( C4::Context->preference("IndependentBranches") ) {
1798 unless ( C4::Context->IsSuperLibrarian() ) {
1801 borrowers.branchcode = ?
1802 OR borrowers.branchcode = ''
1805 push @args, $userenv->{branch};
1809 if ( $ordernumber ) {
1810 $query .= ' AND ( aqorders.ordernumber = ? OR aqorders_transfers.ordernumber_from = ? ) ';
1811 push @args, ( $ordernumber, $ordernumber );
1813 if ( $biblionumber ) {
1814 $query .= 'AND aqorders.biblionumber = ?';
1815 push @args, $biblionumber;
1818 $query .= ' AND (biblio.title LIKE ? OR biblio.author LIKE ? OR biblioitems.isbn LIKE ?)';
1819 push @args, ("%$search%","%$search%","%$search%");
1822 $query .= ' AND biblioitems.ean = ?';
1825 if ( $booksellerid ) {
1826 $query .= 'AND aqbasket.booksellerid = ?';
1827 push @args, $booksellerid;
1830 $query .= 'AND aqbasket.basketno = ?';
1831 push @args, $basketno;
1834 $query .= 'AND aqbasket.basketname LIKE ?';
1835 push @args, "%$basketname%";
1837 if( $basketgroupname ) {
1838 $query .= ' AND aqbasketgroups.name LIKE ?';
1839 push @args, "%$basketgroupname%";
1843 $query .= ' AND aqbasket.authorisedby=? ';
1844 push @args, $userenv->{'number'};
1848 $query .= ' AND aqorders.budget_id = ?';
1849 push @args, $budget_id;
1852 $query .= ' ORDER BY aqbasket.basketno';
1854 my $sth = $dbh->prepare($query);
1855 $sth->execute(@args);
1856 return $sth->fetchall_arrayref({});
1859 #------------------------------------------------------------#
1861 =head3 TransferOrder
1863 my $newordernumber = TransferOrder($ordernumber, $basketno);
1865 Transfer an order line to a basket.
1866 Mark $ordernumber as cancelled with an internal note 'Cancelled and transferred
1867 to BOOKSELLER on DATE' and create new order with internal note
1868 'Transferred from BOOKSELLER on DATE'.
1869 Move all attached items to the new order.
1870 Received orders cannot be transferred.
1871 Return the ordernumber of created order.
1876 my ($ordernumber, $basketno) = @_;
1878 return unless ($ordernumber and $basketno);
1880 my $order = Koha::Acquisition::Orders->find( $ordernumber ) or return;
1881 return if $order->datereceived;
1883 $order = $order->unblessed;
1885 my $basket = GetBasket($basketno);
1886 return unless $basket;
1888 my $dbh = C4::Context->dbh;
1889 my ($query, $sth, $rv);
1893 SET datecancellationprinted = CAST(NOW() AS date), orderstatus = ?
1894 WHERE ordernumber = ?
1896 $sth = $dbh->prepare($query);
1897 $rv = $sth->execute('cancelled', $ordernumber);
1899 delete $order->{'ordernumber'};
1900 delete $order->{parent_ordernumber};
1901 $order->{'basketno'} = $basketno;
1903 my $newordernumber = Koha::Acquisition::Order->new($order)->store->ordernumber;
1906 UPDATE aqorders_items
1908 WHERE ordernumber = ?
1910 $sth = $dbh->prepare($query);
1911 $sth->execute($newordernumber, $ordernumber);
1914 INSERT INTO aqorders_transfers (ordernumber_from, ordernumber_to)
1917 $sth = $dbh->prepare($query);
1918 $sth->execute($ordernumber, $newordernumber);
1920 return $newordernumber;
1923 =head3 get_rounding_sql
1925 $rounding_sql = get_rounding_sql($column_name);
1927 returns the correct SQL routine based on OrderPriceRounding system preference.
1931 sub get_rounding_sql {
1932 my ( $round_string ) = @_;
1933 my $rounding_pref = C4::Context->preference('OrderPriceRounding') // q{};
1934 if ( $rounding_pref eq "nearest_cent" ) {
1935 return "CAST($round_string*100 AS SIGNED)/100";
1937 return $round_string;
1940 =head3 get_rounded_price
1942 $rounded_price = get_rounded_price( $price );
1944 returns a price rounded as specified in OrderPriceRounding system preference.
1948 sub get_rounded_price {
1950 my $rounding_pref = C4::Context->preference('OrderPriceRounding') // q{};
1951 if( $rounding_pref eq 'nearest_cent' ) {
1952 return Koha::Number::Price->new( $price )->round();
1958 =head2 FUNCTIONS ABOUT PARCELS
1962 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1964 get a lists of parcels.
1971 is the bookseller this function has to get parcels.
1974 To know on what criteria the results list has to be ordered.
1977 is the booksellerinvoicenumber.
1979 =item $datefrom & $dateto
1980 to know on what date this function has to filter its search.
1985 a pointer on a hash list containing parcel informations as such :
1991 =item Last operation
1993 =item Number of biblio
1995 =item Number of items
2002 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
2003 my $dbh = C4::Context->dbh;
2004 my @query_params = ();
2006 SELECT aqinvoices.invoicenumber,
2007 datereceived,purchaseordernumber,
2008 count(DISTINCT biblionumber) AS biblio,
2009 sum(quantity) AS itemsexpected,
2010 sum(quantityreceived) AS itemsreceived
2011 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
2012 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
2013 WHERE aqbasket.booksellerid = ? and datereceived IS NOT NULL
2015 push @query_params, $bookseller;
2017 if ( defined $code ) {
2018 $strsth .= ' and aqinvoices.invoicenumber like ? ';
2019 # add a % to the end of the code to allow stemming.
2020 push @query_params, "$code%";
2023 if ( defined $datefrom ) {
2024 $strsth .= ' and datereceived >= ? ';
2025 push @query_params, $datefrom;
2028 if ( defined $dateto ) {
2029 $strsth .= 'and datereceived <= ? ';
2030 push @query_params, $dateto;
2033 $strsth .= "group by aqinvoices.invoicenumber,datereceived ";
2035 # can't use a placeholder to place this column name.
2036 # but, we could probably be checking to make sure it is a column that will be fetched.
2037 $strsth .= "order by $order " if ($order);
2039 my $sth = $dbh->prepare($strsth);
2041 $sth->execute( @query_params );
2042 my $results = $sth->fetchall_arrayref({});
2046 #------------------------------------------------------------#
2050 \@order_loop = GetHistory( %params );
2052 Retreives some acquisition history information
2062 basket - search both basket name and number
2063 booksellerinvoicenumber
2066 orderstatus (note that orderstatus '' will retrieve orders
2067 of any status except cancelled)
2071 get_canceled_order (if set to a true value, cancelled orders will
2075 $order_loop is a list of hashrefs that each look like this:
2077 'author' => 'Twain, Mark',
2079 'biblionumber' => '215',
2081 'creationdate' => 'MM/DD/YYYY',
2082 'datereceived' => undef,
2085 'invoicenumber' => undef,
2087 'ordernumber' => '1',
2089 'quantityreceived' => undef,
2090 'title' => 'The Adventures of Huckleberry Finn',
2091 'managing_library' => 'CPL'
2092 'is_standing' => '1'
2098 # don't run the query if there are no parameters (list would be too long for sure !)
2099 croak "No search params" unless @_;
2101 my $title = $params{title};
2102 my $author = $params{author};
2103 my $isbn = $params{isbn};
2104 my $issn = $params{issn};
2105 my $ean = $params{ean};
2106 my $name = $params{name};
2107 my $internalnote = $params{internalnote};
2108 my $vendornote = $params{vendornote};
2109 my $from_placed_on = $params{from_placed_on};
2110 my $to_placed_on = $params{to_placed_on};
2111 my $basket = $params{basket};
2112 my $booksellerinvoicenumber = $params{booksellerinvoicenumber};
2113 my $basketgroupname = $params{basketgroupname};
2114 my $budget = $params{budget};
2115 my $orderstatus = $params{orderstatus};
2116 my $is_standing = $params{is_standing};
2117 my $biblionumber = $params{biblionumber};
2118 my $get_canceled_order = $params{get_canceled_order} || 0;
2119 my $ordernumber = $params{ordernumber};
2120 my $search_children_too = $params{search_children_too} || 0;
2121 my $created_by = $params{created_by} || [];
2122 my $managing_library = $params{managing_library};
2123 my $ordernumbers = $params{ordernumbers} || [];
2124 my $additional_fields = $params{additional_fields} // [];
2127 my $total_qtyreceived = 0;
2128 my $total_price = 0;
2130 #get variation of isbn
2134 if ( C4::Context->preference("SearchWithISBNVariations") ){
2135 @isbns = C4::Koha::GetVariationsOfISBN( $isbn );
2136 foreach my $isb (@isbns){
2137 push @isbn_params, '?';
2142 push @isbn_params, '?';
2146 #get variation of issn
2150 if ( C4::Context->preference("SearchWithISSNVariations") ){
2151 @issns = C4::Koha::GetVariationsOfISSN( $issn );
2152 push @issn_params, ('?') x @issns;
2156 push @issn_params, '?';
2160 my $dbh = C4::Context->dbh;
2163 COALESCE(biblio.title, deletedbiblio.title) AS title,
2164 COALESCE(biblio.author, deletedbiblio.author) AS author,
2165 COALESCE(biblioitems.isbn, deletedbiblioitems.isbn) AS isbn,
2166 COALESCE(biblioitems.ean, deletedbiblioitems.ean) AS ean,
2168 aqbasket.basketname,
2169 aqbasket.basketgroupid,
2170 aqbasket.authorisedby,
2171 aqbasket.is_standing,
2172 concat( borrowers.firstname,' ',borrowers.surname) AS authorisedbyname,
2173 branch as managing_library,
2174 aqbasketgroups.name as groupname,
2176 aqbasket.creationdate,
2177 aqorders.datereceived,
2179 aqorders.quantityreceived,
2181 aqorders.ordernumber,
2183 aqinvoices.invoicenumber,
2184 aqbooksellers.id as id,
2185 aqorders.biblionumber,
2186 aqorders.orderstatus,
2187 aqorders.parent_ordernumber,
2188 aqorders.order_internalnote,
2189 aqorders.order_vendornote,
2190 aqbudgets.budget_name
2192 $query .= ", aqbudgets.budget_id AS budget" if defined $budget;
2195 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
2196 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
2197 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
2198 LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
2199 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
2200 LEFT JOIN aqbudgets ON aqorders.budget_id=aqbudgets.budget_id
2201 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
2202 LEFT JOIN deletedbiblio ON deletedbiblio.biblionumber=aqorders.biblionumber
2203 LEFT JOIN deletedbiblioitems ON deletedbiblioitems.biblionumber=aqorders.biblionumber
2204 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
2207 $query .= " WHERE 1 ";
2209 unless ($get_canceled_order or (defined $orderstatus and $orderstatus eq 'cancelled')) {
2210 $query .= " AND datecancellationprinted IS NULL ";
2213 my @query_params = ();
2215 if ( $biblionumber ) {
2216 $query .= " AND biblio.biblionumber = ?";
2217 push @query_params, $biblionumber;
2221 $query .= " AND biblio.title LIKE ? ";
2222 $title =~ s/\s+/%/g;
2223 push @query_params, "%$title%";
2227 $query .= " AND biblio.author LIKE ? ";
2228 push @query_params, "%$author%";
2232 $query .= " AND ( biblioitems.isbn LIKE " . join (" OR biblioitems.isbn LIKE ", @isbn_params ) . ")";
2233 foreach my $isb (@isbns){
2234 push @query_params, "%$isb%";
2239 $query .= " AND ( biblioitems.issn LIKE " . join (" OR biblioitems.issn LIKE ", @issn_params ) . ")";
2240 foreach my $isn (@issns){
2241 push @query_params, "%$isn%";
2246 $query .= " AND biblioitems.ean = ? ";
2247 push @query_params, "$ean";
2250 $query .= " AND aqbooksellers.name LIKE ? ";
2251 push @query_params, "%$name%";
2255 $query .= " AND aqbudgets.budget_id = ? ";
2256 push @query_params, "$budget";
2259 if ( $from_placed_on ) {
2260 $query .= " AND creationdate >= ? ";
2261 push @query_params, $from_placed_on;
2264 if ( $to_placed_on ) {
2265 $query .= " AND creationdate <= ? ";
2266 push @query_params, $to_placed_on;
2269 if ( defined $orderstatus and $orderstatus ne '') {
2270 $query .= " AND aqorders.orderstatus = ? ";
2271 push @query_params, "$orderstatus";
2274 if ( $is_standing ) {
2275 $query .= " AND is_standing = ? ";
2276 push @query_params, $is_standing;
2280 if ($basket =~ m/^\d+$/) {
2281 $query .= " AND aqorders.basketno = ? ";
2282 push @query_params, $basket;
2284 $query .= " AND aqbasket.basketname LIKE ? ";
2285 push @query_params, "%$basket%";
2289 if ( $internalnote ) {
2290 $query .= " AND aqorders.order_internalnote LIKE ? ";
2291 push @query_params, "%$internalnote%";
2294 if ( $vendornote ) {
2295 $query .= " AND aqorders.order_vendornote LIKE ?";
2296 push @query_params, "%$vendornote%";
2299 if ($booksellerinvoicenumber) {
2300 $query .= " AND aqinvoices.invoicenumber LIKE ? ";
2301 push @query_params, "%$booksellerinvoicenumber%";
2304 if ($basketgroupname) {
2305 $query .= " AND aqbasketgroups.name LIKE ? ";
2306 push @query_params, "%$basketgroupname%";
2310 $query .= " AND (aqorders.ordernumber = ? ";
2311 push @query_params, $ordernumber;
2312 if ($search_children_too) {
2313 $query .= " OR aqorders.parent_ordernumber = ? ";
2314 push @query_params, $ordernumber;
2319 if ( @$created_by ) {
2320 $query .= ' AND aqbasket.authorisedby IN ( ' . join( ',', ('?') x @$created_by ) . ')';
2321 push @query_params, @$created_by;
2324 if ( $managing_library ) {
2325 $query .= " AND aqbasket.branch = ? ";
2326 push @query_params, $managing_library;
2329 if ( @$ordernumbers ) {
2330 $query .= ' AND (aqorders.ordernumber IN ( ' . join (',', ('?') x @$ordernumbers ) . '))';
2331 push @query_params, @$ordernumbers;
2333 if ( @$additional_fields ) {
2334 my @baskets = Koha::Acquisition::Baskets->filter_by_additional_fields($additional_fields)->as_list;
2336 return [] unless @baskets;
2338 # No parameterization because record IDs come directly from DB
2339 $query .= ' AND aqbasket.basketno IN ( ' . join( ',', map { $_->basketno } @baskets ) . ' )';
2342 if ( C4::Context->preference("IndependentBranches") ) {
2343 unless ( C4::Context->IsSuperLibrarian() ) {
2344 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
2345 push @query_params, C4::Context->userenv->{branch};
2348 $query .= " ORDER BY id";
2350 return $dbh->selectall_arrayref( $query, { Slice => {} }, @query_params );
2353 =head2 GetRecentAcqui
2355 $results = GetRecentAcqui($days);
2357 C<$results> is a ref to a table which contains hashref
2361 sub GetRecentAcqui {
2363 my $dbh = C4::Context->dbh;
2367 ORDER BY timestamp DESC
2370 my $sth = $dbh->prepare($query);
2372 my $results = $sth->fetchall_arrayref({});
2376 #------------------------------------------------------------#
2380 &AddClaim($ordernumber);
2382 Add a claim for an order
2387 my ($ordernumber) = @_;
2388 my $dbh = C4::Context->dbh;
2391 claims_count = claims_count + 1,
2392 claimed_date = CURDATE()
2393 WHERE ordernumber = ?
2395 my $sth = $dbh->prepare($query);
2396 $sth->execute($ordernumber);
2401 my @invoices = GetInvoices(
2402 invoicenumber => $invoicenumber,
2403 supplierid => $supplierid,
2404 suppliername => $suppliername,
2405 shipmentdatefrom => $shipmentdatefrom, # ISO format
2406 shipmentdateto => $shipmentdateto, # ISO format
2407 billingdatefrom => $billingdatefrom, # ISO format
2408 billingdateto => $billingdateto, # ISO format
2409 isbneanissn => $isbn_or_ean_or_issn,
2412 publisher => $publisher,
2413 publicationyear => $publicationyear,
2414 branchcode => $branchcode,
2415 order_by => $order_by
2418 Return a list of invoices that match all given criteria.
2420 $order_by is "column_name (asc|desc)", where column_name is any of
2421 'invoicenumber', 'booksellerid', 'shipmentdate', 'billingdate', 'closedate',
2422 'shipmentcost', 'shipmentcost_budgetid'.
2424 asc is the default if omitted
2431 my $additional_fields = $args{additional_fields} // [];
2432 my $matching_invoice_ids_for_additional_fields = [];
2433 if ( @$additional_fields ) {
2434 my @invoices = Koha::Acquisition::Invoices->filter_by_additional_fields($additional_fields)->as_list;
2436 return () unless @invoices;
2438 $matching_invoice_ids_for_additional_fields = [ map {
2443 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2444 closedate shipmentcost shipmentcost_budgetid);
2446 my $dbh = C4::Context->dbh;
2448 SELECT aqinvoices.invoiceid, aqinvoices.invoicenumber, aqinvoices.booksellerid, aqinvoices.shipmentdate, aqinvoices.billingdate, aqinvoices.closedate, aqinvoices.shipmentcost, aqinvoices.shipmentcost_budgetid, aqinvoices.message_id,
2449 aqbooksellers.name AS suppliername,
2452 aqorders.datereceived IS NOT NULL,
2453 aqorders.biblionumber,
2456 ) AS receivedbiblios,
2459 aqorders.subscriptionid IS NOT NULL,
2460 aqorders.subscriptionid,
2463 ) AS is_linked_to_subscriptions,
2464 SUM(aqorders.quantityreceived) AS receiveditems
2466 LEFT JOIN aqbooksellers ON aqbooksellers.id = aqinvoices.booksellerid
2467 LEFT JOIN aqorders ON aqorders.invoiceid = aqinvoices.invoiceid
2468 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
2469 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
2470 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2471 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
2472 LEFT JOIN subscription ON biblio.biblionumber = subscription.biblionumber
2477 if($args{supplierid}) {
2478 push @bind_strs, " aqinvoices.booksellerid = ? ";
2479 push @bind_args, $args{supplierid};
2481 if($args{invoicenumber}) {
2482 push @bind_strs, " aqinvoices.invoicenumber LIKE ? ";
2483 push @bind_args, "%$args{invoicenumber}%";
2485 if($args{suppliername}) {
2486 push @bind_strs, " aqbooksellers.name LIKE ? ";
2487 push @bind_args, "%$args{suppliername}%";
2489 if($args{shipmentdatefrom}) {
2490 push @bind_strs, " aqinvoices.shipmentdate >= ? ";
2491 push @bind_args, $args{shipmentdatefrom};
2493 if($args{shipmentdateto}) {
2494 push @bind_strs, " aqinvoices.shipmentdate <= ? ";
2495 push @bind_args, $args{shipmentdateto};
2497 if($args{billingdatefrom}) {
2498 push @bind_strs, " aqinvoices.billingdate >= ? ";
2499 push @bind_args, $args{billingdatefrom};
2501 if($args{billingdateto}) {
2502 push @bind_strs, " aqinvoices.billingdate <= ? ";
2503 push @bind_args, $args{billingdateto};
2505 if($args{isbneanissn}) {
2506 push @bind_strs, " (biblioitems.isbn LIKE CONCAT('%', ?, '%') OR biblioitems.ean LIKE CONCAT('%', ?, '%') OR biblioitems.issn LIKE CONCAT('%', ?, '%') ) ";
2507 push @bind_args, $args{isbneanissn}, $args{isbneanissn}, $args{isbneanissn};
2510 push @bind_strs, " biblio.title LIKE CONCAT('%', ?, '%') ";
2511 push @bind_args, $args{title};
2514 push @bind_strs, " biblio.author LIKE CONCAT('%', ?, '%') ";
2515 push @bind_args, $args{author};
2517 if($args{publisher}) {
2518 push @bind_strs, " biblioitems.publishercode LIKE CONCAT('%', ?, '%') ";
2519 push @bind_args, $args{publisher};
2521 if($args{publicationyear}) {
2522 push @bind_strs, " ((biblioitems.publicationyear LIKE CONCAT('%', ?, '%')) OR (biblio.copyrightdate LIKE CONCAT('%', ?, '%'))) ";
2523 push @bind_args, $args{publicationyear}, $args{publicationyear};
2525 if($args{branchcode}) {
2526 push @bind_strs, " borrowers.branchcode = ? ";
2527 push @bind_args, $args{branchcode};
2529 if($args{message_id}) {
2530 push @bind_strs, " aqinvoices.message_id = ? ";
2531 push @bind_args, $args{message_id};
2534 $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
2536 # Handle additional fields filtering
2537 if ( @$additional_fields ) {
2538 my $operator = ' WHERE';
2539 if ( @bind_strs ) { # there's a WHERE already
2542 $query .= "$operator aqinvoices.invoiceid IN ("
2543 . join( ', ', @$matching_invoice_ids_for_additional_fields )
2547 $query .= " GROUP BY aqinvoices.invoiceid, aqinvoices.invoicenumber, aqinvoices.booksellerid, aqinvoices.shipmentdate, aqinvoices.billingdate, aqinvoices.closedate, aqinvoices.shipmentcost, aqinvoices.shipmentcost_budgetid, aqinvoices.message_id, aqbooksellers.name";
2549 if($args{order_by}) {
2550 my ($column, $direction) = split / /, $args{order_by};
2551 if(grep { $_ eq $column } @columns) {
2552 $direction ||= 'ASC';
2553 $query .= " ORDER BY $column $direction";
2557 my $sth = $dbh->prepare($query);
2558 $sth->execute(@bind_args);
2560 my $results = $sth->fetchall_arrayref({});
2566 my $invoice = GetInvoice($invoiceid);
2568 Get informations about invoice with given $invoiceid
2570 Return a hash filled with aqinvoices.* fields
2575 my ($invoiceid) = @_;
2578 return unless $invoiceid;
2580 my $dbh = C4::Context->dbh;
2586 my $sth = $dbh->prepare($query);
2587 $sth->execute($invoiceid);
2589 $invoice = $sth->fetchrow_hashref;
2593 =head3 GetInvoiceDetails
2595 my $invoice = GetInvoiceDetails($invoiceid)
2597 Return informations about an invoice + the list of related order lines
2599 Orders informations are in $invoice->{orders} (array ref)
2603 sub GetInvoiceDetails {
2604 my ($invoiceid) = @_;
2606 if ( !defined $invoiceid ) {
2607 carp 'GetInvoiceDetails called without an invoiceid';
2611 my $dbh = C4::Context->dbh;
2613 SELECT aqinvoices.*, aqbooksellers.name AS suppliername
2615 LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
2618 my $sth = $dbh->prepare($query);
2619 $sth->execute($invoiceid);
2621 my $invoice = $sth->fetchrow_hashref;
2626 biblio.copyrightdate,
2628 biblioitems.publishercode,
2629 biblioitems.publicationyear,
2630 aqbasket.basketname,
2631 aqbasketgroups.id AS basketgroupid,
2632 aqbasketgroups.name AS basketgroupname
2634 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
2635 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
2636 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2637 LEFT JOIN biblioitems ON aqorders.biblionumber = biblioitems.biblionumber
2640 $sth = $dbh->prepare($query);
2641 $sth->execute($invoiceid);
2642 $invoice->{orders} = $sth->fetchall_arrayref({});
2643 $invoice->{orders} ||= []; # force an empty arrayref if fetchall_arrayref fails
2650 my $invoiceid = AddInvoice(
2651 invoicenumber => $invoicenumber,
2652 booksellerid => $booksellerid,
2653 shipmentdate => $shipmentdate,
2654 billingdate => $billingdate,
2655 closedate => $closedate,
2656 shipmentcost => $shipmentcost,
2657 shipmentcost_budgetid => $shipmentcost_budgetid
2660 Create a new invoice and return its id or undef if it fails.
2667 return unless(%invoice and $invoice{invoicenumber});
2669 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2670 closedate shipmentcost shipmentcost_budgetid message_id);
2674 foreach my $key (keys %invoice) {
2675 if(0 < grep { $_ eq $key } @columns) {
2676 push @set_strs, "$key = ?";
2677 push @set_args, ($invoice{$key} || undef);
2683 my $dbh = C4::Context->dbh;
2684 my $query = "INSERT INTO aqinvoices SET ";
2685 $query .= join (",", @set_strs);
2686 my $sth = $dbh->prepare($query);
2687 $rv = $sth->execute(@set_args);
2689 $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
2698 invoiceid => $invoiceid, # Mandatory
2699 invoicenumber => $invoicenumber,
2700 booksellerid => $booksellerid,
2701 shipmentdate => $shipmentdate,
2702 billingdate => $billingdate,
2703 closedate => $closedate,
2704 shipmentcost => $shipmentcost,
2705 shipmentcost_budgetid => $shipmentcost_budgetid
2708 Modify an invoice, invoiceid is mandatory.
2710 Return undef if it fails.
2717 return unless(%invoice and $invoice{invoiceid});
2719 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2720 closedate shipmentcost shipmentcost_budgetid);
2724 foreach my $key (keys %invoice) {
2725 if(0 < grep { $_ eq $key } @columns) {
2726 push @set_strs, "$key = ?";
2727 push @set_args, ($invoice{$key} || undef);
2731 my $dbh = C4::Context->dbh;
2732 my $query = "UPDATE aqinvoices SET ";
2733 $query .= join(",", @set_strs);
2734 $query .= " WHERE invoiceid = ?";
2736 my $sth = $dbh->prepare($query);
2737 $sth->execute(@set_args, $invoice{invoiceid});
2742 CloseInvoice($invoiceid);
2746 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
2751 my ($invoiceid) = @_;
2753 return unless $invoiceid;
2755 my $dbh = C4::Context->dbh;
2758 SET closedate = CAST(NOW() AS DATE)
2761 my $sth = $dbh->prepare($query);
2762 $sth->execute($invoiceid);
2765 =head3 ReopenInvoice
2767 ReopenInvoice($invoiceid);
2771 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => $closedate );
2776 my ($invoiceid) = @_;
2778 return unless $invoiceid;
2780 my $dbh = C4::Context->dbh;
2783 SET closedate = NULL
2786 my $sth = $dbh->prepare($query);
2787 $sth->execute($invoiceid);
2792 DelInvoice($invoiceid);
2794 Delete an invoice if there are no items attached to it.
2799 my ($invoiceid) = @_;
2801 return unless $invoiceid;
2803 my $dbh = C4::Context->dbh;
2809 my $sth = $dbh->prepare($query);
2810 $sth->execute($invoiceid);
2811 my $res = $sth->fetchrow_arrayref;
2812 if ( $res && $res->[0] == 0 ) {
2814 DELETE FROM aqinvoices
2817 my $sth = $dbh->prepare($query);
2818 return ( $sth->execute($invoiceid) > 0 );
2823 =head3 MergeInvoices
2825 MergeInvoices($invoiceid, \@sourceids);
2827 Merge the invoices identified by the IDs in \@sourceids into
2828 the invoice identified by $invoiceid.
2833 my ($invoiceid, $sourceids) = @_;
2835 return unless $invoiceid;
2836 foreach my $sourceid (@$sourceids) {
2837 next if $sourceid == $invoiceid;
2838 my $source = GetInvoiceDetails($sourceid);
2839 foreach my $order (@{$source->{'orders'}}) {
2840 $order->{'invoiceid'} = $invoiceid;
2843 DelInvoice($source->{'invoiceid'});
2848 =head3 GetBiblioCountByBasketno
2850 $biblio_count = &GetBiblioCountByBasketno($basketno);
2852 Looks up the biblio's count that has basketno value $basketno
2858 sub GetBiblioCountByBasketno {
2859 my ($basketno) = @_;
2860 my $dbh = C4::Context->dbh;
2862 SELECT COUNT( DISTINCT( biblionumber ) )
2865 AND datecancellationprinted IS NULL
2868 my $sth = $dbh->prepare($query);
2869 $sth->execute($basketno);
2870 return $sth->fetchrow;
2873 =head3 GetOrderUsers
2875 $order_users_ids = &GetOrderUsers($ordernumber);
2877 Returns a list of all borrowernumbers that are in order users list
2882 my ($ordernumber) = @_;
2884 return unless $ordernumber;
2887 SELECT borrowernumber
2889 WHERE ordernumber = ?
2891 my $dbh = C4::Context->dbh;
2892 my $sth = $dbh->prepare($query);
2893 $sth->execute($ordernumber);
2894 my $results = $sth->fetchall_arrayref( {} );
2896 my @borrowernumbers;
2897 foreach (@$results) {
2898 push @borrowernumbers, $_->{'borrowernumber'};
2901 return @borrowernumbers;
2904 =head3 ModOrderUsers
2906 my @order_users_ids = (1, 2, 3);
2907 &ModOrderUsers($ordernumber, @basketusers_ids);
2909 Delete all users from order users list, and add users in C<@order_users_ids>
2915 my ( $ordernumber, @order_users_ids ) = @_;
2917 return unless $ordernumber;
2919 my $dbh = C4::Context->dbh;
2921 DELETE FROM aqorder_users
2922 WHERE ordernumber = ?
2924 my $sth = $dbh->prepare($query);
2925 $sth->execute($ordernumber);
2928 INSERT INTO aqorder_users (ordernumber, borrowernumber)
2931 $sth = $dbh->prepare($query);
2932 foreach my $order_user_id (@order_users_ids) {
2933 $sth->execute( $ordernumber, $order_user_id );
2937 sub NotifyOrderUsers {
2938 my ($ordernumber) = @_;
2940 my @borrowernumbers = GetOrderUsers($ordernumber);
2941 return unless @borrowernumbers;
2943 my $order = GetOrder( $ordernumber );
2944 for my $borrowernumber (@borrowernumbers) {
2945 my $patron = Koha::Patrons->find( $borrowernumber );
2946 my $library = $patron->library->unblessed;
2947 my $biblio = Koha::Biblios->find( $order->{biblionumber} )->unblessed;
2948 my $letter = C4::Letters::GetPreparedLetter(
2949 module => 'acquisition',
2950 letter_code => 'ACQ_NOTIF_ON_RECEIV',
2951 branchcode => $library->{branchcode},
2952 lang => $patron->lang,
2954 'branches' => $library,
2955 'borrowers' => $patron->unblessed,
2956 'biblio' => $biblio,
2957 'aqorders' => $order,
2961 C4::Letters::EnqueueLetter(
2964 borrowernumber => $borrowernumber,
2965 LibraryName => C4::Context->preference("LibraryName"),
2966 message_transport_type => 'email',
2968 ) or warn "can't enqueue letter $letter";
2973 =head3 FillWithDefaultValues
2975 FillWithDefaultValues( $marc_record, $params );
2977 This will update the record with default value defined in the ACQ framework.
2978 For all existing fields, if a default value exists and there are no subfield, it will be created.
2979 If the field does not exist, it will be created too.
2981 If the parameter only_mandatory => 1 is passed via $params, only the mandatory
2982 defaults are being applied to the record.
2986 sub FillWithDefaultValues {
2987 my ( $record, $params ) = @_;
2988 my $mandatory = $params->{only_mandatory};
2989 my $tagslib = C4::Biblio::GetMarcStructure( 1, 'ACQ', { unsafe => 1 } );
2992 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
2993 for my $tag ( sort keys %$tagslib ) {
2995 next if $tag == $itemfield;
2996 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2997 next if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
2998 next if $mandatory && !$tagslib->{$tag}{$subfield}{mandatory};
2999 my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
3000 if ( defined $defaultvalue and $defaultvalue ne '' ) {
3001 my @fields = $record->field($tag);
3003 for my $field (@fields) {
3004 if ( $field->is_control_field ) {
3005 $field->update($defaultvalue) if not defined $field->data;
3007 elsif ( not defined $field->subfield($subfield) ) {
3008 $field->add_subfields(
3009 $subfield => $defaultvalue );
3014 if ( $tag < 10 ) { # is_control_field
3015 $record->insert_fields_ordered(
3022 $record->insert_fields_ordered(
3024 $tag, '', '', $subfield => $defaultvalue
3040 Koha Development Team <http://koha-community.org/>