예제 #1
0
 /**
  * Form validations.
  *
  * @param array $params
  *   Posted values of the form.
  * @param array $files
  *   List of errors to be posted back to the form.
  * @param \CRM_Batch_Form_Entry $self
  *   Form object.
  *
  * @return array
  *   list of errors to be posted back to the form
  */
 public static function formRule($params, $files, $self)
 {
     $errors = array();
     $batchTypes = CRM_Core_Pseudoconstant::get('CRM_Batch_DAO_Batch', 'type_id', array('flip' => 1), 'validate');
     $fields = array('total_amount' => ts('Amount'), 'financial_type' => ts('Financial Type'), 'payment_instrument' => ts('Payment Method'));
     //CRM-16480 if contact is selected, validate financial type and amount field.
     foreach ($params['field'] as $key => $value) {
         if (isset($value['trxn_id'])) {
             if (0 < CRM_Core_DAO::singleValueQuery('SELECT id FROM civicrm_contribution WHERE trxn_id = %1', array(1 => array($value['trxn_id'], 'String')))) {
                 $errors["field[{$key}][trxn_id]"] = ts('Transaction ID must be unique within the database');
             }
         }
         foreach ($fields as $field => $label) {
             if (!empty($params['primary_contact_id'][$key]) && empty($value[$field])) {
                 $errors["field[{$key}][{$field}]"] = ts('%1 is a required field.', array(1 => $label));
             }
         }
     }
     if (!empty($params['_qf_Entry_upload_force'])) {
         if (!empty($errors)) {
             return $errors;
         }
         return TRUE;
     }
     $batchTotal = 0;
     foreach ($params['field'] as $key => $value) {
         $batchTotal += $value['total_amount'];
         //validate for soft credit fields
         if (!empty($params['soft_credit_contact_id'][$key]) && empty($params['soft_credit_amount'][$key])) {
             $errors["soft_credit_amount[{$key}]"] = ts('Please enter the soft credit amount.');
         }
         if (!empty($params['soft_credit_amount']) && !empty($params['soft_credit_amount'][$key]) && CRM_Utils_Rule::cleanMoney(CRM_Utils_Array::value($key, $params['soft_credit_amount'])) > CRM_Utils_Rule::cleanMoney($value['total_amount'])) {
             $errors["soft_credit_amount[{$key}]"] = ts('Soft credit amount should not be greater than the total amount');
         }
         //membership type is required for membership batch entry
         if ($self->_batchInfo['type_id'] == $batchTypes['Membership']) {
             if (empty($value['membership_type'][1])) {
                 $errors["field[{$key}][membership_type]"] = ts('Membership type is a required field.');
             }
         }
     }
     if ($self->_batchInfo['type_id'] == $batchTypes['Pledge Payment']) {
         foreach (array_unique($params["open_pledges"]) as $value) {
             if (!empty($value)) {
                 $duplicateRows = array_keys($params["open_pledges"], $value);
             }
             if (!empty($duplicateRows) && count($duplicateRows) > 1) {
                 foreach ($duplicateRows as $key) {
                     $errors["open_pledges[{$key}]"] = ts('You can not record two payments for the same pledge in a single batch.');
                 }
             }
         }
     }
     if ((string) $batchTotal != $self->_batchInfo['total']) {
         $self->assign('batchAmountMismatch', TRUE);
         $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.');
     }
     if (!empty($errors)) {
         return $errors;
     }
     $self->assign('batchAmountMismatch', FALSE);
     return TRUE;
 }