2 use Test::More tests => 17;
7 use Koha::Acquisition::Booksellers;
10 use Koha::DateUtils qw( dt_from_string output_pref );
12 my $schema = Koha::Database->new->schema;
13 $schema->storage->txn_begin;
14 my $dbh = C4::Context->dbh;
16 $dbh->do(q|DELETE FROM issues|);
17 $dbh->do(q|DELETE FROM subscription|);
19 my $branchcode = 'CPL';
20 my $bpid = AddBudgetPeriod({
21 budget_period_startdate => '2015-01-01',
22 budget_period_enddate => '2015-12-31',
23 budget_period_description => "budget desc"
26 my $budget_id = AddBudget({
27 budget_code => "ABCD",
28 budget_amount => "123.132",
29 budget_name => "Périodiques",
30 budget_notes => "This is a note",
31 budget_period_id => $bpid
34 my $record = MARC::Record->new();
35 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
37 my $sample_supplier1 = {
39 address1 => 'address1_1',
40 address2 => 'address1-2',
41 address3 => 'address1_2',
42 address4 => 'address1_2',
45 accountnumber => 'accountnumber1',
57 my $sample_supplier2 = {
59 address1 => 'address1_2',
60 address2 => 'address2-2',
61 address3 => 'address3_2',
62 address4 => 'address4_2',
65 accountnumber => 'accountnumber2',
78 my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
79 my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
80 my $supplier_id1 = $supplier1->id;
81 my $supplier_id2 = $supplier2->id;
83 my $supplierlist = eval { GetSuppliersWithLateIssues() };
84 is( length($@), 0, "No SQL problem in GetSuppliersWithLateIssues" );
85 is ( scalar(@$supplierlist), 0, 'There is no late issues yet');
87 my $subscriptionid_not_late = NewSubscription(
88 undef, $branchcode, $supplier_id1, undef, $budget_id, $biblionumber,
89 '2013-01-01', undef, undef, undef, undef,
90 undef, undef, undef, undef, undef, undef,
91 1, "notes",undef, '9999-01-01', undef, undef,
92 undef, undef, 0, "intnotes", 0,
93 undef, undef, 0, undef, '2013-12-31', 0
95 $supplierlist = GetSuppliersWithLateIssues();
96 is ( scalar(@$supplierlist), 0, 'There is still no late issues yet');
98 my $subscriptionid_inlate1 = NewSubscription(
99 undef, $branchcode, $supplier_id1, undef, $budget_id, $biblionumber,
100 '2013-01-01', undef, undef, undef, undef,
101 undef, undef, undef, undef, undef, undef,
102 1, "notes",undef, '2013-01-01', undef, undef,
103 undef, undef, 0, "intnotes", 0,
104 undef, undef, 0, undef, '2013-12-31', 0
107 my $subscriptionid_inlate2 = NewSubscription(
108 undef, $branchcode, $supplier_id2, undef, $budget_id, $biblionumber,
109 '2013-01-01', undef, undef, undef, undef,
110 undef, undef, undef, undef, undef, undef,
111 1, "notes",undef, '2013-01-01', undef, undef,
112 undef, undef, 0, "intnotes", 0,
113 undef, undef, 0, undef, '2013-12-31', 0
116 my $subscriptionid_inlate3 = NewSubscription(
117 undef, $branchcode, $supplier_id2, undef, $budget_id, $biblionumber,
118 '2013-01-02', undef, undef, undef, undef,
119 undef, undef, undef, undef, undef, undef,
120 1, "notes",undef, '2013-01-02', undef, undef,
121 undef, undef, 0, "intnotes", 0,
122 undef, undef, 0, undef, '2013-12-31', 0
126 $supplierlist = GetSuppliersWithLateIssues();
127 is ( scalar(@$supplierlist), 2, '2 suppliers should have issues in late');
129 is( GetLateOrMissingIssues(), undef, 'GetLateOrMissingIssues should return undef without parameter' );
131 my @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id1 );
132 is( scalar(@late_or_missing_issues), 1, 'supplier 1 should have 1 issue in late' );
134 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
135 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
137 is( exists $late_or_missing_issues[0]->{claimdate}, 1, 'GetLateOrMissingIssues returns claimdate' );
138 is( exists $late_or_missing_issues[0]->{claims_count}, 1, 'GetLateOrMissingIssues returns claims_count' );
139 is( $late_or_missing_issues[0]->{claims_count}, 0, 'The issues should not habe been claimed yet' );
141 is( updateClaim(), undef, 'updateClaim should return undef if not param passed' );
142 my $serialid_to_claim = $late_or_missing_issues[0]->{serialid};
143 updateClaim( $serialid_to_claim );
145 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
146 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late (already claimed issues are returns)' );
148 my ( $serial_claimed ) = grep { ($_->{serialid} == $serialid_to_claim) ? $_ : () } @late_or_missing_issues;
149 is( $serial_claimed->{claims_count}, 1, 'The serial should have been claimed' );
151 my @serials_to_claim = map { $_->{serialid} } @late_or_missing_issues;
152 updateClaim( \@serials_to_claim );
153 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
154 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
156 ( $serial_claimed ) = grep { ($_->{serialid} == $serials_to_claim[0]) ? $_ : () } @late_or_missing_issues;
157 is( $serial_claimed->{claims_count}, 2, 'The serial should have been claimed' );
158 ( $serial_claimed ) = grep { ($_->{serialid} == $serials_to_claim[1]) ? $_ : () } @late_or_missing_issues;
159 is( $serial_claimed->{claims_count}, 1, 'The serial should have been claimed' );
162 my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
163 # FIXME: This test should pass. The GetLateOrMissingIssues should not deal with date format!
164 #is( $serial_claimed->{claimdate}, $today, 'The serial should have been claimed today' );