public function savePayments()
 {
     $flash = Flash::Instance();
     $errors = array();
     $data = $this->_data['PeriodicPayment'];
     $db = DB::Instance();
     $db->StartTrans();
     foreach ($data as $id => $set) {
         if (isset($set['pay']) || isset($set['skip'])) {
             $pp = new PeriodicPayment();
             $pp->load($id);
             if (isset($set['pay'])) {
                 //	Create payment record array
                 $data = $pp->makePaymentTransaction();
                 $data['description'] = $set['description'];
                 $data['ext_reference'] = $set['ext_reference'];
                 if (isset($set['next_due_date'])) {
                     $data['transaction_date'] = $set['next_due_date'];
                 }
                 if (isset($set['net_value'])) {
                     $data['net_value'] = $set['net_value'];
                     $data['tax_value'] = $set['tax_value'];
                     $data['value'] = bcadd($data['net_value'], $data['tax_value']);
                 }
                 if (isset($set['gross_value'])) {
                     $data['tax_value'] = 0.0;
                     $data['net_value'] = $data['value'] = $set['gross_value'];
                 }
                 if ($pp->source == 'CR' || $pp->source == 'CP') {
                     if ($pp->source == 'CP') {
                         $data['net_value'] = bcmul($data['net_value'], -1);
                         $data['tax_value'] = bcmul($data['tax_value'], -1);
                         $data['value'] = bcmul($data['value'], -1);
                     }
                     CBTransaction::saveCashPayment($data, $errors);
                 } else {
                     if ($pp->source == 'PP') {
                         $payment = new PLTransaction();
                     } else {
                         $payment = new SLTransaction();
                     }
                     $payment->saveTransaction($data, $errors);
                 }
                 //	Update Periodic Payment record
                 $pp->current++;
                 if (!is_null($pp->end_date) && $pp->next_due_date > $pp->end_date) {
                     $pp->status = 'S';
                 }
                 if (!is_null($pp->occurs) && $pp->current >= $pp->occurs) {
                     $pp->status = 'S';
                 }
                 if ($pp->variable == 't' && $pp->write_variance == 't') {
                     $pp->net_value = $data['net_value'];
                     $pp->tax_value = $data['tax_value'];
                     $pp->gross_value = $data['value'];
                 }
                 if ($pp->status == 'S') {
                     $pp->glaccount_centre_id = null;
                 } else {
                     $pp->glaccount_centre_id = GLAccountCentre::getAccountCentreId($pp->glaccount_id, $pp->glcentre_id, $errors);
                 }
             }
             // Update the date if skipping or paying
             $pp->nextDate();
             if (count($errors) > 0 || !$pp->save()) {
                 $errors[] = 'Failed to update Periodic Payment details';
                 break;
             }
         }
     }
     if (count($errors) == 0 && $db->completeTrans()) {
         $flash->addMessage('Payments processed OK');
         sendTo($_SESSION['refererPage']['controller'], $_SESSION['refererPage']['action'], $_SESSION['refererPage']['modules'], isset($_SESSION['refererPage']['other']) ? $_SESSION['refererPage']['other'] : null);
     } else {
         $db->FailTrans();
         $db->completeTrans();
         $flash->addErrors($errors);
         $flash->addError('Failed to make payments');
         $this->refresh();
     }
 }