Beispiel #1
0
 /**
  * Build the form object.
  */
 public function buildQuickForm()
 {
     // FIXME: This probably needs to be done in preprocess
     if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && $this->_action & CRM_Core_Action::UPDATE && CRM_Utils_Array::value('financial_type_id', $this->_values)) {
         $financialTypeID = CRM_Contribute_PseudoConstant::financialType($this->_values['financial_type_id']);
         CRM_Financial_BAO_FinancialType::checkPermissionedLineItems($this->_id, 'edit');
         if (!CRM_Core_Permission::check('edit contributions of type ' . $financialTypeID)) {
             CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
         }
     }
     $allPanes = array();
     $recurJs = NULL;
     //tax rate from financialType
     $this->assign('taxRates', json_encode(CRM_Core_PseudoConstant::getTaxRates()));
     $this->assign('currencies', json_encode(CRM_Core_OptionGroup::values('currencies_enabled')));
     // build price set form.
     $buildPriceSet = FALSE;
     $invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
     $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings);
     $this->assign('invoicing', $invoicing);
     // display tax amount on edit contribution page
     if ($invoicing && $this->_action & CRM_Core_Action::UPDATE && isset($this->_values['tax_amount'])) {
         $this->assign('totalTaxAmount', $this->_values['tax_amount']);
     }
     if (empty($this->_lineItems) && ($this->_priceSetId || !empty($_POST['price_set_id']))) {
         $buildPriceSet = TRUE;
         $getOnlyPriceSetElements = TRUE;
         if (!$this->_priceSetId) {
             $this->_priceSetId = $_POST['price_set_id'];
             $getOnlyPriceSetElements = FALSE;
         }
         $this->set('priceSetId', $this->_priceSetId);
         CRM_Price_BAO_PriceSet::buildPriceSet($this);
         // get only price set form elements.
         if ($getOnlyPriceSetElements) {
             return;
         }
     }
     // use to build form during form rule.
     $this->assign('buildPriceSet', $buildPriceSet);
     $defaults = $this->_values;
     $additionalDetailFields = array('note', 'thankyou_date', 'invoice_id', 'non_deductible_amount', 'fee_amount', 'net_amount');
     foreach ($additionalDetailFields as $key) {
         if (!empty($defaults[$key])) {
             $defaults['hidden_AdditionalDetail'] = 1;
             break;
         }
     }
     if ($this->_productDAO) {
         if ($this->_productDAO->product_id) {
             $defaults['hidden_Premium'] = 1;
         }
     }
     if ($this->_noteID && isset($this->_values['note'])) {
         $defaults['hidden_AdditionalDetail'] = 1;
     }
     $paneNames = array();
     if (empty($this->_payNow)) {
         $paneNames[ts('Additional Details')] = 'AdditionalDetail';
     }
     //Add Premium pane only if Premium is exists.
     $dao = new CRM_Contribute_DAO_Product();
     $dao->is_active = 1;
     if ($dao->find(TRUE) && empty($this->_payNow)) {
         $paneNames[ts('Premium Information')] = 'Premium';
     }
     if ($this->_mode) {
         if (CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE) == TRUE) {
             if (!empty($this->_recurPaymentProcessors)) {
                 $buildRecurBlock = TRUE;
                 if ($this->_ppID) {
                     // ppID denotes a pledge payment.
                     foreach ($this->_paymentProcessors as $processor) {
                         if (!empty($processor['is_recur']) && !empty($processor['object']) && $processor['object']->supports('recurContributionsForPledges')) {
                             $buildRecurBlock = TRUE;
                             break;
                         }
                         $buildRecurBlock = FALSE;
                     }
                 }
                 if ($buildRecurBlock) {
                     CRM_Contribute_Form_Contribution_Main::buildRecur($this);
                     $this->setDefaults(array('is_recur' => 0));
                     $this->assign('buildRecurBlock', TRUE);
                     $recurJs = array('onChange' => "buildRecurBlock( this.value ); return false;");
                 }
             }
         }
     }
     foreach ($paneNames as $name => $type) {
         $allPanes[$name] = $this->generatePane($type, $defaults);
     }
     $qfKey = $this->controller->_key;
     $this->assign('qfKey', $qfKey);
     $this->assign('allPanes', $allPanes);
     $this->addFormRule(array('CRM_Contribute_Form_Contribution', 'formRule'), $this);
     if ($this->_formType) {
         $this->assign('formType', $this->_formType);
         return;
     }
     $this->applyFilter('__ALL__', 'trim');
     if ($this->_action & CRM_Core_Action::DELETE) {
         $this->addButtons(array(array('type' => 'next', 'name' => ts('Delete'), 'spacing' => '         ', 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel'))));
         return;
     }
     //need to assign custom data type and subtype to the template
     $this->assign('customDataType', 'Contribution');
     $this->assign('customDataSubType', $this->_contributionType);
     $this->assign('entityID', $this->_id);
     if ($this->_context == 'standalone') {
         $this->addEntityRef('contact_id', ts('Contact'), array('create' => TRUE, 'api' => array('extra' => array('email'))), TRUE);
     }
     $attributes = CRM_Core_DAO::getAttribute('CRM_Contribute_DAO_Contribution');
     // Check permissions for financial type first
     CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, $this->_action);
     if (empty($financialTypes)) {
         CRM_Core_Error::statusBounce(ts('You do not have all the permissions needed for this page.'));
     }
     $financialType = $this->add('select', 'financial_type_id', ts('Financial Type'), array('' => ts('- select -')) + $financialTypes, TRUE, array('onChange' => "CRM.buildCustomData( 'Contribution', this.value );"));
     $paymentInstrument = FALSE;
     if (!$this->_mode) {
         $paymentInstrument = $this->add('select', 'payment_instrument_id', ts('Payment Method'), array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::paymentInstrument(), TRUE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);"));
     }
     $trxnId = $this->add('text', 'trxn_id', ts('Transaction ID'), array('class' => 'twelve') + $attributes['trxn_id']);
     //add receipt for offline contribution
     $this->addElement('checkbox', 'is_email_receipt', ts('Send Receipt?'));
     $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails);
     $status = CRM_Contribute_PseudoConstant::contributionStatus();
     // suppressing contribution statuses that are NOT relevant to pledges (CRM-5169)
     $statusName = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
     if ($this->_ppID) {
         foreach (array('Cancelled', 'Failed', 'In Progress') as $suppress) {
             unset($status[CRM_Utils_Array::key($suppress, $statusName)]);
         }
     } elseif (!$this->_ppID && $this->_id || !$this->_id) {
         $suppressFlag = FALSE;
         if ($this->_id) {
             $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id);
             if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) {
                 $suppressFlag = TRUE;
             }
         }
         if (!$suppressFlag) {
             foreach (array('Overdue', 'In Progress') as $suppress) {
                 unset($status[CRM_Utils_Array::key($suppress, $statusName)]);
             }
         } else {
             unset($status[CRM_Utils_Array::key('Overdue', $statusName)]);
         }
     }
     // define the status IDs that show the cancellation info, see CRM-17589
     $cancelInfo_show_ids = array();
     foreach (array_keys($statusName) as $status_id) {
         if (CRM_Contribute_BAO_Contribution::isContributionStatusNegative($status_id)) {
             $cancelInfo_show_ids[] = "'{$status_id}'";
         }
     }
     $this->assign('cancelInfo_show_ids', implode(',', $cancelInfo_show_ids));
     if ($this->_id) {
         $contributionStatus = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $this->_id, 'contribution_status_id');
         $name = CRM_Utils_Array::value($contributionStatus, $statusName);
         switch ($name) {
             case 'Completed':
                 // [CRM-17498] Removing unsupported status change options.
                 unset($status[CRM_Utils_Array::key('Pending', $statusName)]);
                 unset($status[CRM_Utils_Array::key('Failed', $statusName)]);
                 unset($status[CRM_Utils_Array::key('Partially paid', $statusName)]);
                 unset($status[CRM_Utils_Array::key('Pending refund', $statusName)]);
             case 'Cancelled':
             case 'Chargeback':
             case 'Refunded':
                 unset($status[CRM_Utils_Array::key('In Progress', $statusName)]);
                 unset($status[CRM_Utils_Array::key('Pending', $statusName)]);
                 unset($status[CRM_Utils_Array::key('Failed', $statusName)]);
                 break;
             case 'Pending':
             case 'In Progress':
                 unset($status[CRM_Utils_Array::key('Refunded', $statusName)]);
                 unset($status[CRM_Utils_Array::key('Chargeback', $statusName)]);
                 break;
             case 'Failed':
                 foreach (array('Pending', 'Refunded', 'Chargeback', 'Completed', 'In Progress', 'Cancelled') as $suppress) {
                     unset($status[CRM_Utils_Array::key($suppress, $statusName)]);
                 }
                 break;
         }
     } else {
         unset($status[CRM_Utils_Array::key('Refunded', $statusName)]);
         unset($status[CRM_Utils_Array::key('Chargeback', $statusName)]);
     }
     $statusElement = $this->add('select', 'contribution_status_id', ts('Contribution Status'), $status, FALSE);
     $currencyFreeze = FALSE;
     if (!empty($this->_payNow) && $this->_action & CRM_Core_Action::UPDATE) {
         $statusElement->freeze();
         $currencyFreeze = TRUE;
         $attributes['total_amount']['readonly'] = TRUE;
     }
     // CRM-16189, add Revenue Recognition Date
     if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
         $revenueDate = $this->add('date', 'revenue_recognition_date', ts('Revenue Recognition Date'), CRM_Core_SelectValues::date(NULL, 'M Y', NULL, 5));
         if ($this->_id && !CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($this->_id)) {
             $revenueDate->freeze();
         }
     }
     // add various dates
     $this->addDateTime('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
     if ($this->_online) {
         $this->assign('hideCalender', TRUE);
     }
     $checkNumber = $this->add('text', 'check_number', ts('Check Number'), $attributes['check_number']);
     $this->addDateTime('receipt_date', ts('Receipt Date'), FALSE, array('formatType' => 'activityDateTime'));
     $this->addDateTime('cancel_date', ts('Cancelled / Refunded Date'), FALSE, array('formatType' => 'activityDateTime'));
     $this->add('textarea', 'cancel_reason', ts('Cancellation / Refund Reason'), $attributes['cancel_reason']);
     $this->add('text', 'refund_trxn_id', ts('Transaction ID for the refund payment'));
     $element = $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, NULL, $recurJs);
     if ($this->_online) {
         $element->freeze();
     }
     $totalAmount = NULL;
     if (empty($this->_lineItems)) {
         $buildPriceSet = FALSE;
         $priceSets = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute');
         if (!empty($priceSets) && !$this->_ppID) {
             $buildPriceSet = TRUE;
         }
         // don't allow price set for contribution if it is related to participant, or if it is a pledge payment
         // and if we already have line items for that participant. CRM-5095
         if ($buildPriceSet && $this->_id) {
             $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id);
             $pledgePaymentId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $this->_id, 'id', 'contribution_id');
             if ($pledgePaymentId) {
                 $buildPriceSet = FALSE;
             }
             if ($participantID = CRM_Utils_Array::value('participant', $componentDetails)) {
                 $participantLI = CRM_Price_BAO_LineItem::getLineItems($participantID);
                 if (!CRM_Utils_System::isNull($participantLI)) {
                     $buildPriceSet = FALSE;
                 }
             }
         }
         $hasPriceSets = FALSE;
         if ($buildPriceSet) {
             $hasPriceSets = TRUE;
             // CRM-16451: set financial type of 'Price Set' in back office contribution
             // instead of selecting manually
             $financialTypeIds = CRM_Price_BAO_PriceSet::getAssoc(FALSE, 'CiviContribute', 'financial_type_id');
             $element = $this->add('select', 'price_set_id', ts('Choose price set'), array('' => ts('Choose price set')) + $priceSets, NULL, array('onchange' => "buildAmount( this.value, " . json_encode($financialTypeIds) . ");"));
             if ($this->_online && !($this->_action & CRM_Core_Action::UPDATE)) {
                 $element->freeze();
             }
         }
         $this->assign('hasPriceSets', $hasPriceSets);
         if (!($this->_action & CRM_Core_Action::UPDATE)) {
             if ($this->_online || $this->_ppID) {
                 $attributes['total_amount'] = array_merge($attributes['total_amount'], array('READONLY' => TRUE, 'style' => "background-color:#EBECE4"));
                 $optionTypes = array('1' => ts('Adjust Pledge Payment Schedule?'), '2' => ts('Adjust Total Pledge Amount?'));
                 $this->addRadio('option_type', NULL, $optionTypes, array(), '<br/>');
                 $currencyFreeze = TRUE;
             }
         }
         $totalAmount = $this->addMoney('total_amount', ts('Total Amount'), $hasPriceSets ? FALSE : TRUE, $attributes['total_amount'], TRUE, 'currency', NULL, $currencyFreeze);
     }
     $this->add('text', 'source', ts('Source'), CRM_Utils_Array::value('source', $attributes));
     // CRM-7362 --add campaigns.
     CRM_Campaign_BAO_Campaign::addCampaign($this, CRM_Utils_Array::value('campaign_id', $this->_values));
     if (empty($this->_payNow)) {
         CRM_Contribute_Form_SoftCredit::buildQuickForm($this);
     }
     $js = NULL;
     if (!$this->_mode) {
         $js = array('onclick' => "return verify( );");
     }
     $mailingInfo = Civi::settings()->get('mailing_backend');
     $this->assign('outBound_option', $mailingInfo['outBound_option']);
     $this->addButtons(array(array('type' => 'upload', 'name' => ts('Save'), 'js' => $js, 'isDefault' => TRUE), array('type' => 'upload', 'name' => ts('Save and New'), 'js' => $js, 'subName' => 'new'), array('type' => 'cancel', 'name' => ts('Cancel'))));
     // if status is Cancelled freeze Amount, Payment Instrument, Check #, Financial Type,
     // Net and Fee Amounts are frozen in AdditionalInfo::buildAdditionalDetail
     if ($this->_id && $this->_values['contribution_status_id'] == array_search('Cancelled', $statusName)) {
         if ($totalAmount) {
             $totalAmount->freeze();
         }
         $checkNumber->freeze();
         $paymentInstrument->freeze();
         $trxnId->freeze();
         $financialType->freeze();
     }
     // if contribution is related to membership or participant freeze Financial Type, Amount
     if ($this->_id && isset($this->_values['tax_amount'])) {
         $componentDetails = CRM_Contribute_BAO_Contribution::getComponentDetails($this->_id);
         if (CRM_Utils_Array::value('membership', $componentDetails) || CRM_Utils_Array::value('participant', $componentDetails)) {
             if ($totalAmount) {
                 $totalAmount->freeze();
             }
             $financialType->freeze();
             $this->assign('freezeFinancialType', TRUE);
         }
     }
     if ($this->_action & CRM_Core_Action::VIEW) {
         $this->freeze();
     }
 }
 /**
  * Test allowUpdateRevenueRecognitionDate.
  */
 public function testAllowUpdateRevenueRecognitionDate()
 {
     $contactId = $this->individualCreate();
     $params = array('contact_id' => $contactId, 'receive_date' => '2010-01-20', 'total_amount' => 100, 'financial_type_id' => 4);
     $order = $this->callAPISuccess('order', 'create', $params);
     $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
     $this->assertTrue($allowUpdate);
     $event = $this->eventCreate();
     $params = array('contact_id' => $contactId, 'receive_date' => '2010-01-20', 'total_amount' => 300, 'financial_type_id' => $this->getFinancialTypeId('Event Fee'), 'contribution_status_id' => 'Completed');
     $priceFields = $this->createPriceSet('event', $event['id']);
     foreach ($priceFields['values'] as $key => $priceField) {
         $lineItems[$key] = array('price_field_id' => $priceField['price_field_id'], 'price_field_value_id' => $priceField['id'], 'label' => $priceField['label'], 'field_title' => $priceField['label'], 'qty' => 1, 'unit_price' => $priceField['amount'], 'line_total' => $priceField['amount'], 'financial_type_id' => $priceField['financial_type_id'], 'entity_table' => 'civicrm_participant');
     }
     $params['line_items'][] = array('line_item' => $lineItems, 'params' => array('contact_id' => $contactId, 'event_id' => $event['id'], 'status_id' => 1, 'role_id' => 1, 'register_date' => '2007-07-21 00:00:00', 'source' => 'Online Event Registration: API Testing'));
     $order = $this->callAPISuccess('order', 'create', $params);
     $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
     $this->assertFalse($allowUpdate);
     $params = array('contact_id' => $contactId, 'receive_date' => '2010-01-20', 'total_amount' => 200, 'financial_type_id' => $this->getFinancialTypeId('Member Dues'), 'contribution_status_id' => 'Completed');
     $membershipType = $this->membershipTypeCreate();
     $priceFields = $this->createPriceSet();
     $lineItems = array();
     foreach ($priceFields['values'] as $key => $priceField) {
         $lineItems[$key] = array('price_field_id' => $priceField['price_field_id'], 'price_field_value_id' => $priceField['id'], 'label' => $priceField['label'], 'field_title' => $priceField['label'], 'qty' => 1, 'unit_price' => $priceField['amount'], 'line_total' => $priceField['amount'], 'financial_type_id' => $priceField['financial_type_id'], 'entity_table' => 'civicrm_membership', 'membership_type_id' => $membershipType);
     }
     $params['line_items'][] = array('line_item' => array(array_pop($lineItems)), 'params' => array('contact_id' => $contactId, 'membership_type_id' => $membershipType, 'join_date' => '2006-01-21', 'start_date' => '2006-01-21', 'end_date' => '2006-12-21', 'source' => 'Payment', 'is_override' => 1, 'status_id' => 1));
     $order = $this->callAPISuccess('order', 'create', $params);
     $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']);
     $this->assertFalse($allowUpdate);
 }