protected function afterCreate() { if ($this->paid == 'true' && $this->type == 'deposit' || $this->paid == 'true' && $this->type == 'Donation Deposit') { $this->allocateFunds(); } /* * This is goind to loop through this transaction and spend the money sending emails and such */ if ($this->type == 'expense') { //trying to create an expense lets check for funds to see if we have enough $funds = (new \Finances\Models\Funds())->setCondition('type', $this->project)->setCondition('amount', array('$gt' => 0))->getList(); $total = (int) $this->amount; //loops through funds spending them foreach ($funds as $fund) { $val = $fund->amount; if ((int) $total >= (int) $val) { $fund->spend($this, $val); $total = $total - $val; if ($total) { continue; } else { break; } } else { //the value of this donation is higher than the remaining cost $fund->spend($this, $total); break; } } } return parent::afterCreate(); }