Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTHAB-206: Add send invoice by email in bulk action menu for bulk invoice sending #978

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions CRM/Civicase/Hook/BuildForm/AttachQuotationToInvoiceMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CRM_Civicase_Hook_BuildForm_AttachQuotationToInvoiceMail {
* Form Name.
*/
public function run(CRM_Core_Form &$form, $formName) {
if (!$this->shouldRun($formName)) {
if (!$this->shouldRun($form, $formName)) {
return;
}

Expand All @@ -31,31 +31,36 @@ public function run(CRM_Core_Form &$form, $formName) {
/**
* Determines if the hook will run.
*
* @param CRM_Core_Form $form
* Form Class object.
* @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') {
private function shouldRun($form, $formName) {
if (!in_array($formName, [
'CRM_Contribute_Form_Task_Invoice',
'CRM_Invoicehelper_Contribute_Form_Task_Invoice',
])) {
return FALSE;
}

$contributionId = CRM_Utils_Request::retrieve('id', 'Positive');
$contributionId = CRM_Utils_Request::retrieve('id', 'Positive', $form, FALSE);
if (!$contributionId) {
return FALSE;
}

$salesOrder = Contribution::get()
->addSelect('Opportunity_Details.Quotation')
->addWhere('Opportunity_Details.Quotation', 'IS NOT EMPTY')
->addWhere('id', '=', $contributionId)
->addWhere('id', 'IN', explode(',', $contributionId))
->addChain('salesOrder', CaseSalesOrder::get()
->addWhere('id', '=', '$Opportunity_Details.Quotation')
)
->execute()
->first()['salesOrder'];
->getArrayCopy();

return !empty($salesOrder);
}
Expand Down
6 changes: 5 additions & 1 deletion CRM/Civicase/Hook/alterMailParams/AttachQuotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public function run(array &$params, $context) {

$rendered = $this->getContributionQuotationInvoice($params['tokenContext']['contributionId']);

$params['attachments'][] = CRM_Utils_Mail::appendPDF('quotation_invoice.pdf', $rendered['html'], $rendered['format']);
$attachment = CRM_Utils_Mail::appendPDF('quotation_invoice.pdf', $rendered['html'], $rendered['format']);

if ($attachment) {
$params['attachments'][] = $attachment;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion CRM/Civicase/Service/CaseSalesOrderInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class CRM_Civicase_Service_CaseSalesOrderInvoice {

/**
* CreditNoteInvoiceService constructor.
* CaseSalesOrderInvoice constructor.
*
* @param \CRM_Civicase_WorkflowMessage_SalesOrderInvoice $template
* Workflow template.
Expand Down
3 changes: 1 addition & 2 deletions CRM/Civicase/Upgrader/Steps/Step0019.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class CRM_Civicase_Upgrader_Steps_Step0019 {
*/
public function apply() {
try {
$upgrader = CRM_Civicase_Upgrader_Base::instance();
$upgrader->executeSqlFile('sql/auto_install.sql');
CRM_Utils_File::sourceSQLFile(CIVICRM_DSN, CRM_Civicase_ExtensionUtil::path('sql/auto_install.sql'));

(new QuotationTemplateManager())->create();
(new CaseTypeCategoryManager())->create();
Expand Down
14 changes: 14 additions & 0 deletions Civi/Api4/Action/CaseSalesOrder/SalesOrderSaveAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function _run(Result $result) { // phpcs:ignore
$this->matchExisting($record);
if (empty($record['id'])) {
$this->fillDefaults($record);
$this->fillMandatoryFields($record);
}
}
$this->validateValues();
Expand Down Expand Up @@ -129,4 +130,17 @@ private function updateOpportunityDetails($salesOrderId): void {
$caseSaleOrderContributionService->updateOpportunityFinancialDetails();
}

/**
* Fill mandatory fields.
*
* @param array $params
* Single Sales Order Record.
*/
protected function fillMandatoryFields(&$params) {
$saleOrderId = $params['id'] ?? NULL;
$caseSaleOrderContributionService = new \CRM_Civicase_Service_CaseSalesOrderContributionCalculator($saleOrderId);
$params['payment_status_id'] = $caseSaleOrderContributionService->calculateInvoicingStatus();
$params['invoicing_status_id'] = $caseSaleOrderContributionService->calculatePaymentStatus();
}

}
32 changes: 32 additions & 0 deletions civicase.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ function civicase_civicrm_buildForm($formName, &$form) {
if (!empty($_REQUEST['civicase_reload'])) {
$form->civicase_reload = json_decode($_REQUEST['civicase_reload'], TRUE);
}

$isSearchKit = CRM_Utils_Request::retrieve('sk', 'Positive');
if ($formName == 'CRM_Contribute_Form_Task_Invoice' && $isSearchKit) {
$form->add('hidden', 'mail_task_from_sk', $isSearchKit);
CRM_Core_Resources::singleton()->addScriptFile(
CRM_Civicase_ExtensionUtil::LONG_NAME,
'js/invoice-bulk-mail.js',
);
$form->setTitle(ts('Email Contribution Invoice'));
$ids = CRM_Utils_Request::retrieve('id', 'Positive', $form, FALSE);
$form->assign('totalSelectedContributions', count(explode(',', $ids)));
}
}

/**
Expand Down Expand Up @@ -296,6 +308,10 @@ function civicase_civicrm_postProcess($formName, &$form) {
$api = civicrm_api3('Case', 'getdetails', ['check_permissions' => 1] + $form->civicase_reload);
$form->ajaxResponse['civicase_reload'] = $api['values'];
}

if ($formName == 'CRM_Contribute_Form_Task_Invoice' && !empty($form->getVar('_submitValues')['mail_task_from_sk'])) {
CRM_Utils_System::redirect($_SERVER['HTTP_REFERER']);
}
}

/**
Expand Down Expand Up @@ -572,4 +588,20 @@ function civicase_civicrm_searchKitTasks(array &$tasks, bool $checkPermissions,
];

$tasks['CaseSalesOrder'] = $actions;

}

/**
* Implements hook_civicrm_searchTasks().
*/
function civicase_civicrm_searchTasks(string $objectName, array &$tasks) {
if ($objectName === 'contribution') {
$tasks['bulk_invoice'] = [
'title' => ts('Send Invoice by email'),
'class' => 'CRM_Contribute_Form_Task_Invoice',
'icon' => 'fa-paper-plane-o',
'url' => 'civicrm/contribute/task?reset=1&task_item=invoice&sk=1',
'key' => 'invoice',
];
}
}
5 changes: 5 additions & 0 deletions js/invoice-bulk-mail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(function ($, _) {
$('input[value=email_invoice]').prop('checked', true).click();
$('div.help').hide();
$('input[name=output]').parent().parent().hide();
})(CRM.$, CRM._);
2 changes: 1 addition & 1 deletion managed/SavedSearch_Civicase_Quotations.mgd.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@
'icon' => 'fa-paper-plane-o',
'text' => 'Send By Email',
'style' => 'default',
'path' => 'civicrm/contribute/invoice/email/?reset=1&id=[id]&select=email',
'path' => 'civicrm/contribute/invoice/email/?reset=1&id=[id]&select=email&cid=[contact_id]',
'condition' => [],
],
],
Expand Down
8 changes: 7 additions & 1 deletion templates/CRM/Civicase/Form/Contribute/AttachQuotation.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
<td class="html-adjust">{$form.attach_quote.html} <span>Yes</span></td>
</tr>
</table>
<div id="editMessageDetails"></div>

{literal}
<script type="text/javascript">
CRM.$(function ($) {
$('form.CRM_Contribute_Form_Task_Invoice > table.form-layout-compressed > tbody').append($('tr.attach-quote'))
if ($('#html_message').length) {
$('#html_message').parent().parent().after($('tr.attach-quote'))

return
}
$('#email_comment').parent().parent().after($('tr.attach-quote'))
})
</script>
{/literal}
Loading