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