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>.
29 use C4
::Templates
qw(gettemplate);
30 use Koha
::DateUtils
qw( dt_from_string output_pref );
31 use Koha
::Acquisition
::Order
;
32 use Koha
::Acquisition
::Booksellers
;
34 use Koha
::Number
::Price
;
44 use vars
qw(@ISA @EXPORT);
50 &GetBasket &NewBasket &CloseBasket &ReopenBasket &DelBasket &ModBasket
51 &GetBasketAsCSV &GetBasketGroupAsCSV
52 &GetBasketsByBookseller &GetBasketsByBasketgroup
53 &GetBasketsInfosByBookseller
55 &GetBasketUsers &ModBasketUsers
60 &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
61 &GetBasketgroups &ReOpenBasketgroup
63 &DelOrder &ModOrder &GetOrder &GetOrders &GetOrdersByBiblionumber
64 &GetLateOrders &GetOrderFromItemnumber
65 &SearchOrders &GetHistory &GetRecentAcqui
66 &ModReceiveOrder &CancelReceipt
68 &GetLastOrderNotReceivedFromSubscriptionid &GetLastOrderReceivedFromSubscriptionid
83 &GetItemnumbersFromOrder
86 &GetBiblioCountByBasketno
92 &FillWithDefaultValues
100 sub GetOrderFromItemnumber
{
101 my ($itemnumber) = @_;
102 my $dbh = C4
::Context
->dbh;
105 SELECT
* from aqorders LEFT JOIN aqorders_items
106 ON
( aqorders
.ordernumber
= aqorders_items
.ordernumber
)
107 WHERE itemnumber
= ?
|;
109 my $sth = $dbh->prepare($query);
113 $sth->execute($itemnumber);
115 my $order = $sth->fetchrow_hashref;
120 # Returns the itemnumber(s) associated with the ordernumber given in parameter
121 sub GetItemnumbersFromOrder
{
122 my ($ordernumber) = @_;
123 my $dbh = C4
::Context
->dbh;
124 my $query = "SELECT itemnumber FROM aqorders_items WHERE ordernumber=?";
125 my $sth = $dbh->prepare($query);
126 $sth->execute($ordernumber);
129 while (my $order = $sth->fetchrow_hashref) {
130 push @tab, $order->{'itemnumber'};
144 C4::Acquisition - Koha functions for dealing with orders and acquisitions
152 The functions in this module deal with acquisitions, managing book
153 orders, basket and parcels.
157 =head2 FUNCTIONS ABOUT BASKETS
161 $aqbasket = &GetBasket($basketnumber);
163 get all basket informations in aqbasket for a given basket
165 B<returns:> informations for a given basket returned as a hashref.
171 my $dbh = C4
::Context
->dbh;
174 concat( b.firstname,' ',b.surname) AS authorisedbyname
176 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
179 my $sth=$dbh->prepare($query);
180 $sth->execute($basketno);
181 my $basket = $sth->fetchrow_hashref;
185 #------------------------------------------------------------#
189 $basket = &NewBasket( $booksellerid, $authorizedby, $basketname,
190 $basketnote, $basketbooksellernote, $basketcontractnumber, $deliveryplace, $billingplace, $is_standing );
192 Create a new basket in aqbasket table
196 =item C<$booksellerid> is a foreign key in the aqbasket table
198 =item C<$authorizedby> is the username of who created the basket
202 The other parameters are optional, see ModBasketHeader for more info on them.
207 my ( $booksellerid, $authorisedby, $basketname, $basketnote,
208 $basketbooksellernote, $basketcontractnumber, $deliveryplace,
209 $billingplace, $is_standing ) = @_;
210 my $dbh = C4
::Context
->dbh;
212 'INSERT INTO aqbasket (creationdate,booksellerid,authorisedby) '
213 . 'VALUES (now(),?,?)';
214 $dbh->do( $query, {}, $booksellerid, $authorisedby );
216 my $basket = $dbh->{mysql_insertid
};
217 $basketname ||= q{}; # default to empty strings
219 $basketbooksellernote ||= q{};
220 ModBasketHeader
( $basket, $basketname, $basketnote, $basketbooksellernote,
221 $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing );
225 #------------------------------------------------------------#
229 &CloseBasket($basketno);
231 close a basket (becomes unmodifiable, except for receives)
237 my $dbh = C4
::Context
->dbh;
238 $dbh->do('UPDATE aqbasket SET closedate=now() WHERE basketno=?', {}, $basketno );
240 $dbh->do( q{UPDATE aqorders SET orderstatus = 'ordered' WHERE basketno = ? AND orderstatus != 'complete'},
247 &ReopenBasket($basketno);
255 my $dbh = C4
::Context
->dbh;
256 $dbh->do( q{UPDATE aqbasket SET closedate=NULL WHERE basketno=?}, {}, $basketno );
260 SET orderstatus = 'new'
262 AND orderstatus != 'complete'
267 #------------------------------------------------------------#
269 =head3 GetBasketAsCSV
271 &GetBasketAsCSV($basketno);
273 Export a basket as CSV
275 $cgi parameter is needed for column name translation
280 my ($basketno, $cgi) = @_;
281 my $basket = GetBasket
($basketno);
282 my @orders = GetOrders
($basketno);
283 my $contract = GetContract
({
284 contractnumber
=> $basket->{'contractnumber'}
287 my $template = C4
::Templates
::gettemplate
("acqui/csv/basket.tt", "intranet", $cgi);
290 foreach my $order (@orders) {
291 my $bd = GetBiblioData
( $order->{'biblionumber'} );
293 contractname
=> $contract->{'contractname'},
294 ordernumber
=> $order->{'ordernumber'},
295 entrydate
=> $order->{'entrydate'},
296 isbn
=> $order->{'isbn'},
297 author
=> $bd->{'author'},
298 title
=> $bd->{'title'},
299 publicationyear
=> $bd->{'publicationyear'},
300 publishercode
=> $bd->{'publishercode'},
301 collectiontitle
=> $bd->{'collectiontitle'},
302 notes
=> $order->{'order_vendornote'},
303 quantity
=> $order->{'quantity'},
304 rrp
=> $order->{'rrp'},
306 for my $place ( qw( deliveryplace billingplace ) ) {
307 if ( my $library = Koha
::Libraries
->find( $row->{deliveryplace
} ) ) {
308 $row->{$place} = $library->branchname
312 contractname author title publishercode collectiontitle notes
313 deliveryplace billingplace
315 # Double the quotes to not be interpreted as a field end
316 $row->{$_} =~ s/"/""/g if $row->{$_};
322 if(defined $a->{publishercode
} and defined $b->{publishercode
}) {
323 $a->{publishercode
} cmp $b->{publishercode
};
327 $template->param(rows
=> \
@rows);
329 return $template->output;
333 =head3 GetBasketGroupAsCSV
335 &GetBasketGroupAsCSV($basketgroupid);
337 Export a basket group as CSV
339 $cgi parameter is needed for column name translation
343 sub GetBasketGroupAsCSV
{
344 my ($basketgroupid, $cgi) = @_;
345 my $baskets = GetBasketsByBasketgroup
($basketgroupid);
347 my $template = C4
::Templates
::gettemplate
('acqui/csv/basketgroup.tt', 'intranet', $cgi);
350 for my $basket (@
$baskets) {
351 my @orders = GetOrders
( $basket->{basketno
} );
352 my $contract = GetContract
({
353 contractnumber
=> $basket->{contractnumber
}
355 my $bookseller = Koha
::Acquisition
::Booksellers
->find( $basket->{booksellerid
} );
356 my $basketgroup = GetBasketgroup
( $$basket{basketgroupid
} );
358 foreach my $order (@orders) {
359 my $bd = GetBiblioData
( $order->{'biblionumber'} );
361 clientnumber
=> $bookseller->accountnumber,
362 basketname
=> $basket->{basketname
},
363 ordernumber
=> $order->{ordernumber
},
364 author
=> $bd->{author
},
365 title
=> $bd->{title
},
366 publishercode
=> $bd->{publishercode
},
367 publicationyear
=> $bd->{publicationyear
},
368 collectiontitle
=> $bd->{collectiontitle
},
369 isbn
=> $order->{isbn
},
370 quantity
=> $order->{quantity
},
371 rrp_tax_included
=> $order->{rrp_tax_included
},
372 rrp_tax_excluded
=> $order->{rrp_tax_excluded
},
373 discount
=> $bookseller->discount,
374 ecost_tax_included
=> $order->{ecost_tax_included
},
375 ecost_tax_excluded
=> $order->{ecost_tax_excluded
},
376 notes
=> $order->{order_vendornote
},
377 entrydate
=> $order->{entrydate
},
378 booksellername
=> $bookseller->name,
379 bookselleraddress
=> $bookseller->address1,
380 booksellerpostal
=> $bookseller->postal,
381 contractnumber
=> $contract->{contractnumber
},
382 contractname
=> $contract->{contractname
},
385 basketgroupdeliveryplace
=> $basketgroup->{deliveryplace
},
386 basketgroupbillingplace
=> $basketgroup->{billingplace
},
387 basketdeliveryplace
=> $basket->{deliveryplace
},
388 basketbillingplace
=> $basket->{billingplace
},
390 for my $place (qw( basketgroupdeliveryplace basketgroupbillingplace basketdeliveryplace basketbillingplace )) {
391 if ( my $library = Koha
::Libraries
->find( $temp->{$place} ) ) {
392 $row->{$place} = $library->branchname;
396 basketname author title publishercode collectiontitle notes
397 booksellername bookselleraddress booksellerpostal contractname
398 basketgroupdeliveryplace basketgroupbillingplace
399 basketdeliveryplace basketbillingplace
401 # Double the quotes to not be interpreted as a field end
402 $row->{$_} =~ s/"/""/g if $row->{$_};
407 $template->param(rows
=> \
@rows);
409 return $template->output;
413 =head3 CloseBasketgroup
415 &CloseBasketgroup($basketgroupno);
421 sub CloseBasketgroup
{
422 my ($basketgroupno) = @_;
423 my $dbh = C4
::Context
->dbh;
424 my $sth = $dbh->prepare("
425 UPDATE aqbasketgroups
429 $sth->execute($basketgroupno);
432 #------------------------------------------------------------#
434 =head3 ReOpenBaskergroup($basketgroupno)
436 &ReOpenBaskergroup($basketgroupno);
442 sub ReOpenBasketgroup
{
443 my ($basketgroupno) = @_;
444 my $dbh = C4
::Context
->dbh;
445 my $sth = $dbh->prepare("
446 UPDATE aqbasketgroups
450 $sth->execute($basketgroupno);
453 #------------------------------------------------------------#
458 &DelBasket($basketno);
460 Deletes the basket that has basketno field $basketno in the aqbasket table.
464 =item C<$basketno> is the primary key of the basket in the aqbasket table.
471 my ( $basketno ) = @_;
472 my $query = "DELETE FROM aqbasket WHERE basketno=?";
473 my $dbh = C4
::Context
->dbh;
474 my $sth = $dbh->prepare($query);
475 $sth->execute($basketno);
479 #------------------------------------------------------------#
483 &ModBasket($basketinfo);
485 Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
489 =item C<$basketno> is the primary key of the basket in the aqbasket table.
496 my $basketinfo = shift;
497 my $query = "UPDATE aqbasket SET ";
499 foreach my $key (keys %$basketinfo){
500 if ($key ne 'basketno'){
501 $query .= "$key=?, ";
502 push(@params, $basketinfo->{$key} || undef );
505 # get rid of the "," at the end of $query
506 if (substr($query, length($query)-2) eq ', '){
511 $query .= "WHERE basketno=?";
512 push(@params, $basketinfo->{'basketno'});
513 my $dbh = C4
::Context
->dbh;
514 my $sth = $dbh->prepare($query);
515 $sth->execute(@params);
520 #------------------------------------------------------------#
522 =head3 ModBasketHeader
524 &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid);
526 Modifies a basket's header.
530 =item C<$basketno> is the "basketno" field in the "aqbasket" table;
532 =item C<$basketname> is the "basketname" field in the "aqbasket" table;
534 =item C<$note> is the "note" field in the "aqbasket" table;
536 =item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
538 =item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
540 =item C<$booksellerid> is the id (foreign) key in the "aqbooksellers" table for the vendor.
542 =item C<$deliveryplace> is the "deliveryplace" field in the aqbasket table.
544 =item C<$billingplace> is the "billingplace" field in the aqbasket table.
546 =item C<$is_standing> is the "is_standing" field in the aqbasket table.
552 sub ModBasketHeader
{
553 my ($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing) = @_;
556 SET basketname
=?
, note
=?
, booksellernote
=?
, booksellerid
=?
, deliveryplace
=?
, billingplace
=?
, is_standing
=?
560 my $dbh = C4
::Context
->dbh;
561 my $sth = $dbh->prepare($query);
562 $sth->execute($basketname, $note, $booksellernote, $booksellerid, $deliveryplace, $billingplace, $is_standing, $basketno);
564 if ( $contractnumber ) {
565 my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
566 my $sth2 = $dbh->prepare($query2);
567 $sth2->execute($contractnumber,$basketno);
572 #------------------------------------------------------------#
574 =head3 GetBasketsByBookseller
576 @results = &GetBasketsByBookseller($booksellerid, $extra);
578 Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
582 =item C<$booksellerid> is the 'id' field of the bookseller in the aqbooksellers table
584 =item C<$extra> is the extra sql parameters, can be
586 $extra->{groupby}: group baskets by column
587 ex. $extra->{groupby} = aqbasket.basketgroupid
588 $extra->{orderby}: order baskets by column
589 $extra->{limit}: limit number of results (can be helpful for pagination)
595 sub GetBasketsByBookseller
{
596 my ($booksellerid, $extra) = @_;
597 my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
599 if ($extra->{groupby
}) {
600 $query .= " GROUP by $extra->{groupby}";
602 if ($extra->{orderby
}){
603 $query .= " ORDER by $extra->{orderby}";
605 if ($extra->{limit
}){
606 $query .= " LIMIT $extra->{limit}";
609 my $dbh = C4
::Context
->dbh;
610 my $sth = $dbh->prepare($query);
611 $sth->execute($booksellerid);
612 return $sth->fetchall_arrayref({});
615 =head3 GetBasketsInfosByBookseller
617 my $baskets = GetBasketsInfosByBookseller($supplierid, $allbaskets);
619 The optional second parameter allbaskets is a boolean allowing you to
620 select all baskets from the supplier; by default only active baskets (open or
621 closed but still something to receive) are returned.
623 Returns in a arrayref of hashref all about booksellers baskets, plus:
624 total_biblios: Number of distinct biblios in basket
625 total_items: Number of items in basket
626 expected_items: Number of non-received items in basket
630 sub GetBasketsInfosByBookseller
{
631 my ($supplierid, $allbaskets) = @_;
633 return unless $supplierid;
635 my $dbh = C4
::Context
->dbh;
638 SUM(aqorders.quantity) AS total_items,
640 IF ( aqorders.orderstatus = 'cancelled', aqorders.quantity, 0 )
641 ) AS total_items_cancelled,
642 COUNT(DISTINCT aqorders.biblionumber) AS total_biblios,
644 IF(aqorders.datereceived IS NULL
645 AND aqorders.datecancellationprinted IS NULL
650 LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
651 WHERE booksellerid = ?};
653 unless ( $allbaskets ) {
654 $query.=" AND (closedate IS NULL OR (aqorders.quantity > aqorders.quantityreceived AND datecancellationprinted IS NULL))";
656 $query.=" GROUP BY aqbasket.basketno";
658 my $sth = $dbh->prepare($query);
659 $sth->execute($supplierid);
660 my $baskets = $sth->fetchall_arrayref({});
662 # Retrieve the number of biblios cancelled
663 my $cancelled_biblios = $dbh->selectall_hashref( q
|
664 SELECT COUNT
(DISTINCT
(biblionumber
)) AS total_biblios_cancelled
, aqbasket
.basketno
666 LEFT JOIN aqorders ON aqorders
.basketno
= aqbasket
.basketno
667 WHERE booksellerid
= ?
668 AND aqorders
.orderstatus
= 'cancelled'
669 GROUP BY aqbasket
.basketno
670 |, 'basketno', {}, $supplierid );
672 $_->{total_biblios_cancelled
} = $cancelled_biblios->{$_->{basketno
}}{total_biblios_cancelled
} || 0
678 =head3 GetBasketUsers
680 $basketusers_ids = &GetBasketUsers($basketno);
682 Returns a list of all borrowernumbers that are in basket users list
687 my $basketno = shift;
689 return unless $basketno;
692 SELECT borrowernumber
696 my $dbh = C4
::Context
->dbh;
697 my $sth = $dbh->prepare($query);
698 $sth->execute($basketno);
699 my $results = $sth->fetchall_arrayref( {} );
702 foreach (@
$results) {
703 push @borrowernumbers, $_->{'borrowernumber'};
706 return @borrowernumbers;
709 =head3 ModBasketUsers
711 my @basketusers_ids = (1, 2, 3);
712 &ModBasketUsers($basketno, @basketusers_ids);
714 Delete all users from basket users list, and add users in C<@basketusers_ids>
720 my ($basketno, @basketusers_ids) = @_;
722 return unless $basketno;
724 my $dbh = C4
::Context
->dbh;
726 DELETE FROM aqbasketusers
729 my $sth = $dbh->prepare($query);
730 $sth->execute($basketno);
733 INSERT INTO aqbasketusers
(basketno
, borrowernumber
)
736 $sth = $dbh->prepare($query);
737 foreach my $basketuser_id (@basketusers_ids) {
738 $sth->execute($basketno, $basketuser_id);
743 =head3 CanUserManageBasket
745 my $bool = CanUserManageBasket($borrower, $basket[, $userflags]);
746 my $bool = CanUserManageBasket($borrowernumber, $basketno[, $userflags]);
748 Check if a borrower can manage a basket, according to system preference
749 AcqViewBaskets, user permissions and basket properties (creator, users list,
752 First parameter can be either a borrowernumber or a hashref as returned by
753 C4::Members::GetMember.
755 Second parameter can be either a basketno or a hashref as returned by
756 C4::Acquisition::GetBasket.
758 The third parameter is optional. If given, it should be a hashref as returned
759 by C4::Auth::getuserflags. If not, getuserflags is called.
761 If user is authorised to manage basket, returns 1.
766 sub CanUserManageBasket
{
767 my ($borrower, $basket, $userflags) = @_;
769 if (!ref $borrower) {
770 $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrower);
773 $basket = GetBasket
($basket);
776 return 0 unless ($basket and $borrower);
778 my $borrowernumber = $borrower->{borrowernumber
};
779 my $basketno = $basket->{basketno
};
781 my $AcqViewBaskets = C4
::Context
->preference('AcqViewBaskets');
783 if (!defined $userflags) {
784 my $dbh = C4
::Context
->dbh;
785 my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE borrowernumber = ?");
786 $sth->execute($borrowernumber);
787 my ($flags) = $sth->fetchrow_array;
790 $userflags = C4
::Auth
::getuserflags
($flags, $borrower->{userid
}, $dbh);
793 unless ($userflags->{superlibrarian
}
794 || (ref $userflags->{acquisition
} && $userflags->{acquisition
}->{order_manage_all
})
795 || (!ref $userflags->{acquisition
} && $userflags->{acquisition
}))
797 if (not exists $userflags->{acquisition
}) {
801 if ( (ref $userflags->{acquisition
} && !$userflags->{acquisition
}->{order_manage
})
802 || (!ref $userflags->{acquisition
} && !$userflags->{acquisition
}) ) {
806 if ($AcqViewBaskets eq 'user'
807 && $basket->{authorisedby
} != $borrowernumber
808 && grep($borrowernumber, GetBasketUsers
($basketno)) == 0) {
812 if ($AcqViewBaskets eq 'branch' && defined $basket->{branch
}
813 && $basket->{branch
} ne $borrower->{branchcode
}) {
821 #------------------------------------------------------------#
823 =head3 GetBasketsByBasketgroup
825 $baskets = &GetBasketsByBasketgroup($basketgroupid);
827 Returns a reference to all baskets that belong to basketgroup $basketgroupid.
831 sub GetBasketsByBasketgroup
{
832 my $basketgroupid = shift;
834 SELECT
*, aqbasket
.booksellerid as booksellerid
836 LEFT JOIN aqcontract USING
(contractnumber
) WHERE basketgroupid
=?
838 my $dbh = C4
::Context
->dbh;
839 my $sth = $dbh->prepare($query);
840 $sth->execute($basketgroupid);
841 return $sth->fetchall_arrayref({});
844 #------------------------------------------------------------#
846 =head3 NewBasketgroup
848 $basketgroupid = NewBasketgroup(\%hashref);
850 Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
852 $hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
854 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
856 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
858 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
860 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
862 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
864 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
866 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
871 my $basketgroupinfo = shift;
872 die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
873 my $query = "INSERT INTO aqbasketgroups (";
875 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
876 if ( defined $basketgroupinfo->{$field} ) {
877 $query .= "$field, ";
878 push(@params, $basketgroupinfo->{$field});
881 $query .= "booksellerid) VALUES (";
886 push(@params, $basketgroupinfo->{'booksellerid'});
887 my $dbh = C4
::Context
->dbh;
888 my $sth = $dbh->prepare($query);
889 $sth->execute(@params);
890 my $basketgroupid = $dbh->{'mysql_insertid'};
891 if( $basketgroupinfo->{'basketlist'} ) {
892 foreach my $basketno (@
{$basketgroupinfo->{'basketlist'}}) {
893 my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
894 my $sth2 = $dbh->prepare($query2);
895 $sth2->execute($basketgroupid, $basketno);
898 return $basketgroupid;
901 #------------------------------------------------------------#
903 =head3 ModBasketgroup
905 ModBasketgroup(\%hashref);
907 Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
909 $hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
911 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
913 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
915 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
917 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
919 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
921 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
923 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
928 my $basketgroupinfo = shift;
929 die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
930 my $dbh = C4
::Context
->dbh;
931 my $query = "UPDATE aqbasketgroups SET ";
933 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
934 if ( defined $basketgroupinfo->{$field} ) {
935 $query .= "$field=?, ";
936 push(@params, $basketgroupinfo->{$field});
941 $query .= " WHERE id=?";
942 push(@params, $basketgroupinfo->{'id'});
943 my $sth = $dbh->prepare($query);
944 $sth->execute(@params);
946 $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
947 $sth->execute($basketgroupinfo->{'id'});
949 if($basketgroupinfo->{'basketlist'} && @
{$basketgroupinfo->{'basketlist'}}){
950 $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
951 foreach my $basketno (@
{$basketgroupinfo->{'basketlist'}}) {
952 $sth->execute($basketgroupinfo->{'id'}, $basketno);
958 #------------------------------------------------------------#
960 =head3 DelBasketgroup
962 DelBasketgroup($basketgroupid);
964 Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
968 =item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
975 my $basketgroupid = shift;
976 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
977 my $query = "DELETE FROM aqbasketgroups WHERE id=?";
978 my $dbh = C4
::Context
->dbh;
979 my $sth = $dbh->prepare($query);
980 $sth->execute($basketgroupid);
984 #------------------------------------------------------------#
987 =head2 FUNCTIONS ABOUT ORDERS
989 =head3 GetBasketgroup
991 $basketgroup = &GetBasketgroup($basketgroupid);
993 Returns a reference to the hash containing all information about the basketgroup.
998 my $basketgroupid = shift;
999 die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
1000 my $dbh = C4
::Context
->dbh;
1001 my $result_set = $dbh->selectall_arrayref(
1002 'SELECT * FROM aqbasketgroups WHERE id=?',
1006 return $result_set->[0]; # id is unique
1009 #------------------------------------------------------------#
1011 =head3 GetBasketgroups
1013 $basketgroups = &GetBasketgroups($booksellerid);
1015 Returns a reference to the array of all the basketgroups of bookseller $booksellerid.
1019 sub GetBasketgroups
{
1020 my $booksellerid = shift;
1021 die 'bookseller id is required to edit a basketgroup' unless $booksellerid;
1022 my $query = 'SELECT * FROM aqbasketgroups WHERE booksellerid=? ORDER BY id DESC';
1023 my $dbh = C4
::Context
->dbh;
1024 my $sth = $dbh->prepare($query);
1025 $sth->execute($booksellerid);
1026 return $sth->fetchall_arrayref({});
1029 #------------------------------------------------------------#
1031 =head2 FUNCTIONS ABOUT ORDERS
1035 @orders = &GetOrders( $basketno, { orderby => 'biblio.title', cancelled => 0|1 } );
1037 Looks up the pending (non-cancelled) orders with the given basket
1040 If cancelled is set, only cancelled orders will be returned.
1045 my ( $basketno, $params ) = @_;
1047 return () unless $basketno;
1049 my $orderby = $params->{orderby
};
1050 my $cancelled = $params->{cancelled
} || 0;
1052 my $dbh = C4
::Context
->dbh;
1054 SELECT biblio
.*,biblioitems
.*,
1058 $query .= $cancelled
1060 aqorders_transfers
.ordernumber_to AS transferred_to
,
1061 aqorders_transfers
.timestamp AS transferred_to_timestamp
1064 aqorders_transfers
.ordernumber_from AS transferred_from
,
1065 aqorders_transfers
.timestamp AS transferred_from_timestamp
1069 LEFT JOIN aqbudgets ON aqbudgets
.budget_id
= aqorders
.budget_id
1070 LEFT JOIN biblio ON biblio
.biblionumber
= aqorders
.biblionumber
1071 LEFT JOIN biblioitems ON biblioitems
.biblionumber
=biblio
.biblionumber
1073 $query .= $cancelled
1075 LEFT JOIN aqorders_transfers ON aqorders_transfers
.ordernumber_from
= aqorders
.ordernumber
1078 LEFT JOIN aqorders_transfers ON aqorders_transfers
.ordernumber_to
= aqorders
.ordernumber
1086 $orderby ||= q
|biblioitems
.publishercode
, biblio
.title
|;
1088 AND
(datecancellationprinted IS NOT NULL
1089 AND datecancellationprinted
<> '0000-00-00')
1094 q
|aqorders
.datecancellationprinted desc
, aqorders
.timestamp desc
|;
1096 AND
(datecancellationprinted IS NULL OR datecancellationprinted
='0000-00-00')
1100 $query .= " ORDER BY $orderby";
1102 $dbh->selectall_arrayref( $query, { Slice
=> {} }, $basketno );
1107 #------------------------------------------------------------#
1109 =head3 GetOrdersByBiblionumber
1111 @orders = &GetOrdersByBiblionumber($biblionumber);
1113 Looks up the orders with linked to a specific $biblionumber, including
1114 cancelled orders and received orders.
1117 C<@orders> is an array of references-to-hash, whose keys are the
1118 fields from the aqorders, biblio, and biblioitems tables in the Koha database.
1122 sub GetOrdersByBiblionumber
{
1123 my $biblionumber = shift;
1124 return unless $biblionumber;
1125 my $dbh = C4
::Context
->dbh;
1127 SELECT biblio.*,biblioitems.*,
1131 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1132 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1133 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
1134 WHERE aqorders.biblionumber=?
1137 $dbh->selectall_arrayref( $query, { Slice
=> {} }, $biblionumber );
1138 return @
{$result_set};
1142 #------------------------------------------------------------#
1146 $order = &GetOrder($ordernumber);
1148 Looks up an order by order number.
1150 Returns a reference-to-hash describing the order. The keys of
1151 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
1156 my ($ordernumber) = @_;
1157 return unless $ordernumber;
1159 my $dbh = C4
::Context
->dbh;
1160 my $query = qq{SELECT
1164 aqbasket
.basketname
,
1165 borrowers
.branchcode
,
1166 biblioitems
.publicationyear
,
1167 biblio
.copyrightdate
,
1168 biblioitems
.editionstatement
,
1172 biblioitems
.publishercode
,
1173 aqorders
.rrp AS unitpricesupplier
,
1174 aqorders
.ecost AS unitpricelib
,
1175 aqorders
.claims_count AS claims_count
,
1176 aqorders
.claimed_date AS claimed_date
,
1177 aqbudgets
.budget_name AS budget
,
1178 aqbooksellers
.name AS supplier
,
1179 aqbooksellers
.id AS supplierid
,
1180 biblioitems
.publishercode AS publisher
,
1181 ADDDATE
(aqbasket
.closedate
, INTERVAL aqbooksellers
.deliverytime DAY
) AS estimateddeliverydate
,
1182 DATE
(aqbasket
.closedate
) AS orderdate
,
1183 aqorders
.quantity
- COALESCE
(aqorders
.quantityreceived
,0) AS quantity_to_receive
,
1184 (aqorders
.quantity
- COALESCE
(aqorders
.quantityreceived
,0)) * aqorders
.rrp AS subtotal
,
1185 DATEDIFF
(CURDATE
( ),closedate
) AS latesince
1186 FROM aqorders LEFT JOIN biblio ON biblio
.biblionumber
= aqorders
.biblionumber
1187 LEFT JOIN biblioitems ON biblioitems
.biblionumber
= biblio
.biblionumber
1188 LEFT JOIN aqbudgets ON aqorders
.budget_id
= aqbudgets
.budget_id
,
1189 aqbasket LEFT JOIN borrowers ON aqbasket
.authorisedby
= borrowers
.borrowernumber
1190 LEFT JOIN aqbooksellers ON aqbasket
.booksellerid
= aqbooksellers
.id
1191 WHERE aqorders
.basketno
= aqbasket
.basketno
1194 $dbh->selectall_arrayref( $query, { Slice
=> {} }, $ordernumber );
1196 # result_set assumed to contain 1 match
1197 return $result_set->[0];
1200 =head3 GetLastOrderNotReceivedFromSubscriptionid
1202 $order = &GetLastOrderNotReceivedFromSubscriptionid($subscriptionid);
1204 Returns a reference-to-hash describing the last order not received for a subscription.
1208 sub GetLastOrderNotReceivedFromSubscriptionid
{
1209 my ( $subscriptionid ) = @_;
1210 my $dbh = C4
::Context
->dbh;
1212 SELECT
* FROM aqorders
1213 LEFT JOIN subscription
1214 ON
( aqorders
.subscriptionid
= subscription
.subscriptionid
)
1215 WHERE aqorders
.subscriptionid
= ?
1216 AND aqorders
.datereceived IS NULL
1220 $dbh->selectall_arrayref( $query, { Slice
=> {} }, $subscriptionid );
1222 # result_set assumed to contain 1 match
1223 return $result_set->[0];
1226 =head3 GetLastOrderReceivedFromSubscriptionid
1228 $order = &GetLastOrderReceivedFromSubscriptionid($subscriptionid);
1230 Returns a reference-to-hash describing the last order received for a subscription.
1234 sub GetLastOrderReceivedFromSubscriptionid
{
1235 my ( $subscriptionid ) = @_;
1236 my $dbh = C4
::Context
->dbh;
1238 SELECT
* FROM aqorders
1239 LEFT JOIN subscription
1240 ON
( aqorders
.subscriptionid
= subscription
.subscriptionid
)
1241 WHERE aqorders
.subscriptionid
= ?
1242 AND aqorders
.datereceived
=
1244 SELECT MAX
( aqorders
.datereceived
)
1246 LEFT JOIN subscription
1247 ON
( aqorders
.subscriptionid
= subscription
.subscriptionid
)
1248 WHERE aqorders
.subscriptionid
= ?
1249 AND aqorders
.datereceived IS NOT NULL
1251 ORDER BY ordernumber DESC
1255 $dbh->selectall_arrayref( $query, { Slice
=> {} }, $subscriptionid, $subscriptionid );
1257 # result_set assumed to contain 1 match
1258 return $result_set->[0];
1262 #------------------------------------------------------------#
1266 &ModOrder(\%hashref);
1268 Modifies an existing order. Updates the order with order number
1269 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All
1270 other keys of the hash update the fields with the same name in the aqorders
1271 table of the Koha database.
1276 my $orderinfo = shift;
1278 die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '';
1280 my $dbh = C4
::Context
->dbh;
1283 # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
1284 $orderinfo->{uncertainprice
}=1 if $orderinfo->{uncertainprice
};
1286 # delete($orderinfo->{'branchcode'});
1287 # the hash contains a lot of entries not in aqorders, so get the columns ...
1288 my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1290 my $colnames = $sth->{NAME
};
1291 #FIXME Be careful. If aqorders would have columns with diacritics,
1292 #you should need to decode what you get back from NAME.
1293 #See report 10110 and guided_reports.pl
1294 my $query = "UPDATE aqorders SET ";
1296 foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1297 # ... and skip hash entries that are not in the aqorders table
1298 # FIXME : probably not the best way to do it (would be better to have a correct hash)
1299 next unless grep(/^$orderinfokey$/, @
$colnames);
1300 $query .= "$orderinfokey=?, ";
1301 push(@params, $orderinfo->{$orderinfokey});
1304 $query .= "timestamp=NOW() WHERE ordernumber=?";
1305 push(@params, $orderinfo->{'ordernumber'} );
1306 $sth = $dbh->prepare($query);
1307 $sth->execute(@params);
1311 #------------------------------------------------------------#
1315 ModItemOrder($itemnumber, $ordernumber);
1317 Modifies the ordernumber of an item in aqorders_items.
1322 my ($itemnumber, $ordernumber) = @_;
1324 return unless ($itemnumber and $ordernumber);
1326 my $dbh = C4
::Context
->dbh;
1328 UPDATE aqorders_items
1330 WHERE itemnumber
= ?
1332 my $sth = $dbh->prepare($query);
1333 return $sth->execute($ordernumber, $itemnumber);
1336 #------------------------------------------------------------#
1338 =head3 ModReceiveOrder
1340 my ( $date_received, $new_ordernumber ) = ModReceiveOrder(
1342 biblionumber => $biblionumber,
1344 quantityreceived => $quantityreceived,
1346 invoice => $invoice,
1347 budget_id => $budget_id,
1348 received_itemnumbers => \@received_itemnumbers,
1349 order_internalnote => $order_internalnote,
1353 Updates an order, to reflect the fact that it was received, at least
1356 If a partial order is received, splits the order into two.
1358 Updates the order with biblionumber C<$biblionumber> and ordernumber
1359 C<$order->{ordernumber}>.
1364 sub ModReceiveOrder
{
1366 my $biblionumber = $params->{biblionumber
};
1367 my $order = { %{ $params->{order
} } }; # Copy the order, we don't want to modify it
1368 my $invoice = $params->{invoice
};
1369 my $quantrec = $params->{quantityreceived
};
1370 my $user = $params->{user
};
1371 my $budget_id = $params->{budget_id
};
1372 my $received_items = $params->{received_items
};
1374 my $dbh = C4
::Context
->dbh;
1375 my $datereceived = ( $invoice and $invoice->{datereceived
} ) ?
$invoice->{datereceived
} : dt_from_string
;
1376 my $suggestionid = GetSuggestionFromBiblionumber
( $biblionumber );
1377 if ($suggestionid) {
1378 ModSuggestion
( {suggestionid
=>$suggestionid,
1379 STATUS
=>'AVAILABLE',
1380 biblionumber
=> $biblionumber}
1384 my $result_set = $dbh->selectrow_arrayref(
1385 q{SELECT aqbasket.is_standing
1387 WHERE basketno=?},{ Slice
=> {} }, $order->{basketno
});
1388 my $is_standing = $result_set->[0]; # we assume we have a unique basket
1390 my $new_ordernumber = $order->{ordernumber
};
1391 if ( $is_standing || $order->{quantity
} > $quantrec ) {
1392 # Split order line in two parts: the first is the original order line
1393 # without received items (the quantity is decreased),
1394 # the second part is a new order line with quantity=quantityrec
1395 # (entirely received)
1399 orderstatus
= 'partial'|;
1400 $query .= q
|, order_internalnote
= ?
| if defined $order->{order_internalnote
};
1401 $query .= q
| WHERE ordernumber
= ?
|;
1402 my $sth = $dbh->prepare($query);
1405 ( $is_standing ?
1 : ($order->{quantity
} - $quantrec) ),
1406 ( defined $order->{order_internalnote
} ?
$order->{order_internalnote
} : () ),
1407 $order->{ordernumber
}
1410 # Recalculate tax_value
1414 tax_value_on_ordering
= quantity
* ecost_tax_excluded
* tax_rate_on_ordering
,
1415 tax_value_on_receiving
= quantity
* unitprice_tax_excluded
* tax_rate_on_receiving
1416 WHERE ordernumber
= ?
1417 |, undef, $order->{ordernumber
});
1419 delete $order->{ordernumber
};
1420 $order->{budget_id
} = ( $budget_id || $order->{budget_id
} );
1421 $order->{quantity
} = $quantrec;
1422 $order->{quantityreceived
} = $quantrec;
1423 $order->{ecost_tax_excluded
} //= 0;
1424 $order->{tax_rate_on_ordering
} //= 0;
1425 $order->{unitprice_tax_excluded
} //= 0;
1426 $order->{tax_rate_on_receiving
} //= 0;
1427 $order->{tax_value_on_ordering
} = $order->{quantity
} * $order->{ecost_tax_excluded
} * $order->{tax_rate_on_ordering
};
1428 $order->{tax_value_on_receiving
} = $order->{quantity
} * $order->{unitprice_tax_excluded
} * $order->{tax_rate_on_receiving
};
1429 $order->{datereceived
} = $datereceived;
1430 $order->{invoiceid
} = $invoice->{invoiceid
};
1431 $order->{orderstatus
} = 'complete';
1432 $new_ordernumber = Koha
::Acquisition
::Order
->new($order)->insert->{ordernumber
};
1434 if ($received_items) {
1435 foreach my $itemnumber (@
$received_items) {
1436 ModItemOrder
($itemnumber, $new_ordernumber);
1442 SET quantityreceived
= ?
,
1446 orderstatus
= 'complete'
1450 , unitprice
= ?
, unitprice_tax_included
= ?
, unitprice_tax_excluded
= ?
1451 | if defined $order->{unitprice
};
1454 ,tax_value_on_receiving
= ?
1455 | if defined $order->{tax_value_on_receiving
};
1458 ,tax_rate_on_receiving
= ?
1459 | if defined $order->{tax_rate_on_receiving
};
1462 , order_internalnote
= ?
1463 | if defined $order->{order_internalnote
};
1465 $query .= q
| where biblionumber
=?
and ordernumber
=?
|;
1467 my $sth = $dbh->prepare( $query );
1468 my @params = ( $quantrec, $datereceived, $invoice->{invoiceid
}, ( $budget_id ?
$budget_id : $order->{budget_id
} ) );
1470 if ( defined $order->{unitprice
} ) {
1471 push @params, $order->{unitprice
}, $order->{unitprice_tax_included
}, $order->{unitprice_tax_excluded
};
1474 if ( defined $order->{tax_value_on_receiving
} ) {
1475 push @params, $order->{tax_value_on_receiving
};
1478 if ( defined $order->{tax_rate_on_receiving
} ) {
1479 push @params, $order->{tax_rate_on_receiving
};
1482 if ( defined $order->{order_internalnote
} ) {
1483 push @params, $order->{order_internalnote
};
1486 push @params, ( $biblionumber, $order->{ordernumber
} );
1488 $sth->execute( @params );
1490 # All items have been received, sent a notification to users
1491 NotifyOrderUsers
( $order->{ordernumber
} );
1494 return ($datereceived, $new_ordernumber);
1497 =head3 CancelReceipt
1499 my $parent_ordernumber = CancelReceipt($ordernumber);
1501 Cancel an order line receipt and update the parent order line, as if no
1503 If items are created at receipt (AcqCreateItem = receiving) then delete
1509 my $ordernumber = shift;
1511 return unless $ordernumber;
1513 my $dbh = C4
::Context
->dbh;
1515 SELECT datereceived
, parent_ordernumber
, quantity
1517 WHERE ordernumber
= ?
1519 my $sth = $dbh->prepare($query);
1520 $sth->execute($ordernumber);
1521 my $order = $sth->fetchrow_hashref;
1523 warn "CancelReceipt: order $ordernumber does not exist";
1526 unless($order->{'datereceived'}) {
1527 warn "CancelReceipt: order $ordernumber is not received";
1531 my $parent_ordernumber = $order->{'parent_ordernumber'};
1533 my @itemnumbers = GetItemnumbersFromOrder
( $ordernumber );
1535 if($parent_ordernumber == $ordernumber || not $parent_ordernumber) {
1536 # The order line has no parent, just mark it as not received
1539 SET quantityreceived
= ?
,
1542 orderstatus
= 'ordered'
1543 WHERE ordernumber
= ?
1545 $sth = $dbh->prepare($query);
1546 $sth->execute(0, undef, undef, $ordernumber);
1547 _cancel_items_receipt
( $ordernumber );
1549 # The order line has a parent, increase parent quantity and delete
1552 SELECT quantity
, datereceived
1554 WHERE ordernumber
= ?
1556 $sth = $dbh->prepare($query);
1557 $sth->execute($parent_ordernumber);
1558 my $parent_order = $sth->fetchrow_hashref;
1559 unless($parent_order) {
1560 warn "Parent order $parent_ordernumber does not exist.";
1563 if($parent_order->{'datereceived'}) {
1564 warn "CancelReceipt: parent order is received.".
1565 " Can't cancel receipt.";
1571 orderstatus
= 'ordered'
1572 WHERE ordernumber
= ?
1574 $sth = $dbh->prepare($query);
1575 my $rv = $sth->execute(
1576 $order->{'quantity'} + $parent_order->{'quantity'},
1580 warn "Cannot update parent order line, so do not cancel".
1585 # Recalculate tax_value
1589 tax_value_on_ordering
= quantity
* ecost_tax_excluded
* tax_rate_on_ordering
,
1590 tax_value_on_receiving
= quantity
* unitprice_tax_excluded
* tax_rate_on_receiving
1591 WHERE ordernumber
= ?
1592 |, undef, $parent_ordernumber);
1594 _cancel_items_receipt
( $ordernumber, $parent_ordernumber );
1597 DELETE FROM aqorders
1598 WHERE ordernumber
= ?
1600 $sth = $dbh->prepare($query);
1601 $sth->execute($ordernumber);
1605 if(C4
::Context
->preference('AcqCreateItem') eq 'ordering') {
1606 my @affects = split q{\|}, C4
::Context
->preference("AcqItemSetSubfieldsWhenReceiptIsCancelled");
1608 for my $in ( @itemnumbers ) {
1609 my $biblionumber = C4
::Biblio
::GetBiblionumberFromItemnumber
( $in );
1610 my $frameworkcode = GetFrameworkCode
($biblionumber);
1611 my ( $itemfield ) = GetMarcFromKohaField
( 'items.itemnumber', $frameworkcode );
1612 my $item = C4
::Items
::GetMarcItem
( $biblionumber, $in );
1613 for my $affect ( @affects ) {
1614 my ( $sf, $v ) = split q{=}, $affect, 2;
1615 foreach ( $item->field($itemfield) ) {
1616 $_->update( $sf => $v );
1619 C4
::Items
::ModItemFromMarc
( $item, $biblionumber, $in );
1624 return $parent_ordernumber;
1627 sub _cancel_items_receipt
{
1628 my ( $ordernumber, $parent_ordernumber ) = @_;
1629 $parent_ordernumber ||= $ordernumber;
1631 my @itemnumbers = GetItemnumbersFromOrder
($ordernumber);
1632 if(C4
::Context
->preference('AcqCreateItem') eq 'receiving') {
1633 # Remove items that were created at receipt
1635 DELETE FROM items
, aqorders_items
1636 USING items
, aqorders_items
1637 WHERE items
.itemnumber
= ? AND aqorders_items
.itemnumber
= ?
1639 my $dbh = C4
::Context
->dbh;
1640 my $sth = $dbh->prepare($query);
1641 foreach my $itemnumber (@itemnumbers) {
1642 $sth->execute($itemnumber, $itemnumber);
1646 foreach my $itemnumber (@itemnumbers) {
1647 ModItemOrder
($itemnumber, $parent_ordernumber);
1652 #------------------------------------------------------------#
1656 @results = &SearchOrders({
1657 ordernumber => $ordernumber,
1660 booksellerid => $booksellerid,
1661 basketno => $basketno,
1662 basketname => $basketname,
1663 basketgroupname => $basketgroupname,
1667 biblionumber => $biblionumber,
1668 budget_id => $budget_id
1671 Searches for orders filtered by criteria.
1673 C<$ordernumber> Finds matching orders or transferred orders by ordernumber.
1674 C<$search> Finds orders matching %$search% in title, author, or isbn.
1675 C<$owner> Finds order for the logged in user.
1676 C<$pending> Finds pending orders. Ignores completed and cancelled orders.
1677 C<$ordered> Finds orders to receive only (status 'ordered' or 'partial').
1680 C<@results> is an array of references-to-hash with the keys are fields
1681 from aqorders, biblio, biblioitems and aqbasket tables.
1686 my ( $params ) = @_;
1687 my $ordernumber = $params->{ordernumber
};
1688 my $search = $params->{search
};
1689 my $ean = $params->{ean
};
1690 my $booksellerid = $params->{booksellerid
};
1691 my $basketno = $params->{basketno
};
1692 my $basketname = $params->{basketname
};
1693 my $basketgroupname = $params->{basketgroupname
};
1694 my $owner = $params->{owner
};
1695 my $pending = $params->{pending
};
1696 my $ordered = $params->{ordered
};
1697 my $biblionumber = $params->{biblionumber
};
1698 my $budget_id = $params->{budget_id
};
1700 my $dbh = C4
::Context
->dbh;
1703 SELECT aqbasket.basketno,
1705 borrowers.firstname,
1708 biblioitems.biblioitemnumber,
1709 aqbasket.authorisedby,
1710 aqbasket.booksellerid,
1712 aqbasket.creationdate,
1713 aqbasket.basketname,
1714 aqbasketgroups.id as basketgroupid,
1715 aqbasketgroups.name as basketgroupname,
1718 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1719 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
1720 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1721 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1722 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1725 # If we search on ordernumber, we retrieve the transferred order if a transfer has been done.
1727 LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_to = aqorders.ordernumber
1731 WHERE (datecancellationprinted is NULL)
1734 if ( $pending or $ordered ) {
1737 ( aqbasket.is_standing AND aqorders.orderstatus IN ( "new", "ordered", "partial" ) )
1739 ( quantity > quantityreceived OR quantityreceived is NULL )
1743 $query .= q{ AND aqorders.orderstatus IN ( "ordered", "partial" )};
1751 my $userenv = C4
::Context
->userenv;
1752 if ( C4
::Context
->preference("IndependentBranches") ) {
1753 unless ( C4
::Context
->IsSuperLibrarian() ) {
1756 borrowers.branchcode = ?
1757 OR borrowers.branchcode = ''
1760 push @args, $userenv->{branch
};
1764 if ( $ordernumber ) {
1765 $query .= ' AND ( aqorders.ordernumber = ? OR aqorders_transfers.ordernumber_from = ? ) ';
1766 push @args, ( $ordernumber, $ordernumber );
1768 if ( $biblionumber ) {
1769 $query .= 'AND aqorders.biblionumber = ?';
1770 push @args, $biblionumber;
1773 $query .= ' AND (biblio.title LIKE ? OR biblio.author LIKE ? OR biblioitems.isbn LIKE ?)';
1774 push @args, ("%$search%","%$search%","%$search%");
1777 $query .= ' AND biblioitems.ean = ?';
1780 if ( $booksellerid ) {
1781 $query .= 'AND aqbasket.booksellerid = ?';
1782 push @args, $booksellerid;
1785 $query .= 'AND aqbasket.basketno = ?';
1786 push @args, $basketno;
1789 $query .= 'AND aqbasket.basketname LIKE ?';
1790 push @args, "%$basketname%";
1792 if( $basketgroupname ) {
1793 $query .= ' AND aqbasketgroups.name LIKE ?';
1794 push @args, "%$basketgroupname%";
1798 $query .= ' AND aqbasket.authorisedby=? ';
1799 push @args, $userenv->{'number'};
1803 $query .= ' AND aqorders.budget_id = ?';
1804 push @args, $budget_id;
1807 $query .= ' ORDER BY aqbasket.basketno';
1809 my $sth = $dbh->prepare($query);
1810 $sth->execute(@args);
1811 return $sth->fetchall_arrayref({});
1814 #------------------------------------------------------------#
1818 &DelOrder($biblionumber, $ordernumber);
1820 Cancel the order with the given order and biblio numbers. It does not
1821 delete any entries in the aqorders table, it merely marks them as
1827 my ( $bibnum, $ordernumber, $delete_biblio, $reason ) = @_;
1830 my $dbh = C4
::Context
->dbh;
1833 SET datecancellationprinted=now(), orderstatus='cancelled'
1836 $query .= ", cancellationreason = ? ";
1839 WHERE biblionumber=? AND ordernumber=?
1841 my $sth = $dbh->prepare($query);
1843 $sth->execute($reason, $bibnum, $ordernumber);
1845 $sth->execute( $bibnum, $ordernumber );
1849 my @itemnumbers = GetItemnumbersFromOrder
( $ordernumber );
1850 foreach my $itemnumber (@itemnumbers){
1851 my $delcheck = C4
::Items
::DelItemCheck
( $bibnum, $itemnumber );
1853 if($delcheck != 1) {
1854 $error->{'delitem'} = 1;
1858 if($delete_biblio) {
1859 # We get the number of remaining items
1860 my $biblio = Koha
::Biblios
->find( $bibnum );
1861 my $itemcount = $biblio->items->count;
1863 # If there are no items left,
1864 if ( $itemcount == 0 ) {
1865 # We delete the record
1866 my $delcheck = DelBiblio
($bibnum);
1869 $error->{'delbiblio'} = 1;
1877 =head3 TransferOrder
1879 my $newordernumber = TransferOrder($ordernumber, $basketno);
1881 Transfer an order line to a basket.
1882 Mark $ordernumber as cancelled with an internal note 'Cancelled and transferred
1883 to BOOKSELLER on DATE' and create new order with internal note
1884 'Transferred from BOOKSELLER on DATE'.
1885 Move all attached items to the new order.
1886 Received orders cannot be transferred.
1887 Return the ordernumber of created order.
1892 my ($ordernumber, $basketno) = @_;
1894 return unless ($ordernumber and $basketno);
1896 my $order = GetOrder
( $ordernumber );
1897 return if $order->{datereceived
};
1898 my $basket = GetBasket
($basketno);
1899 return unless $basket;
1901 my $dbh = C4
::Context
->dbh;
1902 my ($query, $sth, $rv);
1906 SET datecancellationprinted = CAST(NOW() AS date), orderstatus = ?
1907 WHERE ordernumber = ?
1909 $sth = $dbh->prepare($query);
1910 $rv = $sth->execute('cancelled', $ordernumber);
1912 delete $order->{'ordernumber'};
1913 delete $order->{parent_ordernumber
};
1914 $order->{'basketno'} = $basketno;
1916 my $newordernumber = Koha
::Acquisition
::Order
->new($order)->insert->{ordernumber
};
1919 UPDATE aqorders_items
1921 WHERE ordernumber = ?
1923 $sth = $dbh->prepare($query);
1924 $sth->execute($newordernumber, $ordernumber);
1927 INSERT INTO aqorders_transfers (ordernumber_from, ordernumber_to)
1930 $sth = $dbh->prepare($query);
1931 $sth->execute($ordernumber, $newordernumber);
1933 return $newordernumber;
1936 =head2 FUNCTIONS ABOUT PARCELS
1940 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1942 get a lists of parcels.
1949 is the bookseller this function has to get parcels.
1952 To know on what criteria the results list has to be ordered.
1955 is the booksellerinvoicenumber.
1957 =item $datefrom & $dateto
1958 to know on what date this function has to filter its search.
1963 a pointer on a hash list containing parcel informations as such :
1969 =item Last operation
1971 =item Number of biblio
1973 =item Number of items
1980 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
1981 my $dbh = C4
::Context
->dbh;
1982 my @query_params = ();
1984 SELECT aqinvoices.invoicenumber,
1985 datereceived,purchaseordernumber,
1986 count(DISTINCT biblionumber) AS biblio,
1987 sum(quantity) AS itemsexpected,
1988 sum(quantityreceived) AS itemsreceived
1989 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
1990 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1991 WHERE aqbasket.booksellerid = ? and datereceived IS NOT NULL
1993 push @query_params, $bookseller;
1995 if ( defined $code ) {
1996 $strsth .= ' and aqinvoices.invoicenumber like ? ';
1997 # add a % to the end of the code to allow stemming.
1998 push @query_params, "$code%";
2001 if ( defined $datefrom ) {
2002 $strsth .= ' and datereceived >= ? ';
2003 push @query_params, $datefrom;
2006 if ( defined $dateto ) {
2007 $strsth .= 'and datereceived <= ? ';
2008 push @query_params, $dateto;
2011 $strsth .= "group by aqinvoices.invoicenumber,datereceived ";
2013 # can't use a placeholder to place this column name.
2014 # but, we could probably be checking to make sure it is a column that will be fetched.
2015 $strsth .= "order by $order " if ($order);
2017 my $sth = $dbh->prepare($strsth);
2019 $sth->execute( @query_params );
2020 my $results = $sth->fetchall_arrayref({});
2024 #------------------------------------------------------------#
2026 =head3 GetLateOrders
2028 @results = &GetLateOrders;
2030 Searches for bookseller with late orders.
2033 the table of supplier with late issues. This table is full of hashref.
2039 my $supplierid = shift;
2041 my $estimateddeliverydatefrom = shift;
2042 my $estimateddeliverydateto = shift;
2044 my $dbh = C4
::Context
->dbh;
2046 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
2047 my $dbdriver = C4
::Context
->config("db_scheme") || "mysql";
2049 my @query_params = ();
2051 SELECT aqbasket.basketno,
2052 aqorders.ordernumber,
2053 DATE(aqbasket.closedate) AS orderdate,
2054 aqbasket.basketname AS basketname,
2055 aqbasket.basketgroupid AS basketgroupid,
2056 aqbasketgroups.name AS basketgroupname,
2057 aqorders.rrp AS unitpricesupplier,
2058 aqorders.ecost AS unitpricelib,
2059 aqorders.claims_count AS claims_count,
2060 aqorders.claimed_date AS claimed_date,
2061 aqbudgets.budget_name AS budget,
2062 borrowers.branchcode AS branch,
2063 aqbooksellers.name AS supplier,
2064 aqbooksellers.id AS supplierid,
2065 biblio.author, biblio.title,
2066 biblioitems.publishercode AS publisher,
2067 biblioitems.publicationyear,
2068 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
2072 aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
2073 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
2074 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
2075 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
2076 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
2077 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
2078 WHERE aqorders.basketno = aqbasket.basketno
2079 AND ( datereceived = ''
2080 OR datereceived IS NULL
2081 OR aqorders.quantityreceived < aqorders.quantity
2083 AND aqbasket.closedate IS NOT NULL
2084 AND (aqorders.datecancellationprinted IS NULL OR aqorders.datecancellationprinted='0000-00-00')
2087 if ($dbdriver eq "mysql") {
2089 aqorders.quantity - COALESCE(aqorders.quantityreceived,0) AS quantity,
2090 (aqorders.quantity - COALESCE(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
2091 DATEDIFF(CAST(now() AS date),closedate) AS latesince
2093 if ( defined $delay ) {
2094 $from .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) " ;
2095 push @query_params, $delay;
2097 $having = "HAVING quantity <> 0";
2099 # FIXME: account for IFNULL as above
2101 aqorders.quantity AS quantity,
2102 aqorders.quantity * aqorders.rrp AS subtotal,
2103 (CAST(now() AS date) - closedate) AS latesince
2105 if ( defined $delay ) {
2106 $from .= " AND (closedate <= (CAST(now() AS date) -(INTERVAL ? DAY)) ";
2107 push @query_params, $delay;
2110 if (defined $supplierid) {
2111 $from .= ' AND aqbasket.booksellerid = ? ';
2112 push @query_params, $supplierid;
2114 if (defined $branch) {
2115 $from .= ' AND borrowers.branchcode LIKE ? ';
2116 push @query_params, $branch;
2119 if ( defined $estimateddeliverydatefrom or defined $estimateddeliverydateto ) {
2120 $from .= ' AND aqbooksellers.deliverytime IS NOT NULL ';
2122 if ( defined $estimateddeliverydatefrom ) {
2123 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
2124 push @query_params, $estimateddeliverydatefrom;
2126 if ( defined $estimateddeliverydateto ) {
2127 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
2128 push @query_params, $estimateddeliverydateto;
2130 if ( defined $estimateddeliverydatefrom and not defined $estimateddeliverydateto ) {
2131 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
2133 if (C4
::Context
->preference("IndependentBranches")
2134 && !C4
::Context
->IsSuperLibrarian() ) {
2135 $from .= ' AND borrowers.branchcode LIKE ? ';
2136 push @query_params, C4
::Context
->userenv->{branch
};
2138 $from .= " AND orderstatus <> 'cancelled' ";
2139 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
2140 $debug and print STDERR
"GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
2141 my $sth = $dbh->prepare($query);
2142 $sth->execute(@query_params);
2144 while (my $data = $sth->fetchrow_hashref) {
2145 push @results, $data;
2150 #------------------------------------------------------------#
2154 \@order_loop = GetHistory( %params );
2156 Retreives some acquisition history information
2166 basket - search both basket name and number
2167 booksellerinvoicenumber
2170 orderstatus (note that orderstatus '' will retrieve orders
2171 of any status except cancelled)
2173 get_canceled_order (if set to a true value, cancelled orders will
2177 $order_loop is a list of hashrefs that each look like this:
2179 'author' => 'Twain, Mark',
2181 'biblionumber' => '215',
2183 'creationdate' => 'MM/DD/YYYY',
2184 'datereceived' => undef,
2187 'invoicenumber' => undef,
2189 'ordernumber' => '1',
2191 'quantityreceived' => undef,
2192 'title' => 'The Adventures of Huckleberry Finn'
2198 # don't run the query if there are no parameters (list would be too long for sure !)
2199 croak
"No search params" unless @_;
2201 my $title = $params{title
};
2202 my $author = $params{author
};
2203 my $isbn = $params{isbn
};
2204 my $ean = $params{ean
};
2205 my $name = $params{name
};
2206 my $from_placed_on = $params{from_placed_on
};
2207 my $to_placed_on = $params{to_placed_on
};
2208 my $basket = $params{basket
};
2209 my $booksellerinvoicenumber = $params{booksellerinvoicenumber
};
2210 my $basketgroupname = $params{basketgroupname
};
2211 my $budget = $params{budget
};
2212 my $orderstatus = $params{orderstatus
};
2213 my $biblionumber = $params{biblionumber
};
2214 my $get_canceled_order = $params{get_canceled_order
} || 0;
2215 my $ordernumber = $params{ordernumber
};
2216 my $search_children_too = $params{search_children_too
} || 0;
2217 my $created_by = $params{created_by
} || [];
2221 my $total_qtyreceived = 0;
2222 my $total_price = 0;
2224 my $dbh = C4
::Context
->dbh;
2227 COALESCE(biblio.title, deletedbiblio.title) AS title,
2228 COALESCE(biblio.author, deletedbiblio.author) AS author,
2229 COALESCE(biblioitems.isbn, deletedbiblioitems.isbn) AS isbn,
2230 COALESCE(biblioitems.ean, deletedbiblioitems.ean) AS ean,
2232 aqbasket.basketname,
2233 aqbasket.basketgroupid,
2234 aqbasket.authorisedby,
2235 concat( borrowers.firstname,' ',borrowers.surname) AS authorisedbyname,
2236 aqbasketgroups.name as groupname,
2238 aqbasket.creationdate,
2239 aqorders.datereceived,
2241 aqorders.quantityreceived,
2243 aqorders.ordernumber,
2245 aqinvoices.invoicenumber,
2246 aqbooksellers.id as id,
2247 aqorders.biblionumber,
2248 aqorders.orderstatus,
2249 aqorders.parent_ordernumber,
2250 aqbudgets.budget_name
2252 $query .= ", aqbudgets.budget_id AS budget" if defined $budget;
2255 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
2256 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
2257 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
2258 LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
2259 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
2260 LEFT JOIN aqbudgets ON aqorders.budget_id=aqbudgets.budget_id
2261 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
2262 LEFT JOIN deletedbiblio ON deletedbiblio.biblionumber=aqorders.biblionumber
2263 LEFT JOIN deletedbiblioitems ON deletedbiblioitems.biblionumber=aqorders.biblionumber
2264 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
2267 $query .= " WHERE 1 ";
2269 unless ($get_canceled_order or (defined $orderstatus and $orderstatus eq 'cancelled')) {
2270 $query .= " AND (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
2273 my @query_params = ();
2275 if ( $biblionumber ) {
2276 $query .= " AND biblio.biblionumber = ?";
2277 push @query_params, $biblionumber;
2281 $query .= " AND biblio.title LIKE ? ";
2282 $title =~ s/\s+/%/g;
2283 push @query_params, "%$title%";
2287 $query .= " AND biblio.author LIKE ? ";
2288 push @query_params, "%$author%";
2292 $query .= " AND biblioitems.isbn LIKE ? ";
2293 push @query_params, "%$isbn%";
2296 $query .= " AND biblioitems.ean = ? ";
2297 push @query_params, "$ean";
2300 $query .= " AND aqbooksellers.name LIKE ? ";
2301 push @query_params, "%$name%";
2305 $query .= " AND aqbudgets.budget_id = ? ";
2306 push @query_params, "$budget";
2309 if ( $from_placed_on ) {
2310 $query .= " AND creationdate >= ? ";
2311 push @query_params, $from_placed_on;
2314 if ( $to_placed_on ) {
2315 $query .= " AND creationdate <= ? ";
2316 push @query_params, $to_placed_on;
2319 if ( defined $orderstatus and $orderstatus ne '') {
2320 $query .= " AND aqorders.orderstatus = ? ";
2321 push @query_params, "$orderstatus";
2325 if ($basket =~ m/^\d+$/) {
2326 $query .= " AND aqorders.basketno = ? ";
2327 push @query_params, $basket;
2329 $query .= " AND aqbasket.basketname LIKE ? ";
2330 push @query_params, "%$basket%";
2334 if ($booksellerinvoicenumber) {
2335 $query .= " AND aqinvoices.invoicenumber LIKE ? ";
2336 push @query_params, "%$booksellerinvoicenumber%";
2339 if ($basketgroupname) {
2340 $query .= " AND aqbasketgroups.name LIKE ? ";
2341 push @query_params, "%$basketgroupname%";
2345 $query .= " AND (aqorders.ordernumber = ? ";
2346 push @query_params, $ordernumber;
2347 if ($search_children_too) {
2348 $query .= " OR aqorders.parent_ordernumber = ? ";
2349 push @query_params, $ordernumber;
2354 if ( @
$created_by ) {
2355 $query .= ' AND aqbasket.authorisedby IN ( ' . join( ',', ('?') x @
$created_by ) . ')';
2356 push @query_params, @
$created_by;
2360 if ( C4
::Context
->preference("IndependentBranches") ) {
2361 unless ( C4
::Context
->IsSuperLibrarian() ) {
2362 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
2363 push @query_params, C4
::Context
->userenv->{branch
};
2366 $query .= " ORDER BY id";
2368 return $dbh->selectall_arrayref( $query, { Slice
=> {} }, @query_params );
2371 =head2 GetRecentAcqui
2373 $results = GetRecentAcqui($days);
2375 C<$results> is a ref to a table which containts hashref
2379 sub GetRecentAcqui
{
2381 my $dbh = C4
::Context
->dbh;
2385 ORDER BY timestamp DESC
2388 my $sth = $dbh->prepare($query);
2390 my $results = $sth->fetchall_arrayref({});
2394 #------------------------------------------------------------#
2398 &AddClaim($ordernumber);
2400 Add a claim for an order
2405 my ($ordernumber) = @_;
2406 my $dbh = C4
::Context
->dbh;
2409 claims_count = claims_count + 1,
2410 claimed_date = CURDATE()
2411 WHERE ordernumber = ?
2413 my $sth = $dbh->prepare($query);
2414 $sth->execute($ordernumber);
2419 my @invoices = GetInvoices(
2420 invoicenumber => $invoicenumber,
2421 supplierid => $supplierid,
2422 suppliername => $suppliername,
2423 shipmentdatefrom => $shipmentdatefrom, # ISO format
2424 shipmentdateto => $shipmentdateto, # ISO format
2425 billingdatefrom => $billingdatefrom, # ISO format
2426 billingdateto => $billingdateto, # ISO format
2427 isbneanissn => $isbn_or_ean_or_issn,
2430 publisher => $publisher,
2431 publicationyear => $publicationyear,
2432 branchcode => $branchcode,
2433 order_by => $order_by
2436 Return a list of invoices that match all given criteria.
2438 $order_by is "column_name (asc|desc)", where column_name is any of
2439 'invoicenumber', 'booksellerid', 'shipmentdate', 'billingdate', 'closedate',
2440 'shipmentcost', 'shipmentcost_budgetid'.
2442 asc is the default if omitted
2449 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2450 closedate shipmentcost shipmentcost_budgetid);
2452 my $dbh = C4
::Context
->dbh;
2454 SELECT aqinvoices
.*, aqbooksellers
.name AS suppliername
,
2457 aqorders
.datereceived IS NOT NULL
,
2458 aqorders
.biblionumber
,
2461 ) AS receivedbiblios
,
2464 aqorders
.subscriptionid IS NOT NULL
,
2465 aqorders
.subscriptionid
,
2468 ) AS is_linked_to_subscriptions
,
2469 SUM
(aqorders
.quantityreceived
) AS receiveditems
2471 LEFT JOIN aqbooksellers ON aqbooksellers
.id
= aqinvoices
.booksellerid
2472 LEFT JOIN aqorders ON aqorders
.invoiceid
= aqinvoices
.invoiceid
2473 LEFT JOIN aqbasket ON aqbasket
.basketno
=aqorders
.basketno
2474 LEFT JOIN borrowers ON aqbasket
.authorisedby
=borrowers
.borrowernumber
2475 LEFT JOIN biblio ON aqorders
.biblionumber
= biblio
.biblionumber
2476 LEFT JOIN biblioitems ON biblio
.biblionumber
= biblioitems
.biblionumber
2477 LEFT JOIN subscription ON biblio
.biblionumber
= subscription
.biblionumber
2482 if($args{supplierid
}) {
2483 push @bind_strs, " aqinvoices.booksellerid = ? ";
2484 push @bind_args, $args{supplierid
};
2486 if($args{invoicenumber
}) {
2487 push @bind_strs, " aqinvoices.invoicenumber LIKE ? ";
2488 push @bind_args, "%$args{invoicenumber}%";
2490 if($args{suppliername
}) {
2491 push @bind_strs, " aqbooksellers.name LIKE ? ";
2492 push @bind_args, "%$args{suppliername}%";
2494 if($args{shipmentdatefrom
}) {
2495 push @bind_strs, " aqinvoices.shipmentdate >= ? ";
2496 push @bind_args, $args{shipmentdatefrom
};
2498 if($args{shipmentdateto
}) {
2499 push @bind_strs, " aqinvoices.shipmentdate <= ? ";
2500 push @bind_args, $args{shipmentdateto
};
2502 if($args{billingdatefrom
}) {
2503 push @bind_strs, " aqinvoices.billingdate >= ? ";
2504 push @bind_args, $args{billingdatefrom
};
2506 if($args{billingdateto
}) {
2507 push @bind_strs, " aqinvoices.billingdate <= ? ";
2508 push @bind_args, $args{billingdateto
};
2510 if($args{isbneanissn
}) {
2511 push @bind_strs, " (biblioitems.isbn LIKE CONCAT('%', ?, '%') OR biblioitems.ean LIKE CONCAT('%', ?, '%') OR biblioitems.issn LIKE CONCAT('%', ?, '%') ) ";
2512 push @bind_args, $args{isbneanissn
}, $args{isbneanissn
}, $args{isbneanissn
};
2515 push @bind_strs, " biblio.title LIKE CONCAT('%', ?, '%') ";
2516 push @bind_args, $args{title
};
2519 push @bind_strs, " biblio.author LIKE CONCAT('%', ?, '%') ";
2520 push @bind_args, $args{author
};
2522 if($args{publisher
}) {
2523 push @bind_strs, " biblioitems.publishercode LIKE CONCAT('%', ?, '%') ";
2524 push @bind_args, $args{publisher
};
2526 if($args{publicationyear
}) {
2527 push @bind_strs, " ((biblioitems.publicationyear LIKE CONCAT('%', ?, '%')) OR (biblio.copyrightdate LIKE CONCAT('%', ?, '%'))) ";
2528 push @bind_args, $args{publicationyear
}, $args{publicationyear
};
2530 if($args{branchcode
}) {
2531 push @bind_strs, " borrowers.branchcode = ? ";
2532 push @bind_args, $args{branchcode
};
2534 if($args{message_id
}) {
2535 push @bind_strs, " aqinvoices.message_id = ? ";
2536 push @bind_args, $args{message_id
};
2539 $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
2540 $query .= " GROUP BY aqinvoices.invoiceid ";
2542 if($args{order_by
}) {
2543 my ($column, $direction) = split / /, $args{order_by
};
2544 if(grep /^$column$/, @columns) {
2545 $direction ||= 'ASC';
2546 $query .= " ORDER BY $column $direction";
2550 my $sth = $dbh->prepare($query);
2551 $sth->execute(@bind_args);
2553 my $results = $sth->fetchall_arrayref({});
2559 my $invoice = GetInvoice($invoiceid);
2561 Get informations about invoice with given $invoiceid
2563 Return a hash filled with aqinvoices.* fields
2568 my ($invoiceid) = @_;
2571 return unless $invoiceid;
2573 my $dbh = C4
::Context
->dbh;
2579 my $sth = $dbh->prepare($query);
2580 $sth->execute($invoiceid);
2582 $invoice = $sth->fetchrow_hashref;
2586 =head3 GetInvoiceDetails
2588 my $invoice = GetInvoiceDetails($invoiceid)
2590 Return informations about an invoice + the list of related order lines
2592 Orders informations are in $invoice->{orders} (array ref)
2596 sub GetInvoiceDetails
{
2597 my ($invoiceid) = @_;
2599 if ( !defined $invoiceid ) {
2600 carp
'GetInvoiceDetails called without an invoiceid';
2604 my $dbh = C4
::Context
->dbh;
2606 SELECT aqinvoices.*, aqbooksellers.name AS suppliername
2608 LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
2611 my $sth = $dbh->prepare($query);
2612 $sth->execute($invoiceid);
2614 my $invoice = $sth->fetchrow_hashref;
2619 biblio.copyrightdate,
2620 biblioitems.publishercode,
2621 biblioitems.publicationyear,
2622 aqbasket.basketname,
2623 aqbasketgroups.id AS basketgroupid,
2624 aqbasketgroups.name AS basketgroupname
2626 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
2627 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
2628 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2629 LEFT JOIN biblioitems ON aqorders.biblionumber = biblioitems.biblionumber
2632 $sth = $dbh->prepare($query);
2633 $sth->execute($invoiceid);
2634 $invoice->{orders
} = $sth->fetchall_arrayref({});
2635 $invoice->{orders
} ||= []; # force an empty arrayref if fetchall_arrayref fails
2642 my $invoiceid = AddInvoice(
2643 invoicenumber => $invoicenumber,
2644 booksellerid => $booksellerid,
2645 shipmentdate => $shipmentdate,
2646 billingdate => $billingdate,
2647 closedate => $closedate,
2648 shipmentcost => $shipmentcost,
2649 shipmentcost_budgetid => $shipmentcost_budgetid
2652 Create a new invoice and return its id or undef if it fails.
2659 return unless(%invoice and $invoice{invoicenumber
});
2661 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2662 closedate shipmentcost shipmentcost_budgetid message_id);
2666 foreach my $key (keys %invoice) {
2667 if(0 < grep(/^$key$/, @columns)) {
2668 push @set_strs, "$key = ?";
2669 push @set_args, ($invoice{$key} || undef);
2675 my $dbh = C4
::Context
->dbh;
2676 my $query = "INSERT INTO aqinvoices SET ";
2677 $query .= join (",", @set_strs);
2678 my $sth = $dbh->prepare($query);
2679 $rv = $sth->execute(@set_args);
2681 $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
2690 invoiceid => $invoiceid, # Mandatory
2691 invoicenumber => $invoicenumber,
2692 booksellerid => $booksellerid,
2693 shipmentdate => $shipmentdate,
2694 billingdate => $billingdate,
2695 closedate => $closedate,
2696 shipmentcost => $shipmentcost,
2697 shipmentcost_budgetid => $shipmentcost_budgetid
2700 Modify an invoice, invoiceid is mandatory.
2702 Return undef if it fails.
2709 return unless(%invoice and $invoice{invoiceid
});
2711 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2712 closedate shipmentcost shipmentcost_budgetid);
2716 foreach my $key (keys %invoice) {
2717 if(0 < grep(/^$key$/, @columns)) {
2718 push @set_strs, "$key = ?";
2719 push @set_args, ($invoice{$key} || undef);
2723 my $dbh = C4
::Context
->dbh;
2724 my $query = "UPDATE aqinvoices SET ";
2725 $query .= join(",", @set_strs);
2726 $query .= " WHERE invoiceid = ?";
2728 my $sth = $dbh->prepare($query);
2729 $sth->execute(@set_args, $invoice{invoiceid
});
2734 CloseInvoice($invoiceid);
2738 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
2743 my ($invoiceid) = @_;
2745 return unless $invoiceid;
2747 my $dbh = C4
::Context
->dbh;
2750 SET closedate
= CAST
(NOW
() AS DATE
)
2753 my $sth = $dbh->prepare($query);
2754 $sth->execute($invoiceid);
2757 =head3 ReopenInvoice
2759 ReopenInvoice($invoiceid);
2763 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => output_pref({ dt=>dt_from_string, dateonly=>1, otputpref=>'iso' }))
2768 my ($invoiceid) = @_;
2770 return unless $invoiceid;
2772 my $dbh = C4
::Context
->dbh;
2775 SET closedate
= NULL
2778 my $sth = $dbh->prepare($query);
2779 $sth->execute($invoiceid);
2784 DelInvoice($invoiceid);
2786 Delete an invoice if there are no items attached to it.
2791 my ($invoiceid) = @_;
2793 return unless $invoiceid;
2795 my $dbh = C4
::Context
->dbh;
2801 my $sth = $dbh->prepare($query);
2802 $sth->execute($invoiceid);
2803 my $res = $sth->fetchrow_arrayref;
2804 if ( $res && $res->[0] == 0 ) {
2806 DELETE FROM aqinvoices
2809 my $sth = $dbh->prepare($query);
2810 return ( $sth->execute($invoiceid) > 0 );
2815 =head3 MergeInvoices
2817 MergeInvoices($invoiceid, \@sourceids);
2819 Merge the invoices identified by the IDs in \@sourceids into
2820 the invoice identified by $invoiceid.
2825 my ($invoiceid, $sourceids) = @_;
2827 return unless $invoiceid;
2828 foreach my $sourceid (@
$sourceids) {
2829 next if $sourceid == $invoiceid;
2830 my $source = GetInvoiceDetails
($sourceid);
2831 foreach my $order (@
{$source->{'orders'}}) {
2832 $order->{'invoiceid'} = $invoiceid;
2835 DelInvoice
($source->{'invoiceid'});
2840 =head3 GetBiblioCountByBasketno
2842 $biblio_count = &GetBiblioCountByBasketno($basketno);
2844 Looks up the biblio's count that has basketno value $basketno
2850 sub GetBiblioCountByBasketno
{
2851 my ($basketno) = @_;
2852 my $dbh = C4
::Context
->dbh;
2854 SELECT COUNT( DISTINCT( biblionumber ) )
2857 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
2860 my $sth = $dbh->prepare($query);
2861 $sth->execute($basketno);
2862 return $sth->fetchrow;
2865 # Note this subroutine should be moved to Koha::Acquisition::Order
2866 # Will do when a DBIC decision will be taken.
2867 sub populate_order_with_prices
{
2870 my $order = $params->{order
};
2871 my $booksellerid = $params->{booksellerid
};
2872 return unless $booksellerid;
2874 my $bookseller = Koha
::Acquisition
::Booksellers
->find( $booksellerid );
2876 my $receiving = $params->{receiving
};
2877 my $ordering = $params->{ordering
};
2878 my $discount = $order->{discount
};
2879 $discount /= 100 if $discount > 1;
2882 $order->{tax_rate_on_ordering
} //= $order->{tax_rate
};
2883 if ( $bookseller->listincgst ) {
2884 # The user entered the rrp tax included
2885 $order->{rrp_tax_included
} = $order->{rrp
};
2887 # rrp tax excluded = rrp tax included / ( 1 + tax rate )
2888 $order->{rrp_tax_excluded
} = $order->{rrp_tax_included
} / ( 1 + $order->{tax_rate_on_ordering
} );
2890 # ecost tax excluded = rrp tax excluded * ( 1 - discount )
2891 $order->{ecost_tax_excluded
} = $order->{rrp_tax_excluded
} * ( 1 - $discount );
2893 # ecost tax included = rrp tax included ( 1 - discount )
2894 $order->{ecost_tax_included
} = $order->{rrp_tax_included
} * ( 1 - $discount );
2897 # The user entered the rrp tax excluded
2898 $order->{rrp_tax_excluded
} = $order->{rrp
};
2900 # rrp tax included = rrp tax excluded * ( 1 - tax rate )
2901 $order->{rrp_tax_included
} = $order->{rrp_tax_excluded
} * ( 1 + $order->{tax_rate_on_ordering
} );
2903 # ecost tax excluded = rrp tax excluded * ( 1 - discount )
2904 $order->{ecost_tax_excluded
} = $order->{rrp_tax_excluded
} * ( 1 - $discount );
2906 # ecost tax included = rrp tax excluded * ( 1 - tax rate ) * ( 1 - discount )
2907 $order->{ecost_tax_included
} =
2908 $order->{rrp_tax_excluded
} *
2909 ( 1 + $order->{tax_rate_on_ordering
} ) *
2913 # tax value = quantity * ecost tax excluded * tax rate
2914 $order->{tax_value_on_ordering
} =
2915 $order->{quantity
} * $order->{ecost_tax_excluded
} * $order->{tax_rate_on_ordering
};
2919 $order->{tax_rate_on_receiving
} //= $order->{tax_rate
};
2920 if ( $bookseller->invoiceincgst ) {
2921 # Trick for unitprice. If the unit price rounded value is the same as the ecost rounded value
2922 # we need to keep the exact ecost value
2923 if ( Koha
::Number
::Price
->new( $order->{unitprice
} )->round == Koha
::Number
::Price
->new( $order->{ecost_tax_included
} )->round ) {
2924 $order->{unitprice
} = $order->{ecost_tax_included
};
2927 # The user entered the unit price tax included
2928 $order->{unitprice_tax_included
} = $order->{unitprice
};
2930 # unit price tax excluded = unit price tax included / ( 1 + tax rate )
2931 $order->{unitprice_tax_excluded
} = $order->{unitprice_tax_included
} / ( 1 + $order->{tax_rate_on_receiving
} );
2934 # Trick for unitprice. If the unit price rounded value is the same as the ecost rounded value
2935 # we need to keep the exact ecost value
2936 if ( Koha
::Number
::Price
->new( $order->{unitprice
} )->round == Koha
::Number
::Price
->new( $order->{ecost_tax_excluded
} )->round ) {
2937 $order->{unitprice
} = $order->{ecost_tax_excluded
};
2940 # The user entered the unit price tax excluded
2941 $order->{unitprice_tax_excluded
} = $order->{unitprice
};
2944 # unit price tax included = unit price tax included * ( 1 + tax rate )
2945 $order->{unitprice_tax_included
} = $order->{unitprice_tax_excluded
} * ( 1 + $order->{tax_rate_on_receiving
} );
2948 # tax value = quantity * unit price tax excluded * tax rate
2949 $order->{tax_value_on_receiving
} = $order->{quantity
} * $order->{unitprice_tax_excluded
} * $order->{tax_rate_on_receiving
};
2955 =head3 GetOrderUsers
2957 $order_users_ids = &GetOrderUsers($ordernumber);
2959 Returns a list of all borrowernumbers that are in order users list
2964 my ($ordernumber) = @_;
2966 return unless $ordernumber;
2969 SELECT borrowernumber
2971 WHERE ordernumber
= ?
2973 my $dbh = C4
::Context
->dbh;
2974 my $sth = $dbh->prepare($query);
2975 $sth->execute($ordernumber);
2976 my $results = $sth->fetchall_arrayref( {} );
2978 my @borrowernumbers;
2979 foreach (@
$results) {
2980 push @borrowernumbers, $_->{'borrowernumber'};
2983 return @borrowernumbers;
2986 =head3 ModOrderUsers
2988 my @order_users_ids = (1, 2, 3);
2989 &ModOrderUsers($ordernumber, @basketusers_ids);
2991 Delete all users from order users list, and add users in C<@order_users_ids>
2997 my ( $ordernumber, @order_users_ids ) = @_;
2999 return unless $ordernumber;
3001 my $dbh = C4
::Context
->dbh;
3003 DELETE FROM aqorder_users
3004 WHERE ordernumber
= ?
3006 my $sth = $dbh->prepare($query);
3007 $sth->execute($ordernumber);
3010 INSERT INTO aqorder_users
(ordernumber
, borrowernumber
)
3013 $sth = $dbh->prepare($query);
3014 foreach my $order_user_id (@order_users_ids) {
3015 $sth->execute( $ordernumber, $order_user_id );
3019 sub NotifyOrderUsers
{
3020 my ($ordernumber) = @_;
3022 my @borrowernumbers = GetOrderUsers
($ordernumber);
3023 return unless @borrowernumbers;
3025 my $order = GetOrder
( $ordernumber );
3026 for my $borrowernumber (@borrowernumbers) {
3027 my $borrower = C4
::Members
::GetMember
( borrowernumber
=> $borrowernumber );
3028 my $library = Koha
::Libraries
->find( $borrower->{branchcode
} )->unblessed;
3029 my $biblio = C4
::Biblio
::GetBiblio
( $order->{biblionumber
} );
3030 my $letter = C4
::Letters
::GetPreparedLetter
(
3031 module
=> 'acquisition',
3032 letter_code
=> 'ACQ_NOTIF_ON_RECEIV',
3033 branchcode
=> $library->{branchcode
},
3034 lang
=> $borrower->{lang
},
3036 'branches' => $library,
3037 'borrowers' => $borrower,
3038 'biblio' => $biblio,
3039 'aqorders' => $order,
3043 C4
::Letters
::EnqueueLetter
(
3046 borrowernumber
=> $borrowernumber,
3047 LibraryName
=> C4
::Context
->preference("LibraryName"),
3048 message_transport_type
=> 'email',
3050 ) or warn "can't enqueue letter $letter";
3055 =head3 FillWithDefaultValues
3057 FillWithDefaultValues( $marc_record );
3059 This will update the record with default value defined in the ACQ framework.
3060 For all existing fields, if a default value exists and there are no subfield, it will be created.
3061 If the field does not exist, it will be created too.
3065 sub FillWithDefaultValues
{
3067 my $tagslib = C4
::Biblio
::GetMarcStructure
( 1, 'ACQ', { unsafe
=> 1 } );
3070 C4
::Biblio
::GetMarcFromKohaField
( 'items.itemnumber', '' );
3071 for my $tag ( sort keys %$tagslib ) {
3073 next if $tag == $itemfield;
3074 for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
3075 next if IsMarcStructureInternal
($tagslib->{$tag}{$subfield});
3076 my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue
};
3077 if ( defined $defaultvalue and $defaultvalue ne '' ) {
3078 my @fields = $record->field($tag);
3080 for my $field (@fields) {
3081 unless ( defined $field->subfield($subfield) ) {
3082 $field->add_subfields(
3083 $subfield => $defaultvalue );
3088 $record->insert_fields_ordered(
3090 $tag, '', '', $subfield => $defaultvalue
3105 Koha Development Team <http://koha-community.org/>