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

(-)a/C4/Serials.pm (-35 lines)
Lines 87-93 BEGIN { Link Here
87
      HasItems
87
      HasItems
88
      &GetSubscriptionsFromBorrower
88
      &GetSubscriptionsFromBorrower
89
      &subscriptionCurrentlyOnOrder
89
      &subscriptionCurrentlyOnOrder
90
      &GuessEnddate
91
90
92
    );
91
    );
93
}
92
}
Lines 2716-2755 sub findSerialsByStatus { Link Here
2716
    return @$serials;
2715
    return @$serials;
2717
}
2716
}
2718
2717
2719
=head2 GuessEnddate
2720
2721
my $enddate = GuessEnddate($startdate_iso $frequencyid, $numberlength, $weeklength, $monthlength);
2722
2723
=cut
2724
2725
sub GuessEnddate {
2726
    my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
2727
    my ($year, $month, $day);
2728
    my $enddate;
2729
    if($numberlength != 0) {
2730
        my $frequency = GetSubscriptionFrequency($frequencyid);
2731
        if($frequency->{'unit'} eq 'day') {
2732
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
2733
        } elsif($frequency->{'unit'} eq 'week') {
2734
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
2735
        } elsif($frequency->{'unit'} eq 'month') {
2736
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
2737
        } elsif($frequency->{'unit'} eq 'year') {
2738
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
2739
        }
2740
    } elsif($weeklength != 0) {
2741
        ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
2742
    } elsif($monthlength != 0) {
2743
        ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
2744
    }
2745
    if(defined $year) {
2746
        $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
2747
    } else {
2748
        undef $enddate;
2749
    }
2750
    return $enddate;
2751
}
2752
2753
1;
2718
1;
2754
__END__
2719
__END__
2755
2720
(-)a/Koha/Subscription.pm (-3 / +44 lines)
Lines 21-32 use Modern::Perl; Link Here
21
21
22
use Carp;
22
use Carp;
23
23
24
use C4::Serials::Frequency;
25
24
use Koha::Database;
26
use Koha::Database;
25
use Koha::Biblios;
27
use Koha::Biblios;
26
use Koha::Acquisition::Booksellers;
28
use Koha::Acquisition::Booksellers;
27
use Koha::DateUtils;
29
use Koha::DateUtils;
28
30
29
use Date::Calc qw( Delta_Days );
31
use Date::Calc qw( Delta_Days Add_Delta_Days Add_Delta_YM );
30
32
31
use base qw(Koha::Object);
33
use base qw(Koha::Object);
32
34
Lines 155-163 sub lengthtype { Link Here
155
    }
157
    }
156
}
158
}
157
159
158
=head3 update_irregularities
160
=head3 guess_irregularities
159
161
160
$subscription->update_irregularities
162
my @irregularities = $subscription->guess_irregularities
161
163
162
=cut
164
=cut
163
165
Lines 194-199 sub guess_irregularities { Link Here
194
    return @irregularities;
196
    return @irregularities;
195
}
197
}
196
198
199
=head2 guess_enddate
200
201
my $enddate = $subscription->guess_enddate;
202
203
=cut
204
205
sub guess_enddate {
206
    my ($self) = @_;
207
208
    my ($year, $month, $day, $enddate);
209
    my $startdate = $self->startdate;
210
    my $numberlength = $self->numberlength;
211
    my $weeklength = $self->weeklength;
212
    my $monthlength = $self->monthlength;
213
214
    if($numberlength != 0) {
215
        my $frequency = GetSubscriptionFrequency($self->periodicity);
216
        if($frequency->{'unit'} eq 'day') {
217
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
218
        } elsif($frequency->{'unit'} eq 'week') {
219
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
220
        } elsif($frequency->{'unit'} eq 'month') {
221
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
222
        } elsif($frequency->{'unit'} eq 'year') {
223
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
224
        }
225
    } elsif($weeklength != 0) {
226
        ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $weeklength * 7);
227
    } elsif($monthlength != 0) {
228
        ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), 0, $monthlength);
229
    }
230
    if(defined $year) {
231
        $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
232
    } else {
233
        undef $enddate;
234
    }
235
    return $enddate;
