-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Warehouse identifier for orders and users #100
Draft
cgsmith
wants to merge
7
commits into
master
Choose a base branch
from
feature/warehouse_id
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bbfcd35
initial progress on adding warehouse and tables for users
cgsmith 329dbec
ability to associate customer with warehouse identifier
cgsmith d12ecfe
fixing foreign key
cgsmith 0577ed2
Update common/models/Warehouse.php
cgsmith 9591e7f
updating for warehouse id
cgsmith 3a8d5b3
Merge remote-tracking branch 'origin/feature/warehouse_id' into featu…
cgsmith 46f9aa0
Merge remote-tracking branch 'origin/master' into feature/warehouse_id
cgsmith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM cgsmith105/yii2-php:8.0-apache | ||
FROM yiisoftware/yii2-php:8.1-apache | ||
|
||
# Change document root for Apache | ||
RUN sed -i -e 's|/app/web|/app/api/web|g' /etc/apache2/sites-available/000-default.conf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace common\models; | ||
|
||
use Yii; | ||
|
||
/** | ||
* This is the model class for table "user_warehouse". | ||
* | ||
* @property int $id | ||
* @property int $warehouse_id | ||
* @property int $user_id | ||
*/ | ||
class UserWarehouse extends \yii\db\ActiveRecord | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function tableName() | ||
{ | ||
return 'user_warehouse'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
[['warehouse_id', 'user_id'], 'required'], | ||
[['warehouse_id', 'user_id'], 'integer'], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function attributeLabels() | ||
{ | ||
return [ | ||
'id' => 'ID', | ||
'warehouse_id' => 'Warehouse ID', | ||
'user_id' => 'User ID', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace common\models; | ||
|
||
use common\models\query\WarehouseQuery; | ||
use Yii; | ||
use yii\behaviors\TimestampBehavior; | ||
use yii\helpers\ArrayHelper; | ||
|
||
/** | ||
* This is the model class for table "warehouse". | ||
* | ||
* @property int $id | ||
* @property string $name | ||
* @property int $created_at | ||
* @property int $updated_at | ||
*/ | ||
class Warehouse extends \yii\db\ActiveRecord | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function tableName() | ||
{ | ||
return 'warehouse'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
[['name'], 'required'], | ||
[['name'], 'string', 'max' => 255], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function attributeLabels() | ||
{ | ||
return [ | ||
'id' => 'ID', | ||
'name' => 'Name', | ||
'created_at' => 'Created At', | ||
'updated_at' => 'Updated At', | ||
]; | ||
} | ||
|
||
public function behaviors() | ||
{ | ||
return [ | ||
TimestampBehavior::class, | ||
]; | ||
} | ||
|
||
/** | ||
* Whether the customer is linked to given user | ||
* | ||
* @param int $userId | ||
* | ||
* @return bool | ||
*/ | ||
public function isLinkedToUser($userId) | ||
{ | ||
return $this->getUserWarehouse()->onCondition(['user_id' => (int)$userId])->exists(); | ||
} | ||
|
||
public static function find() | ||
{ | ||
return new WarehouseQuery(get_called_class()); | ||
} | ||
|
||
/** | ||
* Returns list of customers as array [id=>name] | ||
* | ||
* @param string $keyField Field name to use as key | ||
* @param string $valueField Field name to use as value | ||
* | ||
* @todo getList is used in multiple spots and might be worthwhile to make an interface | ||
* @return array | ||
*/ | ||
public static function getList($keyField = 'id', $valueField = 'name') | ||
{ | ||
$data = self::find()->orderBy([$valueField => SORT_ASC])->all(); | ||
|
||
return ArrayHelper::map($data, $keyField, $valueField); | ||
} | ||
|
||
/** | ||
* Get UserCustomer | ||
* | ||
* @return \yii\db\ActiveQuery | ||
*/ | ||
public function getUserWarehouse() | ||
{ | ||
return $this->hasMany(UserWarehouse::class, ['warehouse_id' => 'id']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
|
||
namespace common\models\query; | ||
|
||
|
||
use common\models\Invoice; | ||
|
||
class WarehouseQuery extends \yii\db\ActiveQuery | ||
{ | ||
|
||
/** | ||
* Ensures customer_id is maintained for users that require restriction on the model | ||
* | ||
* @todo this should move to the basequery then all models should reference | ||
* | ||
* @param $modelClass | ||
* @param $config | ||
*/ | ||
public function __construct($modelClass, $config = []) | ||
{ | ||
parent::__construct($modelClass, $config); | ||
|
||
// Admins can see all models - so admins skip this andWhere() | ||
// skip if running from CLI - typically a Job | ||
// also skip if running from API | ||
// @todo load user while making requests to run through ACL | ||
if (\Yii::$app instanceof \yii\web\Application && | ||
\Yii::$app->id !== 'app-api' && | ||
!\Yii::$app->user->identity->isAdmin) { | ||
$this->andWhere(['in', 'id', \Yii::$app->user->identity->warehouseIds]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace common\traits; | ||
|
||
trait LimitBy | ||
{ | ||
public function limitByWarehouse($ids = []) | ||
{ | ||
$this->andWhere(['in', 'warehouse_id', \Yii::$app->user->identity->warehouseIds]); | ||
} | ||
|
||
public function limitByCustomer($ids = []) | ||
{ | ||
$this->andWhere(['in', 'customer_id', \Yii::$app->user->identity->customerIds]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How many warehouses are going to be in the database?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially it'll only be about 10 or so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When it will grow big, this solution won't work.