1 package FS::part_pkg::sql_generic;
4 use vars qw(@ISA %info);
6 #use FS::Record qw(qsearch qsearchs);
7 use FS::part_pkg::flat;
9 @ISA = qw(FS::part_pkg::flat);
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' ],
16 'recur_included' => { 'name' => 'Units included',
19 'recur_unit_charge' => { 'name' => 'Additional charge per unit',
22 'datasrc' => { 'name' => 'DBI data source',
25 'db_username' => { 'name' => 'Database username',
28 'db_password' => { 'name' => 'Database username',
31 'query' => { 'name' => 'SQL query',
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 + \';\'',
45 my $str = $self->SUPER::price_info(@_);
46 $str .= " plus per-service charges" if $str;
51 my($self, $cust_pkg ) = @_;
53 my $dbh = FS::DBI->connect( map { $self->option($_) }
54 qw( datasrc db_username db_password )
56 or die $FS::DBI::errstr;
58 my $sth = $dbh->prepare( $self->option('query') )
62 foreach my $cust_svc (
63 grep { $_->part_svc->svcdb eq "svc_domain" } $cust_pkg->cust_svc
65 my $domain = $cust_svc->svc_x->domain;
66 $sth->execute($domain) or die $sth->errstr;
68 $units += $sth->fetchrow_arrayref->[0];
71 $units -= $self->option('recur_included');
72 $units = 0 if $units < 0;
74 $self->option('recur_fee') + $units * $self->option('recur_unit_charge');
77 sub can_discount { 0; }
80 qw( setup_fee recur_fee recur_unit_charge );
84 my($self, $cust_pkg) = @_;
85 $self->option('recur_fee');