|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Two.inc All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Two\Gateway\Block\Adminhtml\System\Config\Field; |
| 9 | + |
| 10 | +use Magento\Backend\Block\Template\Context; |
| 11 | +use Magento\Config\Block\System\Config\Form\Field; |
| 12 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 13 | +use Magento\Framework\Data\Form\Element\AbstractElement; |
| 14 | +use Two\Gateway\Api\Config\RepositoryInterface as ConfigRepository; |
| 15 | + |
| 16 | +/** |
| 17 | + * Renders a grid of surcharge inputs (fixed, percentage, limit) per payment term. |
| 18 | + * |
| 19 | + * Replaces the individual per-term surcharge fields with a compact table. |
| 20 | + * Reads available terms from the multiselect + custom duration config, |
| 21 | + * and the limits from RepositoryInterface constants (fork-friendly). |
| 22 | + */ |
| 23 | +class SurchargeGrid extends Field |
| 24 | +{ |
| 25 | + /** @var string */ |
| 26 | + protected $_template = 'Two_Gateway::system/config/field/surcharge-grid.phtml'; |
| 27 | + |
| 28 | + /** @var ScopeConfigInterface */ |
| 29 | + private $scopeConfig; |
| 30 | + |
| 31 | + /** @var string */ |
| 32 | + private $scope = 'default'; |
| 33 | + |
| 34 | + /** @var int */ |
| 35 | + private $scopeId = 0; |
| 36 | + |
| 37 | + public function __construct( |
| 38 | + Context $context, |
| 39 | + ScopeConfigInterface $scopeConfig, |
| 40 | + array $data = [] |
| 41 | + ) { |
| 42 | + parent::__construct($context, $data); |
| 43 | + $this->scopeConfig = $scopeConfig; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @inheritDoc |
| 48 | + */ |
| 49 | + public function render(AbstractElement $element): string |
| 50 | + { |
| 51 | + $this->resolveScope($element); |
| 52 | + $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue(); |
| 53 | + return parent::render($element); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @inheritDoc |
| 58 | + */ |
| 59 | + protected function _getElementHtml(AbstractElement $element): string |
| 60 | + { |
| 61 | + return $this->_toHtml(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Get the sorted list of active payment terms (standard + custom). |
| 66 | + */ |
| 67 | + public function getActiveTerms(): array |
| 68 | + { |
| 69 | + $selected = $this->getConfigValue(ConfigRepository::XML_PATH_PAYMENT_TERMS); |
| 70 | + $terms = array_filter(array_map('intval', explode(',', (string)$selected))); |
| 71 | + |
| 72 | + $custom = (int)$this->getConfigValue(ConfigRepository::XML_PATH_PAYMENT_TERMS_DURATION_DAYS); |
| 73 | + if ($custom > 0) { |
| 74 | + $terms[] = $custom; |
| 75 | + } |
| 76 | + |
| 77 | + $terms = array_unique($terms); |
| 78 | + sort($terms); |
| 79 | + return array_values($terms); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the saved surcharge value for a given term and field. |
| 84 | + */ |
| 85 | + public function getSavedValue(int $days, string $field): string |
| 86 | + { |
| 87 | + $path = sprintf('payment/two_payment/surcharge_%d_%s', $days, $field); |
| 88 | + $value = $this->getConfigValue($path); |
| 89 | + return $value !== null ? (string)$value : ''; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the default payment term (for differential mode highlighting). |
| 94 | + */ |
| 95 | + public function getDefaultTerm(): int |
| 96 | + { |
| 97 | + return (int)$this->getConfigValue(ConfigRepository::XML_PATH_DEFAULT_PAYMENT_TERM); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get the surcharge type (none, percentage, fixed, fixed_and_percentage). |
| 102 | + */ |
| 103 | + public function getSurchargeType(): string |
| 104 | + { |
| 105 | + return (string)$this->getConfigValue(ConfigRepository::XML_PATH_SURCHARGE_TYPE); |
| 106 | + } |
| 107 | + |
| 108 | + public function getMaxFixed(): int |
| 109 | + { |
| 110 | + return ConfigRepository::SURCHARGE_FIXED_MAX; |
| 111 | + } |
| 112 | + |
| 113 | + public function getMaxPercentage(): int |
| 114 | + { |
| 115 | + return ConfigRepository::SURCHARGE_PERCENTAGE_MAX; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the HTML field name for a surcharge input. |
| 120 | + * |
| 121 | + * Nests under the surcharge_grid field's value so the backend model receives it: |
| 122 | + * groups[payment_terms][fields][surcharge_grid][value][{days}][{field}] |
| 123 | + */ |
| 124 | + public function getFieldName(int $days, string $field): string |
| 125 | + { |
| 126 | + return sprintf( |
| 127 | + 'groups[payment_terms][fields][surcharge_grid][value][%d][%s]', |
| 128 | + $days, |
| 129 | + $field |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Get the "inherit" checkbox name for scope override. |
| 135 | + */ |
| 136 | + public function getInheritName(int $days, string $field): string |
| 137 | + { |
| 138 | + return sprintf( |
| 139 | + 'groups[payment_terms][fields][surcharge_grid][inherit][%d][%s]', |
| 140 | + $days, |
| 141 | + $field |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Check if a field is using the inherited (default/website) value at current scope. |
| 147 | + */ |
| 148 | + public function isInherited(int $days, string $field): bool |
| 149 | + { |
| 150 | + if ($this->scope === 'default') { |
| 151 | + return false; |
| 152 | + } |
| 153 | + $path = sprintf('payment/two_payment/surcharge_%d_%s', $days, $field); |
| 154 | + // Check if a value exists at this specific scope |
| 155 | + $value = $this->scopeConfig->getValue($path, $this->scope, $this->scopeId); |
| 156 | + $defaultValue = $this->scopeConfig->getValue($path); |
| 157 | + // If the scope-specific value equals the default, it's likely inherited |
| 158 | + // (Magento doesn't expose "is this overridden" directly for system config) |
| 159 | + return $value === $defaultValue; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Whether we're at a non-default scope (website or store). |
| 164 | + */ |
| 165 | + public function isNonDefaultScope(): bool |
| 166 | + { |
| 167 | + return $this->scope !== 'default'; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Available term constants (for JS to know which terms are standard). |
| 172 | + */ |
| 173 | + public function getAvailablePaymentTerms(): array |
| 174 | + { |
| 175 | + return ConfigRepository::AVAILABLE_PAYMENT_TERMS; |
| 176 | + } |
| 177 | + |
| 178 | + private function resolveScope(AbstractElement $element): void |
| 179 | + { |
| 180 | + $form = $element->getForm(); |
| 181 | + if ($form) { |
| 182 | + $scope = (string)$form->getScope(); |
| 183 | + $this->scope = ($scope !== '') ? $scope : 'default'; |
| 184 | + $this->scopeId = (int)$form->getScopeId(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private function getConfigValue(string $path) |
| 189 | + { |
| 190 | + if ($this->scope !== 'default') { |
| 191 | + return $this->scopeConfig->getValue($path, $this->scope, $this->scopeId); |
| 192 | + } |
| 193 | + return $this->scopeConfig->getValue($path); |
| 194 | + } |
| 195 | +} |
0 commit comments