This commit was generated by cvs2svn to compensate for changes in r4407,
[freeside.git] / FS / FS / part_pkg / sql_external.pm
1 package FS::part_pkg::sql_external;
2
3 use strict;
4 use vars qw(@ISA %info);
5 use DBI;
6 #use FS::Record qw(qsearch qsearchs);
7 use FS::part_pkg::flat;
8
9 @ISA = qw(FS::part_pkg::flat);
10
11 %info = (
12   'name' => 'Base charge plus additional fees for external services from a configurable SQL query',
13   'fields' => {
14     'setup_fee' => { 'name' => 'Setup fee for this package',
15                      'default' => 0,
16                    },
17     'recur_flat' => { 'name' => 'Base monthly charge for this package',
18                       'default' => 0,
19                     },
20     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
21                                    ' of service at cancellation',
22                          'type' => 'checkbox',
23                        },
24     'datasrc' => { 'name' => 'DBI data source',
25                    'default' => '',
26                  },
27     'db_username' => { 'name' => 'Database username',
28                        'default' => '',
29                      },
30     'db_password' => { 'name' => 'Database password',
31                        'default' => '',
32                      },
33     'query' => { 'name' => 'SQL query',
34                  'default' => '',
35                },
36   },
37   'fieldorder' => [qw( setup_fee recur_flat unused_credit datasrc db_username db_password query )],
38   #'setup' => 'what.setup_fee.value',
39   #'recur' => q!'my $dbh = DBI->connect("' + what.datasrc.value + '", "' + what.db_username.value + '", "' + what.db_password.value + '" ) or die $DBI::errstr; my $sth = $dbh->prepare("' + what.query.value + '") or die $dbh->errstr; my $price = ' + what.recur_flat.value + '; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc ){ my $id = $cust_svc->svc_x->id; $sth->execute($id) or die $sth->errstr; $price += $sth->fetchrow_arrayref->[0]; } $price;'!,
40   'weight' => '72',
41 );
42
43 sub calc_recur {
44   my($self, $cust_pkg ) = @_;
45
46   my $dbh = DBI->connect( map { $self->option($_) }
47                               qw( datasrc db_username db_password )
48                         )
49     or die $DBI::errstr;
50
51   my $sth = $dbh->prepare( $self->option('query') )
52     or die $dbh->errstr;
53
54   my $price = $self->option('recur_flat');
55
56   foreach my $cust_svc (
57     grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc
58   ) {
59     my $id = $cust_svc->svc_x->id;
60     $sth->execute($id) or die $sth->errstr;
61     $price += $sth->fetchrow_arrayref->[0];
62   }
63
64   $price;
65 }
66
67 sub is_free {
68   0;
69 }
70
71 sub base_recur {
72   my($self, $cust_pkg) = @_;
73   $self->option('recur_flat');
74 }
75
76 1;