[Astpp-commit] SF.net SVN: astpp: [2150] trunk
Brought to you by:
darrenkw
From: <dar...@us...> - 2007-11-03 21:35:25
|
Revision: 2150 http://astpp.svn.sourceforge.net/astpp/?rev=2150&view=rev Author: darrenkw Date: 2007-11-03 14:35:25 -0700 (Sat, 03 Nov 2007) Log Message: ----------- Many minor fixes. Moved some of the get_pricelist subroutines to the perl module. Also, added more pagination support directly in the perl module. Also corrected the issues with bug: [ 1800044 ] Debug Packages. Modified Paths: -------------- trunk/astpp-admin.cgi trunk/astpp-callingcards.agi trunk/astpp-common.pl trunk/modules/ASTPP/lib/ASTPP.pm trunk/templates/account-info.tpl Modified: trunk/astpp-admin.cgi =================================================================== --- trunk/astpp-admin.cgi 2007-11-03 03:07:15 UTC (rev 2149) +++ trunk/astpp-admin.cgi 2007-11-03 21:35:25 UTC (rev 2150) @@ -42,7 +42,7 @@ use Locale::gettext_pp qw(:locale_h); use Data::Dumper; use lib './lib', '../lib'; -#use warnings; +use warnings; use Asterisk::Manager; use Text::CSV; use HTML::Template; @@ -65,8 +65,7 @@ $freepbx_db $rt_db $openser_db $ASTPP); my $starttime = [gettimeofday]; $ASTPP = ASTPP->new; -$ASTPP->set_verbosity($verbosity); #Tell ASTPP debugging how verbose we want to -be. +$ASTPP->set_verbosity($verbosity); #Tell ASTPP debugging how verbose we want to be. $ASTPP->set_asterisk_agi($AGI); $ASTPP->set_pagination_script("astpp-admin.cgi"); my @Home = ( gettext("Home Page") ); @@ -536,6 +535,10 @@ if $params->{mode} eq gettext("Reseller Report"); return &build_reseller_user_report() if $params->{mode} eq gettext("User Report"); + return &build_packages() + if $params->{mode} eq gettext("Packages"); + return &build_counters() + if $params->{mode} eq gettext("Counters"); return &build_homepage() if $params->{mode} eq gettext("Home Page") @@ -1096,7 +1099,7 @@ return gettext("Cannot display reports until database is configured") unless $cdr_db; - return gettext("Comming Soon!"); + return gettext("Coming Soon!"); } # End of code changes by Sonia Ali Khan. @@ -1570,13 +1573,7 @@ filename => '/var/lib/astpp/templates/account-info.tpl', die_on_bad_params => $config->{template_die_on_bad_params} ); return gettext("Cannot view account until database is configured") unless $astpp_db; - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } - +@pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); return gettext("Cannot view account until pricelists configured") unless @pricelists; @@ -1873,7 +1870,7 @@ -values => \%applyablecharges )); - my (@account_did_list); + my (@account_did_list,$sql,$pagination); my @did_list = &list_dids_account( $astpp_db, $accountinfo->{number} ); foreach my $did_info (@did_list) { my (%row); @@ -1894,7 +1891,7 @@ "SELECT * FROM ani_map WHERE account = " . $astpp_db->quote( $accountinfo->{'number'} ) . " ORDER BY number"; - my $sql = $astpp_db->prepare($tmp); + $sql = $astpp_db->prepare($tmp); $sql->execute; my @account_ani_list; while ( my $record = $sql->fetchrow_hashref ) { @@ -1923,60 +1920,25 @@ } $template->param( account_ip_list => \@account_ip_list); $template->param( currency => $currency[0]); - ## START PAGINATION CODE - # set total_entries *once* then pass it around - # in the object's links from then on for efficiency: + ($sql, $pagination) = + $ASTPP->pagination( sql_select => "SELECT * FROM cdrs WHERE cardnum =" + . $astpp_db->quote( $accountinfo->{number} ) + . "and status IN (NULL, '', 0, 1)" + . " ORDER BY callstart DESC ", + sql_count => "SELECT COUNT(*) FROM cdrs WHERE cardnum =" + . $astpp_db->quote( $accountinfo->{number} ) + . "and status IN (NULL, '', 0, 1)", + results_per_page=> $config->{results_per_page}, + ve => $params->{ve}, + te => $params->{ee}, - my $verify = CGI::param('ve') || ''; - my $total_entries = int( CGI::param('te') ); - my $te_match = - $total_entries - ? Digest::MD5::md5_hex("unique_cypher-$total_entries-$where") - : ''; - if ( !$total_entries || $verify ne $te_match ) { - - # its not ok so re-fetch - $tmp = - "SELECT COUNT(*) FROM cdrs WHERE cardnum =" - . $astpp_db->quote( $accountinfo->{number} ) - . "and status IN (NULL, '', 0, 1)" - . " ORDER BY callstart DESC"; + ); + my $results = $astpp_db->prepare($sql); + $results->execute; $sql = $astpp_db->prepare($tmp); $sql->execute; - $record = $sql->fetchrow_hashref; - $total_entries = $record->{"COUNT(*)"}; - $sql->finish; - $te_match = Digest::MD5::md5_hex("unique_cypher-$total_entries-$where"); - } - if ( $total_entries <= 0 ) { $total_entries = 1; } - my $pgr = Data::Paginate->new( - { - 'start_array_index_at_zero' => 1, - 'total_entries' => $total_entries, - 'entries_per_page' => $results_per_page, - 'total_entries_verify_param_value' => $te_match - } - ); - - # only SELECT current page's records: - if ( $total_entries > $pgr->get_entries_on_this_page() ) { - $tmp = - "SELECT * FROM cdrs WHERE cardnum =" - . $astpp_db->quote( $accountinfo->{number} ) - . "and status IN (NULL, '', 0, 1)" - . " ORDER BY callstart DESC " - . " LIMIT ". ($pgr->get_first() - 1) . ", " . $pgr->get_entries_on_this_page(); - } else { - $tmp = - "SELECT * FROM cdrs WHERE cardnum =" - . $astpp_db->quote( $accountinfo->{'number'} ) - . "and status IN (NULL, '', 0, 1)" - . " ORDER BY callstart DESC "; - } - $sql = $astpp_db->prepare($tmp); - $sql->execute; my @account_cdr_list; - while ( my $record = $sql->fetchrow_hashref ) { + while ( my $record = $results->fetchrow_hashref ) { my %row; if (!$record->{callerid}) { $row{callerid} = gettext("N/A"); @@ -1984,11 +1946,14 @@ $row{callerid} = $record->{callerid}; } if (!$record->{uniqueid}) { $row{uniqueid} = gettext("N/A");} else { $row{uniqueid} = $record->{uniqueid};} - if (!$record->{disposition}) {$row{disposition} = gettext("N/A");} else {$row{disposition} = $record->{dispositio}; } + if (!$record->{disposition}) {$row{disposition} = gettext("N/A");} else {$row{disposition} = $record->{disposition}; } if (!$record->{notes}) { $row{notes} = ""; } else { $row{notes} = $record->{notes} ;} if (!$record->{callstart}) { $row{callstart} = ""; } else { $row{callstart} = $record->{callstart}; } if (!$record->{callednum}) { $row{callednum} = ""; } else {$row{callednum} = $record->{callednum};} if (!$record->{billseconds}) { $row{billseconds} = ""; } else { $row{billseconds} = $record->{billseconds};} + if (!$record->{cost}) { $row{cost} = ""; } else { $row{cost} = $record->{cost};} + $row{profit} = sprintf( "%.6f", ($record->{debit} - $record->{cost})/10000); + $row{cost} = sprintf( "%.6f", $row{cost}); if ( $record->{debit} ) { $row{debit} = $record->{debit} / 10000; @@ -2007,30 +1972,7 @@ push(@account_cdr_list, \%row); } $template->param( account_cdr_list => \@account_cdr_list); - if ( $total_entries => 1 ) - { #This section only applies if we have more than 1 page. - $pagination = - "<a href=\"astpp-admin.cgi?mode=" - . gettext("View Details") . "&ve=" - . $params->{ve} . "&te=" - . $total_entries - . "&pg=1\">" - . gettext("First Page") - . "</a> | "; - $pagination .= scalar $pgr->get_navi_html(); - $pagination .= - "<a href=\"astpp-admin.cgi?mode=" - . gettext("View Details") . "&ve=" - . $params->{ve} . "&te=" - . $total_entries . "&pg=" - . $pgr->get_last_page() . "\">" - . gettext("Last Page") . "</a>"; - } - else { - $pagination = gettext("Page 1 of 1"); - } - - $template->param( pagination => $pagination ); + $template->param( pagination => $pagination ); } else { $status .= @@ -2226,12 +2168,7 @@ filename => '/var/lib/astpp/templates/account-create.tpl', die_on_bad_params => $config->{template_die_on_bad_params} ); return gettext("Database is NOT configured!") . " \n" unless $astpp_db; - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); return gettext("Please configure 'Pricelists'") . "\n" unless @pricelists; if ( $params->{action} eq gettext("Generate Account") ) { @@ -2370,12 +2307,7 @@ my $active = gettext("ACTIVE"); my $inactive = gettext("INACTIVE"); my $deleted = gettext("DELETED"); - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); return gettext("Database is NOT configured!") . "\n" unless $astpp_db; if ( $params->{limit} < 1 ) { $params->{limit} = 0; } @@ -2859,6 +2791,9 @@ sub build_create_card() { my ( @pricelists, $status, $body, @brands ); + my $template = + HTML::Template->new( + filename => '/var/lib/astpp/templates/callingcards-create.tpl', die_on_bad_params => $config->{template_die_on_bad_params} ); return gettext("Database is NOT configured!") . " \n" unless $astpp_db; if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { @brands = &list_cc_brands_reseller( $astpp_db, $params->{username} ); @@ -2879,41 +2814,10 @@ } $status .= &generatecallingcards( $params, $config ); } - $body = start_multipart_form; - $body .= "<table class=\"default\">"; - $body .= "<tr class=\"header\"><td>"; - $body .= - gettext("Account Number") - . "</td><td>" - . gettext("Brand") - . "</td><td>" - . gettext("Value in pennies") - . "</td><td>" - . gettext("Quantity") - . "</td><td>" - . gettext("Status") - . "</td></tr>" - . "<tr class=\"rowone\"><td>" - . hidden( -name => "mode", -value => gettext("Create Card") ) - . textfield( -name => "account", -size => 20 ) - . "</td><td>" - . popup_menu( -name => "brand", -values => \@brands ) - . "</td><td>" - . textfield( -name => "value", -size => 8 ) - . "</td><td>" - . textfield( -name => "count", -size => 8 ) - . "</td><td>" - . popup_menu( - -name => "status", - -values => \@cardstatus - ) - . "</td><td>" - . "</td></tr><tr><td>" - . submit( - -name => 'action', - -value => gettext("Generate Card(s)") - ) . "</td></tr></table>"; - return $body; + $template->param(brand_list => popup_menu( -name=>"brand", -values => \@brands)); + $template->param(status_list => popup_menu( -name => "status", -values => \@cardstatus)); + $template->param(status => $status); + return $template->output; } sub build_update_card_status() { @@ -2925,12 +2829,7 @@ else { @brands = &list_cc_brands($astpp_db); } - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); return gettext("Please configure 'Pricelists'!") . "\n" unless @pricelists; if ( $params->{action} eq gettext("Update Status on Card(s)") ) { @@ -4338,12 +4237,7 @@ else { $number = $params->{number}; } - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); my @resellerlist = &list_resellers($astpp_db); unshift( @resellerlist, "" ); $accountinfo = &get_account( $astpp_db, $number ); @@ -4646,12 +4540,7 @@ $params->{action} = gettext("Information..."); } elsif ( $params->{action} eq gettext("Add...") ) { - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); return gettext("Please configure 'Pricelists'!") . "\n" unless @pricelists; push @pricelists, ""; @@ -4698,12 +4587,7 @@ "; } elsif ( $params->{action} eq gettext("Edit...") ) { - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); return gettext("Please configure 'Pricelists'!") . "\n" unless @pricelists; push @pricelists, ""; @@ -7403,12 +7287,7 @@ sub build_routes() { my ( @pricelists, $sql, $count, $tmp ); return gettext("Database is NOT configured!") . "\n" unless $astpp_db; - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); return gettext("Pricelists Do NOT Exist!") . "\n" unless @pricelists; if ( $params->{limit} < 1 ) { $params->{limit} = 0 } my $results_per_page = $config->{results_per_page}; @@ -7684,12 +7563,7 @@ $params->{action} = gettext("Information..."); } elsif ( $params->{action} eq gettext("Add...") ) { - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); $body = start_form . "<table class=\"default\">" . "<tr class=\"header\"><td colspan=2>" @@ -7908,110 +7782,41 @@ } sub build_counters() { - my ($tmp,@counter_list); + my ($tmp,@counter_list,$sql_select,$sql_count); my $template = HTML::Template->new( filename => '/var/lib/astpp/templates/counters-list.tpl', die_on_bad_params => $config->{template_die_on_bad_params} ); - my $results_per_page = $config->{results_per_page}; - if ( $results_per_page eq "" || $results_per_page < 2 ) { - $results_per_page = 25; + if ( $params->{logintype} == 2 ) { + $sql_select = "SELECT counters.id AS id, packages.name AS name,counters.account AS account, counters.seconds AS seconds, counters.status AS status FROM counters,packages WHERE package IN(SELECT id FROM packages WHERE pricelist IN (SELECT name from pricelists WHERE reseller IS NULL)) AND counters.package = packages.id;"; + $sql_count = "SELECT COUNT(*) FROM counters WHERE package IN(SELECT id FROM packages WHERE pricelist IN (SELECT name from pricelists WHERE reseller IS NULL)) AND counters.package = packages.id;"; } - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - $tmp = - "SELECT * FROM pricelists WHERE status < 2 AND reseller = " - . $astpp_db->quote( $params->{username} ); + elsif ( $params->{logintype} == 1 ) { + $sql_select = + "SELECT counters.id AS id, packages.name AS name,counters.account AS account, counters.seconds AS seconds, counters.status AS status FROM counters,packages WHERE package IN(SELECT id FROM packages WHERE pricelist IN(SELECT name FROM pricelists WHERE reseller = " + . $astpp_db->quote( $params->{username} ) . ")) AND counters.package = packages.id;"; + $sql_count = + "SELECT COUNT(*) FROM counters WHERE package IN(SELECT id FROM packages WHERE pricelist IN(SELECT name FROM pricelists WHERE reseller = " + . $astpp_db->quote( $params->{username} ) . ") AND counters.package = packages.id;"; } - else { - $tmp = - "SELECT * FROM pricelists WHERE status < 2 AND reseller IS NULL"; - } - my $sql = $astpp_db->do($sql); - print STDERR $tmp if $config->{debug} == 1; - $sql = $astpp_db->prepare($tmp); - ## START PAGINATION CODE - # set total_entries *once* then pass it around - # in the object's links from then on for efficiency: - - my $verify = CGI::param('ve') || ''; - my $total_entries = int( CGI::param('te') ); - my $te_match = - $total_entries - ? Digest::MD5::md5_hex("unique_cypher-$total_entries-$where") - : ''; - if ( !$total_entries || $verify ne $te_match ) { - - # its not ok so re-fetch - $sql = $astpp_db->prepare("SELECT COUNT(*) FROM counters"); - $sql->execute; - $record = $sql->fetchrow_hashref; - $total_entries = $record->{"COUNT(*)"}; - $sql->finish; - $te_match = Digest::MD5::md5_hex("unique_cypher-$total_entries-$where"); - } - - #if ($te_match <= 0) { $te_match = 0; } - if ( $total_entries <= 0 ) { $total_entries = 1; } - - # otherwise its all ok so use it - my $pgr = Data::Paginate->new( - { - 'start_array_index_at_zero' => 1, - 'total_entries' => $total_entries, - 'entries_per_page' => $results_per_page, - 'total_entries_verify_param_value' => $te_match - } - ); - - # only SELECT current page's records: - if ( $total_entries > $pgr->get_entries_on_this_page() ) { - $sql = - "SELECT * FROM counters ORDER BY id LIMIT " - . ( $pgr->get_first() - 1 ) . ", " - . $pgr->get_entries_on_this_page(); - } - else { - $sql = "SELECT * FROM counters ORDER BY id"; - } - print STDERR $sql if $config->{debug} == 1; - $sql = $astpp_db->prepare($sql); - $sql->execute; - - # END OF PAGINATION CODE - while ( my $counterinfo = $sql->fetchrow_hashref ) { + my ($sql, $pagination) = + $ASTPP->pagination( sql_select => $sql_select, + sql_count => $sql_count, + results_per_page=> $config->{results_per_page}, + ve => $params->{ve}, + te => $params->{ee}, + + ); + my $results = $astpp_db->prepare($sql); + $results->execute; + while ( my $counterinfo = $results->fetchrow_hashref ) { print STDERR "COUNTER: " . $counterinfo->{id} if $config->{debug} == 1; my (%row); - - # $row{} = $name; - # $row{balance} = $balance / 10000; - # $row{call_count} = $record->{"COUNT(*)"}; $row{id} = $counterinfo->{id}; - $row{package} = $counterinfo->{package}; + $row{package} = $counterinfo->{name}; $row{account} = $counterinfo->{account}; $row{seconds} = $counterinfo->{seconds}; push( @counter_list, \%row ); } - if ( $total_entries >= 1 ) - { #This section only applies if we have more than 1 page. - $pagination = - "<a href=\"astpp-admin.cgi?mode=" - . gettext("Counters") . "&ve=" - . $params->{ve} . "&te=" - . $total_entries - . "&pg=1\">" - . gettext("First Page") - . "</a> | "; - $pagination .= scalar $pgr->get_navi_html(); - $pagination .= - "<a href=\"astpp-admin.cgi?mode=" - . gettext("Counters") . "&ve=" - . $params->{ve} . "&te=" - . $total_entries . "&pg=" - . $pgr->get_last_page() . "\">" - . gettext("Last Page") . "</a>"; - } - else { - $pagination = gettext("Page 1 of 1"); - } $template->param( counter_list => \@counter_list ); $template->param( pagination => $pagination ); return $template->output; @@ -8020,72 +7825,33 @@ sub build_packages() { my ( $sql, $record, $count, $tmp, $pageno, $pagesrequired ); return gettext("Database is NOT configured!") . "\n" unless $astpp_db; + my $template = + HTML::Template->new( + filename => '/var/lib/astpp/templates/packages-list.tpl', die_on_bad_params => $config->{template_die_on_bad_params} ); if ( $params->{limit} < 1 ) { $params->{limit} = 0 } my $results_per_page = $config->{results_per_page}; if ( $results_per_page eq "" ) { $results_per_page = 25; } - if ( !$params->{action} ) { - $params->{action} = gettext("Information..."); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); + $template->param( pricelists => popup_menu( + -name => 'pricelist', + -values => \@pricelists + )); if ( $params->{action} eq gettext("Edit...") ) { - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } - $tmp = + $tmp = "SELECT * FROM packages WHERE id = " . $astpp_db->quote( $params->{id} ); $sql = $astpp_db->prepare($tmp); $sql->execute; $record = $sql->fetchrow_hashref; $sql->finish; - $body = start_form; - $body .= - "<table class=\"default\">" - . "<tr class=\"header\"><td>" - . hidden( -name => "mode", -value => gettext("Packages") ) - . hidden( -name => "id", -value => $params->{id} ) - . gettext("Counter Name") - . "</td><td>" - . gettext("Pricelist") - . "</td><td>" - . gettext("Pattern") - . "</td><td>" - . gettext("Included Seconds") - . "</td><td>" - . gettext("Action") - . "</td></tr>"; - $body .= "<tr><td>" - . textfield( - -name => 'name', - -size => 20, - -default => $record->{name} - ) - . "</td><td>" - . popup_menu( - -name => 'pricelist', + $template->param(id => $params->{id} ); + $template->param(current_name => $record->{name} ); + $template->param(edit_pricelists => popup_menu( + -name => 'edit_pricelist', -values => \@pricelists, - -default => $record->{pricelist} - ) - . "</td><td>" - . textfield( - -name => 'pattern', - -size => 30, - -default => $record->{pattern} - ) - . "</td><td>" - . textfield( - -name => 'includedseconds', - -size => 6, - -default => $record->{includedseconds} - ) - . "</td><td>" - . submit( -name => 'action', -value => gettext("Save...") ) - . "</td></tr> -</table> -"; - return $body; + -default => $record->{pricelist} )); + $template->param(current_pattern => $record->{pattern}); + $template->param(current_includedseconds => $record->{includedseconds}); } elsif ( $params->{action} eq gettext("Insert...") ) { my $tmp = @@ -8111,13 +7877,14 @@ } elsif ( $params->{action} eq gettext("Save...") ) { my $tmp = - "UPDATE pricelists SET" . " name=" - . $astpp_db->quote( $params->{name} ) . ", " . " inc=" - . $astpp_db->quote( $params->{inc} ) . ", " - . " markup=" - . $astpp_db->quote( $params->{markup} ) + "UPDATE packages SET name= " + . $astpp_db->quote( $params->{edit_name} ) . ", pricelist = " + . $astpp_db->quote( $params->{edit_pricelist} ) . ", pattern=" + . $astpp_db->quote( $params->{edit_pattern} ) . ", includedseconds=" + . $astpp_db->quote( $params->{edit_includedseconds} ) . " WHERE id = " . $astpp_db->quote( $params->{id} ); + print STDERR $tmp if $config->{debug} == 1; if ( $astpp_db->do($tmp) ) { $status .= gettext("Package: ") @@ -8129,7 +7896,6 @@ gettext("Package: ") . $params->{name} . gettext(" Failed to Update!"); - print STDERR $tmp if $config->{debug} == 1; } $params->{action} = gettext("Information..."); } @@ -8152,158 +7918,44 @@ } $params->{action} = gettext("Information..."); } - elsif ( $params->{action} eq gettext("Add...") ) { - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = ( $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } - $body = start_form; - $body .= - "<table class=\"default\">" - . "<tr class=\"header\"><td>" - . hidden( -name => "mode", -value => gettext("Packages") ) - . gettext("Counter Name") - . "</td><td>" - . gettext("Pricelist") - . "</td><td>" - . gettext("Pattern") - . "</td><td>" - . gettext("Included Seconds") - . "</td><td>" - . gettext("Action") - . "</td></tr> -"; - $body .= "<tr><td>" - . textfield( - -name => 'name', - -size => 20 - ) - . "</td><td>" - . popup_menu( - -name => 'pricelist', - -values => \@pricelists - ) - . "</td><td>" - . textfield( - -name => 'pattern', - -size => 30 - ) - . "</td><td>" - . textfield( - -name => 'includedseconds', - -size => 6 - ) - . "</td><td>" - . submit( -name => 'action', -value => gettext("Insert...") ) - . "</td></tr> -</table> -"; - return $body; + if ( $params->{logintype} == 2 ) { + $sql_select = + "SELECT * FROM packages WHERE status < 2 AND pricelist IN(SELECT name FROM pricelists WHERE reseller IS NULL) ORDER BY id"; + $sql_count = + "SELECT * FROM packages WHERE status < 2 AND pricelist IN(SELECT name FROM pricelists WHERE reseller IS NULL) ORDER BY id"; } - if ( $params->{action} eq gettext("Information...") ) { - $body = start_form; - $body .= - "<table class=\"default\">" - . "<tr><td>" - . submit( -name => 'action', -value => gettext("Add...") ) - . "</td></tr> -" - . "<tr class=\"header\"><td>" - . hidden( -name => "mode", -value => gettext("Packages") ) - . gettext("Counter Name") - . "</td><td>" - . gettext("Pricelist") - . "</td><td>" - . gettext("Pattern") - . "</td><td>" - . gettext("Included Seconds") - . "</td><td>" - . gettext("Action") - . "</td></tr> -"; - $tmp = "SELECT * FROM packages WHERE status < 2"; - $sql = $astpp_db->prepare($tmp); - $sql->execute - || return gettext("Something is wrong with the ASTPP database!") - . "\n"; - my $results = $sql->rows; - my $pagesrequired = ceil( $results / $results_per_page ); - print gettext("Pages Required:") . " $pagesrequired\n" - if ( $config->{debug} == 1 ); - $sql->finish; - $sql = - $astpp_db->prepare( -"SELECT * FROM packages WHERE status < 2 ORDER BY name limit $params->{limit} , $results_per_page" - ); - $sql->execute - || return gettext("Something is wrong with the ASTPP database!") - . "\n"; - $count = 0; - - while ( my $packageinfo = $sql->fetchrow_hashref ) { - if ( $count % 2 == 0 ) { - $body .= "<tr class=\"rowtwo\">"; - } - else { - $body .= "<tr class=\"rowone\">"; - } - $count++; - $body .= - "<td>$packageinfo->{name}" - . "</td><td>$packageinfo->{pricelist}" - . "</td><td>$packageinfo->{pattern}" - . "</td><td>$packageinfo->{includedseconds}" - . "</td><td><a href=\"astpp-admin.cgi?mode=" - . gettext("Packages") - . "&action=" - . gettext("Edit...") . "&id=" - . $packageinfo->{id} . "\">" - . gettext("Edit...") . "</a>" - . " <a href=\"astpp-admin.cgi?mode=" - . gettext("Packages") - . "&action=" - . gettext("Deactivate...") . "&id=" - . $packageinfo->{id} . "\">" - . gettext("Deactivate...") . "</a>" - . "</td></tr>"; - } - $body .= "</table>"; - for ( my $i = 0 ; $i <= $pagesrequired - 1 ; $i++ ) { - if ( $i == 0 ) { - if ( $params->{limit} != 0 ) { - $body .= - "<a href=\"astpp-admin.cgi?mode=" - . gettext("Packages") - . "&limit=0\">"; - $body .= $i + 1; - $body .= "</a>"; - } - else { - $body .= $i + 1; - } - } - if ( $i > 0 ) { - if ( $params->{limit} != ( $i * $results_per_page ) ) { - $body .= - "<a href=\"astpp-admin.cgi?mode=" - . gettext("Packages") - . "&limit="; - $body .= ( $i * $results_per_page ); - $body .= "\">\n"; - $body .= $i + 1 . "</a>"; - } - else { - $pageno = $i + 1; - $body .= " |"; - } - } - } - $body .= ""; - $body .= - gettext("Page") . " $pageno " . gettext("of") . " $pagesrequired"; + elsif ( $params->{logintype} == 1 ) { + $sql_select = + "SELECT * FROM packages WHERE status < 2 AND pricelist IN(SELECT name FROM pricelists WHERE reseller = " + . $astpp_db->quote( $params->{username} ) . ") ORDER BY id"; + $sql_count = + "SELECT * FROM packages WHERE status < 2 AND pricelist IN(SELECT name FROM pricelists WHERE reseller = " + . $astpp_db->quote( $params->{username} ) . ") ORDER BY id"; } + my ($sql, $pagination) = + $ASTPP->pagination( sql_select => $sql_select, + sql_count => $sql_count, + results_per_page=> $config->{results_per_page}, + ve => $params->{ve}, + te => $params->{ee}, + + ); + my $results = $astpp_db->prepare($sql); + $results->execute; + while ( my $packageinfo = $results->fetchrow_hashref ) { + print STDERR "PACKAGE: " . $packgeinfo->{id} if $config->{debug} == 1; + my (%row); + $row{id} = $packageinfo->{id}; + $row{name} = $packageinfo->{name}; + $row{pattern} = $packageinfo->{pattern}; + $row{pricelist} = $packageinfo->{pricelist}; + $row{includedseconds} = $packageinfo->{includedseconds}; + push( @package_list, \%row ); + } + $template->param( package_list => \@package_list ); + $template->param( pagination => $pagination ); + $template->param( status => $status ); + return $template->output; return $body; } @@ -9418,12 +9070,7 @@ sub build_calc_charge() { my ( $status, $body, $cost, $length, $increment ); return gettext("Database is NOT configured!") . "\n" unless $astpp_db; - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = ( $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); if ( $params->{action} eq gettext("Price Call...") ) { my $branddata = &get_pricelist( $astpp_db, $params->{pricelist} ); my $numdata = &get_route( $astpp_db, $config, $params->{phonenumber}, @@ -9552,6 +9199,9 @@ } @modes = sort @modes; push @currency, $config->{currency}; + $ASTPP->set_astpp_db($astpp_db); + $ASTPP->set_cdr_db($cdr_db); +# $ASTPP->set_config(\%config); } ############### Integration with Realtime starts here ####################### @@ -10898,12 +10548,7 @@ -values => \@language, -default => $config->{default_language} ); - if ( $params->{logintype} == 1 || $params->{logintype} == 5 ) { - @pricelists = &list_pricelists( $astpp_db, $params->{username} ); - } - else { - @pricelists = &list_pricelists($astpp_db); - } + @pricelists = $ASTPP->list_pricelists(reseller => $params->{logged_in_reseller}); $pricelists = popup_menu( -name => "pricelist", -values => \@pricelists @@ -11546,6 +11191,7 @@ '1' => gettext("Reseller"), '5' => gettext("CallShop") ); + $params->{logged_in_reseller} = $params->{username}; } elsif ( $params->{logintype} == 2 ) { print STDERR "ASTPP ADMINISTRATOR LOGIN\n" if $config->{debug} == 1; @@ -11578,6 +11224,7 @@ '4' => gettext("Customer Service"), '5' => gettext("CallShop") ); + $params->{logged_in_reseller} = ""; } elsif ( $params->{logintype} == 3 ) { # Vendor Login - Vendors are only allowed to look at stuff that pertains to them. Modified: trunk/astpp-callingcards.agi =================================================================== --- trunk/astpp-callingcards.agi 2007-11-03 03:07:15 UTC (rev 2149) +++ trunk/astpp-callingcards.agi 2007-11-03 21:35:25 UTC (rev 2150) @@ -58,6 +58,7 @@ } $cdr_db = &cdr_connect_db( $config, @output ); $ASTPP->set_cdr_db($cdr_db); + $ASTPP->set_config($config); $sound = &define_sounds($astpp_db); } Modified: trunk/astpp-common.pl =================================================================== --- trunk/astpp-common.pl 2007-11-03 03:07:15 UTC (rev 2149) +++ trunk/astpp-common.pl 2007-11-03 21:35:25 UTC (rev 2150) @@ -3121,7 +3121,7 @@ . $astpp_db->quote($number) . " RLIKE pattern AND pricelist = " . $astpp_db->quote( $carddata->{pricelist} ) - . " ORDER BY LENGTH(pattern) DESC" ); + . " ORDER BY LENGTH(pattern) DESC LIMIT 1" ); $sql->execute; $record = $sql->fetchrow_hashref; $sql->finish; @@ -3663,7 +3663,7 @@ sub rating() { # This routine recieves a specific cdr and takes care of rating it and of marking it as rated. It bills resellers as appropriate. my ( $astpp_db, $cdr_db, $config, $cdrinfo, $carddata, @output ) = @_; my ( $increment, $numdata, $package, $notes, $status ); - print STDERR "----------------------\n"; + print STDERR "----------------------------------------------------------------\n"; print STDERR "uniqueid: $cdrinfo->{uniqueid}, cardno: $carddata->{number}, phoneno: $cdrinfo->{dst}, Userfield: $cdrinfo->{userfield}\n"; print STDERR @@ -3678,7 +3678,7 @@ if $config->{astcdr} == 1; print STDERR "ERROR - ERROR - ERROR - ERROR - ERROR \n"; print STDERR "NO MATCHING PATTERN\n"; - print STDERR "----------------------\n"; + print STDERR "----------------------------------------------------------------\n"; } else { print STDERR "FOUND A MATCHING PATTERN.\n"; @@ -3687,21 +3687,22 @@ "pricelistData: $branddata->{name} $branddata->{markup} $branddata->{inc} $branddata->{status}\n"; my $package = &get_package( $astpp_db, $carddata, $cdrinfo->{dst} ); - if ($package->{name}) { + if ($package->{id}) { my $counter = - &get_counter( $astpp_db, $package->{name}, + &get_counter( $astpp_db, $package->{id}, $carddata->{number} ); my $difference; if ( !$counter->{id}) { my $tmp = "INSERT INTO counters (package,account) VALUES (" - . $astpp_db->quote( $package->{name} ) . ", " + . $astpp_db->quote( $package->{id} ) . ", " . $astpp_db->quote( $carddata->{number} ) . ")"; - print STDERR $tmp . "/n" if $config->{debug} == 1; + print STDERR "/n" . $tmp . "/n" if $config->{debug} == 1; $astpp_db->do($tmp); $counter = - &get_counter( $astpp_db, $package->{name}, + &get_counter( $astpp_db, $package->{id}, $carddata->{number} ); + print STDERR "JUST CREATED COUNTER: $counter->{id}\n" if $config->{debug} == 1; } if ( $package->{includedseconds} > $counter->{seconds}) { my $availableseconds = $package->{includedseconds} - $counter->{seconds}; @@ -3859,10 +3860,10 @@ } elsif ( $cdrinfo->{accountcode} ) { $status = &rating( $astpp_db, $cdr_db, $config, $cdrinfo, $carddata); - $cdrinfo = $savedcdrinfo; if ( $status == 1 ) { my $previous_account = $carddata->{number}; while ( $carddata->{reseller} ne "" ) { +# my $cdrinfo = &get_cdr( $cdr_db, $uniqueid ); # my $reseller = $carddata->{reseller}; print STDERR "Charge $uniqueid to $carddata->{reseller}" if $config->{debug} == 1; $carddata = &get_account( $astpp_db, $carddata->{reseller} ); Modified: trunk/modules/ASTPP/lib/ASTPP.pm =================================================================== --- trunk/modules/ASTPP/lib/ASTPP.pm 2007-11-03 03:07:15 UTC (rev 2149) +++ trunk/modules/ASTPP/lib/ASTPP.pm 2007-11-03 21:35:25 UTC (rev 2150) @@ -33,22 +33,49 @@ { my ($class,%arg) = @_; bless { - _astpp_db => $_[1], - _verbosity_level => $arg{verbosity_level} || 1, - _asterisk_agi => $_[3], - _cdr_db => $_[4], - _verbosity_item_level => $arg{verbosity_item_level} || 1, - _pagination_script => $arg{pagination_script} || "astpp-admin.cgi", + _astpp_db => $_[1], + _verbosity => $arg{verbosity_level} || 1, + _asterisk_agi => $_[3], + _cdr_db => $_[4], + _verbosity_item_level => $arg{verbosity_item_level} || 1, + _script => $arg{script} || "astpp-admin.cgi", + _config => $_[7], }, $class; } +sub set_verbosity_level { $_[0]->{_verbosity_level} } #Sets the verbosity level. sub set_verbosity { $_[0]->{_verbosity_level} } #Sets the verbosity level. -sub set_asterisk_agi { $_[0]->{_asterisk_agi} } #Sets the verbosity level. -sub set_astpp_db { $_[0]->{_astpp_db} } #Sets the verbosity level. -sub set_cdr_db {$_[0]->{_cdr_db} } #Sets the verbosity level. -sub set_pagination_script {$_[00]->{_pagination_script} } +sub set_astpp_db +{ + my ($self, $astpp_db) = @_; + $self->{_astpp_db} = $astpp_db if $astpp_db; +} +sub set_cdr_db +{ + my ($self, $cdr_db) = @_; + $self->{_cdr_db} = $cdr_db if $cdr_db; +} + +sub set_asterisk_agi +{ + my ($self, $asterisk_agi) = @_; + $self->{_asterisk_agi} = $asterisk_agi if $asterisk_agi; +} + +sub set_pagination_script #Set the location of the script we are working in. +{ + my ($self, $script) = @_; + $self->{_script} = $script if $script; +} + +#sub set_config +#{ +# my ($self, %config_hash) = @_; +# $self->{_config} = %config_hash if %config_hash; +#} + sub debug #Prints debugging if appropriate # { @@ -64,43 +91,110 @@ sub pagination #Returns the pagination html code to assist with navigation. { -my ($self, %arg) = @_; + my ($self, %arg) = @_; # We are using Data::Paginate for the real tough stuff. # Therefore we need to pass the Data::Paginate stuff # as well as the total number of pages. - # total_entries = Total number of pages. - # pagination_script = Script name. + # # mode = What mode do we want. - # pgr = Pass along Data::Paginate - # ve = - # navi_html = - # last_page = + # sql = SQL to select what we want without the limit commands. + # + # Check to see if there is a parameter called "results_per_page" set. If it's set it overrides our defaults. + if (!$arg{results_per_page} || $arg{results_per_page} > 1 ) { + $arg{results_per_page} = 25; + } + ## START PAGINATION CODE + # set total_entries *once* then pass it around + # in the object's links from then on for efficiency: + my ($record,$sql); + my $verify = $arg{ve} || ''; + my $total_entries = int( $arg{te} ); + my $te_match = $total_entries + ? Digest::MD5::md5_hex("unique_cypher-$total_entries-$arg{sql_check}") : ''; + if ( !$total_entries || $verify ne $te_match ) { + # its not ok so re-fetch + $sql = $self->{_astpp_db}->prepare($arg{sql_count}); + $sql->execute; + $record = $sql->fetchrow_hashref; + $total_entries = $record->{"COUNT(*)"}; + $sql->finish; + $te_match = Digest::MD5::md5_hex("unique_cypher-$total_entries-$arg{sql_check}"); + } + + #if ($te_match <= 0) { $te_match = 0; } + if ( $total_entries <= 0 ) { $total_entries = 1; } + + # otherwise its all ok so use it + my $pgr = Data::Paginate->new( + { + 'start_array_index_at_zero' => 1, + 'total_entries' => $total_entries, + 'entries_per_page' => $arg{results_per_page}, + 'total_entries_verify_param_value' => $te_match + } + ); + + # only SELECT current page's records: + if ( $total_entries > $pgr->get_entries_on_this_page() ) { + $sql = + $arg{sql_select} . " LIMIT " + . ( $pgr->get_first() - 1 ) . ", " + . $pgr->get_entries_on_this_page(); + } + else { + $sql = $arg{sql_select}; + } + # First we decide if we have multiple pages... + if ($arg{total_entries} > 1) { my $html; $html = - "<a href=\"" . $self->{_pagination_script} . "?mode=" + "<a href=\"" . $self->{_script} . "?mode=" . $arg{mode} . "&ve=" . $arg{ve} . "&te=" - . $arg{total_entries} + . $total_entries . "&pg=1\">" - . gettext("First Page") + . "First Page" . "</a> | " - . scalar $arg{navi_html} - . "<a href=\"" . $self->{_pagination_script} . "?mode=" + . scalar $pgr->get_navi_html() + . "<a href=\"" . $self->{_script} . "?mode=" . $arg{mode} . "&ve=" . $arg{ve} . "&te=" . $arg{total_entries} . "&pg=" . $arg{last_page} . "\">" - . gettext("Last Page") . "</a>"; - return $html; + . "Last Page" . "</a>"; + return ($sql,$html); } else { - return gettext("Page 1 of 1"); + return ($sql,"Page 1 of 1"); } } +sub list_pricelists +{ + my ($self, %arg) = @_; # Return a list of all pricelists either for the appropriate reseller or without reseller. + my ( $sql, @pricelistlist, $row, $tmp ); + if ( !$arg{reseller} || $arg{reseller} eq "") { + $tmp = + "SELECT name FROM pricelists WHERE status < 2 AND reseller IS NULL ORDER BY name"; + } + else { + $tmp = + "SELECT name FROM pricelists WHERE status < 2 AND reseller = " + . $self->{_astpp_db}->quote($arg{reseller}) + . " ORDER BY name"; + } + $sql = $self->{_astpp_db}->prepare($tmp); + $sql->execute; + while ( $row = $sql->fetchrow_hashref ) { + push @pricelistlist, $row->{name}; + } + $sql->finish; + return @pricelistlist; +} + # Preloaded methods go here. 1; Modified: trunk/templates/account-info.tpl =================================================================== --- trunk/templates/account-info.tpl 2007-11-03 03:07:15 UTC (rev 2149) +++ trunk/templates/account-info.tpl 2007-11-03 21:35:25 UTC (rev 2150) @@ -183,12 +183,13 @@ <td>Date & Time</td> <td>Caller*ID</td> <td>Called Number</td> - <td>Trunk</td> <td>Disposition</td> <td>Billable Seconds</td> <td>Charge</td> <td>Credit</td> <td>Notes</td> + <td>Cost</td> + <td>Profit</td> </tr> <TMPL_LOOP NAME="account_cdr_list"> <TR> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |