Spanish and Turkish updates
[koha.git] / admin / smart-rules.pl
index 6e9f371..2be5364 100755 (executable)
@@ -181,6 +181,86 @@ elsif ($op eq "add-branch-cat") {
         }
     }
 }
+elsif ($op eq "add-branch-item") {
+    my $itemtype  = $input->param('itemtype');
+    my $holdallowed   = $input->param('holdallowed');
+    $holdallowed =~ s/\s//g;
+    $holdallowed = undef if $holdallowed !~ /^\d+/;
+
+    if ($branch eq "*") {
+        if ($itemtype eq "*") {
+            my $sth_search = $dbh->prepare("SELECT count(*) AS total
+                                            FROM default_circ_rules");
+            my $sth_insert = $dbh->prepare("INSERT INTO default_circ_rules
+                                            (holdallowed)
+                                            VALUES (?)");
+            my $sth_update = $dbh->prepare("UPDATE default_circ_rules
+                                            SET holdallowed = ?");
+
+            $sth_search->execute();
+            my $res = $sth_search->fetchrow_hashref();
+            if ($res->{total}) {
+                $sth_update->execute($holdallowed);
+            } else {
+                $sth_insert->execute($holdallowed);
+            }
+        } else {
+            my $sth_search = $dbh->prepare("SELECT count(*) AS total
+                                            FROM default_branch_item_rules
+                                            WHERE itemtype = ?");
+            my $sth_insert = $dbh->prepare("INSERT INTO default_branch_item_rules
+                                            (itemtype, holdallowed)
+                                            VALUES (?, ?)");
+            my $sth_update = $dbh->prepare("UPDATE default_branch_item_rules
+                                            SET holdallowed = ?
+                                            WHERE itemtype = ?");
+            $sth_search->execute($itemtype);
+            my $res = $sth_search->fetchrow_hashref();
+            if ($res->{total}) {
+                $sth_update->execute($holdallowed, $itemtype);
+            } else {
+                $sth_insert->execute($itemtype, $holdallowed);
+            }
+        }
+    } elsif ($itemtype eq "*") {
+        my $sth_search = $dbh->prepare("SELECT count(*) AS total
+                                        FROM default_branch_circ_rules
+                                        WHERE branchcode = ?");
+        my $sth_insert = $dbh->prepare("INSERT INTO default_branch_circ_rules
+                                        (branchcode, holdallowed)
+                                        VALUES (?, ?)");
+        my $sth_update = $dbh->prepare("UPDATE default_branch_circ_rules
+                                        SET holdallowed = ?
+                                        WHERE branchcode = ?");
+        $sth_search->execute($branch);
+        my $res = $sth_search->fetchrow_hashref();
+        if ($res->{total}) {
+            $sth_update->execute($holdallowed, $branch);
+        } else {
+            $sth_insert->execute($branch, $holdallowed);
+        }
+    } else {
+        my $sth_search = $dbh->prepare("SELECT count(*) AS total
+                                        FROM branch_item_rules
+                                        WHERE branchcode = ?
+                                        AND   itemtype = ?");
+        my $sth_insert = $dbh->prepare("INSERT INTO branch_item_rules
+                                        (branchcode, itemtype, holdallowed)
+                                        VALUES (?, ?, ?)");
+        my $sth_update = $dbh->prepare("UPDATE branch_item_rules
+                                        SET holdallowed = ?
+                                        WHERE branchcode = ?
+                                        AND itemtype = ?");
+
+        $sth_search->execute($branch, $itemtype);
+        my $res = $sth_search->fetchrow_hashref();
+        if ($res->{total}) {
+            $sth_update->execute($holdallowed, $branch, $itemtype);
+        } else {
+            $sth_insert->execute($branch, $itemtype, $holdallowed);
+        }
+    }
+}
 
 my $branches = GetBranches();
 my @branchloop;
@@ -204,7 +284,6 @@ $sth->finish;
 $sth=$dbh->prepare("SELECT description,itemtype FROM itemtypes ORDER BY description");
 $sth->execute;
 # $i=0;
-my $toggle= 1;
 my @row_loop;
 my @itemtypes;
 while (my $row=$sth->fetchrow_hashref){
@@ -286,7 +365,38 @@ foreach my $entry (@sorted_branch_cat_rules, @sorted_row_loop) {
     $entry->{unlimited_maxissueqty} = 1 unless defined($entry->{maxissueqty});
 }
 
+my $sth_branch_item;
+if ($branch eq "*") {
+    $sth_branch_item = $dbh->prepare("
+        SELECT default_branch_item_rules.*, itemtypes.description AS humanitemtype
+        FROM default_branch_item_rules
+        JOIN itemtypes USING (itemtype)
+    ");
+    $sth_branch_item->execute();
+} else {
+    $sth_branch_item = $dbh->prepare("
+        SELECT branch_item_rules.*, itemtypes.description AS humanitemtype
+        FROM branch_item_rules
+        JOIN itemtypes USING (itemtype)
+        WHERE branch_item_rules.branchcode = ?
+    ");
+    $sth_branch_item->execute($branch);
+}
+
+my @branch_item_rules = ();
+while (my $row = $sth_branch_item->fetchrow_hashref) {
+    push @branch_item_rules, $row;
+}
+my @sorted_branch_item_rules = sort { $a->{'humanitemtype'} cmp $b->{'humanitemtype'} } @branch_item_rules;
+
+# note undef holdallowed so that template can deal with them
+foreach my $entry (@sorted_branch_item_rules) {
+    $entry->{holdallowed_any} = 1 if($entry->{holdallowed} == 2);
+    $entry->{holdallowed_same} = 1 if($entry->{holdallowed} == 1);
+}
+
 $template->param(show_branch_cat_rule_form => 1);
+$template->param(branch_item_rule_loop => \@sorted_branch_item_rules);
 $template->param(branch_cat_rule_loop => \@sorted_branch_cat_rules);
 
 $template->param(categoryloop => \@category_loop,