Skip to content

Instantly share code, notes, and snippets.

@cgi-caesar
Created June 21, 2017 10:17
Show Gist options
  • Save cgi-caesar/e2db82fbaf05bf29717e50cda7277a43 to your computer and use it in GitHub Desktop.
Save cgi-caesar/e2db82fbaf05bf29717e50cda7277a43 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();
$multi = array(
'y' => 365,
'm' => 30,
'd' => 1
);
if ($invoice->first_total>0 && !($invoice->second_total>0)) {
$first_period = null;
foreach ($invoice->getItems() as $item) {
if ($item->first_price>0) {
if ($item->first_period == Am_Period::MAX_SQL_DATE) {
$first_period = Am_Period::MAX_SQL_DATE;
break;
}
$p = new Am_Period($first_period ?: $item->first_period);
$np = new Am_Period($item->first_period);
$first_period =
$p->getCount() * $multi[$p->getUnit()] > $np->getCount() * $multi[$np->getUnit()] ?
strval($p) : strval($np);
}
}
$invoice->first_period = $first_period;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment