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 //membership type is required for membership batch entry
238 if ( $self->_batchInfo
['type_id'] == 2 ) {
239 if ( !CRM_Utils_Array
::value( 1, $value['membership_type'] ) ) {
240 $errors["field[$key][membership_type]"] = ts('Membership type is a required field.');
245 if ($batchTotal != $self->_batchInfo
['total']) {
246 $self->assign('batchAmountMismatch', TRUE);
247 $errors['_qf_defaults'] = ts('Total for amounts entered below does not match the expected batch total.');
250 if (!empty($errors)) {
254 $self->assign('batchAmountMismatch', FALSE);
259 * Override default cancel action
261 function cancelAction() {
262 // redirect to batch listing
263 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
264 CRM_Utils_System
::civiExit();
268 * This function sets the default values for the form.
274 function setDefaultValues() {
275 if (empty($this->_fields
)) {
279 // for add mode set smart defaults
280 if ( $this->_action
& CRM_Core_Action
::ADD
) {
281 list( $currentDate, $currentTime ) = CRM_Utils_Date
::setDateDefaults( NULL, 'activityDateTime' );
284 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
285 $completeStatus = array_search( 'Completed', $allStatus );
286 $specialFields = array(
287 'join_date' => $currentDate,
288 'receive_date' => $currentDate,
289 'receive_date_time' => $currentTime,
290 'contribution_status_id' => $completeStatus
293 for ($rowNumber = 1; $rowNumber <= $this->_batchInfo
['item_count']; $rowNumber++
) {
294 foreach ($specialFields as $key => $value ) {
295 $defaults['field'][$rowNumber][$key] = $value;
300 // get the existing batch values from cache table
301 $cacheKeyString = CRM_Batch_BAO_Batch
::getCacheKeyForBatch($this->_batchId
);
302 $defaults = CRM_Core_BAO_Cache
::getItem('batch entry', $cacheKeyString);
308 * process the form after the input has been submitted and validated
314 public function postProcess() {
315 $params = $this->controller
->exportValues($this->_name
);
317 $params['actualBatchTotal'] = 0;
319 // get the profile information
320 if ($this->_batchInfo
['type_id'] == 1) {
321 $this->processContribution($params);
324 $this->processMembership($params);
327 // update batch to close status
328 $paramValues = array(
329 'id' => $this->_batchId
,
332 'total' => $params['actualBatchTotal'],
335 CRM_Batch_BAO_Batch
::create($paramValues);
337 // delete from cache table
338 $cacheKeyString = CRM_Batch_BAO_Batch
::getCacheKeyForBatch($this->_batchId
);
339 CRM_Core_BAO_Cache
::deleteGroup('batch entry', $cacheKeyString, FALSE);
341 // set success status
342 CRM_Core_Session
::setStatus("", ts("Batch Processed."), "success");
344 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/batch', 'reset=1'));
348 * process contribution records
350 * @param array $params associated array of submitted values
356 private function processContribution(&$params) {
364 // get the price set associated with offline contribution record.
365 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Set', 'default_contribution_amount', 'id', 'name');
366 $this->_priceSet
= current(CRM_Price_BAO_Set
::getSetDetail($priceSetId));
367 $fieldID = key($this->_priceSet
['fields']);
369 $assetRelation = key(CRM_CORE_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Asset Account is' "));
371 if (isset($params['field'])) {
372 foreach ($params['field'] as $key => $value) {
373 // if contact is not selected we should skip the row
374 if (!CRM_Utils_Array
::value($key, $params['primary_contact_select_id'])) {
378 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_select_id']);
380 // update contact information
381 $this->updateContactInfo($value);
383 // handle soft credit
384 if (CRM_Utils_Array
::value('soft_credit_contact_select_id', $params) && CRM_Utils_Array
::value($key, $params['soft_credit_contact_select_id'])) {
385 $value['soft_credit_to'] = $params['soft_credit_contact_select_id'][$key];
388 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($value,
389 CRM_Core_DAO
::$_nullObject,
394 foreach ($dates as $val) {
395 if ( CRM_Utils_Array
::value( $val, $value ) ) {
396 $value[$val] = CRM_Utils_Date
::processDate( $value[$val], $value[$val . '_time'], TRUE );
400 if (CRM_Utils_Array
::value('send_receipt', $value)) {
401 $value['receipt_date'] = date('Y-m-d His');
404 if ($value['financial_type']) {
405 $value['financial_type_id'] = $value['financial_type'];
408 if (CRM_Utils_Array
::value('payment_instrument', $value)) {
409 $value['payment_instrument_id'] = $value['payment_instrument'];
412 if (CRM_Utils_Array
::value('contribution_source', $value)) {
413 $value['source'] = $value['contribution_source'];
416 if (CRM_Utils_Array
::value('contribution_note', $value)) {
417 $value['note'] = $value['contribution_note'];
420 $params['actualBatchTotal'] +
= $value['total_amount'];
422 unset($value['contribution_note']);
423 unset($value['financial_type']);
424 unset($value['contribution_source']);
426 $value['batch_id'] = $this->_batchId
;
427 $value['skipRecentView'] = TRUE;
429 // build line item params
430 $this->_priceSet
['fields'][$fieldID]['options'][$fieldID]['amount'] = $value['total_amount'];
431 $value['price_'.$fieldID] = 1;
434 CRM_Price_BAO_Set
::processAmount($this->_priceSet
['fields'], $value, $lineItem[$priceSetId]);
436 //unset amount level since we always use quick config price set
437 unset($value['amount_level']);
439 //CRM-11529 for backoffice transactions
440 //when financial_type_id is passed in form, update the
441 //lineitems with the financial type selected in form
442 if (CRM_Utils_Array
::value('financial_type_id', $value) && CRM_Utils_Array
::value($priceSetId, $lineItem)) {
443 foreach ($lineItem[$priceSetId] as &$values) {
444 $values['financial_type_id'] = $value['financial_type_id'];
447 $value['line_item'] = $lineItem;
449 //finally call contribution create for all the magic
450 $contribution = CRM_Contribute_BAO_Contribution
::create($value, CRM_Core_DAO
::$_nullArray);
453 if (CRM_Utils_Array
::value('product_name', $value)) {
454 if ($value['product_name'][0] > 0) {
455 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
457 $value['hidden_Premium'] = 1;
458 $value['product_option'] = CRM_Utils_Array
::value(
459 $value['product_name'][1],
460 $options[$value['product_name'][0]]
463 $premiumParams = array(
464 'product_id' => $value['product_name'][0],
465 'contribution_id' => $contribution->id
,
466 'product_option' => $value['product_option'],
469 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
475 if ( $contribution->id
&&
476 CRM_Utils_Array
::value( 'send_receipt', $value ) ) {
477 // add the domain email id
478 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
479 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
481 $value['from_email_address'] = $domainEmail;
482 $value['contribution_id'] = $contribution->id
;
483 CRM_Contribute_Form_AdditionalInfo
::emailReceipt( $this, $value );
491 * process membership records
493 * @param array $params associated array of submitted values
499 private function processMembership(&$params) {
501 'join_date' => 'joinDate',
502 'membership_start_date' => 'startDate',
503 'membership_end_date' => 'endDate'
513 // get the price set associated with offline memebership
514 $priceSetId = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_Set', 'default_membership_type_amount', 'id', 'name');
515 $this->_priceSet
= $priceSets = current(CRM_Price_BAO_Set
::getSetDetail($priceSetId));
517 if (isset($params['field'])) {
518 $customFields = array();
519 foreach ($params['field'] as $key => $value) {
520 // if contact is not selected we should skip the row
521 if (!CRM_Utils_Array
::value($key, $params['primary_contact_select_id'])) {
525 $value['contact_id'] = CRM_Utils_Array
::value($key, $params['primary_contact_select_id']);
527 // update contact information
528 $this->updateContactInfo($value);
530 $membershipTypeId = $value['membership_type_id'] = $value['membership_type'][1];
532 foreach ($dateTypes as $dateField => $dateVariable) {
533 $
$dateVariable = CRM_Utils_Date
::processDate($value[$dateField]);
536 $calcDates = array();
537 $calcDates[$membershipTypeId] = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($membershipTypeId,
538 $joinDate, $startDate, $endDate
541 foreach ($calcDates as $memType => $calcDate) {
542 foreach ($dates as $d) {
543 //first give priority to form values then calDates.
544 $date = CRM_Utils_Array
::value($d, $value);
546 $date = CRM_Utils_Array
::value($d, $calcDate);
549 $value[$d] = CRM_Utils_Date
::processDate($date);
553 if (CRM_Utils_Array
::value('send_receipt', $value)) {
554 $value['receipt_date'] = date('Y-m-d His');
557 if (CRM_Utils_Array
::value('membership_source', $value)) {
558 $value['source'] = $value['membership_source'];
561 unset($value['membership_source']);
563 //Get the membership status
564 if ( CRM_Utils_Array
::value('membership_status', $value) ) {
565 $value['status_id'] = $value['membership_status'];
566 unset($value['membership_status']);
569 if (empty($customFields)) {
570 // membership type custom data
571 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE, $membershipTypeId);
573 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
574 CRM_Core_BAO_CustomField
::getFields('Membership',
575 FALSE, FALSE, NULL, NULL, TRUE
580 //check for custom data
581 $value['custom'] = CRM_Core_BAO_CustomField
::postProcess($params['field'][$key],
588 if (CRM_Utils_Array
::value('financial_type', $value)) {
589 $value['financial_type_id'] = $value['financial_type'];
592 if (CRM_Utils_Array
::value('payment_instrument', $value)) {
593 $value['payment_instrument_id'] = $value['payment_instrument'];
596 // handle soft credit
597 if (CRM_Utils_Array
::value('soft_credit_contact_select_id', $params) && CRM_Utils_Array
::value($key, $params['soft_credit_contact_select_id'])) {
598 $value['soft_credit_to'] = $params['soft_credit_contact_select_id'][$key];
601 if ( CRM_Utils_Array
::value('receive_date', $value) ) {
602 $value['receive_date'] = CRM_Utils_Date
::processDate( $value['receive_date'], $value['receive_date_time'] , TRUE );
605 $params['actualBatchTotal'] +
= $value['total_amount'];
607 unset($value['financial_type']);
608 unset($value['payment_instrument']);
610 $value['batch_id'] = $this->_batchId
;
611 $value['skipRecentView'] = TRUE;
613 // make entry in line item for contribution
615 $editedFieldParams = array(
616 'price_set_id' => $priceSetId,
617 'name' => $value['membership_type'][0]
620 $editedResults = array();
621 CRM_Price_BAO_Field
::retrieve($editedFieldParams, $editedResults);
623 if (!empty($editedResults)) {
624 unset($this->_priceSet
['fields']);
625 $this->_priceSet
['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']];
626 unset($this->_priceSet
['fields'][$editedResults['id']]['options']);
627 $fid = $editedResults['id'];
628 $editedFieldParams = array(
629 'price_field_id' => $editedResults['id'],
630 'membership_type_id' => $value['membership_type_id']
633 $editedResults = array();
634 CRM_Price_BAO_FieldValue
::retrieve($editedFieldParams, $editedResults);
635 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']];
636 if (CRM_Utils_Array
::value('total_amount', $value)) {
637 $this->_priceSet
['fields'][$fid]['options'][$editedResults['id']]['amount'] = $value['total_amount'];
640 $fieldID = key($this->_priceSet
['fields']);
641 $value['price_' . $fieldID] = $editedResults['id'];
644 CRM_Price_BAO_Set
::processAmount($this->_priceSet
['fields'],
645 $value, $lineItem[$priceSetId]
648 //CRM-11529 for backoffice transactions
649 //when financial_type_id is passed in form, update the
650 //lineitems with the financial type selected in form
651 if (CRM_Utils_Array
::value('financial_type_id', $value) && CRM_Utils_Array
::value($priceSetId, $lineItem)) {
652 foreach ($lineItem[$priceSetId] as &$values) {
653 $values['financial_type_id'] = $value['financial_type_id'];
657 $value['lineItems'] = $lineItem;
658 $value['processPriceSet'] = TRUE;
660 // end of contribution related section
662 unset($value['membership_type']);
663 unset($value['membership_start_date']);
664 unset($value['membership_end_date']);
666 $value['is_renew'] = false;
667 if ( CRM_Utils_Array
::value('member_option', $params) && CRM_Utils_Array
::value( $key, $params['member_option'] ) == 2 ) {
668 $this->_params
= $params;
669 $value['is_renew'] = true;
670 $membership = CRM_Member_BAO_Membership
::renewMembership(
671 $value['contact_id'],
672 $value['membership_type_id'],
673 FALSE, $this, NULL, NULL,
677 // make contribution entry
678 CRM_Member_BAO_Membership
::recordMembershipContribution( $value, CRM_Core_DAO
::$_nullArray, $membership->id
);
681 $membership = CRM_Member_BAO_Membership
::create($value, CRM_Core_DAO
::$_nullArray);
685 if (CRM_Utils_Array
::value('product_name', $value)) {
686 if ($value['product_name'][0] > 0) {
687 list($products, $options) = CRM_Contribute_BAO_Premium
::getPremiumProductInfo();
689 $value['hidden_Premium'] = 1;
690 $value['product_option'] = CRM_Utils_Array
::value(
691 $value['product_name'][1],
692 $options[$value['product_name'][0]]
695 $premiumParams = array(
696 'product_id' => $value['product_name'][0],
697 'contribution_id' => $value['contribution_id'],
698 'product_option' => $value['product_option'],
701 CRM_Contribute_BAO_Contribution
::addPremium($premiumParams);
707 if ( $membership->id
&&
708 CRM_Utils_Array
::value( 'send_receipt', $value ) ) {
710 // add the domain email id
711 $domainEmail = CRM_Core_BAO_Domain
::getNameAndEmail();
712 $domainEmail = "$domainEmail[0] <$domainEmail[1]>";
714 $value['from_email_address'] = $domainEmail;
715 $value['membership_id'] = $membership->id
;
716 CRM_Member_Form_Membership
::emailReceipt( $this, $value, $membership );
723 * update contact information
725 * @param array $value associated array of submitted values
731 private function updateContactInfo(&$value) {
732 $value['preserveDBName'] = $this->_preserveDefault
;
734 //parse street address, CRM-7768
735 CRM_Contact_Form_Task_Batch
::parseStreetAddress($value, $this);
737 CRM_Contact_BAO_Contact
::createProfileContact($value, $this->_fields
,