Bug 12194: fix label for oblique_title
[koha.git] / C4 / Budgets.pm
blobf2f376a34d5eb3de0f4ab07576c9ccc93eab9d3e
1 package C4::Budgets;
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use C4::Context;
23 use Koha::Database;
24 use C4::Debug;
25 use vars qw($VERSION @ISA @EXPORT);
27 BEGIN {
28 # set the version for version checking
29 $VERSION = 3.07.00.049;
30 require Exporter;
31 @ISA = qw(Exporter);
32 @EXPORT = qw(
34 &GetBudget
35 &GetBudgetByOrderNumber
36 &GetBudgetByCode
37 &GetBudgets
38 &GetBudgetHierarchy
39 &AddBudget
40 &ModBudget
41 &DelBudget
42 &GetBudgetSpent
43 &GetBudgetOrdered
44 &GetBudgetName
45 &GetPeriodsCount
46 GetBudgetHierarchySpent
47 GetBudgetHierarchyOrdered
49 &GetBudgetUsers
50 &ModBudgetUsers
51 &CanUserUseBudget
52 &CanUserModifyBudget
54 &GetBudgetPeriod
55 &GetBudgetPeriods
56 &ModBudgetPeriod
57 &AddBudgetPeriod
58 &DelBudgetPeriod
60 &ModBudgetPlan
62 &GetCurrency
63 &GetCurrencies
64 &ModCurrencies
65 &ConvertCurrency
67 &GetBudgetsPlanCell
68 &AddBudgetPlanValue
69 &GetBudgetAuthCats
70 &BudgetHasChildren
71 &CheckBudgetParent
72 &CheckBudgetParentPerm
74 &HideCols
75 &GetCols
79 # ----------------------------BUDGETS.PM-----------------------------";
82 =head1 FUNCTIONS ABOUT BUDGETS
84 =cut
86 sub HideCols {
87 my ( $authcat, @hide_cols ) = @_;
88 my $dbh = C4::Context->dbh;
90 my $sth1 = $dbh->prepare(
91 qq|
92 UPDATE aqbudgets_planning SET display = 0
93 WHERE authcat = ?
94 AND authvalue = ? |
96 foreach my $authvalue (@hide_cols) {
97 # $sth1->{TraceLevel} = 3;
98 $sth1->execute( $authcat, $authvalue );
102 sub GetCols {
103 my ( $authcat, $authvalue ) = @_;
105 my $dbh = C4::Context->dbh;
106 my $sth = $dbh->prepare(
108 SELECT count(display) as cnt from aqbudgets_planning
109 WHERE authcat = ?
110 AND authvalue = ? and display = 0 |
113 # $sth->{TraceLevel} = 3;
114 $sth->execute( $authcat, $authvalue );
115 my $res = $sth->fetchrow_hashref;
117 return $res->{cnt} > 0 ? 0: 1
121 sub CheckBudgetParentPerm {
122 my ( $budget, $borrower_id ) = @_;
123 my $depth = $budget->{depth};
124 my $parent_id = $budget->{budget_parent_id};
125 while ($depth) {
126 my $parent = GetBudget($parent_id);
127 $parent_id = $parent->{budget_parent_id};
128 if ( $parent->{budget_owner_id} == $borrower_id ) {
129 return 1;
131 $depth--
133 return 0;
136 sub AddBudgetPeriod {
137 my ($budgetperiod) = @_;
138 return unless($budgetperiod->{budget_period_startdate} && $budgetperiod->{budget_period_enddate});
140 my $resultset = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
141 return $resultset->create($budgetperiod)->id;
143 # -------------------------------------------------------------------
144 sub GetPeriodsCount {
145 my $dbh = C4::Context->dbh;
146 my $sth = $dbh->prepare("
147 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
148 $sth->execute();
149 my $res = $sth->fetchrow_hashref;
150 return $res->{'sum'};
153 # -------------------------------------------------------------------
154 sub CheckBudgetParent {
155 my ( $new_parent, $budget ) = @_;
156 my $new_parent_id = $new_parent->{'budget_id'};
157 my $budget_id = $budget->{'budget_id'};
158 my $dbh = C4::Context->dbh;
159 my $parent_id_tmp = $new_parent_id;
161 # check new-parent is not a child (or a child's child ;)
162 my $sth = $dbh->prepare(qq|
163 SELECT budget_parent_id FROM
164 aqbudgets where budget_id = ? | );
165 while (1) {
166 $sth->execute($parent_id_tmp);
167 my $res = $sth->fetchrow_hashref;
168 if ( $res->{'budget_parent_id'} == $budget_id ) {
169 return 1;
171 if ( not defined $res->{'budget_parent_id'} ) {
172 return 0;
174 $parent_id_tmp = $res->{'budget_parent_id'};
178 # -------------------------------------------------------------------
179 sub BudgetHasChildren {
180 my ( $budget_id ) = @_;
181 my $dbh = C4::Context->dbh;
182 my $sth = $dbh->prepare(qq|
183 SELECT count(*) as sum FROM aqbudgets
184 WHERE budget_parent_id = ? | );
185 $sth->execute( $budget_id );
186 my $sum = $sth->fetchrow_hashref;
187 return $sum->{'sum'};
190 sub GetBudgetChildren {
191 my ( $budget_id ) = @_;
192 my $dbh = C4::Context->dbh;
193 return $dbh->selectall_arrayref(q|
194 SELECT * FROM aqbudgets
195 WHERE budget_parent_id = ?
196 |, { Slice => {} }, $budget_id );
199 sub SetOwnerToFundHierarchy {
200 my ( $budget_id, $borrowernumber ) = @_;
202 my $budget = GetBudget( $budget_id );
203 $budget->{budget_owner_id} = $borrowernumber;
204 ModBudget( $budget );
205 my $children = GetBudgetChildren( $budget_id );
206 for my $child ( @$children ) {
207 SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber );
211 # -------------------------------------------------------------------
212 sub GetBudgetsPlanCell {
213 my ( $cell, $period, $budget ) = @_;
214 my ($actual, $sth);
215 my $dbh = C4::Context->dbh;
216 if ( $cell->{'authcat'} eq 'MONTHS' ) {
217 # get the actual amount
218 $sth = $dbh->prepare( qq|
220 SELECT SUM(ecost) AS actual FROM aqorders
221 WHERE budget_id = ? AND
222 entrydate like "$cell->{'authvalue'}%" |
224 $sth->execute( $cell->{'budget_id'} );
225 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
226 # get the actual amount
227 $sth = $dbh->prepare( qq|
229 SELECT SUM(ecost) FROM aqorders
230 LEFT JOIN aqorders_items
231 ON (aqorders.ordernumber = aqorders_items.ordernumber)
232 LEFT JOIN items
233 ON (aqorders_items.itemnumber = items.itemnumber)
234 WHERE budget_id = ? AND homebranch = ? | );
236 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
237 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
238 # get the actual amount
239 $sth = $dbh->prepare( qq|
241 SELECT SUM( ecost * quantity) AS actual
242 FROM aqorders JOIN biblioitems
243 ON (biblioitems.biblionumber = aqorders.biblionumber )
244 WHERE aqorders.budget_id = ? and itemtype = ? |
246 $sth->execute( $cell->{'budget_id'},
247 $cell->{'authvalue'} );
249 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
250 else {
251 # get the actual amount
252 $sth = $dbh->prepare( qq|
254 SELECT SUM(ecost * quantity) AS actual
255 FROM aqorders
256 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
257 WHERE aqorders.budget_id = ? AND
258 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
259 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
261 $sth->execute( $cell->{'budget_id'},
262 $budget->{'sort1_authcat'},
263 $cell->{'authvalue'},
264 $budget->{'sort2_authcat'},
265 $cell->{'authvalue'}
268 $actual = $sth->fetchrow_array;
270 # get the estimated amount
271 $sth = $dbh->prepare( qq|
273 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
274 WHERE budget_period_id = ? AND
275 budget_id = ? AND
276 authvalue = ? AND
277 authcat = ? |
279 $sth->execute( $cell->{'budget_period_id'},
280 $cell->{'budget_id'},
281 $cell->{'authvalue'},
282 $cell->{'authcat'},
286 my $res = $sth->fetchrow_hashref;
287 # my $display = $res->{'display'};
288 my $estimated = $res->{'estimated'};
291 return $actual, $estimated;
294 # -------------------------------------------------------------------
295 sub ModBudgetPlan {
296 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
297 my $dbh = C4::Context->dbh;
298 foreach my $buds (@$budget_plan) {
299 my $lines = $buds->{lines};
300 my $sth = $dbh->prepare( qq|
301 DELETE FROM aqbudgets_planning
302 WHERE budget_period_id = ? AND
303 budget_id = ? AND
304 authcat = ? |
306 #delete a aqplan line of cells, then insert new cells,
307 # these could be UPDATES rather than DEL/INSERTS...
308 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
310 foreach my $cell (@$lines) {
311 my $sth = $dbh->prepare( qq|
313 INSERT INTO aqbudgets_planning
314 SET budget_id = ?,
315 budget_period_id = ?,
316 authcat = ?,
317 estimated_amount = ?,
318 authvalue = ? |
320 $sth->execute(
321 $cell->{'budget_id'},
322 $cell->{'budget_period_id'},
323 $cell->{'authcat'},
324 $cell->{'estimated_amount'},
325 $cell->{'authvalue'},
331 # -------------------------------------------------------------------
332 sub GetBudgetSpent {
333 my ($budget_id) = @_;
334 my $dbh = C4::Context->dbh;
335 my $sth = $dbh->prepare(qq|
336 SELECT SUM( COALESCE(unitprice, ecost) * quantity ) AS sum FROM aqorders
337 WHERE budget_id = ? AND
338 quantityreceived > 0 AND
339 datecancellationprinted IS NULL
341 $sth->execute($budget_id);
342 my $sum = $sth->fetchrow_array;
344 $sth = $dbh->prepare(qq|
345 SELECT SUM(shipmentcost) AS sum
346 FROM aqinvoices
347 WHERE shipmentcost_budgetid = ?
348 AND closedate IS NOT NULL
350 $sth->execute($budget_id);
351 my ($shipmentcost_sum) = $sth->fetchrow_array;
352 $sum += $shipmentcost_sum;
354 return $sum;
357 # -------------------------------------------------------------------
358 sub GetBudgetOrdered {
359 my ($budget_id) = @_;
360 my $dbh = C4::Context->dbh;
361 my $sth = $dbh->prepare(qq|
362 SELECT SUM(ecost * quantity) AS sum FROM aqorders
363 WHERE budget_id = ? AND
364 quantityreceived = 0 AND
365 datecancellationprinted IS NULL
367 $sth->execute($budget_id);
368 my $sum = $sth->fetchrow_array;
370 $sth = $dbh->prepare(qq|
371 SELECT SUM(shipmentcost) AS sum
372 FROM aqinvoices
373 WHERE shipmentcost_budgetid = ?
374 AND closedate IS NULL
376 $sth->execute($budget_id);
377 my ($shipmentcost_sum) = $sth->fetchrow_array;
378 $sum += $shipmentcost_sum;
380 return $sum;
383 =head2 GetBudgetName
385 my $budget_name = &GetBudgetName($budget_id);
387 get the budget_name for a given budget_id
389 =cut
391 sub GetBudgetName {
392 my ( $budget_id ) = @_;
393 my $dbh = C4::Context->dbh;
394 my $sth = $dbh->prepare(
396 SELECT budget_name
397 FROM aqbudgets
398 WHERE budget_id = ?
401 $sth->execute($budget_id);
402 return $sth->fetchrow_array;
405 # -------------------------------------------------------------------
406 sub GetBudgetAuthCats {
407 my ($budget_period_id) = shift;
408 # now, populate the auth_cats_loop used in the budget planning button
409 # we must retrieve all auth values used by at least one budget
410 my $dbh = C4::Context->dbh;
411 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
412 $sth->execute($budget_period_id);
413 my %authcats;
414 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
415 $authcats{$sort1_authcat}=1;
416 $authcats{$sort2_authcat}=1;
418 my @auth_cats_loop;
419 foreach (sort keys %authcats) {
420 push @auth_cats_loop,{ authcat => $_ };
422 return \@auth_cats_loop;
425 # -------------------------------------------------------------------
426 sub GetBudgetPeriods {
427 my ($filters,$orderby) = @_;
429 my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
430 $rs = $rs->search( $filters, { order_by => $orderby } );
431 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
432 return [ $rs->all ];
434 # -------------------------------------------------------------------
435 sub GetBudgetPeriod {
436 my ($budget_period_id) = @_;
437 my $dbh = C4::Context->dbh;
438 ## $total = number of records linked to the record that must be deleted
439 my $total = 0;
440 ## get information about the record that will be deleted
441 my $sth;
442 if ($budget_period_id) {
443 $sth = $dbh->prepare( qq|
444 SELECT *
445 FROM aqbudgetperiods
446 WHERE budget_period_id=? |
448 $sth->execute($budget_period_id);
449 } else { # ACTIVE BUDGET
450 $sth = $dbh->prepare(qq|
451 SELECT *
452 FROM aqbudgetperiods
453 WHERE budget_period_active=1 |
455 $sth->execute();
457 my $data = $sth->fetchrow_hashref;
458 return $data;
461 # -------------------------------------------------------------------
462 sub DelBudgetPeriod{
463 my ($budget_period_id) = @_;
464 my $dbh = C4::Context->dbh;
465 ; ## $total = number of records linked to the record that must be deleted
466 my $total = 0;
468 ## get information about the record that will be deleted
469 my $sth = $dbh->prepare(qq|
470 DELETE
471 FROM aqbudgetperiods
472 WHERE budget_period_id=? |
474 return $sth->execute($budget_period_id);
477 # -------------------------------------------------------------------
478 sub ModBudgetPeriod {
479 my ($budget_period) = @_;
480 my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
481 return unless($result);
483 $result = $result->update($budget_period);
484 return $result->in_storage;
487 # -------------------------------------------------------------------
488 sub GetBudgetHierarchy {
489 my ( $budget_period_id, $branchcode, $owner ) = @_;
490 my @bind_params;
491 my $dbh = C4::Context->dbh;
492 my $query = qq|
493 SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description
494 FROM aqbudgets
495 JOIN aqbudgetperiods USING (budget_period_id)|;
497 my @where_strings;
498 # show only period X if requested
499 if ($budget_period_id) {
500 push @where_strings," aqbudgets.budget_period_id = ?";
501 push @bind_params, $budget_period_id;
503 # show only budgets owned by me, my branch or everyone
504 if ($owner) {
505 if ($branchcode) {
506 push @where_strings,
507 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="")))};
508 push @bind_params, ( $owner, $branchcode );
509 } else {
510 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
511 push @bind_params, $owner;
513 } else {
514 if ($branchcode) {
515 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
516 push @bind_params, $branchcode;
519 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
520 $debug && warn $query,join(",",@bind_params);
521 my $sth = $dbh->prepare($query);
522 $sth->execute(@bind_params);
524 my %links;
525 # create hash with budget_id has key
526 while ( my $data = $sth->fetchrow_hashref ) {
527 $links{ $data->{'budget_id'} } = $data;
530 # link child to parent
531 my @first_parents;
532 foreach ( sort keys %links ) {
533 my $child = $links{$_};
534 if ( $child->{'budget_parent_id'} ) {
535 my $parent = $links{ $child->{'budget_parent_id'} };
536 if ($parent) {
537 unless ( $parent->{'children'} ) {
538 # init child arrayref
539 $parent->{'children'} = [];
541 # add as child
542 push @{ $parent->{'children'} }, $child;
544 } else {
545 push @first_parents, $child;
549 my @sort = ();
550 foreach my $first_parent (@first_parents) {
551 _add_budget_children(\@sort, $first_parent);
554 foreach my $budget (@sort) {
555 $budget->{budget_spent} = GetBudgetSpent( $budget->{budget_id} );
556 $budget->{budget_ordered} = GetBudgetOrdered( $budget->{budget_id} );
557 $budget->{total_spent} = GetBudgetHierarchySpent( $budget->{budget_id} );
558 $budget->{total_ordered} = GetBudgetHierarchyOrdered( $budget->{budget_id} );
560 return \@sort;
563 # Recursive method to add a budget and its chidren to an array
564 sub _add_budget_children {
565 my $res = shift;
566 my $budget = shift;
567 push @$res, $budget;
568 my $children = $budget->{'children'} || [];
569 return unless @$children; # break recursivity
570 foreach my $child (@$children) {
571 _add_budget_children($res, $child);
575 # -------------------------------------------------------------------
577 sub AddBudget {
578 my ($budget) = @_;
579 return unless ($budget);
581 my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
582 return $resultset->create($budget)->id;
585 # -------------------------------------------------------------------
586 sub ModBudget {
587 my ($budget) = @_;
588 my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
589 return unless($result);
591 $result = $result->update($budget);
592 return $result->in_storage;
595 # -------------------------------------------------------------------
596 sub DelBudget {
597 my ($budget_id) = @_;
598 my $dbh = C4::Context->dbh;
599 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
600 my $rc = $sth->execute($budget_id);
601 return $rc;
605 =head2 GetBudget
607 &GetBudget($budget_id);
609 get a specific budget
611 =cut
613 # -------------------------------------------------------------------
614 sub GetBudget {
615 my ( $budget_id ) = @_;
616 my $dbh = C4::Context->dbh;
617 my $query = "
618 SELECT *
619 FROM aqbudgets
620 WHERE budget_id=?
622 my $sth = $dbh->prepare($query);
623 $sth->execute( $budget_id );
624 my $result = $sth->fetchrow_hashref;
625 return $result;
628 =head2 GetBudgetByOrderNumber
630 &GetBudgetByOrderNumber($ordernumber);
632 get a specific budget by order number
634 =cut
636 # -------------------------------------------------------------------
637 sub GetBudgetByOrderNumber {
638 my ( $ordernumber ) = @_;
639 my $dbh = C4::Context->dbh;
640 my $query = "
641 SELECT aqbudgets.*
642 FROM aqbudgets, aqorders
643 WHERE ordernumber=?
644 AND aqorders.budget_id = aqbudgets.budget_id
646 my $sth = $dbh->prepare($query);
647 $sth->execute( $ordernumber );
648 my $result = $sth->fetchrow_hashref;
649 return $result;
652 =head2 GetBudgetByCode
654 my $budget = &GetBudgetByCode($budget_code);
656 Retrieve all aqbudgets fields as a hashref for the budget that has
657 given budget_code
659 =cut
661 sub GetBudgetByCode {
662 my ( $budget_code ) = @_;
664 my $dbh = C4::Context->dbh;
665 my $query = qq{
666 SELECT *
667 FROM aqbudgets
668 WHERE budget_code = ?
669 ORDER BY budget_id DESC
670 LIMIT 1
672 my $sth = $dbh->prepare( $query );
673 $sth->execute( $budget_code );
674 return $sth->fetchrow_hashref;
677 =head2 GetBudgetHierarchySpent
679 my $spent = GetBudgetHierarchySpent( $budget_id );
681 Gets the total spent of the level and sublevels of $budget_id
683 =cut
685 sub GetBudgetHierarchySpent {
686 my ( $budget_id ) = @_;
687 my $dbh = C4::Context->dbh;
688 my $children_ids = $dbh->selectcol_arrayref(q|
689 SELECT budget_id
690 FROM aqbudgets
691 WHERE budget_parent_id = ?
692 |, {}, $budget_id );
694 my $total_spent = GetBudgetSpent( $budget_id );
695 for my $child_id ( @$children_ids ) {
696 $total_spent += GetBudgetHierarchySpent( $child_id );
698 return $total_spent;
701 =head2 GetBudgetHierarchyOrdered
703 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
705 Gets the total ordered of the level and sublevels of $budget_id
707 =cut
709 sub GetBudgetHierarchyOrdered {
710 my ( $budget_id ) = @_;
711 my $dbh = C4::Context->dbh;
712 my $children_ids = $dbh->selectcol_arrayref(q|
713 SELECT budget_id
714 FROM aqbudgets
715 WHERE budget_parent_id = ?
716 |, {}, $budget_id );
718 my $total_ordered = GetBudgetOrdered( $budget_id );
719 for my $child_id ( @$children_ids ) {
720 $total_ordered += GetBudgetHierarchyOrdered( $child_id );
722 return $total_ordered;
725 =head2 GetBudgets
727 &GetBudgets($filter, $order_by);
729 gets all budgets
731 =cut
733 # -------------------------------------------------------------------
734 sub GetBudgets {
735 my ($filters, $orderby) = @_;
736 $orderby = 'budget_name' unless($orderby);
738 my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
739 $rs = $rs->search( $filters, { order_by => $orderby } );
740 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
741 return [ $rs->all ];
744 =head2 GetBudgetUsers
746 my @borrowernumbers = &GetBudgetUsers($budget_id);
748 Return the list of borrowernumbers linked to a budget
750 =cut
752 sub GetBudgetUsers {
753 my ($budget_id) = @_;
755 my $dbh = C4::Context->dbh;
756 my $query = qq{
757 SELECT borrowernumber
758 FROM aqbudgetborrowers
759 WHERE budget_id = ?
761 my $sth = $dbh->prepare($query);
762 $sth->execute($budget_id);
764 my @borrowernumbers;
765 while (my ($borrowernumber) = $sth->fetchrow_array) {
766 push @borrowernumbers, $borrowernumber
769 return @borrowernumbers;
772 =head2 ModBudgetUsers
774 &ModBudgetUsers($budget_id, @borrowernumbers);
776 Modify the list of borrowernumbers linked to a budget
778 =cut
780 sub ModBudgetUsers {
781 my ($budget_id, @budget_users_id) = @_;
783 return unless $budget_id;
785 my $dbh = C4::Context->dbh;
786 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
787 my $sth = $dbh->prepare($query);
788 $sth->execute($budget_id);
790 $query = qq{
791 INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
792 VALUES (?,?)
794 $sth = $dbh->prepare($query);
795 foreach my $borrowernumber (@budget_users_id) {
796 next unless $borrowernumber;
797 $sth->execute($budget_id, $borrowernumber);
801 sub CanUserUseBudget {
802 my ($borrower, $budget, $userflags) = @_;
804 if (not ref $borrower) {
805 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
807 if (not ref $budget) {
808 $budget = GetBudget($budget);
811 return 0 unless ($borrower and $budget);
813 if (not defined $userflags) {
814 $userflags = C4::Auth::getuserflags($borrower->{flags},
815 $borrower->{userid});
818 unless ($userflags->{superlibrarian}
819 || (ref $userflags->{acquisition}
820 && $userflags->{acquisition}->{budget_manage_all})
821 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
823 if (not exists $userflags->{acquisition}) {
824 return 0;
827 if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
828 return 0;
831 # Budget restricted to owner
832 if ( $budget->{budget_permission} == 1 ) {
833 if ( $budget->{budget_owner_id}
834 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
836 return 0;
840 # Budget restricted to owner, users and library
841 elsif ( $budget->{budget_permission} == 2 ) {
842 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
844 if (
846 $budget->{budget_owner_id}
847 and $budget->{budget_owner_id} !=
848 $borrower->{borrowernumber}
849 or not $budget->{budget_owner_id}
851 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
852 @budget_users )
853 and defined $budget->{budget_branchcode}
854 and $budget->{budget_branchcode} ne
855 C4::Context->userenv->{branch}
858 return 0;
862 # Budget restricted to owner and users
863 elsif ( $budget->{budget_permission} == 3 ) {
864 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
865 if (
867 $budget->{budget_owner_id}
868 and $budget->{budget_owner_id} !=
869 $borrower->{borrowernumber}
870 or not $budget->{budget_owner_id}
872 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
873 @budget_users )
876 return 0;
881 return 1;
884 sub CanUserModifyBudget {
885 my ($borrower, $budget, $userflags) = @_;
887 if (not ref $borrower) {
888 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
890 if (not ref $budget) {
891 $budget = GetBudget($budget);
894 return 0 unless ($borrower and $budget);
896 if (not defined $userflags) {
897 $userflags = C4::Auth::getuserflags($borrower->{flags},
898 $borrower->{userid});
901 unless ($userflags->{superlibrarian}
902 || (ref $userflags->{acquisition}
903 && $userflags->{acquisition}->{budget_manage_all})
904 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
906 if (!CanUserUseBudget($borrower, $budget, $userflags)) {
907 return 0;
910 if (ref $userflags->{acquisition}
911 && !$userflags->{acquisition}->{budget_modify}) {
912 return 0;
916 return 1;
919 # -------------------------------------------------------------------
921 =head2 GetCurrencies
923 @currencies = &GetCurrencies;
925 Returns the list of all known currencies.
927 C<$currencies> is a array; its elements are references-to-hash, whose
928 keys are the fields from the currency table in the Koha database.
930 =cut
932 sub GetCurrencies {
933 my $dbh = C4::Context->dbh;
934 my $query = "
935 SELECT *
936 FROM currency
938 my $sth = $dbh->prepare($query);
939 $sth->execute;
940 my @results = ();
941 while ( my $data = $sth->fetchrow_hashref ) {
942 push( @results, $data );
944 return @results;
947 # -------------------------------------------------------------------
949 sub GetCurrency {
950 my $dbh = C4::Context->dbh;
951 my $query = "
952 SELECT * FROM currency where active = '1' ";
953 my $sth = $dbh->prepare($query);
954 $sth->execute;
955 my $r = $sth->fetchrow_hashref;
956 return $r;
959 =head2 ModCurrencies
961 &ModCurrencies($currency, $newrate);
963 Sets the exchange rate for C<$currency> to be C<$newrate>.
965 =cut
967 sub ModCurrencies {
968 my ( $currency, $rate ) = @_;
969 my $dbh = C4::Context->dbh;
970 my $query = qq|
971 UPDATE currency
972 SET rate=?
973 WHERE currency=? |;
974 my $sth = $dbh->prepare($query);
975 $sth->execute( $rate, $currency );
978 # -------------------------------------------------------------------
980 =head2 ConvertCurrency
982 $foreignprice = &ConvertCurrency($currency, $localprice);
984 Converts the price C<$localprice> to foreign currency C<$currency> by
985 dividing by the exchange rate, and returns the result.
987 If no exchange rate is found, e is one to one.
989 =cut
991 sub ConvertCurrency {
992 my ( $currency, $price ) = @_;
993 my $dbh = C4::Context->dbh;
994 my $query = "
995 SELECT rate
996 FROM currency
997 WHERE currency=?
999 my $sth = $dbh->prepare($query);
1000 $sth->execute($currency);
1001 my $cur = ( $sth->fetchrow_array() )[0];
1002 unless ($cur) {
1003 $cur = 1;
1005 return ( $price / $cur );
1009 =head2 CloneBudgetPeriod
1011 my $new_budget_period_id = CloneBudgetPeriod({
1012 budget_period_id => $budget_period_id,
1013 budget_period_startdate => $budget_period_startdate,
1014 budget_period_enddate => $budget_period_enddate,
1015 mark_original_budget_as_inactive => 1n
1016 reset_all_budgets => 1,
1019 Clone a budget period with all budgets.
1020 If the mark_origin_budget_as_inactive is set (0 by default),
1021 the original budget will be marked as inactive.
1023 If the reset_all_budgets is set (0 by default), all budget (fund)
1024 amounts will be reset.
1026 =cut
1028 sub CloneBudgetPeriod {
1029 my ($params) = @_;
1030 my $budget_period_id = $params->{budget_period_id};
1031 my $budget_period_startdate = $params->{budget_period_startdate};
1032 my $budget_period_enddate = $params->{budget_period_enddate};
1033 my $budget_period_description = $params->{budget_period_description};
1034 my $mark_original_budget_as_inactive =
1035 $params->{mark_original_budget_as_inactive} || 0;
1036 my $reset_all_budgets = $params->{reset_all_budgets} || 0;
1038 my $budget_period = GetBudgetPeriod($budget_period_id);
1040 $budget_period->{budget_period_startdate} = $budget_period_startdate;
1041 $budget_period->{budget_period_enddate} = $budget_period_enddate;
1042 $budget_period->{budget_period_description} = $budget_period_description;
1043 # The new budget (budget_period) should be active by default
1044 $budget_period->{budget_period_active} = 1;
1045 my $original_budget_period_id = $budget_period->{budget_period_id};
1046 delete $budget_period->{budget_period_id};
1047 my $new_budget_period_id = AddBudgetPeriod( $budget_period );
1049 my $budgets = GetBudgetHierarchy($budget_period_id);
1050 CloneBudgetHierarchy(
1052 budgets => $budgets,
1053 new_budget_period_id => $new_budget_period_id
1057 if ($mark_original_budget_as_inactive) {
1058 ModBudgetPeriod(
1060 budget_period_id => $budget_period_id,
1061 budget_period_active => 0,
1066 if ( $reset_all_budgets ) {
1067 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1068 for my $budget ( @$budgets ) {
1069 $budget->{budget_amount} = 0;
1070 ModBudget( $budget );
1074 return $new_budget_period_id;
1077 =head2 CloneBudgetHierarchy
1079 CloneBudgetHierarchy({
1080 budgets => $budgets,
1081 new_budget_period_id => $new_budget_period_id;
1084 Clone a budget hierarchy.
1086 =cut
1088 sub CloneBudgetHierarchy {
1089 my ($params) = @_;
1090 my $budgets = $params->{budgets};
1091 my $new_budget_period_id = $params->{new_budget_period_id};
1092 next unless @$budgets or $new_budget_period_id;
1094 my $children_of = $params->{children_of};
1095 my $new_parent_id = $params->{new_parent_id};
1097 my @first_level_budgets =
1098 ( not defined $children_of )
1099 ? map { ( not $_->{budget_parent_id} ) ? $_ : () } @$budgets
1100 : map { ( $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1102 # get only the columns of aqbudgets
1103 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
1105 for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1106 @first_level_budgets )
1109 my $tidy_budget =
1110 { map { join( ' ', @columns ) =~ /$_/ ? ( $_ => $budget->{$_} ) : () }
1111 keys %$budget };
1112 my $new_budget_id = AddBudget(
1114 %$tidy_budget,
1115 budget_id => undef,
1116 budget_parent_id => $new_parent_id,
1117 budget_period_id => $new_budget_period_id
1120 CloneBudgetHierarchy(
1122 budgets => $budgets,
1123 new_budget_period_id => $new_budget_period_id,
1124 children_of => $budget->{budget_id},
1125 new_parent_id => $new_budget_id
1131 =head2 MoveOrders
1133 my $report = MoveOrders({
1134 from_budget_period_id => $from_budget_period_id,
1135 to_budget_period_id => $to_budget_period_id,
1138 Move orders from one budget period to another.
1140 =cut
1142 sub MoveOrders {
1143 my ($params) = @_;
1144 my $from_budget_period_id = $params->{from_budget_period_id};
1145 my $to_budget_period_id = $params->{to_budget_period_id};
1146 my $move_remaining_unspent = $params->{move_remaining_unspent};
1147 return
1148 if not $from_budget_period_id
1149 or not $to_budget_period_id
1150 or $from_budget_period_id == $to_budget_period_id;
1152 # Can't move orders to an inactive budget (budgetperiod)
1153 my $budget_period = GetBudgetPeriod($to_budget_period_id);
1154 return unless $budget_period->{budget_period_active};
1156 my @report;
1157 my $dbh = C4::Context->dbh;
1158 my $sth_update_aqorders = $dbh->prepare(
1160 UPDATE aqorders
1161 SET budget_id = ?
1162 WHERE ordernumber = ?
1165 my $sth_update_budget_amount = $dbh->prepare(
1167 UPDATE aqbudgets
1168 SET budget_amount = ?
1169 WHERE budget_id = ?
1172 my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1173 for my $from_budget (@$from_budgets) {
1174 my $new_budget_id = $dbh->selectcol_arrayref(
1176 SELECT budget_id
1177 FROM aqbudgets
1178 WHERE budget_period_id = ?
1179 AND budget_code = ?
1180 |, {}, $to_budget_period_id, $from_budget->{budget_code}
1182 $new_budget_id = $new_budget_id->[0];
1183 my $new_budget = GetBudget( $new_budget_id );
1184 unless ( $new_budget ) {
1185 push @report,
1187 moved => 0,
1188 budget => $from_budget,
1189 error => 'budget_code_not_exists',
1191 next;
1193 my $orders_to_move = C4::Acquisition::SearchOrders(
1195 budget_id => $from_budget->{budget_id},
1196 pending => 1,
1200 my @orders_moved;
1201 for my $order (@$orders_to_move) {
1202 $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1203 push @orders_moved, $order;
1206 my $unspent_moved = 0;
1207 if ($move_remaining_unspent) {
1208 my $spent = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1209 my $unspent = $from_budget->{budget_amount} - $spent;
1210 my $new_budget_amount = $new_budget->{budget_amount};
1211 if ( $unspent > 0 ) {
1212 $new_budget_amount += $unspent;
1213 $unspent_moved = $unspent;
1215 $new_budget->{budget_amount} = $new_budget_amount;
1216 $sth_update_budget_amount->execute( $new_budget_amount,
1217 $new_budget->{budget_id} );
1220 push @report,
1222 budget => $new_budget,
1223 orders_moved => \@orders_moved,
1224 moved => 1,
1225 unspent_moved => $unspent_moved,
1228 return \@report;
1231 END { } # module clean-up code here (global destructor)
1234 __END__
1236 =head1 AUTHOR
1238 Koha Development Team <http://koha-community.org/>
1240 =cut