2 use Test::More tests => 17;
6 use Koha::Acquisition::Booksellers;
9 use Koha::DateUtils qw( dt_from_string output_pref );
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
15 $dbh->do(q|DELETE FROM issues|);
16 $dbh->do(q|DELETE FROM subscription|);
18 my $branchcode = 'CPL';
19 my $bpid = AddBudgetPeriod({
20 budget_period_startdate => '2015-01-01',
21 budget_period_enddate => '2015-12-31',
22 budget_period_description => "budget desc"
25 my $budget_id = AddBudget({
26 budget_code => "ABCD",
27 budget_amount => "123.132",
28 budget_name => "Périodiques",
29 budget_notes => "This is a note",
30 budget_period_id => $bpid
33 my $record = MARC::Record->new();
34 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
36 my $sample_supplier1 = {
38 address1 => 'address1_1',
39 address2 => 'address1-2',
40 address3 => 'address1_2',
41 address4 => 'address1_2',
44 accountnumber => 'accountnumber1',
56 my $sample_supplier2 = {
58 address1 => 'address1_2',
59 address2 => 'address2-2',
60 address3 => 'address3_2',
61 address4 => 'address4_2',
64 accountnumber => 'accountnumber2',
77 my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
78 my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
79 my $supplier_id1 = $supplier1->id;
80 my $supplier_id2 = $supplier2->id;
82 my $supplierlist = eval { GetSuppliersWithLateIssues() };
83 is( length($@), 0, "No SQL problem in GetSuppliersWithLateIssues" );
84 is ( scalar(@$supplierlist), 0, 'There is no late issues yet');
86 my $subscriptionid_not_late = NewSubscription(
87 undef, $branchcode, $supplier_id1, undef, $budget_id, $biblionumber,
88 '2013-01-01', undef, undef, undef, undef,
89 undef, undef, undef, undef, undef, undef,
90 1, "notes",undef, '9999-01-01', undef, undef,
91 undef, undef, 0, "intnotes", 0,
92 undef, undef, 0, undef, '2013-12-31', 0
94 $supplierlist = GetSuppliersWithLateIssues();
95 is ( scalar(@$supplierlist), 0, 'There is still no late issues yet');
97 my $subscriptionid_inlate1 = NewSubscription(
98 undef, $branchcode, $supplier_id1, undef, $budget_id, $biblionumber,
99 '2013-01-01', undef, undef, undef, undef,
100 undef, undef, undef, undef, undef, undef,
101 1, "notes",undef, '2013-01-01', undef, undef,
102 undef, undef, 0, "intnotes", 0,
103 undef, undef, 0, undef, '2013-12-31', 0
106 my $subscriptionid_inlate2 = NewSubscription(
107 undef, $branchcode, $supplier_id2, undef, $budget_id, $biblionumber,
108 '2013-01-01', undef, undef, undef, undef,
109 undef, undef, undef, undef, undef, undef,
110 1, "notes",undef, '2013-01-01', undef, undef,
111 undef, undef, 0, "intnotes", 0,
112 undef, undef, 0, undef, '2013-12-31', 0
115 my $subscriptionid_inlate3 = NewSubscription(
116 undef, $branchcode, $supplier_id2, undef, $budget_id, $biblionumber,
117 '2013-01-02', undef, undef, undef, undef,
118 undef, undef, undef, undef, undef, undef,
119 1, "notes",undef, '2013-01-02', undef, undef,
120 undef, undef, 0, "intnotes", 0,
121 undef, undef, 0, undef, '2013-12-31', 0
125 $supplierlist = GetSuppliersWithLateIssues();
126 is ( scalar(@$supplierlist), 2, '2 suppliers should have issues in late');
128 is( GetLateOrMissingIssues(), undef, 'GetLateOrMissingIssues should return undef without parameter' );
130 my @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id1 );
131 is( scalar(@late_or_missing_issues), 1, 'supplier 1 should have 1 issue in late' );
133 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
134 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
136 is( exists $late_or_missing_issues[0]->{claimdate}, 1, 'GetLateOrMissingIssues returns claimdate' );
137 is( exists $late_or_missing_issues[0]->{claims_count}, 1, 'GetLateOrMissingIssues returns claims_count' );
138 is( $late_or_missing_issues[0]->{claims_count}, 0, 'The issues should not habe been claimed yet' );
140 is( updateClaim(), undef, 'updateClaim should return undef if not param passed' );
141 my $serialid_to_claim = $late_or_missing_issues[0]->{serialid};
142 updateClaim( $serialid_to_claim );
144 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
145 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late (already claimed issues are returns)' );
147 my ( $serial_claimed ) = grep { ($_->{serialid} == $serialid_to_claim) ? $_ : () } @late_or_missing_issues;
148 is( $serial_claimed->{claims_count}, 1, 'The serial should have been claimed' );
150 my @serials_to_claim = map { $_->{serialid} } @late_or_missing_issues;
151 updateClaim( \@serials_to_claim );
152 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
153 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
155 ( $serial_claimed ) = grep { ($_->{serialid} == $serials_to_claim[0]) ? $_ : () } @late_or_missing_issues;
156 is( $serial_claimed->{claims_count}, 2, 'The serial should have been claimed' );
157 ( $serial_claimed ) = grep { ($_->{serialid} == $serials_to_claim[1]) ? $_ : () } @late_or_missing_issues;
158 is( $serial_claimed->{claims_count}, 1, 'The serial should have been claimed' );
161 my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
162 # FIXME: This test should pass. The GetLateOrMissingIssues should not deal with date format!
163 #is( $serial_claimed->{claimdate}, $today, 'The serial should have been claimed today' );