4 use Test::More tests => 16;
8 use DateTime::Duration;
15 my $dbh = C4::Context->dbh;
16 $dbh->{AutoCommit} = 0;
17 $dbh->{RaiseError} = 1;
20 DELETE FROM marc_subfield_structure
21 WHERE kohafield = 'items.new' OR kohafield = 'items.stocknumber'
24 my $new_tagfield = 'i';
26 INSERT INTO marc_subfield_structure(tagfield, tagsubfield, kohafield, frameworkcode)
27 VALUES ( 952, '$new_tagfield', 'items.new', '' )
30 my $record = MARC::Record->new();
31 $record->append_fields(
32 MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
33 MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
34 MARC::Field->new('942', ' ', ' ', c => 'ITEMTYPE_T'),
36 my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, '');
38 my ($item_bibnum, $item_bibitemnum, $itemnumber) = C4::Items::AddItem(
41 holdingbranch => 'CPL',
48 my $item = C4::Items::GetItem( $itemnumber );
49 is ( $item->{new}, 'new_value', q|AddItem insert the 'new' field| );
51 my ( $tagfield, undef ) = GetMarcFromKohaField('items.itemnumber', '');
52 my $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
53 is( $marc_item->subfield($tagfield, $new_tagfield), 'new_value', q|Koha mapping is correct|);
55 # Update the items.new field if items.ccode eq 'FIC' => should be updated
60 field => 'items.ccode',
67 value => 'updated_value',
74 C4::Items::ToggleNewStatus( { rules => \@rules } );
76 my $modified_item = C4::Items::GetItem( $itemnumber );
77 is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is updated|);
78 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
79 is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is updated| );
81 # Update the items.new field if items.ccode eq 'DONT_EXIST' => should not be updated
86 field => 'items.ccode',
87 value => 'DONT_EXIST',
93 value => 'new_updated_value',
100 C4::Items::ToggleNewStatus( { rules => \@rules } );
102 $modified_item = C4::Items::GetItem( $itemnumber );
103 is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: The new value is not updated|);
104 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
105 is( $marc_item->subfield($tagfield, $new_tagfield), 'updated_value', q|ToggleNewStatus: The new value is not updated| );
108 $item = C4::Items::GetItem( $itemnumber );
109 my $dt_today = dt_from_string;
110 my $days5ago = $dt_today->add_duration( DateTime::Duration->new( days => -5 ) );
112 C4::Items::ModItem( { dateaccessioned => $days5ago }, $biblionumber, $itemnumber );
113 $item = C4::Items::GetItem( $itemnumber );
119 field => 'items.ccode',
125 field => 'items.new',
126 value => 'new_updated_value',
132 C4::Items::ToggleNewStatus( { rules => \@rules } );
133 $modified_item = C4::Items::GetItem( $itemnumber );
134 is( $modified_item->{new}, 'updated_value', q|ToggleNewStatus: Age = 10 : The new value is not updated|);
136 $rules[0]->{age} = 5;
137 $rules[0]->{substitutions}[0]{value} = 'new_updated_value5';
138 C4::Items::ToggleNewStatus( { rules => \@rules } );
139 $modified_item = C4::Items::GetItem( $itemnumber );
140 is( $modified_item->{new}, 'new_updated_value5', q|ToggleNewStatus: Age = 5 : The new value is updated|);
142 $rules[0]->{age} = '';
143 $rules[0]->{substitutions}[0]{value} = 'new_updated_value_empty_string';
144 C4::Items::ToggleNewStatus( { rules => \@rules } );
145 $modified_item = C4::Items::GetItem( $itemnumber );
146 is( $modified_item->{new}, 'new_updated_value_empty_string', q|ToggleNewStatus: Age = '' : The new value is updated|);
148 $rules[0]->{age} = undef;
149 $rules[0]->{substitutions}[0]{value} = 'new_updated_value_undef';
150 C4::Items::ToggleNewStatus( { rules => \@rules } );
151 $modified_item = C4::Items::GetItem( $itemnumber );
152 is( $modified_item->{new}, 'new_updated_value_undef', q|ToggleNewStatus: Age = undef : The new value is updated|);
159 field => 'items.ccode',
165 field => 'items.new',
173 C4::Items::ToggleNewStatus( { rules => \@rules } );
175 $modified_item = C4::Items::GetItem( $itemnumber );
176 is( $modified_item->{new}, '', q|ToggleNewStatus: The new value is empty|);
177 $marc_item = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
178 is( $marc_item->subfield($tagfield, $new_tagfield), undef, q|ToggleNewStatus: The new field is removed from the item marc| );
180 # conditions multiple
185 field => 'items.ccode',
189 field => 'items.homebranch',
195 field => 'items.new',
196 value => 'new_value',
203 C4::Items::ToggleNewStatus( { rules => \@rules } );
205 $modified_item = C4::Items::GetItem( $itemnumber );
206 is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: all match, the new value is updated|);
212 field => 'items.ccode',
216 field => 'items.homebranch',
217 value => 'DONT_EXIST',
222 field => 'items.new',
223 value => 'new_updated_value',
230 C4::Items::ToggleNewStatus( { rules => \@rules } );
232 $modified_item = C4::Items::GetItem( $itemnumber );
233 is( $modified_item->{new}, 'new_value', q|ToggleNewStatus: conditions multiple: at least 1 condition does not match, the new value is not updated|);
239 field => 'items.ccode',
243 field => 'items.homebranch',
249 field => 'items.new',
250 value => 'new_updated_value',
257 C4::Items::ToggleNewStatus( { rules => \@rules } );
259 $modified_item = C4::Items::GetItem( $itemnumber );
260 is( $modified_item->{new}, 'new_updated_value', q|ToggleNewStatus: conditions multiple: the 2 conditions match, the new value is updated|);
266 field => 'biblioitems.itemtype',
267 value => 'ITEMTYPE_T',
272 field => 'items.new',
273 value => 'another_new_updated_value',
280 C4::Items::ToggleNewStatus( { rules => \@rules } );
282 $modified_item = C4::Items::GetItem( $itemnumber );
283 is( $modified_item->{new}, 'another_new_updated_value', q|ToggleNewStatus: conditions on biblioitems|);