Merge remote-tracking branch 'rajgo94/mailingui-new' into master-abtest
[civicrm-core.git] / CRM / Contribute / Form / Task / Invoice.php
index 757165e07631be9d63f1446e72aa6085063a5382..e0a413841f99008072549905d4e32c77036d4af7 100644 (file)
@@ -66,6 +66,11 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
    */
   public $_invoiceTemplate;
 
+  /**
+   * selected output
+   */
+  public $_selectedOutput;
+
   /**
    * build all the data structures needed to build the form
    *
@@ -79,6 +84,14 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       $this->_componentClause = " civicrm_contribution.id IN ( $id ) ";
       $this->_single = TRUE;
       $this->assign('totalSelectedContributions', 1);
+
+      // set the redirection after actions
+      $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, FALSE);
+      $url = CRM_Utils_System::url('civicrm/contact/view/contribution',
+        "action=view&reset=1&id={$id}&cid={$contactId}&context=contribution&selectedChild=contribute"
+      );
+
+      CRM_Core_Session::singleton()->pushUserContext($url);
     }
     else {
       parent::preProcess();
@@ -119,7 +132,11 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
     );
 
     CRM_Utils_System::appendBreadCrumb($breadCrumb);
-    if (in_array("email", $this->urlPath)) {
+
+    $this->_selectedOutput = CRM_Utils_Request::retrieve('select', 'String', $this);
+    $this->assign('selectedOutput', $this->_selectedOutput);
+
+    if ($this->_selectedOutput == 'email') {
       CRM_Utils_System::setTitle(ts('Email Invoice'));
     }
     else {
@@ -163,25 +180,31 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       $fromEmailAddress[$key] = htmlspecialchars($fromEmailAddress[$key]);
     }
     $fromEmail = CRM_Utils_Array::crmArrayMerge($emails, $fromEmailAddress);
-
-    $this->addElement('radio', 'output', NULL, ts('Email Invoice'), 'email_invoice');
-    $this->addElement('radio', 'output', NULL, ts('PDF Invoice'), 'pdf_invoice');
     $this->add('select', 'from_email_address', ts('From Email Address'), array('' => '- select -') + $fromEmail);
-    $this->addFormRule(array('CRM_Contribute_Form_Task_Invoice', 'formRule'));
+    if ($this->_selectedOutput != 'email') {
+      $this->addElement('radio', 'output', NULL, ts('Email Invoice'), 'email_invoice');
+      $this->addElement('radio', 'output', NULL, ts('PDF Invoice'), 'pdf_invoice');
+      $this->addRule('output', ts('Selection required'), 'required');
+      $this->addFormRule(array('CRM_Contribute_Form_Task_Invoice', 'formRule'));
+    }
+    else {
+      $this->addRule('from_email_address', ts('From Email Address is required'), 'required');
+    }
+
     $this->addWysiwyg('email_comment', ts('If you would like to add personal message to email please add it here. (If sending to more then one receipient the same message will be sent to each contact.)'), array(
       'rows' => 2,
       'cols' => 40
     ));
 
-    if (in_array("email", $this->urlPath)) {
+    if ($this->_selectedOutput == 'email') {
       $this->addButtons(array(
           array(
-            'type' => 'next',
+            'type' => 'upload',
             'name' => ts('Email Invoice'),
             'isDefault' => TRUE,
           ),
           array(
-            'type' => 'back',
+            'type' => 'cancel',
             'name' => ts('Cancel'),
           ),
         )
@@ -190,12 +213,12 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
     else {
       $this->addButtons(array(
           array(
-            'type' => 'next',
+            'type' => 'upload',
             'name' => ts('Process Invoice(s)'),
             'isDefault' => TRUE,
           ),
           array(
-            'type' => 'back',
+            'type' => 'cancel',
             'name' => ts('Cancel'),
           ),
         )
@@ -216,19 +239,11 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
    */
   static function formRule($values) {
     $errors = array();
-    if (!$values['from_email_address']) {
-      if (in_array("Email Invoice", $values)) {
-        $errors['from_email_address'] = ts("From Email Address is required");
-      }
-      else {
-        if (in_array("email_invoice", $values)) {
-          $errors['from_email_address'] = ts("From Email Address is required");
-        }
-        else {
-          $errors['output'] = ts("Selection required");
-        }
-      }
+
+    if ($values['output'] == 'email_invoice' && empty($values['from_email_address'])) {
+      $errors['from_email_address'] = ts("From Email Address is required");
     }
+
     return $errors;
   }
 
@@ -241,7 +256,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
    */
   public function postProcess() {
     $params = $this->controller->exportValues($this->_name);
-    $this->printPDF($this->_contributionIds, $params, $this->_contactIds);
+    $this->printPDF($this->_contributionIds, $params, $this->_contactIds, $this);
   }
 
   /**
@@ -250,12 +265,13 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
    * on click of Print Invoices
    *
    * @param array $contribIDs Contribution Id
-   * @param array $params for pdf or email invoices
+   * @param array $params associated array of submitted values
    * @param array $contactIds Contact Id
+   * @param object $form form object
    * @static
    *
    */
-  static function printPDF($contribIDs, $params, $contactIds) {
+  static function printPDF($contribIDs, &$params, $contactIds, &$form) {
     // get all the details needed to generate a invoice
     $messageInvoice = array();
     $invoiceTemplate = CRM_Core_Smarty::singleton();
@@ -301,7 +317,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       $addressParams = array('contact_id' => $contribution->contact_id);
       $addressDetails = CRM_Core_BAO_Address::getValues($addressParams);
 
-      // to get billing address if present 
+      // to get billing address if present
       $billingAddress = array();
       foreach ($addressDetails as $key => $address) {
         if ((isset($address['is_billing']) && $address['is_billing'] == 1) && (isset($address['is_primary']) && $address['is_primary'] == 1) && $address['contact_id'] == $contribution->contact_id) {
@@ -333,17 +349,18 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       if ($input['component'] == 'contribute') {
         $eid = $contribID;
         $etable = 'contribution';
+        $lineItem = CRM_Price_BAO_LineItem::getLineItems($eid, $etable, NULL, TRUE, TRUE);
       }
       else {
         $eid = $contribution->_relatedObjects['participant']->id;
         $etable = 'participant';
+        $lineItem = CRM_Price_BAO_LineItem::getLineItems($eid, $etable);
       }
 
-      //TO DO: Need to do changes for partially paid to display amount due on PDF invoice 
+      //TO DO: Need to do changes for partially paid to display amount due on PDF invoice
       $amountDue = ($input['amount'] - $input['amount']);
 
-      // retreiving the subtotal and sum of same tax_rate 
-      $lineItem = CRM_Price_BAO_LineItem::getLineItems($eid, $etable);
+      // retreiving the subtotal and sum of same tax_rate
       $dataArray = array();
       $subTotal = 0;
       foreach ($lineItem as $entity_id => $taxRate) {
@@ -429,7 +446,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
         'id' => $contribution->id,
         'source' => $source,
         'invoice_id' => $invoiceId,
-        'imageUploadURL' => $config->imageUploadURL,
+        'resourceBase' => $config->userFrameworkResourceURL,
         'defaultCurrency' => $config->defaultCurrency,
         'amount' => $contribution->total_amount,
         'amountDue' => $amountDue,
@@ -461,9 +478,11 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
         'domain_email' => CRM_Utils_Array::value('email', CRM_Utils_Array::value('1', $locationDefaults['email'])),
         'domain_phone' => CRM_Utils_Array::value('phone', CRM_Utils_Array::value('1', $locationDefaults['phone'])),
       );
+
       if (isset($creditNoteId)) {
         $tplParams['creditnote_id'] = $creditNoteId;
       }
+
       $sendTemplateParams = array(
         'groupName' => 'msg_tpl_workflow_contribution',
         'valueName' => 'contribution_invoice_receipt',
@@ -559,7 +578,7 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
         ));
         // functions call for adding activity with attachment
         $fileName = self::putFile($html);
-        self::addActivities($subject, $contactIds, $fileName, $params['output']);
+        self::addActivities($subject, $contactIds, $fileName, $params);
 
         CRM_Utils_System::civiExit();
       }
@@ -587,28 +606,30 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
    * @param string $subject Activity subject
    * @param array $contactIds Contact Id
    * @param string $fileName gives the location with name of the file
-   * @param array $output for invoices
+   * @param array $params for invoices
    *
    * @access public
    * @static
    */
-  static public function addActivities($subject, $contactIds, $fileName, $output) {
+  static public function addActivities($subject, $contactIds, $fileName, $params) {
     $session = CRM_Core_Session::singleton();
     $userID = $session->get('userID');
     $config = CRM_Core_Config::singleton();
     $config->doNotAttachPDFReceipt = 1;
-    if ($output['output'] == 'email_invoice' || (is_array($output) && array_key_exists("from_email_address", $output))) {
+
+    if (!empty($params['output']) && $params['output'] == 'pdf_invoice') {
       $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type',
-        'Emailed Invoice',
+        'Downloaded Invoice',
         'name'
       );
     }
-    else {
+    else{
       $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type',
-        'Downloaded Invoice',
+        'Emailed Invoice',
         'name'
       );
     }
+
     $activityParams = array(
       'subject' => $subject,
       'source_contact_id' => $userID,
@@ -647,5 +668,16 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
     file_put_contents($fileName, $html);
     return $fileName;
   }
+
+  /**
+   * Callback to perform action on Print Invoice button.
+   */
+  static function getPrintPDF() {
+    $contributionId = CRM_Utils_Request::retrieve('id', 'Positive', CRM_Core_DAO::$_nullObject, FALSE);
+    $contributionIDs = array($contributionId);
+    $contactId = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, FALSE);
+    $params = array('output' => 'pdf_invoice');
+    CRM_Contribute_Form_Task_Invoice::printPDF($contributionIDs, $params, $contactId, CRM_Core_DAO::$_nullObject);
+  }
 }