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);
149 my $cust_pkg = shift;
150 my $cust_main = $cust_pkg->cust_main;
151 # force it to act like a prorate package, is what this means
152 # because we made a distinction once between prorate and flat packages
153 if ( $cust_main->force_prorate_day and $cust_main->prorate_day ) {
154 return ( $cust_main->prorate_day );
156 if ( $self->option('sync_bill_date',1) ) {
157 my $next_bill = $cust_pkg->cust_main->next_bill_date;
159 return (localtime($next_bill))[3];
161 # This is the customer's only active package and hasn't been billed
162 # yet, so set the cutoff day to either today or tomorrow, whichever
163 # would result in a full period after rounding.
164 my $setup = $cust_pkg->setup; # because it's "now"
165 my $rounding_mode = $self->option('prorate_round_day',1);
166 return () if !$setup or !$rounding_mode;
167 my ($sec, $min, $hour, $mday, $mon, $year) = localtime($setup);
169 if ( ( $rounding_mode == 1 and $hour >= 12 )
170 or ( $rounding_mode == 3 and ( $sec > 0 or $min > 0 or $hour > 0 ))
172 # then the prorate period will be rounded down to start from
173 # midnight tomorrow, so the cutoff day should be the current day +
175 $setup = timelocal(59,59,23,$mday,$mon,$year) + 1;
176 $mday = (localtime($setup))[3];
178 # otherwise, it will be rounded up, so leave the cutoff day at today.
185 sub can_discount { 1; }