Hello,
I’ve got an AOS_Invoice logic hook:
$hook_array['after_save'][] = Array(1, 'Product Value', 'custom/modules/AOS_Invoices/LogicHook.php', 'product', 'decrement_stocks');
and then:
function decrement_stocks ( $bean, $event, $arguments) {
$sInvoiceID = $bean->id;
// Get the invoice:
$oInvoice = new AOS_Invoices();
$oInvoice->retrieve($sInvoiceID);
// products decrement et.
$oInvoice->deducted_c = 'Yes';
$oInvoice->save();
deducted_c is by default No.
After I create and save the invoice, it is still No.
After I update and save the invoice, it is Yes.
Why does the logic hook work after save / update but not after save / create?
Thanks,
Chris
first you need to reference the $bean with &
decrement_stocks ( &$bean, $event,
secondly why are you redeclaring the invoice since you already have the data in $bean?
If you need to put some condition to mark the deducted as Yes you can do something like
if( $bean->status == 'Invoiced')
Above is just an example to have a check on status, you should use a proper condition that fulfils your business needs.
I’ve added the reference to the function.
And I’m loading the Invoice since I need to load related products later on. That would work by using the bean as well? I’ll give it a try.
But still … despite reference, deducted_ stays always ‘No’
function decrement_stocks ( &$bean, $event, $arguments)
{
// Take the ID from the bean and load the invoice
$oInvoice = new AOS_Invoices();
$oInvoice->retrieve($bean->id);
// Make sure to deduct the invoice only once:
if ( $oInvoice->invoice_origin_c == 'C' AND $oInvoice->deducted_c == 'No')
{
// Load related product quotes of the invoice:
$oInvoice->load_relationship('aos_products_quotes');
$aProductQuotes = $oInvoice->aos_products_quotes->getBeans();
foreach ($aProductQuotes as $oProductQuote)
{
// Load related products:
$oProductBean = BeanFactory::getBean('AOS_Products', $oProductQuote->product_id);
$oProductBean->stock_c = intval ( $oProduct->stock_c ) - intval ( $oProductQuote->product_qty );
}
$bean->deducted_c = 'Yes';
$oInvoice->deducted_c = 'Yes';
}
}
It turns out, everything seems to be working BUT:
$aProductQuotes = $bean->aos_products_quotes->getBeans();
So therefore products are not being updated. How would I get the related products (from bean or invoice … seems to be both the same)
Hi,
As I understand, by $aProductQuotes = $oInvoice->aos_products_quotes->getBeans(), you already have got the array of related product_quote beans, so you only need loop through them.
Try without $oProductBean = BeanFactory::getBean(‘AOS_Products’, $oProductQuote->product_id);
Best regards
Nikolaj