2 use base qw( FS::Agent_Mixin FS::Record );
5 use FS::Record qw( qsearch qsearchs );
13 FS::sales - Object methods for sales records
19 $record = new FS::sales \%hash;
20 $record = new FS::sales { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::sales object represents a sales person. FS::sales inherits from
33 FS::Record. The following fields are currently supported:
47 Agent (see L<FS::agent)
51 Disabled flag, empty or `Y'
55 Sales person master customer (see L<FS::cust_main>)
65 Creates a new sales person. To add the sales person to the database, see
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.
73 # the new method can be inherited from FS::Record, if a table method is defined
75 sub table { 'sales'; }
79 Adds this record to the database. If there is an error, returns the error,
80 otherwise returns false.
84 # the insert method can be inherited from FS::Record
88 Delete this record from the database.
92 # the delete method can be inherited from FS::Record
94 =item replace OLD_RECORD
96 Replaces the OLD_RECORD with this one in the database. If there is an error,
97 returns the error, otherwise returns false.
101 # the replace method can be inherited from FS::Record
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
111 # the check method should currently be supplied - FS::Record contains some
112 # data checking routines
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' ])
124 return $error if $error;
129 =item sales_cust_main
131 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
136 sub sales_cust_main {
138 qsearchs( 'cust_main', { 'custnum' => $self->sales_custnum } );
141 =item cust_bill_pkg START END OPTIONS
143 Returns the package line items (see L<FS::cust_bill_pkg>) for which this
144 sales person could receive commission.
146 START and END are an optional date range to limit the results.
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.
156 sub cust_bill_pkg_search {
157 my( $self, $sdate, $edate, %search ) = @_;
159 my $cmp_salesnum = delete $search{'cust_main_sales'}
160 ? ' COALESCE( cust_pkg.salesnum, cust_main.salesnum )'
161 : ' cust_pkg.salesnum ';
163 my $salesnum = $self->salesnum;
164 die "bad salesnum" unless $salesnum =~ /^(\d+)$/;
165 my @where = ( "$cmp_salesnum = $salesnum",
166 "sales_pkg_class.salesnum = $salesnum"
168 push @where, "cust_bill._date >= $sdate" if $sdate;
169 push @where, "cust_bill._date < $edate" if $edate;
171 my $classnum_sql = '';
172 if ( exists( $search{'classnum'} ) ) {
173 my $classnum = $search{'classnum'} || '';
174 die "bad classnum" unless $classnum =~ /^(\d*)$/;
177 "part_pkg.classnum ". ( $classnum ? " = $classnum " : ' IS NULL ' );
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)
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 ".
192 if ( $search{'paid'} ) {
193 push @where, FS::cust_bill_pkg->owed_sql . ' <= 0.005';
196 my $extra_sql = "WHERE ".join(' AND ', map {"( $_ )"} @where);
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,
212 qsearch( $self->cust_bill_pkg_search(@_) )
215 sub cust_credit_search {
216 my( $self, $sdate, $edate, %search ) = @_;
218 $search{'hashref'}->{'commission_salesnum'} = $self->salesnum;
221 push @where, "cust_credit._date >= $sdate" if $sdate;
222 push @where, "cust_credit._date < $edate" if $edate;
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"
230 $search{'addl_from'} .=
231 ' LEFT JOIN cust_pkg ON ( commission_pkgnum = cust_pkg.pkgnum ) '.
232 ' LEFT JOIN part_pkg USING ( pkgpart ) ';
235 my $extra_sql = "AND ".join(' AND ', map {"( $_ )"} @where);
237 { 'table' => 'cust_credit',
238 'extra_sql' => $extra_sql,
245 qsearch( $self->cust_credit_search(@_) )
254 L<FS::Record>, schema.html from the base documentation.