diff --git a/composer.json b/composer.json index abc0e1c36..ef0fa5042 100755 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "astrotomic/laravel-translatable": "^11.0.0", "aws/aws-sdk-php": "^3.171", "babenkoivan/elastic-scout-driver": "^1.1", + "bagisto/laravel-datafaker": "dev-master", "bagistobrasil/bagisto-product-social-share": "^0.1.2", "barryvdh/laravel-debugbar": "^3.1", "barryvdh/laravel-dompdf": "^0.8.5|^0.9.0", @@ -30,6 +31,7 @@ "khaled.alshamaa/ar-php": "^6.0.0", "konekt/concord": "^1.2", "laravel/framework": "^8.0", + "laravel/legacy-factories": "^1.1", "laravel/scout": "^8.0", "laravel/socialite": "^5.0", "laravel/tinker": "^2.0", diff --git a/composer.lock b/composer.lock index be6829533..ff5fa5b0b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "ad730dd868891497848ecf7836bbff4b", + "content-hash": "b1710279cb7cc769d4ed404692c85082", "packages": [ { "name": "algolia/algoliasearch-client-php", @@ -516,6 +516,65 @@ ], "time": "2021-08-09T21:09:19+00:00" }, + { + "name": "bagisto/laravel-datafaker", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/bagisto/laravel-data-faker.git", + "reference": "c67d840a0fc2416acf854bd5a0a9cb47a52a417e" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/bagisto/laravel-data-faker/zipball/c67d840a0fc2416acf854bd5a0a9cb47a52a417e", + "reference": "c67d840a0fc2416acf854bd5a0a9cb47a52a417e", + "shasum": "" + }, + "require": { + "konekt/concord": "^1.2", + "laravel/helpers": "^1.3", + "mbezhanov/faker-provider-collection": "^2.0" + }, + "default-branch": true, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Webkul\\DataFaker\\Providers\\DataFakerServiceProvider" + ], + "aliases": [] + } + }, + "autoload": { + "psr-4": { + "Webkul\\DataFaker\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Naresh Verma", + "email": "naresh.verma327@webkul.com", + "homepage": "https://bagisto.com", + "role": "Developer" + } + ], + "description": "Add fake data of products and customers in Bagisto.", + "homepage": "https://github.com/bagisto/laravel-data-faker", + "keywords": [ + "bagisto", + "fake customers", + "fake products" + ], + "support": { + "issues": "https://github.com/bagisto/laravel-data-faker/issues", + "source": "https://github.com/bagisto/laravel-data-faker/tree/master" + }, + "time": "2021-09-23T12:08:32+00:00" + }, { "name": "bagistobrasil/bagisto-product-social-share", "version": "0.1.3", @@ -771,77 +830,6 @@ ], "time": "2021-08-15T20:50:18+00:00" }, - { - "name": "diglactic/laravel-breadcrumbs", - "version": "v7.0.1", - "source": { - "type": "git", - "url": "https://github.com/diglactic/laravel-breadcrumbs.git", - "reference": "0a14e8785fa9423c878edd3975af2a92dfcdaf42" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/diglactic/laravel-breadcrumbs/zipball/0a14e8785fa9423c878edd3975af2a92dfcdaf42", - "reference": "0a14e8785fa9423c878edd3975af2a92dfcdaf42", - "shasum": "" - }, - "require": { - "facade/ignition-contracts": "^1.0", - "laravel/framework": "^6.0 || ^7.0 || ^8.0", - "php": "^7.2 || ^8.0" - }, - "conflict": { - "davejamesmiller/laravel-breadcrumbs": "*" - }, - "require-dev": { - "orchestra/testbench": "^4.10 || ^5.9 || ^6.4", - "php-coveralls/php-coveralls": "^2.4", - "phpunit/phpunit": "^8.5 || ^9.4", - "spatie/phpunit-snapshot-assertions": "^2.2 || ^4.2" - }, - "type": "library", - "extra": { - "laravel": { - "providers": [ - "Diglactic\\Breadcrumbs\\ServiceProvider" - ], - "aliases": { - "Breadcrumbs": "Diglactic\\Breadcrumbs\\Breadcrumbs" - } - } - }, - "autoload": { - "psr-4": { - "Diglactic\\Breadcrumbs\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Sheng Slogar", - "email": "sheng@diglactic.com", - "role": "Maintainer" - }, - { - "name": "Dave James Miller", - "email": "dave@davejamesmiller.com", - "role": "Original Creator" - } - ], - "description": "A simple Laravel-style way to create breadcrumbs.", - "homepage": "https://github.com/diglactic/laravel-breadcrumbs", - "keywords": [ - "laravel" - ], - "support": { - "issues": "https://github.com/diglactic/laravel-breadcrumbs/issues", - "source": "https://github.com/diglactic/laravel-breadcrumbs/tree/v7.0.1" - }, - "time": "2021-09-23T20:31:25+00:00" - }, { "name": "doctrine/cache", "version": "2.1.1", @@ -3124,6 +3112,118 @@ }, "time": "2021-05-19T13:03:18+00:00" }, + { + "name": "laravel/helpers", + "version": "v1.4.1", + "source": { + "type": "git", + "url": "https://github.com/laravel/helpers.git", + "reference": "febb10d8daaf86123825de2cb87f789a3371f0ac" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/helpers/zipball/febb10d8daaf86123825de2cb87f789a3371f0ac", + "reference": "febb10d8daaf86123825de2cb87f789a3371f0ac", + "shasum": "" + }, + "require": { + "illuminate/support": "~5.8.0|^6.0|^7.0|^8.0", + "php": "^7.1.3|^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^7.0|^8.0|^9.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + }, + { + "name": "Dries Vints", + "email": "dries.vints@gmail.com" + } + ], + "description": "Provides backwards compatibility for helpers in the latest Laravel release.", + "keywords": [ + "helpers", + "laravel" + ], + "support": { + "source": "https://github.com/laravel/helpers/tree/v1.4.1" + }, + "time": "2021-02-16T15:27:11+00:00" + }, + { + "name": "laravel/legacy-factories", + "version": "v1.1.1", + "source": { + "type": "git", + "url": "https://github.com/laravel/legacy-factories.git", + "reference": "8091d6d64e0e6ea22fb3326ef0b21936d0a0217c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel/legacy-factories/zipball/8091d6d64e0e6ea22fb3326ef0b21936d0a0217c", + "reference": "8091d6d64e0e6ea22fb3326ef0b21936d0a0217c", + "shasum": "" + }, + "require": { + "illuminate/macroable": "^8.0", + "php": "^7.3|^8.0", + "symfony/finder": "^3.4|^4.0|^5.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.x-dev" + }, + "laravel": { + "providers": [ + "Illuminate\\Database\\Eloquent\\LegacyFactoryServiceProvider" + ] + } + }, + "autoload": { + "files": [ + "helpers.php" + ], + "psr-4": { + "Illuminate\\Database\\Eloquent\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "The legacy version of the Laravel Eloquent factories.", + "homepage": "http://laravel.com", + "support": { + "issues": "https://github.com/laravel/framework/issues", + "source": "https://github.com/laravel/framework" + }, + "time": "2021-10-19T13:10:37+00:00" + }, { "name": "laravel/scout", "version": "v8.6.1", @@ -4160,6 +4260,57 @@ }, "time": "2021-08-01T09:19:02+00:00" }, + { + "name": "mbezhanov/faker-provider-collection", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/mbezhanov/faker-provider-collection.git", + "reference": "ae3a9ba9421c3a593836689f08da1443b4065bc0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/mbezhanov/faker-provider-collection/zipball/ae3a9ba9421c3a593836689f08da1443b4065bc0", + "reference": "ae3a9ba9421c3a593836689f08da1443b4065bc0", + "shasum": "" + }, + "require": { + "fakerphp/faker": "^1.13", + "php": "^7.4|^8.0" + }, + "require-dev": { + "php-mock/php-mock": "^2.3", + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "autoload": { + "psr-4": { + "Bezhanov\\Faker\\": "src/Faker" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Marin Bezhanov", + "email": "marin.bezhanov@gmail.com", + "homepage": "http://marinbezhanov.com" + } + ], + "description": "A collection of custom providers for the Faker library", + "keywords": [ + "data", + "faker", + "fixtures" + ], + "support": { + "issues": "https://github.com/mbezhanov/faker-provider-collection/issues", + "source": "https://github.com/mbezhanov/faker-provider-collection/tree/2.0.1" + }, + "time": "2021-03-12T06:57:05+00:00" + }, { "name": "monolog/monolog", "version": "2.3.5", @@ -11762,6 +11913,7 @@ "aliases": [], "minimum-stability": "dev", "stability-flags": { + "bagisto/laravel-datafaker": 20, "tymon/jwt-auth": 20 }, "prefer-stable": true, @@ -11770,5 +11922,5 @@ "php": "^7.3|^8.0" }, "platform-dev": [], - "plugin-api-version": "2.0.0" + "plugin-api-version": "2.1.0" } diff --git a/database/factories/CustomerAddressFactory.php b/database/factories/CustomerAddressFactory.php new file mode 100644 index 000000000..1373d40af --- /dev/null +++ b/database/factories/CustomerAddressFactory.php @@ -0,0 +1,9 @@ +define(\Webkul\Customer\Models\CustomerAddress::class, function (Faker $faker) { + $fakeData = app('Webkul\DataFaker\Repositories\CustomerAddressRepository')->customerAddressDummyData($faker); + + return $fakeData; +}); diff --git a/database/factories/CustomerFactory.php b/database/factories/CustomerFactory.php new file mode 100644 index 000000000..add773f67 --- /dev/null +++ b/database/factories/CustomerFactory.php @@ -0,0 +1,9 @@ +define(\Webkul\Customer\Models\Customer::class, function (Faker $faker) { + $fakeData = app('Webkul\DataFaker\Repositories\CustomerRepository')->customerDummyData($faker); + + return $fakeData; +}); diff --git a/database/factories/ProductAttributeValueFactory.php b/database/factories/ProductAttributeValueFactory.php new file mode 100644 index 000000000..05baff703 --- /dev/null +++ b/database/factories/ProductAttributeValueFactory.php @@ -0,0 +1,7 @@ +define(\Webkul\Product\Models\ProductAttributeValue::class, function (Faker $faker, $attributeValue) { + return $attributeValue; +}); diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php new file mode 100644 index 000000000..cf4777485 --- /dev/null +++ b/database/factories/ProductFactory.php @@ -0,0 +1,15 @@ +define(\Webkul\Product\Models\Product::class, function (Faker $faker) { + $productName = $faker->userName; + + $sku = substr(strtolower(str_replace(array('a','e','i','o','u'), '', $productName)), 0, 6); + + $productSku = str_replace(' ', '', $sku) . "-". str_replace(' ', '', $sku) . "-" . rand(1,9999999) . "-" . rand(1,9999999); + + return [ + 'sku' => $productSku, + 'attribute_family_id'=>1 + ]; +}); diff --git a/database/factories/ProductFlatFactory.php b/database/factories/ProductFlatFactory.php new file mode 100644 index 000000000..cd9a15df6 --- /dev/null +++ b/database/factories/ProductFlatFactory.php @@ -0,0 +1,60 @@ +define(\Webkul\Product\Models\ProductFlat::class, function (Faker $faker, $data) { + $products = $data['product_id']; + + if ($products->type == 'simple') { + + $product = ['product_id' => $products->id]; + + $fakeData = app('Webkul\DataFaker\Repositories\ProductFlatRepository')->GetProductFlatDummyData($faker, $products->type); + + $attributes = app('Webkul\Attribute\Repositories\AttributeRepository')->get(); + + foreach ($attributes as $attribute) { + + if (! isset($fakeData[$attribute->code]) || (in_array($attribute->type, ['date', 'datetime']) && ! $fakeData[$attribute->code])) + continue; + + if ($attribute->type == 'multiselect' || $attribute->type == 'checkbox') { + $fakeData[$attribute->code] = implode(",", $fakeData[$attribute->code]); + } + + if ($attribute->type == 'image' || $attribute->type == 'file') { + $dir = 'product'; + if (gettype($fakeData[$attribute->code]) == 'object') { + $fakeData[$attribute->code] = request()->file($attribute->code)->store($dir); + } else { + $fakeData[$attribute->code] = NULL; + } + } + + $attributeValue = [ + 'product_id' => $product['product_id'], + 'attribute_id' => $attribute->id, + 'value' => $fakeData[$attribute->code], + 'channel' => $attribute->value_per_channel ? $fakeData['channel'] : null, + 'locale' => $attribute->value_per_locale ? $fakeData['locale'] : null + ]; + + $attributeValue[ProductAttributeValue::$attributeTypeFields[$attribute->type]] = $attributeValue['value']; + + unset($attributeValue['value']); + + factory(\Webkul\Product\Models\ProductAttributeValue::class)->create($attributeValue); + } + + $fakeImage = app('Webkul\DataFaker\Repositories\ProductFlatRepository')->uploadImages($faker, $product); + + factory(\Webkul\Product\Models\ProductImage::class, 5)->create($fakeImage); + + + $category = app('Webkul\DataFaker\Repositories\ProductFlatRepository')->createProductCategories($product, $faker); + + return $fakeData; + } +}); diff --git a/database/factories/ProductImageFactory.php b/database/factories/ProductImageFactory.php new file mode 100644 index 000000000..96e5a1144 --- /dev/null +++ b/database/factories/ProductImageFactory.php @@ -0,0 +1,7 @@ +define(\Webkul\Product\Models\ProductImage::class, function ($faker,$fakeImage) { + return $fakeImage; +}); \ No newline at end of file diff --git a/database/factories/ProductInventoryFactory.php b/database/factories/ProductInventoryFactory.php new file mode 100644 index 000000000..10bbe487f --- /dev/null +++ b/database/factories/ProductInventoryFactory.php @@ -0,0 +1,11 @@ +define(\Webkul\Product\Models\ProductInventory::class, function (Faker $faker,$data) { + + return [ + 'product_id' => $data['product_id'], + 'inventory_source_id' => $data['inventory_source_id'], + 'qty' =>rand(1,999) + ]; +}); \ No newline at end of file diff --git a/packages/Sarga/Shop/src/Database/Seeders/DatabaseSeeder.php b/packages/Sarga/Shop/src/Database/Seeders/DatabaseSeeder.php index ae804c09d..b4be5270b 100644 --- a/packages/Sarga/Shop/src/Database/Seeders/DatabaseSeeder.php +++ b/packages/Sarga/Shop/src/Database/Seeders/DatabaseSeeder.php @@ -15,7 +15,6 @@ class DatabaseSeeder extends Seeder { $this->call(SLocalesTableSeeder::class); $this->call(SCurrencyTableSeeder::class); - $this->call(DemoCategoryTableSeeder::class); $this->call(ChannelTableSeeder::class); } diff --git a/packages/Sarga/Shop/src/Database/Seeders/DemoCategoryTableSeeder.php b/packages/Sarga/Shop/src/Database/Seeders/DemoCategoryTableSeeder.php deleted file mode 100644 index 103e5b240..000000000 --- a/packages/Sarga/Shop/src/Database/Seeders/DemoCategoryTableSeeder.php +++ /dev/null @@ -1,52 +0,0 @@ -faker = $faker; - $this->categoryRepository = $categoryRepository; - } - - public function run() - { - $this->categoryRepository->deleteWhere([['id', '!=', 1]]); - for ($i = 2; $i < $this->numberOfParentCategories; $i++) { - $createdCategory = $this->categoryRepository->create([ - 'id' => $i, - 'slug' => $this->faker->slug, - 'name' => $this->faker->firstName, - 'description' => $this->faker->text(), - 'parent_id' => 1, - 'status' => 1, - ]); - - if ($createdCategory) { - for ($j = ($i-1)*$this->numberOfParentCategories; $j < ($i-1)*$this->numberOfParentCategories+$this->numberOfChildCategories; ++$j) { - - $this->categoryRepository->create([ - 'id' => $j, - 'slug' => $this->faker->slug, - 'name' => $this->faker->firstName, - 'description' => $this->faker->text(), - 'parent_id' => $createdCategory->id, - 'status' => 1 - ]); - } - } - } - } -} diff --git a/packages/Sarga/Shop/src/Repositories/AttributeValueRepository.php b/packages/Sarga/Shop/src/Repositories/AttributeValueRepository.php new file mode 100644 index 000000000..1593c20ba --- /dev/null +++ b/packages/Sarga/Shop/src/Repositories/AttributeValueRepository.php @@ -0,0 +1,199 @@ +attribute = $attribute; + + $this->baseProductRepository = $baseProductRepository; + + $this->attributeValue = $attributeValue; + + $this->productInventory = $productInventory; + + parent::__construct($app); + } + + /** + * Specify Model class name + * + * @return mixed + */ + function model() + { + return 'Webkul\Product\Contracts\ProductAttributeValue'; + } + + /** + * Dummy Data For Customer Table. + * + * @return mixed + */ + public function createAttributeValue($data) + { + $localeCode = core()->getCurrentLocale()->code; + + $channelCode = core()->getCurrentChannel()->code; + + $product = $this->baseProductRepository->find($data['product_id']); + + $attributeValues = [ + 'channel' => $channelCode, + 'locale' => $localeCode, + 'sku' => $data['sku'], + 'name' => $data['name'], + 'url_key' => $data['url_key'], + 'new' => $data['new'], + 'featured' => $data['featured'], + 'visible_individually' => $data['visible_individually'], + 'status' => $data['status'], + 'color' => $data['color'], + 'size' => $data['size'], + 'short_description' => $data['short_description'], + 'description' => $data['description'], + 'meta_title' => $data['meta_title'], + 'meta_keywords' => $data['meta_keywords'], + 'meta_description' => $data['meta_description'], + 'price' => $data['price'], + 'cost' => '', + 'special_price' => $data['special_price'], + 'special_price_from' => $data['special_price_from'], + 'special_price_to' => $data['special_price_to'], + 'width' => $data['width'], + 'height' => $data['height'], + 'depth' => $data['depth'], + 'weight' => $data['weight'], + 'channels' => [ + 0 => 1 + ], + ]; + + $attributes = $product->attribute_family->custom_attributes; + + foreach ($attributes as $attribute) { + if (! isset($attributeValues[$attribute->code]) || (in_array($attribute->type, ['date', 'datetime']) && ! $attributeValues[$attribute->code])) + continue; + + if ($attribute->type == 'multiselect' || $attribute->type == 'checkbox') { + $attributeValues[$attribute->code] = implode(",", $attributeValues[$attribute->code]); + } + + if ($attribute->type == 'image' || $attribute->type == 'file') { + $dir = 'product'; + if (gettype($attributeValues[$attribute->code]) == 'object') { + $attributeValues[$attribute->code] = request()->file($attribute->code)->store($dir); + } else { + $attributeValues[$attribute->code] = NULL; + } + } + + $attributeValue = $this->attributeValue->findOneWhere([ + 'product_id' => $product->id, + 'attribute_id' => $attribute->id, + 'channel' => $attribute->value_per_channel ? $channelCode : null, + 'locale' => $attribute->value_per_locale ? $localeCode : null + ]); + + if (! $attributeValue) { + $attributeValue = $this->createValue([ + 'product_id' => $product->id, + 'attribute_id' => $attribute->id, + 'value' => $attributeValues[$attribute->code], + 'channel' => $attribute->value_per_channel ? $channelCode : null, + 'locale' => $attribute->value_per_locale ? $localeCode : null + ]); + } + + $this->attributeValue->update([ + ProductAttributeValue::$attributeTypeFields[$attribute->type] => $attributeValues[$attribute->code] + ], $attributeValue->id + ); + } + } + + /** + * Create The Attribute Value + * + * @param array $data + * @return mixed + */ + public function createValue(array $data) + { + $data['value'] = 1; + + if (isset($data['attribute_id'])) { + + $attribute = $this->attribute->find($data['attribute_id']); + } else { + $attribute = $this->attribute->findOneByField('code', $data['attribute_code']); + } + + if (! $attribute) + return; + + $data[ProductAttributeValue::$attributeTypeFields[$attribute->type]] = $data['value']; + + unset($data['value']); + + return $this->model->create($data); + } +} \ No newline at end of file diff --git a/packages/Sarga/Shop/src/Repositories/CustomerAddressRepository.php b/packages/Sarga/Shop/src/Repositories/CustomerAddressRepository.php new file mode 100644 index 000000000..c1d84e7cf --- /dev/null +++ b/packages/Sarga/Shop/src/Repositories/CustomerAddressRepository.php @@ -0,0 +1,69 @@ +countryRepository = $countryRepository; + + $this->countryStateRepository = $countryStateRepository; + } + + /** + * Dummy Data For Customer Table. + * + * @param $faker + * @return mixed + */ + public function customerAddressDummyData($faker) + { + $countries = $this->countryRepository->get()->random(); + $default_address = $faker->randomElement([1,0]); + + return [ + 'customer_id' => factory('Webkul\Customer\Models\Customer')->create()->id, + 'address1' => $faker->address, + 'country' => $countries->code, + 'state' => $faker->state, + 'city' => $faker->city, + 'postcode' => $faker->postcode, + 'phone' => $faker->phoneNumber, + 'default_address' => $default_address, + 'address_type' => 'customer' + ]; + } +} \ No newline at end of file diff --git a/packages/Sarga/Shop/src/Repositories/CustomerRepository.php b/packages/Sarga/Shop/src/Repositories/CustomerRepository.php new file mode 100644 index 000000000..4fdf862f9 --- /dev/null +++ b/packages/Sarga/Shop/src/Repositories/CustomerRepository.php @@ -0,0 +1,85 @@ +countryRepository = $countryRepository; + + $this->countryStateRepository = $countryStateRepository; + + $this->customerGroupRepository = $customerGroupRepository; + } + + /** + * Dummy Data For Customer Table. + * + * @return mixed + */ + public function customerDummyData($faker) + { + $gender = $faker->randomElement(['male', 'female']); + + $customerGroup = $this->customerGroupRepository->get()->random(); + + return [ + 'first_name' => $faker->firstName($gender), + 'last_name' => $faker->lastName, + 'gender' => $gender, + 'date_of_birth' => $faker->date($format = 'Y-m-d', $max = 'now'), + 'email' => $faker->unique()->safeEmail(), + 'phone' => $faker->e164PhoneNumber, + 'password' => bcrypt('admin123'), + 'customer_group_id' => $customerGroup->id, + 'is_verified' => 1, + 'remember_token' =>str_random(10) + ]; + } +} \ No newline at end of file diff --git a/packages/Sarga/Shop/src/Repositories/ProductFlatRepository.php b/packages/Sarga/Shop/src/Repositories/ProductFlatRepository.php new file mode 100644 index 000000000..6987321f9 --- /dev/null +++ b/packages/Sarga/Shop/src/Repositories/ProductFlatRepository.php @@ -0,0 +1,614 @@ +attribute = $attribute; + + $this->attributeFamilyRepository = $attributeFamilyRepository; + + $this->productAttributeValue = $productAttributeValue; + + $this->baseProductRepository = $baseProductRepository; + + $this->productInventory = $productInventory; + + $this->productImage = $productImage; + + $this->productFlat = $productFlat; + + $this->product = $product; + + $this->relatedProduct = $relatedProduct; + + $this->categoryRepository = $categoryRepository; + + parent::__construct($app); + } + + /** + * Specify Model class name + * + * @return mixed + */ + function model() + { + return 'Webkul\Product\Contracts\Product'; + } + + /** + * Create Product Dummy Data. + * + * @param $faker + * @return mixed + */ + public function getProductFlatDummyData($faker, $productType) + { + switch( $productType ) { + case 'simple': + $fakeData = $this->getSimpleProductDummyData($faker ,$productType); + return $fakeData; + case 'configurable': + $fakeData = $this->getConfigurableProductDummyData($faker, $productType); + return $fakeData; + } + } + + /** + * Dummy Data For Simple Product + * + * @param $faker, $productType + * @return array + */ + public function getSimpleProductDummyData($faker, $productType) + { + $productName = $faker->userName; + + $sku = substr(strtolower(str_replace(array('a','e','i','o','u'), '', $productName)), 0, 6); + + $productSku = str_replace(' ', '', $sku) . "-". str_replace(' ', '', $sku) . "-" . rand(1,9999999) . "-" . rand(1,9999999); + + $price = $faker->numberBetween($min = 0, $max = 500); + + $specialPrice = rand('0', $faker->numberBetween($min = 0, $max = 500)); + + if ($specialPrice == 0) { + $max = $price; + $min = $price; + } else { + $max = $specialPrice; + $min = $specialPrice; + } + + $localeCode = core()->getCurrentLocale()->code; + + $channelCode = core()->getCurrentChannel()->code; + + $productFaker = \Faker\Factory::create(); + + $productFaker->addProvider(new \Bezhanov\Faker\Provider\Commerce($productFaker)); + + $data = [ + 'sku' => $productSku, + 'name' => $productFaker->productName, + 'url_key' => $faker->unique(true)->word . '-' . rand(1,9999999), + 'new' => 1, + 'featured' => 1, + 'visible_individually' => 1, + 'min_price' => $min, + 'max_price' => $max, + 'status' => 1, + 'color' => 1, + 'price' => $price, + 'special_price' => 0, + 'special_price_from' => null, + 'special_price_to' => null, + 'width' => $faker->randomNumber(2), + 'height' => $faker->randomNumber(2), + 'depth' => $faker->randomNumber(2), + 'meta_title' => '', + 'meta_keywords' => '', + 'meta_description' => '', + 'weight' => $faker->randomNumber(2), + 'color_label' => $faker->colorName, + 'size' => 6, + 'size_label' => 'S', + 'short_description' => '

