/** * getMonthlyPrepayments * Takes a contract and determines how many payments have already * been made against charges assigned to this contract. Payments * are grouped by calendar month. * * @TODO This should be moved to Deferral Util lib * @param Contract $contract * @access public * @return void */ public function getMonthlyPrepayments(Contract $contract) { $buckets = array(); $inits = array(); // Initialize an array of $contract->getDeferralDurationMonths() buckets, indexed by month for ($j = 0; $j < $contract->getDeferralDurationMonths(); $j++) { $bucketDate = clone $contract->getContractStartDate(); $bucketDate->add(new \DateInterval('P' . $j . 'M')); $buckets[$bucketDate->format('Y-m')] = 0; } // Query journal table to determine how much has already been realized by this contract? $conn = $this->em->getConnection(); $stmt = $conn->prepare('select date_format(j.journal_date, "%m") as dmonth, year(j.journal_date) as dyear, sum(j.credit_amount) as damount from tsk_journal j inner join tsk_contract_charge cc where j.fk_credit_account_id=:credit_account_id AND cc.fk_contract_id=:contract_id and j.fk_charge_id=cc.fk_charge_id group by date_format(j.journal_date, "%m"), year(j.journal_date)'); $stmt->bindValue(':credit_account_id', 4); $stmt->bindValue(':contract_id', $contract->getId()); $stmt->execute(); $prepayments = $stmt->fetchAll(); // Add prepayments to buckets foreach ($prepayments as $pp) { $buckets[$pp['dyear'] . '-' . $pp['dmonth']] += $pp['damount']; } // Index bucket array by ints instead of months foreach ($buckets as $month => $bucketAmount) { $inits[] = $bucketAmount; } return $inits; }