Bug 7639: add option to forgive overdue fines when declaring items lost
[koha_fer] / tools / batchMod.pl
index 2c68bd3..1a804d9 100755 (executable)
@@ -34,6 +34,7 @@ use C4::ClassSource;
 use C4::Dates;
 use C4::Debug;
 use MARC::File::XML;
+use List::MoreUtils qw/uniq/;
 
 my $input = new CGI;
 my $dbh = C4::Context->dbh;
@@ -46,7 +47,7 @@ my $del_records  = $input->param('del_records');
 my $completedJobID = $input->param('completedJobID');
 my $runinbackground = $input->param('runinbackground');
 my $src          = $input->param('src');
-
+my $use_default_values = $input->param('use_default_values');
 
 my $template_name;
 my $template_flag;
@@ -77,8 +78,7 @@ my $itemrecord;
 my $nextop="";
 my @errors; # store errors found while checking data BEFORE saving item.
 my $items_display_hashref;
-my $frameworkcode="";
-my $tagslib = &GetMarcStructure(1,$frameworkcode);
+my $tagslib = &GetMarcStructure(1);
 
 my $deleted_items = 0;     # Number of deleted items
 my $deleted_records = 0;   # Number of deleted records ( with no items attached )
@@ -189,7 +189,7 @@ if ($op eq "action") {
                        UpdateMarcWith( $marcitem, $localmarcitem );
                        eval{
                             if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
-                                LostItem($itemnumber, 'MARK RETURNED', 'CHARGE FEE') if $item->{itemlost};
+                                LostItem($itemnumber, 'MARK RETURNED') if $item->{itemlost};
                             }
                         };
                    }
@@ -237,7 +237,7 @@ if ($op eq "show"){
             }
         }
         if ( my $list=$input->param('barcodelist')){
-            push my @barcodelist, split(/\s\n/, $list);
+            push my @barcodelist, uniq( split(/\s\n/, $list) );
 
             foreach my $barcode (@barcodelist) {
 
@@ -260,13 +260,18 @@ if ($op eq "show"){
     } else {
         $template->param("too_many_items" => scalar(@itemnumbers));
         # Even if we do not display the items, we need the itemnumbers
-        my @itemnumbers_hashref = map {{itemnumber => $_}} @itemnumbers;
-        $template->param("itemnumbers_hashref" => \@itemnumbers_hashref);
+        $template->param(itemnumbers_array => \@itemnumbers);
     }
 # now, build the item form for entering a new item
 my @loop_data =();
 my $i=0;
-my $authorised_values_sth = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib");
+my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
+my $query = qq{SELECT authorised_value, lib FROM authorised_values};
+$query  .= qq{ LEFT JOIN authorised_values_branches ON ( id = av_id ) } if $branch_limit;
+$query  .= qq{ WHERE category = ?};
+$query  .= qq{ AND ( branchcode = ? OR branchcode IS NULL ) } if $branch_limit;
+$query  .= qq{ GROUP BY lib ORDER BY lib, lib_opac};
+my $authorised_values_sth = $dbh->prepare( $query );
 
 my $branches = GetBranchesLoop();  # build once ahead of time, instead of multiple times later.
 
@@ -302,7 +307,7 @@ foreach my $tag (sort keys %{$tagslib}) {
        $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
        my ($x,$value);
        $value =~ s/"/"/g;
-       unless ($value) {
+   if ( !$value && $use_default_values) {
            $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
            # get today date & replace YYYY, MM, DD if provided in the default value
            my ( $year, $month, $day ) = split ',', $today_iso;     # FIXME: iso dates don't have commas!
@@ -311,7 +316,7 @@ foreach my $tag (sort keys %{$tagslib}) {
            $value =~ s/DD/$day/g;
        }
        $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
-       # testing branch value if IndependantBranches.
+    # testing branch value if IndependentBranches.
 
        my $attributes_no_value = qq(tabindex="1" id="$subfield_data{id}" name="field_value" class="input_marceditor" size="67" maxlength="255" );
        my $attributes          = qq($attributes_no_value value="$value" );
@@ -359,7 +364,7 @@ foreach my $tag (sort keys %{$tagslib}) {
       }
       else {
           push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
-          $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value} );
+          $authorised_values_sth->execute( $tagslib->{$tag}->{$subfield}->{authorised_value}, $branch_limit ? $branch_limit : () );
           while ( my ( $value, $lib ) = $authorised_values_sth->fetchrow_array ) {
               push @authorised_values, $value;
               $authorised_lib{$value} = $lib;
@@ -427,6 +432,8 @@ foreach my $tag (sort keys %{$tagslib}) {
     $i++
   }
 } # -- End foreach tag
+$authorised_values_sth->finish;
+
 
 
     # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
@@ -483,7 +490,7 @@ sub BuildItemsData{
                        foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
                                # loop through each subfield
                                my $itembranchcode=$field->subfield($branchtagsubfield);
-                               if ($itembranchcode && C4::Context->preference("IndependantBranches")) {
+                if ($itembranchcode && C4::Context->preference("IndependentBranches")) {
                                                #verifying rights
                                                my $userenv = C4::Context->userenv();
                                                unless (($userenv->{'flags'} == 1) or (($userenv->{'branch'} eq $itembranchcode))){