<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[FrontAccounting forum — Navigating the contacts table - tutorial and speedup fixes]]></title>
		<link>https://frontaccounting.com/punbb/viewtopic.php?id=5419</link>
		<atom:link href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=5419&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Navigating the contacts table - tutorial and speedup fixes.]]></description>
		<lastBuildDate>Thu, 25 Dec 2014 09:26:03 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Navigating the contacts table - tutorial and speedup fixes]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=21890#p21890</link>
			<description><![CDATA[<p>This tutorial is also <a href="https://frontaccounting.com/fawiki/index.php?n=Devel.ERDiagram23">wiki</a>-ed.</p><p><span class="bbu">Navigating <strong>0_crm_contacts</strong> table</span></p><p>The <strong>0_crm_contacts</strong> table performs a many to many relationship between the <strong>0_persons</strong> table and the a few <span class="bbu">entity_id</span> tables such as <strong>0_debtors_master</strong>, <strong>0_cust_branch</strong>, <strong>0_suppliers</strong>. Furthermore, this implementation in FA leverages several non standard constructs in php/mysql code that roughly translates into the following SQL statements: <br /></p><div class="codebox"><pre><code>-- Select a CustomerCode (for example) from &#039;&#039;&#039;debtor_no&#039;&#039;&#039; field in the &#039;&#039;&#039;0_debtor_master&#039;&#039;&#039; table
SET @debtor_entity_id:=600;

-- Get the HQ Person ID
SET @branch_person_id:=(SELECT person_id FROM `0_crm_contacts` 
    WHERE `type`=&#039;customer&#039; AND `action`=&#039;general&#039; AND person_id=@debtor_entity_id);

-- Get the BranchCodes 
SET @branch_codes:=(SELECT GROUP_CONCAT(branch_code) FROM `0_cust_branch` WHERE debtor_no=@debtor_entity_id);

-- Show the Input Parameters and Extracted Parameters
SELECT @debtor_entity_id AS CustomerCode, @branch_person_id AS HQPerson, @branch_codes AS BranchCodes;


-- HQ Branch Person details
SELECT * FROM `0_crm_persons` WHERE `id` IN (
SELECT person_id FROM `0_crm_contacts` WHERE `type`=&#039;cust_branch&#039; AND `action`=&#039;general&#039; AND entity_id IN (
        SELECT branch_code FROM `0_cust_branch` WHERE debtor_no=@debtor_entity_id
    ) AND person_id = @branch_person_id
);

-- Non HQ Branch Person Details
SELECT * FROM `0_crm_persons` WHERE `id` IN (
SELECT person_id FROM `0_crm_contacts` WHERE `type`=&#039;cust_branch&#039; AND `action`=&#039;general&#039; AND entity_id IN (
        SELECT branch_code FROM `0_cust_branch` WHERE debtor_no=@debtor_entity_id
    ) AND person_id &lt;&gt; @branch_person_id
);</code></pre></div><p>To effectively leverage this mechanism, a few indices are in order to speedup the process by over a many times (9000ms down to 3.5ms when queried from among 2500 records): <br /></p><div class="codebox"><pre><code>ALTER TABLE `0_crm_contacts` ADD INDEX `entity_id` (`entity_id`);
ALTER TABLE `0_crm_contacts` ADD INDEX `person_id` (`person_id`);</code></pre></div>]]></description>
			<author><![CDATA[null@example.com (apmuthu)]]></author>
			<pubDate>Thu, 25 Dec 2014 09:26:03 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=21890#p21890</guid>
		</item>
	</channel>
</rss>
