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>.
25 use Koha::Acquisition::Invoice::Adjustments;
27 use C4::Log qw(logaction);
29 our (@ISA, @EXPORT_OK);
36 GetBudgetByOrderNumber
51 GetBudgetHierarchySpent
52 GetBudgetHierarchyOrdered
72 SetOwnerToFundHierarchy
82 FieldsForCalculatingFundValues
86 # ----------------------------BUDGETS.PM-----------------------------";
88 =head1 FUNCTIONS ABOUT BUDGETS
93 my ( $authcat, @hide_cols ) = @_;
94 my $dbh = C4::Context->dbh;
96 my $sth1 = $dbh->prepare(
98 UPDATE aqbudgets_planning SET display = 0
102 foreach my $authvalue (@hide_cols) {
103 # $sth1->{TraceLevel} = 3;
104 $sth1->execute( $authcat, $authvalue );
109 my ( $authcat, $authvalue ) = @_;
111 my $dbh = C4::Context->dbh;
112 my $sth = $dbh->prepare(
114 SELECT count(display) as cnt from aqbudgets_planning
116 AND authvalue = ? and display = 0 |
119 # $sth->{TraceLevel} = 3;
120 $sth->execute( $authcat, $authvalue );
121 my $res = $sth->fetchrow_hashref;
123 return $res->{cnt} > 0 ? 0: 1
127 sub CheckBudgetParentPerm {
128 my ( $budget, $borrower_id ) = @_;
129 my $depth = $budget->{depth};
130 my $parent_id = $budget->{budget_parent_id};
132 my $parent = GetBudget($parent_id);
133 $parent_id = $parent->{budget_parent_id};
134 if ( $parent->{budget_owner_id} == $borrower_id ) {
142 sub AddBudgetPeriod {
143 my ($budgetperiod) = @_;
144 return unless($budgetperiod->{budget_period_startdate} && $budgetperiod->{budget_period_enddate});
146 undef $budgetperiod->{budget_period_id};
147 my $resultset = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
148 return $resultset->create($budgetperiod)->id;
150 # -------------------------------------------------------------------
151 sub GetPeriodsCount {
152 my $dbh = C4::Context->dbh;
153 my $sth = $dbh->prepare("
154 SELECT COUNT(*) AS sum FROM aqbudgetperiods ");
156 my $res = $sth->fetchrow_hashref;
157 return $res->{'sum'};
160 # -------------------------------------------------------------------
161 sub CheckBudgetParent {
162 my ( $new_parent, $budget ) = @_;
163 my $new_parent_id = $new_parent->{'budget_id'};
164 my $budget_id = $budget->{'budget_id'};
165 my $dbh = C4::Context->dbh;
166 my $parent_id_tmp = $new_parent_id;
168 # check new-parent is not a child (or a child's child ;)
169 my $sth = $dbh->prepare(qq|
170 SELECT budget_parent_id FROM
171 aqbudgets where budget_id = ? | );
173 $sth->execute($parent_id_tmp);
174 my $res = $sth->fetchrow_hashref;
175 if ( $res->{'budget_parent_id'} == $budget_id ) {
178 if ( not defined $res->{'budget_parent_id'} ) {
181 $parent_id_tmp = $res->{'budget_parent_id'};
185 # -------------------------------------------------------------------
186 sub BudgetHasChildren {
187 my ( $budget_id ) = @_;
188 my $dbh = C4::Context->dbh;
189 my $sth = $dbh->prepare(qq|
190 SELECT count(*) as sum FROM aqbudgets
191 WHERE budget_parent_id = ? | );
192 $sth->execute( $budget_id );
193 my $sum = $sth->fetchrow_hashref;
194 return $sum->{'sum'};
197 sub GetBudgetChildren {
198 my ( $budget_id ) = @_;
199 my $dbh = C4::Context->dbh;
200 return $dbh->selectall_arrayref(q|
201 SELECT * FROM aqbudgets
202 WHERE budget_parent_id = ?
203 |, { Slice => {} }, $budget_id );
206 sub SetOwnerToFundHierarchy {
207 my ( $budget_id, $borrowernumber ) = @_;
209 my $budget = GetBudget( $budget_id );
210 $budget->{budget_owner_id} = $borrowernumber;
211 ModBudget( $budget );
212 my $children = GetBudgetChildren( $budget_id );
213 for my $child ( @$children ) {
214 SetOwnerToFundHierarchy( $child->{budget_id}, $borrowernumber );
218 # -------------------------------------------------------------------
219 sub GetBudgetsPlanCell {
220 my ( $cell, $period, $budget ) = @_; #FIXME we don't use $period
222 my $dbh = C4::Context->dbh;
223 my $roundsql = C4::Acquisition::get_rounding_sql(qq|ecost_tax_included|);
224 if ( $cell->{'authcat'} eq 'MONTHS' ) {
225 # get the actual amount
226 # FIXME we should consider quantity
227 $sth = $dbh->prepare( qq|
229 SELECT SUM(| . $roundsql . qq|) AS actual FROM aqorders
230 WHERE budget_id = ? AND
231 entrydate like "$cell->{'authvalue'}%" |
233 $sth->execute( $cell->{'budget_id'} );
234 } elsif ( $cell->{'authcat'} eq 'BRANCHES' ) {
235 # get the actual amount
236 # FIXME we should consider quantity
237 $sth = $dbh->prepare( qq|
239 SELECT SUM(| . $roundsql . qq|) FROM aqorders
240 LEFT JOIN aqorders_items
241 ON (aqorders.ordernumber = aqorders_items.ordernumber)
243 ON (aqorders_items.itemnumber = items.itemnumber)
244 WHERE budget_id = ? AND homebranch = ? | );
246 $sth->execute( $cell->{'budget_id'}, $cell->{'authvalue'} );
247 } elsif ( $cell->{'authcat'} eq 'ITEMTYPES' ) {
248 # get the actual amount
249 $sth = $dbh->prepare( qq|
251 SELECT SUM( | . $roundsql . qq| * quantity) AS actual
252 FROM aqorders JOIN biblioitems
253 ON (biblioitems.biblionumber = aqorders.biblionumber )
254 WHERE aqorders.budget_id = ? and itemtype = ? |
256 $sth->execute( $cell->{'budget_id'},
257 $cell->{'authvalue'} );
259 # ELSE GENERIC ORDERS SORT1/SORT2 STAT COUNT.
261 # get the actual amount
262 $sth = $dbh->prepare( qq|
264 SELECT SUM(| . $roundsql . qq| * quantity) AS actual
266 JOIN aqbudgets ON (aqbudgets.budget_id = aqorders.budget_id )
267 WHERE aqorders.budget_id = ? AND
268 ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR
269 (aqbudgets.sort2_authcat = ? AND sort2 =?)) |
271 $sth->execute( $cell->{'budget_id'},
272 $budget->{'sort1_authcat'},
273 $cell->{'authvalue'},
274 $budget->{'sort2_authcat'},
278 $actual = $sth->fetchrow_array;
280 # get the estimated amount
281 $sth = $dbh->prepare( qq|
283 SELECT estimated_amount AS estimated, display FROM aqbudgets_planning
284 WHERE budget_period_id = ? AND
289 $sth->execute( $cell->{'budget_period_id'},
290 $cell->{'budget_id'},
291 $cell->{'authvalue'},
296 my $res = $sth->fetchrow_hashref;
297 # my $display = $res->{'display'};
298 my $estimated = $res->{'estimated'};
301 return $actual, $estimated;
304 # -------------------------------------------------------------------
306 my ( $budget_plan, $budget_period_id, $authcat ) = @_;
307 my $dbh = C4::Context->dbh;
308 foreach my $buds (@$budget_plan) {
309 my $lines = $buds->{lines};
310 my $sth = $dbh->prepare( qq|
311 DELETE FROM aqbudgets_planning
312 WHERE budget_period_id = ? AND
316 #delete a aqplan line of cells, then insert new cells,
317 # these could be UPDATES rather than DEL/INSERTS...
318 $sth->execute( $budget_period_id, $lines->[0]{budget_id} , $authcat );
320 foreach my $cell (@$lines) {
321 my $sth = $dbh->prepare( qq|
323 INSERT INTO aqbudgets_planning
325 budget_period_id = ?,
327 estimated_amount = ?,
331 $cell->{'budget_id'},
332 $cell->{'budget_period_id'},
334 $cell->{'estimated_amount'},
335 $cell->{'authvalue'},
341 # -------------------------------------------------------------------
343 my ($budget_id) = @_;
344 my $dbh = C4::Context->dbh;
346 # Choose correct unitprice and ecost fields
347 my ( $unitprice_field, $ecost_field ) = FieldsForCalculatingFundValues();
349 my $sth = $dbh->prepare(qq|
350 SELECT SUM( | . C4::Acquisition::get_rounding_sql("COALESCE($unitprice_field, $ecost_field)") . qq| * quantity ) AS sum FROM aqorders
351 WHERE budget_id = ? AND
352 quantityreceived > 0 AND
353 datecancellationprinted IS NULL
355 $sth->execute($budget_id);
356 my $sum = ( $sth->fetchrow_array || 0 ) + 0;
358 $sth = $dbh->prepare(qq|
359 SELECT SUM(shipmentcost) AS sum
361 WHERE shipmentcost_budgetid = ?
364 $sth->execute($budget_id);
365 my ($shipmentcost_sum) = $sth->fetchrow_array;
366 $sum += ( $shipmentcost_sum || 0 ) + 0;
368 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $budget_id, closedate => { '!=' => undef } },{ join => 'invoiceid' });
369 while ( my $adj = $adjustments->next ){
370 $sum += $adj->adjustment;
376 # -------------------------------------------------------------------
377 sub GetBudgetOrdered {
378 my ($budget_id) = @_;
379 my $dbh = C4::Context->dbh;
381 # Get correct unitprice and ecost prices as possible
382 my ( $unitprice_field, $ecost_field ) = FieldsForCalculatingFundValues();
384 my $sth = $dbh->prepare(qq|
385 SELECT SUM(| . C4::Acquisition::get_rounding_sql(qq|$ecost_field|) . qq| * quantity) AS sum FROM aqorders
386 WHERE budget_id = ? AND
387 quantityreceived = 0 AND
388 datecancellationprinted IS NULL
390 $sth->execute($budget_id);
391 my $sum = ( $sth->fetchrow_array || 0 ) + 0;
393 my $adjustments = Koha::Acquisition::Invoice::Adjustments->search({budget_id => $budget_id, encumber_open => 1, closedate => undef},{ join => 'invoiceid' });
394 while ( my $adj = $adjustments->next ){
395 $sum += $adj->adjustment;
403 my $budget_name = &GetBudgetName($budget_id);
405 get the budget_name for a given budget_id
410 my ( $budget_id ) = @_;
411 my $dbh = C4::Context->dbh;
412 my $sth = $dbh->prepare(
419 $sth->execute($budget_id);
420 return $sth->fetchrow_array;
423 =head2 GetBudgetAuthCats
425 my $auth_cats = &GetBudgetAuthCats($budget_period_id);
427 Return the list of authcat for a given budget_period_id
431 sub GetBudgetAuthCats {
432 my ($budget_period_id) = shift;
433 # now, populate the auth_cats_loop used in the budget planning button
434 # we must retrieve all auth values used by at least one budget
435 my $dbh = C4::Context->dbh;
436 my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
437 $sth->execute($budget_period_id);
439 while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
440 $authcats{$sort1_authcat}=1 if $sort1_authcat;
441 $authcats{$sort2_authcat}=1 if $sort2_authcat;
443 return [ sort keys %authcats ];
446 # -------------------------------------------------------------------
447 sub GetBudgetPeriods {
448 my ($filters,$orderby) = @_;
450 my $rs = Koha::Database->new()->schema->resultset('Aqbudgetperiod');
451 $rs = $rs->search( $filters, { order_by => $orderby } );
452 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
455 # -------------------------------------------------------------------
456 sub GetBudgetPeriod {
457 my ($budget_period_id) = @_;
458 my $dbh = C4::Context->dbh;
459 my $sth = $dbh->prepare( qq|
462 WHERE budget_period_id=? |
464 $sth->execute($budget_period_id);
465 return $sth->fetchrow_hashref;
469 my ($budget_period_id) = @_;
470 my $dbh = C4::Context->dbh;
471 ; ## $total = number of records linked to the record that must be deleted
474 ## get information about the record that will be deleted
475 my $sth = $dbh->prepare(qq|
478 WHERE budget_period_id=? |
480 return $sth->execute($budget_period_id);
483 # -------------------------------------------------------------------
484 sub ModBudgetPeriod {
485 my ($budget_period) = @_;
486 my $result = Koha::Database->new()->schema->resultset('Aqbudgetperiod')->find($budget_period);
487 return unless($result);
489 $result = $result->update($budget_period);
490 return $result->in_storage;
493 # -------------------------------------------------------------------
494 sub GetBudgetHierarchy {
495 my ( $budget_period_id, $branchcode, $owner, $skiptotals ) = @_;
497 my $dbh = C4::Context->dbh;
499 SELECT aqbudgets.*, aqbudgetperiods.budget_period_active, aqbudgetperiods.budget_period_description,
500 b.firstname as budget_owner_firstname, b.surname as budget_owner_surname, b.borrowernumber as budget_owner_borrowernumber
502 LEFT JOIN borrowers b on b.borrowernumber = aqbudgets.budget_owner_id
503 JOIN aqbudgetperiods USING (budget_period_id)|;
506 # show only period X if requested
507 if ($budget_period_id) {
508 push @where_strings," aqbudgets.budget_period_id = ?";
509 push @bind_params, $budget_period_id;
511 # show only budgets owned by me, my branch or everyone
515 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="")))};
516 push @bind_params, ( $owner, $branchcode );
518 push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") ';
519 push @bind_params, $owner;
523 push @where_strings," (budget_branchcode =? or budget_branchcode is NULL OR budget_branchcode='')";
524 push @bind_params, $branchcode;
527 $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings;
528 my $sth = $dbh->prepare($query);
529 $sth->execute(@bind_params);
532 # create hash with budget_id has key
533 while ( my $data = $sth->fetchrow_hashref ) {
534 $links{ $data->{'budget_id'} } = $data;
537 # link child to parent
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'} };
544 unless ( $parent->{'children'} ) {
545 # init child arrayref
546 $parent->{'children'} = [];
549 push @{ $parent->{'children'} }, $child;
552 push @first_parents, $child;
557 foreach my $first_parent (@first_parents) {
558 _add_budget_children(\@sort, $first_parent, 0);
561 # Get correct unitprice and ecost prices as possible
562 my ( $unitprice_field, $ecost_field ) = FieldsForCalculatingFundValues();
565 # Get all the budgets totals in as few queries as possible
566 my $hr_budget_spent = $dbh->selectall_hashref(q|
567 SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
568 SUM( | . C4::Acquisition::get_rounding_sql(qq|COALESCE($unitprice_field, $ecost_field)|) . q| * quantity ) AS budget_spent
569 FROM aqorders JOIN aqbudgets USING (budget_id)
570 WHERE quantityreceived > 0 AND datecancellationprinted IS NULL
571 GROUP BY budget_id, budget_parent_id
573 my $hr_budget_ordered = $dbh->selectall_hashref(q|
574 SELECT aqorders.budget_id, aqbudgets.budget_parent_id,
575 SUM( | . C4::Acquisition::get_rounding_sql(qq|$ecost_field|) . q| * quantity) AS budget_ordered
576 FROM aqorders JOIN aqbudgets USING (budget_id)
577 WHERE quantityreceived = 0 AND datecancellationprinted IS NULL
578 GROUP BY budget_id, budget_parent_id
580 my $hr_budget_spent_shipment = $dbh->selectall_hashref(q|
581 SELECT shipmentcost_budgetid as budget_id,
582 SUM(shipmentcost) as shipmentcost
584 GROUP BY shipmentcost_budgetid
586 my $hr_budget_spent_adjustment = $dbh->selectall_hashref(q|
588 SUM(adjustment) as adjustments
589 FROM aqinvoice_adjustments
590 JOIN aqinvoices USING (invoiceid)
591 WHERE closedate IS NOT NULL
594 my $hr_budget_ordered_adjustment = $dbh->selectall_hashref(q|
596 SUM(adjustment) as adjustments
597 FROM aqinvoice_adjustments
598 JOIN aqinvoices USING (invoiceid)
599 WHERE closedate IS NULL AND encumber_open = 1
604 foreach my $budget (@sort) {
605 if ( not defined $budget->{budget_parent_id} ) {
606 _recursiveAdd( $budget, undef, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
614 my ($budget, $parent, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment ) = @_;
616 foreach my $child (@{$budget->{children}}){
617 _recursiveAdd($child, $budget, $hr_budget_spent, $hr_budget_spent_shipment, $hr_budget_ordered, $hr_budget_spent_adjustment, $hr_budget_ordered_adjustment );
620 $budget->{budget_spent} += $hr_budget_spent->{$budget->{budget_id}}->{budget_spent} || 0;
621 $budget->{budget_spent} += $hr_budget_spent_shipment->{$budget->{budget_id}}->{shipmentcost} || 0;
622 $budget->{budget_spent} += $hr_budget_spent_adjustment->{$budget->{budget_id}}->{adjustments} || 0;
623 $budget->{budget_ordered} += $hr_budget_ordered->{$budget->{budget_id}}->{budget_ordered} || 0;
624 $budget->{budget_ordered} += $hr_budget_ordered_adjustment->{$budget->{budget_id}}->{adjustments} || 0;
626 $budget->{total_spent} += $budget->{budget_spent};
627 $budget->{total_ordered} += $budget->{budget_ordered};
630 $parent->{total_spent} += $budget->{total_spent};
631 $parent->{total_ordered} += $budget->{total_ordered};
635 # Recursive method to add a budget and its chidren to an array
636 sub _add_budget_children {
639 $budget->{budget_level} = shift;
641 my $children = $budget->{'children'} || [];
642 return unless @$children; # break recursivity
643 foreach my $child (@$children) {
644 _add_budget_children($res, $child, $budget->{budget_level} + 1);
648 # -------------------------------------------------------------------
649 # FIXME Must be replaced by Koha::Acquisition::Fund->store
652 return unless ($budget);
654 undef $budget->{budget_encumb} if defined $budget->{budget_encumb} && $budget->{budget_encumb} eq '';
655 undef $budget->{budget_owner_id} if defined $budget->{budget_owner_id} && $budget->{budget_owner_id} eq '';
656 my $resultset = Koha::Database->new()->schema->resultset('Aqbudget');
657 my $id = $resultset->create($budget)->id;
660 if (C4::Context->preference("AcquisitionLog")) {
662 budget_amount => $budget->{budget_amount},
663 budget_encumb => $budget->{budget_encumb},
664 budget_expend => $budget->{budget_expend}
676 # -------------------------------------------------------------------
677 # FIXME Must be replaced by Koha::Acquisition::Fund->store
680 my $result = Koha::Database->new()->schema->resultset('Aqbudget')->find($budget);
681 return unless($result);
683 # Log this modification
684 if (C4::Context->preference("AcquisitionLog")) {
686 budget_amount_new => $budget->{budget_amount},
687 budget_encumb_new => $budget->{budget_encumb},
688 budget_expend_new => $budget->{budget_expend},
689 budget_amount_old => $result->budget_amount,
690 budget_encumb_old => $result->budget_encumb,
691 budget_expend_old => $result->budget_expend,
692 budget_amount_change => 0 - ($result->budget_amount - $budget->{budget_amount})
697 $budget->{budget_id},
702 undef $budget->{budget_encumb} if defined $budget->{budget_encumb} && $budget->{budget_encumb} eq '';
703 undef $budget->{budget_owner_id} if defined $budget->{budget_owner_id} && $budget->{budget_owner_id} eq '';
704 $result = $result->update($budget);
705 return $result->in_storage;
708 # -------------------------------------------------------------------
709 # FIXME Must be replaced by Koha::Acquisition::Fund->delete
711 my ($budget_id) = @_;
712 my $dbh = C4::Context->dbh;
713 my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?");
714 my $rc = $sth->execute($budget_id);
716 if (C4::Context->preference("AcquisitionLog")) {
727 # -------------------------------------------------------------------
731 &GetBudget($budget_id);
733 get a specific budget
738 my ( $budget_id ) = @_;
739 my $dbh = C4::Context->dbh;
745 my $sth = $dbh->prepare($query);
746 $sth->execute( $budget_id );
747 my $result = $sth->fetchrow_hashref;
751 # -------------------------------------------------------------------
753 =head2 GetBudgetByOrderNumber
755 &GetBudgetByOrderNumber($ordernumber);
757 get a specific budget by order number
761 sub GetBudgetByOrderNumber {
762 my ( $ordernumber ) = @_;
763 my $dbh = C4::Context->dbh;
766 FROM aqbudgets, aqorders
768 AND aqorders.budget_id = aqbudgets.budget_id
770 my $sth = $dbh->prepare($query);
771 $sth->execute( $ordernumber );
772 my $result = $sth->fetchrow_hashref;
776 =head2 GetBudgetReport
778 &GetBudgetReport( [$budget_id] );
780 Get all orders for a specific budget, without cancelled orders.
782 Returns an array of hashrefs.
786 # --------------------------------------------------------------------
787 sub GetBudgetReport {
788 my ( $budget_id ) = @_;
789 my $dbh = C4::Context->dbh;
791 SELECT o.*, b.budget_name
793 INNER JOIN aqorders o
794 ON b.budget_id = o.budget_id
796 AND (o.orderstatus != "cancelled")
797 ORDER BY b.budget_name';
799 my $sth = $dbh->prepare($query);
800 $sth->execute( $budget_id );
803 while ( my $data = $sth->fetchrow_hashref ) {
804 push( @results, $data );
809 =head2 GetBudgetsByActivity
811 &GetBudgetsByActivity( $budget_period_active );
813 Get all active or inactive budgets, depending of the value
821 # --------------------------------------------------------------------
822 sub GetBudgetsByActivity {
823 my ( $budget_period_active ) = @_;
824 my $dbh = C4::Context->dbh;
827 FROM aqbudgetperiods bp
828 INNER JOIN aqbudgets b
829 ON bp.budget_period_id = b.budget_period_id
830 WHERE bp.budget_period_active=?
832 my $sth = $dbh->prepare($query);
833 $sth->execute( $budget_period_active );
835 while ( my $data = $sth->fetchrow_hashref ) {
836 push( @results, $data );
840 # --------------------------------------------------------------------
842 =head2 GetBudgetsReport
844 &GetBudgetsReport( [$activity] );
846 Get all but cancelled orders for all funds.
848 If the optionnal activity parameter is passed, returns orders for active/inactive budgets only.
853 Returns an array of hashrefs.
857 sub GetBudgetsReport {
859 my $dbh = C4::Context->dbh;
861 SELECT o.*, b.budget_name
862 FROM aqbudgetperiods bp
863 INNER JOIN aqbudgets b
864 ON bp.budget_period_id = b.budget_period_id
865 INNER JOIN aqorders o
866 ON b.budget_id = o.budget_id ';
867 if ( $activity && $activity ne '' ) {
868 $query .= 'WHERE bp.budget_period_active=? ';
870 $query .= 'AND (o.orderstatus != "cancelled")
871 ORDER BY b.budget_name';
873 my $sth = $dbh->prepare($query);
874 if ( $activity && $activity ne '' ) {
875 $sth->execute($activity);
881 while ( my $data = $sth->fetchrow_hashref ) {
882 push( @results, $data );
887 =head2 GetBudgetByCode
889 my $budget = &GetBudgetByCode($budget_code);
891 Retrieve all aqbudgets fields as a hashref for the budget that has
896 sub GetBudgetByCode {
897 my ( $budget_code ) = @_;
899 my $dbh = C4::Context->dbh;
903 JOIN aqbudgetperiods USING (budget_period_id)
904 WHERE budget_code = ?
905 ORDER BY budget_period_active DESC, budget_id DESC
908 my $sth = $dbh->prepare( $query );
909 $sth->execute( $budget_code );
910 return $sth->fetchrow_hashref;
913 =head2 GetBudgetHierarchySpent
915 my $spent = GetBudgetHierarchySpent( $budget_id );
917 Gets the total spent of the level and sublevels of $budget_id
921 sub GetBudgetHierarchySpent {
922 my ( $budget_id ) = @_;
923 my $dbh = C4::Context->dbh;
924 my $children_ids = $dbh->selectcol_arrayref(q|
927 WHERE budget_parent_id = ?
930 my $total_spent = GetBudgetSpent( $budget_id );
931 for my $child_id ( @$children_ids ) {
932 $total_spent += GetBudgetHierarchySpent( $child_id );
937 =head2 GetBudgetHierarchyOrdered
939 my $ordered = GetBudgetHierarchyOrdered( $budget_id );
941 Gets the total ordered of the level and sublevels of $budget_id
945 sub GetBudgetHierarchyOrdered {
946 my ( $budget_id ) = @_;
947 my $dbh = C4::Context->dbh;
948 my $children_ids = $dbh->selectcol_arrayref(q|
951 WHERE budget_parent_id = ?
954 my $total_ordered = GetBudgetOrdered( $budget_id );
955 for my $child_id ( @$children_ids ) {
956 $total_ordered += GetBudgetHierarchyOrdered( $child_id );
958 return $total_ordered;
963 &GetBudgets($filter, $order_by);
969 # -------------------------------------------------------------------
971 my ($filters, $orderby) = @_;
972 $orderby = 'budget_name' unless($orderby);
974 my $rs = Koha::Database->new()->schema->resultset('Aqbudget');
975 $rs = $rs->search( $filters, { order_by => $orderby } );
976 $rs->result_class('DBIx::Class::ResultClass::HashRefInflator');
980 =head2 GetBudgetUsers
982 my @borrowernumbers = &GetBudgetUsers($budget_id);
984 Return the list of borrowernumbers linked to a budget
989 my ($budget_id) = @_;
991 my $dbh = C4::Context->dbh;
993 SELECT borrowernumber
994 FROM aqbudgetborrowers
997 my $sth = $dbh->prepare($query);
998 $sth->execute($budget_id);
1000 my @borrowernumbers;
1001 while (my ($borrowernumber) = $sth->fetchrow_array) {
1002 push @borrowernumbers, $borrowernumber
1005 return @borrowernumbers;
1008 =head2 ModBudgetUsers
1010 &ModBudgetUsers($budget_id, @borrowernumbers);
1012 Modify the list of borrowernumbers linked to a budget
1016 sub ModBudgetUsers {
1017 my ($budget_id, @budget_users_id) = @_;
1019 return unless $budget_id;
1021 my $dbh = C4::Context->dbh;
1022 my $query = "DELETE FROM aqbudgetborrowers WHERE budget_id = ?";
1023 my $sth = $dbh->prepare($query);
1024 $sth->execute($budget_id);
1027 INSERT INTO aqbudgetborrowers (budget_id, borrowernumber)
1030 $sth = $dbh->prepare($query);
1031 foreach my $borrowernumber (@budget_users_id) {
1032 next unless $borrowernumber;
1033 $sth->execute($budget_id, $borrowernumber);
1037 sub CanUserUseBudget {
1038 my ($borrower, $budget, $userflags) = @_;
1040 if (not ref $borrower) {
1041 $borrower = Koha::Patrons->find( $borrower );
1042 return 0 unless $borrower;
1043 $borrower = $borrower->unblessed;
1045 if (not ref $budget) {
1046 $budget = GetBudget($budget);
1049 return 0 unless ($borrower and $budget);
1051 if (not defined $userflags) {
1052 $userflags = C4::Auth::getuserflags($borrower->{flags},
1053 $borrower->{userid});
1056 unless ($userflags->{superlibrarian}
1057 || (ref $userflags->{acquisition}
1058 && $userflags->{acquisition}->{budget_manage_all})
1059 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
1061 if (not exists $userflags->{acquisition}) {
1065 if (!ref $userflags->{acquisition} && !$userflags->{acquisition}) {
1069 # Budget restricted to owner
1070 if ( $budget->{budget_permission} == 1 ) {
1071 if ( $budget->{budget_owner_id}
1072 and $budget->{budget_owner_id} != $borrower->{borrowernumber} )
1078 # Budget restricted to owner, users and library
1079 elsif ( $budget->{budget_permission} == 2 ) {
1080 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
1084 $budget->{budget_owner_id}
1085 and $budget->{budget_owner_id} !=
1086 $borrower->{borrowernumber}
1087 or not $budget->{budget_owner_id}
1089 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
1091 and defined $budget->{budget_branchcode}
1092 and $budget->{budget_branchcode} ne
1093 C4::Context->userenv->{branch}
1100 # Budget restricted to owner and users
1101 elsif ( $budget->{budget_permission} == 3 ) {
1102 my @budget_users = GetBudgetUsers( $budget->{budget_id} );
1105 $budget->{budget_owner_id}
1106 and $budget->{budget_owner_id} !=
1107 $borrower->{borrowernumber}
1108 or not $budget->{budget_owner_id}
1110 and ( 0 == grep { $borrower->{borrowernumber} == $_ }
1122 sub CanUserModifyBudget {
1123 my ($borrower, $budget, $userflags) = @_;
1125 if (not ref $borrower) {
1126 $borrower = Koha::Patrons->find( $borrower );
1127 return 0 unless $borrower;
1128 $borrower = $borrower->unblessed;
1130 if (not ref $budget) {
1131 $budget = GetBudget($budget);
1134 return 0 unless ($borrower and $budget);
1136 if (not defined $userflags) {
1137 $userflags = C4::Auth::getuserflags($borrower->{flags},
1138 $borrower->{userid});
1141 unless ($userflags->{superlibrarian}
1142 || (ref $userflags->{acquisition}
1143 && $userflags->{acquisition}->{budget_manage_all})
1144 || (!ref $userflags->{acquisition} && $userflags->{acquisition}))
1146 if (!CanUserUseBudget($borrower, $budget, $userflags)) {
1150 if (ref $userflags->{acquisition}
1151 && !$userflags->{acquisition}->{budget_modify}) {
1160 my ($value, $increment) = @_;
1162 if ($increment && $increment != 0) {
1163 $value = int($value / $increment) * $increment;
1169 =head2 CloneBudgetPeriod
1171 my $new_budget_period_id = CloneBudgetPeriod({
1172 budget_period_id => $budget_period_id,
1173 budget_period_startdate => $budget_period_startdate,
1174 budget_period_enddate => $budget_period_enddate,
1175 mark_original_budget_as_inactive => 1n
1176 reset_all_budgets => 1,
1179 Clone a budget period with all budgets.
1180 If the mark_origin_budget_as_inactive is set (0 by default),
1181 the original budget will be marked as inactive.
1183 If the reset_all_budgets is set (0 by default), all budget (fund)
1184 amounts will be reset.
1188 sub CloneBudgetPeriod {
1190 my $budget_period_id = $params->{budget_period_id};
1191 my $budget_period_startdate = $params->{budget_period_startdate};
1192 my $budget_period_enddate = $params->{budget_period_enddate};
1193 my $budget_period_description = $params->{budget_period_description};
1194 my $amount_change_percentage = $params->{amount_change_percentage};
1195 my $amount_change_round_increment = $params->{amount_change_round_increment};
1196 my $mark_original_budget_as_inactive =
1197 $params->{mark_original_budget_as_inactive} || 0;
1198 my $reset_all_budgets = $params->{reset_all_budgets} || 0;
1200 my $budget_period = GetBudgetPeriod($budget_period_id);
1202 $budget_period->{budget_period_startdate} = $budget_period_startdate;
1203 $budget_period->{budget_period_enddate} = $budget_period_enddate;
1204 $budget_period->{budget_period_description} = $budget_period_description;
1205 # The new budget (budget_period) should be active by default
1206 $budget_period->{budget_period_active} = 1;
1208 if ($amount_change_percentage) {
1209 my $total = $budget_period->{budget_period_total};
1210 $total += $total * $amount_change_percentage / 100;
1211 $total = _round($total, $amount_change_round_increment);
1212 $budget_period->{budget_period_total} = $total;
1215 my $original_budget_period_id = $budget_period->{budget_period_id};
1216 delete $budget_period->{budget_period_id};
1217 my $new_budget_period_id = AddBudgetPeriod( $budget_period );
1219 my $budgets = GetBudgetHierarchy($budget_period_id);
1220 CloneBudgetHierarchy(
1222 budgets => $budgets,
1223 new_budget_period_id => $new_budget_period_id
1227 if ($mark_original_budget_as_inactive) {
1230 budget_period_id => $budget_period_id,
1231 budget_period_active => 0,
1236 if ( $reset_all_budgets ) {
1237 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1238 for my $budget ( @$budgets ) {
1239 $budget->{budget_amount} = 0;
1240 ModBudget( $budget );
1242 } elsif ($amount_change_percentage) {
1243 my $budgets = GetBudgets({ budget_period_id => $new_budget_period_id });
1244 for my $budget ( @$budgets ) {
1245 my $amount = $budget->{budget_amount};
1246 $amount += $amount * $amount_change_percentage / 100;
1247 $amount = _round($amount, $amount_change_round_increment);
1248 $budget->{budget_amount} = $amount;
1249 ModBudget( $budget );
1253 return $new_budget_period_id;
1256 =head2 CloneBudgetHierarchy
1258 CloneBudgetHierarchy({
1259 budgets => $budgets,
1260 new_budget_period_id => $new_budget_period_id;
1263 Clone a budget hierarchy.
1267 sub CloneBudgetHierarchy {
1269 my $budgets = $params->{budgets};
1270 my $new_budget_period_id = $params->{new_budget_period_id};
1271 next unless @$budgets or $new_budget_period_id;
1273 my $children_of = $params->{children_of};
1274 my $new_parent_id = $params->{new_parent_id};
1276 my @first_level_budgets =
1277 ( not defined $children_of )
1278 ? map { ( not $_->{budget_parent_id} ) ? $_ : () } @$budgets
1279 : map { ( defined $_->{budget_parent_id} && $_->{budget_parent_id} == $children_of ) ? $_ : () } @$budgets;
1281 # get only the columns of aqbudgets
1282 my @columns = Koha::Database->new()->schema->source('Aqbudget')->columns;
1284 for my $budget ( sort { $a->{budget_id} <=> $b->{budget_id} }
1285 @first_level_budgets )
1289 { map { join( ' ', @columns ) =~ /$_/ ? ( $_ => $budget->{$_} ) : () }
1291 delete $tidy_budget->{timestamp};
1292 my $new_budget_id = AddBudget(
1296 budget_parent_id => $new_parent_id,
1297 budget_period_id => $new_budget_period_id
1300 my @borrowernumbers = GetBudgetUsers($budget->{budget_id});
1301 ModBudgetUsers($new_budget_id, @borrowernumbers);
1302 CloneBudgetHierarchy(
1304 budgets => $budgets,
1305 new_budget_period_id => $new_budget_period_id,
1306 children_of => $budget->{budget_id},
1307 new_parent_id => $new_budget_id
1315 my $report = MoveOrders({
1316 from_budget_period_id => $from_budget_period_id,
1317 to_budget_period_id => $to_budget_period_id,
1320 Move orders from one budget period to another.
1326 my $from_budget_period_id = $params->{from_budget_period_id};
1327 my $to_budget_period_id = $params->{to_budget_period_id};
1328 my $move_remaining_unspent = $params->{move_remaining_unspent};
1330 if not $from_budget_period_id
1331 or not $to_budget_period_id
1332 or $from_budget_period_id == $to_budget_period_id;
1334 # Can't move orders to an inactive budget (budgetperiod)
1335 my $budget_period = GetBudgetPeriod($to_budget_period_id);
1336 return unless $budget_period->{budget_period_active};
1339 my $dbh = C4::Context->dbh;
1340 my $sth_update_aqorders = $dbh->prepare(
1344 WHERE ordernumber = ?
1347 my $sth_update_budget_amount = $dbh->prepare(
1350 SET budget_amount = ?
1354 my $from_budgets = GetBudgetHierarchy($from_budget_period_id);
1355 for my $from_budget (@$from_budgets) {
1356 my $new_budget_id = $dbh->selectcol_arrayref(
1360 WHERE budget_period_id = ?
1362 |, {}, $to_budget_period_id, $from_budget->{budget_code}
1364 $new_budget_id = $new_budget_id->[0];
1365 my $new_budget = GetBudget( $new_budget_id );
1366 unless ( $new_budget ) {
1370 budget => $from_budget,
1371 error => 'budget_code_not_exists',
1375 my $orders_to_move = C4::Acquisition::SearchOrders(
1377 budget_id => $from_budget->{budget_id},
1383 for my $order (@$orders_to_move) {
1384 $sth_update_aqorders->execute( $new_budget->{budget_id}, $order->{ordernumber} );
1385 push @orders_moved, $order;
1388 my $unspent_moved = 0;
1389 if ($move_remaining_unspent) {
1390 my $spent = GetBudgetHierarchySpent( $from_budget->{budget_id} );
1391 my $unspent = $from_budget->{budget_amount} - $spent;
1392 my $new_budget_amount = $new_budget->{budget_amount};
1393 if ( $unspent > 0 ) {
1394 $new_budget_amount += $unspent;
1395 $unspent_moved = $unspent;
1397 $new_budget->{budget_amount} = $new_budget_amount;
1398 $sth_update_budget_amount->execute( $new_budget_amount,
1399 $new_budget->{budget_id} );
1404 budget => $new_budget,
1405 orders_moved => \@orders_moved,
1407 unspent_moved => $unspent_moved,
1413 =head2 FieldsForCalculatingFundValues
1415 my ( $unitprice_field, $ecost_field ) = FieldsForCalculatingFundValues();
1417 Fetch the tax inclusive or tax exclusive database fields for calculating fund values based
1418 on the value of the CalculateFundValuesIncludingTax system preference.
1422 sub FieldsForCalculatingFundValues {
1424 # Choose correct unitprice and ecost fields
1425 my $unitprice_field = 'unitprice_tax_included';
1426 my $ecost_field = 'ecost_tax_included';
1428 if ( !C4::Context->preference('CalculateFundValuesIncludingTax') ) {
1429 $unitprice_field = 'unitprice_tax_excluded';
1430 $ecost_field = 'ecost_tax_excluded';
1433 return ( $unitprice_field, $ecost_field);
1436 END { } # module clean-up code here (global destructor)
1443 Koha Development Team <http://koha-community.org/>