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',
52 'name' => 'Query output style',
54 'select_options' => \%query_style,
58 'fieldorder' => [qw( recur_method cutoff_day sync_bill_date),
59 FS::part_pkg::prorate_Mixin::fieldorder,
60 qw( datasrc db_username db_password query query_style
67 my $str = $self->SUPER::price_info(@_);
68 $str .= " plus per-service charges" if $str;
74 my($cust_pkg, $sdate, $details, $param ) = @_;
76 my $quantity; # can be overridden; if not we use the default
78 my $dbh = FS::DBI->connect( map { $self->option($_) }
79 qw( datasrc db_username db_password )
81 or die $FS::DBI::errstr;
83 my $sth = $dbh->prepare( $self->option('query') )
86 foreach my $cust_svc (
87 grep { $_->part_svc->svcdb eq "svc_external" } $cust_pkg->cust_svc
89 my $id = $cust_svc->svc_x->id;
90 $sth->execute($id) or die $sth->errstr;
92 if ( $self->option('query_style') eq 'detailed' ) {
94 while (my $row = $sth->fetchrow_hashref) {
95 if (exists $row->{amount}) {
96 if ( $row->{amount} eq '' ) {
98 } elsif ( $row->{amount} =~ /^\d+(?:\.\d+)?$/ ) {
99 $price += $row->{amount};
101 die "sql_external query returned non-numeric amount: $row->{amount}";
104 if (defined $row->{quantity}) {
105 if ( $row->{quantity} eq '' ) {
107 } elsif ( $row->{quantity} =~ /^\d+$/ ) {
108 $quantity += $row->{quantity};
110 die "sql_external query returned non-integer quantity: $row->{quantity}";
114 my $detail = FS::cust_bill_pkg_detail->new;
115 foreach my $field (@detail_cols) {
116 if (exists $row->{$field}) {
117 $detail->set($field, $row->{$field});
120 if (!$detail->get('detail')) {
121 die "sql_external query did not return detail description";
122 # or make something up?
123 # or just don't insert the detail?
126 push @$details, $detail;
131 # simple style: returns only a single value, which is the price
132 $price += $sth->fetchrow_arrayref->[0];
136 $price = sprintf('%.2f', $price);
138 # XXX probably shouldn't allow package quantity > 1 on these packages.
139 if ($cust_pkg->quantity > 1) {
140 warn "sql_external package #".$cust_pkg->pkgnum." has quantity > 1\n";
143 $param->{'override_quantity'} = $quantity;
144 $param->{'override_charges'} = $price;
145 ($cust_pkg->quantity || 1) * $self->calc_recur_Common($cust_pkg,$sdate,$details,$param);
149 my( $self, $cust_pkg ) = @_;
150 my $error = FS::part_pkg::flat::cutoff_day( $self, $cust_pkg );
154 sub can_discount { 1; }