Skip to content

Commit

Permalink
Merge pull request #949 from bakaphp/warehouse-type-update
Browse files Browse the repository at this point in the history
Warehouses type update
  • Loading branch information
kaioken authored Feb 16, 2024
2 parents a0ee320 + e44377c commit f738ca3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
7 changes: 4 additions & 3 deletions graphql/schemas/Inventory/warehouse.graphql
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
type Warehouse {
id: Int!
companies: Company!
apps: App!
regions: Region!
apps_id: Int!
regions_id: Int!
companies_id: Int!
company: Company!
uuid: String!
name: String!
location: String
is_default: Boolean!
is_published: Boolean!
total_products: Int!
@method(name: "getTotalProducts")
}

input WarehouseInput {
Expand Down Expand Up @@ -65,7 +66,7 @@ extend type Query @guard {
getWarehouses(
where: _
@whereConditions(
columns: ["name", "location", "is_default", "is_published"]
columns: ["id", "name", "location", "is_default", "is_published"]
)
): [Warehouse]!
@paginate(
Expand Down
35 changes: 21 additions & 14 deletions src/Domains/Inventory/Warehouses/Models/Warehouses.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Baka\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kanvas\Apps\Models\Apps;
use Kanvas\Companies\Models\Companies;
use Kanvas\Inventory\Models\BaseModel;
use Kanvas\Inventory\Regions\Models\Regions;
use Kanvas\Inventory\Traits\DefaultTrait;
use Kanvas\Inventory\Variants\Models\VariantsWarehouses;

/**
* Class Warehouses.
Expand Down Expand Up @@ -39,30 +41,21 @@ class Warehouses extends BaseModel

protected $guarded = [];

/**
* Get the companies that owns the Warehouses.
*/
public function companies(): BelongsTo
{
return $this->belongsTo(Companies::class, 'companies_id');
}

/**
*
*/
public function apps(): BelongsTo
{
return $this->belongsTo(Apps::class, 'apps_id');
}

/**
*
*/
public function regions(): BelongsTo
{
return $this->belongsTo(Regions::class, 'regions_id');
}

public function variantsWarehouses(): HasMany
{
return $this->hasMany(VariantsWarehouses::class, 'warehouses_id');
}

/**
* quantityAttribute.
*/
Expand Down Expand Up @@ -172,4 +165,18 @@ public function isVariantPublished(): ?Attribute
get: fn () => $this->pivot->is_published
);
}

/**
* Get the total amount of products of a warehouse.
*
* @return Int
*/
public function getTotalProducts(): int
{
if (! $totalProducts = $this->get('total_products')) {
$this->set('total_products', $this->variantsWarehouses()->count());
return $this->get('total_products');
}
return $totalProducts;
}
}

0 comments on commit f738ca3

Please sign in to comment.