[Astpp-commit] SF.net SVN: astpp:[2271] trunk/modules/ASTPP/lib/ASTPP.pm
Brought to you by:
darrenkw
From: <dar...@us...> - 2009-10-02 20:44:33
|
Revision: 2271 http://astpp.svn.sourceforge.net/astpp/?rev=2271&view=rev Author: darrenkw Date: 2009-10-02 20:44:18 +0000 (Fri, 02 Oct 2009) Log Message: ----------- Copy a bunch of code into the module. This is required to get it working under mod_perl. Modified Paths: -------------- trunk/modules/ASTPP/lib/ASTPP.pm Modified: trunk/modules/ASTPP/lib/ASTPP.pm =================================================================== --- trunk/modules/ASTPP/lib/ASTPP.pm 2009-10-02 20:43:15 UTC (rev 2270) +++ trunk/modules/ASTPP/lib/ASTPP.pm 2009-10-02 20:44:18 UTC (rev 2271) @@ -5,7 +5,12 @@ use warnings; use DBI; use Data::Paginate; +use Locale::gettext_pp qw(:locale_h); +bindtextdomain( "astpp", "/usr/local/share/locale" ); +textdomain("astpp"); + + require Exporter; our @ISA = qw(Exporter); @@ -83,6 +88,47 @@ # $self->{_config} = %config_hash if %config_hash; #} +sub load_config +{ + my ($self, %arg) = @_; + my $config; + open( CONFIG, "</var/lib/astpp/astpp-config.conf" ); + while (<CONFIG>) { + chomp; # no newline + s/#.*//; # no comments + s/^\s+//; # no leading white + s/\s+$//; # no trailing white + next unless length; # anything left? + my ( $var, $value ) = split( /\s*=\s*/, $_, 2 ); + $config->{$var} = $value; + } + close(CONFIG); + return $config; +} + +sub connect_db +{ + my ($self, %arg) = @_; + my ( $dbh, $dsn ); + if ( $arg{dbengine} eq "MySQL" ) { + $dsn = "DBI:mysql:database=$arg{dbname};host=$arg{dbhost}"; + } + elsif ( $arg{dbengine} eq "Pgsql" ) { + $dsn = "DBI:Pg:database=$arg{dbname};host=$arg{dbhost}"; + } + $dbh = DBI->connect( $dsn, $arg{dbuser}, $arg{dbpass} ); + if ( !$dbh ) { + print STDERR "ASTPP DATABASE IS DOWN\n"; + return 0; + } + else { + $dbh->{mysql_auto_reconnect} = 1; + print STDERR gettext("Connected to ASTPP Database!") . "\n"; + return $dbh; + } +} + + sub ip_address_authenticate #Authenticates call by caller ip address. Works with both Asterisk(tm) and Freeswitch(tm) # Requires # ip_address = IP Address of calling device @@ -972,8 +1018,281 @@ } } +sub get_did +{ + my ($self, %arg) = @_; + my ( $tmp, $sql, $diddata ); + if (!$arg{reseller} || $arg{reseller} eq "") { + $tmp = "SELECT * FROM dids WHERE number = " + . $self->{_astpp_db}->quote($arg{did}); + } else { + $tmp = + "SELECT dids.number AS number, " + . "reseller_pricing.monthlycost AS monthlycost, " + . "reseller_pricing.prorate AS prorate, " + . "reseller_pricing.setup AS setup, " + . "reseller_pricing.cost AS cost, " + . "reseller_pricing.connectcost AS connectcost, " + . "reseller_pricing.includedseconds AS includedseconds, " + . "reseller_pricing.inc AS inc, " + . "reseller_pricing.disconnectionfee AS disconnectionfee, " + . "dids.provider AS provider, " + . "dids.country AS country, " + . "dids.city AS city, " + . "dids.province AS province, " + . "dids.extensions AS extensions, " + . "dids.account AS account, " + . "dids.variables AS variables, " + . "dids.options AS options, " + . "dids.maxchannels AS maxchannels, " + . "dids.chargeonallocation AS chargeonallocation, " + . "dids.allocation_bill_status AS allocation_bill_status, " + . "dids.limittime AS limittime, " + . "dids.dial_as AS dial_as, " + . "dids.status AS status " + . "FROM dids, reseller_pricing " + . "WHERE dids.number = " . $self->{_astpp_db}->quote($arg{did}) + . " AND reseller_pricing.type = '1' AND reseller_pricing.reseller = " + . $self->{_astpp_db}->quote($arg{reseller}) . " AND reseller_pricing.note = " + . $self->{_astpp_db}->quote($arg{did}); + } + print STDERR "$tmp\n"; + $sql = + $self->{_astpp_db}->prepare($tmp); + $sql->execute; + $diddata = $sql->fetchrow_hashref; + $sql->finish; + return $diddata; +} +sub get_account() { + my ($self, %arg) = @_; + my ( $sql, $accountdata ); + $sql = + $self->{_astpp_db}->prepare( "SELECT * FROM accounts WHERE number = " + . $self->{_astpp_db}->quote($arg{account}) + . " AND status = 1" ); + $sql->execute; + $accountdata = $sql->fetchrow_hashref; + $sql->finish; + if ($accountdata) { + return $accountdata; + } else { + $sql = + $self->{_astpp_db}->prepare( "SELECT * FROM accounts WHERE cc = " + . $self->{_astpp_db}->quote($arg{account}) + . " AND status = 1" ); + $sql->execute; + $accountdata = $sql->fetchrow_hashref; + $sql->finish; + } + if ($accountdata) { + return $accountdata; + } else { + $sql = + $self->{_astpp_db}->prepare( "SELECT * FROM accounts WHERE accountid = " + . $self->{_astpp_db}->quote($arg{account}) + . " AND status = 1" ); + $sql->execute; + $accountdata = $sql->fetchrow_hashref; + $sql->finish; + return $accountdata; + } +} + +# Return data on specified pricelist +sub get_pricelist() { + my ($self, %arg) = @_; + my $tmp = "SELECT * FROM pricelists WHERE name = " . $self->{_astpp_db}->quote($arg{pricelist}); + my $sql = $self->{_astpp_db}->prepare($tmp); + print STDERR "$tmp\n" . "\n"; + $sql->execute; + my $pricelistdata = $sql->fetchrow_hashref; + $sql->finish; + return $pricelistdata; +} + + +sub max_length() { + my ($self, %arg) = @_; + my ($branddata, $numdata, $credit, $credit_limit, $maxlength); + $branddata = &get_pricelist($self, pricelist => $arg{account_pricelist} ); # Fetch all the brand info from the db. + + $numdata = &get_route($self, account => $arg{account}, destination => $arg{destination}, pricelist => $arg{account_pricelist} ); # Find the appropriate rate to charge the customer. + + if ( !$numdata->{pattern} ){ # If the pattern doesn't exist, we don't know what to charge the customer + # and therefore must exit. + print STDERR "CALLSTATUS 1\n" if $arg{debug} == 1; + print STDERR "INVALID PHONE NUMBER\n" if $arg{debug} == 1; + return (1,0); + } + print STDERR "Found pattern: $numdata->{pattern}\n"; + $credit = &accountbalance($self, account => $arg{account} ); # Find the available credit to the customer. + print STDERR "Account Balance: " . $credit * 10000; + $credit_limit = $arg{account_credit_limit} * 10000; + print STDERR "Credit Limit: $credit_limit"; + $credit = ($credit * -1) + ($credit_limit); # Add on the accounts credit limit. + #$credit = $credit / $arg{maxchannels} if $arg{maxchannels} > 0; + print STDERR "Credit: $credit \n"; + if ($arg{markup} > 0) { + $numdata->{connectcost} = + $numdata->{connectcost} * ( ( $arg{markup} / 10000 ) + 1 ); + $numdata->{cost} = + $numdata->{cost} * ( ( $arg{markup} / 10000 ) + 1 ); + } + if ( $numdata->{connectcost} > $credit ) { # If our connection fee is higher than the available money we can't connect. + return (0,0); + } + if ( $numdata->{cost} > 0 ) { + $maxlength = ( ( $credit - $numdata->{connectcost} ) / $numdata->{cost} ); + if ($arg{call_max_length} && $maxlength < $arg{call_max_length} / 1000){ + $maxlength = $arg{call_max_length} / 1000 / 60; + } + } + else { + $maxlength = $arg{max_free_length}; # If the call is set to be free then assign a max length. + } + if ( $numdata->{cost} > 0 ) { + $maxlength = ( ( $credit - $numdata->{connectcost} ) / $numdata->{cost} ); + if ($arg{call_max_length} && $maxlength < $arg{call_max_length} / 1000){ + $maxlength = $arg{call_max_length} / 1000 / 60; + } + } + else { + $maxlength = $arg{max_free_length}; # If the call is set to be free then assign a max length. + } + return (1, $maxlength,$numdata); +} + + + +# Return the balance for a specific ASTPP account. +sub accountbalance() { + my ($self, %arg) = @_; + my ( $tmp, $sql, $row, $debit, $credit, $balance, $posted_balance ); + $tmp = + "SELECT SUM(debit) FROM cdrs WHERE cardnum= " + . $self->{_astpp_db}->quote($arg{account}) + . " AND status NOT IN (1, 2)"; + $sql = $self->{_astpp_db}->prepare($tmp); + $sql->execute; + $row = $sql->fetchrow_hashref; + $debit = $row->{"SUM(debit)"}; + $sql->finish; + $tmp = + "SELECT SUM(credit) FROM cdrs WHERE cardnum= " + . $self->{_astpp_db}->quote($arg{account}) + . " AND status NOT IN (1, 2)"; + $sql = $self->{_astpp_db}->prepare($tmp); + $sql->execute; + $row = $sql->fetchrow_hashref; + $credit = $row->{"SUM(credit)"}; + $sql->finish; + $tmp = + "SELECT * FROM accounts WHERE number = " . $self->{_astpp_db}->quote($arg{account}); + $sql = $self->{_astpp_db}->prepare($tmp); + $sql->execute; + $row = $sql->fetchrow_hashref; + $posted_balance = $row->{balance}; + $sql->finish; + if ( !$credit ) { $credit = 0; } + if ( !$debit ) { $debit = 0; } + if ( !$posted_balance ) { $posted_balance = 0; } + $balance = ( $debit - $credit + $posted_balance ); + return $balance; +} + +# Return the appropriate "route" to use for determining costing on a call. +sub get_route() { + my ($self, %arg) = @_; +# TEMPORARY +my $config; +$config->{thirdlane_mods} = 1; +# + my ($branddata, $record, $sql, $tmp ); + my $carddata = &get_account($self, account => $arg{account}); + if ($arg{type} =~ /ASTPP-DID/) { + print STDERR "Call belongs to a DID.\n"; + $record = &get_did( reseller => $arg{reseller}, did => $arg{destination}); + $record->{comment} = $record->{city} . "," . $record->{province} . "," . $record->{country}; + $record->{pattern} = "DID:" . $arg{destination}; + $record->{pricelist} = $arg{pricelist}; + $branddata = &get_pricelist($self, pricelist => $arg{pricelist}); + print STDERR "pattern: $record->{pattern}\n" if $record->{pattern}; + } + elsif ($config->{thirdlane_mods} == 1 && $arg{type} =~ m/.\d\d\d-IN/) { + print STDERR "Call belongs to a Thirdlane(tm) DID.\n"; + ($arg{destination} = $arg{type}) =~ s/-IN//g; + print STDERR "Destination: $arg{destination} \n"; + $record = &get_did( reseller => $arg{reseller}, did => $arg{destination}); + $record->{comment} = $record->{city} . "," . $record->{province} . "," . $record->{country}; $record->{pattern} = "DID:" . $arg{destination}; + $branddata = &get_pricelist($self, pricelist => $arg{pricelist}); + print STDERR "pattern: $record->{pattern}\n" if $record->{pattern}; + } + else { + my @pricelists = split ( m/,/m, $arg{pricelist} ); + foreach my $pricelistname (@pricelists) { + $pricelistname =~ s/"//g; #Strip off quotation marks + print STDERR "Pricelist: $pricelistname \n"; + $record = &search_for_route($self, destination => $arg{destination}, pricelist => $arg{pricelist}); + print STDERR "pattern: $record->{pattern}\n" if $record->{pattern}; + last if $record->{pattern}; #Returnes if we've found a match. + } + + while ( !$record->{pattern} && $carddata->{reseller} ) { + $carddata = &get_account($self, account => $carddata->{reseller}); + $record = &search_for_route($self, destination => $arg{destination}, pricelist => $arg{pricelist}); + print STDERR "pattern: $record->{pattern}\n" if $record->{pattern}; + } + if (!$record->{pattern}) { #If we still haven't found a match then we modify the dialed number as per the regular expressions set + # in the account. + my @regexs = split(m/","/m, $carddata->{dialed_modify}); + foreach my $regex (@regexs) { + $regex =~ s/"//g; #Strip off quotation marks + my ($grab,$replace) = split(m!/!i, $regex); # This will split the variable into a "grab" and "replace" as needed + print STDERR "Grab: $grab\n"; + print STDERR "Replacement: $replace\n"; + print STDERR "Phone Before: $arg{destination}\n"; + $arg{destination} =~ s/$grab/$replace/is; + print STDERR "Phone After: $arg{destination}\n"; + } + $record = &search_for_route($self, destination => $arg{destination}, pricelist => $arg{default_brand}); + print STDERR "pattern: $record->{pattern}\n" if $record->{pattern}; + } + if ( !$record->{pattern} ) { #If we have not found a route yet then we look in the "Default" pricelist. + $record = &search_for_route($self, destination => $arg{destination}, pricelist => $arg{default_brand}); + print STDERR "pattern: $record->{pattern}\n" if $record->{pattern}; + } + print STDERR "Route: $record->{comment} Cost: $record->{cost} Pricelist: $record->{pricelist} Pattern: $record->{pattern}\n" if $record; + } + if ( $record->{inc} eq "" || $record->{inc} == 0 ) { + $branddata = &get_pricelist($self, pricelist => $arg{pricelist}); + $record->{inc} = $branddata->{inc}; + } + return $record; +} + +sub search_for_route(){ +# my ($astpp_db,$config,$destination,$pricelist) = @_; + my ($self, %arg) = @_; + my ($tmp,$sql,$record); + $tmp = "SELECT * FROM routes WHERE " + . $self->{_astpp_db}->quote($arg{destination}) + . " RLIKE pattern AND pricelist = " + . $self->{_astpp_db}->quote($arg{pricelist}) + . " ORDER BY LENGTH(pattern) DESC"; + print STDERR "$tmp\n"; + $sql = + $self->{_astpp_db}->prepare($tmp); + $sql->execute; + $record = $sql->fetchrow_hashref; + $sql->finish; + return $record; +}; + + + # Preloaded methods go here. 1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |