Bug 18536: [QA Follow-up] Tiny regex simplification
[koha_ffzg] / serials / subscription-add.pl
index fa71236..ce7f168 100755 (executable)
@@ -23,16 +23,16 @@ use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days Add_Delta_YM);
 use C4::Koha;
 use C4::Biblio;
 use C4::Auth;
-use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Acquisition;
 use C4::Output;
 use C4::Context;
-use C4::Branch; # GetBranches
 use C4::Serials;
 use C4::Serials::Frequency;
 use C4::Serials::Numberpattern;
 use C4::Letters;
 use Koha::AdditionalField;
+use Koha::DateUtils;
+use Koha::ItemTypes;
 use Carp;
 
 #use Smart::Comments;
@@ -42,7 +42,6 @@ my $op = $query->param('op') || '';
 my $dbh = C4::Context->dbh;
 my $sub_length;
 
-my @budgets;
 
 # Permission needed if it is a modification : edit_subscription
 # Permission needed otherwise (nothing or dup) : create_subscription
@@ -127,34 +126,15 @@ if ($op eq 'modify' || $op eq 'dup' || $op eq 'modsubscription') {
 
 }
 
-my $onlymine =
-     C4::Context->preference('IndependentBranches')
-  && C4::Context->userenv
-  && !C4::Context->IsSuperLibrarian
-  && (
-    not C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'superserials' } )
-  )
-  && C4::Context->userenv->{branch};
-my $branches = GetBranches($onlymine);
-my $branchloop;
-for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
-    my $selected = 0;
-    $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
-    push @{$branchloop}, {
-        value => $thisbranch,
-        selected => $selected,
-        branchname => $branches->{$thisbranch}->{'branchname'},
-    };
-}
-
-my $locations_loop = GetAuthorisedValues("LOC",$subs->{'location'});
+my $locations_loop = GetAuthorisedValues("LOC");
 
-$template->param(branchloop => $branchloop,
+$template->param(
+    branchcode => $subs->{branchcode},
     locations_loop=>$locations_loop,
 );
 
 
-my $additional_fields = Koha::AdditionalField->all( { table => 'subscription' } );
+my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
 for my $field ( @$additional_fields ) {
     if ( $field->{authorised_value_category} ) {
         $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
@@ -162,7 +142,23 @@ for my $field ( @$additional_fields ) {
 }
 $template->param( additional_fields_for_subscription => $additional_fields );
 
+my $typeloop = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
+
+# FIXME We should use the translated_description for item types
+my @typearg =
+    map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{itemtype} and $_ eq $subs->{itemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
+my @previoustypearg =
+    map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{previousitemtype} and $_ eq $subs->{previousitemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
+
+$template->param(
+    typeloop                 => \@typearg,
+    previoustypeloop         => \@previoustypearg,
+    locations_loop=>$locations_loop,
+);
+
 # prepare template variables common to all $op conditions:
+$template->param('makePreviousSerialAvailable' => 1) if (C4::Context->preference('makePreviousSerialAvailable'));
+
 if ($op!~/^mod/) {
     my $letters = get_letter_loop();
     $template->param( letterloop => $letters );
@@ -237,6 +233,7 @@ if ($op eq 'addsubscription') {
 
 sub get_letter_loop {
     my ($selected_lettercode) = @_;
+    $selected_lettercode //= '';
     my $letters = GetLetters({ module => 'serial' });
     return [
         map {
@@ -294,7 +291,7 @@ sub redirect_add_subscription {
     my $cost           = $query->param('cost');
     my $aqbudgetid     = $query->param('aqbudgetid');
     my $periodicity    = $query->param('frequency');
-    my @irregularity   = $query->param('irregularity');
+    my @irregularity   = $query->multi_param('irregularity');
     my $numberpattern  = $query->param('numbering_pattern');
     my $locale         = $query->param('locale');
     my $graceperiod    = $query->param('graceperiod') || 0;
@@ -321,15 +318,19 @@ sub redirect_add_subscription {
     my $staffdisplaycount = $query->param('staffdisplaycount');
     my $opacdisplaycount  = $query->param('opacdisplaycount');
     my $location          = $query->param('location');
+    my $itemtype          = $query->param('itemtype');
+    my $previousitemtype  = $query->param('previousitemtype');
     my $skip_serialseq    = $query->param('skip_serialseq');
-    my $startdate = format_date_in_iso( $query->param('startdate') );
-    my $enddate = format_date_in_iso( $query->param('enddate') );
-    my $firstacquidate  = format_date_in_iso($query->param('firstacquidate'));
+
+    my $startdate      = output_pref( { str => scalar $query->param('startdate'),      dateonly => 1, dateformat => 'iso' } );
+    my $enddate        = output_pref( { str => scalar $query->param('enddate'),        dateonly => 1, dateformat => 'iso' } );
+    my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
+
     if(!defined $enddate || $enddate eq '') {
         if($subtype eq "issues") {
-            $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
+            $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
         } else {
-            $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
+            $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
         }
     }
 
@@ -344,15 +345,8 @@ sub redirect_add_subscription {
         $skip_serialseq
     );
 
-    my $additional_fields = Koha::AdditionalField->all( { table => 'subscription' } );
-    my @additional_field_values;
-    for my $field ( @$additional_fields ) {
-        my $af = Koha::AdditionalField->new({ id => $field->{id} });
-        $af->{values} = {
-            $subscriptionid => $query->param('additional_fields_' . $field->{name})
-        };
-        $af->insert_values;
-    }
+    my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
+    insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
 
     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
     return;
@@ -360,20 +354,24 @@ sub redirect_add_subscription {
 
 sub redirect_mod_subscription {
     my $subscriptionid = $query->param('subscriptionid');
-    my @irregularity = $query->param('irregularity');
+    my @irregularity = $query->multi_param('irregularity');
     my $auser = $query->param('user');
-    my $librarian => $query->param('librarian'),
+    my $librarian => scalar $query->param('librarian'),
     my $branchcode = $query->param('branchcode');
     my $cost = $query->param('cost');
     my $aqbooksellerid = $query->param('aqbooksellerid');
     my $biblionumber = $query->param('biblionumber');
     my $aqbudgetid = $query->param('aqbudgetid');
-    my $startdate = format_date_in_iso($query->param('startdate'));
-    my $firstacquidate = format_date_in_iso( $query->param('firstacquidate') );
-    my $nextacquidate = $query->param('nextacquidate') ?
-                            format_date_in_iso($query->param('nextacquidate')):
-                            $firstacquidate;
-    my $enddate = format_date_in_iso($query->param('enddate'));
+
+    my $startdate      = output_pref( { str => scalar $query->param('startdate'),      dateonly => 1, dateformat => 'iso' } );
+    my $enddate        = output_pref( { str => scalar $query->param('enddate'),        dateonly => 1, dateformat => 'iso' } );
+    my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
+
+    my $nextacquidate  = $query->param('nextacquidate');
+    $nextacquidate = $nextacquidate
+        ? output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } )
+        : $firstacquidate;
+
     my $periodicity = $query->param('frequency');
 
     my $subtype = $query->param('subtype');
@@ -399,6 +397,8 @@ sub redirect_mod_subscription {
        my $opacdisplaycount = $query->param('opacdisplaycount');
     my $graceperiod     = $query->param('graceperiod') || 0;
     my $location = $query->param('location');
+    my $itemtype          = $query->param('itemtype');
+    my $previousitemtype  = $query->param('previousitemtype');
     my $skip_serialseq    = $query->param('skip_serialseq');
 
     # Guess end date
@@ -426,29 +426,35 @@ sub redirect_mod_subscription {
         $status, $biblionumber, $callnumber, $notes, $letter,
         $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
         $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
-        $skip_serialseq
+        $skip_serialseq, $itemtype, $previousitemtype
     );
 
-    my $additional_fields = Koha::AdditionalField->all( { table => 'subscription' } );
-    my @additional_field_values;
+    my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
+    insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
+
+    print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
+    return;
+}
+
+sub insert_additional_fields {
+    my ( $additional_fields, $biblionumber, $subscriptionid ) = @_;
+    my $record = GetMarcBiblio( $biblionumber, 1 );
     for my $field ( @$additional_fields ) {
         my $af = Koha::AdditionalField->new({ id => $field->{id} })->fetch;
         if ( $af->{marcfield} ) {
-            my $record = GetMarcBiblio( $biblionumber, 1 );
             my ( $field, $subfield ) = split /\$/, $af->{marcfield};
-            next unless $field and $subfield;
-            my $value = $record->subfield( $field, $subfield );
-            $af->{values} = {
-                $subscriptionid => $value
-            };
+            $af->{values} = undef;
+            if ( $field and $subfield ) {
+                my $value = $record->subfield( $field, $subfield );
+                $af->{values} = {
+                    $subscriptionid => $value
+                };
+            }
         } else {
             $af->{values} = {
-                $subscriptionid => $query->param('additional_fields_' . $field->{name})
-            };
+                $subscriptionid => scalar $query->param('additional_field_' . $field->{id})
+            } if defined $query->param('additional_field_' . $field->{id});
         }
         $af->insert_values;
     }
-
-    print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
-    return;
 }