Skip to content
Code-Schnipsel Gruppen Projekte
Commit 80918859 erstellt von Henning Leutz's avatar Henning Leutz :martial_arts_uniform:
Dateien durchsuchen

refactor: add shipping rules to the shipping entry

Übergeordneter 194b1fd5
Keine zugehörigen Branchen gefunden
Keine zugehörigen Tags gefunden
Keine zugehörigen Merge Requests gefunden
<?php
/**
* This file contains package_quiqqer_shipping_ajax_backend_rules_getRules
*/
/**
* Return all wanted shipping rules
*
* @return array
*/
QUI::$Ajax->registerFunction(
'package_quiqqer_shipping_ajax_backend_rules_getRules',
function ($ruleIds) {
$ruleIds = \json_decode($ruleIds, true);
$Rules = QUI\ERP\Shipping\Rules\Factory::getInstance();
$result = [];
foreach ($ruleIds as $ruleId) {
try {
$result[] = $Rules->getChild($ruleId)->toArray();
} catch (QUI\Exception $Exception) {
\QUI\System\Log::addDebug($Exception);
}
}
return $result;
},
['ruleIds'],
'Permission::checkAdminUser'
);
......@@ -38,6 +38,15 @@ function ($shippingId, $data) {
}
$ShippingEntry->setAttributes($data);
if (isset($data['shipping_rules'])) {
$shipping = json_decode($data['shipping_rules'], true);
foreach ($shipping as $shippingId) {
$ShippingEntry->addShippingRuleId($shippingId);
}
}
$ShippingEntry->update();
QUI::getMessagesHandler()->addSuccess(
......
......@@ -43,6 +43,22 @@ define('package/quiqqer/shipping/bin/backend/classes/ShippingRules', [
onError : reject
});
});
},
/**
* Return the wanted rules
*
* @param ruleIds
* @return {Promise}
*/
getRules: function (ruleIds) {
return new Promise(function (resolve, reject) {
QUIAjax.get('package_quiqqer_shipping_ajax_backend_rules_getRules', resolve, {
'package': 'quiqqer/shipping',
ruleIds : JSON.encode(ruleIds),
onError : reject
});
});
}
});
});
/**
* @module package/quiqqer/shipping/bin/backend/controls/ShippingEntry.List
* @author www.pcsg.de (Henning Leutz)
*
* @event onLoad [self]
*/
define('package/quiqqer/shipping/bin/backend/controls/ShippingEntry.List', [
......@@ -18,17 +20,21 @@ define('package/quiqqer/shipping/bin/backend/controls/ShippingEntry.List', [
Type : 'package/quiqqer/shipping/bin/backend/controls/ShippingEntry.List',
Binds: [
'$onInject'
'$onInject',
'$openAddDialog',
'$openDeleteDialog'
],
options: {
shippingId: false
shippingId: false,
name : 'shipping-rules'
},
initialize: function (options) {
this.parent(options);
this.$Grid = null;
this.$Grid = null;
this.$Input = null;
this.addEvents({
onInject: this.$onInject
......@@ -44,14 +50,36 @@ define('package/quiqqer/shipping/bin/backend/controls/ShippingEntry.List', [
this.$Elm.addClass('quiqqer-shipping-rules');
this.$Input = new Element('input', {
type: 'hidden',
name: this.getAttribute('name')
}).inject(this.$Elm);
return this.$Elm;
},
/**
* refresh the input value
*/
refreshInput: function () {
if (!this.$Grid) {
return;
}
var value = this.$Grid.getData().map(function (entry) {
return parseInt(entry.id);
});
value = JSON.encode(value);
this.$Input.value = value;
},
/**
* event: on inject
*/
$onInject: function () {
var self = this;
var Container = new Element('div').inject(this.getElm());
var width = Container.getSize().x - 5;
......@@ -99,19 +127,85 @@ define('package/quiqqer/shipping/bin/backend/controls/ShippingEntry.List', [
});
this.$Grid.setWidth(width);
require([
'package/quiqqer/shipping/bin/backend/Shipping',
'package/quiqqer/shipping/bin/backend/ShippingRules',
'package/quiqqer/shipping/bin/backend/utils/ShippingUtils'
], function (Shipping, ShippingRules, Utils) {
Shipping.getShippingEntry(
self.getAttribute('shippingId')
).then(function (result) {
var shippingRules = result.shipping_rules;
try {
shippingRules = JSON.decode(shippingRules);
} catch (e) {
shippingRules = [];
}
return ShippingRules.getRules(shippingRules);
}).then(function (rules) {
self.$Grid.setData({
data: Utils.parseRulesDataForGrid(rules)
});
self.fireEvent('load', [self]);
});
});
},
/**
* Add shipping rules to the shipping entry
*
* @param {Array} shippingRules - list of ids
*/
addShippingRules: function (shippingRules) {
var self = this;
return new Promise(function (resolve) {
require([
'package/quiqqer/shipping/bin/backend/ShippingRules'
], function (ShippingRules) {
ShippingRules.getRules(shippingRules).then(function (rules) {
var current = QUILocale.getCurrent();
rules.forEach(function (v, k) {
var title = '';
if (typeof v.title[current] !== 'undefined') {
title = v.title[current];
} else {
title = Object.values(v.title)[0];
}
rules[k].title = title;
});
self.$Grid.setData({
data: rules
});
self.refreshInput();
resolve();
});
});
});
},
/**
* open rule window to add a rule to the shipping rules
*/
$openAddDialog: function () {
var self = this;
require([
'package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRuleListWindow'
], function (ShippingRuleListWindow) {
new ShippingRuleListWindow({
events: {
onSubmit: function () {
console.log('submit');
onSubmit: function (Win, selected) {
self.addShippingRules(selected);
}
}
}).open();
......
......@@ -370,7 +370,8 @@ define('package/quiqqer/shipping/bin/backend/controls/ShippingEntry', [
Container.getElement('.field-id').set('html', data.id);
new ShippingRulesGrid({
shippingId: self.getAttribute('shippingId')
shippingId: self.getAttribute('shippingId'),
name : 'shipping_rules'
}).inject(
Container.getElement('.shipping-rules tbody td')
);
......
......@@ -7,10 +7,11 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
'qui/QUI',
'qui/controls/Control',
'package/quiqqer/shipping/bin/backend/ShippingRules',
'package/quiqqer/shipping/bin/backend/utils/ShippingUtils',
'controls/grid/Grid',
'Locale'
], function (QUI, QUIControl, ShippingRules, Grid, QUILocale) {
], function (QUI, QUIControl, ShippingRules, ShippingUtils, Grid, QUILocale) {
"use strict";
return new Class({
......@@ -18,11 +19,16 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
Extends: QUIControl,
Type : 'package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRuleList',
options: {
multiple: true
},
Binds: [
'$onInject',
'$openCreateDialog',
'$openDeleteDialog',
'refresh'
'refresh',
'getSelected'
],
initialize: function (options) {
......@@ -73,10 +79,11 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
var height = size.y;
this.$Grid = new Grid(Container, {
height : height,
width : width,
pagination : true,
buttons : [{
height : height,
width : width,
pagination : true,
multipleSelection: this.getAttribute('multiple'),
buttons : [{
name : 'create',
text : QUILocale.get('quiqqer/quiqqer', 'create'),
textimage: 'fa fa-plus',
......@@ -92,7 +99,7 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
onClick: this.$openDeleteDialog
}
}],
columnModel: [{
columnModel : [{
header : QUILocale.get('quiqqer/system', 'id'),
dataIndex: 'id',
dataType : 'number',
......@@ -135,17 +142,8 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
ShippingRules.getList().then(function (rules) {
self.fireEvent('refresh', [self, rules]);
var current = QUILocale.getCurrent();
var data = rules.map(function (entry) {
entry.title = entry.title[current];
entry.workingTitle = entry.workingTitle[current];
return entry;
});
self.$Grid.setData({
data: data
data: ShippingUtils.parseRulesDataForGrid(rules)
});
return rules;
......@@ -169,8 +167,8 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
return [];
}
return this.$Grid.getSelected().map(function (entry) {
return entry.id;
return this.$Grid.getSelectedData().map(function (entry) {
return parseInt(entry.id);
});
},
......@@ -187,7 +185,7 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
], function (CreateRuleWindow) {
new CreateRuleWindow({
events: {
onCloseCreateRuleWindow: function () {
onClose: function () {
self.fireEvent('closeCreateRuleWindow', [self]);
}
}
......
......@@ -24,8 +24,7 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
options: {
maxHeight: 600,
maxWidth : 600,
autoclose: false
maxWidth : 600
},
initialize: function (options) {
......@@ -71,11 +70,16 @@ define('package/quiqqer/shipping/bin/backend/controls/shippingRules/ShippingRule
},
/**
* event: on submit
* Submit the window
*
* @method qui/controls/windows/Confirm#submit
*/
$onSubmit: function () {
var selected = this.$List.getSelected();
submit: function () {
this.fireEvent('submit', [this, this.$List.getSelected()]);
if (this.getAttribute('autoclose')) {
this.close();
}
}
});
});
/**
* @module package/quiqqer/shipping/bin/backend/utils/ShippingUtils
* @author www.pcsg.de (Henning Leutz)
*/
define('package/quiqqer/shipping/bin/backend/utils/ShippingUtils', [
'Locale'
], function (QUILocale) {
"use strict";
return {
/**
* Return a rule data from ajax for a rule grid
*
* @param {Array} rules
* @return {Array}
*/
parseRulesDataForGrid: function (rules) {
var self = this;
return rules.map(function (entry) {
return self.parseRuleDataForGrid(entry);
});
},
/**
* parse one rule entry of an ajax rule array to a rule grid entry
*
* @param {Object} ruleData
* @return {Object}
*/
parseRuleDataForGrid: function (ruleData) {
var current = QUILocale.getCurrent();
ruleData.title = ruleData.title[current];
ruleData.workingTitle = ruleData.workingTitle[current];
return ruleData;
}
};
});
\ No newline at end of file
......@@ -49,14 +49,6 @@ public function createChild($data = [])
$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;
}
......
......@@ -12,8 +12,8 @@
use QUI\Permissions\Permission;
use QUI\ERP\Shipping\Api;
use QUI\ERP\Areas\Utils as AreaUtils;
use QUI\ERP\Shipping\Exceptions\ShippingCanNotBeUsed;
use QUI\ERP\Shipping\Rules\Factory as RuleFactory;
use QUI\ERP\Shipping\Rules\ShippingRule;
/**
* Class ShippingEntry
......@@ -384,4 +384,63 @@ protected function setShippingLocale($var, $title)
}
}
//endregion
//region rules
/**
* @param ShippingRule $Rule
*/
public function addShippingRule(ShippingRule $Rule)
{
$shippingRules = $this->getAttribute('shipping_rules');
$shippingRules = \json_decode($shippingRules, true);
if (!\in_array($Rule->getId(), $shippingRules)) {
$shippingRules[] = $Rule->getId();
}
$this->setAttribute('shipping_rules', \json_encode($shippingRules));
}
/**
* Add a shipping rule by its id
*
* @param integer $shippingRuleId
* @throws QUI\Exception
*/
public function addShippingRuleId($shippingRuleId)
{
/* @var $Rule ShippingRule */
$Rule = RuleFactory::getInstance()->getChild($shippingRuleId);
$this->addShippingRule($Rule);
}
/**
* Return the shipping rules of the
* @return ShippingRule[]
*/
public function getShippingRules()
{
$shippingRules = $this->getAttribute('shipping_rules');
$shippingRules = \json_decode($shippingRules, true);
if (!is_array($shippingRules)) {
return [];
}
$result = [];
$Rules = RuleFactory::getInstance();
foreach ($shippingRules as $shippingRule) {
try {
$result[] = $Rules->getChild($shippingRule);
} catch (QUI\Exception $Exception) {
QUI\System\Log::addDebug($Exception);
}
}
return $result;
}
//endregion
}
0% Lade oder .
You are about to add 0 people to the discussion. Proceed with caution.
Bearbeitung dieser Nachricht zuerst beenden!
Bitte registrieren oder zum Kommentieren