Towards CRM-20392 form cleanup, shared setting of ->mode
[civicrm-core.git] / CRM / Contribute / Form / AdditionalPayment.php
index 6543d9f9c82f16f7d24c40d113bcecb0690c5791..387144a170bf50c09b54c19c67f72abfe8ecc9f2 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.7                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2016                                |
+ | Copyright CiviCRM LLC (c) 2004-2017                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2016
+ * @copyright CiviCRM LLC (c) 2004-2017
  */
 
 /**
@@ -74,6 +74,8 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
   public $_action = NULL;
 
   public function preProcess() {
+
+    parent::preProcess();
     $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
     $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
     $this->_component = CRM_Utils_Request::retrieve('component', 'String', $this, TRUE);
@@ -130,9 +132,6 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       CRM_Core_Error::fatal(ts('No payment information found for this record'));
     }
 
-    //set the payment mode - _mode property is defined in parent class
-    $this->_mode = CRM_Utils_Request::retrieve('mode', 'String', $this);
-
     if (!empty($this->_mode) && $this->_paymentType == 'refund') {
       CRM_Core_Error::fatal(ts('Credit card payment is not for Refund payments use'));
     }
@@ -176,28 +175,12 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
     }
     $defaults = array();
     if ($this->_mode) {
-      $defaults = $this->_values;
-
-      $config = CRM_Core_Config::singleton();
-      // set default country from config if no country set
-      if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
-        $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry;
-      }
-
-      if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
-        $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince;
-      }
-
-      $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactId);
-      $defaults = array_merge($defaults, $billingDefaults);
+      CRM_Core_Payment_Form::setDefaultValues($this, $this->_contactId);
+      $defaults = array_merge($defaults, $this->_defaults);
     }
 
-    if (empty($defaults['trxn_date']) && empty($defaults['trxn_date_time'])) {
-      list($defaults['trxn_date'], $defaults['trxn_date_time'])
-        = CRM_Utils_Date::setDateDefaults(
-          CRM_Utils_Array::value('register_date', $defaults),
-          'activityDateTime'
-        );
+    if (empty($defaults['trxn_date'])) {
+      $defaults['trxn_date'] = date('Y-m-d H:i:s');
     }
 
     if ($this->_refund) {
@@ -209,6 +192,9 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
     return $defaults;
   }
 
+  /**
+   * Build the form object.
+   */
   public function buildQuickForm() {
     if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
       $this->addButtons(array(
@@ -222,56 +208,8 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
       );
       return;
     }
-    $ccPane = NULL;
-    if ($this->_mode) {
-      if (CRM_Utils_Array::value('payment_type', $this->_processors) & CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
-      ) {
-        $ccPane = array(ts('Direct Debit Information') => 'DirectDebit');
-      }
-      else {
-        $ccPane = array(ts('Credit Card Information') => 'CreditCard');
-      }
-      $defaults = $this->_values;
-      $showAdditionalInfo = FALSE;
-
-      foreach ($ccPane as $name => $type) {
-        if ($this->_formType == $type || !empty($_POST["hidden_{$type}"]) ||
-          CRM_Utils_Array::value("hidden_{$type}", $defaults)
-        ) {
-          $showAdditionalInfo = TRUE;
-          $allPanes[$name]['open'] = 'true';
-        }
 
-        $urlParams = "snippet=4&formType={$type}";
-        if ($this->_mode) {
-          $urlParams .= "&mode={$this->_mode}";
-        }
-        $open = 'false';
-        if ($type == 'CreditCard' ||
-          $type == 'DirectDebit'
-        ) {
-          $open = 'true';
-        }
-
-        $allPanes[$name] = array(
-          'url' => CRM_Utils_System::url('civicrm/payment/add', $urlParams),
-          'open' => $open,
-          'id' => $type,
-        );
-
-        CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE);
-
-        $qfKey = $this->controller->_key;
-        $this->assign('qfKey', $qfKey);
-        $this->assign('allPanes', $allPanes);
-        $this->assign('showAdditionalInfo', $showAdditionalInfo);
-
-        if ($this->_formType) {
-          $this->assign('formType', $this->_formType);
-          return;
-        }
-      }
-    }
+    CRM_Core_Payment_Form::buildPaymentForm($this, $this->_paymentProcessor, FALSE, TRUE);
     $attributes = CRM_Core_DAO::getAttribute('CRM_Financial_DAO_FinancialTrxn');
 
     $this->add('select', 'payment_processor_id', ts('Payment Processor'), $this->_processors, NULL);
@@ -299,9 +237,8 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
 
     $this->add('textarea', 'receipt_text', ts('Confirmation Message'));
 
-    // add various dates
     $dateLabel = ($this->_refund) ? ts('Refund Date') : ts('Date Received');
