From dc31a6bc9980edfbd54195a0c5f0beb6e168cd3f Mon Sep 17 00:00:00 2001 From: FlusherDock1 Date: Sun, 6 Feb 2022 13:46:25 +0300 Subject: [PATCH] 1.1.0: Added new cache generators, changed cache iterator. --- Plugin.php | 4 + README.md | 102 ++++----- composer.json | 16 +- console/cache/CacheHelper.php | 40 ++-- console/cache/GenerateBrandsCache.php | 104 +++++---- console/cache/GenerateCache.php | 16 +- console/cache/GenerateCategoriesCache.php | 104 +++++---- console/cache/GenerateMeasureCache.php | 51 +++++ console/cache/GenerateOffersCache.php | 104 +++++---- console/cache/GenerateProductsCache.php | 104 +++++---- console/cache/GeneratePropertyCache.php | 51 +++++ console/cache/GeneratePropertySetCache.php | 51 +++++ console/cache/GeneratePropertyValueCache.php | 51 +++++ console/detachers/AddCategoryToProducts.php | 122 +++++------ console/detachers/DetachHelper.php | 122 +++++------ console/generators/GenerateOffers.php | 114 +++++----- console/linkers/AddCategoriesToProducts.php | 122 +++++------ .../AddCategoryToProductsByAddCategory.php | 146 ++++++------- .../AddCategoryToProductsByCategory.php | 146 ++++++------- console/linkers/BrandToProducts.php | 122 +++++------ console/linkers/BrandToProductsByCategory.php | 200 +++++++++--------- console/linkers/CategoryToProducts.php | 124 +++++------ .../linkers/CategoryToProductsByCategory.php | 140 ++++++------ console/linkers/LinkHelper.php | 168 +++++++-------- .../linkers/ProductsToBrandByProductName.php | 154 +++++++------- console/prices/DownscalePriceByPercentage.php | 120 +++++------ .../DownscalePriceByPercentageByBrand.php | 122 +++++------ .../DownscalePriceByPercentageByCategory.php | 122 +++++------ console/prices/PriceHelper.php | 66 +++--- console/prices/UpscalePriceByPercentage.php | 120 +++++------ .../UpscalePriceByPercentageByBrand.php | 122 +++++------ .../UpscalePriceByPercentageByCategory.php | 138 ++++++------ .../prices/UpscalePriceByPercentageCustom.php | 118 +++++------ console/setters/AttributeHelper.php | 74 +++---- console/setters/SetAttribute.php | 118 +++++------ updates/version.yaml | 7 +- 36 files changed, 1913 insertions(+), 1692 deletions(-) create mode 100644 console/cache/GenerateMeasureCache.php create mode 100644 console/cache/GeneratePropertyCache.php create mode 100644 console/cache/GeneratePropertySetCache.php create mode 100644 console/cache/GeneratePropertyValueCache.php diff --git a/Plugin.php b/Plugin.php index 7f337a0..0d9108c 100644 --- a/Plugin.php +++ b/Plugin.php @@ -39,6 +39,10 @@ public function register() $this->registerConsoleCommand('shopahelper:cache.products', 'BizMark\Shopahelper\Console\Cache\GenerateProductsCache'); $this->registerConsoleCommand('shopahelper:cache.offers', 'BizMark\Shopahelper\Console\Cache\GenerateOffersCache'); $this->registerConsoleCommand('shopahelper:cache.brands', 'BizMark\Shopahelper\Console\Cache\GenerateBrandsCache'); + $this->registerConsoleCommand('shopahelper:cache.measure', 'BizMark\ShopaHelper\Console\Cache\GenerateMeasureCache'); + $this->registerConsoleCommand('shopahelper:cache.properties', 'BizMark\ShopaHelper\Console\Cache\GeneratePropertyCache'); + $this->registerConsoleCommand('shopahelper:cache.propertysets', 'BizMark\ShopaHelper\Console\Cache\GeneratePropertySetCache'); + $this->registerConsoleCommand('shopahelper:cache.propertyvalues', 'BizMark\ShopaHelper\Console\Cache\GeneratePropertyValueCache'); // Linkers //// Brands diff --git a/README.md b/README.md index 74401a2..2395faa 100644 --- a/README.md +++ b/README.md @@ -1,51 +1,51 @@ -# Shopahelper plugin for Shopaholic - -Shopahelper is a basic bulk tool that helps you to manage your catalog. - -### Caching: -- `shopahelper:cache.all {chunk}` - Cache all Products, Brands, Categories, Offers by chunks with N items. -- `shopahelper:cache.products {chunk}` - Cache Products by chunks with N products. -- `shopahelper:cache.brands {chunk}` - Cache Brands by chunks with N brands. -- `shopahelper:cache.categories {chunk}` - Cache Categories by chunks with N categories. -- `shopahelper:cache.offers {chunk}` - Cache Offers by chunks with N offers. - -### Generators -- `shopahelper:generate.offers` - Generating offers for each product that doesn't have it. - -### Linkers - -- `{products}` - In that variable you can pass a single ID or Multiple ID's separated by commas. Example: `shopahelper:link.brand-to-products 1 13,14,15` - Link Brand with id 1 to Products with id 13,14 and 15. - - -- Brands - - `shopahelper:link.brand-to-products {brand_id} {products}` - Link Brand by id to products by id's separated by comma. - - `shopahelper:link.brand-to-products-by-category {brand_id} {category_id}` - Link Brand by ID to products inside Category by ID -- Category - - `shopahelper:link.category-to-products {category_id} {products}` - Link Category by ID to Products by ID's separated by comma - - `shopahelper:link.category-to-products-by-category {old_category_id} {new_category_id}` - Link main Category ID to Products by their main Category ID; -- Additional Category - - `shopahelper:link.add-categories-to-products {category_id} {products}` - Link Additional Category by ID to Products by ID's separated by comma - - `shopahelper:link.add-category-to-products-by-add-category {target_category_id} {current_category_id}` - Link Additional Category by ID to Products that has additional category by ID - - `shopahelper:link.add-category-to-products-by-category {target_category_id} {current_category_id}` - Link Additional Category by ID to Products that has main Category by ID - -### Detachers - -- `{products}` - In that variable you can pass a single ID or Multiple ID's separated by commas. - -- `shopahelper:detach.add-category-from-products {category} {products}` - Detaches Additional Category from Products by ID's separated by comma - -### Prices - -- `{products}` - In that variable you can pass a single ID or Multiple ID's separated by commas. Example: `shopahelper:price.upscale-by-percentage 10 13,14,15` - Upscale offers price by 10% in Products with id's 13,14,15 -- `{category_tree}` - Boolean variable that accepts `true` or `false` strings. If passed `true` - proceeds action to all subcategories of target category. - - -- Upscale - - `shopahelper:price.upscale-by-percentage {percentage} {products}` - Upscale price by N percentage to Offers of Products by ID's separated by comma - - `shopahelper:price.upscale-by-percentage-by-brand {percentage} {brand_id}` - Upscale price by N percentage to Offers of Products of Brand by ID - - `shopahelper:price.upscale-by-percentage-by-category {percentage} {category_id} {category_tree}` - Upscale price by N percentage to Offers of Products of Category by ID. - - `shopahelper:price.upscale-by-percentage-custom` - Upscale price by N percentage to Offers of Products by ID's separated by comma with custom condition that you can specify in code. -- Downscale - - `shopahelper:price.downscale-by-percentage` - Downscale price by N percentage to Offers of Products by ID's separated by comma. - - `shopahelper:price.downscale-by-percentage-by-brand` - Downscale price by N percentage to Offers of Products of Brand by ID - - `shopahelper:price.downscale-by-percentage-by-category` - Downscale price by N percentage to Offers of Products of Category by ID. +# Shopahelper plugin for Shopaholic + +Shopahelper is a basic bulk tool that helps you to manage your catalog. + +### Caching: +- `shopahelper:cache.all {chunk}` - Cache all Products, Brands, Categories, Offers by chunks with N items. +- `shopahelper:cache.products {chunk}` - Cache Products by chunks with N products. +- `shopahelper:cache.brands {chunk}` - Cache Brands by chunks with N brands. +- `shopahelper:cache.categories {chunk}` - Cache Categories by chunks with N categories. +- `shopahelper:cache.offers {chunk}` - Cache Offers by chunks with N offers. + +### Generators +- `shopahelper:generate.offers` - Generating offers for each product that doesn't have it. + +### Linkers + +- `{products}` - In that variable you can pass a single ID or Multiple ID's separated by commas. Example: `shopahelper:link.brand-to-products 1 13,14,15` - Link Brand with id 1 to Products with id 13,14 and 15. + + +- Brands + - `shopahelper:link.brand-to-products {brand_id} {products}` - Link Brand by id to products by id's separated by comma. + - `shopahelper:link.brand-to-products-by-category {brand_id} {category_id}` - Link Brand by ID to products inside Category by ID +- Category + - `shopahelper:link.category-to-products {category_id} {products}` - Link Category by ID to Products by ID's separated by comma + - `shopahelper:link.category-to-products-by-category {old_category_id} {new_category_id}` - Link main Category ID to Products by their main Category ID; +- Additional Category + - `shopahelper:link.add-categories-to-products {category_id} {products}` - Link Additional Category by ID to Products by ID's separated by comma + - `shopahelper:link.add-category-to-products-by-add-category {target_category_id} {current_category_id}` - Link Additional Category by ID to Products that has additional category by ID + - `shopahelper:link.add-category-to-products-by-category {target_category_id} {current_category_id}` - Link Additional Category by ID to Products that has main Category by ID + +### Detachers + +- `{products}` - In that variable you can pass a single ID or Multiple ID's separated by commas. + +- `shopahelper:detach.add-category-from-products {category} {products}` - Detaches Additional Category from Products by ID's separated by comma + +### Prices + +- `{products}` - In that variable you can pass a single ID or Multiple ID's separated by commas. Example: `shopahelper:price.upscale-by-percentage 10 13,14,15` - Upscale offers price by 10% in Products with id's 13,14,15 +- `{category_tree}` - Boolean variable that accepts `true` or `false` strings. If passed `true` - proceeds action to all subcategories of target category. + + +- Upscale + - `shopahelper:price.upscale-by-percentage {percentage} {products}` - Upscale price by N percentage to Offers of Products by ID's separated by comma + - `shopahelper:price.upscale-by-percentage-by-brand {percentage} {brand_id}` - Upscale price by N percentage to Offers of Products of Brand by ID + - `shopahelper:price.upscale-by-percentage-by-category {percentage} {category_id} {category_tree}` - Upscale price by N percentage to Offers of Products of Category by ID. + - `shopahelper:price.upscale-by-percentage-custom` - Upscale price by N percentage to Offers of Products by ID's separated by comma with custom condition that you can specify in code. +- Downscale + - `shopahelper:price.downscale-by-percentage` - Downscale price by N percentage to Offers of Products by ID's separated by comma. + - `shopahelper:price.downscale-by-percentage-by-brand` - Downscale price by N percentage to Offers of Products of Brand by ID + - `shopahelper:price.downscale-by-percentage-by-category` - Downscale price by N percentage to Offers of Products of Category by ID. diff --git a/composer.json b/composer.json index de65cd6..11977e7 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ -{ - "name": "bizmark/shopahelper-plugin", - "type": "october-plugin", - "description": "The almighty helper in bulk actions for Shopaholic", - "require": { - "composer/installers": "~1.0" - } -} +{ + "name": "bizmark/shopahelpershopaholic-plugin", + "type": "october-plugin", + "description": "The almighty helper in bulk actions for Shopaholic", + "require": { + "composer/installers": "~1.0" + } +} diff --git a/console/cache/CacheHelper.php b/console/cache/CacheHelper.php index 3f441d8..b978b0b 100644 --- a/console/cache/CacheHelper.php +++ b/console/cache/CacheHelper.php @@ -1,27 +1,41 @@ output->writeln('Generating ' . $sName . ' cache.'); + $this->info('Generating '.$sName.' cache.'); + $obBar = $this->output->createProgressBar($obModel::active()->count()); + + foreach ($obModel::active()->orderBy('id')->cursor() as $obRecord) { + $obModelItem::make($obRecord->id); + $obBar->advance(); + unset($obRecord); + } - $iChunkCount = $obModel::active()->count(); - $iChunkCount = $iChunkCount / $this->argument('chunks'); - $iChunkCount = round($iChunkCount); + $obBar->finish(); + $this->getOutput()->newLine(2); - $obModel::active()->chunk($iChunkCount, function($records) use ($obModelItem, $sName) { - foreach ($records as $record) { - $obModelItem::make($record->id); - $this->output->writeln($sName . ' #' . $record->id . ' generated cache.'); - } - }); + unset($obBar); + unset($sName); + unset($obModel); + unset($obModelItem); } -} \ No newline at end of file +} diff --git a/console/cache/GenerateBrandsCache.php b/console/cache/GenerateBrandsCache.php index 261493e..5d731f5 100644 --- a/console/cache/GenerateBrandsCache.php +++ b/console/cache/GenerateBrandsCache.php @@ -1,53 +1,51 @@ -generateCache('Brand', Brand::class, BrandItem::class); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['chunks', InputArgument::REQUIRED, 'How many chunks of each collection to iterate. (2, 3, 4 and etc.)'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +generateCache('Brand', Brand::class, BrandItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/cache/GenerateCache.php b/console/cache/GenerateCache.php index b26e7ff..49215ca 100644 --- a/console/cache/GenerateCache.php +++ b/console/cache/GenerateCache.php @@ -6,11 +6,18 @@ use Lovata\Shopaholic\Models\Offer; use Lovata\Shopaholic\Models\Product; use Lovata\Shopaholic\Models\Category; +use Lovata\PropertiesShopaholic\Models\Property; +use Lovata\PropertiesShopaholic\Models\PropertySet; +use Lovata\PropertiesShopaholic\Models\PropertyValue; use Lovata\Shopaholic\Classes\Item\BrandItem; use Lovata\Shopaholic\Classes\Item\OfferItem; use Lovata\Shopaholic\Classes\Item\ProductItem; use Lovata\Shopaholic\Classes\Item\CategoryItem; +use Lovata\Shopaholic\Classes\Item\MeasureItem; +use Lovata\PropertiesShopaholic\Classes\Item\PropertyItem; +use Lovata\PropertiesShopaholic\Classes\Item\PropertySetItem; +use Lovata\PropertiesShopaholic\Classes\Item\PropertyValueItem; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputArgument; @@ -35,11 +42,14 @@ class GenerateCache extends Command */ public function handle() { - $this->output->writeln('Starting process of caching Categories, Products, Offers, Brands'); $this->generateCache('Category', Category::class, CategoryItem::class); $this->generateCache('Product', Product::class, ProductItem::class); $this->generateCache('Offer', Offer::class, OfferItem::class); $this->generateCache('Brand', Brand::class, BrandItem::class); + $this->generateCache('Measure', Measure::class, MeasureItem::class); + $this->generateCache('Property', Property::class, PropertyItem::class); + $this->generateCache('PropertySet', PropertySet::class, PropertySetItem::class); + $this->generateCache('PropertyValue', PropertyValue::class, PropertyValueItem::class); } /** @@ -48,9 +58,7 @@ public function handle() */ protected function getArguments() { - return [ - ['chunks', InputArgument::REQUIRED, 'How many chunks of each collection to iterate. (2, 3, 4 and etc.)'], - ]; + return []; } /** diff --git a/console/cache/GenerateCategoriesCache.php b/console/cache/GenerateCategoriesCache.php index bf2f990..dba423a 100644 --- a/console/cache/GenerateCategoriesCache.php +++ b/console/cache/GenerateCategoriesCache.php @@ -1,53 +1,51 @@ -generateCache('Category', Category::class, CategoryItem::class); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['chunks', InputArgument::REQUIRED, 'How many chunks of each collection to iterate. (2, 3, 4 and etc.)'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +generateCache('Category', Category::class, CategoryItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/cache/GenerateMeasureCache.php b/console/cache/GenerateMeasureCache.php new file mode 100644 index 0000000..1fdce48 --- /dev/null +++ b/console/cache/GenerateMeasureCache.php @@ -0,0 +1,51 @@ +generateCache('Measure', Measure::class, MeasureItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/cache/GenerateOffersCache.php b/console/cache/GenerateOffersCache.php index 2896f10..6f44f85 100644 --- a/console/cache/GenerateOffersCache.php +++ b/console/cache/GenerateOffersCache.php @@ -1,53 +1,51 @@ -generateCache('Offer', Offer::class, OfferItem::class); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['chunks', InputArgument::REQUIRED, 'How many chunks of each collection to iterate. (2, 3, 4 and etc.)'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +generateCache('Offer', Offer::class, OfferItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/cache/GenerateProductsCache.php b/console/cache/GenerateProductsCache.php index ff71925..3b598c0 100644 --- a/console/cache/GenerateProductsCache.php +++ b/console/cache/GenerateProductsCache.php @@ -1,53 +1,51 @@ -generateCache('Product', Product::class, ProductItem::class); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['chunks', InputArgument::REQUIRED, 'How many chunks of each collection to iterate. (2, 3, 4 and etc.)'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +generateCache('Product', Product::class, ProductItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/cache/GeneratePropertyCache.php b/console/cache/GeneratePropertyCache.php new file mode 100644 index 0000000..2888ae8 --- /dev/null +++ b/console/cache/GeneratePropertyCache.php @@ -0,0 +1,51 @@ +generateCache('Property', Property::class, PropertyItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/cache/GeneratePropertySetCache.php b/console/cache/GeneratePropertySetCache.php new file mode 100644 index 0000000..f1e5f8f --- /dev/null +++ b/console/cache/GeneratePropertySetCache.php @@ -0,0 +1,51 @@ +generateCache('PropertySet', PropertySet::class, PropertySetItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/cache/GeneratePropertyValueCache.php b/console/cache/GeneratePropertyValueCache.php new file mode 100644 index 0000000..e5b372b --- /dev/null +++ b/console/cache/GeneratePropertyValueCache.php @@ -0,0 +1,51 @@ +generateCache('PropertyValue', PropertyValue::class, PropertyValueItem::class); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return []; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/detachers/AddCategoryToProducts.php b/console/detachers/AddCategoryToProducts.php index 9bc2d69..ac155e7 100644 --- a/console/detachers/AddCategoryToProducts.php +++ b/console/detachers/AddCategoryToProducts.php @@ -1,61 +1,61 @@ -argument('category')); - if (empty($obCategory)) { - $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); - return; - } - - $this->detachArrayOrSingle($obCategory, $this->argument('products'), Product::class, 'additional_category', true); - $this->output->writeln('Detaching additional category from products successfully ended'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['category', InputArgument::REQUIRED, 'Additional Category ID'], - ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +argument('category')); + if (empty($obCategory)) { + $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); + return; + } + + $this->detachArrayOrSingle($obCategory, $this->argument('products'), Product::class, 'additional_category', true); + $this->output->writeln('Detaching additional category from products successfully ended'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['category', InputArgument::REQUIRED, 'Additional Category ID'], + ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/detachers/DetachHelper.php b/console/detachers/DetachHelper.php index 67027af..76fe5eb 100644 --- a/console/detachers/DetachHelper.php +++ b/console/detachers/DetachHelper.php @@ -1,62 +1,62 @@ -whereIn('id', $arSlavesID)->get(); - if ($obSlaves->isNotEmpty()) { - foreach ($obSlaves as $obSlave) { - if ($manyRelation){ - $this->detachSlaveFromManyMaster($obSlave, $obMaster, $masterField); - } else { - $this->detachSlaveFromMaster($obSlave, $obMaster, $masterField); - } - } - } else { - $this->output->writeln('Slaves with given array not found'); - return; - } - } else { - $obSlave = $slaveModel::active()->find($arSlave); - if (empty($obSlave)) { - $this->output->writeln(sprintf("Slave with id %s not found", $arSlave)); - return; - } - if ($manyRelation){ - $this->detachSlaveFromManyMaster($obSlave, $obMaster, $masterField); - } else { - $this->detachSlaveFromMaster($obSlave, $obMaster, $masterField); - } - } - } - - protected function detachSlaveFromMaster($obSlave, $obMaster, $masterField) - { - $this->output->writeln(sprintf("Detaching #%s from #%s", $obSlave->id, $obMaster->id)); - try { - $obSlave->{$masterField}()->remove($obMaster); - } catch (\Exception $ex){ - $this->output->writeln(sprintf("Detaching #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); - trace_log($ex); - } - } - - protected function detachSlaveFromManyMaster($obSlave, $obMaster, $masterField) - { - $this->output->writeln(sprintf("Detaching #%s from #%s", $obSlave->id, $obMaster->id)); - try { - $obSlave->{$masterField}()->detach($obMaster); - } catch (\Exception $ex){ - $this->output->writeln(sprintf("Detaching #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); - trace_log($ex); - } - } +whereIn('id', $arSlavesID)->get(); + if ($obSlaves->isNotEmpty()) { + foreach ($obSlaves as $obSlave) { + if ($manyRelation){ + $this->detachSlaveFromManyMaster($obSlave, $obMaster, $masterField); + } else { + $this->detachSlaveFromMaster($obSlave, $obMaster, $masterField); + } + } + } else { + $this->output->writeln('Slaves with given array not found'); + return; + } + } else { + $obSlave = $slaveModel::active()->find($arSlave); + if (empty($obSlave)) { + $this->output->writeln(sprintf("Slave with id %s not found", $arSlave)); + return; + } + if ($manyRelation){ + $this->detachSlaveFromManyMaster($obSlave, $obMaster, $masterField); + } else { + $this->detachSlaveFromMaster($obSlave, $obMaster, $masterField); + } + } + } + + protected function detachSlaveFromMaster($obSlave, $obMaster, $masterField) + { + $this->output->writeln(sprintf("Detaching #%s from #%s", $obSlave->id, $obMaster->id)); + try { + $obSlave->{$masterField}()->remove($obMaster); + } catch (\Exception $ex){ + $this->output->writeln(sprintf("Detaching #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); + trace_log($ex); + } + } + + protected function detachSlaveFromManyMaster($obSlave, $obMaster, $masterField) + { + $this->output->writeln(sprintf("Detaching #%s from #%s", $obSlave->id, $obMaster->id)); + try { + $obSlave->{$masterField}()->detach($obMaster); + } catch (\Exception $ex){ + $this->output->writeln(sprintf("Detaching #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); + trace_log($ex); + } + } } \ No newline at end of file diff --git a/console/generators/GenerateOffers.php b/console/generators/GenerateOffers.php index f9708ed..24213f5 100644 --- a/console/generators/GenerateOffers.php +++ b/console/generators/GenerateOffers.php @@ -1,57 +1,57 @@ -get(); - foreach ($products as $product) { - $offer = new Offer(); - $this->output->writeln('#'.$product->id); - } - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['chunks', InputArgument::REQUIRED, 'How many chunks of each collection to iterate. (2, 3, 4 and etc.)'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +get(); + foreach ($products as $product) { + $offer = new Offer(); + $this->output->writeln('#'.$product->id); + } + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['chunks', InputArgument::REQUIRED, 'How many chunks of each collection to iterate. (2, 3, 4 and etc.)'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/AddCategoriesToProducts.php b/console/linkers/AddCategoriesToProducts.php index ab2373d..ac66e5b 100644 --- a/console/linkers/AddCategoriesToProducts.php +++ b/console/linkers/AddCategoriesToProducts.php @@ -1,61 +1,61 @@ -argument('category')); - if (empty($obCategory)) { - $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); - return; - } - - $this->linkArrayOrSingle($obCategory, $this->argument('products'), Product::class, 'additional_category', true); - $this->output->writeln('Additional category linking successfully ended'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['category', InputArgument::REQUIRED, 'Additional Category ID'], - ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +argument('category')); + if (empty($obCategory)) { + $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); + return; + } + + $this->linkArrayOrSingle($obCategory, $this->argument('products'), Product::class, 'additional_category', true); + $this->output->writeln('Additional category linking successfully ended'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['category', InputArgument::REQUIRED, 'Additional Category ID'], + ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/AddCategoryToProductsByAddCategory.php b/console/linkers/AddCategoryToProductsByAddCategory.php index df648f7..620a69a 100644 --- a/console/linkers/AddCategoryToProductsByAddCategory.php +++ b/console/linkers/AddCategoryToProductsByAddCategory.php @@ -1,73 +1,73 @@ -argument('current_category')); - if (empty($obCurrentCategory)) { - $this->output->writeln(sprintf("Current category with id %s not found", $this->argument('current_category'))); - return; - } - - $obTargetCategory = Category::find($this->argument('target_category')); - if (empty($obTargetCategory)) { - $this->output->writeln(sprintf("Target category with id %s not found", $this->argument('target_category'))); - return; - } - - if (!empty($obCurrentCategory->product_link)){ - foreach ($obCurrentCategory->product_link as $obProduct) { - $this->linkSlaveToManyMaster($obProduct, $obTargetCategory, 'additional_category'); - } - } - - $this->output->writeln('Category linking successfully ended'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['target_category', InputArgument::REQUIRED, 'Target category ID'], - ['current_category', InputArgument::REQUIRED, 'Current category ID'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +argument('current_category')); + if (empty($obCurrentCategory)) { + $this->output->writeln(sprintf("Current category with id %s not found", $this->argument('current_category'))); + return; + } + + $obTargetCategory = Category::find($this->argument('target_category')); + if (empty($obTargetCategory)) { + $this->output->writeln(sprintf("Target category with id %s not found", $this->argument('target_category'))); + return; + } + + if (!empty($obCurrentCategory->product_link)){ + foreach ($obCurrentCategory->product_link as $obProduct) { + $this->linkSlaveToManyMaster($obProduct, $obTargetCategory, 'additional_category'); + } + } + + $this->output->writeln('Category linking successfully ended'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['target_category', InputArgument::REQUIRED, 'Target category ID'], + ['current_category', InputArgument::REQUIRED, 'Current category ID'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/AddCategoryToProductsByCategory.php b/console/linkers/AddCategoryToProductsByCategory.php index c3ab467..823221f 100644 --- a/console/linkers/AddCategoryToProductsByCategory.php +++ b/console/linkers/AddCategoryToProductsByCategory.php @@ -1,73 +1,73 @@ -argument('current_category')); - if (empty($obCurrentCategory)) { - $this->output->writeln(sprintf("Current category with id %s not found", $this->argument('current_category'))); - return; - } - - $obTargetCategory = Category::find($this->argument('target_category')); - if (empty($obTargetCategory)) { - $this->output->writeln(sprintf("Target category with id %s not found", $this->argument('target_category'))); - return; - } - - if (!empty($obCurrentCategory->product)){ - foreach ($obCurrentCategory->product as $obProduct) { - $this->linkSlaveToManyMaster($obProduct, $obTargetCategory, 'additional_category'); - } - } - - $this->output->writeln('Category linking successfully ended'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['target_category', InputArgument::REQUIRED, 'Target category ID'], - ['current_category', InputArgument::REQUIRED, 'Current category ID'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +argument('current_category')); + if (empty($obCurrentCategory)) { + $this->output->writeln(sprintf("Current category with id %s not found", $this->argument('current_category'))); + return; + } + + $obTargetCategory = Category::find($this->argument('target_category')); + if (empty($obTargetCategory)) { + $this->output->writeln(sprintf("Target category with id %s not found", $this->argument('target_category'))); + return; + } + + if (!empty($obCurrentCategory->product)){ + foreach ($obCurrentCategory->product as $obProduct) { + $this->linkSlaveToManyMaster($obProduct, $obTargetCategory, 'additional_category'); + } + } + + $this->output->writeln('Category linking successfully ended'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['target_category', InputArgument::REQUIRED, 'Target category ID'], + ['current_category', InputArgument::REQUIRED, 'Current category ID'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/BrandToProducts.php b/console/linkers/BrandToProducts.php index 7565319..95a2e0b 100644 --- a/console/linkers/BrandToProducts.php +++ b/console/linkers/BrandToProducts.php @@ -1,61 +1,61 @@ -find($this->argument('brand')); - if (empty($obBrand)) { - $this->output->writeln(sprintf("Brand with id %s not found", $this->argument('brand'))); - return; - } - - $this->linkArrayOrSingle($obBrand, $this->argument('products'), Product::class, 'brand_id'); - $this->output->writeln('Brand linking successfully ended'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['brand', InputArgument::REQUIRED, 'Brand ID'], - ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +find($this->argument('brand')); + if (empty($obBrand)) { + $this->output->writeln(sprintf("Brand with id %s not found", $this->argument('brand'))); + return; + } + + $this->linkArrayOrSingle($obBrand, $this->argument('products'), Product::class, 'brand_id'); + $this->output->writeln('Brand linking successfully ended'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['brand', InputArgument::REQUIRED, 'Brand ID'], + ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/BrandToProductsByCategory.php b/console/linkers/BrandToProductsByCategory.php index 224fa03..389422b 100644 --- a/console/linkers/BrandToProductsByCategory.php +++ b/console/linkers/BrandToProductsByCategory.php @@ -1,100 +1,100 @@ -find($this->argument('brand')); - if (empty($obBrand)) { - $this->output->writeln(sprintf("Brand with id %s not found", $this->argument('brand'))); - return; - } - - if (strpos($this->argument('category'), ',') !== false){ - $arCategoriesID = explode(',', $this->argument('category')); - $obCategories = Category::active()->whereIn('id', $arCategoriesID)->get(); - if ($obCategories->isNotEmpty()) { - foreach ($obCategories as $obCategory) { - $this->output->writeln('Iterating category '. $obCategory->name. ' #'.$obCategory->id); - $this->iterateCategory($obCategory, $obBrand); - } - } else { - $this->output->writeln('Categories with given array not found'); - } - } else { - $obCategory = Category::active()->find($this->argument('category')); - if (empty($obCategory)) { - $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); - return; - } - $this->output->writeln('Iterating category '. $obCategory->name. ' #'.$obCategory->id); - $this->iterateCategory($obCategory, $obBrand); - } - - $this->output->writeln('Brand linking successfully ended'); - } - - /** - * @param Category $obCategory - * @param Brand $obBrand - */ - protected function iterateCategory($obCategory, $obBrand) - { - foreach ($obCategory->getAllChildrenAndSelf() as $obCategoryTree){ - if ($obCategoryTree->product->isNotEmpty()){ - foreach ($obCategoryTree->product as $obProduct){ - if (empty($obProduct->brand)){ - $obProduct->brand_id = $obBrand->id; - $obProduct->save(); - $this->output->writeln(sprintf("Brand %s attached to product #%s", $obBrand->name, $obProduct->id)); - } else { - $this->output->writeln(sprintf("Product already has brand %s", $obProduct->brand->name)); - } - } - } - } - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['brand', InputArgument::REQUIRED, 'Brand ID'], - ['category', InputArgument::REQUIRED, 'Category ID or ID\'s with comma'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +find($this->argument('brand')); + if (empty($obBrand)) { + $this->output->writeln(sprintf("Brand with id %s not found", $this->argument('brand'))); + return; + } + + if (strpos($this->argument('category'), ',') !== false){ + $arCategoriesID = explode(',', $this->argument('category')); + $obCategories = Category::active()->whereIn('id', $arCategoriesID)->get(); + if ($obCategories->isNotEmpty()) { + foreach ($obCategories as $obCategory) { + $this->output->writeln('Iterating category '. $obCategory->name. ' #'.$obCategory->id); + $this->iterateCategory($obCategory, $obBrand); + } + } else { + $this->output->writeln('Categories with given array not found'); + } + } else { + $obCategory = Category::active()->find($this->argument('category')); + if (empty($obCategory)) { + $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); + return; + } + $this->output->writeln('Iterating category '. $obCategory->name. ' #'.$obCategory->id); + $this->iterateCategory($obCategory, $obBrand); + } + + $this->output->writeln('Brand linking successfully ended'); + } + + /** + * @param Category $obCategory + * @param Brand $obBrand + */ + protected function iterateCategory($obCategory, $obBrand) + { + foreach ($obCategory->getAllChildrenAndSelf() as $obCategoryTree){ + if ($obCategoryTree->product->isNotEmpty()){ + foreach ($obCategoryTree->product as $obProduct){ + if (empty($obProduct->brand)){ + $obProduct->brand_id = $obBrand->id; + $obProduct->save(); + $this->output->writeln(sprintf("Brand %s attached to product #%s", $obBrand->name, $obProduct->id)); + } else { + $this->output->writeln(sprintf("Product already has brand %s", $obProduct->brand->name)); + } + } + } + } + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['brand', InputArgument::REQUIRED, 'Brand ID'], + ['category', InputArgument::REQUIRED, 'Category ID or ID\'s with comma'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/CategoryToProducts.php b/console/linkers/CategoryToProducts.php index afa1f6a..70e9c04 100644 --- a/console/linkers/CategoryToProducts.php +++ b/console/linkers/CategoryToProducts.php @@ -1,62 +1,62 @@ -argument('category')); - if (empty($obCategory)) { - $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); - return; - } - - $this->linkArrayOrSingle($obCategory, $this->argument('products'), Product::class, 'category_id'); - $this->output->writeln('Category linking successfully ended'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['category', InputArgument::REQUIRED, 'Category ID'], - ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +argument('category')); + if (empty($obCategory)) { + $this->output->writeln(sprintf("Category with id %s not found", $this->argument('category'))); + return; + } + + $this->linkArrayOrSingle($obCategory, $this->argument('products'), Product::class, 'category_id'); + $this->output->writeln('Category linking successfully ended'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['category', InputArgument::REQUIRED, 'Category ID'], + ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/CategoryToProductsByCategory.php b/console/linkers/CategoryToProductsByCategory.php index 1058d51..0859a16 100644 --- a/console/linkers/CategoryToProductsByCategory.php +++ b/console/linkers/CategoryToProductsByCategory.php @@ -1,70 +1,70 @@ -find($this->argument('old_category')); - if (empty($obOldCategory)){ - $this->output->writeln(sprintf("Category with id %s not found", $this->argument('old_category'))); - return; - } - - $obNewCategory = Category::active()->find($this->argument('new_category')); - if (empty($obNewCategory)){ - $this->output->writeln(sprintf("Category with id %s not found", $this->argument('new_category'))); - return; - } - - foreach ($obOldCategory->getAllChildrenAndSelf() as $obOldCategoryTree){ - if ($obOldCategoryTree->product->isNotEmpty()){ - foreach ($obOldCategoryTree->product as $obProduct){ - $obProduct->category_id = $obNewCategory->id; - $obProduct->save(); - $this->output->writeln(sprintf("Category #%s attached to product #%s", $obNewCategory->id, $obProduct->id)); - } - } - } - $this->output->writeln('Category linking successfully ended'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['old_category', InputArgument::REQUIRED, 'Old Category ID'], - ['new_category', InputArgument::REQUIRED, 'New Category ID'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +find($this->argument('old_category')); + if (empty($obOldCategory)){ + $this->output->writeln(sprintf("Category with id %s not found", $this->argument('old_category'))); + return; + } + + $obNewCategory = Category::active()->find($this->argument('new_category')); + if (empty($obNewCategory)){ + $this->output->writeln(sprintf("Category with id %s not found", $this->argument('new_category'))); + return; + } + + foreach ($obOldCategory->getAllChildrenAndSelf() as $obOldCategoryTree){ + if ($obOldCategoryTree->product->isNotEmpty()){ + foreach ($obOldCategoryTree->product as $obProduct){ + $obProduct->category_id = $obNewCategory->id; + $obProduct->save(); + $this->output->writeln(sprintf("Category #%s attached to product #%s", $obNewCategory->id, $obProduct->id)); + } + } + } + $this->output->writeln('Category linking successfully ended'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['old_category', InputArgument::REQUIRED, 'Old Category ID'], + ['new_category', InputArgument::REQUIRED, 'New Category ID'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/linkers/LinkHelper.php b/console/linkers/LinkHelper.php index 0a09242..2794fa7 100644 --- a/console/linkers/LinkHelper.php +++ b/console/linkers/LinkHelper.php @@ -1,85 +1,85 @@ -whereIn('id', $arSlavesID)->get(); - if ($obSlaves->isNotEmpty()) { - foreach ($obSlaves as $obSlave) { - if ($manyRelation){ - $this->linkSlaveToManyMaster($obSlave, $obMaster, $masterField); - } else { - $this->linkSlaveToMaster($obSlave, $obMaster, $masterField); - } - } - } else { - $this->output->writeln('Slaves with given array not found'); - return; - } - } else { - $obSlave = $slaveModel::active()->find($arSlave); - if (empty($obSlave)) { - $this->output->writeln(sprintf("Slave with id %s not found", $arSlave)); - return; - } - if ($manyRelation){ - $this->linkSlaveToManyMaster($obSlave, $obMaster, $masterField); - } else { - $this->linkSlaveToMaster($obSlave, $obMaster, $masterField); - } - } - } - - /** - * @param Offer|Brand|Product|Category $obSlave - * @param Offer|Brand|Product|Category $obMaster - * @param string $masterField - */ - function linkSlaveToMaster($obSlave, $obMaster, $masterField) - { - $this->output->writeln(sprintf("Linking #%s to #%s", $obSlave->id, $obMaster->id)); - try { - $obSlave->{$masterField} = $obMaster->id; - $obSlave->save(); - $this->info('Linking success'); - } catch (\Exception $ex){ - $this->output->writeln(sprintf("Linking #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); - trace_log($ex); - } - } - - /** - * @param Offer|Brand|Product|Category $obSlave - * @param Offer|Brand|Product|Category $obMaster - * @param string $masterField - */ - function linkSlaveToManyMaster($obSlave, $obMaster, $masterField) - { - $this->output->writeln(sprintf("Linking #%s to #%s", $obSlave->id, $obMaster->id)); - try { - $obSlave->{$masterField}()->attach($obMaster); - $obSlave->save(); - } catch (\Exception $ex){ - $this->output->writeln(sprintf("Linking #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); - trace_log($ex); - } - } +whereIn('id', $arSlavesID)->get(); + if ($obSlaves->isNotEmpty()) { + foreach ($obSlaves as $obSlave) { + if ($manyRelation){ + $this->linkSlaveToManyMaster($obSlave, $obMaster, $masterField); + } else { + $this->linkSlaveToMaster($obSlave, $obMaster, $masterField); + } + } + } else { + $this->output->writeln('Slaves with given array not found'); + return; + } + } else { + $obSlave = $slaveModel::active()->find($arSlave); + if (empty($obSlave)) { + $this->output->writeln(sprintf("Slave with id %s not found", $arSlave)); + return; + } + if ($manyRelation){ + $this->linkSlaveToManyMaster($obSlave, $obMaster, $masterField); + } else { + $this->linkSlaveToMaster($obSlave, $obMaster, $masterField); + } + } + } + + /** + * @param Offer|Brand|Product|Category $obSlave + * @param Offer|Brand|Product|Category $obMaster + * @param string $masterField + */ + function linkSlaveToMaster($obSlave, $obMaster, $masterField) + { + $this->output->writeln(sprintf("Linking #%s to #%s", $obSlave->id, $obMaster->id)); + try { + $obSlave->{$masterField} = $obMaster->id; + $obSlave->save(); + $this->info('Linking success'); + } catch (\Exception $ex){ + $this->output->writeln(sprintf("Linking #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); + trace_log($ex); + } + } + + /** + * @param Offer|Brand|Product|Category $obSlave + * @param Offer|Brand|Product|Category $obMaster + * @param string $masterField + */ + function linkSlaveToManyMaster($obSlave, $obMaster, $masterField) + { + $this->output->writeln(sprintf("Linking #%s to #%s", $obSlave->id, $obMaster->id)); + try { + $obSlave->{$masterField}()->attach($obMaster); + $obSlave->save(); + } catch (\Exception $ex){ + $this->output->writeln(sprintf("Linking #%s to #%s caused some troubles, look at console", $obSlave->id, $obMaster->id)); + trace_log($ex); + } + } } \ No newline at end of file diff --git a/console/linkers/ProductsToBrandByProductName.php b/console/linkers/ProductsToBrandByProductName.php index 8b6d99b..77a9aa0 100644 --- a/console/linkers/ProductsToBrandByProductName.php +++ b/console/linkers/ProductsToBrandByProductName.php @@ -1,77 +1,77 @@ -argument('brand'),'"'); - $obBrand = Brand::getByName($sBrandName)->first(); - if (empty($obBrand)) { - $this->error(sprintf("Brand with name %s not found", $sBrandName)); - return; - } - $this->info('Brand #'.$obBrand->id); - - $this->info('Gathering products...'); - $obProductCollection = Product::select('id', 'name', 'brand_id') - ->where('name', 'LIKE', '%'.$sBrandName.'%') - ->get(); - - $this->info('Found products: '.$obProductCollection->count()); - - /** @var Product $obProduct */ - foreach ($obProductCollection as $obProduct) { - $this->output->writeln('----------'); - $this->info('Product name:'.$obProduct->name); - if (!empty($obProduct->brand)) { - $this->info('Original brand: ' . $obProduct->brand->name); - } - $this->linkSlaveToMaster($obProduct, $obBrand, 'brand_id'); - } - - $this->output->writeln('----------'); - $this->info('Command executed successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['brand', InputArgument::REQUIRED, 'Brand name needle'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +argument('brand'),'"'); + $obBrand = Brand::getByName($sBrandName)->first(); + if (empty($obBrand)) { + $this->error(sprintf("Brand with name %s not found", $sBrandName)); + return; + } + $this->info('Brand #'.$obBrand->id); + + $this->info('Gathering products...'); + $obProductCollection = Product::select('id', 'name', 'brand_id') + ->where('name', 'LIKE', '%'.$sBrandName.'%') + ->get(); + + $this->info('Found products: '.$obProductCollection->count()); + + /** @var Product $obProduct */ + foreach ($obProductCollection as $obProduct) { + $this->output->writeln('----------'); + $this->info('Product name:'.$obProduct->name); + if (!empty($obProduct->brand)) { + $this->info('Original brand: ' . $obProduct->brand->name); + } + $this->linkSlaveToMaster($obProduct, $obBrand, 'brand_id'); + } + + $this->output->writeln('----------'); + $this->info('Command executed successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['brand', InputArgument::REQUIRED, 'Brand name needle'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/prices/DownscalePriceByPercentage.php b/console/prices/DownscalePriceByPercentage.php index f334c18..4370177 100644 --- a/console/prices/DownscalePriceByPercentage.php +++ b/console/prices/DownscalePriceByPercentage.php @@ -1,60 +1,60 @@ -output->writeln('Starting of price downscaling'); - $obProducts = $this->prepareProducts($this->argument('products')); - - foreach ($obProducts as $obProduct){ - foreach ($obProduct->offer as $obOffer){ - $this->downscalePricePercentage($obOffer); - } - } - $this->output->writeln('Price downscaling by percentage ended successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], - ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Starting of price downscaling'); + $obProducts = $this->prepareProducts($this->argument('products')); + + foreach ($obProducts as $obProduct){ + foreach ($obProduct->offer as $obOffer){ + $this->downscalePricePercentage($obOffer); + } + } + $this->output->writeln('Price downscaling by percentage ended successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], + ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/prices/DownscalePriceByPercentageByBrand.php b/console/prices/DownscalePriceByPercentageByBrand.php index 36a01bb..6177cfe 100644 --- a/console/prices/DownscalePriceByPercentageByBrand.php +++ b/console/prices/DownscalePriceByPercentageByBrand.php @@ -1,61 +1,61 @@ -output->writeln('Starting of price downscaling'); - $obProducts = Product::where('brand_id', $this->argument('brand'))->get(); - - foreach ($obProducts as $obProduct){ - foreach ($obProduct->offer as $obOffer){ - $this->downscalePricePercentage($obOffer); - } - } - $this->output->writeln('Price upscaling by percentage ended successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], - ['brand', InputArgument::REQUIRED, 'Brand ID'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Starting of price downscaling'); + $obProducts = Product::where('brand_id', $this->argument('brand'))->get(); + + foreach ($obProducts as $obProduct){ + foreach ($obProduct->offer as $obOffer){ + $this->downscalePricePercentage($obOffer); + } + } + $this->output->writeln('Price upscaling by percentage ended successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], + ['brand', InputArgument::REQUIRED, 'Brand ID'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/prices/DownscalePriceByPercentageByCategory.php b/console/prices/DownscalePriceByPercentageByCategory.php index 6d76843..4b689a6 100644 --- a/console/prices/DownscalePriceByPercentageByCategory.php +++ b/console/prices/DownscalePriceByPercentageByCategory.php @@ -1,61 +1,61 @@ -output->writeln('Starting of price downscaling'); - $obProducts = Product::where('category_id', $this->argument('category'))->get(); - - foreach ($obProducts as $obProduct){ - foreach ($obProduct->offer as $obOffer){ - $this->downscalePricePercentage($obOffer); - } - } - $this->output->writeln('Price upscaling by percentage ended successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], - ['category', InputArgument::REQUIRED, 'Category ID'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Starting of price downscaling'); + $obProducts = Product::where('category_id', $this->argument('category'))->get(); + + foreach ($obProducts as $obProduct){ + foreach ($obProduct->offer as $obOffer){ + $this->downscalePricePercentage($obOffer); + } + } + $this->output->writeln('Price upscaling by percentage ended successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], + ['category', InputArgument::REQUIRED, 'Category ID'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/prices/PriceHelper.php b/console/prices/PriceHelper.php index cf3a268..fde21cb 100644 --- a/console/prices/PriceHelper.php +++ b/console/prices/PriceHelper.php @@ -1,34 +1,34 @@ -get(); - } else { - return Product::find($string); - } - } - - function upscalePricePercentage($obOffer) - { - $iPrice = $obOffer->price_value; - $iNewPrice = $iPrice + (($iPrice / 100) * $this->argument('percentage')); - $obOffer->price = $iNewPrice; - $obOffer->save(); - $this->output->writeln(sprintf("#%s %s | %s -> %d", $obOffer->id, $obOffer->name, $iPrice, $iNewPrice)); - } - - function downscalePricePercentage($obOffer) - { - $iPrice = $obOffer->price_value; - $iNewPrice = $iPrice - (($iPrice / 100) * $this->argument('percentage')); - $obOffer->price = $iNewPrice; - $obOffer->save(); - $this->output->writeln(sprintf("#%s %s | %s -> %d", $obOffer->id, $obOffer->name, $iPrice, $iNewPrice)); - } +get(); + } else { + return Product::find($string); + } + } + + function upscalePricePercentage($obOffer) + { + $iPrice = $obOffer->price_value; + $iNewPrice = $iPrice + (($iPrice / 100) * $this->argument('percentage')); + $obOffer->price = $iNewPrice; + $obOffer->save(); + $this->output->writeln(sprintf("#%s %s | %s -> %d", $obOffer->id, $obOffer->name, $iPrice, $iNewPrice)); + } + + function downscalePricePercentage($obOffer) + { + $iPrice = $obOffer->price_value; + $iNewPrice = $iPrice - (($iPrice / 100) * $this->argument('percentage')); + $obOffer->price = $iNewPrice; + $obOffer->save(); + $this->output->writeln(sprintf("#%s %s | %s -> %d", $obOffer->id, $obOffer->name, $iPrice, $iNewPrice)); + } } \ No newline at end of file diff --git a/console/prices/UpscalePriceByPercentage.php b/console/prices/UpscalePriceByPercentage.php index 51590b7..b6b1e5f 100644 --- a/console/prices/UpscalePriceByPercentage.php +++ b/console/prices/UpscalePriceByPercentage.php @@ -1,60 +1,60 @@ -output->writeln('Starting of price upscaling'); - $obProducts = $this->prepareProducts($this->argument('products')); - - foreach ($obProducts as $obProduct){ - foreach ($obProduct->offer as $obOffer){ - $this->upscalePricePercentage($obOffer); - } - } - $this->output->writeln('Price upscaling by percentage ended successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], - ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Starting of price upscaling'); + $obProducts = $this->prepareProducts($this->argument('products')); + + foreach ($obProducts as $obProduct){ + foreach ($obProduct->offer as $obOffer){ + $this->upscalePricePercentage($obOffer); + } + } + $this->output->writeln('Price upscaling by percentage ended successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], + ['products', InputArgument::REQUIRED, 'Product ID or ID\'s with comma'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/prices/UpscalePriceByPercentageByBrand.php b/console/prices/UpscalePriceByPercentageByBrand.php index 8806178..70cc175 100644 --- a/console/prices/UpscalePriceByPercentageByBrand.php +++ b/console/prices/UpscalePriceByPercentageByBrand.php @@ -1,61 +1,61 @@ -output->writeln('Starting of price upscaling'); - $obProducts = Product::where('brand_id', $this->argument('brand'))->get(); - - foreach ($obProducts as $obProduct){ - foreach ($obProduct->offer as $obOffer){ - $this->upscalePricePercentage($obOffer); - } - } - $this->output->writeln('Price upscaling by percentage ended successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], - ['brand', InputArgument::REQUIRED, 'Brand ID'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Starting of price upscaling'); + $obProducts = Product::where('brand_id', $this->argument('brand'))->get(); + + foreach ($obProducts as $obProduct){ + foreach ($obProduct->offer as $obOffer){ + $this->upscalePricePercentage($obOffer); + } + } + $this->output->writeln('Price upscaling by percentage ended successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], + ['brand', InputArgument::REQUIRED, 'Brand ID'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/prices/UpscalePriceByPercentageByCategory.php b/console/prices/UpscalePriceByPercentageByCategory.php index e43eec5..398c0e4 100644 --- a/console/prices/UpscalePriceByPercentageByCategory.php +++ b/console/prices/UpscalePriceByPercentageByCategory.php @@ -1,69 +1,69 @@ -output->writeln('Starting of price upscaling'); - if ($this->argument('category_tree') == true) { - $arProducts = ProductCollection::make()->category($this->argument('category'), true)->getIDList(); - $obProducts = Product::whereIn('id', $arProducts)->get(); - } else { - $obProducts = Product::where('category_id', $this->argument('category'))->get(); - } - - foreach ($obProducts as $obProduct){ - foreach ($obProduct->offer as $obOffer){ - $this->upscalePricePercentage($obOffer); - } - } - - $this->output->writeln('Price upscaling by percentage ended successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], - ['category', InputArgument::REQUIRED, 'Category ID'], - ['category_tree', InputArgument::REQUIRED, 'Process products from subcategories'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Starting of price upscaling'); + if ($this->argument('category_tree') == true) { + $arProducts = ProductCollection::make()->category($this->argument('category'), true)->getIDList(); + $obProducts = Product::whereIn('id', $arProducts)->get(); + } else { + $obProducts = Product::where('category_id', $this->argument('category'))->get(); + } + + foreach ($obProducts as $obProduct){ + foreach ($obProduct->offer as $obOffer){ + $this->upscalePricePercentage($obOffer); + } + } + + $this->output->writeln('Price upscaling by percentage ended successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['percentage', InputArgument::REQUIRED, 'Percentage of scale'], + ['category', InputArgument::REQUIRED, 'Category ID'], + ['category_tree', InputArgument::REQUIRED, 'Process products from subcategories'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/prices/UpscalePriceByPercentageCustom.php b/console/prices/UpscalePriceByPercentageCustom.php index da301cb..6ebe283 100644 --- a/console/prices/UpscalePriceByPercentageCustom.php +++ b/console/prices/UpscalePriceByPercentageCustom.php @@ -1,59 +1,59 @@ -output->writeln('Starting of price upscaling'); - $obProducts = Product::whereDate('created_at', '2020-07-09')->get(); - - foreach ($obProducts as $obProduct){ - foreach ($obProduct->offer as $obOffer){ - $this->upscalePricePercentage($obOffer); - } - } - $this->output->writeln('Price upscaling by percentage ended successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['percentage', InputArgument::REQUIRED, 'Percentage of scale'] - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Starting of price upscaling'); + $obProducts = Product::whereDate('created_at', '2020-07-09')->get(); + + foreach ($obProducts as $obProduct){ + foreach ($obProduct->offer as $obOffer){ + $this->upscalePricePercentage($obOffer); + } + } + $this->output->writeln('Price upscaling by percentage ended successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['percentage', InputArgument::REQUIRED, 'Percentage of scale'] + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/console/setters/AttributeHelper.php b/console/setters/AttributeHelper.php index 98884ea..701e4cf 100644 --- a/console/setters/AttributeHelper.php +++ b/console/setters/AttributeHelper.php @@ -1,38 +1,38 @@ -initModel($sModelName); - - if (strpos($sIds, ',') !== false){ - $arIds = explode(',', $sIds); - foreach ($arIds as $sId) { - try { - $obModel = $obModel->find($sId); - $obModel->$sAttributeName = $sAttributeValue; - $obModel->save(); - $this->output->writeln("#{$sId} {$sAttributeName} = {$sAttributeValue}"); - } catch (\Exception $ex) { - $this->output->writeln("Произошла ошибка с #".$sId. ' - '.$ex->getMessage()); - } - } - } else { - $obModel = $obModel->find($sIds); - $obModel->$sAttributeName = $sAttributeValue; - $obModel->save(); - $this->output->writeln("#{$sIds} {$sAttributeName} = {$sAttributeValue}"); - } - } +initModel($sModelName); + + if (strpos($sIds, ',') !== false){ + $arIds = explode(',', $sIds); + foreach ($arIds as $sId) { + try { + $obModel = $obModel->find($sId); + $obModel->$sAttributeName = $sAttributeValue; + $obModel->save(); + $this->output->writeln("#{$sId} {$sAttributeName} = {$sAttributeValue}"); + } catch (\Exception $ex) { + $this->output->writeln("Произошла ошибка с #".$sId. ' - '.$ex->getMessage()); + } + } + } else { + $obModel = $obModel->find($sIds); + $obModel->$sAttributeName = $sAttributeValue; + $obModel->save(); + $this->output->writeln("#{$sIds} {$sAttributeName} = {$sAttributeValue}"); + } + } } \ No newline at end of file diff --git a/console/setters/SetAttribute.php b/console/setters/SetAttribute.php index d933658..2e6f103 100644 --- a/console/setters/SetAttribute.php +++ b/console/setters/SetAttribute.php @@ -1,59 +1,59 @@ -output->writeln('Setting attribute '.$this->argument('attribute_name')); - $this->setAttribute( - $this->argument('model'), - $this->argument('attribute_name'), - $this->argument('attribute_value'), - $this->argument('ids') - ); - $this->output->writeln('Setting completed successfully'); - } - - /** - * Get the console command arguments. - * @return array - */ - protected function getArguments() - { - return [ - ['model', InputArgument::REQUIRED, 'Model'], - ['attribute_name', InputArgument::REQUIRED, 'Attribute'], - ['attribute_value', InputArgument::REQUIRED, 'Attribute'], - ['ids', InputArgument::REQUIRED, 'Values array by comma'], - ]; - } - - /** - * Get the console command options. - * @return array - */ - protected function getOptions() - { - return []; - } -} +output->writeln('Setting attribute '.$this->argument('attribute_name')); + $this->setAttribute( + $this->argument('model'), + $this->argument('attribute_name'), + $this->argument('attribute_value'), + $this->argument('ids') + ); + $this->output->writeln('Setting completed successfully'); + } + + /** + * Get the console command arguments. + * @return array + */ + protected function getArguments() + { + return [ + ['model', InputArgument::REQUIRED, 'Model'], + ['attribute_name', InputArgument::REQUIRED, 'Attribute'], + ['attribute_value', InputArgument::REQUIRED, 'Attribute'], + ['ids', InputArgument::REQUIRED, 'Values array by comma'], + ]; + } + + /** + * Get the console command options. + * @return array + */ + protected function getOptions() + { + return []; + } +} diff --git a/updates/version.yaml b/updates/version.yaml index 1e888b2..981e7dc 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -1,4 +1,3 @@ -1.0.1: First version of Shopahelper -1.0.2: Updated README with detachers, added Shopaholic to required plugins -1.0.3: Small typo fix in README, thanks @igor-tv. Added support for October CMS v2 -1.0.4: Small typo fix +1.0.1: First version of Shopahelper +1.0.2: Updated README with detachers, added Shopaholic to required plugins +1.0.3: Small typo fix in README, thanks @igor-tv. Added support for October CMS v2