Skip to content

Commit

Permalink
Merge pull request #979 from compucorp/BTHAB-145-create-contrib
Browse files Browse the repository at this point in the history
BTHAB-145: Add button to create invoice
  • Loading branch information
olayiwola-compucorp authored Sep 27, 2023
2 parents 33ebeab + 23d8404 commit b8be601
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 11 deletions.
31 changes: 29 additions & 2 deletions CRM/Civicase/Hook/BuildForm/AddEntityReferenceToCustomField.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Civi\Api4\CaseContact;
use CRM_Civicase_ExtensionUtil as E;

/**
Expand All @@ -20,13 +21,13 @@ public function run(CRM_Core_Form &$form, $formName) {
return;
}

$customFields[] = [
$customFields['case'] = [
'name' => CRM_Core_BAO_CustomField::getCustomFieldID('Case_Opportunity', 'Opportunity_Details', TRUE),
'entity' => 'Case',
'placeholder' => '- Select Case/Opportunity -',
];

$customFields[] = [
$customFields['salesOrder'] = [
'name' => CRM_Core_BAO_CustomField::getCustomFieldID('Quotation', 'Opportunity_Details', TRUE),
'entity' => 'CaseSalesOrder',
'placeholder' => '- Select Quotation -',
Expand All @@ -36,9 +37,35 @@ public function run(CRM_Core_Form &$form, $formName) {
'scriptFile' => [E::LONG_NAME, 'js/contribution-entityref-field.js'],
'region' => 'page-header',
]);

$this->populateDefaultFields($form, $customFields);
\Civi::resources()->addVars('civicase', ['entityRefCustomFields' => $customFields]);
}

/**
* Populates default fields.
*
* @param \CRM_Core_Form &$form
* Form Class object.
* @param array &$customFields
* Custom fields to set default value.
*/
private function populateDefaultFields(CRM_Core_Form &$form, array &$customFields) {
$caseId = CRM_Utils_Request::retrieve('caseId', 'Positive', $form);
if (!$caseId) {
return;
}

$customFields['case']['value'] = $caseId;
$caseClient = CaseContact::get()
->addSelect('contact_id')
->addWhere('case_id', '=', $caseId)
->execute()
->first()['contact_id'] ?? NULL;

$form->setDefaults(array_merge($form->_defaultValues, ['contact_id' => $caseClient]));
}

/**
* Checks if the hook should run.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<div id="bootstrap-theme" class="civicase__container">
<h1 crm-page-title>{{ ts('Quotation Invoices') }}</h1>

<a
ng-click="contributionURL()"
class="btn btn-primary civicase-workflow-list__new-button">
<i class="material-icons">add_circle</i>
{{:: ts('Create Invoice') }}
</a>

<div class="panel panel-default">
<div class="panel-body">
<afsearch-quotation-invoices></afsearch-quotation-invoices>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
* @param {object} $window window object of the browser
*/
function invoicesListController ($scope, $location, $window) {
$scope.contributionURL = async () => {
let url = CRM.url('/contribute/add?reset=1&action=add&context=standalone');
const caseId = $location.search().caseId;
if (caseId) {
url += `&caseId=${caseId}`;
}

$window.location.href = url;
};
}
})(angular, CRM._);
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,12 @@
return;
}

crmApi4('Relationship', 'get', {
select: ['contact_id_a'],
where: [['case_id', '=', $scope.defaultCaseId], ['relationship_type_id:name', '=', 'Case Coordinator is'], ['is_current', '=', true]],
limit: 1
}).then(function (relationships) {
if (Array.isArray(relationships) && relationships.length > 0) {
$scope.salesOrder.client_id = relationships[0].contact_id_a ?? null;
crmApi4('CaseContact', 'get', {
select: ['contact_id'],
where: [['case_id', '=', $scope.defaultCaseId]]
}).then(function (caseContacts) {
if (Array.isArray(caseContacts) && caseContacts.length > 0) {
$scope.salesOrder.client_id = caseContacts[0].contact_id ?? null;
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Redirects user to new quotation screen
*/
function redirectToQuotationCreationScreen () {
let url = '/civicrm/case-features/a#/quotations/new';
let url = CRM.url('/case-features/a#/quotations/new');
const caseId = $location.search().caseId;
if (caseId) {
url += `?caseId=${caseId}`;
Expand Down
6 changes: 5 additions & 1 deletion js/contribution-entityref-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
};

$(document).one('crmLoad', function () {
const entityRefCustomFields = CRM.vars.civicase.entityRefCustomFields ?? [];
const entityRefCustomFields = Object.values(CRM.vars.civicase.entityRefCustomFields ?? {});

/* eslint-disable no-undef */
waitForElement($, '#customData', function ($, elem) {
Expand All @@ -20,6 +20,10 @@
entity: field.entity,
create: false
});

if (field.value) {
$(`[name^=${field.name}_]`).val(field.value).trigger('change');
}
});
});
});
Expand Down

0 comments on commit b8be601

Please sign in to comment.