function processAction()
 {
     $clinicalNoteId = (int) $this->_getParam('clinicalNoteId');
     $revisionId = (int) $this->_getParam('revisionId');
     $data = $this->_getParam('namespaceData');
     $saveDate = date('Y-m-d H:i:s');
     $cn = new ClinicalNote();
     $cn->clinicalNoteId = $clinicalNoteId;
     $cn->populate();
     if (!$revisionId > 0) {
         $revisionId = GenericData::getUnsignedRevisionId(get_class($cn), $cn->clinicalNoteId);
     }
     $eSignatureId = ESignature::retrieveSignatureId(get_class($cn), $revisionId);
     if ($eSignatureId > 0) {
         $msg = __('Failed to save. Note is already signed');
     } else {
         $cn->dateTime = date('Y-m-d H:i:s');
         $cn->persist();
         $msg = __('Data saved.');
         $template = $cn->clinicalNoteDefinition->clinicalNoteTemplate->template;
         $xml = simplexml_load_string($template);
         $objectClass = 'ClinicalNote';
         list($name, $value) = each($data);
         $gd = new GenericData();
         $gd->objectClass = $objectClass;
         $gd->objectId = $clinicalNoteId;
         $gd->name = $name;
         $rowExists = $gd->doesRowExist(true);
         $preQueries = null;
         if ($rowExists) {
             $revisionId = (int) $gd->revisionId;
             $preQueries = 'DELETE FROM `' . $gd->_table . '` WHERE `revisionId`=' . $revisionId;
         } else {
             $revisionId = WebVista_Model_ORM::nextSequenceId();
         }
         $otm = new WebVista_Model_ORMTransactionManager();
         foreach ($data as $name => $value) {
             $gd = new GenericData();
             $gd->objectClass = $objectClass;
             $gd->objectId = $clinicalNoteId;
             $gd->dateTime = $saveDate;
             $gd->name = $name;
             $gd->value = $value;
             $gd->revisionId = $revisionId;
             $otm->addORM($gd);
         }
         if (!$otm->persist($preQueries)) {
             $msg = __('Failed to save.');
         }
         if ((string) $xml->attributes()->useNSDR && (string) $xml->attributes()->useNSDR == 'true') {
             if (!ClinicalNote::processNSDRPersist($xml, $cn, $data)) {
                 $msg = __('Failed to save.');
             }
         }
     }
     $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
     $json->suppressExit = true;
     $json->direct($msg);
 }
 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));
 }