Provided as an example.
PHP Code:
// Iterate overall all Pending Invoices in the SalesOrder, check if any are due to be sent.
// Get id of this SalesOrder
$salesOrderId= id;
// Get ids of all Pending Invoices associated with this SalesOrder. Related is the Relationship 'invoices'.
// OrderBy=dateInvoiced, ascending.
// Filter: Pending
$invoiceIds= record\findRelatedMany('SalesOrder', $salesOrderId, 'invoices', 16, 'dateInvoiced', 'asc', 'status=', 'Pending');
$invoiceCnt= array\length($invoiceIds);
$traceMsg= string\concatenate('Invoices Pending: ', $invoiceCnt);
output\printLine($traceMsg);
$invoiceIndex= 0;
while ($invoiceIndex < $invoiceCnt)
{
// Get next Invoice
$invoiceId= array\at($invoiceIds, $invoiceIndex);
$invoiceStatus= record\attribute('Invoice', $invoiceId, 'status');
$invoiceName= record\attribute('Invoice', $invoiceId, 'name');
$invoiceDueDate= record\attribute('Invoice', $invoiceId, 'dateInvoiced');
// Trace
$traceMsg= string\concatenate('Invoice: ', $invoiceName, ' Status: ', $invoiceStatus, ' Due: ', $invoiceDueDate);
output\printLine($traceMsg);
// See if it's time to send
ifThen(
datetime\diff(datetime\today(), $invoiceDueDate, 'days') >= 0,
// then
// Generate Pdf.
$attachmentId= ext\pdf\generate(
'Invoice', // entity type for template
$invoiceId,
'6604227b7b7454967', // invoice template id
'invoice.pdf' // file name - name shown in attachments list
);
// Email the invoice
$toStr= billingContact.emailAddress;
$sender= 'accounting@yourdomain.com';
$subject= 'none';
$body= 'none';
$emailId = record\create(
'Email',
'to', $toStr,
'cc', $sender,
'from', $sender,
'subject', $subject,
'body', $body,
'isHtml', true,
'status', 'Sending',
'attachmentsIds', list($attachmentId),
'parentId', $invoiceId,
'parentType', 'Invoice'
);
// Email Template - Invoice
// (Note, when using a template, the following parameters described above do not need to be defined: 'subject', 'body', 'is Html')
ext\email\applyTemplate($emailId, '6605db6b9360232ca');
// Send the email
ext\email\send($emailId);
// Update Invoice Status to Sent. This removes it from processing.
record\update('Invoice', $invoiceId, 'status', 'Sent');
$traceMsg= string\concatenate('Invoice Sent: ', $invoiceName, ' Status: ', $invoiceStatus, ' Due: ', $invoiceDueDate);
output\printLine($traceMsg);
// Option to limit to max of 1 per day.
// break;
$dummyStatement=''
); // end Invoice Due
$invoiceIndex= $invoiceIndex + 1;
}
Leave a comment: