bug fix:
[koha.git] / C4 / Acquisition.pm
1 package C4::Acquisition;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # $Id$
21
22 use strict;
23 require Exporter;
24 use C4::Context;
25 use C4::Date;
26 use MARC::Record;
27 use C4::Suggestions;
28 use Time::localtime;
29
30 use vars qw($VERSION @ISA @EXPORT);
31
32 # set the version for version checking
33 $VERSION = do { my @v = '$Revision$' =~ /\d+/g; shift(@v) . "." . join( "_", map { sprintf "%03d", $_ } @v ); };
34
35 # used in receiveorder subroutine
36 # to provide library specific handling
37 my $library_name = C4::Context->preference("LibraryName");
38
39 =head1 NAME
40
41 C4::Acquisition - Koha functions for dealing with orders and acquisitions
42
43 =head1 SYNOPSIS
44
45 use C4::Acquisition;
46
47 =head1 DESCRIPTION
48
49 The functions in this module deal with acquisitions, managing book
50 orders, basket and parcels.
51
52 =head1 FUNCTIONS
53
54 =over 2
55
56 =cut
57
58 @ISA    = qw(Exporter);
59 @EXPORT = qw(
60   &GetBasket &NewBasket &CloseBasket
61   &GetPendingOrders &GetOrder &GetOrders
62   &GetOrderNumber &GetLateOrders &NewOrder &DelOrder
63   &SearchOrder &GetHistory &GetRecentAcqui
64   &ModOrder &ModReceiveOrder &ModOrderBiblioNumber
65   &GetParcels &GetParcel
66 );
67
68 =head2 FUNCTIONS ABOUT BASKETS
69
70 =over 2
71
72 =cut
73
74 #------------------------------------------------------------#
75
76 =head3 GetBasket
77
78 =over 4
79
80 $aqbasket = &GetBasket($basketnumber);
81
82 get all basket informations in aqbasket for a given basket
83
84 return :
85 informations for a given basket returned as a hashref.
86
87 =back
88
89 =back
90
91 =cut
92
93 sub GetBasket {
94     my ($basketno) = @_;
95     my $dbh        = C4::Context->dbh;
96     my $query = "
97         SELECT  aqbasket.*,
98                 borrowers.firstname+' '+borrowers.surname AS authorisedbyname,
99                 borrowers.branchcode AS branch
100         FROM    aqbasket
101         LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
102         WHERE basketno=?
103     ";
104     my $sth=$dbh->prepare($query);
105     $sth->execute($basketno);
106     return ( $sth->fetchrow_hashref );
107 }
108
109 #------------------------------------------------------------#
110
111 =head3 NewBasket
112
113 =over 4
114
115 $basket = &NewBasket();
116
117 Create a new basket in aqbasket table
118
119 =back
120
121 =cut
122
123 # FIXME : this function seems to be unused.
124
125 sub NewBasket {
126     my ( $booksellerid, $authorisedby ) = @_;
127     my $dbh = C4::Context->dbh;
128     my $query = "
129         INSERT INTO aqbasket
130                 (creationdate,booksellerid,authorisedby)
131         VALUES  (now(),'$booksellerid','$authorisedby')
132     ";
133     my $sth =
134       $dbh->do($query);
135
136 #find & return basketno MYSQL dependant, but $dbh->last_insert_id always returns null :-(
137     my $basket = $dbh->{'mysql_insertid'};
138     return $basket;
139 }
140
141 #------------------------------------------------------------#
142
143 =head3 CloseBasket
144
145 =over 4
146
147 &CloseBasket($basketno);
148
149 close a basket (becomes unmodifiable,except for recieves)
150
151 =back
152
153 =cut
154
155 sub CloseBasket {
156     my ($basketno) = @_;
157     my $dbh        = C4::Context->dbh;
158     my $query = "
159         UPDATE aqbasket
160         SET    closedate=now()
161         WHERE  basketno=?
162     ";
163     my $sth = $dbh->prepare($query);
164     $sth->execute($basketno);
165 }
166
167 #------------------------------------------------------------#
168
169 =back
170
171 =head2 FUNCTIONS ABOUT ORDERS
172
173 =over 2
174
175 =cut
176
177 #------------------------------------------------------------#
178
179 =head3 GetPendingOrders
180
181 =over 4
182
183 $orders = &GetPendingOrders($booksellerid, $grouped);
184
185 Finds pending orders from the bookseller with the given ID. Ignores
186 completed and cancelled orders.
187
188 C<$orders> is a reference-to-array; each element is a
189 reference-to-hash with the following fields:
190 C<$grouped> is a boolean that, if set to 1 will group all order lines of the same basket
191 in a single result line 
192
193 =over 2
194
195 =item C<authorizedby>
196
197 =item C<entrydate>
198
199 =item C<basketno>
200
201 These give the value of the corresponding field in the aqorders table
202 of the Koha database.
203
204 =back
205
206 =back
207
208 Results are ordered from most to least recent.
209
210 =cut
211
212 sub GetPendingOrders {
213     my ($supplierid,$grouped) = @_;
214     my $dbh = C4::Context->dbh;
215     my $strsth = "
216         SELECT    ".($grouped?"count(*),":"")."aqbasket.basketno,
217                     surname,firstname,aqorders.*,
218                     aqbasket.closedate, aqbasket.creationdate
219         FROM      aqorders
220         LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
221         LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
222         WHERE booksellerid=?
223             AND (quantity > quantityreceived OR quantityreceived is NULL)
224             AND datecancellationprinted IS NULL
225             AND (to_days(now())-to_days(closedate) < 180 OR closedate IS NULL)
226     ";
227     ## FIXME  Why 180 days ???
228     if ( C4::Context->preference("IndependantBranches") ) {
229         my $userenv = C4::Context->userenv;
230         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
231             $strsth .=
232                 " and (borrowers.branchcode = '"
233               . $userenv->{branch}
234               . "' or borrowers.branchcode ='')";
235         }
236     }
237     $strsth .= " group by aqbasket.basketno" if $grouped;
238     $strsth .= " order by aqbasket.basketno";
239
240     my $sth = $dbh->prepare($strsth);
241     $sth->execute($supplierid);
242     my $results = $sth->fetchall_arrayref({});
243     $sth->finish;
244     return $results;
245 }
246
247 #------------------------------------------------------------#
248
249 =head3 GetOrders
250
251 =over 4
252
253 @orders = &GetOrders($basketnumber, $orderby);
254
255 Looks up the pending (non-cancelled) orders with the given basket
256 number. If C<$booksellerID> is non-empty, only orders from that seller
257 are returned.
258
259 return :
260 C<&basket> returns a two-element array. C<@orders> is an array of
261 references-to-hash, whose keys are the fields from the aqorders,
262 biblio, and biblioitems tables in the Koha database.
263
264 =back
265
266 =cut
267
268 sub GetOrders {
269     my ( $basketno, $orderby ) = @_;
270     my $dbh   = C4::Context->dbh;
271     my $query  ="
272          SELECT  aqorderbreakdown.*,
273                 biblio.*,biblioitems.*,
274                 aqorders.*,
275                 aqbookfund.bookfundname,
276                 biblio.title
277         FROM    aqorders
278             LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
279             LEFT JOIN biblio           ON biblio.biblionumber=aqorders.biblionumber
280             LEFT JOIN biblioitems      ON biblioitems.biblioitemnumber=aqorders.biblioitemnumber
281             LEFT JOIN aqbookfund       ON aqbookfund.bookfundid=aqorderbreakdown.bookfundid
282         WHERE   basketno=?
283             AND (datecancellationprinted IS NULL OR datecancellationprinted='0000-00-00')
284     ";
285
286     $orderby = "biblioitems.publishercode" unless $orderby;
287     $query .= " ORDER BY $orderby";
288     my $sth = $dbh->prepare($query);
289     $sth->execute($basketno);
290     my @results;
291
292     while ( my $data = $sth->fetchrow_hashref ) {
293         push @results, $data;
294     }
295     $sth->finish;
296     return @results;
297 }
298
299 #------------------------------------------------------------#
300
301 =head3 GetOrderNumber
302
303 =over 4
304
305 $ordernumber = &GetOrderNumber($biblioitemnumber, $biblionumber);
306
307 Looks up the ordernumber with the given biblionumber and biblioitemnumber.
308
309 Returns the number of this order.
310
311 =item C<$ordernumber> is the order number.
312
313 =back
314
315 =cut
316 sub GetOrderNumber {
317     my ( $biblionumber,$biblioitemnumber ) = @_;
318     my $dbh = C4::Context->dbh;
319     my $query = "
320         SELECT ordernumber
321         FROM   aqorders
322         WHERE  biblionumber=?
323         AND    biblioitemnumber=?
324     ";
325     my $sth = $dbh->prepare($query);
326     $sth->execute( $biblionumber, $biblioitemnumber );
327
328     return $sth->fetchrow;
329 }
330
331 #------------------------------------------------------------#
332
333 =head3 GetOrder
334
335 =over 4
336
337 $order = &GetOrder($ordernumber);
338
339 Looks up an order by order number.
340
341 Returns a reference-to-hash describing the order. The keys of
342 C<$order> are fields from the biblio, biblioitems, aqorders, and
343 aqorderbreakdown tables of the Koha database.
344
345 =back
346
347 =cut
348
349 sub GetOrder {
350     my ($ordnum) = @_;
351     my $dbh      = C4::Context->dbh;
352     my $query = "
353         SELECT *
354         FROM   aqorders
355         LEFT JOIN aqorderbreakdown ON aqorders.ordernumber=aqorderbreakdown.ordernumber
356         LEFT JOIN biblio on           biblio.biblionumber=aqorders.biblionumber
357         LEFT JOIN biblioitems on       biblioitems.biblionumber=aqorders.biblionumber
358         WHERE aqorders.ordernumber=?
359
360     ";
361     my $sth= $dbh->prepare($query);
362     $sth->execute($ordnum);
363     my $data = $sth->fetchrow_hashref;
364     $sth->finish;
365     return $data;
366 }
367
368 #------------------------------------------------------------#
369
370 =head3 NewOrder
371
372 =over 4
373
374   &NewOrder($basket, $biblionumber, $title, $quantity, $listprice,
375     $booksellerid, $who, $notes, $bookfund, $biblioitemnumber, $rrp,
376     $ecost, $gst, $budget, $unitprice, $subscription,
377     $booksellerinvoicenumber);
378
379 Adds a new order to the database. Any argument that isn't described
380 below is the new value of the field with the same name in the aqorders
381 table of the Koha database.
382
383 C<$ordnum> is a "minimum order number." After adding the new entry to
384 the aqorders table, C<&neworder> finds the first entry in aqorders
385 with order number greater than or equal to C<$ordnum>, and adds an
386 entry to the aqorderbreakdown table, with the order number just found,
387 and the book fund ID of the newly-added order.
388
389 C<$budget> is effectively ignored.
390
391 C<$subscription> may be either "yes", or anything else for "no".
392
393 =back
394
395 =cut
396
397 sub NewOrder {
398    my (
399         $basketno,  $bibnum,       $title,        $quantity,
400         $listprice, $booksellerid, $authorisedby, $notes,
401         $bookfund,  $bibitemnum,   $rrp,          $ecost,
402         $gst,       $budget,       $cost,         $sub,
403         $invoice,   $sort1,        $sort2
404       )
405       = @_;
406
407     my $year  = localtime->year() + 1900;
408     my $month = localtime->mon() + 1;       # months starts at 0, add 1
409
410     if ( !$budget || $budget eq 'now' ) {
411         $budget = "now()";
412     }
413
414     # if month is july or more, budget start is 1 jul, next year.
415     elsif ( $month >= '7' ) {
416         ++$year;                            # add 1 to year , coz its next year
417         $budget = "'$year-07-01'";
418     }
419     else {
420
421         # START OF NEW BUDGET, 1ST OF JULY, THIS YEAR
422         $budget = "'$year-07-01'";
423     }
424
425     if ( $sub eq 'yes' ) {
426         $sub = 1;
427     }
428     else {
429         $sub = 0;
430     }
431
432     # if $basket empty, it's also a new basket, create it
433     unless ($basketno) {
434         $basketno = NewBasket( $booksellerid, $authorisedby );
435     }
436
437     my $dbh = C4::Context->dbh;
438     my $query = "
439         INSERT INTO aqorders
440            ( biblionumber,title,basketno,quantity,listprice,notes,
441            biblioitemnumber,rrp,ecost,gst,unitprice,subscription,sort1,sort2,budgetdate,entrydate)
442         VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,$budget,now() )
443     ";
444     my $sth = $dbh->prepare($query);
445
446     $sth->execute(
447         $bibnum, $title,      $basketno, $quantity, $listprice,
448         $notes,  $bibitemnum, $rrp,      $ecost,    $gst,
449         $cost,   $sub,        $sort1,    $sort2
450     );
451     $sth->finish;
452
453     #get ordnum MYSQL dependant, but $dbh->last_insert_id returns null
454     my $ordnum = $dbh->{'mysql_insertid'};
455     $query = "
456         INSERT INTO aqorderbreakdown (ordernumber,bookfundid)
457         VALUES (?,?)
458     ";
459     $sth = $dbh->prepare($query);
460     $sth->execute( $ordnum, $bookfund );
461     $sth->finish;
462     return ( $basketno, $ordnum );
463 }
464
465 #------------------------------------------------------------#
466
467 =head3 ModOrder
468
469 =over 4
470
471 &ModOrder($title, $ordernumber, $quantity, $listprice,
472     $biblionumber, $basketno, $supplier, $who, $notes,
473     $bookfundid, $bibitemnum, $rrp, $ecost, $gst, $budget,
474     $unitprice, $booksellerinvoicenumber);
475
476 Modifies an existing order. Updates the order with order number
477 C<$ordernumber> and biblionumber C<$biblionumber>. All other arguments
478 update the fields with the same name in the aqorders table of the Koha
479 database.
480
481 Entries with order number C<$ordernumber> in the aqorderbreakdown
482 table are also updated to the new book fund ID.
483
484 =back
485
486 =cut
487
488 sub ModOrder {
489     my (
490         $title,      $ordnum,   $quantity, $listprice, $bibnum,
491         $basketno,   $supplier, $who,      $notes,     $bookfund,
492         $bibitemnum, $rrp,      $ecost,    $gst,       $budget,
493         $cost,       $invoice,  $sort1,    $sort2
494       )
495       = @_;
496     my $dbh = C4::Context->dbh;
497     my $query = "
498         UPDATE aqorders
499         SET    title=?,
500                quantity=?,listprice=?,basketno=?,
501                rrp=?,ecost=?,unitprice=?,booksellerinvoicenumber=?,
502                notes=?,sort1=?, sort2=?
503         WHERE  ordernumber=? AND biblionumber=?
504     ";
505     my $sth = $dbh->prepare($query);
506     $sth->execute(
507         $title, $quantity, $listprice, $basketno, $rrp,
508         $ecost, $cost,     $invoice,   $notes,    $sort1,
509         $sort2, $ordnum,   $bibnum
510     );
511     $sth->finish;
512     $query = "
513         UPDATE aqorderbreakdown
514         SET    bookfundid=?
515         WHERE  ordernumber=?
516     ";
517     $sth = $dbh->prepare($query);
518
519     unless ( $sth->execute( $bookfund, $ordnum ) )
520     {    # zero rows affected [Bug 734]
521         my $query ="
522             INSERT INTO aqorderbreakdown
523                      (ordernumber,bookfundid)
524             VALUES   (?,?)
525         ";
526         $sth = $dbh->prepare($query);
527         $sth->execute( $ordnum, $bookfund );
528     }
529     $sth->finish;
530 }
531
532 #------------------------------------------------------------#
533
534 =head3 ModOrderBiblioNumber
535
536 =over 4
537
538 &ModOrderBiblioNumber($biblioitemnumber,$ordnum, $biblionumber);
539
540 Modifies the biblioitemnumber for an existing order.
541 Updates the order with order number C<$ordernum> and biblionumber C<$biblionumber>.
542
543 =back
544
545 =cut
546
547 sub ModOrderBiblioNumber {
548     my ($biblioitemnumber,$ordnum, $biblionumber) = @_;
549     my $dbh = C4::Context->dbh;
550     my $query = "
551       UPDATE aqorders
552       SET    biblioitemnumber = ?
553       WHERE  ordernumber = ?
554       AND biblionumber =  ?";
555     my $sth = $dbh->prepare($query);
556     $sth->execute( $biblioitemnumber, $ordnum, $biblionumber );
557 }
558
559 #------------------------------------------------------------#
560
561 =head3 ModReceiveOrder
562
563 =over 4
564
565 &ModReceiveOrder($biblionumber, $ordernumber, $quantityreceived, $user,
566     $unitprice, $booksellerinvoicenumber, $biblioitemnumber,
567     $freight, $bookfund, $rrp);
568
569 Updates an order, to reflect the fact that it was received, at least
570 in part. All arguments not mentioned below update the fields with the
571 same name in the aqorders table of the Koha database.
572
573 Updates the order with bibilionumber C<$biblionumber> and ordernumber
574 C<$ordernumber>.
575
576 Also updates the book fund ID in the aqorderbreakdown table.
577
578 =back
579
580 =cut
581
582
583 sub ModReceiveOrder {
584     my (
585         $biblionumber,    $ordnum,  $quantrec, $user, $cost,
586         $invoiceno, $freight, $rrp, $bookfund, $daterecieved
587       )
588       = @_;
589     my $dbh = C4::Context->dbh;
590 #     warn "DATE BEFORE : $daterecieved";
591     $daterecieved=POSIX::strftime("%Y-%m-%d",CORE::localtime) unless $daterecieved;
592 #     warn "DATE REC : $daterecieved";
593     my $query = "
594         UPDATE aqorders
595         SET    quantityreceived=?,datereceived=?,booksellerinvoicenumber=?,
596                unitprice=?,freight=?,rrp=?
597         WHERE biblionumber=? AND ordernumber=?
598     ";
599     my $sth = $dbh->prepare($query);
600     my $suggestionid = GetSuggestionFromBiblionumber( $dbh, $biblionumber );
601     if ($suggestionid) {
602         ModStatus( $suggestionid, 'AVAILABLE', '', $biblionumber );
603     }
604     $sth->execute( $quantrec,$daterecieved, $invoiceno, $cost, $freight, $rrp, $biblionumber,
605         $ordnum);
606     $sth->finish;
607
608     # Allows libraries to change their bookfund during receiving orders
609     # allows them to adjust budgets
610     if ( C4::Context->preferene("LooseBudgets") ) {
611         my $query = "
612             UPDATE aqorderbreakdown
613             SET    bookfundid=?
614             WHERE  ordernumber=?
615         ";
616         my $sth = $dbh->prepare($query);
617         $sth->execute( $bookfund, $ordnum );
618         $sth->finish;
619     }
620     return $daterecieved;
621 }
622
623 #------------------------------------------------------------#
624
625 =head3 SearchOrder
626
627 @results = &SearchOrder($search, $biblionumber, $complete);
628
629 Searches for orders.
630
631 C<$search> may take one of several forms: if it is an ISBN,
632 C<&ordersearch> returns orders with that ISBN. If C<$search> is an
633 order number, C<&ordersearch> returns orders with that order number
634 and biblionumber C<$biblionumber>. Otherwise, C<$search> is considered
635 to be a space-separated list of search terms; in this case, all of the
636 terms must appear in the title (matching the beginning of title
637 words).
638
639 If C<$complete> is C<yes>, the results will include only completed
640 orders. In any case, C<&ordersearch> ignores cancelled orders.
641
642 C<&ordersearch> returns an array.
643 C<@results> is an array of references-to-hash with the following keys:
644
645 =over 4
646
647 =item C<author>
648
649 =item C<seriestitle>
650
651 =item C<branchcode>
652
653 =item C<bookfundid>
654
655 =back
656
657 =cut
658
659 sub SearchOrder {
660     my ( $search, $id, $biblionumber, $catview ) = @_;
661     my $dbh = C4::Context->dbh;
662     my @data = split( ' ', $search );
663     my @searchterms;
664     if ($id) {
665         @searchterms = ($id);
666     }
667     map { push( @searchterms, "$_%", "% $_%" ) } @data;
668     push( @searchterms, $search, $search, $biblionumber );
669     my $query;
670     if ($id) {
671         $query =
672           "SELECT *,biblio.title FROM aqorders,biblioitems,biblio,aqbasket
673             WHERE aqorders.biblioitemnumber = biblioitems.biblioitemnumber AND
674             aqorders.basketno = aqbasket.basketno
675             AND aqbasket.booksellerid = ?
676             AND biblio.biblionumber=aqorders.biblionumber
677             AND ((datecancellationprinted is NULL)
678             OR (datecancellationprinted = '0000-00-00'))
679             AND (("
680           . (
681             join( " AND ",
682                 map { "(biblio.title like ? or biblio.title like ?)" } @data )
683           )
684           . ") OR biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
685
686     }
687     else {
688         $query =
689           " SELECT *,biblio.title
690             FROM   aqorders,biblioitems,biblio,aqbasket
691             WHERE  aqorders.biblioitemnumber = biblioitems.biblioitemnumber
692             AND    aqorders.basketno = aqbasket.basketno
693             AND    biblio.biblionumber=aqorders.biblionumber
694             AND    ((datecancellationprinted is NULL)
695             OR     (datecancellationprinted = '0000-00-00'))
696             AND    (aqorders.quantityreceived < aqorders.quantity OR aqorders.quantityreceived is NULL)
697             AND (("
698           . (
699             join( " AND ",
700                 map { "(biblio.title like ? OR biblio.title like ?)" } @data )
701           )
702           . ") or biblioitems.isbn=? OR (aqorders.ordernumber=? AND aqorders.biblionumber=?)) ";
703     }
704     $query .= " GROUP BY aqorders.ordernumber";
705     ### $query
706     my $sth = $dbh->prepare($query);
707     $sth->execute(@searchterms);
708     my @results = ();
709     my $query2 = "
710         SELECT *
711         FROM   biblio
712         WHERE  biblionumber=?
713     ";
714     my $sth2 = $dbh->prepare($query2);
715     my $query3 = "
716         SELECT *
717         FROM   aqorderbreakdown
718         WHERE  ordernumber=?
719     ";
720     my $sth3 = $dbh->prepare($query3);
721
722     while ( my $data = $sth->fetchrow_hashref ) {
723         $sth2->execute( $data->{'biblionumber'} );
724         my $data2 = $sth2->fetchrow_hashref;
725         $data->{'author'}      = $data2->{'author'};
726         $data->{'seriestitle'} = $data2->{'seriestitle'};
727         $sth3->execute( $data->{'ordernumber'} );
728         my $data3 = $sth3->fetchrow_hashref;
729         $data->{'branchcode'} = $data3->{'branchcode'};
730         $data->{'bookfundid'} = $data3->{'bookfundid'};
731         push( @results, $data );
732     }
733     ### @results
734     $sth->finish;
735     $sth2->finish;
736     $sth3->finish;
737     return @results;
738 }
739
740 #------------------------------------------------------------#
741
742 =head3 DelOrder
743
744 =over 4
745
746 &DelOrder($biblionumber, $ordernumber);
747
748 Cancel the order with the given order and biblio numbers. It does not
749 delete any entries in the aqorders table, it merely marks them as
750 cancelled.
751
752 =back
753
754 =cut
755
756 sub DelOrder {
757     my ( $bibnum, $ordnum ) = @_;
758     my $dbh = C4::Context->dbh;
759     my $query = "
760         UPDATE aqorders
761         SET    datecancellationprinted=now()
762         WHERE  biblionumber=? AND ordernumber=?
763     ";
764     my $sth = $dbh->prepare($query);
765     $sth->execute( $bibnum, $ordnum );
766     $sth->finish;
767 }
768
769
770 =back
771
772 =back
773
774 =head2 FUNCTIONS ABOUT PARCELS
775
776 =over 2
777
778 =cut
779
780 #------------------------------------------------------------#
781
782 =head3 GetParcel
783
784 =over 4
785
786 @results = &GetParcel($booksellerid, $code, $date);
787
788 Looks up all of the received items from the supplier with the given
789 bookseller ID at the given date, for the given code (bookseller Invoice number). Ignores cancelled and completed orders.
790
791 C<@results> is an array of references-to-hash. The keys of each element are fields from
792 the aqorders, biblio, and biblioitems tables of the Koha database.
793
794 C<@results> is sorted alphabetically by book title.
795
796 =back
797
798 =cut
799
800 sub GetParcel {
801     #gets all orders from a certain supplier, orders them alphabetically
802     my ( $supplierid, $code, $datereceived ) = @_;
803     my $dbh     = C4::Context->dbh;
804     my @results = ();
805     $code .= '%'
806       if $code;  # add % if we search on a given code (otherwise, let him empty)
807     my $strsth ="
808         SELECT  authorisedby,
809                 creationdate,
810                 aqbasket.basketno,
811                 closedate,surname,
812                 firstname,
813                 aqorders.biblionumber,
814                 aqorders.title,
815                 aqorders.ordernumber,
816                 aqorders.quantity,
817                 aqorders.quantityreceived,
818                 aqorders.unitprice,
819                 aqorders.listprice,
820                 aqorders.rrp,
821                 aqorders.ecost
822         FROM aqorders,aqbasket
823         LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
824         WHERE aqbasket.basketno=aqorders.basketno
825             AND aqbasket.booksellerid=?
826             AND aqorders.booksellerinvoicenumber LIKE  \"$code\"
827             AND aqorders.datereceived= \'$datereceived\'";
828
829     if ( C4::Context->preference("IndependantBranches") ) {
830         my $userenv = C4::Context->userenv;
831         if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
832             $strsth .=
833                 " and (borrowers.branchcode = '"
834               . $userenv->{branch}
835               . "' or borrowers.branchcode ='')";
836         }
837     }
838     $strsth .= " order by aqbasket.basketno";
839     ### parcelinformation : $strsth
840     my $sth = $dbh->prepare($strsth);
841     $sth->execute($supplierid);
842     while ( my $data = $sth->fetchrow_hashref ) {
843         push( @results, $data );
844     }
845     ### countparcelbiblio: scalar(@results)
846     $sth->finish;
847
848     return @results;
849 }
850
851 #------------------------------------------------------------#
852
853 =head3 GetParcels
854
855 =over 4
856
857 $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
858 get a lists of parcels.
859
860 * Input arg :
861
862 =item $bookseller
863 is the bookseller this function has to get parcels.
864
865 =item $order
866 To know on what criteria the results list has to be ordered.
867
868 =item $code
869 is the booksellerinvoicenumber.
870
871 =item $datefrom & $dateto
872 to know on what date this function has to filter its search.
873
874 * return:
875 a pointer on a hash list containing parcel informations as such :
876
877 =item Creation date
878
879 =item Last operation
880
881 =item Number of biblio
882
883 =item Number of items
884
885 =back
886
887 =cut
888
889 sub GetParcels {
890     my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
891     my $dbh    = C4::Context->dbh;
892     my $strsth ="
893         SELECT  aqorders.booksellerinvoicenumber,
894                 datereceived,
895                 count(DISTINCT biblionumber) AS biblio,
896                 sum(quantity) AS itemsexpected,
897                 sum(quantityreceived) AS itemsreceived
898         FROM   aqorders, aqbasket
899         WHERE  aqbasket.basketno = aqorders.basketno
900              AND aqbasket.booksellerid = $bookseller and datereceived IS NOT NULL
901     ";
902
903     $strsth .= "and aqorders.booksellerinvoicenumber like \"$code%\" " if ($code);
904
905     $strsth .= "and datereceived >=" . $dbh->quote($datefrom) . " " if ($datefrom);
906
907     $strsth .= "and datereceived <=" . $dbh->quote($dateto) . " " if ($dateto);
908
909     $strsth .= "group by aqorders.booksellerinvoicenumber,datereceived ";
910     $strsth .= "order by $order " if ($order);
911 ### $strsth
912     my $sth = $dbh->prepare($strsth);
913
914     $sth->execute;
915     my $results = $sth->fetchall_arrayref({});
916     $sth->finish;
917     return @$results;
918 }
919
920 #------------------------------------------------------------#
921
922 =head3 GetLateOrders
923
924 =over 4
925
926 @results = &GetLateOrders;
927
928 Searches for bookseller with late orders.
929
930 return:
931 the table of supplier with late issues. This table is full of hashref.
932
933 =back
934
935 =cut
936
937 sub GetLateOrders {
938     my $delay      = shift;
939     my $supplierid = shift;
940     my $branch     = shift;
941
942     my $dbh = C4::Context->dbh;
943
944     #BEWARE, order of parenthesis and LEFT JOIN is important for speed
945     my $strsth;
946     my $dbdriver = C4::Context->config("db_scheme") || "mysql";
947
948     #    warn " $dbdriver";
949     if ( $dbdriver eq "mysql" ) {
950         $strsth = "
951             SELECT aqbasket.basketno,aqorders.ordernumber,
952                 DATE(aqbasket.closedate) AS orderdate,
953                 aqorders.quantity - IFNULL(aqorders.quantityreceived,0) AS quantity,
954                 aqorders.rrp AS unitpricesupplier,
955                 aqorders.ecost AS unitpricelib,
956                 (aqorders.quantity - IFNULL(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
957                 aqbookfund.bookfundname AS budget,
958                 borrowers.branchcode AS branch,
959                 aqbooksellers.name AS supplier,
960                 aqorders.title,
961                 biblio.author,
962                 biblioitems.publishercode AS publisher,
963                 biblioitems.publicationyear,
964                 DATEDIFF(CURDATE( ),closedate) AS latesince
965             FROM  (((
966                 (aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber)
967             LEFT JOIN biblioitems ON  biblioitems.biblionumber=biblio.biblionumber)
968             LEFT JOIN aqorderbreakdown ON aqorders.ordernumber = aqorderbreakdown.ordernumber)
969             LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
970             (aqbasket LEFT JOIN borrowers ON aqbasket.authorisedby = borrowers.borrowernumber)
971             LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
972             WHERE aqorders.basketno = aqbasket.basketno
973             AND (closedate < DATE_SUB(CURDATE( ),INTERVAL $delay DAY))
974             AND ((datereceived = '' OR datereceived is null)
975             OR (aqorders.quantityreceived < aqorders.quantity) )
976         ";
977         $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
978         $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'"
979           if ($branch);
980         $strsth .=
981           " AND borrowers.branchcode like \'"
982           . C4::Context->userenv->{branch} . "\'"
983           if ( C4::Context->preference("IndependantBranches")
984             && C4::Context->userenv
985             && C4::Context->userenv->{flags} != 1 );
986         $strsth .=" HAVING quantity<>0
987                     AND unitpricesupplier<>0
988                     AND unitpricelib<>0
989                     ORDER BY latesince,basketno,borrowers.branchcode, supplier
990         ";
991     }
992     else {
993         $strsth = "
994             SELECT aqbasket.basketno,
995                    DATE(aqbasket.closedate) AS orderdate,
996                     aqorders.quantity, aqorders.rrp AS unitpricesupplier,
997                     aqorders.ecost as unitpricelib,
998                     aqorders.quantity * aqorders.rrp AS subtotal
999                     aqbookfund.bookfundname AS budget,
1000                     borrowers.branchcode AS branch,
1001                     aqbooksellers.name AS supplier,
1002                     biblio.title,
1003                     biblio.author,
1004                     biblioitems.publishercode AS publisher,
1005                     biblioitems.publicationyear,
1006                     (CURDATE -  closedate) AS latesince
1007                     FROM(( (
1008                         (aqorders LEFT JOIN biblio on biblio.biblionumber = aqorders.biblionumber)
1009                         LEFT JOIN biblioitems on  biblioitems.biblionumber=biblio.biblionumber)
1010                         LEFT JOIN aqorderbreakdown on aqorders.ordernumber = aqorderbreakdown.ordernumber)
1011                         LEFT JOIN aqbookfund ON aqorderbreakdown.bookfundid = aqbookfund.bookfundid),
1012                         (aqbasket LEFT JOIN borrowers on aqbasket.authorisedby = borrowers.borrowernumber) LEFT JOIN aqbooksellers ON aqbasket.booksellerid = aqbooksellers.id
1013                     WHERE aqorders.basketno = aqbasket.basketno
1014                     AND (closedate < (CURDATE -(INTERVAL $delay DAY))
1015                     AND ((datereceived = '' OR datereceived is null)
1016                     OR (aqorders.quantityreceived < aqorders.quantity) ) ";
1017         $strsth .= " AND aqbasket.booksellerid = $supplierid " if ($supplierid);
1018
1019         $strsth .= " AND borrowers.branchcode like \'" . $branch . "\'" if ($branch);
1020         $strsth .=" AND borrowers.branchcode like \'". C4::Context->userenv->{branch} . "\'"
1021             if (C4::Context->preference("IndependantBranches") && C4::Context->userenv->{flags} != 1 );
1022         $strsth .=" ORDER BY latesince,basketno,borrowers.branchcode, supplier";
1023     }
1024     my $sth = $dbh->prepare($strsth);
1025     $sth->execute;
1026     my @results;
1027     my $hilighted = 1;
1028     while ( my $data = $sth->fetchrow_hashref ) {
1029         $data->{hilighted} = $hilighted if ( $hilighted > 0 );
1030         $data->{orderdate} = format_date( $data->{orderdate} );
1031         push @results, $data;
1032         $hilighted = -$hilighted;
1033     }
1034     $sth->finish;
1035     return @results;
1036 }
1037
1038 #------------------------------------------------------------#
1039
1040 =head3 GetHistory
1041
1042 =over 4
1043
1044 (\@order_loop, $total_qty, $total_price, $total_qtyreceived)=&GetHistory( $title, $author, $name, $from_placed_on, $to_placed_on )
1045
1046 this function get the search history.
1047
1048 =back
1049
1050 =cut
1051
1052 sub GetHistory {
1053     my ( $title, $author, $name, $from_placed_on, $to_placed_on ) = @_;
1054     my @order_loop;
1055     my $total_qty         = 0;
1056     my $total_qtyreceived = 0;
1057     my $total_price       = 0;
1058
1059 # don't run the query if there are no parameters (list would be too long for sure !)
1060     if ( $title || $author || $name || $from_placed_on || $to_placed_on ) {
1061         my $dbh   = C4::Context->dbh;
1062         my $query ="
1063             SELECT
1064                 biblio.title,
1065                 biblio.author,
1066                 aqorders.basketno,
1067                 name,aqbasket.creationdate,
1068                 aqorders.datereceived,
1069                 aqorders.quantity,
1070                 aqorders.quantityreceived,
1071                 aqorders.ecost,
1072                 aqorders.ordernumber,
1073                 aqorders.booksellerinvoicenumber as invoicenumber,
1074                 aqbooksellers.id as id
1075             FROM aqorders,aqbasket,aqbooksellers,biblio";
1076
1077         $query .= ",borrowers "
1078           if ( C4::Context->preference("IndependantBranches") );
1079
1080         $query .="
1081             WHERE aqorders.basketno=aqbasket.basketno
1082             AND   aqbasket.booksellerid=aqbooksellers.id
1083             AND   biblio.biblionumber=aqorders.biblionumber ";
1084
1085         $query .= " AND aqbasket.authorisedby=borrowers.borrowernumber"
1086           if ( C4::Context->preference("IndependantBranches") );
1087
1088         $query .= " AND biblio.title LIKE " . $dbh->quote( "%" . $title . "%" )
1089           if $title;
1090
1091         $query .=
1092           " AND biblio.author LIKE " . $dbh->quote( "%" . $author . "%" )
1093           if $author;
1094
1095         $query .= " AND name LIKE " . $dbh->quote( "%" . $name . "%" ) if $name;
1096
1097         $query .= " AND creationdate >" . $dbh->quote($from_placed_on)
1098           if $from_placed_on;
1099
1100         $query .= " AND creationdate<" . $dbh->quote($to_placed_on)
1101           if $to_placed_on;
1102
1103         if ( C4::Context->preference("IndependantBranches") ) {
1104             my $userenv = C4::Context->userenv;
1105             if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
1106                 $query .=
1107                     " AND (borrowers.branchcode = '"
1108                   . $userenv->{branch}
1109                   . "' OR borrowers.branchcode ='')";
1110             }
1111         }
1112         $query .= " ORDER BY booksellerid";
1113         my $sth = $dbh->prepare($query);
1114         $sth->execute;
1115         my $cnt = 1;
1116         while ( my $line = $sth->fetchrow_hashref ) {
1117             $line->{count} = $cnt++;
1118             $line->{toggle} = 1 if $cnt % 2;
1119             push @order_loop, $line;
1120             $line->{creationdate} = format_date( $line->{creationdate} );
1121             $line->{datereceived} = format_date( $line->{datereceived} );
1122             $total_qty         += $line->{'quantity'};
1123             $total_qtyreceived += $line->{'quantityreceived'};
1124             $total_price       += $line->{'quantity'} * $line->{'ecost'};
1125         }
1126     }
1127     return \@order_loop, $total_qty, $total_price, $total_qtyreceived;
1128 }
1129
1130 =head2 GetRecentAcqui
1131
1132    $results = GetRecentAcqui($days);
1133
1134    C<$results> is a ref to a table which containts hashref
1135
1136 =cut
1137
1138 sub GetRecentAcqui {
1139     my $limit  = shift;
1140     my $dbh    = C4::Context->dbh;
1141     my $query = "
1142         SELECT *
1143         FROM   biblio
1144         ORDER BY timestamp DESC
1145         LIMIT  0,".$limit;
1146
1147     my $sth = $dbh->prepare($query);
1148     $sth->execute;
1149     my @results;
1150     while(my $data = $sth->fetchrow_hashref){
1151         push @results,$data;
1152     }
1153     return \@results;
1154 }
1155
1156 END { }    # module clean-up code here (global destructor)
1157
1158 1;
1159
1160 __END__
1161
1162 =back
1163
1164 =head1 AUTHOR
1165
1166 Koha Developement team <info@koha.org>
1167
1168 =cut