From 1d1018ac5068bbf763e8fefdb5012f3038e88d13 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Sat, 17 Aug 2019 17:40:17 +0530 Subject: [PATCH 1/3] Refactoring of cart rules and syntax standard sugar --- packages/Webkul/Admin/src/Config/menu.php | 16 +- .../Admin/src/DataGrids/AttributeDataGrid.php | 4 +- .../src/DataGrids/AttributeFamilyDataGrid.php | 4 +- .../Http/Controllers/Admin/PageController.php | 12 +- .../Discount/src/Helpers/Catalog/Apply.php | 140 +++++++++++++++++- .../Helpers/Catalog/ConvertXToProductId.php | 6 + .../Discount/src/Helpers/Catalog/Sale.php | 6 + .../Controllers/CatalogRuleController.php | 5 +- .../Repositories/CartRuleCartRepository.php | 4 +- .../CartRuleChannelsRepository.php | 6 +- .../CartRuleCouponsRepository.php | 6 + .../CartRuleCustomerGroupsRepository.php | 4 +- .../Repositories/CartRuleLabelsRepository.php | 6 + .../src/Repositories/CartRuleRepository.php | 30 +++- .../CatalogRuleChannelsRepository.php | 6 +- .../CatalogRuleCustomerGroupsRepository.php | 6 +- .../CatalogRuleProductsPriceRepository.php | 27 +--- .../CatalogRuleProductsRepository.php | 114 ++------------ .../Repositories/CatalogRuleRepository.php | 30 +++- 19 files changed, 267 insertions(+), 165 deletions(-) diff --git a/packages/Webkul/Admin/src/Config/menu.php b/packages/Webkul/Admin/src/Config/menu.php index 1091a60fb..2c73ae44d 100755 --- a/packages/Webkul/Admin/src/Config/menu.php +++ b/packages/Webkul/Admin/src/Config/menu.php @@ -188,15 +188,13 @@ return [ 'route' => 'admin.cart-rule.index', 'sort' => 1, 'icon-class' => '', - ], - // [ - // 'key' => 'promotions.catalog-rule', - // 'name' => 'admin::app.promotion.catalog-rule', - // 'route' => 'admin.catalog-rule.index', - // 'sort' => 1, - // 'icon-class' => '', - // ], - [ + ], [ + 'key' => 'promotions.catalog-rule', + 'name' => 'admin::app.promotion.catalog-rule', + 'route' => 'admin.catalog-rule.index', + 'sort' => 1, + 'icon-class' => '', + ], [ 'key' => 'cms', 'name' => 'admin::app.layouts.cms', 'route' => 'admin.cms.index', diff --git a/packages/Webkul/Admin/src/DataGrids/AttributeDataGrid.php b/packages/Webkul/Admin/src/DataGrids/AttributeDataGrid.php index 094b219aa..382944d1c 100755 --- a/packages/Webkul/Admin/src/DataGrids/AttributeDataGrid.php +++ b/packages/Webkul/Admin/src/DataGrids/AttributeDataGrid.php @@ -8,8 +8,8 @@ use DB; /** * AttributeDataGrid class * - * @author Prashant Singh @prashant-webkul - * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + * @author Prashant Singh @prashant-webkul + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) */ class AttributeDataGrid extends DataGrid { diff --git a/packages/Webkul/Admin/src/DataGrids/AttributeFamilyDataGrid.php b/packages/Webkul/Admin/src/DataGrids/AttributeFamilyDataGrid.php index 613b72ff9..0c631a4ed 100755 --- a/packages/Webkul/Admin/src/DataGrids/AttributeFamilyDataGrid.php +++ b/packages/Webkul/Admin/src/DataGrids/AttributeFamilyDataGrid.php @@ -8,8 +8,8 @@ use DB; /** * AttributeFamilyDataGrid Class * - * @author Prashant Singh @prashant-webkul - * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + * @author Prashant Singh @prashant-webkul + * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) */ class AttributeFamilyDataGrid extends DataGrid { diff --git a/packages/Webkul/CMS/src/Http/Controllers/Admin/PageController.php b/packages/Webkul/CMS/src/Http/Controllers/Admin/PageController.php index d635fb763..2dffd9771 100644 --- a/packages/Webkul/CMS/src/Http/Controllers/Admin/PageController.php +++ b/packages/Webkul/CMS/src/Http/Controllers/Admin/PageController.php @@ -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() { diff --git a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php index 823c3df24..f46c611db 100644 --- a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php +++ b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php @@ -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-webkul + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) + */ class Apply extends Sale { /** @@ -117,7 +123,7 @@ class Apply extends Sale if (is_array($productIDs)) { // apply on selected products foreach ($productIDs as $productID) { - // $this->catalogRuleProduct->createOrUpdate($rule, $productID); + $this->createOrUpdate($rule, $productID); // $this->catalogRuleProductPrice->createOrUpdate($rule, $productID); } @@ -127,4 +133,136 @@ class Apply extends Sale $this->catalogRuleProductPrice->createOrUpdate($rule, $productIDs); } } + + /** + * Create or update catalog rule product resource + * + * @param CatalogRule $rule + * @param Integer $productID + * + * @return Void + */ + public function createOrUpdate($rule, $productID) + { + $channels = $rule->channels; + $customerGroups = $rule->customer_groups; + + $channelsGroupsCross = $channels->crossJoin($customerGroups); + + if ($productID == '*') { + $products = $this->product->all('id'); + + foreach ($channelsGroupsCross as $channelGroup) { + $channelId = $channelGroup[0]->channel_id; + $groupId = $channelGroup[1]->customer_group_id; + + $model = new $this->model(); + + foreach ($products as $product) { + $productID = $product->id; + + $catalogRuleProduct = $model->where([ + 'channel_id' => $channelId, + 'customer_group_id' => $groupId, + 'product_id' => $productID + ])->get(); + + if ($catalogRuleProduct->count()) { + // check for tie breaker rules and then 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, + 'customer_group_id' => $groupId, + 'channel_id' => $channelId, + '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 { + $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->catalogRuleProduct->create($data); + } + } + } + } else { + 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 + ]); + } + } + } + } + + /** + * 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); + } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Helpers/Catalog/ConvertXToProductId.php b/packages/Webkul/Discount/src/Helpers/Catalog/ConvertXToProductId.php index 6c1e3c632..4c000f893 100644 --- a/packages/Webkul/Discount/src/Helpers/Catalog/ConvertXToProductId.php +++ b/packages/Webkul/Discount/src/Helpers/Catalog/ConvertXToProductId.php @@ -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-webkul + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) + */ class ConvertXToProductId { /** diff --git a/packages/Webkul/Discount/src/Helpers/Catalog/Sale.php b/packages/Webkul/Discount/src/Helpers/Catalog/Sale.php index 8fbb1cce0..93087ec4b 100644 --- a/packages/Webkul/Discount/src/Helpers/Catalog/Sale.php +++ b/packages/Webkul/Discount/src/Helpers/Catalog/Sale.php @@ -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-webkul + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) + */ abstract class Sale { abstract function apply(); diff --git a/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php b/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php index 31db687f2..3ba311928 100644 --- a/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php +++ b/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php @@ -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-webkul * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) diff --git a/packages/Webkul/Discount/src/Repositories/CartRuleCartRepository.php b/packages/Webkul/Discount/src/Repositories/CartRuleCartRepository.php index 39523db45..e891556b0 100644 --- a/packages/Webkul/Discount/src/Repositories/CartRuleCartRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CartRuleCartRepository.php @@ -5,10 +5,10 @@ namespace Webkul\Discount\Repositories; use Webkul\Core\Eloquent\Repository; /** - * Cart Rule Cart Reposotory + * CartRuleCartReposotory * * @author Prashant Singh - * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) */ class CartRuleCartRepository extends Repository { diff --git a/packages/Webkul/Discount/src/Repositories/CartRuleChannelsRepository.php b/packages/Webkul/Discount/src/Repositories/CartRuleChannelsRepository.php index fe8680e40..2d6209445 100644 --- a/packages/Webkul/Discount/src/Repositories/CartRuleChannelsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CartRuleChannelsRepository.php @@ -5,10 +5,10 @@ namespace Webkul\Discount\Repositories; use Webkul\Core\Eloquent\Repository; /** - * Cart Rule Reposotory + * CartRuleRepository * - * @author Prashant Singh - * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + * @author Prashant Singh @prashant-webkul + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) */ class CartRuleChannelsRepository extends Repository { diff --git a/packages/Webkul/Discount/src/Repositories/CartRuleCouponsRepository.php b/packages/Webkul/Discount/src/Repositories/CartRuleCouponsRepository.php index 6db964d5b..8b0e53f99 100644 --- a/packages/Webkul/Discount/src/Repositories/CartRuleCouponsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CartRuleCouponsRepository.php @@ -4,6 +4,12 @@ namespace Webkul\Discount\Repositories; use Webkul\Core\Eloquent\Repository; +/** + * CartRuleCouponsRepository + * + * @author Prashant Singh @prashant-webkul + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) + */ class CartRuleCouponsRepository extends Repository { /** diff --git a/packages/Webkul/Discount/src/Repositories/CartRuleCustomerGroupsRepository.php b/packages/Webkul/Discount/src/Repositories/CartRuleCustomerGroupsRepository.php index a2038e507..cfd4dbe75 100644 --- a/packages/Webkul/Discount/src/Repositories/CartRuleCustomerGroupsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CartRuleCustomerGroupsRepository.php @@ -5,10 +5,10 @@ namespace Webkul\Discount\Repositories; use Webkul\Core\Eloquent\Repository; /** - * Cart Rule Customer Groups Reposotory + * CartRuleCustomerGroupsReposotory * * @author Prashant Singh - * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) */ class CartRuleCustomerGroupsRepository extends Repository { diff --git a/packages/Webkul/Discount/src/Repositories/CartRuleLabelsRepository.php b/packages/Webkul/Discount/src/Repositories/CartRuleLabelsRepository.php index ba5ba2aaa..2a4d03be7 100644 --- a/packages/Webkul/Discount/src/Repositories/CartRuleLabelsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CartRuleLabelsRepository.php @@ -4,6 +4,12 @@ namespace Webkul\Discount\Repositories; use Webkul\Core\Eloquent\Repository; +/** + * CartRuleLabelsRepository + * + * @author Prashant Singh + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) + */ class CartRuleLabelsRepository extends Repository { /** diff --git a/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php b/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php index a28f27aa7..e45911cce 100644 --- a/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CartRuleRepository.php @@ -10,26 +10,46 @@ use Webkul\Discount\Repositories\CartRuleLabelsRepository as CartRuleLabels; use Illuminate\Container\Container as App; /** - * Cart Rule Reposotory + * CartRuleReposotory * * @author Prashant Singh - * @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; diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleChannelsRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleChannelsRepository.php index dbdf996ec..50ade8dbc 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleChannelsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleChannelsRepository.php @@ -5,17 +5,17 @@ namespace Webkul\Discount\Repositories; use Webkul\Core\Eloquent\Repository; /** - * Catalog Rule Customer Groups Reposotory + * CatalogRuleChannelsReposotory * * @author Prashant Singh - * @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() { diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleCustomerGroupsRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleCustomerGroupsRepository.php index 76323d394..2d7184610 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleCustomerGroupsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleCustomerGroupsRepository.php @@ -5,17 +5,17 @@ namespace Webkul\Discount\Repositories; use Webkul\Core\Eloquent\Repository; /** - * Catalog Rule Customer Groups Reposotory + * CatalogRuleCustomerGroupsReposotory * * @author Prashant Singh - * @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() { diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php index 99a146db6..4772a9dcf 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php @@ -6,10 +6,10 @@ use Webkul\Core\Eloquent\Repository; use Webkul\Product\Repositories\ProductRepository as Product; /** - * Catalog Rule Products Price Reposotory + * CatalogRuleProductsPriceRepository * - * @author Prashant Singh - * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + * @author Prashant Singh @prashant-webkul + * @copyright 2019 Webkul Software Pvt Ltd (http://www.webkul.com) */ class CatalogRuleProductsPriceRepository extends Repository { @@ -18,6 +18,9 @@ class CatalogRuleProductsPriceRepository extends Repository */ protected $product; + /** + * @param Product $product + */ public function __construct(Product $product) { $this->product = $product; @@ -26,26 +29,10 @@ class CatalogRuleProductsPriceRepository extends Repository /** * Specify Model class name * - * @return mixed + * @return String */ 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) { - - } - } - } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php index 609a1ea83..8838c9dea 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php @@ -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 - * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) + * @author Prashant Singh @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; @@ -29,106 +39,10 @@ class CatalogRuleProductsRepository extends Repository /** * Specify Model class name * - * @return Mixed + * @return String */ 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'); - - foreach ($channelsGroupsCross as $channelGroup) { - $channelId = $channelGroup[0]->channel_id; - $groupId = $channelGroup[1]->customer_group_id; - - $model = new $this->model(); - - foreach ($products as $product) { - $productID = $product->id; - - $catalogRuleProduct = $model->where([ - '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([ - '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 { - $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 - ]); - } - } - } - } else { - 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 - ]); - } - } - } - } } diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php index d70f9ff75..a68f5aba6 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php @@ -8,32 +8,38 @@ use Webkul\Discount\Repositories\CatalogRuleCustomerGroupsRepository as CatalogR use Illuminate\Container\Container as App; /** - * Catalog Rule Reposotory + * CatalogRuleReposotory * * @author Prashant Singh - * @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) + public function __construct(CatalogRuleChannels $catalogRuleChannels, CatalogRuleCustomerGroups $catalogRuleCustomerGroups) { $this->catalogRuleChannels = $catalogRuleChannels; $this->catalogRuleCustomerGroups = $catalogRuleCustomerGroups; - - parent::__construct($app); } /** * Specify Model class name * - * @return mixed + * @return String */ function model() { @@ -42,6 +48,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 +101,11 @@ class CatalogRuleRepository extends Repository /** * To sync the channels related records + * + * @param Array $newChannels + * @param CatalogRule $catalogRule + * + * @return Boolean */ public function ChannelSync($newChannels, $catalogRule) { From aca7b4c6c79b5df7cf17b9cab9f9a502384bc579 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Sat, 17 Aug 2019 20:18:39 +0530 Subject: [PATCH 2/3] Catalog rule tie breaker content in progress --- packages/Webkul/Admin/src/Http/routes.php | 4 + .../Admin/src/Resources/lang/en/app.php | 3 + .../promotions/catalog-rule/index.blade.php | 8 +- .../Discount/src/Helpers/Catalog/Apply.php | 88 +++++++++++++++---- .../Controllers/CatalogRuleController.php | 18 ++++ .../src/Models/CatalogRuleProductsPrice.php | 2 +- .../CatalogRuleProductsPriceRepository.php | 5 +- .../Repositories/CatalogRuleRepository.php | 5 +- 8 files changed, 113 insertions(+), 20 deletions(-) diff --git a/packages/Webkul/Admin/src/Http/routes.php b/packages/Webkul/Admin/src/Http/routes.php index 5782bbf7e..f7112a0f6 100755 --- a/packages/Webkul/Admin/src/Http/routes.php +++ b/packages/Webkul/Admin/src/Http/routes.php @@ -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', [ diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index 5be44fd1f..62a1ed826 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -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', diff --git a/packages/Webkul/Admin/src/Resources/views/promotions/catalog-rule/index.blade.php b/packages/Webkul/Admin/src/Resources/views/promotions/catalog-rule/index.blade.php index f0bbfc8ca..2f807cdae 100644 --- a/packages/Webkul/Admin/src/Resources/views/promotions/catalog-rule/index.blade.php +++ b/packages/Webkul/Admin/src/Resources/views/promotions/catalog-rule/index.blade.php @@ -13,12 +13,16 @@ diff --git a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php index f46c611db..371298b8b 100644 --- a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php +++ b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php @@ -99,11 +99,16 @@ class Apply extends Sale } if ($this->active->count()) { - foreach ($this->activeRules as $rule) { - $productIDs = $this->getProductIds($rule); + $productIDs = array(); - $this->setSale($rule, $productIDs); + foreach ($this->activeRules as $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; + } + } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php b/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php index 3ba311928..76b75c283 100644 --- a/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php +++ b/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php @@ -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(); diff --git a/packages/Webkul/Discount/src/Models/CatalogRuleProductsPrice.php b/packages/Webkul/Discount/src/Models/CatalogRuleProductsPrice.php index 61f439e26..f9597a0a4 100644 --- a/packages/Webkul/Discount/src/Models/CatalogRuleProductsPrice.php +++ b/packages/Webkul/Discount/src/Models/CatalogRuleProductsPrice.php @@ -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']; } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php index 4772a9dcf..69d8aad59 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php @@ -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); } /** diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php index a68f5aba6..527a0a915 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php @@ -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); } /** From 8a9198e5d9efda0689d0cc74023781324bac3748 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Mon, 19 Aug 2019 12:36:43 +0530 Subject: [PATCH 3/3] Catalog rules tie break in progress, squased a line of dead code in CartRuleController --- packages/Webkul/Admin/src/Config/menu.php | 16 +- .../Discount/src/Actions/Catalog/Action.php | 8 + .../Actions/Catalog/AdjustToDiscountValue.php | 22 +++ .../src/Actions/Catalog/AdjustToPercent.php | 19 +++ .../src/Actions/Catalog/FixedAmount.php | 20 +++ .../src/Actions/Catalog/PercentOfOriginal.php | 19 +++ .../Discount/src/Config/rule-conditions.php | 6 +- .../Discount/src/Helpers/Catalog/Apply.php | 148 ++++-------------- .../Http/Controllers/CartRuleController.php | 1 - .../CatalogRuleProductsPriceRepository.php | 146 ++++++++++++++++- .../CatalogRuleProductsRepository.php | 145 +++++++++++++++++ 11 files changed, 420 insertions(+), 130 deletions(-) create mode 100644 packages/Webkul/Discount/src/Actions/Catalog/Action.php diff --git a/packages/Webkul/Admin/src/Config/menu.php b/packages/Webkul/Admin/src/Config/menu.php index 2c73ae44d..1091a60fb 100755 --- a/packages/Webkul/Admin/src/Config/menu.php +++ b/packages/Webkul/Admin/src/Config/menu.php @@ -188,13 +188,15 @@ return [ 'route' => 'admin.cart-rule.index', 'sort' => 1, 'icon-class' => '', - ], [ - 'key' => 'promotions.catalog-rule', - 'name' => 'admin::app.promotion.catalog-rule', - 'route' => 'admin.catalog-rule.index', - 'sort' => 1, - 'icon-class' => '', - ], [ + ], + // [ + // 'key' => 'promotions.catalog-rule', + // 'name' => 'admin::app.promotion.catalog-rule', + // 'route' => 'admin.catalog-rule.index', + // 'sort' => 1, + // 'icon-class' => '', + // ], + [ 'key' => 'cms', 'name' => 'admin::app.layouts.cms', 'route' => 'admin.cms.index', diff --git a/packages/Webkul/Discount/src/Actions/Catalog/Action.php b/packages/Webkul/Discount/src/Actions/Catalog/Action.php new file mode 100644 index 000000000..ddd2ddc56 --- /dev/null +++ b/packages/Webkul/Discount/src/Actions/Catalog/Action.php @@ -0,0 +1,8 @@ +discount_amount; + $price = $product->price; + + if ($discountAmount <= $price) { + $discount = $price - $discountAmount; + + return $discount; + } else { + return $price; + } + } +} \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Actions/Catalog/AdjustToPercent.php b/packages/Webkul/Discount/src/Actions/Catalog/AdjustToPercent.php index e69de29bb..6c085a63a 100644 --- a/packages/Webkul/Discount/src/Actions/Catalog/AdjustToPercent.php +++ b/packages/Webkul/Discount/src/Actions/Catalog/AdjustToPercent.php @@ -0,0 +1,19 @@ +discount_amount; + + $price = $product->price; + + $discount = ($discountAmount / 100) * $price; + + return $discount; + } +} \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Actions/Catalog/FixedAmount.php b/packages/Webkul/Discount/src/Actions/Catalog/FixedAmount.php index e69de29bb..d37f65df9 100644 --- a/packages/Webkul/Discount/src/Actions/Catalog/FixedAmount.php +++ b/packages/Webkul/Discount/src/Actions/Catalog/FixedAmount.php @@ -0,0 +1,20 @@ +discount_amount; + $price = $product->price; + + if ($discountAmount <= $price) { + return $discountAmount; + } else { + return $price; + } + } +} \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Actions/Catalog/PercentOfOriginal.php b/packages/Webkul/Discount/src/Actions/Catalog/PercentOfOriginal.php index e69de29bb..4fc7c0408 100644 --- a/packages/Webkul/Discount/src/Actions/Catalog/PercentOfOriginal.php +++ b/packages/Webkul/Discount/src/Actions/Catalog/PercentOfOriginal.php @@ -0,0 +1,19 @@ +discount_amount; + + $price = $product->price; + + $discount = ($discountAmount / 100) * $price; + + return $discount; + } +} \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Config/rule-conditions.php b/packages/Webkul/Discount/src/Config/rule-conditions.php index 20c371df9..85df3ce89 100644 --- a/packages/Webkul/Discount/src/Config/rule-conditions.php +++ b/packages/Webkul/Discount/src/Config/rule-conditions.php @@ -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' => [ diff --git a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php index 371298b8b..69af195c5 100644 --- a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php +++ b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php @@ -100,16 +100,19 @@ class Apply extends Sale if ($this->active->count()) { $productIDs = array(); + $temp = collect(); foreach ($this->activeRules as $rule) { - array_push($productIDs, $this->getProductIds($rule)); + $productIDs = $this->getProductIds($rule); + + $productIDs = $this->expandProducts($productIDs); + + $result = $this->setSale($rule, $productIDs); } - dd(array_flatten($productIDs)); - - $this->setSale($rule, $productIDs); - + dd($result, 'processing done'); } else { + // handle the deceased rules here dd($this->deceased); } } @@ -126,9 +129,11 @@ class Apply extends Sale if (is_array($productIDs)) { // apply on selected products foreach ($productIDs as $productID) { - $this->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); @@ -138,127 +143,36 @@ class Apply extends Sale } /** - * Create or update catalog rule product resource + * To expand the productIDs of configurable products * - * @param CatalogRule $rule - * @param Integer $productID + * @param Array $productIDs * - * @return Void + * @return Array */ - public function createOrUpdate($rule, $productID) + protected function expandProducts($productIDs) { - $channels = $rule->channels; - $customerGroups = $rule->customer_groups; + $products = app('Webkul\Product\Repositories\ProductRepository'); - $channelsGroupsCross = $channels->crossJoin($customerGroups); + $newProductIDs = collect(); - if ($productID == '*') { - $products = $this->product->all('id'); + foreach ($productIDs as $productID) { + $product = $products->find($productID); - foreach ($channelsGroupsCross as $channelGroup) { - $channelId = $channelGroup[0]->channel_id; - $groupId = $channelGroup[1]->customer_group_id; + if ($product->type == 'configurable') { + $variants = $product->variants; - $model = new $this->model(); - - foreach ($products as $product) { - $productID = $product->id; - - $catalogRuleProduct = $this->catalogRuleProduct->findWhere([ - 'channel_id' => $channelId, - 'customer_group_id' => $groupId, - 'product_id' => $productID - ]); - - if ($catalogRuleProduct->count()) { - // check for tie breaker rules and then 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, - 'customer_group_id' => $groupId, - 'channel_id' => $channelId, - '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 { - $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->catalogRuleProduct->create($data); - } + foreach($variants as $variant) { + $newProductIDs->push($variant->id); } + } else { + $newProductIDs->push($productID); } + } + + if ($newProductIDs->count()) { + return $newProductIDs->toArray(); } 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 - ]); - - 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); - - // 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 if ($catalogRuleProduct->count() == 0) { - // create - $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->catalogRuleProduct->create($data); - } else { - // do the reassessment updation if the cart rule action changes the new action and its amount needs to be updated - } - } + return []; } } diff --git a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php index 72666418d..5f99edbf4 100644 --- a/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php +++ b/packages/Webkul/Discount/src/Http/Controllers/CartRuleController.php @@ -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', diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php index 69d8aad59..05e60735b 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php @@ -4,6 +4,7 @@ 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; /** @@ -15,17 +16,26 @@ use Illuminate\Container\Container as App; class CatalogRuleProductsPriceRepository extends Repository { /** - * ProductRepository instance + * To hold ProductRepository instance */ protected $product; + /** + * To hold CatalogRuleProductRepository instance + * + */ + protected $catalogRuleProduct; + /** * @param Product $product */ - public function __construct(Product $product, App $app) + public function __construct(Product $product, CatalogRuleProduct $catalogRuleProduct,App $app) { + $this->product = $product; + $this->catalogRuleProduct = $catalogRuleProduct; + parent::__construct($app); } @@ -38,4 +48,136 @@ class CatalogRuleProductsPriceRepository extends Repository { return 'Webkul\Discount\Contracts\CatalogRuleProductsPrice'; } + + /** + * Create or update catalog rule product resource + * + * @param CatalogRule $rule + * @param Integer $productID + * + * @return Void + */ + public function createOrUpdate($rule, $productID) + { + $channels = $rule->channels; + $customerGroups = $rule->customer_groups; + + $channelsGroupsCross = $channels->crossJoin($customerGroups); + + if ($productID == '*') { + $products = $this->product->all('id'); + + 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 + } + } + } + } } \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php index 8838c9dea..f490930ab 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php @@ -36,6 +36,18 @@ 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 * @@ -45,4 +57,137 @@ class CatalogRuleProductsRepository extends Repository { return 'Webkul\Discount\Contracts\CatalogRuleProducts'; } + + /** + * Create or update catalog rule product resource + * + * @param CatalogRule $rule + * @param Integer $productID + * + * @return Void + */ + public function createOrUpdate($rule, $productID) + { + $channels = $rule->channels; + $customerGroups = $rule->customer_groups; + + $channelsGroupsCross = $channels->crossJoin($customerGroups); + + if ($productID == '*') { + $products = $this->product->all('id'); + + foreach ($channelsGroupsCross as $channelGroup) { + $channelId = $channelGroup[0]->channel_id; + $groupId = $channelGroup[1]->customer_group_id; + + $model = new $this->model(); + + foreach ($products as $product) { + $productID = $product->id; + + $catalogRuleProduct = $this->findWhere([ + 'channel_id' => $channelId, + 'customer_group_id' => $groupId, + 'product_id' => $productID + ]); + + if ($catalogRuleProduct->count()) { + // check for tie breaker rules and then 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, + 'customer_group_id' => $groupId, + 'channel_id' => $channelId, + '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 { + $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->catalogRuleProduct->create($data); + } + } + } + } else { + foreach ($channelsGroupsCross as $channelGroup) { + $channelId = $channelGroup[0]->channel_id; + $groupId = $channelGroup[1]->customer_group_id; + + $catalogRuleProduct = $this->findWhere([ + 'channel_id' => $channelId, + 'customer_group_id' => $groupId, + 'product_id' => $productID + ]); + + 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); + + // 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' => $discountAmount + ]); + } 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, + 'customer_group_id' => $groupId, + 'channel_id' => $channelId, + 'product_id' => $productID, + 'action_code' => $rule->action_code, + '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 + } + } + } + } }