Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Last active June 8, 2017 08:48
Show Gist options
  • Save cgi-caesar/c508b2464cc1db3e1d0133b4051f233b to your computer and use it in GitHub Desktop.
Save cgi-caesar/c508b2464cc1db3e1d0133b4051f233b to your computer and use it in GitHub Desktop.
Combine terms in case of multiple items within invoice with custom logic
<?php
Am_Di::getInstance()->hook->add(Am_Event::INVOICE_TERMS, function(Am_Event $e) {
$invoice = $e->getInvoice();
if ($invoice->first_total>0 && !($invoice->second_total>0)) {
$n = new DateTime('00:00');
$f = clone $n;
foreach ($invoice->getItems() as $item) {
if ($item->first_total > 0) {
$n->add(new DateInterval("P" . strtoupper($item->first_period)));
}
}
$i = $f->diff($n);
$period = array();
foreach (array(
'y' => array('Year', 'Years'),
'm' => array('Month', 'Months'),
'd' => array('Day', 'Days')) as $token => $label) {
if (($_ = $i->$token) > 0) {
$period[] = sprintf('%d %s', $_, $_ > 1 ? $label[1] : $label[0]);
}
}
$terms = sprintf('%s for %s',
Am_Currency::render($invoice->first_total, $invoice->currency),
implode(' and ', $period));
$e->setReturn($terms);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment