Bug 17047: subscriptions management with Mana-KB
[koha-ffzg.git] / serials / subscription-add.pl
index fb7e797..71166b8 100755 (executable)
@@ -35,6 +35,10 @@ use Koha::DateUtils;
 use Koha::ItemTypes;
 use Carp;
 
+use Koha::Subscription::Numberpattern;
+use Koha::Subscription::Frequency;
+use Koha::SharedContent;
+
 #use Smart::Comments;
 
 our $query = CGI->new;
@@ -288,15 +292,61 @@ sub _guess_enddate {
     return $enddate;
 }
 
+sub manage_subscription_numbering_pattern_id {
+    my $params;
+    if ( $query->param('numbering_pattern') eq 'mana' ) {
+        foreach (qw/numberingmethod label1 add1 every1 whenmorethan1 setto1
+                   numbering1 label2 add2 every2 whenmorethan2 setto2 numbering2
+                   label3 add3 every3 whenmorethan3 setto3 numbering3/) {
+            $params->{$_} = $query->param($_) if $query->param($_);
+        }
+
+        my $existing = Koha::Subscription::Numberpatterns->search($params)->next();
+
+        if ($existing) {
+            return $existing->id;
+        }
+
+        $params->{label} = Koha::Subscription::Numberpattern->uniqueLabel($query->param('patternname'));
+        $params->{description} = $query->param('sndescription');
+
+
+        my $subscription_np = Koha::Subscription::Numberpattern->new()->set($params)->store();
+        return $subscription_np->id;
+    }
+
+    return $query->param('numbering_pattern');
+}
+
+sub manage_subscription_frequencies_id {
+    my $periodicity;
+    if ( $query->param('frequency') eq 'mana' ) {
+        my $subscription_freq = Koha::Subscription::Frequency->new()->set(
+            {
+                description   => $query->param('sfdescription'),
+                unit          => $query->param('unit'),
+                unitsperissue => $query->param('unitsperissue'),
+                issuesperunit => $query->param('issuesperunit'),
+            }
+        )->store();
+        $periodicity = $subscription_freq->id;
+    }
+    else {
+        $periodicity = $query->param('frequency');
+    }
+    return $periodicity;
+}
+
 sub redirect_add_subscription {
+    my $periodicity = manage_subscription_frequencies_id();
+    my $numberpattern = manage_subscription_numbering_pattern_id();
+
     my $auser          = $query->param('user');
     my $branchcode     = $query->param('branchcode');
     my $aqbooksellerid = $query->param('aqbooksellerid');
     my $cost           = $query->param('cost');
     my $aqbudgetid     = $query->param('aqbudgetid');
-    my $periodicity    = $query->param('frequency');
     my @irregularity   = $query->multi_param('irregularity');
-    my $numberpattern  = $query->param('numbering_pattern');
     my $locale         = $query->param('locale');
     my $graceperiod    = $query->param('graceperiod') || 0;
 
@@ -326,6 +376,15 @@ sub redirect_add_subscription {
     my $previousitemtype  = $query->param('previousitemtype');
     my $skip_serialseq    = $query->param('skip_serialseq');
 
+    my $mana_id;
+    if ( $query->param('mana_id') ne "" ) {
+        $mana_id = $query->param('mana_id');
+        Koha::SharedContent::manaNewUserPatchRequest("subscription",$mana_id);
+    }
+    else {
+        $mana_id = undef;
+    }
+
     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' } );
@@ -337,7 +396,6 @@ sub redirect_add_subscription {
             $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
         }
     }
-
     my $subscriptionid = NewSubscription(
         $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
         $startdate, $periodicity, $numberlength, $weeklength,
@@ -346,7 +404,7 @@ sub redirect_add_subscription {
         join(";",@irregularity), $numberpattern, $locale, $callnumber,
         $manualhistory, $internalnotes, $serialsadditems,
         $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
-        $skip_serialseq, $itemtype, $previousitemtype
+        $skip_serialseq, $itemtype, $previousitemtype, $mana_id
     );
 
     my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
@@ -376,13 +434,13 @@ sub redirect_mod_subscription {
         ? output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } )
         : $firstacquidate;
 
-    my $periodicity = $query->param('frequency');
+    my $periodicity = manage_subscription_frequencies_id();
+    my $numberpattern = manage_subscription_numbering_pattern_id();
 
     my $subtype = $query->param('subtype');
     my $sublength = $query->param('sublength');
     my ($numberlength, $weeklength, $monthlength)
         = _get_sub_length( $subtype, $sublength );
-    my $numberpattern = $query->param('numbering_pattern');
     my $locale = $query->param('locale');
     my $lastvalue1 = $query->param('lastvalue1');
     my $innerloop1 = $query->param('innerloop1');
@@ -405,6 +463,15 @@ sub redirect_mod_subscription {
     my $previousitemtype  = $query->param('previousitemtype');
     my $skip_serialseq    = $query->param('skip_serialseq');
 
+    my $mana_id;
+    if ( defined( $query->param('mana_id') ) ) {
+        $mana_id = $query->param('mana_id');
+        Koha::SharedContent::manaNewUserPatchRequest("subscription",$mana_id);
+    }
+    else {
+        $mana_id = undef;
+    }
+
     # Guess end date
     if(!defined $enddate || $enddate eq '') {
         if($subtype eq "issues") {
@@ -430,7 +497,7 @@ sub redirect_mod_subscription {
         $status, $biblionumber, $callnumber, $notes, $letter,
         $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
         $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
-        $skip_serialseq, $itemtype, $previousitemtype
+        $skip_serialseq, $itemtype, $previousitemtype, $mana_id
     );
 
     my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );