import torrus 1.0.9
[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::flat;
8
9 @ISA = qw(FS::part_pkg::flat);
10
11 %info = (
12   'name' => 'Base charge plus a per-domain metered rate from a configurable SQL query',
13   'shortname' => 'Bulk (per-domain from SQL query)',
14   'inherit_fields' => [ 'global_Mixin' ],
15   'fields' => {
16     'recur_included' => { 'name' => 'Units included',
17                           'default' => 0,
18                         },
19     'recur_unit_charge' => { 'name' => 'Additional charge per unit',
20                              'default' => 0,
21                            },
22     'datasrc' => { 'name' => 'DBI data source',
23                    'default' => '',
24                  },
25     'db_username' => { 'name' => 'Database username',
26                        'default' => '',
27                      },
28     'db_password' => { 'name' => 'Database username',
29                        'default' => '',
30                      },
31     'query' => { 'name' => 'SQL query',
32                  'default' => '',
33                },
34   },
35   'fieldorder' => [qw( recur_included recur_unit_charge datasrc db_username db_password query )],
36  # 'setup' => 'what.setup_fee.value',
37  # 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\") or die $DBI::errstr; \'',
38  #'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_fee.value + \' + $units * \' + what.recur_unit_charge.value + \';\'',
39   #'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_fee.value + \' + $units * \' + what.recur_unit_charge + \';\'',
40   'weight' => '56',
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 $units = 0;
55   foreach my $cust_svc (
56     grep { $_->part_svc->svcdb eq "svc_domain" } $cust_pkg->cust_svc
57   ) {
58     my $domain = $cust_svc->svc_x->domain;
59     $sth->execute($domain) or die $sth->errstr;
60
61     $units += $sth->fetchrow_arrayref->[0];
62   }
63
64   $units -= $self->option('recur_included');
65   $units = 0 if $units < 0;
66
67   $self->option('recur_fee') + $units * $self->option('recur_unit_charge');
68 }
69
70 sub can_discount { 0; }
71
72 sub is_free_options {
73   qw( setup_fee recur_fee recur_unit_charge );
74 }
75
76 sub base_recur {
77   my($self, $cust_pkg) = @_;
78   $self->option('recur_fee');
79 }
80
81 1;