View | Details | Raw Unified | Return to bug 38061
Collapse All | Expand All

(-)a/serials/serials-collection.pl (-134 / +146 lines)
Lines 18-28 Link Here
18
# You should have received a copy of the GNU General Public License
18
# You should have received a copy of the GNU General Public License
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
20
21
22
use Modern::Perl;
21
use Modern::Perl;
23
use CGI qw ( -utf8 );
22
use CGI      qw ( -utf8 );
24
use C4::Auth qw( get_template_and_user );
23
use C4::Auth qw( get_template_and_user );
25
use C4::Serials qw( ModSerialStatus GetSubscription GetNextExpected GetNextSeq GetNextDate NewIssue HasSubscriptionExpired abouttoexpire check_routing GetFullSubscription PrepareSerialsData CountSubscriptionFromBiblionumber GetSubscriptionsFromBiblionumber GetFullSubscriptionsFromBiblionumber );
24
use C4::Serials
25
    qw( ModSerialStatus GetSubscription GetNextExpected GetNextSeq GetNextDate NewIssue HasSubscriptionExpired abouttoexpire check_routing GetFullSubscription PrepareSerialsData CountSubscriptionFromBiblionumber GetSubscriptionsFromBiblionumber GetFullSubscriptionsFromBiblionumber );
26
use C4::Output qw( output_and_exit output_html_with_http_headers );
26
use C4::Output qw( output_and_exit output_html_with_http_headers );
27
use C4::Context;
27
use C4::Context;
28
use Koha::Serial::Items;
28
use Koha::Serial::Items;
Lines 31-217 use Koha::DateUtils qw( dt_from_string ); Link Here
31
31
32
use List::MoreUtils qw( uniq );
32
use List::MoreUtils qw( uniq );
33
33
34
34
my $query               = CGI->new;
35
my $query = CGI->new;
35
my $op                  = $query->param('op') || q{};
36
my $op = $query->param('op') || q{};
36
my $nbissues            = $query->param('nbissues');
37
my $nbissues=$query->param('nbissues');
38
my $date_received_today = $query->param('date_received_today') || 0;
37
my $date_received_today = $query->param('date_received_today') || 0;
39
my $dbh = C4::Context->dbh;
38
my $dbh                 = C4::Context->dbh;
40
39
41
my ($template, $loggedinuser, $cookie)
40
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42
  = get_template_and_user({template_name => "serials/serials-collection.tt",
41
    {
43
                            query => $query,
42
        template_name => "serials/serials-collection.tt",
44
                            type => "intranet",
43
        query         => $query,
45
                            flagsrequired => {serials => '*'},
44
        type          => "intranet",
46
                            });
45
        flagsrequired => { serials => '*' },
47
my $biblionumber = $query->param('biblionumber');
46
    }
47
);
48
my $biblionumber   = $query->param('biblionumber');
48
my @subscriptionid = $query->multi_param('subscriptionid');
49
my @subscriptionid = $query->multi_param('subscriptionid');
49
my $skip_issues = $query->param('skip_issues') || 0;
50
my $skip_issues    = $query->param('skip_issues') || 0;
50
my $count_forward = $skip_issues + 1;
51
my $count_forward  = $skip_issues + 1;
51
52
52
@subscriptionid= uniq @subscriptionid;
53
@subscriptionid = uniq @subscriptionid;
53
@subscriptionid= sort @subscriptionid;
54
@subscriptionid = sort @subscriptionid;
54
my $subscriptiondescs;
55
my $subscriptiondescs;
55
my $subscriptions;
56
my $subscriptions;
56
57
57
if($op eq 'cud-gennext' && @subscriptionid){
58
if ( $op eq 'cud-gennext' && @subscriptionid ) {
58
    my $subscriptionid = $subscriptionid[0];
59
    my $subscriptionid = $subscriptionid[0];
59
    my $sth = $dbh->prepare("
60
    my $sth            = $dbh->prepare( "
60
        SELECT publisheddate, publisheddatetext, serialid, serialseq,
61
        SELECT publisheddate, publisheddatetext, serialid, serialseq,
61
            planneddate, notes, routingnotes
62
            planneddate, notes, routingnotes
62
        FROM serial
63
        FROM serial
63
        WHERE status = 1 AND subscriptionid = ?
64
        WHERE status = 1 AND subscriptionid = ?
64
    ");
65
    " );
65
    my $status = defined( $nbissues ) ? 2 : 3;
66
    my $status = defined($nbissues) ? 2 : 3;
66
    $nbissues ||= 1;
67
    $nbissues ||= 1;
67
    for ( my $i = 0; $i < $nbissues; $i++ ){
68
    for ( my $i = 0 ; $i < $nbissues ; $i++ ) {
68
        $sth->execute($subscriptionid);
69
        $sth->execute($subscriptionid);
70
69
        # modify actual expected issue, to generate the next
71
        # modify actual expected issue, to generate the next
70
        if ( my $issue = $sth->fetchrow_hashref ) {
72
        if ( my $issue = $sth->fetchrow_hashref ) {
71
            my $planneddate = $date_received_today ? dt_from_string : $issue->{planneddate};
73
            my $planneddate = $date_received_today ? dt_from_string : $issue->{planneddate};
72
            ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
74
            ModSerialStatus(
73
                    $planneddate, $issue->{publisheddate},
75
                $issue->{serialid},          $issue->{serialseq},
74
                    $issue->{publisheddatetext}, $status, "", $count_forward );
76
                $planneddate,                $issue->{publisheddate},
77
                $issue->{publisheddatetext}, $status, "", $count_forward
78
            );
75
        } else {
79
        } else {
76
            require C4::Serials::Numberpattern;
80
            require C4::Serials::Numberpattern;
77
            my $subscription = GetSubscription($subscriptionid);
81
            my $subscription = GetSubscription($subscriptionid);
78
            my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
82
            my $pattern   = C4::Serials::Numberpattern::GetSubscriptionNumberpattern( $subscription->{numberpattern} );
79
            my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subscription->{periodicity});
83
            my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency( $subscription->{periodicity} );
80
            my $expected = GetNextExpected($subscriptionid);
84
            my $expected  = GetNextExpected($subscriptionid);
81
            my (
85
            my (
82
                 $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
86
                $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
83
                 $newinnerloop1, $newinnerloop2, $newinnerloop3
87
                $newinnerloop1, $newinnerloop2, $newinnerloop3
84
            ) = GetNextSeq($subscription, $pattern, $frequency, $expected->{publisheddate}, $count_forward);
88
            ) = GetNextSeq( $subscription, $pattern, $frequency, $expected->{publisheddate}, $count_forward );
85
89
86
             ## We generate the next publication date
90
            ## We generate the next publication date
87
             my $nextpublisheddate = GetNextDate($subscription, $expected->{publisheddate}, $frequency, 1);
91
            my $nextpublisheddate = GetNextDate( $subscription, $expected->{publisheddate}, $frequency, 1 );
88
             my $planneddate = $date_received_today ? dt_from_string : $nextpublisheddate;
92
            my $planneddate       = $date_received_today ? dt_from_string : $nextpublisheddate;
89
             ## Creating the new issue
93
            ## Creating the new issue
90
             NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'},
94
            NewIssue(
91
                     1, $planneddate, $nextpublisheddate, undef,
95
                $newserialseq,   $subscriptionid, $subscription->{'biblionumber'},
92
                     $issue->{notes}, $issue->{routingnotes} );
96
                1,               $planneddate,    $nextpublisheddate, undef,
93
97
                $issue->{notes}, $issue->{routingnotes}
94
             ## Updating the subscription seq status
98
            );
95
             my $squery = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
99
100
            ## Updating the subscription seq status
101
            my $squery =
102
                "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
96
                         WHERE  subscriptionid = ?";
103
                         WHERE  subscriptionid = ?";
97
             my $seqsth = $dbh->prepare($squery);
104
            my $seqsth = $dbh->prepare($squery);
98
             $seqsth->execute(
105
            $seqsth->execute(
99
                 $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
106
                $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
100
                 $newinnerloop2, $newinnerloop3, $subscriptionid
107
                $newinnerloop2, $newinnerloop3, $subscriptionid
101
                 );
108
            );
102
109
103
        }
110
        }
104
        last if $nbissues == 1;
111
        last if $nbissues == 1;
105
        last if HasSubscriptionExpired($subscriptionid) > 0;
112
        last if HasSubscriptionExpired($subscriptionid) > 0;
106
    }
113
    }
107
    print $query->redirect('/cgi-bin/koha/serials/serials-collection.pl?subscriptionid='.$subscriptionid);
114
    print $query->redirect( '/cgi-bin/koha/serials/serials-collection.pl?subscriptionid=' . $subscriptionid );
108
    exit;
115
    exit;
109
}
116
}
110
117
111
my $countitems = 0;
118
my $countitems     = 0;
112
my @serialsid = $query->multi_param('serialid');
119
my @serialsid      = $query->multi_param('serialid');
113
my $subscriptionid = $subscriptionid[0];
120
my $subscriptionid = $subscriptionid[0];
114
121
115
if($op eq 'delete_confirm'){
122
if ( $op eq 'delete_confirm' ) {
116
    foreach my $serialid (@serialsid){
123
    foreach my $serialid (@serialsid) {
117
        $countitems += Koha::Serial::Items->search({serialid => $serialid})->count();
124
        $countitems += Koha::Serial::Items->search( { serialid => $serialid } )->count();
118
    }
125
    }
119
}elsif($op eq 'cud-delete_confirmed'){
126
} elsif ( $op eq 'cud-delete_confirmed' ) {
120
    if($query->param('delitems') eq "Yes"){
127
    if ( $query->param('delitems') eq "Yes" ) {
121
        my @itemnumbers;
128
        my @itemnumbers;
122
        foreach my $serialid (@serialsid){
129
        foreach my $serialid (@serialsid) {
123
            my @ids = Koha::Serial::Items->search({serialid => $serialid})->get_column('itemnumber');
130
            my @ids = Koha::Serial::Items->search( { serialid => $serialid } )->get_column('itemnumber');
124
            push(@itemnumbers, @ids);
131
            push( @itemnumbers, @ids );
125
        }
132
        }
126
        my $items = Koha::Items->search({ itemnumber => \@itemnumbers });
133
        my $items = Koha::Items->search( { itemnumber => \@itemnumbers } );
127
        while ( my $item = $items->next ) {
134
        while ( my $item = $items->next ) {
128
            my $deleted = $item->safe_delete;
135
            my $deleted = $item->safe_delete;
129
            $template->param(error_delitem => 1)
136
            $template->param( error_delitem => 1 )
130
                unless $deleted;
137
                unless $deleted;
131
        }
138
        }
132
    }
139
    }
133
    for my $serialid (@serialsid){
140
    for my $serialid (@serialsid) {
134
        my $serial = Koha::Serials->find($serialid);
141
        my $serial = Koha::Serials->find($serialid);
135
        ModSerialStatus($serialid, $serial->serialseq, $serial->planneddate, $serial->publisheddate, $serial->publisheddatetext, 6, "");
142
        ModSerialStatus(
143
            $serialid, $serial->serialseq, $serial->planneddate, $serial->publisheddate,
144
            $serial->publisheddatetext, 6, ""
145
        );
136
    }
146
    }
137
}
147
}
138
148
139
my $subscriptioncount;
149
my $subscriptioncount;
140
my ($location, $callnumber);
150
my ( $location, $callnumber );
141
if (@subscriptionid){
151
if (@subscriptionid) {
142
   my @subscriptioninformation=();
152
    my @subscriptioninformation = ();
143
   my $closed = 0;
153
    my $closed                  = 0;
144
   foreach my $subscriptionid (@subscriptionid){
154
    foreach my $subscriptionid (@subscriptionid) {
145
    my $subs= GetSubscription($subscriptionid);
155
        my $subs = GetSubscription($subscriptionid);
146
    next unless $subs;
156
        next unless $subs;
147
    $closed = 1 if $subs->{closed};
157
        $closed = 1 if $subs->{closed};
148
158
149
    ##these are display information
159
        ##these are display information
150
    $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
160
        $subs->{'abouttoexpire'}       = abouttoexpire( $subs->{'subscriptionid'} );
151
    $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
161
        $subs->{'subscriptionexpired'} = HasSubscriptionExpired( $subs->{'subscriptionid'} );
152
    $subs->{'subscriptionid'} = $subscriptionid;  # FIXME - why was this lost ?
162
        $subs->{'subscriptionid'}      = $subscriptionid;       # FIXME - why was this lost ?
153
    $location = $subs->{'location'};
163
        $location                      = $subs->{'location'};
154
	$callnumber = $subs->{callnumber};
164
        $callnumber                    = $subs->{callnumber};
155
    my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
165
        my $frequency     = C4::Serials::Frequency::GetSubscriptionFrequency( $subs->{periodicity} );
156
    my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
166
        my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern( $subs->{numberpattern} );
157
    $subs->{frequency} = $frequency;
167
        $subs->{frequency}     = $frequency;
158
    $subs->{numberpattern} = $numberpattern;
168
        $subs->{numberpattern} = $numberpattern;
159
    $subs->{'hasRouting'} = check_routing($subscriptionid);
169
        $subs->{'hasRouting'}  = check_routing($subscriptionid);
160
    push @$subscriptiondescs,$subs;
170
        push @$subscriptiondescs, $subs;
161
    my $tmpsubscription= GetFullSubscription($subscriptionid);
171
        my $tmpsubscription = GetFullSubscription($subscriptionid);
162
    @subscriptioninformation=(@$tmpsubscription,@subscriptioninformation);
172
        @subscriptioninformation = ( @$tmpsubscription, @subscriptioninformation );
163
  }
173
    }
164
174
165
  output_and_exit( $query, $cookie, $template, 'unknown_subscription') unless @subscriptioninformation;
175
    output_and_exit( $query, $cookie, $template, 'unknown_subscription' ) unless @subscriptioninformation;
166
176
167
  $template->param(closed => $closed);
177
    $template->param( closed => $closed );
168
  $subscriptions=PrepareSerialsData(\@subscriptioninformation, 1);
178
    $subscriptions     = PrepareSerialsData( \@subscriptioninformation, 1 );
169
  $subscriptioncount = CountSubscriptionFromBiblionumber($subscriptiondescs->[0]{'biblionumber'});
179
    $subscriptioncount = CountSubscriptionFromBiblionumber( $subscriptiondescs->[0]{'biblionumber'} );
170
} else {
180
} else {
171
  $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber);
181
    $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber);
