Skip to content
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
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions common/models/UserWarehouse.php
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',
];
}
}
98 changes: 98 additions & 0 deletions common/models/Warehouse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace common\models;

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'],
[['created_at', 'updated_at'], 'integer'],
[['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();
}


/**
* 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();
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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.


return ArrayHelper::map($data, $keyField, $valueField);
}

/**
* Get UserCustomer
*
* @return \yii\db\ActiveQuery
*/
public function getUserWarehouse()
{
return $this->hasMany(UserWarehouse::class, ['warehouse_id' => 'id']);
}
}
11 changes: 10 additions & 1 deletion common/models/base/BaseOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace common\models\base;

use common\models\Warehouse;

/**
* This is the model class for table "orders".
*
* @property int $id
* @property int $customer_id
* @property int $warehouse_id
* @property string $order_reference
* @property string $customer_reference
* @property int $status_id
Expand Down Expand Up @@ -54,7 +57,7 @@ public function rules()
{
return [
[['customer_id', 'customer_reference', 'address_id'], 'required'],
[['customer_id', 'status_id', 'address_id', 'carrier_id', 'service_id', 'transit'], 'integer'],
[['customer_id', 'warehouse_id', 'status_id', 'address_id', 'carrier_id', 'service_id', 'transit'], 'integer'],
[['created_date', 'updated_date', 'requested_ship_date', 'must_arrive_by_date', 'carrier_id', 'service_id', 'transit'], 'safe'],
[['order_reference', 'tracking'], 'string', 'max' => 45],
[['customer_reference', 'origin', 'uuid'], 'string', 'max' => 64],
Expand Down Expand Up @@ -88,6 +91,7 @@ public function attributeLabels()
return [
'id' => 'ID',
'customer_id' => 'Customer ID',
'warehouse_id' => 'Warehouse ID',
'order_reference' => 'WMS Order #',
'customer_reference' => 'Customer Order #',
'status_id' => 'Status ID',
Expand All @@ -103,4 +107,9 @@ public function attributeLabels()
'label_type' => 'Label Type',
];
}

public function getWarehouse()
{
return $this->hasOne(Warehouse::class, ['id' => 'warehouse_id']);
}
}
70 changes: 70 additions & 0 deletions console/migrations/m221230_194520_create_warehouse_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

use yii\db\Migration;

/**
* Handles the creation of table `{{%warehouse}}`.
*/
class m221230_194520_create_warehouse_table extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->createTable('{{%warehouse}}', [
'id' => $this->primaryKey(),
'name' => $this->string()->notNull(),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
]);

$this->createTable('{{%user_warehouse}}', [
'id' => $this->primaryKey(),
'warehouse_id' => $this->integer()->notNull(),
'user_id' => $this->integer()->notNull(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we define foreign keys for these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'll get that added.

]);

$this->addColumn('{{%orders}}', 'warehouse_id', $this->integer()->null());

// creates index for column `warehouse_id`
$this->createIndex(
'{{%idx-orders-warehouse_id}}',
'{{%orders}}',
'warehouse_id'
);

// add foreign key for table `{{%orders}}`
$this->addForeignKey(
'{{%fk-orders-warehouse_id}}',
'{{%orders}}',
'warehouse_id',
'{{%orders}}',
'id',
'RESTRICT'
);

}

/**
* {@inheritdoc}
*/
public function safeDown()
{
// drops foreign key for table `{{%orders}}`
$this->dropForeignKey(
'{{%fk-orders-warehouse_id}}',
'{{%orders}}'
);

// drops index for column `warehouse_id`
$this->dropIndex(
'{{%idx-orders-warehouse_id}}',
'{{%orders}}'
);

$this->dropColumn('{{%orders}}', 'warehouse_id');
$this->dropTable('{{%warehouse}}');
$this->dropTable('{{%user_warehouse}}');
}
}
10 changes: 9 additions & 1 deletion frontend/controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

use common\pdf\OrderPackingSlip;
use common\models\forms\OrderForm;
use common\models\{base\BaseBatch, Country, ScheduledOrder, State, Status, shipping\Carrier, shipping\Service};
use common\models\{base\BaseBatch,
Country,
ScheduledOrder,
State,
Status,
shipping\Carrier,
shipping\Service,
Warehouse};
use frontend\models\Customer;
use Yii;
use frontend\models\{Address,
Expand Down Expand Up @@ -320,6 +327,7 @@ public function actionUpdate($id)
'services' => Service::getList('id', 'name', $model->order->carrier_id),
'countries' => Country::getList(),
'states' => State::getList('id', 'name', $model->address->country),
'warehouses' => Warehouse::getList(),
]
);
}
Expand Down
Loading