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-257: Quotation and quotation invoice fix #987

Merged
merged 5 commits into from
Feb 6, 2024
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
29 changes: 16 additions & 13 deletions CRM/Civicase/Hook/BuildForm/AttachQuotationToInvoiceMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,21 @@ public function run(CRM_Core_Form &$form, $formName) {
return;
}

$form->add('checkbox', 'attach_quote', ts('Attach Quotation'));
$form->setDefaults(array_merge($form->_defaultValues, ['attach_quote' => TRUE]));
$contributionId = CRM_Utils_Request::retrieve('id', 'Positive', $form, FALSE);
$salesOrder = Contribution::get()
->addSelect('Opportunity_Details.Quotation')
->addWhere('Opportunity_Details.Quotation', 'IS NOT EMPTY')
->addWhere('id', 'IN', explode(',', $contributionId))
->addChain('salesOrder', CaseSalesOrder::get()
->addWhere('id', '=', '$Opportunity_Details.Quotation')
)
->execute()
->getArrayCopy();

if (!empty($salesOrder)) {
$form->add('checkbox', 'attach_quote', ts('Attach Quotation'));
$form->setDefaults(array_merge($form->_defaultValues, ['attach_quote' => TRUE]));
}

CRM_Core_Region::instance('page-body')->add([
'template' => "CRM/Civicase/Form/Contribute/AttachQuotation.tpl",
Expand Down Expand Up @@ -53,17 +66,7 @@ private function shouldRun($form, $formName) {
return FALSE;
}

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

return !empty($salesOrder);
return TRUE;
}

}
10 changes: 10 additions & 0 deletions Civi/Api4/CaseSalesOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ public static function computeTotalAmountInvoiced(bool $checkPermissions = FALSE
->setCheckPermissions($checkPermissions);
}

/**
* {@inheritDoc}
*/
public static function permissions() {
return [
'meta' => ['access CiviCRM'],
'default' => ['access CiviCRM'],
];
}

}
10 changes: 10 additions & 0 deletions Civi/Api4/CaseSalesOrderLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@
*/
class CaseSalesOrderLine extends DAOEntity {

/**
* {@inheritDoc}
*/
public static function permissions() {
return [
'meta' => ['access CiviCRM'],
'default' => ['access CiviCRM'],
];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,11 @@
};

const productId = $scope.salesOrder.items[index].product_id;
if (productsCache.has(productId)) {
updateProductDependentFields(productId);
return;
}

civicaseCrmApi('Product', 'get', { id: productId })
.then(function (result) {
if (result.count > 0) {
productsCache.set(productId, result.values[productId]);
updateProductDependentFields(productId);
}
});
getProduct(productId).then(function (result) {
if (result) {
updateProductDependentFields(productId);
}
});
}

/**
Expand All @@ -235,6 +228,9 @@
select: ['membership_type_id.Product_Discounts.Product_Discount_Amount'],
where: [['contact_id', '=', clientID], ['status_id.is_current_member', '=', true]]
}).then(function (results) {
if (!results || results.length < 1) {
return;
}
let discountPercentage = 0;
results.forEach((membership) => {
discountPercentage += membership['membership_type_id.Product_Discounts.Product_Discount_Amount'];
Expand Down Expand Up @@ -327,9 +323,14 @@
*
* @param {number} index index of the sales order line item
*/
function validateProductPrice (index) {
async function validateProductPrice (index) {
const productId = $scope.salesOrder.items[index].product_id;
const shouldCompareCost = productId && productsCache.has(productId) && parseFloat(productsCache.get(productId).cost) > 0;
if (!productId) {
return;
}

const product = await getProduct(productId);
const shouldCompareCost = product && product.cost > 0;
if (!shouldCompareCost) {
return;
}
Expand Down Expand Up @@ -416,5 +417,31 @@
function formatMoney (value, currency) {
return CRM.formatMoney(value, true, CurrencyCodes.getFormat(currency));
}

/**
* Get product by ID
*
* @param {number} productId id of product to fetch
*
* @returns {Promise<any>} product
*/
async function getProduct (productId) {
let product = null;
if (productsCache.has(productId)) {
return productsCache.get(productId);
}

try {
const result = await civicaseCrmApi('Product', 'get', { id: productId });
if (result.count > 0) {
product = result.values[productId];
productsCache.set(productId, product);
}
} catch (error) {
return null;
}

return product;
}
}
})(angular, CRM.$, CRM._);
17 changes: 16 additions & 1 deletion civicase.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ function civicase_civicrm_buildForm($formName, &$form) {
}

$isSearchKit = CRM_Utils_Request::retrieve('sk', 'Positive');
if ($formName == 'CRM_Contribute_Form_Task_PDF' && $isSearchKit) {
$form->add('hidden', 'mail_task_from_sk', $isSearchKit);
}

if ($formName == 'CRM_Contribute_Form_Task_Invoice' && $isSearchKit) {
$form->add('hidden', 'mail_task_from_sk', $isSearchKit);
CRM_Core_Resources::singleton()->addScriptFile(
Expand Down Expand Up @@ -309,7 +313,12 @@ function civicase_civicrm_postProcess($formName, &$form) {
$form->ajaxResponse['civicase_reload'] = $api['values'];
}

if ($formName == 'CRM_Contribute_Form_Task_Invoice' && !empty($form->getVar('_submitValues')['mail_task_from_sk'])) {
if (
in_array($formName, [
'CRM_Contribute_Form_Task_Invoice', 'CRM_Contribute_Form_Task_PDF',
])
&& !empty($form->getVar('_submitValues')['mail_task_from_sk'])
) {
CRM_Utils_System::redirect($_SERVER['HTTP_REFERER']);
}
}
Expand Down Expand Up @@ -603,5 +612,11 @@ function civicase_civicrm_searchTasks(string $objectName, array &$tasks) {
'url' => 'civicrm/contribute/task?reset=1&task_item=invoice&sk=1',
'key' => 'invoice',
];

foreach ($tasks as &$task) {
if ($task['class'] === 'CRM_Contribute_Form_Task_PDF') {
$task['url'] .= '&sk=1';
}
}
}
}
2 changes: 1 addition & 1 deletion templates/CRM/Civicase/Form/Contribute/AttachQuotation.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<table>
<tr class="crm-email-element attach-quote">
<td class="label">{$form.attach_quote.label}</td>
<td class="html-adjust">{$form.attach_quote.html} <span>Yes</span></td>
<td class="html-adjust">{$form.attach_quote.html} {if isset($form.attach_quote.html)}<span>Yes</span>{/if}</td>
</tr>
</table>
<div id="editMessageDetails"></div>
Expand Down
Loading