shorter names and rearranged weights for a brighter tommorow^W^Wbetter price plan...
[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   'shortname' => 'External SQL query',
14   'fields' => {
15     'setup_fee' => { 'name' => 'Setup fee for this package',
16                      'default' => 0,
17                    },
18     'recur_flat' => { 'name' => 'Base recurring fee for this package',
19                       'default' => 0,
20                     },
21     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
22                                    ' of service at cancellation',
23                          'type' => 'checkbox',
24                        },
25     'datasrc' => { 'name' => 'DBI data source',
26                    'default' => '',
27                  },
28     'db_username' => { 'name' => 'Database username',
29                        'default' => '',
30                      },
31     'db_password' => { 'name' => 'Database password',
32                        'default' => '',
33                      },
34     'query' => { 'name' => 'SQL query',
35                  'default' => '',
36                },
37   },
38   'fieldorder' => [qw( setup_fee recur_flat unused_credit datasrc db_username db_password query )],
39   #'setup' => 'what.setup_fee.value',
40   #'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;'!,
41   'weight' => '58',
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 sub is_free {
69   0;
70 }
71
72 sub base_recur {
73   my($self, $cust_pkg) = @_;
74   $self->option('recur_flat');
75 }
76
77 1;