Merge remote-tracking branch 'rajgo94/mailingui-new' into master-abtest
[civicrm-core.git] / CRM / Contribute / Form / Task / Invoice.php
index b7f9da96d8f0862ce3c43e4bc872f3076bfff503..e0a413841f99008072549905d4e32c77036d4af7 100644 (file)
@@ -84,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();
@@ -188,15 +196,15 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task {
       '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'),
           ),
         )
@@ -205,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'),
           ),
         )
@@ -248,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);
   }
 
   /**
@@ -257,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();
@@ -308,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) {
@@ -340,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) {
@@ -436,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,
@@ -468,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',
@@ -566,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();
       }
@@ -594,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,
@@ -654,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);
+  }
 }