diff --git a/ajax/activate.php b/ajax/activate.php new file mode 100644 index 0000000000000000000000000000000000000000..05f3390c5b964be137008789ddd700a2b76af0ad --- /dev/null +++ b/ajax/activate.php @@ -0,0 +1,25 @@ +<?php + +/** + * This file contains package_quiqqer_discount_ajax_activate + */ + +/** + * Activate a discount + * + * @param integer $discountId - Discount-ID + */ +QUI::$Ajax->registerFunction( + 'package_quiqqer_discount_ajax_activate', + function ($discountId) { + $Handler = new QUI\ERP\Discount\Handler(); + $Discount = $Handler->getChild($discountId); + + $Discount->setAttribute('active', 1); + $Discount->update(); + + return $Discount->isActive(); + }, + array('discountId'), + 'Permission::checkAdminUser' +); diff --git a/ajax/create.php b/ajax/create.php index 821d80f0b8cc64f302d4d4dc47c81e2257a21048..9858366cdec1ff3598f0776e2256e12822bfdba5 100644 --- a/ajax/create.php +++ b/ajax/create.php @@ -14,7 +14,12 @@ function ($params) { $params = json_decode($params, true); $Discounts = new QUI\ERP\Discount\Handler(); - $Discount = $Discounts->createChild($params); + + if (!isset($params['active'])) { + $params['active'] = 0; + } + + $Discount = $Discounts->createChild($params); return $Discount->getId(); }, diff --git a/ajax/deactivate.php b/ajax/deactivate.php new file mode 100644 index 0000000000000000000000000000000000000000..23c7c6507d36140eb840a600a4353401c6eb35ca --- /dev/null +++ b/ajax/deactivate.php @@ -0,0 +1,25 @@ +<?php + +/** + * This file contains package_quiqqer_discount_ajax_deactivate + */ + +/** + * Deactivate a discount + * + * @param integer $discountId - Discount-ID + */ +QUI::$Ajax->registerFunction( + 'package_quiqqer_discount_ajax_deactivate', + function ($discountId) { + $Handler = new QUI\ERP\Discount\Handler(); + $Discount = $Handler->getChild($discountId); + + $Discount->setAttribute('active', 0); + $Discount->update(); + + return $Discount->isActive(); + }, + array('discountId'), + 'Permission::checkAdminUser' +); diff --git a/ajax/search.php b/ajax/search.php index 22b3e97b2c77779152b51000add26e620392b2bd..de1917c99aaffa5e7b6745870695db5224e92564 100644 --- a/ajax/search.php +++ b/ajax/search.php @@ -23,11 +23,17 @@ function ($params) { ); foreach ($data as $entry) { - $result[] = array( - 'id' => $entry['id'], - 'title' => $Locale->getPartsOfLocaleString($entry['title']), - 'text' => $Locale->parseLocaleString($entry['title']) + $entry['title'] = array( + 'quiqqer/discount', + 'discount.' . $entry['id'] . '.title' ); + + $entry['text'] = $Locale->get( + 'quiqqer/discount', + 'discount.' . $entry['id'] . '.title' + ); + + $result[] = $entry; } usort($result, function ($a, $b) { diff --git a/ajax/toggle.php b/ajax/toggle.php new file mode 100644 index 0000000000000000000000000000000000000000..680659a036d797e9a90e0f4c061449a9d2612b8c --- /dev/null +++ b/ajax/toggle.php @@ -0,0 +1,31 @@ +<?php + +/** + * This file contains package_quiqqer_discount_ajax_toggle + */ + +/** + * Toggle the status from a tax entry + * + * @param integer $discountId - Discount-ID + */ +QUI::$Ajax->registerFunction( + 'package_quiqqer_discount_ajax_toggle', + function ($discountId) { + $Handler = new QUI\ERP\Discount\Handler(); + $Discount = $Handler->getChild($discountId); + + /* @var $Tax \QUI\ERP\Tax\TaxEntry */ + if ($Discount->isActive()) { + $Discount->setAttribute('active', 0); + } else { + $Discount->setAttribute('active', 1); + } + + $Discount->update(); + + return $Discount->isActive(); + }, + array('discountId'), + 'Permission::checkAdminUser' +); diff --git a/bin/classes/Handler.js b/bin/classes/Handler.js index eb54fa4f59ae27afba4c680d58b64452ff8b0648..cb9332d76961135d967f4de7d68b622eb3f2db1d 100644 --- a/bin/classes/Handler.js +++ b/bin/classes/Handler.js @@ -63,20 +63,6 @@ define('package/quiqqer/discount/bin/classes/Handler', [ return this.search(); }, - /** - * Return all unassigned countries - * - * @returns {Promise} - */ - getUnAssignedCountries: function () { - return new Promise(function (resolve, reject) { - Ajax.get('package_quiqqer_discount_ajax_getUnAssignedCountries', resolve, { - 'package': 'quiqqer/discount', - onError : reject - }); - }); - }, - /** * Create a new Discount * @@ -140,7 +126,7 @@ define('package/quiqqer/discount/bin/classes/Handler', [ * @param {Number} discountId * @param {Object} data - Discount attributes */ - save: function (discountId, data) { + update: function (discountId, data) { return new Promise(function (resolve, reject) { Ajax.post('package_quiqqer_discount_ajax_update', resolve, { 'package' : 'quiqqer/discount', @@ -149,6 +135,60 @@ define('package/quiqqer/discount/bin/classes/Handler', [ params : JSON.encode(data) }); }); + }, + + /** + * Toggle the status from the discount + * + * @param {Number} discountId + * @returns {Promise} + */ + toggleStatus: function (discountId) { + return new Promise(function (resolve, reject) { + Ajax.post( + 'package_quiqqer_discount_ajax_toggle', + resolve, { + 'package' : 'quiqqer/discount', + discountId: discountId, + onError : reject + }); + }); + }, + + /** + * Activate the discount + * + * @param {Number} discountId + * @returns {Promise} + */ + activate: function (discountId) { + return new Promise(function (resolve, reject) { + Ajax.post( + 'package_quiqqer_discount_ajax_activate', + resolve, { + 'package' : 'quiqqer/discount', + discountId: discountId, + onError : reject + }); + }); + }, + + /** + * Deactivate the discount + * + * @param {Number} discountId + * @returns {Promise} + */ + deactivate: function (discountId) { + return new Promise(function (resolve, reject) { + Ajax.post( + 'package_quiqqer_discount_ajax_deactivate', + resolve, { + 'package' : 'quiqqer/discount', + discountId: discountId, + onError : reject + }); + }); } }); }); diff --git a/bin/controls/DiscountEdit.css b/bin/controls/DiscountEdit.css new file mode 100644 index 0000000000000000000000000000000000000000..7df3e478bb7dfbfbeba5ec58148600c9679259e0 --- /dev/null +++ b/bin/controls/DiscountEdit.css @@ -0,0 +1,8 @@ + +.discount-edit td { + width: 50%; +} + +.field-container-item:first-child { + width: 170px !important; +} \ No newline at end of file diff --git a/bin/controls/DiscountEdit.html b/bin/controls/DiscountEdit.html new file mode 100644 index 0000000000000000000000000000000000000000..aab1e1a14e1a2dbdfaa83482420c53ee97803feb --- /dev/null +++ b/bin/controls/DiscountEdit.html @@ -0,0 +1,185 @@ +<form> +<table class="data-table data-table-flexbox"> + <thead> + <tr> + <th colspan="2"> + Rabatt + </th> + </tr> + </thead> + + <tbody> + <tr class="odd"> + <td> + <label class="field-container"> + <span class="field-container-item"> + ID + </span> + <div class="field-container-field field-id"></div> + </label> + </td> + </tr> + <tr class="even"> + <td> + <label class="field-container"> + <span class="field-container-item"> + Titel + </span> + <div class="field-container-field discount-title"></div> + </label> + </td> + </tr> + <tr class="even"> + <td> + <label class="field-container"> + <span class="field-container-item"> + Rabatt + </span> + <input type="text" name="discount" + class="field-container-field"/> + <select name="discount_type" + class="field-container-item" + style="width: 100px;" + > + <option value="%">%</option> + <option value="currency">€</option> + </select> + </label> + </td> + </tr> + </tbody> +</table> + +<table class="data-table data-table-flexbox"> + <thead> + <tr> + <th colspan="2"> + Nutzung definieren + </th> + </tr> + </thead> + + <tbody> + <tr class="odd"> + <td> + <label class="field-container"> + <span class="field-container-item"> + <span class="fa fa-calendar"></span> + Nutzbar von + </span> + <input type="datetime" name="date_from" + class="field-container-field"/> + </label> + </td> + <td> + <label class="field-container"> + <span class="field-container-item"> + <span class="fa fa-calendar"></span> + Nutzbar bis + </span> + <input type="datetime" name="date_until" + class="field-container-field"/> + </label> + </td> + </tr> + <tr class="even"> + <td> + <label class="field-container"> + <span class="field-container-item"> + Einkaufsmenge von + </span> + <input type="number" name="purchase_quantity_from" + class="field-container-field"/> + </label> + </td> + <td> + <label class="field-container"> + <span class="field-container-item"> + Einkaufsmenge bis + </span> + <input type="number" name="purchase_quantity_until" + class="field-container-field"/> + </label> + </td> + </tr> + <tr class="odd"> + <td> + <label class="field-container"> + <span class="field-container-item"> + <span class="fa fa-money"></span> + Einkaufswert von + </span> + <input type="number" name="purchase_value_from" + class="field-container-field"/> + </label> + </td> + <td> + <label class="field-container"> + <span class="field-container-item"> + <span class="fa fa-money"></span> + Einkaufswert bis + </span> + <input type="number" name="purchase_value_until" + class="field-container-field"/> + </label> + </td> + </tr> + </tbody> +</table> + +<table class="data-table data-table-flexbox"> + <thead> + <tr> + <th colspan="2"> + Zuweisung + </th> + </tr> + </thead> + + <tbody> + <tr class="odd"> + <td> + <label class="field-container"> + <span class="field-container-item">Artikel</span> + <input type="text" name="articles" + class="field-container-field"/> + </label> + </td> + </tr> + <tr class="even"> + <td> + <label class="field-container"> + <span class="field-container-item"> + Kategorien + </span> + + <input type="text" name="categories" + class="field-container-field"/> + </label> + </td> + </tr> + <tr class="odd"> + <td> + <label class="field-container"> + <span class="field-container-item"> + Benutzer + </span> + <input type="text" name="user_groups" + class="field-container-field"/> + </label> + </td> + </tr> + <tr class="even"> + <td> + <label class="field-container"> + <span class="field-container-item"> + Kombinerbar mit + </span> + <input type="text" name="combined" + class="field-container-field"/> + </label> + </td> + </tr> + </tbody> +</table> +</form> \ No newline at end of file diff --git a/bin/controls/DiscountEdit.js b/bin/controls/DiscountEdit.js new file mode 100644 index 0000000000000000000000000000000000000000..5903fa70dc3d68ab3464eb58c3f87f3039fca5f9 --- /dev/null +++ b/bin/controls/DiscountEdit.js @@ -0,0 +1,130 @@ +/** + * Discount edit control + * + * @author www.pcsg.de (Henning Leutz) + * + * @require qui/QUI + * @require qui/controls/Control + * @require qui/controls/buttons/Button + * @require Locale + * @require package/quiqqer/discount/bin/classes/Handler + * @require package/quiqqer/translator/bin/controls/VariableTranslation + * @require text!package/quiqqer/discount/bin/controls/DiscountEdit.html + * + * @event onLoaded + */ +define('package/quiqqer/discount/bin/controls/DiscountEdit', [ + + 'qui/QUI', + 'qui/controls/Control', + 'qui/controls/buttons/Button', + 'qui/utils/Form', + 'Locale', + 'package/quiqqer/discount/bin/classes/Handler', + 'package/quiqqer/translator/bin/controls/VariableTranslation', + + 'text!package/quiqqer/discount/bin/controls/DiscountEdit.html', + 'css!package/quiqqer/discount/bin/controls/DiscountEdit.css' + +], function (QUI, QUIControl, QUIButton, QUIFormUtils, QUILocale, + Handler, Translation, template) { + "use strict"; + + var lg = 'quiqqer/discount'; + + var Discounts = new Handler(); + + return new Class({ + + Extends: QUIControl, + Type : 'package/quiqqer/discount/bin/controls/DiscountEdit', + + Binds: [ + '$onInject' + ], + + options: { + discountId: false + }, + + initialize: function (options) { + this.parent(options); + + this.addEvents({ + onInject: this.$onInject + }); + }, + + /** + * Return the DOMNode Element + * + * @returns {HTMLDivElement} + */ + create: function () { + this.$Elm = this.parent(); + this.$Elm.set('html', template); + this.$Elm.set('class', 'discount-edit'); + + return this.$Elm; + }, + + /** + * event : on inject + */ + $onInject: function () { + var self = this; + + require([ + 'utils/Controls' + ], function (Utils) { + QUI.parse(self.$Elm).then( + Utils.parse(self.$Elm) + ).then(function () { + return Discounts.getChild(self.getAttribute('discountId')); + }).then(function (data) { + + var Elm = self.getElm(), + Form = Elm.getElement('form'); + + new Translation({ + 'group': 'quiqqer/discount', + 'var' : 'discount.' + data.id + '.title' + }).inject( + Elm.getElement('.discount-title') + ); + + Elm.getElement('.field-id').set( + 'html', + '#' + self.getAttribute('discountId') + ); +console.log(data); + QUIFormUtils.setDataToForm(data, Form); + + self.fireEvent('loaded'); + }); + }); + }, + + /** + * Save the Discount + * + * @return {Promise} + */ + save: function () { + var self = this; + return new Promise(function (resolve, reject) { + var Elm = self.getElm(), + Form = Elm.getElement('form'); + + var data = QUIFormUtils.getFormData(Form); +console.warn(data); + Discounts.update( + self.getAttribute('discountId'), + data + ).then(function () { + resolve(); + }).catch(reject); + }); + } + }); +}); diff --git a/bin/controls/Discounts.js b/bin/controls/Discounts.js index d1d58f464849cea40c8c62934f71beedb15eca9c..9ae59ef16bf47aaef36afd04c3b429c3e0da5e04 100644 --- a/bin/controls/Discounts.js +++ b/bin/controls/Discounts.js @@ -3,6 +3,7 @@ * Create and edit discounts * * @author www.pcsg.de (Henning Leutz) + * @module package/quiqqer/discount/bin/controls/Discounts * * @require qui/QUI * @require qui/controls/desktop/Panel @@ -41,7 +42,9 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ 'deleteChildren', 'refresh', '$onCreate', - '$onResize' + '$onResize', + '$toggleStatus', + '$setTaxEntryButtonStatus' ], initialize: function (options) { @@ -49,10 +52,6 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ this.$Grid = null; - this.setAttributes({ - 'title': QUILocale.get(lg, 'menu.erp.discount.panel.title') - }); - this.addEvents({ onCreate: this.$onCreate, onResize: this.$onResize @@ -64,6 +63,12 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ */ $onCreate: function () { + var self = this; + + this.setAttributes({ + title: QUILocale.get(lg, 'discount.panel.title') + }); + // buttons this.addButton({ name : 'add', @@ -80,7 +85,11 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ textimage: 'icon-edit fa fa-edit', disabled : true, events : { - onClick: this.createChild + onClick: function () { + self.editChild( + self.$Grid.getSelectedData()[0].id + ); + } } }); @@ -108,8 +117,6 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ }); // Grid - var self = this; - var Container = new Element('div').inject( this.getContent() ); @@ -122,15 +129,65 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ dataType : 'number', width : 60 }, { - header : QUILocale.get(lg, 'discount.grid.discount.title'), - dataIndex: 'title', + header : QUILocale.get('quiqqer/system', 'status'), + dataIndex: 'status', + dataType : 'button', + width : 60 + }, { + header : QUILocale.get(lg, 'discount.grid.discount'), + dataIndex: 'discount', + dataType : 'number', + width : 100 + }, { + header : QUILocale.get('quiqqer/system', 'title'), + dataIndex: 'text', dataType : 'string', width : 200 }, { - header : QUILocale.get(lg, 'discount.grid.discount.areas'), + header : QUILocale.get(lg, 'discount.grid.date_from'), + dataIndex: 'date_from', + dataType : 'date', + width : 200 + }, { + header : QUILocale.get(lg, 'discount.grid.date_until'), + dataIndex: 'date_until', + dataType : 'date', + width : 200 + }, { + header : QUILocale.get(lg, 'discount.grid.purchase_quantity'), + dataIndex: 'purchase_quantity', + dataType : 'number', + width : 100 + }, { + header : QUILocale.get(lg, 'discount.grid.purchase_value'), + dataIndex: 'purchase_value', + dataType : 'number', + width : 100 + }, { + header : QUILocale.get(lg, 'discount.grid.areas'), dataIndex: 'areas', dataType : 'string', - width : 300 + width : 200 + }, { + header : QUILocale.get(lg, 'discount.grid.articles'), + dataIndex: 'articles', + dataType : 'string', + width : 200 + }, { + header : QUILocale.get(lg, 'discount.grid.categories'), + dataIndex: 'categories', + dataType : 'string', + width : 200 + }, { + header : QUILocale.get(lg, 'discount.grid.user_groups'), + dataIndex: 'user_groups', + dataType : 'string', + width : 200 + }, { + header : QUILocale.get(lg, 'discount.grid.combined'), + dataIndex: 'combined', + dataType : 'string', + width : 100 }] }); @@ -180,6 +237,8 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ /** * refresh the display + * + * @return {Promise} */ refresh: function () { @@ -188,14 +247,41 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ this.Loader.show(); this.getButtons('delete').disable(); this.getButtons('edit').disable(); + this.parent(); - Discounts.getList().then(function (data) { - var dataEntry, + return Discounts.getList().then(function (data) { + var i, len, active, entry, gridData = []; - for (var i = 0, len = data.length; i < len; i++) { - dataEntry = data[i]; + for (i = 0, len = data.length; i < len; i++) { + entry = data[i]; + active = parseInt(data[i].active); + + if (active) { + entry.status = { + icon : 'icon-ok fa fa-ok', + discountId: entry.id, + styles : { + lineHeight: 16 + }, + events : { + onClick: self.$toggleStatus + } + }; + } else { + entry.status = { + icon : 'icon-remove fa fa-remove', + discountId: entry.id, + styles : { + lineHeight: 16 + }, + events : { + onClick: self.$toggleStatus + } + }; + } + gridData.push(entry); } self.$Grid.setData({ @@ -204,7 +290,155 @@ define('package/quiqqer/discount/bin/controls/Discounts', [ self.Loader.hide(); }); + }, + + /** + * Create the child and open the edit + * + * @return {Promise} + */ + createChild: function () { + var self = this; + + this.Loader.show(); + + return Discounts.createChild().then(function (discountId) { + + return self.refresh().then(function () { + self.Loader.hide(); + return self.editChild(discountId); + }); + }); + }, + + /** + * Open the discount edit + * + * @param {Number} discountId + */ + editChild: function (discountId) { + + var self = this; + + self.Loader.show(); + + return new Promise(function (resolve) { + self.createSheet({ + events: { + onShow: function (Sheet) { + + Sheet.getContent().set({ + styles: { + padding: 20 + } + }); + + require([ + 'package/quiqqer/discount/bin/controls/DiscountEdit' + ], function (DiscountEdit) { + + var Discount = new DiscountEdit({ + discountId: discountId, + events : { + onLoaded: function () { + self.Loader.hide(); + resolve(); + } + } + }).inject(Sheet.getContent()); + + Sheet.addButton({ + text : QUILocale.get( + 'quiqqer/system', + 'save' + ), + textimage: 'icon-save', + events : { + click: function () { + self.Loader.show(); + + Discount.save().then(function () { + self.refresh(); + }).catch(function () { + self.Loader.hide(); + }); + } + } + }); + }); + + } + } + }).show(); + }); + }, + + /** + * Opens the delete dialog - delete + * + * @param {Array} discountIds + */ + deleteChildren: function (discountIds) { + var self = this, + str = discountIds.join(','); + + new QUIConfirm({ + title : QUILocale.get(lg, 'discount.window.delete.title'), + text : QUILocale.get(lg, 'discount.window.delete.text', { + ids: str + }), + information: QUILocale.get(lg, 'discount.window.delete.information', { + ids: str + }), + icon : 'icon-trash fa fa-trash', + textimage : 'icon-trash fa fa-trash', + maxHeight : 300, + maxWidth : 450, + autoclose : false, + events : { + onSubmit: function (Win) { + Win.Loader.show(); + Discounts.deleteChildren(discountIds).then(function () { + return self.refresh(); + }).then(function () { + Win.close(); + }); + } + } + }).open(); + }, + + /** + * Toggle the discount status + * + * @param {Object} Button - qui/controls/buttons/Button + */ + $toggleStatus: function (Button) { + Button.setAttribute( + 'icon', + 'icon-spinner icon-spin fa fa-spinner fa-spin' + ); + + Discounts.toggleStatus( + Button.getAttribute('discountId') + ).then(function (status) { + this.$setTaxEntryButtonStatus(Button, status); + }.bind(this)); + }, + + /** + * Change the statusbutton of a discount + * + * @param {Object} Button + * @param {Boolean|Number} status + */ + $setTaxEntryButtonStatus: function (Button, status) { + if (status) { + Button.setAttribute('icon', 'icon-ok'); + return; + } + + Button.setAttribute('icon', 'icon-remove'); } }); }); - diff --git a/database.xml b/database.xml index d6389e5687e5487aca258c96ffad014d04a9453f..d8cf0d9858407cf13097a60a22b93708d04a2694 100644 --- a/database.xml +++ b/database.xml @@ -4,11 +4,13 @@ <table name="discounts"> <field type="INT( 10 ) NOT NULL PRIMARY KEY">id</field> <field type="INT( 1 )">active</field> - <field type="text">discount</field> + <field type="varchar( 255 )">discount</field> <field type="text">date_from</field> - <field type="text">data_to</field> - <field type="INT( 10 )">purchase_quantity</field> - <field type="text">purchase_value</field> + <field type="text">date_until</field> + <field type="INT( 10 )">purchase_quantity_from</field> + <field type="INT( 10 )">purchase_quantity_until</field> + <field type="text">purchase_value_from</field> + <field type="text">purchase_value_until</field> <field type="text">areas</field> <field type="text">articles</field> diff --git a/locale.xml b/locale.xml index c2a61c54a205d57b6a2679770ea9ba714d8bae05..ddcd60132efa1535c3e666117a01048a34fcf4fb 100644 --- a/locale.xml +++ b/locale.xml @@ -12,6 +12,50 @@ </groups> <groups name="quiqqer/discount" datatype="js"> + <locale name="discount.panel.title"> + <de><![CDATA[Rabatte]]></de> + <en><![CDATA[Discounts]]></en> + </locale> + <locale name="discount.grid.discount"> + <de><![CDATA[Rabatt]]></de> + <en><![CDATA[Discount]]></en> + </locale> + <locale name="discount.grid.date_from"> + <de><![CDATA[Nutzbar von]]></de> + <en><![CDATA[Available from]]></en> + </locale> + <locale name="discount.grid.date_until"> + <de><![CDATA[Nutzbar bis]]></de> + <en><![CDATA[Available until]]></en> + </locale> + <locale name="discount.grid.areas"> + <de><![CDATA[Zugewiesene Zonen]]></de> + <en><![CDATA[Assigned Areas]]></en> + </locale> + <locale name="discount.grid.purchase_quantity"> + <de><![CDATA[Einkaufsmenge]]></de> + <en><![CDATA[Purchase quantity]]></en> + </locale> + <locale name="discount.grid.purchase_value"> + <de><![CDATA[Einkaufswert]]></de> + <en><![CDATA[Purchase value]]></en> + </locale> + <locale name="discount.grid.articles"> + <de><![CDATA[Zugewiesene Artikel]]></de> + <en><![CDATA[Assigned products]]></en> + </locale> + <locale name="discount.grid.categories"> + <de><![CDATA[Zugewiesene Kategorien]]></de> + <en><![CDATA[Assigned categories]]></en> + </locale> + <locale name="discount.grid.user_groups"> + <de><![CDATA[Zugewiesene Benutzer und Gruppen]]></de> + <en><![CDATA[Assigned users and groups]]></en> + </locale> + <locale name="discount.grid.combined"> + <de><![CDATA[Kombinierbar mit]]></de> + <en><![CDATA[Combined with]]></en> + </locale> </groups> </locales> \ No newline at end of file diff --git a/src/QUI/ERP/Discount/Discount.php b/src/QUI/ERP/Discount/Discount.php index 0fcc65566be5cc268089519047cec2681eedb155..572d4d6a8d31cd347509fc033ba77e23ed627eba 100644 --- a/src/QUI/ERP/Discount/Discount.php +++ b/src/QUI/ERP/Discount/Discount.php @@ -7,6 +7,8 @@ use QUI; use QUI\Users\User; +use QUI\Rights\Permission; +use QUI\Utils\Security\Orthos; /** * Class Discount @@ -14,6 +16,73 @@ */ class Discount extends QUI\CRUD\Child { + /** + * Discount constructor. + * @param int $id + * @param Handler $Factory + */ + public function __construct($id, Handler $Factory) + { + parent::__construct($id, $Factory); + + $this->Events->addEvent('onDeleteBegin', function () { + Permission::checkPermission('quiqqer.areas.area.delete'); + }); + + $this->Events->addEvent('onDeleteEnd', function () { + QUI\Translator::delete( + 'quiqqer/discount', + 'discount.' . $this->getId() . '.title' + ); + }); + + $this->Events->addEvent('onSaveBegin', function () { + Permission::checkPermission('quiqqer.areas.area.edit'); + + if ($this->getAttribute('date_from') + && !Orthos::checkMySqlDatetimeSyntax($this->getAttribute('date_from')) + ) { + throw new QUI\Exception(array( + 'quiqqer/discount', + 'exception.discount.date.wrong' + )); + } + + if ($this->getAttribute('date_until') + && !Orthos::checkMySqlDatetimeSyntax($this->getAttribute('date_until')) + ) { + throw new QUI\Exception(array( + 'quiqqer/discount', + 'exception.discount.date.wrong' + )); + } + + + QUI\System\Log::writeRecursive($this->getAttributes()); + + }); + } + + /** + * @return string + */ + public function getTitle() + { + return QUI::getLocale()->get( + 'quiqqer/discount', + 'discount.' . $this->getId() . '.title' + ); + } + + /** + * Return the discount status + * + * @return boolean + */ + public function isActive() + { + return $this->getAttribute('active') ? true : false; + } /** * Is the discount combinable with another discount? @@ -52,6 +121,10 @@ public function canCombinedWith(Discount $Discount) */ public function canUsedBy(User $User) { + if ($this->isActive() === false) { + return false; + } + $userGroups = QUI\UsersGroups\Utils::parseUsersGroupsString( $this->getAttribute('user_groups') ); diff --git a/src/QUI/ERP/Discount/Handler.php b/src/QUI/ERP/Discount/Handler.php index 83ea0bea2281e10d2e5dd66c386fcdc4ed2f8bad..d956e8ebf9423f958681a33b1a096331782c9fd2 100644 --- a/src/QUI/ERP/Discount/Handler.php +++ b/src/QUI/ERP/Discount/Handler.php @@ -39,10 +39,16 @@ public function __construct() $title = QUI::getLocale()->get($parts[0], $parts[1]); } - QUI\Translator::addUserVar('quiqqer/discount', $newVar, array( - $current => $title, - 'datatype' => 'php,js' - )); + try { + QUI\Translator::addUserVar('quiqqer/discount', $newVar, array( + $current => $title, + 'datatype' => 'php,js' + )); + } catch (QUI\Exception $Exception) { + QUI::getMessagesHandler()->addAttention( + $Exception->getMessage() + ); + } }); } @@ -77,12 +83,14 @@ public function getChildAttributes() 'active', 'discount', 'date_from', - 'data_to', - 'purchase_quantity', - 'purchase_value', - 'area', - 'article', - 'category', + 'date_until', + 'purchase_quantity_from', + 'purchase_quantity_until', + 'purchase_value_from', + 'purchase_value_until', + 'areas', + 'articles', + 'categories', 'user_groups', 'combined' );