Bug 13603 - autoBarcode setting hbyymmincr not taking month into account when looking...
[koha.git] / C4 / Acquisition.pm
blob15f69c004f27242dcd3ae96da46dec5627e27b80
1 package C4::Acquisition;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 use Modern::Perl;
22 use Carp;
23 use C4::Context;
24 use C4::Debug;
25 use C4::Dates qw(format_date format_date_in_iso);
26 use MARC::Record;
27 use C4::Suggestions;
28 use C4::Biblio;
29 use C4::Contract;
30 use C4::Debug;
31 use C4::Templates qw(gettemplate);
32 use Koha::DateUtils qw( dt_from_string output_pref );
33 use Koha::Acquisition::Order;
34 use Koha::Acquisition::Bookseller;
35 use Koha::Number::Price;
37 use Time::localtime;
38 use HTML::Entities;
40 use vars qw($VERSION @ISA @EXPORT);
42 BEGIN {
43 # set the version for version checking
44 $VERSION = 3.07.00.049;
45 require Exporter;
46 @ISA = qw(Exporter);
47 @EXPORT = qw(
48 &GetBasket &NewBasket &CloseBasket &ReopenBasket &DelBasket &ModBasket
49 &GetBasketAsCSV &GetBasketGroupAsCSV
50 &GetBasketsByBookseller &GetBasketsByBasketgroup
51 &GetBasketsInfosByBookseller
53 &GetBasketUsers &ModBasketUsers
54 &CanUserManageBasket
56 &ModBasketHeader
58 &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
59 &GetBasketgroups &ReOpenBasketgroup
61 &DelOrder &ModOrder &GetOrder &GetOrders &GetOrdersByBiblionumber
62 &GetLateOrders &GetOrderFromItemnumber
63 &SearchOrders &GetHistory &GetRecentAcqui
64 &ModReceiveOrder &CancelReceipt
65 &GetCancelledOrders &TransferOrder
66 &GetLastOrderNotReceivedFromSubscriptionid &GetLastOrderReceivedFromSubscriptionid
67 &ModItemOrder
69 &GetParcels
71 &GetInvoices
72 &GetInvoice
73 &GetInvoiceDetails
74 &AddInvoice
75 &ModInvoice
76 &CloseInvoice
77 &ReopenInvoice
78 &DelInvoice
79 &MergeInvoices
81 &GetItemnumbersFromOrder
83 &AddClaim
84 &GetBiblioCountByBasketno
92 sub GetOrderFromItemnumber {
93 my ($itemnumber) = @_;
94 my $dbh = C4::Context->dbh;
95 my $query = qq|
97 SELECT * from aqorders LEFT JOIN aqorders_items
98 ON ( aqorders.ordernumber = aqorders_items.ordernumber )
99 WHERE itemnumber = ? |;
101 my $sth = $dbh->prepare($query);
103 # $sth->trace(3);
105 $sth->execute($itemnumber);
107 my $order = $sth->fetchrow_hashref;
108 return ( $order );
112 # Returns the itemnumber(s) associated with the ordernumber given in parameter
113 sub GetItemnumbersFromOrder {
114 my ($ordernumber) = @_;
115 my $dbh = C4::Context->dbh;
116 my $query = "SELECT itemnumber FROM aqorders_items WHERE ordernumber=?";
117 my $sth = $dbh->prepare($query);
118 $sth->execute($ordernumber);
119 my @tab;
121 while (my $order = $sth->fetchrow_hashref) {
122 push @tab, $order->{'itemnumber'};
125 return @tab;
134 =head1 NAME
136 C4::Acquisition - Koha functions for dealing with orders and acquisitions
138 =head1 SYNOPSIS
140 use C4::Acquisition;
142 =head1 DESCRIPTION
144 The functions in this module deal with acquisitions, managing book
145 orders, basket and parcels.
147 =head1 FUNCTIONS
149 =head2 FUNCTIONS ABOUT BASKETS
151 =head3 GetBasket
153 $aqbasket = &GetBasket($basketnumber);
155 get all basket informations in aqbasket for a given basket
157 B<returns:> informations for a given basket returned as a hashref.
159 =cut
161 sub GetBasket {
162 my ($basketno) = @_;
163 my $dbh = C4::Context->dbh;
164 my $query = "
165 SELECT aqbasket.*,
166 concat( b.firstname,' ',b.surname) AS authorisedbyname
167 FROM aqbasket
168 LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
169 WHERE basketno=?
171 my $sth=$dbh->prepare($query);
172 $sth->execute($basketno);
173 my $basket = $sth->fetchrow_hashref;
174 return ( $basket );
177 #------------------------------------------------------------#
179 =head3 NewBasket
181 $basket = &NewBasket( $booksellerid, $authorizedby, $basketname,
182 $basketnote, $basketbooksellernote, $basketcontractnumber, $deliveryplace, $billingplace );
184 Create a new basket in aqbasket table
186 =over
188 =item C<$booksellerid> is a foreign key in the aqbasket table
190 =item C<$authorizedby> is the username of who created the basket
192 =back
194 The other parameters are optional, see ModBasketHeader for more info on them.
196 =cut
198 sub NewBasket {
199 my ( $booksellerid, $authorisedby, $basketname, $basketnote,
200 $basketbooksellernote, $basketcontractnumber, $deliveryplace,
201 $billingplace ) = @_;
202 my $dbh = C4::Context->dbh;
203 my $query =
204 'INSERT INTO aqbasket (creationdate,booksellerid,authorisedby) '
205 . 'VALUES (now(),?,?)';
206 $dbh->do( $query, {}, $booksellerid, $authorisedby );
208 my $basket = $dbh->{mysql_insertid};
209 $basketname ||= q{}; # default to empty strings
210 $basketnote ||= q{};
211 $basketbooksellernote ||= q{};
212 ModBasketHeader( $basket, $basketname, $basketnote, $basketbooksellernote,
213 $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace );
214 return $basket;
217 #------------------------------------------------------------#
219 =head3 CloseBasket
221 &CloseBasket($basketno);
223 close a basket (becomes unmodifiable, except for receives)
225 =cut
227 sub CloseBasket {
228 my ($basketno) = @_;
229 my $dbh = C4::Context->dbh;
230 my $query = "
231 UPDATE aqbasket
232 SET closedate=now()
233 WHERE basketno=?
235 my $sth = $dbh->prepare($query);
236 $sth->execute($basketno);
238 my @orders = GetOrders($basketno);
239 foreach my $order (@orders) {
240 $query = qq{
241 UPDATE aqorders
242 SET orderstatus = 'ordered'
243 WHERE ordernumber = ?;
245 $sth = $dbh->prepare($query);
246 $sth->execute($order->{'ordernumber'});
250 =head3 ReopenBasket
252 &ReopenBasket($basketno);
254 reopen a basket
256 =cut
258 sub ReopenBasket {
259 my ($basketno) = @_;
260 my $dbh = C4::Context->dbh;
261 my $query = "
262 UPDATE aqbasket
263 SET closedate=NULL
264 WHERE basketno=?
266 my $sth = $dbh->prepare($query);
267 $sth->execute($basketno);
269 my @orders = GetOrders($basketno);
270 foreach my $order (@orders) {
271 $query = qq{
272 UPDATE aqorders
273 SET orderstatus = 'new'
274 WHERE ordernumber = ?;
276 $sth = $dbh->prepare($query);
277 $sth->execute($order->{'ordernumber'});
281 #------------------------------------------------------------#
283 =head3 GetBasketAsCSV
285 &GetBasketAsCSV($basketno);
287 Export a basket as CSV
289 $cgi parameter is needed for column name translation
291 =cut
293 sub GetBasketAsCSV {
294 my ($basketno, $cgi) = @_;
295 my $basket = GetBasket($basketno);
296 my @orders = GetOrders($basketno);
297 my $contract = GetContract({
298 contractnumber => $basket->{'contractnumber'}
301 my $template = C4::Templates::gettemplate("acqui/csv/basket.tt", "intranet", $cgi);
303 my @rows;
304 foreach my $order (@orders) {
305 my $bd = GetBiblioData( $order->{'biblionumber'} );
306 my $row = {
307 contractname => $contract->{'contractname'},
308 ordernumber => $order->{'ordernumber'},
309 entrydate => $order->{'entrydate'},
310 isbn => $order->{'isbn'},
311 author => $bd->{'author'},
312 title => $bd->{'title'},
313 publicationyear => $bd->{'publicationyear'},
314 publishercode => $bd->{'publishercode'},
315 collectiontitle => $bd->{'collectiontitle'},
316 notes => $order->{'order_vendornote'},
317 quantity => $order->{'quantity'},
318 rrp => $order->{'rrp'},
319 deliveryplace => C4::Branch::GetBranchName( $basket->{'deliveryplace'} ),
320 billingplace => C4::Branch::GetBranchName( $basket->{'billingplace'} ),
322 foreach(qw(
323 contractname author title publishercode collectiontitle notes
324 deliveryplace billingplace
325 ) ) {
326 # Double the quotes to not be interpreted as a field end
327 $row->{$_} =~ s/"/""/g if $row->{$_};
329 push @rows, $row;
332 @rows = sort {
333 if(defined $a->{publishercode} and defined $b->{publishercode}) {
334 $a->{publishercode} cmp $b->{publishercode};
336 } @rows;
338 $template->param(rows => \@rows);
340 return $template->output;
344 =head3 GetBasketGroupAsCSV
346 &GetBasketGroupAsCSV($basketgroupid);
348 Export a basket group as CSV
350 $cgi parameter is needed for column name translation
352 =cut
354 sub GetBasketGroupAsCSV {
355 my ($basketgroupid, $cgi) = @_;
356 my $baskets = GetBasketsByBasketgroup($basketgroupid);
358 my $template = C4::Templates::gettemplate('acqui/csv/basketgroup.tt', 'intranet', $cgi);
360 my @rows;
361 for my $basket (@$baskets) {
362 my @orders = GetOrders( $basket->{basketno} );
363 my $contract = GetContract({
364 contractnumber => $basket->{contractnumber}
366 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $basket->{booksellerid} });
367 my $basketgroup = GetBasketgroup( $$basket{basketgroupid} );
369 foreach my $order (@orders) {
370 my $bd = GetBiblioData( $order->{'biblionumber'} );
371 my $row = {
372 clientnumber => $bookseller->{accountnumber},
373 basketname => $basket->{basketname},
374 ordernumber => $order->{ordernumber},
375 author => $bd->{author},
376 title => $bd->{title},
377 publishercode => $bd->{publishercode},
378 publicationyear => $bd->{publicationyear},
379 collectiontitle => $bd->{collectiontitle},
380 isbn => $order->{isbn},
381 quantity => $order->{quantity},
382 rrp => $order->{rrp},
383 discount => $bookseller->{discount},
384 ecost => $order->{ecost},
385 notes => $order->{order_vendornote},
386 entrydate => $order->{entrydate},
387 booksellername => $bookseller->{name},
388 bookselleraddress => $bookseller->{address1},
389 booksellerpostal => $bookseller->{postal},
390 contractnumber => $contract->{contractnumber},
391 contractname => $contract->{contractname},
392 basketgroupdeliveryplace => C4::Branch::GetBranchName( $basketgroup->{deliveryplace} ),
393 basketgroupbillingplace => C4::Branch::GetBranchName( $basketgroup->{billingplace} ),
394 basketdeliveryplace => C4::Branch::GetBranchName( $basket->{deliveryplace} ),
395 basketbillingplace => C4::Branch::GetBranchName( $basket->{billingplace} ),
397 foreach(qw(
398 basketname author title publishercode collectiontitle notes
399 booksellername bookselleraddress booksellerpostal contractname
400 basketgroupdeliveryplace basketgroupbillingplace
401 basketdeliveryplace basketbillingplace
402 ) ) {
403 # Double the quotes to not be interpreted as a field end
404 $row->{$_} =~ s/"/""/g if $row->{$_};
406 push @rows, $row;
409 $template->param(rows => \@rows);
411 return $template->output;
415 =head3 CloseBasketgroup
417 &CloseBasketgroup($basketgroupno);
419 close a basketgroup
421 =cut
423 sub CloseBasketgroup {
424 my ($basketgroupno) = @_;
425 my $dbh = C4::Context->dbh;
426 my $sth = $dbh->prepare("
427 UPDATE aqbasketgroups
428 SET closed=1
429 WHERE id=?
431 $sth->execute($basketgroupno);
434 #------------------------------------------------------------#
436 =head3 ReOpenBaskergroup($basketgroupno)
438 &ReOpenBaskergroup($basketgroupno);
440 reopen a basketgroup
442 =cut
444 sub ReOpenBasketgroup {
445 my ($basketgroupno) = @_;
446 my $dbh = C4::Context->dbh;
447 my $sth = $dbh->prepare("
448 UPDATE aqbasketgroups
449 SET closed=0
450 WHERE id=?
452 $sth->execute($basketgroupno);
455 #------------------------------------------------------------#
458 =head3 DelBasket
460 &DelBasket($basketno);
462 Deletes the basket that has basketno field $basketno in the aqbasket table.
464 =over
466 =item C<$basketno> is the primary key of the basket in the aqbasket table.
468 =back
470 =cut
472 sub DelBasket {
473 my ( $basketno ) = @_;
474 my $query = "DELETE FROM aqbasket WHERE basketno=?";
475 my $dbh = C4::Context->dbh;
476 my $sth = $dbh->prepare($query);
477 $sth->execute($basketno);
478 return;
481 #------------------------------------------------------------#
483 =head3 ModBasket
485 &ModBasket($basketinfo);
487 Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
489 =over
491 =item C<$basketno> is the primary key of the basket in the aqbasket table.
493 =back
495 =cut
497 sub ModBasket {
498 my $basketinfo = shift;
499 my $query = "UPDATE aqbasket SET ";
500 my @params;
501 foreach my $key (keys %$basketinfo){
502 if ($key ne 'basketno'){
503 $query .= "$key=?, ";
504 push(@params, $basketinfo->{$key} || undef );
507 # get rid of the "," at the end of $query
508 if (substr($query, length($query)-2) eq ', '){
509 chop($query);
510 chop($query);
511 $query .= ' ';
513 $query .= "WHERE basketno=?";
514 push(@params, $basketinfo->{'basketno'});
515 my $dbh = C4::Context->dbh;
516 my $sth = $dbh->prepare($query);
517 $sth->execute(@params);
519 return;
522 #------------------------------------------------------------#
524 =head3 ModBasketHeader
526 &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid);
528 Modifies a basket's header.
530 =over
532 =item C<$basketno> is the "basketno" field in the "aqbasket" table;
534 =item C<$basketname> is the "basketname" field in the "aqbasket" table;
536 =item C<$note> is the "note" field in the "aqbasket" table;
538 =item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
540 =item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
542 =item C<$booksellerid> is the id (foreign) key in the "aqbooksellers" table for the vendor.
544 =item C<$deliveryplace> is the "deliveryplace" field in the aqbasket table.
546 =item C<$billingplace> is the "billingplace" field in the aqbasket table.
548 =back
550 =cut
552 sub ModBasketHeader {
553 my ($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid, $deliveryplace, $billingplace) = @_;
554 my $query = qq{
555 UPDATE aqbasket
556 SET basketname=?, note=?, booksellernote=?, booksellerid=?, deliveryplace=?, billingplace=?
557 WHERE basketno=?
560 my $dbh = C4::Context->dbh;
561 my $sth = $dbh->prepare($query);
562 $sth->execute($basketname, $note, $booksellernote, $booksellerid, $deliveryplace, $billingplace, $basketno);
564 if ( $contractnumber ) {
565 my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
566 my $sth2 = $dbh->prepare($query2);
567 $sth2->execute($contractnumber,$basketno);
569 return;
572 #------------------------------------------------------------#
574 =head3 GetBasketsByBookseller
576 @results = &GetBasketsByBookseller($booksellerid, $extra);
578 Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
580 =over
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)
591 =back
593 =cut
595 sub GetBasketsByBookseller {
596 my ($booksellerid, $extra) = @_;
597 my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
598 if ($extra){
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
628 =cut
630 sub GetBasketsInfosByBookseller {
631 my ($supplierid, $allbaskets) = @_;
633 return unless $supplierid;
635 my $dbh = C4::Context->dbh;
636 my $query = q{
637 SELECT aqbasket.*,
638 SUM(aqorders.quantity) AS total_items,
639 SUM(
640 IF ( aqorders.orderstatus = 'cancelled', aqorders.quantity, 0 )
641 ) AS total_items_cancelled,
642 COUNT(DISTINCT aqorders.biblionumber) AS total_biblios,
643 SUM(
644 IF(aqorders.datereceived IS NULL
645 AND aqorders.datecancellationprinted IS NULL
646 , aqorders.quantity
647 , 0)
648 ) AS expected_items
649 FROM aqbasket
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
665 FROM aqbasket
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 );
671 map {
672 $_->{total_biblios_cancelled} = $cancelled_biblios->{$_->{basketno}}{total_biblios_cancelled} || 0
673 } @$baskets;
675 return $baskets;
678 =head3 GetBasketUsers
680 $basketusers_ids = &GetBasketUsers($basketno);
682 Returns a list of all borrowernumbers that are in basket users list
684 =cut
686 sub GetBasketUsers {
687 my $basketno = shift;
689 return unless $basketno;
691 my $query = qq{
692 SELECT borrowernumber
693 FROM aqbasketusers
694 WHERE basketno = ?
696 my $dbh = C4::Context->dbh;
697 my $sth = $dbh->prepare($query);
698 $sth->execute($basketno);
699 my $results = $sth->fetchall_arrayref( {} );
701 my @borrowernumbers;
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>
715 to this users list.
717 =cut
719 sub ModBasketUsers {
720 my ($basketno, @basketusers_ids) = @_;
722 return unless $basketno;
724 my $dbh = C4::Context->dbh;
725 my $query = qq{
726 DELETE FROM aqbasketusers
727 WHERE basketno = ?
729 my $sth = $dbh->prepare($query);
730 $sth->execute($basketno);
732 $query = qq{
733 INSERT INTO aqbasketusers (basketno, borrowernumber)
734 VALUES (?, ?)
736 $sth = $dbh->prepare($query);
737 foreach my $basketuser_id (@basketusers_ids) {
738 $sth->execute($basketno, $basketuser_id);
740 return;
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,
750 branch).
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.
762 Otherwise returns 0.
764 =cut
766 sub CanUserManageBasket {
767 my ($borrower, $basket, $userflags) = @_;
769 if (!ref $borrower) {
770 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
772 if (!ref $basket) {
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;
788 $sth->finish;
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}) {
798 return 0;
801 if ( (ref $userflags->{acquisition} && !$userflags->{acquisition}->{order_manage})
802 || (!ref $userflags->{acquisition} && !$userflags->{acquisition}) ) {
803 return 0;
806 if ($AcqViewBaskets eq 'user'
807 && $basket->{authorisedby} != $borrowernumber
808 && grep($borrowernumber, GetBasketUsers($basketno)) == 0) {
809 return 0;
812 if ($AcqViewBaskets eq 'branch' && defined $basket->{branch}
813 && $basket->{branch} ne $borrower->{branchcode}) {
814 return 0;
818 return 1;
821 #------------------------------------------------------------#
823 =head3 GetBasketsByBasketgroup
825 $baskets = &GetBasketsByBasketgroup($basketgroupid);
827 Returns a reference to all baskets that belong to basketgroup $basketgroupid.
829 =cut
831 sub GetBasketsByBasketgroup {
832 my $basketgroupid = shift;
833 my $query = qq{
834 SELECT *, aqbasket.booksellerid as booksellerid
835 FROM aqbasket
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.
868 =cut
870 sub NewBasketgroup {
871 my $basketgroupinfo = shift;
872 die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
873 my $query = "INSERT INTO aqbasketgroups (";
874 my @params;
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 (";
882 foreach (@params) {
883 $query .= "?, ";
885 $query .= "?)";
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.
925 =cut
927 sub ModBasketgroup {
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 ";
932 my @params;
933 foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
934 if ( defined $basketgroupinfo->{$field} ) {
935 $query .= "$field=?, ";
936 push(@params, $basketgroupinfo->{$field});
939 chop($query);
940 chop($query);
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);
955 return;
958 #------------------------------------------------------------#
960 =head3 DelBasketgroup
962 DelBasketgroup($basketgroupid);
964 Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
966 =over
968 =item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
970 =back
972 =cut
974 sub DelBasketgroup {
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);
981 return;
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.
995 =cut
997 sub GetBasketgroup {
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=?',
1003 { Slice => {} },
1004 $basketgroupid
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.
1017 =cut
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
1033 =head3 GetOrders
1035 @orders = &GetOrders($basketnumber, $orderby);
1037 Looks up the pending (non-cancelled) orders with the given basket
1038 number. If C<$booksellerID> is non-empty, only orders from that seller
1039 are returned.
1041 return :
1042 C<&basket> returns a two-element array. C<@orders> is an array of
1043 references-to-hash, whose keys are the fields from the aqorders,
1044 biblio, and biblioitems tables in the Koha database.
1046 =cut
1048 sub GetOrders {
1049 my ( $basketno, $orderby ) = @_;
1050 return () unless $basketno;
1051 my $dbh = C4::Context->dbh;
1052 my $query ="
1053 SELECT biblio.*,biblioitems.*,
1054 aqorders.*,
1055 aqbudgets.*,
1056 aqorders_transfers.ordernumber_from AS transferred_from,
1057 aqorders_transfers.timestamp AS transferred_from_timestamp
1058 FROM aqorders
1059 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1060 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1061 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
1062 LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_to = aqorders.ordernumber
1063 WHERE basketno=?
1064 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
1067 $orderby = "biblioitems.publishercode,biblio.title" unless $orderby;
1068 $query .= " ORDER BY $orderby";
1069 my $result_set =
1070 $dbh->selectall_arrayref( $query, { Slice => {} }, $basketno );
1071 return @{$result_set};
1075 #------------------------------------------------------------#
1077 =head3 GetOrdersByBiblionumber
1079 @orders = &GetOrdersByBiblionumber($biblionumber);
1081 Looks up the orders with linked to a specific $biblionumber, including
1082 cancelled orders and received orders.
1084 return :
1085 C<@orders> is an array of references-to-hash, whose keys are the
1086 fields from the aqorders, biblio, and biblioitems tables in the Koha database.
1088 =cut
1090 sub GetOrdersByBiblionumber {
1091 my $biblionumber = shift;
1092 return unless $biblionumber;
1093 my $dbh = C4::Context->dbh;
1094 my $query ="
1095 SELECT biblio.*,biblioitems.*,
1096 aqorders.*,
1097 aqbudgets.*
1098 FROM aqorders
1099 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1100 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1101 LEFT JOIN biblioitems ON biblioitems.biblionumber =biblio.biblionumber
1102 WHERE aqorders.biblionumber=?
1104 my $result_set =
1105 $dbh->selectall_arrayref( $query, { Slice => {} }, $biblionumber );
1106 return @{$result_set};
1110 #------------------------------------------------------------#
1112 =head3 GetOrder
1114 $order = &GetOrder($ordernumber);
1116 Looks up an order by order number.
1118 Returns a reference-to-hash describing the order. The keys of
1119 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
1121 =cut
1123 sub GetOrder {
1124 my ($ordernumber) = @_;
1125 return unless $ordernumber;
1127 my $dbh = C4::Context->dbh;
1128 my $query = qq{SELECT
1129 aqorders.*,
1130 biblio.title,
1131 biblio.author,
1132 aqbasket.basketname,
1133 borrowers.branchcode,
1134 biblioitems.publicationyear,
1135 biblio.copyrightdate,
1136 biblioitems.editionstatement,
1137 biblioitems.isbn,
1138 biblioitems.ean,
1139 biblio.seriestitle,
1140 biblioitems.publishercode,
1141 aqorders.rrp AS unitpricesupplier,
1142 aqorders.ecost AS unitpricelib,
1143 aqorders.claims_count AS claims_count,
1144 aqorders.claimed_date AS claimed_date,
1145 aqbudgets.budget_name AS budget,
1146 aqbooksellers.name AS supplier,
1147 aqbooksellers.id AS supplierid,
1148 biblioitems.publishercode AS publisher,
1149 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
1150 DATE(aqbasket.closedate) AS orderdate,
1151 aqorders.quantity - COALESCE(aqorders.quantityreceived,0) AS quantity_to_receive,
1152 (aqorders.quantity - COALESCE(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1153 DATEDIFF(CURDATE( ),closedate) AS latesince
1154 FROM aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1155 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1156 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
1157 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
1158 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1159 WHERE aqorders.basketno = aqbasket.basketno
1160 AND ordernumber=?};
1161 my $result_set =
1162 $dbh->selectall_arrayref( $query, { Slice => {} }, $ordernumber );
1164 # result_set assumed to contain 1 match
1165 return $result_set->[0];
1168 =head3 GetLastOrderNotReceivedFromSubscriptionid
1170 $order = &GetLastOrderNotReceivedFromSubscriptionid($subscriptionid);
1172 Returns a reference-to-hash describing the last order not received for a subscription.
1174 =cut
1176 sub GetLastOrderNotReceivedFromSubscriptionid {
1177 my ( $subscriptionid ) = @_;
1178 my $dbh = C4::Context->dbh;
1179 my $query = qq|
1180 SELECT * FROM aqorders
1181 LEFT JOIN subscription
1182 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1183 WHERE aqorders.subscriptionid = ?
1184 AND aqorders.datereceived IS NULL
1185 LIMIT 1
1187 my $result_set =
1188 $dbh->selectall_arrayref( $query, { Slice => {} }, $subscriptionid );
1190 # result_set assumed to contain 1 match
1191 return $result_set->[0];
1194 =head3 GetLastOrderReceivedFromSubscriptionid
1196 $order = &GetLastOrderReceivedFromSubscriptionid($subscriptionid);
1198 Returns a reference-to-hash describing the last order received for a subscription.
1200 =cut
1202 sub GetLastOrderReceivedFromSubscriptionid {
1203 my ( $subscriptionid ) = @_;
1204 my $dbh = C4::Context->dbh;
1205 my $query = qq|
1206 SELECT * FROM aqorders
1207 LEFT JOIN subscription
1208 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1209 WHERE aqorders.subscriptionid = ?
1210 AND aqorders.datereceived =
1212 SELECT MAX( aqorders.datereceived )
1213 FROM aqorders
1214 LEFT JOIN subscription
1215 ON ( aqorders.subscriptionid = subscription.subscriptionid )
1216 WHERE aqorders.subscriptionid = ?
1217 AND aqorders.datereceived IS NOT NULL
1219 ORDER BY ordernumber DESC
1220 LIMIT 1
1222 my $result_set =
1223 $dbh->selectall_arrayref( $query, { Slice => {} }, $subscriptionid, $subscriptionid );
1225 # result_set assumed to contain 1 match
1226 return $result_set->[0];
1230 #------------------------------------------------------------#
1232 =head3 ModOrder
1234 &ModOrder(\%hashref);
1236 Modifies an existing order. Updates the order with order number
1237 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All
1238 other keys of the hash update the fields with the same name in the aqorders
1239 table of the Koha database.
1241 =cut
1243 sub ModOrder {
1244 my $orderinfo = shift;
1246 die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '' ;
1247 die "Biblionumber is required" if $orderinfo->{'biblionumber'} eq '';
1249 my $dbh = C4::Context->dbh;
1250 my @params;
1252 # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
1253 $orderinfo->{uncertainprice}=1 if $orderinfo->{uncertainprice};
1255 # delete($orderinfo->{'branchcode'});
1256 # the hash contains a lot of entries not in aqorders, so get the columns ...
1257 my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1258 $sth->execute;
1259 my $colnames = $sth->{NAME};
1260 #FIXME Be careful. If aqorders would have columns with diacritics,
1261 #you should need to decode what you get back from NAME.
1262 #See report 10110 and guided_reports.pl
1263 my $query = "UPDATE aqorders SET ";
1265 foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1266 # ... and skip hash entries that are not in the aqorders table
1267 # FIXME : probably not the best way to do it (would be better to have a correct hash)
1268 next unless grep(/^$orderinfokey$/, @$colnames);
1269 $query .= "$orderinfokey=?, ";
1270 push(@params, $orderinfo->{$orderinfokey});
1273 $query .= "timestamp=NOW() WHERE ordernumber=?";
1274 push(@params, $orderinfo->{'ordernumber'} );
1275 $sth = $dbh->prepare($query);
1276 $sth->execute(@params);
1277 return;
1280 #------------------------------------------------------------#
1282 =head3 ModItemOrder
1284 ModItemOrder($itemnumber, $ordernumber);
1286 Modifies the ordernumber of an item in aqorders_items.
1288 =cut
1290 sub ModItemOrder {
1291 my ($itemnumber, $ordernumber) = @_;
1293 return unless ($itemnumber and $ordernumber);
1295 my $dbh = C4::Context->dbh;
1296 my $query = qq{
1297 UPDATE aqorders_items
1298 SET ordernumber = ?
1299 WHERE itemnumber = ?
1301 my $sth = $dbh->prepare($query);
1302 return $sth->execute($ordernumber, $itemnumber);
1305 #------------------------------------------------------------#
1307 =head3 GetCancelledOrders
1309 my @orders = GetCancelledOrders($basketno, $orderby);
1311 Returns cancelled orders for a basket
1313 =cut
1315 sub GetCancelledOrders {
1316 my ( $basketno, $orderby ) = @_;
1318 return () unless $basketno;
1320 my $dbh = C4::Context->dbh;
1321 my $query = "
1322 SELECT
1323 biblio.*,
1324 biblioitems.*,
1325 aqorders.*,
1326 aqbudgets.*,
1327 aqorders_transfers.ordernumber_to AS transferred_to,
1328 aqorders_transfers.timestamp AS transferred_to_timestamp
1329 FROM aqorders
1330 LEFT JOIN aqbudgets ON aqbudgets.budget_id = aqorders.budget_id
1331 LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1332 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1333 LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_from = aqorders.ordernumber
1334 WHERE basketno = ?
1335 AND (datecancellationprinted IS NOT NULL
1336 AND datecancellationprinted <> '0000-00-00')
1339 $orderby = "aqorders.datecancellationprinted desc, aqorders.timestamp desc"
1340 unless $orderby;
1341 $query .= " ORDER BY $orderby";
1342 my $sth = $dbh->prepare($query);
1343 $sth->execute($basketno);
1344 my $results = $sth->fetchall_arrayref( {} );
1346 return @$results;
1350 #------------------------------------------------------------#
1352 =head3 ModReceiveOrder
1354 &ModReceiveOrder({
1355 biblionumber => $biblionumber,
1356 ordernumber => $ordernumber,
1357 quantityreceived => $quantityreceived,
1358 user => $user,
1359 cost => $cost,
1360 ecost => $ecost,
1361 invoiceid => $invoiceid,
1362 rrp => $rrp,
1363 budget_id => $budget_id,
1364 datereceived => $datereceived,
1365 received_itemnumbers => \@received_itemnumbers,
1366 order_internalnote => $order_internalnote,
1367 order_vendornote => $order_vendornote,
1370 Updates an order, to reflect the fact that it was received, at least
1371 in part. All arguments not mentioned below update the fields with the
1372 same name in the aqorders table of the Koha database.
1374 If a partial order is received, splits the order into two.
1376 Updates the order with bibilionumber C<$biblionumber> and ordernumber
1377 C<$ordernumber>.
1379 =cut
1382 sub ModReceiveOrder {
1383 my ( $params ) = @_;
1384 my $biblionumber = $params->{biblionumber};
1385 my $ordernumber = $params->{ordernumber};
1386 my $quantrec = $params->{quantityreceived};
1387 my $user = $params->{user};
1388 my $cost = $params->{cost};
1389 my $ecost = $params->{ecost};
1390 my $invoiceid = $params->{invoiceid};
1391 my $rrp = $params->{rrp};
1392 my $budget_id = $params->{budget_id};
1393 my $datereceived = $params->{datereceived};
1394 my $received_items = $params->{received_items};
1395 my $order_internalnote = $params->{order_internalnote};
1396 my $order_vendornote = $params->{order_vendornote};
1398 my $dbh = C4::Context->dbh;
1399 $datereceived = C4::Dates->output('iso') unless $datereceived;
1400 my $suggestionid = GetSuggestionFromBiblionumber( $biblionumber );
1401 if ($suggestionid) {
1402 ModSuggestion( {suggestionid=>$suggestionid,
1403 STATUS=>'AVAILABLE',
1404 biblionumber=> $biblionumber}
1408 my $result_set = $dbh->selectall_arrayref(
1409 q{SELECT * FROM aqorders WHERE biblionumber=? AND aqorders.ordernumber=?},
1410 { Slice => {} }, $biblionumber, $ordernumber
1413 # we assume we have a unique order
1414 my $order = $result_set->[0];
1416 my $new_ordernumber = $ordernumber;
1417 if ( $order->{quantity} > $quantrec ) {
1418 # Split order line in two parts: the first is the original order line
1419 # without received items (the quantity is decreased),
1420 # the second part is a new order line with quantity=quantityrec
1421 # (entirely received)
1422 my $query = q|
1423 UPDATE aqorders
1424 SET quantity = ?,
1425 orderstatus = 'partial'|;
1426 $query .= q|, order_internalnote = ?| if defined $order_internalnote;
1427 $query .= q|, order_vendornote = ?| if defined $order_vendornote;
1428 $query .= q| WHERE ordernumber = ?|;
1429 my $sth = $dbh->prepare($query);
1431 $sth->execute(
1432 $order->{quantity} - $quantrec,
1433 ( defined $order_internalnote ? $order_internalnote : () ),
1434 ( defined $order_vendornote ? $order_vendornote : () ),
1435 $ordernumber
1438 delete $order->{'ordernumber'};
1439 $order->{'budget_id'} = ( $budget_id || $order->{'budget_id'} );
1440 $order->{'quantity'} = $quantrec;
1441 $order->{'quantityreceived'} = $quantrec;
1442 $order->{'datereceived'} = $datereceived;
1443 $order->{'invoiceid'} = $invoiceid;
1444 $order->{'unitprice'} = $cost;
1445 $order->{'rrp'} = $rrp;
1446 $order->{ecost} = $ecost;
1447 $order->{'orderstatus'} = 'complete';
1448 $new_ordernumber = Koha::Acquisition::Order->new($order)->insert->{ordernumber};
1450 if ($received_items) {
1451 foreach my $itemnumber (@$received_items) {
1452 ModItemOrder($itemnumber, $new_ordernumber);
1455 } else {
1456 my $query = q|
1457 update aqorders
1458 set quantityreceived=?,datereceived=?,invoiceid=?,
1459 unitprice=?,rrp=?,ecost=?,budget_id=?,orderstatus='complete'|;
1460 $query .= q|, order_internalnote = ?| if defined $order_internalnote;
1461 $query .= q|, order_vendornote = ?| if defined $order_vendornote;
1462 $query .= q| where biblionumber=? and ordernumber=?|;
1463 my $sth = $dbh->prepare( $query );
1464 $sth->execute(
1465 $quantrec,
1466 $datereceived,
1467 $invoiceid,
1468 $cost,
1469 $rrp,
1470 $ecost,
1471 ( $budget_id ? $budget_id : $order->{budget_id} ),
1472 ( defined $order_internalnote ? $order_internalnote : () ),
1473 ( defined $order_vendornote ? $order_vendornote : () ),
1474 $biblionumber,
1475 $ordernumber
1478 return ($datereceived, $new_ordernumber);
1481 =head3 CancelReceipt
1483 my $parent_ordernumber = CancelReceipt($ordernumber);
1485 Cancel an order line receipt and update the parent order line, as if no
1486 receipt was made.
1487 If items are created at receipt (AcqCreateItem = receiving) then delete
1488 these items.
1490 =cut
1492 sub CancelReceipt {
1493 my $ordernumber = shift;
1495 return unless $ordernumber;
1497 my $dbh = C4::Context->dbh;
1498 my $query = qq{
1499 SELECT datereceived, parent_ordernumber, quantity
1500 FROM aqorders
1501 WHERE ordernumber = ?
1503 my $sth = $dbh->prepare($query);
1504 $sth->execute($ordernumber);
1505 my $order = $sth->fetchrow_hashref;
1506 unless($order) {
1507 warn "CancelReceipt: order $ordernumber does not exist";
1508 return;
1510 unless($order->{'datereceived'}) {
1511 warn "CancelReceipt: order $ordernumber is not received";
1512 return;
1515 my $parent_ordernumber = $order->{'parent_ordernumber'};
1517 my @itemnumbers = GetItemnumbersFromOrder( $ordernumber );
1519 if($parent_ordernumber == $ordernumber || not $parent_ordernumber) {
1520 # The order line has no parent, just mark it as not received
1521 $query = qq{
1522 UPDATE aqorders
1523 SET quantityreceived = ?,
1524 datereceived = ?,
1525 invoiceid = ?,
1526 orderstatus = 'ordered'
1527 WHERE ordernumber = ?
1529 $sth = $dbh->prepare($query);
1530 $sth->execute(0, undef, undef, $ordernumber);
1531 _cancel_items_receipt( $ordernumber );
1532 } else {
1533 # The order line has a parent, increase parent quantity and delete
1534 # the order line.
1535 $query = qq{
1536 SELECT quantity, datereceived
1537 FROM aqorders
1538 WHERE ordernumber = ?
1540 $sth = $dbh->prepare($query);
1541 $sth->execute($parent_ordernumber);
1542 my $parent_order = $sth->fetchrow_hashref;
1543 unless($parent_order) {
1544 warn "Parent order $parent_ordernumber does not exist.";
1545 return;
1547 if($parent_order->{'datereceived'}) {
1548 warn "CancelReceipt: parent order is received.".
1549 " Can't cancel receipt.";
1550 return;
1552 $query = qq{
1553 UPDATE aqorders
1554 SET quantity = ?,
1555 orderstatus = 'ordered'
1556 WHERE ordernumber = ?
1558 $sth = $dbh->prepare($query);
1559 my $rv = $sth->execute(
1560 $order->{'quantity'} + $parent_order->{'quantity'},
1561 $parent_ordernumber
1563 unless($rv) {
1564 warn "Cannot update parent order line, so do not cancel".
1565 " receipt";
1566 return;
1568 _cancel_items_receipt( $ordernumber, $parent_ordernumber );
1569 # Delete order line
1570 $query = qq{
1571 DELETE FROM aqorders
1572 WHERE ordernumber = ?
1574 $sth = $dbh->prepare($query);
1575 $sth->execute($ordernumber);
1579 if(C4::Context->preference('AcqCreateItem') eq 'ordering') {
1580 my @affects = split q{\|}, C4::Context->preference("AcqItemSetSubfieldsWhenReceiptIsCancelled");
1581 if ( @affects ) {
1582 for my $in ( @itemnumbers ) {
1583 my $biblionumber = C4::Biblio::GetBiblionumberFromItemnumber( $in );
1584 my $frameworkcode = GetFrameworkCode($biblionumber);
1585 my ( $itemfield ) = GetMarcFromKohaField( 'items.itemnumber', $frameworkcode );
1586 my $item = C4::Items::GetMarcItem( $biblionumber, $in );
1587 for my $affect ( @affects ) {
1588 my ( $sf, $v ) = split q{=}, $affect, 2;
1589 foreach ( $item->field($itemfield) ) {
1590 $_->update( $sf => $v );
1593 C4::Items::ModItemFromMarc( $item, $biblionumber, $in );
1598 return $parent_ordernumber;
1601 sub _cancel_items_receipt {
1602 my ( $ordernumber, $parent_ordernumber ) = @_;
1603 $parent_ordernumber ||= $ordernumber;
1605 my @itemnumbers = GetItemnumbersFromOrder($ordernumber);
1606 if(C4::Context->preference('AcqCreateItem') eq 'receiving') {
1607 # Remove items that were created at receipt
1608 my $query = qq{
1609 DELETE FROM items, aqorders_items
1610 USING items, aqorders_items
1611 WHERE items.itemnumber = ? AND aqorders_items.itemnumber = ?
1613 my $dbh = C4::Context->dbh;
1614 my $sth = $dbh->prepare($query);
1615 foreach my $itemnumber (@itemnumbers) {
1616 $sth->execute($itemnumber, $itemnumber);
1618 } else {
1619 # Update items
1620 foreach my $itemnumber (@itemnumbers) {
1621 ModItemOrder($itemnumber, $parent_ordernumber);
1626 #------------------------------------------------------------#
1628 =head3 SearchOrders
1630 @results = &SearchOrders({
1631 ordernumber => $ordernumber,
1632 search => $search,
1633 biblionumber => $biblionumber,
1634 ean => $ean,
1635 booksellerid => $booksellerid,
1636 basketno => $basketno,
1637 owner => $owner,
1638 pending => $pending
1639 ordered => $ordered
1642 Searches for orders.
1644 C<$owner> Finds order for the logged in user.
1645 C<$pending> Finds pending orders. Ignores completed and cancelled orders.
1646 C<$ordered> Finds orders to receive only (status 'ordered' or 'partial').
1649 C<@results> is an array of references-to-hash with the keys are fields
1650 from aqorders, biblio, biblioitems and aqbasket tables.
1652 =cut
1654 sub SearchOrders {
1655 my ( $params ) = @_;
1656 my $ordernumber = $params->{ordernumber};
1657 my $search = $params->{search};
1658 my $ean = $params->{ean};
1659 my $booksellerid = $params->{booksellerid};
1660 my $basketno = $params->{basketno};
1661 my $basketname = $params->{basketname};
1662 my $basketgroupname = $params->{basketgroupname};
1663 my $owner = $params->{owner};
1664 my $pending = $params->{pending};
1665 my $ordered = $params->{ordered};
1666 my $biblionumber = $params->{biblionumber};
1667 my $budget_id = $params->{budget_id};
1669 my $dbh = C4::Context->dbh;
1670 my @args = ();
1671 my $query = q{
1672 SELECT aqbasket.basketno,
1673 borrowers.surname,
1674 borrowers.firstname,
1675 biblio.*,
1676 biblioitems.isbn,
1677 biblioitems.biblioitemnumber,
1678 aqbasket.authorisedby,
1679 aqbasket.booksellerid,
1680 aqbasket.closedate,
1681 aqbasket.creationdate,
1682 aqbasket.basketname,
1683 aqbasketgroups.id as basketgroupid,
1684 aqbasketgroups.name as basketgroupname,
1685 aqorders.*
1686 FROM aqorders
1687 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1688 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
1689 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1690 LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1691 LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1694 # If we search on ordernumber, we retrieve the transfered order if a transfer has been done.
1695 $query .= q{
1696 LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_to = aqorders.ordernumber
1697 } if $ordernumber;
1699 $query .= q{
1700 WHERE (datecancellationprinted is NULL)
1703 if ( $pending or $ordered ) {
1704 $query .= q{ AND (quantity > quantityreceived OR quantityreceived is NULL)};
1706 if ( $ordered ) {
1707 $query .= q{ AND aqorders.orderstatus IN ( "ordered", "partial" )};
1710 my $userenv = C4::Context->userenv;
1711 if ( C4::Context->preference("IndependentBranches") ) {
1712 unless ( C4::Context->IsSuperLibrarian() ) {
1713 $query .= q{
1714 AND (
1715 borrowers.branchcode = ?
1716 OR borrowers.branchcode = ''
1719 push @args, $userenv->{branch};
1723 if ( $ordernumber ) {
1724 $query .= ' AND ( aqorders.ordernumber = ? OR aqorders_transfers.ordernumber_from = ? ) ';
1725 push @args, ( $ordernumber, $ordernumber );
1727 if ( $biblionumber ) {
1728 $query .= 'AND aqorders.biblionumber = ?';
1729 push @args, $biblionumber;
1731 if( $search ) {
1732 $query .= ' AND (biblio.title LIKE ? OR biblio.author LIKE ? OR biblioitems.isbn LIKE ?)';
1733 push @args, ("%$search%","%$search%","%$search%");
1735 if ( $ean ) {
1736 $query .= ' AND biblioitems.ean = ?';
1737 push @args, $ean;
1739 if ( $booksellerid ) {
1740 $query .= 'AND aqbasket.booksellerid = ?';
1741 push @args, $booksellerid;
1743 if( $basketno ) {
1744 $query .= 'AND aqbasket.basketno = ?';
1745 push @args, $basketno;
1747 if( $basketname ) {
1748 $query .= 'AND aqbasket.basketname LIKE ?';
1749 push @args, "%$basketname%";
1751 if( $basketgroupname ) {
1752 $query .= ' AND aqbasketgroups.name LIKE ?';
1753 push @args, "%$basketgroupname%";
1756 if ( $owner ) {
1757 $query .= ' AND aqbasket.authorisedby=? ';
1758 push @args, $userenv->{'number'};
1761 if ( $budget_id ) {
1762 $query .= ' AND aqorders.budget_id = ?';
1763 push @args, $budget_id;
1766 $query .= ' ORDER BY aqbasket.basketno';
1768 my $sth = $dbh->prepare($query);
1769 $sth->execute(@args);
1770 return $sth->fetchall_arrayref({});
1773 #------------------------------------------------------------#
1775 =head3 DelOrder
1777 &DelOrder($biblionumber, $ordernumber);
1779 Cancel the order with the given order and biblio numbers. It does not
1780 delete any entries in the aqorders table, it merely marks them as
1781 cancelled.
1783 =cut
1785 sub DelOrder {
1786 my ( $bibnum, $ordernumber, $delete_biblio, $reason ) = @_;
1788 my $error;
1789 my $dbh = C4::Context->dbh;
1790 my $query = "
1791 UPDATE aqorders
1792 SET datecancellationprinted=now(), orderstatus='cancelled'
1794 if($reason) {
1795 $query .= ", cancellationreason = ? ";
1797 $query .= "
1798 WHERE biblionumber=? AND ordernumber=?
1800 my $sth = $dbh->prepare($query);
1801 if($reason) {
1802 $sth->execute($reason, $bibnum, $ordernumber);
1803 } else {
1804 $sth->execute( $bibnum, $ordernumber );
1806 $sth->finish;
1808 my @itemnumbers = GetItemnumbersFromOrder( $ordernumber );
1809 foreach my $itemnumber (@itemnumbers){
1810 my $delcheck = C4::Items::DelItemCheck( $dbh, $bibnum, $itemnumber );
1812 if($delcheck != 1) {
1813 $error->{'delitem'} = 1;
1817 if($delete_biblio) {
1818 # We get the number of remaining items
1819 my $itemcount = C4::Items::GetItemsCount($bibnum);
1821 # If there are no items left,
1822 if ( $itemcount == 0 ) {
1823 # We delete the record
1824 my $delcheck = DelBiblio($bibnum);
1826 if($delcheck) {
1827 $error->{'delbiblio'} = 1;
1832 return $error;
1835 =head3 TransferOrder
1837 my $newordernumber = TransferOrder($ordernumber, $basketno);
1839 Transfer an order line to a basket.
1840 Mark $ordernumber as cancelled with an internal note 'Cancelled and transfered
1841 to BOOKSELLER on DATE' and create new order with internal note
1842 'Transfered from BOOKSELLER on DATE'.
1843 Move all attached items to the new order.
1844 Received orders cannot be transfered.
1845 Return the ordernumber of created order.
1847 =cut
1849 sub TransferOrder {
1850 my ($ordernumber, $basketno) = @_;
1852 return unless ($ordernumber and $basketno);
1854 my $order = GetOrder( $ordernumber );
1855 return if $order->{datereceived};
1856 my $basket = GetBasket($basketno);
1857 return unless $basket;
1859 my $dbh = C4::Context->dbh;
1860 my ($query, $sth, $rv);
1862 $query = q{
1863 UPDATE aqorders
1864 SET datecancellationprinted = CAST(NOW() AS date)
1865 WHERE ordernumber = ?
1867 $sth = $dbh->prepare($query);
1868 $rv = $sth->execute($ordernumber);
1870 delete $order->{'ordernumber'};
1871 delete $order->{parent_ordernumber};
1872 $order->{'basketno'} = $basketno;
1874 my $newordernumber = Koha::Acquisition::Order->new($order)->insert->{ordernumber};
1876 $query = q{
1877 UPDATE aqorders_items
1878 SET ordernumber = ?
1879 WHERE ordernumber = ?
1881 $sth = $dbh->prepare($query);
1882 $sth->execute($newordernumber, $ordernumber);
1884 $query = q{
1885 INSERT INTO aqorders_transfers (ordernumber_from, ordernumber_to)
1886 VALUES (?, ?)
1888 $sth = $dbh->prepare($query);
1889 $sth->execute($ordernumber, $newordernumber);
1891 return $newordernumber;
1894 =head2 FUNCTIONS ABOUT PARCELS
1896 =head3 GetParcels
1898 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1900 get a lists of parcels.
1902 * Input arg :
1904 =over
1906 =item $bookseller
1907 is the bookseller this function has to get parcels.
1909 =item $order
1910 To know on what criteria the results list has to be ordered.
1912 =item $code
1913 is the booksellerinvoicenumber.
1915 =item $datefrom & $dateto
1916 to know on what date this function has to filter its search.
1918 =back
1920 * return:
1921 a pointer on a hash list containing parcel informations as such :
1923 =over
1925 =item Creation date
1927 =item Last operation
1929 =item Number of biblio
1931 =item Number of items
1933 =back
1935 =cut
1937 sub GetParcels {
1938 my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
1939 my $dbh = C4::Context->dbh;
1940 my @query_params = ();
1941 my $strsth ="
1942 SELECT aqinvoices.invoicenumber,
1943 datereceived,purchaseordernumber,
1944 count(DISTINCT biblionumber) AS biblio,
1945 sum(quantity) AS itemsexpected,
1946 sum(quantityreceived) AS itemsreceived
1947 FROM aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
1948 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
1949 WHERE aqbasket.booksellerid = ? and datereceived IS NOT NULL
1951 push @query_params, $bookseller;
1953 if ( defined $code ) {
1954 $strsth .= ' and aqinvoices.invoicenumber like ? ';
1955 # add a % to the end of the code to allow stemming.
1956 push @query_params, "$code%";
1959 if ( defined $datefrom ) {
1960 $strsth .= ' and datereceived >= ? ';
1961 push @query_params, $datefrom;
1964 if ( defined $dateto ) {
1965 $strsth .= 'and datereceived <= ? ';
1966 push @query_params, $dateto;
1969 $strsth .= "group by aqinvoices.invoicenumber,datereceived ";
1971 # can't use a placeholder to place this column name.
1972 # but, we could probably be checking to make sure it is a column that will be fetched.
1973 $strsth .= "order by $order " if ($order);
1975 my $sth = $dbh->prepare($strsth);
1977 $sth->execute( @query_params );
1978 my $results = $sth->fetchall_arrayref({});
1979 return @{$results};
1982 #------------------------------------------------------------#
1984 =head3 GetLateOrders
1986 @results = &GetLateOrders;
1988 Searches for bookseller with late orders.
1990 return:
1991 the table of supplier with late issues. This table is full of hashref.
1993 =cut
1995 sub GetLateOrders {
1996 my $delay = shift;
1997 my $supplierid = shift;
1998 my $branch = shift;
1999 my $estimateddeliverydatefrom = shift;
2000 my $estimateddeliverydateto = shift;
2002 my $dbh = C4::Context->dbh;
2004 #BEWARE, order of parenthesis and LEFT JOIN is important for speed
2005 my $dbdriver = C4::Context->config("db_scheme") || "mysql";
2007 my @query_params = ();
2008 my $select = "
2009 SELECT aqbasket.basketno,
2010 aqorders.ordernumber,
2011 DATE(aqbasket.closedate) AS orderdate,
2012 aqbasket.basketname AS basketname,
2013 aqbasket.basketgroupid AS basketgroupid,
2014 aqbasketgroups.name AS basketgroupname,
2015 aqorders.rrp AS unitpricesupplier,
2016 aqorders.ecost AS unitpricelib,
2017 aqorders.claims_count AS claims_count,
2018 aqorders.claimed_date AS claimed_date,
2019 aqbudgets.budget_name AS budget,
2020 borrowers.branchcode AS branch,
2021 aqbooksellers.name AS supplier,
2022 aqbooksellers.id AS supplierid,
2023 biblio.author, biblio.title,
2024 biblioitems.publishercode AS publisher,
2025 biblioitems.publicationyear,
2026 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
2028 my $from = "
2029 FROM
2030 aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
2031 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
2032 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
2033 aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber
2034 LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
2035 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
2036 WHERE aqorders.basketno = aqbasket.basketno
2037 AND ( datereceived = ''
2038 OR datereceived IS NULL
2039 OR aqorders.quantityreceived < aqorders.quantity
2041 AND aqbasket.closedate IS NOT NULL
2042 AND (aqorders.datecancellationprinted IS NULL OR aqorders.datecancellationprinted='0000-00-00')
2044 my $having = "";
2045 if ($dbdriver eq "mysql") {
2046 $select .= "
2047 aqorders.quantity - COALESCE(aqorders.quantityreceived,0) AS quantity,
2048 (aqorders.quantity - COALESCE(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
2049 DATEDIFF(CAST(now() AS date),closedate) AS latesince
2051 if ( defined $delay ) {
2052 $from .= " AND (closedate <= DATE_SUB(CAST(now() AS date),INTERVAL ? DAY)) " ;
2053 push @query_params, $delay;
2055 $having = "
2056 HAVING quantity <> 0
2057 AND unitpricesupplier <> 0
2058 AND unitpricelib <> 0
2060 } else {
2061 # FIXME: account for IFNULL as above
2062 $select .= "
2063 aqorders.quantity AS quantity,
2064 aqorders.quantity * aqorders.rrp AS subtotal,
2065 (CAST(now() AS date) - closedate) AS latesince
2067 if ( defined $delay ) {
2068 $from .= " AND (closedate <= (CAST(now() AS date) -(INTERVAL ? DAY)) ";
2069 push @query_params, $delay;
2072 if (defined $supplierid) {
2073 $from .= ' AND aqbasket.booksellerid = ? ';
2074 push @query_params, $supplierid;
2076 if (defined $branch) {
2077 $from .= ' AND borrowers.branchcode LIKE ? ';
2078 push @query_params, $branch;
2081 if ( defined $estimateddeliverydatefrom or defined $estimateddeliverydateto ) {
2082 $from .= ' AND aqbooksellers.deliverytime IS NOT NULL ';
2084 if ( defined $estimateddeliverydatefrom ) {
2085 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) >= ?';
2086 push @query_params, $estimateddeliverydatefrom;
2088 if ( defined $estimateddeliverydateto ) {
2089 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= ?';
2090 push @query_params, $estimateddeliverydateto;
2092 if ( defined $estimateddeliverydatefrom and not defined $estimateddeliverydateto ) {
2093 $from .= ' AND ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) <= CAST(now() AS date)';
2095 if (C4::Context->preference("IndependentBranches")
2096 && !C4::Context->IsSuperLibrarian() ) {
2097 $from .= ' AND borrowers.branchcode LIKE ? ';
2098 push @query_params, C4::Context->userenv->{branch};
2100 $from .= " AND orderstatus <> 'cancelled' ";
2101 my $query = "$select $from $having\nORDER BY latesince, basketno, borrowers.branchcode, supplier";
2102 $debug and print STDERR "GetLateOrders query: $query\nGetLateOrders args: " . join(" ",@query_params);
2103 my $sth = $dbh->prepare($query);
2104 $sth->execute(@query_params);
2105 my @results;
2106 while (my $data = $sth->fetchrow_hashref) {
2107 push @results, $data;
2109 return @results;
2112 #------------------------------------------------------------#
2114 =head3 GetHistory
2116 \@order_loop = GetHistory( %params );
2118 Retreives some acquisition history information
2120 params:
2121 title
2122 author
2123 name
2124 isbn
2126 from_placed_on
2127 to_placed_on
2128 basket - search both basket name and number
2129 booksellerinvoicenumber
2130 basketgroupname
2131 budget
2132 orderstatus (note that orderstatus '' will retrieve orders
2133 of any status except cancelled)
2134 biblionumber
2135 get_canceled_order (if set to a true value, cancelled orders will
2136 be included)
2138 returns:
2139 $order_loop is a list of hashrefs that each look like this:
2141 'author' => 'Twain, Mark',
2142 'basketno' => '1',
2143 'biblionumber' => '215',
2144 'count' => 1,
2145 'creationdate' => 'MM/DD/YYYY',
2146 'datereceived' => undef,
2147 'ecost' => '1.00',
2148 'id' => '1',
2149 'invoicenumber' => undef,
2150 'name' => '',
2151 'ordernumber' => '1',
2152 'quantity' => 1,
2153 'quantityreceived' => undef,
2154 'title' => 'The Adventures of Huckleberry Finn'
2157 =cut
2159 sub GetHistory {
2160 # don't run the query if there are no parameters (list would be too long for sure !)
2161 croak "No search params" unless @_;
2162 my %params = @_;
2163 my $title = $params{title};
2164 my $author = $params{author};
2165 my $isbn = $params{isbn};
2166 my $ean = $params{ean};
2167 my $name = $params{name};
2168 my $from_placed_on = $params{from_placed_on};
2169 my $to_placed_on = $params{to_placed_on};
2170 my $basket = $params{basket};
2171 my $booksellerinvoicenumber = $params{booksellerinvoicenumber};
2172 my $basketgroupname = $params{basketgroupname};
2173 my $budget = $params{budget};
2174 my $orderstatus = $params{orderstatus};
2175 my $biblionumber = $params{biblionumber};
2176 my $get_canceled_order = $params{get_canceled_order} || 0;
2177 my $ordernumber = $params{ordernumber};
2178 my $search_children_too = $params{search_children_too} || 0;
2179 my $created_by = $params{created_by} || [];
2181 my @order_loop;
2182 my $total_qty = 0;
2183 my $total_qtyreceived = 0;
2184 my $total_price = 0;
2186 my $dbh = C4::Context->dbh;
2187 my $query ="
2188 SELECT
2189 COALESCE(biblio.title, deletedbiblio.title) AS title,
2190 COALESCE(biblio.author, deletedbiblio.author) AS author,
2191 COALESCE(biblioitems.isbn, deletedbiblioitems.isbn) AS isbn,
2192 COALESCE(biblioitems.ean, deletedbiblioitems.ean) AS ean,
2193 aqorders.basketno,
2194 aqbasket.basketname,
2195 aqbasket.basketgroupid,
2196 aqbasket.authorisedby,
2197 concat( borrowers.firstname,' ',borrowers.surname) AS authorisedbyname,
2198 aqbasketgroups.name as groupname,
2199 aqbooksellers.name,
2200 aqbasket.creationdate,
2201 aqorders.datereceived,
2202 aqorders.quantity,
2203 aqorders.quantityreceived,
2204 aqorders.ecost,
2205 aqorders.ordernumber,
2206 aqorders.invoiceid,
2207 aqinvoices.invoicenumber,
2208 aqbooksellers.id as id,
2209 aqorders.biblionumber,
2210 aqorders.orderstatus,
2211 aqorders.parent_ordernumber,
2212 aqbudgets.budget_name
2214 $query .= ", aqbudgets.budget_id AS budget" if defined $budget;
2215 $query .= "
2216 FROM aqorders
2217 LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
2218 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
2219 LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
2220 LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
2221 LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
2222 LEFT JOIN aqbudgets ON aqorders.budget_id=aqbudgets.budget_id
2223 LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
2224 LEFT JOIN deletedbiblio ON deletedbiblio.biblionumber=aqorders.biblionumber
2225 LEFT JOIN deletedbiblioitems ON deletedbiblioitems.biblionumber=aqorders.biblionumber
2226 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
2229 $query .= " WHERE 1 ";
2231 unless ($get_canceled_order or (defined $orderstatus and $orderstatus eq 'cancelled')) {
2232 $query .= " AND (datecancellationprinted is NULL or datecancellationprinted='0000-00-00') ";
2235 my @query_params = ();
2237 if ( $biblionumber ) {
2238 $query .= " AND biblio.biblionumber = ?";
2239 push @query_params, $biblionumber;
2242 if ( $title ) {
2243 $query .= " AND biblio.title LIKE ? ";
2244 $title =~ s/\s+/%/g;
2245 push @query_params, "%$title%";
2248 if ( $author ) {
2249 $query .= " AND biblio.author LIKE ? ";
2250 push @query_params, "%$author%";
2253 if ( $isbn ) {
2254 $query .= " AND biblioitems.isbn LIKE ? ";
2255 push @query_params, "%$isbn%";
2257 if ( $ean ) {
2258 $query .= " AND biblioitems.ean = ? ";
2259 push @query_params, "$ean";
2261 if ( $name ) {
2262 $query .= " AND aqbooksellers.name LIKE ? ";
2263 push @query_params, "%$name%";
2266 if ( $budget ) {
2267 $query .= " AND aqbudgets.budget_id = ? ";
2268 push @query_params, "$budget";
2271 if ( $from_placed_on ) {
2272 $query .= " AND creationdate >= ? ";
2273 push @query_params, $from_placed_on;
2276 if ( $to_placed_on ) {
2277 $query .= " AND creationdate <= ? ";
2278 push @query_params, $to_placed_on;
2281 if ( defined $orderstatus and $orderstatus ne '') {
2282 $query .= " AND aqorders.orderstatus = ? ";
2283 push @query_params, "$orderstatus";
2286 if ($basket) {
2287 if ($basket =~ m/^\d+$/) {
2288 $query .= " AND aqorders.basketno = ? ";
2289 push @query_params, $basket;
2290 } else {
2291 $query .= " AND aqbasket.basketname LIKE ? ";
2292 push @query_params, "%$basket%";
2296 if ($booksellerinvoicenumber) {
2297 $query .= " AND aqinvoices.invoicenumber LIKE ? ";
2298 push @query_params, "%$booksellerinvoicenumber%";
2301 if ($basketgroupname) {
2302 $query .= " AND aqbasketgroups.name LIKE ? ";
2303 push @query_params, "%$basketgroupname%";
2306 if ($ordernumber) {
2307 $query .= " AND (aqorders.ordernumber = ? ";
2308 push @query_params, $ordernumber;
2309 if ($search_children_too) {
2310 $query .= " OR aqorders.parent_ordernumber = ? ";
2311 push @query_params, $ordernumber;
2313 $query .= ") ";
2316 if ( @$created_by ) {
2317 $query .= ' AND aqbasket.authorisedby IN ( ' . join( ',', ('?') x @$created_by ) . ')';
2318 push @query_params, @$created_by;
2322 if ( C4::Context->preference("IndependentBranches") ) {
2323 unless ( C4::Context->IsSuperLibrarian() ) {
2324 $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
2325 push @query_params, C4::Context->userenv->{branch};
2328 $query .= " ORDER BY id";
2330 return $dbh->selectall_arrayref( $query, { Slice => {} }, @query_params );
2333 =head2 GetRecentAcqui
2335 $results = GetRecentAcqui($days);
2337 C<$results> is a ref to a table which containts hashref
2339 =cut
2341 sub GetRecentAcqui {
2342 my $limit = shift;
2343 my $dbh = C4::Context->dbh;
2344 my $query = "
2345 SELECT *
2346 FROM biblio
2347 ORDER BY timestamp DESC
2348 LIMIT 0,".$limit;
2350 my $sth = $dbh->prepare($query);
2351 $sth->execute;
2352 my $results = $sth->fetchall_arrayref({});
2353 return $results;
2356 #------------------------------------------------------------#
2358 =head3 AddClaim
2360 &AddClaim($ordernumber);
2362 Add a claim for an order
2364 =cut
2366 sub AddClaim {
2367 my ($ordernumber) = @_;
2368 my $dbh = C4::Context->dbh;
2369 my $query = "
2370 UPDATE aqorders SET
2371 claims_count = claims_count + 1,
2372 claimed_date = CURDATE()
2373 WHERE ordernumber = ?
2375 my $sth = $dbh->prepare($query);
2376 $sth->execute($ordernumber);
2379 =head3 GetInvoices
2381 my @invoices = GetInvoices(
2382 invoicenumber => $invoicenumber,
2383 supplierid => $supplierid,
2384 suppliername => $suppliername,
2385 shipmentdatefrom => $shipmentdatefrom, # ISO format
2386 shipmentdateto => $shipmentdateto, # ISO format
2387 billingdatefrom => $billingdatefrom, # ISO format
2388 billingdateto => $billingdateto, # ISO format
2389 isbneanissn => $isbn_or_ean_or_issn,
2390 title => $title,
2391 author => $author,
2392 publisher => $publisher,
2393 publicationyear => $publicationyear,
2394 branchcode => $branchcode,
2395 order_by => $order_by
2398 Return a list of invoices that match all given criteria.
2400 $order_by is "column_name (asc|desc)", where column_name is any of
2401 'invoicenumber', 'booksellerid', 'shipmentdate', 'billingdate', 'closedate',
2402 'shipmentcost', 'shipmentcost_budgetid'.
2404 asc is the default if omitted
2406 =cut
2408 sub GetInvoices {
2409 my %args = @_;
2411 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2412 closedate shipmentcost shipmentcost_budgetid);
2414 my $dbh = C4::Context->dbh;
2415 my $query = qq{
2416 SELECT aqinvoices.*, aqbooksellers.name AS suppliername,
2417 COUNT(
2418 DISTINCT IF(
2419 aqorders.datereceived IS NOT NULL,
2420 aqorders.biblionumber,
2421 NULL
2423 ) AS receivedbiblios,
2424 COUNT(
2425 DISTINCT IF(
2426 aqorders.subscriptionid IS NOT NULL,
2427 aqorders.subscriptionid,
2428 NULL
2430 ) AS is_linked_to_subscriptions,
2431 SUM(aqorders.quantityreceived) AS receiveditems
2432 FROM aqinvoices
2433 LEFT JOIN aqbooksellers ON aqbooksellers.id = aqinvoices.booksellerid
2434 LEFT JOIN aqorders ON aqorders.invoiceid = aqinvoices.invoiceid
2435 LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
2436 LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
2437 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2438 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
2439 LEFT JOIN subscription ON biblio.biblionumber = subscription.biblionumber
2442 my @bind_args;
2443 my @bind_strs;
2444 if($args{supplierid}) {
2445 push @bind_strs, " aqinvoices.booksellerid = ? ";
2446 push @bind_args, $args{supplierid};
2448 if($args{invoicenumber}) {
2449 push @bind_strs, " aqinvoices.invoicenumber LIKE ? ";
2450 push @bind_args, "%$args{invoicenumber}%";
2452 if($args{suppliername}) {
2453 push @bind_strs, " aqbooksellers.name LIKE ? ";
2454 push @bind_args, "%$args{suppliername}%";
2456 if($args{shipmentdatefrom}) {
2457 push @bind_strs, " aqinvoices.shipmentdate >= ? ";
2458 push @bind_args, $args{shipmentdatefrom};
2460 if($args{shipmentdateto}) {
2461 push @bind_strs, " aqinvoices.shipmentdate <= ? ";
2462 push @bind_args, $args{shipmentdateto};
2464 if($args{billingdatefrom}) {
2465 push @bind_strs, " aqinvoices.billingdate >= ? ";
2466 push @bind_args, $args{billingdatefrom};
2468 if($args{billingdateto}) {
2469 push @bind_strs, " aqinvoices.billingdate <= ? ";
2470 push @bind_args, $args{billingdateto};
2472 if($args{isbneanissn}) {
2473 push @bind_strs, " (biblioitems.isbn LIKE CONCAT('%', ?, '%') OR biblioitems.ean LIKE CONCAT('%', ?, '%') OR biblioitems.issn LIKE CONCAT('%', ?, '%') ) ";
2474 push @bind_args, $args{isbneanissn}, $args{isbneanissn}, $args{isbneanissn};
2476 if($args{title}) {
2477 push @bind_strs, " biblio.title LIKE CONCAT('%', ?, '%') ";
2478 push @bind_args, $args{title};
2480 if($args{author}) {
2481 push @bind_strs, " biblio.author LIKE CONCAT('%', ?, '%') ";
2482 push @bind_args, $args{author};
2484 if($args{publisher}) {
2485 push @bind_strs, " biblioitems.publishercode LIKE CONCAT('%', ?, '%') ";
2486 push @bind_args, $args{publisher};
2488 if($args{publicationyear}) {
2489 push @bind_strs, " ((biblioitems.publicationyear LIKE CONCAT('%', ?, '%')) OR (biblio.copyrightdate LIKE CONCAT('%', ?, '%'))) ";
2490 push @bind_args, $args{publicationyear}, $args{publicationyear};
2492 if($args{branchcode}) {
2493 push @bind_strs, " borrowers.branchcode = ? ";
2494 push @bind_args, $args{branchcode};
2497 $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
2498 $query .= " GROUP BY aqinvoices.invoiceid ";
2500 if($args{order_by}) {
2501 my ($column, $direction) = split / /, $args{order_by};
2502 if(grep /^$column$/, @columns) {
2503 $direction ||= 'ASC';
2504 $query .= " ORDER BY $column $direction";
2508 my $sth = $dbh->prepare($query);
2509 $sth->execute(@bind_args);
2511 my $results = $sth->fetchall_arrayref({});
2512 return @$results;
2515 =head3 GetInvoice
2517 my $invoice = GetInvoice($invoiceid);
2519 Get informations about invoice with given $invoiceid
2521 Return a hash filled with aqinvoices.* fields
2523 =cut
2525 sub GetInvoice {
2526 my ($invoiceid) = @_;
2527 my $invoice;
2529 return unless $invoiceid;
2531 my $dbh = C4::Context->dbh;
2532 my $query = qq{
2533 SELECT *
2534 FROM aqinvoices
2535 WHERE invoiceid = ?
2537 my $sth = $dbh->prepare($query);
2538 $sth->execute($invoiceid);
2540 $invoice = $sth->fetchrow_hashref;
2541 return $invoice;
2544 =head3 GetInvoiceDetails
2546 my $invoice = GetInvoiceDetails($invoiceid)
2548 Return informations about an invoice + the list of related order lines
2550 Orders informations are in $invoice->{orders} (array ref)
2552 =cut
2554 sub GetInvoiceDetails {
2555 my ($invoiceid) = @_;
2557 if ( !defined $invoiceid ) {
2558 carp 'GetInvoiceDetails called without an invoiceid';
2559 return;
2562 my $dbh = C4::Context->dbh;
2563 my $query = q{
2564 SELECT aqinvoices.*, aqbooksellers.name AS suppliername
2565 FROM aqinvoices
2566 LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
2567 WHERE invoiceid = ?
2569 my $sth = $dbh->prepare($query);
2570 $sth->execute($invoiceid);
2572 my $invoice = $sth->fetchrow_hashref;
2574 $query = q{
2575 SELECT aqorders.*,
2576 biblio.*,
2577 biblio.copyrightdate,
2578 biblioitems.publishercode,
2579 biblioitems.publicationyear,
2580 aqbasket.basketname,
2581 aqbasketgroups.id AS basketgroupid,
2582 aqbasketgroups.name AS basketgroupname
2583 FROM aqorders
2584 LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
2585 LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
2586 LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2587 LEFT JOIN biblioitems ON aqorders.biblionumber = biblioitems.biblionumber
2588 WHERE invoiceid = ?
2590 $sth = $dbh->prepare($query);
2591 $sth->execute($invoiceid);
2592 $invoice->{orders} = $sth->fetchall_arrayref({});
2593 $invoice->{orders} ||= []; # force an empty arrayref if fetchall_arrayref fails
2595 return $invoice;
2598 =head3 AddInvoice
2600 my $invoiceid = AddInvoice(
2601 invoicenumber => $invoicenumber,
2602 booksellerid => $booksellerid,
2603 shipmentdate => $shipmentdate,
2604 billingdate => $billingdate,
2605 closedate => $closedate,
2606 shipmentcost => $shipmentcost,
2607 shipmentcost_budgetid => $shipmentcost_budgetid
2610 Create a new invoice and return its id or undef if it fails.
2612 =cut
2614 sub AddInvoice {
2615 my %invoice = @_;
2617 return unless(%invoice and $invoice{invoicenumber});
2619 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2620 closedate shipmentcost shipmentcost_budgetid);
2622 my @set_strs;
2623 my @set_args;
2624 foreach my $key (keys %invoice) {
2625 if(0 < grep(/^$key$/, @columns)) {
2626 push @set_strs, "$key = ?";
2627 push @set_args, ($invoice{$key} || undef);
2631 my $rv;
2632 if(@set_args > 0) {
2633 my $dbh = C4::Context->dbh;
2634 my $query = "INSERT INTO aqinvoices SET ";
2635 $query .= join (",", @set_strs);
2636 my $sth = $dbh->prepare($query);
2637 $rv = $sth->execute(@set_args);
2638 if($rv) {
2639 $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
2642 return $rv;
2645 =head3 ModInvoice
2647 ModInvoice(
2648 invoiceid => $invoiceid, # Mandatory
2649 invoicenumber => $invoicenumber,
2650 booksellerid => $booksellerid,
2651 shipmentdate => $shipmentdate,
2652 billingdate => $billingdate,
2653 closedate => $closedate,
2654 shipmentcost => $shipmentcost,
2655 shipmentcost_budgetid => $shipmentcost_budgetid
2658 Modify an invoice, invoiceid is mandatory.
2660 Return undef if it fails.
2662 =cut
2664 sub ModInvoice {
2665 my %invoice = @_;
2667 return unless(%invoice and $invoice{invoiceid});
2669 my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2670 closedate shipmentcost shipmentcost_budgetid);
2672 my @set_strs;
2673 my @set_args;
2674 foreach my $key (keys %invoice) {
2675 if(0 < grep(/^$key$/, @columns)) {
2676 push @set_strs, "$key = ?";
2677 push @set_args, ($invoice{$key} || undef);
2681 my $dbh = C4::Context->dbh;
2682 my $query = "UPDATE aqinvoices SET ";
2683 $query .= join(",", @set_strs);
2684 $query .= " WHERE invoiceid = ?";
2686 my $sth = $dbh->prepare($query);
2687 $sth->execute(@set_args, $invoice{invoiceid});
2690 =head3 CloseInvoice
2692 CloseInvoice($invoiceid);
2694 Close an invoice.
2696 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
2698 =cut
2700 sub CloseInvoice {
2701 my ($invoiceid) = @_;
2703 return unless $invoiceid;
2705 my $dbh = C4::Context->dbh;
2706 my $query = qq{
2707 UPDATE aqinvoices
2708 SET closedate = CAST(NOW() AS DATE)
2709 WHERE invoiceid = ?
2711 my $sth = $dbh->prepare($query);
2712 $sth->execute($invoiceid);
2715 =head3 ReopenInvoice
2717 ReopenInvoice($invoiceid);
2719 Reopen an invoice
2721 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => C4::Dates->new()->output('iso'))
2723 =cut
2725 sub ReopenInvoice {
2726 my ($invoiceid) = @_;
2728 return unless $invoiceid;
2730 my $dbh = C4::Context->dbh;
2731 my $query = qq{
2732 UPDATE aqinvoices
2733 SET closedate = NULL
2734 WHERE invoiceid = ?
2736 my $sth = $dbh->prepare($query);
2737 $sth->execute($invoiceid);
2740 =head3 DelInvoice
2742 DelInvoice($invoiceid);
2744 Delete an invoice if there are no items attached to it.
2746 =cut
2748 sub DelInvoice {
2749 my ($invoiceid) = @_;
2751 return unless $invoiceid;
2753 my $dbh = C4::Context->dbh;
2754 my $query = qq{
2755 SELECT COUNT(*)
2756 FROM aqorders
2757 WHERE invoiceid = ?
2759 my $sth = $dbh->prepare($query);
2760 $sth->execute($invoiceid);
2761 my $res = $sth->fetchrow_arrayref;
2762 if ( $res && $res->[0] == 0 ) {
2763 $query = qq{
2764 DELETE FROM aqinvoices
2765 WHERE invoiceid = ?
2767 my $sth = $dbh->prepare($query);
2768 return ( $sth->execute($invoiceid) > 0 );
2770 return;
2773 =head3 MergeInvoices
2775 MergeInvoices($invoiceid, \@sourceids);
2777 Merge the invoices identified by the IDs in \@sourceids into
2778 the invoice identified by $invoiceid.
2780 =cut
2782 sub MergeInvoices {
2783 my ($invoiceid, $sourceids) = @_;
2785 return unless $invoiceid;
2786 foreach my $sourceid (@$sourceids) {
2787 next if $sourceid == $invoiceid;
2788 my $source = GetInvoiceDetails($sourceid);
2789 foreach my $order (@{$source->{'orders'}}) {
2790 $order->{'invoiceid'} = $invoiceid;
2791 ModOrder($order);
2793 DelInvoice($source->{'invoiceid'});
2795 return;
2798 =head3 GetBiblioCountByBasketno
2800 $biblio_count = &GetBiblioCountByBasketno($basketno);
2802 Looks up the biblio's count that has basketno value $basketno
2804 Returns a quantity
2806 =cut
2808 sub GetBiblioCountByBasketno {
2809 my ($basketno) = @_;
2810 my $dbh = C4::Context->dbh;
2811 my $query = "
2812 SELECT COUNT( DISTINCT( biblionumber ) )
2813 FROM aqorders
2814 WHERE basketno = ?
2815 AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
2818 my $sth = $dbh->prepare($query);
2819 $sth->execute($basketno);
2820 return $sth->fetchrow;
2823 # This is *not* the good way to calcul prices
2824 # But it's how it works at the moment into Koha
2825 # This will be fixed later.
2826 # Note this subroutine should be moved to Koha::Acquisition::Order
2827 # Will do when a DBIC decision will be taken.
2828 sub populate_order_with_prices {
2829 my ($params) = @_;
2831 my $order = $params->{order};
2832 my $booksellerid = $params->{booksellerid};
2833 return unless $booksellerid;
2835 my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
2837 my $receiving = $params->{receiving};
2838 my $ordering = $params->{ordering};
2839 my $discount = $order->{discount};
2840 $discount /= 100 if $discount > 1;
2842 $order->{rrp} = Koha::Number::Price->new( $order->{rrp} )->round;
2843 $order->{ecost} = Koha::Number::Price->new( $order->{ecost} )->round;
2844 if ($ordering) {
2845 if ( $bookseller->{listincgst} ) {
2846 $order->{rrpgsti} = $order->{rrp};
2847 $order->{rrpgste} = Koha::Number::Price->new(
2848 $order->{rrpgsti} / ( 1 + $order->{gstrate} ) )->round;
2849 $order->{ecostgsti} = $order->{ecost};
2850 $order->{ecostgste} = Koha::Number::Price->new(
2851 $order->{ecost} / ( 1 + $order->{gstrate} ) )->round;
2852 $order->{gstvalue} = Koha::Number::Price->new(
2853 ( $order->{ecostgsti} - $order->{ecostgste} ) *
2854 $order->{quantity} )->round;
2855 $order->{totalgste} = $order->{ecostgste} * $order->{quantity};
2856 $order->{totalgsti} = $order->{ecostgsti} * $order->{quantity};
2858 else {
2859 $order->{rrpgste} = $order->{rrp};
2860 $order->{rrpgsti} = Koha::Number::Price->new(
2861 $order->{rrp} * ( 1 + $order->{gstrate} ) )->round;
2862 $order->{ecostgste} = $order->{ecost};
2863 $order->{ecostgsti} = Koha::Number::Price->new(
2864 $order->{ecost} * ( 1 + $order->{gstrate} ) )->round;
2865 $order->{gstvalue} = Koha::Number::Price->new(
2866 ( $order->{ecostgsti} - $order->{ecostgste} ) *
2867 $order->{quantity} )->round;
2868 $order->{totalgste} = $order->{ecostgste} * $order->{quantity};
2869 $order->{totalgsti} = $order->{ecostgsti} * $order->{quantity};
2873 # Not used yet
2874 #if ($receiving) {
2875 # if ( $bookseller->{invoiceincgst} ) {
2876 # $order->{unitpricegsti} = $order->{unitprice};
2877 # $order->{unitpricegste} =
2878 # $order->{unitpricegsti} / ( 1 + $order->{gstrate} );
2880 # else {
2881 # $order->{unitpricegste} = $order->{unitprice};
2882 # $order->{unitpricegsti} =
2883 # $order->{unitpricegste} * ( 1 + $order->{gstrate} );
2885 # $order->{gstvalue} =
2886 # $order->{quantityreceived} *
2887 # $order->{unitpricegste} *
2888 # $order->{gstrate};
2891 return $order;
2895 __END__
2897 =head1 AUTHOR
2899 Koha Development Team <http://koha-community.org/>
2901 =cut