forked from civicrm/org.civicrm.civicase
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #976 from compucorp/BTHAB-182-quote-attach
BTHAB-182: Attach quotation invoice to contribution mail
- Loading branch information
Showing
6 changed files
with
303 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
CRM/Civicase/Hook/BuildForm/AttachQuotationToInvoiceMail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
use Civi\Api4\CaseSalesOrder; | ||
use Civi\Api4\Contribution; | ||
|
||
/** | ||
* Gives user the option to attach quotation to invoice mail. | ||
*/ | ||
class CRM_Civicase_Hook_BuildForm_AttachQuotationToInvoiceMail { | ||
|
||
/** | ||
* Adds the Attach Quote checkbox to invoice mail form. | ||
* | ||
* @param CRM_Core_Form $form | ||
* Form Class object. | ||
* @param string $formName | ||
* Form Name. | ||
*/ | ||
public function run(CRM_Core_Form &$form, $formName) { | ||
if (!$this->shouldRun($formName)) { | ||
return; | ||
} | ||
|
||
$form->add('checkbox', 'attach_quote', ts('Attach Quotation')); | ||
|
||
CRM_Core_Region::instance('page-body')->add([ | ||
'template' => "CRM/Civicase/Form/Contribute/AttachQuotation.tpl", | ||
]); | ||
} | ||
|
||
/** | ||
* Determines if the hook will run. | ||
* | ||
* @param string $formName | ||
* Form Name. | ||
* | ||
* @return bool | ||
* TRUE if the hook should run, FALSE otherwise. | ||
*/ | ||
private function shouldRun($formName) { | ||
if ($formName != 'CRM_Contribute_Form_Task_Invoice') { | ||
return FALSE; | ||
} | ||
|
||
$contributionId = CRM_Utils_Request::retrieve('id', 'Positive'); | ||
if (!$contributionId) { | ||
return FALSE; | ||
} | ||
|
||
$salesOrder = Contribution::get() | ||
->addSelect('Opportunity_Details.Quotation') | ||
->addWhere('Opportunity_Details.Quotation', 'IS NOT EMPTY') | ||
->addWhere('id', '=', $contributionId) | ||
->addChain('salesOrder', CaseSalesOrder::get() | ||
->addWhere('id', '=', '$Opportunity_Details.Quotation') | ||
) | ||
->execute() | ||
->first()['salesOrder']; | ||
|
||
return !empty($salesOrder); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
use Civi\Api4\CaseSalesOrder; | ||
use Civi\Api4\Contribution; | ||
|
||
/** | ||
* Adds Quotation invoice as an attachement. | ||
*/ | ||
class CRM_Civicase_Hook_alterMailParams_AttachQuotation { | ||
|
||
/** | ||
* Attaches quotation to single invoice. | ||
* | ||
* @param array $params | ||
* Mail parameters. | ||
* @param string $context | ||
* Mail context. | ||
*/ | ||
public function run(array &$params, $context) { | ||
$shouldAttachQuote = CRM_Utils_Request::retrieve('attach_quote', 'String'); | ||
|
||
if (!$this->shouldRun($params, $context, $shouldAttachQuote)) { | ||
return; | ||
} | ||
|
||
$rendered = $this->getContributionQuotationInvoice($params['tokenContext']['contributionId']); | ||
|
||
$params['attachments'][] = CRM_Utils_Mail::appendPDF('quotation_invoice.pdf', $rendered['html'], $rendered['format']); | ||
} | ||
|
||
/** | ||
* Renders the Invoice for the quotation linked to the contribution. | ||
* | ||
* @param int $contributionId | ||
* The contribution ID. | ||
*/ | ||
private function getContributionQuotationInvoice($contributionId) { | ||
$salesOrder = Contribution::get() | ||
->addSelect('Opportunity_Details.Quotation') | ||
->addWhere('Opportunity_Details.Quotation', 'IS NOT EMPTY') | ||
->addWhere('id', '=', $contributionId) | ||
->addChain('salesOrder', CaseSalesOrder::get() | ||
->addWhere('id', '=', '$Opportunity_Details.Quotation') | ||
) | ||
->execute() | ||
->first()['salesOrder']; | ||
|
||
if (empty($salesOrder)) { | ||
return; | ||
} | ||
|
||
/** @var \CRM_Civicase_Service_CaseSalesOrderInvoice */ | ||
$invoiceService = new \CRM_Civicase_Service_CaseSalesOrderInvoice(new CRM_Civicase_WorkflowMessage_SalesOrderInvoice()); | ||
return $invoiceService->render($salesOrder[0]['id']); | ||
} | ||
|
||
/** | ||
* Determines if the hook will run. | ||
* | ||
* @param array $params | ||
* Mail parameters. | ||
* @param string $context | ||
* Mail context. | ||
* @param string $shouldAttachQuote | ||
* If the Attach Quote is set. | ||
* | ||
* @return bool | ||
* returns TRUE if hook should run, FALSE otherwise. | ||
*/ | ||
private function shouldRun(array $params, $context, $shouldAttachQuote) { | ||
$component = $params['tplParams']['component'] ?? ''; | ||
if ($component !== 'contribute' || $context !== 'messageTemplate' || empty($shouldAttachQuote)) { | ||
return FALSE; | ||
} | ||
|
||
return TRUE; | ||
} | ||
|
||
} |
Oops, something went wrong.