Bug 12178: Update serial status to "claimed" when exporting to CSV
[koha.git] / t / db_dependent / Serials / Claims.t
blobb887d78a052aea32b3846669adefa9de1bb5775a
1 use Modern::Perl;
2 use Test::More tests => 17;
4 use C4::Acquisition;
5 use C4::Bookseller;
6 use C4::Budgets;
7 use_ok('C4::Serials');
9 use Koha::DateUtils qw( dt_from_string output_pref );
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
15 $dbh->do(q|DELETE FROM issues|);
16 $dbh->do(q|DELETE FROM subscription|);
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 });
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 });
33 my $record = MARC::Record->new();
34 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio($record, '');
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     gstrate       => '1.0000',
52     discount      => '1.0000',
53     notes         => 'notes1',
54     deliverytime  => undef
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     gstrate       => '2.0000',
72     discount      => '2.0000',
73     notes         => 'notes2',
74     deliverytime  => 2
77 my $supplier_id1 = C4::Bookseller::AddBookseller($sample_supplier1);
78 my $supplier_id2 = C4::Bookseller::AddBookseller($sample_supplier2);
80 my $supplierlist = eval { GetSuppliersWithLateIssues() };
81 is( length($@), 0, "No SQL problem in GetSuppliersWithLateIssues" );
82 is ( scalar(@$supplierlist), 0, 'There is no late issues yet');
84 my $subscriptionid_not_late = NewSubscription(
85     undef,      $branchcode,     $supplier_id1, undef, $budget_id, $biblionumber,
86     '2013-01-01', undef, undef, undef,  undef,
87     undef,      undef,  undef, undef, undef, undef,
88     1,          "notes",undef, '9999-01-01', undef, undef,
89     undef,       undef,  0,    "intnotes",  0,
90     undef, undef, 0,          undef,         '2013-12-31', 0
92 $supplierlist = GetSuppliersWithLateIssues();
93 is ( scalar(@$supplierlist), 0, 'There is still no late issues yet');
95 my $subscriptionid_inlate1 = NewSubscription(
96     undef,      $branchcode,     $supplier_id1, undef, $budget_id, $biblionumber,
97     '2013-01-01', undef, undef, undef,  undef,
98     undef,      undef,  undef, undef, undef, undef,
99     1,          "notes",undef, '2013-01-01', undef, undef,
100     undef,       undef,  0,    "intnotes",  0,
101     undef, undef, 0,          undef,         '2013-12-31', 0
104 my $subscriptionid_inlate2 = NewSubscription(
105     undef,      $branchcode,     $supplier_id2, undef, $budget_id, $biblionumber,
106     '2013-01-01', undef, undef, undef,  undef,
107     undef,      undef,  undef, undef, undef, undef,
108     1,          "notes",undef, '2013-01-01', undef, undef,
109     undef,       undef,  0,    "intnotes",  0,
110     undef, undef, 0,          undef,         '2013-12-31', 0
113 my $subscriptionid_inlate3 = NewSubscription(
114     undef,      $branchcode,     $supplier_id2, undef, $budget_id, $biblionumber,
115     '2013-01-02', undef, undef, undef,  undef,
116     undef,      undef,  undef, undef, undef, undef,
117     1,          "notes",undef, '2013-01-02', undef, undef,
118     undef,       undef,  0,    "intnotes",  0,
119     undef, undef, 0,          undef,         '2013-12-31', 0
123 $supplierlist = GetSuppliersWithLateIssues();
124 is ( scalar(@$supplierlist), 2, '2 suppliers should have issues in late');
126 is( GetLateOrMissingIssues(), undef, 'GetLateOrMissingIssues should return undef without parameter' );
128 my @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id1 );
129 is( scalar(@late_or_missing_issues), 1, 'supplier 1 should have 1 issue in late' );
131 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
132 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
134 is( exists $late_or_missing_issues[0]->{claimdate}, 1, 'GetLateOrMissingIssues returns claimdate' );
135 is( exists $late_or_missing_issues[0]->{claims_count}, 1, 'GetLateOrMissingIssues returns claims_count' );
136 is( $late_or_missing_issues[0]->{claims_count}, 0, 'The issues should not habe been claimed yet' );
138 is( updateClaim(), undef, 'updateClaim should return undef if not param passed' );
139 my $serialid_to_claim = $late_or_missing_issues[0]->{serialid};
140 updateClaim( $serialid_to_claim );
142 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
143 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late (already claimed issues are returns)' );
145 my ( $serial_claimed ) = grep { ($_->{serialid} == $serialid_to_claim) ? $_ : () } @late_or_missing_issues;
146 is( $serial_claimed->{claims_count}, 1, 'The serial should have been claimed' );
148 my @serials_to_claim = map { $_->{serialid} } @late_or_missing_issues;
149 updateClaim( \@serials_to_claim );
150 @late_or_missing_issues = GetLateOrMissingIssues( $supplier_id2);
151 is( scalar(@late_or_missing_issues), 2, 'supplier 2 should have 2 issues in late' );
153 ( $serial_claimed ) = grep { ($_->{serialid} == $serials_to_claim[0]) ? $_ : () } @late_or_missing_issues;
154 is( $serial_claimed->{claims_count}, 2, 'The serial should have been claimed' );
155 ( $serial_claimed ) = grep { ($_->{serialid} == $serials_to_claim[1]) ? $_ : () } @late_or_missing_issues;
156 is( $serial_claimed->{claims_count}, 1, 'The serial should have been claimed' );
159 my $today = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
160 # FIXME: This test should pass. The GetLateOrMissingIssues should not deal with date format!
161 #is( $serial_claimed->{claimdate}, $today, 'The serial should have been claimed today' );