Bug 13726: Make Koha::Acq::Bookseller using Koha::Object
[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::Acquisition::Booksellers;
7 use_ok('C4::Serials');
8
9 use Koha::DateUtils qw( dt_from_string output_pref );
10
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
14
15 $dbh->do(q|DELETE FROM issues|);
16 $dbh->do(q|DELETE FROM subscription|);
17
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"
23 });
24
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
31 });
32
33 my $record = MARC::Record->new();
34 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
35
36 my $sample_supplier1 = {
37     name          => 'Name1',
38     address1      => 'address1_1',
39     address2      => 'address1-2',
40     address3      => 'address1_2',
41     address4      => 'address1_2',
42     postal        => 'postal1',
43     phone         => 'phone1',
44     accountnumber => 'accountnumber1',
45     fax           => 'fax1',
46     url           => 'url1',
47     active        => 1,
48     gstreg        => 1,
49     listincgst    => 1,
50     invoiceincgst => 1,
51     tax_rate       => '1.0000',
52     discount      => '1.0000',
53     notes         => 'notes1',
54     deliverytime  => undef
55 };
56 my $sample_supplier2 = {
57     name          => 'Name2',
58     address1      => 'address1_2',
59     address2      => 'address2-2',
60     address3      => 'address3_2',
61     address4      => 'address4_2',
62     postal        => 'postal2',
63     phone         => 'phone2',
64     accountnumber => 'accountnumber2',
65     fax           => 'fax2',
66     url           => 'url2',
67     active        => 1,
68     gstreg        => 1,
69     listincgst    => 1,
70     invoiceincgst => 1,
71     tax_rate       => '2.0000',
72     discount      => '2.0000',
73     notes         => 'notes2',
74     deliverytime  => 2
75 };
76
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;
81
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');
85
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
93 );
94 $supplierlist = GetSuppliersWithLateIssues();
95 is ( scalar(@$supplierlist), 0, 'There is still no late issues yet');
96
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
104 );
105
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
113 );
114
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
122 );
123
124
125 $supplierlist = GetSuppliersWithLateIssues();
126 is ( scalar(@$supplierlist), 2, '2 suppliers should have issues in late');
127
128 is( GetLateOrMissingIssues(), undef, 'GetLateOrMissingIssues should return undef without parameter' );
129
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' );
132
133 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
134 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
135
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' );
139
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 );
143
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)' );
146
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' );
149
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' );
154
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' );
159
160
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' );