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

(-)a/C4/Items.pm (-20 / +23 lines)
Lines 1560-1582 sub PrepareItemrecordDisplay { Link Here
1560
}
1560
}
1561
1561
1562
sub ToggleNewStatus {
1562
sub ToggleNewStatus {
1563
    my ( $params ) = @_;
1563
    my ($params)    = @_;
1564
    my @rules = @{ $params->{rules} };
1564
    my @rules       = @{ $params->{rules} };
1565
    my $report_only = $params->{report_only};
1565
    my $report_only = $params->{report_only};
1566
1566
1567
    my $dbh = C4::Context->dbh;
1567
    my $dbh = C4::Context->dbh;
1568
    my @errors;
1568
    my @errors;
1569
    my @item_columns = map { "items.$_" } Koha::Items->columns;
1569
    my @item_columns       = map { "items.$_" } Koha::Items->columns;
1570
    my @biblioitem_columns = map { "biblioitems.$_" } Koha::Biblioitems->columns;
1570
    my @biblioitem_columns = map { "biblioitems.$_" } Koha::Biblioitems->columns;
1571
    my @biblio_columns     = map { "biblio.$_" } Koha::Biblios->columns;
1571
    my $report;
1572
    my $report;
1572
    for my $rule ( @rules ) {
1573
    for my $rule (@rules) {
1573
        my $age = $rule->{age};
1574
        my $age = $rule->{age};
1575
1574
        # Default to using items.dateaccessioned if there's an old item modification rule
1576
        # Default to using items.dateaccessioned if there's an old item modification rule
1575
        # missing an agefield value
1577
        # missing an agefield value
1576
        my $agefield = $rule->{agefield} ? $rule->{agefield} : 'items.dateaccessioned';
1578
        my $agefield      = $rule->{agefield} ? $rule->{agefield} : 'items.dateaccessioned';
1577
        my $conditions = $rule->{conditions};
1579
        my $conditions    = $rule->{conditions};
1578
        my $substitutions = $rule->{substitutions};
1580
        my $substitutions = $rule->{substitutions};
1579
        foreach ( @$substitutions ) {
1581
        foreach (@$substitutions) {
1580
            ( $_->{item_field} ) = ( $_->{field} =~ /items\.(.*)/ );
1582
            ( $_->{item_field} ) = ( $_->{field} =~ /items\.(.*)/ );
1581
        }
1583
        }
1582
        my @params;
1584
        my @params;
Lines 1585-1603 sub ToggleNewStatus { Link Here
1585
            SELECT items.*
1587
            SELECT items.*
1586
            FROM items
1588
            FROM items
1587
            LEFT JOIN biblioitems ON biblioitems.biblionumber = items.biblionumber
1589
            LEFT JOIN biblioitems ON biblioitems.biblionumber = items.biblionumber
1590
            LEFT JOIN biblio ON biblio.biblionumber = biblioitems.biblionumber
1588
            WHERE 1
1591
            WHERE 1
1589
        |;
1592
        |;
1590
        for my $condition ( @$conditions ) {
1593
        for my $condition (@$conditions) {
1591
            next unless $condition->{field};
1594
            next unless $condition->{field};
1592
            if (
1595
            if (   grep { $_ eq $condition->{field} } @item_columns
1593
                 grep { $_ eq $condition->{field} } @item_columns
1596
                or grep { $_ eq $condition->{field} } @biblioitem_columns
1594
              or grep { $_ eq $condition->{field} } @biblioitem_columns
1597
                or grep { $_ eq $condition->{field} } @biblio_columns )
1595
            ) {
1598
            {
1596
                if ( $condition->{value} =~ /\|/ ) {
1599
                if ( $condition->{value} =~ /\|/ ) {
1597
                    my @values = split /\|/, $condition->{value};
1600
                    my @values = split /\|/, $condition->{value};
1598
                    $query .= qq| AND $condition->{field} IN (|
1601
                    $query .= qq| AND $condition->{field} IN (| . join( ',', ('?') x scalar @values ) . q|)|;
1599
                        . join( ',', ('?') x scalar @values )
1600
                        . q|)|;
1601
                    push @params, @values;
1602
                    push @params, @values;
1602
                } else {
1603
                } else {
1603
                    $query .= qq| AND $condition->{field} = ?|;
1604
                    $query .= qq| AND $condition->{field} = ?|;
Lines 1610-1625 sub ToggleNewStatus { Link Here
1610
            push @params, $age;
1611
            push @params, $age;
1611
        }
1612
        }
1612
        my $sth = $dbh->prepare($query);
1613
        my $sth = $dbh->prepare($query);
1613
        $sth->execute( @params );
1614
        $sth->execute(@params);
1614
        while ( my $values = $sth->fetchrow_hashref ) {
1615
        while ( my $values = $sth->fetchrow_hashref ) {
1615
            my $biblionumber = $values->{biblionumber};
1616
            my $biblionumber = $values->{biblionumber};
1616
            my $itemnumber = $values->{itemnumber};
1617
            my $itemnumber   = $values->{itemnumber};
1617
            my $item = Koha::Items->find($itemnumber);
1618
            my $item         = Koha::Items->find($itemnumber);
1618
            for my $substitution ( @$substitutions ) {
1619
            for my $substitution (@$substitutions) {
1619
                my $field = $substitution->{item_field};
1620
                my $field = $substitution->{item_field};
1620
                my $value = $substitution->{value};
1621
                my $value = $substitution->{value};
1621
                next unless $substitution->{field};
1622
                next unless $substitution->{field};
1622
                next if ( defined $values->{ $substitution->{item_field} } and $values->{ $substitution->{item_field} } eq $substitution->{value} );
1623
                next
1624
                    if ( defined $values->{ $substitution->{item_field} }
1625
                    and $values->{ $substitution->{item_field} } eq $substitution->{value} );
1623
                $item->$field($value);
1626
                $item->$field($value);
1624
                push @{ $report->{$itemnumber} }, $substitution;
1627
                push @{ $report->{$itemnumber} }, $substitution;
1625
            }
1628
            }
(-)a/t/db_dependent/Items/AutomaticItemModificationByAge.t (-4 / +29 lines)
Lines 1-7 Link Here
1
#!/usr/bin/perl
1
#!/usr/bin/perl
2
2
3
use Modern::Perl;
3
use Modern::Perl;
4
use Test::More tests => 19;
4
use Test::More tests => 20;
5
use MARC::Record;
5
use MARC::Record;
6
use MARC::Field;
6
use MARC::Field;
7
use DateTime;
7
use DateTime;
Lines 46-54 $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode"); Link Here
46
46
47
my $record = MARC::Record->new();
47
my $record = MARC::Record->new();
48
$record->append_fields(
48
$record->append_fields(
49
    MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
49
    MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
50
    MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
50
    MARC::Field->new( '245', ' ', ' ', a => 'Silence in the library', h => 'Book' ),
51
    MARC::Field->new('942', ' ', ' ', c => 'ITEMTYPE_T'),
51
    MARC::Field->new( '942', ' ', ' ', c => 'ITEMTYPE_T' ),
52
);
52
);
53
my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, $frameworkcode);
53
my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, $frameworkcode);
54
54
Lines 330-335 C4::Items::ToggleNewStatus( { rules => \@rules } ); Link Here
330
$modified_item = Koha::Items->find( $itemnumber );
330
$modified_item = Koha::Items->find( $itemnumber );
331
is( $modified_item->new_status, 'agefield_new_value', q|ToggleNewStatus: Age = 2, agefield = 'items.datelastseen' : The new_status value is updated|);
331
is( $modified_item->new_status, 'agefield_new_value', q|ToggleNewStatus: Age = 2, agefield = 'items.datelastseen' : The new_status value is updated|);
332
332
333
# Condition on biblio column
334
@rules = (
335
    {
336
        # does not exist
337
        conditions => [
338
            {
339
                field => 'biblio.medium',
340
                value => 'Book',
341
            },
342
        ],
343
        substitutions => [
344
            {
345
                field => 'items.new_status',
346
                value => 'new_updated_value_biblio',
347
            },
348
        ],
349
        age => '0',
350
    },
351
);
352
353
C4::Items::ToggleNewStatus( { rules => \@rules } );
354
355
$modified_item = Koha::Items->find( $itemnumber );
356
is( $modified_item->new_status, 'new_updated_value_biblio', q|ToggleNewStatus: conditions on biblio|);
357
333
# Run twice
358
# Run twice
334
t::lib::Mocks::mock_preference('CataloguingLog', 1);
359
t::lib::Mocks::mock_preference('CataloguingLog', 1);
335
my $actions_nb = $schema->resultset('ActionLog')->count();
360
my $actions_nb = $schema->resultset('ActionLog')->count();
(-)a/tools/automatic_item_modification_by_age.pl (-8 / +12 lines)
Lines 42-47 use C4::Koha; Link Here
42
42
43
use Koha::Items;
43
use Koha::Items;
44
use Koha::Biblioitems;
44
use Koha::Biblioitems;
45
use Koha::Biblios;
45
46
46
my $cgi = CGI->new;
47
my $cgi = CGI->new;
47
48
Lines 113-128 if ( $@ ) { Link Here
113
    exit;
114
    exit;
114
}
115
}
115
116
116
my @item_fields = map { "items.$_" } Koha::Items->columns;
117
my @item_fields       = map { "items.$_" } Koha::Items->columns;
117
my @biblioitem_fields = map { "biblioitems.$_" } Koha::Biblioitems->columns;
118
my @biblioitem_fields = map { "biblioitems.$_" } Koha::Biblioitems->columns;
118
my @age_fields = ('items.dateaccessioned', 'items.replacementpricedate', 'items.datelastborrowed', 'items.datelastseen', 'items.damaged_on', 'items.itemlost_on', 'items.withdrawn_on');
119
my @biblio_fields     = map { "biblio.$_" } Koha::Biblios->columns;
120
my @age_fields        = (
121
    'items.dateaccessioned', 'items.replacementpricedate', 'items.datelastborrowed', 'items.datelastseen',
122
    'items.damaged_on',      'items.itemlost_on',          'items.withdrawn_on'
123
);
119
$template->param(
124
$template->param(
120
    op => $op,
125
    op                  => $op,
121
    messages => \@messages,
126
    messages            => \@messages,
122
    agefields => [ @age_fields ],
127
    agefields           => [@age_fields],
123
    condition_fields => [ @item_fields, @biblioitem_fields ],
128
    condition_fields    => [ @item_fields, @biblioitem_fields, @biblio_fields ],
124
    substitution_fields => \@item_fields,
129
    substitution_fields => \@item_fields,
125
    rules => $rules,
130
    rules               => $rules,
126
);
131
);
127
132
128
output_html_with_http_headers $cgi, $cookie, $template->output;
133
output_html_with_http_headers $cgi, $cookie, $template->output;
129
- 

Return to bug 32029