' . $faker->paragraph . '

', + 'description' => '

' . $faker->paragraph . '

', + 'channel' => $channelCode, + 'locale' => $localeCode, + ]; + + return $data; + } + + /** + * Dummy Data For Configurable Product + * + * @param $faker, $productType + * @return array + */ + public function getConfigurableProductDummyData($faker, $productType) + { + $productFaker = \Faker\Factory::create(); + + $productFaker->addProvider(new \Bezhanov\Faker\Provider\Commerce($productFaker)); + + $productName = $productFaker->productName; + + $sku = substr(strtolower(str_replace(array('a','e','i','o','u'), '', $productName)), 0, 6); + + $productSku = str_replace(' ', '', $sku) . "-" . rand(100,9999999); + + $attributeFamily = $this->attributeFamilyRepository->get()->random(); + + $productsTableData = [ + 'type' =>'configurable', + 'attribute_family_id' => $attributeFamily->id, + 'sku' => $productSku, + ]; + + $this->createConfigurableProduct($productsTableData, $faker, $productFaker); + } + + /** + * Create Configurable Product + * + * @param $faker, $data, $productFaker + * @return void + */ + public function createConfigurableProduct($product, $faker, $productFaker) + { + $parentData = $this->createProduct($product,$faker,$productFaker); + $data = [ + 'super_attributes' =>[ + 'size' => [ + 0 => 6, + 1 => 7, + // 2 => 8, + // 3 => 9 + ] + ], + 'family' => 1 + ]; + + $nameAttribute = $this->attribute->findOneByField('code', 'status'); + + $addttribute = $this->productAttributeValue->createValue([ + 'product_id' => $product->id, + 'attribute_id' => $nameAttribute->id, + 'value' => 1 + ]); + + if (isset($data['super_attributes'])) { + + $super_attributes = []; + + foreach ($data['super_attributes'] as $attributeCode => $attributeOptions) { + $attribute = $this->attribute->findOneByField('code', $attributeCode); + + $super_attributes[$attribute->id] = $attributeOptions; + + $product->super_attributes()->attach($attribute->id); + } + + foreach (array_permutation($super_attributes) as $permutation) { + $variantProduct = $this->createVariant($product, $permutation, $faker); + + if (isset($variantProduct)) { + //insert data into product flat + $this->createVariantProduct($variantProduct->getOriginal(), $faker, $productFaker, $permutation['24']); + } + } + } + } + + /** + * Create Product Variant + * + * @param mixed $product + * @param array $permutation + * @param array $data + * @return mixed + */ + public function createVariant($product, $permutation, $faker, $data = []) + { + if (! count($data)) { + $data = [ + "sku" => $product->sku . '-variant-' . implode('-', $permutation), + "name" => "", + "inventories" => [], + "price" => 0, + "weight" => 0, + "status" => 1 + ]; + } + + $variant = $this->model->create([ + 'parent_id' => $product->id, + 'type' => 'simple', + 'attribute_family_id' => $product->attribute_family_id, + 'sku' => $data['sku'], + ]); + + foreach (['sku', 'name', 'price', 'weight', 'status'] as $attributeCode) { + $attribute = $this->attribute->findOneByField('code', $attributeCode); + + if ($attribute->value_per_channel) { + if ($attribute->value_per_locale) { + foreach (core()->getAllChannels() as $channel) { + foreach (core()->getAllLocales() as $locale) { + $datas = $this->productAttributeValue->createValue([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'channel' => $channel->code, + 'locale' => $locale->code, + 'value' => $data[$attributeCode] + ]); + } + } + } else { + foreach (core()->getAllChannels() as $channel) { + $datas = $this->productAttributeValue->createValue([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'channel' => $channel->code, + 'value' => $data[$attributeCode] + ]); + } + } + } else { + if ($attribute->value_per_locale) { + foreach (core()->getAllLocales() as $locale) { + $datas = $this->productAttributeValue->createValue([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'locale' => $locale->code, + 'value' => $data[$attributeCode] + ]); + } + } else { + $datas = $this->productAttributeValue->createValue([ + 'product_id' => $variant->id, + 'attribute_id' => $attribute->id, + 'value' => $data[$attributeCode] + ]); + } + } + } + + foreach ($permutation as $attributeId => $optionId) { + $this->productAttributeValue->createValue([ + 'product_id' => $variant->id, + 'attribute_id' => $attributeId, + 'value' => $optionId + ]); + } + + $inventory = [ + 'inventories' => [ + 1 => $faker->randomNumber(2) + ] + ]; + + $this->productInventory->saveInventories($inventory, $variant); + + return $variant; + } + + /** + * Create Product + * + * @param mixed $product + * @param array $productFaker + * @param array $faker + * @return mixed + */ + public function createProduct($product, $faker, $productFaker) + { + $localeCode = core()->getCurrentLocale()->code; + $channelCode = core()->getCurrentChannel()->code; + + if ($product['type'] == 'configurable') { + $price = null; + $parentId = null; + $urlKey = $faker->unique(true)->word . '-' . rand(1,9999999); + $new = 1; + $feature = 1; + $description = $faker->paragraph; + $description = $faker->paragraph; + $visibleIndividually = 1; + } + + $data = [ + 'product_id' => $product['id'], + 'sku' => $faker->word, + 'name' => $productFaker->productName, + 'url_key' => $urlKey, + 'new' => $new, + 'featured' => $feature, + 'visible_individually' => $visibleIndividually, + 'min_price' => $faker->numberBetween($min = 100, $max = 200), + 'max_price' => $faker->numberBetween($min = 200, $max = 500), + 'parent_id' => $parentId, + 'status' => 1, + 'color' => 1, + 'price' => $price, + 'width' => null, + 'height' =>null, + 'depth' => null, + 'meta_title' => '', + 'meta_keywords' => '', + 'meta_description' => '', + 'weight' => null, + 'color_label' => 'Red', + 'size' => null, + 'size_label' => null, + 'short_description' => '

