From 029b77df233f63e9987975518e80b541fdc4c62a Mon Sep 17 00:00:00 2001 From: Abdullah Al-Faqeir Date: Mon, 4 Oct 2021 11:30:32 +0300 Subject: [PATCH] - Refactoring to Laravel 8 Class Model Factories. --- composer.json | 1 - composer.lock | 60 +----- database/factories/UserFactory.php | 12 +- .../Database/Factories/AttributeFactory.php | 190 +++++++++++------ .../Factories/AttributeFamilyFactory.php | 39 +++- .../Factories/AttributeOptionFactory.php | 95 ++------- .../Webkul/Attribute/src/Models/Attribute.php | 29 ++- .../Providers/AttributeServiceProvider.php | 9 +- .../BookingProductEventTicketFactory.php | 38 +++- .../Factories/BookingProductFactory.php | 44 ++-- .../src/Models/BookingProduct.php | 34 ++- .../src/Models/BookingProductEventTicket.php | 21 +- .../BookingProductServiceProvider.php | 19 +- .../Webkul/CartRule/src/Models/CartRule.php | 24 ++- .../CartRule/src/Models/CartRuleCoupon.php | 18 +- .../Database/Factories/CategoryFactory.php | 68 +++++- .../Webkul/Category/src/Models/Category.php | 59 ++++-- .../src/Providers/CategoryServiceProvider.php | 18 +- .../Database/Factories/CartAddressFactory.php | 38 +++- .../src/Database/Factories/CartFactory.php | 80 ++++--- .../Database/Factories/CartItemFactory.php | 73 ++++--- .../Database/Factories/CartPaymentFactory.php | 36 +++- packages/Webkul/Checkout/src/Models/Cart.php | 77 ++++--- .../Checkout/src/Models/CartAddress.php | 30 ++- .../Webkul/Checkout/src/Models/CartItem.php | 43 ++-- .../Checkout/src/Models/CartPayment.php | 16 ++ .../src/Providers/CheckoutServiceProvider.php | 27 +-- .../Factories/CartRuleCouponFactory.php | 45 ++-- .../Database/Factories/CartRuleFactory.php | 69 +++--- .../src/Database/Factories/ChannelFactory.php | 63 +++--- .../ChannelInventorySourceFactory.php | 34 ++- .../Database/Factories/CurrencyFactory.php | 32 ++- .../src/Database/Factories/LocaleFactory.php | 60 ++++-- .../Factories/SubscriberListFactory.php | 37 +++- packages/Webkul/Core/src/Models/Address.php | 49 +++-- packages/Webkul/Core/src/Models/Channel.php | 34 ++- packages/Webkul/Core/src/Models/Currency.php | 24 ++- packages/Webkul/Core/src/Models/Locale.php | 16 ++ .../Core/src/Models/SubscribersList.php | 19 +- .../src/Providers/CoreServiceProvider.php | 43 ++-- .../Factories/CustomerAddressFactory.php | 66 ++++-- .../Database/Factories/CustomerFactory.php | 91 +++++--- .../Factories/CustomerGroupFactory.php | 41 +++- .../Webkul/Customer/src/Models/Customer.php | 87 +++++--- .../Customer/src/Models/CustomerAddress.php | 18 +- .../Customer/src/Models/CustomerGroup.php | 27 ++- .../src/Providers/CustomerServiceProvider.php | 34 +-- .../Factories/InventorySourceFactory.php | 68 +++--- .../Inventory/src/Models/InventorySource.php | 16 ++ .../Providers/InventoryServiceProvider.php | 11 +- .../ProductAttributeValueFactory.php | 38 +++- .../ProductDownloadableLinkFactory.php | 64 +++--- .../ProductDownloadableLinkTranslation.php | 17 -- ...ductDownloadableLinkTranslationFactory.php | 33 +++ .../src/Database/Factories/ProductFactory.php | 86 ++++++-- .../Factories/ProductInventoryFactory.php | 40 ++-- .../Factories/ProductReviewFactory.php | 41 ++-- .../Webkul/Product/src/Models/Product.php | 200 +++++++++++------- .../src/Models/ProductAttributeValue.php | 21 +- .../src/Models/ProductDownloadableLink.php | 30 ++- .../ProductDownloadableLinkTranslation.php | 16 ++ .../Product/src/Models/ProductInventory.php | 21 +- .../Product/src/Models/ProductReview.php | 24 ++- .../src/Providers/ProductServiceProvider.php | 38 ++-- .../Factories/InventorySourceFactory.php | 26 --- .../src/Database/Factories/InvoiceFactory.php | 129 +++++++---- .../Database/Factories/InvoiceItemFactory.php | 73 ++++--- .../Factories/OrderAddressFactory.php | 76 +++++-- .../src/Database/Factories/OrderFactory.php | 148 ++++++++----- .../Database/Factories/OrderItemFactory.php | 85 +++++--- .../Factories/OrderPaymentFactory.php | 32 ++- .../src/Database/Factories/RefundFactory.php | 34 ++- .../Database/Factories/ShipmentFactory.php | 43 ++-- packages/Webkul/Sales/src/Models/Invoice.php | 41 +++- .../Webkul/Sales/src/Models/InvoiceItem.php | 41 +++- packages/Webkul/Sales/src/Models/Order.php | 85 +++++--- .../Webkul/Sales/src/Models/OrderAddress.php | 27 ++- .../Webkul/Sales/src/Models/OrderItem.php | 67 +++--- .../Webkul/Sales/src/Models/OrderPayment.php | 18 +- packages/Webkul/Sales/src/Models/Refund.php | 38 +++- packages/Webkul/Sales/src/Models/Shipment.php | 31 ++- .../src/Providers/SalesServiceProvider.php | 27 +-- .../Database/Factories/TaxCategoryFactory.php | 36 +++- .../src/Database/Factories/TaxMapFactory.php | 39 ++-- .../src/Database/Factories/TaxRateFactory.php | 46 ++-- .../Webkul/Tax/src/Models/TaxCategory.php | 28 ++- packages/Webkul/Tax/src/Models/TaxMap.php | 14 ++ packages/Webkul/Tax/src/Models/TaxRate.php | 17 +- 88 files changed, 2677 insertions(+), 1375 deletions(-) delete mode 100644 packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslation.php create mode 100644 packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslationFactory.php delete mode 100644 packages/Webkul/Sales/src/Database/Factories/InventorySourceFactory.php diff --git a/composer.json b/composer.json index 9fec01322..e78a54f53 100755 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "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 cbe5fd3c3..3189b4011 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": "ad18338e686e8f7ba9eaf04d9bfa2374", + "content-hash": "487d5fda315fc77479141cb31da7a32c", "packages": [ { "name": "algolia/algoliasearch-client-php", @@ -3061,62 +3061,6 @@ }, "time": "2021-05-11T14:00:02+00:00" }, - { - "name": "laravel/legacy-factories", - "version": "v1.1.0", - "source": { - "type": "git", - "url": "https://github.com/laravel/legacy-factories.git", - "reference": "5e3fe2fd5fda64e20ea5c74c831a7346294e902a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/laravel/legacy-factories/zipball/5e3fe2fd5fda64e20ea5c74c831a7346294e902a", - "reference": "5e3fe2fd5fda64e20ea5c74c831a7346294e902a", - "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": "2020-10-27T14:25:32+00:00" - }, { "name": "laravel/scout", "version": "v8.6.1", @@ -11594,5 +11538,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/UserFactory.php b/database/factories/UserFactory.php index 467777c5f..72f13b707 100755 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -1,6 +1,5 @@ define(App\User::class, function (Faker $faker) { - return [ - 'name' => $faker->name, - 'email' => $faker->unique()->safeEmail, - 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret - 'remember_token' => str_random(10), - ]; -}); \ No newline at end of file +*/ \ No newline at end of file diff --git a/packages/Webkul/Attribute/src/Database/Factories/AttributeFactory.php b/packages/Webkul/Attribute/src/Database/Factories/AttributeFactory.php index 507f8bcdf..9682f5761 100644 --- a/packages/Webkul/Attribute/src/Database/Factories/AttributeFactory.php +++ b/packages/Webkul/Attribute/src/Database/Factories/AttributeFactory.php @@ -1,84 +1,144 @@ define(Attribute::class, function (Faker $faker, array $attributes) { - $types = [ - 'text', - 'textarea', - 'price', - 'boolean', - 'select', - 'multiselect', - 'datetime', - 'date', - 'image', - 'file', - 'checkbox', +class AttributeFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Attribute::class; + + /** + * @var array + */ + protected $states = [ + 'validation_numeric', + 'validation_email', + 'validation_decimal', + 'validation_url', + 'required', + 'unique', + 'filterable', + 'configurable', ]; - $locales = Locale::pluck('code')->all(); + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $types = [ + 'text', + 'textarea', + 'price', + 'boolean', + 'select', + 'multiselect', + 'datetime', + 'date', + 'image', + 'file', + 'checkbox', + ]; - // array $attributes does not contain any locale code - if (count(array_diff_key(array_flip($locales), $attributes) ) === count($locales)) { - $localeCode = $locales[0]; - - $attributes[$localeCode] = [ - 'name' => $faker->word, + return [ + 'admin_name' => $this->faker->word, + 'code' => $this->faker->word, + 'type' => array_rand($types), + 'validation' => '', + 'position' => $this->faker->randomDigit, + 'is_required' => false, + 'is_unique' => false, + 'value_per_locale' => false, + 'value_per_channel' => false, + 'is_filterable' => false, + 'is_configurable' => false, + 'is_user_defined' => true, + 'is_visible_on_front' => true, + 'swatch_type' => null, + 'use_in_flat' => true, ]; } - return [ - 'admin_name' => $faker->word, - 'code' => $faker->word, - 'type' => array_rand($types), - 'validation' => '', - 'position' => $faker->randomDigit, - 'is_required' => false, - 'is_unique' => false, - 'value_per_locale' => false, - 'value_per_channel' => false, - 'is_filterable' => false, - 'is_configurable' => false, - 'is_user_defined' => true, - 'is_visible_on_front' => true, - 'swatch_type' => null, - 'use_in_flat' => true, - ]; -}); + public function validation_numeric(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'validation' => 'numeric', + ]; + }); + } -$factory->state(Attribute::class, 'validation_numeric', [ - 'validation' => 'numeric', -]); + public function validation_email(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'validation' => 'email', + ]; + }); + } -$factory->state(Attribute::class, 'validation_email', [ - 'validation' => 'email', -]); + public function validation_decimal(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'validation' => 'decimal', + ]; + }); + } -$factory->state(Attribute::class, 'validation_decimal', [ - 'validation' => 'decimal', -]); + public function validation_url(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'validation' => 'url', + ]; + }); + } -$factory->state(Attribute::class, 'validation_url', [ - 'validation' => 'url', -]); + public function required(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'is_required' => true, + ]; + }); + } -$factory->state(Attribute::class, 'required', [ - 'is_required' => true, -]); + public function unique(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'is_unique' => true, + ]; + }); + } -$factory->state(Attribute::class, 'unique', [ - 'is_unique' => true, -]); + public function filterable(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'is_filterable' => true, + ]; + }); + } -$factory->state(Attribute::class, 'filterable', [ - 'is_filterable' => true, -]); + public function configurable(): AttributeFactory + { + return $this->state(function (array $attributes) { + return [ + 'is_configurable' => true, + ]; + }); + } -$factory->state(Attribute::class, 'configurable', [ - 'is_configurable' => true, -]); \ No newline at end of file +} diff --git a/packages/Webkul/Attribute/src/Database/Factories/AttributeFamilyFactory.php b/packages/Webkul/Attribute/src/Database/Factories/AttributeFamilyFactory.php index bf4eb0fc5..caaa05ecb 100644 --- a/packages/Webkul/Attribute/src/Database/Factories/AttributeFamilyFactory.php +++ b/packages/Webkul/Attribute/src/Database/Factories/AttributeFamilyFactory.php @@ -1,15 +1,34 @@ define(AttributeFamily::class, function (Faker $faker) { - return [ - 'name' => $faker->word(), - 'code' => $faker->word(), - 'is_user_defined' => rand(0, 1), - 'status' => 0, - ]; -}); +class AttributeFamilyFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = AttributeFamily::class; + + /** + * Define the model's default state. + * + * @return array + * @throws \Exception + */ + public function definition(): array + { + return [ + 'name' => $this->faker->word(), + 'code' => $this->faker->word(), + 'is_user_defined' => random_int(0, 1), + 'status' => 0, + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Attribute/src/Database/Factories/AttributeOptionFactory.php b/packages/Webkul/Attribute/src/Database/Factories/AttributeOptionFactory.php index 963717b79..94b848922 100644 --- a/packages/Webkul/Attribute/src/Database/Factories/AttributeOptionFactory.php +++ b/packages/Webkul/Attribute/src/Database/Factories/AttributeOptionFactory.php @@ -1,83 +1,34 @@ define(AttributeOption::class, function (Faker $faker, array $attributes) { +class AttributeOptionFactory extends Factory +{ - $locales = Locale::pluck('code')->all(); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = AttributeOption::class; - // array $attributes does not contain any locale code - if (count(array_diff_key(array_flip($locales), $attributes) ) === count($locales)) { - $localeCode = $locales[0]; - - $attributes[$localeCode] = [ - 'label' => $faker->word, + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'admin_name' => $this->faker->word, + 'sort_order' => $this->faker->randomDigit, + 'attribute_id' => Attribute::factory(['swatch_type' => 'text']), + 'swatch_value' => null, ]; } - return [ - 'admin_name' => $faker->word, - 'sort_order' => $faker->randomDigit, - 'attribute_id' => function () { - return factory(Attribute::class)->create()->id; - }, - 'swatch_value' => null, - ]; -}); - -$factory->define(AttributeOption::class, function (Faker $faker, array $attributes) { - return [ - 'admin_name' => $faker->word, - 'sort_order' => $faker->randomDigit, - 'attribute_id' => function () { - return factory(Attribute::class) - ->create(['swatch_type' => 'color']) - ->id; - }, - 'swatch_value' => $faker->hexColor, - ]; -}); - -$factory->define(AttributeOption::class, function (Faker $faker, array $attributes) { - return [ - 'admin_name' => $faker->word, - 'sort_order' => $faker->randomDigit, - 'attribute_id' => function () { - return factory(Attribute::class) - ->create(['swatch_type' => 'image']) - ->id; - }, - 'swatch_value' => '/tests/_data/ProductImageExampleForUpload.jpg', - ]; -}); - -$factory->define(AttributeOption::class, function (Faker $faker, array $attributes) { - return [ - 'admin_name' => $faker->word, - 'sort_order' => $faker->randomDigit, - 'attribute_id' => function () { - return factory(Attribute::class) - ->create(['swatch_type' => 'dropdown']) - ->id; - }, - 'swatch_value' => null, - ]; -}); - -$factory->define(AttributeOption::class, function (Faker $faker, array $attributes) { - return [ - 'admin_name' => $faker->word, - 'sort_order' => $faker->randomDigit, - 'attribute_id' => function () { - return factory(Attribute::class) - ->create(['swatch_type' => 'text']) - ->id; - }, - 'swatch_value' => null, - ]; -}); \ No newline at end of file +} \ No newline at end of file diff --git a/packages/Webkul/Attribute/src/Models/Attribute.php b/packages/Webkul/Attribute/src/Models/Attribute.php index 73cdfb403..43ea1be24 100755 --- a/packages/Webkul/Attribute/src/Models/Attribute.php +++ b/packages/Webkul/Attribute/src/Models/Attribute.php @@ -2,11 +2,18 @@ namespace Webkul\Attribute\Models; +use Illuminate\Database\Eloquent\Builder; use Webkul\Core\Eloquent\TranslatableModel; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Attribute\Database\Factories\AttributeFactory; use Webkul\Attribute\Contracts\Attribute as AttributeContract; class Attribute extends TranslatableModel implements AttributeContract { + + use HasFactory; + public $translatedAttributes = ['name']; protected $fillable = [ @@ -33,7 +40,7 @@ class Attribute extends TranslatableModel implements AttributeContract /** * Get the options. */ - public function options() + public function options(): HasMany { return $this->hasMany(AttributeOptionProxy::modelClass()); } @@ -41,11 +48,25 @@ class Attribute extends TranslatableModel implements AttributeContract /** * Scope a query to only include popular users. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query + * * @return \Illuminate\Database\Eloquent\Builder */ - public function scopeFilterableAttributes($query) + public function scopeFilterableAttributes(Builder $query): Builder { - return $query->where('is_filterable', 1)->where('swatch_type', '<>', 'image')->orderBy('position'); + return $query->where('is_filterable', 1) + ->where('swatch_type', '<>', 'image') + ->orderBy('position'); } + + /** + * Create a new factory instance for the model + * + * @return AttributeFactory + */ + protected static function newFactory(): AttributeFactory + { + return AttributeFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Attribute/src/Providers/AttributeServiceProvider.php b/packages/Webkul/Attribute/src/Providers/AttributeServiceProvider.php index 6fa64e726..17a639e61 100755 --- a/packages/Webkul/Attribute/src/Providers/AttributeServiceProvider.php +++ b/packages/Webkul/Attribute/src/Providers/AttributeServiceProvider.php @@ -2,20 +2,19 @@ namespace Webkul\Attribute\Providers; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; use Illuminate\Support\ServiceProvider; class AttributeServiceProvider extends ServiceProvider { + /** * Bootstrap services. * * @return void */ - public function boot() + public function boot(): void { - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); - - $this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); } + } \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductEventTicketFactory.php b/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductEventTicketFactory.php index 608dd477d..584be0ac1 100644 --- a/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductEventTicketFactory.php +++ b/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductEventTicketFactory.php @@ -1,17 +1,33 @@ define(BookingProductEventTicket::class, static function (Faker $faker, array $attributes) { - return [ - 'price' => $faker->randomFloat(4, 3, 900), - 'qty' => $faker->numberBetween(100, 1000), - 'booking_product_id' => static function () { - return factory(BookingProduct::class)->create(['type' => 'event'])->id; - } - ]; -}); \ No newline at end of file +class BookingProductEventTicketFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = BookingProductEventTicket::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'price' => $this->faker->randomFloat(4, 3, 900), + 'qty' => $this->faker->numberBetween(100, 1000), + 'booking_product_id' => BookingProduct::factory(), + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductFactory.php b/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductFactory.php index 738d0e8a3..13edec3b5 100644 --- a/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductFactory.php +++ b/packages/Webkul/BookingProduct/src/Database/Factories/BookingProductFactory.php @@ -1,22 +1,38 @@ define(BookingProduct::class, function (Faker $faker, array $attributes) { - $bookingTypes = ['event']; +class BookingProductFactory extends Factory +{ - return [ - 'type' => $bookingTypes[array_rand(['event'])], - 'qty' => $faker->randomNumber(2), - 'available_from' => Carbon::yesterday(), - 'available_to' => Carbon::tomorrow(), - 'product_id' => function () { - return factory(Product::class)->create(['type' => 'booking'])->id; - } - ]; -}); \ No newline at end of file + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = BookingProduct::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $bookingTypes = ['event']; + + return [ + 'type' => $bookingTypes[array_rand(['event'])], + 'qty' => $this->faker->randomNumber(2), + 'available_from' => Carbon::yesterday(), + 'available_to' => Carbon::tomorrow(), + 'product_id' => Product::factory(['type' => 'booking']), + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Models/BookingProduct.php b/packages/Webkul/BookingProduct/src/Models/BookingProduct.php index 287247fcf..a1cc69704 100644 --- a/packages/Webkul/BookingProduct/src/Models/BookingProduct.php +++ b/packages/Webkul/BookingProduct/src/Models/BookingProduct.php @@ -4,10 +4,15 @@ namespace Webkul\BookingProduct\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Product\Models\ProductProxy; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\BookingProduct\Database\Factories\BookingProductFactory; use Webkul\BookingProduct\Contracts\BookingProduct as BookingProductContract; class BookingProduct extends Model implements BookingProductContract { + + use HasFactory; + protected $fillable = [ 'location', 'show_location', @@ -19,7 +24,13 @@ class BookingProduct extends Model implements BookingProductContract 'product_id', ]; - protected $with = ['default_slot', 'appointment_slot', 'event_tickets', 'rental_slot', 'table_slot']; + protected $with = [ + 'default_slot', + 'appointment_slot', + 'event_tickets', + 'rental_slot', + 'table_slot', + ]; protected $casts = [ 'available_from' => 'datetime', @@ -29,7 +40,7 @@ class BookingProduct extends Model implements BookingProductContract /** * The Product Default Booking that belong to the product booking. */ - public function default_slot() + public function default_slot(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(BookingProductDefaultSlotProxy::modelClass()); } @@ -37,7 +48,7 @@ class BookingProduct extends Model implements BookingProductContract /** * The Product Appointment Booking that belong to the product booking. */ - public function appointment_slot() + public function appointment_slot(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(BookingProductAppointmentSlotProxy::modelClass()); } @@ -45,7 +56,7 @@ class BookingProduct extends Model implements BookingProductContract /** * The Product Event Booking that belong to the product booking. */ - public function event_tickets() + public function event_tickets(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(BookingProductEventTicketProxy::modelClass()); } @@ -53,7 +64,7 @@ class BookingProduct extends Model implements BookingProductContract /** * The Product Rental Booking that belong to the product booking. */ - public function rental_slot() + public function rental_slot(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(BookingProductRentalSlotProxy::modelClass()); } @@ -61,8 +72,19 @@ class BookingProduct extends Model implements BookingProductContract /** * The Product Table Booking that belong to the product booking. */ - public function table_slot() + public function table_slot(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(BookingProductTableSlotProxy::modelClass()); } + + /** + * Create a new factory instance for the model. + * + * @return BookingProductFactory + */ + protected static function newFactory(): BookingProductFactory + { + return BookingProductFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Models/BookingProductEventTicket.php b/packages/Webkul/BookingProduct/src/Models/BookingProductEventTicket.php index ec35a5788..ad364ce4d 100644 --- a/packages/Webkul/BookingProduct/src/Models/BookingProductEventTicket.php +++ b/packages/Webkul/BookingProduct/src/Models/BookingProductEventTicket.php @@ -3,13 +3,21 @@ namespace Webkul\BookingProduct\Models; use Webkul\Core\Eloquent\TranslatableModel; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\BookingProduct\Database\Factories\BookingProductEventTicketFactory; use Webkul\BookingProduct\Contracts\BookingProductEventTicket as BookingProductEventTicketContract; class BookingProductEventTicket extends TranslatableModel implements BookingProductEventTicketContract { + + use HasFactory; + public $timestamps = false; - public $translatedAttributes = ['name', 'description']; + public $translatedAttributes = [ + 'name', + 'description', + ]; protected $fillable = [ 'price', @@ -19,4 +27,15 @@ class BookingProductEventTicket extends TranslatableModel implements BookingProd 'special_price_to', 'booking_product_id', ]; + + /** + * Create a new factory instance for the model. + * + * @return BookingProductEventTicketFactory + */ + protected static function newFactory(): BookingProductEventTicketFactory + { + return BookingProductEventTicketFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/BookingProduct/src/Providers/BookingProductServiceProvider.php b/packages/Webkul/BookingProduct/src/Providers/BookingProductServiceProvider.php index b31d28d7f..c816bdf3e 100644 --- a/packages/Webkul/BookingProduct/src/Providers/BookingProductServiceProvider.php +++ b/packages/Webkul/BookingProduct/src/Providers/BookingProductServiceProvider.php @@ -2,11 +2,11 @@ namespace Webkul\BookingProduct\Providers; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; use Illuminate\Support\ServiceProvider; class BookingProductServiceProvider extends ServiceProvider { + /** * Bootstrap services. * @@ -15,21 +15,19 @@ class BookingProductServiceProvider extends ServiceProvider */ public function boot(): void { - $this->loadRoutesFrom(__DIR__ . '/../Http/front-routes.php'); + $this->loadRoutesFrom(__DIR__.'/../Http/front-routes.php'); - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'bookingproduct'); + $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'bookingproduct'); - $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'bookingproduct'); + $this->loadViewsFrom(__DIR__.'/../Resources/views', 'bookingproduct'); $this->publishes([ - __DIR__ . '/../../publishable/assets' => public_path('themes/default/assets'), + __DIR__.'/../../publishable/assets' => public_path('themes/default/assets'), ], 'public'); $this->app->register(EventServiceProvider::class); - - $this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories'); } /** @@ -39,8 +37,7 @@ class BookingProductServiceProvider extends ServiceProvider */ public function register(): void { - $this->mergeConfigFrom( - dirname(__DIR__) . '/Config/product_types.php', 'product_types' - ); + $this->mergeConfigFrom(dirname(__DIR__).'/Config/product_types.php', 'product_types'); } + } \ No newline at end of file diff --git a/packages/Webkul/CartRule/src/Models/CartRule.php b/packages/Webkul/CartRule/src/Models/CartRule.php index 23a6af933..ac2c9f9ba 100644 --- a/packages/Webkul/CartRule/src/Models/CartRule.php +++ b/packages/Webkul/CartRule/src/Models/CartRule.php @@ -3,12 +3,17 @@ namespace Webkul\CartRule\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\CartRule\Database\Factories\CartRuleFactory; use Webkul\CartRule\Contracts\CartRule as CartRuleContract; use Webkul\Core\Models\ChannelProxy; use Webkul\Customer\Models\CustomerGroupProxy; class CartRule extends Model implements CartRuleContract { + + use HasFactory; + protected $fillable = [ 'name', 'description', @@ -105,7 +110,8 @@ class CartRule extends Model implements CartRuleContract */ public function coupon_code(): \Illuminate\Database\Eloquent\Relations\HasOne { - return $this->cart_rule_coupon()->where('is_primary', 1); + return $this->cart_rule_coupon() + ->where('is_primary', 1); } /** @@ -115,12 +121,24 @@ class CartRule extends Model implements CartRuleContract */ public function getCouponCodeAttribute() { - $coupon = $this->coupon_code()->first(); + $coupon = $this->coupon_code() + ->first(); - if (! $coupon) { + if (!$coupon) { return; } return $coupon->code; } + + /** + * Create a new factory instance for the model. + * + * @return CartRuleFactory + */ + protected static function newFactory(): CartRuleFactory + { + return CartRuleFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/CartRule/src/Models/CartRuleCoupon.php b/packages/Webkul/CartRule/src/Models/CartRuleCoupon.php index d072f1ee4..01a5f02f7 100644 --- a/packages/Webkul/CartRule/src/Models/CartRuleCoupon.php +++ b/packages/Webkul/CartRule/src/Models/CartRuleCoupon.php @@ -3,10 +3,15 @@ namespace Webkul\CartRule\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\CartRule\Database\Factories\CartRuleCouponFactory; use Webkul\CartRule\Contracts\CartRuleCoupon as CartRuleCouponContract; class CartRuleCoupon extends Model implements CartRuleCouponContract { + use HasFactory; + protected $fillable = [ 'code', 'usage_limit', @@ -21,8 +26,19 @@ class CartRuleCoupon extends Model implements CartRuleCouponContract /** * Get the cart rule that owns the cart rule coupon. */ - public function cart_rule() + public function cart_rule(): BelongsTo { return $this->belongsTo(CartRuleProxy::modelClass()); } + + /** + * Create a new factory instance for the model + * + * @return CartRuleCouponFactory + */ + protected static function newFactory(): CartRuleCouponFactory + { + return CartRuleCouponFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php b/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php index 359cae23c..79ecddbb9 100644 --- a/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php +++ b/packages/Webkul/Category/src/Database/Factories/CategoryFactory.php @@ -1,18 +1,64 @@ define(Category::class, function (Faker $faker, array $attributes) { +class CategoryFactory extends Factory +{ - return [ - 'status' => 1, - 'position' => $faker->randomDigit, - 'parent_id' => 1, + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Category::class; + + /** + * @var string[] + */ + protected $states = [ + 'inactive', + 'rtl', ]; -}); -$factory->state(Category::class, 'inactive', [ - 'status' => 0, -]); + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'status' => 1, + 'position' => $this->faker->randomDigit, + 'parent_id' => 1, + ]; + } + + /** + * + */ + public function inactive(): CategoryFactory + { + return $this->state(function (array $attributes) { + return [ + 'status' => 0, + ]; + }); + } + + /** + * Handle rtl state + */ + public function rtl(): CategoryFactory + { + return $this->state(function (array $attributes) { + return [ + 'direction' => 'rtl', + ]; + }); + } + +} diff --git a/packages/Webkul/Category/src/Models/Category.php b/packages/Webkul/Category/src/Models/Category.php index 7091e0eac..107aa473c 100755 --- a/packages/Webkul/Category/src/Models/Category.php +++ b/packages/Webkul/Category/src/Models/Category.php @@ -5,6 +5,8 @@ namespace Webkul\Category\Models; use Webkul\Core\Eloquent\TranslatableModel; use Kalnoy\Nestedset\NodeTrait; use Illuminate\Support\Facades\Storage; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Category\Database\Factories\CategoryFactory; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Webkul\Category\Contracts\Category as CategoryContract; use Webkul\Attribute\Models\AttributeProxy; @@ -20,7 +22,9 @@ use Webkul\Product\Models\ProductProxy; */ class Category extends TranslatableModel implements CategoryContract { + use NodeTrait; + use HasFactory; public $translatedAttributes = [ 'name', @@ -47,8 +51,9 @@ class Category extends TranslatableModel implements CategoryContract */ public function image_url() { - if (! $this->image) + if (!$this->image) { return; + } return Storage::url($this->image); } @@ -61,14 +66,17 @@ class Category extends TranslatableModel implements CategoryContract return $this->image_url(); } - /** + /** * The filterable attributes that belong to the category. */ public function filterableAttributes() { - return $this->belongsToMany(AttributeProxy::modelClass(), 'category_filterable_attributes')->with(['options' => function($query) { - $query->orderBy('sort_order'); - }]); + return $this->belongsToMany(AttributeProxy::modelClass(), 'category_filterable_attributes') + ->with([ + 'options' => function ($query) { + $query->orderBy('sort_order'); + }, + ]); } /** @@ -79,10 +87,23 @@ class Category extends TranslatableModel implements CategoryContract public function getRootCategory(): Category { return Category::where([ - ['parent_id', '=', null], - ['_lft', '<=', $this->_lft], - ['_rgt', '>=', $this->_rgt], - ])->first(); + [ + 'parent_id', + '=', + null, + ], + [ + '_lft', + '<=', + $this->_lft, + ], + [ + '_rgt', + '>=', + $this->_rgt, + ], + ]) + ->first(); } /** @@ -97,7 +118,7 @@ class Category extends TranslatableModel implements CategoryContract $categories = [$category]; while (isset($category->parent)) { - $category = $category->parent; + $category = $category->parent; $categories[] = $category; } @@ -109,18 +130,19 @@ class Category extends TranslatableModel implements CategoryContract * will search in root category by default * is used to minimize the numbers of sql queries for it only uses the already cached tree * - * @param Category[] $categoryTree + * @param Category[] $categoryTree + * * @return Category */ public function findInTree($categoryTree = null): Category { - if (! $categoryTree) { + if (!$categoryTree) { $categoryTree = app(CategoryRepository::class)->getVisibleCategoryTree($this->getRootCategory()->id); } $category = $categoryTree->first(); - if (! $category) { + if (!$category) { throw new NotFoundHttpException('category not found in tree'); } @@ -138,4 +160,15 @@ class Category extends TranslatableModel implements CategoryContract { return $this->belongsToMany(ProductProxy::modelClass(), 'product_categories'); } + + /** + * Create a new factory instance for the model. + * + * @return CategoryFactory + */ + protected static function newFactory(): CategoryFactory + { + return CategoryFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Category/src/Providers/CategoryServiceProvider.php b/packages/Webkul/Category/src/Providers/CategoryServiceProvider.php index 0af0c484b..c4905e07c 100755 --- a/packages/Webkul/Category/src/Providers/CategoryServiceProvider.php +++ b/packages/Webkul/Category/src/Providers/CategoryServiceProvider.php @@ -2,35 +2,23 @@ namespace Webkul\Category\Providers; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; use Illuminate\Support\ServiceProvider; use Webkul\Category\Models\CategoryProxy; use Webkul\Category\Observers\CategoryObserver; class CategoryServiceProvider extends ServiceProvider { + /** * Bootstrap services. * * @return void */ - public function boot() + public function boot(): void { - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); CategoryProxy::observe(CategoryObserver::class); - - $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); } - /** - * Register factories. - * - * @param string $path - * @return void - */ - protected function registerEloquentFactoriesFrom($path): void - { - $this->app->make(EloquentFactory::class)->load($path); - } } \ No newline at end of file diff --git a/packages/Webkul/Checkout/src/Database/Factories/CartAddressFactory.php b/packages/Webkul/Checkout/src/Database/Factories/CartAddressFactory.php index c78b813e7..d83dcdf9a 100644 --- a/packages/Webkul/Checkout/src/Database/Factories/CartAddressFactory.php +++ b/packages/Webkul/Checkout/src/Database/Factories/CartAddressFactory.php @@ -1,15 +1,33 @@ define(CartAddress::class, function (Faker $faker) { - return [ - 'first_name' => $faker->firstName(), - 'last_name' => $faker->lastName, - 'email' => $faker->email, - 'address_type' => CartAddress::ADDRESS_TYPE_BILLING, - ]; -}); +class CartAddressFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = CartAddress::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'first_name' => $this->faker->firstName(), + 'last_name' => $this->faker->lastName, + 'email' => $this->faker->email, + 'address_type' => CartAddress::ADDRESS_TYPE_BILLING, + ]; + } + +} diff --git a/packages/Webkul/Checkout/src/Database/Factories/CartFactory.php b/packages/Webkul/Checkout/src/Database/Factories/CartFactory.php index 394c543c9..ee99e724f 100644 --- a/packages/Webkul/Checkout/src/Database/Factories/CartFactory.php +++ b/packages/Webkul/Checkout/src/Database/Factories/CartFactory.php @@ -1,41 +1,61 @@ define(Cart::class, function (Faker $faker) { - $now = date("Y-m-d H:i:s"); +class CartFactory extends Factory +{ - $lastOrder = DB::table('orders') - ->orderBy('id', 'desc') - ->select('id') - ->first(); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Cart::class; - $customer = factory(Customer::class)->create(); - $cartAddress = factory(CartAddress::class)->create(); + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $now = date("Y-m-d H:i:s"); + + $lastOrder = DB::table('orders') + ->orderBy('id', 'desc') + ->select('id') + ->first(); + + $customer = Customer::factory() + ->create(); + $cartAddress = CartAddress::factory() + ->create(); + + return [ + 'is_guest' => 0, + 'is_active' => 1, + 'customer_id' => $customer->id, + 'customer_email' => $customer->email, + 'customer_first_name' => $customer->first_name, + 'customer_last_name' => $customer->last_name, + 'is_gift' => 0, + 'base_currency_code' => 'EUR', + 'channel_currency_code' => 'EUR', + 'grand_total' => 0.0000, + 'base_grand_total' => 0.0000, + 'sub_total' => 0.0000, + 'base_sub_total' => 0.0000, + 'channel_id' => 1, + 'created_at' => $now, + 'updated_at' => $now, + ]; + } + +} - return [ - 'is_guest' => 0, - 'is_active' => 1, - 'customer_id' => $customer->id, - 'customer_email' => $customer->email, - 'customer_first_name' => $customer->first_name, - 'customer_last_name' => $customer->last_name, - 'is_gift' => 0, - 'base_currency_code' => 'EUR', - 'channel_currency_code' => 'EUR', - 'grand_total' => 0.0000, - 'base_grand_total' => 0.0000, - 'sub_total' => 0.0000, - 'base_sub_total' => 0.0000, - 'channel_id' => 1, - 'created_at' => $now, - 'updated_at' => $now, - ]; -}); diff --git a/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php b/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php index c43f66dbd..c20838dd2 100644 --- a/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php +++ b/packages/Webkul/Checkout/src/Database/Factories/CartItemFactory.php @@ -1,38 +1,57 @@ define(CartItem::class, function (Faker $faker, array $attributes) { - $now = date("Y-m-d H:i:s"); +class CartItemFactory extends Factory +{ - if (isset($attributes['product_id'])) { - $product = Product::where('id', $attributes['product_id'])->first(); - } else { - $product = factory(Product::class)->create(); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = CartItem::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $now = date("Y-m-d H:i:s"); + + if (isset($attributes['product_id'])) { + $product = Product::query() + ->where('id', $attributes['product_id']) + ->first(); + } else { + $product = Product::factory() + ->create(); + } + + $fallbackPrice = $this->faker->randomFloat(4, 0, 1000); + + return [ + 'quantity' => 1, + 'sku' => $product->sku, + 'type' => $product->type, + 'name' => $product->name, + 'price' => $product->price ?? $fallbackPrice, + 'base_price' => $product->price ?? $fallbackPrice, + 'total' => $product->price ?? $fallbackPrice, + 'base_total' => $product->price ?? $fallbackPrice, + 'product_id' => $product->id, + 'cart_id' => Cart::factory(), + 'created_at' => $now, + 'updated_at' => $now, + ]; } - $fallbackPrice = $faker->randomFloat(4, 0, 1000); - - return [ - 'quantity' => 1, - 'sku' => $product->sku, - 'type' => $product->type, - 'name' => $product->name, - 'price' => $product->price ?? $fallbackPrice, - 'base_price' => $product->price ?? $fallbackPrice, - 'total' => $product->price ?? $fallbackPrice, - 'base_total' => $product->price ?? $fallbackPrice, - 'product_id' => $product->id, - 'cart_id' => function () { - return factory(Cart::class)->create()->id; - }, - 'created_at' => $now, - 'updated_at' => $now, - ]; -}); - +} diff --git a/packages/Webkul/Checkout/src/Database/Factories/CartPaymentFactory.php b/packages/Webkul/Checkout/src/Database/Factories/CartPaymentFactory.php index 3d45f70b0..692c3202f 100644 --- a/packages/Webkul/Checkout/src/Database/Factories/CartPaymentFactory.php +++ b/packages/Webkul/Checkout/src/Database/Factories/CartPaymentFactory.php @@ -1,16 +1,36 @@ define(CartPayment::class, function (Faker $faker) { - $now = date("Y-m-d H:i:s"); +class CartPaymentFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = CartPayment::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $now = date("Y-m-d H:i:s"); + + return [ + 'created_at' => $now, + 'updated_at' => $now, + ]; + } + +} - return [ - 'created_at' => $now, - 'updated_at' => $now, - ]; -}); diff --git a/packages/Webkul/Checkout/src/Models/Cart.php b/packages/Webkul/Checkout/src/Models/Cart.php index 40bcc42ac..f5960f31f 100755 --- a/packages/Webkul/Checkout/src/Models/Cart.php +++ b/packages/Webkul/Checkout/src/Models/Cart.php @@ -4,9 +4,14 @@ namespace Webkul\Checkout\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Checkout\Contracts\Cart as CartContract; +use Webkul\Checkout\Database\Factories\CartFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Cart extends Model implements CartContract { + + use HasFactory; + protected $table = 'cart'; protected $guarded = [ @@ -23,31 +28,35 @@ class Cart extends Model implements CartContract /** * To get relevant associated items with the cart instance */ - public function items() { - return $this->hasMany(CartItemProxy::modelClass())->whereNull('parent_id'); + public function items(): \Illuminate\Database\Eloquent\Relations\HasMany + { + return $this->hasMany(CartItemProxy::modelClass()) + ->whereNull('parent_id'); } /** * To get all the associated items with the cart instance even the parent and child items of configurable products */ - public function all_items() { + public function all_items(): \Illuminate\Database\Eloquent\Relations\HasMany + { return $this->hasMany(CartItemProxy::modelClass()); } /** * Get the addresses for the cart. */ - public function addresses() + public function addresses(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(CartAddressProxy::modelClass()); } /** - * Get the biling address for the cart. + * Get the billing address for the cart. */ - public function billing_address() + public function billing_address(): \Illuminate\Database\Eloquent\Relations\HasMany { - return $this->addresses()->where('address_type', CartAddress::ADDRESS_TYPE_BILLING); + return $this->addresses() + ->where('address_type', CartAddress::ADDRESS_TYPE_BILLING); } /** @@ -55,15 +64,17 @@ class Cart extends Model implements CartContract */ public function getBillingAddressAttribute() { - return $this->billing_address()->first(); + return $this->billing_address() + ->first(); } /** * Get the shipping address for the cart. */ - public function shipping_address() + public function shipping_address(): \Illuminate\Database\Eloquent\Relations\HasMany { - return $this->addresses()->where('address_type', CartAddress::ADDRESS_TYPE_SHIPPING); + return $this->addresses() + ->where('address_type', CartAddress::ADDRESS_TYPE_SHIPPING); } /** @@ -71,37 +82,41 @@ class Cart extends Model implements CartContract */ public function getShippingAddressAttribute() { - return $this->shipping_address()->first(); + return $this->shipping_address() + ->first(); } /** * Get the shipping rates for the cart. */ - public function shipping_rates() + public function shipping_rates(): \Illuminate\Database\Eloquent\Relations\HasManyThrough { return $this->hasManyThrough(CartShippingRateProxy::modelClass(), CartAddressProxy::modelClass(), 'cart_id', 'cart_address_id'); } /** - * Get all of the attributes for the attribute groups. + * Get all the attributes for the attribute groups. */ - public function selected_shipping_rate() + public function selected_shipping_rate(): \Illuminate\Database\Eloquent\Relations\HasManyThrough { - return $this->shipping_rates()->where('method', $this->shipping_method); + return $this->shipping_rates() + ->where('method', $this->shipping_method); } /** - * Get all of the attributes for the attribute groups. + * Get all the attributes for the attribute groups. */ public function getSelectedShippingRateAttribute() { - return $this->selected_shipping_rate()->where('method', $this->shipping_method)->first(); + return $this->selected_shipping_rate() + ->where('method', $this->shipping_method) + ->first(); } /** * Get the payment associated with the cart. */ - public function payment() + public function payment(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(CartPaymentProxy::modelClass()); } @@ -111,7 +126,7 @@ class Cart extends Model implements CartContract * * @return boolean */ - public function haveStockableItems() + public function haveStockableItems(): bool { foreach ($this->items as $item) { if ($item->product->isStockable()) { @@ -127,10 +142,10 @@ class Cart extends Model implements CartContract * * @return boolean */ - public function hasDownloadableItems() + public function hasDownloadableItems(): bool { foreach ($this->items as $item) { - if (stristr($item->type,'downloadable') !== false) { + if (stristr($item->type, 'downloadable') !== false) { return true; } } @@ -141,12 +156,14 @@ class Cart extends Model implements CartContract /** * Returns true if cart contains one or many products with quantity box. * (for example: simple, configurable, virtual) + * * @return bool */ public function hasProductsWithQuantityBox(): bool { foreach ($this->items as $item) { - if ($item->product->getTypeInstance()->showQuantityBox() === true) { + if ($item->product->getTypeInstance() + ->showQuantityBox() === true) { return true; } } @@ -158,7 +175,7 @@ class Cart extends Model implements CartContract * * @return boolean */ - public function hasGuestCheckoutItems() + public function hasGuestCheckoutItems(): bool { foreach ($this->items as $item) { if ($item->product->getAttribute('guest_checkout') === 0) { @@ -176,10 +193,22 @@ class Cart extends Model implements CartContract */ public function checkMinimumOrder(): bool { - $minimumOrderAmount = (float) core()->getConfigData('sales.orderSettings.minimum-order.minimum_order_amount') ?? 0; + $minimumOrderAmount = (float) (core()->getConfigData('sales.orderSettings.minimum-order.minimum_order_amount') ?? 0); $cartBaseSubTotal = (float) $this->base_sub_total; return $cartBaseSubTotal >= $minimumOrderAmount; } + + + /** + * Create a new factory instance for the model + * + * @return CartFactory + */ + protected static function newFactory(): CartFactory + { + return CartFactory::new(); + } + } diff --git a/packages/Webkul/Checkout/src/Models/CartAddress.php b/packages/Webkul/Checkout/src/Models/CartAddress.php index 88cb4ec29..e6f8aac00 100755 --- a/packages/Webkul/Checkout/src/Models/CartAddress.php +++ b/packages/Webkul/Checkout/src/Models/CartAddress.php @@ -3,20 +3,29 @@ namespace Webkul\Checkout\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Checkout\Database\Factories\CartAddressFactory; use Webkul\Checkout\Contracts\CartAddress as CartAddressContract; use Webkul\Core\Models\Address; /** * Class CartAddress + * * @package Webkul\Checkout\Models * * @property integer $cart_id - * @property Cart $cart + * @property Cart $cart * */ class CartAddress extends Address implements CartAddressContract { + + use HasFactory; + public const ADDRESS_TYPE_SHIPPING = 'cart_shipping'; + public const ADDRESS_TYPE_BILLING = 'cart_billing'; /** @@ -31,12 +40,12 @@ class CartAddress extends Address implements CartAddressContract * * @return void */ - protected static function boot() + protected static function boot(): void { static::addGlobalScope('address_type', static function (Builder $builder) { $builder->whereIn('address_type', [ self::ADDRESS_TYPE_BILLING, - self::ADDRESS_TYPE_SHIPPING + self::ADDRESS_TYPE_SHIPPING, ]); }); @@ -46,7 +55,7 @@ class CartAddress extends Address implements CartAddressContract /** * Get the shipping rates for the cart address. */ - public function shipping_rates() + public function shipping_rates(): HasMany { return $this->hasMany(CartShippingRateProxy::modelClass()); } @@ -54,8 +63,19 @@ class CartAddress extends Address implements CartAddressContract /** * Get the cart record associated with the address. */ - public function cart() + public function cart(): BelongsTo { return $this->belongsTo(Cart::class); } + + /** + * Create a new factory instance for the model + * + * @return CartAddressFactory + */ + protected static function newFactory(): CartAddressFactory + { + return CartAddressFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Checkout/src/Models/CartItem.php b/packages/Webkul/Checkout/src/Models/CartItem.php index 561b85c64..6a092e6c3 100755 --- a/packages/Webkul/Checkout/src/Models/CartItem.php +++ b/packages/Webkul/Checkout/src/Models/CartItem.php @@ -5,11 +5,19 @@ namespace Webkul\Checkout\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Product\Models\ProductProxy; use Webkul\Product\Models\ProductFlatProxy; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Checkout\Database\Factories\CartItemFactory; use Webkul\Checkout\Contracts\CartItem as CartItemContract; class CartItem extends Model implements CartItemContract { + + use HasFactory; + protected $table = 'cart_items'; protected $casts = [ @@ -22,7 +30,7 @@ class CartItem extends Model implements CartItemContract 'updated_at', ]; - public function product() + public function product(): HasOne { return $this->hasOne(ProductProxy::modelClass(), 'id', 'product_id'); } @@ -32,22 +40,22 @@ class CartItem extends Model implements CartItemContract */ public function product_flat() { - return (ProductFlatProxy::modelClass()) - ::where('product_flat.product_id', $this->product_id) - ->where('product_flat.locale', app()->getLocale()) - ->where('product_flat.channel', core()->getCurrentChannelCode()) - ->select('product_flat.*'); + return (ProductFlatProxy::modelClass())::where('product_flat.product_id', $this->product_id) + ->where('product_flat.locale', app()->getLocale()) + ->where('product_flat.channel', core()->getCurrentChannelCode()) + ->select('product_flat.*'); } /** - * Get all of the attributes for the attribute groups. + * Get all the attributes for the attribute groups. */ public function getProductFlatAttribute() { - return $this->product_flat()->first(); + return $this->product_flat() + ->first(); } - public function cart() + public function cart(): HasOne { return $this->hasOne(CartProxy::modelClass(), 'id', 'cart_id'); } @@ -55,7 +63,7 @@ class CartItem extends Model implements CartItemContract /** * Get the child item. */ - public function child() + public function child(): BelongsTo { return $this->belongsTo(static::class, 'id', 'parent_id'); } @@ -63,7 +71,7 @@ class CartItem extends Model implements CartItemContract /** * Get the parent item record associated with the cart item. */ - public function parent() + public function parent(): BelongsTo { return $this->belongsTo(self::class, 'parent_id'); } @@ -71,8 +79,19 @@ class CartItem extends Model implements CartItemContract /** * Get the children items. */ - public function children() + public function children(): HasMany { return $this->hasMany(self::class, 'parent_id'); } + + /** + * Create a new factory instance for the model + * + * @return CartItemFactory + */ + protected static function newFactory(): CartItemFactory + { + return CartItemFactory::new(); + } + } diff --git a/packages/Webkul/Checkout/src/Models/CartPayment.php b/packages/Webkul/Checkout/src/Models/CartPayment.php index 76eec136f..68d2a10b7 100755 --- a/packages/Webkul/Checkout/src/Models/CartPayment.php +++ b/packages/Webkul/Checkout/src/Models/CartPayment.php @@ -3,9 +3,25 @@ namespace Webkul\Checkout\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Checkout\Database\Factories\CartPaymentFactory; use Webkul\Checkout\Contracts\CartPayment as CartPaymentContract; class CartPayment extends Model implements CartPaymentContract { + + use HasFactory; + protected $table = 'cart_payment'; + + /** + * Create a new factory instance for the model + * + * @return CartPaymentFactory + */ + protected static function newFactory(): CartPaymentFactory + { + return CartPaymentFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Checkout/src/Providers/CheckoutServiceProvider.php b/packages/Webkul/Checkout/src/Providers/CheckoutServiceProvider.php index b0714c511..dd4326c0b 100755 --- a/packages/Webkul/Checkout/src/Providers/CheckoutServiceProvider.php +++ b/packages/Webkul/Checkout/src/Providers/CheckoutServiceProvider.php @@ -5,21 +5,19 @@ namespace Webkul\Checkout\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Foundation\AliasLoader; use Webkul\Checkout\Facades\Cart; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; class CheckoutServiceProvider extends ServiceProvider { - public function boot() - { - include __DIR__ . '/../Http/helpers.php'; - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + public function boot(): void + { + include __DIR__.'/../Http/helpers.php'; + + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); $this->app->register(ModuleServiceProvider::class); $this->app->register(EventServiceProvider::class); - - $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); } /** @@ -27,7 +25,7 @@ class CheckoutServiceProvider extends ServiceProvider * * @return void */ - public function register() + public function register(): void { $this->registerFacades(); } @@ -37,7 +35,7 @@ class CheckoutServiceProvider extends ServiceProvider * * @return void */ - protected function registerFacades() + protected function registerFacades(): void { //to make the cart facade and bind the //alias to the class needed to be called. @@ -52,15 +50,4 @@ class CheckoutServiceProvider extends ServiceProvider $this->app->bind('cart', 'Webkul\Checkout\Cart'); } - /** - * Register factories. - * - * @param string $path - * - * @return void - */ - protected function registerEloquentFactoriesFrom($path): void - { - $this->app->make(EloquentFactory::class)->load($path); - } } \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Factories/CartRuleCouponFactory.php b/packages/Webkul/Core/src/Database/Factories/CartRuleCouponFactory.php index f51b437cc..8d0fdbfe1 100644 --- a/packages/Webkul/Core/src/Database/Factories/CartRuleCouponFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/CartRuleCouponFactory.php @@ -1,20 +1,37 @@ define(CartRuleCoupon::class, function (Faker $faker) { - return [ - 'code' => $faker->uuid(), - 'usage_limit' => 100, - 'usage_per_customer' => 100, - 'type' => 0, - 'is_primary' => 1, - 'cart_rule_id' => static function () { - return factory(CartRule::class)->create()->id; - }, - ]; -}); +class CartRuleCouponFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = CartRuleCoupon::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'code' => Str::uuid(), + 'usage_limit' => 100, + 'usage_per_customer' => 100, + 'type' => 0, + 'is_primary' => 1, + 'cart_rule_id' => CartRule::factory(), + ]; + } + +} diff --git a/packages/Webkul/Core/src/Database/Factories/CartRuleFactory.php b/packages/Webkul/Core/src/Database/Factories/CartRuleFactory.php index 330c3caa6..60e53339b 100644 --- a/packages/Webkul/Core/src/Database/Factories/CartRuleFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/CartRuleFactory.php @@ -1,30 +1,49 @@ define(CartRule::class, function (Faker $faker) { - return [ - 'name' => $faker->uuid, - 'description' => $faker->sentence(), - 'starts_from' => null, - 'ends_till' => null, - 'coupon_type' => '1', - 'use_auto_generation' => '0', - 'usage_per_customer' => '100', - 'uses_per_coupon' => '100', - 'times_used' => '0', - 'condition_type' => '2', - 'end_other_rules' => '0', - 'uses_attribute_conditions' => '0', - 'discount_quantity' => '0', - 'discount_step' => '0', - 'apply_to_shipping' => '0', - 'free_shipping' => '0', - 'sort_order' => '0', - 'status' => '1', - 'conditions' => null, - ]; -}); \ No newline at end of file +class CartRuleFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = CartRule::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'name' => Str::uuid(), + 'description' => $this->faker->sentence(), + 'starts_from' => null, + 'ends_till' => null, + 'coupon_type' => '1', + 'use_auto_generation' => '0', + 'usage_per_customer' => '100', + 'uses_per_coupon' => '100', + 'times_used' => '0', + 'condition_type' => '2', + 'end_other_rules' => '0', + 'uses_attribute_conditions' => '0', + 'discount_quantity' => '0', + 'discount_step' => '0', + 'apply_to_shipping' => '0', + 'free_shipping' => '0', + 'sort_order' => '0', + 'status' => '1', + 'conditions' => null, + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Factories/ChannelFactory.php b/packages/Webkul/Core/src/Database/Factories/ChannelFactory.php index 80f04d976..e71bd75e2 100644 --- a/packages/Webkul/Core/src/Database/Factories/ChannelFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/ChannelFactory.php @@ -1,39 +1,50 @@ define(Channel::class, function (Faker $faker, array $attributes) { +class ChannelFactory extends Factory +{ - $seoTitle = $attributes['seo_title'] ?? $faker->word; - $seoDescription = $attributes['seo_description'] ?? $faker->words(10, true); - $seoKeywords = $attributes['seo_keywords'] ?? $faker->words(3, true); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Channel::class; - $seoData = [ - 'meta_title' => $seoTitle, - 'meta_description' => $seoDescription, - 'meta_keywords' => $seoKeywords, - ]; + /** + * Define the model's default state. + * + * @return array + * @throws \JsonException + */ + public function definition(): array + { + $seoTitle = $this->faker->word; + $seoDescription = $this->faker->words(10, true); + $seoKeywords = $this->faker->words(3, true); - unset($attributes['seo_title'], $attributes['seo_description'], $attributes['seo_keywords']); + $seoData = [ + 'meta_title' => $seoTitle, + 'meta_description' => $seoDescription, + 'meta_keywords' => $seoKeywords, + ]; + return [ + 'code' => $this->faker->unique()->word, + 'name' => $this->faker->word, + 'default_locale_id' => Locale::factory(), + 'base_currency_id' => Currency::factory(), + 'root_category_id' => Category::factory(), + 'home_seo' => json_encode($seoData, JSON_THROW_ON_ERROR), + ]; + } - return [ - 'code' => $faker->unique()->word, - 'name' => $faker->word, - 'default_locale_id' => function () { - return factory(Locale::class)->create()->id; - }, - 'base_currency_id' => function () { - return factory(Currency::class)->create()->id; - }, - 'root_category_id' => function () { - return factory(Category::class)->create()->id; - }, - 'home_seo' => json_encode($seoData), - ]; -}); +} \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Factories/ChannelInventorySourceFactory.php b/packages/Webkul/Core/src/Database/Factories/ChannelInventorySourceFactory.php index 4b8c272c5..193a5b6f3 100644 --- a/packages/Webkul/Core/src/Database/Factories/ChannelInventorySourceFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/ChannelInventorySourceFactory.php @@ -1,10 +1,30 @@ define('channel_inventory_sources', function () { - return [ - 'channel_id' => core()->getCurrentChannel()->id, - 'inventory_source_id' => 1, - ]; -}); \ No newline at end of file +use Illuminate\Database\Eloquent\Factories\Factory; + +class ChannelInventorySourceFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = 'channel_inventory_sources'; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'channel_id' => core()->getCurrentChannel()->id, + 'inventory_source_id' => 1, + ]; + } + +} diff --git a/packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php b/packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php index 2b551eae9..c5faa1b83 100644 --- a/packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/CurrencyFactory.php @@ -1,14 +1,32 @@ define(Currency::class, function (Faker $faker, array $attributes) { +class CurrencyFactory extends Factory +{ - return [ - 'code' => $faker->unique()->currencyCode, - 'name' => $faker->word, - ]; + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Currency::class; -}); \ No newline at end of file + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'code' => $this->faker->unique()->currencyCode, + 'name' => $this->faker->word, + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php b/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php index 69628675e..7f740198f 100644 --- a/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/LocaleFactory.php @@ -1,22 +1,54 @@ define(Locale::class, function (Faker $faker, array $attributes) { +class LocaleFactory extends Factory +{ - do { - $languageCode = $faker->languageCode; - } while (Locale::query()->where('code', $languageCode)->exists()); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Locale::class; - return [ - 'code' => $languageCode, - 'name' => $faker->country, - 'direction' => 'ltr', + /** + * @var string[] + */ + protected $states = [ + 'rtl', ]; -}); -$factory->state(Category::class, 'rtl', [ - 'direction' => 'rtl', -]); \ No newline at end of file + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + do { + $languageCode = $this->faker->languageCode; + } while (Locale::query() + ->where('code', $languageCode) + ->exists()); + + return [ + 'code' => $languageCode, + 'name' => $this->faker->country, + 'direction' => 'ltr', + ]; + } + + public function rtl(): LocaleFactory + { + return $this->state(function (array $attributes) { + return [ + 'direction' => 'rtl', + ]; + }); + } + +} \ No newline at end of file diff --git a/packages/Webkul/Core/src/Database/Factories/SubscriberListFactory.php b/packages/Webkul/Core/src/Database/Factories/SubscriberListFactory.php index 947e8cf2e..80f779824 100644 --- a/packages/Webkul/Core/src/Database/Factories/SubscriberListFactory.php +++ b/packages/Webkul/Core/src/Database/Factories/SubscriberListFactory.php @@ -1,14 +1,33 @@ $this->faker->safeEmail, + 'is_subscribed' => $this->faker->boolean, + 'channel_id' => 1, + ]; + } + +} -$factory->define(SubscribersList::class, function (Faker $faker) { - return [ - 'email' => $faker->safeEmail, - 'is_subscribed' => $faker->boolean, - 'channel_id' => 1, - ]; -}); diff --git a/packages/Webkul/Core/src/Models/Address.php b/packages/Webkul/Core/src/Models/Address.php index f66279332..d47a5ce85 100644 --- a/packages/Webkul/Core/src/Models/Address.php +++ b/packages/Webkul/Core/src/Models/Address.php @@ -12,34 +12,36 @@ use Webkul\Core\Contracts\Address as AddressContract; /** * Class Address + * * @package Webkul\Core\Models * - * @property string $address_type - * @property integer $customer_id - * @property Customer $customer - * @property string $first_name - * @property string $last_name - * @property string $gender - * @property string $company_name - * @property string $address1 - * @property string $address2 - * @property string $postcode - * @property string $city - * @property string $state - * @property string $country - * @property string $email - * @property string $phone - * @property boolean $default_address - * @property array $additional + * @property string $address_type + * @property integer $customer_id + * @property Customer $customer + * @property string $first_name + * @property string $last_name + * @property string $gender + * @property string $company_name + * @property string $address1 + * @property string $address2 + * @property string $postcode + * @property string $city + * @property string $state + * @property string $country + * @property string $email + * @property string $phone + * @property boolean $default_address + * @property array $additional * * @property-read integer $id - * @property-read string $name - * @property-read Carbon $created_at - * @property-read Carbon $updated_at + * @property-read string $name + * @property-read Carbon $created_at + * @property-read Carbon $updated_at * */ abstract class Address extends Model implements AddressContract { + protected $table = 'addresses'; protected $guarded = [ @@ -71,16 +73,16 @@ abstract class Address extends Model implements AddressContract ]; protected $casts = [ - 'additional' => 'array', + 'additional' => 'array', 'default_address' => 'boolean', ]; /** - * Get all of the attributes for the attribute groups. + * Get all the attributes for the attribute groups. */ public function getNameAttribute(): string { - return $this->first_name . ' ' . $this->last_name; + return $this->first_name.' '.$this->last_name; } /** @@ -90,4 +92,5 @@ abstract class Address extends Model implements AddressContract { return $this->belongsTo(Customer::class); } + } diff --git a/packages/Webkul/Core/src/Models/Channel.php b/packages/Webkul/Core/src/Models/Channel.php index 45741f123..7e7c08564 100755 --- a/packages/Webkul/Core/src/Models/Channel.php +++ b/packages/Webkul/Core/src/Models/Channel.php @@ -6,10 +6,15 @@ use Illuminate\Support\Facades\Storage; use Webkul\Category\Models\CategoryProxy; use Webkul\Core\Eloquent\TranslatableModel; use Webkul\Inventory\Models\InventorySourceProxy; +use Webkul\Core\Database\Factories\ChannelFactory; use Webkul\Core\Contracts\Channel as ChannelContract; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Channel extends TranslatableModel implements ChannelContract { + + use HasFactory; + protected $fillable = [ 'code', 'name', @@ -24,7 +29,7 @@ class Channel extends TranslatableModel implements ChannelContract 'home_seo', 'is_maintenance_on', 'maintenance_mode_text', - 'allowed_ips' + 'allowed_ips', ]; public $translatedAttributes = [ @@ -39,7 +44,7 @@ class Channel extends TranslatableModel implements ChannelContract /** * Get the channel locales. */ - public function locales() + public function locales(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(LocaleProxy::modelClass(), 'channel_locales'); } @@ -47,7 +52,7 @@ class Channel extends TranslatableModel implements ChannelContract /** * Get the default locale */ - public function default_locale() + public function default_locale(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(LocaleProxy::modelClass()); } @@ -55,7 +60,7 @@ class Channel extends TranslatableModel implements ChannelContract /** * Get the channel locales. */ - public function currencies() + public function currencies(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(CurrencyProxy::modelClass(), 'channel_currencies'); } @@ -63,7 +68,7 @@ class Channel extends TranslatableModel implements ChannelContract /** * Get the channel inventory sources. */ - public function inventory_sources() + public function inventory_sources(): \Illuminate\Database\Eloquent\Relations\BelongsToMany { return $this->belongsToMany(InventorySourceProxy::modelClass(), 'channel_inventory_sources'); } @@ -71,7 +76,7 @@ class Channel extends TranslatableModel implements ChannelContract /** * Get the base currency */ - public function base_currency() + public function base_currency(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(CurrencyProxy::modelClass()); } @@ -79,7 +84,7 @@ class Channel extends TranslatableModel implements ChannelContract /** * Get the base currency */ - public function root_category() + public function root_category(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(CategoryProxy::modelClass(), 'root_category_id'); } @@ -89,7 +94,7 @@ class Channel extends TranslatableModel implements ChannelContract */ public function logo_url() { - if (! $this->logo) { + if (!$this->logo) { return; } @@ -109,7 +114,7 @@ class Channel extends TranslatableModel implements ChannelContract */ public function favicon_url() { - if (! $this->favicon) { + if (!$this->favicon) { return; } @@ -123,4 +128,15 @@ class Channel extends TranslatableModel implements ChannelContract { return $this->favicon_url(); } + + /** + * Create a new factory instance for the model + * + * @return ChannelFactory + */ + protected static function newFactory(): ChannelFactory + { + return ChannelFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Core/src/Models/Currency.php b/packages/Webkul/Core/src/Models/Currency.php index 7fc11b6d7..16f9fe929 100755 --- a/packages/Webkul/Core/src/Models/Currency.php +++ b/packages/Webkul/Core/src/Models/Currency.php @@ -3,10 +3,16 @@ namespace Webkul\Core\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Webkul\Core\Database\Factories\CurrencyFactory; use Webkul\Core\Contracts\Currency as CurrencyContract; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Currency extends Model implements CurrencyContract { + + use HasFactory; + /** * The attributes that are mass assignable. * @@ -21,10 +27,11 @@ class Currency extends Model implements CurrencyContract /** * Set currency code in capital * - * @param string $value + * @param $code + * * @return void */ - public function setCodeAttribute($code) + public function setCodeAttribute($code): void { $this->attributes['code'] = strtoupper($code); } @@ -32,8 +39,19 @@ class Currency extends Model implements CurrencyContract /** * Get the exchange rate associated with the currency. */ - public function exchange_rate() + public function exchange_rate(): HasOne { return $this->hasOne(CurrencyExchangeRateProxy::modelClass(), 'target_currency'); } + + /** + * Create a new factory instance for the model. + * + * @return CurrencyFactory + */ + protected static function newFactory(): CurrencyFactory + { + return CurrencyFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Core/src/Models/Locale.php b/packages/Webkul/Core/src/Models/Locale.php index 79e6a879d..391e4cced 100755 --- a/packages/Webkul/Core/src/Models/Locale.php +++ b/packages/Webkul/Core/src/Models/Locale.php @@ -3,10 +3,15 @@ namespace Webkul\Core\Models; use Illuminate\Database\Eloquent\Model; +use Webkul\Core\Database\Factories\LocaleFactory; use Webkul\Core\Contracts\Locale as LocaleContract; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Locale extends Model implements LocaleContract { + + use HasFactory; + /** * The attributes that are mass assignable. * @@ -17,4 +22,15 @@ class Locale extends Model implements LocaleContract 'name', 'direction', ]; + + /** + * Create a new factory instance for the model. + * + * @return LocaleFactory + */ + protected static function newFactory(): LocaleFactory + { + return LocaleFactory::new(); + } + } diff --git a/packages/Webkul/Core/src/Models/SubscribersList.php b/packages/Webkul/Core/src/Models/SubscribersList.php index d016047f0..21c8c3006 100755 --- a/packages/Webkul/Core/src/Models/SubscribersList.php +++ b/packages/Webkul/Core/src/Models/SubscribersList.php @@ -4,10 +4,16 @@ namespace Webkul\Core\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Customer\Models\CustomerProxy; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Core\Database\Factories\SubscriberListFactory; use Webkul\Core\Contracts\SubscribersList as SubscribersListContract; class SubscribersList extends Model implements SubscribersListContract { + + use HasFactory; + /** * The attributes that are mass assignable. * @@ -29,8 +35,19 @@ class SubscribersList extends Model implements SubscribersListContract /** * Get the customer associated with the subscription. */ - public function customer() + public function customer(): BelongsTo { return $this->belongsTo(CustomerProxy::modelClass()); } + + /** + * Create a new factory instance for the model. + * + * @return SubscriberListFactory + */ + protected static function newFactory(): SubscriberListFactory + { + return SubscriberListFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Core/src/Providers/CoreServiceProvider.php b/packages/Webkul/Core/src/Providers/CoreServiceProvider.php index d47c9bf53..53307552f 100755 --- a/packages/Webkul/Core/src/Providers/CoreServiceProvider.php +++ b/packages/Webkul/Core/src/Providers/CoreServiceProvider.php @@ -3,7 +3,6 @@ namespace Webkul\Core\Providers; use Illuminate\Contracts\Debug\ExceptionHandler; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; use Illuminate\Foundation\AliasLoader; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Validator; @@ -18,21 +17,19 @@ use Webkul\Theme\ViewRenderEventManager; class CoreServiceProvider extends ServiceProvider { + /** * Bootstrap services. * * @return void - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ - public function boot() + public function boot(): void { - include __DIR__ . '/../Http/helpers.php'; + include __DIR__.'/../Http/helpers.php'; - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); - $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); - - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'core'); + $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'core'); Validator::extend('slug', 'Webkul\Core\Contracts\Validations\Slug@passes'); @@ -41,18 +38,15 @@ class CoreServiceProvider extends ServiceProvider Validator::extend('decimal', 'Webkul\Core\Contracts\Validations\Decimal@passes'); $this->publishes([ - dirname(__DIR__) . '/Config/concord.php' => config_path('concord.php'), - dirname(__DIR__) . '/Config/scout.php' => config_path('scout.php'), + dirname(__DIR__).'/Config/concord.php' => config_path('concord.php'), + dirname(__DIR__).'/Config/scout.php' => config_path('scout.php'), ]); - $this->app->bind( - ExceptionHandler::class, - Handler::class - ); + $this->app->bind(ExceptionHandler::class, Handler::class); SliderProxy::observe(SliderObserver::class); - $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'core'); + $this->loadViewsFrom(__DIR__.'/../Resources/views', 'core'); Event::listen('bagisto.shop.layout.body.after', static function (ViewRenderEventManager $viewRenderEventManager) { $viewRenderEventManager->addTemplate('core::blade.tracer.style'); @@ -76,7 +70,7 @@ class CoreServiceProvider extends ServiceProvider * * @return void */ - public function register() + public function register(): void { $this->registerFacades(); @@ -90,7 +84,7 @@ class CoreServiceProvider extends ServiceProvider * * @return void */ - protected function registerFacades() + protected function registerFacades(): void { $loader = AliasLoader::getInstance(); $loader->alias('core', CoreFacade::class); @@ -122,27 +116,16 @@ class CoreServiceProvider extends ServiceProvider ]); } - /** - * Register factories. - * - * @param string $path - * @return void - * @throws \Illuminate\Contracts\Container\BindingResolutionException - */ - protected function registerEloquentFactoriesFrom($path): void - { - $this->app->make(EloquentFactory::class)->load($path); - } - /** * Register the Blade compiler implementation. * * @return void */ - public function registerBladeCompiler() + public function registerBladeCompiler(): void { $this->app->singleton('blade.compiler', function ($app) { return new BladeCompiler($app['files'], $app['config']['view.compiled']); }); } + } diff --git a/packages/Webkul/Customer/src/Database/Factories/CustomerAddressFactory.php b/packages/Webkul/Customer/src/Database/Factories/CustomerAddressFactory.php index 1aae97cd8..d148c5cd2 100644 --- a/packages/Webkul/Customer/src/Database/Factories/CustomerAddressFactory.php +++ b/packages/Webkul/Customer/src/Database/Factories/CustomerAddressFactory.php @@ -1,34 +1,54 @@ define(CustomerAddress::class, function (Faker $faker) { - // use an locale from a country in europe so the vat id can be generated - $fakerIt = \Faker\Factory::create('it_IT'); +class CustomerAddressFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = CustomerAddress::class; + + /** + * Define the model's default state. + * + * @return array + * @throws \Exception + */ + public function definition(): array + { + $fakerIt = \Faker\Factory::create('it_IT'); + + return [ + 'customer_id' => Customer::factory(), + 'company_name' => $this->faker->company, + 'vat_id' => $fakerIt->vatId(), + 'first_name' => $this->faker->firstName, + 'last_name' => $this->faker->lastName, + 'address1' => $this->faker->streetAddress, + 'country' => $this->faker->countryCode, + 'state' => $this->faker->state, + 'city' => $this->faker->city, + 'postcode' => $this->faker->postcode, + 'phone' => $this->faker->e164PhoneNumber, + 'default_address' => Arr::random([ + 0, + 1, + ]), + 'address_type' => CustomerAddress::ADDRESS_TYPE, + ]; + } + +} - return [ - 'customer_id' => function () { - return factory(Customer::class)->create()->id; - }, - 'company_name' => $faker->company, - 'vat_id' => $fakerIt->vatId(), - 'first_name' => $faker->firstName, - 'last_name' => $faker->lastName, - 'address1' => $faker->streetAddress, - 'country' => $faker->countryCode, - 'state' => $faker->state, - 'city' => $faker->city, - 'postcode' => $faker->postcode, - 'phone' => $faker->e164PhoneNumber, - 'default_address' => Arr::random([0, 1]), - 'address_type' => CustomerAddress::ADDRESS_TYPE, - ]; -}); diff --git a/packages/Webkul/Customer/src/Database/Factories/CustomerFactory.php b/packages/Webkul/Customer/src/Database/Factories/CustomerFactory.php index 28789f7ef..4e9689e90 100644 --- a/packages/Webkul/Customer/src/Database/Factories/CustomerFactory.php +++ b/packages/Webkul/Customer/src/Database/Factories/CustomerFactory.php @@ -1,35 +1,76 @@ define(Customer::class, function (Faker $faker) { - $now = date("Y-m-d H:i:s"); - $password = $faker->password; - - return [ - 'first_name' => $faker->firstName(), - 'last_name' => $faker->lastName, - 'gender' => Arr::random(['male', 'female', 'other']), - 'email' => $faker->email, - 'status' => 1, - 'password' => Hash::make($password), - 'customer_group_id' => 2, - 'is_verified' => 1, - 'created_at' => $now, - 'updated_at' => $now, - 'notes' => json_encode(['plain_password' => $password]), +class CustomerFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Customer::class; + + /** + * @var array + */ + protected $states = [ + 'male', + 'female', ]; -}); -$factory->state(Customer::class, 'male', [ - 'gender' => 'Male', -]); + /** + * Define the model's default state. + * + * @return array + * @throws \Exception + */ + public function definition(): array + { + $now = date("Y-m-d H:i:s"); + $password = $this->faker->password; -$factory->state(Customer::class, 'female', [ - 'gender' => 'Female', -]); + return [ + 'first_name' => $this->faker->firstName(), + 'last_name' => $this->faker->lastName, + 'gender' => Arr::random([ + 'male', + 'female', + 'other', + ]), + 'email' => $this->faker->email, + 'status' => 1, + 'password' => Hash::make($password), + 'customer_group_id' => 2, + 'is_verified' => 1, + 'created_at' => $now, + 'updated_at' => $now, + 'notes' => json_encode(['plain_password' => $password], JSON_THROW_ON_ERROR), + ]; + } + + public function male(): CustomerFactory + { + return $this->state(function (array $attributes) { + return [ + 'gender' => 'Male', + ]; + }); + } + + public function female(): CustomerFactory + { + return $this->state(function (array $attributes) { + return [ + 'gender' => 'Female', + ]; + }); + } + +} diff --git a/packages/Webkul/Customer/src/Database/Factories/CustomerGroupFactory.php b/packages/Webkul/Customer/src/Database/Factories/CustomerGroupFactory.php index ccc77cb33..012ae252b 100644 --- a/packages/Webkul/Customer/src/Database/Factories/CustomerGroupFactory.php +++ b/packages/Webkul/Customer/src/Database/Factories/CustomerGroupFactory.php @@ -1,16 +1,35 @@ define(CustomerGroup::class, function (Faker $faker) { - $name = ucfirst($faker->word); - - return [ - 'name' => $name, - 'is_user_defined' => $faker->boolean, - 'code' => lcfirst($name), - ]; -}); +class CustomerGroupFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = CustomerGroup::class; + + /** + * Define the model's default state. + * + * @return array + * @throws \Exception + */ + public function definition(): array + { + $name = ucfirst($this->faker->word); + + return [ + 'name' => $name, + 'is_user_defined' => $this->faker->boolean, + 'code' => lcfirst($name), + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Customer/src/Models/Customer.php b/packages/Webkul/Customer/src/Models/Customer.php index 12a3a3283..efde4d197 100755 --- a/packages/Webkul/Customer/src/Models/Customer.php +++ b/packages/Webkul/Customer/src/Models/Customer.php @@ -3,18 +3,26 @@ namespace Webkul\Customer\Models; use Illuminate\Notifications\Notifiable; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Foundation\Auth\User as Authenticatable; use Tymon\JWTAuth\Contracts\JWTSubject; use Webkul\Checkout\Models\CartProxy; use Webkul\Sales\Models\OrderProxy; use Webkul\Core\Models\SubscribersListProxy; use Webkul\Product\Models\ProductReviewProxy; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Customer\Database\Factories\CustomerFactory; use Webkul\Customer\Notifications\CustomerResetPassword; use Webkul\Customer\Contracts\Customer as CustomerContract; +use Webkul\Customer\Database\Factories\CustomerAddressFactory; class Customer extends Authenticatable implements CustomerContract, JWTSubject { + use Notifiable; + use HasFactory; protected $table = 'customers'; @@ -35,45 +43,50 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject 'status', ]; - protected $hidden = ['password', 'api_token', 'remember_token']; + protected $hidden = [ + 'password', + 'api_token', + 'remember_token', + ]; /** * Get the customer full name. */ - public function getNameAttribute() + public function getNameAttribute(): string { - return ucfirst($this->first_name) . ' ' . ucfirst($this->last_name); + return ucfirst($this->first_name).' '.ucfirst($this->last_name); } /** * Email exists or not */ - public function emailExists($email) + public function emailExists($email): bool { - $results = $this->where('email', $email); + $results = $this->where('email', $email); - if ($results->count() == 0) { + if ($results->count() === 0) { return false; - } else { - return true; } + + return true; } /** * Get the customer group that owns the customer. */ - public function group() + public function group(): BelongsTo { return $this->belongsTo(CustomerGroupProxy::modelClass(), 'customer_group_id'); } /** - * Send the password reset notification. - * - * @param string $token - * @return void - */ - public function sendPasswordResetNotification($token) + * Send the password reset notification. + * + * @param string $token + * + * @return void + */ + public function sendPasswordResetNotification($token): void { $this->notify(new CustomerResetPassword($token)); } @@ -81,7 +94,7 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject /** * Get the customer address that owns the customer. */ - public function addresses() + public function addresses(): HasMany { return $this->hasMany(CustomerAddressProxy::modelClass(), 'customer_id'); } @@ -89,15 +102,16 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject /** * Get default customer address that owns the customer. */ - public function default_address() + public function default_address(): HasOne { - return $this->hasOne(CustomerAddressProxy::modelClass(), 'customer_id')->where('default_address', 1); + return $this->hasOne(CustomerAddressProxy::modelClass(), 'customer_id') + ->where('default_address', 1); } /** * Customer's relation with wishlist items */ - public function wishlist_items() + public function wishlist_items(): HasMany { return $this->hasMany(WishlistProxy::modelClass(), 'customer_id'); } @@ -105,31 +119,33 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject /** * get all cart inactive cart instance of a customer */ - public function all_carts() + public function all_carts(): HasMany { return $this->hasMany(CartProxy::modelClass(), 'customer_id'); } /** - * get inactive cart inactive cart instance of a customer + * get inactive cart instance of a customer */ - public function inactive_carts() + public function inactive_carts(): HasMany { - return $this->hasMany(CartProxy::modelClass(), 'customer_id')->where('is_active', 0); + return $this->hasMany(CartProxy::modelClass(), 'customer_id') + ->where('is_active', 0); } /** * get active cart inactive cart instance of a customer */ - public function active_carts() + public function active_carts(): HasMany { - return $this->hasMany(CartProxy::modelClass(), 'customer_id')->where('is_active', 1); + return $this->hasMany(CartProxy::modelClass(), 'customer_id') + ->where('is_active', 1); } /** * get all reviews of a customer - */ - public function all_reviews() + */ + public function all_reviews(): HasMany { return $this->hasMany(ProductReviewProxy::modelClass(), 'customer_id'); } @@ -137,7 +153,7 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject /** * get all orders of a customer */ - public function all_orders() + public function all_orders(): HasMany { return $this->hasMany(OrderProxy::modelClass(), 'customer_id'); } @@ -157,7 +173,7 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject * * @return array */ - public function getJWTCustomClaims() + public function getJWTCustomClaims(): array { return []; } @@ -165,8 +181,19 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject /** * Get the customer's subscription. */ - public function subscription() + public function subscription(): HasOne { return $this->hasOne(SubscribersListProxy::modelClass(), 'customer_id'); } + + /** + * Create a new factory instance for the model + * + * @return CustomerFactory + */ + protected static function newFactory(): CustomerFactory + { + return CustomerFactory::new(); + } + } diff --git a/packages/Webkul/Customer/src/Models/CustomerAddress.php b/packages/Webkul/Customer/src/Models/CustomerAddress.php index 89cecb4cb..19857cd6b 100755 --- a/packages/Webkul/Customer/src/Models/CustomerAddress.php +++ b/packages/Webkul/Customer/src/Models/CustomerAddress.php @@ -4,10 +4,15 @@ namespace Webkul\Customer\Models; use Illuminate\Database\Eloquent\Builder; use Webkul\Core\Models\Address; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Customer\Database\Factories\CustomerAddressFactory; use Webkul\Customer\Contracts\CustomerAddress as CustomerAddressContract; class CustomerAddress extends Address implements CustomerAddressContract { + + use HasFactory; + public const ADDRESS_TYPE = 'customer'; /** @@ -22,7 +27,7 @@ class CustomerAddress extends Address implements CustomerAddressContract * * @return void */ - protected static function boot() + protected static function boot(): void { static::addGlobalScope('address_type', static function (Builder $builder) { $builder->where('address_type', self::ADDRESS_TYPE); @@ -30,4 +35,15 @@ class CustomerAddress extends Address implements CustomerAddressContract parent::boot(); } + + /** + * Create a new factory instance for the model + * + * @return CustomerAddressFactory + */ + protected static function newFactory(): CustomerAddressFactory + { + return CustomerAddressFactory::new(); + } + } diff --git a/packages/Webkul/Customer/src/Models/CustomerGroup.php b/packages/Webkul/Customer/src/Models/CustomerGroup.php index e8a546d53..8ce906b11 100755 --- a/packages/Webkul/Customer/src/Models/CustomerGroup.php +++ b/packages/Webkul/Customer/src/Models/CustomerGroup.php @@ -1,20 +1,41 @@ hasMany(CustomerProxy::modelClass()); } + + /** + * Create a new factory instance for the model + * + * @return CustomerGroupFactory + */ + protected static function newFactory(): CustomerGroupFactory + { + return CustomerGroupFactory::new(); + } + } diff --git a/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php b/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php index a482df188..b95331374 100755 --- a/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php +++ b/packages/Webkul/Customer/src/Providers/CustomerServiceProvider.php @@ -2,14 +2,15 @@ namespace Webkul\Customer\Providers; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider; use Webkul\Customer\Captcha; +use Illuminate\Database\Eloquent\Factories\Factory; use Webkul\Customer\Http\Middleware\RedirectIfNotCustomer; class CustomerServiceProvider extends ServiceProvider { + /** * Bootstrap application services. * @@ -19,11 +20,11 @@ class CustomerServiceProvider extends ServiceProvider { $router->aliasMiddleware('customer', RedirectIfNotCustomer::class); - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); - $this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'customer'); + $this->loadTranslationsFrom(__DIR__.'/../Resources/lang', 'customer'); - $this->loadViewsFrom(__DIR__ . '/../Resources/views', 'customer'); + $this->loadViewsFrom(__DIR__.'/../Resources/views', 'customer'); $this->app['validator']->extend('captcha', function ($attribute, $value, $parameters) { return $this->app['captcha']->validateResponse($value); @@ -34,41 +35,24 @@ class CustomerServiceProvider extends ServiceProvider * Register services. * * @return void - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ - public function register() + public function register(): void { $this->registerConfig(); - $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); - $this->app->singleton('captcha', function ($app) { return new Captcha(); }); } - /** - * Register factories. - * - * @param string $path - * @return void - * @throws \Illuminate\Contracts\Container\BindingResolutionException - */ - protected function registerEloquentFactoriesFrom($path): void - { - $this->app->make(EloquentFactory::class)->load($path); - } - /** * Register package config. * * @return void */ - protected function registerConfig() + protected function registerConfig(): void { - $this->mergeConfigFrom( - dirname(__DIR__) . '/Config/system.php', - 'core' - ); + $this->mergeConfigFrom(dirname(__DIR__).'/Config/system.php', 'core'); } + } diff --git a/packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php b/packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php index feac8d1e9..58d36d07c 100644 --- a/packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php +++ b/packages/Webkul/Inventory/src/Database/Factories/InventorySourceFactory.php @@ -1,28 +1,46 @@ define(InventorySource::class, function (Faker $faker) { - $now = date("Y-m-d H:i:s"); - $code = $faker->unique()->word; - - return [ - 'code' => $faker->unique()->word, - 'name' => $code, - 'description' => $faker->sentence, - 'contact_name' => $faker->name, - 'contact_email' => $faker->safeEmail, - 'contact_number' => $faker->phoneNumber, - 'country' => $faker->countryCode, - 'state' => $faker->state, - 'city' => $faker->city, - 'street' => $faker->streetAddress, - 'postcode' => $faker->postcode, - 'priority' => 0, - 'status' => 1, - 'created_at' => $now, - 'updated_at' => $now, - ]; -}); \ No newline at end of file +use Webkul\Inventory\Models\InventorySource; +use Illuminate\Database\Eloquent\Factories\Factory; + +class InventorySourceFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = InventorySource::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $now = date("Y-m-d H:i:s"); + $code = $this->faker->unique()->word; + return [ + 'code' => $this->faker->unique()->word, + 'name' => $code, + 'description' => $this->faker->sentence, + 'contact_name' => $this->faker->name, + 'contact_email' => $this->faker->safeEmail, + 'contact_number' => $this->faker->phoneNumber, + 'country' => $this->faker->countryCode, + 'state' => $this->faker->state, + 'city' => $this->faker->city, + 'street' => $this->faker->streetAddress, + 'postcode' => $this->faker->postcode, + 'priority' => 0, + 'status' => 1, + 'created_at' => $now, + 'updated_at' => $now, + ]; + } + +} diff --git a/packages/Webkul/Inventory/src/Models/InventorySource.php b/packages/Webkul/Inventory/src/Models/InventorySource.php index 6ab8922ed..222b3bb1a 100755 --- a/packages/Webkul/Inventory/src/Models/InventorySource.php +++ b/packages/Webkul/Inventory/src/Models/InventorySource.php @@ -3,9 +3,25 @@ namespace Webkul\Inventory\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Inventory\Database\Factories\InventorySourceFactory; use Webkul\Inventory\Contracts\InventorySource as InventorySourceContract; class InventorySource extends Model implements InventorySourceContract { + + use HasFactory; + protected $guarded = ['_token']; + + /** + * Create a new factory instance for the model. + * + * @return InventorySourceFactory + */ + protected static function newFactory(): InventorySourceFactory + { + return InventorySourceFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php b/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php index 9a9520998..2e10fed18 100755 --- a/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php +++ b/packages/Webkul/Inventory/src/Providers/InventoryServiceProvider.php @@ -3,20 +3,18 @@ namespace Webkul\Inventory\Providers; use Illuminate\Support\ServiceProvider; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; class InventoryServiceProvider extends ServiceProvider { + /** * Bootstrap services. * * @return void */ - public function boot() + public function boot(): void { - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); - - $this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); } /** @@ -24,7 +22,8 @@ class InventoryServiceProvider extends ServiceProvider * * @return void */ - public function register() + public function register(): void { } + } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php index 432963515..90f8d5060 100644 --- a/packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php +++ b/packages/Webkul/Product/src/Database/Factories/ProductAttributeValueFactory.php @@ -1,17 +1,33 @@ define(ProductAttributeValue::class, function (Faker $faker) { - return [ - 'product_id' => function () { - return factory(Product::class)->create()->id; - }, - 'locale' => 'en', - 'channel' => 'default', - ]; -}); +class ProductAttributeValueFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = ProductAttributeValue::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'product_id' => Product::factory(), + 'locale' => 'en', + 'channel' => 'default', + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkFactory.php index e2cb8668e..4ddcf862e 100644 --- a/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkFactory.php +++ b/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkFactory.php @@ -1,35 +1,43 @@ define(ProductDownloadableLink::class, function (Faker $faker) { - $now = date("Y-m-d H:i:s"); - $filename = 'ProductImageExampleForUpload.jpg'; - $filepath = '/tests/_data/'; - - return [ - 'url' => '', - 'file' => $filepath . $filename, - 'file_name' => $filename, - 'type' => 'file', - 'price' => 0.0000, - 'downloads' => $faker->randomNumber(1), - 'product_id' => function () { - return factory(Product::class)->create()->id; - }, - 'created_at' => $now, - 'updated_at' => $now, - ]; -}); +class ProductDownloadableLinkFactory extends Factory +{ -$factory->define(ProductDownloadableLinkTranslation::class, function (Faker $faker) { - return [ - 'locale' => 'en', - 'title' => $faker->word, - ]; -}); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = ProductDownloadableLink::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $now = date("Y-m-d H:i:s"); + $filename = 'ProductImageExampleForUpload.jpg'; + $filepath = '/tests/_data/'; + + return [ + 'url' => '', + 'file' => $filepath.$filename, + 'file_name' => $filename, + 'type' => 'file', + 'price' => 0.0000, + 'downloads' => $this->faker->randomNumber(1), + 'product_id' => Product::factory(), + 'created_at' => $now, + 'updated_at' => $now, + ]; + } + +} diff --git a/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslation.php b/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslation.php deleted file mode 100644 index 1a9ecdcfc..000000000 --- a/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslation.php +++ /dev/null @@ -1,17 +0,0 @@ -define(ProductDownloadableLinkTranslation::class, function (Faker $faker) { - return [ - 'locale' => 'en', - 'title' => $faker->word, - 'product_downloadable_link_id' => function () { - return factory(ProductDownloadableLink::class)->create()->id; - }, - ]; -}); diff --git a/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslationFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslationFactory.php new file mode 100644 index 000000000..bb1b71c83 --- /dev/null +++ b/packages/Webkul/Product/src/Database/Factories/ProductDownloadableLinkTranslationFactory.php @@ -0,0 +1,33 @@ + 'en', + 'title' => $this->faker->word, + 'product_downloadable_link_id' => ProductDownloadableLink::factory(), + ]; + } + +} diff --git a/packages/Webkul/Product/src/Database/Factories/ProductFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductFactory.php index d0f4240b8..51a355564 100644 --- a/packages/Webkul/Product/src/Database/Factories/ProductFactory.php +++ b/packages/Webkul/Product/src/Database/Factories/ProductFactory.php @@ -1,29 +1,77 @@ define(Product::class, function (Faker $faker) { - return [ - 'sku' => $faker->uuid, - 'attribute_family_id' => 1, +class ProductFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Product::class; + + /** + * @var string[] + */ + protected $states = [ + 'simple', + 'virtual', + 'downloadable', + 'booking', ]; -}); -$factory->state(Product::class, 'simple', [ - 'type' => 'simple', -]); + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'sku' => $this->faker->uuid, + 'attribute_family_id' => 1, + ]; + } -$factory->state(Product::class, 'virtual', [ - 'type' => 'virtual', -]); + public function simple(): ProductFactory + { + return $this->state(function (array $attributes) { + return [ + 'type' => 'simple', + ]; + }); + } -$factory->state(Product::class, 'downloadable', [ - 'type' => 'downloadable', -]); + public function virtual(): ProductFactory + { + return $this->state(function (array $attributes) { + return [ + 'type' => 'virtual', + ]; + }); + } -$factory->state(Product::class, 'booking', [ - 'type' => 'booking', -]); \ No newline at end of file + public function downloadable(): ProductFactory + { + return $this->state(function (array $attributes) { + return [ + 'type' => 'downloadable', + ]; + }); + } + + public function booking(): ProductFactory + { + return $this->state(function (array $attributes) { + return [ + 'type' => 'booking', + ]; + }); + } + +} \ No newline at end of file diff --git a/packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php index 53e81effe..d6dfd4a88 100644 --- a/packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php +++ b/packages/Webkul/Product/src/Database/Factories/ProductInventoryFactory.php @@ -1,20 +1,34 @@ define(ProductInventory::class, function (Faker $faker) { - return [ - 'qty' => $faker->numberBetween(100, 200), - 'product_id' => function () { - return factory(Product::class)->create()->id; - }, - 'inventory_source_id' => function () { - return factory(InventorySource::class)->create()->id; - }, - ]; -}); \ No newline at end of file +class ProductInventoryFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = ProductInventory::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'qty' => $this->faker->numberBetween(100, 200), + 'product_id' => Product::factory(), + 'inventory_source_id' => InventorySource::factory(), + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Product/src/Database/Factories/ProductReviewFactory.php b/packages/Webkul/Product/src/Database/Factories/ProductReviewFactory.php index 34926b1c0..7be9cd047 100644 --- a/packages/Webkul/Product/src/Database/Factories/ProductReviewFactory.php +++ b/packages/Webkul/Product/src/Database/Factories/ProductReviewFactory.php @@ -1,20 +1,35 @@ define(ProductReview::class, function (Faker $faker, array $attributes) { - if (! array_key_exists('product_id', $attributes)) { - throw new InvalidArgumentException('product_id must be provided. You may use $I->haveProduct() to generate a product'); +class ProductReviewFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = ProductReview::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'title' => $this->faker->words(5, true), + 'rating' => $this->faker->numberBetween(0, 10), + 'status' => 1, + 'comment' => $this->faker->sentence(20), + 'product_id' => Product::factory(), + ]; } - return [ - 'title' => $faker->words(5, true), - 'rating' => $faker->numberBetween(0, 10), - 'status' => 1, - 'comment' => $faker->sentence(20), - 'product_id' => $attributes['product_id'], - ]; -}); +} diff --git a/packages/Webkul/Product/src/Models/Product.php b/packages/Webkul/Product/src/Models/Product.php index 220a5a15c..ecfaf48eb 100755 --- a/packages/Webkul/Product/src/Models/Product.php +++ b/packages/Webkul/Product/src/Models/Product.php @@ -3,18 +3,27 @@ namespace Webkul\Product\Models; use Exception; +use Illuminate\Support\Collection; use Illuminate\Database\Eloquent\Model; use Webkul\Attribute\Models\AttributeFamilyProxy; use Webkul\Attribute\Models\AttributeProxy; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Webkul\Product\Database\Factories\ProductFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Webkul\Attribute\Repositories\AttributeRepository; use Webkul\Category\Models\CategoryProxy; use Webkul\Inventory\Models\InventorySourceProxy; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Webkul\Product\Contracts\Product as ProductContract; use Webkul\Product\Database\Eloquent\Builder; use Webkul\Product\Type\AbstractType; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class Product extends Model implements ProductContract { + + use HasFactory; + /** * The attributes that are mass assignable. * @@ -33,7 +42,7 @@ class Product extends Model implements ProductContract * @var $casts */ protected $casts = [ - 'additional' => 'array' + 'additional' => 'array', ]; /** @@ -55,7 +64,7 @@ class Product extends Model implements ProductContract * * @return void */ - protected static function booted() + protected static function booted(): void { parent::boot(); @@ -77,7 +86,7 @@ class Product extends Model implements ProductContract * * @return void */ - public function refreshloadedAttributeValues() + public function refreshloadedAttributeValues(): void { self::$loadedAttributeValues = []; } @@ -85,7 +94,7 @@ class Product extends Model implements ProductContract /** * Get the product attribute family that owns the product. */ - public function attribute_family() + public function attribute_family(): BelongsTo { return $this->belongsTo(AttributeFamilyProxy::modelClass()); } @@ -93,7 +102,7 @@ class Product extends Model implements ProductContract /** * Get the product attribute values that owns the product. */ - public function attribute_values() + public function attribute_values(): HasMany { return $this->hasMany(ProductAttributeValueProxy::modelClass()); } @@ -102,7 +111,7 @@ class Product extends Model implements ProductContract * Get the product flat entries that are associated with product. * May be one for each locale and each channel. */ - public function product_flats() + public function product_flats(): HasMany { return $this->hasMany(ProductFlatProxy::modelClass(), 'product_id'); } @@ -110,7 +119,7 @@ class Product extends Model implements ProductContract /** * Get the product variants that owns the product. */ - public function variants() + public function variants(): HasMany { return $this->hasMany(static::class, 'parent_id'); } @@ -118,7 +127,7 @@ class Product extends Model implements ProductContract /** * Get the product reviews that owns the product. */ - public function reviews() + public function reviews(): HasMany { return $this->hasMany(ProductReviewProxy::modelClass()); } @@ -126,7 +135,7 @@ class Product extends Model implements ProductContract /** * Get the product that owns the product. */ - public function parent() + public function parent(): BelongsTo { return $this->belongsTo(static::class, 'parent_id'); } @@ -134,7 +143,7 @@ class Product extends Model implements ProductContract /** * The categories that belong to the product. */ - public function categories() + public function categories(): BelongsToMany { return $this->belongsToMany(CategoryProxy::modelClass(), 'product_categories'); } @@ -142,7 +151,7 @@ class Product extends Model implements ProductContract /** * The inventories that belong to the product. */ - public function inventories() + public function inventories(): HasMany { return $this->hasMany(ProductInventoryProxy::modelClass(), 'product_id'); } @@ -150,7 +159,7 @@ class Product extends Model implements ProductContract /** * The ordered inventories that belong to the product. */ - public function ordered_inventories() + public function ordered_inventories(): HasMany { return $this->hasMany(ProductOrderedInventoryProxy::modelClass(), 'product_id'); } @@ -158,15 +167,16 @@ class Product extends Model implements ProductContract /** * The inventory sources that belong to the product. */ - public function inventory_sources() + public function inventory_sources(): BelongsToMany { - return $this->belongsToMany(InventorySourceProxy::modelClass(), 'product_inventories')->withPivot('id', 'qty'); + return $this->belongsToMany(InventorySourceProxy::modelClass(), 'product_inventories') + ->withPivot('id', 'qty'); } /** * The super attributes that belong to the product. */ - public function super_attributes() + public function super_attributes(): BelongsToMany { return $this->belongsToMany(AttributeProxy::modelClass(), 'product_super_attributes'); } @@ -174,7 +184,7 @@ class Product extends Model implements ProductContract /** * The images that belong to the product. */ - public function images() + public function images(): HasMany { return $this->hasMany(ProductImageProxy::modelClass(), 'product_id'); } @@ -182,7 +192,7 @@ class Product extends Model implements ProductContract /** * The videos that belong to the product. */ - public function videos() + public function videos(): HasMany { return $this->hasMany(ProductVideoProxy::modelClass(), 'product_id'); } @@ -192,39 +202,43 @@ class Product extends Model implements ProductContract */ public function getBaseImageUrlAttribute() { - $image = $this->images()->first(); + $image = $this->images() + ->first(); - return $image ? $image->url : null; + return $image->url ?? null; } /** * The related products that belong to the product. */ - public function related_products() + public function related_products(): BelongsToMany { - return $this->belongsToMany(static::class, 'product_relations', 'parent_id', 'child_id')->limit(4); + return $this->belongsToMany(static::class, 'product_relations', 'parent_id', 'child_id') + ->limit(4); } /** * The up sells that belong to the product. */ - public function up_sells() + public function up_sells(): BelongsToMany { - return $this->belongsToMany(static::class, 'product_up_sells', 'parent_id', 'child_id')->limit(4); + return $this->belongsToMany(static::class, 'product_up_sells', 'parent_id', 'child_id') + ->limit(4); } /** * The cross sells that belong to the product. */ - public function cross_sells() + public function cross_sells(): BelongsToMany { - return $this->belongsToMany(static::class, 'product_cross_sells', 'parent_id', 'child_id')->limit(4); + return $this->belongsToMany(static::class, 'product_cross_sells', 'parent_id', 'child_id') + ->limit(4); } /** * The images that belong to the product. */ - public function downloadable_samples() + public function downloadable_samples(): HasMany { return $this->hasMany(ProductDownloadableSampleProxy::modelClass()); } @@ -232,7 +246,7 @@ class Product extends Model implements ProductContract /** * The images that belong to the product. */ - public function downloadable_links() + public function downloadable_links(): HasMany { return $this->hasMany(ProductDownloadableLinkProxy::modelClass()); } @@ -240,7 +254,7 @@ class Product extends Model implements ProductContract /** * Get the grouped products that owns the product. */ - public function grouped_products() + public function grouped_products(): HasMany { return $this->hasMany(ProductGroupedProductProxy::modelClass()); } @@ -248,7 +262,7 @@ class Product extends Model implements ProductContract /** * Get the bundle options that owns the product. */ - public function bundle_options() + public function bundle_options(): HasMany { return $this->hasMany(ProductBundleOptionProxy::modelClass()); } @@ -256,7 +270,7 @@ class Product extends Model implements ProductContract /** * Get the product customer group prices that owns the product. */ - public function customer_group_prices() + public function customer_group_prices(): HasMany { return $this->hasMany(ProductCustomerGroupPriceProxy::modelClass()); } @@ -264,33 +278,33 @@ class Product extends Model implements ProductContract /** * Get inventory source quantity. * - * @param integer $qty + * @param $inventorySourceId + * * @return bool */ - public function inventory_source_qty($inventorySourceId) + public function inventory_source_qty($inventorySourceId): bool { return $this->inventories() - ->where('inventory_source_id', $inventorySourceId) - ->sum('qty'); + ->where('inventory_source_id', $inventorySourceId) + ->sum('qty'); } /** * Get type instance. * * @return AbstractType + * @throws \Exception */ - public function getTypeInstance() + public function getTypeInstance(): AbstractType { if ($this->typeInstance) { return $this->typeInstance; } - $this->typeInstance = app(config('product_types.' . $this->type . '.class')); + $this->typeInstance = app(config('product_types.'.$this->type.'.class')); - if (! $this->typeInstance instanceof AbstractType) { - throw new Exception( - "Please ensure the product type '{$this->type}' is configured in your application." - ); + if (!$this->typeInstance instanceof AbstractType) { + throw new Exception("Please ensure the product type '{$this->type}' is configured in your application."); } $this->typeInstance->setProduct($this); @@ -301,61 +315,76 @@ class Product extends Model implements ProductContract /** * Is saleable. * - * @param string $key + * @param string $key + * * @return bool + * @throws \Exception */ - public function isSaleable() + public function isSaleable(): bool { - return $this->getTypeInstance()->isSaleable(); + return $this->getTypeInstance() + ->isSaleable(); } /** * Total quantity. * * @return integer + * @throws \Exception */ - public function totalQuantity() + public function totalQuantity(): int { - return $this->getTypeInstance()->totalQuantity(); + return $this->getTypeInstance() + ->totalQuantity(); } /** * Have sufficient quantity. * - * @param int $qty + * @param int $qty + * * @return bool + * @throws \Exception */ public function haveSufficientQuantity(int $qty): bool { - return $this->getTypeInstance()->haveSufficientQuantity($qty); + return $this->getTypeInstance() + ->haveSufficientQuantity($qty); } /** * Is stockable. * * @return bool + * @throws \Exception */ - public function isStockable() + public function isStockable(): bool { - return $this->getTypeInstance()->isStockable(); + return $this->getTypeInstance() + ->isStockable(); } /** * Get an attribute from the model. * - * @param string $key + * @param string $key + * * @return mixed */ public function getAttribute($key) { - if (! method_exists(static::class, $key) - && ! in_array($key, ['pivot', 'parent_id', 'attribute_family_id']) - && ! isset($this->attributes[$key]) - ) { + if (!method_exists(static::class, $key) + && !in_array($key, [ + 'pivot', + 'parent_id', + 'attribute_family_id', + ]) + && !isset($this->attributes[$key])) { if (isset($this->id)) { $this->attributes[$key] = ''; - $attribute = core()->getSingletonInstance(AttributeRepository::class) + $attribute = core() + ->getSingletonInstance(AttributeRepository::class) ->getAttributeByCode($key); $this->attributes[$key] = $this->getCustomAttributeValue($attribute); @@ -370,14 +399,16 @@ class Product extends Model implements ProductContract /** * Retrieve product attributes. * - * @param Group $group - * @param bool $skipSuperAttribute + * @param Group $group + * @param bool $skipSuperAttribute * - * @return Collection + * @return \Illuminate\Support\Collection + * @throws \Exception */ - public function getEditableAttributes($group = null, $skipSuperAttribute = true) + public function getEditableAttributes($group = null, $skipSuperAttribute = true): Collection { - return $this->getTypeInstance()->getEditableAttributes($group, $skipSuperAttribute); + return $this->getTypeInstance() + ->getEditableAttributes($group, $skipSuperAttribute); } /** @@ -387,31 +418,41 @@ class Product extends Model implements ProductContract */ public function getCustomAttributeValue($attribute) { - if (! $attribute) { + if (!$attribute) { return; } - $locale = core()->checkRequestedLocaleCodeInRequestedChannel(); + $locale = core()->checkRequestedLocaleCodeInRequestedChannel(); $channel = core()->getRequestedChannelCode(); - if ( - array_key_exists($this->id, self::$loadedAttributeValues) - && array_key_exists($attribute->id, self::$loadedAttributeValues[$this->id]) - ) { + if (array_key_exists($this->id, self::$loadedAttributeValues) + && array_key_exists($attribute->id, self::$loadedAttributeValues[$this->id])) { return self::$loadedAttributeValues[$this->id][$attribute->id]; } if ($attribute->value_per_channel) { if ($attribute->value_per_locale) { - $attributeValue = $this->attribute_values()->where('channel', $channel)->where('locale', $locale)->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this->attribute_values() + ->where('channel', $channel) + ->where('locale', $locale) + ->where('attribute_id', $attribute->id) + ->first(); } else { - $attributeValue = $this->attribute_values()->where('channel', $channel)->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this->attribute_values() + ->where('channel', $channel) + ->where('attribute_id', $attribute->id) + ->first(); } } else { if ($attribute->value_per_locale) { - $attributeValue = $this->attribute_values()->where('locale', $locale)->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this->attribute_values() + ->where('locale', $locale) + ->where('attribute_id', $attribute->id) + ->first(); } else { - $attributeValue = $this->attribute_values()->where('attribute_id', $attribute->id)->first(); + $attributeValue = $this->attribute_values() + ->where('attribute_id', $attribute->id) + ->first(); } } @@ -423,7 +464,7 @@ class Product extends Model implements ProductContract * * @return array */ - public function attributesToArray() + public function attributesToArray(): array { $attributes = parent::attributesToArray(); @@ -448,6 +489,7 @@ class Product extends Model implements ProductContract * Overrides the default Eloquent query builder. * * @param \Illuminate\Database\Query\Builder $query + * * @return \Webkul\Product\Database\Eloquent\Builder */ public function newEloquentBuilder($query) @@ -476,7 +518,7 @@ class Product extends Model implements ProductContract * * @return object */ - public function checkInLoadedFamilyAttributes() + public function checkInLoadedFamilyAttributes(): object { static $loadedFamilyAttributes = []; @@ -484,7 +526,19 @@ class Product extends Model implements ProductContract return $loadedFamilyAttributes[$this->attribute_family_id]; } - return $loadedFamilyAttributes[$this->attribute_family_id] = core()->getSingletonInstance(AttributeRepository::class) + return $loadedFamilyAttributes[$this->attribute_family_id] = core() + ->getSingletonInstance(AttributeRepository::class) ->getFamilyAttributes($this->attribute_family); } + + /** + * Create a new factory instance for the model. + * + * @return ProductFactory + */ + protected static function newFactory(): ProductFactory + { + return ProductFactory::new(); + } + } diff --git a/packages/Webkul/Product/src/Models/ProductAttributeValue.php b/packages/Webkul/Product/src/Models/ProductAttributeValue.php index 615baf5e5..67ec1cb2b 100755 --- a/packages/Webkul/Product/src/Models/ProductAttributeValue.php +++ b/packages/Webkul/Product/src/Models/ProductAttributeValue.php @@ -4,10 +4,16 @@ namespace Webkul\Product\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Attribute\Models\AttributeProxy; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Product\Database\Factories\ProductAttributeValueFactory; use Webkul\Product\Contracts\ProductAttributeValue as ProductAttributeValueContract; class ProductAttributeValue extends Model implements ProductAttributeValueContract { + + use HasFactory; + /** * Indicates if the model should be timestamped. * @@ -56,7 +62,7 @@ class ProductAttributeValue extends Model implements ProductAttributeValueContra /** * Get the attribute that owns the attribute value. */ - public function attribute() + public function attribute(): BelongsTo { return $this->belongsTo(AttributeProxy::modelClass()); } @@ -64,8 +70,19 @@ class ProductAttributeValue extends Model implements ProductAttributeValueContra /** * Get the product that owns the attribute value. */ - public function product() + public function product(): BelongsTo { return $this->belongsTo(ProductProxy::modelClass()); } + + /** + * Create a new factory instance for the model. + * + * @return ProductAttributeValueFactory + */ + protected static function newFactory(): ProductAttributeValueFactory + { + return ProductAttributeValueFactory::new(); + } + } diff --git a/packages/Webkul/Product/src/Models/ProductDownloadableLink.php b/packages/Webkul/Product/src/Models/ProductDownloadableLink.php index 88bf361c5..ce1ae4e85 100644 --- a/packages/Webkul/Product/src/Models/ProductDownloadableLink.php +++ b/packages/Webkul/Product/src/Models/ProductDownloadableLink.php @@ -4,10 +4,15 @@ namespace Webkul\Product\Models; use Webkul\Core\Eloquent\TranslatableModel; use Illuminate\Support\Facades\Storage; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Product\Database\Factories\ProductDownloadableLinkFactory; use Webkul\Product\Contracts\ProductDownloadableLink as ProductDownloadableLinkContract; class ProductDownloadableLink extends TranslatableModel implements ProductDownloadableLinkContract { + + use HasFactory; + public $translatedAttributes = ['title']; protected $fillable = [ @@ -31,7 +36,7 @@ class ProductDownloadableLink extends TranslatableModel implements ProductDownlo /** * Get the product that owns the image. */ - public function product() + public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(ProductProxy::modelClass()); } @@ -39,7 +44,7 @@ class ProductDownloadableLink extends TranslatableModel implements ProductDownlo /** * Get image url for the file. */ - public function file_url() + public function file_url(): string { return Storage::url($this->path); } @@ -47,7 +52,7 @@ class ProductDownloadableLink extends TranslatableModel implements ProductDownlo /** * Get image url for the file. */ - public function getFileUrlAttribute() + public function getFileUrlAttribute(): string { return $this->file_url(); } @@ -55,7 +60,7 @@ class ProductDownloadableLink extends TranslatableModel implements ProductDownlo /** * Get image url for the sample file. */ - public function sample_file_url() + public function sample_file_url(): string { return Storage::url($this->path); } @@ -63,7 +68,7 @@ class ProductDownloadableLink extends TranslatableModel implements ProductDownlo /** * Get image url for the sample file. */ - public function getSampleFileUrlAttribute() + public function getSampleFileUrlAttribute(): string { return $this->sample_file_url(); } @@ -71,13 +76,13 @@ class ProductDownloadableLink extends TranslatableModel implements ProductDownlo /** * @return array */ - public function toArray() + public function toArray(): array { $array = parent::toArray(); $translation = $this->translate(core()->getRequestedLocaleCode()); - $array['title'] = $translation ? $translation->title : ''; + $array['title'] = $translation->title ?? ''; $array['file_url'] = $this->file ? Storage::url($this->file) : null; @@ -85,4 +90,15 @@ class ProductDownloadableLink extends TranslatableModel implements ProductDownlo return $array; } + + /** + * Create a new factory instance for the model. + * + * @return ProductDownloadableLinkFactory + */ + protected static function newFactory(): ProductDownloadableLinkFactory + { + return ProductDownloadableLinkFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Models/ProductDownloadableLinkTranslation.php b/packages/Webkul/Product/src/Models/ProductDownloadableLinkTranslation.php index 8dfe77b4e..e4fa4f764 100644 --- a/packages/Webkul/Product/src/Models/ProductDownloadableLinkTranslation.php +++ b/packages/Webkul/Product/src/Models/ProductDownloadableLinkTranslation.php @@ -3,11 +3,27 @@ namespace Webkul\Product\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Product\Database\Factories\ProductDownloadableLinkTranslationFactory; use Webkul\Product\Contracts\ProductDownloadableLinkTranslation as ProductDownloadableLinkTranslationContract; class ProductDownloadableLinkTranslation extends Model implements ProductDownloadableLinkTranslationContract { + + use HasFactory; + public $timestamps = false; protected $fillable = ['title']; + + /** + * Create a new factory instance for the model. + * + * @return ProductDownloadableLinkTranslationFactory + */ + protected static function newFactory(): ProductDownloadableLinkTranslationFactory + { + return ProductDownloadableLinkTranslationFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Models/ProductInventory.php b/packages/Webkul/Product/src/Models/ProductInventory.php index 4b7d41301..faa86ffeb 100755 --- a/packages/Webkul/Product/src/Models/ProductInventory.php +++ b/packages/Webkul/Product/src/Models/ProductInventory.php @@ -4,10 +4,16 @@ namespace Webkul\Product\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Inventory\Models\InventorySourceProxy; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Product\Database\Factories\ProductInventoryFactory; use Webkul\Product\Contracts\ProductInventory as ProductInventoryContract; class ProductInventory extends Model implements ProductInventoryContract { + + use HasFactory; + public $timestamps = false; protected $fillable = [ @@ -20,7 +26,7 @@ class ProductInventory extends Model implements ProductInventoryContract /** * Get the product attribute family that owns the product. */ - public function inventory_source() + public function inventory_source(): BelongsTo { return $this->belongsTo(InventorySourceProxy::modelClass()); } @@ -28,8 +34,19 @@ class ProductInventory extends Model implements ProductInventoryContract /** * Get the product that owns the product inventory. */ - public function product() + public function product(): BelongsTo { return $this->belongsTo(ProductProxy::modelClass()); } + + /** + * Create a new factory instance for the model. + * + * @return ProductInventoryFactory + */ + protected static function newFactory(): ProductInventoryFactory + { + return ProductInventoryFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Models/ProductReview.php b/packages/Webkul/Product/src/Models/ProductReview.php index 859b38fec..becccc86b 100755 --- a/packages/Webkul/Product/src/Models/ProductReview.php +++ b/packages/Webkul/Product/src/Models/ProductReview.php @@ -5,10 +5,17 @@ namespace Webkul\Product\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Customer\Models\CustomerProxy; use Webkul\Product\Models\Product; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Product\Database\Factories\ProductReviewFactory; use Webkul\Product\Contracts\ProductReview as ProductReviewContract; class ProductReview extends Model implements ProductReviewContract { + + use HasFactory; + protected $fillable = [ 'comment', 'title', @@ -22,7 +29,7 @@ class ProductReview extends Model implements ProductReviewContract /** * Get the product attribute family that owns the product. */ - public function customer() + public function customer(): BelongsTo { return $this->belongsTo(CustomerProxy::modelClass()); } @@ -30,7 +37,7 @@ class ProductReview extends Model implements ProductReviewContract /** * Get the product. */ - public function product() + public function product(): BelongsTo { return $this->belongsTo(ProductProxy::modelClass()); } @@ -38,8 +45,19 @@ class ProductReview extends Model implements ProductReviewContract /** * The images that belong to the review. */ - public function images() + public function images(): HasMany { return $this->hasMany(ProductReviewImageProxy::modelClass(), 'review_id'); } + + /** + * Create a new factory instance for the model. + * + * @return ProductReviewFactory + */ + protected static function newFactory(): ProductReviewFactory + { + return ProductReviewFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Product/src/Providers/ProductServiceProvider.php b/packages/Webkul/Product/src/Providers/ProductServiceProvider.php index 783b6a286..745a683eb 100755 --- a/packages/Webkul/Product/src/Providers/ProductServiceProvider.php +++ b/packages/Webkul/Product/src/Providers/ProductServiceProvider.php @@ -2,7 +2,6 @@ namespace Webkul\Product\Providers; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; use Illuminate\Support\ServiceProvider; use Webkul\Product\Models\ProductProxy; use Webkul\Product\Observers\ProductObserver; @@ -16,23 +15,22 @@ use Webkul\Product\ProductVideo; class ProductServiceProvider extends ServiceProvider { + /** * Bootstrap services. * * @return void */ - public function boot() + public function boot(): void { - include __DIR__ . '/../Http/helpers.php'; + include __DIR__.'/../Http/helpers.php'; - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); - - $this->app->make(EloquentFactory::class)->load(__DIR__ . '/../Database/Factories'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); $this->app->register(EventServiceProvider::class); $this->publishes([ - dirname(__DIR__) . '/Config/imagecache.php' => config_path('imagecache.php'), + dirname(__DIR__).'/Config/imagecache.php' => config_path('imagecache.php'), ]); ProductProxy::observe(ProductObserver::class); @@ -50,8 +48,6 @@ class ProductServiceProvider extends ServiceProvider $this->registerCommands(); $this->registerFacades(); - - $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); } /** @@ -61,9 +57,7 @@ class ProductServiceProvider extends ServiceProvider */ public function registerConfig(): void { - $this->mergeConfigFrom( - dirname(__DIR__) . '/Config/product_types.php', 'product_types' - ); + $this->mergeConfigFrom(dirname(__DIR__).'/Config/product_types.php', 'product_types'); } /** @@ -74,28 +68,19 @@ class ProductServiceProvider extends ServiceProvider protected function registerCommands(): void { if ($this->app->runningInConsole()) { - $this->commands([PriceUpdate::class, GenerateProducts::class,]); + $this->commands([ + PriceUpdate::class, + GenerateProducts::class, + ]); } } - /** - * Register factories. - * - * @param string $path - * @return void - */ - protected function registerEloquentFactoriesFrom($path): void - { - $this->app->make(EloquentFactory::class)->load($path); - } - - /** * Register Bouncer as a singleton. * * @return void */ - protected function registerFacades() + protected function registerFacades(): void { // Product image $loader = AliasLoader::getInstance(); @@ -112,4 +97,5 @@ class ProductServiceProvider extends ServiceProvider return app()->make(ProductVideo::class); }); } + } \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Database/Factories/InventorySourceFactory.php b/packages/Webkul/Sales/src/Database/Factories/InventorySourceFactory.php deleted file mode 100644 index 2ac56182c..000000000 --- a/packages/Webkul/Sales/src/Database/Factories/InventorySourceFactory.php +++ /dev/null @@ -1,26 +0,0 @@ -define(InventorySource::class, function (Faker $faker) { - $code = $faker->unique()->word; - - return [ - 'code' => $faker->unique()->word, - 'name' => $code, - 'description' => $faker->sentence, - 'contact_name' => $faker->name, - 'contact_email' => $faker->safeEmail, - 'contact_number' => $faker->phoneNumber, - 'country' => $faker->countryCode, - 'state' => $faker->state, - 'city' => $faker->city, - 'street' => $faker->streetAddress, - 'postcode' => $faker->postcode, - 'priority' => 0, - 'status' => 1, - ]; -}); diff --git a/packages/Webkul/Sales/src/Database/Factories/InvoiceFactory.php b/packages/Webkul/Sales/src/Database/Factories/InvoiceFactory.php index 03c5cd36f..1dbb3fbcb 100644 --- a/packages/Webkul/Sales/src/Database/Factories/InvoiceFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/InvoiceFactory.php @@ -1,61 +1,96 @@ define(Invoice::class, function (Faker $faker, array $attributes) { - $subTotal = $faker->randomFloat(2); - $shippingAmount = $faker->randomFloat(2); - $taxAmount = $faker->randomFloat(2); +class InvoiceFactory extends Factory +{ - if (! isset($attributes['order_id'])) { - $attributes['order_id'] = function () { - return factory(Order::class)->create()->id; - }; - } + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Invoice::class; - if (! isset($attributes['order_address_id'])) { - $attributes['order_address_id'] = function () use ($attributes) { - return factory(OrderAddress::class) - ->create(['order_id' => $attributes['order_id']]) - ->id; - }; - } - - return [ - 'email_sent' => 0, - 'total_qty' => $faker->randomNumber(), - 'base_currency_code' => 'EUR', - 'channel_currency_code' => 'EUR', - 'order_currency_code' => 'EUR', - 'sub_total' => $subTotal, - 'base_sub_total' => $subTotal, - 'grand_total' => $subTotal, - 'base_grand_total' => $subTotal, - 'shipping_amount' => $shippingAmount, - 'base_shipping_amount' => $shippingAmount, - 'tax_amount' => $taxAmount, - 'base_tax_amount' => $taxAmount, - 'discount_amount' => 0, - 'base_discount_amount' => 0, - 'order_id' => $attributes['order_id'], - 'order_address_id' => $attributes['order_address_id'], + /** + * @var array + */ + protected $states = [ + 'pending', + 'paid', + 'refunded', ]; -}); -$factory->state(Invoice::class, 'pending', [ - 'status' => 'pending', -]); + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $subTotal = $this->faker->randomFloat(2); + $shippingAmount = $this->faker->randomFloat(2); + $taxAmount = $this->faker->randomFloat(2); -$factory->state(Invoice::class, 'paid', [ - 'status' => 'paid', -]); + if (!isset($attributes['order_id'])) { + $attributes['order_id'] = Order::factory(); + } -$factory->state(Invoice::class, 'refunded', [ - 'status' => 'refunded', -]); + if (!isset($attributes['order_address_id'])) { + $attributes['order_address_id'] = OrderAddress::factory(); + } + return [ + 'email_sent' => 0, + 'total_qty' => $this->faker->randomNumber(), + 'base_currency_code' => 'EUR', + 'channel_currency_code' => 'EUR', + 'order_currency_code' => 'EUR', + 'sub_total' => $subTotal, + 'base_sub_total' => $subTotal, + 'grand_total' => $subTotal, + 'base_grand_total' => $subTotal, + 'shipping_amount' => $shippingAmount, + 'base_shipping_amount' => $shippingAmount, + 'tax_amount' => $taxAmount, + 'base_tax_amount' => $taxAmount, + 'discount_amount' => 0, + 'base_discount_amount' => 0, + 'order_id' => $attributes['order_id'], + 'order_address_id' => $attributes['order_address_id'], + ]; + } + + public function pending(): InvoiceFactory + { + return $this->state(function (array $attributes) { + return [ + 'status' => 'pending', + ]; + }); + } + + public function paid(): InvoiceFactory + { + return $this->state(function (array $attributes) { + return [ + 'status' => 'paid', + ]; + }); + } + + public function refunded(): InvoiceFactory + { + return $this->state(function (array $attributes) { + return [ + 'status' => 'refunded', + ]; + }); + } + +} \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Database/Factories/InvoiceItemFactory.php b/packages/Webkul/Sales/src/Database/Factories/InvoiceItemFactory.php index 21cd87411..8895d6e33 100644 --- a/packages/Webkul/Sales/src/Database/Factories/InvoiceItemFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/InvoiceItemFactory.php @@ -1,41 +1,54 @@ define(InvoiceItem::class, function (Faker $faker, array $attributes) { +class InvoiceItemFactory extends Factory +{ - $basePrice = $faker->randomFloat(2); - $quantity = $faker->randomNumber(); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = InvoiceItem::class; - if (! $attributes['order_item_id']) { - $attributes['order_item_id'] = function () { - return factory(OrderItem::class)->create()->id; - }; + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $basePrice = $this->faker->randomFloat(2); + $quantity = $this->faker->randomNumber(); + + if (!isset($attributes['order_item_id'])) { + $attributes['order_item_id'] = OrderItem::factory(); + } + + $orderItem = OrderItem::query() + ->find($attributes['order_item_id']); + + return [ + 'name' => $this->faker->word, + 'sku' => $this->faker->unique()->ean13, + 'qty' => $quantity, + 'price' => $basePrice, + 'base_price' => $basePrice, + 'total' => $quantity * $basePrice, + 'base_total' => $quantity * $basePrice, + 'tax_amount' => 0, + 'base_tax_amount' => 0, + 'product_id' => $orderItem->product_id, + 'product_type' => $orderItem->product_type, + 'order_item_id' => $attributes['order_item_id'], + 'invoice_id' => Invoice::factory(), + ]; } - $orderItem = OrderItem::find($attributes['order_item_id']); - - return [ - 'name' => $faker->word, - 'sku' => $faker->unique()->ean13, - 'qty' => $quantity, - 'price' => $basePrice, - 'base_price' => $basePrice, - 'total' => $quantity * $basePrice, - 'base_total' => $quantity * $basePrice, - 'tax_amount' => 0, - 'base_tax_amount' => 0, - 'product_id' => $orderItem->product_id, - 'product_type' => $orderItem->product_type, - 'order_item_id' => $attributes['order_item_id'], - 'invoice_id' => function () { - return factory(Invoice::class)->create()->id; - }, - ]; -}); +} diff --git a/packages/Webkul/Sales/src/Database/Factories/OrderAddressFactory.php b/packages/Webkul/Sales/src/Database/Factories/OrderAddressFactory.php index d8b44c4bc..e2e91c02e 100644 --- a/packages/Webkul/Sales/src/Database/Factories/OrderAddressFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/OrderAddressFactory.php @@ -1,34 +1,64 @@ define(OrderAddress::class, function (Faker $faker) { - $customer = factory(Customer::class)->create(); - $customerAddress = factory(CustomerAddress::class)->make(); +class OrderAddressFactory extends Factory +{ - return [ - 'first_name' => $customer->first_name, - 'last_name' => $customer->last_name, - 'email' => $customer->email, - 'address1' => $customerAddress->address1, - 'country' => $customerAddress->country, - 'state' => $customerAddress->state, - 'city' => $customerAddress->city, - 'postcode' => $customerAddress->postcode, - 'phone' => $customerAddress->phone, - 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING, - 'order_id' => function () { - return factory(Order::class)->create()->id; - }, + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = OrderAddress::class; + + /** + * @var string[] + */ + protected $states = [ + 'shipping', ]; -}); -$factory->state(OrderAddress::class, 'shipping', [ - 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING, -]); \ No newline at end of file + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $customer = Customer::factory() + ->create(); + $customerAddress = CustomerAddress::factory() + ->make(); + + return [ + 'first_name' => $customer->first_name, + 'last_name' => $customer->last_name, + 'email' => $customer->email, + 'address1' => $customerAddress->address1, + 'country' => $customerAddress->country, + 'state' => $customerAddress->state, + 'city' => $customerAddress->city, + 'postcode' => $customerAddress->postcode, + 'phone' => $customerAddress->phone, + 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING, + 'order_id' => Order::factory(), + ]; + } + + public function shipping(): void + { + $this->state(function (array $attributes) { + return [ + 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING, + ]; + }); + } + +} \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Database/Factories/OrderFactory.php b/packages/Webkul/Sales/src/Database/Factories/OrderFactory.php index a32a31e65..5956bbabc 100644 --- a/packages/Webkul/Sales/src/Database/Factories/OrderFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/OrderFactory.php @@ -1,67 +1,109 @@ define(Order::class, function (Faker $faker) { - $lastOrder = DB::table('orders') - ->orderBy('id', 'desc') - ->select('id') - ->first() - ->id ?? 0; +class OrderFactory extends Factory +{ + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Order::class; - $customer = factory(Customer::class)->create(); - - return [ - 'increment_id' => $lastOrder + 1, - 'status' => 'pending', - 'channel_name' => 'Default', - 'is_guest' => 0, - 'customer_id' => $customer->id, - 'customer_email' => $customer->email, - 'customer_first_name' => $customer->first_name, - 'customer_last_name' => $customer->last_name, - 'is_gift' => 0, - 'total_item_count' => 1, - 'total_qty_ordered' => 1, - 'base_currency_code' => 'EUR', - 'channel_currency_code' => 'EUR', - 'order_currency_code' => 'EUR', - 'grand_total' => 0.0000, - 'base_grand_total' => 0.0000, - 'grand_total_invoiced' => 0.0000, - 'base_grand_total_invoiced' => 0.0000, - 'grand_total_refunded' => 0.0000, - 'base_grand_total_refunded' => 0.0000, - 'sub_total' => 0.0000, - 'base_sub_total' => 0.0000, - 'sub_total_invoiced' => 0.0000, - 'base_sub_total_invoiced' => 0.0000, - 'sub_total_refunded' => 0.0000, - 'base_sub_total_refunded' => 0.0000, - 'customer_type' => Customer::class, - 'channel_id' => 1, - 'channel_type' => Channel::class, - 'cart_id' => 0, - 'shipping_method' => 'free_free', - 'shipping_title' => 'Free Shipping', + /** + * @var string[] + */ + protected $states = [ + 'pending', + 'completed', + 'closed', ]; -}); -$factory->state(Order::class, 'pending', [ - 'status' => 'pending', -]); + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $lastOrder = DB::table('orders') + ->orderBy('id', 'desc') + ->select('id') + ->first()->id ?? 0; -$factory->state(Order::class, 'completed', [ - 'status' => 'completed', -]); -$factory->state(Order::class, 'closed', [ - 'status' => 'closed', -]); + $customer = Customer::factory() + ->create(); + + return [ + 'increment_id' => $lastOrder + 1, + 'status' => 'pending', + 'channel_name' => 'Default', + 'is_guest' => 0, + 'customer_id' => $customer->id, + 'customer_email' => $customer->email, + 'customer_first_name' => $customer->first_name, + 'customer_last_name' => $customer->last_name, + 'is_gift' => 0, + 'total_item_count' => 1, + 'total_qty_ordered' => 1, + 'base_currency_code' => 'EUR', + 'channel_currency_code' => 'EUR', + 'order_currency_code' => 'EUR', + 'grand_total' => 0.0000, + 'base_grand_total' => 0.0000, + 'grand_total_invoiced' => 0.0000, + 'base_grand_total_invoiced' => 0.0000, + 'grand_total_refunded' => 0.0000, + 'base_grand_total_refunded' => 0.0000, + 'sub_total' => 0.0000, + 'base_sub_total' => 0.0000, + 'sub_total_invoiced' => 0.0000, + 'base_sub_total_invoiced' => 0.0000, + 'sub_total_refunded' => 0.0000, + 'base_sub_total_refunded' => 0.0000, + 'customer_type' => Customer::class, + 'channel_id' => 1, + 'channel_type' => Channel::class, + 'cart_id' => 0, + 'shipping_method' => 'free_free', + 'shipping_title' => 'Free Shipping', + ]; + } + + public function pending(): OrderFactory + { + return $this->state(function (array $attributes) { + return [ + 'status' => 'pending', + ]; + }); + } + + public function completed(): OrderFactory + { + return $this->state(function (array $attributes) { + return [ + 'status' => 'completed', + ]; + }); + } + + public function closed(): OrderFactory + { + return $this->state(function (array $attributes) { + return [ + 'status' => 'closed', + ]; + }); + } + +} diff --git a/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php b/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php index 7eeb89c7b..e58c0fb0d 100644 --- a/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/OrderItemFactory.php @@ -1,43 +1,62 @@ define(OrderItem::class, function (Faker $faker, array $attributes) { - $now = date("Y-m-d H:i:s"); +class OrderItemFactory extends Factory +{ - if (isset($attributes['product_id'])) { - $product = Product::where('id', $attributes['product_id'])->first(); - } else { - $product = factory(Product::class)->create(); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = OrderItem::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $now = date("Y-m-d H:i:s"); + + if (isset($attributes['product_id'])) { + $product = Product::query() + ->where('id', $attributes['product_id']) + ->first(); + } else { + $product = Product::factory() + ->create(); + } + + $fallbackPrice = $this->faker->randomFloat(4, 0, 1000); + + return [ + 'sku' => $product->sku, + 'type' => $product->type, + 'name' => $product->name, + 'price' => $product->price ?? $fallbackPrice, + 'base_price' => $product->price ?? $fallbackPrice, + 'total' => $product->price ?? $fallbackPrice, + 'base_total' => $product->price ?? $fallbackPrice, + 'product_id' => $product->id, + 'qty_ordered' => 1, + 'qty_shipped' => 0, + 'qty_invoiced' => 0, + 'qty_canceled' => 0, + 'qty_refunded' => 0, + 'additional' => [], + 'order_id' => Order::factory(), + 'created_at' => $now, + 'updated_at' => $now, + 'product_type' => Product::class, + ]; } - $fallbackPrice = $faker->randomFloat(4, 0, 1000); - - return [ - 'sku' => $product->sku, - 'type' => $product->type, - 'name' => $product->name, - 'price' => $product->price ?? $fallbackPrice, - 'base_price' => $product->price ?? $fallbackPrice, - 'total' => $product->price ?? $fallbackPrice, - 'base_total' => $product->price ?? $fallbackPrice, - 'product_id' => $product->id, - 'qty_ordered' => 1, - 'qty_shipped' => 0, - 'qty_invoiced' => 0, - 'qty_canceled' => 0, - 'qty_refunded' => 0, - 'additional' => [], - 'order_id' => function () { - return factory(Order::class)->create()->id; - }, - 'created_at' => $now, - 'updated_at' => $now, - 'product_type' => Product::class, - ]; -}); \ No newline at end of file +} \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Database/Factories/OrderPaymentFactory.php b/packages/Webkul/Sales/src/Database/Factories/OrderPaymentFactory.php index 5acce649d..a0be980ef 100644 --- a/packages/Webkul/Sales/src/Database/Factories/OrderPaymentFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/OrderPaymentFactory.php @@ -1,19 +1,31 @@ define(OrderPayment::class, function (Faker $faker, array $attributes) { +class OrderPaymentFactory extends Factory +{ - if (!array_key_exists('order_id', $attributes)) { - throw new InvalidArgumentException('order_id must be provided.'); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = OrderPayment::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'method' => 'cashondelivery', + ]; } - return [ - 'method' => 'cashondelivery', - ]; -}); +} diff --git a/packages/Webkul/Sales/src/Database/Factories/RefundFactory.php b/packages/Webkul/Sales/src/Database/Factories/RefundFactory.php index c4a5ba62a..4dfd66300 100644 --- a/packages/Webkul/Sales/src/Database/Factories/RefundFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/RefundFactory.php @@ -1,16 +1,32 @@ define(Refund::class, function (Faker $faker, array $attributes) { - return [ - 'order_id' => function () { - return factory(Order::class)->create()->id; - }, - ]; -}); +class RefundFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Refund::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'order_id' => Order::factory(), + ]; + } + +} diff --git a/packages/Webkul/Sales/src/Database/Factories/ShipmentFactory.php b/packages/Webkul/Sales/src/Database/Factories/ShipmentFactory.php index 4332e18d3..339e7b99a 100644 --- a/packages/Webkul/Sales/src/Database/Factories/ShipmentFactory.php +++ b/packages/Webkul/Sales/src/Database/Factories/ShipmentFactory.php @@ -1,22 +1,39 @@ define(Shipment::class, function (Faker $faker) { - $address = factory(OrderAddress::class)->create(); +class ShipmentFactory extends Factory +{ - return [ - 'total_qty' => $faker->numberBetween(1, 20), - 'order_id' => $address->order_id, - 'order_address_id' => $address->id, - 'inventory_source_id' => function () { - return factory(InventorySource::class)->create()->id; - }, - ]; -}); + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = Shipment::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $address = OrderAddress::factory() + ->create(); + + return [ + 'total_qty' => $this->faker->numberBetween(1, 20), + 'order_id' => $address->order_id, + 'order_address_id' => $address->id, + 'inventory_source_id' => InventorySource::factory(), + ]; + } + +} diff --git a/packages/Webkul/Sales/src/Models/Invoice.php b/packages/Webkul/Sales/src/Models/Invoice.php index 3ce7d640d..785f4c52d 100755 --- a/packages/Webkul/Sales/src/Models/Invoice.php +++ b/packages/Webkul/Sales/src/Models/Invoice.php @@ -3,19 +3,30 @@ namespace Webkul\Sales\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\HasMany; use Webkul\Sales\Contracts\Invoice as InvoiceContract; use Webkul\Sales\Traits\PaymentTerm; +use Webkul\Sales\Database\Factories\InvoiceFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Invoice extends Model implements InvoiceContract { + use PaymentTerm; + use HasFactory; /** * The attributes that aren't mass assignable. * * @var string[]|bool */ - protected $guarded = ['id', 'created_at', 'updated_at']; + protected $guarded = [ + 'id', + 'created_at', + 'updated_at', + ]; /** * Invoice status. @@ -33,13 +44,13 @@ class Invoice extends Model implements InvoiceContract */ public function getStatusLabelAttribute() { - return isset($this->statusLabel[$this->state]) ? $this->statusLabel[$this->state] : ''; + return $this->statusLabel[$this->state] ?? ''; } /** * Get the order that belongs to the invoice. */ - public function order() + public function order(): BelongsTo { return $this->belongsTo(OrderProxy::modelClass()); } @@ -47,15 +58,16 @@ class Invoice extends Model implements InvoiceContract /** * Get the invoice items record associated with the invoice. */ - public function items() + public function items(): HasMany { - return $this->hasMany(InvoiceItemProxy::modelClass())->whereNull('parent_id'); + return $this->hasMany(InvoiceItemProxy::modelClass()) + ->whereNull('parent_id'); } /** * Get the customer record associated with the invoice. */ - public function customer() + public function customer(): MorphTo { return $this->morphTo(); } @@ -63,7 +75,7 @@ class Invoice extends Model implements InvoiceContract /** * Get the channel record associated with the invoice. */ - public function channel() + public function channel(): MorphTo { return $this->morphTo(); } @@ -71,9 +83,20 @@ class Invoice extends Model implements InvoiceContract /** * Get the address for the invoice. */ - public function address() + public function address(): BelongsTo { return $this->belongsTo(OrderAddressProxy::modelClass(), 'order_address_id') - ->where('address_type', OrderAddress::ADDRESS_TYPE_BILLING); + ->where('address_type', OrderAddress::ADDRESS_TYPE_BILLING); } + + /** + * Create a new factory instance for the model. + * + * @return InvoiceFactory + */ + protected static function newFactory(): InvoiceFactory + { + return InvoiceFactory::new(); + } + } diff --git a/packages/Webkul/Sales/src/Models/InvoiceItem.php b/packages/Webkul/Sales/src/Models/InvoiceItem.php index 9eaa6f47c..de39941f0 100755 --- a/packages/Webkul/Sales/src/Models/InvoiceItem.php +++ b/packages/Webkul/Sales/src/Models/InvoiceItem.php @@ -2,12 +2,26 @@ namespace Webkul\Sales\Models; +use Webkul\Product\Type\AbstractType; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Sales\Database\Factories\InvoiceItemFactory; use Webkul\Sales\Contracts\InvoiceItem as InvoiceItemContract; class InvoiceItem extends Model implements InvoiceItemContract { - protected $guarded = ['id', 'created_at', 'updated_at']; + + use HasFactory; + + protected $guarded = [ + 'id', + 'created_at', + 'updated_at', + ]; protected $casts = [ 'additional' => 'array', @@ -18,15 +32,15 @@ class InvoiceItem extends Model implements InvoiceItemContract * * @return AbstractType */ - public function getTypeInstance() + public function getTypeInstance(): AbstractType { return $this->order_item->getTypeInstance(); } - + /** * Get the invoice record associated with the invoice item. */ - public function invoice() + public function invoice(): BelongsTo { return $this->belongsTo(InvoiceProxy::modelClass()); } @@ -34,7 +48,7 @@ class InvoiceItem extends Model implements InvoiceItemContract /** * Get the order item record associated with the invoice item. */ - public function order_item() + public function order_item(): BelongsTo { return $this->belongsTo(OrderItemProxy::modelClass()); } @@ -42,7 +56,7 @@ class InvoiceItem extends Model implements InvoiceItemContract /** * Get the invoice record associated with the invoice item. */ - public function product() + public function product(): MorphTo { return $this->morphTo(); } @@ -50,7 +64,7 @@ class InvoiceItem extends Model implements InvoiceItemContract /** * Get the child item record associated with the invoice item. */ - public function child() + public function child(): HasOne { return $this->hasOne(InvoiceItemProxy::modelClass(), 'parent_id'); } @@ -58,7 +72,7 @@ class InvoiceItem extends Model implements InvoiceItemContract /** * Get the children items. */ - public function children() + public function children(): HasMany { return $this->hasMany(self::class, 'parent_id'); } @@ -70,4 +84,15 @@ class InvoiceItem extends Model implements InvoiceItemContract { return $this->order_item->type; } + + /** + * Create a new factory instance for the model. + * + * @return InvoiceItemFactory + */ + protected static function newFactory(): InvoiceItemFactory + { + return InvoiceItemFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Models/Order.php b/packages/Webkul/Sales/src/Models/Order.php index e9df0aecd..21687144b 100755 --- a/packages/Webkul/Sales/src/Models/Order.php +++ b/packages/Webkul/Sales/src/Models/Order.php @@ -5,15 +5,26 @@ namespace Webkul\Sales\Models; use Webkul\Checkout\Models\CartProxy; use Illuminate\Database\Eloquent\Model; use Webkul\Sales\Contracts\Order as OrderContract; +use Webkul\Sales\Database\Factories\OrderFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Order extends Model implements OrderContract { + + use HasFactory; + public const STATUS_PENDING = 'pending'; + public const STATUS_PENDING_PAYMENT = 'pending_payment'; + public const STATUS_PROCESSING = 'processing'; + public const STATUS_COMPLETED = 'completed'; + public const STATUS_CANCELED = 'canceled'; + public const STATUS_CLOSED = 'closed'; + public const STATUS_FRAUD = 'fraud'; protected $guarded = [ @@ -41,9 +52,9 @@ class Order extends Model implements OrderContract /** * Get the order items record associated with the order. */ - public function getCustomerFullNameAttribute() + public function getCustomerFullNameAttribute(): string { - return $this->customer_first_name . ' ' . $this->customer_last_name; + return $this->customer_first_name.' '.$this->customer_last_name; } /** @@ -73,7 +84,7 @@ class Order extends Model implements OrderContract /** * Get the associated cart that was used to create this order. */ - public function cart() + public function cart(): \Illuminate\Database\Eloquent\Relations\BelongsTo { return $this->belongsTo(CartProxy::modelClass()); } @@ -81,15 +92,16 @@ class Order extends Model implements OrderContract /** * Get the order items record associated with the order. */ - public function items() + public function items(): \Illuminate\Database\Eloquent\Relations\HasMany { - return $this->hasMany(OrderItemProxy::modelClass())->whereNull('parent_id'); + return $this->hasMany(OrderItemProxy::modelClass()) + ->whereNull('parent_id'); } /** * Get the comments record associated with the order. */ - public function comments() + public function comments(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(OrderCommentProxy::modelClass()); } @@ -97,7 +109,7 @@ class Order extends Model implements OrderContract /** * Get the order items record associated with the order. */ - public function all_items() + public function all_items(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(OrderItemProxy::modelClass()); } @@ -105,7 +117,7 @@ class Order extends Model implements OrderContract /** * Get the order shipments record associated with the order. */ - public function shipments() + public function shipments(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(ShipmentProxy::modelClass()); } @@ -113,7 +125,7 @@ class Order extends Model implements OrderContract /** * Get the order invoices record associated with the order. */ - public function invoices() + public function invoices(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(InvoiceProxy::modelClass()); } @@ -121,7 +133,7 @@ class Order extends Model implements OrderContract /** * Get the order refunds record associated with the order. */ - public function refunds() + public function refunds(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(RefundProxy::modelClass()); } @@ -129,7 +141,7 @@ class Order extends Model implements OrderContract /** * Get the order transactions record associated with the order. */ - public function transactions() + public function transactions(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(OrderTransactionProxy::modelClass()); } @@ -137,7 +149,7 @@ class Order extends Model implements OrderContract /** * Get the customer record associated with the order. */ - public function customer() + public function customer(): \Illuminate\Database\Eloquent\Relations\MorphTo { return $this->morphTo(); } @@ -145,7 +157,7 @@ class Order extends Model implements OrderContract /** * Get the addresses for the order. */ - public function addresses() + public function addresses(): \Illuminate\Database\Eloquent\Relations\HasMany { return $this->hasMany(OrderAddressProxy::modelClass()); } @@ -153,17 +165,18 @@ class Order extends Model implements OrderContract /** * Get the payment for the order. */ - public function payment() + public function payment(): \Illuminate\Database\Eloquent\Relations\HasOne { return $this->hasOne(OrderPaymentProxy::modelClass()); } /** - * Get the biling address for the order. + * Get the billing address for the order. */ - public function billing_address() + public function billing_address(): \Illuminate\Database\Eloquent\Relations\HasMany { - return $this->addresses()->where('address_type', OrderAddress::ADDRESS_TYPE_BILLING); + return $this->addresses() + ->where('address_type', OrderAddress::ADDRESS_TYPE_BILLING); } /** @@ -171,15 +184,17 @@ class Order extends Model implements OrderContract */ public function getBillingAddressAttribute() { - return $this->billing_address()->first(); + return $this->billing_address() + ->first(); } /** * Get the shipping address for the order. */ - public function shipping_address() + public function shipping_address(): \Illuminate\Database\Eloquent\Relations\HasMany { - return $this->addresses()->where('address_type', OrderAddress::ADDRESS_TYPE_SHIPPING); + return $this->addresses() + ->where('address_type', OrderAddress::ADDRESS_TYPE_SHIPPING); } /** @@ -187,7 +202,8 @@ class Order extends Model implements OrderContract */ public function getShippingAddressAttribute() { - return $this->shipping_address()->first(); + return $this->shipping_address() + ->first(); } /** @@ -206,7 +222,8 @@ class Order extends Model implements OrderContract public function haveStockableItems(): bool { foreach ($this->items as $item) { - if ($item->getTypeInstance()->isStockable()) { + if ($item->getTypeInstance() + ->isStockable()) { return true; } } @@ -268,12 +285,13 @@ class Order extends Model implements OrderContract if ($this->payment->method == 'moneytransfer' && core()->getConfigData('sales.paymentmethods.moneytransfer.generate_invoice')) { return false; } - + if ($this->status === self::STATUS_FRAUD) { return false; } - $pendingInvoice = $this->invoices->where('state', 'pending')->first(); + $pendingInvoice = $this->invoices->where('state', 'pending') + ->first(); if ($pendingInvoice) { return true; } @@ -297,8 +315,9 @@ class Order extends Model implements OrderContract if ($this->status === self::STATUS_FRAUD) { return false; } - - $pendingInvoice = $this->invoices->where('state', 'pending')->first(); + + $pendingInvoice = $this->invoices->where('state', 'pending') + ->first(); if ($pendingInvoice) { return false; } @@ -309,10 +328,22 @@ class Order extends Model implements OrderContract } } - if ($this->base_grand_total_invoiced - $this->base_grand_total_refunded - $this->refunds()->sum('base_adjustment_fee') > 0) { + if ($this->base_grand_total_invoiced - $this->base_grand_total_refunded - $this->refunds() + ->sum('base_adjustment_fee') > 0) { return true; } return false; } + + /** + * Create a new factory instance for the model. + * + * @return OrderFactory + */ + protected static function newFactory(): OrderFactory + { + return OrderFactory::new(); + } + } diff --git a/packages/Webkul/Sales/src/Models/OrderAddress.php b/packages/Webkul/Sales/src/Models/OrderAddress.php index a8a63f700..4358a764d 100755 --- a/packages/Webkul/Sales/src/Models/OrderAddress.php +++ b/packages/Webkul/Sales/src/Models/OrderAddress.php @@ -4,20 +4,28 @@ namespace Webkul\Sales\Models; use Webkul\Checkout\Models\CartAddress; use Webkul\Core\Models\Address; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Sales\Database\Factories\OrderAddressFactory; use Webkul\Sales\Contracts\OrderAddress as OrderAddressContract; use Illuminate\Database\Eloquent\Builder; /** * Class OrderAddress + * * @package Webkul\Sales\Models * * @property integer $order_id - * @property Order $order + * @property Order $order * */ class OrderAddress extends Address implements OrderAddressContract { + + use HasFactory; + public const ADDRESS_TYPE_SHIPPING = 'order_shipping'; + public const ADDRESS_TYPE_BILLING = 'order_billing'; /** @@ -32,12 +40,12 @@ class OrderAddress extends Address implements OrderAddressContract * * @return void */ - protected static function boot() + protected static function boot(): void { static::addGlobalScope('address_type', function (Builder $builder) { $builder->whereIn('address_type', [ self::ADDRESS_TYPE_BILLING, - self::ADDRESS_TYPE_SHIPPING + self::ADDRESS_TYPE_SHIPPING, ]); }); @@ -58,8 +66,19 @@ class OrderAddress extends Address implements OrderAddressContract /** * Get the order record associated with the address. */ - public function order() + public function order(): BelongsTo { return $this->belongsTo(Order::class); } + + /** + * Create a new factory instance for the model. + * + * @return OrderAddressFactory + */ + protected static function newFactory(): OrderAddressFactory + { + return OrderAddressFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Models/OrderItem.php b/packages/Webkul/Sales/src/Models/OrderItem.php index cfd479d4e..5c43f37cb 100755 --- a/packages/Webkul/Sales/src/Models/OrderItem.php +++ b/packages/Webkul/Sales/src/Models/OrderItem.php @@ -2,12 +2,21 @@ namespace Webkul\Sales\Models; +use Webkul\Product\Type\AbstractType; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\HasOne; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Webkul\Sales\Database\Factories\OrderItemFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Webkul\Sales\Contracts\OrderItem as OrderItemContract; -use Webkul\Product\Models\Product; class OrderItem extends Model implements OrderItemContract { + + use HasFactory; + protected $guarded = [ 'id', 'child', @@ -27,13 +36,13 @@ class OrderItem extends Model implements OrderItemContract * * @return AbstractType */ - public function getTypeInstance() + public function getTypeInstance(): AbstractType { if ($this->typeInstance) { return $this->typeInstance; } - $this->typeInstance = app(config('product_types.' . $this->type . '.class')); + $this->typeInstance = app(config('product_types.'.$this->type.'.class')); if ($this->product) { $this->typeInstance->setProduct($this); @@ -45,17 +54,18 @@ class OrderItem extends Model implements OrderItemContract /** * @return bool */ - public function isStockable() + public function isStockable(): bool { - return $this->getTypeInstance()->isStockable(); + return $this->getTypeInstance() + ->isStockable(); } /** - * Checks if new shipment is allow or not + * Checks if new shipment is allowed or not */ - public function canShip() + public function canShip(): bool { - if (! $this->isStockable()) { + if (!$this->isStockable()) { return false; } @@ -71,7 +81,7 @@ class OrderItem extends Model implements OrderItemContract */ public function getQtyToShipAttribute() { - if (! $this->isStockable()) { + if (!$this->isStockable()) { return 0; } @@ -101,13 +111,9 @@ class OrderItem extends Model implements OrderItemContract /** * Checks if new cancel is allow or not */ - public function canCancel() + public function canCancel(): bool { - if ($this->qty_to_cancel > 0) { - return true; - } - - return false; + return $this->qty_to_cancel > 0; } /** @@ -129,7 +135,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the order record associated with the order item. */ - public function order() + public function order(): BelongsTo { return $this->belongsTo(OrderProxy::modelClass()); } @@ -137,7 +143,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the product record associated with the order item. */ - public function product() + public function product(): MorphTo { return $this->morphTo(); } @@ -145,7 +151,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the child item record associated with the order item. */ - public function child() + public function child(): HasOne { return $this->hasOne(OrderItemProxy::modelClass(), 'parent_id'); } @@ -153,7 +159,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the parent item record associated with the order item. */ - public function parent() + public function parent(): BelongsTo { return $this->belongsTo(self::class, 'parent_id'); } @@ -161,7 +167,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the children items. */ - public function children() + public function children(): HasMany { return $this->hasMany(self::class, 'parent_id'); } @@ -169,7 +175,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the invoice items record associated with the order item. */ - public function invoice_items() + public function invoice_items(): HasMany { return $this->hasMany(InvoiceItemProxy::modelClass()); } @@ -177,7 +183,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the shipment items record associated with the order item. */ - public function shipment_items() + public function shipment_items(): HasMany { return $this->hasMany(ShipmentItemProxy::modelClass()); } @@ -185,7 +191,7 @@ class OrderItem extends Model implements OrderItemContract /** * Get the refund items record associated with the order item. */ - public function refund_items() + public function refund_items(): HasMany { return $this->hasMany(RefundItemProxy::modelClass()); } @@ -193,7 +199,7 @@ class OrderItem extends Model implements OrderItemContract /** * Returns configurable option html */ - public function downloadable_link_purchased() + public function downloadable_link_purchased(): HasMany { return $this->hasMany(DownloadableLinkPurchasedProxy::modelClass()); } @@ -201,7 +207,7 @@ class OrderItem extends Model implements OrderItemContract /** * @return array */ - public function toArray() + public function toArray(): array { $array = parent::toArray(); @@ -217,4 +223,15 @@ class OrderItem extends Model implements OrderItemContract return $array; } + + /** + * Create a new factory instance for the model. + * + * @return OrderItemFactory + */ + protected static function newFactory(): OrderItemFactory + { + return OrderItemFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Models/OrderPayment.php b/packages/Webkul/Sales/src/Models/OrderPayment.php index 23eeccd5f..34eedb442 100755 --- a/packages/Webkul/Sales/src/Models/OrderPayment.php +++ b/packages/Webkul/Sales/src/Models/OrderPayment.php @@ -3,10 +3,15 @@ namespace Webkul\Sales\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Webkul\Sales\Database\Factories\OrderPaymentFactory; use Webkul\Sales\Contracts\OrderPayment as OrderPaymentContract; class OrderPayment extends Model implements OrderPaymentContract { + + use HasFactory; + protected $table = 'order_payment'; protected $guarded = [ @@ -16,6 +21,17 @@ class OrderPayment extends Model implements OrderPaymentContract ]; protected $casts = [ - 'additional' => 'array' + 'additional' => 'array', ]; + + /** + * Create a new factory instance for the model. + * + * @return OrderPaymentFactory + */ + protected static function newFactory(): OrderPaymentFactory + { + return OrderPaymentFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Models/Refund.php b/packages/Webkul/Sales/src/Models/Refund.php index fa70bbc23..645cc60ef 100644 --- a/packages/Webkul/Sales/src/Models/Refund.php +++ b/packages/Webkul/Sales/src/Models/Refund.php @@ -3,31 +3,38 @@ namespace Webkul\Sales\Models; use Illuminate\Database\Eloquent\Model; +use Webkul\Sales\Database\Factories\RefundFactory; use Webkul\Sales\Contracts\Refund as RefundContract; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; class Refund extends Model implements RefundContract { + + use HasFactory; + protected $guarded = [ 'id', 'created_at', 'updated_at', ]; - protected $statusLabel = [ - ]; + protected $statusLabel = []; /** * Returns the status label from status code */ public function getStatusLabelAttribute() { - return isset($this->statusLabel[$this->state]) ? $this->statusLabel[$this->state] : ''; + return $this->statusLabel[$this->state] ?? ''; } /** * Get the order that belongs to the Refund. */ - public function order() + public function order(): BelongsTo { return $this->belongsTo(OrderProxy::modelClass()); } @@ -35,14 +42,16 @@ class Refund extends Model implements RefundContract /** * Get the Refund items record associated with the Refund. */ - public function items() { - return $this->hasMany(RefundItemProxy::modelClass())->whereNull('parent_id'); + public function items(): HasMany + { + return $this->hasMany(RefundItemProxy::modelClass()) + ->whereNull('parent_id'); } /** * Get the customer record associated with the Refund. */ - public function customer() + public function customer(): MorphTo { return $this->morphTo(); } @@ -50,7 +59,7 @@ class Refund extends Model implements RefundContract /** * Get the channel record associated with the Refund. */ - public function channel() + public function channel(): MorphTo { return $this->morphTo(); } @@ -58,8 +67,19 @@ class Refund extends Model implements RefundContract /** * Get the addresses for the shipment. */ - public function address() + public function address(): BelongsTo { return $this->belongsTo(OrderAddressProxy::modelClass(), 'order_address_id'); } + + /** + * Create a new factory instance for the model. + * + * @return RefundFactory + */ + protected static function newFactory(): RefundFactory + { + return RefundFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Models/Shipment.php b/packages/Webkul/Sales/src/Models/Shipment.php index 8dc2cbb87..942060a3d 100755 --- a/packages/Webkul/Sales/src/Models/Shipment.php +++ b/packages/Webkul/Sales/src/Models/Shipment.php @@ -4,10 +4,18 @@ namespace Webkul\Sales\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Inventory\Models\InventorySource; +use Illuminate\Database\Eloquent\Relations\MorphTo; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Webkul\Sales\Database\Factories\ShipmentFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Webkul\Sales\Contracts\Shipment as ShipmentContract; class Shipment extends Model implements ShipmentContract { + + use HasFactory; + protected $guarded = [ 'id', 'created_at', @@ -17,7 +25,7 @@ class Shipment extends Model implements ShipmentContract /** * Get the order that belongs to the invoice. */ - public function order() + public function order(): BelongsTo { return $this->belongsTo(OrderProxy::modelClass()); } @@ -25,7 +33,7 @@ class Shipment extends Model implements ShipmentContract /** * Get the shipment items record associated with the shipment. */ - public function items() + public function items(): HasMany { return $this->hasMany(ShipmentItemProxy::modelClass()); } @@ -33,7 +41,7 @@ class Shipment extends Model implements ShipmentContract /** * Get the inventory source associated with the shipment. */ - public function inventory_source() + public function inventory_source(): BelongsTo { return $this->belongsTo(InventorySource::class, 'inventory_source_id'); } @@ -41,7 +49,7 @@ class Shipment extends Model implements ShipmentContract /** * Get the customer record associated with the shipment. */ - public function customer() + public function customer(): MorphTo { return $this->morphTo(); } @@ -49,9 +57,20 @@ class Shipment extends Model implements ShipmentContract /** * Get the address for the shipment. */ - public function address() + public function address(): BelongsTo { return $this->belongsTo(OrderAddressProxy::modelClass(), 'order_address_id') - ->where('address_type', OrderAddress::ADDRESS_TYPE_SHIPPING); + ->where('address_type', OrderAddress::ADDRESS_TYPE_SHIPPING); } + + /** + * Create a new factory instance for the model. + * + * @return ShipmentFactory + */ + protected static function newFactory(): ShipmentFactory + { + return ShipmentFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Sales/src/Providers/SalesServiceProvider.php b/packages/Webkul/Sales/src/Providers/SalesServiceProvider.php index a3d2a6003..f966f91ac 100755 --- a/packages/Webkul/Sales/src/Providers/SalesServiceProvider.php +++ b/packages/Webkul/Sales/src/Providers/SalesServiceProvider.php @@ -2,41 +2,24 @@ namespace Webkul\Sales\Providers; -use Illuminate\Database\Eloquent\Factory as EloquentFactory; use Illuminate\Support\ServiceProvider; class SalesServiceProvider extends ServiceProvider { - public function boot() + + public function boot(): void { - $this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations'); + $this->loadMigrationsFrom(__DIR__.'/../Database/Migrations'); } /** * Register services. * * @return void - * @throws \Illuminate\Contracts\Container\BindingResolutionException */ - public function register() + public function register(): void { - $this->registerEloquentFactoriesFrom(__DIR__ . '/../Database/Factories'); - - $this->mergeConfigFrom( - dirname(__DIR__) . '/Config/system.php', 'core' - ); + $this->mergeConfigFrom(dirname(__DIR__).'/Config/system.php', 'core'); } - /** - * Register factories. - * - * @param string $path - * - * @return void - * @throws \Illuminate\Contracts\Container\BindingResolutionException - */ - protected function registerEloquentFactoriesFrom($path): void - { - $this->app->make(EloquentFactory::class)->load($path); - } } \ No newline at end of file diff --git a/packages/Webkul/Tax/src/Database/Factories/TaxCategoryFactory.php b/packages/Webkul/Tax/src/Database/Factories/TaxCategoryFactory.php index a0aab3cab..2078f7398 100644 --- a/packages/Webkul/Tax/src/Database/Factories/TaxCategoryFactory.php +++ b/packages/Webkul/Tax/src/Database/Factories/TaxCategoryFactory.php @@ -1,14 +1,32 @@ define(TaxCategory::class, function (Faker $faker) { - return [ - 'code' => $faker->uuid, - 'name' => $faker->words(2, true), - 'description' => $faker->sentence(10), - ]; -}); +class TaxCategoryFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = TaxCategory::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'code' => $this->faker->uuid, + 'name' => $this->faker->words(2, true), + 'description' => $this->faker->sentence(10), + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Tax/src/Database/Factories/TaxMapFactory.php b/packages/Webkul/Tax/src/Database/Factories/TaxMapFactory.php index 7e7d50769..bd90e0300 100644 --- a/packages/Webkul/Tax/src/Database/Factories/TaxMapFactory.php +++ b/packages/Webkul/Tax/src/Database/Factories/TaxMapFactory.php @@ -1,19 +1,34 @@ TaxCategory::factory(), + 'tax_rate_id' => TaxRate::factory(), + ]; + } + +} -$factory->define(TaxMap::class, function (Faker $faker) { - return [ - 'tax_category_id' => function () { - return factory(TaxCategory::class)->create()->id; - }, - 'tax_rate_id' => function () { - return factory(TaxRate::class)->create()->id; - }, - ]; -}); diff --git a/packages/Webkul/Tax/src/Database/Factories/TaxRateFactory.php b/packages/Webkul/Tax/src/Database/Factories/TaxRateFactory.php index a611bebc0..0008f14a4 100644 --- a/packages/Webkul/Tax/src/Database/Factories/TaxRateFactory.php +++ b/packages/Webkul/Tax/src/Database/Factories/TaxRateFactory.php @@ -1,19 +1,37 @@ define(TaxRate::class, function (Faker $faker) { - return [ - 'identifier' => $faker->uuid, - 'is_zip' => 0, - 'zip_code' => '*', - 'zip_from' => null, - 'zip_to' => null, - 'state' => '', - 'country' => $faker->countryCode, - 'tax_rate' => $faker->randomFloat(2, 3, 25), - ]; -}); +class TaxRateFactory extends Factory +{ + + /** + * The name of the factory's corresponding model. + * + * @var string + */ + protected $model = TaxRate::class; + + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'identifier' => $this->faker->uuid, + 'is_zip' => 0, + 'zip_code' => '*', + 'zip_from' => null, + 'zip_to' => null, + 'state' => '', + 'country' => $this->faker->countryCode, + 'tax_rate' => $this->faker->randomFloat(2, 3, 25), + ]; + } + +} \ No newline at end of file diff --git a/packages/Webkul/Tax/src/Models/TaxCategory.php b/packages/Webkul/Tax/src/Models/TaxCategory.php index 011e37106..e2984ecf9 100755 --- a/packages/Webkul/Tax/src/Models/TaxCategory.php +++ b/packages/Webkul/Tax/src/Models/TaxCategory.php @@ -3,10 +3,16 @@ namespace Webkul\Tax\Models; use Illuminate\Database\Eloquent\Model; +use Webkul\Tax\Database\Factories\TaxCategoryFactory; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Webkul\Tax\Contracts\TaxCategory as TaxCategoryContract; class TaxCategory extends Model implements TaxCategoryContract { + + use HasFactory; + /** * The attributes that are mass assignable. * @@ -16,14 +22,26 @@ class TaxCategory extends Model implements TaxCategoryContract protected $table = 'tax_categories'; protected $fillable = [ - 'code', - 'name', - 'description', + 'code', + 'name', + 'description', ]; //for joining the two way pivot table - public function tax_rates() + public function tax_rates(): BelongsToMany { - return $this->belongsToMany(TaxRateProxy::modelClass(), 'tax_categories_tax_rates', 'tax_category_id')->withPivot('id'); + return $this->belongsToMany(TaxRateProxy::modelClass(), 'tax_categories_tax_rates', 'tax_category_id') + ->withPivot('id'); } + + /** + * Create a new factory instance for the model. + * + * @return TaxCategoryFactory + */ + protected static function newFactory(): TaxCategoryFactory + { + return TaxCategoryFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Tax/src/Models/TaxMap.php b/packages/Webkul/Tax/src/Models/TaxMap.php index a74c5fa1f..f1aeda2f9 100755 --- a/packages/Webkul/Tax/src/Models/TaxMap.php +++ b/packages/Webkul/Tax/src/Models/TaxMap.php @@ -5,10 +5,14 @@ namespace Webkul\Tax\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Tax\Models\TaxCategory; use Webkul\Tax\Models\TaxRate; +use Webkul\Tax\Database\Factories\TaxMapFactory; use Webkul\Tax\Contracts\TaxMap as TaxMapContract; +use Illuminate\Database\Eloquent\Factories\HasFactory; class TaxMap extends Model implements TaxMapContract { + use HasFactory; + /** * The attributes that are mass assignable. * @@ -22,4 +26,14 @@ class TaxMap extends Model implements TaxMapContract 'tax_rate_id', ]; + /** + * Create a new factory instance for the model. + * + * @return TaxMapFactory + */ + protected static function newFactory(): TaxMapFactory + { + return TaxMapFactory::new(); + } + } \ No newline at end of file diff --git a/packages/Webkul/Tax/src/Models/TaxRate.php b/packages/Webkul/Tax/src/Models/TaxRate.php index 404d4a154..b3a8fdb0d 100755 --- a/packages/Webkul/Tax/src/Models/TaxRate.php +++ b/packages/Webkul/Tax/src/Models/TaxRate.php @@ -4,10 +4,14 @@ namespace Webkul\Tax\Models; use Illuminate\Database\Eloquent\Model; use Webkul\Tax\Models\TaxCategory; +use Webkul\Tax\Database\Factories\TaxRateFactory; use Webkul\Tax\Contracts\TaxRate as TaxRateContract; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; class TaxRate extends Model implements TaxRateContract { + use HasFactory; /** * The attributes that are mass assignable. * @@ -27,8 +31,19 @@ class TaxRate extends Model implements TaxRateContract 'tax_rate', ]; - public function tax_categories() + public function tax_categories(): BelongsToMany { return $this->belongsToMany(TaxCategoryProxy::modelClass(), 'tax_categories_tax_rates', 'tax_rate_id', 'id'); } + + /** + * Create a new factory instance for the model. + * + * @return TaxRateFactory + */ + protected static function newFactory(): TaxRateFactory + { + return TaxRateFactory::new(); + } + } \ No newline at end of file