doc
[freeside.git] / FS / FS / sales.pm
1 package FS::sales;
2 use base qw( FS::Agent_Mixin FS::Record );
3
4 use strict;
5 use FS::Record qw( qsearch qsearchs );
6 use FS::agent;
7 use FS::cust_main;
8 use FS::cust_bill_pkg;
9 use FS::cust_credit;
10
11 =head1 NAME
12
13 FS::sales - Object methods for sales records
14
15 =head1 SYNOPSIS
16
17   use FS::sales;
18
19   $record = new FS::sales \%hash;
20   $record = new FS::sales { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::sales object represents a sales person.  FS::sales inherits from
33 FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item salesnum
38
39 primary key
40
41 =item salesperson
42
43 Name
44
45 =item agentnum
46
47 Agent (see L<FS::agent)
48
49 =item disabled
50
51 Disabled flag, empty or `Y'
52
53 =item sales_custnum
54
55 Sales person master customer (see L<FS::cust_main>)
56
57 =back
58
59 =head1 METHODS
60
61 =over 4
62
63 =item new HASHREF
64
65 Creates a new sales person.  To add the sales person to the database, see
66 L<"insert">.
67
68 Note that this stores the hash reference, not a distinct copy of the hash it
69 points to.  You can ask the object for a copy with the I<hash> method.
70
71 =cut
72
73 # the new method can be inherited from FS::Record, if a table method is defined
74
75 sub table { 'sales'; }
76
77 =item insert
78
79 Adds this record to the database.  If there is an error, returns the error,
80 otherwise returns false.
81
82 =cut
83
84 # the insert method can be inherited from FS::Record
85
86 =item delete
87
88 Delete this record from the database.
89
90 =cut
91
92 # the delete method can be inherited from FS::Record
93
94 =item replace OLD_RECORD
95
96 Replaces the OLD_RECORD with this one in the database.  If there is an error,
97 returns the error, otherwise returns false.
98
99 =cut
100
101 # the replace method can be inherited from FS::Record
102
103 =item check
104
105 Checks all fields to make sure this is a valid sales person.  If there is
106 an error, returns the error, otherwise returns false.  Called by the insert
107 and replace methods.
108
109 =cut
110
111 # the check method should currently be supplied - FS::Record contains some
112 # data checking routines
113
114 sub check {
115   my $self = shift;
116
117   my $error = 
118     $self->ut_numbern('salesnum')
119     || $self->ut_text('salesperson')
120     || $self->ut_foreign_key('agentnum', 'agent', 'agentnum')
121     || $self->ut_foreign_keyn('sales_custnum', 'cust_main', 'custnum')
122     || $self->ut_enum('disabled', [ '', 'Y' ])
123   ;
124   return $error if $error;
125
126   $self->SUPER::check;
127 }
128
129 =item sales_cust_main
130
131 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
132 sales person.
133
134 =cut
135
136 sub sales_cust_main {
137   my $self = shift;
138   qsearchs( 'cust_main', { 'custnum' => $self->sales_custnum } );
139 }
140
141 =item cust_bill_pkg START END OPTIONS
142
143 Returns the package line items (see L<FS::cust_bill_pkg>) for which this 
144 sales person could receive commission.
145
146 START and END are an optional date range to limit the results.
147
148 OPTIONS may contain:
149 - I<cust_main_sales>: if this is a true value, sales of packages that have no
150 package sales person will be included if this is their customer sales person.
151 - I<classnum>: limit to this package classnum.
152 - I<paid>: limit to sales that have no unpaid balance.
153
154 =cut
155
156 sub cust_bill_pkg_search {
157   my( $self, $sdate, $edate, %search ) = @_;
158
159   my $cmp_salesnum = delete $search{'cust_main_sales'}
160                        ? ' COALESCE( cust_pkg.salesnum, cust_main.salesnum )'
161                        : ' cust_pkg.salesnum ';
162
163   my $salesnum = $self->salesnum;
164   die "bad salesnum" unless $salesnum =~ /^(\d+)$/;
165   my @where = ( "$cmp_salesnum    = $salesnum",
166                 "sales_pkg_class.salesnum = $salesnum"
167               );
168   push @where, "cust_bill._date >= $sdate" if $sdate;
169   push @where, "cust_bill._date  < $edate" if $edate;
170
171   my $classnum_sql = '';
172   if ( exists( $search{'classnum'}  ) ) {
173     my $classnum = $search{'classnum'} || '';
174     die "bad classnum" unless $classnum =~ /^(\d*)$/;
175
176     push @where,
177       "part_pkg.classnum ". ( $classnum ? " = $classnum " : ' IS NULL ' );
178   }
179
180   # sales_pkg_class number-of-months limit, grr
181   # (we should be able to just check for the cust_event record from the 
182   # commission credit, but the report is supposed to act as a check on that)
183   #
184   # Pg-specific, of course
185   my $setup_date = 'TO_TIMESTAMP( cust_pkg.setup )';
186   my $interval = "(sales_pkg_class.commission_duration || ' months')::interval";
187   my $charge_date = 'TO_TIMESTAMP( cust_bill._date )';
188   push @where, "CASE WHEN sales_pkg_class.commission_duration IS NOT NULL ".
189                "THEN $charge_date < $setup_date + $interval ".
190                "ELSE TRUE END";
191
192   if ( $search{'paid'} ) {
193     push @where, FS::cust_bill_pkg->owed_sql . ' <= 0.005';
194   }
195
196   my $extra_sql = "WHERE ".join(' AND ', map {"( $_ )"} @where);
197
198   { 'table'     => 'cust_bill_pkg',
199     'select'    => 'cust_bill_pkg.*',
200     'addl_from' => ' LEFT JOIN cust_bill USING ( invnum ) '.
201                    ' LEFT JOIN cust_pkg  USING ( pkgnum ) '.
202                    ' LEFT JOIN part_pkg  USING ( pkgpart ) '.
203                    ' LEFT JOIN cust_main ON ( cust_pkg.custnum = cust_main.custnum )'.
204                    ' JOIN sales_pkg_class ON ( '.
205                    ' COALESCE( sales_pkg_class.classnum, 0) = COALESCE( part_pkg.classnum, 0) )',
206     'extra_sql' => $extra_sql,
207  };
208 }
209
210 sub cust_bill_pkg {
211   my $self = shift;
212   qsearch( $self->cust_bill_pkg_search(@_) )
213 }
214
215 sub cust_credit_search {
216   my( $self, $sdate, $edate, %search ) = @_;
217
218   $search{'hashref'}->{'commission_salesnum'} = $self->salesnum;
219
220   my @where = ();
221   push @where, "cust_credit._date >= $sdate" if $sdate;
222   push @where, "cust_credit._date  < $edate" if $edate;
223
224   my $classnum_sql = '';
225   if ( exists($search{'commission_classnum'}) ) {
226     my $classnum = delete($search{'commission_classnum'});
227     push @where, 'part_pkg.classnum '. ( $classnum ? " = $classnum"
228                                                    : " IS NULL "    );
229
230     $search{'addl_from'} .=
231       ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
232       ' LEFT JOIN part_pkg USING ( pkgpart ) ';
233   }
234
235   my $extra_sql = "AND ".join(' AND ', map {"( $_ )"} @where);
236
237   { 'table'     => 'cust_credit',
238     'extra_sql' => $extra_sql,
239     %search,
240   };
241 }
242
243 sub cust_credit {
244   my $self = shift;
245   qsearch( $self->cust_credit_search(@_) )
246 }
247
248 =back
249
250 =head1 BUGS
251
252 =head1 SEE ALSO
253
254 L<FS::Record>, schema.html from the base documentation.
255
256 =cut
257
258 1;
259