/** * 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; }
/** * CRM-18000 - Test start_date, end_date after renewal */ public function testMembershipRenewalDates() { $form = new CRM_Batch_Form_Entry(); foreach (array($this->_contactID, $this->_contactID2) as $contactID) { $membershipParams = array('membership_type_id' => $this->_membershipTypeID2, 'contact_id' => $contactID, 'start_date' => "01/01/2015", 'join_date' => "01/01/2010", 'end_date' => "12/31/2015"); $this->contactMembershipCreate($membershipParams); } $params = $this->getMembershipData(); //ensure membership renewal $params['member_option'] = array(1 => 2, 2 => 2); $params['field'][1]['membership_type'] = array(0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2); $params['field'][1]['receive_date'] = date('Y-m-d'); // explicitly specify start and end dates $params['field'][2]['membership_type'] = array(0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2); $params['field'][2]['membership_start_date'] = "04/01/2016"; $params['field'][2]['membership_end_date'] = "03/31/2017"; $params['field'][2]['receive_date'] = "04/01/2016"; $this->assertTrue($form->testProcessMembership($params)); $result = $this->callAPISuccess('membership', 'get', array()); // renewal dates should be from current if start_date and end_date is passed as NULL $this->assertEquals(date('Y-m-d'), $result['values'][1]['start_date']); $endDate = date("Y-m-d", strtotime(date("Y-m-d") . " +1 year -1 day")); $this->assertEquals($endDate, $result['values'][1]['end_date']); // verify if the modified dates asserts with the dates passed above $this->assertEquals('2016-04-01', $result['values'][2]['start_date']); $this->assertEquals('2017-03-31', $result['values'][2]['end_date']); }
/** * Test Import */ function testProcessMembership() { $form = new CRM_Batch_Form_Entry(); $params = $this->getMembershipData(); $this->assertTrue($form->testProcessMembership($params)); $result = $this->callAPISuccess('membership', 'get', array()); $this->assertEquals(3, $result['count']); //check start dates #1 should default to 1 Jan this year, #2 should be as entered $this->assertEquals(date('Y-m-d', strtotime('first day of January 2013')), $result['values'][1]['start_date']); $this->assertEquals('2013-02-03', $result['values'][2]['start_date']); //check start dates #1 should default to 1 Jan this year, #2 should be as entered $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][1]['end_date']); $this->assertEquals(date('Y-m-d', strtotime('last day of December 2013')), $result['values'][2]['end_date']); $this->assertEquals('2013-12-01', $result['values'][3]['end_date']); //check start dates #1 should default to 1 Jan this year, #2 should be as entered $this->assertEquals(date('Y-m-d', strtotime('07/22/2013')), $result['values'][1]['join_date']); $this->assertEquals(date('Y-m-d', strtotime('07/03/2013')), $result['values'][2]['join_date']); $this->assertEquals(date('Y-m-d', strtotime('now')), $result['values'][3]['join_date']); }
/** * Test Contribution Import. */ public function testProcessContribution() { $this->offsetDefaultPriceSet(); $form = new CRM_Batch_Form_Entry(); $params = $this->getContributionData(); $this->assertTrue($form->testProcessContribution($params)); $result = $this->callAPISuccess('contribution', 'get', array('return' => 'total_amount')); $this->assertEquals(2, $result['count']); foreach ($result['values'] as $contribution) { $this->assertEquals($this->callAPISuccess('line_item', 'getvalue', array('contribution_id' => $contribution['id'], 'return' => 'line_total')), $contribution['total_amount']); } }