show credit balance on invoices, #11564
[freeside.git] / FS / FS / agent.pm
1 package FS::agent;
2
3 use strict;
4 use vars qw( @ISA );
5 #use Crypt::YAPassGen;
6 use Business::CreditCard 0.28;
7 use FS::Record qw( dbh qsearch qsearchs );
8 use FS::cust_main;
9 use FS::cust_pkg;
10 use FS::agent_type;
11 use FS::reg_code;
12 use FS::TicketSystem;
13 use FS::Conf;
14
15 @ISA = qw( FS::m2m_Common FS::Record );
16
17 =head1 NAME
18
19 FS::agent - Object methods for agent records
20
21 =head1 SYNOPSIS
22
23   use FS::agent;
24
25   $record = new FS::agent \%hash;
26   $record = new FS::agent { 'column' => 'value' };
27
28   $error = $record->insert;
29
30   $error = $new_record->replace($old_record);
31
32   $error = $record->delete;
33
34   $error = $record->check;
35
36   $agent_type = $record->agent_type;
37
38   $hashref = $record->pkgpart_hashref;
39   #may purchase $pkgpart if $hashref->{$pkgpart};
40
41 =head1 DESCRIPTION
42
43 An FS::agent object represents an agent.  Every customer has an agent.  Agents
44 can be used to track things like resellers or salespeople.  FS::agent inherits
45 from FS::Record.  The following fields are currently supported:
46
47 =over 4
48
49 =item agentnum - primary key (assigned automatically for new agents)
50
51 =item agent - Text name of this agent
52
53 =item typenum - Agent type (see L<FS::agent_type>)
54
55 =item ticketing_queueid - Ticketing Queue
56
57 =item invoice_template - Invoice template name
58
59 =item agent_custnum - Optional agent customer (see L<FS::cust_main>)
60
61 =item disabled - Disabled flag, empty or 'Y'
62
63 =item prog - Deprecated (never used)
64
65 =item freq - Deprecated (never used)
66
67 =item username - (Deprecated) Username for the Agent interface
68
69 =item _password - (Deprecated) Password for the Agent interface
70
71 =back
72
73 =head1 METHODS
74
75 =over 4
76
77 =item new HASHREF
78
79 Creates a new agent.  To add the agent to the database, see L<"insert">.
80
81 =cut
82
83 sub table { 'agent'; }
84
85 =item insert
86
87 Adds this agent to the database.  If there is an error, returns the error,
88 otherwise returns false.
89
90 =item delete
91
92 Deletes this agent from the database.  Only agents with no customers can be
93 deleted.  If there is an error, returns the error, otherwise returns false.
94
95 =cut
96
97 sub delete {
98   my $self = shift;
99
100   return "Can't delete an agent with customers!"
101     if qsearch( 'cust_main', { 'agentnum' => $self->agentnum } );
102
103   $self->SUPER::delete;
104 }
105
106 =item replace OLD_RECORD
107
108 Replaces OLD_RECORD with this one in the database.  If there is an error,
109 returns the error, otherwise returns false.
110
111 =item check
112
113 Checks all fields to make sure this is a valid agent.  If there is an error,
114 returns the error, otherwise returns false.  Called by the insert and replace
115 methods.
116
117 =cut
118
119 sub check {
120   my $self = shift;
121
122   my $error =
123     $self->ut_numbern('agentnum')
124       || $self->ut_text('agent')
125       || $self->ut_number('typenum')
126       || $self->ut_numbern('freq')
127       || $self->ut_textn('prog')
128       || $self->ut_textn('invoice_template')
129       || $self->ut_foreign_keyn('agent_custnum', 'cust_main', 'custnum' )
130   ;
131   return $error if $error;
132
133   if ( $self->dbdef_table->column('disabled') ) {
134     $error = $self->ut_enum('disabled', [ '', 'Y' ] );
135     return $error if $error;
136   }
137
138   if ( $self->dbdef_table->column('username') ) {
139     $error = $self->ut_alphan('username');
140     return $error if $error;
141     if ( length($self->username) ) {
142       my $conflict = qsearchs('agent', { 'username' => $self->username } );
143       return 'duplicate agent username (with '. $conflict->agent. ')'
144         if $conflict && $conflict->agentnum != $self->agentnum;
145       $error = $self->ut_text('password'); # ut_text... arbitrary choice
146     } else {
147       $self->_password('');
148     }
149   }
150
151   return "Unknown typenum!"
152     unless $self->agent_type;
153
154   $self->SUPER::check;
155 }
156
157 =item agent_type
158
159 Returns the FS::agent_type object (see L<FS::agent_type>) for this agent.
160
161 =cut
162
163 sub agent_type {
164   my $self = shift;
165   qsearchs( 'agent_type', { 'typenum' => $self->typenum } );
166 }
167
168 =item agent_cust_main
169
170 Returns the FS::cust_main object (see L<FS::cust_main>), if any, for this
171 agent.
172
173 =cut
174
175 sub agent_cust_main {
176   my $self = shift;
177   qsearchs( 'cust_main', { 'custnum' => $self->agent_custnum } );
178 }
179
180 =item pkgpart_hashref
181
182 Returns a hash reference.  The keys of the hash are pkgparts.  The value is
183 true if this agent may purchase the specified package definition.  See
184 L<FS::part_pkg>.
185
186 =cut
187
188 sub pkgpart_hashref {
189   my $self = shift;
190   $self->agent_type->pkgpart_hashref;
191 }
192
193 =item ticketing_queue
194
195 Returns the queue name corresponding with the id from the I<ticketing_queueid>
196 field, or the empty string.
197
198 =cut
199
200 sub ticketing_queue {
201   my $self = shift;
202   FS::TicketSystem->queue($self->ticketing_queueid);
203 };
204
205 =item payment_gateway [ OPTION => VALUE, ... ]
206
207 Returns a payment gateway object (see L<FS::payment_gateway>) for this agent.
208
209 Currently available options are I<nofatal>, I<invnum>, I<method>, and I<payinfo>.
210
211 If I<nofatal> is set, and no gateway is available, then the empty string
212 will be returned instead of throwing a fatal exception.
213
214 If I<invnum> is set to the number of an invoice (see L<FS::cust_bill>) then
215 an attempt will be made to select a gateway suited for the taxes paid on 
216 the invoice.
217
218 The I<method> and I<payinfo> options can be used to influence the choice
219 as well.  Presently only 'CC' and 'ECHECK' methods are meaningful.
220
221 When the I<method> is 'CC' then the card number in I<payinfo> can direct
222 this routine to route to a gateway suited for that type of card.
223
224 =cut
225
226 sub payment_gateway {
227   my ( $self, %options ) = @_;
228
229   my $taxclass = '';
230   if ( $options{invnum} ) {
231
232     my $cust_bill = qsearchs('cust_bill', { 'invnum' => $options{invnum} } );
233     die "invnum ". $options{'invnum'}. " not found" unless $cust_bill;
234
235     my @part_pkg =
236       map  { $_->part_pkg }
237       grep { $_ }
238       map  { $_->cust_pkg }
239       $cust_bill->cust_bill_pkg;
240
241     my @taxclasses = map $_->taxclass, @part_pkg;
242
243     $taxclass = $taxclasses[0]
244       unless grep { $taxclasses[0] ne $_ } @taxclasses; #unless there are
245                                                         #different taxclasses
246   }
247
248   #look for an agent gateway override first
249   my $cardtype;
250   if ( $options{method} && $options{method} eq 'CC' && $options{payinfo} ) {
251     $cardtype = cardtype($options{payinfo});
252   } elsif ( $options{method} && $options{method} eq 'ECHECK' ) {
253     $cardtype = 'ACH';
254   } else {
255     $cardtype = $options{method} || '';
256   }
257
258   my $override =
259        qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
260                                            cardtype => $cardtype,
261                                            taxclass => $taxclass,       } )
262     || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
263                                            cardtype => '',
264                                            taxclass => $taxclass,       } )
265     || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
266                                            cardtype => $cardtype,
267                                            taxclass => '',              } )
268     || qsearchs('agent_payment_gateway', { agentnum => $self->agentnum,
269                                            cardtype => '',
270                                            taxclass => '',              } );
271
272   my $payment_gateway;
273   my $conf = new FS::Conf;
274   if ( $override ) { #use a payment gateway override
275
276     $payment_gateway = $override->payment_gateway;
277
278     $payment_gateway->gateway_namespace('Business::OnlinePayment')
279       unless $payment_gateway->gateway_namespace;
280
281   } else { #use the standard settings from the config
282
283     # the standard settings from the config could be moved to a null agent
284     # agent_payment_gateway referenced payment_gateway
285
286     unless ( $conf->exists('business-onlinepayment') ) {
287       if ( $options{'nofatal'} ) {
288         return '';
289       } else {
290         die "Real-time processing not enabled\n";
291       }
292     }
293
294     #load up config
295     my $bop_config = 'business-onlinepayment';
296     $bop_config .= '-ach'
297       if ( $options{method}
298            && $options{method} =~ /^(ECHECK|CHEK)$/
299            && $conf->exists($bop_config. '-ach')
300          );
301     my ( $processor, $login, $password, $action, @bop_options ) =
302       $conf->config($bop_config);
303     $action ||= 'normal authorization';
304     pop @bop_options if scalar(@bop_options) % 2 && $bop_options[-1] =~ /^\s*$/;
305     die "No real-time processor is enabled - ".
306         "did you set the business-onlinepayment configuration value?\n"
307       unless $processor;
308
309     $payment_gateway = new FS::payment_gateway;
310
311     $payment_gateway->gateway_namespace( $conf->config('business-onlinepayment-namespace') ||
312                                  'Business::OnlinePayment');
313     $payment_gateway->gateway_module($processor);
314     $payment_gateway->gateway_username($login);
315     $payment_gateway->gateway_password($password);
316     $payment_gateway->gateway_action($action);
317     $payment_gateway->set('options', [ @bop_options ]);
318
319   }
320
321   unless ( $payment_gateway->gateway_namespace ) {
322     $payment_gateway->gateway_namespace(
323       scalar($conf->config('business-onlinepayment-namespace'))
324       || 'Business::OnlinePayment'
325     );
326   }
327
328   $payment_gateway;
329 }
330
331 =item num_prospect_cust_main
332
333 Returns the number of prospects (customers with no packages ever ordered) for
334 this agent.
335
336 =cut
337
338 sub num_prospect_cust_main {
339   shift->num_sql(FS::cust_main->prospect_sql);
340 }
341
342 sub num_sql {
343   my( $self, $sql ) = @_;
344   my $statement = "SELECT COUNT(*) FROM cust_main WHERE agentnum = ? AND $sql";
345   my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
346   $sth->execute($self->agentnum) or die $sth->errstr. " executing $statement";
347   $sth->fetchrow_arrayref->[0];
348 }
349
350 =item prospect_cust_main
351
352 Returns the prospects (customers with no packages ever ordered) for this agent,
353 as cust_main objects.
354
355 =cut
356
357 sub prospect_cust_main {
358   shift->cust_main_sql(FS::cust_main->prospect_sql);
359 }
360
361 sub cust_main_sql {
362   my( $self, $sql ) = @_;
363   qsearch( 'cust_main',
364            { 'agentnum' => $self->agentnum },
365            '',
366            " AND $sql"
367   );
368 }
369
370 =item num_active_cust_main
371
372 Returns the number of active customers for this agent (customers with active
373 recurring packages).
374
375 =cut
376
377 sub num_active_cust_main {
378   shift->num_sql(FS::cust_main->active_sql);
379 }
380
381 =item active_cust_main
382
383 Returns the active customers for this agent, as cust_main objects.
384
385 =cut
386
387 sub active_cust_main {
388   shift->cust_main_sql(FS::cust_main->active_sql);
389 }
390
391 =item num_inactive_cust_main
392
393 Returns the number of inactive customers for this agent (customers with no
394 active recurring packages, but otherwise unsuspended/uncancelled).
395
396 =cut
397
398 sub num_inactive_cust_main {
399   shift->num_sql(FS::cust_main->inactive_sql);
400 }
401
402 =item inactive_cust_main
403
404 Returns the inactive customers for this agent, as cust_main objects.
405
406 =cut
407
408 sub inactive_cust_main {
409   shift->cust_main_sql(FS::cust_main->inactive_sql);
410 }
411
412
413 =item num_susp_cust_main
414
415 Returns the number of suspended customers for this agent.
416
417 =cut
418
419 sub num_susp_cust_main {
420   shift->num_sql(FS::cust_main->susp_sql);
421 }
422
423 =item susp_cust_main
424
425 Returns the suspended customers for this agent, as cust_main objects.
426
427 =cut
428
429 sub susp_cust_main {
430   shift->cust_main_sql(FS::cust_main->susp_sql);
431 }
432
433 =item num_cancel_cust_main
434
435 Returns the number of cancelled customer for this agent.
436
437 =cut
438
439 sub num_cancel_cust_main {
440   shift->num_sql(FS::cust_main->cancel_sql);
441 }
442
443 =item cancel_cust_main
444
445 Returns the cancelled customers for this agent, as cust_main objects.
446
447 =cut
448
449 sub cancel_cust_main {
450   shift->cust_main_sql(FS::cust_main->cancel_sql);
451 }
452
453 =item num_active_cust_pkg
454
455 Returns the number of active customer packages for this agent.
456
457 =cut
458
459 sub num_active_cust_pkg {
460   shift->num_pkg_sql(FS::cust_pkg->active_sql);
461 }
462
463 sub num_pkg_sql {
464   my( $self, $sql ) = @_;
465   my $statement = 
466     "SELECT COUNT(*) FROM cust_pkg LEFT JOIN cust_main USING ( custnum )".
467     " WHERE agentnum = ? AND $sql";
468   my $sth = dbh->prepare($statement) or die dbh->errstr." preparing $statement";
469   $sth->execute($self->agentnum) or die $sth->errstr. "executing $statement";
470   $sth->fetchrow_arrayref->[0];
471 }
472
473 =item num_inactive_cust_pkg
474
475 Returns the number of inactive customer packages (one-time packages otherwise
476 unsuspended/uncancelled) for this agent.
477
478 =cut
479
480 sub num_inactive_cust_pkg {
481   shift->num_pkg_sql(FS::cust_pkg->inactive_sql);
482 }
483
484 =item num_susp_cust_pkg
485
486 Returns the number of suspended customer packages for this agent.
487
488 =cut
489
490 sub num_susp_cust_pkg {
491   shift->num_pkg_sql(FS::cust_pkg->susp_sql);
492 }
493
494 =item num_cancel_cust_pkg
495
496 Returns the number of cancelled customer packages for this agent.
497
498 =cut
499
500 sub num_cancel_cust_pkg {
501   shift->num_pkg_sql(FS::cust_pkg->cancel_sql);
502 }
503
504 =item generate_reg_codes NUM PKGPART_ARRAYREF
505
506 Generates the specified number of registration codes, allowing purchase of the
507 specified package definitions.  Returns an array reference of the newly
508 generated codes, or a scalar error message.
509
510 =cut
511
512 #false laziness w/prepay_credit::generate
513 sub generate_reg_codes {
514   my( $self, $num, $pkgparts ) = @_;
515
516   my @codeset = ( 'A'..'Z' );
517
518   local $SIG{HUP} = 'IGNORE';
519   local $SIG{INT} = 'IGNORE';
520   local $SIG{QUIT} = 'IGNORE';
521   local $SIG{TERM} = 'IGNORE';
522   local $SIG{TSTP} = 'IGNORE';
523   local $SIG{PIPE} = 'IGNORE';
524
525   my $oldAutoCommit = $FS::UID::AutoCommit;
526   local $FS::UID::AutoCommit = 0;
527   my $dbh = dbh;
528
529   my @codes = ();
530   for ( 1 ... $num ) {
531     my $reg_code = new FS::reg_code {
532       'agentnum' => $self->agentnum,
533       'code'     => join('', map($codeset[int(rand $#codeset)], (0..7) ) ),
534     };
535     my $error = $reg_code->insert($pkgparts);
536     if ( $error ) {
537       $dbh->rollback if $oldAutoCommit;
538       return $error;
539     }
540     push @codes, $reg_code->code;
541   }
542
543   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
544
545   \@codes;
546
547 }
548
549 =item num_reg_code
550
551 Returns the number of unused registration codes for this agent.
552
553 =cut
554
555 sub num_reg_code {
556   my $self = shift;
557   my $sth = dbh->prepare(
558     "SELECT COUNT(*) FROM reg_code WHERE agentnum = ?"
559   ) or die dbh->errstr;
560   $sth->execute($self->agentnum) or die $sth->errstr;
561   $sth->fetchrow_arrayref->[0];
562 }
563
564 =item num_prepay_credit
565
566 Returns the number of unused prepaid cards for this agent.
567
568 =cut
569
570 sub num_prepay_credit {
571   my $self = shift;
572   my $sth = dbh->prepare(
573     "SELECT COUNT(*) FROM prepay_credit WHERE agentnum = ?"
574   ) or die dbh->errstr;
575   $sth->execute($self->agentnum) or die $sth->errstr;
576   $sth->fetchrow_arrayref->[0];
577 }
578
579
580 =back
581
582 =head1 BUGS
583
584 =head1 SEE ALSO
585
586 L<FS::Record>, L<FS::agent_type>, L<FS::cust_main>, L<FS::part_pkg>, 
587 schema.html from the base documentation.
588
589 =cut
590
591 1;
592