communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[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   'fields' => {
15     'setup_fee' => { 'name' => 'Setup fee for this package',
16                      'default' => 0,
17                    },
18     'recur_fee' => { '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     'recur_included' => { 'name' => 'Units included',
26                           'default' => 0,
27                         },
28     'recur_unit_charge' => { 'name' => 'Additional charge per unit',
29                              'default' => 0,
30                            },
31     'datasrc' => { 'name' => 'DBI data source',
32                    'default' => '',
33                  },
34     'db_username' => { 'name' => 'Database username',
35                        'default' => '',
36                      },
37     'db_password' => { 'name' => 'Database username',
38                        'default' => '',
39                      },
40     'query' => { 'name' => 'SQL query',
41                  'default' => '',
42                },
43   },
44   'fieldorder' => [qw( setup_fee recur_fee unused_credit recur_included recur_unit_charge datasrc db_username db_password query )],
45  # 'setup' => 'what.setup_fee.value',
46  # 'recur' => '\'my $dbh = DBI->connect(\"\' + what.datasrc.value + \'\", \"\' + what.db_username.value + \'\") or die $DBI::errstr; \'',
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_fee.value + \' + $units * \' + what.recur_unit_charge.value + \';\'',
48   #'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 + \';\'',
49   'weight' => '56',
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_fee') + $units * $self->option('recur_unit_charge');
77 }
78
79 sub can_discount { 0; }
80
81 sub is_free_options {
82   qw( setup_fee recur_fee recur_unit_charge );
83 }
84
85 sub base_recur {
86   my($self, $cust_pkg) = @_;
87   $self->option('recur_fee');
88 }
89
90 1;