Bug 26384: Fix executable flags
[koha.git] / t / db_dependent / Serials / Claims.t
1 use Modern::Perl;
2 use Test::More tests => 17;
3
4 use C4::Acquisition;
5 use C4::Budgets;
6 use Koha::Database;
7 use Koha::Acquisition::Booksellers;
8 use_ok('C4::Serials');
9
10 use Koha::DateUtils qw( dt_from_string output_pref );
11
12 my $schema = Koha::Database->new->schema;
13 $schema->storage->txn_begin;
14 my $dbh = C4::Context->dbh;
15
16 $dbh->do(q|DELETE FROM issues|);
17 $dbh->do(q|DELETE FROM subscription|);
18
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"
24 });
25
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
32 });
33
34 my $record = MARC::Record->new();
35 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
36
37 my $sample_supplier1 = {
38     name          => 'Name1',
39     address1      => 'address1_1',
40     address2      => 'address1-2',
41     address3      => 'address1_2',
42     address4      => 'address1_2',
43     postal        => 'postal1',
44     phone         => 'phone1',
45     accountnumber => 'accountnumber1',
46     fax           => 'fax1',
47     url           => 'url1',
48     active        => 1,
49     gstreg        => 1,
50     listincgst    => 1,
51     invoiceincgst => 1,
52     tax_rate       => '1.0000',
53     discount      => '1.0000',
54     notes         => 'notes1',
55     deliverytime  => undef
56 };
57 my $sample_supplier2 = {
58     name          => 'Name2',
59     address1      => 'address1_2',
60     address2      => 'address2-2',
61     address3      => 'address3_2',
62     address4      => 'address4_2',
63     postal        => 'postal2',
64     phone         => 'phone2',
65     accountnumber => 'accountnumber2',
66     fax           => 'fax2',
67     url           => 'url2',
68     active        => 1,
69     gstreg        => 1,
70     listincgst    => 1,
71     invoiceincgst => 1,
72     tax_rate       => '2.0000',
73     discount      => '2.0000',
74     notes         => 'notes2',
75     deliverytime  => 2
76 };
77
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;
82
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');
86
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
94 );
95 $supplierlist = GetSuppliersWithLateIssues();
96 is ( scalar(@$supplierlist), 0, 'There is still no late issues yet');
97
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
105 );
106
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
114 );
115
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
123 );
124
125
126 $supplierlist = GetSuppliersWithLateIssues();
127 is ( scalar(@$supplierlist), 2, '2 suppliers should have issues in late');
128
129 is( GetLateOrMissingIssues(), undef, 'GetLateOrMissingIssues should return undef without parameter' );
130
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' );
133
134 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
135 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
136
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' );
140
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 );
144
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)' );
147
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' );
150
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' );
155
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' );
160
161
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' );