add agent selection to payment and credit reports, add link to agent browse, closes...
[freeside.git] / FS / FS / h_Common.pm
1 package FS::h_Common;
2
3 use strict;
4 use FS::Record qw(dbdef);
5
6 =head1 NAME
7
8 FS::h_Common - History table "mixin" common base class
9
10 =head1 SYNOPSIS
11
12 package FS::h_tablename;
13 @ISA = qw( FS::h_Common FS::tablename ); 
14
15 sub table { 'h_table_name'; }
16
17 sub insert { return "can't insert history records manually"; }
18 sub delete { return "can't delete history records"; }
19 sub replace { return "can't modify history records"; }
20
21 =head1 DESCRIPTION
22
23 FS::h_Common is intended as a "mixin" base class for history table classes to
24 inherit from.
25
26 =head1 METHODS
27
28 =over 4
29
30 =item sql_h_search END_TIMESTAMP [ START_TIMESTAMP ] 
31
32 Returns an a list consisting of the "SELECT" and "EXTRA_SQL" SQL fragments to
33 search for the appropriate history records created before END_TIMESTAMP
34 and (optionally) not cancelled before START_TIMESTAMP.
35
36 =cut
37
38 sub sql_h_search {
39   my( $self, $end ) = ( shift, shift );
40
41   my $table = $self->table;
42   my $pkey = dbdef->table($table)->primary_key
43     or die "can't (yet) search history table $table without a primary key";
44
45   my $notcancelled = '';
46   if ( scalar(@_) && $_[0] ) {
47     $notcancelled = "AND 0 = ( SELECT COUNT(*) FROM $table as notdel
48                                 WHERE notdel.$pkey = maintable.$pkey
49                                 AND notdel.history_action = 'delete'
50                                 AND notdel.history_date > maintable.history_date
51                                 AND notdel.history_date <= $_[0]
52                              )";
53   }
54
55   (
56     "DISTINCT ON ( $pkey ) *",
57
58     "AND history_date <= $end
59      AND (    history_action = 'insert'
60            OR history_action = 'replace_new'
61          )
62      $notcancelled
63      ORDER BY $pkey ASC, history_date DESC",
64
65      '',
66
67      'AS maintable',
68   );
69
70 }
71
72 =item sql_h_searchs END_TIMESTAMP [ START_TIMESTAMP ] 
73
74 Like sql_h_search, but limited to the single most recent record (before
75 END_TIMESTAMP)
76
77 =cut
78
79 sub sql_h_searchs {
80   my $self = shift;
81   my($select, $where, $cacheobj, $as) = $self->sql_h_search(@_);
82   $where .= ' LIMIT 1';
83   ($select, $where, $cacheobj, $as);
84 }
85
86 =back
87
88 =head1 BUGS
89
90 =head1 SEE ALSO
91
92 L<FS::Record>, schema.html from the base documentation
93
94 =cut
95
96 1;
97