3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * This class provides the functionality for batch entry for contributions/memeberships
39 class CRM_Batch_Form_Entry
extends CRM_Core_Form
{
42 * maximum profile fields that will be displayed
45 protected $_rowCount = 1;
55 protected $_batchInfo = array();
58 * store the profile id associated with the batch type
60 protected $_profileId;
68 public $_membershipId = null;
70 * when not to reset sort_name
72 protected $_preserveDefault = TRUE;
77 protected $_contactFields = array();
80 * build all the data structures needed to build the form
85 function preProcess() {
86 $this->_batchId
= CRM_Utils_Request
::retrieve('id', 'Positive', $this, TRUE);
88 $this->_action
= CRM_Utils_Request
::retrieve('action', 'String', $this, FALSE, 'browse');
90 if (empty($this->_batchInfo
)) {
91 $params = array('id' => $this->_batchId
);
92 CRM_Batch_BAO_Batch
::retrieve($params, $this->_batchInfo
);
94 $this->assign('batchTotal', $this->_batchInfo
['total']);
95 $this->assign('batchType', $this->_batchInfo
['type_id']);
97 // get the profile id associted with this batch type
98 $this->_profileId
= CRM_Batch_BAO_Batch
::getProfileId($this->_batchInfo
['type_id']);
109 function buildQuickForm() {
110 if (!$this->_profileId
) {
111 CRM_Core_Error
::fatal(ts('Profile for bulk data entry is missing.'));
114 $this->addElement('hidden', 'batch_id', $this->_batchId
);
116 // get the profile information
117 if ($this->_batchInfo
['type_id'] == 1) {
118 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Contributions'));
119 $customFields = CRM_Core_BAO_CustomField
::getFields('Contribution');
122 CRM_Utils_System
::setTitle(ts('Batch Data Entry for Memberships'));
123 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership');
126 $this->_fields
= array();
127 $this->_fields
= CRM_Core_BAO_UFGroup
::getFields($this->_profileId
, FALSE, CRM_Core_Action
::VIEW
);
129 // remove file type field and then limit fields
130 $suppressFields = FALSE;
131 $removehtmlTypes = array('File', 'Autocomplete-Select');
132 foreach ($this->_fields
as $name => $field) {
133 if ($cfID = CRM_Core_BAO_CustomField
::getKeyID($name) &&
134 in_array($this->_fields
[$name]['html_type'], $removehtmlTypes)
136 $suppressFields = TRUE;
137 unset($this->_fields
[$name]);
140 //fix to reduce size as we are using this field in grid
141 if (is_array($field['attributes']) && $this->_fields
[$name]['attributes']['size'] > 19) {
142 //shrink class to "form-text-medium"
143 $this->_fields
[$name]['attributes']['size'] = 19;
147 $this->addFormRule(array('CRM_Batch_Form_Entry', 'formRule'), $this);
149 // add the force save button
150 $forceSave = $this->getButtonName('upload', 'force');
152 $this->addElement('submit',
154 ts('Ignore Mismatch & Process the Batch?')
157 $this->addButtons(array(
160 'name' => ts('Validate & Process the Batch'),
165 'name' => ts('Save & Continue Later'),
170 $this->assign('rowCount', $this->_batchInfo
['item_count'] +
1);
172 $fileFieldExists = FALSE;
173 $preserveDefaultsArray = array(
174 'first_name', 'last_name', 'middle_name',
179 $contactTypes = array('Contact', 'Individual', 'Household', 'Organization');
180 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
181 CRM_Contact_Form_NewContact
::buildQuickForm($this, $rowNumber, NULL, TRUE, 'primary_');
183 // special field specific to membership batch udpate
184 if ($this->_batchInfo
['type_id'] == 2) {
186 1 => ts('Add Membership'),
187 2 => ts('Renew Membership'),
189 $this->add('select', "member_option[$rowNumber]", '', $options);
192 foreach ($this->_fields
as $name => $field) {
193 if (in_array($field['field_type'], $contactTypes)) {
194 $this->_contactFields
[$field['name']] = 1;
196 CRM_Core_BAO_UFGroup
::buildProfile($this, $field, NULL, NULL, FALSE, FALSE, $rowNumber);
198 if (in_array($field['name'], $preserveDefaultsArray)) {
199 $this->_preserveDefault
= FALSE;
204 $this->assign('fields', $this->_fields
);
205 $this->assign('contactFields', $this->_contactFields
);
207 // don't set the status message when form is submitted.
208 $buttonName = $this->controller
->getButtonName('submit');
210 if ($suppressFields && $buttonName != '_qf_Entry_next') {
211 CRM_Core_Session
::setStatus(ts("FILE or Autocomplete Select type field(s) in the selected profile are not supported for Batch Update."), ts("Some fields have been excluded."), "info");
218 * @param array $params posted values of the form
219 * @param array $files list of errors to be posted back to the form
220 * @param array $self form object
222 * @return array list of errors to be posted back to the form
226 static function formRule($params, $files, $self) {
229 if (CRM_Utils_Array
::value('_qf_Entry_upload_force', $params)) {
234 foreach ($params['field'] as $key => $value) {
235 $batchTotal +
= $value['total_amount'];
237 //validate for soft credit fields
238 if (CRM_Utils_Array
::value($key, $params['soft_credit_contact_select_id']) && !CRM_Utils_Array
::value($key, $params['soft_credit_amount'])) {
239 $errors["soft_credit_amount[$key]"] = ts('Please enter the soft credit amount');
241 if (CRM_Utils_Array
::value($key, $params['soft_credit_amount'])
242 && CRM_Utils_Rule
::cleanMoney(CRM_Utils_Array
::value($key, $params['soft_credit_amount'])) > CRM_Utils_Rule
::cleanMoney($value['total_amount'])) {
243 $errors["soft_credit_amount[$key]"] = ts('Soft credit amount should not be greater than the total amount');
246 //membership type is required for membership batch entry
247 if ( $self->_batchInfo
['type_id'] == 2 ) {
248 if ( !CRM_Utils_Array
::value( 1, $value['membership_type'] ) ) {
249 $errors["field[$key][membership_type]"] = ts('Membership type is a required field.');
254 if ($batchTotal != $self->_batchInfo
['total']) {
255 $self->assign('batchAmountMismatch', TRUE);
256 $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.');
259 if (!empty($errors)) {
263 $self->assign('batchAmountMismatch', FALSE);
268 * Override default cancel action
270 function cancelAction() {
271 // redirect to batch listing
272 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
273 CRM_Utils_System
::civiExit();
277 * This function sets the default values for the form.
283 function setDefaultValues() {
284 if (empty($this->_fields
)) {
288 // for add mode set smart defaults
289 if ( $this->_action
& CRM_Core_Action
::ADD
) {
290 list( $currentDate, $currentTime ) = CRM_Utils_Date
::setDateDefaults( NULL, 'activityDateTime' );
293 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
294 $completeStatus = array_search( 'Completed', $allStatus );
295 $specialFields = array(
296 'join_date' => $currentDate,
297 'receive_date' => $currentDate,
298 'receive_date_time' => $currentTime,
299 'contribution_status_id' => $completeStatus
302 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
303 foreach ($specialFields as $key => $value ) {
304 $defaults['field'][$rowNumber][$key] = $value;
309 // get the existing batch values from cache table
310 $cacheKeyString = CRM_Batch_BAO_Batch
::getCacheKeyForBatch($this->_batchId
);
311 $defaults = CRM_Core_BAO_Cache
::getItem('batch entry', $cacheKeyString);
317 * process the form after the input has been submitted and validated
323 public function postProcess() {
324 $params = $this->controller
->exportValues($this->_name
);
326 $params['actualBatchTotal'] = 0;
328 // get the profile information
329 if ($this->_batchInfo
['type_id'] == 1) {
330 $this->processContribution($params);
333 $this->processMembership($params);
336 // update batch to close status
337 $paramValues = array(
338 'id' => $this->_batchId
,
341 'total' => $params['actualBatchTotal'],
344 CRM_Batch_BAO_Batch
::create($paramValues);
346 // delete from cache table
347 $cacheKeyString = CRM_Batch_BAO_Batch
::getCacheKeyForBatch($this->_batchId
);
348 CRM_Core_BAO_Cache
::deleteGroup('batch entry', $cacheKeyString, FALSE);
350 // set success status
351 CRM_Core_Session
::setStatus("", ts("Batch Processed."), "success");
353 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
357 * process contribution records
359 * @param array $params associated array of submitted values
365 private function processContribution(&$params) {
373 // get the price set associated with offline contribution record.
374 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Set', 'default_contribution_amount', 'id', 'name');
375 $this->_priceSet
= current(CRM_Price_BAO_Set
::getSetDetail($priceSetId));
376 $fieldID = key($this->_priceSet
['fields']);
378 if (isset($params['field'])) {
379 foreach ($params['field'] as $key => $value) {
380 // if contact is not selected we should skip the row
381 if (!CRM_Utils_Array
::value($key, $params['primary_contact_select_id'])) {
385 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_select_id']);
387 // update contact information
388 $this->updateContactInfo($value);
390 //build soft credit params
391 if (CRM_Utils_Array
::value($key, $params['soft_credit_contact_select_id']) && CRM_Utils_Array
::value($key, $params['soft_credit_amount'])) {
392 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_select_id'][$key];
393 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
396 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
397 CRM_Core_DAO
::$_nullObject,
402 foreach ($dates as $val) {
403 if ( CRM_Utils_Array
::value( $val, $value ) ) {
404 $value[$val] = CRM_Utils_Date
::processDate( $value[$val], $value[$val . '_time'], TRUE );
408 if (CRM_Utils_Array
::value('send_receipt', $value)) {
409 $value['receipt_date'] = date('Y-m-d His');
412 if ($value['financial_type']) {
413 $value['financial_type_id'] = $value['financial_type'];
416 if (CRM_Utils_Array
::value('payment_instrument', $value)) {
417 $value['payment_instrument_id'] = $value['payment_instrument'];
420 if (CRM_Utils_Array
::value('contribution_source', $value)) {
421 $value['source'] = $value['contribution_source'];
424 if (CRM_Utils_Array
::value('contribution_note', $value)) {
425 $value['note'] = $value['contribution_note'];
428 $params['actualBatchTotal'] +
= $value['total_amount'];
430 unset($value['contribution_note']);
431 unset($value['financial_type']);
432 unset($value['contribution_source']);
434 $value['batch_id'] = $this->_batchId
;
435 $value['skipRecentView'] = TRUE;
437 // build line item params
438 $this->_priceSet
['fields'][$fieldID]['options'][$fieldID]['amount'] = $value['total_amount'];
439 $value['price_'.$fieldID] = 1;
442 CRM_Price_BAO_Set
::processAmount($this->_priceSet
['fields'], $value, $lineItem[$priceSetId]);
444 //unset amount level since we always use quick config price set
445 unset($value['amount_level']);
447 //CRM-11529 for backoffice transactions
448 //when financial_type_id is passed in form, update the
449 //lineitems with the financial type selected in form
450 if (CRM_Utils_Array
::value('financial_type_id', $value) && CRM_Utils_Array
::value($priceSetId, $lineItem)) {
451 foreach ($lineItem[$priceSetId] as &$values) {
452 $values['financial_type_id'] = $value['financial_type_id'];
455 $value['line_item'] = $lineItem;
457 //finally call contribution create for all the magic
458 $contribution = CRM_Contribute_BAO_Contribution
::create($value, CRM_Core_DAO
::$_nullArray);
461 if (CRM_Utils_Array
::value('product_name', $value)) {
462 if ($value['product_name'][0] > 0) {
463 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
465 $value['hidden_Premium'] = 1;
466 $value['product_option'] = CRM_Utils_Array
::value(
467 $value['product_name'][1],
468 $options[$value['product_name'][0]]
471 $premiumParams = array(
472 'product_id' => $value['product_name'][0],
473 'contribution_id' => $contribution->id
,
474 'product_option' => $value['product_option'],
477 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
483 if ( $contribution->id
&&
484 CRM_Utils_Array
::value( 'send_receipt', $value ) ) {
485 // add the domain email id
486 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
487 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
489 $value['from_email_address'] = $domainEmail;
490 $value['contribution_id'] = $contribution->id
;
491 CRM_Contribute_Form_AdditionalInfo
::emailReceipt( $this, $value );
499 * process membership records
501 * @param array $params associated array of submitted values
507 private function processMembership(&$params) {
509 'join_date' => 'joinDate',
510 'membership_start_date' => 'startDate',
511 'membership_end_date' => 'endDate'
521 // get the price set associated with offline memebership
522 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Set', 'default_membership_type_amount', 'id', 'name');
523 $this->_priceSet
= $priceSets = current(CRM_Price_BAO_Set
::getSetDetail($priceSetId));
525 if (isset($params['field'])) {
526 $customFields = array();
527 foreach ($params['field'] as $key => $value) {
528 // if contact is not selected we should skip the row
529 if (!CRM_Utils_Array
::value($key, $params['primary_contact_select_id'])) {
533 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_select_id']);
535 // update contact information
536 $this->updateContactInfo($value);
538 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
540 foreach ($dateTypes as $dateField => $dateVariable) {
541 $
$dateVariable = CRM_Utils_Date
::processDate($value[$dateField]);
544 $calcDates = array();
545 $calcDates[$membershipTypeId] = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($membershipTypeId,
546 $joinDate, $startDate, $endDate
549 foreach ($calcDates as $memType => $calcDate) {
550 foreach ($dates as $d) {
551 //first give priority to form values then calDates.
552 $date = CRM_Utils_Array
::value($d, $value);
554 $date = CRM_Utils_Array
::value($d, $calcDate);
557 $value[$d] = CRM_Utils_Date
::processDate($date);
561 if (CRM_Utils_Array
::value('send_receipt', $value)) {
562 $value['receipt_date'] = date('Y-m-d His');
565 if (CRM_Utils_Array
::value('membership_source', $value)) {
566 $value['source'] = $value['membership_source'];
569 unset($value['membership_source']);
571 //Get the membership status
572 if ( CRM_Utils_Array
::value('membership_status', $value) ) {
573 $value['status_id'] = $value['membership_status'];
574 unset($value['membership_status']);
577 if (empty($customFields)) {
578 // membership type custom data
579 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE, $membershipTypeId);
581 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
582 CRM_Core_BAO_CustomField
::getFields('Membership',
583 FALSE, FALSE, NULL, NULL, TRUE
588 //check for custom data
589 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($params['field'][$key],
596 if (CRM_Utils_Array
::value('financial_type', $value)) {
597 $value['financial_type_id'] = $value['financial_type'];
600 if (CRM_Utils_Array
::value('payment_instrument', $value)) {
601 $value['payment_instrument_id'] = $value['payment_instrument'];
604 // handle soft credit
605 if (CRM_Utils_Array
::value($key, $params['soft_credit_contact_select_id']) && CRM_Utils_Array
::value($key, $params['soft_credit_amount'])) {
606 $value['soft_credit'][$key]['contact_id'] = $params['soft_credit_contact_select_id'][$key];
607 $value['soft_credit'][$key]['amount'] = CRM_Utils_Rule
::cleanMoney($params['soft_credit_amount'][$key]);
610 if ( CRM_Utils_Array
::value('receive_date', $value) ) {
611 $value['receive_date'] = CRM_Utils_Date
::processDate( $value['receive_date'], $value['receive_date_time'] , TRUE );
614 $params['actualBatchTotal'] +
= $value['total_amount'];
616 unset($value['financial_type']);
617 unset($value['payment_instrument']);
619 $value['batch_id'] = $this->_batchId
;
620 $value['skipRecentView'] = TRUE;
622 // make entry in line item for contribution
624 $editedFieldParams = array(
625 'price_set_id' => $priceSetId,
626 'name' => $value['membership_type'][0]
629 $editedResults = array();
630 CRM_Price_BAO_Field
::retrieve($editedFieldParams, $editedResults);
632 if (!empty($editedResults)) {
633 unset($this->_priceSet
['fields']);
634 $this->_priceSet
['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']];
635 unset($this->_priceSet
['fields'][$editedResults['id']]['options']);
636 $fid = $editedResults['id'];
637 $editedFieldParams = array(
638 'price_field_id' => $editedResults['id'],
639 'membership_type_id' => $value['membership_type_id']
642 $editedResults = array();
643 CRM_Price_BAO_FieldValue
::retrieve($editedFieldParams, $editedResults);
644 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']];
645 if (CRM_Utils_Array
::value('total_amount', $value)) {
646 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']]['amount'] = $value['total_amount'];
649 $fieldID = key($this->_priceSet
['fields']);
650 $value['price_' . $fieldID] = $editedResults['id'];
653 CRM_Price_BAO_Set
::processAmount($this->_priceSet
['fields'],
654 $value, $lineItem[$priceSetId]
657 //CRM-11529 for backoffice transactions
658 //when financial_type_id is passed in form, update the
659 //lineitems with the financial type selected in form
660 if (CRM_Utils_Array
::value('financial_type_id', $value) && CRM_Utils_Array
::value($priceSetId, $lineItem)) {
661 foreach ($lineItem[$priceSetId] as &$values) {
662 $values['financial_type_id'] = $value['financial_type_id'];
666 $value['lineItems'] = $lineItem;
667 $value['processPriceSet'] = TRUE;
669 // end of contribution related section
671 unset($value['membership_type']);
672 unset($value['membership_start_date']);
673 unset($value['membership_end_date']);
675 $value['is_renew'] = false;
676 if ( CRM_Utils_Array
::value('member_option', $params) && CRM_Utils_Array
::value( $key, $params['member_option'] ) == 2 ) {
677 $this->_params
= $params;
678 $value['is_renew'] = true;
679 $membership = CRM_Member_BAO_Membership
::renewMembership(
680 $value['contact_id'],
681 $value['membership_type_id'],
682 FALSE, $this, NULL, NULL,
686 // make contribution entry
687 CRM_Member_BAO_Membership
::recordMembershipContribution( $value, CRM_Core_DAO
::$_nullArray, $membership->id
);
690 $membership = CRM_Member_BAO_Membership
::create($value, CRM_Core_DAO
::$_nullArray);
694 if (CRM_Utils_Array
::value('product_name', $value)) {
695 if ($value['product_name'][0] > 0) {
696 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
698 $value['hidden_Premium'] = 1;
699 $value['product_option'] = CRM_Utils_Array
::value(
700 $value['product_name'][1],
701 $options[$value['product_name'][0]]
704 $premiumParams = array(
705 'product_id' => $value['product_name'][0],
706 'contribution_id' => $value['contribution_id'],
707 'product_option' => $value['product_option'],
710 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
716 if ( $membership->id
&&
717 CRM_Utils_Array
::value( 'send_receipt', $value ) ) {
719 // add the domain email id
720 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
721 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
723 $value['from_email_address'] = $domainEmail;
724 $value['membership_id'] = $membership->id
;
725 CRM_Member_Form_Membership
::emailReceipt( $this, $value, $membership );
732 * update contact information
734 * @param array $value associated array of submitted values
740 private function updateContactInfo(&$value) {
741 $value['preserveDBName'] = $this->_preserveDefault
;
743 //parse street address, CRM-7768
744 CRM_Contact_Form_Task_Batch
::parseStreetAddress($value, $this);
746 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
,