View | Details | Raw Unified | Return to bug 20510
Collapse All | Expand All

(-)a/C4/Stats.pm (-46 lines)
Lines 32-38 BEGIN { Link Here
32
	@ISA    = qw(Exporter);
32
	@ISA    = qw(Exporter);
33
	@EXPORT = qw(
33
	@EXPORT = qw(
34
		&UpdateStats
34
		&UpdateStats
35
		&TotalPaid
36
	);
35
	);
37
}
36
}
38
37
Lines 143-193 sub UpdateStats { Link Here
143
    );
142
    );
144
}
143
}
145
144
146
=head2 TotalPaid
147
148
  @total = &TotalPaid ( $time, [$time2], [$spreadsheet ]);
149
150
Returns an array containing the payments and writeoffs made between two dates
151
C<$time> and C<$time2>, or on a specific one, or from C<$time> onwards.
152
153
C<$time> param is mandatory.
154
If C<$time> eq 'today', returns are limited to the current day
155
If C<$time2> eq '', results are returned from C<$time> onwards.
156
If C<$time2> is undef, returns are limited to C<$time>
157
C<$spreadsheet> param is optional and controls the sorting of the results.
158
159
Returns undef if no param is given
160
161
=cut
162
163
sub TotalPaid {
164
    my ( $time, $time2, $spreadsheet ) = @_;
165
    return () unless (defined $time);
166
    $time2 = $time unless $time2;
167
    my $dbh   = C4::Context->dbh;
168
    my $query = "SELECT * FROM statistics 
169
  LEFT JOIN borrowers ON statistics.borrowernumber= borrowers.borrowernumber
170
  WHERE (statistics.type='payment' OR statistics.type='writeoff') ";
171
    if ( $time eq 'today' ) {
172
# FIXME wrong condition. Now() will not get all the payments of the day but of a specific timestamp
173
        $query .= " AND datetime = now()";
174
    } else {
175
        $query .= " AND datetime > '$time'";    # FIXME: use placeholders
176
    }
177
    if ( $time2 ne '' ) {
178
        $query .= " AND datetime < '$time2'";   # FIXME: use placeholders
179
    }
180
# FIXME if $time2 is undef, query will be "AND datetime > $time AND AND datetime < $time"
181
# Operators should probably be <= and >=
182
    if ($spreadsheet) {
183
        $query .= " ORDER BY branch, type";
184
    }
185
    $debug and warn "TotalPaid query: $query";
186
    my $sth = $dbh->prepare($query);
187
    $sth->execute();
188
    return @{$sth->fetchall_arrayref({})};
189
}
190
191
1;
145
1;
192
__END__
146
__END__
193
147
(-)a/t/db_dependent/Stats.t (-10 / +2 lines)
Lines 3-18 Link Here
3
use Modern::Perl;
3
use Modern::Perl;
4
use C4::Stats;
4
use C4::Stats;
5
5
6
use Test::More tests => 20;
6
use Test::More tests => 19;
7
7
8
BEGIN {
8
BEGIN {
9
    use_ok('C4::Stats');
9
    use_ok('C4::Stats');
10
}
10
}
11
can_ok(
11
can_ok(
12
    'C4::Stats',
12
    'C4::Stats',
13
    qw(UpdateStats
13
    qw(UpdateStats)
14
    TotalPaid
15
      )
16
);
14
);
17
15
18
#Start transaction
16
#Start transaction
Lines 164-174 $line = ${ $sth->fetchall_arrayref( {} ) }[0]; Link Here
164
is( $line->{location}, undef,
162
is( $line->{location}, undef,
165
    "UpdateStats sets location to NULL if undef is passed in." );
163
    "UpdateStats sets location to NULL if undef is passed in." );
166
164
167
#
168
# Test TotalPaid
169
#
170
171
is (TotalPaid (),undef,"TotalPaid returns undef if no params are given");
172
# More tests to write!
165
# More tests to write!
173
166
174
#End transaction
167
#End transaction
175
- 

Return to bug 20510