Whilst getting all contacts and using it for emailing (To, Cc, Bcc, etc) is fine, getting only one contact is important for report printing and for use in the FA user interface screens.
Currently, the function get_branch_contacts is used in:
sales/includes/ui/sales_order_ui.inc
repXXX.php
where XXX in 103 (commented out), 107 (Invoice), 108 (commented out), 109 through to 113.
In the extensions, rep_email_customers calls it in modules/reporting/rep_email_customers/includes/customer_emailer.inc with the last parameter as false (no defaults). Also, when the second parameter ($action) is set to null as in this file, all contacts accrue.
In sales/includes/ui/sales_order_ui.inc the following construct in lines 112-114:
$contact = get_branch_contacts($branch_id, 'order', $customer_id);
$order->set_branch($branch_id, $myrow["tax_group_id"],
$myrow["tax_group_name"], @$contact["phone"], @$contact["email"]);
expect to have defaults (missing last parameter assumed $default as true) and restricted to the 'orders' type alone and is used as a single contact for populating the branch details alone.
The standard repXXX.php listed above use the following constructs:
rep107.php:
$contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);
rep109.php:
$contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no'], true);
rep110.php:
$contacts = get_branch_contacts($branch['branch_code'], 'delivery', $branch['debtor_no'], true);
rep111.php:
$contacts = get_branch_contacts($branch['branch_code'], 'order', $branch['debtor_no'], true);
rep112.php:
$contacts = get_branch_contacts($myrow['branch_code'], 'invoice', $myrow['debtor_no']);
rep113.php:
$contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);
which makes last parameter $default to be true but allows for (but currently does not obtain) multiple contacts in subsequent emailing usage alone.
Either we determine it to be a single contact (count elements in array), or choose only the first one for the sales/includes/ui/sales_order_ui.inc and all other places we need single contacts or make a new function called get_branch_contact (singular) with the current code in it and make new content for the existing function get_branch_contacts (plural) for usage where multiple contacts are needed.
As far as emailing is concerned, we can make the first contact as the "To" recipient and all others as the "Cc" recipients if they exist!
Way forward is to be decided - all your thoughts on this is welcome before a patch is forwarded to @joe.
Hence currently, no code change is required except for setting the last parameter as false in reports that need to be emailed to multiple entities!