+ if (!empty($formValues['is_renew'])) {
+ $form->assign('receiptType', 'membership renewal');
+ }
+ else {
+ $form->assign('receiptType', 'membership signup');
+ }
+ $form->assign('receive_date', CRM_Utils_Date::processDate(CRM_Utils_Array::value('receive_date', $formValues)));
+ $form->assign('formValues', $formValues);
+
+ if (empty($lineItem)) {
+ $form->assign('mem_start_date', CRM_Utils_Date::customFormat($membership->start_date, '%B %E%f, %Y'));
+ if (!CRM_Utils_System::isNull($membership->end_date)) {
+ $form->assign('mem_end_date', CRM_Utils_Date::customFormat($membership->end_date, '%B %E%f, %Y'));
+ }
+ $form->assign('membership_name', CRM_Member_PseudoConstant::membershipType($membership->membership_type_id));
+ }
+
+ $isBatchProcess = is_a($form, 'CRM_Batch_Form_Entry');
+ if ((empty($form->_contributorDisplayName) || empty($form->_contributorEmail)) || $isBatchProcess) {
+ // in this case the form is being called statically from the batch editing screen
+ // having one class in the form layer call another statically is not greate
+ // & we should aim to move this function to the BAO layer in future.
+ // however, we can assume that the contact_id passed in by the batch
+ // function will be the recipient
+ list($form->_contributorDisplayName, $form->_contributorEmail)
+ = CRM_Contact_BAO_Contact_Location::getEmailDetails($formValues['contact_id']);
+ if (empty($form->_receiptContactId) || $isBatchProcess) {
+ $form->_receiptContactId = $formValues['contact_id'];
+ }
+ }
+ $template = CRM_Core_Smarty::singleton();
+ $taxAmt = $template->get_template_vars('dataArray');
+ $eventTaxAmt = $template->get_template_vars('totalTaxAmount');
+ $prefixValue = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings');
+ $invoicing = CRM_Utils_Array::value('invoicing', $prefixValue);
+ if ((!empty($taxAmt) || isset($eventTaxAmt)) && (isset($invoicing) && isset($prefixValue['is_email_pdf']))) {
+ $isEmailPdf = TRUE;
+ }
+ else {
+ $isEmailPdf = FALSE;
+ }
+
+ list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate(
+ array(
+ 'groupName' => 'msg_tpl_workflow_membership',
+ 'valueName' => 'membership_offline_receipt',
+ 'contactId' => $form->_receiptContactId,
+ 'from' => $receiptFrom,
+ 'toName' => $form->_contributorDisplayName,
+ 'toEmail' => $form->_contributorEmail,
+ 'PDFFilename' => ts('receipt') . '.pdf',
+ 'isEmailPdf' => $isEmailPdf,
+ 'contributionId' => $formValues['contribution_id'],
+ 'isTest' => (bool) ($form->_action & CRM_Core_Action::PREVIEW),
+ )
+ );
+
+ return TRUE;
+ }
+
+ /**
+ * Submit function.
+ *
+ * This is also accessed by unit tests.
+ *
+ * @param array $formValues
+ *
+ * @return array
+ */
+ public function submit($formValues) {
+ $isTest = ($this->_mode == 'test') ? 1 : 0;
+
+ $joinDate = $startDate = $endDate = NULL;
+ $membershipTypes = $membership = $calcDate = array();
+ $membershipType = NULL;
+
+ $mailSend = FALSE;
+ $priceSetID = CRM_Utils_Array::value('price_set_id', $formValues);
+
+ $params = $softParams = $ids = array();
+
+ $allMemberStatus = CRM_Member_PseudoConstant::membershipStatus();
+ $allContributionStatus = CRM_Contribute_PseudoConstant::contributionStatus();
+
+ if ($this->_id) {
+ $ids['membership'] = $params['id'] = $this->_id;
+ }
+ $ids['userId'] = CRM_Core_Session::singleton()->get('userID');
+
+ // Set variables that we normally get from context.
+ // In form mode these are set in preProcess.
+ //TODO: set memberships, fixme
+ $this->setContextVariables($formValues);
+ $priceSetDetails = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID);
+ $this->_memTypeSelected = self::getSelectedMemberships(
+ $priceSetDetails[$priceSetID],
+ $formValues
+ );
+ if (empty($formValues['financial_type_id'])) {
+ $formValues['financial_type_id'] = $priceSetDetails[$priceSetID]['financial_type_id'];
+ }
+
+ $config = CRM_Core_Config::singleton();
+
+ $this->convertDateFieldsToMySQL($formValues);
+
+ $membershipTypeValues = array();
+ foreach ($this->_memTypeSelected as $memType) {
+ $membershipTypeValues[$memType]['membership_type_id'] = $memType;
+ }
+
+ //take the required membership recur values.
+ if ($this->_mode && !empty($formValues['auto_renew'])) {
+ $params['is_recur'] = $this->_params['is_recur'] = $formValues['is_recur'] = TRUE;
+ $mapping = array(
+ 'frequency_interval' => 'duration_interval',
+ 'frequency_unit' => 'duration_unit',
+ );
+
+ $count = 0;
+ foreach ($this->_memTypeSelected as $memType) {
+ $recurMembershipTypeValues = CRM_Utils_Array::value($memType,
+ $this->_recurMembershipTypes, array()
+ );
+ foreach ($mapping as $mapVal => $mapParam) {
+ $membershipTypeValues[$memType][$mapVal] = CRM_Utils_Array::value($mapParam,
+ $recurMembershipTypeValues
+ );
+ if (!$count) {
+ $this->_params[$mapVal] = $formValues[$mapVal] = CRM_Utils_Array::value($mapParam,
+ $recurMembershipTypeValues
+ );
+ }
+ }
+ $count++;
+ }
+ }
+
+ // process price set and get total amount and line items.
+ if (!$priceSetID) {
+ $priceSetID = CRM_Member_BAO_Membership::createLineItems(
+ $this,
+ $formValues['membership_type_id'],
+ NULL
+ );
+ }
+ $isQuickConfig = FALSE;
+ if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetID, 'is_quick_config')) {
+ $isQuickConfig = 1;
+ }
+ $termsByType = array();
+
+ $lineItem = array();
+ CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'],
+ $this->_params, $lineItem[$priceSetID]);
+ if (CRM_Utils_Array::value('tax_amount', $this->_params)) {
+ $params['tax_amount'] = $this->_params['tax_amount'];
+ }
+ $params['total_amount'] = CRM_Utils_Array::value('amount', $this->_params);
+ $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $formValues);
+ if (!empty($lineItem[$priceSetID])) {
+ foreach ($lineItem[$priceSetID] as &$li) {
+ if (!empty($li['membership_type_id'])) {
+ if (!empty($li['membership_num_terms'])) {
+ $termsByType[$li['membership_type_id']] = $li['membership_num_terms'];
+ }
+ }
+
+ ///CRM-11529 for quick config backoffice transactions
+ //when financial_type_id is passed in form, update the
+ //lineitems with the financial type selected in form
+ if ($isQuickConfig && $submittedFinancialType) {
+ $li['financial_type_id'] = $submittedFinancialType;
+ }
+ }
+ }
+
+ $this->storeContactFields($formValues);
+
+ $params['contact_id'] = $this->_contactID;
+
+ $fields = array(
+ 'status_id',
+ 'source',
+ 'is_override',
+ 'campaign_id',
+ );
+
+ foreach ($fields as $f) {
+ $params[$f] = CRM_Utils_Array::value($f, $formValues);
+ }
+
+ // fix for CRM-3724
+ // when is_override false ignore is_admin statuses during membership
+ // status calculation. similarly we did fix for import in CRM-3570.
+ if (empty($params['is_override'])) {
+ $params['exclude_is_admin'] = TRUE;