Created
June 21, 2017 10:17
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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