I'm trying to write a function that calculates the grand total price of each order, when given the price of the product and a credit that this user has. The credit should be deducted and if anything remains it should be deducted from the next order.
The function should return the final price of each order and the credits that remain foreach each account in one single array with the customer IDs as keys. Any negative numbers (credit or order prices) should all be set to 0.
'abc
' and 'def
' are customer IDs. This sample code should explain things much better. Do I need a recursive function for this?
Input:
//should return: order 1 = 375, order 2 = 90, remaining credit = 0;
$order['abc'][1] = 500;
$order['abc'][2] = 90;
$credit['abc'] = 125;
//should return: order 1: 0, order 2: 0, remaining credit = 125
$order['def'][1] = 100;
$order['def'][2] = 75;
$credits['def'] = 300;
The return should be one single array, as such:
$set['abc'][1] = 375;
$set['abc'][2] = 90;
$set['abc']['credit_remaining'] = 0;
$set['def'][1] = 0;
$set['def'][2] = 0;
$set['def']['credit_remaining'] = 125;