diff --git a/CRM/Civicase/Hook/BuildForm/AttachQuotationToInvoiceMail.php b/CRM/Civicase/Hook/BuildForm/AttachQuotationToInvoiceMail.php index 9c36c431b..ea9dd080a 100644 --- a/CRM/Civicase/Hook/BuildForm/AttachQuotationToInvoiceMail.php +++ b/CRM/Civicase/Hook/BuildForm/AttachQuotationToInvoiceMail.php @@ -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; } @@ -31,18 +31,23 @@ 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; } @@ -50,12 +55,12 @@ private function shouldRun($formName) { $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); } diff --git a/CRM/Civicase/Hook/alterMailParams/AttachQuotation.php b/CRM/Civicase/Hook/alterMailParams/AttachQuotation.php index dcf2d8ecf..e368a33ef 100644 --- a/CRM/Civicase/Hook/alterMailParams/AttachQuotation.php +++ b/CRM/Civicase/Hook/alterMailParams/AttachQuotation.php @@ -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; + } } /** diff --git a/CRM/Civicase/Service/CaseSalesOrderInvoice.php b/CRM/Civicase/Service/CaseSalesOrderInvoice.php index 57dfa46a2..426b2352a 100644 --- a/CRM/Civicase/Service/CaseSalesOrderInvoice.php +++ b/CRM/Civicase/Service/CaseSalesOrderInvoice.php @@ -13,7 +13,7 @@ class CRM_Civicase_Service_CaseSalesOrderInvoice { /** - * CreditNoteInvoiceService constructor. + * CaseSalesOrderInvoice constructor. * * @param \CRM_Civicase_WorkflowMessage_SalesOrderInvoice $template * Workflow template. diff --git a/CRM/Civicase/Upgrader/Steps/Step0019.php b/CRM/Civicase/Upgrader/Steps/Step0019.php index c89186996..14cfd30cf 100644 --- a/CRM/Civicase/Upgrader/Steps/Step0019.php +++ b/CRM/Civicase/Upgrader/Steps/Step0019.php @@ -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(); diff --git a/Civi/Api4/Action/CaseSalesOrder/SalesOrderSaveAction.php b/Civi/Api4/Action/CaseSalesOrder/SalesOrderSaveAction.php index c5918c126..9529b80e8 100644 --- a/Civi/Api4/Action/CaseSalesOrder/SalesOrderSaveAction.php +++ b/Civi/Api4/Action/CaseSalesOrder/SalesOrderSaveAction.php @@ -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(); @@ -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(); + } + } diff --git a/civicase.php b/civicase.php index a2eaae277..7d15c0fd1 100644 --- a/civicase.php +++ b/civicase.php @@ -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))); + } } /** @@ -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']); + } } /** @@ -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', + ]; + } } diff --git a/js/invoice-bulk-mail.js b/js/invoice-bulk-mail.js new file mode 100644 index 000000000..d63bb1bac --- /dev/null +++ b/js/invoice-bulk-mail.js @@ -0,0 +1,5 @@ +(function ($, _) { + $('input[value=email_invoice]').prop('checked', true).click(); + $('div.help').hide(); + $('input[name=output]').parent().parent().hide(); +})(CRM.$, CRM._); diff --git a/managed/SavedSearch_Civicase_Quotations.mgd.php b/managed/SavedSearch_Civicase_Quotations.mgd.php index df80a6b6c..1000f31fc 100644 --- a/managed/SavedSearch_Civicase_Quotations.mgd.php +++ b/managed/SavedSearch_Civicase_Quotations.mgd.php @@ -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' => [], ], ], diff --git a/templates/CRM/Civicase/Form/Contribute/AttachQuotation.tpl b/templates/CRM/Civicase/Form/Contribute/AttachQuotation.tpl index 82a949091..b84493606 100644 --- a/templates/CRM/Civicase/Form/Contribute/AttachQuotation.tpl +++ b/templates/CRM/Civicase/Form/Contribute/AttachQuotation.tpl @@ -4,11 +4,17 @@