modular price plans!
[freeside.git] / FS / FS / part_pkg / sql_generic.pm
1 package FS::part_pkg::sql_generic;
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 a per-domain metered rate 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       'recur_included' => { 'name' => 'Units included',
21                             'default' => 0,
22                           },
23       'recur_unit_charge' => { 'name' => 'Additional charge per unit',
24                                'default' => 0,
25                              },
26       'datasrc' => { 'name' => 'DBI data source',
27                      'default' => '',
28                    },
29       'db_username' => { 'name' => 'Database username',
30                          'default' => '',
31                        },
32       'db_password' => { 'name' => 'Database username',
33                          'default' => '',
34                        },
35       'query' => { 'name' => 'SQL query',
36                    'default' => '',
37                  },
38     },
39     'fieldorder' => [qw( setup_fee recur_flat recur_included recur_unit_charge datasrc db_username db_password query )],
40    # 'setup' => 'what.setup_fee.value',
41    # 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\") or die $DBI::errstr; \'',
42    #'recur' => '\'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 $units = 0; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq \"svc_domain\" } $cust_pkg->cust_svc ) { my $domain = $cust_svc->svc_x->domain; $sth->execute($domain) or die $sth->errstr; $units += $sth->fetchrow_arrayref->[0]; } $units -= \' + what.recur_included.value + \'; $units = 0 if $units < 0; \' + what.recur_flat.value + \' + $units * \' + what.recur_unit_charge.value + \';\'',
43     #'recur' => '\'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 $units = 0; foreach my $cust_svc ( grep { $_->part_svc->svcdb eq "svc_domain" } $cust_pkg->cust_svc ) { my $domain = $cust_svc->svc_x->domain; $sth->execute($domain) or die $sth->errstr; $units += $sth->fetchrow_arrayref->[0]; } $units -= \' + what.recur_included.value + \'; $units = 0 if $units < 0; \' + what.recur_flat.value + \' + $units * \' + what.recur_unit_charge + \';\'',
44     'weight' => '70',
45 );
46
47 sub calc_setup {
48   my($self, $cust_pkg ) = @_;
49   $self->option('setup_fee');
50 }
51
52 sub calc_recur {
53   my($self, $cust_pkg ) = @_;
54
55   my $dbh = DBI->connect( map { $self->option($_) }
56                               qw( datasrc db_username db_password )
57                         )
58     or die $DBI::errstr;
59
60   my $sth = $dbh->prepare( $self->option('query') )
61     or die $dbh->errstr;
62
63   my $units = 0;
64   foreach my $cust_svc (
65     grep { $_->part_svc->svcdb eq "svc_domain" } $cust_pkg->cust_svc
66   ) {
67     my $domain = $cust_svc->svc_x->domain;
68     $sth->execute($domain) or die $sth->errstr;
69
70     $units += $sth->fetchrow_arrayref->[0];
71   }
72
73   $units -= $self->option('recur_included');
74   $units = 0 if $units < 0;
75
76   $self->option('recur_flat') + $units * $self->option('recur_unit_charge');
77 }
78
79 1;