In my system, a user has a number of bookings (nodes of content type 'booking') associated. Each of these bookings has a 'field_price' field and a 'field_customer_user' field (for which I am using the Entity Reference module) that links the booking to its customer. The user also has a 'field_bill' field containing the sum of all his bookings' prices.
Each time a booking gets deleted, I want to subtract its price from its user's bill. I'm doing it the following way, using hook_node_delete:
function mysite_node_delete($node) {
if ($node->type == 'booking') {
if ($account = user_load($node->field_customer_user[LANGUAGE_NONE][0]['target_id'])) {
$bill = $account->field_bill[LANGUAGE_NONE][0]['value'];
$bill -= $node->field_price[LANGUAGE_NONE][0]['value'];
$account->field_bill[LANGUAGE_NONE][0]['value'] = $bill;
user_save($account);
}
}
}
However, sometimes this does not work as expected. Given the following case:
User booking #1: 100$
User booking #2: 200$
User booking #3: 50$
Total bill: 350$
An administrator logs in and deletes all three bookings for this user sequentially, starting from booking #1 and finishing with booking #3. The user's total bill should be 0 now, but it ends up being
Total bill: 300$.
I guess that this means that when user_save($account) was called for booking #3, the user bill had not been properly updated by the first two calls to hook_node_delete. Therefore, the old value (350$) gets fetched and subtracted the price of booking #3, and end ups being 300$, which is not correct.
Is there anyway of making it work?
$account->field_total_bill[LANGUAGE_NONE][0]['value'];
Where as in the line before the user_save the updated value is assigned to$account->field_bill[LANGUAGE_NONE][0]['value']