@ -88,90 +88,91 @@ foreach my $provider_contract( @{$dbh->selectall_arrayref('select contracts.*,re
#according to /reseller/ajax_reseller_filter
foreach my $ client_contact ( @ { $ dbh - > selectall_arrayref ( 'select contacts.* from contacts where reseller_id = ?' . ify ( ' and contacts.id' , @ { $ opt - > { client_contact_id } } ) , { Slice = > { } } , $ provider_contract - > { reseller_core_id } , @ { $ opt - > { client_contact_id } } ) } ) {
my $ client_contract = $ dbh - > selectrow_hashref ( 'select contracts.* from contracts where contracts.contact_id=? ' , undef , $ client_contact - > { id } ) ;
if ( my $ billing_profile = $ dbh - > selectrow_hashref ( ' select distinct billing_profiles . *
from billing_mappings
inner join billing_profiles on billing_mappings . billing_profile_id = billing_profiles . id
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 . status != "terminated"
and contracts . contact_id = ?
and ( billing_mappings . start_date <= ? OR billing_mappings . start_date IS NULL )
and ( billing_mappings . end_date >= ? OR billing_mappings . end_date IS NULL ) '
, undef , $ client_contact - > { id } , $ etime - > epoch , $ stime - > epoch
) ) {
my ( $ contract_balance , $ invoice ) = ( { } , { } ) ;
( $ contract_balance , $ invoice ) = get_contract_balance ( $ client_contract , $ billing_profile , $ contract_balance , $ invoice , $ stime , $ etime ) ;
my $ invoice_details_calls = $ dbh - > selectall_arrayref ( ' select cdr . * , bzh . zone , bzh . detail as zone_detail
from accounting . cdr
LEFT JOIN billing . billing_zones_history bzh ON bzh . id = cdr . source_customer_billing_zone_id
where
cdr . source_user_id != 0
and cdr . call_status = "ok"
and cdr . source_account_id = ?
and cdr . start_time >= ?
and cdr . start_time <= ?
order by cdr . start_time
- - limit 25 '
, { Slice = > { } }
, $ client_contract - > { id } , $ stime - > epoch , $ etime - > epoch
) ;
my $ invoice_details_zones = $ dbh - > selectall_arrayref ( ' select SUM ( cdr . source_customer_cost ) AS cost , COUNT ( * ) AS number , SUM ( cdr . duration ) AS duration , sum ( cdr . source_customer_free_time ) as free_time , bzh . zone
from accounting . cdr
LEFT JOIN billing . billing_zones_history bzh ON bzh . id = cdr . source_customer_billing_zone_id
where
cdr . source_user_id != 0
and cdr . call_status = "ok"
and cdr . source_account_id = ?
and cdr . start_time >= ?
and cdr . start_time <= ?
group by bzh . zone
order by bzh . zone '
, { Slice = > { } }
, $ client_contract - > { id } , $ stime - > epoch , $ etime - > epoch
) ;
my $ i = 1 ;
$ invoice_details_calls = [ map { [ $ i + + , $ _ ] } ( @$ invoice_details_calls ) x 1 ] ;
$ i = 1 ;
$ invoice_details_zones = [ map { [ $ i + + , $ _ ] } ( @$ invoice_details_zones ) x 1 ] ;
my ( $ in , $ out ) ;
#tt_id used only as part in temporary directory
$ in = {
no_fake_data = > 1 ,
provider_id = > $ provider_contract - > { reseller_core_id } ,
tt_type = > 'svg' ,
tt_sourcestate = > 'saved' ,
tt_id = > $ provider_contract - > { reseller_core_id } ,
} ;
$ out = {
tt_id = > $ provider_contract - > { reseller_core_id } ,
} ;
my $ stash = {
provider = > $ provider_contact ,
client = > $ client_contact ,
invoice = > $ invoice ,
invoice_details_zones = > $ invoice_details_zones ,
invoice_details_calls = > $ invoice_details_calls ,
} ;
my $ svg = $ dbh - > selectrow_array ( 'select base64_saved from invoice_templates where is_active = 1 and type = "svg" and reseller_id=?' , undef , $ provider_contract - > { reseller_core_id } ) ;
if ( $ svg ) {
NGCP::Panel::Utils::InvoiceTemplate:: preprocessInvoiceTemplateSvg ( $ in , \ $ svg ) ;
} else {
$ svg = $ svg_default ;
foreach my $ client_contract ( @ { $ dbh - > selectall_arrayref ( 'select contracts.* from contracts where contracts.contact_id=? ' , { Slice = > { } } , $ client_contact - > { id } ) } ) {
if ( my $ billing_profile = $ dbh - > selectrow_hashref ( ' select distinct billing_profiles . *
from billing_mappings
inner join billing_profiles on billing_mappings . billing_profile_id = billing_profiles . id
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 . status != "terminated"
and contracts . id = ?
and ( billing_mappings . start_date <= ? OR billing_mappings . start_date IS NULL )
and ( billing_mappings . end_date >= ? OR billing_mappings . end_date IS NULL ) '
, undef , $ client_contract - > { id } , $ etime - > epoch , $ stime - > epoch
) ) {
my ( $ contract_balance , $ invoice ) = ( { } , { } ) ;
( $ contract_balance , $ invoice ) = get_contract_balance ( $ client_contract , $ billing_profile , $ contract_balance , $ invoice , $ stime , $ etime ) ;
my $ invoice_details_calls = $ dbh - > selectall_arrayref ( ' select cdr . * , bzh . zone , bzh . detail as zone_detail
from accounting . cdr
LEFT JOIN billing . billing_zones_history bzh ON bzh . id = cdr . source_customer_billing_zone_id
where
cdr . source_user_id != 0
and cdr . call_status = "ok"
and cdr . source_account_id = ?
and cdr . start_time >= ?
and cdr . start_time <= ?
order by cdr . start_time
- - limit 25 '
, { Slice = > { } }
, $ client_contract - > { id } , $ stime - > epoch , $ etime - > epoch
) ;
my $ invoice_details_zones = $ dbh - > selectall_arrayref ( ' select SUM ( cdr . source_customer_cost ) AS cost , COUNT ( * ) AS number , SUM ( cdr . duration ) AS duration , sum ( cdr . source_customer_free_time ) as free_time , bzh . zone
from accounting . cdr
LEFT JOIN billing . billing_zones_history bzh ON bzh . id = cdr . source_customer_billing_zone_id
where
cdr . source_user_id != 0
and cdr . call_status = "ok"
and cdr . source_account_id = ?
and cdr . start_time >= ?
and cdr . start_time <= ?
group by bzh . zone
order by bzh . zone '
, { Slice = > { } }
, $ client_contract - > { id } , $ stime - > epoch , $ etime - > epoch
) ;
my $ i = 1 ;
$ invoice_details_calls = [ map { [ $ i + + , $ _ ] } ( @$ invoice_details_calls ) x 1 ] ;
$ i = 1 ;
$ invoice_details_zones = [ map { [ $ i + + , $ _ ] } ( @$ invoice_details_zones ) x 1 ] ;
my ( $ in , $ out ) ;
#tt_id used only as part in temporary directory
$ in = {
no_fake_data = > 1 ,
provider_id = > $ provider_contract - > { reseller_core_id } ,
tt_type = > 'svg' ,
tt_sourcestate = > 'saved' ,
tt_id = > $ provider_contract - > { reseller_core_id } ,
} ;
$ out = {
tt_id = > $ provider_contract - > { reseller_core_id } ,
} ;
my $ stash = {
provider = > $ provider_contact ,
client = > $ client_contact ,
invoice = > $ invoice ,
invoice_details_zones = > $ invoice_details_zones ,
invoice_details_calls = > $ invoice_details_calls ,
} ;
my $ svg = $ dbh - > selectrow_array ( 'select base64_saved from invoice_templates where is_active = 1 and type = "svg" and reseller_id=?' , undef , $ provider_contract - > { reseller_core_id } ) ;
if ( $ svg ) {
NGCP::Panel::Utils::InvoiceTemplate:: preprocessInvoiceTemplateSvg ( $ in , \ $ svg ) ;
} else {
$ svg = $ svg_default ;
}
$ svg = $ view - > getTemplateProcessed ( $ c_mock , \ $ svg , $ stash ) ;
#print $svg;
#die();
NGCP::Panel::Utils::InvoiceTemplate:: convertSvg2Pdf ( undef , \ $ svg , $ in , $ out ) ;
#binmode(STDOUT);
#print $out->{tt_string_pdf};
#die;
$ dbh - > do ( 'update invoices set data=? where id=?' , undef , $ out - > { tt_string_pdf } , $ invoice - > { id } ) ;
email ( $ provider_contact , $ client_contact , $ invoice , \ $ out - > { tt_string_pdf } ) ;
}
$ svg = $ view - > getTemplateProcessed ( $ c_mock , \ $ svg , $ stash ) ;
#print $svg;
#die();
NGCP::Panel::Utils::InvoiceTemplate:: convertSvg2Pdf ( undef , \ $ svg , $ in , $ out ) ;
#binmode(STDOUT);
#print $out->{tt_string_pdf};
#die;
$ dbh - > do ( 'update invoices set data=? where id=?' , undef , $ out - > { tt_string_pdf } , $ invoice - > { id } ) ;
email ( $ provider_contact , $ client_contact , $ invoice , \ $ out - > { tt_string_pdf } ) ;
}
}
}
@ -240,7 +241,7 @@ sub email{
content_type = > "text/plain" ,
charset = > "US-ASCII" ,
} ,
body_str = > "Dear Customer!\n\nEmail:$client_contact->{email} \n\nSome text from sales here. Using loc somehow?\n Best regards,",
body_str = > "Dear Customer!\n\nEmail:$client_contact->{email} . \n\nSome text from sales here. Using loc somehow?\n Best regards,",
) ,
]
) ;