172
  foreach my $s (@$subscriptiondescs) {
182
    foreach my $s (@$subscriptiondescs) {
173
    my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($s->{periodicity});
183
        my $frequency     = C4::Serials::Frequency::GetSubscriptionFrequency( $s->{periodicity} );
174
    my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($s->{numberpattern});
184
        my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern( $s->{numberpattern} );
175
    $s->{frequency} = $frequency;
185
        $s->{frequency}     = $frequency;
176
    $s->{numberpattern} = $numberpattern;
186
        $s->{numberpattern} = $numberpattern;
177
  }
187
    }
178
  my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
188
    my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
179
  $subscriptions=PrepareSerialsData($subscriptioninformation, 1);
189
    $subscriptions = PrepareSerialsData( $subscriptioninformation, 1 );
180
}
190
}
181
191
182
my $title = $subscriptiondescs->[0]{bibliotitle};
192
my $title = $subscriptiondescs->[0]{bibliotitle};
183
my $yearmax=($subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions)>1)? $subscriptions->[1]{year} :$subscriptions->[0]{year};
193
my $yearmax =
184
my $yearmin=$subscriptions->[scalar(@$subscriptions)-1]{year};
194
    ( $subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions) > 1 )
185
my $subscriptionidlist="";
195
    ? $subscriptions->[1]{year}
