Skip to content

Commit

Permalink
PES-273: home delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Jiskra committed Feb 21, 2022
1 parent a76b477 commit 0cafe4f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGE_LOG.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
37 changes: 25 additions & 12 deletions media/admin/com_virtuemart/fields/vmzasilkovnacarriers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));
}
Expand All @@ -68,6 +81,6 @@ protected function getOptions() {
}
}

return $this->getOptionsForShipment($shipmentMethodId);
return $this->getOptionsForShipment($shipmentMethodId, true);
}
}
17 changes: 13 additions & 4 deletions media/admin/com_virtuemart/models/zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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 = [];
Expand All @@ -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))) {
Expand Down
1 change: 1 addition & 0 deletions media/admin/cs-CZ.plg_vmshipment_zasilkovna.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions media/admin/en-GB.plg_vmshipment_zasilkovna.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions media/admin/sk-SK.plg_vmshipment_zasilkovna.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 14 additions & 1 deletion zasilkovna.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 0cafe4f

Please sign in to comment.