Skip to content

Commit

Permalink
Refactor to aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Apr 26, 2024
1 parent f17e702 commit b2d1c59
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 32 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Added

- Added the ability to link customers in the plugin settings.
- Added the ability to define customer aliases in the plugin settings.

### Changed

Expand Down
4 changes: 2 additions & 2 deletions src/controllers/SlideoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class SlideoutController extends Controller
public function actionRender(): Response
{
$customer = $this->request->getRequiredParam('customer');
$linkedCustomers = PluginSales::$plugin->settings->getLinkedCustomers($customer);
$customerAliases = PluginSales::$plugin->settings->getCustomerAliases($customer);
$url = $this->getUrlFromCustomer($customer);
$sales = PluginSales::$plugin->reports->getSalesData($customer);

return $this->asCpScreen()
->contentTemplate('plugin-sales/_slideout', [
'customer' => $customer,
'linkedCustomers' => $linkedCustomers,
'customerAliases' => $customerAliases,
'url' => $url,
'sales' => $sales,
]);
Expand Down
16 changes: 8 additions & 8 deletions src/models/SettingsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,27 @@ class SettingsModel extends Model
/**
* @var array
*/
public array $linkedCustomers = [];
public array $customerAliases = [];

/**
* @var int
*/
public int $refreshSalesJobTtr = 3600;

/**
* Returns linked customers for the given customer.
* Returns customer aliases for the given customer.
*/
public function getLinkedCustomers(string $customer): array
public function getCustomerAliases(string $customer): array
{
$linkedCustomers = [];
$customerAliases = [];

foreach ($this->linkedCustomers as $linkedCustomer) {
if ($linkedCustomer['canonical'] === $customer) {
$linkedCustomers[] = $linkedCustomer['linked'];
foreach ($this->customerAliases as $customerAlias) {
if ($customerAlias['customer'] === $customer) {
$customerAliases[] = $customerAlias['alias'];
}
}

return $linkedCustomers;
return $customerAliases;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/services/SalesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function refresh(callable $setProgressHandler = null): bool|int
}
}

$this->updateLinkedCustomers();
$this->updateCustomerAliases();
$this->updateFirstSales();

$refreshRecord = new RefreshRecord();
Expand Down Expand Up @@ -327,20 +327,20 @@ private function saveSales(array $sales): int
}

/**
* Updates all linked customers.
* Updates all customer aliases.
*/
private function updateLinkedCustomers(): void
private function updateCustomerAliases(): void
{
$linkedCustomers = PluginSales::$plugin->settings->linkedCustomers;
$customerAliases = PluginSales::$plugin->settings->customerAliases;

foreach ($linkedCustomers as $linkedCustomer) {
foreach ($customerAliases as $customerAlias) {
Db::update(
SaleRecord::tableName(),
[
'customer' => $linkedCustomer['canonical'],
'customer' => $customerAlias['customer'],
],
[
'customer' => $linkedCustomer['linked'],
'customer' => $customerAlias['alias'],
]
);
}
Expand Down
8 changes: 4 additions & 4 deletions src/templates/_slideout.twig
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<h2>
{{ customer }}
{% if linkedCustomers %}
{% if customerAliases %}
<span class="info">
Linked to from
{% for linkedCustomer in linkedCustomers %}
<code>{{ linkedCustomer }}</code>
Customer aliases:
{% for customerAlias in customerAliases %}
<code>{{ customerAlias }}</code>
{{- not loop.last ? ',' }}
{% endfor %}
</span>
Expand Down
20 changes: 10 additions & 10 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,22 @@
} %}

{{ forms.editableTableField({
label: 'Linked Customers'|t('plugin-sales'),
instructions: 'Customers can be linked together, useful to ease the transition from Craft ID to Craft Console, which added support for organisations.'|t('plugin-sales'),
name: 'linkedCustomers',
id: 'linkedCustomers',
label: 'Customer Aliases'|t('plugin-sales'),
instructions: 'Customers can be assigned aliases, useful to ease the transition from Craft ID to Craft Console, which added support for organisations. Customer aliases will be replaced with the customer name on each refresh.'|t('plugin-sales'),
name: 'customerAliases',
id: 'customerAliases',
cols: {
canonical: {
customer: {
type: 'singleline',
heading: 'Canonical customer'|t('plugin-sales'),
heading: 'Customer'|t('plugin-sales'),
},
linked: {
alias: {
type: 'singleline',
heading: 'Linked customer'|t('plugin-sales'),
heading: 'Alias'|t('plugin-sales'),
},
},
rows: settings.linkedCustomers,
addRowLabel: 'Add a linked customer'|t('plugin-sales'),
rows: settings.customerAliases,
addRowLabel: 'Add customer alias'|t('plugin-sales'),
allowAdd: true,
allowDelete: true,
allowReorder: true,
Expand Down

0 comments on commit b2d1c59

Please sign in to comment.