diff --git a/CHANGE_LOG.txt b/CHANGE_LOG.txt index 0e6646e..91a5c78 100644 --- a/CHANGE_LOG.txt +++ b/CHANGE_LOG.txt @@ -1,3 +1,6 @@ +x.x.x - Added: home delivery support + - Updated: external pickup points are no longer available with internal pickup points + 1.4.0 - Added: manual trigger for carrier list update via Packeta API - Added: carrier v4 import diff --git a/media/admin/com_virtuemart/fields/vmzasilkovnacarriers.php b/media/admin/com_virtuemart/fields/vmzasilkovnacarriers.php index 0c400d3..7f63733 100644 --- a/media/admin/com_virtuemart/fields/vmzasilkovnacarriers.php +++ b/media/admin/com_virtuemart/fields/vmzasilkovnacarriers.php @@ -6,13 +6,8 @@ class JFormFieldVmZasilkovnaCarriers extends JFormFieldList { - /** - * Element name - * - * @access protected - * @var string - */ - var $type = 'vmZasilkovnaCarriers'; + /** @var string */ + protected $type = 'vmZasilkovnaCarriers'; /** @var \VirtueMartModelZasilkovna\Carrier\Repository */ private $carrierRepository; @@ -26,24 +21,42 @@ public function __construct($form = null) { $this->model = \VmModel::getModel('zasilkovna'); } - public function getFirstCarrierId($options) { + /** + * @param array $options + * @return string + */ + public function getFirstCarrierId(array $options) { $carrierOptionsKeys = array_keys($options); $carrierOptionsKey = array_shift($carrierOptionsKeys); return (string) $carrierOptionsKey; } - public function getOptionsForShipment($shipmentMethodId) { + /** + * @param int $shipmentMethodId + * @param bool $includePrompt + * @return array + */ + public function getOptionsForShipment($shipmentMethodId, $includePrompt = false) { $zasMethod = $this->model->getPacketeryShipmentMethod($shipmentMethodId); - return $this->getOptionsForPacketeryShipmentMethod($zasMethod); + return $this->getOptionsForPacketeryShipmentMethod($zasMethod, $includePrompt); } - public function getOptionsForPacketeryShipmentMethod($zasMethod) { + /** + * @param \VirtueMartModelZasilkovna\ShipmentMethod $zasMethod + * @param bool $includePrompt + * @return array + */ + public function getOptionsForPacketeryShipmentMethod(\VirtueMartModelZasilkovna\ShipmentMethod $zasMethod, $includePrompt = false) { $allowedCountryCodes = $this->model->getAllowedCountryCodes($zasMethod); $blockedCountryCodes = $this->model->getBlockedCountryCodes($zasMethod); $carriers = $this->carrierRepository->getAllActiveCarriers($allowedCountryCodes, $blockedCountryCodes); $fields = []; + if ($includePrompt) { + $fields[] = JHtml::_('select.option', '', JText::_('PLG_VMSHIPMENT_PACKETERY_SELECT_CARRIER')); + } + if ($this->model->hasPacketaPickupPointCountryCode($allowedCountryCodes, $blockedCountryCodes)) { $fields[\VirtueMartModelZasilkovna\Carrier\Repository::FORM_FIELD_PACKETA_PICKUP_POINTS] = JHtml::_('select.option', \VirtueMartModelZasilkovna\Carrier\Repository::FORM_FIELD_PACKETA_PICKUP_POINTS, JText::_('PLG_VMSHIPMENT_PACKETERY_PICKUP_POINT_OPTION_LABEL')); } @@ -68,6 +81,6 @@ protected function getOptions() { } } - return $this->getOptionsForShipment($shipmentMethodId); + return $this->getOptionsForShipment($shipmentMethodId, true); } } diff --git a/media/admin/com_virtuemart/models/zasilkovna.php b/media/admin/com_virtuemart/models/zasilkovna.php index 7c5a1bf..f0fe891 100644 --- a/media/admin/com_virtuemart/models/zasilkovna.php +++ b/media/admin/com_virtuemart/models/zasilkovna.php @@ -227,7 +227,7 @@ public function getPacketeryShipmentMethod($shipmentMethodId) { * @param \VirtueMartModelZasilkovna\ShipmentMethod $zasMethod * @return array */ - public function getAllowedCountryCodes($zasMethod) { + public function getAllowedCountryCodes(\VirtueMartModelZasilkovna\ShipmentMethod $zasMethod) { $allowedCountryIds = $zasMethod->getAllowedCountries(); $allowedCountryCodes = []; @@ -244,7 +244,7 @@ public function getAllowedCountryCodes($zasMethod) { * @param \VirtueMartModelZasilkovna\ShipmentMethod $zasMethod * @return array */ - public function getBlockedCountryCodes($zasMethod) { + public function getBlockedCountryCodes(\VirtueMartModelZasilkovna\ShipmentMethod $zasMethod) { $blockingCountries = $zasMethod->getBlockingCountries(); $blockedCountryCodes = []; @@ -263,8 +263,17 @@ public function getBlockedCountryCodes($zasMethod) { * @return bool */ public function hasPacketaPickupPointCountryCode(array $allowedCountryCodes, array $blockedCountryCodes) { - $baseCountries = ['CZ', 'SK', 'RO', 'HU']; - $baseCountriesDiff = array_diff($baseCountries, $blockedCountryCodes); + return $this->hasShipmentCountryCodes(['CZ', 'SK', 'RO', 'HU'], $allowedCountryCodes, $blockedCountryCodes); + } + + /** + * @param array $countryCodesToTest + * @param array $allowedCountryCodes + * @param array $blockedCountryCodes + * @return bool + */ + public function hasShipmentCountryCodes(array $countryCodesToTest, array $allowedCountryCodes, array $blockedCountryCodes) { + $baseCountriesDiff = array_diff($countryCodesToTest, $blockedCountryCodes); $packetaCountries = array_intersect($baseCountriesDiff, $allowedCountryCodes); if (!empty($packetaCountries) || (empty($allowedCountryCodes) && !empty($baseCountriesDiff))) { diff --git a/media/admin/cs-CZ.plg_vmshipment_zasilkovna.ini b/media/admin/cs-CZ.plg_vmshipment_zasilkovna.ini index 22ed139..c47e690 100644 --- a/media/admin/cs-CZ.plg_vmshipment_zasilkovna.ini +++ b/media/admin/cs-CZ.plg_vmshipment_zasilkovna.ini @@ -116,3 +116,4 @@ PLG_VMSHIPMENT_PACKETERY_DATETIME_FORMAT="d. m. Y H:i" PLG_VMSHIPMENT_PACKETERY_CARRIERS_UPDATED="Seznam dopravců byl aktualizován" PLG_VMSHIPMENT_PACKETERY_NEVER="Nikdy" PLG_VMSHIPMENT_PACKETERY_PICKUP_POINT_OPTION_LABEL="Zásilkovna - výdejní místa" +PLG_VMSHIPMENT_PACKETERY_SELECT_CARRIER="Zvolte dopravce" diff --git a/media/admin/en-GB.plg_vmshipment_zasilkovna.ini b/media/admin/en-GB.plg_vmshipment_zasilkovna.ini index edca625..f038745 100644 --- a/media/admin/en-GB.plg_vmshipment_zasilkovna.ini +++ b/media/admin/en-GB.plg_vmshipment_zasilkovna.ini @@ -116,3 +116,4 @@ PLG_VMSHIPMENT_PACKETERY_DATETIME_FORMAT="Y-m-d H:i" PLG_VMSHIPMENT_PACKETERY_CARRIERS_UPDATED="Carriers were updated" PLG_VMSHIPMENT_PACKETERY_NEVER="Never" PLG_VMSHIPMENT_PACKETERY_PICKUP_POINT_OPTION_LABEL="Packeta - pickup points" +PLG_VMSHIPMENT_PACKETERY_SELECT_CARRIER="Select carrier" diff --git a/media/admin/sk-SK.plg_vmshipment_zasilkovna.ini b/media/admin/sk-SK.plg_vmshipment_zasilkovna.ini index e0e9ca4..f2ef03a 100644 --- a/media/admin/sk-SK.plg_vmshipment_zasilkovna.ini +++ b/media/admin/sk-SK.plg_vmshipment_zasilkovna.ini @@ -116,3 +116,4 @@ PLG_VMSHIPMENT_PACKETERY_DATETIME_FORMAT="d. m. Y H:i" PLG_VMSHIPMENT_PACKETERY_CARRIERS_UPDATED="Seznam dopravců byl aktualizován" PLG_VMSHIPMENT_PACKETERY_NEVER="Never" PLG_VMSHIPMENT_PACKETERY_PICKUP_POINT_OPTION_LABEL="Zásilkovna - výdejní místa" +PLG_VMSHIPMENT_PACKETERY_SELECT_CARRIER="Zvolte dopravce" diff --git a/zasilkovna.php b/zasilkovna.php index b15cb5d..ff1714c 100644 --- a/zasilkovna.php +++ b/zasilkovna.php @@ -659,6 +659,19 @@ public function plgVmOnCheckoutCheckDataShipment(VirtueMartCart $cart) { return false; } + $selectedDestinationAddress = $this->getAddressFromCart($cart); + if(!$selectedDestinationAddress || !isset($selectedDestinationAddress['virtuemart_country_id'])) { + return false; + } + + $selectedCountryCode = ShopFunctions::getCountryByID($selectedDestinationAddress['virtuemart_country_id'], 'country_2_code'); + $allowedCountryCodes = $this->model->getAllowedCountryCodes($zasMethod); + $blockedCountryCodes = $this->model->getBlockedCountryCodes($zasMethod); + + if (!$this->model->hasShipmentCountryCodes([$selectedCountryCode], $allowedCountryCodes, $blockedCountryCodes)) { + return false; + } + return true; } @@ -1040,7 +1053,7 @@ function plgVmSetOnTablePluginShipment(&$data, &$table) $carrierFormFieldList = new \JFormFieldVmZasilkovnaCarriers(); $carrierOptions = $carrierFormFieldList->getOptionsForPacketeryShipmentMethod($method); if (empty($carrierOptions) || !isset($carrierOptions[$data[\VirtueMartModelZasilkovna\ShipmentMethod::CARRIER_ID]])) { - $data[\VirtueMartModelZasilkovna\ShipmentMethod::CARRIER_ID] = $carrierFormFieldList->getFirstCarrierId($carrierOptions); + $data[\VirtueMartModelZasilkovna\ShipmentMethod::CARRIER_ID] = ''; $data['published'] = '0'; vmWarn(JText::_('PLG_VMSHIPMENT_PACKETERY_SHIPPING_EMPTY_CARRIER_WARNING')); return;