- AND 0 < ( SELECT COUNT(*) FROM cdr
- WHERE ( freesidestatus IS NULL OR freesidestatus = '' )
- AND ( charged_party = svc_phone.phonenum
- OR charged_party = svc_phone.countrycode
- || svc_phone.phonenum
- OR src = svc_phone.phonenum
- OR src = svc_phone.countrycode
- || svc_phone.phonenum
- )
- )
- )
+ AND EXISTS ( SELECT 1 FROM cdr
+ WHERE ( freesidestatus IS NULL OR freesidestatus = '' )
+ AND ( charged_party = svc_phone.phonenum
+ OR charged_party = svc_phone.countrycode
+ || svc_phone.phonenum
+ OR src = svc_phone.phonenum
+ OR src = svc_phone.countrycode
+ || svc_phone.phonenum
+ )
+ LIMIT 1
+ )
+ LIMIT 1
+ )
+ AND NOT EXISTS (
+ SELECT 1 FROM queue
+ WHERE queue.job = 'FS::cust_main::queued_bill'
+ AND queue.custnum = cust_pkg.custnum
+ LIMIT 1
+ )
+