688f9188709f8f2cd136c56a7f15b0dfe8fec550
[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);
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_setup {
40   my($self, $cust_pkg ) = @_;
41   $self->option('setup_fee');
42 }
43
44 sub calc_recur {
45   my($self, $cust_pkg ) = @_;
46
47   my $dbh = DBI->connect( map { $self->option($_) }
48                               qw( datasrc db_username db_password )
49                         )
50     or die $DBI::errstr;
51
52   my $sth = $dbh->prepare( $self->option('query') )
53     or die $dbh->errstr;
54
55   my $price = $self->option('recur_flat');
56
57   foreach my $cust_svc (
58     grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc
59   ) {
60     my $id = $cust_svc->svc_x->id;
61     $sth->execute($id) or die $sth->errstr;
62     $price += $sth->fetchrow_arrayref->[0];
63   }
64
65   $price;
66 }
67
68 1;