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

(-)a/Koha/CirculationRules.pm (+24 lines)
Lines 25-30 use Koha::Exceptions::CirculationRule; Link Here
25
use Koha::CirculationRule;
25
use Koha::CirculationRule;
26
use Koha::Caches;
26
use Koha::Caches;
27
use Koha::Cache::Memory::Lite;
27
use Koha::Cache::Memory::Lite;
28
use Koha::ItemTypes;
28
use Koha::Number::Price;
29
use Koha::Number::Price;
29
30
30
use base qw(Koha::Objects);
31
use base qw(Koha::Objects);
Lines 294-299 sub get_effective_rule { Link Here
294
        }
295
        }
295
    )->single;
296
    )->single;
296
297
298
    if ( !defined $itemtype || ( $rule && defined $rule->itemtype ) ) {
299
        return $rule;
300
    }
301
302
    my $itemtype_obj = Koha::ItemTypes->find($itemtype);
303
    return $rule unless $itemtype_obj;
304
305
    my $parent = $itemtype_obj->parent;
306
    return $rule unless $parent;
307
308
    my $parent_search = {%$search_params};
309
    $parent_search->{itemtype} = [ $parent->itemtype, undef ];
310
311
    my $parent_rule = $self->search(
312
        $parent_search,
313
        {
314
            order_by => $order_by,
315
            rows     => 1,
316
        }
317
    )->single;
318
319
    return $parent_rule if $parent_rule && defined $parent_rule->itemtype;
320
297
    return $rule;
321
    return $rule;
298
}
322
}
299
323
(-)a/t/db_dependent/Koha/CirculationRules.t (-2 / +114 lines)
Lines 21-27 use Modern::Perl; Link Here
21
21
22
use Benchmark;
22
use Benchmark;
23
use Test::NoWarnings;
23
use Test::NoWarnings;
24
use Test::More tests => 9;
24
use Test::More tests => 10;
25
use Test::Deep qw( cmp_methods );
25
use Test::Deep qw( cmp_methods );
26
use Test::Exception;
26
use Test::Exception;
27
27
Lines 1149-1154 subtest 'get_lostreturn_policy() tests' => sub { Link Here
1149
    $schema->storage->txn_rollback;
1149
    $schema->storage->txn_rollback;
1150
};
1150
};
1151
1151
1152
subtest 'get_effective_rule parent itemtype fallback' => sub {
1153
    plan tests => 6;
1154
1155
    $schema->storage->txn_begin;
1156
1157
    my $branch   = $builder->build( { source => 'Branch' } )->{branchcode};
1158
    my $branch2  = $builder->build( { source => 'Branch' } )->{branchcode};
1159
    my $category = $builder->build( { source => 'Category' } )->{categorycode};
1160
1161
    my $parent_itype = $builder->build_object( { class => 'Koha::ItemTypes', value => { parent_type => undef } } );
1162
    my $child_itype =
1163
        $builder->build_object( { class => 'Koha::ItemTypes', value => { parent_type => $parent_itype->itemtype } } );
1164
    my $orphan_itype = $builder->build_object( { class => 'Koha::ItemTypes', value => { parent_type => undef } } );
1165
1166
    Koha::CirculationRules->set_rule(
1167
        {
1168
            branchcode   => undef,
1169
            categorycode => undef,
1170
            itemtype     => undef,
1171
            rule_name    => 'fine',
1172
            rule_value   => '0.50',
1173
        }
1174
    );
1175
1176
    Koha::CirculationRules->set_rule(
1177
        {
1178
            branchcode   => undef,
1179
            categorycode => undef,
1180
            itemtype     => $parent_itype->itemtype,
1181
            rule_name    => 'fine',
1182
            rule_value   => '1.00',
1183
        }
1184
    );
1185
1186
    my $rule = Koha::CirculationRules->get_effective_rule(
1187
        {
1188
            rule_name => 'fine',
1189
            itemtype  => $child_itype->itemtype,
1190
        }
1191
    );
1192
    is( $rule->rule_value, '1.00', 'Child itemtype falls back to parent-specific rule over global' );
1193
1194
    Koha::CirculationRules->set_rule(
1195
        {
1196
            branchcode   => undef,
1197
            categorycode => undef,
1198
            itemtype     => $child_itype->itemtype,
1199
            rule_name    => 'fine',
1200
            rule_value   => '2.00',
1201
        }
1202
    );
1203
1204
    $rule = Koha::CirculationRules->get_effective_rule(
1205
        {
1206
            rule_name => 'fine',
1207
            itemtype  => $child_itype->itemtype,
1208
        }
1209
    );
1210
    is( $rule->rule_value, '2.00', 'Child-specific rule takes priority over parent' );
1211
1212
    Koha::CirculationRules->search(
1213
        {
1214
            itemtype  => $child_itype->itemtype,
1215
            rule_name => 'fine',
1216
        }
1217
    )->delete;
1218
1219
    $rule = Koha::CirculationRules->get_effective_rule(
1220
        {
1221
            rule_name => 'fine',
1222
            itemtype  => $child_itype->itemtype,
1223
        }
1224
    );
1225
    is( $rule->rule_value, '1.00', 'After deleting child rule, falls back to parent again' );
1226
1227
    $rule = Koha::CirculationRules->get_effective_rule(
1228
        {
1229
            rule_name => 'fine',
1230
            itemtype  => $orphan_itype->itemtype,
1231
        }
1232
    );
1233
    is( $rule->rule_value, '0.50', 'Orphan itemtype (no parent) falls back to global' );
1234
1235
    Koha::CirculationRules->set_rule(
1236
        {
1237
            branchcode   => $branch,
1238
            categorycode => undef,
1239
            itemtype     => $parent_itype->itemtype,
1240
            rule_name    => 'fine',
1241
            rule_value   => '3.00',
1242
        }
1243
    );
1244
1245
    $rule = Koha::CirculationRules->get_effective_rule(
1246
        {
1247
            rule_name  => 'fine',
1248
            itemtype   => $child_itype->itemtype,
1249
            branchcode => $branch,
1250
        }
1251
    );
1252
    is( $rule->rule_value, '3.00', 'Branch-specific parent rule takes priority' );
1253
1254
    $rule = Koha::CirculationRules->get_effective_rule(
1255
        {
1256
            rule_name => 'fine',
1257
            itemtype  => undef,
1258
        }
1259
    );
1260
    is( $rule->rule_value, '0.50', 'Undef itemtype returns global (no parent lookup)' );
1261
1262
    $schema->storage->txn_rollback;
1263
};
1264
1152
sub _is_row_match {
1265
sub _is_row_match {
1153
    my ( $rule, $expected, $message ) = @_;
1266
    my ( $rule, $expected, $message ) = @_;
1154
1267
1155
- 

Return to bug 41917