' . $description . '

', + 'description' => '

' . $description . '

', + 'channel' => $channelCode, + 'locale' => $localeCode, + 'special_price' => null, + 'special_price_from' => null, + 'special_price_to' => null, + ]; + + $this->productAttributeValue->createAttributeValue($data); + + $this->productFlat->create($data); + } + + /** + * Create Variant Product + * + * @param mixed $product + * @param array $productFaker + * @param array $faker + * @return mixed + */ + public function createVariantProduct($variantProduct, $faker, $productFaker, $size) + { + $localeCode = core()->getCurrentLocale()->code; + + $channelCode = core()->getCurrentChannel()->code; + + if ($variantProduct['type'] == 'simple') { + $price = $faker->numberBetween($min = 0, $max = 500); + $parentId = $variantProduct['id']; + } + + switch($size) { + case '6': $sizeLabel = 'S'; + break; + case '7': $sizeLabel = 'M'; + break; + case '8': $sizeLabel = 'L'; + break; + case '9': $sizeLabel = 'XL'; + } + + $data = [ + 'product_id' => $variantProduct['id'], + 'sku' => $variantProduct['sku'], + 'name' => $productFaker->productName, + 'url_key' => null, + 'new' => null, + 'featured' => null, + 'visible_individually' => null, + 'min_price' => $price, + 'max_price' => $price, + 'parent_id' => $parentId, + 'status' => 1, + 'color' => null, + 'price' => $price, + 'width' => $faker->randomNumber(2), + 'height' => $faker->randomNumber(2), + 'depth' => $faker->randomNumber(2), + 'meta_title' => '', + 'meta_keywords' => '', + 'meta_description' => '', + 'weight' => $faker->randomNumber(2), + 'color_label' => null, + 'size' => $size, + 'size_label' => $sizeLabel, + 'short_description' => null, + 'description' => null, + 'channel' => $channelCode, + 'locale' => $localeCode, + 'special_price' => null, + 'special_price_from' => null, + 'special_price_to' => null, + ]; + + $parentId = $this->productFlat->findOneWhere(['product_id' => $variantProduct['parent_id']])->id; + + $data['parent_id'] = $parentId; + + $this->productAttributeValue->createAttributeValue($data); + + $this->productFlat->create($data); + } + + /** + * Upload Product Images + * + * @param mixed $product + * @param array $productFaker + * @return mixed + */ + public function uploadImages($faker, $product) + { + + $filepath = storage_path('app/public/product/'); + + Storage::makeDirectory('/product/'. $product['product_id']); + + $path = $faker->image($filepath. $product['product_id'], 800, 800, 'food', true, true); + + + $pos = strpos($path, 'product'); + + $imagePath = substr($path, $pos); + + $data = [ + 'path' => $imagePath, + 'product_id' => $product['product_id'] + ]; + + return $data; + } + + /** + * Create Product Categories + * + * @param mixed $product + * @param array $productFaker + * @return mixed + */ + public function createProductCategories($product, $faker) + { + $categories = $this->categoryRepository->all()->random(3); + + $filterableAttribute = ['11', '23', '24', '25']; + + foreach ($categories as $category) { + if (! empty($category->translations) && count($category->translations) > 0) { + foreach ($category->translations as $translation) { + + DB::table('product_categories')->insert([ + 'product_id' => $product['product_id'], + 'category_id' => $translation->category_id, + ]); + + foreach ($filterableAttribute as $categoryFilterableAttribute) { + + $categoryExist = DB::table('category_filterable_attributes')->where('category_id',$translation->category_id)->count(); + + if ($categoryExist < 4) { + DB::table('category_filterable_attributes')->insert([ + 'attribute_id' => $categoryFilterableAttribute, + 'category_id' => $translation->category_id, + ]); + } + } + } + } + } + } +} \ No newline at end of file diff --git a/packages/Sarga/Shop/src/Repositories/RelatedProductRepository.php b/packages/Sarga/Shop/src/Repositories/RelatedProductRepository.php new file mode 100644 index 000000000..a50540306 --- /dev/null +++ b/packages/Sarga/Shop/src/Repositories/RelatedProductRepository.php @@ -0,0 +1,66 @@ +product = $product; + } + + /** + * Dummy Data For Customer Table. + * + * @return mixed + */ + public function getRelatedProducts($product) + { + // $productCount = $this->product->count(); + + // if ($productCount > 4) { + + // $products = $this->product->get()->random(4); + + // foreach ($products as $relatedProduct) { + // if ( $product['product_id'] != $relatedProduct->id) { + // DB::table('product_up_sells')->insert([ + // 'parent_id' => $product['product_id'], + // 'child_id' => $relatedProduct->id, + // ]); + + // DB::table('product_cross_sells')->insert([ + // 'parent_id' => $product['product_id'], + // 'child_id' => $relatedProduct->id, + // ]); + // } + // } + // } + } +}