Bug 18736: (follow-up) Fix missing rounding and bad formatting
[koha.git] / C4 / Budgets.pm
1 package C4::Budgets;
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 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Context;
23 use Koha::Database;
24 use Koha::Patrons;
25 use Koha::Acquisition::Invoice::Adjustments;
26 use C4::Debug;
27 use vars qw(@ISA @EXPORT);
28
29 BEGIN {
30         require Exporter;
31         @ISA    = qw(Exporter);
32         @EXPORT = qw(
33
34         &GetBudget
35         &GetBudgetByOrderNumber
36         &GetBudgetByCode
37         &GetBudgets
38         &BudgetsByActivity
39         &GetBudgetsReport
40         &GetBudgetReport
41         &GetBudgetHierarchy
42             &AddBudget
43         &ModBudget
44         &DelBudget
45         &GetBudgetSpent
46         &GetBudgetOrdered
47         &GetBudgetName
48         &GetPeriodsCount
49         GetBudgetHierarchySpent
50         GetBudgetHierarchyOrdered
51
52         &GetBudgetUsers
53         &ModBudgetUsers
54         &CanUserUseBudget
55         &CanUserModifyBudget
56
57             &GetBudgetPeriod
58         &GetBudgetPeriods
59         &ModBudgetPeriod
60         &AddBudgetPeriod
61             &DelBudgetPeriod
62
63         &ModBudgetPlan
64
65                 &GetBudgetsPlanCell
66         &AddBudgetPlanValue
67         &GetBudgetAuthCats
68         &BudgetHasChildren
69         &CheckBudgetParent
70         &CheckBudgetParentPerm
71
72         &HideCols
73         &GetCols
74         );
75 }
76
77 # ----------------------------BUDGETS.PM-----------------------------";
78
79 =head1 FUNCTIONS ABOUT BUDGETS
80
81 =cut
82
83 sub HideCols {
84     my ( $authcat, @hide_cols ) = @_;
85     my $dbh = C4::Context->dbh;
86
87     my $sth1 = $dbh->prepare(
88         qq|
89         UPDATE aqbudgets_planning SET display = 0 
90         WHERE authcat = ? 
91         AND  authvalue = ? |
92     );
93     foreach my $authvalue (@hide_cols) {
94 #        $sth1->{TraceLevel} = 3;
95         $sth1->execute(  $authcat, $authvalue );
96     }
97 }
98
99 sub GetCols {
100     my ( $authcat, $authvalue ) = @_;
101
102     my $dbh = C4::Context->dbh;
103     my $sth = $dbh->prepare(
104         qq|
105         SELECT count(display) as cnt from aqbudgets_planning
106         WHERE  authcat = ? 
107         AND authvalue = ? and display  = 0   |
108     );
109
110 #    $sth->{TraceLevel} = 3;
111     $sth->execute( $authcat, $authvalue );
112     my $res  = $sth->fetchrow_hashref;
113
114     return  $res->{cnt} > 0 ? 0: 1
115
116 }
117
118 sub CheckBudgetParentPerm {
119     my ( $budget, $borrower_id ) = @_;
120     my $depth = $budget->{depth};
121     my $parent_id = $budget->{budget_parent_id};
122     while ($depth) {
123         my $parent = GetBudget($parent_id);
124         $parent_id = $parent->{budget_parent_id};
125         if ( $parent->{budget_owner_id} == $borrower_id ) {
126             return 1;
127         }
128         $depth--
129     }
130     return 0;
131 }
132
133 sub AddBudgetPeriod {
134     my ($budgetperiod) = @_;
135     return unless($budgetperiod->{budget_period_startdate} && $budgetperiod->{budget_period_enddate});
136
137     undef $budgetperiod->{budget_period_id};
138     my $resultset = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
139     return $resultset->create($budgetperiod)->id;
140 }
141 # -------------------------------------------------------------------
142 sub GetPeriodsCount {
143     my $dbh = C4::Context->dbh;
144     my $sth = $dbh->prepare("
145         SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
146     $sth->execute();
147     my $res = $sth->fetchrow_hashref;
148     return $res->{'sum'};
149 }
150
151 # -------------------------------------------------------------------
152 sub CheckBudgetParent {
153     my ( $new_parent, $budget ) = @_;
154     my $new_parent_id = $new_parent->{'budget_id'};
155     my $budget_id     = $budget->{'budget_id'};
156     my $dbh           = C4::Context->dbh;
157     my $parent_id_tmp = $new_parent_id;
158
159     # check new-parent is not a child (or a child's child ;)
160     my $sth = $dbh->prepare(qq|
161         SELECT budget_parent_id FROM
162             aqbudgets where budget_id = ? | );
163     while (1) {
164         $sth->execute($parent_id_tmp);
165         my $res = $sth->fetchrow_hashref;
166         if ( $res->{'budget_parent_id'} == $budget_id ) {
167             return 1;
168         }
169         if ( not defined $res->{'budget_parent_id'} ) {
170             return 0;
171         }
172         $parent_id_tmp = $res->{'budget_parent_id'};
173     }
174 }
175
176 # -------------------------------------------------------------------
177 sub BudgetHasChildren {
178     my ( $budget_id  ) = @_;
179     my $dbh = C4::Context->dbh;
180     my $sth = $dbh->prepare(qq|
181        SELECT count(*) as sum FROM  aqbudgets
182         WHERE budget_parent_id = ?   | );
183     $sth->execute( $budget_id );
184     my $sum = $sth->fetchrow_hashref;
185     return $sum->{'sum'};
186 }
187
188 sub GetBudgetChildren {
189     my ( $budget_id ) = @_;
190     my $dbh = C4::Context->dbh;
191     return $dbh->selectall_arrayref(q|
192        SELECT  * FROM  aqbudgets
193         WHERE budget_parent_id = ?
194     |, { Slice => {} }, $budget_id );
195 }
196
197 sub SetOwnerToFundHierarchy {
198     my ( $budget_id, $borrowernumber ) = @_;
199
200     my $budget = GetBudget( $budget_id );
201     $budget->{budget_owner_id} = $borrowernumber;
202     ModBudget( $budget );
203     my $children = GetBudgetChildren( $budget_id );
204     for my $child ( @$children ) {
205         SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber );
206     }
207 }
208
209 # -------------------------------------------------------------------
210 sub GetBudgetsPlanCell {
211     my ( $cell, $period, $budget ) = @_; #FIXME we don't use $period
212     my ($actual, $sth);
213     my $dbh = C4::Context->dbh;
214     my $roundsql = _get_rounding_sql(qq|ecost_tax_included|);
215     if ( $cell->{'authcat'} eq 'MONTHS' ) {
216         # get the actual amount
217         # FIXME we should consider quantity
218         $sth = $dbh->prepare( qq|
219
220             SELECT SUM(| .  $roundsql . qq|) AS actual FROM aqorders
221                 WHERE    budget_id = ? AND
222                 entrydate like "$cell->{'authvalue'}%"  |
223         );
224         $sth->execute( $cell->{'budget_id'} );
225     } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
226         # get the actual amount
227         # FIXME we should consider quantity
228         $sth = $dbh->prepare( qq|
229
230             SELECT SUM(| . $roundsql . qq|) FROM aqorders
231                 LEFT JOIN aqorders_items
232                 ON (aqorders.ordernumber = aqorders_items.ordernumber)
233                 LEFT JOIN items
234                 ON (aqorders_items.itemnumber = items.itemnumber)
235                 WHERE budget_id = ? AND homebranch = ? |          );
236
237         $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
238     } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
239         # get the actual amount
240         $sth = $dbh->prepare(  qq|
241
242             SELECT SUM( | . $roundsql . qq| *  quantity) AS actual
243                 FROM aqorders JOIN biblioitems
244                 ON (biblioitems.biblionumber = aqorders.biblionumber )
245                 WHERE aqorders.budget_id = ? and itemtype  = ? |
246         );
247         $sth->execute(  $cell->{'budget_id'},
248                         $cell->{'authvalue'} );
249     }
250     # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
251     else {
252         # get the actual amount
253         $sth = $dbh->prepare( qq|
254
255         SELECT  SUM(| . $roundsql . qq| * quantity) AS actual
256             FROM aqorders
257             JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
258             WHERE  aqorders.budget_id = ? AND
259                 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
260                 (aqbudgets.sort2_authcat = ? AND sort2 =?))    |
261         );
262         $sth->execute(  $cell->{'budget_id'},
263                         $budget->{'sort1_authcat'},
264                         $cell->{'authvalue'},
265                         $budget->{'sort2_authcat'},
266                         $cell->{'authvalue'}
267         );
268     }
269     $actual = $sth->fetchrow_array;
270
271     # get the estimated amount
272     $sth = $dbh->prepare( qq|
273
274         SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
275             WHERE budget_period_id = ? AND
276                 budget_id = ? AND
277                 authvalue = ? AND
278                 authcat = ?         |
279     );
280     $sth->execute(  $cell->{'budget_period_id'},
281                     $cell->{'budget_id'},
282                     $cell->{'authvalue'},
283                     $cell->{'authcat'},
284     );
285
286
287     my $res  = $sth->fetchrow_hashref;
288   #  my $display = $res->{'display'};
289     my $estimated = $res->{'estimated'};
290
291
292     return $actual, $estimated;
293 }
294
295 # -------------------------------------------------------------------
296 sub ModBudgetPlan {
297     my ( $budget_plan, $budget_period_id, $authcat ) = @_;
298     my $dbh = C4::Context->dbh;
299     foreach my $buds (@$budget_plan) {
300         my $lines = $buds->{lines};
301         my $sth = $dbh->prepare( qq|
302                 DELETE FROM aqbudgets_planning
303                     WHERE   budget_period_id   = ? AND
304                             budget_id   = ? AND
305                             authcat            = ? |
306         );
307     #delete a aqplan line of cells, then insert new cells, 
308     # these could be UPDATES rather than DEL/INSERTS...
309         $sth->execute( $budget_period_id,  $lines->[0]{budget_id}   , $authcat );
310
311         foreach my $cell (@$lines) {
312             my $sth = $dbh->prepare( qq|
313
314                 INSERT INTO aqbudgets_planning
315                      SET   budget_id     = ?,
316                      budget_period_id  = ?,
317                      authcat          = ?,
318                      estimated_amount  = ?,
319                      authvalue       = ?  |
320             );
321             $sth->execute(
322                             $cell->{'budget_id'},
323                             $cell->{'budget_period_id'},
324                             $cell->{'authcat'},
325                             $cell->{'estimated_amount'},
326                             $cell->{'authvalue'},
327             );
328         }
329     }
330 }
331
332 # -------------------------------------------------------------------
333 sub GetBudgetSpent {
334     my ($budget_id) = @_;
335     my $dbh = C4::Context->dbh;
336     # unitprice_tax_included should always been set here
337     # we should not need to retrieve ecost_tax_included
338     my $sth = $dbh->prepare(qq|
339         SELECT SUM( | . _get_rounding_sql("COALESCE(unitprice_tax_included, ecost_tax_included)") . qq| * quantity ) AS sum FROM aqorders
340             WHERE budget_id = ? AND
341             quantityreceived > 0 AND
342             datecancellationprinted IS NULL
343     |);
344         $sth->execute($budget_id);
345     my $sum = 0 + $sth->fetchrow_array;
346
347     $sth = $dbh->prepare(qq|
348         SELECT SUM(shipmentcost) AS sum
349         FROM aqinvoices
350         WHERE shipmentcost_budgetid = ?
351     |);
352
353     $sth->execute($budget_id);
354     my ($shipmentcost_sum) = $sth->fetchrow_array;
355     $sum += $shipmentcost_sum;
356
357     my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $budget_id, closedate => { '!=' => undef } },{ join => 'invoiceid' });
358     while ( my $adj = $adjustments->next ){
359         $sum += $adj->adjustment;
360     }
361
362         return $sum;
363 }
364
365 # -------------------------------------------------------------------
366 sub GetBudgetOrdered {
367         my ($budget_id) = @_;
368         my $dbh = C4::Context->dbh;
369         my $sth = $dbh->prepare(qq|
370         SELECT SUM(| . _get_rounding_sql(qq|ecost_tax_included|) . qq| *  quantity) AS sum FROM aqorders
371             WHERE budget_id = ? AND
372             quantityreceived = 0 AND
373             datecancellationprinted IS NULL
374     |);
375         $sth->execute($budget_id);
376     my $sum =  0 + $sth->fetchrow_array;
377
378     my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $budget_id, encumber_open => 1, closedate => undef},{ join => 'invoiceid' });
379     while ( my $adj = $adjustments->next ){
380         $sum += $adj->adjustment;
381     }
382
383         return $sum;
384 }
385
386 =head2 GetBudgetName
387
388   my $budget_name = &GetBudgetName($budget_id);
389
390 get the budget_name for a given budget_id
391
392 =cut
393
394 sub GetBudgetName {
395     my ( $budget_id ) = @_;
396     my $dbh         = C4::Context->dbh;
397     my $sth         = $dbh->prepare(
398         qq|
399         SELECT budget_name
400         FROM aqbudgets
401         WHERE budget_id = ?
402     |);
403
404     $sth->execute($budget_id);
405     return $sth->fetchrow_array;
406 }
407
408 =head2 GetBudgetAuthCats
409
410   my $auth_cats = &GetBudgetAuthCats($budget_period_id);
411
412 Return the list of authcat for a given budget_period_id
413
414 =cut
415
416 sub GetBudgetAuthCats  {
417     my ($budget_period_id) = shift;
418     # now, populate the auth_cats_loop used in the budget planning button
419     # we must retrieve all auth values used by at least one budget
420     my $dbh = C4::Context->dbh;
421     my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
422     $sth->execute($budget_period_id);
423     my %authcats;
424     while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
425         $authcats{$sort1_authcat}=1 if $sort1_authcat;
426         $authcats{$sort2_authcat}=1 if $sort2_authcat;
427     }
428     return [ sort keys %authcats ];
429 }
430
431 # -------------------------------------------------------------------
432 sub GetBudgetPeriods {
433         my ($filters,$orderby) = @_;
434
435     my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
436     $rs = $rs->search( $filters, { order_by => $orderby } );
437     $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
438     return [ $rs->all ];
439 }
440 # -------------------------------------------------------------------
441 sub GetBudgetPeriod {
442         my ($budget_period_id) = @_;
443         my $dbh = C4::Context->dbh;
444         ## $total = number of records linked to the record that must be deleted
445         my $total = 0;
446         ## get information about the record that will be deleted
447         my $sth;
448         if ($budget_period_id) {
449                 $sth = $dbh->prepare( qq|
450               SELECT      *
451                 FROM aqbudgetperiods
452                 WHERE budget_period_id=? |
453                 );
454                 $sth->execute($budget_period_id);
455         } else {         # ACTIVE BUDGET
456                 $sth = $dbh->prepare(qq|
457                           SELECT      *
458                 FROM aqbudgetperiods
459                 WHERE budget_period_active=1 |
460                 );
461                 $sth->execute();
462         }
463         my $data = $sth->fetchrow_hashref;
464         return $data;
465 }
466
467 sub DelBudgetPeriod{
468         my ($budget_period_id) = @_;
469         my $dbh = C4::Context->dbh;
470           ; ## $total = number of records linked to the record that must be deleted
471     my $total = 0;
472
473         ## get information about the record that will be deleted
474         my $sth = $dbh->prepare(qq|
475                 DELETE 
476          FROM aqbudgetperiods
477          WHERE budget_period_id=? |
478         );
479         return $sth->execute($budget_period_id);
480 }
481
482 # -------------------------------------------------------------------
483 sub ModBudgetPeriod {
484     my ($budget_period) = @_;
485     my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
486     return unless($result);
487
488     $result = $result->update($budget_period);
489     return $result->in_storage;
490 }
491
492 # -------------------------------------------------------------------
493 sub GetBudgetHierarchy {
494     my ( $budget_period_id, $branchcode, $owner ) = @_;
495     my @bind_params;
496     my $dbh   = C4::Context->dbh;
497     my $query = qq|
498                     SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description,
499                            b.firstname as budget_owner_firstname, b.surname as budget_owner_surname, b.borrowernumber as budget_owner_borrowernumber
500                     FROM aqbudgets 
501                     LEFT JOIN borrowers b on b.borrowernumber = aqbudgets.budget_owner_id
502                     JOIN aqbudgetperiods USING (budget_period_id)|;
503
504         my @where_strings;
505     # show only period X if requested
506     if ($budget_period_id) {
507         push @where_strings," aqbudgets.budget_period_id = ?";
508         push @bind_params, $budget_period_id;
509     }
510         # show only budgets owned by me, my branch or everyone
511     if ($owner) {
512         if ($branchcode) {
513             push @where_strings,
514             qq{ (budget_owner_id = ? OR budget_branchcode = ? OR ((budget_branchcode IS NULL or budget_branchcode="") AND (budget_owner_id IS NULL OR budget_owner_id="")))};
515             push @bind_params, ( $owner, $branchcode );
516         } else {
517             push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
518             push @bind_params, $owner;
519         }
520     } else {
521         if ($branchcode) {
522             push @where_strings," (budget_branchcode =? or budget_branchcode is NULL OR budget_branchcode='')";
523             push @bind_params, $branchcode;
524         }
525     }
526         $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
527         $debug && warn $query,join(",",@bind_params);
528         my $sth = $dbh->prepare($query);
529         $sth->execute(@bind_params);
530
531     my %links;
532     # create hash with budget_id has key
533     while ( my $data = $sth->fetchrow_hashref ) {
534         $links{ $data->{'budget_id'} } = $data;
535     }
536
537     # link child to parent
538     my @first_parents;
539     foreach my $budget ( sort { $a->{budget_code} cmp $b->{budget_code} } values %links ) {
540         my $child = $links{$budget->{budget_id}};
541         if ( $child->{'budget_parent_id'} ) {
542             my $parent = $links{ $child->{'budget_parent_id'} };
543             if ($parent) {
544                 unless ( $parent->{'children'} ) {
545                     # init child arrayref
546                     $parent->{'children'} = [];
547                 }
548                 # add as child
549                 push @{ $parent->{'children'} }, $child;
550             }
551         } else {
552             push @first_parents, $child;
553         }
554     }
555
556     my @sort = ();
557     foreach my $first_parent (@first_parents) {
558         _add_budget_children(\@sort, $first_parent, 0);
559     }
560
561     # Get all the budgets totals in as few queries as possible
562     my $hr_budget_spent = $dbh->selectall_hashref(q|
563         SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
564                SUM( | . _get_rounding_sql(qq|COALESCE(unitprice_tax_included, ecost_tax_included)|) . q| * quantity ) AS budget_spent
565         FROM aqorders JOIN aqbudgets USING (budget_id)
566         WHERE quantityreceived > 0 AND datecancellationprinted IS NULL
567         GROUP BY budget_id, budget_parent_id
568         |, 'budget_id');
569     my $hr_budget_ordered = $dbh->selectall_hashref(q|
570         SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
571                SUM( | . _get_rounding_sql(qq|ecost_tax_included|) . q| *  quantity) AS budget_ordered
572         FROM aqorders JOIN aqbudgets USING (budget_id)
573         WHERE quantityreceived = 0 AND datecancellationprinted IS NULL
574         GROUP BY budget_id, budget_parent_id
575         |, 'budget_id');
576     my $hr_budget_spent_shipment = $dbh->selectall_hashref(q|
577         SELECT shipmentcost_budgetid as budget_id,
578                SUM(shipmentcost) as shipmentcost
579         FROM aqinvoices
580         WHERE closedate IS NOT NULL
581         GROUP BY shipmentcost_budgetid
582         |, 'budget_id');
583     my $hr_budget_ordered_shipment = $dbh->selectall_hashref(q|
584         SELECT shipmentcost_budgetid as budget_id,
585                SUM(shipmentcost) as shipmentcost
586         FROM aqinvoices
587         WHERE closedate IS NULL
588         GROUP BY shipmentcost_budgetid
589         |, 'budget_id');
590     my $hr_budget_spent_adjustment = $dbh->selectall_hashref(q|
591         SELECT budget_id,
592                SUM(adjustment) as adjustments
593         FROM aqinvoice_adjustments
594         JOIN aqinvoices USING (invoiceid)
595         WHERE closedate IS NOT NULL
596         GROUP BY budget_id
597         |, 'budget_id');
598     my $hr_budget_ordered_adjustment = $dbh->selectall_hashref(q|
599         SELECT budget_id,
600                SUM(adjustment) as adjustments
601         FROM aqinvoice_adjustments
602         JOIN aqinvoices USING (invoiceid)
603         WHERE closedate IS NULL AND encumber_open = 1
604         GROUP BY budget_id
605         |, 'budget_id');
606
607
608     foreach my $budget (@sort) {
609         if ( not defined $budget->{budget_parent_id} ) {
610             _recursiveAdd( $budget, undef, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
611         }
612     }
613     return \@sort;
614 }
615
616 sub _recursiveAdd {
617     my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment ) = @_;
618
619     foreach my $child (@{$budget->{children}}){
620         _recursiveAdd($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_ordered_shipment, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
621     }
622
623     $budget->{budget_spent} += $hr_budget_spent->{$budget->{budget_id}}->{budget_spent};
624     $budget->{budget_spent} += $hr_budget_spent_shipment->{$budget->{budget_id}}->{shipmentcost};
625     $budget->{budget_spent} += $hr_budget_spent_adjustment->{$budget->{budget_id}}->{adjustments};
626     $budget->{budget_ordered} += $hr_budget_ordered->{$budget->{budget_id}}->{budget_ordered};
627     $budget->{budget_ordered} += $hr_budget_ordered_shipment->{$budget->{budget_id}}->{shipmentcost};
628     $budget->{budget_ordered} += $hr_budget_ordered_adjustment->{$budget->{budget_id}}->{adjustments};
629
630     $budget->{total_spent} += $budget->{budget_spent};
631     $budget->{total_ordered} += $budget->{budget_ordered};
632
633     if ($parent) {
634         $parent->{total_spent} += $budget->{total_spent};
635         $parent->{total_ordered} += $budget->{total_ordered};
636     }
637 }
638
639 # Recursive method to add a budget and its chidren to an array
640 sub _add_budget_children {
641     my $res = shift;
642     my $budget = shift;
643     $budget->{budget_level} = shift;
644     push @$res, $budget;
645     my $children = $budget->{'children'} || [];
646     return unless @$children; # break recursivity
647     foreach my $child (@$children) {
648         _add_budget_children($res, $child, $budget->{budget_level} + 1);
649     }
650 }
651
652 # -------------------------------------------------------------------
653
654 sub AddBudget {
655     my ($budget) = @_;
656     return unless ($budget);
657
658     undef $budget->{budget_encumb} if $budget->{budget_encumb} eq '';
659     undef $budget->{budget_owner_id} if $budget->{budget_owner_id} eq '';
660     my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
661     return $resultset->create($budget)->id;
662 }
663
664 # -------------------------------------------------------------------
665 sub ModBudget {
666     my ($budget) = @_;
667     my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
668     return unless($result);
669
670     undef $budget->{budget_encumb} if $budget->{budget_encumb} eq '';
671     undef $budget->{budget_owner_id} if $budget->{budget_owner_id} eq '';
672     $result = $result->update($budget);
673     return $result->in_storage;
674 }
675
676 # -------------------------------------------------------------------
677 sub DelBudget {
678         my ($budget_id) = @_;
679         my $dbh         = C4::Context->dbh;
680         my $sth         = $dbh->prepare("delete from aqbudgets where budget_id=?");
681         my $rc          = $sth->execute($budget_id);
682         return $rc;
683 }
684
685
686 # -------------------------------------------------------------------
687
688 =head2 GetBudget
689
690   &GetBudget($budget_id);
691
692 get a specific budget
693
694 =cut
695
696 sub GetBudget {
697     my ( $budget_id ) = @_;
698     my $dbh = C4::Context->dbh;
699     my $query = "
700         SELECT *
701         FROM   aqbudgets
702         WHERE  budget_id=?
703         ";
704     my $sth = $dbh->prepare($query);
705     $sth->execute( $budget_id );
706     my $result = $sth->fetchrow_hashref;
707     return $result;
708 }
709
710 # -------------------------------------------------------------------
711
712 =head2 GetBudgetByOrderNumber
713
714   &GetBudgetByOrderNumber($ordernumber);
715
716 get a specific budget by order number
717
718 =cut
719
720 sub GetBudgetByOrderNumber {
721     my ( $ordernumber ) = @_;
722     my $dbh = C4::Context->dbh;
723     my $query = "
724         SELECT aqbudgets.*
725         FROM   aqbudgets, aqorders
726         WHERE  ordernumber=?
727         AND    aqorders.budget_id = aqbudgets.budget_id
728         ";
729     my $sth = $dbh->prepare($query);
730     $sth->execute( $ordernumber );
731     my $result = $sth->fetchrow_hashref;
732     return $result;
733 }
734
735 =head2 GetBudgetReport
736
737   &GetBudgetReport( [$budget_id] );
738
739 Get all orders for a specific budget, without cancelled orders.
740
741 Returns an array of hashrefs.
742
743 =cut
744
745 # --------------------------------------------------------------------
746 sub GetBudgetReport {
747     my ( $budget_id ) = @_;
748     my $dbh = C4::Context->dbh;
749     my $query = '
750         SELECT o.*, b.budget_name
751         FROM   aqbudgets b
752         INNER JOIN aqorders o
753         ON b.budget_id = o.budget_id
754         WHERE  b.budget_id=?
755         AND (o.orderstatus != "cancelled")
756         ORDER BY b.budget_name';
757
758     my $sth = $dbh->prepare($query);
759     $sth->execute( $budget_id );
760
761     my @results = ();
762     while ( my $data = $sth->fetchrow_hashref ) {
763         push( @results, $data );
764     }
765     return @results;
766 }
767
768 =head2 GetBudgetsByActivity
769
770   &GetBudgetsByActivity( $budget_period_active );
771
772 Get all active or inactive budgets, depending of the value
773 of the parameter.
774
775 1 = active
776 0 = inactive
777
778 =cut
779
780 # --------------------------------------------------------------------
781 sub GetBudgetsByActivity {
782     my ( $budget_period_active ) = @_;
783     my $dbh = C4::Context->dbh;
784     my $query = "
785         SELECT DISTINCT b.*
786         FROM   aqbudgetperiods bp
787         INNER JOIN aqbudgets b
788         ON bp.budget_period_id = b.budget_period_id
789         WHERE  bp.budget_period_active=?
790         ";
791     my $sth = $dbh->prepare($query);
792     $sth->execute( $budget_period_active );
793     my @results = ();
794     while ( my $data = $sth->fetchrow_hashref ) {
795         push( @results, $data );
796     }
797     return @results;
798 }
799 # --------------------------------------------------------------------
800
801 =head2 GetBudgetsReport
802
803   &GetBudgetsReport( [$activity] );
804
805 Get all but cancelled orders for all funds.
806
807 If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
808
809 active = 1
810 inactive = 0
811
812 Returns an array of hashrefs.
813
814 =cut
815
816 sub GetBudgetsReport {
817     my ($activity) = @_;
818     my $dbh = C4::Context->dbh;
819     my $query = '
820         SELECT o.*, b.budget_name
821         FROM   aqbudgetperiods bp
822         INNER JOIN aqbudgets b
823         ON bp.budget_period_id = b.budget_period_id
824         INNER JOIN aqorders o
825         ON b.budget_id = o.budget_id ';
826     if($activity ne ''){
827         $query .= 'WHERE  bp.budget_period_active=? ';
828     }
829     $query .= 'AND (o.orderstatus != "cancelled")
830                ORDER BY b.budget_name';
831
832     my $sth = $dbh->prepare($query);
833     if($activity ne ''){
834         $sth->execute($activity);
835     }
836     else{
837         $sth->execute;
838     }
839     my @results = ();
840     while ( my $data = $sth->fetchrow_hashref ) {
841         push( @results, $data );
842     }
843     return @results;
844 }
845
846 =head2 GetBudgetByCode
847
848     my $budget = &GetBudgetByCode($budget_code);
849
850 Retrieve all aqbudgets fields as a hashref for the budget that has
851 given budget_code
852
853 =cut
854
855 sub GetBudgetByCode {
856     my ( $budget_code ) = @_;
857
858     my $dbh = C4::Context->dbh;
859     my $query = qq{
860         SELECT aqbudgets.*
861         FROM aqbudgets
862         JOIN aqbudgetperiods USING (budget_period_id)
863         WHERE budget_code = ?
864         ORDER BY budget_period_active DESC, budget_id DESC
865         LIMIT 1
866     };
867     my $sth = $dbh->prepare( $query );
868     $sth->execute( $budget_code );
869     return $sth->fetchrow_hashref;
870 }
871
872 =head2 GetBudgetHierarchySpent
873
874   my $spent = GetBudgetHierarchySpent( $budget_id );
875
876 Gets the total spent of the level and sublevels of $budget_id
877
878 =cut
879
880 sub GetBudgetHierarchySpent {
881     my ( $budget_id ) = @_;
882     my $dbh = C4::Context->dbh;
883     my $children_ids = $dbh->selectcol_arrayref(q|
884         SELECT budget_id
885         FROM   aqbudgets
886         WHERE  budget_parent_id = ?
887     |, {}, $budget_id );
888
889     my $total_spent = GetBudgetSpent( $budget_id );
890     for my $child_id ( @$children_ids ) {
891         $total_spent += GetBudgetHierarchySpent( $child_id );
892     }
893     return $total_spent;
894 }
895
896 =head2 GetBudgetHierarchyOrdered
897
898   my $ordered = GetBudgetHierarchyOrdered( $budget_id );
899
900 Gets the total ordered of the level and sublevels of $budget_id
901
902 =cut
903
904 sub GetBudgetHierarchyOrdered {
905     my ( $budget_id ) = @_;
906     my $dbh = C4::Context->dbh;
907     my $children_ids = $dbh->selectcol_arrayref(q|
908         SELECT budget_id
909         FROM   aqbudgets
910         WHERE  budget_parent_id = ?
911     |, {}, $budget_id );
912
913     my $total_ordered = GetBudgetOrdered( $budget_id );
914     for my $child_id ( @$children_ids ) {
915         $total_ordered += GetBudgetHierarchyOrdered( $child_id );
916     }
917     return $total_ordered;
918 }
919
920 =head2 GetBudgets
921
922   &GetBudgets($filter, $order_by);
923
924 gets all budgets
925
926 =cut
927
928 # -------------------------------------------------------------------
929 sub GetBudgets {
930     my ($filters, $orderby) = @_;
931     $orderby = 'budget_name' unless($orderby);
932
933     my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
934     $rs = $rs->search( $filters, { order_by => $orderby } );
935     $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
936     return [ $rs->all  ];
937 }
938
939 =head2 GetBudgetUsers
940
941     my @borrowernumbers = &GetBudgetUsers($budget_id);
942
943 Return the list of borrowernumbers linked to a budget
944
945 =cut
946
947 sub GetBudgetUsers {
948     my ($budget_id) = @_;
949
950     my $dbh = C4::Context->dbh;
951     my $query = qq{
952         SELECT borrowernumber
953         FROM aqbudgetborrowers
954         WHERE budget_id = ?
955     };
956     my $sth = $dbh->prepare($query);
957     $sth->execute($budget_id);
958
959     my @borrowernumbers;
960     while (my ($borrowernumber) = $sth->fetchrow_array) {
961         push @borrowernumbers, $borrowernumber
962     }
963
964     return @borrowernumbers;
965 }
966
967 =head2 ModBudgetUsers
968
969     &ModBudgetUsers($budget_id, @borrowernumbers);
970
971 Modify the list of borrowernumbers linked to a budget
972
973 =cut
974
975 sub ModBudgetUsers {
976     my ($budget_id, @budget_users_id) = @_;
977
978     return unless $budget_id;
979
980     my $dbh = C4::Context->dbh;
981     my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
982     my $sth = $dbh->prepare($query);
983     $sth->execute($budget_id);
984
985     $query = qq{
986         INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
987         VALUES (?,?)
988     };
989     $sth = $dbh->prepare($query);
990     foreach my $borrowernumber (@budget_users_id) {
991         next unless $borrowernumber;
992         $sth->execute($budget_id, $borrowernumber);
993     }
994 }
995
996 sub CanUserUseBudget {
997     my ($borrower, $budget, $userflags) = @_;
998
999     if (not ref $borrower) {
1000         $borrower = Koha::Patrons->find( $borrower );
1001         return 0 unless $borrower;
1002         $borrower = $borrower->unblessed;
1003     }
1004     if (not ref $budget) {
1005         $budget = GetBudget($budget);
1006     }
1007
1008     return 0 unless ($borrower and $budget);
1009
1010     if (not defined $userflags) {
1011         $userflags = C4::Auth::getuserflags($borrower->{flags},
1012             $borrower->{userid});
1013     }
1014
1015     unless ($userflags->{superlibrarian}
1016     || (ref $userflags->{acquisition}
1017         && $userflags->{acquisition}->{budget_manage_all})
1018     || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
1019     {
1020         if (not exists $userflags->{acquisition}) {
1021             return 0;
1022         }
1023
1024         if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
1025             return 0;
1026         }
1027
1028         # Budget restricted to owner
1029         if ( $budget->{budget_permission} == 1 ) {
1030             if (    $budget->{budget_owner_id}
1031                 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
1032             {
1033                 return 0;
1034             }
1035         }
1036
1037         # Budget restricted to owner, users and library
1038         elsif ( $budget->{budget_permission} == 2 ) {
1039             my @budget_users = GetBudgetUsers( $budget->{budget_id} );
1040
1041             if (
1042                 (
1043                         $budget->{budget_owner_id}
1044                     and $budget->{budget_owner_id} !=
1045                     $borrower->{borrowernumber}
1046                     or not $budget->{budget_owner_id}
1047                 )
1048                 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
1049                     @budget_users )
1050                 and defined $budget->{budget_branchcode}
1051                 and $budget->{budget_branchcode} ne
1052                 C4::Context->userenv->{branch}
1053               )
1054             {
1055                 return 0;
1056             }
1057         }
1058
1059         # Budget restricted to owner and users
1060         elsif ( $budget->{budget_permission} == 3 ) {
1061             my @budget_users = GetBudgetUsers( $budget->{budget_id} );
1062             if (
1063                 (
1064                         $budget->{budget_owner_id}
1065                     and $budget->{budget_owner_id} !=
1066                     $borrower->{borrowernumber}
1067                     or not $budget->{budget_owner_id}
1068                 )
1069                 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
1070                     @budget_users )
1071               )
1072             {
1073                 return 0;
1074             }
1075         }
1076     }
1077
1078     return 1;
1079 }
1080
1081 sub CanUserModifyBudget {
1082     my ($borrower, $budget, $userflags) = @_;
1083
1084     if (not ref $borrower) {
1085         $borrower = Koha::Patrons->find( $borrower );
1086         return 0 unless $borrower;
1087         $borrower = $borrower->unblessed;
1088     }
1089     if (not ref $budget) {
1090         $budget = GetBudget($budget);
1091     }
1092
1093     return 0 unless ($borrower and $budget);
1094
1095     if (not defined $userflags) {
1096         $userflags = C4::Auth::getuserflags($borrower->{flags},
1097             $borrower->{userid});
1098     }
1099
1100     unless ($userflags->{superlibrarian}
1101     || (ref $userflags->{acquisition}
1102         && $userflags->{acquisition}->{budget_manage_all})
1103     || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
1104     {
1105         if (!CanUserUseBudget($borrower, $budget, $userflags)) {
1106             return 0;
1107         }
1108
1109         if (ref $userflags->{acquisition}
1110         && !$userflags->{acquisition}->{budget_modify}) {
1111             return 0;
1112         }
1113     }
1114
1115     return 1;
1116 }
1117
1118 sub _round {
1119     my ($value, $increment) = @_;
1120
1121     if ($increment && $increment != 0) {
1122         $value = int($value / $increment) * $increment;
1123     }
1124
1125     return $value;
1126 }
1127
1128 =head2 CloneBudgetPeriod
1129
1130   my $new_budget_period_id = CloneBudgetPeriod({
1131     budget_period_id => $budget_period_id,
1132     budget_period_startdate => $budget_period_startdate,
1133     budget_period_enddate   => $budget_period_enddate,
1134     mark_original_budget_as_inactive => 1n
1135     reset_all_budgets => 1,
1136   });
1137
1138 Clone a budget period with all budgets.
1139 If the mark_origin_budget_as_inactive is set (0 by default),
1140 the original budget will be marked as inactive.
1141
1142 If the reset_all_budgets is set (0 by default), all budget (fund)
1143 amounts will be reset.
1144
1145 =cut
1146
1147 sub CloneBudgetPeriod {
1148     my ($params)                  = @_;
1149     my $budget_period_id          = $params->{budget_period_id};
1150     my $budget_period_startdate   = $params->{budget_period_startdate};
1151     my $budget_period_enddate     = $params->{budget_period_enddate};
1152     my $budget_period_description = $params->{budget_period_description};
1153     my $amount_change_percentage  = $params->{amount_change_percentage};
1154     my $amount_change_round_increment = $params->{amount_change_round_increment};
1155     my $mark_original_budget_as_inactive =
1156       $params->{mark_original_budget_as_inactive} || 0;
1157     my $reset_all_budgets = $params->{reset_all_budgets} || 0;
1158
1159     my $budget_period = GetBudgetPeriod($budget_period_id);
1160
1161     $budget_period->{budget_period_startdate}   = $budget_period_startdate;
1162     $budget_period->{budget_period_enddate}     = $budget_period_enddate;
1163     $budget_period->{budget_period_description} = $budget_period_description;
1164     # The new budget (budget_period) should be active by default
1165     $budget_period->{budget_period_active}    = 1;
1166
1167     if ($amount_change_percentage) {
1168         my $total = $budget_period->{budget_period_total};
1169         $total += $total * $amount_change_percentage / 100;
1170         $total = _round($total, $amount_change_round_increment);
1171         $budget_period->{budget_period_total} = $total;
1172     }
1173
1174     my $original_budget_period_id = $budget_period->{budget_period_id};
1175     delete $budget_period->{budget_period_id};
1176     my $new_budget_period_id = AddBudgetPeriod( $budget_period );
1177
1178     my $budgets = GetBudgetHierarchy($budget_period_id);
1179     CloneBudgetHierarchy(
1180         {
1181             budgets              => $budgets,
1182             new_budget_period_id => $new_budget_period_id
1183         }
1184     );
1185
1186     if ($mark_original_budget_as_inactive) {
1187         ModBudgetPeriod(
1188             {
1189                 budget_period_id     => $budget_period_id,
1190                 budget_period_active => 0,
1191             }
1192         );
1193     }
1194
1195     if ( $reset_all_budgets ) {
1196         my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1197         for my $budget ( @$budgets ) {
1198             $budget->{budget_amount} = 0;
1199             ModBudget( $budget );
1200         }
1201     } elsif ($amount_change_percentage) {
1202         my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1203         for my $budget ( @$budgets ) {
1204             my $amount = $budget->{budget_amount};
1205             $amount += $amount * $amount_change_percentage / 100;
1206             $amount = _round($amount, $amount_change_round_increment);
1207             $budget->{budget_amount} = $amount;
1208             ModBudget( $budget );
1209         }
1210     }
1211
1212     return $new_budget_period_id;
1213 }
1214
1215 =head2 CloneBudgetHierarchy
1216
1217   CloneBudgetHierarchy({
1218     budgets => $budgets,
1219     new_budget_period_id => $new_budget_period_id;
1220   });
1221
1222 Clone a budget hierarchy.
1223
1224 =cut
1225
1226 sub CloneBudgetHierarchy {
1227     my ($params)             = @_;
1228     my $budgets              = $params->{budgets};
1229     my $new_budget_period_id = $params->{new_budget_period_id};
1230     next unless @$budgets or $new_budget_period_id;
1231
1232     my $children_of   = $params->{children_of};
1233     my $new_parent_id = $params->{new_parent_id};
1234
1235     my @first_level_budgets =
1236       ( not defined $children_of )
1237       ? map { ( not $_->{budget_parent_id} )             ? $_ : () } @$budgets
1238       : map { ( $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1239
1240     # get only the columns of aqbudgets
1241     my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
1242
1243     for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1244         @first_level_budgets )
1245     {
1246
1247         my $tidy_budget =
1248           { map { join( ' ', @columns ) =~ /$_/ ? ( $_ => $budget->{$_} ) : () }
1249               keys %$budget };
1250         delete $tidy_budget->{timestamp};
1251         my $new_budget_id = AddBudget(
1252             {
1253                 %$tidy_budget,
1254                 budget_id        => undef,
1255                 budget_parent_id => $new_parent_id,
1256                 budget_period_id => $new_budget_period_id
1257             }
1258         );
1259         CloneBudgetHierarchy(
1260             {
1261                 budgets              => $budgets,
1262                 new_budget_period_id => $new_budget_period_id,
1263                 children_of          => $budget->{budget_id},
1264                 new_parent_id        => $new_budget_id
1265             }
1266         );
1267     }
1268 }
1269
1270 =head2 MoveOrders
1271
1272   my $report = MoveOrders({
1273     from_budget_period_id => $from_budget_period_id,
1274     to_budget_period_id   => $to_budget_period_id,
1275   });
1276
1277 Move orders from one budget period to another.
1278
1279 =cut
1280
1281 sub MoveOrders {
1282     my ($params)              = @_;
1283     my $from_budget_period_id = $params->{from_budget_period_id};
1284     my $to_budget_period_id   = $params->{to_budget_period_id};
1285     my $move_remaining_unspent = $params->{move_remaining_unspent};
1286     return
1287       if not $from_budget_period_id
1288           or not $to_budget_period_id
1289           or $from_budget_period_id == $to_budget_period_id;
1290
1291     # Can't move orders to an inactive budget (budgetperiod)
1292     my $budget_period = GetBudgetPeriod($to_budget_period_id);
1293     return unless $budget_period->{budget_period_active};
1294
1295     my @report;
1296     my $dbh     = C4::Context->dbh;
1297     my $sth_update_aqorders = $dbh->prepare(
1298         q|
1299             UPDATE aqorders
1300             SET budget_id = ?
1301             WHERE ordernumber = ?
1302         |
1303     );
1304     my $sth_update_budget_amount = $dbh->prepare(
1305         q|
1306             UPDATE aqbudgets
1307             SET budget_amount = ?
1308             WHERE budget_id = ?
1309         |
1310     );
1311     my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1312     for my $from_budget (@$from_budgets) {
1313         my $new_budget_id = $dbh->selectcol_arrayref(
1314             q|
1315                 SELECT budget_id
1316                 FROM aqbudgets
1317                 WHERE budget_period_id = ?
1318                     AND budget_code = ?
1319             |, {}, $to_budget_period_id, $from_budget->{budget_code}
1320         );
1321         $new_budget_id = $new_budget_id->[0];
1322         my $new_budget = GetBudget( $new_budget_id );
1323         unless ( $new_budget ) {
1324             push @report,
1325               {
1326                 moved       => 0,
1327                 budget      => $from_budget,
1328                 error       => 'budget_code_not_exists',
1329               };
1330             next;
1331         }
1332         my $orders_to_move = C4::Acquisition::SearchOrders(
1333             {
1334                 budget_id => $from_budget->{budget_id},
1335                 pending   => 1,
1336             }
1337         );
1338
1339         my @orders_moved;
1340         for my $order (@$orders_to_move) {
1341             $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1342             push @orders_moved, $order;
1343         }
1344
1345         my $unspent_moved = 0;
1346         if ($move_remaining_unspent) {
1347             my $spent   = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1348             my $unspent = $from_budget->{budget_amount} - $spent;
1349             my $new_budget_amount = $new_budget->{budget_amount};
1350             if ( $unspent > 0 ) {
1351                 $new_budget_amount += $unspent;
1352                 $unspent_moved = $unspent;
1353             }
1354             $new_budget->{budget_amount} = $new_budget_amount;
1355             $sth_update_budget_amount->execute( $new_budget_amount,
1356                 $new_budget->{budget_id} );
1357         }
1358
1359         push @report,
1360           {
1361             budget        => $new_budget,
1362             orders_moved  => \@orders_moved,
1363             moved         => 1,
1364             unspent_moved => $unspent_moved,
1365           };
1366     }
1367     return \@report;
1368 }
1369
1370 =head1 INTERNAL FUNCTIONS
1371
1372 =cut
1373
1374 =head3 _get_rounding_sql
1375
1376     $rounding_sql = _get_rounding_sql("mysql_variable_to_round_string");
1377
1378 returns the correct SQL routine based on OrderPriceRounding system preference.
1379
1380 =cut
1381
1382 sub _get_rounding_sql {
1383     my $to_round = shift;
1384     my $rounding_pref = C4::Context->preference('OrderPriceRounding');
1385     if   ($rounding_pref eq 'nearest_cent') { return "CAST($to_round*100 AS INTEGER)/100"; }
1386     else { return "$to_round"; }
1387 }
1388
1389 END { }    # module clean-up code here (global destructor)
1390
1391 1;
1392 __END__
1393
1394 =head1 AUTHOR
1395
1396 Koha Development Team <http://koha-community.org/>
1397
1398 =cut