-    $this->addDateTime('trxn_date', $dateLabel, FALSE, array('formatType' => 'activityDateTime'));
+    $this->addField('trxn_date', array('entity' => 'FinancialTrxn', 'label' => $dateLabel, 'context' => 'Contribution'), FALSE, FALSE);
 
     if ($this->_contactId && $this->_id) {
       if ($this->_component == 'event') {
@@ -365,22 +302,39 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
     if ($self->_paymentType == 'refund' && $fields['total_amount'] != abs($self->_refund)) {
       $errors['total_amount'] = ts('Refund amount must equal refund due amount.');
     }
-    $netAmt = $fields['total_amount'] - $fields['fee_amount'];
+    $netAmt = $fields['total_amount'] - CRM_Utils_Array::value('fee_amount', $fields, 0);
     if (!empty($fields['net_amount']) && $netAmt != $fields['net_amount']) {
       $errors['net_amount'] = ts('Net amount should be equal to the difference between payment amount and fee amount.');
     }
     return $errors;
   }
 
+  /**
+   * Process the form submission.
+   */
   public function postProcess() {
-    $participantId = NULL;
+    $submittedValues = $this->controller->exportValues($this->_name);
+    $this->submit($submittedValues);
     $childTab = 'contribute';
     if ($this->_component == 'event') {
-      $participantId = $this->_id;
       $childTab = 'participant';
     }
-    $submittedValues = $this->controller->exportValues($this->_name);
-    $submittedValues['confirm_email_text'] = CRM_Utils_Array::value('receipt_text', $submittedValues);
+    $session = CRM_Core_Session::singleton();
+    $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
+      "reset=1&cid={$this->_contactId}&selectedChild={$childTab}"
+    ));
+  }
+
+  /**
+   * Process Payments.
+   * @param array $submittedValues
+   *
+   */
+  public function submit($submittedValues) {
+    $participantId = NULL;
+    if ($this->_component == 'event') {
+      $participantId = $this->_id;
+    }
     $contributionStatuses = CRM_Core_PseudoConstant::get('CRM_Contribute_DAO_Contribution',
       'contribution_status_id',
       array('labelColumn' => 'name')
@@ -395,56 +349,40 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
         )
       );
     }
-    $submittedValues['trxn_date'] = CRM_Utils_Date::processDate($submittedValues['trxn_date'], $submittedValues['trxn_date_time']);
+
     if ($this->_mode) {
       // process credit card
       $this->assign('contributeMode', 'direct');
       $this->processCreditCard($submittedValues);
       $submittedValues = $this->_params;
     }
-    else {
-      $defaults = array();
-      $contribution = civicrm_api3('Contribution', 'getsingle', array(
-        'return' => array("contribution_status_id"),
-        'id' => $this->_contributionId,
-      ));
-      $contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution);
-      $result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId);
-      // Fetch the contribution & do proportional line item assignment
-      $params = array('id' => $this->_contributionId);
-      $contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params);
-      CRM_Contribute_BAO_Contribution::addPayments(array($contribution), $contributionStatusId);
-
-      // email sending
-      if (!empty($result) && !empty($submittedValues['is_email_receipt'])) {
-        $submittedValues['contact_id'] = $this->_contactId;
-        $submittedValues['contribution_id'] = $this->_contributionId;
-
-        // to get 'from email id' for send receipt
-        $this->fromEmailId = $submittedValues['from_email_address'];
-        $sendReceipt = $this->emailReceipt($submittedValues);
-      }
 
-      $statusMsg = ts('The payment record has been processed.');
-      if (!empty($submittedValues['is_email_receipt']) && $sendReceipt) {
+    $defaults = array();
+    $contribution = civicrm_api3('Contribution', 'getsingle', array(
+      'return' => array("contribution_status_id"),
+      'id' => $this->_contributionId,
+    ));
+    $contributionStatusId = CRM_Utils_Array::value('contribution_status_id', $contribution);
+    $result = CRM_Contribute_BAO_Contribution::recordAdditionalPayment($this->_contributionId, $submittedValues, $this->_paymentType, $participantId);
+    // Fetch the contribution & do proportional line item assignment
+    $params = array('id' => $this->_contributionId);
+    $contribution = CRM_Contribute_BAO_Contribution::retrieve($params, $defaults, $params);
+    CRM_Contribute_BAO_Contribution::addPayments(array($contribution), $contributionStatusId);
+
+    $statusMsg = ts('The payment record has been processed.');
+    // send email
+    if (!empty($result) && !empty($submittedValues['is_email_receipt'])) {
+      $submittedValues['contact_id'] = $this->_contactId;
+      $submittedValues['contribution_id'] = $this->_contributionId;
+      // to get 'from email id' for send receipt
+      $this->fromEmailId = $submittedValues['from_email_address'];
+      $sendReceipt = $this->emailReceipt($submittedValues);
+      if ($sendReceipt) {
         $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.');
       }
-      // email sending
-      if (!empty($result) && !empty($submittedValues['is_email_receipt'])) {
-        $submittedValues['contact_id'] = $this->_contactId;
-        $submittedValues['contribution_id'] = $this->_contributionId;
-        // to get 'from email id' for send receipt
-        $this->fromEmailId = $submittedValues['from_email_address'];
-        $sendReceipt = $this->emailReceipt($submittedValues);
-      }
-
-      CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
-
-      $session = CRM_Core_Session::singleton();
-      $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view',
-        "reset=1&cid={$this->_contactId}&selectedChild={$childTab}"
-      ));
     }
+
+    CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
   }
 
   /**
@@ -542,7 +480,7 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract
     );
 
     if (!empty($this->_params['trxn_date'])) {
-      $this->_params['receive_date'] = CRM_Utils_Date::processDate($this->_params['trxn_date'], $this->_params['trxn_date_time']);
+      $this->_params['receive_date'] = $this->_params['trxn_date'];
     }
 
     if (empty($this->_params['invoice_id'])) {