Skip to content

Commit

Permalink
BTHAB-182: Use Invoice render service
Browse files Browse the repository at this point in the history
  • Loading branch information
olayiwola-compucorp committed Sep 21, 2023
1 parent 0f6bfbd commit 162e73d
Showing 1 changed file with 3 additions and 105 deletions.
108 changes: 3 additions & 105 deletions CRM/Civicase/Form/CaseSalesOrderInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,101 +159,10 @@ protected function getContactIds(): array {
*/
public static function getQuotationInvoice(): array {
$salesOrderId = CRM_Utils_Request::retrieveValue('id', 'Positive');
$caseSalesOrder = CaseSalesOrder::get()
->addWhere('id', '=', $salesOrderId)
->addChain('items', CaseSalesOrderLine::get()
->addWhere('sales_order_id', '=', '$id')
->addSelect('*', 'product_id.name', 'financial_type_id.name')
)
->addChain('computedRates', CaseSalesOrder::computeTotal()
->setLineItems('$items')
)
->addChain('client', Contact::get()
->addWhere('id', '=', '$client_id'), 0
)
->execute()
->first();

if (!empty($caseSalesOrder['client_id'])) {
$caseSalesOrder['clientAddress'] = Address::get()
->addSelect('*', 'country_id:label', 'state_province_id:label')
->addWhere('contact_id', '=', $caseSalesOrder['client_id'])
->execute()
->first();
$caseSalesOrder['clientAddress']['country'] = $caseSalesOrder['clientAddress']['country_id:label'];
$caseSalesOrder['clientAddress']['state'] = $caseSalesOrder['clientAddress']['state_province_id:label'];
}

$caseSalesOrder['taxRates'] = $caseSalesOrder['computedRates'][0]['taxRates'] ?? [];
$caseSalesOrder['quotation_date'] = date('Y-m-d', strtotime($caseSalesOrder['quotation_date']));

$domain = CRM_Core_BAO_Domain::getDomain();
$organisation = Contact::get()
->addSelect('image_URL')
->addWhere('id', '=', $domain->contact_id)
->execute()
->first();

$model = new CRM_Civicase_WorkflowMessage_SalesOrderInvoice();
$terms = self::getTerms();
$model->setDomainLogo($organisation['image_URL']);
$model->setSalesOrder($caseSalesOrder);
$model->setTerms($terms);
$model->setSalesOrderId($salesOrderId);
$model->setDomainLocation(self::getDomainLocation());
$model->setDomainName($domain->name ?? '');
$rendered = $model->renderTemplate();

$rendered['format'] = $rendered['format'] ?? self::defaultInvoiceFormat();

return $rendered;
}

/**
* Returns the Quotation invoice terms.
*/
private static function getTerms() {
$terms = NULL;
$invoicing = Setting::get()
->addSelect('invoicing')
->execute()
->first();

if (!empty($invoicing['value'])) {
$terms = Civi::settings()->get('quotations_notes');
}

return $terms;
}

/**
* Gets domain location.
*
* @return array
* An array of address lines.
*/
private static function getDomainLocation() {
$domain = CRM_Core_BAO_Domain::getDomain();
$locParams = ['contact_id' => $domain->contact_id];
$locationDefaults = CRM_Core_BAO_Location::getValues($locParams);
if (empty($locationDefaults['address'][1])) {
return [];
}
$stateProvinceId = $locationDefaults['address'][1]['state_province_id'] ?? NULL;
$stateProvinceAbbreviationDomain = !empty($stateProvinceId) ? CRM_Core_PseudoConstant::stateProvinceAbbreviation($stateProvinceId) : '';
$countryId = $locationDefaults['address'][1]['country_id'];
$countryDomain = !empty($countryId) ? CRM_Core_PseudoConstant::country($countryId) : '';

return [
'street_address' => CRM_Utils_Array::value('street_address', CRM_Utils_Array::value('1', $locationDefaults['address'])),
'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', CRM_Utils_Array::value('1', $locationDefaults['address'])),
'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', CRM_Utils_Array::value('1', $locationDefaults['address'])),
'supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', CRM_Utils_Array::value('1', $locationDefaults['address'])),
'city' => CRM_Utils_Array::value('city', CRM_Utils_Array::value('1', $locationDefaults['address'])),
'postal_code' => CRM_Utils_Array::value('postal_code', CRM_Utils_Array::value('1', $locationDefaults['address'])),
'state' => $stateProvinceAbbreviationDomain,
'country' => $countryDomain,
];
/** @var \CRM_Civicase_Service_CaseSalesOrderInvoice */
$invoiceService = new \CRM_Civicase_Service_CaseSalesOrderInvoice(new \CRM_Civicase_WorkflowMessage_SalesOrderInvoice());
return $invoiceService->render($salesOrderId);
}

/**
Expand All @@ -273,17 +182,6 @@ protected function getRows(): array {
return $rows;
}

/**
* Returns the default format to use for Invoice.
*/
private static function defaultInvoiceFormat() {
return [
'margin_top' => 10,
'margin_left' => 65,
'metric' => 'px',
];
}

/**
* Renders and return the generated PDF to the browser.
*/
Expand Down

0 comments on commit 162e73d

Please sign in to comment.