fix mixin inheritence preventing prorate_delayed packages from billing, RT#14372
[freeside.git] / FS / FS / part_pkg / sql_external.pm
1 package FS::part_pkg::sql_external;
2 use base qw( FS::part_pkg::discount_Mixin FS::part_pkg::recur_Common );
3
4 use strict;
5 use vars qw( %info );
6 use DBI;
7 #use FS::Record qw(qsearch qsearchs);
8
9 %info = (
10   'name' => 'Base charge plus additional fees for external services from a configurable SQL query',
11   'shortname' => 'External SQL query',
12   'inherit_fields' => [ 'global_Mixin' ],
13   'fields' => {
14     'cutoff_day'    => { 'name' => 'Billing Day (1 - 28) for prorating or '.
15                                    'subscription',
16                          'default' => '1',
17                        },
18     'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
19                                     'for one full period after that',
20                           'type' => 'checkbox',
21                        },
22
23     'recur_method'  => { 'name' => 'Recurring fee method',
24                          #'type' => 'radio',
25                          #'options' => \%recur_method,
26                          'type' => 'select',
27                          'select_options' => \%FS::part_pkg::recur_Common::recur_method,
28                        },
29     'datasrc' => { 'name' => 'DBI data source',
30                    'default' => '',
31                  },
32     'db_username' => { 'name' => 'Database username',
33                        'default' => '',
34                      },
35     'db_password' => { 'name' => 'Database password',
36                        'default' => '',
37                      },
38     'query' => { 'name' => 'SQL query',
39                  'default' => '',
40                },
41   },
42   'fieldorder' => [qw( recur_method cutoff_day
43                       add_full_period datasrc db_username db_password query 
44                   )],
45   'weight' => '58',
46 );
47
48 sub price_info {
49     my $self = shift;
50     my $str = $self->SUPER::price_info;
51     $str .= " plus per-service charges" if $str;
52     $str;
53 }
54
55 sub calc_recur {
56   my $self = shift;
57   my($cust_pkg, $sdate, $details, $param ) = @_;
58   my $price = 0;
59
60   my $dbh = DBI->connect( map { $self->option($_) }
61                               qw( datasrc db_username db_password )
62                         )
63     or die $DBI::errstr;
64
65   my $sth = $dbh->prepare( $self->option('query') )
66     or die $dbh->errstr;
67
68   foreach my $cust_svc (
69     grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc
70   ) {
71     my $id = $cust_svc->svc_x->id;
72     $sth->execute($id) or die $sth->errstr;
73     $price += $sth->fetchrow_arrayref->[0];
74   }
75
76   $param->{'override_charges'} = $price;
77   $self->calc_recur_Common($cust_pkg,$sdate,$details,$param);
78 }
79
80 sub can_discount { 1; }
81
82 sub is_free { 0; }
83
84 1;