Skip to content
Code-Schnipsel Gruppen Projekte
Handler.php 3,71 KiB
Newer Older
  • Learn to ignore specific revisions
  • Henning Leutz's avatar
    Henning Leutz committed
    <?php
    
    /**
     * This file contains QUI\ERP\Discount\Handler
     */
    namespace QUI\ERP\Discount;
    
    use QUI;
    
    use QUI\Permissions\Permission;
    
    Henning Leutz's avatar
    Henning Leutz committed
    
    /**
     * Class Handler
     *
     * @package QUI\ERP\Discount
     */
    class Handler extends QUI\CRUD\Factory
    {
    
        /**
         * discount type -> percent
         */
        const DISCOUNT_TYPE_PERCENT = 1;
    
        /**
         * discount type -> crrency
         */
        const DISCOUNT_TYPE_CURRENCY = 2;
    
    
        /**
         * discount scope -> discount is for every product
         */
        const DISCOUNT_SCOPE_EVERY_PRODUCT = 1;
    
        /**
         * discount scope -> discount is for all products (for the complete order)
         */
        const DISCOUNT_SCOPE_TOTAL = 2;
    
        /**
         * discount scope -> unique is for one product
         */
        const DISCOUNT_SCOPE_UNIQUE = 3;
    
    
        /**
         * pricefactor discount type
         */
        const DISCOUNT_PRICEFACTOR_TYPE = 'DISCOUNT_PRICE_FACTOR';
    
    
    Henning Leutz's avatar
    Henning Leutz committed
        /**
         * Handler constructor.
         */
        public function __construct()
        {
            parent::__construct();
    
            $this->Events->addEvent('onCreateBegin', function () {
                Permission::checkPermission('quiqqer.discount.create');
            });
    
            // create new translation var for the discount
    
            $this->Events->addEvent('onCreateEnd', function ($New, $data) {
    
    Henning Leutz's avatar
    Henning Leutz committed
                /* @var $New QUI\ERP\Discount\Discount */
                $newVar  = 'discount.' . $New->getId() . '.title';
                $current = QUI::getLocale()->getCurrent();
    
                $title = $New->getAttribute('title');
    
    
                if (!$title && isset($data['title'])) {
                    $title = QUI\Utils\Security\Orthos::clear($data['title']);
                }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                if (QUI::getLocale()->isLocaleString($title)) {
                    $parts = QUI::getLocale()->getPartsOfLocaleString($title);
                    $title = QUI::getLocale()->get($parts[0], $parts[1]);
                }
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                try {
                    QUI\Translator::addUserVar('quiqqer/discount', $newVar, array(
    
                        $current   => $title,
    
    Henning Leutz's avatar
    Henning Leutz committed
                        'datatype' => 'php,js'
                    ));
                } catch (QUI\Exception $Exception) {
                    QUI::getMessagesHandler()->addAttention(
                        $Exception->getMessage()
                    );
                }
    
    Henning Leutz's avatar
    Henning Leutz committed
            });
        }
    
        /**
         * return the discount db table name
         *
         * @return string
         */
        public function getDataBaseTableName()
        {
            return QUI::getDBTableName('discounts');
        }
    
        /**
         * Return the name of the child crud class
         *
         * @return string
         */
        public function getChildClass()
        {
            return 'QUI\ERP\Discount\Discount';
        }
    
        /**
         * Return the crud attributes for the children class
         *
         * @return array
         */
        public function getChildAttributes()
        {
            return array(
                'active',
                'discount',
    
                'discount_type',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'date_from',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'date_until',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'price_calculation_basis',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'purchase_quantity_from',
                'purchase_quantity_until',
                'purchase_value_from',
                'purchase_value_until',
                'areas',
                'articles',
                'categories',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'user_groups',
    
    Henning Leutz's avatar
    Henning Leutz committed
                'combined',
    
                'lastProductDiscount',
                'vat'
    
    Henning Leutz's avatar
    Henning Leutz committed
            );
        }
    
        /**
    
         * Return the children
         * If you want only the data, please use getChildrenData
    
    Henning Leutz's avatar
    Henning Leutz committed
         *
    
         * @param array $queryParams
         * @return array - [Child, Child, Child]
    
    Henning Leutz's avatar
    Henning Leutz committed
         */
    
        public function getChildrenData($queryParams = array())
    
    Henning Leutz's avatar
    Henning Leutz committed
        {
    
            if (!isset($queryParams['order'])) {
                $queryParams['order'] = 'priority ASC';
            }
    
            return parent::getChildrenData($queryParams);
    
    Henning Leutz's avatar
    Henning Leutz committed
        }
    }