add: full multi-tenancy control
This commit is contained in:
5
packages/Webkul/Warehouse/src/Contracts/Location.php
Normal file
5
packages/Webkul/Warehouse/src/Contracts/Location.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Contracts;
|
||||
|
||||
interface Location {}
|
||||
5
packages/Webkul/Warehouse/src/Contracts/Warehouse.php
Normal file
5
packages/Webkul/Warehouse/src/Contracts/Warehouse.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Contracts;
|
||||
|
||||
interface Warehouse {}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('warehouses', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
$table->text('description')->nullable();
|
||||
$table->string('contact_name');
|
||||
$table->json('contact_emails');
|
||||
$table->json('contact_numbers');
|
||||
$table->json('contact_address');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('warehouses');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('warehouse_locations', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('name');
|
||||
|
||||
$table->integer('warehouse_id')->unsigned();
|
||||
$table->foreign('warehouse_id')->references('id')->on('warehouses')->onDelete('cascade');
|
||||
|
||||
$table->unique(['warehouse_id', 'name']);
|
||||
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('warehouse_locations');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('warehouse_activities', function (Blueprint $table) {
|
||||
$table->integer('activity_id')->unsigned();
|
||||
$table->foreign('activity_id')->references('id')->on('activities')->onDelete('cascade');
|
||||
|
||||
$table->integer('warehouse_id')->unsigned();
|
||||
$table->foreign('warehouse_id')->references('id')->on('warehouses')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('warehouse_activities');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('warehouse_tags', function (Blueprint $table) {
|
||||
$table->integer('tag_id')->unsigned();
|
||||
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
|
||||
|
||||
$table->integer('warehouse_id')->unsigned();
|
||||
$table->foreign('warehouse_id')->references('id')->on('warehouses')->onDelete('cascade');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('warehouse_tags');
|
||||
}
|
||||
};
|
||||
32
packages/Webkul/Warehouse/src/Models/Location.php
Executable file
32
packages/Webkul/Warehouse/src/Models/Location.php
Executable file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Webkul\Warehouse\Contracts\Location as LocationContract;
|
||||
|
||||
class Location extends Model implements LocationContract
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected $table = 'warehouse_locations';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'warehouse_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the warehouse that owns the location.
|
||||
*/
|
||||
public function warehouse()
|
||||
{
|
||||
return $this->belongsTo(WarehouseProxy::modelClass());
|
||||
}
|
||||
}
|
||||
7
packages/Webkul/Warehouse/src/Models/LocationProxy.php
Normal file
7
packages/Webkul/Warehouse/src/Models/LocationProxy.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Models;
|
||||
|
||||
use Konekt\Concord\Proxies\ModelProxy;
|
||||
|
||||
class LocationProxy extends ModelProxy {}
|
||||
64
packages/Webkul/Warehouse/src/Models/Warehouse.php
Executable file
64
packages/Webkul/Warehouse/src/Models/Warehouse.php
Executable file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Webkul\Activity\Models\ActivityProxy;
|
||||
use Webkul\Activity\Traits\LogsActivity;
|
||||
use Webkul\Attribute\Traits\CustomAttribute;
|
||||
use Webkul\Tag\Models\TagProxy;
|
||||
use Webkul\Warehouse\Contracts\Warehouse as WarehouseContract;
|
||||
|
||||
class Warehouse extends Model implements WarehouseContract
|
||||
{
|
||||
use CustomAttribute, LogsActivity;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'description',
|
||||
'contact_name',
|
||||
'contact_emails',
|
||||
'contact_numbers',
|
||||
'contact_address',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that are castable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'contact_emails' => 'array',
|
||||
'contact_numbers' => 'array',
|
||||
'contact_address' => 'array',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the locations for the warehouse.
|
||||
*/
|
||||
public function locations()
|
||||
{
|
||||
return $this->hasMany(LocationProxy::modelClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags that belong to the lead.
|
||||
*/
|
||||
public function tags()
|
||||
{
|
||||
return $this->belongsToMany(TagProxy::modelClass(), 'warehouse_tags');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the activities.
|
||||
*/
|
||||
public function activities()
|
||||
{
|
||||
return $this->belongsToMany(ActivityProxy::modelClass(), 'warehouse_activities');
|
||||
}
|
||||
}
|
||||
7
packages/Webkul/Warehouse/src/Models/WarehouseProxy.php
Normal file
7
packages/Webkul/Warehouse/src/Models/WarehouseProxy.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Models;
|
||||
|
||||
use Konekt\Concord\Proxies\ModelProxy;
|
||||
|
||||
class WarehouseProxy extends ModelProxy {}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Providers;
|
||||
|
||||
use Webkul\Core\Providers\BaseModuleServiceProvider;
|
||||
|
||||
class ModuleServiceProvider extends BaseModuleServiceProvider
|
||||
{
|
||||
protected $models = [
|
||||
\Webkul\Warehouse\Models\Location::class,
|
||||
\Webkul\Warehouse\Models\Warehouse::class,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Providers;
|
||||
|
||||
use Illuminate\Routing\Router;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class WarehouseServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot(Router $router)
|
||||
{
|
||||
$this->loadMigrationsFrom(__DIR__.'/../Database/Migrations');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register() {}
|
||||
}
|
||||
26
packages/Webkul/Warehouse/src/Repositories/LocationRepository.php
Executable file
26
packages/Webkul/Warehouse/src/Repositories/LocationRepository.php
Executable file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Repositories;
|
||||
|
||||
use Webkul\Core\Eloquent\Repository;
|
||||
|
||||
class LocationRepository extends Repository
|
||||
{
|
||||
/**
|
||||
* Searchable fields
|
||||
*/
|
||||
protected $fieldSearchable = [
|
||||
'name',
|
||||
'warehouse_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Specify Model class name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function model()
|
||||
{
|
||||
return 'Webkul\Warehouse\Contracts\Location';
|
||||
}
|
||||
}
|
||||
101
packages/Webkul/Warehouse/src/Repositories/WarehouseRepository.php
Executable file
101
packages/Webkul/Warehouse/src/Repositories/WarehouseRepository.php
Executable file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Webkul\Warehouse\Repositories;
|
||||
|
||||
use Illuminate\Container\Container;
|
||||
use Webkul\Attribute\Repositories\AttributeRepository;
|
||||
use Webkul\Attribute\Repositories\AttributeValueRepository;
|
||||
use Webkul\Core\Eloquent\Repository;
|
||||
use Webkul\Warehouse\Contracts\Warehouse;
|
||||
|
||||
class WarehouseRepository extends Repository
|
||||
{
|
||||
/**
|
||||
* Searchable fields.
|
||||
*/
|
||||
protected $fieldSearchable = [
|
||||
'name',
|
||||
'contact_name',
|
||||
'contact_emails',
|
||||
'contact_numbers',
|
||||
'contact_address',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create a new repository instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
protected AttributeRepository $attributeRepository,
|
||||
protected AttributeValueRepository $attributeValueRepository,
|
||||
Container $container
|
||||
) {
|
||||
parent::__construct($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify model class name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function model()
|
||||
{
|
||||
return Warehouse::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create.
|
||||
*
|
||||
* @return \Webkul\Warehouse\Contracts\Warehouse
|
||||
*/
|
||||
public function create(array $data)
|
||||
{
|
||||
$warehouse = parent::create($data);
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $warehouse->id,
|
||||
]));
|
||||
|
||||
return $warehouse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update.
|
||||
*
|
||||
* @param int $id
|
||||
* @param array $attribute
|
||||
* @return \Webkul\Warehouse\Contracts\Warehouse
|
||||
*/
|
||||
public function update(array $data, $id, $attributes = [])
|
||||
{
|
||||
$warehouse = parent::update($data, $id);
|
||||
|
||||
/**
|
||||
* If attributes are provided then only save the provided attributes and return.
|
||||
*/
|
||||
if (! empty($attributes)) {
|
||||
$conditions = ['entity_type' => $data['entity_type']];
|
||||
|
||||
if (isset($data['quick_add'])) {
|
||||
$conditions['quick_add'] = 1;
|
||||
}
|
||||
|
||||
$attributes = $this->attributeRepository->where($conditions)
|
||||
->whereIn('code', $attributes)
|
||||
->get();
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $warehouse->id,
|
||||
]), $attributes);
|
||||
|
||||
return $warehouse;
|
||||
}
|
||||
|
||||
$this->attributeValueRepository->save(array_merge($data, [
|
||||
'entity_id' => $warehouse->id,
|
||||
]));
|
||||
|
||||
return $warehouse;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user