public function processPaymentsAction() { $visitId = (int) $this->_getParam('visitId'); $params = $this->_getParam('payment'); $paidAmounts = $this->_getParam('paid'); $writeOffAmounts = $this->_getParam('writeOff'); $note = $this->_getParam('note'); $sourceOfFunds = $this->_getParam('sourceOfFunds'); $checkNo = $this->_getParam('checkNo'); $userId = (int) Zend_Auth::getInstance()->getIdentity()->personId; // TODO: need to record payment as a postingJournal entry against the new payment row that represents the new check # or other source of unallocated funds if (is_array($params)) { $visit = new Visit(); $visit->visitId = $visitId; $visit->populate(); $payment = new Payment(); $payment->populateWithArray($params); $payerId = (int) $payment->payerId; $personId = (int) $payment->personId; $visitId = (int) $payment->visitId; $paymentDate = $payment->paymentDate; $otm = new WebVista_Model_ORMTransactionManager(); if (is_array($paidAmounts)) { $payments = array(); if ($sourceOfFunds == 'checkFundsId') { // check funds // uniqueCheckNumbers $checkFunds = Payment::listCheckFunds($checkNo); $payments = $checkFunds['details']; } else { // unallocated funds $unAllocatedFunds = $visit->unallocatedFunds; $payments = $unAllocatedFunds['details']; } $firstRow = strlen($note) > 0 ? false : true; foreach ($paidAmounts as $claimLineId => $amount) { $amount = (double) $amount; if (!$amount > 0) { continue; } $billable = $amount; foreach ($payments as $paymentId => $payment) { $amount = (double) $payment->unallocated; if (!$amount > 0) { unset($payments[$paymentId]); continue; } if ($amount > $billable) { $amount = $billable; } $payment->allocated += $amount; $payment->persist(); $otm->addORM($payment); $payments[$paymentId] = $payment; $postingJournal = new PostingJournal(); $postingJournal->paymentId = (int) $payment->paymentId; $postingJournal->patientId = $personId; $postingJournal->payerId = $payerId; $postingJournal->claimLineId = $claimLineId; $postingJournal->visitId = $visitId; $postingJournal->amount = $amount; $postingJournal->note = $note; $postingJournal->userId = $userId; $dateTime = date('Y-m-d H:i:s'); $postingJournal->datePosted = $paymentDate; $postingJournal->dateTime = $dateTime; $postingJournal->persist(); $otm->addORM($postingJournal); $billable -= $amount; if ($billable <= 0) { break; } } if (!$firstRow) { // persist payment note on first claim $claimLine = new ClaimLine(); $claimLine->claimLineId = (int) $claimLineId; if ($claimLine->populate()) { $claimLine->note = $note; $claimLine->persist(); $otm->addORM($claimLine); $firstRow = true; } } } } if (is_array($writeOffAmounts)) { $writeOff = new WriteOff(); $writeOff->populateWithArray($params); $writeOff->userId = (int) Zend_Auth::getInstance()->getIdentity()->personId; $writeOff->timestamp = date('Y-m-d H:i:s'); $writeOff->payerId = $payerId; foreach ($writeOffAmounts as $claimLineId => $amount) { if (!$amount > 0) { continue; } $writeOff->writeOffId = 0; $writeOff->claimLineId = (int) $claimLineId; $writeOff->amount = $amount; $writeOff->persist(); $otm->addORM($writeOff); } } if (count($otm->getQueries()) > 0 && !$otm->persist()) { trigger_error(__('Failed to save.')); } } $unallocatedFunds = 0; if (isset($visit)) { $funds = $visit->unallocatedFunds; $unallocatedFunds = (double) $funds['total']; } if ($unallocatedFunds < 0) { $unallocatedFunds = 0; } $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json'); $json->suppressExit = true; $json->direct(sprintf('%.2f', $unallocatedFunds)); }