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/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/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/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 823c3df24..69af195c5 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 { /** @@ -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; + } + } } \ 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/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/Http/Controllers/CatalogRuleController.php b/packages/Webkul/Discount/src/Http/Controllers/CatalogRuleController.php index 31db687f2..76b75c283 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) @@ -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(); 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/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..05e60735b 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsPriceRepository.php @@ -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 - * @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 { /** - * 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 + } } } } diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleProductsRepository.php index 609a1ea83..f490930ab 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; @@ -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 } } } diff --git a/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php b/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php index d70f9ff75..527a0a915 100644 --- a/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php +++ b/packages/Webkul/Discount/src/Repositories/CatalogRuleRepository.php @@ -8,23 +8,32 @@ 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) { $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) {