add skip_dcontext_suffix to skip CDRs with dcontext ending in a definable string...
[freeside.git] / FS / FS / vend_main.pm
1 package FS::vend_main;
2 use base qw( FS::Record );
3
4 use strict;
5
6 =head1 NAME
7
8 FS::vend_main - Object methods for vend_main records
9
10 =head1 SYNOPSIS
11
12   use FS::vend_main;
13
14   $record = new FS::vend_main \%hash;
15   $record = new FS::vend_main { 'column' => 'value' };
16
17   $error = $record->insert;
18
19   $error = $new_record->replace($old_record);
20
21   $error = $record->delete;
22
23   $error = $record->check;
24
25 =head1 DESCRIPTION
26
27 An FS::vend_main object represents a vendor.  FS::vend_main inherits from
28 FS::Record.  The following fields are currently supported:
29
30 =over 4
31
32 =item vendnum
33
34 primary key
35
36 =item vendname
37
38 vendname
39
40 =item classnum
41
42 classnum
43
44 =item disabled
45
46 disabled
47
48
49 =back
50
51 =head1 METHODS
52
53 =over 4
54
55 =item new HASHREF
56
57 Creates a new vendor.  To add the vendor to the database, see L<"insert">.
58
59 Note that this stores the hash reference, not a distinct copy of the hash it
60 points to.  You can ask the object for a copy with the I<hash> method.
61
62 =cut
63
64 sub table { 'vend_main'; }
65
66 =item insert
67
68 Adds this record to the database.  If there is an error, returns the error,
69 otherwise returns false.
70
71 =item delete
72
73 Delete this record from the database.
74
75 =item replace OLD_RECORD
76
77 Replaces the OLD_RECORD with this one in the database.  If there is an error,
78 returns the error, otherwise returns false.
79
80 =item check
81
82 Checks all fields to make sure this is a valid vendor.  If there is
83 an error, returns the error, otherwise returns false.  Called by the insert
84 and replace methods.
85
86 =cut
87
88 sub check {
89   my $self = shift;
90
91   my $error = 
92     $self->ut_numbern('vendnum')
93     || $self->ut_text('vendname')
94     || $self->ut_foreign_key('classnum', 'vend_class', 'classnum')
95     || $self->ut_enum('disabled', [ '', 'Y' ] )
96   ;
97   return $error if $error;
98
99   $self->SUPER::check;
100 }
101
102 =item vend_class
103
104 =item search
105
106 =cut
107
108 sub search {
109   my ($class, $param) = @_;
110
111   my @where = ();
112   my $addl_from = '';
113
114   #_date
115   if ( $param->{_date} ) {
116     my($beginning, $ending) = @{$param->{_date}};
117
118     push @where, "vend_bill._date >= $beginning",
119                  "vend_bill._date <  $ending";
120   }
121
122   #payment_date
123   if ( $param->{payment_date} ) {
124     my($beginning, $ending) = @{$param->{payment_date}};
125
126     push @where, "vend_pay._date >= $beginning",
127                  "vend_pay._date <  $ending";
128   }
129
130   if ( $param->{'classnum'} =~ /^(\d+)$/ ) {
131     push @where, "vend_main.classnum = $1";
132   }
133
134   my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
135
136   my $group_by = ' GROUP BY vend_main.vendnum ';
137
138   my $addl_from_vend_bill = ' LEFT JOIN vend_bill_pay USING (vendbillnum) '.
139                             ' LEFT JOIN vend_pay      USING (vendpaynum)  ';
140
141   $addl_from .= " LEFT JOIN vend_bill USING ( vendnum ) $addl_from_vend_bill";
142
143   #simplistic, but how we are for now
144
145   my $count_query = "
146     SELECT COUNT(*),
147            ( SELECT SUM(charged) from vend_bill $addl_from_vend_bill $extra_sql
148            ) AS sum_charged
149       FROM vend_main "; #XXX classnum, sum_charged > 0
150
151   +{
152     'table'         => 'vend_main',
153     'select'        => 'vend_main.*, sum(vend_bill.charged) as sum_charged',
154     'addl_from'     => $addl_from,
155     'hashref'       => {},
156     'extra_sql'     => "$extra_sql $group_by",
157     'order_by'      => 'ORDER BY sum_charged desc',
158     'count_query'   => $count_query,
159     #'extra_headers' => \@extra_headers,
160     #'extra_fields'  => \@extra_fields,
161   };
162 }
163
164 =back
165
166 =head1 BUGS
167
168 =head1 SEE ALSO
169
170 L<FS::Record>, schema.html from the base documentation.
171
172 =cut
173
174 1;
175