This commit was generated by cvs2svn to compensate for changes in r3921,
[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;
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       'datasrc' => { 'name' => 'DBI data source',
21                      'default' => '',
22                    },
23       'db_username' => { 'name' => 'Database username',
24                          'default' => '',
25                        },
26       'db_password' => { 'name' => 'Database password',
27                          'default' => '',
28                        },
29       'query' => { 'name' => 'SQL query',
30                    'default' => '',
31                  },
32     },
33     'fieldorder' => [qw( setup_fee recur_flat datasrc db_username db_password query )],
34     #'setup' => 'what.setup_fee.value',
35     #'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;'!,
36     'weight' => '72',
37 );
38
39 sub calc_recur {
40   my($self, $cust_pkg ) = @_;
41
42   my $dbh = DBI->connect( map { $self->option($_) }
43                               qw( datasrc db_username db_password )
44                         )
45     or die $DBI::errstr;
46
47   my $sth = $dbh->prepare( $self->option('query') )
48     or die $dbh->errstr;
49
50   my $price = $self->option('recur_flat');
51
52   foreach my $cust_svc (
53     grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc
54   ) {
55     my $id = $cust_svc->svc_x->id;
56     $sth->execute($id) or die $sth->errstr;
57     $price += $sth->fetchrow_arrayref->[0];
58   }
59
60   $price;
61 }
62
63 sub is_free {
64   0;
65 }
66
67 1;