introduce channel and customer group filter for cart rule grid

This commit is contained in:
Herbert Maschke 2020-04-29 09:19:47 +02:00
parent 22b4079fdb
commit 3154d27f52
7 changed files with 205 additions and 78 deletions

View File

@ -11,18 +11,51 @@ class CartRuleDataGrid extends DataGrid
protected $sortOrder = 'desc';
protected $customer_group = 'all';
protected $channel = 'all';
public function __construct()
{
parent::__construct();
$this->customer_group = request()->get('customer_group') ?? 'all';
$this->channel = request()->get('channel') ?? 'all';
}
public function prepareQueryBuilder()
{
$queryBuilder = DB::table('cart_rules')
->leftJoin('cart_rule_coupons', function($leftJoin) {
->leftJoin('cart_rule_coupons', function ($leftJoin) {
$leftJoin->on('cart_rule_coupons.cart_rule_id', '=', 'cart_rules.id')
->where('cart_rule_coupons.is_primary', 1);
->where('cart_rule_coupons.is_primary', 1);
})
->addSelect('cart_rules.id', 'name', 'cart_rule_coupons.code as coupon_code', 'status', 'starts_from', 'ends_till', 'sort_order');
->addSelect('cart_rules.id', 'name', 'cart_rule_coupons.code as coupon_code',
'status', 'starts_from', 'ends_till', 'sort_order');
$this->addFilter('id', 'cart_rules.id');
$this->addFilter('coupon_code', 'cart_rule_coupons.code');
if ($this->customer_group !== 'all') {
$queryBuilder->leftJoin(
'cart_rule_customer_groups',
'cart_rule_customer_groups.cart_rule_id',
'=',
'cart_rules.id'
);
$queryBuilder->where('cart_rule_customer_groups.customer_group_id', $this->customer_group);
}
if ($this->channel !== 'all') {
$queryBuilder->leftJoin(
'cart_rule_channels',
'cart_rule_channels.cart_rule_id',
'=',
'cart_rules.id');
$queryBuilder->where('cart_rule_channels.channel_id', $this->channel);
}
$this->setQueryBuilder($queryBuilder);
}
@ -80,7 +113,7 @@ class CartRuleDataGrid extends DataGrid
'searchable' => true,
'sortable' => true,
'filterable' => true,
'wrapper' => function($value) {
'wrapper' => function ($value) {
if ($value->status == 1) {
return trans('admin::app.datagrid.active');
} else {

View File

@ -22,7 +22,7 @@ class ProductDataGrid extends DataGrid
parent::__construct();
$this->locale = request()->get('locale') ?? 'all';
$this->channel = request()->get('channel') ?? 'all';
}
@ -119,7 +119,7 @@ class ProductDataGrid extends DataGrid
'sortable' => true,
'searchable' => false,
'filterable' => true,
'wrapper' => function($value) {
'wrapper' => function ($value) {
if ($value->status == 1) {
return trans('admin::app.datagrid.active');
} else {
@ -144,7 +144,7 @@ class ProductDataGrid extends DataGrid
'sortable' => true,
'searchable' => false,
'filterable' => false,
'wrapper' => function($value) {
'wrapper' => function ($value) {
if (is_null($value->quantity)) {
return 0;
} else {
@ -161,7 +161,7 @@ class ProductDataGrid extends DataGrid
'method' => 'GET',
'route' => 'admin.catalog.products.edit',
'icon' => 'icon pencil-lg-icon',
'condition' => function() {
'condition' => function () {
return true;
},
]);

View File

@ -1306,8 +1306,9 @@ return [
'order-number-suffix' => 'Order Number Suffix',
'default' => 'Default',
'sandbox' => 'Sandbox',
'all-channels' => 'All',
'all-locales' => 'All',
'all-channels' => 'All Channels',
'all-locales' => 'All Locales',
'all-customer-groups' => 'All Customer groups',
'invoice-slip-design' => 'Invoice Slip Design',
'logo' => 'logo'
]

View File

@ -19,7 +19,6 @@
</option>
@foreach (core()->getAllChannels() as $channelModel)
<option
value="{{ $channelModel->code }}" {{ (isset($channel) && ($channelModel->code) == $channel) ? 'selected' : '' }}>
{{ $channelModel->name }}
@ -90,6 +89,6 @@
window.location.href = url.href;
}
</script>
@endpush

View File

@ -5,14 +5,47 @@
@stop
@section('content')
<div class="content">
<?php $customer_group = request()->get('customer_group') ?: null; ?>
<?php $channel = request()->get('channel') ?: null; ?>
<div class="page-header">
<div class="page-title">
<h1>{{ __('admin::app.promotions.cart-rules.title') }}</h1>
</div>
<div class="page-action">
<div class="control-group">
<select class="control" id="channel-switcher" name="channel" onchange="reloadPage('channel', this.value)" >
<option value="all" {{ ! isset($channel) ? 'selected' : '' }}>
{{ __('admin::app.admin.system.all-channels') }}
</option>
@foreach (core()->getAllChannels() as $channelModel)
<option
value="{{ $channelModel->id }}" {{ (isset($channel) && ($channelModel->id) == $channel) ? 'selected' : '' }}>
{{ $channelModel->name }}
</option>
@endforeach
</select>
</div>
<div class="control-group">
<select class="control" id="customer-group-switcher" name="customer_group" onchange="reloadPage('customer_group', this.value)" >
<option value="all" {{ ! isset($locale) ? 'selected' : '' }}>
{{ __('admin::app.admin.system.all-customer-groups') }}
</option>
@foreach (core()->getAllCustomerGroups() as $customerGroupModel)
<option
value="{{ $customerGroupModel->id }}" {{ (isset($customerGroupModel) && ($customerGroupModel->id) == $customer_group) ? 'selected' : '' }}>
{{ $customerGroupModel->name }}
</option>
@endforeach
</select>
</div>
<a href="{{ route('admin.cart-rules.create') }}" class="btn btn-lg btn-primary">
{{ __('admin::app.promotions.cart-rules.add-title') }}
</a>
@ -24,4 +57,15 @@
{!! $cartRuleGrid->render() !!}
</div>
</div>
@endsection
@endsection
@push('scripts')
<script>
function reloadPage(getVar, getVal) {
let url = new URL(window.location.href);
url.searchParams.set(getVar, getVal);
window.location.href = url.href;
}
</script>
@endpush

View File

@ -12,6 +12,7 @@ use Webkul\Core\Repositories\ChannelRepository;
use Webkul\Core\Repositories\LocaleRepository;
use Webkul\Core\Repositories\CoreConfigRepository;
use Illuminate\Support\Facades\Config;
use Webkul\Customer\Repositories\CustomerGroupRepository;
class Core
{
@ -57,6 +58,13 @@ class Core
*/
protected $localeRepository;
/**
* CustomerGroupRepository class
*
* @var CustomerGroupRepository
*/
protected $customerGroupRepository;
/**
* CoreConfigRepository class
*
@ -70,13 +78,14 @@ class Core
/**
* Create a new instance.
*
* @param \Webkul\Core\Repositories\ChannelRepository $channelRepository
* @param \Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param \Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
* @param \Webkul\Core\Repositories\CountryRepository $countryRepository
* @param \Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* @param \Webkul\Core\Repositories\LocaleRepository $localeRepository
* @param \Webkul\Core\Repositories\CoreConfigRepository $coreConfigRepository
* @param \Webkul\Core\Repositories\ChannelRepository $channelRepository
* @param \Webkul\Core\Repositories\CurrencyRepository $currencyRepository
* @param \Webkul\Core\Repositories\ExchangeRateRepository $exchangeRateRepository
* @param \Webkul\Core\Repositories\CountryRepository $countryRepository
* @param \Webkul\Core\Repositories\CountryStateRepository $countryStateRepository
* @param \Webkul\Core\Repositories\LocaleRepository $localeRepository
* @param \Webkul\Core\Repositories\CustomerGroupRepository $customerGroupRepository
* @param \Webkul\Core\Repositories\CoreConfigRepository $coreConfigRepository
*
* @return void
*/
@ -87,6 +96,7 @@ class Core
CountryRepository $countryRepository,
CountryStateRepository $countryStateRepository,
LocaleRepository $localeRepository,
CustomerGroupRepository $customerGroupRepository,
CoreConfigRepository $coreConfigRepository
)
{
@ -102,6 +112,8 @@ class Core
$this->localeRepository = $localeRepository;
$this->customerGroupRepository = $customerGroupRepository;
$this->coreConfigRepository = $coreConfigRepository;
}
@ -247,6 +259,22 @@ class Core
return $locale;
}
/**
* Returns all Customer Groups
*
* @return \Illuminate\Support\Collection
*/
public function getAllCustomerGroups()
{
static $customerGroups;
if ($customerGroups) {
return $customerGroups;
}
return $customerGroups = $this->customerGroupRepository->all();
}
/**
* Returns all currencies
*
@ -376,9 +404,10 @@ class Core
/**
* Converts price
*
* @param float $amount
* @param string $targetCurrencyCode
* @param string $orderCurrencyCode
* @param float $amount
* @param string $targetCurrencyCode
* @param string $orderCurrencyCode
*
* @return string
*/
public function convertPrice($amount, $targetCurrencyCode = null, $orderCurrencyCode = null)
@ -394,16 +423,16 @@ class Core
if (($targetCurrencyCode != $this->lastOrderCode)
&& ($targetCurrencyCode != $orderCurrencyCode)
&& ($orderCurrencyCode != $this->getBaseCurrencyCode())
&& ($orderCurrencyCode != $this->lastCurrencyCode)
&& ($orderCurrencyCode != $this->getBaseCurrencyCode())
&& ($orderCurrencyCode != $this->lastCurrencyCode)
) {
$amount = $this->convertToBasePrice($amount, $orderCurrencyCode);
}
}
$targetCurrency = ! $targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
if (! $targetCurrency) {
return $amount;
@ -429,15 +458,16 @@ class Core
/**
* Converts to base price
*
* @param float $amount
* @param string $targetCurrencyCode
* @param float $amount
* @param string $targetCurrencyCode
*
* @return string
*/
public function convertToBasePrice($amount, $targetCurrencyCode = null)
{
$targetCurrency = ! $targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
if (! $targetCurrency) {
return $amount;
@ -457,7 +487,8 @@ class Core
/**
* Format and convert price with currency symbol
*
* @param float $price
* @param float $price
*
* @return string
*/
public function currency($amount = 0)
@ -472,7 +503,8 @@ class Core
/**
* Return currency symbol from currency code
*
* @param float $price
* @param float $price
*
* @return string
*/
public function currencySymbol($code)
@ -483,17 +515,18 @@ class Core
}
/**
* Format and convert price with currency symbol
*
* @param float $price
* @return string
*/
* Format and convert price with currency symbol
*
* @param float $price
*
* @return string
*/
public function formatPrice($price, $currencyCode)
{
if (is_null($price))
$price = 0;
$formatter = new \NumberFormatter( app()->getLocale(), \NumberFormatter::CURRENCY );
$formatter = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY);
return $formatter->formatCurrency($price, $currencyCode);
}
@ -523,7 +556,8 @@ class Core
/**
* Format price with base currency symbol
*
* @param float $price
* @param float $price
*
* @return string
*/
public function formatBasePrice($price)
@ -551,8 +585,9 @@ class Core
* Checks if current date of the given channel (in the channel timezone) is within the range
*
* @param int|string|\Webkul\Core\Contracts\Channel $channel
* @param string|null $dateFrom
* @param string|null $dateTo
* @param string|null $dateFrom
* @param string|null $dateTo
*
* @return bool
*/
public function isChannelDateInInterval($dateFrom = null, $dateTo = null)
@ -583,7 +618,8 @@ class Core
/**
* Get channel timestamp, timstamp will be builded with channel timezone settings
*
* @param \Webkul\Core\Contracts\Channel $channel
* @param \Webkul\Core\Contracts\Channel $channel
*
* @return int
*/
public function channelTimeStamp($channel)
@ -604,7 +640,8 @@ class Core
/**
* Check whether sql date is empty
*
* @param string $date
* @param string $date
*
* @return bool
*/
function is_empty_date($date)
@ -615,8 +652,8 @@ class Core
/**
* Format date using current channel.
*
* @param \Illuminate\Support\Carbon|null $date
* @param string $format
* @param \Illuminate\Support\Carbon|null $date
* @param string $format
*
* @return string
*/
@ -636,9 +673,9 @@ class Core
/**
* Retrieve information from payment configuration
*
* @param string $field
* @param int|string|null $channelId
* @param string|null $locale
* @param string $field
* @param int|string|null $channelId
* @param string|null $locale
*
* @return mixed
*/
@ -707,7 +744,8 @@ class Core
/**
* Retrieve a group of information from the core config table
*
* @param mixed $criteria
* @param mixed $criteria
*
* @return mixed
*/
public function retrieveGroupConfig($criteria)
@ -728,7 +766,8 @@ class Core
/**
* Returns country name by code
*
* @param string $code
* @param string $code
*
* @return string
*/
public function country_name($code)
@ -741,7 +780,8 @@ class Core
/**
* Retrieve all country states
*
* @param string $countryCode
* @param string $countryCode
*
* @return \Illuminate\Support\Collection
*/
public function states($countryCode)
@ -786,8 +826,9 @@ class Core
/**
* Returns time intervals
*
* @param \Illuminate\Support\Carbon $startDate
* @param \Illuminate\Support\Carbon $endDate
* @param \Illuminate\Support\Carbon $startDate
* @param \Illuminate\Support\Carbon $endDate
*
* @return array
*/
public function getTimeInterval($startDate, $endDate)
@ -808,8 +849,8 @@ class Core
$start = Carbon::createFromTimeString($date->format('Y-m-d') . ' 00:00:01');
$end = $totalMonths - 1 == $i
? $endDate
: Carbon::createFromTimeString($date->format('Y-m-d') . ' 23:59:59');
? $endDate
: Carbon::createFromTimeString($date->format('Y-m-d') . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('M')];
}
@ -819,11 +860,11 @@ class Core
$date->addWeeks($i);
$start = $i == 0
? $startDate
: Carbon::createFromTimeString($this->xWeekRange($date, 0) . ' 00:00:01');
? $startDate
: Carbon::createFromTimeString($this->xWeekRange($date, 0) . ' 00:00:01');
$end = $totalWeeks - 1 == $i
? $endDate
: Carbon::createFromTimeString($this->xWeekRange($date, 1) . ' 23:59:59');
? $endDate
: Carbon::createFromTimeString($this->xWeekRange($date, 1) . ' 23:59:59');
$timeIntervals[] = ['start' => $start, 'end' => $end, 'formatedDate' => $date->format('d M')];
}
@ -844,8 +885,9 @@ class Core
/**
*
* @param string $date
* @param int $day
* @param string $date
* @param int $day
*
* @return string
*/
public function xWeekRange($date, $day)
@ -866,7 +908,8 @@ class Core
/**
* Method to sort through the acl items and put them in order
*
* @param array $items
* @param array $items
*
* @return array
*/
public function sortItems($items)
@ -889,7 +932,8 @@ class Core
}
/**
* @param string $fieldName
* @param string $fieldName
*
* @return array
*/
public function getConfigField($fieldName)
@ -908,7 +952,8 @@ class Core
}
/**
* @param array $items
* @param array $items
*
* @return array
*/
public function convertToAssociativeArray($items)
@ -941,9 +986,10 @@ class Core
}
/**
* @param array $items
* @param string $key
* @param string|int|float $value
* @param array $items
* @param string $key
* @param string|int|float $value
*
* @return array
*/
public function array_set(&$array, $key, $value)
@ -977,8 +1023,9 @@ class Core
}
/**
* @param array $array1
* @param array $array2
* @param array $array1
* @param array $array2
*
* @return array
*/
protected function arrayMerge(array &$array1, array &$array2)
@ -997,7 +1044,8 @@ class Core
}
/**
* @param array $array1
* @param array $array1
*
* @return array
*/
public function convertEmptyStringsToNull($array)
@ -1014,7 +1062,8 @@ class Core
/**
* Create singletom object through single facade
*
* @param string $className
* @param string $className
*
* @return object
*/
public function getSingletonInstance($className)
@ -1031,7 +1080,8 @@ class Core
/**
* Returns a string as selector part for identifying elements in views
*
* @param float $taxRate
* @param float $taxRate
*
* @return string
*/
public static function taxRateAsIdentifier(float $taxRate): string
@ -1051,8 +1101,8 @@ class Core
$sender_email = core()->getConfigData('general.general.email_settings.shop_email_from') ? core()->getConfigData('general.general.email_settings.shop_email_from') : config('mail.from.address');
return [
'name' => $sender_name,
'email' => $sender_email,
'name' => $sender_name,
'email' => $sender_email,
];
}
@ -1068,8 +1118,8 @@ class Core
$admin_email = core()->getConfigData('general.general.email_settings.admin_email') ? core()->getConfigData('general.general.email_settings.admin_email') : config('mail.admin.address');
return [
'name' => $admin_name,
'email' => $admin_email,
'name' => $admin_name,
'email' => $admin_email,
];
}
}