budgfixing budgets & planning and code cleaning
authorPaul Poulain <paul.poulain@biblibre.com>
Fri, 5 Jun 2009 06:01:46 +0000 (08:01 +0200)
committerHenri-Damien LAURENT <henridamien.laurent@biblibre.com>
Wed, 30 Sep 2009 09:30:25 +0000 (11:30 +0200)
* fix a bug for new orders budget : the Asort1 was displayed by default. Now, it's the sortN_authcat of the 1st budget (the one selected by default)
* change in GetBudgetAuthCats : returns all auth values used by at least one budget (not Asort* categories, as previously)
* code cleaning in GetAuthvalueDropbox (and select aqbudgets.* instead of *, to avoid data corruption due to duplicate column name)

C4/Budgets.pm
acqui/neworderempty.pl
admin/aqbudgets.pl
admin/aqplan.pl

index b739303..ddb65cc 100644 (file)
@@ -365,25 +365,22 @@ sub GetAuthcatDropbox  {
 
 # -------------------------------------------------------------------
 sub GetBudgetAuthCats  {
-       my @auth_cats;
-       my $value;
-       my $dbh = C4::Context->dbh;
-       my $sth = $dbh->prepare(
-               "SELECT distinct(category)
-            FROM authorised_values where category like 'Asort%'
-            ORDER BY category"
-       );
-       $sth->execute();
-    while ( my $value = $sth->fetchrow_array ) {
-        push @auth_cats, $value;
+    my ($budget_period_id) = shift;
+    # now, populate the auth_cats_loop used in the budget planning button
+    # we must retrieve all auth values used by at least one budget
+    my $dbh = C4::Context->dbh;
+    my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?");
+    $sth->execute($budget_period_id);
+    my %authcats;
+    while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) {
+        $authcats{$sort1_authcat}=1;
+        $authcats{$sort2_authcat}=1;
     }
-    my @loop_data = ();    # initialize an array to hold your loop
-    while (@auth_cats) {
-        my %row_data;      # get a fresh hash for the row data
-        $row_data{authcat} = shift @auth_cats;
-        push( @loop_data, \%row_data );
+    my @auth_cats_loop;
+    foreach (sort keys %authcats) {
+        push @auth_cats_loop,{ authcat => $_ };
     }
-    return @loop_data;
+    return \@auth_cats_loop;
 }
 
 # -------------------------------------------------------------------
@@ -545,7 +542,7 @@ sub GetBudgetHierarchy {
        my @bind_params;
        my $dbh   = C4::Context->dbh;
        my $query = qq|
-                    SELECT *
+                    SELECT aqbudgets.*
                     FROM aqbudgets
                     JOIN aqbudgetperiods USING (budget_period_id)
                     WHERE budget_period_active=1 |;
@@ -654,20 +651,13 @@ sub GetBudgetHierarchy {
 
         $r->{'budget_spent'}       = GetBudgetSpent( $r->{'budget_id'} );
 
-#        $budget->{'budget_alloc'}       = sprintf( "%.2f", $budget->{'budget_alloc'} - $budget->{'budget_amount  alloc'} );
-#        $budget->{'budget_alloc'} = sprintf( "%.2f", $budget->{'budget_alloc'} );
-
         $r->{'budget_amount_total'} =  $r->{'budget_amount'} + $r->{'budget_amount_sublevel'}  ;
-#          $r->{budget_alloc} = $r->{'budget_amount'} - $r->{'budget_amount_sublevel'}  ;
-
-         #  $r->{'budget_amount_sublevel'}  ;
 
         # foreach sub-levels
         my $unalloc_count ;
 
                foreach my $sub (@subs_arr) {
                        my $sub_budget = GetBudget($sub);
-                       # $r->{budget_spent_sublevel} += $bud->{'budget_amount'} ;
 
                        $r->{budget_spent_sublevel} +=    GetBudgetSpent( $sub_budget->{'budget_id'} );
                        $unalloc_count +=   $sub_budget->{'budget_amount'} + $sub_budget->{'budget_amount_sublevel'};
@@ -675,25 +665,6 @@ sub GetBudgetHierarchy {
 
            $r->{budget_unalloc_sublevel} =  $r->{'budget_amount_sublevel'}   -   $unalloc_count;
 
-        #                (($r->{'budget_amount'} - $r->{'budget_alloc'}) /  $r->{'budget_amount'}) * 100;
-
-=c
-#        my $percent =     $r->{'budget_amount'}  ? (  $r->{'budget_alloc'} / $r->{'budget_amount'} ) * 100 :  0;
- #       my $spent_percent = ( $r->{'budget_spent'} / $r->{'budget_amount'} ) * 100 if $r->{'budget_amount'};
-
-        #                (($r->{'budget_amount'} - $r->{'budget_alloc'}) /  $r->{'budget_amount'}) * 100;
-#        my $percent = ( $r->{'budget_alloc'} / $r->{'budget_amount'} ) * 100 if $r->{'budget_amount'};
-#        my $spent_percent = ( $r->{'budget_spent'} / $r->{'budget_amount'} ) * 100 if $r->{'budget_amount'};
-               if ($percent == 0) {
-                       $r->{budget_alloc_none} = 1;
-               } elsif ($percent == 100) {
-                       $r->{budget_alloc_full} = 1
-
-               } else {
-                       $r->{budget_alloc_percent} =    sprintf("%00d", $percent);
-               }
-=cut
-
         if ( scalar  @subs_arr == 0  && $r->{budget_amount_sublevel} > 0 ) {
             $r->{warn_no_subs} = 1;
         }
index 32b70a5..e475b17 100755 (executable)
@@ -262,7 +262,7 @@ if ($budget) {    # its a mod ..
         $CGIsort1 = GetAuthvalueDropbox( 'sort1', $budget->{'sort1_authcat'}, $data->{'sort1'} );
     }
 } else {
-    $CGIsort1 = GetAuthvalueDropbox( 'sort1', 'Asort1', '' );
+    $CGIsort1 = GetAuthvalueDropbox( 'sort1', @$budgets[0]->{'sort1_authcat'}, '' );
 }
 
 # if CGIsort is successfully fetched, the use it
@@ -279,7 +279,7 @@ if ($budget) {
         $CGIsort2 = GetAuthvalueDropbox( 'sort2', $budget->{'sort2_authcat'}, $data->{'sort2'} );
     }
 } else {
-    $CGIsort2 = GetAuthvalueDropbox( 'sort2', 'Asort2', '' );
+    $CGIsort2 = GetAuthvalueDropbox( 'sort2', @$budgets[0]->{sort2_authcat}, '' );
 }
 if ($CGIsort2) {
     $template->param( CGIsort2 => $CGIsort2 );
@@ -289,25 +289,25 @@ if ($CGIsort2) {
 
 
 
-#do a biblioitems lookup on bib
-my @bibitems = GetBiblioItemByBiblioNumber($biblionumber);
-my $bibitemscount = scalar @bibitems;
-
-if ( $bibitemscount > 0 ) {
-    # warn "NEWBIBLIO: bibitems for $biblio exists\n";
-    my @bibitemloop;
-    for ( my $i = 0 ; $i < $bibitemscount ; $i++ ) {
-        my %line;
-        $line{biblioitemnumber} = $bibitems[$i]->{'biblioitemnumber'};
-        $line{isbn}             = $bibitems[$i]->{'isbn'};
-        $line{itemtype}         = $bibitems[$i]->{'itemtype'};
-        $line{volumeddesc}      = $bibitems[$i]->{'volumeddesc'};
-        push( @bibitemloop, \%line );
-
-        $template->param( bibitemloop => \@bibitemloop );
-    }
-    $template->param( bibitemexists => "1" );
-}
+# #do a biblioitems lookup on bib
+my @bibitems = GetBiblioItemByBiblioNumber($biblionumber);
+my $bibitemscount = scalar @bibitems;
+# 
+if ( $bibitemscount > 0 ) {
+    # warn "NEWBIBLIO: bibitems for $biblio exists\n";
+    my @bibitemloop;
+    for ( my $i = 0 ; $i < $bibitemscount ; $i++ ) {
+        my %line;
+        $line{biblioitemnumber} = $bibitems[$i]->{'biblioitemnumber'};
+        $line{isbn}             = $bibitems[$i]->{'isbn'};
+        $line{itemtype}         = $bibitems[$i]->{'itemtype'};
+        $line{volumeddesc}      = $bibitems[$i]->{'volumeddesc'};
+        push( @bibitemloop, \%line );
+# 
+        $template->param( bibitemloop => \@bibitemloop );
+    }
+    $template->param( bibitemexists => "1" );
+}
 
 if (C4::Context->preference('AcqCreateItem') eq 'ordering' && !$ordnum) {
     # prepare empty item form
index 8e059b1..f307e58 100755 (executable)
@@ -146,6 +146,8 @@ foreach my $thisbranch (keys %$branches) {
     push @branchloop2, \%row;
 }
 
+$template->param(auth_cats_loop => GetBudgetAuthCats($budget_period_id) );
+
 # Used to create form to add or  modify a record
 if ($op eq 'add_form') {
 #### ------------------- ADD_FORM -------------------------
@@ -215,7 +217,7 @@ if ($op eq 'add_form') {
 
     my $budget_perm_dropbox =
     GetBudgetPermDropbox($budget->{'budget_permission'});
-
+    
     # if no buget_id is passed then its an add
     $template->param(
         add_form                  => 1,
index 787cc01..cb17f0e 100755 (executable)
@@ -65,7 +65,7 @@ $template->param( period_button_only => 1 ) if $count == 0;
 
 
 # authcats_loop populates the YUI planning button
-my @auth_cats_loop            = GetBudgetAuthCats();
+my $auth_cats_loop            = GetBudgetAuthCats($budget_period_id);
 my $budget_period_id          = $period->{'budget_period_id'};
 my $budget_period_startdate   = $period->{'budget_period_startdate'};
 my $budget_period_enddate     = $period->{'budget_period_enddate'};
@@ -73,15 +73,13 @@ my $budget_period_locked      = $period->{'budget_period_locked'};
 my $budget_period_description = $period->{'budget_period_description'};
 my $budget_period_dropbox     = GetBudgetPeriodsDropbox($budget_period_id );
 
-    ## ## @auth_cats_loop
-
 
 $template->param(
     budget_period_id          => $budget_period_id,
     budget_period_locked      => $budget_period_locked,
     budget_period_description => $budget_period_description,
     budget_period_dropbox     => $budget_period_dropbox,
-    auth_cats_loop            => \@auth_cats_loop,
+    auth_cats_loop            => $auth_cats_loop,
 );
 
 # ------- get periods stuff ------------------