236
}
237
197
=head3 type
238
=head3 type
198
239
199
=cut
240
=cut
(-)a/serials/subscription-add.pl (-6 / +20 lines)
Lines 301-311 sub redirect_add_subscription { Link Here
301
    my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
301
    my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
302
302
303
    if(!defined $enddate || $enddate eq '') {
303
    if(!defined $enddate || $enddate eq '') {
304
        my $subscription = Koha::Subscriptions->new({
305
            startdate       => $startdate,
306
            periodicity     => $periodicity,
307
            weeklength      => $weeklength,
308
            monthlength     =>$monthlength,
309
            numberlength    => $numberlength
310
        });
311
304
        if($subtype eq "issues") {
312
        if($subtype eq "issues") {
305
            $enddate = GuessEnddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
313
            $subscription->startdate($firstacquidate);
306
        } else {
307
            $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
308
        }
314
        }
315
        $enddate = $subscription->guess_enddate;
309
    }
316
    }
310
317
311
    my $subscriptionid = NewSubscription(
318
    my $subscriptionid = NewSubscription(
Lines 378-388 sub redirect_mod_subscription { Link Here
378
385
379
    # Guess end date
386
    # Guess end date
380
    if(!defined $enddate || $enddate eq '') {
387
    if(!defined $enddate || $enddate eq '') {
388
        my $subscription = Koha::Subscriptions->new({
389
            startdate       => $startdate,
390
            periodicity     => $periodicity,
391
            weeklength      => $weeklength,
392
            monthlength     =>$monthlength,
393
            numberlength    => $numberlength
394
        });
395
381
        if($subtype eq "issues") {
396
        if($subtype eq "issues") {
382
            $enddate = GuessEnddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
397
            $subscription->startdate($nextacquidate);
383
        } else {
384
            $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
385
        }
398
        }
399
        $enddate = $subscription->guess_enddate;
386
    }
400
    }
387
401
388
    my $nextexpected = GetNextExpected($subscriptionid);
402
    my $nextexpected = GetNextExpected($subscriptionid);
(-)a/serials/subscription-renew.pl (-5 / +8 lines)
Lines 125-135 if ( $op eq "renew" ) { Link Here
125
    my $subscription_o = Koha::Subscriptions->find($subscription->{subscriptionid});
125
    my $subscription_o = Koha::Subscriptions->find($subscription->{subscriptionid});
126
    my $lengthtype = $subscription_o->lengthtype;
126
    my $lengthtype = $subscription_o->lengthtype;
127
    my $nextexpected = GetNextExpected($subscriptionid);
127
    my $nextexpected = GetNextExpected($subscriptionid);
128
    my $enddate = GuessEnddate($subscription->{enddate} || dt_from_string,
128
    $subscription_o->update({
129
                                 $subscription->{periodicity},
129
        startdate       => $subscription->{enddate},
130
                                 $subscription->{numberlength},
130
        periodicity     => $subscription->{periodicity},
131
                                 $subscription->{weeklength},
131
        weeklength      => $subscription->{weeklength},
132
                                 $subscription->{monthlength});
132
        monthlength     => $subscription->{monthlength},
133
        numberlength    => $subscription->{numberlength}
134
    });
135
    my $enddate = $subscription_o->guess_enddate;
133
136
134
    my $sub_length;
137
    my $sub_length;
135
    foreach my $length_unit (qw(numberlength weeklength monthlength)) {
138
    foreach my $length_unit (qw(numberlength weeklength monthlength)) {
(-)a/t/db_dependent/Koha/Subscription/GuessEnddate.t (-1 / +148 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WIT HOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 15;
21
use t::lib::TestBuilder;
22
23
use Koha::Database;
24
use Koha::Subscriptions;
25
26
BEGIN {
27
    use_ok('Koha::Subscriptions');
28
}
29
30
my $schema  = Koha::Database->new->schema;
31
my $builder = t::lib::TestBuilder->new;
32
33
$schema->storage->txn_begin;
34
35
36
my $frequency = $builder->build({
37
    source => 'SubscriptionFrequency',
38
    value => {
39
        unit => 'day',
40
        unitsperissue => 1,
41
        issuesperunit => 1
42
    }
43
});
44
45
my $subscription = $builder->build({
46
        source => 'Subscription',
47
        value => {
48
            startdate => '2018-01-01',
49
            weeklength => 0,
50
            monthlength => 0,
51
            numberlength => 7,
52
            periodicity => $frequency->{id},
53
        }
54
});
55
56
$subscription = Koha::Subscriptions->find($subscription->{subscriptionid});
57
$frequency = $schema->resultset('SubscriptionFrequency')->find($frequency->{id});
58
is($subscription->guess_enddate, '2018-01-08');
59
60
$frequency->update({
61
    unit => 'day',
62
    unitsperissue => 2,
63
    issuesperunit => 1
64
});
65
is($subscription->guess_enddate, '2018-01-15');
66
67
$frequency->update({
68
    unit => 'day',
69
    unitsperissue => 1,
70
    issuesperunit => 2
71
});
72
is($subscription->guess_enddate, '2018-01-04');
73
74
$frequency->update({
75
    unit => 'week',
76
    unitsperissue => 1,
77
    issuesperunit => 1
78
});
79
is($subscription->guess_enddate, '2018-02-19');
80
81
$frequency->update({
82
    unit => 'week',
83
    unitsperissue => 2,
84
    issuesperunit => 1
85
});
86
is($subscription->guess_enddate, '2018-04-09');
87
88
$frequency->update({
89
    unit => 'week',
90
    unitsperissue => 1,
91
    issuesperunit => 2
92
});
93
is($subscription->guess_enddate, '2018-01-25');
94
95
$frequency->update({
96
    unit => 'month',
97
    unitsperissue => 1,
98
    issuesperunit => 1
99
});
100
is($subscription->guess_enddate, '2018-08-01');
101
102
$frequency->update({
103
    unit => 'month',
104
    unitsperissue => 2,
105
    issuesperunit => 1
106
});
107
is($subscription->guess_enddate, '2019-03-01');
108
109
$frequency->update({
110
    unit => 'month',
111
    unitsperissue => 1,
112
    issuesperunit => 2
113
});
114
is($subscription->guess_enddate, '2018-04-01');
115
116
$frequency->update({
117
    unit => 'year',
118
    unitsperissue => 1,
119
    issuesperunit => 1
120
});
121
is($subscription->guess_enddate, '2025-01-01');
122
123
$frequency->update({
124
    unit => 'year',
125
    unitsperissue => 2,
126
    issuesperunit => 1
127
});
128
is($subscription->guess_enddate, '2032-01-01');
129
130
$frequency->update({
131
    unit => 'year',
132
    unitsperissue => 1,
133
    issuesperunit => 2
134
});
135
is($subscription->guess_enddate, '2021-01-01');
136
137
138
$subscription->numberlength(0);
139
$subscription->weeklength(2);
140
$subscription->store;
141
is($subscription->guess_enddate, '2018-01-15');
142
143
$subscription->weeklength(0);
144
$subscription->monthlength(1);
145
$subscription->store;
146
is($subscription->guess_enddate, '2018-02-01');
147
148
$schema->storage->txn_rollback;

Return to bug 17656