2 use base qw( FS::Commission_Mixin 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.
158 my $salesnum = $self->salesnum;
159 die "bad salesnum" unless $salesnum =~ /^(\d+)$/;
162 my $cmp_salesnum = 'cust_pkg.salesnum';
163 if ($opt{cust_main_sales}) {
164 $cmp_salesnum = 'COALESCE(cust_pkg.salesnum, cust_main.salesnum)';
167 my @where = ( "$cmp_salesnum = $salesnum",
168 "sales_pkg_class.salesnum = $salesnum"
171 # sales_pkg_class number-of-months limit, grr
172 # (we should be able to just check for the cust_event record from the
173 # commission credit, but the report is supposed to act as a check on that)
175 # Pg-specific, of course
176 my $setup_date = 'TO_TIMESTAMP( cust_pkg.setup )';
177 my $interval = "(sales_pkg_class.commission_duration || ' months')::interval";
178 my $charge_date = 'TO_TIMESTAMP( cust_bill._date )';
179 push @where, "CASE WHEN sales_pkg_class.commission_duration IS NOT NULL ".
180 "THEN $charge_date < $setup_date + $interval ".
186 sub commission_where {
188 'cust_credit.commission_salesnum = ' . $self->salesnum;
192 sub cust_bill_pkg_search {
194 my $search = $self->SUPER::cust_bill_pkg_search(@_);
195 $search->{addl_from} .= '
196 JOIN sales_pkg_class ON( COALESCE(sales_pkg_class.classnum, 0) = COALESCE(part_pkg.classnum, 0) )';
207 L<FS::Record>, schema.html from the base documentation.