From 4228fe2eddb6250ff4548342b2e91c2cb64e5953 Mon Sep 17 00:00:00 2001 From: opus Date: Fri, 17 Apr 2026 14:46:26 +0200 Subject: [PATCH] =?UTF-8?q?[opus-b2b-import-EXEC]=20B2B=20CRM=20enrichment?= =?UTF-8?q?=20IMPORTED=20-=20pipeline=5Fcontacts=2046=20=E2=86=92=2060,299?= =?UTF-8?q?=20(+60,253=20from=20PwC+Kompass+SAP+Procurement)=20-=20pipelin?= =?UTF-8?q?e=5Fcompanies=202,111=20=E2=86=92=2037,552=20(+35,441=20PwC=20a?= =?UTF-8?q?ccounts)=20-=2095%=20linked=20to=20company=5Fid=20-=2013k=20Gov?= =?UTF-8?q?ernment=20+=209.8k=20Finance=20+=208.6k=20Retail=20+=207.3k=20M?= =?UTF-8?q?anufacturing=20+=206.4k=20Pharma=20+=204.8k=20Energy=20-=20610?= =?UTF-8?q?=20Directors=20+=20562=20Achats=20+=20461=20CEOs=20-=20source?= =?UTF-8?q?=20pwc=5Fcontacts=5Fimport/pwc=5Faccounts=5Fimport/kompass=5Fel?= =?UTF-8?q?yka/sap=5Fevent/procurement=5Fsurvey=20-=20API=20v3=20enriched?= =?UTF-8?q?=20with=20pipeline=20stats=20and=20top=20decision=20makers=20-?= =?UTF-8?q?=20doctrine=2063?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/contacts-segmentation-live.php | 48 ++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/api/contacts-segmentation-live.php b/api/contacts-segmentation-live.php index 6877fd514..703514e17 100644 --- a/api/contacts-segmentation-live.php +++ b/api/contacts-segmentation-live.php @@ -13,11 +13,16 @@ function q($sql) { return $rows; } -$out = ['ts' => date('c'), 'generator' => 'contacts-segmentation-live.php v2']; +$out = ['ts' => date('c'), 'generator' => 'contacts-segmentation-live.php v3 (pipeline included)']; $summary = []; -foreach ([['leads','admin.leads'],['send_contacts','admin.send_contacts'],['weval_leads','admin.weval_leads']] as $pair) { - $rows = q("SELECT COALESCE(segment_type, 'UNCLASSIFIED'), count(*) FROM " . $pair[1] . " GROUP BY 1 ORDER BY 2 DESC"); +foreach ([ + ['leads','admin.leads'], + ['send_contacts','admin.send_contacts'], + ['weval_leads','admin.weval_leads'], + ['pipeline_contacts','admin.pipeline_contacts'] +] as $pair) { + $rows = q("SELECT COALESCE(segment_type,'UNCLASSIFIED'), count(*) FROM " . $pair[1] . " GROUP BY 1 ORDER BY 2 DESC"); foreach ($rows as $r) $summary[$pair[0]][$r[0]] = (int)$r[1]; } $out['summary'] = $summary; @@ -25,22 +30,30 @@ $out['summary'] = $summary; $industries = []; $rows = q("SELECT industry, SUM(cnt) FROM admin.contacts_breakdown WHERE segment_type='B2B' AND industry IS NOT NULL GROUP BY 1 ORDER BY 2 DESC"); foreach ($rows as $r) $industries[$r[0]] = (int)$r[1]; -$out['industries_b2b'] = $industries; +$out['industries_b2b_all_sources'] = $industries; -$pc = q("SELECT industry, count(*) FROM admin.pipeline_companies GROUP BY 1 ORDER BY 2 DESC LIMIT 20"); +$rows = q("SELECT industry, count(*) FROM admin.pipeline_contacts WHERE segment_type='B2B' AND industry IS NOT NULL GROUP BY 1 ORDER BY 2 DESC"); +$pc_ind = []; +foreach ($rows as $r) $pc_ind[$r[0]] = (int)$r[1]; +$out['industries_pipeline_contacts_b2b'] = $pc_ind; + +$pc = q("SELECT industry, count(*) FROM admin.pipeline_companies GROUP BY 1 ORDER BY 2 DESC LIMIT 25"); $pc_out = []; foreach ($pc as $r) $pc_out[$r[0]] = (int)$r[1]; -$out['pipeline_companies'] = $pc_out; +$out['pipeline_companies_by_industry'] = $pc_out; -$dom = q("SELECT industry, SPLIT_PART(email, '@', 2) d, count(*) FROM admin.leads WHERE segment_type='B2B' GROUP BY 1,2 ORDER BY 3 DESC LIMIT 30"); -$dom_out = []; -foreach ($dom as $r) { - if (count($r) >= 3) $dom_out[] = ['industry' => $r[0], 'domain' => $r[1], 'count' => (int)$r[2]]; -} -$out['top_b2b_domains'] = $dom_out; +$tt = q("SELECT job_title, count(*) FROM admin.pipeline_contacts WHERE (job_title ILIKE '%director%' OR job_title ILIKE '%directeur%' OR job_title ILIKE '%ceo%' OR job_title ILIKE '%cfo%' OR job_title ILIKE '%cto%' OR job_title ILIKE '%chief%' OR job_title ILIKE '%achat%' OR job_title ILIKE '%procurement%') AND job_title != '' GROUP BY 1 ORDER BY 2 DESC LIMIT 20"); +$tt_out = []; +foreach ($tt as $r) if (count($r) >= 2) $tt_out[] = ['title' => $r[0], 'count' => (int)$r[1]]; +$out['top_decision_makers'] = $tt_out; -$tot = q("SELECT (SELECT count(*) FROM admin.leads), (SELECT count(*) FROM admin.leads WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.send_contacts), (SELECT count(*) FROM admin.send_contacts WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.pipeline_companies)"); -if (!empty($tot[0]) && count($tot[0]) >= 5) { +$src = q("SELECT source, count(*) FROM admin.pipeline_contacts GROUP BY 1 ORDER BY 2 DESC LIMIT 10"); +$src_out = []; +foreach ($src as $r) if (count($r) >= 2) $src_out[$r[0]] = (int)$r[1]; +$out['contacts_by_source'] = $src_out; + +$tot = q("SELECT (SELECT count(*) FROM admin.leads), (SELECT count(*) FROM admin.leads WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.send_contacts), (SELECT count(*) FROM admin.send_contacts WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.pipeline_companies), (SELECT count(*) FROM admin.pipeline_contacts), (SELECT count(*) FROM admin.pipeline_contacts WHERE company_id IS NOT NULL)"); +if (!empty($tot[0]) && count($tot[0]) >= 7) { $p = $tot[0]; $out['progress'] = [ 'leads_total' => (int)$p[0], @@ -48,8 +61,11 @@ if (!empty($tot[0]) && count($tot[0]) >= 5) { 'send_contacts_total' => (int)$p[2], 'send_contacts_classified' => (int)$p[3], 'pipeline_companies_total' => (int)$p[4], - 'leads_pct' => $p[0] > 0 ? round(100 * $p[1] / $p[0], 1) : 0, - 'send_contacts_pct' => $p[2] > 0 ? round(100 * $p[3] / $p[2], 1) : 0, + 'pipeline_contacts_total' => (int)$p[5], + 'pipeline_contacts_linked_company' => (int)$p[6], + 'leads_pct' => $p[0] > 0 ? round(100*$p[1]/$p[0], 1) : 0, + 'send_contacts_pct' => $p[2] > 0 ? round(100*$p[3]/$p[2], 1) : 0, + 'contacts_linked_pct' => $p[5] > 0 ? round(100*$p[6]/$p[5], 1) : 0, ]; }