186
foreach my $subscription (@$subscriptiondescs){
196
    : $subscriptions->[0]{year};
187
  $subscriptionidlist.=$subscription->{'subscriptionid'}."," ;
197
my $yearmin            = $subscriptions->[ scalar(@$subscriptions) - 1 ]{year};
188
  $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
198
my $subscriptionidlist = "";
189
  $subscription->{'hasRouting'} = check_routing($subscription->{'subscriptionid'});
199
foreach my $subscription (@$subscriptiondescs) {
200
    $subscriptionidlist .= $subscription->{'subscriptionid'} . ",";
201
    $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
202
    $subscription->{'hasRouting'} = check_routing( $subscription->{'subscriptionid'} );
190
}
203
}
191
204
192
chop $subscriptionidlist;
205
chop $subscriptionidlist;
193
$template->param(
206
$template->param(
194
          subscriptionidlist => $subscriptionidlist,
207
    subscriptionidlist                           => $subscriptionidlist,
195
          biblionumber => $biblionumber,
208
    biblionumber                                 => $biblionumber,
196
          subscriptions => $subscriptiondescs,
209
    subscriptions                                => $subscriptiondescs,
197
          years => $subscriptions,
210
    years                                        => $subscriptions,
198
          yearmin => $yearmin,
211
    yearmin                                      => $yearmin,
199
          yearmax =>$yearmax,
212
    yearmax                                      => $yearmax,
200
          bibliotitle => $title,
213
    bibliotitle                                  => $title,
201
          suggestion => C4::Context->preference("suggestion"),
214
    suggestion                                   => C4::Context->preference("suggestion"),
202
          virtualshelves => C4::Context->preference("virtualshelves"),
215
    virtualshelves                               => C4::Context->preference("virtualshelves"),
203
          routing => C4::Context->preference("RoutingSerials"),
216
    routing                                      => C4::Context->preference("RoutingSerials"),
204
          subscr=>scalar $query->param('subscriptionid'),
217
    subscr                                       => scalar $query->param('subscriptionid'),
205
          subscriptioncount => $subscriptioncount,
218
    subscriptioncount                            => $subscriptioncount,
206
          location => $location,
219
    location                                     => $location,
207
          callnumber	       => $callnumber,
220
    callnumber                                   => $callnumber,
208
          uc(C4::Context->preference("marcflavour")) => 1,
221
    uc( C4::Context->preference("marcflavour") ) => 1,
209
          serialsadditems   => $subscriptiondescs->[0]{'serialsadditems'},
222
    serialsadditems                              => $subscriptiondescs->[0]{'serialsadditems'},
210
          delete => ($op eq 'delete_confirm'),
223
    delete                                       => ( $op eq 'delete_confirm' ),
211
          subscriptionid => $subscriptionid,
224
    subscriptionid                               => $subscriptionid,
212
          countitems => $countitems,
225
    countitems                                   => $countitems,
213
          serialnumber => scalar @serialsid,
226
    serialnumber                                 => scalar @serialsid,
214
          serialsid => \@serialsid,
227
    serialsid                                    => \@serialsid,
215
          );
228
);
216
229
217
output_html_with_http_headers $query, $cookie, $template->output;
230
output_html_with_http_headers $query, $cookie, $template->output;
218
- 

Return to bug 38061