Bug 16371: Move GetDailyQuote to get_daily_quote
[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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
21 use Modern::Perl;
22 use Carp;
23 use Text::CSV_XS;
24 use C4::Context;
25 use C4::Debug;
26 use C4::Suggestions;
27 use C4::Biblio;
28 use C4::Contract;
29 use C4::Debug;
30 use C4::Templates qw(gettemplate);
31 use Koha::DateUtils qw( dt_from_string output_pref );
32 use Koha::Acquisition::Baskets;
33 use Koha::Acquisition::Booksellers;
34 use Koha::Acquisition::Orders;
35 use Koha::Biblios;
36 use Koha::Exceptions;
37 use Koha::Items;
38 use Koha::Number::Price;
39 use Koha::Libraries;
40 use Koha::CsvProfiles;
41 use Koha::Patrons;
42
43 use C4::Koha;
44
45 use MARC::Field;
46 use MARC::Record;
47
48 use Time::localtime;
49
50 use vars qw(@ISA @EXPORT);
51
52 BEGIN {
53     require Exporter;
54     @ISA    = qw(Exporter);
55     @EXPORT = qw(
56         &GetBasket &NewBasket &CloseBasket &ReopenBasket &DelBasket &ModBasket
57         &GetBasketAsCSV &GetBasketGroupAsCSV
58         &GetBasketsByBookseller &GetBasketsByBasketgroup
59         &GetBasketsInfosByBookseller
60
61         &GetBasketUsers &ModBasketUsers
62         &CanUserManageBasket
63
64         &ModBasketHeader
65
66         &ModBasketgroup &NewBasketgroup &DelBasketgroup &GetBasketgroup &CloseBasketgroup
67         &GetBasketgroups &ReOpenBasketgroup
68
69         &DelOrder &ModOrder &GetOrder &GetOrders &GetOrdersByBiblionumber
70         &GetOrderFromItemnumber
71         &SearchOrders &GetHistory &GetRecentAcqui
72         &ModReceiveOrder &CancelReceipt
73         &TransferOrder
74         &ModItemOrder
75
76         &GetParcels
77
78         &GetInvoices
79         &GetInvoice
80         &GetInvoiceDetails
81         &AddInvoice
82         &ModInvoice
83         &CloseInvoice
84         &ReopenInvoice
85         &DelInvoice
86         &MergeInvoices
87
88         &AddClaim
89         &GetBiblioCountByBasketno
90
91         &GetOrderUsers
92         &ModOrderUsers
93         &NotifyOrderUsers
94
95         &FillWithDefaultValues
96
97         &get_rounded_price
98         &get_rounding_sql
99     );
100 }
101
102
103
104
105
106 sub GetOrderFromItemnumber {
107     my ($itemnumber) = @_;
108     my $dbh          = C4::Context->dbh;
109     my $query        = qq|
110
111     SELECT  * from aqorders    LEFT JOIN aqorders_items
112     ON (     aqorders.ordernumber = aqorders_items.ordernumber   )
113     WHERE itemnumber = ?  |;
114
115     my $sth = $dbh->prepare($query);
116
117 #    $sth->trace(3);
118
119     $sth->execute($itemnumber);
120
121     my $order = $sth->fetchrow_hashref;
122     return ( $order  );
123
124 }
125
126 =head1 NAME
127
128 C4::Acquisition - Koha functions for dealing with orders and acquisitions
129
130 =head1 SYNOPSIS
131
132 use C4::Acquisition;
133
134 =head1 DESCRIPTION
135
136 The functions in this module deal with acquisitions, managing book
137 orders, basket and parcels.
138
139 =head1 FUNCTIONS
140
141 =head2 FUNCTIONS ABOUT BASKETS
142
143 =head3 GetBasket
144
145   $aqbasket = &GetBasket($basketnumber);
146
147 get all basket informations in aqbasket for a given basket
148
149 B<returns:> informations for a given basket returned as a hashref.
150
151 =cut
152
153 sub GetBasket {
154     my ($basketno) = @_;
155     my $dbh        = C4::Context->dbh;
156     my $query = "
157         SELECT  aqbasket.*,
158                 concat( b.firstname,' ',b.surname) AS authorisedbyname
159         FROM    aqbasket
160         LEFT JOIN borrowers b ON aqbasket.authorisedby=b.borrowernumber
161         WHERE basketno=?
162     ";
163     my $sth=$dbh->prepare($query);
164     $sth->execute($basketno);
165     my $basket = $sth->fetchrow_hashref;
166     return ( $basket );
167 }
168
169 #------------------------------------------------------------#
170
171 =head3 NewBasket
172
173   $basket = &NewBasket( $booksellerid, $authorizedby, $basketname,
174       $basketnote, $basketbooksellernote, $basketcontractnumber, $deliveryplace, $billingplace, $is_standing, $create_items );
175
176 Create a new basket in aqbasket table
177
178 =over
179
180 =item C<$booksellerid> is a foreign key in the aqbasket table
181
182 =item C<$authorizedby> is the username of who created the basket
183
184 =back
185
186 The other parameters are optional, see ModBasketHeader for more info on them.
187
188 =cut
189
190 sub NewBasket {
191     my ( $booksellerid, $authorisedby, $basketname, $basketnote,
192         $basketbooksellernote, $basketcontractnumber, $deliveryplace,
193         $billingplace, $is_standing, $create_items ) = @_;
194     my $dbh = C4::Context->dbh;
195     my $query =
196         'INSERT INTO aqbasket (creationdate,booksellerid,authorisedby) '
197       . 'VALUES  (now(),?,?)';
198     $dbh->do( $query, {}, $booksellerid, $authorisedby );
199
200     my $basket = $dbh->{mysql_insertid};
201     $basketname           ||= q{}; # default to empty strings
202     $basketnote           ||= q{};
203     $basketbooksellernote ||= q{};
204     ModBasketHeader( $basket, $basketname, $basketnote, $basketbooksellernote,
205         $basketcontractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items );
206     return $basket;
207 }
208
209 #------------------------------------------------------------#
210
211 =head3 CloseBasket
212
213   &CloseBasket($basketno);
214
215 close a basket (becomes unmodifiable, except for receives)
216
217 =cut
218
219 sub CloseBasket {
220     my ($basketno) = @_;
221     my $dbh        = C4::Context->dbh;
222     $dbh->do('UPDATE aqbasket SET closedate=now() WHERE basketno=?', {}, $basketno );
223
224     $dbh->do(
225 q{UPDATE aqorders SET orderstatus = 'ordered' WHERE basketno = ? AND orderstatus NOT IN ( 'complete', 'cancelled')},
226         {}, $basketno
227     );
228     return;
229 }
230
231 =head3 ReopenBasket
232
233   &ReopenBasket($basketno);
234
235 reopen a basket
236
237 =cut
238
239 sub ReopenBasket {
240     my ($basketno) = @_;
241     my $dbh        = C4::Context->dbh;
242     $dbh->do( q{UPDATE aqbasket SET closedate=NULL WHERE  basketno=?}, {}, $basketno );
243
244     $dbh->do( q{
245         UPDATE aqorders
246         SET orderstatus = 'new'
247         WHERE basketno = ?
248         AND orderstatus NOT IN ( 'complete', 'cancelled' )
249         }, {}, $basketno);
250     return;
251 }
252
253 #------------------------------------------------------------#
254
255 =head3 GetBasketAsCSV
256
257   &GetBasketAsCSV($basketno);
258
259 Export a basket as CSV
260
261 $cgi parameter is needed for column name translation
262
263 =cut
264
265 sub GetBasketAsCSV {
266     my ($basketno, $cgi, $csv_profile_id) = @_;
267     my $basket = GetBasket($basketno);
268     my @orders = GetOrders($basketno);
269     my $contract = GetContract({
270         contractnumber => $basket->{'contractnumber'}
271     });
272
273     my $template = C4::Templates::gettemplate("acqui/csv/basket.tt", "intranet", $cgi);
274     my @rows;
275     if ($csv_profile_id) {
276         my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
277         Koha::Exceptions::ObjectNotFound->throw( 'There is no valid csv profile given') unless $csv_profile;
278
279         my $csv = Text::CSV_XS->new({'quote_char'=>'"','escape_char'=>'"','sep_char'=>$csv_profile->csv_separator,'binary'=>1});
280         my $csv_profile_content = $csv_profile->content;
281         my ( @headers, @fields );
282         while ( $csv_profile_content =~ /
283             ([^=\|]+) # header
284             =?
285             ([^\|]*) # fieldname (table.row or row)
286             \|? /gxms
287         ) {
288             my $header = $1;
289             my $field = ($2 eq '') ? $1 : $2;
290
291             $header =~ s/^\s+|\s+$//g; # Trim whitespaces
292             push @headers, $header;
293
294             $field =~ s/[^\.]*\.{1}//; # Remove the table name if exists.
295             $field =~ s/^\s+|\s+$//g; # Trim whitespaces
296             push @fields, $field;
297         }
298         for my $order (@orders) {
299             my @row;
300             my $biblio = Koha::Biblios->find( $order->{biblionumber} );
301             my $biblioitem = $biblio->biblioitem;
302             $order = { %$order, %{ $biblioitem->unblessed } };
303             if ($contract) {
304                 $order = {%$order, %$contract};
305             }
306             $order = {%$order, %$basket, %{ $biblio->unblessed }};
307             for my $field (@fields) {
308                 push @row, $order->{$field};
309             }
310             push @rows, \@row;
311         }
312         my $content = join( $csv_profile->csv_separator, @headers ) . "\n";
313         for my $row ( @rows ) {
314             $csv->combine(@$row);
315             my $string = $csv->string;
316             $content .= $string . "\n";
317         }
318         return $content;
319     }
320     else {
321         foreach my $order (@orders) {
322             my $biblio = Koha::Biblios->find( $order->{biblionumber} );
323             my $biblioitem = $biblio->biblioitem;
324             my $row = {
325                 contractname => $contract->{'contractname'},
326                 ordernumber => $order->{'ordernumber'},
327                 entrydate => $order->{'entrydate'},
328                 isbn => $order->{'isbn'},
329                 author => $biblio->author,
330                 title => $biblio->title,
331                 publicationyear => $biblioitem->publicationyear,
332                 publishercode => $biblioitem->publishercode,
333                 collectiontitle => $biblioitem->collectiontitle,
334                 notes => $order->{'order_vendornote'},
335                 quantity => $order->{'quantity'},
336                 rrp => $order->{'rrp'},
337             };
338             for my $place ( qw( deliveryplace billingplace ) ) {
339                 if ( my $library = Koha::Libraries->find( $row->{deliveryplace} ) ) {
340                     $row->{$place} = $library->branchname
341                 }
342             }
343             foreach(qw(
344                 contractname author title publishercode collectiontitle notes
345                 deliveryplace billingplace
346             ) ) {
347                 # Double the quotes to not be interpreted as a field end
348                 $row->{$_} =~ s/"/""/g if $row->{$_};
349             }
350             push @rows, $row;
351          }
352
353         @rows = sort {
354             if(defined $a->{publishercode} and defined $b->{publishercode}) {
355                 $a->{publishercode} cmp $b->{publishercode};
356             }
357         } @rows;
358
359         $template->param(rows => \@rows);
360
361         return $template->output;
362     }
363 }
364
365
366 =head3 GetBasketGroupAsCSV
367
368   &GetBasketGroupAsCSV($basketgroupid);
369
370 Export a basket group as CSV
371
372 $cgi parameter is needed for column name translation
373
374 =cut
375
376 sub GetBasketGroupAsCSV {
377     my ($basketgroupid, $cgi) = @_;
378     my $baskets = GetBasketsByBasketgroup($basketgroupid);
379
380     my $template = C4::Templates::gettemplate('acqui/csv/basketgroup.tt', 'intranet', $cgi);
381
382     my @rows;
383     for my $basket (@$baskets) {
384         my @orders     = GetOrders( $basket->{basketno} );
385         my $contract   = GetContract({
386             contractnumber => $basket->{contractnumber}
387         });
388         my $bookseller = Koha::Acquisition::Booksellers->find( $basket->{booksellerid} );
389         my $basketgroup = GetBasketgroup( $$basket{basketgroupid} );
390
391         foreach my $order (@orders) {
392             my $biblio = Koha::Biblios->find( $order->{biblionumber} );
393             my $biblioitem = $biblio->biblioitem;
394             my $row = {
395                 clientnumber => $bookseller->accountnumber,
396                 basketname => $basket->{basketname},
397                 ordernumber => $order->{ordernumber},
398                 author => $biblio->author,
399                 title => $biblio->title,
400                 publishercode => $biblioitem->publishercode,
401                 publicationyear => $biblioitem->publicationyear,
402                 collectiontitle => $biblioitem->collectiontitle,
403                 isbn => $order->{isbn},
404                 quantity => $order->{quantity},
405                 rrp_tax_included => $order->{rrp_tax_included},
406                 rrp_tax_excluded => $order->{rrp_tax_excluded},
407                 discount => $bookseller->discount,
408                 ecost_tax_included => $order->{ecost_tax_included},
409                 ecost_tax_excluded => $order->{ecost_tax_excluded},
410                 notes => $order->{order_vendornote},
411                 entrydate => $order->{entrydate},
412                 booksellername => $bookseller->name,
413                 bookselleraddress => $bookseller->address1,
414                 booksellerpostal => $bookseller->postal,
415                 contractnumber => $contract->{contractnumber},
416                 contractname => $contract->{contractname},
417             };
418             my $temp = {
419                 basketgroupdeliveryplace => $basketgroup->{deliveryplace},
420                 basketgroupbillingplace  => $basketgroup->{billingplace},
421                 basketdeliveryplace      => $basket->{deliveryplace},
422                 basketbillingplace       => $basket->{billingplace},
423             };
424             for my $place (qw( basketgroupdeliveryplace basketgroupbillingplace basketdeliveryplace basketbillingplace )) {
425                 if ( my $library = Koha::Libraries->find( $temp->{$place} ) ) {
426                     $row->{$place} = $library->branchname;
427                 }
428             }
429             foreach(qw(
430                 basketname author title publishercode collectiontitle notes
431                 booksellername bookselleraddress booksellerpostal contractname
432                 basketgroupdeliveryplace basketgroupbillingplace
433                 basketdeliveryplace basketbillingplace
434             ) ) {
435                 # Double the quotes to not be interpreted as a field end
436                 $row->{$_} =~ s/"/""/g if $row->{$_};
437             }
438             push @rows, $row;
439          }
440      }
441     $template->param(rows => \@rows);
442
443     return $template->output;
444
445 }
446
447 =head3 CloseBasketgroup
448
449   &CloseBasketgroup($basketgroupno);
450
451 close a basketgroup
452
453 =cut
454
455 sub CloseBasketgroup {
456     my ($basketgroupno) = @_;
457     my $dbh        = C4::Context->dbh;
458     my $sth = $dbh->prepare("
459         UPDATE aqbasketgroups
460         SET    closed=1
461         WHERE  id=?
462     ");
463     $sth->execute($basketgroupno);
464 }
465
466 #------------------------------------------------------------#
467
468 =head3 ReOpenBaskergroup($basketgroupno)
469
470   &ReOpenBaskergroup($basketgroupno);
471
472 reopen a basketgroup
473
474 =cut
475
476 sub ReOpenBasketgroup {
477     my ($basketgroupno) = @_;
478     my $dbh        = C4::Context->dbh;
479     my $sth = $dbh->prepare("
480         UPDATE aqbasketgroups
481         SET    closed=0
482         WHERE  id=?
483     ");
484     $sth->execute($basketgroupno);
485 }
486
487 #------------------------------------------------------------#
488
489
490 =head3 DelBasket
491
492   &DelBasket($basketno);
493
494 Deletes the basket that has basketno field $basketno in the aqbasket table.
495
496 =over
497
498 =item C<$basketno> is the primary key of the basket in the aqbasket table.
499
500 =back
501
502 =cut
503
504 sub DelBasket {
505     my ( $basketno ) = @_;
506     my $query = "DELETE FROM aqbasket WHERE basketno=?";
507     my $dbh = C4::Context->dbh;
508     my $sth = $dbh->prepare($query);
509     $sth->execute($basketno);
510     return;
511 }
512
513 #------------------------------------------------------------#
514
515 =head3 ModBasket
516
517   &ModBasket($basketinfo);
518
519 Modifies a basket, using a hashref $basketinfo for the relevant information, only $basketinfo->{'basketno'} is required.
520
521 =over
522
523 =item C<$basketno> is the primary key of the basket in the aqbasket table.
524
525 =back
526
527 =cut
528
529 sub ModBasket {
530     my $basketinfo = shift;
531     my $query = "UPDATE aqbasket SET ";
532     my @params;
533     foreach my $key (keys %$basketinfo){
534         if ($key ne 'basketno'){
535             $query .= "$key=?, ";
536             push(@params, $basketinfo->{$key} || undef );
537         }
538     }
539 # get rid of the "," at the end of $query
540     if (substr($query, length($query)-2) eq ', '){
541         chop($query);
542         chop($query);
543         $query .= ' ';
544     }
545     $query .= "WHERE basketno=?";
546     push(@params, $basketinfo->{'basketno'});
547     my $dbh = C4::Context->dbh;
548     my $sth = $dbh->prepare($query);
549     $sth->execute(@params);
550
551     return;
552 }
553
554 #------------------------------------------------------------#
555
556 =head3 ModBasketHeader
557
558   &ModBasketHeader($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid);
559
560 Modifies a basket's header.
561
562 =over
563
564 =item C<$basketno> is the "basketno" field in the "aqbasket" table;
565
566 =item C<$basketname> is the "basketname" field in the "aqbasket" table;
567
568 =item C<$note> is the "note" field in the "aqbasket" table;
569
570 =item C<$booksellernote> is the "booksellernote" field in the "aqbasket" table;
571
572 =item C<$contractnumber> is the "contractnumber" (foreign) key in the "aqbasket" table.
573
574 =item C<$booksellerid> is the id (foreign) key in the "aqbooksellers" table for the vendor.
575
576 =item C<$deliveryplace> is the "deliveryplace" field in the aqbasket table.
577
578 =item C<$billingplace> is the "billingplace" field in the aqbasket table.
579
580 =item C<$is_standing> is the "is_standing" field in the aqbasket table.
581
582 =item C<$create_items> should be set to 'ordering', 'receiving' or 'cataloguing' (or undef, in which
583 case the AcqCreateItem syspref takes precedence).
584
585 =back
586
587 =cut
588
589 sub ModBasketHeader {
590     my ($basketno, $basketname, $note, $booksellernote, $contractnumber, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items) = @_;
591
592     $is_standing ||= 0;
593     my $query = qq{
594         UPDATE aqbasket
595         SET basketname=?, note=?, booksellernote=?, booksellerid=?, deliveryplace=?, billingplace=?, is_standing=?, create_items=?
596         WHERE basketno=?
597     };
598
599     my $dbh = C4::Context->dbh;
600     my $sth = $dbh->prepare($query);
601     $sth->execute($basketname, $note, $booksellernote, $booksellerid, $deliveryplace, $billingplace, $is_standing, $create_items || undef, $basketno);
602
603     if ( $contractnumber ) {
604         my $query2 ="UPDATE aqbasket SET contractnumber=? WHERE basketno=?";
605         my $sth2 = $dbh->prepare($query2);
606         $sth2->execute($contractnumber,$basketno);
607     }
608     return;
609 }
610
611 #------------------------------------------------------------#
612
613 =head3 GetBasketsByBookseller
614
615   @results = &GetBasketsByBookseller($booksellerid, $extra);
616
617 Returns a list of hashes of all the baskets that belong to bookseller 'booksellerid'.
618
619 =over
620
621 =item C<$booksellerid> is the 'id' field of the bookseller in the aqbooksellers table
622
623 =item C<$extra> is the extra sql parameters, can be
624
625  $extra->{groupby}: group baskets by column
626     ex. $extra->{groupby} = aqbasket.basketgroupid
627  $extra->{orderby}: order baskets by column
628  $extra->{limit}: limit number of results (can be helpful for pagination)
629
630 =back
631
632 =cut
633
634 sub GetBasketsByBookseller {
635     my ($booksellerid, $extra) = @_;
636     my $query = "SELECT * FROM aqbasket WHERE booksellerid=?";
637     if ($extra){
638         if ($extra->{groupby}) {
639             $query .= " GROUP by $extra->{groupby}";
640         }
641         if ($extra->{orderby}){
642             $query .= " ORDER by $extra->{orderby}";
643         }
644         if ($extra->{limit}){
645             $query .= " LIMIT $extra->{limit}";
646         }
647     }
648     my $dbh = C4::Context->dbh;
649     my $sth = $dbh->prepare($query);
650     $sth->execute($booksellerid);
651     return $sth->fetchall_arrayref({});
652 }
653
654 =head3 GetBasketsInfosByBookseller
655
656     my $baskets = GetBasketsInfosByBookseller($supplierid, $allbaskets);
657
658 The optional second parameter allbaskets is a boolean allowing you to
659 select all baskets from the supplier; by default only active baskets (open or 
660 closed but still something to receive) are returned.
661
662 Returns in a arrayref of hashref all about booksellers baskets, plus:
663     total_biblios: Number of distinct biblios in basket
664     total_items: Number of items in basket
665     expected_items: Number of non-received items in basket
666
667 =cut
668
669 sub GetBasketsInfosByBookseller {
670     my ($supplierid, $allbaskets) = @_;
671
672     return unless $supplierid;
673
674     my $dbh = C4::Context->dbh;
675     my $query = q{
676         SELECT aqbasket.basketno, aqbasket.basketname, aqbasket.note, aqbasket.booksellernote, aqbasket.contractnumber, aqbasket.creationdate, aqbasket.closedate, aqbasket.booksellerid, aqbasket.authorisedby, aqbasket.booksellerinvoicenumber, aqbasket.basketgroupid, aqbasket.deliveryplace, aqbasket.billingplace, aqbasket.branch, aqbasket.is_standing, aqbasket.create_items,
677           SUM(aqorders.quantity) AS total_items,
678           SUM(
679             IF ( aqorders.orderstatus = 'cancelled', aqorders.quantity, 0 )
680           ) AS total_items_cancelled,
681           COUNT(DISTINCT aqorders.biblionumber) AS total_biblios,
682           SUM(
683             IF(aqorders.datereceived IS NULL
684               AND aqorders.datecancellationprinted IS NULL
685             , aqorders.quantity
686             , 0)
687           ) AS expected_items,
688         SUM( aqorders.uncertainprice ) AS uncertainprices
689         FROM aqbasket
690           LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
691         WHERE booksellerid = ?};
692
693     $query.=" GROUP BY aqbasket.basketno, aqbasket.basketname, aqbasket.note, aqbasket.booksellernote, aqbasket.contractnumber, aqbasket.creationdate, aqbasket.closedate, aqbasket.booksellerid, aqbasket.authorisedby, aqbasket.booksellerinvoicenumber, aqbasket.basketgroupid, aqbasket.deliveryplace, aqbasket.billingplace, aqbasket.branch, aqbasket.is_standing, aqbasket.create_items";
694
695     unless ( $allbaskets ) {
696         # Don't show the basket if it's NOT CLOSED or is FULLY RECEIVED
697         $query.=" HAVING (closedate IS NULL OR (
698           SUM(
699             IF(aqorders.datereceived IS NULL
700               AND aqorders.datecancellationprinted IS NULL
701             , aqorders.quantity
702             , 0)
703             ) > 0))"
704     }
705
706     my $sth = $dbh->prepare($query);
707     $sth->execute($supplierid);
708     my $baskets = $sth->fetchall_arrayref({});
709
710     # Retrieve the number of biblios cancelled
711     my $cancelled_biblios = $dbh->selectall_hashref( q|
712         SELECT COUNT(DISTINCT(biblionumber)) AS total_biblios_cancelled, aqbasket.basketno
713         FROM aqbasket
714         LEFT JOIN aqorders ON aqorders.basketno = aqbasket.basketno
715         WHERE booksellerid = ?
716         AND aqorders.orderstatus = 'cancelled'
717         GROUP BY aqbasket.basketno
718     |, 'basketno', {}, $supplierid );
719     map {
720         $_->{total_biblios_cancelled} = $cancelled_biblios->{$_->{basketno}}{total_biblios_cancelled} || 0
721     } @$baskets;
722
723     return $baskets;
724 }
725
726 =head3 GetBasketUsers
727
728     $basketusers_ids = &GetBasketUsers($basketno);
729
730 Returns a list of all borrowernumbers that are in basket users list
731
732 =cut
733
734 sub GetBasketUsers {
735     my $basketno = shift;
736
737     return unless $basketno;
738
739     my $query = qq{
740         SELECT borrowernumber
741         FROM aqbasketusers
742         WHERE basketno = ?
743     };
744     my $dbh = C4::Context->dbh;
745     my $sth = $dbh->prepare($query);
746     $sth->execute($basketno);
747     my $results = $sth->fetchall_arrayref( {} );
748
749     my @borrowernumbers;
750     foreach (@$results) {
751         push @borrowernumbers, $_->{'borrowernumber'};
752     }
753
754     return @borrowernumbers;
755 }
756
757 =head3 ModBasketUsers
758
759     my @basketusers_ids = (1, 2, 3);
760     &ModBasketUsers($basketno, @basketusers_ids);
761
762 Delete all users from basket users list, and add users in C<@basketusers_ids>
763 to this users list.
764
765 =cut
766
767 sub ModBasketUsers {
768     my ($basketno, @basketusers_ids) = @_;
769
770     return unless $basketno;
771
772     my $dbh = C4::Context->dbh;
773     my $query = qq{
774         DELETE FROM aqbasketusers
775         WHERE basketno = ?
776     };
777     my $sth = $dbh->prepare($query);
778     $sth->execute($basketno);
779
780     $query = qq{
781         INSERT INTO aqbasketusers (basketno, borrowernumber)
782         VALUES (?, ?)
783     };
784     $sth = $dbh->prepare($query);
785     foreach my $basketuser_id (@basketusers_ids) {
786         $sth->execute($basketno, $basketuser_id);
787     }
788     return;
789 }
790
791 =head3 CanUserManageBasket
792
793     my $bool = CanUserManageBasket($borrower, $basket[, $userflags]);
794     my $bool = CanUserManageBasket($borrowernumber, $basketno[, $userflags]);
795
796 Check if a borrower can manage a basket, according to system preference
797 AcqViewBaskets, user permissions and basket properties (creator, users list,
798 branch).
799
800 First parameter can be either a borrowernumber or a hashref as returned by
801 Koha::Patron->unblessed
802
803 Second parameter can be either a basketno or a hashref as returned by
804 C4::Acquisition::GetBasket.
805
806 The third parameter is optional. If given, it should be a hashref as returned
807 by C4::Auth::getuserflags. If not, getuserflags is called.
808
809 If user is authorised to manage basket, returns 1.
810 Otherwise returns 0.
811
812 =cut
813
814 sub CanUserManageBasket {
815     my ($borrower, $basket, $userflags) = @_;
816
817     if (!ref $borrower) {
818         # FIXME This needs to be replaced
819         # We should not accept both scalar and array
820         # Tests need to be updated
821         $borrower = Koha::Patrons->find( $borrower )->unblessed;
822     }
823     if (!ref $basket) {
824         $basket = GetBasket($basket);
825     }
826
827     return 0 unless ($basket and $borrower);
828
829     my $borrowernumber = $borrower->{borrowernumber};
830     my $basketno = $basket->{basketno};
831
832     my $AcqViewBaskets = C4::Context->preference('AcqViewBaskets');
833
834     if (!defined $userflags) {
835         my $dbh = C4::Context->dbh;
836         my $sth = $dbh->prepare("SELECT flags FROM borrowers WHERE borrowernumber = ?");
837         $sth->execute($borrowernumber);
838         my ($flags) = $sth->fetchrow_array;
839         $sth->finish;
840
841         $userflags = C4::Auth::getuserflags($flags, $borrower->{userid}, $dbh);
842     }
843
844     unless ($userflags->{superlibrarian}
845     || (ref $userflags->{acquisition} && $userflags->{acquisition}->{order_manage_all})
846     || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
847     {
848         if (not exists $userflags->{acquisition}) {
849             return 0;
850         }
851
852         if ( (ref $userflags->{acquisition} && !$userflags->{acquisition}->{order_manage})
853         || (!ref $userflags->{acquisition} && !$userflags->{acquisition}) ) {
854             return 0;
855         }
856
857         if ($AcqViewBaskets eq 'user'
858         && $basket->{authorisedby} != $borrowernumber
859         && ! grep { $borrowernumber eq $_ } GetBasketUsers($basketno)) {
860              return 0;
861         }
862
863         if ($AcqViewBaskets eq 'branch' && defined $basket->{branch}
864         && $basket->{branch} ne $borrower->{branchcode}) {
865             return 0;
866         }
867     }
868
869     return 1;
870 }
871
872 #------------------------------------------------------------#
873
874 =head3 GetBasketsByBasketgroup
875
876   $baskets = &GetBasketsByBasketgroup($basketgroupid);
877
878 Returns a reference to all baskets that belong to basketgroup $basketgroupid.
879
880 =cut
881
882 sub GetBasketsByBasketgroup {
883     my $basketgroupid = shift;
884     my $query = qq{
885         SELECT *, aqbasket.booksellerid as booksellerid
886         FROM aqbasket
887         LEFT JOIN aqcontract USING(contractnumber) WHERE basketgroupid=?
888     };
889     my $dbh = C4::Context->dbh;
890     my $sth = $dbh->prepare($query);
891     $sth->execute($basketgroupid);
892     return $sth->fetchall_arrayref({});
893 }
894
895 #------------------------------------------------------------#
896
897 =head3 NewBasketgroup
898
899   $basketgroupid = NewBasketgroup(\%hashref);
900
901 Adds a basketgroup to the aqbasketgroups table, and add the initial baskets to it.
902
903 $hashref->{'booksellerid'} is the 'id' field of the bookseller in the aqbooksellers table,
904
905 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
906
907 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
908
909 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
910
911 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
912
913 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
914
915 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
916
917 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
918
919 =cut
920
921 sub NewBasketgroup {
922     my $basketgroupinfo = shift;
923     die "booksellerid is required to create a basketgroup" unless $basketgroupinfo->{'booksellerid'};
924     my $query = "INSERT INTO aqbasketgroups (";
925     my @params;
926     foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
927         if ( defined $basketgroupinfo->{$field} ) {
928             $query .= "$field, ";
929             push(@params, $basketgroupinfo->{$field});
930         }
931     }
932     $query .= "booksellerid) VALUES (";
933     foreach (@params) {
934         $query .= "?, ";
935     }
936     $query .= "?)";
937     push(@params, $basketgroupinfo->{'booksellerid'});
938     my $dbh = C4::Context->dbh;
939     my $sth = $dbh->prepare($query);
940     $sth->execute(@params);
941     my $basketgroupid = $dbh->{'mysql_insertid'};
942     if( $basketgroupinfo->{'basketlist'} ) {
943         foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
944             my $query2 = "UPDATE aqbasket SET basketgroupid=? WHERE basketno=?";
945             my $sth2 = $dbh->prepare($query2);
946             $sth2->execute($basketgroupid, $basketno);
947         }
948     }
949     return $basketgroupid;
950 }
951
952 #------------------------------------------------------------#
953
954 =head3 ModBasketgroup
955
956   ModBasketgroup(\%hashref);
957
958 Modifies a basketgroup in the aqbasketgroups table, and add the baskets to it.
959
960 $hashref->{'id'} is the 'id' field of the basketgroup in the aqbasketgroup table, this parameter is mandatory,
961
962 $hashref->{'name'} is the 'name' field of the basketgroup in the aqbasketgroups table,
963
964 $hashref->{'basketlist'} is a list reference of the 'id's of the baskets that belong to this group,
965
966 $hashref->{'billingplace'} is the 'billingplace' field of the basketgroup in the aqbasketgroups table,
967
968 $hashref->{'deliveryplace'} is the 'deliveryplace' field of the basketgroup in the aqbasketgroups table,
969
970 $hashref->{'freedeliveryplace'} is the 'freedeliveryplace' field of the basketgroup in the aqbasketgroups table,
971
972 $hashref->{'deliverycomment'} is the 'deliverycomment' field of the basketgroup in the aqbasketgroups table,
973
974 $hashref->{'closed'} is the 'closed' field of the aqbasketgroups table, it is false if 0, true otherwise.
975
976 =cut
977
978 sub ModBasketgroup {
979     my $basketgroupinfo = shift;
980     die "basketgroup id is required to edit a basketgroup" unless $basketgroupinfo->{'id'};
981     my $dbh = C4::Context->dbh;
982     my $query = "UPDATE aqbasketgroups SET ";
983     my @params;
984     foreach my $field (qw(name billingplace deliveryplace freedeliveryplace deliverycomment closed)) {
985         if ( defined $basketgroupinfo->{$field} ) {
986             $query .= "$field=?, ";
987             push(@params, $basketgroupinfo->{$field});
988         }
989     }
990     chop($query);
991     chop($query);
992     $query .= " WHERE id=?";
993     push(@params, $basketgroupinfo->{'id'});
994     my $sth = $dbh->prepare($query);
995     $sth->execute(@params);
996
997     $sth = $dbh->prepare('UPDATE aqbasket SET basketgroupid = NULL WHERE basketgroupid = ?');
998     $sth->execute($basketgroupinfo->{'id'});
999
1000     if($basketgroupinfo->{'basketlist'} && @{$basketgroupinfo->{'basketlist'}}){
1001         $sth = $dbh->prepare("UPDATE aqbasket SET basketgroupid=? WHERE basketno=?");
1002         foreach my $basketno (@{$basketgroupinfo->{'basketlist'}}) {
1003             $sth->execute($basketgroupinfo->{'id'}, $basketno);
1004         }
1005     }
1006     return;
1007 }
1008
1009 #------------------------------------------------------------#
1010
1011 =head3 DelBasketgroup
1012
1013   DelBasketgroup($basketgroupid);
1014
1015 Deletes a basketgroup in the aqbasketgroups table, and removes the reference to it from the baskets,
1016
1017 =over
1018
1019 =item C<$basketgroupid> is the 'id' field of the basket in the aqbasketgroup table
1020
1021 =back
1022
1023 =cut
1024
1025 sub DelBasketgroup {
1026     my $basketgroupid = shift;
1027     die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
1028     my $query = "DELETE FROM aqbasketgroups WHERE id=?";
1029     my $dbh = C4::Context->dbh;
1030     my $sth = $dbh->prepare($query);
1031     $sth->execute($basketgroupid);
1032     return;
1033 }
1034
1035 #------------------------------------------------------------#
1036
1037
1038 =head2 FUNCTIONS ABOUT ORDERS
1039
1040 =head3 GetBasketgroup
1041
1042   $basketgroup = &GetBasketgroup($basketgroupid);
1043
1044 Returns a reference to the hash containing all information about the basketgroup.
1045
1046 =cut
1047
1048 sub GetBasketgroup {
1049     my $basketgroupid = shift;
1050     die "basketgroup id is required to edit a basketgroup" unless $basketgroupid;
1051     my $dbh = C4::Context->dbh;
1052     my $result_set = $dbh->selectall_arrayref(
1053         'SELECT * FROM aqbasketgroups WHERE id=?',
1054         { Slice => {} },
1055         $basketgroupid
1056     );
1057     return $result_set->[0];    # id is unique
1058 }
1059
1060 #------------------------------------------------------------#
1061
1062 =head3 GetBasketgroups
1063
1064   $basketgroups = &GetBasketgroups($booksellerid);
1065
1066 Returns a reference to the array of all the basketgroups of bookseller $booksellerid.
1067
1068 =cut
1069
1070 sub GetBasketgroups {
1071     my $booksellerid = shift;
1072     die 'bookseller id is required to edit a basketgroup' unless $booksellerid;
1073     my $query = 'SELECT * FROM aqbasketgroups WHERE booksellerid=? ORDER BY id DESC';
1074     my $dbh = C4::Context->dbh;
1075     my $sth = $dbh->prepare($query);
1076     $sth->execute($booksellerid);
1077     return $sth->fetchall_arrayref({});
1078 }
1079
1080 #------------------------------------------------------------#
1081
1082 =head2 FUNCTIONS ABOUT ORDERS
1083
1084 =head3 GetOrders
1085
1086   @orders = &GetOrders( $basketno, { orderby => 'biblio.title', cancelled => 0|1 } );
1087
1088 Looks up the pending (non-cancelled) orders with the given basket
1089 number.
1090
1091 If cancelled is set, only cancelled orders will be returned.
1092
1093 =cut
1094
1095 sub GetOrders {
1096     my ( $basketno, $params ) = @_;
1097
1098     return () unless $basketno;
1099
1100     my $orderby = $params->{orderby};
1101     my $cancelled = $params->{cancelled} || 0;
1102
1103     my $dbh   = C4::Context->dbh;
1104     my $query = q|
1105         SELECT biblio.*,biblioitems.*,
1106                 aqorders.*,
1107                 aqbudgets.*,
1108         |;
1109     $query .= $cancelled
1110       ? q|
1111                 aqorders_transfers.ordernumber_to AS transferred_to,
1112                 aqorders_transfers.timestamp AS transferred_to_timestamp
1113     |
1114       : q|
1115                 aqorders_transfers.ordernumber_from AS transferred_from,
1116                 aqorders_transfers.timestamp AS transferred_from_timestamp
1117     |;
1118     $query .= q|
1119         FROM    aqorders
1120             LEFT JOIN aqbudgets        ON aqbudgets.budget_id = aqorders.budget_id
1121             LEFT JOIN biblio           ON biblio.biblionumber = aqorders.biblionumber
1122             LEFT JOIN biblioitems      ON biblioitems.biblionumber =biblio.biblionumber
1123     |;
1124     $query .= $cancelled
1125       ? q|
1126             LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_from = aqorders.ordernumber
1127     |
1128       : q|
1129             LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_to = aqorders.ordernumber
1130
1131     |;
1132     $query .= q|
1133         WHERE   basketno=?
1134     |;
1135
1136     if ($cancelled) {
1137         $orderby ||= q|biblioitems.publishercode, biblio.title|;
1138         $query .= q|
1139             AND datecancellationprinted IS NOT NULL
1140         |;
1141     }
1142     else {
1143         $orderby ||=
1144           q|aqorders.datecancellationprinted desc, aqorders.timestamp desc|;
1145         $query .= q|
1146             AND datecancellationprinted IS NULL
1147         |;
1148     }
1149
1150     $query .= " ORDER BY $orderby";
1151     my $orders =
1152       $dbh->selectall_arrayref( $query, { Slice => {} }, $basketno );
1153     return @{$orders};
1154
1155 }
1156
1157 #------------------------------------------------------------#
1158
1159 =head3 GetOrdersByBiblionumber
1160
1161   @orders = &GetOrdersByBiblionumber($biblionumber);
1162
1163 Looks up the orders with linked to a specific $biblionumber, including
1164 cancelled orders and received orders.
1165
1166 return :
1167 C<@orders> is an array of references-to-hash, whose keys are the
1168 fields from the aqorders, biblio, and biblioitems tables in the Koha database.
1169
1170 =cut
1171
1172 sub GetOrdersByBiblionumber {
1173     my $biblionumber = shift;
1174     return unless $biblionumber;
1175     my $dbh   = C4::Context->dbh;
1176     my $query  ="
1177         SELECT biblio.*,biblioitems.*,
1178                 aqorders.*,
1179                 aqbudgets.*
1180         FROM    aqorders
1181             LEFT JOIN aqbudgets        ON aqbudgets.budget_id = aqorders.budget_id
1182             LEFT JOIN biblio           ON biblio.biblionumber = aqorders.biblionumber
1183             LEFT JOIN biblioitems      ON biblioitems.biblionumber =biblio.biblionumber
1184         WHERE   aqorders.biblionumber=?
1185     ";
1186     my $result_set =
1187       $dbh->selectall_arrayref( $query, { Slice => {} }, $biblionumber );
1188     return @{$result_set};
1189
1190 }
1191
1192 #------------------------------------------------------------#
1193
1194 =head3 GetOrder
1195
1196   $order = &GetOrder($ordernumber);
1197
1198 Looks up an order by order number.
1199
1200 Returns a reference-to-hash describing the order. The keys of
1201 C<$order> are fields from the biblio, biblioitems, aqorders tables of the Koha database.
1202
1203 =cut
1204
1205 sub GetOrder {
1206     my ($ordernumber) = @_;
1207     return unless $ordernumber;
1208
1209     my $dbh      = C4::Context->dbh;
1210     my $query = qq{SELECT
1211                 aqorders.*,
1212                 biblio.title,
1213                 biblio.author,
1214                 aqbasket.basketname,
1215                 borrowers.branchcode,
1216                 biblioitems.publicationyear,
1217                 biblio.copyrightdate,
1218                 biblioitems.editionstatement,
1219                 biblioitems.isbn,
1220                 biblioitems.ean,
1221                 biblio.seriestitle,
1222                 biblioitems.publishercode,
1223                 aqorders.rrp              AS unitpricesupplier,
1224                 aqorders.ecost            AS unitpricelib,
1225                 aqbudgets.budget_name     AS budget,
1226                 aqbooksellers.name        AS supplier,
1227                 aqbooksellers.id          AS supplierid,
1228                 biblioitems.publishercode AS publisher,
1229                 ADDDATE(aqbasket.closedate, INTERVAL aqbooksellers.deliverytime DAY) AS estimateddeliverydate,
1230                 DATE(aqbasket.closedate)  AS orderdate,
1231                 aqorders.quantity - COALESCE(aqorders.quantityreceived,0)                 AS quantity_to_receive,
1232                 (aqorders.quantity - COALESCE(aqorders.quantityreceived,0)) * aqorders.rrp AS subtotal,
1233                 DATEDIFF(CURDATE( ),closedate) AS latesince
1234                 FROM aqorders LEFT JOIN biblio ON biblio.biblionumber = aqorders.biblionumber
1235                 LEFT JOIN biblioitems ON biblioitems.biblionumber = biblio.biblionumber
1236                 LEFT JOIN aqbudgets ON aqorders.budget_id = aqbudgets.budget_id,
1237                 aqbasket LEFT JOIN borrowers  ON aqbasket.authorisedby = borrowers.borrowernumber
1238                 LEFT JOIN aqbooksellers       ON aqbasket.booksellerid = aqbooksellers.id
1239                 WHERE aqorders.basketno = aqbasket.basketno
1240                     AND ordernumber=?};
1241     my $result_set =
1242       $dbh->selectall_arrayref( $query, { Slice => {} }, $ordernumber );
1243
1244     # result_set assumed to contain 1 match
1245     return $result_set->[0];
1246 }
1247
1248 =head3 ModOrder
1249
1250   &ModOrder(\%hashref);
1251
1252 Modifies an existing order. Updates the order with order number
1253 $hashref->{'ordernumber'} and biblionumber $hashref->{'biblionumber'}. All 
1254 other keys of the hash update the fields with the same name in the aqorders 
1255 table of the Koha database.
1256
1257 =cut
1258
1259 sub ModOrder {
1260     my $orderinfo = shift;
1261
1262     die "Ordernumber is required" if $orderinfo->{'ordernumber'} eq '';
1263
1264     my $dbh = C4::Context->dbh;
1265     my @params;
1266
1267     # update uncertainprice to an integer, just in case (under FF, checked boxes have the value "ON" by default)
1268     $orderinfo->{uncertainprice}=1 if $orderinfo->{uncertainprice};
1269
1270 #    delete($orderinfo->{'branchcode'});
1271     # the hash contains a lot of entries not in aqorders, so get the columns ...
1272     my $sth = $dbh->prepare("SELECT * FROM aqorders LIMIT 1;");
1273     $sth->execute;
1274     my $colnames = $sth->{NAME};
1275         #FIXME Be careful. If aqorders would have columns with diacritics,
1276         #you should need to decode what you get back from NAME.
1277         #See report 10110 and guided_reports.pl
1278     my $query = "UPDATE aqorders SET ";
1279
1280     foreach my $orderinfokey (grep(!/ordernumber/, keys %$orderinfo)){
1281         # ... and skip hash entries that are not in the aqorders table
1282         # FIXME : probably not the best way to do it (would be better to have a correct hash)
1283         next unless grep { $_ eq $orderinfokey } @$colnames;
1284             $query .= "$orderinfokey=?, ";
1285             push(@params, $orderinfo->{$orderinfokey});
1286     }
1287
1288     $query .= "timestamp=NOW()  WHERE  ordernumber=?";
1289     push(@params, $orderinfo->{'ordernumber'} );
1290     $sth = $dbh->prepare($query);
1291     $sth->execute(@params);
1292     return;
1293 }
1294
1295 #------------------------------------------------------------#
1296
1297 =head3 ModItemOrder
1298
1299     ModItemOrder($itemnumber, $ordernumber);
1300
1301 Modifies the ordernumber of an item in aqorders_items.
1302
1303 =cut
1304
1305 sub ModItemOrder {
1306     my ($itemnumber, $ordernumber) = @_;
1307
1308     return unless ($itemnumber and $ordernumber);
1309
1310     my $dbh = C4::Context->dbh;
1311     my $query = qq{
1312         UPDATE aqorders_items
1313         SET ordernumber = ?
1314         WHERE itemnumber = ?
1315     };
1316     my $sth = $dbh->prepare($query);
1317     return $sth->execute($ordernumber, $itemnumber);
1318 }
1319
1320 #------------------------------------------------------------#
1321
1322 =head3 ModReceiveOrder
1323
1324     my ( $date_received, $new_ordernumber ) = ModReceiveOrder(
1325         {
1326             biblionumber         => $biblionumber,
1327             order                => $order,
1328             quantityreceived     => $quantityreceived,
1329             user                 => $user,
1330             invoice              => $invoice,
1331             budget_id            => $budget_id,
1332             datereceived         => $datereceived,
1333             received_itemnumbers => \@received_itemnumbers,
1334         }
1335     );
1336
1337 Updates an order, to reflect the fact that it was received, at least
1338 in part.
1339
1340 If a partial order is received, splits the order into two.
1341
1342 Updates the order with biblionumber C<$biblionumber> and ordernumber
1343 C<$order->{ordernumber}>.
1344
1345 =cut
1346
1347
1348 sub ModReceiveOrder {
1349     my ($params)       = @_;
1350     my $biblionumber   = $params->{biblionumber};
1351     my $order          = { %{ $params->{order} } }; # Copy the order, we don't want to modify it
1352     my $invoice        = $params->{invoice};
1353     my $quantrec       = $params->{quantityreceived};
1354     my $user           = $params->{user};
1355     my $budget_id      = $params->{budget_id};
1356     my $datereceived   = $params->{datereceived};
1357     my $received_items = $params->{received_items};
1358
1359     my $dbh = C4::Context->dbh;
1360     $datereceived = output_pref(
1361         {
1362             dt => ( $datereceived ? dt_from_string( $datereceived ) : dt_from_string ),
1363             dateformat => 'iso',
1364             dateonly => 1,
1365         }
1366     );
1367
1368     my $suggestionid = GetSuggestionFromBiblionumber( $biblionumber );
1369     if ($suggestionid) {
1370         ModSuggestion( {suggestionid=>$suggestionid,
1371                         STATUS=>'AVAILABLE',
1372                         biblionumber=> $biblionumber}
1373                         );
1374     }
1375
1376     my $result_set = $dbh->selectrow_arrayref(
1377             q{SELECT aqbasket.is_standing
1378             FROM aqbasket
1379             WHERE basketno=?},{ Slice => {} }, $order->{basketno});
1380     my $is_standing = $result_set->[0];  # we assume we have a unique basket
1381
1382     my $new_ordernumber = $order->{ordernumber};
1383     if ( $is_standing || $order->{quantity} > $quantrec ) {
1384         # Split order line in two parts: the first is the original order line
1385         # without received items (the quantity is decreased),
1386         # the second part is a new order line with quantity=quantityrec
1387         # (entirely received)
1388         my $query = q|
1389             UPDATE aqorders
1390             SET quantity = ?,
1391                 orderstatus = 'partial'|;
1392         $query .= q| WHERE ordernumber = ?|;
1393         my $sth = $dbh->prepare($query);
1394
1395         $sth->execute(
1396             ( $is_standing ? 1 : ($order->{quantity} - $quantrec) ),
1397             $order->{ordernumber}
1398         );
1399
1400         if ( not $order->{subscriptionid} && defined $order->{order_internalnote} ) {
1401             $dbh->do(
1402                 q|UPDATE aqorders
1403                 SET order_internalnote = ?
1404                 WHERE ordernumber = ?|, {},
1405                 $order->{order_internalnote}, $order->{ordernumber}
1406             );
1407         }
1408
1409         # Recalculate tax_value
1410         $dbh->do(q|
1411             UPDATE aqorders
1412             SET
1413                 tax_value_on_ordering = quantity * | . get_rounding_sql(q|ecost_tax_excluded|) . q| * tax_rate_on_ordering,
1414                 tax_value_on_receiving = quantity * | . get_rounding_sql(q|unitprice_tax_excluded|) . q| * tax_rate_on_receiving
1415             WHERE ordernumber = ?
1416         |, undef, $order->{ordernumber});
1417
1418         delete $order->{ordernumber};
1419         $order->{budget_id} = ( $budget_id || $order->{budget_id} );
1420         $order->{quantity} = $quantrec;
1421         $order->{quantityreceived} = $quantrec;
1422         $order->{ecost_tax_excluded} //= 0;
1423         $order->{tax_rate_on_ordering} //= 0;
1424         $order->{unitprice_tax_excluded} //= 0;
1425         $order->{tax_rate_on_receiving} //= 0;
1426         $order->{tax_value_on_ordering} = $order->{quantity} * get_rounded_price($order->{ecost_tax_excluded}) * $order->{tax_rate_on_ordering};
1427         $order->{tax_value_on_receiving} = $order->{quantity} * get_rounded_price($order->{unitprice_tax_excluded}) * $order->{tax_rate_on_receiving};
1428         $order->{datereceived} = $datereceived;
1429         $order->{invoiceid} = $invoice->{invoiceid};
1430         $order->{orderstatus} = 'complete';
1431         $new_ordernumber = Koha::Acquisition::Order->new($order)->store->ordernumber; # TODO What if the store fails?
1432
1433         if ($received_items) {
1434             foreach my $itemnumber (@$received_items) {
1435                 ModItemOrder($itemnumber, $new_ordernumber);
1436             }
1437         }
1438     } else {
1439         my $query = q|
1440             UPDATE aqorders
1441             SET quantityreceived = ?,
1442                 datereceived = ?,
1443                 invoiceid = ?,
1444                 budget_id = ?,
1445                 orderstatus = 'complete'
1446         |;
1447
1448         $query .= q|
1449             , replacementprice = ?
1450         | if defined $order->{replacementprice};
1451
1452         $query .= q|
1453             , unitprice = ?, unitprice_tax_included = ?, unitprice_tax_excluded = ?
1454         | if defined $order->{unitprice};
1455
1456         $query .= q|
1457             ,tax_value_on_receiving = ?
1458         | if defined $order->{tax_value_on_receiving};
1459
1460         $query .= q|
1461             ,tax_rate_on_receiving = ?
1462         | if defined $order->{tax_rate_on_receiving};
1463
1464         $query .= q|
1465             , order_internalnote = ?
1466         | if defined $order->{order_internalnote};
1467
1468         $query .= q| where biblionumber=? and ordernumber=?|;
1469
1470         my $sth = $dbh->prepare( $query );
1471         my @params = ( $quantrec, $datereceived, $invoice->{invoiceid}, ( $budget_id ? $budget_id : $order->{budget_id} ) );
1472
1473         if ( defined $order->{replacementprice} ) {
1474             push @params, $order->{replacementprice};
1475         }
1476
1477         if ( defined $order->{unitprice} ) {
1478             push @params, $order->{unitprice}, $order->{unitprice_tax_included}, $order->{unitprice_tax_excluded};
1479         }
1480
1481         if ( defined $order->{tax_value_on_receiving} ) {
1482             push @params, $order->{tax_value_on_receiving};
1483         }
1484
1485         if ( defined $order->{tax_rate_on_receiving} ) {
1486             push @params, $order->{tax_rate_on_receiving};
1487         }
1488
1489         if ( defined $order->{order_internalnote} ) {
1490             push @params, $order->{order_internalnote};
1491         }
1492
1493         push @params, ( $biblionumber, $order->{ordernumber} );
1494
1495         $sth->execute( @params );
1496
1497         # All items have been received, sent a notification to users
1498         NotifyOrderUsers( $order->{ordernumber} );
1499
1500     }
1501     return ($datereceived, $new_ordernumber);
1502 }
1503
1504 =head3 CancelReceipt
1505
1506     my $parent_ordernumber = CancelReceipt($ordernumber);
1507
1508     Cancel an order line receipt and update the parent order line, as if no
1509     receipt was made.
1510     If items are created at receipt (AcqCreateItem = receiving) then delete
1511     these items.
1512
1513 =cut
1514
1515 sub CancelReceipt {
1516     my $ordernumber = shift;
1517
1518     return unless $ordernumber;
1519
1520     my $dbh = C4::Context->dbh;
1521     my $query = qq{
1522         SELECT datereceived, parent_ordernumber, quantity
1523         FROM aqorders
1524         WHERE ordernumber = ?
1525     };
1526     my $sth = $dbh->prepare($query);
1527     $sth->execute($ordernumber);
1528     my $order = $sth->fetchrow_hashref;
1529     unless($order) {
1530         warn "CancelReceipt: order $ordernumber does not exist";
1531         return;
1532     }
1533     unless($order->{'datereceived'}) {
1534         warn "CancelReceipt: order $ordernumber is not received";
1535         return;
1536     }
1537
1538     my $parent_ordernumber = $order->{'parent_ordernumber'};
1539
1540     my $order_obj = Koha::Acquisition::Orders->find( $ordernumber ); # FIXME rewrite all this subroutine using this object
1541     my @itemnumbers = $order_obj->items->get_column('itemnumber');
1542
1543     if($parent_ordernumber == $ordernumber || not $parent_ordernumber) {
1544         # The order line has no parent, just mark it as not received
1545         $query = qq{
1546             UPDATE aqorders
1547             SET quantityreceived = ?,
1548                 datereceived = ?,
1549                 invoiceid = ?,
1550                 orderstatus = 'ordered'
1551             WHERE ordernumber = ?
1552         };
1553         $sth = $dbh->prepare($query);
1554         $sth->execute(0, undef, undef, $ordernumber);
1555         _cancel_items_receipt( $order_obj );
1556     } else {
1557         # The order line has a parent, increase parent quantity and delete
1558         # the order line.
1559         unless ( $order_obj->basket->is_standing ) {
1560             $query = qq{
1561                 SELECT quantity, datereceived
1562                 FROM aqorders
1563                 WHERE ordernumber = ?
1564             };
1565             $sth = $dbh->prepare($query);
1566             $sth->execute($parent_ordernumber);
1567             my $parent_order = $sth->fetchrow_hashref;
1568             unless($parent_order) {
1569                 warn "Parent order $parent_ordernumber does not exist.";
1570                 return;
1571             }
1572             if($parent_order->{'datereceived'}) {
1573                 warn "CancelReceipt: parent order is received.".
1574                     " Can't cancel receipt.";
1575                 return;
1576             }
1577             $query = qq{
1578                 UPDATE aqorders
1579                 SET quantity = ?,
1580                     orderstatus = 'ordered'
1581                 WHERE ordernumber = ?
1582             };
1583             $sth = $dbh->prepare($query);
1584             my $rv = $sth->execute(
1585                 $order->{'quantity'} + $parent_order->{'quantity'},
1586                 $parent_ordernumber
1587             );
1588             unless($rv) {
1589                 warn "Cannot update parent order line, so do not cancel".
1590                     " receipt";
1591                 return;
1592             }
1593
1594             # Recalculate tax_value
1595             $dbh->do(q|
1596                 UPDATE aqorders
1597                 SET
1598                     tax_value_on_ordering = quantity * | . get_rounding_sql(q|ecost_tax_excluded|) . q| * tax_rate_on_ordering,
1599                     tax_value_on_receiving = quantity * | . get_rounding_sql(q|unitprice_tax_excluded|) . q| * tax_rate_on_receiving
1600                 WHERE ordernumber = ?
1601             |, undef, $parent_ordernumber);
1602         }
1603
1604         _cancel_items_receipt( $order_obj, $parent_ordernumber );
1605         # Delete order line
1606         $query = qq{
1607             DELETE FROM aqorders
1608             WHERE ordernumber = ?
1609         };
1610         $sth = $dbh->prepare($query);
1611         $sth->execute($ordernumber);
1612
1613     }
1614
1615     if( $order_obj->basket->effective_create_items eq 'ordering' ) {
1616         my @affects = split q{\|}, C4::Context->preference("AcqItemSetSubfieldsWhenReceiptIsCancelled");
1617         if ( @affects ) {
1618             for my $in ( @itemnumbers ) {
1619                 my $item = Koha::Items->find( $in ); # FIXME We do not need that, we already have Koha::Items from $order_obj->items
1620                 my $biblio = $item->biblio;
1621                 my ( $itemfield ) = GetMarcFromKohaField( 'items.itemnumber' );
1622                 my $item_marc = C4::Items::GetMarcItem( $biblio->biblionumber, $in );
1623                 for my $affect ( @affects ) {
1624                     my ( $sf, $v ) = split q{=}, $affect, 2;
1625                     foreach ( $item_marc->field($itemfield) ) {
1626                         $_->update( $sf => $v );
1627                     }
1628                 }
1629                 C4::Items::ModItemFromMarc( $item_marc, $biblio->biblionumber, $in );
1630             }
1631         }
1632     }
1633
1634     return $parent_ordernumber;
1635 }
1636
1637 sub _cancel_items_receipt {
1638     my ( $order, $parent_ordernumber ) = @_;
1639     $parent_ordernumber ||= $order->ordernumber;
1640
1641     my $items = $order->items;
1642     if ( $order->basket->effective_create_items eq 'receiving' ) {
1643         # Remove items that were created at receipt
1644         my $query = qq{
1645             DELETE FROM items, aqorders_items
1646             USING items, aqorders_items
1647             WHERE items.itemnumber = ? AND aqorders_items.itemnumber = ?
1648         };
1649         my $dbh = C4::Context->dbh;
1650         my $sth = $dbh->prepare($query);
1651         while ( my $item = $items->next ) {
1652             $sth->execute($item->itemnumber, $item->itemnumber);
1653         }
1654     } else {
1655         # Update items
1656         while ( my $item = $items->next ) {
1657             ModItemOrder($item->itemnumber, $parent_ordernumber);
1658         }
1659     }
1660 }
1661
1662 #------------------------------------------------------------#
1663
1664 =head3 SearchOrders
1665
1666 @results = &SearchOrders({
1667     ordernumber => $ordernumber,
1668     search => $search,
1669     ean => $ean,
1670     booksellerid => $booksellerid,
1671     basketno => $basketno,
1672     basketname => $basketname,
1673     basketgroupname => $basketgroupname,
1674     owner => $owner,
1675     pending => $pending
1676     ordered => $ordered
1677     biblionumber => $biblionumber,
1678     budget_id => $budget_id
1679 });
1680
1681 Searches for orders filtered by criteria.
1682
1683 C<$ordernumber> Finds matching orders or transferred orders by ordernumber.
1684 C<$search> Finds orders matching %$search% in title, author, or isbn.
1685 C<$owner> Finds order for the logged in user.
1686 C<$pending> Finds pending orders. Ignores completed and cancelled orders.
1687 C<$ordered> Finds orders to receive only (status 'ordered' or 'partial').
1688
1689
1690 C<@results> is an array of references-to-hash with the keys are fields
1691 from aqorders, biblio, biblioitems and aqbasket tables.
1692
1693 =cut
1694
1695 sub SearchOrders {
1696     my ( $params ) = @_;
1697     my $ordernumber = $params->{ordernumber};
1698     my $search = $params->{search};
1699     my $ean = $params->{ean};
1700     my $booksellerid = $params->{booksellerid};
1701     my $basketno = $params->{basketno};
1702     my $basketname = $params->{basketname};
1703     my $basketgroupname = $params->{basketgroupname};
1704     my $owner = $params->{owner};
1705     my $pending = $params->{pending};
1706     my $ordered = $params->{ordered};
1707     my $biblionumber = $params->{biblionumber};
1708     my $budget_id = $params->{budget_id};
1709
1710     my $dbh = C4::Context->dbh;
1711     my @args = ();
1712     my $query = q{
1713         SELECT aqbasket.basketno,
1714                borrowers.surname,
1715                borrowers.firstname,
1716                biblio.*,
1717                biblioitems.isbn,
1718                biblioitems.biblioitemnumber,
1719                biblioitems.publishercode,
1720                biblioitems.publicationyear,
1721                aqbasket.authorisedby,
1722                aqbasket.booksellerid,
1723                aqbasket.closedate,
1724                aqbasket.creationdate,
1725                aqbasket.basketname,
1726                aqbasketgroups.id as basketgroupid,
1727                aqbasketgroups.name as basketgroupname,
1728                aqorders.*
1729         FROM aqorders
1730             LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
1731             LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
1732             LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
1733             LEFT JOIN biblio ON aqorders.biblionumber=biblio.biblionumber
1734             LEFT JOIN biblioitems ON biblioitems.biblionumber=biblio.biblionumber
1735     };
1736
1737     # If we search on ordernumber, we retrieve the transferred order if a transfer has been done.
1738     $query .= q{
1739             LEFT JOIN aqorders_transfers ON aqorders_transfers.ordernumber_to = aqorders.ordernumber
1740     } if $ordernumber;
1741
1742     $query .= q{
1743         WHERE (datecancellationprinted is NULL)
1744     };
1745
1746     if ( $pending or $ordered ) {
1747         $query .= q{
1748             AND (
1749                 ( aqbasket.is_standing AND aqorders.orderstatus IN ( "new", "ordered", "partial" ) )
1750                 OR (
1751                     ( quantity > quantityreceived OR quantityreceived is NULL )
1752         };
1753
1754         if ( $ordered ) {
1755             $query .= q{ AND aqorders.orderstatus IN ( "ordered", "partial" )};
1756         }
1757         $query .= q{
1758                 )
1759             )
1760         };
1761     }
1762
1763     my $userenv = C4::Context->userenv;
1764     if ( C4::Context->preference("IndependentBranches") ) {
1765         unless ( C4::Context->IsSuperLibrarian() ) {
1766             $query .= q{
1767                 AND (
1768                     borrowers.branchcode = ?
1769                     OR borrowers.branchcode  = ''
1770                 )
1771             };
1772             push @args, $userenv->{branch};
1773         }
1774     }
1775
1776     if ( $ordernumber ) {
1777         $query .= ' AND ( aqorders.ordernumber = ? OR aqorders_transfers.ordernumber_from = ? ) ';
1778         push @args, ( $ordernumber, $ordernumber );
1779     }
1780     if ( $biblionumber ) {
1781         $query .= 'AND aqorders.biblionumber = ?';
1782         push @args, $biblionumber;
1783     }
1784     if( $search ) {
1785         $query .= ' AND (biblio.title LIKE ? OR biblio.author LIKE ? OR biblioitems.isbn LIKE ?)';
1786         push @args, ("%$search%","%$search%","%$search%");
1787     }
1788     if ( $ean ) {
1789         $query .= ' AND biblioitems.ean = ?';
1790         push @args, $ean;
1791     }
1792     if ( $booksellerid ) {
1793         $query .= 'AND aqbasket.booksellerid = ?';
1794         push @args, $booksellerid;
1795     }
1796     if( $basketno ) {
1797         $query .= 'AND aqbasket.basketno = ?';
1798         push @args, $basketno;
1799     }
1800     if( $basketname ) {
1801         $query .= 'AND aqbasket.basketname LIKE ?';
1802         push @args, "%$basketname%";
1803     }
1804     if( $basketgroupname ) {
1805         $query .= ' AND aqbasketgroups.name LIKE ?';
1806         push @args, "%$basketgroupname%";
1807     }
1808
1809     if ( $owner ) {
1810         $query .= ' AND aqbasket.authorisedby=? ';
1811         push @args, $userenv->{'number'};
1812     }
1813
1814     if ( $budget_id ) {
1815         $query .= ' AND aqorders.budget_id = ?';
1816         push @args, $budget_id;
1817     }
1818
1819     $query .= ' ORDER BY aqbasket.basketno';
1820
1821     my $sth = $dbh->prepare($query);
1822     $sth->execute(@args);
1823     return $sth->fetchall_arrayref({});
1824 }
1825
1826 #------------------------------------------------------------#
1827
1828 =head3 DelOrder
1829
1830   &DelOrder($biblionumber, $ordernumber);
1831
1832 Cancel the order with the given order and biblio numbers. It does not
1833 delete any entries in the aqorders table, it merely marks them as
1834 cancelled.
1835
1836 =cut
1837
1838 sub DelOrder {
1839     my ( $bibnum, $ordernumber, $delete_biblio, $reason ) = @_;
1840     my $error;
1841     my $dbh = C4::Context->dbh;
1842     my $query = "
1843         UPDATE aqorders
1844         SET    datecancellationprinted=now(), orderstatus='cancelled'
1845     ";
1846     if($reason) {
1847         $query .= ", cancellationreason = ? ";
1848     }
1849     $query .= "
1850         WHERE biblionumber=? AND ordernumber=?
1851     ";
1852     my $sth = $dbh->prepare($query);
1853     if($reason) {
1854         $sth->execute($reason, $bibnum, $ordernumber);
1855     } else {
1856         $sth->execute( $bibnum, $ordernumber );
1857     }
1858     $sth->finish;
1859
1860     my $order = Koha::Acquisition::Orders->find($ordernumber);
1861     my $items = $order->items;
1862     while ( my $item = $items->next ) { # Should be moved to Koha::Acquisition::Order->delete
1863         my $delcheck = $item->safe_delete;
1864
1865         if($delcheck ne '1') {
1866             $error->{'delitem'} = 1;
1867         }
1868     }
1869
1870     if($delete_biblio) {
1871         # We get the number of remaining items
1872         my $biblio = Koha::Biblios->find( $bibnum );
1873         my $itemcount = $biblio->items->count;
1874
1875         # If there are no items left,
1876         if ( $itemcount == 0 ) {
1877             # We delete the record
1878             my $delcheck = DelBiblio($bibnum);
1879
1880             if($delcheck) {
1881                 $error->{'delbiblio'} = 1;
1882             }
1883         }
1884     }
1885
1886     return $error;
1887 }
1888
1889 =head3 TransferOrder
1890
1891     my $newordernumber = TransferOrder($ordernumber, $basketno);
1892
1893 Transfer an order line to a basket.
1894 Mark $ordernumber as cancelled with an internal note 'Cancelled and transferred
1895 to BOOKSELLER on DATE' and create new order with internal note
1896 'Transferred from BOOKSELLER on DATE'.
1897 Move all attached items to the new order.
1898 Received orders cannot be transferred.
1899 Return the ordernumber of created order.
1900
1901 =cut
1902
1903 sub TransferOrder {
1904     my ($ordernumber, $basketno) = @_;
1905
1906     return unless ($ordernumber and $basketno);
1907
1908     my $order = Koha::Acquisition::Orders->find( $ordernumber ) or return;
1909     return if $order->datereceived;
1910
1911     $order = $order->unblessed;
1912
1913     my $basket = GetBasket($basketno);
1914     return unless $basket;
1915
1916     my $dbh = C4::Context->dbh;
1917     my ($query, $sth, $rv);
1918
1919     $query = q{
1920         UPDATE aqorders
1921         SET datecancellationprinted = CAST(NOW() AS date), orderstatus = ?
1922         WHERE ordernumber = ?
1923     };
1924     $sth = $dbh->prepare($query);
1925     $rv = $sth->execute('cancelled', $ordernumber);
1926
1927     delete $order->{'ordernumber'};
1928     delete $order->{parent_ordernumber};
1929     $order->{'basketno'} = $basketno;
1930
1931     my $newordernumber = Koha::Acquisition::Order->new($order)->store->ordernumber;
1932
1933     $query = q{
1934         UPDATE aqorders_items
1935         SET ordernumber = ?
1936         WHERE ordernumber = ?
1937     };
1938     $sth = $dbh->prepare($query);
1939     $sth->execute($newordernumber, $ordernumber);
1940
1941     $query = q{
1942         INSERT INTO aqorders_transfers (ordernumber_from, ordernumber_to)
1943         VALUES (?, ?)
1944     };
1945     $sth = $dbh->prepare($query);
1946     $sth->execute($ordernumber, $newordernumber);
1947
1948     return $newordernumber;
1949 }
1950
1951 =head3 get_rounding_sql
1952
1953     $rounding_sql = get_rounding_sql($column_name);
1954
1955 returns the correct SQL routine based on OrderPriceRounding system preference.
1956
1957 =cut
1958
1959 sub get_rounding_sql {
1960     my ( $round_string ) = @_;
1961     my $rounding_pref = C4::Context->preference('OrderPriceRounding') // q{};
1962     if ( $rounding_pref eq "nearest_cent"  ) {
1963         return "CAST($round_string*100 AS SIGNED)/100";
1964     }
1965     return $round_string;
1966 }
1967
1968 =head3 get_rounded_price
1969
1970     $rounded_price = get_rounded_price( $price );
1971
1972 returns a price rounded as specified in OrderPriceRounding system preference.
1973
1974 =cut
1975
1976 sub get_rounded_price {
1977     my ( $price ) =  @_;
1978     my $rounding_pref = C4::Context->preference('OrderPriceRounding') // q{};
1979     if( $rounding_pref eq 'nearest_cent' ) {
1980         return Koha::Number::Price->new( $price )->round();
1981     }
1982     return $price;
1983 }
1984
1985
1986 =head2 FUNCTIONS ABOUT PARCELS
1987
1988 =head3 GetParcels
1989
1990   $results = &GetParcels($bookseller, $order, $code, $datefrom, $dateto);
1991
1992 get a lists of parcels.
1993
1994 * Input arg :
1995
1996 =over
1997
1998 =item $bookseller
1999 is the bookseller this function has to get parcels.
2000
2001 =item $order
2002 To know on what criteria the results list has to be ordered.
2003
2004 =item $code
2005 is the booksellerinvoicenumber.
2006
2007 =item $datefrom & $dateto
2008 to know on what date this function has to filter its search.
2009
2010 =back
2011
2012 * return:
2013 a pointer on a hash list containing parcel informations as such :
2014
2015 =over
2016
2017 =item Creation date
2018
2019 =item Last operation
2020
2021 =item Number of biblio
2022
2023 =item Number of items
2024
2025 =back
2026
2027 =cut
2028
2029 sub GetParcels {
2030     my ($bookseller,$order, $code, $datefrom, $dateto) = @_;
2031     my $dbh    = C4::Context->dbh;
2032     my @query_params = ();
2033     my $strsth ="
2034         SELECT  aqinvoices.invoicenumber,
2035                 datereceived,purchaseordernumber,
2036                 count(DISTINCT biblionumber) AS biblio,
2037                 sum(quantity) AS itemsexpected,
2038                 sum(quantityreceived) AS itemsreceived
2039         FROM   aqorders LEFT JOIN aqbasket ON aqbasket.basketno = aqorders.basketno
2040         LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
2041         WHERE aqbasket.booksellerid = ? and datereceived IS NOT NULL
2042     ";
2043     push @query_params, $bookseller;
2044
2045     if ( defined $code ) {
2046         $strsth .= ' and aqinvoices.invoicenumber like ? ';
2047         # add a % to the end of the code to allow stemming.
2048         push @query_params, "$code%";
2049     }
2050
2051     if ( defined $datefrom ) {
2052         $strsth .= ' and datereceived >= ? ';
2053         push @query_params, $datefrom;
2054     }
2055
2056     if ( defined $dateto ) {
2057         $strsth .=  'and datereceived <= ? ';
2058         push @query_params, $dateto;
2059     }
2060
2061     $strsth .= "group by aqinvoices.invoicenumber,datereceived ";
2062
2063     # can't use a placeholder to place this column name.
2064     # but, we could probably be checking to make sure it is a column that will be fetched.
2065     $strsth .= "order by $order " if ($order);
2066
2067     my $sth = $dbh->prepare($strsth);
2068
2069     $sth->execute( @query_params );
2070     my $results = $sth->fetchall_arrayref({});
2071     return @{$results};
2072 }
2073
2074 #------------------------------------------------------------#
2075
2076 =head3 GetHistory
2077
2078   \@order_loop = GetHistory( %params );
2079
2080 Retreives some acquisition history information
2081
2082 params:  
2083   title
2084   author
2085   name
2086   isbn
2087   ean
2088   from_placed_on
2089   to_placed_on
2090   basket                  - search both basket name and number
2091   booksellerinvoicenumber 
2092   basketgroupname
2093   budget
2094   orderstatus (note that orderstatus '' will retrieve orders
2095                of any status except cancelled)
2096   managing_library
2097   biblionumber
2098   get_canceled_order (if set to a true value, cancelled orders will
2099                       be included)
2100
2101 returns:
2102     $order_loop is a list of hashrefs that each look like this:
2103             {
2104                 'author'           => 'Twain, Mark',
2105                 'basketno'         => '1',
2106                 'biblionumber'     => '215',
2107                 'count'            => 1,
2108                 'creationdate'     => 'MM/DD/YYYY',
2109                 'datereceived'     => undef,
2110                 'ecost'            => '1.00',
2111                 'id'               => '1',
2112                 'invoicenumber'    => undef,
2113                 'name'             => '',
2114                 'ordernumber'      => '1',
2115                 'quantity'         => 1,
2116                 'quantityreceived' => undef,
2117                 'title'            => 'The Adventures of Huckleberry Finn',
2118                 'managing_library' => 'CPL'
2119             }
2120
2121 =cut
2122
2123 sub GetHistory {
2124 # don't run the query if there are no parameters (list would be too long for sure !)
2125     croak "No search params" unless @_;
2126     my %params = @_;
2127     my $title = $params{title};
2128     my $author = $params{author};
2129     my $isbn   = $params{isbn};
2130     my $ean    = $params{ean};
2131     my $name = $params{name};
2132     my $from_placed_on = $params{from_placed_on};
2133     my $to_placed_on = $params{to_placed_on};
2134     my $basket = $params{basket};
2135     my $booksellerinvoicenumber = $params{booksellerinvoicenumber};
2136     my $basketgroupname = $params{basketgroupname};
2137     my $budget = $params{budget};
2138     my $orderstatus = $params{orderstatus};
2139     my $biblionumber = $params{biblionumber};
2140     my $get_canceled_order = $params{get_canceled_order} || 0;
2141     my $ordernumber = $params{ordernumber};
2142     my $search_children_too = $params{search_children_too} || 0;
2143     my $created_by = $params{created_by} || [];
2144     my $managing_library = $params{managing_library};
2145     my $ordernumbers = $params{ordernumbers} || [];
2146     my $additional_fields = $params{additional_fields} // [];
2147
2148     my $total_qty         = 0;
2149     my $total_qtyreceived = 0;
2150     my $total_price       = 0;
2151
2152     #get variation of isbn
2153     my @isbn_params;
2154     my @isbns;
2155     if ($isbn){
2156         if ( C4::Context->preference("SearchWithISBNVariations") ){
2157             @isbns = C4::Koha::GetVariationsOfISBN( $isbn );
2158             foreach my $isb (@isbns){
2159                 push @isbn_params, '?';
2160             }
2161         }
2162         unless (@isbns){
2163             push @isbns, $isbn;
2164             push @isbn_params, '?';
2165         }
2166     }
2167
2168     my $dbh   = C4::Context->dbh;
2169     my $query ="
2170         SELECT
2171             COALESCE(biblio.title,     deletedbiblio.title)     AS title,
2172             COALESCE(biblio.author,    deletedbiblio.author)    AS author,
2173             COALESCE(biblioitems.isbn, deletedbiblioitems.isbn) AS isbn,
2174             COALESCE(biblioitems.ean,  deletedbiblioitems.ean)  AS ean,
2175             aqorders.basketno,
2176             aqbasket.basketname,
2177             aqbasket.basketgroupid,
2178             aqbasket.authorisedby,
2179             concat( borrowers.firstname,' ',borrowers.surname) AS authorisedbyname,
2180             branch as managing_library,
2181             aqbasketgroups.name as groupname,
2182             aqbooksellers.name,
2183             aqbasket.creationdate,
2184             aqorders.datereceived,
2185             aqorders.quantity,
2186             aqorders.quantityreceived,
2187             aqorders.ecost,
2188             aqorders.ordernumber,
2189             aqorders.invoiceid,
2190             aqinvoices.invoicenumber,
2191             aqbooksellers.id as id,
2192             aqorders.biblionumber,
2193             aqorders.orderstatus,
2194             aqorders.parent_ordernumber,
2195             aqbudgets.budget_name
2196             ";
2197     $query .= ", aqbudgets.budget_id AS budget" if defined $budget;
2198     $query .= "
2199         FROM aqorders
2200         LEFT JOIN aqbasket ON aqorders.basketno=aqbasket.basketno
2201         LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid=aqbasketgroups.id
2202         LEFT JOIN aqbooksellers ON aqbasket.booksellerid=aqbooksellers.id
2203         LEFT JOIN biblioitems ON biblioitems.biblionumber=aqorders.biblionumber
2204         LEFT JOIN biblio ON biblio.biblionumber=aqorders.biblionumber
2205         LEFT JOIN aqbudgets ON aqorders.budget_id=aqbudgets.budget_id
2206         LEFT JOIN aqinvoices ON aqorders.invoiceid = aqinvoices.invoiceid
2207         LEFT JOIN deletedbiblio ON deletedbiblio.biblionumber=aqorders.biblionumber
2208         LEFT JOIN deletedbiblioitems ON deletedbiblioitems.biblionumber=aqorders.biblionumber
2209         LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
2210         ";
2211
2212     $query .= " WHERE 1 ";
2213
2214     unless ($get_canceled_order or (defined $orderstatus and $orderstatus eq 'cancelled')) {
2215         $query .= " AND datecancellationprinted IS NULL ";
2216     }
2217
2218     my @query_params  = ();
2219
2220     if ( $biblionumber ) {
2221         $query .= " AND biblio.biblionumber = ?";
2222         push @query_params, $biblionumber;
2223     }
2224
2225     if ( $title ) {
2226         $query .= " AND biblio.title LIKE ? ";
2227         $title =~ s/\s+/%/g;
2228         push @query_params, "%$title%";
2229     }
2230
2231     if ( $author ) {
2232         $query .= " AND biblio.author LIKE ? ";
2233         push @query_params, "%$author%";
2234     }
2235
2236     if ( @isbns ) {
2237         $query .= " AND ( biblioitems.isbn LIKE " . join (" OR biblioitems.isbn LIKE ", @isbn_params ) . ")";
2238         foreach my $isb (@isbns){
2239             push @query_params, "%$isb%";
2240         }
2241     }
2242
2243     if ( $ean ) {
2244         $query .= " AND biblioitems.ean = ? ";
2245         push @query_params, "$ean";
2246     }
2247     if ( $name ) {
2248         $query .= " AND aqbooksellers.name LIKE ? ";
2249         push @query_params, "%$name%";
2250     }
2251
2252     if ( $budget ) {
2253         $query .= " AND aqbudgets.budget_id = ? ";
2254         push @query_params, "$budget";
2255     }
2256
2257     if ( $from_placed_on ) {
2258         $query .= " AND creationdate >= ? ";
2259         push @query_params, $from_placed_on;
2260     }
2261
2262     if ( $to_placed_on ) {
2263         $query .= " AND creationdate <= ? ";
2264         push @query_params, $to_placed_on;
2265     }
2266
2267     if ( defined $orderstatus and $orderstatus ne '') {
2268         $query .= " AND aqorders.orderstatus = ? ";
2269         push @query_params, "$orderstatus";
2270     }
2271
2272     if ($basket) {
2273         if ($basket =~ m/^\d+$/) {
2274             $query .= " AND aqorders.basketno = ? ";
2275             push @query_params, $basket;
2276         } else {
2277             $query .= " AND aqbasket.basketname LIKE ? ";
2278             push @query_params, "%$basket%";
2279         }
2280     }
2281
2282     if ($booksellerinvoicenumber) {
2283         $query .= " AND aqinvoices.invoicenumber LIKE ? ";
2284         push @query_params, "%$booksellerinvoicenumber%";
2285     }
2286
2287     if ($basketgroupname) {
2288         $query .= " AND aqbasketgroups.name LIKE ? ";
2289         push @query_params, "%$basketgroupname%";
2290     }
2291
2292     if ($ordernumber) {
2293         $query .= " AND (aqorders.ordernumber = ? ";
2294         push @query_params, $ordernumber;
2295         if ($search_children_too) {
2296             $query .= " OR aqorders.parent_ordernumber = ? ";
2297             push @query_params, $ordernumber;
2298         }
2299         $query .= ") ";
2300     }
2301
2302     if ( @$created_by ) {
2303         $query .= ' AND aqbasket.authorisedby IN ( ' . join( ',', ('?') x @$created_by ) . ')';
2304         push @query_params, @$created_by;
2305     }
2306
2307     if ( $managing_library ) {
2308         $query .= " AND aqbasket.branch = ? ";
2309         push @query_params, $managing_library;
2310     }
2311
2312     if ( @$ordernumbers ) {
2313         $query .= ' AND (aqorders.ordernumber IN ( ' . join (',', ('?') x @$ordernumbers ) . '))';
2314         push @query_params, @$ordernumbers;
2315     }
2316     if ( @$additional_fields ) {
2317         my @baskets = Koha::Acquisition::Baskets->filter_by_additional_fields($additional_fields);
2318
2319         return [] unless @baskets;
2320
2321         # No parameterization because record IDs come directly from DB
2322         $query .= ' AND aqbasket.basketno IN ( ' . join( ',', map { $_->basketno } @baskets ) . ' )';
2323     }
2324
2325     if ( C4::Context->preference("IndependentBranches") ) {
2326         unless ( C4::Context->IsSuperLibrarian() ) {
2327             $query .= " AND (borrowers.branchcode = ? OR borrowers.branchcode ='' ) ";
2328             push @query_params, C4::Context->userenv->{branch};
2329         }
2330     }
2331     $query .= " ORDER BY id";
2332
2333     return $dbh->selectall_arrayref( $query, { Slice => {} }, @query_params );
2334 }
2335
2336 =head2 GetRecentAcqui
2337
2338   $results = GetRecentAcqui($days);
2339
2340 C<$results> is a ref to a table which contains hashref
2341
2342 =cut
2343
2344 sub GetRecentAcqui {
2345     my $limit  = shift;
2346     my $dbh    = C4::Context->dbh;
2347     my $query = "
2348         SELECT *
2349         FROM   biblio
2350         ORDER BY timestamp DESC
2351         LIMIT  0,".$limit;
2352
2353     my $sth = $dbh->prepare($query);
2354     $sth->execute;
2355     my $results = $sth->fetchall_arrayref({});
2356     return $results;
2357 }
2358
2359 #------------------------------------------------------------#
2360
2361 =head3 AddClaim
2362
2363   &AddClaim($ordernumber);
2364
2365 Add a claim for an order
2366
2367 =cut
2368
2369 sub AddClaim {
2370     my ($ordernumber) = @_;
2371     my $dbh          = C4::Context->dbh;
2372     my $query        = "
2373         UPDATE aqorders SET
2374             claims_count = claims_count + 1,
2375             claimed_date = CURDATE()
2376         WHERE ordernumber = ?
2377         ";
2378     my $sth = $dbh->prepare($query);
2379     $sth->execute($ordernumber);
2380 }
2381
2382 =head3 GetInvoices
2383
2384     my @invoices = GetInvoices(
2385         invoicenumber => $invoicenumber,
2386         supplierid => $supplierid,
2387         suppliername => $suppliername,
2388         shipmentdatefrom => $shipmentdatefrom, # ISO format
2389         shipmentdateto => $shipmentdateto, # ISO format
2390         billingdatefrom => $billingdatefrom, # ISO format
2391         billingdateto => $billingdateto, # ISO format
2392         isbneanissn => $isbn_or_ean_or_issn,
2393         title => $title,
2394         author => $author,
2395         publisher => $publisher,
2396         publicationyear => $publicationyear,
2397         branchcode => $branchcode,
2398         order_by => $order_by
2399     );
2400
2401 Return a list of invoices that match all given criteria.
2402
2403 $order_by is "column_name (asc|desc)", where column_name is any of
2404 'invoicenumber', 'booksellerid', 'shipmentdate', 'billingdate', 'closedate',
2405 'shipmentcost', 'shipmentcost_budgetid'.
2406
2407 asc is the default if omitted
2408
2409 =cut
2410
2411 sub GetInvoices {
2412     my %args = @_;
2413
2414     my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2415         closedate shipmentcost shipmentcost_budgetid);
2416
2417     my $dbh = C4::Context->dbh;
2418     my $query = qq{
2419         SELECT aqinvoices.invoiceid, aqinvoices.invoicenumber, aqinvoices.booksellerid, aqinvoices.shipmentdate, aqinvoices.billingdate, aqinvoices.closedate, aqinvoices.shipmentcost, aqinvoices.shipmentcost_budgetid, aqinvoices.message_id,
2420             aqbooksellers.name AS suppliername,
2421           COUNT(
2422             DISTINCT IF(
2423               aqorders.datereceived IS NOT NULL,
2424               aqorders.biblionumber,
2425               NULL
2426             )
2427           ) AS receivedbiblios,
2428           COUNT(
2429              DISTINCT IF(
2430               aqorders.subscriptionid IS NOT NULL,
2431               aqorders.subscriptionid,
2432               NULL
2433             )
2434           ) AS is_linked_to_subscriptions,
2435           SUM(aqorders.quantityreceived) AS receiveditems
2436         FROM aqinvoices
2437           LEFT JOIN aqbooksellers ON aqbooksellers.id = aqinvoices.booksellerid
2438           LEFT JOIN aqorders ON aqorders.invoiceid = aqinvoices.invoiceid
2439           LEFT JOIN aqbasket ON aqbasket.basketno=aqorders.basketno
2440           LEFT JOIN borrowers ON aqbasket.authorisedby=borrowers.borrowernumber
2441           LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2442           LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
2443           LEFT JOIN subscription ON biblio.biblionumber = subscription.biblionumber
2444     };
2445
2446     my @bind_args;
2447     my @bind_strs;
2448     if($args{supplierid}) {
2449         push @bind_strs, " aqinvoices.booksellerid = ? ";
2450         push @bind_args, $args{supplierid};
2451     }
2452     if($args{invoicenumber}) {
2453         push @bind_strs, " aqinvoices.invoicenumber LIKE ? ";
2454         push @bind_args, "%$args{invoicenumber}%";
2455     }
2456     if($args{suppliername}) {
2457         push @bind_strs, " aqbooksellers.name LIKE ? ";
2458         push @bind_args, "%$args{suppliername}%";
2459     }
2460     if($args{shipmentdatefrom}) {
2461         push @bind_strs, " aqinvoices.shipmentdate >= ? ";
2462         push @bind_args, $args{shipmentdatefrom};
2463     }
2464     if($args{shipmentdateto}) {
2465         push @bind_strs, " aqinvoices.shipmentdate <= ? ";
2466         push @bind_args, $args{shipmentdateto};
2467     }
2468     if($args{billingdatefrom}) {
2469         push @bind_strs, " aqinvoices.billingdate >= ? ";
2470         push @bind_args, $args{billingdatefrom};
2471     }
2472     if($args{billingdateto}) {
2473         push @bind_strs, " aqinvoices.billingdate <= ? ";
2474         push @bind_args, $args{billingdateto};
2475     }
2476     if($args{isbneanissn}) {
2477         push @bind_strs, " (biblioitems.isbn LIKE CONCAT('%', ?, '%') OR biblioitems.ean LIKE CONCAT('%', ?, '%') OR biblioitems.issn LIKE CONCAT('%', ?, '%') ) ";
2478         push @bind_args, $args{isbneanissn}, $args{isbneanissn}, $args{isbneanissn};
2479     }
2480     if($args{title}) {
2481         push @bind_strs, " biblio.title LIKE CONCAT('%', ?, '%') ";
2482         push @bind_args, $args{title};
2483     }
2484     if($args{author}) {
2485         push @bind_strs, " biblio.author LIKE CONCAT('%', ?, '%') ";
2486         push @bind_args, $args{author};
2487     }
2488     if($args{publisher}) {
2489         push @bind_strs, " biblioitems.publishercode LIKE CONCAT('%', ?, '%') ";
2490         push @bind_args, $args{publisher};
2491     }
2492     if($args{publicationyear}) {
2493         push @bind_strs, " ((biblioitems.publicationyear LIKE CONCAT('%', ?, '%')) OR (biblio.copyrightdate LIKE CONCAT('%', ?, '%'))) ";
2494         push @bind_args, $args{publicationyear}, $args{publicationyear};
2495     }
2496     if($args{branchcode}) {
2497         push @bind_strs, " borrowers.branchcode = ? ";
2498         push @bind_args, $args{branchcode};
2499     }
2500     if($args{message_id}) {
2501         push @bind_strs, " aqinvoices.message_id = ? ";
2502         push @bind_args, $args{message_id};
2503     }
2504
2505     $query .= " WHERE " . join(" AND ", @bind_strs) if @bind_strs;
2506     $query .= " GROUP BY aqinvoices.invoiceid, aqinvoices.invoicenumber, aqinvoices.booksellerid, aqinvoices.shipmentdate, aqinvoices.billingdate, aqinvoices.closedate, aqinvoices.shipmentcost, aqinvoices.shipmentcost_budgetid, aqinvoices.message_id, aqbooksellers.name";
2507
2508     if($args{order_by}) {
2509         my ($column, $direction) = split / /, $args{order_by};
2510         if(grep  { $_ eq $column } @columns) {
2511             $direction ||= 'ASC';
2512             $query .= " ORDER BY $column $direction";
2513         }
2514     }
2515
2516     my $sth = $dbh->prepare($query);
2517     $sth->execute(@bind_args);
2518
2519     my $results = $sth->fetchall_arrayref({});
2520     return @$results;
2521 }
2522
2523 =head3 GetInvoice
2524
2525     my $invoice = GetInvoice($invoiceid);
2526
2527 Get informations about invoice with given $invoiceid
2528
2529 Return a hash filled with aqinvoices.* fields
2530
2531 =cut
2532
2533 sub GetInvoice {
2534     my ($invoiceid) = @_;
2535     my $invoice;
2536
2537     return unless $invoiceid;
2538
2539     my $dbh = C4::Context->dbh;
2540     my $query = qq{
2541         SELECT *
2542         FROM aqinvoices
2543         WHERE invoiceid = ?
2544     };
2545     my $sth = $dbh->prepare($query);
2546     $sth->execute($invoiceid);
2547
2548     $invoice = $sth->fetchrow_hashref;
2549     return $invoice;
2550 }
2551
2552 =head3 GetInvoiceDetails
2553
2554     my $invoice = GetInvoiceDetails($invoiceid)
2555
2556 Return informations about an invoice + the list of related order lines
2557
2558 Orders informations are in $invoice->{orders} (array ref)
2559
2560 =cut
2561
2562 sub GetInvoiceDetails {
2563     my ($invoiceid) = @_;
2564
2565     if ( !defined $invoiceid ) {
2566         carp 'GetInvoiceDetails called without an invoiceid';
2567         return;
2568     }
2569
2570     my $dbh = C4::Context->dbh;
2571     my $query = q{
2572         SELECT aqinvoices.*, aqbooksellers.name AS suppliername
2573         FROM aqinvoices
2574           LEFT JOIN aqbooksellers ON aqinvoices.booksellerid = aqbooksellers.id
2575         WHERE invoiceid = ?
2576     };
2577     my $sth = $dbh->prepare($query);
2578     $sth->execute($invoiceid);
2579
2580     my $invoice = $sth->fetchrow_hashref;
2581
2582     $query = q{
2583         SELECT aqorders.*,
2584                 biblio.*,
2585                 biblio.copyrightdate,
2586                 biblioitems.isbn,
2587                 biblioitems.publishercode,
2588                 biblioitems.publicationyear,
2589                 aqbasket.basketname,
2590                 aqbasketgroups.id AS basketgroupid,
2591                 aqbasketgroups.name AS basketgroupname
2592         FROM aqorders
2593           LEFT JOIN aqbasket ON aqorders.basketno = aqbasket.basketno
2594           LEFT JOIN aqbasketgroups ON aqbasket.basketgroupid = aqbasketgroups.id
2595           LEFT JOIN biblio ON aqorders.biblionumber = biblio.biblionumber
2596           LEFT JOIN biblioitems ON aqorders.biblionumber = biblioitems.biblionumber
2597         WHERE invoiceid = ?
2598     };
2599     $sth = $dbh->prepare($query);
2600     $sth->execute($invoiceid);
2601     $invoice->{orders} = $sth->fetchall_arrayref({});
2602     $invoice->{orders} ||= []; # force an empty arrayref if fetchall_arrayref fails
2603
2604     return $invoice;
2605 }
2606
2607 =head3 AddInvoice
2608
2609     my $invoiceid = AddInvoice(
2610         invoicenumber => $invoicenumber,
2611         booksellerid => $booksellerid,
2612         shipmentdate => $shipmentdate,
2613         billingdate => $billingdate,
2614         closedate => $closedate,
2615         shipmentcost => $shipmentcost,
2616         shipmentcost_budgetid => $shipmentcost_budgetid
2617     );
2618
2619 Create a new invoice and return its id or undef if it fails.
2620
2621 =cut
2622
2623 sub AddInvoice {
2624     my %invoice = @_;
2625
2626     return unless(%invoice and $invoice{invoicenumber});
2627
2628     my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2629         closedate shipmentcost shipmentcost_budgetid message_id);
2630
2631     my @set_strs;
2632     my @set_args;
2633     foreach my $key (keys %invoice) {
2634         if(0 < grep { $_ eq $key } @columns) {
2635             push @set_strs, "$key = ?";
2636             push @set_args, ($invoice{$key} || undef);
2637         }
2638     }
2639
2640     my $rv;
2641     if(@set_args > 0) {
2642         my $dbh = C4::Context->dbh;
2643         my $query = "INSERT INTO aqinvoices SET ";
2644         $query .= join (",", @set_strs);
2645         my $sth = $dbh->prepare($query);
2646         $rv = $sth->execute(@set_args);
2647         if($rv) {
2648             $rv = $dbh->last_insert_id(undef, undef, 'aqinvoices', undef);
2649         }
2650     }
2651     return $rv;
2652 }
2653
2654 =head3 ModInvoice
2655
2656     ModInvoice(
2657         invoiceid => $invoiceid,    # Mandatory
2658         invoicenumber => $invoicenumber,
2659         booksellerid => $booksellerid,
2660         shipmentdate => $shipmentdate,
2661         billingdate => $billingdate,
2662         closedate => $closedate,
2663         shipmentcost => $shipmentcost,
2664         shipmentcost_budgetid => $shipmentcost_budgetid
2665     );
2666
2667 Modify an invoice, invoiceid is mandatory.
2668
2669 Return undef if it fails.
2670
2671 =cut
2672
2673 sub ModInvoice {
2674     my %invoice = @_;
2675
2676     return unless(%invoice and $invoice{invoiceid});
2677
2678     my @columns = qw(invoicenumber booksellerid shipmentdate billingdate
2679         closedate shipmentcost shipmentcost_budgetid);
2680
2681     my @set_strs;
2682     my @set_args;
2683     foreach my $key (keys %invoice) {
2684         if(0 < grep { $_ eq $key } @columns) {
2685             push @set_strs, "$key = ?";
2686             push @set_args, ($invoice{$key} || undef);
2687         }
2688     }
2689
2690     my $dbh = C4::Context->dbh;
2691     my $query = "UPDATE aqinvoices SET ";
2692     $query .= join(",", @set_strs);
2693     $query .= " WHERE invoiceid = ?";
2694
2695     my $sth = $dbh->prepare($query);
2696     $sth->execute(@set_args, $invoice{invoiceid});
2697 }
2698
2699 =head3 CloseInvoice
2700
2701     CloseInvoice($invoiceid);
2702
2703 Close an invoice.
2704
2705 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => undef);
2706
2707 =cut
2708
2709 sub CloseInvoice {
2710     my ($invoiceid) = @_;
2711
2712     return unless $invoiceid;
2713
2714     my $dbh = C4::Context->dbh;
2715     my $query = qq{
2716         UPDATE aqinvoices
2717         SET closedate = CAST(NOW() AS DATE)
2718         WHERE invoiceid = ?
2719     };
2720     my $sth = $dbh->prepare($query);
2721     $sth->execute($invoiceid);
2722 }
2723
2724 =head3 ReopenInvoice
2725
2726     ReopenInvoice($invoiceid);
2727
2728 Reopen an invoice
2729
2730 Equivalent to ModInvoice(invoiceid => $invoiceid, closedate => output_pref({ dt=>dt_from_string, dateonly=>1, otputpref=>'iso' }))
2731
2732 =cut
2733
2734 sub ReopenInvoice {
2735     my ($invoiceid) = @_;
2736
2737     return unless $invoiceid;
2738
2739     my $dbh = C4::Context->dbh;
2740     my $query = qq{
2741         UPDATE aqinvoices
2742         SET closedate = NULL
2743         WHERE invoiceid = ?
2744     };
2745     my $sth = $dbh->prepare($query);
2746     $sth->execute($invoiceid);
2747 }
2748
2749 =head3 DelInvoice
2750
2751     DelInvoice($invoiceid);
2752
2753 Delete an invoice if there are no items attached to it.
2754
2755 =cut
2756
2757 sub DelInvoice {
2758     my ($invoiceid) = @_;
2759
2760     return unless $invoiceid;
2761
2762     my $dbh   = C4::Context->dbh;
2763     my $query = qq{
2764         SELECT COUNT(*)
2765         FROM aqorders
2766         WHERE invoiceid = ?
2767     };
2768     my $sth = $dbh->prepare($query);
2769     $sth->execute($invoiceid);
2770     my $res = $sth->fetchrow_arrayref;
2771     if ( $res && $res->[0] == 0 ) {
2772         $query = qq{
2773             DELETE FROM aqinvoices
2774             WHERE invoiceid = ?
2775         };
2776         my $sth = $dbh->prepare($query);
2777         return ( $sth->execute($invoiceid) > 0 );
2778     }
2779     return;
2780 }
2781
2782 =head3 MergeInvoices
2783
2784     MergeInvoices($invoiceid, \@sourceids);
2785
2786 Merge the invoices identified by the IDs in \@sourceids into
2787 the invoice identified by $invoiceid.
2788
2789 =cut
2790
2791 sub MergeInvoices {
2792     my ($invoiceid, $sourceids) = @_;
2793
2794     return unless $invoiceid;
2795     foreach my $sourceid (@$sourceids) {
2796         next if $sourceid == $invoiceid;
2797         my $source = GetInvoiceDetails($sourceid);
2798         foreach my $order (@{$source->{'orders'}}) {
2799             $order->{'invoiceid'} = $invoiceid;
2800             ModOrder($order);
2801         }
2802         DelInvoice($source->{'invoiceid'});
2803     }
2804     return;
2805 }
2806
2807 =head3 GetBiblioCountByBasketno
2808
2809 $biblio_count = &GetBiblioCountByBasketno($basketno);
2810
2811 Looks up the biblio's count that has basketno value $basketno
2812
2813 Returns a quantity
2814
2815 =cut
2816
2817 sub GetBiblioCountByBasketno {
2818     my ($basketno) = @_;
2819     my $dbh          = C4::Context->dbh;
2820     my $query        = "
2821         SELECT COUNT( DISTINCT( biblionumber ) )
2822         FROM   aqorders
2823         WHERE  basketno = ?
2824             AND datecancellationprinted IS NULL
2825         ";
2826
2827     my $sth = $dbh->prepare($query);
2828     $sth->execute($basketno);
2829     return $sth->fetchrow;
2830 }
2831
2832 =head3 populate_order_with_prices
2833
2834 $order = populate_order_with_prices({
2835     order        => $order #a hashref with the order values
2836     booksellerid => $booksellerid #FIXME - should obtain from order basket
2837     receiving    => 1 # boolean representing order stage, should pass only this or ordering
2838     ordering     => 1 # boolean representing order stage
2839 });
2840
2841
2842 Sets calculated values for an order - all values are stored with full precision
2843 regardless of rounding preference except for tax value which is calculated
2844 on rounded values if requested
2845
2846 For ordering the values set are:
2847     rrp_tax_included
2848     rrp_tax_excluded
2849     ecost_tax_included
2850     ecost_tax_excluded
2851     tax_value_on_ordering
2852 For receiving the value set are:
2853     unitprice_tax_included
2854     unitprice_tax_excluded
2855     tax_value_on_receiving
2856
2857 Note: When receiving, if the rounded value of the unitprice matches the rounded
2858 value of the ecost then then ecost (full precision) is used.
2859
2860 Returns a hashref of the order
2861
2862 FIXME: Move this to Koha::Acquisition::Order.pm
2863
2864 =cut
2865
2866 sub populate_order_with_prices {
2867     my ($params) = @_;
2868
2869     my $order        = $params->{order};
2870     my $booksellerid = $params->{booksellerid};
2871     return unless $booksellerid;
2872
2873     my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
2874
2875     my $receiving = $params->{receiving};
2876     my $ordering  = $params->{ordering};
2877     my $discount  = $order->{discount};
2878     $discount /= 100 if $discount > 1;
2879
2880     if ($ordering) {
2881         $order->{tax_rate_on_ordering} //= $order->{tax_rate};
2882         if ( $bookseller->listincgst ) {
2883
2884             # The user entered the prices tax included
2885             $order->{unitprice} += 0;
2886             $order->{unitprice_tax_included} = $order->{unitprice};
2887             $order->{rrp_tax_included} = $order->{rrp};
2888
2889             # price tax excluded = price tax included / ( 1 + tax rate )
2890             $order->{unitprice_tax_excluded} = $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate_on_ordering} );
2891             $order->{rrp_tax_excluded} = $order->{rrp_tax_included} / ( 1 + $order->{tax_rate_on_ordering} );
2892
2893             # ecost tax included = rrp tax included  ( 1 - discount )
2894             $order->{ecost_tax_included} = $order->{rrp_tax_included} * ( 1 - $discount );
2895
2896             # ecost tax excluded = rrp tax excluded * ( 1 - discount )
2897             $order->{ecost_tax_excluded} = $order->{rrp_tax_excluded} * ( 1 - $discount );
2898
2899             # tax value = quantity * ecost tax excluded * tax rate
2900             # we should use the unitprice if included
2901             my $cost_tax_included = $order->{unitprice_tax_included} || $order->{ecost_tax_included};
2902             my $cost_tax_excluded = $order->{unitprice_tax_excluded} || $order->{ecost_tax_excluded};
2903             $order->{tax_value_on_ordering} = ( get_rounded_price($cost_tax_included) - get_rounded_price($cost_tax_excluded) ) * $order->{quantity};
2904
2905         }
2906         else {
2907             # The user entered the prices tax excluded
2908             $order->{unitprice_tax_excluded} = $order->{unitprice};
2909             $order->{rrp_tax_excluded} = $order->{rrp};
2910
2911             # price tax included = price tax excluded * ( 1 - tax rate )
2912             $order->{unitprice_tax_included} = $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} );
2913             $order->{rrp_tax_included} = $order->{rrp_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} );
2914
2915             # ecost tax excluded = rrp tax excluded * ( 1 - discount )
2916             $order->{ecost_tax_excluded} = $order->{rrp_tax_excluded} * ( 1 - $discount );
2917
2918             # ecost tax included = rrp tax excluded * ( 1 + tax rate ) * ( 1 - discount ) = ecost tax excluded * ( 1 + tax rate )
2919             $order->{ecost_tax_included} = $order->{ecost_tax_excluded} * ( 1 + $order->{tax_rate_on_ordering} );
2920
2921             # tax value = quantity * ecost tax included * tax rate
2922             # we should use the unitprice if included
2923             my $cost_tax_excluded = $order->{unitprice_tax_excluded} || $order->{ecost_tax_excluded};
2924             $order->{tax_value_on_ordering} = $order->{quantity} * get_rounded_price($cost_tax_excluded) * $order->{tax_rate_on_ordering};
2925         }
2926     }
2927
2928     if ($receiving) {
2929         $order->{tax_rate_on_receiving} //= $order->{tax_rate};
2930         if ( $bookseller->invoiceincgst ) {
2931             # Trick for unitprice. If the unit price rounded value is the same as the ecost rounded value
2932             # we need to keep the exact ecost value
2933             if ( Koha::Number::Price->new( $order->{unitprice} )->round == Koha::Number::Price->new( $order->{ecost_tax_included} )->round ) {
2934                 $order->{unitprice} = $order->{ecost_tax_included};
2935             }
2936
2937             # The user entered the unit price tax included
2938             $order->{unitprice_tax_included} = $order->{unitprice};
2939
2940             # unit price tax excluded = unit price tax included / ( 1 + tax rate )
2941             $order->{unitprice_tax_excluded} = $order->{unitprice_tax_included} / ( 1 + $order->{tax_rate_on_receiving} );
2942         }
2943         else {
2944             # Trick for unitprice. If the unit price rounded value is the same as the ecost rounded value
2945             # we need to keep the exact ecost value
2946             if ( Koha::Number::Price->new( $order->{unitprice} )->round == Koha::Number::Price->new( $order->{ecost_tax_excluded} )->round ) {
2947                 $order->{unitprice} = $order->{ecost_tax_excluded};
2948             }
2949
2950             # The user entered the unit price tax excluded
2951             $order->{unitprice_tax_excluded} = $order->{unitprice};
2952
2953
2954             # unit price tax included = unit price tax included * ( 1 + tax rate )
2955             $order->{unitprice_tax_included} = $order->{unitprice_tax_excluded} * ( 1 + $order->{tax_rate_on_receiving} );
2956         }
2957
2958         # tax value = quantity * unit price tax excluded * tax rate
2959         $order->{tax_value_on_receiving} = $order->{quantity} * get_rounded_price($order->{unitprice_tax_excluded}) * $order->{tax_rate_on_receiving};
2960     }
2961
2962     return $order;
2963 }
2964
2965 =head3 GetOrderUsers
2966
2967     $order_users_ids = &GetOrderUsers($ordernumber);
2968
2969 Returns a list of all borrowernumbers that are in order users list
2970
2971 =cut
2972
2973 sub GetOrderUsers {
2974     my ($ordernumber) = @_;
2975
2976     return unless $ordernumber;
2977
2978     my $query = q|
2979         SELECT borrowernumber
2980         FROM aqorder_users
2981         WHERE ordernumber = ?
2982     |;
2983     my $dbh = C4::Context->dbh;
2984     my $sth = $dbh->prepare($query);
2985     $sth->execute($ordernumber);
2986     my $results = $sth->fetchall_arrayref( {} );
2987
2988     my @borrowernumbers;
2989     foreach (@$results) {
2990         push @borrowernumbers, $_->{'borrowernumber'};
2991     }
2992
2993     return @borrowernumbers;
2994 }
2995
2996 =head3 ModOrderUsers
2997
2998     my @order_users_ids = (1, 2, 3);
2999     &ModOrderUsers($ordernumber, @basketusers_ids);
3000
3001 Delete all users from order users list, and add users in C<@order_users_ids>
3002 to this users list.
3003
3004 =cut
3005
3006 sub ModOrderUsers {
3007     my ( $ordernumber, @order_users_ids ) = @_;
3008
3009     return unless $ordernumber;
3010
3011     my $dbh   = C4::Context->dbh;
3012     my $query = q|
3013         DELETE FROM aqorder_users
3014         WHERE ordernumber = ?
3015     |;
3016     my $sth = $dbh->prepare($query);
3017     $sth->execute($ordernumber);
3018
3019     $query = q|
3020         INSERT INTO aqorder_users (ordernumber, borrowernumber)
3021         VALUES (?, ?)
3022     |;
3023     $sth = $dbh->prepare($query);
3024     foreach my $order_user_id (@order_users_ids) {
3025         $sth->execute( $ordernumber, $order_user_id );
3026     }
3027 }
3028
3029 sub NotifyOrderUsers {
3030     my ($ordernumber) = @_;
3031
3032     my @borrowernumbers = GetOrderUsers($ordernumber);
3033     return unless @borrowernumbers;
3034
3035     my $order = GetOrder( $ordernumber );
3036     for my $borrowernumber (@borrowernumbers) {
3037         my $patron = Koha::Patrons->find( $borrowernumber );
3038         my $library = $patron->library->unblessed;
3039         my $biblio = Koha::Biblios->find( $order->{biblionumber} )->unblessed;
3040         my $letter = C4::Letters::GetPreparedLetter(
3041             module      => 'acquisition',
3042             letter_code => 'ACQ_NOTIF_ON_RECEIV',
3043             branchcode  => $library->{branchcode},
3044             lang        => $patron->lang,
3045             tables      => {
3046                 'branches'    => $library,
3047                 'borrowers'   => $patron->unblessed,
3048                 'biblio'      => $biblio,
3049                 'aqorders'    => $order,
3050             },
3051         );
3052         if ( $letter ) {
3053             C4::Letters::EnqueueLetter(
3054                 {
3055                     letter         => $letter,
3056                     borrowernumber => $borrowernumber,
3057                     LibraryName    => C4::Context->preference("LibraryName"),
3058                     message_transport_type => 'email',
3059                 }
3060             ) or warn "can't enqueue letter $letter";
3061         }
3062     }
3063 }
3064
3065 =head3 FillWithDefaultValues
3066
3067 FillWithDefaultValues( $marc_record, $params );
3068
3069 This will update the record with default value defined in the ACQ framework.
3070 For all existing fields, if a default value exists and there are no subfield, it will be created.
3071 If the field does not exist, it will be created too.
3072
3073 If the parameter only_mandatory => 1 is passed via $params, only the mandatory
3074 defaults are being applied to the record.
3075
3076 =cut
3077
3078 sub FillWithDefaultValues {
3079     my ( $record, $params ) = @_;
3080     my $mandatory = $params->{only_mandatory};
3081     my $tagslib = C4::Biblio::GetMarcStructure( 1, 'ACQ', { unsafe => 1 } );
3082     if ($tagslib) {
3083         my ($itemfield) =
3084           C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
3085         for my $tag ( sort keys %$tagslib ) {
3086             next unless $tag;
3087             next if $tag == $itemfield;
3088             for my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
3089                 next if IsMarcStructureInternal($tagslib->{$tag}{$subfield});
3090                 next if $mandatory && !$tagslib->{$tag}{$subfield}{mandatory};
3091                 my $defaultvalue = $tagslib->{$tag}{$subfield}{defaultvalue};
3092                 if ( defined $defaultvalue and $defaultvalue ne '' ) {
3093                     my @fields = $record->field($tag);
3094                     if (@fields) {
3095                         for my $field (@fields) {
3096                             if ( $field->is_control_field ) {
3097                                 $field->update($defaultvalue) if not defined $field->data;
3098                             }
3099                             elsif ( not defined $field->subfield($subfield) ) {
3100                                 $field->add_subfields(
3101                                     $subfield => $defaultvalue );
3102                             }
3103                         }
3104                     }
3105                     else {
3106                         if ( $tag < 10 ) { # is_control_field
3107                             $record->insert_fields_ordered(
3108                                 MARC::Field->new(
3109                                     $tag, $defaultvalue
3110                                 )
3111                             );
3112                         }
3113                         else {
3114                             $record->insert_fields_ordered(
3115                                 MARC::Field->new(
3116                                     $tag, '', '', $subfield => $defaultvalue
3117                                 )
3118                             );
3119                         }
3120                     }
3121                 }
3122             }
3123         }
3124     }
3125 }
3126
3127 1;
3128 __END__
3129
3130 =head1 AUTHOR
3131
3132 Koha Development Team <http://koha-community.org/>
3133
3134 =cut