From e97069d2beb4bce5ad259ca4dc432c6ce0556212 Mon Sep 17 00:00:00 2001 From: Prashant Singh Date: Mon, 19 Aug 2019 15:05:56 +0530 Subject: [PATCH] Catalog rule implementation complete, new product price mechanish remaining --- packages/Webkul/Admin/src/Config/menu.php | 80 +++-- .../Discount/src/Helpers/Catalog/Apply.php | 327 +++++++++++++++++- .../src/Helpers/Catalog/SyncPreviousRules.php | 0 .../CatalogRuleProductsPriceRepository.php | 132 ------- .../CatalogRuleProductsRepository.php | 156 +-------- packages/Webkul/Product/src/Helpers/Price.php | 1 - 6 files changed, 356 insertions(+), 340 deletions(-) delete mode 100644 packages/Webkul/Discount/src/Helpers/Catalog/SyncPreviousRules.php diff --git a/packages/Webkul/Admin/src/Config/menu.php b/packages/Webkul/Admin/src/Config/menu.php index 1091a60fb..dd92aa092 100755 --- a/packages/Webkul/Admin/src/Config/menu.php +++ b/packages/Webkul/Admin/src/Config/menu.php @@ -6,207 +6,205 @@ return [ 'name' => 'admin::app.layouts.dashboard', 'route' => 'admin.dashboard.index', 'sort' => 1, - 'icon-class' => 'dashboard-icon', + 'icon-class' => 'dashboard-icon' ], [ 'key' => 'sales', 'name' => 'admin::app.layouts.sales', 'route' => 'admin.sales.orders.index', 'sort' => 2, - 'icon-class' => 'sales-icon', + 'icon-class' => 'sales-icon' ], [ 'key' => 'sales.orders', 'name' => 'admin::app.layouts.orders', 'route' => 'admin.sales.orders.index', 'sort' => 1, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'sales.shipments', 'name' => 'admin::app.layouts.shipments', 'route' => 'admin.sales.shipments.index', 'sort' => 2, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'sales.invoices', 'name' => 'admin::app.layouts.invoices', 'route' => 'admin.sales.invoices.index', 'sort' => 3, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'catalog', 'name' => 'admin::app.layouts.catalog', 'route' => 'admin.catalog.products.index', 'sort' => 3, - 'icon-class' => 'catalog-icon', + 'icon-class' => 'catalog-icon' ], [ 'key' => 'catalog.products', 'name' => 'admin::app.layouts.products', 'route' => 'admin.catalog.products.index', 'sort' => 4, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'catalog.categories', 'name' => 'admin::app.layouts.categories', 'route' => 'admin.catalog.categories.index', 'sort' => 3, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'catalog.attributes', 'name' => 'admin::app.layouts.attributes', 'route' => 'admin.catalog.attributes.index', 'sort' => 1, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'catalog.families', 'name' => 'admin::app.layouts.attribute-families', 'route' => 'admin.catalog.families.index', 'sort' => 2, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'customers', 'name' => 'admin::app.layouts.customers', 'route' => 'admin.customer.index', 'sort' => 4, - 'icon-class' => 'customer-icon', + 'icon-class' => 'customer-icon' ], [ 'key' => 'customers.customers', 'name' => 'admin::app.layouts.customers', 'route' => 'admin.customer.index', 'sort' => 1, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'customers.groups', 'name' => 'admin::app.layouts.groups', 'route' => 'admin.groups.index', 'sort' => 2, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'customers.reviews', 'name' => 'admin::app.layouts.reviews', 'route' => 'admin.customer.review.index', 'sort' => 3, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'customers.subscribers', 'name' => 'admin::app.layouts.newsletter-subscriptions', 'route' => 'admin.customers.subscribers.index', 'sort' => 4, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'configuration', 'name' => 'admin::app.layouts.configure', 'route' => 'admin.configuration.index', 'sort' => 7, - 'icon-class' => 'configuration-icon', + 'icon-class' => 'configuration-icon' ], [ 'key' => 'settings', 'name' => 'admin::app.layouts.settings', 'route' => 'admin.locales.index', 'sort' => 6, - 'icon-class' => 'settings-icon', + 'icon-class' => 'settings-icon' ], [ 'key' => 'settings.locales', 'name' => 'admin::app.layouts.locales', 'route' => 'admin.locales.index', 'sort' => 1, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.currencies', 'name' => 'admin::app.layouts.currencies', 'route' => 'admin.currencies.index', 'sort' => 2, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.exchange_rates', 'name' => 'admin::app.layouts.exchange-rates', 'route' => 'admin.exchange_rates.index', 'sort' => 3, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.inventory_sources', 'name' => 'admin::app.layouts.inventory-sources', 'route' => 'admin.inventory_sources.index', 'sort' => 4, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.channels', 'name' => 'admin::app.layouts.channels', 'route' => 'admin.channels.index', 'sort' => 5, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.users', 'name' => 'admin::app.layouts.users', 'route' => 'admin.users.index', 'sort' => 6, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.users.users', 'name' => 'admin::app.layouts.users', 'route' => 'admin.users.index', 'sort' => 1, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.users.roles', 'name' => 'admin::app.layouts.roles', 'route' => 'admin.roles.index', 'sort' => 2, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.sliders', 'name' => 'admin::app.layouts.sliders', 'route' => 'admin.sliders.index', 'sort' => 7, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.taxes', 'name' => 'admin::app.layouts.taxes', 'route' => 'admin.tax-categories.index', 'sort' => 8, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.taxes.tax-categories', 'name' => 'admin::app.layouts.tax-categories', 'route' => 'admin.tax-categories.index', 'sort' => 1, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'settings.taxes.tax-rates', 'name' => 'admin::app.layouts.tax-rates', 'route' => 'admin.tax-rates.index', 'sort' => 2, - 'icon-class' => '', + 'icon-class' => '' ], [ 'key' => 'promotions', 'name' => 'admin::app.layouts.promotion', 'route' => 'admin.cart-rule.index', 'sort' => 5, - 'icon-class' => 'promotion-icon', + 'icon-class' => 'promotion-icon' ], [ 'key' => 'promotions.cart-rule', 'name' => 'admin::app.promotion.cart-rule', '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', 'sort' => 6, - 'icon-class' => 'cms-icon', + 'icon-class' => 'cms-icon' ], [ 'key' => 'cms.pages', 'name' => 'admin::app.cms.pages.pages', 'route' => 'admin.cms.index', 'sort' => 1, - 'icon-class' => '', + 'icon-class' => '' ] ]; \ No newline at end of file diff --git a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php index 69af195c5..e254760c3 100644 --- a/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php +++ b/packages/Webkul/Discount/src/Helpers/Catalog/Apply.php @@ -6,6 +6,7 @@ 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\Product\Repositories\ProductRepository as Product; use Webkul\Discount\Helpers\Catalog\Sale; /** @@ -46,6 +47,11 @@ class Apply extends Sale */ protected $catalogRuleProductPrice; + /** + * Hold ProductRepository instance + */ + protected $product; + /** * To hold the rule classes */ @@ -58,7 +64,8 @@ class Apply extends Sale CatalogRule $catalogRule, ConvertX $convertX, CatalogRuleProducts $catalogRuleProduct, - CatalogRuleProductsPrice $catalogRuleProductPrice + CatalogRuleProductsPrice $catalogRuleProductPrice, + Product $product ) { $this->catalogRule = $catalogRule; @@ -68,6 +75,8 @@ class Apply extends Sale $this->catalogRuleProductPrice = $catalogRuleProductPrice; + $this->product = $product; + $this->active = collect(); $this->activeRules = collect(); @@ -100,20 +109,19 @@ class Apply extends Sale if ($this->active->count()) { $productIDs = array(); - $temp = collect(); foreach ($this->activeRules as $rule) { $productIDs = $this->getProductIds($rule); $productIDs = $this->expandProducts($productIDs); - $result = $this->setSale($rule, $productIDs); + $this->setSale($rule, $productIDs); } - dd($result, 'processing done'); + // dd($result, 'processing done'); } else { // handle the deceased rules here - dd($this->deceased); + // dd($this->deceased); } } @@ -130,18 +138,303 @@ class Apply extends Sale // apply on selected products foreach ($productIDs as $productID) { // catalog rule product resource is updated here - $this->catalogRuleProduct->createOrUpdate($rule, $productID); + $this->createOrUpdateCatalogRuleProduct($rule, $productID); // catalog rule product price resource is updated here - $this->catalogRuleProductPrice->createOrUpdate($rule, $productID); + $this->createOrUpdateCatalogRuleProductPrice($rule, $productID); } } else if ($productIDs == '*') { - $this->catalogRuleProduct->createOrUpdate($rule, $productIDs); + $this->catalogRuleProduct->createOrUpdateCatalogRuleProduct($rule, $productIDs); - $this->catalogRuleProductPrice->createOrUpdate($rule, $productIDs); + $this->catalogRuleProductPrice->createOrUpdateCatalogRuleProductPrice($rule, $productIDs); } } + /** + * Create or update catalog rule product resource + * + * @param CatalogRule $rule + * @param Integer $productID + * + * @return Void + */ + public function createOrUpdateCatalogRuleProduct($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; + + $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 + $product = $this->product->find($productID); + + $productPrice = $product->price; + + // check for tie breaker rules and then update + $previousRuleID = $catalogRuleProduct->first()->catalog_rule_id; + + $newRuleID = $rule->id; + + $winnerRuleId = $this->breakTie($previousRuleID, $newRuleID, $product); + + $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' => $discountPrice + ]; + + 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->catalogRuleProduct->findWhere([ + 'channel_id' => $channelId, + 'customer_group_id' => $groupId, + 'product_id' => $productID + ]); + + if ($catalogRuleProduct->count()) { + $product = $this->product->find($productID); + $productPrice = $product->price; + + // check for tie breaker rules and then update + $previousRuleID = $catalogRuleProduct->first()->catalog_rule_id; + + $newRuleID = $rule->id; + + $winnerRuleId = $this->breakTie($previousRuleID, $newRuleID, $product); + + // update + if ($winnerRuleId != $rule->id) { + $discountAmount = $this->getDiscountAmount($product, $rule); + + $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 { + //do reassessment + } + } else if ($catalogRuleProduct->count() == 0) { + $product = $this->product->find($productID); + + $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->catalogRuleProduct->create($data); + } else { + // do the reassessment updation if the cart rule action changes the new action and its amount needs to be updated + } + } + } + } + + /** + * Create or update catalog rule product resource + * + * @param CatalogRule $rule + * @param Integer $productID + * + * @return Void + */ + public function createOrUpdateCatalogRuleProductPrice($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->catalogRuleProductPrice->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->catalogRuleProductPrice->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->catalogRuleProductPrice->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->catalogRuleProductPrice->findWhere([ + 'channel_id' => $channelId, + 'customer_group_id' => $groupId, + 'product_id' => $productID + ]); + + if ($catalogRuleProductPrice->count()) { + $catalogRuleProduct = $catalogRuleProduct->first(); + + $product = $this->product->find($productID); + + $productPrice = $product->price - $catalogRuleProduct->action_amount; + + $winnerRuleId = $this->breakTie($catalogRuleProduct->catalog_rule_id, $rule->id, $product); + + if ($winnerRuleId != $rule->id) { + $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 + ]; + + $catalogRuleProductPrice->first()->update($data); + } else { + // do reassement + } + } else if ($catalogRuleProductPrice->count() == 0) { + $catalogRuleProduct = $catalogRuleProduct->first(); + + $discountAmount = $catalogRuleProduct->action_amount; + + $product = $this->product->find($productID); + + $productPrice = $product->price - $discountAmount; + + $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->catalogRuleProductPrice->create($data); + } else { + // do the reassessment updation if the cart rule action changes the new action and its amount needs to be updated + } + } + } + } + + /** + * Get discount amount for the rule and product + * + * @return Decimal $discountAmount + */ + public function getDiscountAmount($product, $rule) + { + + $actionClass = config('discount-rules.catalog')[$rule->action_code]; + + $actionInstance = new $actionClass(); + + $discountAmount = $actionInstance->calculate($rule, $product); + + return $discountAmount; + } + /** * To expand the productIDs of configurable products * @@ -184,13 +477,25 @@ class Apply extends Sale * * @return String $id */ - public function breakTie($previousRuleID, $newRuleID) + public function breakTie($previousRuleID, $newRuleID, $product) { $oldRule = $this->catalogRule->find($previousRuleID); $newRule = $this->catalogRule->find($newRuleID); - dd($oldRule->name, $newRule->name); + if ($oldRule->ends_other_rules) { + return $oldRule->id; + } else { + $oldRuleDiscount = $this->getDiscountAmount($product, $oldRule); + + $newRuleDiscount = $this->getDiscountAmount($product, $newRule); + + if ($newRuleDiscount > $oldRuleDiscount) { + return $newRule->id; + } else { + return $oldRule->id; + } + } } /** diff --git a/packages/Webkul/Discount/src/Helpers/Catalog/SyncPreviousRules.php b/packages/Webkul/Discount/src/Helpers/Catalog/SyncPreviousRules.php deleted file mode 100644 index e69de29bb..000000000 diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php index 05e60735b..5becbb9eb 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php @@ -48,136 +48,4 @@ 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 f490930ab..aedf092fd 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php @@ -14,40 +14,19 @@ use Illuminate\Container\Container as App; */ class CatalogRuleProductsRepository extends Repository { - /** - * ProductRepository instance - */ - protected $product; - /** * CatalogRule Apply instance */ protected $apply; /** - * @param Product $product * @param App $app - * @param Apply $apply */ - public function __construct(Product $product, App $app) + public function __construct(App $app) { - $this->product = $product; - 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 * @@ -57,137 +36,4 @@ 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 - } - } - } - } } diff --git a/packages/Webkul/Product/src/Helpers/Price.php b/packages/Webkul/Product/src/Helpers/Price.php index da28d0193..bfdb08c51 100755 --- a/packages/Webkul/Product/src/Helpers/Price.php +++ b/packages/Webkul/Product/src/Helpers/Price.php @@ -3,7 +3,6 @@ namespace Webkul\Product\Helpers; use Webkul\Attribute\Repositories\AttributeRepository as Attribute; -use Webkul\Product\Models\ProductAttributeValue; use Webkul\Product\Models\Product; use Webkul\Product\Models\ProductFlat;