Bug 12539: PROG/CCSR deprecation: Remove hardcoded theme from C4/Templates.pm
[koha.git] / C4 / Budgets.pm
blob1a7f7e46890e6c65207e0049e92e0a0cefb08feb
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 # -------------------------------------------------------------------
191 sub GetBudgetsPlanCell {
192 my ( $cell, $period, $budget ) = @_;
193 my ($actual, $sth);
194 my $dbh = C4::Context->dbh;
195 if ( $cell->{'authcat'} eq 'MONTHS' ) {
196 # get the actual amount
197 $sth = $dbh->prepare( qq|
199 SELECT SUM(ecost) AS actual FROM aqorders
200 WHERE budget_id = ? AND
201 entrydate like "$cell->{'authvalue'}%" |
203 $sth->execute( $cell->{'budget_id'} );
204 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
205 # get the actual amount
206 $sth = $dbh->prepare( qq|
208 SELECT SUM(ecost) FROM aqorders
209 LEFT JOIN aqorders_items
210 ON (aqorders.ordernumber = aqorders_items.ordernumber)
211 LEFT JOIN items
212 ON (aqorders_items.itemnumber = items.itemnumber)
213 WHERE budget_id = ? AND homebranch = ? | );
215 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
216 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
217 # get the actual amount
218 $sth = $dbh->prepare( qq|
220 SELECT SUM( ecost * quantity) AS actual
221 FROM aqorders JOIN biblioitems
222 ON (biblioitems.biblionumber = aqorders.biblionumber )
223 WHERE aqorders.budget_id = ? and itemtype = ? |
225 $sth->execute( $cell->{'budget_id'},
226 $cell->{'authvalue'} );
228 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
229 else {
230 # get the actual amount
231 $sth = $dbh->prepare( qq|
233 SELECT SUM(ecost * quantity) AS actual
234 FROM aqorders
235 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
236 WHERE aqorders.budget_id = ? AND
237 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
238 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
240 $sth->execute( $cell->{'budget_id'},
241 $budget->{'sort1_authcat'},
242 $cell->{'authvalue'},
243 $budget->{'sort2_authcat'},
244 $cell->{'authvalue'}
247 $actual = $sth->fetchrow_array;
249 # get the estimated amount
250 $sth = $dbh->prepare( qq|
252 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
253 WHERE budget_period_id = ? AND
254 budget_id = ? AND
255 authvalue = ? AND
256 authcat = ? |
258 $sth->execute( $cell->{'budget_period_id'},
259 $cell->{'budget_id'},
260 $cell->{'authvalue'},
261 $cell->{'authcat'},
265 my $res = $sth->fetchrow_hashref;
266 # my $display = $res->{'display'};
267 my $estimated = $res->{'estimated'};
270 return $actual, $estimated;
273 # -------------------------------------------------------------------
274 sub ModBudgetPlan {
275 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
276 my $dbh = C4::Context->dbh;
277 foreach my $buds (@$budget_plan) {
278 my $lines = $buds->{lines};
279 my $sth = $dbh->prepare( qq|
280 DELETE FROM aqbudgets_planning
281 WHERE budget_period_id = ? AND
282 budget_id = ? AND
283 authcat = ? |
285 #delete a aqplan line of cells, then insert new cells,
286 # these could be UPDATES rather than DEL/INSERTS...
287 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
289 foreach my $cell (@$lines) {
290 my $sth = $dbh->prepare( qq|
292 INSERT INTO aqbudgets_planning
293 SET budget_id = ?,
294 budget_period_id = ?,
295 authcat = ?,
296 estimated_amount = ?,
297 authvalue = ? |
299 $sth->execute(
300 $cell->{'budget_id'},
301 $cell->{'budget_period_id'},
302 $cell->{'authcat'},
303 $cell->{'estimated_amount'},
304 $cell->{'authvalue'},
310 # -------------------------------------------------------------------
311 sub GetBudgetSpent {
312 my ($budget_id) = @_;
313 my $dbh = C4::Context->dbh;
314 my $sth = $dbh->prepare(qq|
315 SELECT SUM( COALESCE(unitprice, ecost) * quantity ) AS sum FROM aqorders
316 WHERE budget_id = ? AND
317 quantityreceived > 0 AND
318 datecancellationprinted IS NULL
320 $sth->execute($budget_id);
321 my $sum = $sth->fetchrow_array;
323 $sth = $dbh->prepare(qq|
324 SELECT SUM(shipmentcost) AS sum
325 FROM aqinvoices
326 WHERE shipmentcost_budgetid = ?
327 AND closedate IS NOT NULL
329 $sth->execute($budget_id);
330 my ($shipmentcost_sum) = $sth->fetchrow_array;
331 $sum += $shipmentcost_sum;
333 return $sum;
336 # -------------------------------------------------------------------
337 sub GetBudgetOrdered {
338 my ($budget_id) = @_;
339 my $dbh = C4::Context->dbh;
340 my $sth = $dbh->prepare(qq|
341 SELECT SUM(ecost * quantity) AS sum FROM aqorders
342 WHERE budget_id = ? AND
343 quantityreceived = 0 AND
344 datecancellationprinted IS NULL
346 $sth->execute($budget_id);
347 my $sum = $sth->fetchrow_array;
349 $sth = $dbh->prepare(qq|
350 SELECT SUM(shipmentcost) AS sum
351 FROM aqinvoices
352 WHERE shipmentcost_budgetid = ?
353 AND closedate IS NULL
355 $sth->execute($budget_id);
356 my ($shipmentcost_sum) = $sth->fetchrow_array;
357 $sum += $shipmentcost_sum;
359 return $sum;
362 =head2 GetBudgetName
364 my $budget_name = &GetBudgetName($budget_id);
366 get the budget_name for a given budget_id
368 =cut
370 sub GetBudgetName {
371 my ( $budget_id ) = @_;
372 my $dbh = C4::Context->dbh;
373 my $sth = $dbh->prepare(
375 SELECT budget_name
376 FROM aqbudgets
377 WHERE budget_id = ?
380 $sth->execute($budget_id);
381 return $sth->fetchrow_array;
384 # -------------------------------------------------------------------
385 sub GetBudgetAuthCats {
386 my ($budget_period_id) = shift;
387 # now, populate the auth_cats_loop used in the budget planning button
388 # we must retrieve all auth values used by at least one budget
389 my $dbh = C4::Context->dbh;
390 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
391 $sth->execute($budget_period_id);
392 my %authcats;
393 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
394 $authcats{$sort1_authcat}=1;
395 $authcats{$sort2_authcat}=1;
397 my @auth_cats_loop;
398 foreach (sort keys %authcats) {
399 push @auth_cats_loop,{ authcat => $_ };
401 return \@auth_cats_loop;
404 # -------------------------------------------------------------------
405 sub GetBudgetPeriods {
406 my ($filters,$orderby) = @_;
408 my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
409 $rs = $rs->search( $filters, { order_by => $orderby } );
410 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
411 return [ $rs->all ];
413 # -------------------------------------------------------------------
414 sub GetBudgetPeriod {
415 my ($budget_period_id) = @_;
416 my $dbh = C4::Context->dbh;
417 ## $total = number of records linked to the record that must be deleted
418 my $total = 0;
419 ## get information about the record that will be deleted
420 my $sth;
421 if ($budget_period_id) {
422 $sth = $dbh->prepare( qq|
423 SELECT *
424 FROM aqbudgetperiods
425 WHERE budget_period_id=? |
427 $sth->execute($budget_period_id);
428 } else { # ACTIVE BUDGET
429 $sth = $dbh->prepare(qq|
430 SELECT *
431 FROM aqbudgetperiods
432 WHERE budget_period_active=1 |
434 $sth->execute();
436 my $data = $sth->fetchrow_hashref;
437 return $data;
440 # -------------------------------------------------------------------
441 sub DelBudgetPeriod{
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;
447 ## get information about the record that will be deleted
448 my $sth = $dbh->prepare(qq|
449 DELETE
450 FROM aqbudgetperiods
451 WHERE budget_period_id=? |
453 return $sth->execute($budget_period_id);
456 # -------------------------------------------------------------------
457 sub ModBudgetPeriod {
458 my ($budget_period) = @_;
459 my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
460 return unless($result);
462 $result = $result->update($budget_period);
463 return $result->in_storage;
466 # -------------------------------------------------------------------
467 sub GetBudgetHierarchy {
468 my ( $budget_period_id, $branchcode, $owner ) = @_;
469 my @bind_params;
470 my $dbh = C4::Context->dbh;
471 my $query = qq|
472 SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description
473 FROM aqbudgets
474 JOIN aqbudgetperiods USING (budget_period_id)|;
476 my @where_strings;
477 # show only period X if requested
478 if ($budget_period_id) {
479 push @where_strings," aqbudgets.budget_period_id = ?";
480 push @bind_params, $budget_period_id;
482 # show only budgets owned by me, my branch or everyone
483 if ($owner) {
484 if ($branchcode) {
485 push @where_strings,
486 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="")))};
487 push @bind_params, ( $owner, $branchcode );
488 } else {
489 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
490 push @bind_params, $owner;
492 } else {
493 if ($branchcode) {
494 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)";
495 push @bind_params, $branchcode;
498 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
499 $debug && warn $query,join(",",@bind_params);
500 my $sth = $dbh->prepare($query);
501 $sth->execute(@bind_params);
502 my $results = $sth->fetchall_arrayref({});
503 my @res = @$results;
504 my $i = 0;
505 while (1) {
506 my $depth_cnt = 0;
507 foreach my $r (@res) {
508 my @child;
509 # look for children
510 $r->{depth} = '0' if !defined $r->{budget_parent_id};
511 foreach my $r2 (@res) {
512 if (defined $r2->{budget_parent_id}
513 && $r2->{budget_parent_id} == $r->{budget_id}) {
514 push @child, $r2->{budget_id};
515 $r2->{depth} = ($r->{depth} + 1) if defined $r->{depth};
518 $r->{child} = \@child if scalar @child > 0; # add the child
519 $depth_cnt++ if !defined $r->{'depth'};
521 last if ($depth_cnt == 0 || $i == 100);
522 $i++;
525 # look for top parents 1st
526 my (@sort, $depth_count);
527 ($i, $depth_count) = 0;
528 while (1) {
529 my $children = 0;
530 foreach my $r (@res) {
531 if ($r->{depth} == $depth_count) {
532 $children++ if (ref $r->{child} eq 'ARRAY');
534 # find the parent id element_id and insert it after
535 my $i2 = 0;
536 my $parent;
537 if ($depth_count > 0) {
539 # add indent
540 my $depth = $r->{depth} * 2;
541 $r->{budget_code_indent} = $r->{budget_code};
542 $r->{budget_name_indent} = $r->{budget_name};
543 foreach my $r3 (@sort) {
544 if ($r3->{budget_id} == $r->{budget_parent_id}) {
545 $parent = $i2;
546 last;
548 $i2++;
550 } else {
551 $r->{budget_code_indent} = $r->{budget_code};
552 $r->{budget_name_indent} = $r->{budget_name};
555 if (defined $parent) {
556 splice @sort, ($parent + 1), 0, $r;
557 } else {
558 push @sort, $r;
562 $i++;
563 } # --------------foreach
564 $depth_count++;
565 last if $children == 0;
569 foreach my $budget (@sort) {
570 $budget->{budget_spent} = GetBudgetSpent( $budget->{budget_id} );
571 $budget->{budget_ordered} = GetBudgetOrdered( $budget->{budget_id} );
572 $budget->{total_spent} = GetBudgetHierarchySpent( $budget->{budget_id} );
573 $budget->{total_ordered} = GetBudgetHierarchyOrdered( $budget->{budget_id} );
575 return \@sort;
578 # -------------------------------------------------------------------
580 sub AddBudget {
581 my ($budget) = @_;
582 return unless ($budget);
584 my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
585 return $resultset->create($budget)->id;
588 # -------------------------------------------------------------------
589 sub ModBudget {
590 my ($budget) = @_;
591 my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
592 return unless($result);
594 $result = $result->update($budget);
595 return $result->in_storage;
598 # -------------------------------------------------------------------
599 sub DelBudget {
600 my ($budget_id) = @_;
601 my $dbh = C4::Context->dbh;
602 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
603 my $rc = $sth->execute($budget_id);
604 return $rc;
608 =head2 GetBudget
610 &GetBudget($budget_id);
612 get a specific budget
614 =cut
616 # -------------------------------------------------------------------
617 sub GetBudget {
618 my ( $budget_id ) = @_;
619 my $dbh = C4::Context->dbh;
620 my $query = "
621 SELECT *
622 FROM aqbudgets
623 WHERE budget_id=?
625 my $sth = $dbh->prepare($query);
626 $sth->execute( $budget_id );
627 my $result = $sth->fetchrow_hashref;
628 return $result;
631 =head2 GetBudgetByOrderNumber
633 &GetBudgetByOrderNumber($ordernumber);
635 get a specific budget by order number
637 =cut
639 # -------------------------------------------------------------------
640 sub GetBudgetByOrderNumber {
641 my ( $ordernumber ) = @_;
642 my $dbh = C4::Context->dbh;
643 my $query = "
644 SELECT aqbudgets.*
645 FROM aqbudgets, aqorders
646 WHERE ordernumber=?
647 AND aqorders.budget_id = aqbudgets.budget_id
649 my $sth = $dbh->prepare($query);
650 $sth->execute( $ordernumber );
651 my $result = $sth->fetchrow_hashref;
652 return $result;
655 =head2 GetBudgetByCode
657 my $budget = &GetBudgetByCode($budget_code);
659 Retrieve all aqbudgets fields as a hashref for the budget that has
660 given budget_code
662 =cut
664 sub GetBudgetByCode {
665 my ( $budget_code ) = @_;
667 my $dbh = C4::Context->dbh;
668 my $query = qq{
669 SELECT *
670 FROM aqbudgets
671 WHERE budget_code = ?
672 ORDER BY budget_id DESC
673 LIMIT 1
675 my $sth = $dbh->prepare( $query );
676 $sth->execute( $budget_code );
677 return $sth->fetchrow_hashref;
680 =head2 GetBudgetHierarchySpent
682 my $spent = GetBudgetHierarchySpent( $budget_id );
684 Gets the total spent of the level and sublevels of $budget_id
686 =cut
688 sub GetBudgetHierarchySpent {
689 my ( $budget_id ) = @_;
690 my $dbh = C4::Context->dbh;
691 my $children_ids = $dbh->selectcol_arrayref(q|
692 SELECT budget_id
693 FROM aqbudgets
694 WHERE budget_parent_id = ?
695 |, {}, $budget_id );
697 my $total_spent = GetBudgetSpent( $budget_id );
698 for my $child_id ( @$children_ids ) {
699 $total_spent += GetBudgetHierarchySpent( $child_id );
701 return $total_spent;
704 =head2 GetBudgetHierarchyOrdered
706 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
708 Gets the total ordered of the level and sublevels of $budget_id
710 =cut
712 sub GetBudgetHierarchyOrdered {
713 my ( $budget_id ) = @_;
714 my $dbh = C4::Context->dbh;
715 my $children_ids = $dbh->selectcol_arrayref(q|
716 SELECT budget_id
717 FROM aqbudgets
718 WHERE budget_parent_id = ?
719 |, {}, $budget_id );
721 my $total_ordered = GetBudgetOrdered( $budget_id );
722 for my $child_id ( @$children_ids ) {
723 $total_ordered += GetBudgetHierarchyOrdered( $child_id );
725 return $total_ordered;
728 =head2 GetBudgets
730 &GetBudgets($filter, $order_by);
732 gets all budgets
734 =cut
736 # -------------------------------------------------------------------
737 sub GetBudgets {
738 my ($filters, $orderby) = @_;
739 $orderby = 'budget_name' unless($orderby);
741 my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
742 $rs = $rs->search( $filters, { order_by => $orderby } );
743 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
744 return [ $rs->all ];
747 =head2 GetBudgetUsers
749 my @borrowernumbers = &GetBudgetUsers($budget_id);
751 Return the list of borrowernumbers linked to a budget
753 =cut
755 sub GetBudgetUsers {
756 my ($budget_id) = @_;
758 my $dbh = C4::Context->dbh;
759 my $query = qq{
760 SELECT borrowernumber
761 FROM aqbudgetborrowers
762 WHERE budget_id = ?
764 my $sth = $dbh->prepare($query);
765 $sth->execute($budget_id);
767 my @borrowernumbers;
768 while (my ($borrowernumber) = $sth->fetchrow_array) {
769 push @borrowernumbers, $borrowernumber
772 return @borrowernumbers;
775 =head2 ModBudgetUsers
777 &ModBudgetUsers($budget_id, @borrowernumbers);
779 Modify the list of borrowernumbers linked to a budget
781 =cut
783 sub ModBudgetUsers {
784 my ($budget_id, @budget_users_id) = @_;
786 return unless $budget_id;
788 my $dbh = C4::Context->dbh;
789 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
790 my $sth = $dbh->prepare($query);
791 $sth->execute($budget_id);
793 $query = qq{
794 INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
795 VALUES (?,?)
797 $sth = $dbh->prepare($query);
798 foreach my $borrowernumber (@budget_users_id) {
799 next unless $borrowernumber;
800 $sth->execute($budget_id, $borrowernumber);
804 sub CanUserUseBudget {
805 my ($borrower, $budget, $userflags) = @_;
807 if (not ref $borrower) {
808 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
810 if (not ref $budget) {
811 $budget = GetBudget($budget);
814 return 0 unless ($borrower and $budget);
816 if (not defined $userflags) {
817 $userflags = C4::Auth::getuserflags($borrower->{flags},
818 $borrower->{userid});
821 unless ($userflags->{superlibrarian}
822 || (ref $userflags->{acquisition}
823 && $userflags->{acquisition}->{budget_manage_all})
824 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
826 if (not exists $userflags->{acquisition}) {
827 return 0;
830 if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
831 return 0;
834 # Budget restricted to owner
835 if ( $budget->{budget_permission} == 1 ) {
836 if ( $budget->{budget_owner_id}
837 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
839 return 0;
843 # Budget restricted to owner, users and library
844 elsif ( $budget->{budget_permission} == 2 ) {
845 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
847 if (
849 $budget->{budget_owner_id}
850 and $budget->{budget_owner_id} !=
851 $borrower->{borrowernumber}
852 or not $budget->{budget_owner_id}
854 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
855 @budget_users )
856 and defined $budget->{budget_branchcode}
857 and $budget->{budget_branchcode} ne
858 C4::Context->userenv->{branch}
861 return 0;
865 # Budget restricted to owner and users
866 elsif ( $budget->{budget_permission} == 3 ) {
867 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
868 if (
870 $budget->{budget_owner_id}
871 and $budget->{budget_owner_id} !=
872 $borrower->{borrowernumber}
873 or not $budget->{budget_owner_id}
875 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
876 @budget_users )
879 return 0;
884 return 1;
887 sub CanUserModifyBudget {
888 my ($borrower, $budget, $userflags) = @_;
890 if (not ref $borrower) {
891 $borrower = C4::Members::GetMember(borrowernumber => $borrower);
893 if (not ref $budget) {
894 $budget = GetBudget($budget);
897 return 0 unless ($borrower and $budget);
899 if (not defined $userflags) {
900 $userflags = C4::Auth::getuserflags($borrower->{flags},
901 $borrower->{userid});
904 unless ($userflags->{superlibrarian}
905 || (ref $userflags->{acquisition}
906 && $userflags->{acquisition}->{budget_manage_all})
907 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
909 if (!CanUserUseBudget($borrower, $budget, $userflags)) {
910 return 0;
913 if (ref $userflags->{acquisition}
914 && !$userflags->{acquisition}->{budget_modify}) {
915 return 0;
919 return 1;
922 # -------------------------------------------------------------------
924 =head2 GetCurrencies
926 @currencies = &GetCurrencies;
928 Returns the list of all known currencies.
930 C<$currencies> is a array; its elements are references-to-hash, whose
931 keys are the fields from the currency table in the Koha database.
933 =cut
935 sub GetCurrencies {
936 my $dbh = C4::Context->dbh;
937 my $query = "
938 SELECT *
939 FROM currency
941 my $sth = $dbh->prepare($query);
942 $sth->execute;
943 my @results = ();
944 while ( my $data = $sth->fetchrow_hashref ) {
945 push( @results, $data );
947 return @results;
950 # -------------------------------------------------------------------
952 sub GetCurrency {
953 my $dbh = C4::Context->dbh;
954 my $query = "
955 SELECT * FROM currency where active = '1' ";
956 my $sth = $dbh->prepare($query);
957 $sth->execute;
958 my $r = $sth->fetchrow_hashref;
959 return $r;
962 =head2 ModCurrencies
964 &ModCurrencies($currency, $newrate);
966 Sets the exchange rate for C<$currency> to be C<$newrate>.
968 =cut
970 sub ModCurrencies {
971 my ( $currency, $rate ) = @_;
972 my $dbh = C4::Context->dbh;
973 my $query = qq|
974 UPDATE currency
975 SET rate=?
976 WHERE currency=? |;
977 my $sth = $dbh->prepare($query);
978 $sth->execute( $rate, $currency );
981 # -------------------------------------------------------------------
983 =head2 ConvertCurrency
985 $foreignprice = &ConvertCurrency($currency, $localprice);
987 Converts the price C<$localprice> to foreign currency C<$currency> by
988 dividing by the exchange rate, and returns the result.
990 If no exchange rate is found, e is one to one.
992 =cut
994 sub ConvertCurrency {
995 my ( $currency, $price ) = @_;
996 my $dbh = C4::Context->dbh;
997 my $query = "
998 SELECT rate
999 FROM currency
1000 WHERE currency=?
1002 my $sth = $dbh->prepare($query);
1003 $sth->execute($currency);
1004 my $cur = ( $sth->fetchrow_array() )[0];
1005 unless ($cur) {
1006 $cur = 1;
1008 return ( $price / $cur );
1012 =head2 CloneBudgetPeriod
1014 my $new_budget_period_id = CloneBudgetPeriod({
1015 budget_period_id => $budget_period_id,
1016 budget_period_startdate => $budget_period_startdate,
1017 budget_period_enddate => $budget_period_enddate,
1018 mark_original_budget_as_inactive => 1n
1019 reset_all_budgets => 1,
1022 Clone a budget period with all budgets.
1023 If the mark_origin_budget_as_inactive is set (0 by default),
1024 the original budget will be marked as inactive.
1026 If the reset_all_budgets is set (0 by default), all budget (fund)
1027 amounts will be reset.
1029 =cut
1031 sub CloneBudgetPeriod {
1032 my ($params) = @_;
1033 my $budget_period_id = $params->{budget_period_id};
1034 my $budget_period_startdate = $params->{budget_period_startdate};
1035 my $budget_period_enddate = $params->{budget_period_enddate};
1036 my $budget_period_description = $params->{budget_period_description};
1037 my $mark_original_budget_as_inactive =
1038 $params->{mark_original_budget_as_inactive} || 0;
1039 my $reset_all_budgets = $params->{reset_all_budgets} || 0;
1041 my $budget_period = GetBudgetPeriod($budget_period_id);
1043 $budget_period->{budget_period_startdate} = $budget_period_startdate;
1044 $budget_period->{budget_period_enddate} = $budget_period_enddate;
1045 $budget_period->{budget_period_description} = $budget_period_description;
1046 # The new budget (budget_period) should be active by default
1047 $budget_period->{budget_period_active} = 1;
1048 my $original_budget_period_id = $budget_period->{budget_period_id};
1049 delete $budget_period->{budget_period_id};
1050 my $new_budget_period_id = AddBudgetPeriod( $budget_period );
1052 my $budgets = GetBudgetHierarchy($budget_period_id);
1053 CloneBudgetHierarchy(
1055 budgets => $budgets,
1056 new_budget_period_id => $new_budget_period_id
1060 if ($mark_original_budget_as_inactive) {
1061 ModBudgetPeriod(
1063 budget_period_id => $budget_period_id,
1064 budget_period_active => 0,
1069 if ( $reset_all_budgets ) {
1070 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1071 for my $budget ( @$budgets ) {
1072 $budget->{budget_amount} = 0;
1073 ModBudget( $budget );
1077 return $new_budget_period_id;
1080 =head2 CloneBudgetHierarchy
1082 CloneBudgetHierarchy({
1083 budgets => $budgets,
1084 new_budget_period_id => $new_budget_period_id;
1087 Clone a budget hierarchy.
1089 =cut
1091 sub CloneBudgetHierarchy {
1092 my ($params) = @_;
1093 my $budgets = $params->{budgets};
1094 my $new_budget_period_id = $params->{new_budget_period_id};
1095 next unless @$budgets or $new_budget_period_id;
1097 my $children_of = $params->{children_of};
1098 my $new_parent_id = $params->{new_parent_id};
1100 my @first_level_budgets =
1101 ( not defined $children_of )
1102 ? map { ( not $_->{budget_parent_id} ) ? $_ : () } @$budgets
1103 : map { ( $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1105 # get only the columns of aqbudgets
1106 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
1108 for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1109 @first_level_budgets )
1112 my $tidy_budget =
1113 { map { join( ' ', @columns ) =~ /$_/ ? ( $_ => $budget->{$_} ) : () }
1114 keys %$budget };
1115 my $new_budget_id = AddBudget(
1117 %$tidy_budget,
1118 budget_id => undef,
1119 budget_parent_id => $new_parent_id,
1120 budget_period_id => $new_budget_period_id
1123 CloneBudgetHierarchy(
1125 budgets => $budgets,
1126 new_budget_period_id => $new_budget_period_id,
1127 children_of => $budget->{budget_id},
1128 new_parent_id => $new_budget_id
1134 =head2 MoveOrders
1136 my $report = MoveOrders({
1137 from_budget_period_id => $from_budget_period_id,
1138 to_budget_period_id => $to_budget_period_id,
1141 Move orders from one budget period to another.
1143 =cut
1145 sub MoveOrders {
1146 my ($params) = @_;
1147 my $from_budget_period_id = $params->{from_budget_period_id};
1148 my $to_budget_period_id = $params->{to_budget_period_id};
1149 my $move_remaining_unspent = $params->{move_remaining_unspent};
1150 return
1151 if not $from_budget_period_id
1152 or not $to_budget_period_id
1153 or $from_budget_period_id == $to_budget_period_id;
1155 # Can't move orders to an inactive budget (budgetperiod)
1156 my $budget_period = GetBudgetPeriod($to_budget_period_id);
1157 return unless $budget_period->{budget_period_active};
1159 my @report;
1160 my $dbh = C4::Context->dbh;
1161 my $sth_update_aqorders = $dbh->prepare(
1163 UPDATE aqorders
1164 SET budget_id = ?
1165 WHERE ordernumber = ?
1168 my $sth_update_budget_amount = $dbh->prepare(
1170 UPDATE aqbudgets
1171 SET budget_amount = ?
1172 WHERE budget_id = ?
1175 my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1176 for my $from_budget (@$from_budgets) {
1177 my $new_budget_id = $dbh->selectcol_arrayref(
1179 SELECT budget_id
1180 FROM aqbudgets
1181 WHERE budget_period_id = ?
1182 AND budget_code = ?
1183 |, {}, $to_budget_period_id, $from_budget->{budget_code}
1185 $new_budget_id = $new_budget_id->[0];
1186 my $new_budget = GetBudget( $new_budget_id );
1187 unless ( $new_budget ) {
1188 push @report,
1190 moved => 0,
1191 budget => $from_budget,
1192 error => 'budget_code_not_exists',
1194 next;
1196 my $orders_to_move = C4::Acquisition::SearchOrders(
1198 budget_id => $from_budget->{budget_id},
1199 pending => 1,
1203 my @orders_moved;
1204 for my $order (@$orders_to_move) {
1205 $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1206 push @orders_moved, $order;
1209 my $unspent_moved = 0;
1210 if ($move_remaining_unspent) {
1211 my $spent = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1212 my $unspent = $from_budget->{budget_amount} - $spent;
1213 my $new_budget_amount = $new_budget->{budget_amount};
1214 if ( $unspent > 0 ) {
1215 $new_budget_amount += $unspent;
1216 $unspent_moved = $unspent;
1218 $new_budget->{budget_amount} = $new_budget_amount;
1219 $sth_update_budget_amount->execute( $new_budget_amount,
1220 $new_budget->{budget_id} );
1223 push @report,
1225 budget => $new_budget,
1226 orders_moved => \@orders_moved,
1227 moved => 1,
1228 unspent_moved => $unspent_moved,
1231 return \@report;
1234 END { } # module clean-up code here (global destructor)
1237 __END__
1239 =head1 AUTHOR
1241 Koha Development Team <http://koha-community.org/>
1243 =cut