enable CardFortress in test database, #71513
[freeside.git] / FS / FS / sales.pm
1 package FS::sales;
2 use base qw( FS::Commission_Mixin 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 sales_where {
157   my $self = shift;
158   my $salesnum = $self->salesnum;
159   die "bad salesnum" unless $salesnum =~ /^(\d+)$/;
160   my %opt = @_;
161
162   my $cmp_salesnum = 'cust_pkg.salesnum';
163   if ($opt{cust_main_sales}) {
164     $cmp_salesnum = 'COALESCE(cust_pkg.salesnum, cust_main.salesnum)';
165   }
166
167   my @where = ( "$cmp_salesnum    = $salesnum",
168                 "sales_pkg_class.salesnum = $salesnum"
169               );
170
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)
174   #
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 ".
181                "ELSE TRUE END";
182
183   @where;
184 }
185
186 sub commission_where {
187   my $self = shift;
188   'cust_credit.commission_salesnum = ' . $self->salesnum;
189 }
190
191 # slightly modify it
192 sub cust_bill_pkg_search {
193   my $self = shift;
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) )';
197
198   return $search;
199 }
200
201 =back
202
203 =head1 BUGS
204
205 =head1 SEE ALSO
206
207 L<FS::Record>, schema.html from the base documentation.
208
209 =cut
210
211 1;
212