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