Bug 5342: (qa-followup) FIX some issues
[koha.git] / t / db_dependent / Serials / Claims.t
1 use Modern::Perl;
2 use Test::More tests => 13;
3
4 use C4::Acquisition;
5 use C4::Budgets;
6 use_ok('C4::Serials');
7
8 use Koha::DateUtils qw( dt_from_string output_pref );
9
10 my $dbh = C4::Context->dbh;
11 $dbh->{AutoCommit} = 0;
12 $dbh->{RaiseError} = 1;
13
14 $dbh->do(q|DELETE FROM issues|);
15
16 my $branchcode = 'CPL';
17 my $bpid = AddBudgetPeriod({
18     budget_period_startdate   => '2015-01-01',
19     budget_period_enddate     => '2015-12-31',
20     budget_period_description => "budget desc"
21 });
22
23 my $budget_id = AddBudget({
24     budget_code        => "ABCD",
25     budget_amount      => "123.132",
26     budget_name        => "Périodiques",
27     budget_notes       => "This is a note",
28     budget_period_id   => $bpid
29 });
30
31 my $record = MARC::Record->new();
32 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
33
34 my $sample_supplier1 = {
35     name          => 'Name1',
36     address1      => 'address1_1',
37     address2      => 'address1-2',
38     address3      => 'address1_2',
39     address4      => 'address1_2',
40     postal        => 'postal1',
41     phone         => 'phone1',
42     accountnumber => 'accountnumber1',
43     fax           => 'fax1',
44     url           => 'url1',
45     active        => 1,
46     gstreg        => 1,
47     listincgst    => 1,
48     invoiceincgst => 1,
49     gstrate       => '1.0000',
50     discount      => '1.0000',
51     notes         => 'notes1',
52     deliverytime  => undef
53 };
54 my $sample_supplier2 = {
55     name          => 'Name2',
56     address1      => 'address1_2',
57     address2      => 'address2-2',
58     address3      => 'address3_2',
59     address4      => 'address4_2',
60     postal        => 'postal2',
61     phone         => 'phone2',
62     accountnumber => 'accountnumber2',
63     fax           => 'fax2',
64     url           => 'url2',
65     active        => 1,
66     gstreg        => 1,
67     listincgst    => 1,
68     invoiceincgst => 1,
69     gstrate       => '2.0000',
70     discount      => '2.0000',
71     notes         => 'notes2',
72     deliverytime  => 2
73 };
74
75 my $supplier_id1 = C4::Bookseller::AddBookseller($sample_supplier1);
76 my $supplier_id2 = C4::Bookseller::AddBookseller($sample_supplier2);
77
78 my $supplierlist = eval { GetSuppliersWithLateIssues() };
79 is( length($@), 0, "No SQL problem in GetSuppliersWithLateIssues" );
80 is ( scalar(@$supplierlist), 0, 'There is no late issues yet');
81
82 my $subscriptionid_not_late = NewSubscription(
83     undef,      $branchcode,     $supplier_id1, undef, $budget_id, $biblionumber,
84     '2013-01-01', undef, undef, undef,  undef,
85     undef,      undef,  undef, undef, undef, undef,
86     1,          "notes",undef, '9999-01-01', undef, undef,
87     undef,       undef,  0,    "intnotes",  0,
88     undef, undef, 0,          undef,         '2013-12-31', 0
89 );
90 $supplierlist = GetSuppliersWithLateIssues();
91 is ( scalar(@$supplierlist), 0, 'There is still no late issues yet');
92
93 my $subscriptionid_inlate1 = NewSubscription(
94     undef,      $branchcode,     $supplier_id1, undef, $budget_id, $biblionumber,
95     '2013-01-01', undef, undef, undef,  undef,
96     undef,      undef,  undef, undef, undef, undef,
97     1,          "notes",undef, '2013-01-01', undef, undef,
98     undef,       undef,  0,    "intnotes",  0,
99     undef, undef, 0,          undef,         '2013-12-31', 0
100 );
101
102 my $subscriptionid_inlate2 = NewSubscription(
103     undef,      $branchcode,     $supplier_id2, undef, $budget_id, $biblionumber,
104     '2013-01-01', undef, undef, undef,  undef,
105     undef,      undef,  undef, undef, undef, undef,
106     1,          "notes",undef, '2013-01-01', undef, undef,
107     undef,       undef,  0,    "intnotes",  0,
108     undef, undef, 0,          undef,         '2013-12-31', 0
109 );
110
111 my $subscriptionid_inlate3 = NewSubscription(
112     undef,      $branchcode,     $supplier_id2, undef, $budget_id, $biblionumber,
113     '2013-01-02', undef, undef, undef,  undef,
114     undef,      undef,  undef, undef, undef, undef,
115     1,          "notes",undef, '2013-01-02', undef, undef,
116     undef,       undef,  0,    "intnotes",  0,
117     undef, undef, 0,          undef,         '2013-12-31', 0
118 );
119
120
121 $supplierlist = GetSuppliersWithLateIssues();
122 is ( scalar(@$supplierlist), 2, '2 suppliers should have issues in late');
123
124 is( GetLateOrMissingIssues(), undef, 'GetLateOrMissingIssues should return undef without parameter' );
125
126 my @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id1 );
127 is( scalar(@late_or_missing_issues), 1, 'supplier 1 should have 1 issue in late' );
128
129 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
130 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
131
132 is( exists $late_or_missing_issues[0]->{claimdate}, 1, 'GetLateOrMissingIssues returns claimdate' );
133 is( exists $late_or_missing_issues[0]->{claims_count}, 1, 'GetLateOrMissingIssues returns claims_count' );
134 is( $late_or_missing_issues[0]->{claims_count}, 0, 'The issues should not habe been claimed yet' );
135
136 my $serialid_to_claim = $late_or_missing_issues[0]->{serialid};
137 updateClaim( $serialid_to_claim );
138
139 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
140 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late (already claimed issues are returns)' );
141
142 my ( $serial_claimed ) = grep { ($_->{serialid} == $serialid_to_claim) ? $_ : () } @late_or_missing_issues;
143 is( $serial_claimed->{claims_count}, 1, 'The serial should have been claimed' );
144
145 my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
146 # FIXME: This test should pass. The GetLateOrMissingIssues should not deal with date format!
147 #is( $serial_claimed->{claimdate}, $today, 'The serial should have been claimed today' );