This commit was generated by cvs2svn to compensate for changes in r4407,
[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   '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     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
21                                    ' of service at cancellation',
22                          'type' => 'checkbox',
23                        },
24     'recur_included' => { 'name' => 'Units included',
25                           'default' => 0,
26                         },
27     'recur_unit_charge' => { 'name' => 'Additional charge per unit',
28                              'default' => 0,
29                            },
30     'datasrc' => { 'name' => 'DBI data source',
31                    'default' => '',
32                  },
33     'db_username' => { 'name' => 'Database username',
34                        'default' => '',
35                      },
36     'db_password' => { 'name' => 'Database username',
37                        'default' => '',
38                      },
39     'query' => { 'name' => 'SQL query',
40                  'default' => '',
41                },
42   },
43   'fieldorder' => [qw( setup_fee recur_flat unused_credit recur_included recur_unit_charge datasrc db_username db_password query )],
44  # 'setup' => 'what.setup_fee.value',
45  # 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\") or die $DBI::errstr; \'',
46  #'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 + \';\'',
47   #'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 + \';\'',
48   'weight' => '70',
49 );
50
51 sub calc_recur {
52   my($self, $cust_pkg ) = @_;
53
54   my $dbh = DBI->connect( map { $self->option($_) }
55                               qw( datasrc db_username db_password )
56                         )
57     or die $DBI::errstr;
58
59   my $sth = $dbh->prepare( $self->option('query') )
60     or die $dbh->errstr;
61
62   my $units = 0;
63   foreach my $cust_svc (
64     grep { $_->part_svc->svcdb eq "svc_domain" } $cust_pkg->cust_svc
65   ) {
66     my $domain = $cust_svc->svc_x->domain;
67     $sth->execute($domain) or die $sth->errstr;
68
69     $units += $sth->fetchrow_arrayref->[0];
70   }
71
72   $units -= $self->option('recur_included');
73   $units = 0 if $units < 0;
74
75   $self->option('recur_flat') + $units * $self->option('recur_unit_charge');
76 }
77
78 sub is_free_options {
79   qw( setup_fee recur_flat recur_unit_charge );
80 }
81
82 sub base_recur {
83   my($self, $cust_pkg) = @_;
84   $self->option('recur_flat');
85 }
86
87 1;