/** * Given this "unposted" Contribution entry, this will create PostLineItem(s) for this contribution entry * for a given StewardshipPost object. * * This will also update the UnpostedFlag to false and save. * * This will return a boolean specifying whether or not anything was actually posted. * * @param StewardshipPost $objPost the post to submit post-line-items for * @return boolean */ public function PostLineItems(StewardshipPost $objPost) { // Note that all fltArrays are multidimensional arrays that track amounts based on PersonId and FundId // e.g. $fltArray[$intPersonId][$intFundId] = $fltAmount // Calculate Posted-thus-far $fltPostedArray = array(); foreach (StewardshipPostLineItem::LoadArrayByStewardshipContributionId($this->intId) as $objPostLineItem) { if (!array_key_exists($objPostLineItem->PersonId, $fltPostedArray)) { $fltPostedArray[$objPostLineItem->PersonId] = array(); } if (!array_key_exists($objPostLineItem->StewardshipFundId, $fltPostedArray[$objPostLineItem->PersonId])) { $fltPostedArray[$objPostLineItem->PersonId][$objPostLineItem->StewardshipFundId] = $objPostLineItem->Amount; } else { $fltPostedArray[$objPostLineItem->PersonId][$objPostLineItem->StewardshipFundId] += $objPostLineItem->Amount; } } // Calculate the Actuals $fltActualArray = array(); $fltActualArray[$this->PersonId] = array(); foreach ($this->GetStewardshipContributionAmountArray() as $objAmount) { if (!array_key_exists($objAmount->StewardshipFundId, $fltActualArray[$this->PersonId])) { $fltActualArray[$this->PersonId][$objAmount->StewardshipFundId] = $objAmount->Amount; } else { $fltActualArray[$this->PersonId][$objAmount->StewardshipFundId] += $objAmount->Amount; } } // Calculate Difference/Discrepancies to Post foreach ($fltPostedArray as $intPersonId => $fltAmountByFundArray) { if (!array_key_exists($intPersonId, $fltActualArray)) { $fltActualArray[$intPersonId] = array(); } foreach ($fltAmountByFundArray as $intFundId => $fltAmount) { if (!array_key_exists($intFundId, $fltActualArray[$intPersonId])) { $fltActualArray[$intPersonId][$intFundId] = 0; } } } foreach ($fltActualArray as $intPersonId => $fltAmountByFundArray) { if (!array_key_exists($intPersonId, $fltPostedArray)) { $fltPostedArray[$intPersonId] = array(); } foreach ($fltAmountByFundArray as $intFundId => $fltAmount) { if (!array_key_exists($intFundId, $fltPostedArray[$intPersonId])) { $fltPostedArray[$intPersonId][$intFundId] = 0; } } } $fltDifferenceArray = array(); foreach ($fltActualArray as $intPersonId => $fltAmountByFundArray) { foreach ($fltAmountByFundArray as $intFundId => $fltAmount) { if ($fltAmount != $fltPostedArray[$intPersonId][$intFundId]) { $fltDifferenceArray[$intPersonId][$intFundId] = $fltAmount - $fltPostedArray[$intPersonId][$intFundId]; } } } // Post any differences that were calculated (if applicable) $blnPosted = false; foreach ($fltDifferenceArray as $intPersonId => $fltAmountByFundArray) { foreach ($fltAmountByFundArray as $intFundId => $fltAmount) { $objPostLineItem = new StewardshipPostLineItem(); $objPostLineItem->StewardshipPost = $objPost; $objPostLineItem->StewardshipContribution = $this; $objPostLineItem->PersonId = $intPersonId; $objPostLineItem->StewardshipFundId = $intFundId; $objPostLineItem->Description = $this->PostLineItemDescription; $objPostLineItem->Amount = $fltAmount; $objPostLineItem->Save(); $blnPosted = true; } } $this->blnUnpostedFlag = false; $this->Save(); return $blnPosted; }
/** * Deletes all associated StewardshipPostLineItems * @return void */ public function DeleteAllStewardshipPostLineItems() { if (is_null($this->intId)) { throw new QUndefinedPrimaryKeyException('Unable to call UnassociateStewardshipPostLineItem on this unsaved StewardshipContribution.'); } // Get the Database Object for this Class $objDatabase = StewardshipContribution::GetDatabase(); // Journaling if ($objDatabase->JournalingDatabase) { foreach (StewardshipPostLineItem::LoadArrayByStewardshipContributionId($this->intId) as $objStewardshipPostLineItem) { $objStewardshipPostLineItem->Journal('DELETE'); } } // Perform the SQL Query $objDatabase->NonQuery(' DELETE FROM `stewardship_post_line_item` WHERE `stewardship_contribution_id` = ' . $objDatabase->SqlVariable($this->intId) . ' '); }