@ -431,103 +431,133 @@ sub getInvoiceProviderClients{
sub get_contract_calls_rs {
my $ self = shift ;
my % params = @ _ ;
( my ( $ c , $ provider_ contact_id, $ client_cont act_id, $ stime , $ etime ) ) = @ params { qw/c provider_id clien t_id stime etime/} ; #I think it may be a record (very long var name)
my $ source_account_id_condition ;
( my ( $ c , $ provider_ id, $ client_ contact_id, $ client_cont r act_id, $ stime , $ etime ) ) = @ params { qw/c provider_id clien t_contact_id client_contrac t_id stime etime/} ; #I think it may be a record (very long var name)
my % source_account_id_condition ;
$ stime || = NGCP::Panel::Utils::DateTime:: current_local ( ) - > truncate ( to = > 'month' ) ;
$ etime || = $ stime - > clone - > add ( months = > 1 ) ;
if ( ! $ client_contract_id ) {
#$source_account_id_condition = {
# 'in' => $self->getInvoiceProviderClients(%params)->search_rs({},{
# 'select' => 'me.id',
# })->as_query()
#};
$ source_account_id_condition = { 'in' = > $ self - > getInvoiceProviderClients (
provider_contact_id = > $ provider_contact_id ,
stime = > $ stime ,
etime = > $ etime ,
) - > search_rs ( undef , {
'select' = > 'me.id' ,
} ) - > as_query } ;
} else {
$ source_account_id_condition = $ client_contract_id ;
if ( $ client_contract_id ) {
% source_account_id_condition = ( 'source_user_id' = > $ client_contract_id ) ;
} elsif ( $ client_contact_id ) {
% source_account_id_condition = ( 'source_account.contact_id' = > $ client_contact_id ) ;
} elsif ( $ provider_id ) {
% source_account_id_condition = ( 'contact.reseller_id' = > $ client_contact_id ) ;
}
#I find that sql is more compact and clear
$ sql_of_all_provider_contract_id_clients_contact_ids_sip_pbx = [ '
select contacts . *
from contacts
where
contacts . reseller_id = ?
and exists ( select *
from billing_mappings
inner join contracts on contracts . id = billing_mappings . contract_id
inner join products on billing_mappings . product_id = products . id and products . class in ( "sipaccount" , "pbxaccount" )
where contracts . contact_id = contacts . id
and ( start_date <= ? OR start_date IS NULL )
and ( end_date >= ? OR end_date IS NULL )
) ' , $ provider_contract_id , $ etime - > epoch , $ stime - > epoch ] ;
$ sql_of_all_client_contract_id_calls = '
select cdr . *
from accounting . cdr
inner join contracts on cdr . source_account_id = contracts . id
and contracts . status != "terminated"
where
cdr . source_user_id != 0
and cdr . call_status = "ok"
and exists ( select *
from billing_mappings
inner join products on billing_mappings . product_id = products . id and products . class in ( "sipaccount" , "pbxaccount" )
where contracts . id = billing_mappings . contract_id
and ( billing_mappings . start_date >= now ( ) OR start_date IS NULL )
and ( billing_mappings . end_date <= now ( ) OR end_date IS NULL )
)
and contracts . contact_id = 22
order by cdr . start_time
' ;
# $source_account_id_condition = {
# 'in' => $self->getInvoiceProviderClients(%params)->search_rs({},{
# 'select' => 'me.id',
# })->as_query()
# };
# $source_account_id_condition = { 'in' => $self->getInvoiceProviderClients(
# provider_contact_id => $provider_contact_id,
# stime => $stime,
# etime => $etime,
# )->search_rs(undef,{
# 'select' => 'me.id',
# })->as_query };
#}
##I find that sql is more compact and clear
#$sql_of_all_provider_contract_id_clients_contact_ids_sip_pbx = ['
#select contacts.*
#from contacts
#where
# contacts.reseller_id=?
# and exists (select *
# from billing_mappings
# inner join contracts on contracts.id=billing_mappings.contract_id
# inner join products on billing_mappings.product_id=products.id and products.class in("sipaccount","pbxaccount")
# where contracts.contact_id=contacts.id
# and (start_date <= ? OR start_date IS NULL)
# and (end_date >= ? OR end_date IS NULL)
#)',$provider_contract_id, $etime->epoch, $stime->epoch];
#$sql_of_all_client_contract_id_calls = '
#select cdr.*
#from accounting.cdr
# inner join contracts on cdr.source_account_id=contracts.id
# and contracts.status != "terminated"
#where
# cdr.source_user_id != 0
# and cdr.call_status="ok"
# and exists (select *
# from billing_mappings
# inner join products on billing_mappings.product_id=products.id and products.class in("sipaccount","pbxaccount")
# where contracts.id=billing_mappings.contract_id
# and (billing_mappings.start_date >= now() OR start_date IS NULL)
# and (billing_mappings.end_date <= now() OR end_date IS NULL)
# )
# and contracts.contact_id=22
# order by cdr.start_time
#';
#
#$sql_of_all_provider_contact_id_clients_calls = '
#select cdr.*
#from contacts
# inner join contracts on contracts.contact_id=contacts.id
# and contacts.reseller_id=2 and contacts.reseller_id!=contacts.id
# inner join accounting.cdr on cdr.source_account_id=contracts.id
# and contracts.status != "terminated"
#where
# cdr.source_user_id != 0
# and cdr.call_status="ok"
# and exists (select *
# from billing_mappings
# inner join products on billing_mappings.product_id=products.id and products.class in("sipaccount","pbxaccount")
# where contracts.id=billing_mappings.contract_id
# and (billing_mappings.start_date >= now() OR start_date IS NULL)
# and (billing_mappings.end_date <= now() OR end_date IS NULL)
# )
# order by cdr.start_time
#';
#my $calls_rs = $self->schema->resultset('cdr')->search( {
# # source_user_id => { 'in' => [ map {$_->uuid} @{$contract->{subscriber}} ] },
# 'call_status' => 'ok',
# 'source_user_id' => { '!=' => '0' },
# 'contact.id' => { '!=' => $provider_contact_id },
# '-and' => [
# #'contact.reseller_id' => $provider_id, #$client_contract_id - contract of the client
# 'contact.reseller_id' => { '!=' => undef },
# ],
# 'source_account.status' => { '!=' => 'terminated'},
# '-exists' => $self->schema->resultset('billing_mappings')->search({
# 'contract_id' => \'= source_account.id',
# 'product.class' => [ "sipaccount", "pbxaccount" ],
# 'start_date' => [ -or =>
# { '<=' => 'cdr.start_time' },
# { -is => undef },
# ],
# 'end_date' => [ -or =>
# { '>=' => 'cdr.start_time' },
# { -is => undef },
# ],
# },{
# alias => 'billing_mappings_top',
# join => 'product',
# })->as_query,
#},{
# '+select' => [
# 'source_customer_billing_zones_history.zone',
# 'source_customer_billing_zones_history.detail',
# 'destination_user_in',
# ],
# '+as' => [qw/zone zone_detail destination/],
# 'join' => [
# {
# 'source_account' => 'contact',
# },
# 'source_customer_billing_zones_history',
# ]
#} );
$ sql_of_all_provider_contact_id_clients_calls = '
select cdr . *
from contacts
inner join contracts on contracts . contact_id = contacts . id
and contacts . reseller_id = 2 and contacts . reseller_id != contacts . id
inner join accounting . cdr on cdr . source_account_id = contracts . id
and contracts . status != "terminated"
where
cdr . source_user_id != 0
and cdr . call_status = "ok"
and exists ( select *
from billing_mappings
inner join products on billing_mappings . product_id = products . id and products . class in ( "sipaccount" , "pbxaccount" )
where contracts . id = billing_mappings . contract_id
and ( billing_mappings . start_date >= now ( ) OR start_date IS NULL )
and ( billing_mappings . end_date <= now ( ) OR end_date IS NULL )
)
order by cdr . start_time
' ;
my $ calls_rs = $ self - > schema - > resultset ( 'cdr' ) - > search ( {
# source_user_id => { 'in' => [ map {$_->uuid} @{$contract->{subscriber}} ] },
'call_status' = > 'ok' ,
'source_user_id' = > { '!=' = > '0' } ,
'contact.id' = > { '!=' = > $ provider_contact_id } ,
'-and' = > [
#'contact.reseller_id' => $provider_id, #$client_contract_id - contract of the client
'contact.reseller_id' = > { '!=' = > undef } ,
] ,
'source_account.status' = > { '!=' = > 'terminated' } ,
'-exists' = > $ self - > schema - > resultset ( 'billing_mappings' ) - > search ( {
'contract_id' = > \ '= source_account.id' ,
'product.class' = > [ "sipaccount" , "pbxaccount" ] ,
'start_date' = > [ - or = >
{ '<=' = > 'cdr.start_time' } ,
{ - is = > undef } ,
'contact.reseller_id' = > { '!=' = > undef } ,
start_time = >
[ - and = >
{ '>=' = > $ stime - > epoch } ,
{ '<=' = > $ etime - > epoch } ,
] ,
'end_date' = > [ - or = >
{ '>=' = > 'cdr.start_time' } ,
{ - is = > undef } ,
] ,
} , {
alias = > 'billing_mappings_top' ,
join = > 'product' ,
} ) - > as_query ,
% source_account_id_condition
} , {
'+select' = > [
'source_customer_billing_zones_history.zone' ,
@ -542,14 +572,14 @@ sub get_contract_calls_rs{
'source_customer_billing_zones_history' ,
]
} ) ;
return $ calls_rs ;
}
sub get_contract_zonesfees_rs {
my $ self = shift ;
my % params = @ _ ;
( my ( $ c , $ provider_id , $ client_ id, $ stime , $ etime ) ) = @ params { qw/c provider_id clien t_id stime etime/} ;
my $ source_account_id_condition ;
( my ( $ c , $ provider_id , $ client_ contact_id, $ client_contract_ id, $ stime , $ etime ) ) = @ params { qw/c provider_id clien t_contact_id client_contrac t_id stime etime/} ;
my % source_account_id_condition ;
if ( ! $ client_id ) {
$ source_account_id_condition = { 'in' = > $ self - > getInvoiceProviderClients ( % params ) - > search_rs ( { } , {
'select' = > 'me.id' ,