diff --git a/app/GraphQL/Inventory/Mutations/Variants/Variants.php b/app/GraphQL/Inventory/Mutations/Variants/Variants.php index a2849ed2d..e619690b1 100644 --- a/app/GraphQL/Inventory/Mutations/Variants/Variants.php +++ b/app/GraphQL/Inventory/Mutations/Variants/Variants.php @@ -14,6 +14,7 @@ use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantDto; use Kanvas\Inventory\Variants\DataTransferObject\VariantsWarehouses; use Kanvas\Inventory\Variants\Models\Variants as VariantModel; +use Kanvas\Inventory\Variants\Models\VariantsWarehouses as ModelsVariantsWarehouses; use Kanvas\Inventory\Variants\Repositories\VariantsRepository; use Kanvas\Inventory\Warehouses\Repositories\WarehouseRepository; @@ -31,7 +32,12 @@ public function create(mixed $root, array $req): VariantModel { $variantDto = VariantDto::viaRequest($req['input']); $action = new CreateVariantsAction($variantDto, auth()->user()); - return $action->execute(); + $variantModel = $action->execute(); + + WarehouseRepository::getById($variantDto->warehouse_id, $variantDto->product->company()->get()->first()); + $variantModel->warehouses()->attach($variantDto->warehouse_id); + + return $variantModel; } /** @@ -44,7 +50,7 @@ public function create(mixed $root, array $req): VariantModel */ public function update(mixed $root, array $req): VariantModel { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); $variant->update($req['input']); return $variant; } @@ -59,9 +65,9 @@ public function update(mixed $root, array $req): VariantModel */ public function delete(mixed $root, array $req): bool { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); - return $variant->delete(); + return $variant->softdelete(); } /** @@ -74,7 +80,7 @@ public function delete(mixed $root, array $req): bool */ public function addToWarehouse(mixed $root, array $req): VariantModel { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); $warehouse = WarehouseRepository::getById($req['warehouse_id']); $variantWarehouses = VariantsWarehouses::from($req['input']); @@ -91,7 +97,7 @@ public function addToWarehouse(mixed $root, array $req): VariantModel */ public function removeToWarehouse(mixed $root, array $req): VariantModel { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); $warehouse = WarehouseRepository::getById($req['warehouse_id']); $variant->warehouses()->detach($warehouse); @@ -108,9 +114,9 @@ public function removeToWarehouse(mixed $root, array $req): VariantModel */ public function addAttribute(mixed $root, array $req): VariantModel { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); - $attribute = AttributesRepository::getById($req['attributes_id']); + $attribute = AttributesRepository::getById((int) $req['attributes_id']); (new AddAttributeAction($variant, $attribute, $req['input']['value']))->execute(); return $variant; } @@ -125,9 +131,9 @@ public function addAttribute(mixed $root, array $req): VariantModel */ public function removeAttribute(mixed $root, array $req): VariantModel { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); - $attribute = AttributesRepository::getById($req['attributes_id']); + $attribute = AttributesRepository::getById((int) $req['attributes_id']); $variant->attributes()->detach($attribute); return $variant; } @@ -142,12 +148,15 @@ public function removeAttribute(mixed $root, array $req): VariantModel */ public function addToChannel(mixed $root, array $req): VariantModel { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); + $warehouse = WarehouseRepository::getById((int) $req['warehouses_id']); + $variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variant->getId()) + ->where('warehouses_id', $warehouse->getId()) + ->firstOrFail(); - $warehouse = WarehouseRepository::getById($req['warehouses_id']); - $channel = ChannelRepository::getById($req['channels_id']); + $channel = ChannelRepository::getById((int) $req['channels_id']); $variantChannel = VariantChannel::from($req['input']); - (new AddVariantToChannel($variant, $channel, $warehouse, $variantChannel))->execute(); + (new AddVariantToChannel($variantWarehouses, $channel, $variantChannel))->execute(); return $variant; } @@ -161,10 +170,13 @@ public function addToChannel(mixed $root, array $req): VariantModel */ public function removeChannel(mixed $root, array $req): VariantModel { - $variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany()); - - $channel = ChannelRepository::getById($req['channels_id']); - $variant->channels()->detach($channel->id); + $variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany()); + $warehouse = WarehouseRepository::getById((int) $req['warehouses_id']); + $variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variant->getId()) + ->where('warehouses_id', $warehouse->getId()) + ->firstOrFail(); + $channel = ChannelRepository::getById((int) $req['channels_id']); + $variantWarehouses->channels()->where('id', $channel->getId())->detach($channel->id); return $variant; } } diff --git a/database/migrations/Inventory/2023_06_22_223035_update_product_variants_warehouses.php b/database/migrations/Inventory/2023_06_22_223035_update_product_variants_warehouses.php new file mode 100644 index 000000000..a4b63f769 --- /dev/null +++ b/database/migrations/Inventory/2023_06_22_223035_update_product_variants_warehouses.php @@ -0,0 +1,40 @@ +dropForeign('products_variants_warehouses_warehouses_id_foreign'); + $table->dropForeign('products_variants_warehouses_products_variants_id_foreign'); + + $table->dropPrimary(); + Schema::enableForeignKeyConstraints(); + }); + + Schema::table('products_variants_warehouses', function (Blueprint $table) { + Schema::disableForeignKeyConstraints(); + $table->id()->first(); + $table->foreign('products_variants_id')->references('id')->on('products_variants'); + $table->foreign('warehouses_id')->references('id')->on('warehouses'); + $table->unique(['products_variants_id', 'warehouses_id'], 'product_variants_warehouses_unique'); + Schema::enableForeignKeyConstraints(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +}; diff --git a/database/migrations/Inventory/2023_06_27_164405_update_product_variants_channels.php b/database/migrations/Inventory/2023_06_27_164405_update_product_variants_channels.php new file mode 100644 index 000000000..b5678c7d2 --- /dev/null +++ b/database/migrations/Inventory/2023_06_27_164405_update_product_variants_channels.php @@ -0,0 +1,42 @@ +dropForeign('products_variants_channels_products_variants_id_foreign'); + $table->dropForeign('products_variants_channels_warehouses_id_foreign'); + $table->dropForeign('products_variants_channels_channels_id_foreign'); + $table->dropPrimary(); + $table->dropColumn(['products_variants_id', 'warehouses_id']); + + Schema::enableForeignKeyConstraints(); + }); + Schema::table('products_variants_channels', function (Blueprint $table) { + Schema::disableForeignKeyConstraints(); + + $table->bigInteger('product_variants_warehouse_id')->unsigned()->after('channels_id'); + $table->foreign('product_variants_warehouse_id')->references('id')->on('products_variants_warehouses'); + $table->foreign('channels_id')->references('id')->on('channels'); + + $table->unique(['product_variants_warehouse_id', 'channels_id'], 'variants_warehouse_channel'); + Schema::enableForeignKeyConstraints(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + // + } +}; diff --git a/database/migrations/Inventory/2023_07_10_033443_update_variant_channel_price.php b/database/migrations/Inventory/2023_07_10_033443_update_variant_channel_price.php new file mode 100644 index 000000000..b0d3247bd --- /dev/null +++ b/database/migrations/Inventory/2023_07_10_033443_update_variant_channel_price.php @@ -0,0 +1,41 @@ +double('price', 8, 2)->default(0.00)->change(); + }); + + Schema::table('products_variants_warehouses', function (Blueprint $table) { + $table->double('price', 8, 2)->default(0.00)->change(); + $table->integer('position')->default(0)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('products_variants_channels', function (Blueprint $table) { + $table->double('price', 8, 2)->change(); + }); + + Schema::table('products_variants_warehouses', function (Blueprint $table) { + $table->double('price', 8, 2)->change(); + $table->integer('position')->change(); + }); + } +}; diff --git a/graphql/schemas/Inventory/attributes.graphql b/graphql/schemas/Inventory/attributes.graphql index 24463bee0..62e10e588 100644 --- a/graphql/schemas/Inventory/attributes.graphql +++ b/graphql/schemas/Inventory/attributes.graphql @@ -9,7 +9,7 @@ input AttributeUpdateInput { } type Attributes { - id: Int! + id: ID! uuid: String name: String created_at: String @@ -19,7 +19,7 @@ type Attributes { } type AttributesValue { - id: Int! + id: ID! value: Mixed } @@ -28,11 +28,11 @@ extend type Mutation @guard { @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeMutation@create" ) - updateAttribute(id: Int!, input: AttributeUpdateInput!): Attributes! + updateAttribute(id: ID!, input: AttributeUpdateInput!): Attributes! @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeMutation@update" ) - deleteAttribute(id: Int!): Attributes! + deleteAttribute(id: ID!): Attributes! @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeMutation@delete" ) diff --git a/graphql/schemas/Inventory/region.graphql b/graphql/schemas/Inventory/region.graphql index deeadd19a..8fe96b539 100644 --- a/graphql/schemas/Inventory/region.graphql +++ b/graphql/schemas/Inventory/region.graphql @@ -18,7 +18,7 @@ type Currency { input RegionInput { currency_id: Int! name: String! - slug: String! + slug: String short_slug: String! settings: String is_default: Int! diff --git a/graphql/schemas/Inventory/variant.graphql b/graphql/schemas/Inventory/variant.graphql index 61506a830..451948b52 100644 --- a/graphql/schemas/Inventory/variant.graphql +++ b/graphql/schemas/Inventory/variant.graphql @@ -1,5 +1,5 @@ type Variant { - id: Int + id: ID uuid: String! slug: String! products_id: Int! @@ -17,15 +17,15 @@ type Variant { interactions(visitor: VisitorEntityInput!): Interactions @method(name: "getEntitySocialInteractions") product: Product - warehouses: [WarehouseVariantRelationship] - attributes: [VariantsAttributes] - channels: [VariantChannelRelationship] + warehouses: [WarehouseVariantRelationship!]! @hasMany(relation: "variantWarehouses") + attributes: [VariantsAttributes!]! companies: Company } type WarehouseVariantRelationship { - id: Int! - uuid: String! + id: ID! + warehouseinfo: Warehouse @hasMany(relation: "warehouse") + channels: [VariantChannelRelationship] quantity: Float price: Float sku: String @@ -39,14 +39,14 @@ type WarehouseVariantRelationship { can_pre_order: Boolean is_coming_soon: Boolean is_new: Boolean - is_variant_published: Boolean + is_published: Boolean } type VariantsAttributes { name: String! value: Mixed } type VariantChannelRelationship { - warehouses_id: Int + name: String price: Float discounted_price: Float is_published: Boolean @@ -88,6 +88,7 @@ input VariantsInput { price: Float source_id: Mixed attributes: [VariantsAttributesInput] + warehouse_id: Int! } input VariantsUpdateInput { products_id: Int @@ -112,16 +113,16 @@ extend type Mutation @guard { @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@create" ) - updateVariant(input: VariantsUpdateInput!, id: Int!): Variant + updateVariant(input: VariantsUpdateInput!, id: ID!): Variant @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@update" ) - deleteVariant(id: Int!): Boolean + deleteVariant(id: ID!): Boolean @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@delete" ) addVariantToWarehouse( - id: Int! + id: ID! warehouse_id: Int! input: VariantsWarehousesInput! ): Variant @@ -133,27 +134,31 @@ extend type Mutation @guard { resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@removeToWarehouse" ) addAttributeToVariant( - id: Int! - attributes_id: Int! + id: ID! + attributes_id: ID! input: VariantsAttributesInput! ): Variant @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@addAttribute" ) - removeAttributeToVariant(id: Int!, attributes_id: Int!): Variant + removeAttributeToVariant(id: ID!, attributes_id: ID!): Variant @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@removeAttribute" ) addVariantToChannel( - id: Int! - channels_id: Int! - warehouses_id: Int! + id: ID! + channels_id: ID! + warehouses_id: ID! input: VariantChannelInput! ): Variant @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@addToChannel" ) - removeVariantChannel(id: Int!, channels_id: Int!): Variant + removeVariantChannel( + id: ID! + channels_id: ID! + warehouses_id: ID! + ): Variant @field( resolver: "App\\GraphQL\\Inventory\\Mutations\\Variants\\Variants@removeChannel" ) diff --git a/src/Inventory/Importer/Actions/ProductImporterAction.php b/src/Inventory/Importer/Actions/ProductImporterAction.php index 8b11aec8e..da4e73586 100644 --- a/src/Inventory/Importer/Actions/ProductImporterAction.php +++ b/src/Inventory/Importer/Actions/ProductImporterAction.php @@ -36,6 +36,7 @@ use Kanvas\Inventory\Variants\Models\Variants as VariantsModel; use Kanvas\Inventory\Warehouses\Actions\CreateWarehouseAction; use Kanvas\Inventory\Warehouses\DataTransferObject\Warehouses; +use Kanvas\Inventory\Variants\Models\VariantsWarehouses as ModelsVariantsWarehouses; use Throwable; class ProductImporterAction @@ -377,10 +378,14 @@ public function addVariantsToLocation(VariantsModel $variantModel): void ]), ))->execute(); + $variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variantModel->getId()) + ->where('warehouses_id', $warehouse->getId()) + ->firstOrFail(); + + (new AddVariantToChannel( - $variantModel, + $variantWarehouses, $channel, - $warehouse, $variantChannel ))->execute(); } diff --git a/src/Inventory/Variants/Actions/AddVariantToChannel.php b/src/Inventory/Variants/Actions/AddVariantToChannel.php index b20d61648..a309ea497 100644 --- a/src/Inventory/Variants/Actions/AddVariantToChannel.php +++ b/src/Inventory/Variants/Actions/AddVariantToChannel.php @@ -7,39 +7,36 @@ use Kanvas\Inventory\Channels\Models\Channels; use Kanvas\Inventory\Variants\DataTransferObject\VariantChannel; use Kanvas\Inventory\Variants\Models\Variants; -use Kanvas\Inventory\Warehouses\Models\Warehouses; +use Kanvas\Inventory\Variants\Models\VariantsWarehouses; class AddVariantToChannel { public function __construct( - protected Variants $variants, + protected VariantsWarehouses $variantsWarehouses, protected Channels $channel, - protected Warehouses $warehouse, protected VariantChannel $variantChannel ) { } public function execute(): Variants { - if ($this->variants->channels()->find($this->channel->getId())) { - $this->variants->channels()->syncWithoutDetaching([ + if ($this->variantsWarehouses->channels()->find($this->channel->getId())) { + $this->variantsWarehouses->channels()->syncWithoutDetaching([ $this->channel->getId() => [ - 'warehouses_id' => $this->warehouse->getId(), 'price' => $this->variantChannel->price, 'discounted_price' => $this->variantChannel->discounted_price, 'is_published' => $this->variantChannel->is_published, ] ]); } else { - $this->variants->channels()->attach([ + $this->variantsWarehouses->channels()->attach([ $this->channel->getId() => [ - 'warehouses_id' => $this->warehouse->getId(), 'price' => $this->variantChannel->price, 'discounted_price' => $this->variantChannel->discounted_price, 'is_published' => $this->variantChannel->is_published, ] ]); } - return $this->variants; + return $this->variantsWarehouses->variant; } } diff --git a/src/Inventory/Variants/DataTransferObject/Variants.php b/src/Inventory/Variants/DataTransferObject/Variants.php index e9eb9c206..6308f919a 100644 --- a/src/Inventory/Variants/DataTransferObject/Variants.php +++ b/src/Inventory/Variants/DataTransferObject/Variants.php @@ -14,6 +14,7 @@ public function __construct( public Products $product, public string $name, public string $description, + public int $warehouse_id, public ?string $short_description = null, public ?string $html_description = null, public ?string $sku = null, @@ -31,6 +32,7 @@ public static function viaRequest(array $request): self ProductsRepository::getById($request['products_id'], auth()->user()->getCurrentCompany()), $request['name'], $request['description'] ?? '', + $request['warehouse_id'], $request['short_description'] ?? null, $request['html_description'] ?? null, $request['sku'] ?? null, diff --git a/src/Inventory/Variants/Models/Variants.php b/src/Inventory/Variants/Models/Variants.php index a2a90602e..5393eafc0 100644 --- a/src/Inventory/Variants/Models/Variants.php +++ b/src/Inventory/Variants/Models/Variants.php @@ -9,9 +9,9 @@ use Baka\Users\Contracts\UserInterface; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; use Kanvas\Apps\Models\Apps; use Kanvas\Inventory\Attributes\Models\Attributes; -use Kanvas\Inventory\Channels\Models\Channels; use Kanvas\Inventory\Attributes\DataTransferObject\Attributes as AttributesDto; use Kanvas\Inventory\Attributes\Actions\CreateAttribute; use Kanvas\Inventory\Variants\Actions\AddAttributeAction; @@ -92,33 +92,22 @@ public function product(): BelongsTo return $this->belongsTo(Products::class, 'products_id'); } + public function variantWarehouses(): HasMany + { + return $this->hasMany(VariantsWarehouses::class, 'products_variants_id'); + } + /** - * The warehouses that belong to the Variants. + * warehouses. */ public function warehouses(): BelongsToMany { return $this->belongsToMany( Warehouses::class, - 'products_variants_warehouses', + VariantsWarehouses::class, 'products_variants_id', 'warehouses_id' - ) - ->withPivot( - 'quantity', - 'price', - 'sku', - 'position', - 'serial_number', - 'is_oversellable', - 'is_default', - 'is_default', - 'is_best_seller', - 'is_on_sale', - 'is_on_promo', - 'can_pre_order', - 'is_new', - 'is_published' - ); + ); } /** @@ -135,25 +124,6 @@ public function attributes(): BelongsToMany ->withPivot('value'); } - /** - * channels. - */ - public function channels(): BelongsToMany - { - return $this->belongsToMany( - Channels::class, - VariantsChannels::class, - 'products_variants_id', - 'channels_id' - ) - ->withPivot( - 'price', - 'discounted_price', - 'is_published', - 'warehouses_id' - ); - } - /** * Add/create new attributes from a variant. * diff --git a/src/Inventory/Variants/Models/VariantsChannels.php b/src/Inventory/Variants/Models/VariantsChannels.php index a60f724c1..515160f2f 100644 --- a/src/Inventory/Variants/Models/VariantsChannels.php +++ b/src/Inventory/Variants/Models/VariantsChannels.php @@ -5,6 +5,8 @@ namespace Kanvas\Inventory\Variants\Models; use Baka\Traits\HasCompositePrimaryKeyTrait; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Kanvas\Inventory\Channels\Models\Channels; use Kanvas\Inventory\Models\BaseModel; /** @@ -25,12 +27,16 @@ class VariantsChannels extends BaseModel protected $table = 'products_variants_channels'; protected $guarded = [ - 'products_variants_id', + 'product_variants_warehouse_id', 'channels_id', - 'warehouses_id', 'price', 'discount_price' ]; - protected $primaryKey = ['products_variants_id', 'channels_id', 'warehouses_id']; + protected $primaryKey = ['product_variants_warehouse_id', 'channels_id']; + + public function channel(): BelongsTo + { + return $this->belongsTo(Channels::class, 'channels_id'); + } } diff --git a/src/Inventory/Variants/Models/VariantsWarehouses.php b/src/Inventory/Variants/Models/VariantsWarehouses.php index 8e022f728..20eea266e 100644 --- a/src/Inventory/Variants/Models/VariantsWarehouses.php +++ b/src/Inventory/Variants/Models/VariantsWarehouses.php @@ -4,8 +4,12 @@ namespace Kanvas\Inventory\Variants\Models; -use Baka\Traits\HasCompositePrimaryKeyTrait; +use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Kanvas\Inventory\Channels\Models\Channels; use Kanvas\Inventory\Models\BaseModel; +use Kanvas\Inventory\Warehouses\Models\Warehouses; /** * Class Variants Warehouse. @@ -33,28 +37,34 @@ */ class VariantsWarehouses extends BaseModel { - use HasCompositePrimaryKeyTrait; - protected $table = 'products_variants_warehouses'; - protected $guarded = [ - 'products_variants_id', - 'warehouses_id', - 'quantity', - 'price', - 'sku', - 'position', - 'serial_number', - 'is_default', - 'is_oversellable', - 'is_default', - 'is_best_seller', - 'is_on_sale', - 'is_on_promo', - 'can_pre_order', - 'is_coming_soon', - 'is_new', - 'is_published' - ]; + protected $guarded = []; + + /** + * channels. + */ + public function channels(): BelongsToMany + { + return $this->belongsToMany( + Channels::class, + VariantsChannels::class, + 'product_variants_warehouse_id', + 'channels_id' + ) + ->withPivot( + 'price', + 'discounted_price', + 'is_published' + ); + } + + public function variant(): BelongsTo + { + return $this->belongsTo(Variants::class, 'products_variants_id'); + } - protected $primaryKey = ['products_variants_id', 'warehouses_id']; + public function warehouse(): BelongsTo + { + return $this->belongsTo(Warehouses::class, 'warehouses_id'); + } } diff --git a/src/Inventory/Variants/Services/Variants.php b/src/Inventory/Variants/Services/Variants.php index b7d00dcb9..a3259233b 100644 --- a/src/Inventory/Variants/Services/Variants.php +++ b/src/Inventory/Variants/Services/Variants.php @@ -8,6 +8,7 @@ use Kanvas\Inventory\Products\Models\Products; use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantsDto; use Kanvas\Inventory\Variants\Actions\CreateVariantsAction; +use Kanvas\Inventory\Warehouses\Repositories\WarehouseRepository; class Variants { @@ -34,6 +35,8 @@ public static function createVariantsFromArray(Products $product, array $variant $variantModel->addAttributes($user, $variant['attributes']); } + WarehouseRepository::getById($variantDto->warehouse_id, $variantDto->product->company()->get()->first()); + $variantModel->warehouses()->attach($variantDto->warehouse_id); $variantsData[] = $variantModel; } diff --git a/tests/GraphQL/Inventory/ProductsTest.php b/tests/GraphQL/Inventory/ProductsTest.php index e1996a882..c6de08a34 100644 --- a/tests/GraphQL/Inventory/ProductsTest.php +++ b/tests/GraphQL/Inventory/ProductsTest.php @@ -10,8 +10,6 @@ class ProductsTest extends TestCase { /** * testSave. - * - * @return void */ public function testSave(): void { @@ -28,14 +26,12 @@ public function testSave(): void description } }', ['data' => $data])->assertJson([ - 'data' => ['createProduct' => $data] + 'data' => ['createProduct' => $data], ]); } /** * test get product. - * - * @return void */ public function testGetProduct(): void { @@ -52,7 +48,7 @@ public function testGetProduct(): void description } }', ['data' => $data])->assertJson([ - 'data' => ['createProduct' => $data] + 'data' => ['createProduct' => $data], ]); $this->graphQL(' @@ -64,14 +60,12 @@ public function testGetProduct(): void } } }')->assertJson([ - 'data' => ['products' => ['data' => [$data]]] + 'data' => ['products' => ['data' => [$data]]], ]); } /** * test update product. - * - * @return void */ public function testUpdateProduct(): void { @@ -87,7 +81,7 @@ public function testUpdateProduct(): void description } }', ['data' => $data])->assertJson([ - 'data' => ['createProduct' => $data] + 'data' => ['createProduct' => $data], ]); $response = $this->graphQL(' @@ -118,14 +112,12 @@ public function testUpdateProduct(): void description } }', ['data' => $data, 'id' => $id])->assertJson([ - 'data' => ['updateProduct' => $data] + 'data' => ['updateProduct' => $data], ]); } /** * testDeleteProduct. - * - * @return void */ public function testDeleteProduct(): void { @@ -144,24 +136,70 @@ public function testDeleteProduct(): void }', ['data' => $data]); $response->assertJson([ - 'data' => ['createProduct' => $data] + 'data' => ['createProduct' => $data], ]); $id = $response->json()['data']['createProduct']['id']; $this->graphQL(' mutation($id: Int!) { deleteProduct(id: $id) }', ['id' => $id])->assertJson([ - 'data' => ['deleteProduct' => true] + 'data' => ['deleteProduct' => true], ]); } /** * testAddVariantToProduct. - * - * @return void */ public function testAddVariantToProduct(): void { + $region = [ + 'name' => fake()->name, + 'short_slug' => fake()->name, + 'is_default' => 1, + 'currency_id' => 1, + ]; + $regionResponse = $this->graphQL(' + mutation($data: RegionInput!) { + createRegion(input: $data) + { + id + name + slug + short_slug + currency_id + is_default + } + } + ', [ + 'data' => $region, + ])->assertJson([ + 'data' => ['createRegion' => $region], + ]); + $regionResponse = $regionResponse->decodeResponseJson(); + + $warehouseData = [ + 'regions_id' => $regionResponse['data']['createRegion']['id'], + 'name' => fake()->name, + 'location' => 'Test Location', + 'is_default' => false, + 'is_published' => 1, + ]; + + $warehouseResponse = $this->graphQL(' + mutation($data: WarehouseInput!) { + createWarehouse(input: $data) + { + id + regions_id + name + location + is_default + is_published + } + }', ['data' => $warehouseData])->assertJson([ + 'data' => ['createWarehouse' => $warehouseData], + ]); + $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -174,7 +212,7 @@ public function testAddVariantToProduct(): void description } }', ['data' => $data])->assertJson([ - 'data' => ['createProduct' => $data] + 'data' => ['createProduct' => $data], ]); $response = $this->graphQL(' @@ -196,28 +234,76 @@ public function testAddVariantToProduct(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $id + 'products_id' => $id, + 'warehouse_id' => $warehouseResponse['data']['createWarehouse']['id'], ]; - $this->graphQL(' + $variantResponse = $this->graphQL(' mutation($data: VariantsInput!) { createVariant(input: $data) { + id name description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); + }', ['data' => $data]); + $this->assertArrayHasKey('id', $variantResponse->json()['data']['createVariant']); } /** * testDeleteVariantToProduct. - * - * @return void */ public function testDeleteVariantToProduct(): void { + $region = [ + 'name' => 'Test Region', + 'slug' => 'test-region', + 'short_slug' => 'test-region', + 'is_default' => 1, + 'currency_id' => 1, + ]; + $regionResponse = $this->graphQL(' + mutation($data: RegionInput!) { + createRegion(input: $data) + { + id + name + slug + short_slug + currency_id + is_default + } + } + ', [ + 'data' => $region, + ])->assertJson([ + 'data' => ['createRegion' => $region], + ]); + $regionResponse = $regionResponse->decodeResponseJson(); + + $warehouseData = [ + 'regions_id' => $regionResponse['data']['createRegion']['id'], + 'name' => fake()->name, + 'location' => 'Test Location', + 'is_default' => false, + 'is_published' => 1, + ]; + + $warehouseResponse = $this->graphQL(' + mutation($data: WarehouseInput!) { + createWarehouse(input: $data) + { + id + regions_id + name + location + is_default + is_published + } + }', ['data' => $warehouseData])->assertJson([ + 'data' => ['createWarehouse' => $warehouseData], + ]); + $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -230,8 +316,9 @@ public function testDeleteVariantToProduct(): void description } }', ['data' => $data])->assertJson([ - 'data' => ['createProduct' => $data] + 'data' => ['createProduct' => $data], ]); + $response = $this->graphQL(' query { products { @@ -242,13 +329,19 @@ public function testDeleteVariantToProduct(): void } } }'); + $this->assertArrayHasKey('data', $response->json()); + $this->assertArrayHasKey('products', $response->json()['data']); + $this->assertArrayHasKey('data', $response->json()['data']['products']); + $this->assertArrayHasKey('id', $response->json()['data']['products']['data'][0]); + $id = $response->json()['data']['products']['data'][0]['id']; $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $id + 'products_id' => $id, + 'warehouse_id' => $warehouseResponse['data']['createWarehouse']['id'], ]; - $response = $this->graphQL(' + $variantResponse = $this->graphQL(' mutation($data: VariantsInput!) { createVariant(input: $data) { @@ -258,12 +351,14 @@ public function testDeleteVariantToProduct(): void products_id } }', ['data' => $data]); - $id = $response->json()['data']['createVariant']['id']; + $this->assertArrayHasKey('id', $variantResponse->json()['data']['createVariant']); + + $variantResponseId = $variantResponse->json()['data']['createVariant']['id']; $this->graphQL(' - mutation($id: Int!) { + mutation($id: ID!) { deleteVariant(id: $id) - }', ['id' => $id])->assertJson([ - 'data' => ['deleteVariant' => true] + }', ['id' => $variantResponseId])->assertJson([ + 'data' => ['deleteVariant' => true], ]); } } diff --git a/tests/GraphQL/Inventory/RemoveVariantsToWarehouseTest.php b/tests/GraphQL/Inventory/RemoveVariantsToWarehouseTest.php index 6c1c92a2a..6f3abd1e1 100644 --- a/tests/GraphQL/Inventory/RemoveVariantsToWarehouseTest.php +++ b/tests/GraphQL/Inventory/RemoveVariantsToWarehouseTest.php @@ -80,7 +80,8 @@ public function testRemoveVariantToWarehouse(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $productId + 'products_id' => $productId, + 'warehouse_id' => $warehouseId ]; $response = $this->graphQL(' mutation($data: VariantsInput!) { @@ -91,24 +92,28 @@ public function testRemoveVariantToWarehouse(): void description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); + }', ['data' => $data]); $variantId = $response->json()['data']['createVariant']['id']; - $data = [ - 'price' => 10.00, - 'quantity' => 1, - 'position' => 1, + + $data = [ + 'price' => rand(1, 1000), + 'quantity' => rand(1, 5), + 'position' => rand(1, 4), ]; - $response = $this->graphQL(' - mutation($data: VariantsWarehousesInput! $id: Int! $warehouse_id: Int!) { + $warehouseResponse = $this->graphQL(' + mutation($data: VariantsWarehousesInput! $id: ID! $warehouse_id: Int!) { addVariantToWarehouse(input: $data id: $id warehouse_id: $warehouse_id) { id name description products_id + warehouses{ + warehouseinfo{ + id + } + } } }', [ 'data' => $data, diff --git a/tests/GraphQL/Inventory/VariantAttributeTest.php b/tests/GraphQL/Inventory/VariantAttributeTest.php index 326b8e778..ae76a6320 100644 --- a/tests/GraphQL/Inventory/VariantAttributeTest.php +++ b/tests/GraphQL/Inventory/VariantAttributeTest.php @@ -15,6 +15,52 @@ class VariantAttributeTest extends TestCase */ public function testAddAttributeToVariant(): void { + $dataRegion = [ + 'name' => 'Test Region', + 'slug' => 'test-region', + 'short_slug' => 'test-region', + 'is_default' => 1, + 'currency_id' => 1, + ]; + $response = $this->graphQL(' + mutation($data: RegionInput!) { + createRegion(input: $data) + { + id + name + slug + short_slug + currency_id + is_default + } + }', ['data' => $dataRegion]) + ->assertJson([ + 'data' => ['createRegion' => $dataRegion] + ]); + $idRegion = $response->json()['data']['createRegion']['id']; + $data = [ + 'regions_id' => $idRegion, + 'name' => 'Test Warehouse', + 'location' => 'Test Location', + 'is_default' => false, + 'is_published' => 1, + ]; + + $response = $this->graphQL(' + mutation($data: WarehouseInput!) { + createWarehouse(input: $data) + { + id + regions_id + name + location + is_default + is_published + } + }', ['data' => $data])->assertJson([ + 'data' => ['createWarehouse' => $data] + ]); + $warehouseId = $response->json()['data']['createWarehouse']['id']; $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -34,7 +80,8 @@ public function testAddAttributeToVariant(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $productId + 'products_id' => $productId, + 'warehouse_id' => $warehouseId ]; $response = $this->graphQL(' mutation($data: VariantsInput!) { @@ -45,9 +92,7 @@ public function testAddAttributeToVariant(): void description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); + }', ['data' => $data]); $variantId = $response->json()['data']['createVariant']['id']; $dataAtribute = [ @@ -67,7 +112,7 @@ public function testAddAttributeToVariant(): void }', ['data' => $dataAtribute]); $attributeId = $response->json()['data']['createAttribute']['id']; $response = $this->graphQL(' - mutation($id: Int! $attributes_id: Int! $input: VariantsAttributesInput!) { + mutation($id: ID! $attributes_id: ID! $input: VariantsAttributesInput!) { addAttributeToVariant(id: $id, attributes_id: $attributes_id, input: $input) { id @@ -92,6 +137,52 @@ public function testAddAttributeToVariant(): void */ public function testRemoveAttributeFromVariant(): void { + $dataRegion = [ + 'name' => 'Test Region', + 'slug' => 'test-region', + 'short_slug' => 'test-region', + 'is_default' => 1, + 'currency_id' => 1, + ]; + $response = $this->graphQL(' + mutation($data: RegionInput!) { + createRegion(input: $data) + { + id + name + slug + short_slug + currency_id + is_default + } + }', ['data' => $dataRegion]) + ->assertJson([ + 'data' => ['createRegion' => $dataRegion] + ]); + $idRegion = $response->json()['data']['createRegion']['id']; + $data = [ + 'regions_id' => $idRegion, + 'name' => 'Test Warehouse', + 'location' => 'Test Location', + 'is_default' => false, + 'is_published' => 1, + ]; + + $response = $this->graphQL(' + mutation($data: WarehouseInput!) { + createWarehouse(input: $data) + { + id + regions_id + name + location + is_default + is_published + } + }', ['data' => $data])->assertJson([ + 'data' => ['createWarehouse' => $data] + ]); + $warehouseId = $response->json()['data']['createWarehouse']['id']; $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -111,7 +202,8 @@ public function testRemoveAttributeFromVariant(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $productId + 'products_id' => $productId, + 'warehouse_id' => $warehouseId ]; $response = $this->graphQL(' mutation($data: VariantsInput!) { @@ -122,11 +214,26 @@ public function testRemoveAttributeFromVariant(): void description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); + }', ['data' => $data]); $variantId = $response->json()['data']['createVariant']['id']; + $dataAtribute = [ + 'name' => fake()->name, + 'value' => fake()->name + ]; + $response = $this->graphQL(' + mutation($data: AttributeInput!) { + createAttribute(input: $data) + { + id + name + values { + value + } + } + }', ['data' => $dataAtribute]); + $attributeId = $response->json()['data']['createAttribute']['id']; + $dataAtribute = [ 'name' => fake()->name, 'value' => fake()->name @@ -141,7 +248,7 @@ public function testRemoveAttributeFromVariant(): void }', ['data' => $dataAtribute]); $attributeId = $response->json()['data']['createAttribute']['id']; $response = $this->graphQL(' - mutation($id: Int! $attributes_id: Int! $input: VariantsAttributesInput!) { + mutation($id: ID! $attributes_id: ID! $input: VariantsAttributesInput!) { addAttributeToVariant(id: $id, attributes_id: $attributes_id, input: $input) { id @@ -158,7 +265,7 @@ public function testRemoveAttributeFromVariant(): void ]); $this->assertArrayHasKey('data', $response->json()); $response = $this->graphQL(' - mutation($id: Int! $attributesId: Int!) { + mutation($id: ID! $attributesId: ID!) { removeAttributeToVariant(id:$id attributes_id:$attributesId) { id diff --git a/tests/GraphQL/Inventory/VariantTest.php b/tests/GraphQL/Inventory/VariantTest.php index 08e99d683..85c4f8687 100644 --- a/tests/GraphQL/Inventory/VariantTest.php +++ b/tests/GraphQL/Inventory/VariantTest.php @@ -15,6 +15,55 @@ class VariantTest extends TestCase */ public function testUpdateVariant(): void { + $region = [ + 'name' => 'Test Region', + 'slug' => 'test-region', + 'short_slug' => 'test-region', + 'is_default' => 1, + 'currency_id' => 1, + ]; + $regionResponse = $this->graphQL(' + mutation($data: RegionInput!) { + createRegion(input: $data) + { + id + name + slug + short_slug + currency_id + is_default + } + } + ', [ + 'data' => $region + ])->assertJson([ + 'data' => ['createRegion' => $region] + ]); + $regionResponse = $regionResponse->decodeResponseJson(); + + $warehouseData = [ + 'regions_id' => $regionResponse['data']['createRegion']['id'], + 'name' => fake()->name, + 'location' => 'Test Location', + 'is_default' => false, + 'is_published' => 1, + ]; + + $warehouseResponse = $this->graphQL(' + mutation($data: WarehouseInput!) { + createWarehouse(input: $data) + { + id + regions_id + name + location + is_default + is_published + } + }', ['data' => $warehouseData])->assertJson([ + 'data' => ['createWarehouse' => $warehouseData] + ]); + $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -49,27 +98,28 @@ public function testUpdateVariant(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $id + 'products_id' => $id, + 'warehouse_id' => $warehouseResponse['data']['createWarehouse']['id'] ]; - $response = $this->graphQL(' + $variantResponse = $this->graphQL(' mutation($data: VariantsInput!) { createVariant(input: $data) - { + { id name description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); - $id = $response->json()['data']['createVariant']['id']; + }', ['data' => $data]); + $this->assertArrayHasKey('id', $variantResponse->json()['data']['createVariant']); + + $id = $variantResponse->json()['data']['createVariant']['id']; $data = [ 'name' => fake()->name, 'description' => fake()->text, ]; $this->graphQL(' - mutation($id: Int! $data: VariantsUpdateInput!) { + mutation($id: ID! $data: VariantsUpdateInput!) { updateVariant(id: $id, input: $data) { id @@ -153,7 +203,8 @@ public function testAddVariantToWarehouse(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $productId + 'products_id' => $productId, + 'warehouse_id' => $warehouseId ]; $response = $this->graphQL(' mutation($data: VariantsInput!) { @@ -164,30 +215,37 @@ public function testAddVariantToWarehouse(): void description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); + }', ['data' => $data]); $variantId = $response->json()['data']['createVariant']['id']; - $data = [ + + $data = [ 'price' => rand(1, 1000), 'quantity' => rand(1, 5), 'position' => rand(1, 4), ]; - $this->graphQL(' - mutation($data: VariantsWarehousesInput! $id: Int! $warehouse_id: Int!) { + $warehouseResponse = $this->graphQL(' + mutation($data: VariantsWarehousesInput! $id: ID! $warehouse_id: Int!) { addVariantToWarehouse(input: $data id: $id warehouse_id: $warehouse_id) { id name description products_id + warehouses{ + warehouseinfo{ + id + } + } } }', [ 'data' => $data, 'id' => $variantId, 'warehouse_id' => $warehouseId ]); - $this->assertArrayHasKey('data', $response->json()); + $this->assertEquals( + $warehouseId, + $warehouseResponse['data']['addVariantToWarehouse']['warehouses'][0]['warehouseinfo']['id'] + ); } } diff --git a/tests/GraphQL/Inventory/VariantsChannelsTest.php b/tests/GraphQL/Inventory/VariantsChannelsTest.php index a93d87093..f6b8ad773 100644 --- a/tests/GraphQL/Inventory/VariantsChannelsTest.php +++ b/tests/GraphQL/Inventory/VariantsChannelsTest.php @@ -15,7 +15,7 @@ class VariantsChannelsTest extends TestCase */ public function testVariantToChannel(): void { - $data = [ + $dataRegion = [ 'name' => 'Test Region', 'slug' => 'test-region', 'short_slug' => 'test-region', @@ -33,17 +33,13 @@ public function testVariantToChannel(): void currency_id is_default } - } - ', [ - 'data' => $data - ])->assertJson([ - 'data' => ['createRegion' => $data] - ]); - - $response = $response->decodeResponseJson(); - + }', ['data' => $dataRegion]) + ->assertJson([ + 'data' => ['createRegion' => $dataRegion] + ]); + $idRegion = $response->json()['data']['createRegion']['id']; $data = [ - 'regions_id' => $response['data']['createRegion']['id'], + 'regions_id' => $idRegion, 'name' => 'Test Warehouse', 'location' => 'Test Location', 'is_default' => false, @@ -64,7 +60,7 @@ public function testVariantToChannel(): void }', ['data' => $data])->assertJson([ 'data' => ['createWarehouse' => $data] ]); - $warehousesId = $response->json()['data']['createWarehouse']['id']; + $warehouseId = $response->json()['data']['createWarehouse']['id']; $data = [ 'name' => fake()->name, 'description' => fake()->text, @@ -84,7 +80,8 @@ public function testVariantToChannel(): void $data = [ 'name' => fake()->name, 'description' => fake()->text, - 'products_id' => $productId + 'products_id' => $productId, + 'warehouse_id' => $warehouseId ]; $response = $this->graphQL(' mutation($data: VariantsInput!) { @@ -95,9 +92,7 @@ public function testVariantToChannel(): void description products_id } - }', ['data' => $data])->assertJson([ - 'data' => ['createVariant' => $data] - ]); + }', ['data' => $data]); $variantId = $response->json()['data']['createVariant']['id']; $dataChannel = [ @@ -122,7 +117,7 @@ public function testVariantToChannel(): void $channelId = $response->json()['data']['createChannel']['id']; $response = $this->graphQL( ' - mutation addVariantToChannel($id: Int! $channels_id: Int! $warehouses_id: Int! $input: VariantChannelInput!){ + mutation addVariantToChannel($id: ID! $channels_id: ID! $warehouses_id: ID! $input: VariantChannelInput!){ addVariantToChannel(id: $id channels_id:$channels_id warehouses_id:$warehouses_id input:$input){ id } @@ -131,7 +126,7 @@ public function testVariantToChannel(): void [ 'id' => $variantId, 'channels_id' => $channelId, - 'warehouses_id' => $warehousesId, + 'warehouses_id' => $warehouseId, 'input' => [ 'price' => 100, 'discounted_price' => 10, @@ -145,15 +140,16 @@ public function testVariantToChannel(): void ]); $response = $this->graphQL( - 'mutation ($id: Int! $channels_id: Int!) { - removeVariantChannel(id: $id channels_id: $channels_id) + 'mutation ($id: ID! $channels_id: ID! $warehouses_id: ID!) { + removeVariantChannel(id: $id channels_id: $channels_id warehouses_id: $warehouses_id) { id } }', [ 'id' => $variantId, - 'channels_id' => $channelId + 'channels_id' => $channelId, + 'warehouses_id' => $warehouseId, ] ); $response->assertJson([ diff --git a/tests/Inventory/Integration/ImporterTest.php b/tests/Inventory/Integration/ImporterTest.php index 4420a8c93..7a2309188 100644 --- a/tests/Inventory/Integration/ImporterTest.php +++ b/tests/Inventory/Integration/ImporterTest.php @@ -9,6 +9,8 @@ use Kanvas\Inventory\Importer\DataTransferObjects\ProductImporter; use Kanvas\Inventory\Regions\Repositories\RegionRepository; use Kanvas\Inventory\Support\Setup; +use Kanvas\Inventory\Warehouses\Actions\CreateWarehouseAction; +use Kanvas\Inventory\Warehouses\DataTransferObject\Warehouses as WarehousesDto; use Tests\TestCase; final class ImporterTest extends TestCase @@ -37,6 +39,20 @@ public function testImportAction(): void ], ]; + $region = RegionRepository::getByName('default', $company); + + $warehouse = [ + 'name' => fake()->word(), + 'regions_id' => $region->getId(), + 'is_default' => true, + 'is_published' => 1, + ]; + + $warehouseData = (new CreateWarehouseAction( + WarehousesDto::viaRequest($warehouse), + auth()->user() + ))->execute(); + $productData = ProductImporter::from([ 'name' => fake()->word(), 'description' => fake()->sentence(), @@ -58,6 +74,7 @@ public function testImportAction(): void 'variants' => [ [ 'name' => fake()->word(), + 'warehouse_id' => $warehouseData->getId(), 'description' => fake()->sentence(), 'sku' => fake()->word(), 'price' => fake()->randomNumber(2), @@ -77,6 +94,7 @@ public function testImportAction(): void ], [ 'name' => fake()->word(), + 'warehouse_id' => $warehouseData->getId(), 'description' => fake()->sentence(), 'sku' => fake()->word(), 'price' => fake()->randomNumber(2), @@ -99,7 +117,7 @@ public function testImportAction(): void $productData, $company, auth()->user(), - RegionRepository::getByName('default', $company) + $region ); $this->assertTrue($productImporter->execute());