Newer
Older
<?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;
use QUI\ERP\Shipping\Rules\Factory as RuleFactory;
/**
* 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');
// delete locale
$id = $this->getId();
QUI\Translator::delete('quiqqer/shipping', 'shipping.'.$id.'.rule.title');
QUI\Translator::delete('quiqqer/shipping', 'shipping.'.$id.'.rule.workingTitle');
});
$this->Events->addEvent('onSaveBegin', function () {
Permission::checkPermission('quiqqer.shipping.edit');
$id = $this->getId();
$attributes = $this->getAttributes();
if (\is_array($attributes['title'])) {
'quiqqer/shipping',
$attributes['title']
);
};
if (\is_array($attributes['workingTitle'])) {
'quiqqer/shipping',
$attributes['workingTitle']
);
};
QUI\Translator::publish('quiqqer/shipping');
// discount
$attributes['discount'] = \floatval($attributes['discount']);
if (\is_numeric($attributes['discount_type']) || empty($attributes['discount_type'])) {
$attributes['discount_type'] = (int)$attributes['discount_type'];
}
if ($attributes['discount_type'] === RuleFactory::DISCOUNT_TYPE_PERCENTAGE ||
$attributes['discount_type'] === 'PERCENTAGE'
) {
$attributes['discount_type'] = RuleFactory::DISCOUNT_TYPE_PERCENTAGE;
} else {
$attributes['discount_type'] = RuleFactory::DISCOUNT_TYPE_ABS;
}
// update for saving
$this->setAttributes($attributes);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Return the payment as an array
*
* @return array
*/
public function toArray()
{
$lg = 'quiqqer/shipping';
$id = $this->getId();
$attributes = $this->getAttributes();
$Locale = QUI::getLocale();
try {
$availableLanguages = QUI\Translator::getAvailableLanguages();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeException($Exception);
$availableLanguages = [];
}
foreach ($availableLanguages as $language) {
$attributes['title'][$language] = $Locale->getByLang(
$language,
$lg,
'shipping.'.$id.'.rule.title'
);
$attributes['workingTitle'][$language] = $Locale->getByLang(
$language,
$lg,
'shipping.'.$id.'.rule.workingTitle'
);
}
return $attributes;
}
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Return the shipping rule title
*
* @param null $Locale
* @return string
*/
public function getTitle($Locale = null)
{
if ($Locale === null) {
$Locale = QUI::getLocale();
}
$language = $Locale->getCurrent();
$id = $this->getId();
return $Locale->getByLang(
$language,
'quiqqer/shipping',
'shipping.'.$id.'.rule.title'
);
}
/**
* Return the shipping rule priority
*
* @return int
*/
public function getPriority()
{
return (int)$this->getAttribute('priority');
}
/**
* Return the shipping rule discount value
*
* @return float
*/
public function getDiscount()
{
return \floatval($this->getAttribute('discount'));
}
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* 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;
}
/**
* Return the discount type
*
* @return int
*/
public function getDiscountType()
{
return (int)$this->getAttribute('discount_type');
}
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// 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
}