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
      &CountIssues
87
      &CountIssues
88
      HasItems
88
      HasItems
89
      &subscriptionCurrentlyOnOrder
89
      &subscriptionCurrentlyOnOrder
90
      &GuessEnddate
91
90
92
    );
91
    );
93
}
92
}
Lines 2664-2703 sub findSerialsByStatus { Link Here
2664
    return @$serials;
2663
    return @$serials;
2665
}
2664
}
2666
2665
2667
=head2 GuessEnddate
2668
2669
my $enddate = GuessEnddate($startdate_iso $frequencyid, $numberlength, $weeklength, $monthlength);
2670
2671
=cut
2672
2673
sub GuessEnddate {
2674
    my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
2675
    my ($year, $month, $day);
2676
    my $enddate;
2677
    if($numberlength != 0) {
2678
        my $frequency = GetSubscriptionFrequency($frequencyid);
2679
        if($frequency->{'unit'} eq 'day') {
2680
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
2681
        } elsif($frequency->{'unit'} eq 'week') {
2682
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
2683
        } elsif($frequency->{'unit'} eq 'month') {
2684
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
2685
        } elsif($frequency->{'unit'} eq 'year') {
2686
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
2687
        }
2688
    } elsif($weeklength != 0) {
2689
        ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
2690
    } elsif($monthlength != 0) {
2691
        ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
2692
    }
2693
    if(defined $year) {
2694
        $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
2695
    } else {
2696
        undef $enddate;
2697
    }
2698
    return $enddate;
2699
}
2700
2701
1;
2666
1;
2702
__END__
2667
__END__
2703
2668
(-)a/Koha/Subscription.pm (-3 / +44 lines)
Lines 21-26 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;
Lines 30-36 use Koha::Subscription::Frequencies; Link Here
30
use Koha::Subscription::Numberpatterns;
32
use Koha::Subscription::Numberpatterns;
31
use Koha::DateUtils;
33
use Koha::DateUtils;
32
use JSON;
34
use JSON;
33
use Date::Calc qw( Delta_Days );
35
use Date::Calc qw( Delta_Days Add_Delta_Days Add_Delta_YM );
34
36
35
use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields);
37
use base qw(Koha::Object Koha::Object::Mixin::AdditionalFields);
36
38
Lines 245-253 sub lengthtype { Link Here
245
    }
247
    }
246
}
248
}
247
249
248
=head3 update_irregularities
250
=head3 guess_irregularities
249
251
250
$subscription->update_irregularities
252
my @irregularities = $subscription->guess_irregularities
251
253
252
=cut
254
=cut
253
255
Lines 284-289 sub guess_irregularities { Link Here
284
    return @irregularities;
286
    return @irregularities;
285
}
287
}
286
288
289
=head2 guess_enddate
290
291
my $enddate = $subscription->guess_enddate;
292
293
=cut
294
295
sub guess_enddate {
296
    my ($self) = @_;
297
298
    my ($year, $month, $day, $enddate);
299
    my $startdate = $self->startdate;
300
    my $numberlength = $self->numberlength;
301
    my $weeklength = $self->weeklength;
302
    my $monthlength = $self->monthlength;
303
304
    if($numberlength != 0) {
305
        my $frequency = GetSubscriptionFrequency($self->periodicity);
306
        if($frequency->{'unit'} eq 'day') {
307
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
308
        } elsif($frequency->{'unit'} eq 'week') {
309
            ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
310
        } elsif($frequency->{'unit'} eq 'month') {
311
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
312
        } elsif($frequency->{'unit'} eq 'year') {
313
            ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
314
        }
315
    } elsif($weeklength != 0) {
316
        ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate), $weeklength * 7);
317
    } elsif($monthlength != 0) {
318
        ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate), 0, $monthlength);
319
    }
320
    if(defined $year) {
321
        $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
322
    } else {
323
        undef $enddate;
324
    }
325
    return $enddate;
326
}
327
287
=head3 type
328
=head3 type
288
329
289
=cut
330
=cut
(-)a/serials/subscription-add.pl (-6 / +20 lines)
Lines 337-347 sub redirect_add_subscription { Link Here
337
    my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
337
    my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
338
338
339
    if(!defined $enddate || $enddate eq '') {
339
    if(!defined $enddate || $enddate eq '') {
340
        my $subscription = Koha::Subscriptions->new({
341
            startdate       => $startdate,
342
            periodicity     => $periodicity,
343
            weeklength      => $weeklength,
344
            monthlength     =>$monthlength,
345
            numberlength    => $numberlength
346
        });
347
340
        if($subtype eq "issues") {
348
        if($subtype eq "issues") {
341
            $enddate = GuessEnddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
349
            $subscription->startdate($firstacquidate);
342
        } else {
343
            $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
344
        }
350
        }
351
        $enddate = $subscription->guess_enddate;
345
    }
352
    }
346
    my $subscriptionid = NewSubscription(
353
    my $subscriptionid = NewSubscription(
347
        $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
354
        $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
Lines 451-461 sub redirect_mod_subscription { Link Here
451
458
452
    # Guess end date
459
    # Guess end date
453
    if(!defined $enddate || $enddate eq '') {
460
    if(!defined $enddate || $enddate eq '') {
461
        my $subscription = Koha::Subscriptions->new({
462
            startdate       => $startdate,
463
            periodicity     => $periodicity,
464
            weeklength      => $weeklength,
465
            monthlength     =>$monthlength,
466
            numberlength    => $numberlength
467
        });
468
454
        if($subtype eq "issues") {
469
        if($subtype eq "issues") {
455
            $enddate = GuessEnddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
470
            $subscription->startdate($nextacquidate);
456
        } else {
457
            $enddate = GuessEnddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
458
        }
471
        }
472
        $enddate = $subscription->guess_enddate;
459
    }
473
    }
460
474
461
    my $nextexpected = GetNextExpected($subscriptionid);
475
    my $nextexpected = GetNextExpected($subscriptionid);
(-)a/serials/subscription-renew.pl (-5 / +8 lines)
Lines 147-157 if ( $op eq "renew" ) { Link Here
147
    my $subscription_o = Koha::Subscriptions->find($subscription->{subscriptionid});
147
    my $subscription_o = Koha::Subscriptions->find($subscription->{subscriptionid});
148
    my $lengthtype = $subscription_o->lengthtype;
148
    my $lengthtype = $subscription_o->lengthtype;
149
    my $nextexpected = GetNextExpected($subscriptionid);
149
    my $nextexpected = GetNextExpected($subscriptionid);
150
    my $enddate = GuessEnddate($subscription->{enddate} || dt_from_string,
150
    $subscription_o->update({
151
                                 $subscription->{periodicity},
151
        startdate       => $subscription->{enddate},
152
                                 $subscription->{numberlength},
152
        periodicity     => $subscription->{periodicity},
153
                                 $subscription->{weeklength},
153
        weeklength      => $subscription->{weeklength},
154
                                 $subscription->{monthlength});
154
        monthlength     => $subscription->{monthlength},
155
        numberlength    => $subscription->{numberlength}
156
    });
157
    my $enddate = $subscription_o->guess_enddate;
155
158
156
    my $sub_length;
159
    my $sub_length;
157
    foreach my $length_unit (qw(numberlength weeklength monthlength)) {
160
    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