Re: email single invoice to multiple recipients

apmuthu wrote:

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!

Either I am misunderstanding what this means, or this is not the case.

I think this means that in rep107.php if I change this line:

$contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], true);

to this:

$contacts = get_branch_contacts($branch['branch_code'], 'invoice', $branch['debtor_no'], false);

the mail should send to both contacts.  I had actually tried this before, based on the content of one of the posts you pointed me at earlier.  If I put the sales/includes/db/branches_db.inc file back to how it was originally and change true to false as described above, it throws this error on the screen when I send an email:

You have no email contact defined for this type of document for 'Computerisms'.

If I am misunderstanding, can you please clarify?

Re: email single invoice to multiple recipients

Even if it did work - and I have no idea if it does - it would still send one message to each recipient.

Message creation (in reporting/includes/pdf_report.inc) needs to be moved out of the foreach loop, with the approriate headers ("to" and/or "cc") being added as the script loops over the contacts array.

28 (edited by apmuthu 02/09/2015 09:09:33 am)

Re: email single invoice to multiple recipients

@bobloblian: Let's see what line 978 onwards in reporting/includes/pdf_report.inc has:

                $contactData = array();
                if ($this->contactData)
                    foreach($this->contactData as $contact)
                        if (!empty($contact['email']))
                            $contactData[] = $contact;

                if(!count($contactData)) {
                    $this->SetLang(user_language());
                    display_warning(sprintf(_("You have no email contact defined for this type of document for '%s'."), $this->formData['recipient_name']));
                } else {
                    $sent = $try = 0;

Only if there was no email available would you get the error stated

You have no email contact defined for this type of document for 'Computerisms'.

Therefore in the incoming $this->contactData itself, there would be no such record with email contact details and that narrows it down to the output of the get_branch_contacts() function. A check on with and without the last parameter being false will reveal that when it is true, the defaults have kicked in and not the appropriate contact for the specified action type. Try adding more than one contact to the branch where you choose it to be of invoice type (rep107.php) and then see what happens on making the last parameter false.

Please note that you will need tobackportsome report fixes from FA 2.4 since the SetCommonData() method needs to precede the Header2() call in reports that call it. This is necessary because the property $this->contactData is assigned from the $contact in line 409 and the latter comes from the said method SetCommonData() which in turn gets it from the get_branch_contacts() function call in rep107.php a couple of lines earlier.

@joe: the backports of the repXXX.php files done on 2015-01-21 is hence necessary.

Re: email single invoice to multiple recipients

@apmuthu

Why are we moving the SetHeaderTmpl to be after SetCommonData? As I can see in the code there is no call to Header2() before a NewPage is initiated. The SetHeaderTmpl is only setting the HeaderTmpl.

The NewPage call is after the SetCommonData.

So in my Point of view this is not necessary. I guess this was not necessary in 2.4 either, although it does not harm anything.

/Joe

Re: email single invoice to multiple recipients

@joe: I stand corrected that the Tmpl inclusion is needed for other reasons. The $rep->SetHeaderType('Header2') "includes" the header2.inc and doctext.inc files. These included files refer to data obtained in SetCommonData() which is useful for including non field db data as illustrated in the wiki for Bin Location type of functionality.

In this instance in rep107.php (Invoicing), $contacts is populated with data from the get_branch_contacts() function and is passed on as an argument into the $rep->SetCommonData() method which in turn populates the $this->formData and $this->contactData properties which in turn is used to get the contacts to email the reports to. In fact the SetCommonData() method makes even a single email id into an array, ie., $this->contactData.

Will investigate further....

Re: email single invoice to multiple recipients

@apmuthu

Hello again.

The member function Header2() is called in the member function NewPage on line 944 in pdf_report.inc.

Therefore it doesn't matter if the SetHeaderType member function is placed Before or after the SetCommonData member function.

/Joe

32 (edited by apmuthu 02/09/2015 01:05:38 pm)

Re: email single invoice to multiple recipients

Just tested out the multiple contacts availability by setting the last parameter $default=false in the get_branch_contacts() function call in rep107.php. Did a dump of the $contacts array before the function call and the $rep->contactData property after the function call with the $default parameter set to true (single) and false (multiple) and attached the outputs herein.

Hence set the last parameter in get_branch_contacts() to false if you want multiple mailing and leave it at true for single email.

The sequence of customer and branch creation is as follows:
1. Sales => Add and Manage Customers => Added New Customer with email id.
2. Customer Branches => Select Customer => Edit Customer Branch => Contacts => Add 2 New Contacts For Branch
=> Invoice and General Type with email id
=> Invoice and Orders Type with email id
3. Sales => Direct Invoice => Make an invoice for the customer branch.
4. Sales => Customer and Sales Reports => Print Invoices =. Choose to email customers and select the newly created invoice and Display Print Invoice (after setting rep107.php last parameter in get_branch_contacts() to false).

Works as expected - emails to 3 different recipients but separate emails.

@joe: Your logic is sound. Tested it out as well and it corroborates well - the NewPage is the one that actually gives effect to  the Tmpl setting and it's sequence anywhere prior to it is immaterial.

Post's attachments

contacts_array.zip 1.6 kb, 3 downloads since 2015-02-09 

You don't have the permssions to download the attachments of this post.