Merge pull request #1313 from prashant-webkul/development

Development
This commit is contained in:
Jitendra Singh 2019-08-19 13:56:04 +05:30 committed by GitHub
commit b7bc6344bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 560 additions and 75 deletions

View File

@ -8,8 +8,8 @@ use DB;
/**
* AttributeDataGrid class
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AttributeDataGrid extends DataGrid
{

View File

@ -8,8 +8,8 @@ use DB;
/**
* AttributeFamilyDataGrid Class
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class AttributeFamilyDataGrid extends DataGrid
{

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

@ -170,7 +170,9 @@ use Webkul\Core\Repositories\LocaleRepository as Locale;
/**
* To update the previously created CMS page in storage
*
* @return view
* @param Integer $id
*
* @return View
*/
public function update($id)
{
@ -206,6 +208,8 @@ use Webkul\Core\Repositories\LocaleRepository as Locale;
/**
* To preview the content of the currently creating page or previously creating page
*
* @param Integer $id
*
* @return mixed
*/
public function preview($id)
@ -218,7 +222,9 @@ use Webkul\Core\Repositories\LocaleRepository as Locale;
/**
* To delete the previously create CMS page
*
* @return json
* @param Integer $id
*
* @return Response JSON
*/
public function delete($id)
{
@ -237,6 +243,8 @@ use Webkul\Core\Repositories\LocaleRepository as Locale;
/**
* To mass delete the CMS resource from storage
*
* @return Response redirect
*/
public function massDelete()
{

View File

@ -0,0 +1,8 @@
<?php
namespace Webkul\Discount\Actions\Catalog;
abstract class Action
{
abstract public function calculate($rule, $price);
}

View File

@ -0,0 +1,22 @@
<?php
namespace Webkul\Discount\Actions\Catalog;
use Webkul\Discount\Actions\Catalog\Action;
class AdjustToDiscountValue extends Action
{
public function calculate($rule, $product)
{
$discountAmount = $rule->discount_amount;
$price = $product->price;
if ($discountAmount <= $price) {
$discount = $price - $discountAmount;
return $discount;
} else {
return $price;
}
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Webkul\Discount\Actions\Catalog;
use Webkul\Discount\Actions\Catalog\Action;
class AdjustToPerenct extends Action
{
public function calculate($rule, $product)
{
$discountAmount = $rule->discount_amount;
$price = $product->price;
$discount = ($discountAmount / 100) * $price;
return $discount;
}
}

View File

@ -0,0 +1,20 @@
<?php
namespace Webkul\Discount\Actions\Catalog;
use Webkul\Discount\Actions\Catalog\Action;
class FixedAmount extends Action
{
public function calculate($rule, $product)
{
$discountAmount = $rule->discount_amount;
$price = $product->price;
if ($discountAmount <= $price) {
return $discountAmount;
} else {
return $price;
}
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Webkul\Discount\Actions\Catalog;
use Webkul\Discount\Actions\Catalog\Action;
class PercentOfOriginal extends Action
{
public function calculate($rule, $product)
{
$discountAmount = $rule->discount_amount;
$price = $product->price;
$discount = ($discountAmount / 100) * $price;
return $discount;
}
}

View File

@ -109,10 +109,10 @@ return [
'catalog' => [
'actions' => [
'percent_of_product' => 'Percentage of product',
'percent_of_original' => 'Percentage of product',
'fixed_amount' => 'Apply as fixed amount',
'adjust_to_percent' => 'Adjust price to percentage',
'adjust_to_fixed_amount' => 'Adjust price to given amount'
'final_price_to_percent' => 'Adjust price to percentage',
'to_discount_value' => 'Adjust price to given amount'
],
'validation' => [

View File

@ -8,6 +8,12 @@ use Webkul\Discount\Repositories\CatalogRuleProductsPriceRepository as CatalogRu
use Webkul\Discount\Helpers\Catalog\ConvertXToProductId as ConvertX;
use Webkul\Discount\Helpers\Catalog\Sale;
/**
* Apply - Applies catalog rule to products intended in the rules
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class Apply extends Sale
{
/**
@ -93,12 +99,20 @@ class Apply extends Sale
}
if ($this->active->count()) {
$productIDs = array();
$temp = collect();
foreach ($this->activeRules as $rule) {
$productIDs = $this->getProductIds($rule);
$this->setSale($rule, $productIDs);
$productIDs = $this->expandProducts($productIDs);
$result = $this->setSale($rule, $productIDs);
}
dd($result, 'processing done');
} else {
// handle the deceased rules here
dd($this->deceased);
}
}
@ -112,14 +126,14 @@ class Apply extends Sale
*/
public function setSale($rule, $productIDs)
{
dd($productIDs);
if (is_array($productIDs)) {
// apply on selected products
foreach ($productIDs as $productID) {
// $this->catalogRuleProduct->createOrUpdate($rule, $productID);
// catalog rule product resource is updated here
$this->catalogRuleProduct->createOrUpdate($rule, $productID);
// $this->catalogRuleProductPrice->createOrUpdate($rule, $productID);
// catalog rule product price resource is updated here
$this->catalogRuleProductPrice->createOrUpdate($rule, $productID);
}
} else if ($productIDs == '*') {
$this->catalogRuleProduct->createOrUpdate($rule, $productIDs);
@ -127,4 +141,100 @@ class Apply extends Sale
$this->catalogRuleProductPrice->createOrUpdate($rule, $productIDs);
}
}
/**
* To expand the productIDs of configurable products
*
* @param Array $productIDs
*
* @return Array
*/
protected function expandProducts($productIDs)
{
$products = app('Webkul\Product\Repositories\ProductRepository');
$newProductIDs = collect();
foreach ($productIDs as $productID) {
$product = $products->find($productID);
if ($product->type == 'configurable') {
$variants = $product->variants;
foreach($variants as $variant) {
$newProductIDs->push($variant->id);
}
} else {
$newProductIDs->push($productID);
}
}
if ($newProductIDs->count()) {
return $newProductIDs->toArray();
} else {
return [];
}
}
/**
* To break tie between two rules
*
* @param Integer $previousRuleID
* @param Integer $newRuleID
*
* @return String $id
*/
public function breakTie($previousRuleID, $newRuleID)
{
$oldRule = $this->catalogRule->find($previousRuleID);
$newRule = $this->catalogRule->find($newRuleID);
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

@ -9,6 +9,12 @@ use Webkul\Category\Repositories\CategoryRepository as Category;
use Webkul\Product\Repositories\ProductRepository as Product;
use Webkul\Product\Models\ProductAttributeValue as ProductAttributeValue;
/**
* ConvertXToProductID
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class ConvertXToProductId
{
/**

View File

@ -5,6 +5,12 @@ namespace Webkul\Discount\Helpers\Catalog;
use Webkul\Discount\Repositories\CatalogRepository as CatalogRule;
use Webkul\Discount\Helpers\Catalog\ConvertXToProductId;
/**
* Sale - Abstract class designed to initiate the application of Catalog Rules
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
abstract class Sale
{
abstract function apply();

View File

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

View File

@ -2,9 +2,6 @@
namespace Webkul\Discount\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Webkul\Attribute\Repositories\AttributeRepository as Attribute;
use Webkul\Attribute\Repositories\AttributeFamilyRepository as AttributeFamily;
use Webkul\Category\Repositories\CategoryRepository as Category;
@ -15,7 +12,7 @@ use Webkul\Discount\Repositories\CatalogRuleCustomerGroupsRepository as CatalogR
use Webkul\Discount\Helpers\Catalog\Apply;
/**
* Catalog Rule controller
* CatalogRule controller
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
@ -363,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

@ -5,10 +5,10 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Cart Rule Cart Reposotory
* CartRuleCartReposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartRuleCartRepository extends Repository
{

View File

@ -5,10 +5,10 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Cart Rule Reposotory
* CartRuleRepository
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartRuleChannelsRepository extends Repository
{

View File

@ -4,6 +4,12 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* CartRuleCouponsRepository
*
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartRuleCouponsRepository extends Repository
{
/**

View File

@ -5,10 +5,10 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Cart Rule Customer Groups Reposotory
* CartRuleCustomerGroupsReposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartRuleCustomerGroupsRepository extends Repository
{

View File

@ -4,6 +4,12 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* CartRuleLabelsRepository
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartRuleLabelsRepository extends Repository
{
/**

View File

@ -10,26 +10,46 @@ use Webkul\Discount\Repositories\CartRuleLabelsRepository as CartRuleLabels;
use Illuminate\Container\Container as App;
/**
* Cart Rule Reposotory
* CartRuleReposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CartRuleRepository extends Repository
{
/**
* Will hold cartRuleChannelsRepository instance
*/
protected $cartRuleChannels;
/**
* Will hold cartRuleCustomerGroupsRepository instance
*/
protected $cartRuleCustomerGroups;
/**
* Will hold cartRuleCoupons instance
*/
protected $cartRuleCoupons;
/**
* Will hold cartRuleCustomerGroupsRepository instance
*/
protected $cartRuleLabels;
/**
*
* @param CartRuleChannels $cartRuleChannels
* @param CartRuleCustomerGroups $cartRuleCustomerGroups
* @param CartRuleCoupons $cartRuleCoupons
* @param
*/
public function __construct(CartRuleChannels $cartRuleChannels, CartRuleCustomerGroups $cartRuleCustomerGroups, CartRuleCoupons $cartRuleCoupons, CartRuleLabels $cartRuleLabels, App $app)
{
public function __construct(
CartRuleChannels $cartRuleChannels,
CartRuleCustomerGroups $cartRuleCustomerGroups,
CartRuleCoupons $cartRuleCoupons,
CartRuleLabels $cartRuleLabels,
App $app
) {
$this->cartRuleChannels = $cartRuleChannels;
$this->cartRuleCustomerGroups = $cartRuleCustomerGroups;
$this->cartRuleCoupons = $cartRuleCoupons;

View File

@ -5,17 +5,17 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Catalog Rule Customer Groups Reposotory
* CatalogRuleChannelsReposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleChannelsRepository extends Repository
{
/**
* Specify Model class name
*
* @return mixed
* @return String
*/
function model()
{

View File

@ -5,17 +5,17 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
/**
* Catalog Rule Customer Groups Reposotory
* CatalogRuleCustomerGroupsReposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleCustomerGroupsRepository extends Repository
{
/**
* Specify Model class name
*
* @return mixed
* @return String
*/
function model()
{

View File

@ -4,29 +4,45 @@ namespace Webkul\Discount\Repositories;
use Webkul\Core\Eloquent\Repository;
use Webkul\Product\Repositories\ProductRepository as Product;
use Webkul\Discount\Repositories\CatalogRuleProductsRepository as CatalogRuleProduct;
use Illuminate\Container\Container as App;
/**
* Catalog Rule Products Price Reposotory
* CatalogRuleProductsPriceRepository
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleProductsPriceRepository extends Repository
{
/**
* ProductRepository instance
* To hold ProductRepository instance
*/
protected $product;
public function __construct(Product $product)
/**
* To hold CatalogRuleProductRepository instance
*
*/
protected $catalogRuleProduct;
/**
* @param Product $product
*/
public function __construct(Product $product, CatalogRuleProduct $catalogRuleProduct,App $app)
{
$this->product = $product;
$this->catalogRuleProduct = $catalogRuleProduct;
parent::__construct($app);
}
/**
* Specify Model class name
*
* @return mixed
* @return String
*/
function model()
{
@ -34,17 +50,133 @@ class CatalogRuleProductsPriceRepository extends Repository
}
/**
* Create or update resource
* Create or update catalog rule product resource
*
* @param CatalogRule $rule
* @param Integer $productID
*
* @return Void
*/
public function createOrUpdate($rule, $productID)
{
if ($productID == '*') {
$channels = $rule->channels;
$customerGroups = $rule->customer_groups;
} else {
$channelsGroupsCross = $channels->crossJoin($customerGroups);
if ($productID == '*') {
$products = $this->product->all('id');
foreach ($products as $product) {
foreach ($channelsGroupsCross as $channelGroup) {
$channelId = $channelGroup[0]->channel_id;
$groupId = $channelGroup[1]->customer_group_id;
foreach ($products as $product) {
$productID = $product->id;
$catalogRuleProductPrice = $this->findWhere([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
]);
if ($catalogRuleProductPrice->count()) {
// check for tie breaker rules and then update
$data = [
'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
];
$this->update($data, $catalogRuleProductPrice->first()->id);
} else {
$data = [
'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
];
$this->create($data);
}
}
}
} else {
foreach ($channelsGroupsCross as $channelGroup) {
$channelId = $channelGroup[0]->channel_id;
$groupId = $channelGroup[1]->customer_group_id;
$catalogRuleProduct = $this->catalogRuleProduct->findWhere([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
]);
$catalogRuleProductPrice = $this->findWhere([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
]);
if ($catalogRuleProductPrice->count() && $catalogRuleProductPrice->first()->catalog_rule_id != $rule->id) {
$catalogRuleProduct = $catalogRuleProduct->first();
$discountAmount = $rule->discount_amount;
$product = $this->product->find($productID);
$productPrice = $product->price - $catalogRuleProduct->action_amount;
$data = [
'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,
'rule_price' => $productPrice
];
// update
$catalogRuleProductPrice->first()->update($data);
} else if ($catalogRuleProductPrice->count() == 0) {
$catalogRuleProduct = $catalogRuleProduct->first();
$discountAmount = $rule->discount_amount;
$product = $this->product->find($productID);
$productPrice = $product->price - $catalogRuleProduct->action_amount;
if ($productPrice <= $discountAmount) {
$product->price = $productPrice - $discountAmount;
} else {
$product->price = 0;
}
$data = [
'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,
'rule_price' => $productPrice
];
$this->create($data);
} else {
// do the reassessment updation if the cart rule action changes the new action and its amount needs to be updated
}
}
}
}

View File

@ -7,10 +7,10 @@ use Webkul\Product\Repositories\ProductRepository as Product;
use Illuminate\Container\Container as App;
/**
* Catalog Rule Products Reposotory
* CatalogRuleProductsRepository
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleProductsRepository extends Repository
{
@ -19,6 +19,16 @@ class CatalogRuleProductsRepository extends Repository
*/
protected $product;
/**
* CatalogRule Apply instance
*/
protected $apply;
/**
* @param Product $product
* @param App $app
* @param Apply $apply
*/
public function __construct(Product $product, App $app)
{
$this->product = $product;
@ -26,10 +36,22 @@ class CatalogRuleProductsRepository extends Repository
parent::__construct($app);
}
public function getDiscountAmount($product, $rule)
{
$actionClass = config('discount-rules.catalog')[$rule->action_code];
$actionInstance = new $actionClass();
$discountAmount = $actionInstance->calculate($rule, $product);
return $discountAmount;
}
/**
* Specify Model class name
*
* @return Mixed
* @return String
*/
function model()
{
@ -38,6 +60,11 @@ class CatalogRuleProductsRepository extends Repository
/**
* Create or update catalog rule product resource
*
* @param CatalogRule $rule
* @param Integer $productID
*
* @return Void
*/
public function createOrUpdate($rule, $productID)
{
@ -58,15 +85,21 @@ class CatalogRuleProductsRepository extends Repository
foreach ($products as $product) {
$productID = $product->id;
$catalogRuleProduct = $model->where([
$catalogRuleProduct = $this->findWhere([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
])->get();
]);
if ($catalogRuleProduct->count()) {
// check for tie breaker rules and then update
$catalogRuleProduct->first()->update([
$previousRuleID = $catalogRuleProduct->first()->catalog_rule_id;
$newRuleID = $rule->id;
$winnerRuleId = $this->breakTie($previousRuleID, $newRuleID);
$data = [
'catalog_rule_id' => $rule->id,
'starts_from' => $rule->starts_from,
'ends_till' => $rule->ends_till,
@ -75,9 +108,15 @@ class CatalogRuleProductsRepository extends Repository
'product_id' => $productID,
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
]);
];
if ($rule->id == $winnerRuleId) {
$this->catalogRuleProduct->create($data);
} else {
$this->catalogRuleProduct->update($data, $catalogRuleProduct->first()->id);
}
} else {
$this->create([
$data = [
'catalog_rule_id' => $rule->id,
'starts_from' => $rule->starts_from,
'ends_till' => $rule->ends_till,
@ -86,7 +125,9 @@ class CatalogRuleProductsRepository extends Repository
'product_id' => $productID,
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
]);
];
$this->catalogRuleProduct->create($data);
}
}
}
@ -95,15 +136,25 @@ class CatalogRuleProductsRepository extends Repository
$channelId = $channelGroup[0]->channel_id;
$groupId = $channelGroup[1]->customer_group_id;
$model = new $this->model();
$catalogRuleProduct = $model->where([
$catalogRuleProduct = $this->findWhere([
'channel_id' => $channelId,
'customer_group_id' => $groupId,
'product_id' => $productID
])->get();
]);
if ($catalogRuleProduct->count() && $catalogRuleProduct->first()->catalog_rule_id != $rule->id) {
$product = $this->product->find($productID);
$productPrice = $product->price;
$discountAmount = $this->getDiscountAmount($product, $rule);
// 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,
@ -113,11 +164,15 @@ class CatalogRuleProductsRepository extends Repository
'channel_id' => $channelId,
'product_id' => $productID,
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
'action_amount' => $discountAmount
]);
} else {
// create
$this->create([
} else if ($catalogRuleProduct->count() == 0) {
$product = $this->product->find($productID);
$productPrice = $product->price;
$discountAmount = $this->getDiscountAmount($product, $rule);
$data = [
'catalog_rule_id' => $rule->id,
'starts_from' => $rule->starts_from,
'ends_till' => $rule->ends_till,
@ -125,8 +180,12 @@ class CatalogRuleProductsRepository extends Repository
'channel_id' => $channelId,
'product_id' => $productID,
'action_code' => $rule->action_code,
'action_amount' => $rule->discount_amount
]);
'action_amount' => $discountAmount
];
$this->create($data);
} else {
// do the reassessment updation if the cart rule action changes the new action and its amount needs to be updated
}
}
}

View File

@ -8,23 +8,32 @@ use Webkul\Discount\Repositories\CatalogRuleCustomerGroupsRepository as CatalogR
use Illuminate\Container\Container as App;
/**
* Catalog Rule Reposotory
* CatalogRuleReposotory
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
* @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class CatalogRuleRepository extends Repository
{
/**
* Will hold catalogRuleChannelsRepository instance
*/
protected $catalogRuleChannels;
/**
* Will hold catalogRuleCustomerGroupsRepository instance
*/
protected $catalogRuleCustomerGroups;
/**
*
* @param CatalogRuleChannels $catalogRuleChannels
* @param CatalogRuleCustomerGroups $catalogRuleCustomerGroups
* @param App $app
*/
public function __construct(CatalogRuleChannels $catalogRuleChannels, CatalogRuleCustomerGroups $catalogRuleCustomerGroups, App $app)
{
$this->catalogRuleChannels = $catalogRuleChannels;
$this->catalogRuleCustomerGroups = $catalogRuleCustomerGroups;
parent::__construct($app);
@ -33,7 +42,7 @@ class CatalogRuleRepository extends Repository
/**
* Specify Model class name
*
* @return mixed
* @return String
*/
function model()
{
@ -42,6 +51,11 @@ class CatalogRuleRepository extends Repository
/**
* To sync the customer groups related records
*
* @param Array $newCustomerGroups
* @param CatalogRule $catalogRule
*
* @return Boolean
*/
public function CustomerGroupSync($newCustomerGroups, $catalogRule)
{
@ -90,6 +104,11 @@ class CatalogRuleRepository extends Repository
/**
* To sync the channels related records
*
* @param Array $newChannels
* @param CatalogRule $catalogRule
*
* @return Boolean
*/
public function ChannelSync($newChannels, $catalogRule)
{