<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[FrontAccounting forum — Navigating the contacts table - tutorial and speedup fixes]]></title>
	<link rel="self" href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=5419&amp;type=atom" />
	<updated>2014-12-25T09:26:03Z</updated>
	<generator>PunBB</generator>
	<id>https://frontaccounting.com/punbb/viewtopic.php?id=5419</id>
		<entry>
			<title type="html"><![CDATA[Navigating the contacts table - tutorial and speedup fixes]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=21890#p21890" />
			<content type="html"><![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>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2014-12-25T09:26:03Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=21890#p21890</id>
		</entry>
</feed>
