Catalog rule tie breaker content in progress

This commit is contained in:
Prashant Singh 2019-08-17 20:18:39 +05:30
parent 1d1018ac50
commit aca7b4c6c7
8 changed files with 113 additions and 20 deletions

View File

@ -638,6 +638,10 @@ Route::group(['middleware' => ['web']], function () {
Route::post('/catalog-rules/delete/{id}', 'Webkul\Discount\Http\Controllers\CatalogRuleController@destroy')->name('admin.catalog-rule.delete');
Route::get('/catalog-rules/declutter', 'Webkul\Discount\Http\Controllers\CatalogRuleController@deClutter')->defaults('_config', [
'redirect' => 'admin.catalog-rule.index'
])->name('admin.catalog-rule.declut');
Route::post('fetch/options', 'Webkul\Discount\Http\Controllers\CatalogRuleController@fetchAttributeOptions')->name('admin.catalog-rule.options');
Route::get('cart-rules', 'Webkul\Discount\Http\Controllers\CartRuleController@index')->defaults('_config', [

View File

@ -849,6 +849,9 @@ return [
'rule-name' => 'Enter Rule Name',
'rule-desc' => 'Enter Rule Description',
'convert-x-note' => 'If this section is left empty, then rule will get applied to all the products in the cart.',
'declut' => 'Declutter Rules',
'declut-success' => 'Catalog rules decluttering successful',
'declut-failure' => 'Catalog rules decluttering failed',
'add-attr-condition' => 'Add Attribute Condition',
'general-info' => [
'sku-like' => 'SKU Like',

View File

@ -13,12 +13,16 @@
</div>
<div class="page-action">
<a href="{{ route('admin.catalog-rule.create') }}" class="btn btn-lg btn-primary">
{{ __('admin::app.promotion.add-catalog-rule') }}
</a>
<a href="{{ route('admin.catalog-rule.apply') }}" class="btn btn-lg btn-primary">
{{ __('Apply Rules') }}
</a>
<a href="{{ route('admin.catalog-rule.create') }}" class="btn btn-lg btn-primary">
{{ __('admin::app.promotion.add-catalog-rule') }}
<a href="{{ route('admin.catalog-rule.declut') }}" class="btn btn-lg btn-primary">
{{ __('admin::app.promotion.declut') }}
</a>
</div>
</div>

View File

@ -99,11 +99,16 @@ class Apply extends Sale
}
if ($this->active->count()) {
$productIDs = array();
foreach ($this->activeRules as $rule) {
$productIDs = $this->getProductIds($rule);
array_push($productIDs, $this->getProductIds($rule));
}
dd(array_flatten($productIDs));
$this->setSale($rule, $productIDs);
}
} else {
dd($this->deceased);
}
@ -118,8 +123,6 @@ class Apply extends Sale
*/
public function setSale($rule, $productIDs)
{
dd($productIDs);
if (is_array($productIDs)) {
// apply on selected products
foreach ($productIDs as $productID) {
@ -161,11 +164,11 @@ class Apply extends Sale
foreach ($products as $product) {
$productID = $product->id;
$catalogRuleProduct = $model->where([
$catalogRuleProduct = $this->catalogRuleProduct->findWhere([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
])->get();
]);
if ($catalogRuleProduct->count()) {
// check for tie breaker rules and then update
@ -212,15 +215,21 @@ class Apply extends Sale
$channelId = $channelGroup[0]->channel_id;
$groupId = $channelGroup[1]->customer_group_id;
$model = new $this->model();
$catalogRuleProduct = $model->where([
$catalogRuleProduct = $this->catalogRuleProduct->findWhere([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
])->get();
]);
if ($catalogRuleProduct->count() && $catalogRuleProduct->first()->catalog_rule_id != $rule->id) {
// check for tie breaker rules and then update
$previousRuleID = $catalogRuleProduct->first()->catalog_rule_id;
$newRuleID = $rule->id;
$winnerRuleId = $this->breakTie($previousRuleID, $newRuleID);
if ($catalogRuleProduct->count()) {
// update
$catalogRuleProduct->first()->update([
'catalog_rule_id' => $rule->id,
@ -232,9 +241,9 @@ class Apply extends Sale
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
]);
} else {
} else if ($catalogRuleProduct->count() == 0) {
// create
$this->create([
$data = [
'catalog_rule_id' => $rule->id,
'starts_from' => $rule->starts_from,
'ends_till' => $rule->ends_till,
@ -243,7 +252,11 @@ class Apply extends Sale
'product_id' => $productID,
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
]);
];
$this->catalogRuleProduct->create($data);
} else {
// do the reassessment updation if the cart rule action changes the new action and its amount needs to be updated
}
}
}
@ -265,4 +278,49 @@ class Apply extends Sale
dd($oldRule->name, $newRule->name);
}
/**
* To declutter the catalog rules that are either inactive or deleted
*
* @return Boolean
*/
public function deClutter()
{
$rules = $this->catalogRule->all();
foreach ($rules as $rule) {
$validated = $this->checkApplicability($rule);
if (! $validated) {
$this->deceased->push($rule->id);
}
}
if (count($this->deceased)) {
$count = 0;
foreach ($this->deceased as $deceased) {
$cartRuleProducts = $this->catalogRuleProduct->findWhere([
'catalog_rule_id' => $deceased
]);
$cartRuleProductsPrice = $this->catalogRuleProductPrice->findWhere([
'catalog_rule_id' => $deceased
]);
// obvious logic for removing entries as entries in both storage needs to be exactly equal for a product
foreach ($cartRuleProducts as $cartRuleProduct) {
// deletes cartRuleProducts resource
$cartRuleProduct->delete();
// deletes cartRuleProductsPrice resource
$cartRuleProductsPrice->delete();
}
}
return true;
} else {
return false;
}
}
}

View File

@ -360,6 +360,24 @@ class CatalogRuleController extends Controller
$this->sale->apply();
}
/**
* Initiates decluttering of rules and even reindexes the table if empty
*
* @return Response redirect
*/
public function deClutter()
{
$result = $this->sale->deClutter();
if ($result) {
session()->flash('success', trans('admin::app.promotion.declut-success'));
} else {
session()->flash('warning', trans('admin::app.promotion.declut-failure'));
}
return redirect()->route($this->_config['redirect']);
}
public function fetchOptionableAttributes()
{
$attributesWithOptions = array();

View File

@ -7,7 +7,7 @@ use Webkul\Discount\Contracts\CatalogRuleProductsPrice as CatalogRuleProductsPri
class CatalogRuleProductsPrice extends Model implements CatalogRuleProductsPriceContract
{
protected $table = 'catalog_rules_products_price';
protected $table = 'catalog_rule_products_price';
protected $fillable = ['catalog_rule_id', 'starts_from', 'ends_till', 'customer_group_id', 'channel_id', 'product_id', 'rule_price'];
}

View File

@ -4,6 +4,7 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
use Webkul\Product\Repositories\ProductRepository as Product;
use Illuminate\Container\Container as App;
/**
* CatalogRuleProductsPriceRepository
@ -21,9 +22,11 @@ class CatalogRuleProductsPriceRepository extends Repository
/**
* @param Product $product
*/
public function __construct(Product $product)
public function __construct(Product $product, App $app)
{
$this->product = $product;
parent::__construct($app);
}
/**

View File

@ -30,10 +30,13 @@ class CatalogRuleRepository extends Repository
* @param CatalogRuleCustomerGroups $catalogRuleCustomerGroups
* @param App $app
*/
public function __construct(CatalogRuleChannels $catalogRuleChannels, CatalogRuleCustomerGroups $catalogRuleCustomerGroups)
public function __construct(CatalogRuleChannels $catalogRuleChannels, CatalogRuleCustomerGroups $catalogRuleCustomerGroups, App $app)
{
$this->catalogRuleChannels = $catalogRuleChannels;
$this->catalogRuleCustomerGroups = $catalogRuleCustomerGroups;
parent::__construct($app);
}
/**