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:
58 Creates a new sales person. To add the sales person to the database, see
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to. You can ask the object for a copy with the I<hash> method.
66 # the new method can be inherited from FS::Record, if a table method is defined
68 sub table { 'sales'; }
72 Adds this record to the database. If there is an error, returns the error,
73 otherwise returns false.
77 # the insert method can be inherited from FS::Record
81 Delete this record from the database.
85 # the delete method can be inherited from FS::Record
87 =item replace OLD_RECORD
89 Replaces the OLD_RECORD with this one in the database. If there is an error,
90 returns the error, otherwise returns false.
94 # the replace method can be inherited from FS::Record
98 Checks all fields to make sure this is a valid sales person. If there is
99 an error, returns the error, otherwise returns false. Called by the insert
104 # the check method should currently be supplied - FS::Record contains some
105 # data checking routines
111 $self->ut_numbern('salesnum')
112 || $self->ut_text('salesperson')
113 || $self->ut_foreign_key('agentnum', 'agent', 'agentnum')
114 || $self->ut_foreign_keyn('sales_custnum', 'cust_main', 'custnum')
115 || $self->ut_enum('disabled', [ '', 'Y' ])
117 return $error if $error;
122 =item sales_cust_main
124 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
129 sub sales_cust_main {
131 qsearchs( 'cust_main', { 'custnum' => $self->sales_custnum } );
134 =item cust_bill_pkg START END OPTIONS
136 Returns the package line items (see L<FS::cust_bill_pkg>) for which this
137 sales person could receive commission.
139 START and END are an optional date range to limit the results.
142 - I<cust_main_sales>: if this is a true value, sales of packages that have no
143 package sales person will be included if this is their customer sales person.
144 - I<classnum>: limit to this package classnum.
145 - I<paid>: limit to sales that have no unpaid balance.
151 my $salesnum = $self->salesnum;
152 die "bad salesnum" unless $salesnum =~ /^(\d+)$/;
155 my $cmp_salesnum = 'cust_pkg.salesnum';
156 if ($opt{cust_main_sales}) {
157 $cmp_salesnum = 'COALESCE(cust_pkg.salesnum, cust_main.salesnum)';
160 my @where = ( "$cmp_salesnum = $salesnum",
161 "sales_pkg_class.salesnum = $salesnum"
164 # sales_pkg_class number-of-months limit, grr
165 # (we should be able to just check for the cust_event record from the
166 # commission credit, but the report is supposed to act as a check on that)
168 # Pg-specific, of course
169 my $setup_date = 'TO_TIMESTAMP( cust_pkg.setup )';
170 my $interval = "(sales_pkg_class.commission_duration || ' months')::interval";
171 my $charge_date = 'TO_TIMESTAMP( cust_bill._date )';
172 push @where, "CASE WHEN sales_pkg_class.commission_duration IS NOT NULL ".
173 "THEN $charge_date < $setup_date + $interval ".
179 sub commission_where {
181 'cust_credit.commission_salesnum = ' . $self->salesnum;
185 sub cust_bill_pkg_search {
187 my $search = $self->SUPER::cust_bill_pkg_search(@_);
188 $search->{addl_from} .= '
189 JOIN sales_pkg_class ON( COALESCE(sales_pkg_class.classnum, 0) = COALESCE(part_pkg.classnum, 0) )';
200 L<FS::Record>, schema.html from the base documentation.