diff --git a/database.xml b/database.xml index ce3abc12c9f69e6a2c19c50c44a931778a1c07e1..160fe6123cc01455b2b46fd6a73eb99e0859c459 100644 --- a/database.xml +++ b/database.xml @@ -7,6 +7,16 @@ <field type="INT(1) NOT NULL DEFAULT 0">active</field> <field type="VARCHAR(255) NOT NULL">shipping_type</field> <field type="VARCHAR(255) NOT NULL">icon</field> + <field type="TEXT">shipping_rules</field> + <field type="INT(4) NULL">priority</field> + + <primary>id</primary> + <auto_increment>id</auto_increment> + </table> + + <table name="shipping_rules"> + <field type="INT(11) NOT NULL">id</field> + <field type="INT(1) NOT NULL DEFAULT 0">active</field> <field type="text NULL">date_from</field> <field type="text NULL">date_until</field> <field type="INT(10) NULL DEFAULT 0">purchase_quantity_from</field> diff --git a/locale.xml b/locale.xml index 5a2b317545b87506e70b41d770cc94c31986028e..a0ff14573359febadb85887ef997fd2d375a0b16 100644 --- a/locale.xml +++ b/locale.xml @@ -42,16 +42,16 @@ </locale> <locale name="shipping.standard.title"> - <de><![CDATA[Standard Versand Methode]]></de> - <en><![CDATA[Standard shipping type]]></en> + <de><![CDATA[Standard Versand]]></de> + <en><![CDATA[Standard shipping]]></en> </locale> <locale name="shipping.free.description"> - <de><![CDATA[Standard Versand Methode]]></de> - <en><![CDATA[Standard shipping type]]></en> + <de><![CDATA[Standard Versand]]></de> + <en><![CDATA[Standard shipping]]></en> </locale> <locale name="shipping.free.workingTitle"> - <de><![CDATA[Standard Versand Methode]]></de> - <en><![CDATA[Standard shipping type]]></en> + <de><![CDATA[Standard Versand]]></de> + <en><![CDATA[Standard shipping]]></en> </locale> </groups> @@ -106,6 +106,10 @@ <groups name="quiqqer/shipping" datatype="js"> + <locale name="shipping.type"> + <de><![CDATA[Versand Art]]></de> + <en><![CDATA[Shipping type]]></en> + </locale> <locale name="shipping.edit.template.title"> <de><![CDATA[Titel]]></de> <en><![CDATA[Title]]></en> diff --git a/src/QUI/ERP/Shipping/Api/AbstractShippingType.php b/src/QUI/ERP/Shipping/Api/AbstractShippingType.php new file mode 100644 index 0000000000000000000000000000000000000000..5945831c40fd841fc8a837e426b1c31331d8651f --- /dev/null +++ b/src/QUI/ERP/Shipping/Api/AbstractShippingType.php @@ -0,0 +1,122 @@ +<?php + +/** + * This file contains \QUI\ERP\Shipping\Api\AbstractShippingEntry + */ + +namespace QUI\ERP\Shipping\Api; + +use QUI; + +/** + * Shipping abstract class + * This is the parent shipping class for all shipping methods + * + * @author www.pcsg.de (Henning Leutz) + */ +abstract class AbstractShippingType extends QUI\QDOM implements QUI\ERP\Shipping\Api\ShippingTypeInterface +{ + /** + * @return string + */ + public function getType() + { + return \get_class($this); + } + + /** + * @param QUI\Interfaces\Users\User $User + * @return bool + */ + public function canUsedBy(QUI\Interfaces\Users\User $User) + { + return true; + } + + /** + * @param QUI\Locale|null $Locale + * @return array + */ + public function toArray($Locale = null) + { + if ($Locale === null) { + $Locale = QUI::getLocale(); + } + + return [ + 'title' => $this->getTitle($Locale), + 'description' => $this->getDescription($Locale), + 'workingTitle' => $this->getWorkingTitle($Locale), + 'shippingType' => $this->getType(), + 'icon' => $this->getIcon() + ]; + } + + /** + * @param $Locale + * @return array|string + */ + public function getName($Locale = null) + { + $ShippingType = $this->getShipping(); + + if ($Locale !== null) { + $ShippingType->setLocale($Locale); + } + + return $ShippingType->getTitle(); + } + + /** + * @param $Locale + * @return array|string + */ + public function getTitle($Locale = null) + { + $ShippingType = $this->getShipping(); + + if ($Locale !== null) { + $ShippingType->setLocale($Locale); + } + + return $ShippingType->getTitle(); + } + + /** + * @param $Locale + * @return array|string + */ + public function getWorkingTitle($Locale = null) + { + $ShippingType = $this->getShipping(); + + if ($Locale !== null) { + $ShippingType->setLocale($Locale); + } + + return $ShippingType->getWorkingTitle(); + } + + /** + * @param $Locale + * @return array|string + */ + public function getDescription($Locale = null) + { + $ShippingType = $this->getShipping(); + + if ($Locale !== null) { + $ShippingType->setLocale($Locale); + } + + return $ShippingType->getDescription(); + } + + /** + * @return string + */ + public function getIcon() + { + return $this->getShipping()->getIcon(); + } +} diff --git a/src/QUI/ERP/Shipping/Api/ShippingTypeInterface.php b/src/QUI/ERP/Shipping/Api/ShippingTypeInterface.php index a3b827a7873f76298d9f6cd07ce8a80f65c4196b..19e1861f4b230d5733257135ea99da73f8d38fe0 100644 --- a/src/QUI/ERP/Shipping/Api/ShippingTypeInterface.php +++ b/src/QUI/ERP/Shipping/Api/ShippingTypeInterface.php @@ -47,7 +47,7 @@ public function toArray($Locale = null); /** * @return \QUI\ERP\Shipping\Api\AbstractShippingEntry */ - public function getShippingType(); + public function getShipping(); /** * @param QUI\Interfaces\Users\User $User diff --git a/src/QUI/ERP/Shipping/Methods/Standard/Shipping.php b/src/QUI/ERP/Shipping/Methods/Standard/Shipping.php index 9f4a22de867c776874a26f3c15d62d7130ed4156..73577a8687cbdd2d515de9f4a36e93abd9732c4c 100644 --- a/src/QUI/ERP/Shipping/Methods/Standard/Shipping.php +++ b/src/QUI/ERP/Shipping/Methods/Standard/Shipping.php @@ -16,11 +16,6 @@ */ class Shipping extends QUI\ERP\Shipping\Api\AbstractShippingEntry { - /** - * Standard shipping id - */ - const ID = -1; - /** * @return array|string */ diff --git a/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php b/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php index b0d4296d13da81052b10d0356053af762e851c34..95d83992c5a879d086b99675b6586bb2b58994b0 100644 --- a/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php +++ b/src/QUI/ERP/Shipping/Methods/Standard/ShippingType.php @@ -19,7 +19,7 @@ class ShippingType extends QUI\ERP\Shipping\Api\AbstractShippingType /** * @return Shipping */ - public function getShippingType() + public function getShipping() { return new Shipping(); } diff --git a/src/QUI/ERP/Shipping/Order/Shipping.php b/src/QUI/ERP/Shipping/Order/Shipping.php index 06def33b38a696a166668c45d34a0e639063d379..a4cd1869e7752e8097a69149818fc78d10f57757 100644 --- a/src/QUI/ERP/Shipping/Order/Shipping.php +++ b/src/QUI/ERP/Shipping/Order/Shipping.php @@ -24,7 +24,7 @@ public function __construct($attributes = []) { parent::__construct($attributes); - $this->addCSSFile(dirname(__FILE__).'/Shipping.css'); + $this->addCSSFile(\dirname(__FILE__).'/Shipping.css'); } /** @@ -78,7 +78,7 @@ public function getBody() 'shippingList' => $shippingList ]); - return $Engine->fetch(dirname(__FILE__).'/Shipping.html'); + return $Engine->fetch(\dirname(__FILE__).'/Shipping.html'); } /** diff --git a/src/QUI/ERP/Shipping/Rules/Factory.php b/src/QUI/ERP/Shipping/Rules/Factory.php new file mode 100644 index 0000000000000000000000000000000000000000..c20cb7927bfd5c430ee622d5df1d1778040483b7 --- /dev/null +++ b/src/QUI/ERP/Shipping/Rules/Factory.php @@ -0,0 +1,164 @@ +<?php + +/** + * This file contains QUI\ERP\Shipping\Rules\Factory + */ + +namespace QUI\ERP\Shipping\Rules; + +use QUI; +use QUI\Permissions\Permission; + +/** + * Class Factory + * + * @package QUI\ERP\Shipping\Types + */ +class Factory extends QUI\CRUD\Factory +{ + /** + * Handler constructor. + */ + public function __construct() + { + parent::__construct(); + + $self = $this; + + $this->Events->addEvent('onCreateBegin', function () { + Permission::checkPermission('quiqqer.shipping.rule.create'); + }); + + // create new translation var for the area + $this->Events->addEvent('onCreateEnd', function () use ($self) { + QUI\Translator::publish('quiqqer/shipping'); + }); + } + + /** + * @param array $data + * + * @return ShippingRule + * + * @throws QUI\Exception + */ + public function createChild($data = []) + { + if (!isset($data['active']) || !\is_integer($data['active'])) { + $data['active'] = 0; + } + + if (!isset($data['purchase_quantity_from']) || !\is_integer($data['purchase_quantity_from'])) { + $data['purchase_quantity_from'] = 0; + } + + if (!isset($data['purchase_quantity_until']) || !\is_integer($data['purchase_quantity_until'])) { + $data['purchase_quantity_until'] = 0; + } + + if (!isset($data['priority']) || !\is_integer($data['priority'])) { + $data['priority'] = 0; + } + + QUI::getEvents()->fireEvent('shippingRuleCreateBegin', [$data]); + + /* @var $NewChild ShippingRule */ + $NewChild = parent::createChild($data); + + $this->createShippingLocale( + 'shipping.'.$NewChild->getId().'.title', + '' + ); + + try { + QUI\Translator::publish('quiqqer/shipping'); + } catch (QUI\Exception $Exception) { + QUI\System\Log::writeException($Exception); + } + + QUI::getEvents()->fireEvent('shippingCreateEnd', [$NewChild]); + + return $NewChild; + } + + /** + * @return string + */ + public function getDataBaseTableName() + { + return 'shipping_rules'; + } + + /** + * @return string + */ + public function getChildClass() + { + return ShippingRule::class; + } + + /** + * @return array + */ + public function getChildAttributes() + { + return [ + 'id', + 'active', + + 'date_from', + 'date_until', + 'purchase_quantity_from', + 'purchase_quantity_until', + 'purchase_value_from', + 'purchase_value_until', + 'priority', + + 'areas', + 'articles', + 'categories', + 'user_groups' + ]; + } + + /** + * @param int $id + * + * @return QUI\ERP\Shipping\Api\AbstractShippingEntry + * + * @throws QUI\Exception + */ + public function getChild($id) + { + /* @var QUI\ERP\Shipping\Api\AbstractShippingEntry $Shipping */ + $Shipping = parent::getChild($id); + + return $Shipping; + } + + /** + * Creates a locale + * + * @param $var + * @param $title + */ + protected function createShippingLocale($var, $title) + { + $current = QUI::getLocale()->getCurrent(); + + if (QUI::getLocale()->isLocaleString($title)) { + $parts = QUI::getLocale()->getPartsOfLocaleString($title); + $title = QUI::getLocale()->get($parts[0], $parts[1]); + } + + try { + QUI\Translator::addUserVar('quiqqer/shipping', $var, [ + $current => $title, + 'datatype' => 'php,js', + 'package' => 'quiqqer/shipping' + ]); + } catch (QUI\Exception $Exception) { + QUI\System\Log::addNotice($Exception->getMessage()); + } + } +} diff --git a/src/QUI/ERP/Shipping/Rules/ShippingRule.php b/src/QUI/ERP/Shipping/Rules/ShippingRule.php new file mode 100644 index 0000000000000000000000000000000000000000..7afa2db2e8d438be5f735d68ddcb71cb7d24dc1e --- /dev/null +++ b/src/QUI/ERP/Shipping/Rules/ShippingRule.php @@ -0,0 +1,186 @@ +<?php + +/** + * This file contains QUI\ERP\Shipping\Rules\ShippingRule + */ + +namespace QUI\ERP\Shipping\Rules; + +use QUI; +use QUI\CRUD\Factory; +use QUI\Permissions\Permission; + +use QUI\ERP\Areas\Utils as AreaUtils; +use QUI\ERP\Shipping\Exceptions\ShippingCanNotBeUsed; + +/** + * Class ShippingEntry + * A user created shipping entry + * + * @package QUI\ERP\Shipping\Types + */ +class ShippingRule extends QUI\CRUD\Child +{ + /** + * Shipping constructor. + * + * @param int $id + * @param Factory $Factory + */ + public function __construct($id, Factory $Factory) + { + parent::__construct($id, $Factory); + + $this->Events->addEvent('onDeleteBegin', function () { + Permission::checkPermission('quiqqer.shipping.delete'); + }); + + $this->Events->addEvent('onSaveBegin', function () { + Permission::checkPermission('quiqqer.shipping.edit'); + }); + } + + /** + * is the user allowed to use this shipping + * + * @param QUI\Interfaces\Users\User $User + * @return boolean + */ + public function canUsedBy(QUI\Interfaces\Users\User $User) + { + if ($this->isActive() === false) { + return false; + } + + try { + QUI::getEvents()->fireEvent('quiqqerShippingCanUsedBy', [$this, $User]); + } catch (ShippingCanNotBeUsed $Exception) { + return false; + } catch (QUI\Exception $Exception) { + QUI\System\Log::writeDebugException($Exception); + + return false; + } + + + // usage definitions / limits + $dateFrom = $this->getAttribute('date_from'); + $dateUntil = $this->getAttribute('date_until'); + $now = \time(); + + if ($dateFrom && \strtotime($dateFrom) > $now) { + return false; + } + + if ($dateUntil && \strtotime($dateUntil) < $now) { + return false; + } + + // assignment + $userGroupValue = $this->getAttribute('user_groups'); + $areasValue = $this->getAttribute('areas'); + + // if groups and areas are empty, everybody is allowed + if (empty($userGroupValue) && empty($areasValue)) { + return true; + } + + // not in area + $areasValue = \explode(',', $areasValue); + + if (!empty($areasValue) && !AreaUtils::isUserInAreas($User, $areasValue)) { + return false; + } + + $userGroups = QUI\Utils\UserGroups::parseUsersGroupsString( + $this->getAttribute('user_groups') + ); + + $discountUsers = $userGroups['users']; + $discountGroups = $userGroups['groups']; + + if (empty($discountUsers) && empty($discountGroups)) { + return true; + } + + // user checking + foreach ($discountUsers as $uid) { + if ($User->getId() == $uid) { + return true; + } + } + + // group checking + $groupsOfUser = $User->getGroups(); + + /* @var $Group QUI\Groups\Group */ + foreach ($discountGroups as $gid) { + foreach ($groupsOfUser as $Group) { + if ($Group->getId() == $gid) { + return true; + } + } + } + + return false; + } + + /** + * is the shipping allowed in the order? + * + * @param QUI\ERP\Order\OrderInterface $Order + * @return bool + */ + public function canUsedInOrder(QUI\ERP\Order\OrderInterface $Order) + { + try { + QUI::getEvents()->fireEvent('shippingCanUsedInOrder', [$this, $Order]); + } catch (ShippingCanNotBeUsed $Exception) { + return false; + } catch (QUI\Exception $Exception) { + QUI\System\Log::addDebug($Exception->getMessage()); + + return false; + } + + return true; + } + + // region activation / deactivation + + /** + * Activate the shipping type + * + * @throws QUI\ExceptionStack|QUI\Exception + */ + public function activate() + { + $this->setAttribute('active', 1); + $this->update(); + $this->refresh(); + } + + /** + * Is the shipping active? + * + * @return bool + */ + public function isActive() + { + return !!$this->getAttribute('active'); + } + + /** + * Deactivate the shipping type + * + * @throws QUI\ExceptionStack|QUI\Exception + */ + public function deactivate() + { + $this->setAttribute('active', 0); + $this->update(); + $this->refresh(); + } + + //endregion +} diff --git a/src/QUI/ERP/Shipping/Shipping.php b/src/QUI/ERP/Shipping/Shipping.php index 10eaeae19442fc37044b09fdf8f57f4ba997b418..60081fd482e6ae9be1020e34187535d9ad088831 100644 --- a/src/QUI/ERP/Shipping/Shipping.php +++ b/src/QUI/ERP/Shipping/Shipping.php @@ -144,10 +144,6 @@ public function getShippingType($shippingType) */ public function getShippingEntry($shippingId) { - if ((int)$shippingId == Methods\Free\Shipping::ID) { - return new Methods\Free\Shipping(); - } - try { return Factory::getInstance()->getChild($shippingId); } catch (QUI\Exception $Exception) { diff --git a/src/QUI/ERP/Shipping/Types/Factory.php b/src/QUI/ERP/Shipping/Types/Factory.php index bbfa815617347bd41166ba6ec09e841dc4b2ee56..d4c044e6939fbc191a1b5f71714cdebbbf85debb 100644 --- a/src/QUI/ERP/Shipping/Types/Factory.php +++ b/src/QUI/ERP/Shipping/Types/Factory.php @@ -45,23 +45,23 @@ public function __construct() */ public function createChild($data = []) { - if (!isset($data['active']) || !is_integer($data['active'])) { + if (!isset($data['active']) || !\is_integer($data['active'])) { $data['active'] = 0; } - if (!isset($data['purchase_quantity_from']) || !is_integer($data['purchase_quantity_from'])) { - $data['purchase_quantity_from'] = 0; - } - - if (!isset($data['purchase_quantity_until']) || !is_integer($data['purchase_quantity_until'])) { - $data['purchase_quantity_until'] = 0; - } +// if (!isset($data['purchase_quantity_from']) || !\is_integer($data['purchase_quantity_from'])) { +// $data['purchase_quantity_from'] = 0; +// } +// +// if (!isset($data['purchase_quantity_until']) || !\is_integer($data['purchase_quantity_until'])) { +// $data['purchase_quantity_until'] = 0; +// } - if (!isset($data['priority']) || !is_integer($data['priority'])) { + if (!isset($data['priority']) || !\is_integer($data['priority'])) { $data['priority'] = 0; } - if (!isset($data['shipping_type']) || !class_exists($data['shipping_type'])) { + if (!isset($data['shipping_type']) || !\class_exists($data['shipping_type'])) { throw new QUI\ERP\Shipping\Exception([ 'quiqqer/shipping', 'exception.create.shipping.class.not.found' @@ -75,12 +75,12 @@ public function createChild($data = []) $this->createShippingLocale( 'shipping.'.$NewChild->getId().'.title', - '[quiqqer/shipping] new.shipping.placeholder' + $NewChild->getShippingType()->getTitle() ); $this->createShippingLocale( 'shipping.'.$NewChild->getId().'.workingTitle', - '[quiqqer/shipping] new.shipping.placeholder' + $NewChild->getShippingType()->getTitle().' - '.$NewChild->getId() ); $this->createShippingLocale( @@ -125,19 +125,8 @@ public function getChildAttributes() 'shipping_type', 'active', 'icon', - - 'date_from', - 'date_until', - 'purchase_quantity_from', - 'purchase_quantity_until', - 'purchase_value_from', - 'purchase_value_until', - 'priority', - - 'areas', - 'articles', - 'categories', - 'user_groups' + 'shipping_rules', + 'priority' ]; } diff --git a/src/QUI/ERP/Shipping/Types/ShippingEntry.php b/src/QUI/ERP/Shipping/Types/ShippingEntry.php index e239059a44f3a4a8c58d4adfbc5d19a9963dd5c9..65bd54fae3a0689b151f920b967a3b5f3bcdfb41 100644 --- a/src/QUI/ERP/Shipping/Types/ShippingEntry.php +++ b/src/QUI/ERP/Shipping/Types/ShippingEntry.php @@ -106,14 +106,14 @@ public function toArray() /** * Return the shipping type of the type * - * @return Api\AbstractShippingEntry + * @return Api\ShippingTypeInterface * @throws QUI\ERP\Shipping\Exception */ public function getShippingType() { $type = $this->getAttribute('shipping_type'); - if (!class_exists($type)) { + if (!\class_exists($type)) { throw new QUI\ERP\Shipping\Exception([ 'quiqqer/shipping', 'exception.shipping.type.not.found', @@ -123,7 +123,7 @@ public function getShippingType() $Type = new $type(); - if (!($Type instanceof Api\AbstractShippingEntry)) { + if (!($Type instanceof Api\ShippingTypeInterface)) { throw new QUI\ERP\Shipping\Exception([ 'quiqqer/shipping', 'exception.shipping.type.not.abstractShipping', @@ -142,89 +142,7 @@ public function getShippingType() */ public function canUsedBy(QUI\Interfaces\Users\User $User) { - if ($this->isActive() === false) { - return false; - } - - try { - QUI::getEvents()->fireEvent('quiqqerShippingCanUsedBy', [$this, $User]); - } catch (ShippingCanNotBeUsed $Exception) { - return false; - } catch (QUI\Exception $Exception) { - QUI\System\Log::writeDebugException($Exception); - - return false; - } - - - try { - $this->getShippingType(); - } catch (QUI\Exception $Exception) { - QUI\System\Log::writeException($Exception); - - return false; - } - - // usage definitions / limits - $dateFrom = $this->getAttribute('date_from'); - $dateUntil = $this->getAttribute('date_until'); - $now = time(); - - if ($dateFrom && strtotime($dateFrom) > $now) { - return false; - } - - if ($dateUntil && strtotime($dateUntil) < $now) { - return false; - } - - // assignment - $userGroupValue = $this->getAttribute('user_groups'); - $areasValue = $this->getAttribute('areas'); - - // if groups and areas are empty, everybody is allowed - if (empty($userGroupValue) && empty($areasValue)) { - return true; - } - - // not in area - $areasValue = explode(',', $areasValue); - - if (!empty($areasValue) && !AreaUtils::isUserInAreas($User, $areasValue)) { - return false; - } - - $userGroups = QUI\Utils\UserGroups::parseUsersGroupsString( - $this->getAttribute('user_groups') - ); - - $discountUsers = $userGroups['users']; - $discountGroups = $userGroups['groups']; - - if (empty($discountUsers) && empty($discountGroups)) { - return true; - } - - // user checking - foreach ($discountUsers as $uid) { - if ($User->getId() == $uid) { - return true; - } - } - - // group checking - $groupsOfUser = $User->getGroups(); - - /* @var $Group QUI\Groups\Group */ - foreach ($discountGroups as $gid) { - foreach ($groupsOfUser as $Group) { - if ($Group->getId() == $gid) { - return true; - } - } - } - - return false; + // @todo check shipping rules } /**