Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use DBI;
- use DBD::Pg;
- use Config::General;
- use Getopt::Std;
- use POSIX qw( strftime );
- use Data::Dumper;
- use Spreadsheet::WriteExcel;
- use Email::MIME;
- use Email::Sender::Simple qw(sendmail);
- use Email::Sender::Transport::SMTP;
- use Email::Sender::Transport::SMTP::TLS;
- use Log::Minimal;
- use Try::Tiny;
- use Config::IniFiles;
- use File::Basename;
- my $start_log = 0;
- my $start_pid = 0;
- my $start_cmd = "$^X $0 @ARGV";
- my %opts;
- &init();
- #my $conf = new Config::General( $opts{c} );
- #my %config = $conf->getall;
- my %config;
- tie %config, 'Config::IniFiles', ( -file => $opts{c}, -handle_trailing_comment => 1 );
- for my $k1( keys %config ){
- for my $k2( keys %{$config{$k1}} ){
- if( $config{$k1}{$k2} =~ /(.*)\.\$(.*)/ ){
- $config{$k1}{$k2} = $config{$1}{$2};
- }
- }
- }
- my $log_dir = dirname(__FILE__) . '/storage/';
- if( ! -d $log_dir ){
- if( ! mkdir $log_dir,0777 ){
- print "create $log_dir failed:$!\n";
- exit;
- }
- }
- my $class4_log_file = $log_dir."/class4.log";
- my $open_log = 1;
- if( ! open CLASS4_LOG, ">>$class4_log_file" ){
- print "open $class4_log_file failed:$!\n";
- $open_log = 0;
- }
- $ENV{LM_DEBUG} = 1;
- local $Log::Minimal::AUTODUMP = 1;
- local $Log::Minimal::LOG_LEVEL = $config{script_log}{ log_level };
- local $Log::Minimal::PRINT = sub {
- my ( $time, $type, $message, $trace, $raw_message ) = @_;
- print "$time [$type] $trace $message\n";
- print CLASS4_LOG "$time [$type] $trace $message\n" if( $open_log );
- };
- infof( "start script : $start_cmd" );
- my $db_name = $config{db}{ dbname };
- my $db_host = $config{db}{ hostaddr };
- my $db_port = $config{db}{ port };
- my $db_username = $config{db}{ user };
- my $db_password = $config{db}{ password };
- my $dbh = DBI->connect( "dbi:Pg:dbname=$db_name; host=$db_host; port=$db_port",
- $db_username,
- $db_password,
- { AutoCommit => 1, pg_server_prepare => 1 } );
- if( ! $dbh ){
- critf( $DBI::errstr );
- &do_exit();
- };
- # 记录脚本的开始时间
- my $sql = "INSERT INTO scheduler_log (script_name, start_time) VALUES ('Dialy Summary Report', current_timestamp(0))";
- my $sth = $dbh->prepare( $sql );
- $sth->execute();
- my $scheduler_log_id = $dbh->last_insert_id(undef,undef, "scheduler_log", "id");
- my $file_dir = $log_dir."/summary_report";
- if( ! -d $file_dir ){
- if( ! mkdir $file_dir,0777 ){
- critf( "create $file_dir failed:$!" );
- &do_exit();
- }
- }
- my $run_pid_file = $log_dir."/dnl_summary_report.pid";
- &start_log();
- my %mail_template_data;
- my $mail_from_ref = $dbh->selectrow_hashref("SELECT auto_summary_from as from from mail_tmplate");
- my $mail_hr;
- if( $mail_from_ref && $mail_from_ref->{ from } && $mail_from_ref->{ from } ne 'Default' && $mail_from_ref->{ from } ne 'default' ){
- $mail_hr = $dbh->selectrow_hashref( "SELECT loginemail,smtp_host as smtphost, smtp_port as smtpport, username as emailusername, password as emailpassword, email as fromemail, secure as smtp_secure FROM mail_sender WHERE id = $mail_from_ref->{ from }" );
- }else{
- $mail_hr = $dbh->selectrow_hashref( "SELECT * from system_parameter" );
- }
- #my $mail_hr = $dbh->selectrow_hashref( "SELECT * from system_parameter" );
- $mail_template_data{ from } = $mail_hr->{ fromemail };
- $mail_template_data{ fake_from} = $mail_hr->{ emailname };
- $mail_template_data{ server } = $mail_hr->{ smtphost };
- $mail_template_data{ port } = $mail_hr->{ smtpport };
- $mail_template_data{ username } = $mail_hr->{ emailusername };
- $mail_template_data{ password } = $mail_hr->{ emailpassword };
- $mail_template_data{ auth } = ($mail_hr->{ loginemail } eq "true") ? 1 : 0;
- #$mail_template_data{ tls } = $config{ sendmail_tls_require };
- $mail_template_data{ tls } = $mail_hr->{ smtp_secure };
- $mail_template_data{ ssl } = ($mail_hr->{ smtp_secure } == 2) ? 1 : 0;
- my $mail_template_hr = $dbh->selectrow_hashref( "SELECT * from mail_tmplate" );
- $mail_template_data{ cc } = $mail_template_hr->{ auto_summary_cc };
- if( $opts{a} ){
- #&start_pid( $run_pid_file );
- my $report_client_sql = "
- SELECT
- 0 as period_type,
- c4_client_balance.balance,
- name,
- client.client_id,
- last_autoreport_time,
- auto_send_zone,
- case auto_summary_group_by when 0 then 'country' when 1 then 'code_name' when 2 then 'code' else 'country' end as auto_summary_group_by,
- to_char(current_date-1,'YYYY-MM-DD 00:00:00')||COALESCE(auto_send_zone,'+00') as start_time,
- to_char(current_date-1,'YYYY-MM-DD 23:59:59')||COALESCE(auto_send_zone,'+00') as end_time,
- to_char(current_date,'YYYYMMDD') as current_table_suffix
- FROM
- client
- left join
- c4_client_balance
- on client.client_id::text=c4_client_balance.client_id
- WHERE
- is_auto_summary = true and
- status=true and
- (auto_summary_period is null or auto_summary_period = 24) and
- (last_autoreport_time is null or last_autoreport_time < current_date::timestamp with time zone at time zone auto_send_zone::interval) and
- current_timestamp(0) at time zone auto_send_zone::interval > current_date::timestamp with time zone and
- (auto_summary_hour is null or EXTRACT(HOUR FROM now() at time zone auto_send_zone::interval)>=auto_summary_hour)
- union
- SELECT
- 0 as period_type,
- c4_client_balance.balance,
- name,
- client.client_id,
- last_autoreport_time,
- auto_send_zone,
- case auto_summary_group_by when 0 then 'country' when 1 then 'code_name' when 2 then 'code' else 'country' end as auto_summary_group_by,
- to_char(current_timestamp(0)-auto_summary_period*interval '1 hour','YYYY-MM-DD HH24:00:00') as start_time,
- to_char(current_timestamp(0)-interval '1 hour','YYYY-MM-DD HH24:59:59') as end_time,
- to_char(current_date,'YYYYMMDD') as current_table_suffix
- FROM
- client
- left join
- c4_client_balance
- on client.client_id::text=c4_client_balance.client_id
- WHERE
- is_auto_summary = true and
- status=true and
- (auto_summary_period is not null and auto_summary_period != 24) and
- EXTRACT(HOUR FROM now() at time zone auto_send_zone::interval)::integer%auto_summary_period=0
- ";
- print $report_client_sql;
- my $report_client_hr = $dbh->selectall_hashref( $report_client_sql, "client_id" );
- for my $k1( keys %$report_client_hr ){
- &mod_data( "update client set last_autoreport_time=current_timestamp(0) where client_id=?","$k1" );
- }
- my $has_report_data = 0;
- for my $k1( keys %$report_client_hr ){
- my $timeout = 0;
- my $current_table_suffix = $report_client_hr->{ $k1 }->{ current_table_suffix };
- if ($has_report_data != 1){
- while( 1 ){
- my $report_time_hr = $dbh->selectrow_hashref( "SELECT report_time from cdr_report_detail$current_table_suffix where report_time >= '$report_client_hr->{ $k1 }->{ end_time }' limit 1" );
- if ( ! $report_time_hr ){
- if ($timeout < 10){
- $timeout += 1;
- debugf( "after $report_client_hr->{ $k1 }->{ end_time } not found report,wait" );
- sleep(60);
- next;
- }else{
- debugf( "after $report_client_hr->{ $k1 }->{ end_time } not found report,exit" );
- exit;
- }
- }else{
- $has_report_data = 1;
- last;
- }
- }
- }
- my $report_file = $file_dir."/".$report_client_hr->{ $k1 }->{ name }."_summary_report_".substr($report_client_hr->{ $k1 }->{ start_time },0,10).".xls";
- if( &create_report( $report_client_hr->{ $k1 }->{ start_time },$report_client_hr->{ $k1 }->{ end_time },$k1,$report_client_hr->{ $k1 }->{ auto_summary_group_by },$report_file,$report_client_hr->{ $k1 }->{ balance } ) < 0 ){
- next;
- }
- my $send_mail_res = &send_mail( %mail_template_data );
- if( $send_mail_res ){
- warnf( "send mail failed : $send_mail_res" );
- }else{
- #&mod_data( "update client set last_autoreport_time=current_timestamp(0) where client_id=?","$k1" );
- }
- my $send_mail_log = [ $send_mail_res, strftime("%Y-%m-%d %H:%M:%S", gmtime), $k1, $mail_template_data{ to }, $report_file, 2, 0, $mail_template_data{ subject }, $mail_template_data{ content } ];
- &send_mail_log( $send_mail_log );
- }
- }else{
- my $client_hr = $dbh->selectrow_hashref( "select * from client where client_id=$opts{i}" );
- if( $client_hr ){
- my $report_file = $file_dir."/".$client_hr->{ name }."_summary_report_".substr($opts{s},0,10).".xls";
- #if( &create_report_cdr( $opts{s}, $opts{e}, $opts{i}, $opts{g}, $report_file, $opts{b} ) == 0 ){
- my $send_mail_res = &send_mail( %mail_template_data );
- if( $send_mail_res ){
- warnf( "send mail failed : $send_mail_res" );
- }
- my $send_mail_log = [ $send_mail_res, strftime("%Y-%m-%d %H:%M:%S", gmtime), $opts{i}, $mail_template_data{ to }, $report_file, 2, 0, $mail_template_data{ subject }, $mail_template_data{ content } ];
- &send_mail_log( $send_mail_log );
- #}
- }else{
- debugf( "client $opts{i} not exists" );
- }
- }
- # 记录脚本的结束时间
- my $sql = "update scheduler_log set end_time = current_timestamp(0) where id =?";
- my $sth = $dbh->prepare( $sql );
- $sth->execute($scheduler_log_id);
- infof( "finished script" );
- &do_exit();
- sub init()
- {
- my $opt_string = 'ahc:s:e:i:g:b:';
- getopts( "$opt_string", \%opts ) or usage();
- usage() if $opts{h};
- }
- sub usage()
- {
- print "perl class4_summary_report.pl -c class4.conf -a or -s start_time -e end_time -i client_id -g code/code_name/country -b end_time_balance\n";
- exit;
- }
- sub mod_data {
- my( $data_sql,$data_value ) = @_;
- my @value_array;
- if( ref( $data_value ) eq "ARRAY" ){
- @value_array = @$data_value;
- }else{
- @value_array = split( /,/, $data_value, -1 );
- }
- my $sth = $dbh->prepare( $data_sql );
- $sth->execute( @value_array );
- if( $sth->state ){
- critf( "update database error sql:%s,data:%s,error:%s", $data_sql, join(",",@value_array), $sth->errstr );
- return 1;
- }
- return 0;
- }
- sub start_log(){
- my @script_name = split(/\//,$0);
- &mod_data( "update scheduler set last_run = current_timestamp(0) where script_name=?", "@script_name[$#script_name]" );
- $start_log = 1;
- }
- sub start_pid(){
- my( $pid_file ) = @_;
- if( -e $pid_file ){
- my $pid = `head -1 $pid_file`;
- debugf( "last pid : $pid" );
- if( $pid !~ /^\s*$/ && -e "/proc/$pid" ){
- debugf( "pid $pid alreay running" );
- &do_exit();
- }
- }
- if( ! open PID_FILE, ">$pid_file" ){
- critf( "open pid file error:$!" );
- &do_exit();
- }
- print PID_FILE "$$";
- close PID_FILE;
- debugf( "new pid : $$" );
- $start_pid = 1;
- return 0;
- }
- sub end_pid(){
- my( $pid_file ) = @_;
- debugf( "process done" );
- if( ! open PID_FILE, ">$pid_file" ){
- critf( "close pid file error:$!" );
- }
- print PID_FILE "";
- close PID_FILE;
- return 0;
- }
- sub do_exit(){
- &start_log() if( $start_log );
- $dbh->disconnect if( $dbh );
- close CLASS4_LOG;
- &end_pid( $run_pid_file ) if( $start_pid );
- exit;
- }
- sub send_mail(){
- my ( %mail_data ) = @_;
- my $mail_from = $mail_data{ from };
- my $mail_fake_from = $mail_data{ fake_from};
- my $mail_server = $mail_data{ server };
- my $mail_port = $mail_data{ port };
- my $mail_auth = $mail_data{ auth };
- my $mail_username = $mail_data{ username };
- my $mail_password = $mail_data{ password };
- my $mail_tls_require = $mail_data{ tls };
- my $mail_to = $mail_data{ to };
- my $mail_subject = $mail_data{ subject };
- my $mail_content = $mail_data{ content };
- my $mail_attach_files = $mail_data{ files };
- my $mail_ssl = $mail_data{ ssl };
- my $mail_cc = $mail_data{ cc };
- my $attach_data;
- if( $mail_attach_files ){
- for my $k1( @$mail_attach_files ){
- if( ! open( MAIL_DATA, "<$k1" ) ){
- critf( "open $k1 failed,$!" );
- return "open $k1 failed,$!";
- }else{
- local $/;
- #my @file_data = <MAIL_DATA>;
- my @mail_attach_files = split( /\//, "$k1" );
- $attach_data->{ @mail_attach_files[$#mail_attach_files] } = <MAIL_DATA>;
- close MAIL_DATA;
- }
- }
- }
- my $email;
- if( $mail_attach_files ){
- my @parts;
- push @parts, Email::MIME->create(
- attributes => {
- content_type => "text/html",
- # charset => "us-ascii",
- charset => "utf-8",
- encoding => "base64",
- },
- body_str => $mail_content,
- );
- for my $k1( keys %{ $attach_data } ){
- push @parts, Email::MIME->create(
- attributes => {
- content_type => "application/octet-stream",
- encoding => "base64",
- filename => "$k1",
- },
- body => $attach_data->{ $k1 },
- );
- }
- $email = Email::MIME->create(
- header_str => [
- From => $mail_from,
- To => $mail_to,
- Subject => $mail_subject,
- Cc => $mail_cc,
- ],
- parts => [ @parts ],
- );
- }else{
- $email = Email::MIME->create(
- header_str => [
- From => $mail_from,
- To => $mail_to,
- Subject => $mail_subject,
- Cc => $mail_cc,
- ],
- body => $mail_content,
- attributes => {
- content_type => "text/html",
- # charset => "us-ascii",
- charset => "utf-8",
- encoding => "base64",
- },
- );
- }
- #print $email->as_string;
- my $send_res;
- if( $mail_auth == 0 ){
- try{
- sendmail(
- $email
- );
- }catch{
- $send_res = $_;
- }
- }elsif( $mail_tls_require == 1 ){
- try{
- sendmail(
- $email,
- {
- transport => Email::Sender::Transport::SMTP::TLS->new(
- host => $mail_server,
- port => $mail_port,
- username => $mail_username,
- password => $mail_password,
- ),
- }
- );
- }catch{
- $send_res = $_;
- }
- }else{
- try{
- sendmail(
- $email,
- {
- transport => Email::Sender::Transport::SMTP->new(
- host => $mail_server,
- port => $mail_port,
- sasl_username => $mail_username,
- sasl_password => $mail_password,
- ssl => $mail_data{ ssl },
- ),
- }
- );
- }catch{
- $send_res = $_;
- }
- }
- #print "send result : $send_res\n";
- $send_res =~ s#Trace begun[\s\S]*##g;
- return $send_res;
- }
- sub create_report(){
- my( $report_start_time,$report_end_time,$client_id,$group_type,$file_name,$client_balance ) = @_;
- my $report_sql;
- $report_sql = "select ingress_client_id,egress_client_id,ingress_prefix,ingress_code,not_zero_calls,ingress_total_calls,ingress_bill_time,ingress_call_cost,egress_total_calls,egress_bill_time,egress_call_cost,duration from cdr_report_detail where report_time between '$report_start_time' and '$report_end_time' and (ingress_client_id=$client_id or egress_client_id=$client_id)" if( $group_type eq "code" );
- $report_sql = "select ingress_client_id,egress_client_id,ingress_prefix,ingress_code_name,not_zero_calls,ingress_total_calls,ingress_bill_time,ingress_call_cost,egress_total_calls,egress_bill_time,egress_call_cost,duration from cdr_report_detail where report_time between '$report_start_time' and '$report_end_time' and (ingress_client_id=$client_id or egress_client_id=$client_id)" if( $group_type eq "code_name" );
- $report_sql = "select ingress_client_id,egress_client_id,ingress_prefix,ingress_country,not_zero_calls,ingress_total_calls,ingress_bill_time,ingress_call_cost,egress_total_calls,egress_bill_time,egress_call_cost,duration from cdr_report_detail where report_time between '$report_start_time' and '$report_end_time' and (ingress_client_id=$client_id or egress_client_id=$client_id)" if( $group_type eq "country" );
- my $client_balance_hr = $dbh->selectrow_hashref( "select * from c4_client_balance where client_id='$client_id'" );
- $client_balance = $client_balance_hr->{ balance } if( ! defined $client_balance );
- #my $client_balance = &report_balance( $client_id, $report_start_time, $report_end_time );
- my $buy_call_time = 0;
- my $sell_call_time = 0;
- my $buy_calls = 0;
- my $buy_not_zero_calls = 0;
- my $buy_mins = 0;
- my $buy_amount = 0;
- my $sell_calls = 0;
- my $sell_not_zero_calls = 0;
- my $sell_mins = 0;
- my $sell_amount = 0;
- my $buy_data;
- my $sell_data;
- my $report_res = $dbh->selectall_arrayref( $report_sql );
- for my $k1( @$report_res ){
- #print "@$k1\n";
- if( $k1->[0] == $client_id ){
- ##buy data
- $buy_data->{ $k1->[2] }->{ $k1->[3] }->{ total_calls } += $k1->[5];
- $buy_data->{ $k1->[2] }->{ $k1->[3] }->{ not_zero_calls } += $k1->[4];
- $buy_data->{ $k1->[2] }->{ $k1->[3] }->{ bill_time } += $k1->[6];
- $buy_data->{ $k1->[2] }->{ $k1->[3] }->{ cost } += $k1->[7];
- $buy_data->{ $k1->[2] }->{ $k1->[3] }->{ call_time } += $k1->[11];
- $buy_calls += $k1->[5];
- $buy_not_zero_calls += $k1->[4];
- $buy_mins += $k1->[6];
- $buy_amount += $k1->[7];
- $buy_call_time += $k1->[11];
- }
- if( $k1->[1] == $client_id ){
- ##sell data
- $sell_data->{ $k1->[3] }->{ total_calls } += $k1->[8];
- $sell_data->{ $k1->[3] }->{ not_zero_calls } += $k1->[4];
- $sell_data->{ $k1->[3] }->{ bill_time } += $k1->[9];
- $sell_data->{ $k1->[3] }->{ cost } += $k1->[10];
- $sell_data->{ $k1->[3] }->{ call_time } += $k1->[11];
- $sell_calls += $k1->[8];
- $sell_not_zero_calls += $k1->[4];
- $sell_mins += $k1->[9];
- $sell_amount += $k1->[10];
- $sell_call_time += $k1->[11];
- }
- }
- $buy_not_zero_calls += 0;
- $sell_not_zero_calls += 0;
- if( ($buy_not_zero_calls + $sell_not_zero_calls) == 0 ){
- debugf( "no cdr found,exit" );
- return -1;
- }
- ###excel
- my $alert_file_hr;
- push @{ $alert_file_hr->{ buy } }, ["Tech Prefix",$group_type,"Total Call","Success Call","Bill Min","Avg Rate","Cost","Call Min"];
- #print "BUY DATA : Tech Prefix,$group_type,Total Call,Success Call,Bill Min,Avg Rate,Cost,Call Min\n";
- for my $k1( keys %$buy_data ){
- for my $k2( keys %{ $buy_data->{ $k1 } } ){
- $buy_data->{ $k1 }->{ $k2 }->{ bill_time } = $buy_data->{ $k1 }->{ $k2 }->{ bill_time }/60;
- $buy_data->{ $k1 }->{ $k2 }->{ call_time } = $buy_data->{ $k1 }->{ $k2 }->{ call_time }/60;
- my $avg_rate = $buy_data->{ $k1 }->{ $k2 }->{ bill_time } == 0 ?0 : $buy_data->{ $k1 }->{ $k2 }->{ cost }/$buy_data->{ $k1 }->{ $k2 }->{ bill_time };
- #print "$k1,$k2,$buy_data->{ $k1 }->{ $k2 }->{ total_calls },$buy_data->{ $k1 }->{ $k2 }->{ not_zero_calls },$buy_data->{ $k1 }->{ $k2 }->{ bill_time },$avg_rate,$buy_data->{ $k1 }->{ $k2 }->{ cost },$buy_data->{ $k1 }->{ $k2 }->{ call_time }\n";
- push @{ $alert_file_hr->{ buy } }, [$k1,$k2,$buy_data->{ $k1 }->{ $k2 }->{ total_calls },$buy_data->{ $k1 }->{ $k2 }->{ not_zero_calls },$buy_data->{ $k1 }->{ $k2 }->{ bill_time },$avg_rate,$buy_data->{ $k1 }->{ $k2 }->{ cost },$buy_data->{ $k1 }->{ $k2 }->{ call_time }];
- }
- }
- push @{ $alert_file_hr->{ sell } }, [$group_type,"Total Call","Success Call","Bill Min","Avg Rate","Cost","Call Min"];
- #print "SELL DATA : $group_type,Total Call,Success Call,Bill Min,Avg Rate,Cost,Call min\n";
- for my $k1( keys %$sell_data ){
- $sell_data->{ $k1 }->{ bill_time } = $sell_data->{ $k1 }->{ bill_time }/60;
- $sell_data->{ $k1 }->{ call_time } = $sell_data->{ $k1 }->{ call_time }/60;
- my $avg_rate = $sell_data->{ $k1 }->{ bill_time } == 0 ?0 : $sell_data->{ $k1 }->{ cost }/$sell_data->{ $k1 }->{ bill_time };
- #print "$k1,$sell_data->{ $k1 }->{ total_calls },$sell_data->{ $k1 }->{ not_zero_calls },$sell_data->{ $k1 }->{ bill_time },$avg_rate,$sell_data->{ $k1 }->{ cost },$sell_data->{ $k1 }->{ call_time }\n";
- push @{ $alert_file_hr->{ sell } }, [$k1,$sell_data->{ $k1 }->{ total_calls },$sell_data->{ $k1 }->{ not_zero_calls },$sell_data->{ $k1 }->{ bill_time },$avg_rate,$sell_data->{ $k1 }->{ cost },$sell_data->{ $k1 }->{ call_time }];
- }
- &write_excel( $file_name, $alert_file_hr );
- $mail_template_data{ subject } = $mail_template_hr->{ auto_summary_subject };
- $mail_template_data{ content } = $mail_template_hr->{ auto_summary_content };
- my $client_info = $dbh->selectrow_hashref( "SELECT * from client where client_id=$client_id" );
- if( $client_info->{ auto_daily_balance_recipient } == 1 ){
- $mail_template_data{ to } = $mail_hr->{ system_admin_email };
- }elsif( $client_info->{ auto_daily_balance_recipient } == 0 ){
- $mail_template_data{ to } = $client_info->{ email };
- }elsif( $client_info->{ auto_daily_balance_recipient } == 2 ){
- $mail_template_data{ to } = "$mail_hr->{ system_admin_email },$client_info->{ email }";
- }
- $mail_template_data{ files } = ["$file_name"];
- $buy_mins = $buy_mins/60;
- $sell_mins = $sell_mins/60;
- $buy_call_time = $buy_call_time/60;
- $sell_call_time = $sell_call_time/60;
- $buy_mins = sprintf( "%.3f", $buy_mins );
- $buy_amount = sprintf( "%.3f", $buy_amount );
- $client_balance = sprintf( "%.3f", $client_balance );
- $sell_mins = sprintf( "%.3f", $sell_mins );
- $sell_amount = sprintf( "%.3f", $sell_amount );
- $buy_call_time = sprintf( "%.3f", $buy_call_time );
- $sell_call_time = sprintf( "%.3f", $sell_call_time );
- $mail_template_data{ subject } =~ s/{client_name}/$client_info->{ name }/g;
- $mail_template_data{ subject } =~ s/{client_name}/$client_info->{ name }/g;
- $mail_template_data{ subject } =~ s/{begin_time}/$report_start_time/g;
- $mail_template_data{ subject } =~ s/{end_time}/$report_end_time/g;
- $mail_template_data{ subject } =~ s/{customer_gmt}//g;
- $mail_template_data{ subject } =~ s/{total_call_buy}/$buy_calls/g;
- $mail_template_data{ subject } =~ s/{total_not_zero_calls_buy}/$buy_not_zero_calls/g;
- $mail_template_data{ subject } =~ s/{total_success_call_buy}/$buy_calls/g;
- $mail_template_data{ subject } =~ s/{total_billed_min_buy}/$buy_mins/g;
- $mail_template_data{ subject } =~ s/{total_billed_amount_buy}/$buy_amount/g;
- $mail_template_data{ subject } =~ s/{credit_limit}//g;
- $mail_template_data{ subject } =~ s/{remaining_credit}//g;
- $mail_template_data{ subject } =~ s/{balance}/$client_balance/g;
- $mail_template_data{ subject } =~ s/{total_call_sell}/$sell_calls/g;
- $mail_template_data{ subject } =~ s/{total_not_zero_calls_sell}/$sell_not_zero_calls/g;
- $mail_template_data{ subject } =~ s/{total_success_call_sell}/$sell_calls/g;
- $mail_template_data{ subject } =~ s/{total_billed_min_sell}/$sell_mins/g;
- $mail_template_data{ subject } =~ s/{total_billed_amount_sell}/$sell_amount/g;
- $mail_template_data{ subject } =~ s/{buy_total_duration}/$buy_call_time/g;
- $mail_template_data{ subject } =~ s/{sell_total_duration}/$sell_call_time/g;
- $mail_template_data{ subject } =~ s/{switch_alias}/$mail_hr->{ switch_alias }/g;
- $mail_template_data{ content } =~ s/{client_name}/$client_info->{ name }/g;
- $mail_template_data{ content } =~ s/{begin_time}/$report_start_time/g;
- $mail_template_data{ content } =~ s/{end_time}/$report_end_time/g;
- $mail_template_data{ content } =~ s/{customer_gmt}//g;
- $mail_template_data{ content } =~ s/{total_call_buy}/$buy_calls/g;
- $mail_template_data{ content } =~ s/{total_not_zero_calls_buy}/$buy_not_zero_calls/g;
- $mail_template_data{ content } =~ s/{total_success_call_buy}/$buy_calls/g;
- $mail_template_data{ content } =~ s/{total_billed_min_buy}/$buy_mins/g;
- $mail_template_data{ content } =~ s/{total_billed_amount_buy}/$buy_amount/g;
- $mail_template_data{ content } =~ s/{credit_limit}//g;
- $mail_template_data{ content } =~ s/{remaining_credit}//g;
- $mail_template_data{ content } =~ s/{balance}/$client_balance/g;
- $mail_template_data{ content } =~ s/{total_call_sell}/$sell_calls/g;
- $mail_template_data{ content } =~ s/{total_not_zero_calls_sell}/$sell_not_zero_calls/g;
- $mail_template_data{ content } =~ s/{total_success_call_sell}/$sell_calls/g;
- $mail_template_data{ content } =~ s/{total_billed_min_sell}/$sell_mins/g;
- $mail_template_data{ content } =~ s/{total_billed_amount_sell}/$sell_amount/g;
- $mail_template_data{ content } =~ s/{buy_total_duration}/$buy_call_time/g;
- $mail_template_data{ content } =~ s/{sell_total_duration}/$sell_call_time/g;
- $mail_template_data{ content } =~ s/{switch_alias}/$mail_hr->{ switch_alias }/g;
- return 0;
- }
- sub report_balance(){
- my( $id_client, $balance_start_time, $balance_end_time ) = @_;
- #$balance_start_time = "2000-01-01" if( ! $balance_start_time );
- $balance_start_time = "2000-01-01";
- my $payment_hr;
- my $cdr_cost_hr;
- my $reset_balance = 0;
- my $reset_hr = $dbh->selectrow_hashref( "select * from client_payment where client_id = $id_client and payment_type = 14 order by receiving_time desc limit 1" );
- if( $reset_hr ){
- $reset_balance = $reset_hr->{ amount } + $reset_hr->{ egress_amount };
- $payment_hr = $dbh->selectrow_hashref( "select sum(case when payment_type=4 then amount else 0 end) as invoice_received,sum(case when payment_type=5 then amount else 0 end) as payment_received,sum(case when payment_type=8 then amount else 0 end) as credit_note_sent,sum(case when payment_type=12 then amount else 0 end) as debit_note_sent,sum(case when payment_type=3 then amount else 0 end) as invoice_sent,sum(case when payment_type=6 then amount else 0 end) as payment_sent,sum(case when payment_type=7 then amount else 0 end) as credit_note_received,sum(case when payment_type=11 then amount else 0 end) as debit_note_received from client_payment where client_id=$id_client and payment_time between '$balance_start_time' and '$balance_end_time' and payment_time >= '$reset_hr->{ payment_time }'" );
- $cdr_cost_hr = $dbh->selectrow_hashref( "select sum(case when ingress_client_id=$id_client then ingress_call_cost else 0 end) as total_ingress_cost, sum(case when egress_client_id=$id_client then egress_call_cost else 0 end) as total_egress_cost from cdr_report where report_time between '$balance_start_time' and '$balance_end_time' and report_time >= '$reset_hr->{ payment_time }' and (ingress_client_id=$id_client or egress_client_id=$id_client)" );
- }else{
- $payment_hr = $dbh->selectrow_hashref( "select sum(case when payment_type=4 then amount else 0 end) as invoice_received,sum(case when payment_type=5 then amount else 0 end) as payment_received,sum(case when payment_type=8 then amount else 0 end) as credit_note_sent,sum(case when payment_type=12 then amount else 0 end) as debit_note_sent,sum(case when payment_type=3 then amount else 0 end) as invoice_sent,sum(case when payment_type=6 then amount else 0 end) as payment_sent,sum(case when payment_type=7 then amount else 0 end) as credit_note_received,sum(case when payment_type=11 then amount else 0 end) as debit_note_received from client_payment where client_id=$id_client and payment_time between '$balance_start_time' and '$balance_end_time'" );
- $cdr_cost_hr = $dbh->selectrow_hashref( "select sum(case when ingress_client_id=$id_client then ingress_call_cost else 0 end) as total_ingress_cost, sum(case when egress_client_id=$id_client then egress_call_cost else 0 end) as total_egress_cost from cdr_report where report_time between '$balance_start_time' and '$balance_end_time' and (ingress_client_id=$id_client or egress_client_id=$id_client)" );
- }
- debugf( $reset_hr );
- debugf( $payment_hr );
- debugf( $cdr_cost_hr );
- return ($reset_balance
- +$payment_hr->{ invoice_received }
- +$payment_hr->{ payment_received }
- +$payment_hr->{ credit_note_sent }
- +$payment_hr->{ debit_note_sent }
- +$cdr_cost_hr->{ total_egress_cost }
- -$payment_hr->{ invoice_sent }
- -$payment_hr->{ payment_sent }
- -$payment_hr->{ credit_note_received }
- -$payment_hr->{ debit_note_received }
- -$cdr_cost_hr->{ total_ingress_cost }
- );
- }
- sub send_mail_log(){
- my ( $log_data ) = @_;
- &mod_data( "insert into email_log(email_res,send_time,client_id,email_addresses,files,type,status,subject,content) values(?,?,?,?,?,?,?,?,?)", $log_data );
- return 0;
- }
- sub write_excel(){
- my( $filename, $filecontent ) = @_;
- my $workbook = Spreadsheet::WriteExcel->new( $filename );
- if( ! defined $workbook ){
- critf( "Problems creating new Excel file $filename: $!" );
- return -1;
- }
- for my $k1( sort keys %{ $filecontent } ){
- my $worksheet = $workbook->add_worksheet( substr( $k1, 0, 31 ) );
- $worksheet->write_col( 0, 0, $filecontent->{ $k1 } );
- }
- $workbook->close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement