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\Products\Utils\Fields as FieldUtils;
use QUI\ERP\Shipping\Rules\Factory as RuleFactory;
use QUI\ERP\Shipping\Exceptions\ShippingCanNotBeUsed;
use QUI\ERP\Products\Handler\Products;
use QUI\ERP\Products\Handler\Fields;
/**
* 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;
}
if (!isset($attributes['articles_only']) || empty($attributes['articles_only'])) {
$attributes['articles_only'] = 0;
} else {
$attributes['articles_only'] = (int)$attributes['articles_only'];
}
if (isset($attributes['unit_terms']) && \is_array($attributes['unit_terms'])) {
$attributes['unit_terms'] = \json_encode($attributes['unit_terms']);
}
// null fix
$nullEmpty = [
'purchase_quantity_from',
'purchase_quantity_until',
'purchase_value_from',
'purchase_value_until',
];
foreach ($nullEmpty as $k) {
if (empty($attributes[$k])) {
$attributes[$k] = null;
}
// update for saving
$this->setAttributes($attributes);
/**
* Return the payment as an array
*
* @return array
*/
public function toArray()
{
$lg = 'quiqqer/shipping';
$id = $this->getId();
$attributes = $this->getAttributes();
$Locale = QUI::getLocale();
$availableLanguages = QUI\Translator::getAvailableLanguages();
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'
);
}
$attributes['unit_terms'] = $this->getUnitTerms();
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
/**
* 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'));
}
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
/**
* 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
if ($areasValue) {
$areasValue = \explode(',', $areasValue);
}
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
278
279
280
281
282
283
284
285
286
287
288
289
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
*/
if (!$this->isValid()) {
return false;
}
if (!($Order instanceof QUI\ERP\Order\OrderInterface)) {
return true;
}
if (!$this->canUsedBy($Order->getCustomer())) {
return false;
}
/* @var $Order QUI\ERP\Order\Order */
$Articles = $Order->getArticles();
$articleList = $Articles->getArticles();
$articles = $this->getAttribute('articles');
$articleOnly = $this->getAttribute('articles_only');
$quantityFrom = $this->getAttribute('purchase_quantity_from'); // Einkaufsmenge ab
$quantityUntil = $this->getAttribute('purchase_quantity_until'); // Einkaufsmenge bis
$purchaseFrom = $this->getAttribute('purchase_value_from'); // Einkaufswert ab
$purchaseUntil = $this->getAttribute('purchase_value_until'); // Einkaufswert bis
// article checks
$Shipping = QUI\ERP\Shipping\Shipping::getInstance();
$unitIds = $Shipping->getShippingRuleUnitFieldIds();
$articleFound = true;
$articleUnits = [];
if (!empty($articles)) {
$articleFound = false;
if (\is_string($articles)) {
$articles = \explode(',', $articles);
}
if (!\is_array($articles)) {
$articles = [$articles];
}
$articles = \array_flip($articles);
}
foreach ($articleList as $Article) {
$aid = $Article->getId();
try {
$Product = Products::getProduct($aid);
foreach ($unitIds as $unitId) {
$Weight = $Product->getField($unitId);
$weight = $Weight->getValue();
if ($unitId === Fields::FIELD_WEIGHT) {
$weight = FieldUtils::weightFieldToKilogram($Weight);
}
if (!isset($articleUnits[$unitId])) {
$articleUnits[$unitId] = 0;
}
$articleUnits[$unitId] = $articleUnits[$unitId] + $weight;
}
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
}
if (empty($articles)) {
continue;
}
if (isset($articles[$aid])) {
$articleFound = true;
break;
}
}
if ($articleFound && $articleOnly && \count($articleList) !== 1) {
return false;
}
if ($articleFound === false) {
return false;
}
// unit terms check
if (!empty($unitTerms)) {
foreach ($unitTerms as $unitTerm) {
if (!\is_array($unitTerm)) {
continue;
}
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
$id = (int)$unitTerm['id'];
$unit = $unitTerm['unit'];
$value = \floatval($unitTerm['value']);
$term = $unitTerm['term'];
if ($value === '') {
continue;
}
if (!isset($articleUnits[$id])) {
continue;
}
if ($id === Fields::FIELD_WEIGHT) {
$unitValue = FieldUtils::weightToKilogram($value, $unit);
$compare = FieldUtils::compare($articleUnits[$id], $unitValue, $term);
if ($compare === false) {
return false;
}
continue;
}
$compare = FieldUtils::compare($articleUnits[$id], $value, $term);
if ($compare === false) {
return false;
}
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
}
}
// quantity check
$count = $Order->count();
if (!empty($quantityFrom) && $quantityFrom < $count) {
return false;
}
if (!empty($quantityUntil) && $quantityFrom > $count) {
return false;
}
// purchase
try {
$Calculation = $Order->getPriceCalculation();
$sum = $Calculation->getSum();
} catch (QUI\Exception $Exception) {
QUI\System\Log::writeDebugException($Exception);
return false;
}
if (!empty($purchaseFrom)) {
$purchaseFrom = \floatval($purchaseFrom);
if ($purchaseFrom < $sum) {
return false;
}
}
if (!empty($purchaseUntil)) {
$purchaseUntil = \floatval($purchaseUntil);
if ($purchaseUntil > $sum) {
return false;
}
}
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;
}
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/**
* Return the validation status of this rule
* can the rule be used?
*/
public function isValid()
{
if (!$this->isActive()) {
return false;
}
// check date
$usageFrom = $this->getAttribute('date_from');
$usageUntil = $this->getAttribute('date_until');
$time = \time();
if (!empty($usageFrom)) {
$usageFrom = \strtotime($usageFrom);
if ($usageFrom > $time) {
return false;
}
}
if (!empty($usageUntil)) {
$usageUntil = \strtotime($usageUntil);
if ($usageUntil < $time) {
return false;
}
}
return true;
}
/**
* Return the discount type
*
* @return int
*/
public function getDiscountType()
{
return (int)$this->getAttribute('discount_type');
}
/**
* Return the unit terms
*
* @return bool|array
*/
public function getUnitTerms()
{
$unitTerms = $this->getAttribute('unit_terms');
if (empty($unitTerms)) {
return false;
}
$unitTerms = \json_decode($unitTerms, true);
if ($unitTerms) {
return $unitTerms;
}
return false;
}
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// 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
}