bug 2473: correct display of items.content
[koha_fer] / admin / aqbookfund.pl
index 56c74f5..d5673c8 100755 (executable)
@@ -52,21 +52,23 @@ C<op> can be equal to:
 
 use strict;
 use CGI;
-use HTML::Template;
 use List::Util qw/min/;
+use C4::Branch; # GetBranches
 use C4::Auth;
 use C4::Koha;
 use C4::Context;
 use C4::Bookfund;
 use C4::Output;
-use C4::Interface::CGI::Output;
-use C4::Search;
-use C4::Date;
+use C4::Dates;
+use C4::Debug;
+
+# use Smart::Comments;
 
 my $dbh = C4::Context->dbh;
 my $input = new CGI;
 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
 my $bookfundid=$input->param('bookfundid');
+my $branchcodeid=$input->param('branchcode')|'';
 my $pagesize = 10;
 my $op = $input->param('op') || '';
 
@@ -76,7 +78,7 @@ my ($template, $borrowernumber, $cookie)
          query => $input,
          type => "intranet",
          authnotrequired => 0,
-         flagsrequired => {parameters => 1, management => 1},
+         flagsrequired => {parameters => 1},
          debug => 1,
         }
     );
@@ -95,18 +97,19 @@ $template->param(action => $script_name);
 
 my $branches = GetBranches;
 
-################## ADD_FORM ##################################
+#-----############# ADD_FORM ##################################
 # called by default. Used to create form to add or  modify a record
 if ($op eq 'add_form') {
        #---- if primkey exists, it's a modify action, so read values to modify...
        my $dataaqbookfund;
        my $header;
        if ($bookfundid) {
-       $dataaqbookfund = GetBookFund($bookfundid);
+       $dataaqbookfund = GetBookFund($bookfundid,$branchcodeid);
        }
        if ($bookfundid) {
            $header = "Modify book fund";
            $template->param('header-is-modify-p' => 1);
+           $template->param('current_branch' =>  $branchcodeid);
        } else {
            $header = "Add book fund";
            $template->param('header-is-add-p' => 1);
@@ -141,50 +144,61 @@ if ($op eq 'add_form') {
 
 } # END $OP eq ADD_FORM
 
-################## ADD_VALIDATE ##################################
+#-----############# ADD_VALIDATE ##################################
 # called by add_form, used to insert/modify data in DB
 elsif ($op eq 'add_validate') {
+### add
        my $bookfundid = uc $input->param('bookfundid');
-
-    my $number = Countbookfund($bookfundid);
-
-    my $bookfund_already_exists = $number > 0 ? 1 : 0;
-
-    if ($bookfund_already_exists) {
         my $bookfundname = $input->param('bookfundname');
         my $branchcode = $input->param('branchcode') || undef;
 
-        ModBookFund($bookfundname,$branchcode,$bookfundid);
-    }
-    else {
+    my $number = Countbookfund($bookfundid,$branchcodeid);
+    if ($number == 0 ) {
+
         NewBookFund(
             $bookfundid,
             $input->param('bookfundname'),
-            $input->param('branchcode')
+            $input->param('branchcode')||''
         );
     }
     $input->redirect('aqbookfund.pl');
 # END $OP eq ADD_VALIDATE
 }
-################## DELETE_CONFIRM ##################################
-# called by default form, used to confirm deletion of data in DB
 
+#-----############# MOD_VALIDATE ##################################
+# called by add_form, used to insert/modify data in DB
+elsif ($op eq 'mod_validate') {
+       my $bookfundid  = uc $input->param('bookfundid');
+       my $bookfundname   = $input->param('bookfundname');
+       my $branchcode     = $input->param('branchcode'    ) || undef;
+       my $current_branch = $input->param('current_branch') || undef;
+       $debug and warn "$bookfundid, $bookfundname, $branchcode";
+
+       my $number = Countbookfund($bookfundid,$branchcodeid);
+    if ($number < 2)  {
+         $debug and warn "name :$bookfundname branch:$branchcode";
+        ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode);
+    }
+   $input->redirect('aqbookfund.pl');
+}
+
+#-----############# DELETE_CONFIRM ##################################
+# called by default form, used to confirm deletion of data in DB
 elsif ($op eq 'delete_confirm') {
-    my $data = GetBookFund($bookfundid);
+    my $data = GetBookFund($bookfundid,$branchcodeid);
        $template->param(bookfundid => $bookfundid);
        $template->param(bookfundname => $data->{'bookfundname'});
+       $template->param(branchcode => $data->{'branchcode'});
 } # END $OP eq DELETE_CONFIRM
 
-
-################## DELETE_CONFIRMED ##################################
+#-----############# DELETE_CONFIRMED ##################################
 # called by delete_confirm, used to effectively confirm deletion of data in DB
 elsif ($op eq 'delete_confirmed') {
-    DelBookFund(uc($input->param('bookfundid')));
+    DelBookFund(uc($input->param('bookfundid')),$branchcodeid);
 
 }# END $OP eq DELETE_CONFIRMED
 
-
-################## DEFAULT ##################################
+#-----############# DEFAULT ##################################
 else { # DEFAULT
     my ($query, $sth);
 
@@ -197,17 +211,15 @@ else { # DEFAULT
             code => $branchcode,
             name => $branches->{$branchcode}->{branchname},
         };
-
         if (defined $input->param('filter_branchcode')
             and $input->param('filter_branchcode') eq $branchcode) {
             $row->{selected} = 1;
         }
-
         push @branchloop, $row;
     }
 
     my @bookfundids_loop;
-    my $sth = GetBookFundsId();
+    $sth = GetBookFundsId();
 
     while (my $row = $sth->fetchrow_hashref) {
         if (defined $input->param('filter_bookfundid') and $input->param('filter_bookfundid') eq $row->{bookfundid}){
@@ -232,7 +244,7 @@ else { # DEFAULT
 
     # does the book funds have budgets?
     my @loop_id;
-    my $sth = GetBookFundsId();
+    $sth = GetBookFundsId();
     while (my $row = $sth->fetchrow){
         push @loop_id, $row;
     }
@@ -280,10 +292,4 @@ else { # DEFAULT
             )
         );
 } #---- END $OP eq DEFAULT
-$template->param(
-    intranetcolorstylesheet =>C4::Context->preference("intranetcolorstylesheet"),
-    intranetstylesheet => C4::Context->preference("intranetstylesheet"),
-    IntranetNav => C4::Context->preference("IntranetNav"),
-    );
-
 output_html_with_http_headers $input, $cookie, $template->output;