Newer
Older
<?php
/**
* This file contains QUI\ERP\Discount\Utils
*/
namespace QUI\ERP\Discount;
use QUI\ERP\Products\Product\Product;
use QUI\Utils\UserGroups;
use QUI\Interfaces\Users\User as UserInterface;
/**
* Class Utils
*
* @package QUI\ERP\Discount
*/
class Utils
{
/**
* Return all discounts which are usable by the user
*
* @param \QUI\Interfaces\Users\User $User
* @return array
*/
public static function getUserDiscounts(UserInterface $User)
{
$guString = UserGroups::getUserGroupStringFromUser($User);
$guString = ','.\str_replace(',', ',|,', $guString).',';
$personalDiscounts = $Discounts->getChildren([
'where' => [
'user_groups' => [
$discounts = $Discounts->getChildren([
'where' => [
$result = \array_merge($personalDiscounts, $result);
}
return $result;
}
/**
* Return all discounts which are usable with the product
*
* @param Product $Product
* @return array
*/
public static function getProductDiscounts(Product $Product)
{
$productDiscounts = $Discounts->getChildren([
'where' => [
'user_groups' => [
'value' => ','.$Product->getId().','
]
]
]);
$discounts = $Discounts->getChildren([
'where' => [
$result = \array_merge($productDiscounts, $result);
}
return $result;
}
/**
* Return all active discounts which are usable by the user
*
* @param \QUI\Interfaces\Users\User $User
* @return array
*/
public static function getActiveUserDiscounts(UserInterface $User)
{
$guString = UserGroups::getUserGroupStringFromUser($User);
$guString = ','.\str_replace(',', ',|,', $guString).',';
$personalDiscounts = $Discounts->getChildren([
'where' => [
$discounts = $Discounts->getChildren([
'where' => [
$discountsNULL = $Discounts->getChildren([
'where' => [
'active' => 1,
'user_groups' => null
]
]);
$discounts = \array_merge($discounts, $discountsNULL);
$result = \array_merge($personalDiscounts, $result);
$alreadyAttached = [];
$result = \array_filter($result, function ($Discount) use (&$alreadyAttached) {
/* @var $Discount Discount */
$id = $Discount->getId();
if (isset($alreadyAttached[$id])) {
return false;
}
$alreadyAttached[$id] = true;
return true;
});
return $result;
}
/**
* Return all active and usable discounts which are usable by the user
*
* @param \QUI\Interfaces\Users\User $User
* @return array
*/
public static function getUsableUserDiscounts(UserInterface $User)
{
$discounts = self::getActiveUserDiscounts($User);