4 use Test::More tests => 16;
8 use_ok('C4::Overdues');
9 can_ok('C4::Overdues', 'GetOverdueMessageTransportTypes');
10 can_ok('C4::Overdues', 'GetBranchcodesWithOverdueRules');
12 my $dbh = C4::Context->dbh;
13 $dbh->{AutoCommit} = 0;
14 $dbh->{RaiseError} = 1;
16 $dbh->do(q|DELETE FROM letter|);
17 $dbh->do(q|DELETE FROM message_queue|);
18 $dbh->do(q|DELETE FROM message_transport_types|);
19 $dbh->do(q|DELETE FROM overduerules|);
20 $dbh->do(q|DELETE FROM overduerules_transport_types|);
23 INSERT INTO message_transport_types( message_transport_type ) VALUES ('email'), ('phone'), ('print'), ('sms')
27 INSERT INTO overduerules ( branchcode, categorycode ) VALUES
35 INSERT INTO overduerules_transport_types( branchcode, categorycode, letternumber, message_transport_type ) VALUES
36 ('CPL', 'PT', 1, 'email'),
37 ('CPL', 'PT', 2, 'sms'),
38 ('CPL', 'PT', 3, 'email'),
39 ('CPL', 'YA', 3, 'print'),
40 ('', 'PT', 1, 'email'),
41 ('', 'PT', 2, 'email'),
44 ('', 'PT', 3, 'email'),
45 ('', 'PT', 3, 'print'),
51 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT');
52 is( $mtts, undef, 'GetOverdueMessageTransportTypes: returns undef if no letternumber given' );
54 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', undef, 1);
55 is( $mtts, undef, 'GetOverdueMessageTransportTypes: returns undef if no categorycode given' );
57 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL');
58 is( $mtts, undef, 'GetOverdueMessageTransportTypes: returns undef if no letternumber and categorycode given' );
60 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT', 1);
61 is_deeply( $mtts, ['email'], 'GetOverdueMessageTransportTypes: first overdue is by email for PT (CPL)' );
63 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT', 2);
64 is_deeply( $mtts, ['sms'], 'GetOverdueMessageTransportTypes: second overdue is by sms for PT (CPL)' );
66 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('CPL', 'PT', 3);
67 is_deeply( $mtts, ['email'], 'GetOverdueMessageTransportTypes: third overdue is by email for PT (CPL)' );
69 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('', 'PT', 1);
70 is_deeply( $mtts, ['email'], 'GetOverdueMessageTransportTypes: first overdue is by email for PT (default)' );
72 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('', 'PT', 2);
73 is_deeply( $mtts, ['email', 'sms'], 'GetOverdueMessageTransportTypes: second overdue is by email and sms for PT (default)' );
75 $mtts = C4::Overdues::GetOverdueMessageTransportTypes('', 'PT', 3);
76 is_deeply( $mtts, ['print', 'sms', 'email'], 'GetOverdueMessageTransportTypes: third overdue is by print, sms and email for PT (default). With print in first.' );
78 # Test GetBranchcodesWithOverdueRules
79 $dbh->do(q|DELETE FROM overduerules|);
81 INSERT INTO overduerules
82 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
84 ( '', '', 1, 'LETTER_CODE1', 1, 5, 'LETTER_CODE2', 1, 10, 'LETTER_CODE3', 1 )
87 my $all_branches = C4::Branch::GetBranches;
88 my @branchcodes = keys %$all_branches;
90 my @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
91 is_deeply( [ sort @overdue_branches ], [ sort @branchcodes ], 'If a default rule exists, all branches should be returned' );
94 INSERT INTO overduerules
95 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
97 ( 'CPL', '', 1, 'LETTER_CODE1', 1, 5, 'LETTER_CODE2', 1, 10, 'LETTER_CODE3', 1 )
100 @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
101 is_deeply( [ sort @overdue_branches ], [ sort @branchcodes ], 'If a default rule exists and a specific rule exists, all branches should be returned' );
103 $dbh->do(q|DELETE FROM overduerules|);
105 INSERT INTO overduerules
106 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
108 ( 'CPL', '', 1, 'LETTER_CODE1', 1, 5, 'LETTER_CODE2', 1, 10, 'LETTER_CODE3', 1 )
111 @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
112 is_deeply( \@overdue_branches, ['CPL'] , 'If only a specific rule exist, only 1 branch should be returned' );
114 $dbh->do(q|DELETE FROM overduerules|);
116 INSERT INTO overduerules
117 ( branchcode,categorycode, delay1,letter1,debarred1, delay2,letter2,debarred2, delay3,letter3,debarred3 )
119 ( 'CPL', '', 1, 'LETTER_CODE1_CPL', 1, 5, 'LETTER_CODE2_CPL', 1, 10, 'LETTER_CODE3_CPL', 1 ),
120 ( 'MPL', '', 1, 'LETTER_CODE1_MPL', 1, 5, 'LETTER_CODE2_MPL', 1, 10, 'LETTER_CODE3_MPL', 1 )
123 @overdue_branches = C4::Overdues::GetBranchcodesWithOverdueRules();
124 is_deeply( \@overdue_branches, ['CPL', 'MPL'] , 'If only 2 specific rules exist, 2 branches should be returned' );