1 package FS::part_pkg::sql_external;
2 use base qw( FS::part_pkg::discount_Mixin FS::part_pkg::recur_Common );
7 #use FS::Record qw(qsearch qsearchs);
9 tie our %query_style, 'Tie::IxHash', (
10 'simple' => 'Simple (a single value for the recurring charge)',
11 'detailed' => 'Detailed (multiple rows for invoice details)',
14 our @detail_cols = ( qw(amount format duration phonenum accountcode
15 startdate regionname detail)
18 'name' => 'Base charge plus additional fees for external services from a configurable SQL query',
19 'shortname' => 'External SQL query',
20 'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
22 'sync_bill_date' => { 'name' => 'Prorate first month to synchronize '.
23 'with the customer\'s other packages',
26 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
31 'recur_method' => { 'name' => 'Recurring fee method',
33 #'options' => \%recur_method,
35 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
37 'datasrc' => { 'name' => 'DBI data source',
40 'db_username' => { 'name' => 'Database username',
43 'db_password' => { 'name' => 'Database password',
46 'query' => { 'name' => 'SQL query',
51 'name' => 'Query output style',
53 'select_options' => \%query_style,
57 'fieldorder' => [qw( recur_method cutoff_day sync_bill_date),
58 FS::part_pkg::prorate_Mixin::fieldorder,
59 qw( datasrc db_username db_password query query_style
66 my $str = $self->SUPER::price_info(@_);
67 $str .= " plus per-service charges" if $str;
73 my($cust_pkg, $sdate, $details, $param ) = @_;
75 my $quantity; # can be overridden; if not we use the default
77 my $dbh = DBI->connect( map { $self->option($_) }
78 qw( datasrc db_username db_password )
82 my $sth = $dbh->prepare( $self->option('query') )
85 foreach my $cust_svc (
86 grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc
88 my $id = $cust_svc->svc_x->id;
89 $sth->execute($id) or die $sth->errstr;
91 if ( $self->option('query_style') eq 'detailed' ) {
93 while (my $row = $sth->fetchrow_hashref) {
94 if (exists $row->{amount}) {
95 if ( $row->{amount} eq '' ) {
97 } elsif ( $row->{amount} =~ /^\d+(?:\.\d+)?$/ ) {
98 $price += $row->{amount};
100 die "sql_external query returned non-numeric amount: $row->{amount}";
103 if (defined $row->{quantity}) {
104 if ( $row->{quantity} eq '' ) {
106 } elsif ( $row->{quantity} =~ /^\d+$/ ) {
107 $quantity += $row->{quantity};
109 die "sql_external query returned non-integer quantity: $row->{quantity}";
113 my $detail = FS::cust_bill_pkg_detail->new;
114 foreach my $field (@detail_cols) {
115 if (exists $row->{$field}) {
116 $detail->set($field, $row->{$field});
119 if (!$detail->get('detail')) {
120 die "sql_external query did not return detail description";
121 # or make something up?
122 # or just don't insert the detail?
125 push @$details, $detail;
130 # simple style: returns only a single value, which is the price
131 $price += $sth->fetchrow_arrayref->[0];
135 $price = sprintf('%.2f', $price);
137 # XXX probably shouldn't allow package quantity > 1 on these packages.
138 if ($cust_pkg->quantity > 1) {
139 warn "sql_external package #".$cust_pkg->pkgnum." has quantity > 1\n";
142 $param->{'override_quantity'} = $quantity;
143 $param->{'override_charges'} = $price;
144 ($cust_pkg->quantity || 1) * $self->calc_recur_Common($cust_pkg,$sdate,$details,$param);
148 my( $self, $cust_pkg ) = @_;
149 my $error = FS::part_pkg::flat::cutoff_day( $self, $cust_pkg );
153 sub can_discount { 1; }