Catalog rule implementation in progress

This commit is contained in:
Prashant Singh 2019-08-12 19:23:33 +05:30
parent da093662db
commit 2a36c6bd0e
16 changed files with 275 additions and 108 deletions

View File

@ -689,7 +689,7 @@
delete this.attribute_values[i].options;
}
if (this.category_values.length > 0) {
if (this.category_values != null && this.category_values.length > 0) {
this.all_attributes.categories = this.category_values;
}

View File

@ -1,7 +1,7 @@
@extends('admin::layouts.content')
@section('page_title')
{{ __('admin::app.promotion.add-catalog-rule') }}
{{ __('admin::app.promotion.edit-catalog-rule') }}
@stop
@section('content')
@ -20,13 +20,13 @@
<h1>
<i class="icon angle-left-icon back-link" onclick="history.length > 1 ? history.go(-1) : window.location = '{{ url('/admin/dashboard') }}';"></i>
{{ __('admin::app.promotion.add-catalog-rule') }}
{{ __('admin::app.promotion.edit-catalog-rule') }}
</h1>
</div>
<div class="page-action fixed-action">
<button type="submit" class="btn btn-lg btn-primary">
{{ __('admin::app.promotion.save-btn-title') }}
{{ __('admin::app.promotion.edit-btn-title') }}
</button>
</div>
</div>

View File

@ -11,7 +11,8 @@ use Auth;
use Hash;
/**
* Customer controlller for the customer basically for the tasks of customers which will be done after customer authentication.
* Customer controlller for the customer basically for the tasks of customers which will be
* done after customer authentication.
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)

View File

@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCatalogRuleProductPriceTable extends Migration
class CreateCatalogRuleProductsPriceTable extends Migration
{
/**
* Run the migrations.
@ -13,7 +13,7 @@ class CreateCatalogRuleProductPriceTable extends Migration
*/
public function up()
{
Schema::create('catalog_rule_product_price', function (Blueprint $table) {
Schema::create('catalog_rule_products_price', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('channel_id')->unsigned();
$table->foreign('channel_id')->references('id')->on('channels')->onDelete('cascade');

View File

@ -3,6 +3,9 @@
namespace Webkul\Discount\Helpers\Catalog;
use Webkul\Discount\Repositories\CatalogRuleRepository as CatalogRule;
use Webkul\Discount\Repositories\CatalogRuleProductsRepository as CatalogRuleProducts;
use Webkul\Discount\Repositories\CatalogRuleProductsPriceRepository as CatalogRuleProductsPrice;
use Webkul\Discount\Helpers\Catalog\ConvertXToProductId as ConvertX;
use Webkul\Discount\Helpers\Catalog\Sale;
class Apply extends Sale
@ -22,6 +25,21 @@ class Apply extends Sale
*/
protected $deceased;
/**
* Holds convertXToProductId instance
*/
protected $convertX;
/**
* Hold catalog rule products repository instance
*/
protected $catalogRuleProduct;
/**
* Hold catalog rule products price repository instance
*/
protected $catalogRuleProductPrice;
/**
* To hold the rule classes
*/
@ -30,10 +48,20 @@ class Apply extends Sale
/**
* @param CatalogRule $catalogRule
*/
public function __construct(CatalogRule $catalogRule)
{
public function __construct(
CatalogRule $catalogRule,
ConvertX $convertX,
CatalogRuleProducts $catalogRuleProduct,
CatalogRuleProductsPrice $catalogRuleProductPrice
) {
$this->catalogRule = $catalogRule;
$this->convertX = $convertX;
$this->catalogRuleProduct = $catalogRuleProduct;
$this->catalogRuleProductPrice = $catalogRuleProductPrice;
$this->active = collect();
$this->deceased = collect();
@ -55,14 +83,39 @@ class Apply extends Sale
$this->active->push($rule->id);
// Job execution for active rules
$products = $this->getProductIds($rule);
$productIDs = $this->getProductIds($rule);
$this->setSalePrice($products);
$this->setSale($rule, $productIDs);
} else {
$this->deceased->push($rule->id);
// Job execution for deceased rules
}
}
dd('done');
}
/**
* To set the sale price for products falling catalog rule criteria
*
* @param CatalogRule $catalogRule
*
* @return Void
*/
public function setSale($rule, $productIDs)
{
if (is_array($productIDs)) {
// apply on selected products
foreach ($productIDs as $productID) {
$this->catalogRuleProduct->createOrUpdate($rule, $productID);
$this->catalogRuleProductPrice->createOrUpdate($rule, $productID);
}
} else if ($productIDs == '*') {
$this->catalogRuleProduct->createOrUpdate($rule, $productIDs);
$this->catalogRuleProductPrice->createOrUpdate($rule, $productIDs);
}
}
}

View File

@ -71,7 +71,7 @@ class ConvertXToProductId
public function convertX($attribute_conditions)
{
$attributeConditions = json_decode($attribute_conditions);
$attributeConditions = json_decode(json_decode($attribute_conditions));
$categoryValues = $attributeConditions->categories;

View File

@ -7,33 +7,6 @@ use Webkul\Discount\Helpers\Catalog\ConvertXToProductId;
abstract class Sale
{
/**
* To hold the catalog rule repository instance
*/
protected $catalogRule;
/**
* ConvertXToProductId instance
*/
protected $convertX;
/**
* To hold the rule classes
*/
protected $rules;
/**
* @param CatalogRule $catalogRule
*/
public function __construct(CatalogRule $catalogRule, ConvertXToProductId $convertX)
{
$this->catalogRule = $catalogRule;
$this->convertX = $convertX;
$this->rules = config('discount-rules.catalog');
}
abstract function apply();
/**
@ -81,23 +54,22 @@ abstract class Sale
/**
* Function to maintain products and catalog rule
*
* @param CatalogRule $rule
*
* @return String|NULL
*/
public function getProductIds($rule)
{
$productIDs = $rule->conditions;
if ($rule->conditions) {
$conditions = $rule->conditions;
// $convertX = new ConvertXToProductId();
dd($this->convertX);
$productIDs = $this->convertX->convertX($rule->conditions);
} else {
// apply on all products
$productIDs = '*';
}
dd($productIDs);
return $productIDs;
}
/**

View File

@ -117,7 +117,7 @@ class CartRuleController extends Controller
public function store()
{
$validated = $this->validate(request(), [
'name' => 'required|string',
'name' => 'required|string|unique:cart_rules,name',
'description' => 'string',
'customer_groups' => 'required|array',
'channels' => 'required|array',
@ -349,8 +349,9 @@ class CartRuleController extends Controller
*/
public function update($id)
{
dd(request()->all());
$this->validate(request(), [
'name' => 'required|string',
'name' => 'required|string|unique:cart_rules,name,'.$id,
'description' => 'string',
'customer_groups' => 'required|array',
'channels' => 'required|array',

View File

@ -104,12 +104,21 @@ class CatalogRuleController extends Controller
return view($this->_config['view']);
}
/**
* To load create form for catalog rule
*
* @return View
*/
public function create()
{
// dd($this->appliedConfig);
return view($this->_config['view'])->with('catalog_rule', [$this->appliedConfig, $this->category->getPartial(), $this->getStatesAndCountries(), $this->attribute->getPartial()]);
}
/**
* To store newly created catalog rule and store it
*
* @return Redirect
*/
public function store()
{
$this->validate(request(), [
@ -148,8 +157,11 @@ class CatalogRuleController extends Controller
unset($catalog_rule['criteria']);
$catalog_rule['conditions'] = $catalog_rule['all_conditions'];
unset($catalog_rule['all_conditions']);
$catalog_rule['conditions'] = json_encode($catalog_rule['conditions']);
if (isset($catalog_rule['disc_amount'])) {
$catalog_rule['actions'] = [
'action_code' => $catalog_rule['action_type'],
@ -173,7 +185,6 @@ class CatalogRuleController extends Controller
unset($catalog_rule['all_actions']);
$catalog_rule['actions'] = json_encode($catalog_rule['actions']);
$catalog_rule['conditions'] = json_encode($catalog_rule['conditions']);
$catalogRule = $this->catalogRule->create($catalog_rule);
@ -203,6 +214,13 @@ class CatalogRuleController extends Controller
}
}
/**
* To load edit for previously created catalog rule
*
* @param $id
*
* @return View
*/
public function edit($id)
{
$catalog_rule = $this->catalogRule->find($id);
@ -224,12 +242,19 @@ class CatalogRuleController extends Controller
]);
}
/**
* To update previously created catalog rule
*
* @param $id
*
* @return Redirect
*/
public function update($id)
{
$data = request()->input();
// $validated = \Validator::make($data, [
// 'name' => 'required|stringunique:catalog_rule,name,' . $id,
// 'name' => 'required|string|unique:catalog_rules,name,' . $id,
// 'starts_from' => 'present|nullable|date',
// 'ends_till' => 'present|nullable|date',
// 'description' => 'string',
@ -249,7 +274,8 @@ class CatalogRuleController extends Controller
// }
$this->validate(request(), [
'name' => 'required|string|unique:catalog_rules,name,' . $id,
'name' => 'required|string|unique:catalog_rules,name,'.$id,
// 'name' => 'required|string',
'starts_from' => 'present|nullable|date',
'ends_till' => 'present|nullable|date',
'description' => 'string',
@ -300,6 +326,9 @@ class CatalogRuleController extends Controller
];
}
$catalog_rule['discount_amount'] = $catalog_rule['disc_amount'];
unset($catalog_rule['disc_amount']);
unset($catalog_rule['apply']);
unset($catalog_rule['attributes']);
unset($catalog_rule['_token']);
@ -324,6 +353,11 @@ class CatalogRuleController extends Controller
}
}
/**
* To apply the catalog rules
*
* @return Redirect
*/
public function applyRules()
{
$this->sale->apply();
@ -342,6 +376,13 @@ class CatalogRuleController extends Controller
return $attributesWithOptions;
}
/**
* To delete existing catalog rule
*
* @param Integer $id
*
* @return Redirect
*/
public function destroy($id)
{
$catalogRule = $this->catalogRule->findOrFail($id);
@ -357,7 +398,6 @@ class CatalogRuleController extends Controller
}
}
/**
* Get Countries and states list from core helpers
*

View File

@ -3,13 +3,13 @@
namespace Webkul\Discount\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Discount\Contracts\CatalogRuleProducts as CatalogRuleProductContract;
use Webkul\Discount\Contracts\CatalogRuleProducts as CatalogRuleProductsContract;
use Webkul\Discount\Models\CatalogRuleChannelsProxy as CatalogRuleChannels;
use Webkul\Discount\Models\CatalogRuleCustomerGroupsProxy as CatalogRuleCustomerGroups;
class CatalogRuleProducts extends Model implements CatalogRuleProductContract
class CatalogRuleProducts extends Model implements CatalogRuleProductsContract
{
protected $table = 'catalog_rules_products';
protected $table = 'catalog_rule_products';
protected $guarded = ['created_at', 'updated_at'];
protected $fillable = ['catalog_rule_id', 'starts_from', 'ends_till', 'customer_group_id', 'channel_id', 'product_id', 'action_code', 'action_amount'];
}

View File

@ -3,11 +3,11 @@
namespace Webkul\Discount\Models;
use Illuminate\Database\Eloquent\Model;
use Webkul\Discount\Contracts\CatalogRuleProductsPrice as CatalogRuleProductPriceContract;
use Webkul\Discount\Contracts\CatalogRuleProductsPrice as CatalogRuleProductsPriceContract;
class CatalogRuleProductsPrice extends Model implements CatalogRuleProductContract
class CatalogRuleProductsPrice extends Model implements CatalogRuleProductsPriceContract
{
protected $table = 'catalog_rules_products_price';
protected $guarded = ['created_at', 'updated_at'];
protected $fillable = ['catalog_rule_id', 'starts_from', 'ends_till', 'customer_group_id', 'channel_id', 'product_id', 'rule_price'];
}

View File

@ -11,6 +11,7 @@ class ModuleServiceProvider extends BaseModuleServiceProvider
\Webkul\Discount\Models\CatalogRuleChannels::class,
\Webkul\Discount\Models\CatalogRuleCustomerGroups::class,
\Webkul\Discount\Models\CatalogRuleProducts::class,
\Webkul\Discount\Models\CatalogRuleProductsPrice::class,
\Webkul\Discount\Models\CartRule::class,
\Webkul\Discount\Models\CartRuleChannels::class,
\Webkul\Discount\Models\CartRuleCustomerGroups::class,

View File

@ -1,24 +0,0 @@
<?php
namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Catalog Rule Products Reposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleProductsRepository extends Repository
{
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Discount\Contracts\CatalogRuleProducts';
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Catalog Rule Products Price Reposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleProductsPriceRepository extends Repository
{
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Discount\Contracts\CatalogRuleProductsPrice';
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
use Webkul\Product\Repositories\ProductRepository as Product;
/**
* Catalog Rule Products Price Reposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleProductsPriceRepository extends Repository
{
/**
* ProductRepository instance
*/
protected $product;
public function __construct(Product $product)
{
$this->product = $product;
}
/**
* Specify Model class name
*
* @return mixed
*/
function model()
{
return 'Webkul\Discount\Contracts\CatalogRuleProductsPrice';
}
/**
* Create or update resource
*/
public function createOrUpdate($rule, $productID)
{
if ($productID == '*') {
} else {
$products = $this->product->all('id');
foreach ($products as $product) {
}
}
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
use Webkul\Product\Repositories\ProductRepository as Product;
use Illuminate\Container\Container as App;
/**
* Catalog Rule Products Reposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleProductsRepository extends Repository
{
/**
* ProductRepository instance
*/
protected $product;
public function __construct(Product $product, App $app)
{
$this->product = $product;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return Mixed
*/
function model()
{
return 'Webkul\Discount\Contracts\CatalogRuleProducts';
}
/**
* Create or update catalog rule product resource
*/
public function createOrUpdate($rule, $productID)
{
$channels = $rule->channels;
$customerGroups = $rule->customer_groups;
$channelsGroupsCross = $channels->crossJoin($customerGroups);
if ($productID == '*') {
$products = $this->product->all('id');
// for all products do that thing later
} else {
$product = $this->product->find($productID);
foreach ($channelsGroupsCross as $channelGroup) {
$channelId = $channelGroup[0]->channel_id;
$groupId = $channelGroup[1]->customer_group_id;
$model = new $this->model();
$catalogRuleProduct = $model->where([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
])->get();
if ($catalogRuleProduct->count()) {
// update
$catalogRuleProduct->first()->update([
'catalog_rule_id' => $rule->id,
'starts_from' => $rule->starts_from,
'ends_till' => $rule->ends_till,
'customer_group_id' => $groupId,
'channel_id' => $channelId,
'product_id' => $productID,
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
]);
} else {
// create
$this->create([
'catalog_rule_id' => $rule->id,
'starts_from' => $rule->starts_from,
'ends_till' => $rule->ends_till,
'customer_group_id' => $groupId,
'channel_id' => $channelId,
'product_id' => $productID,
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
]);
}
}
}
}
}