|
- <?php
-
-
-
- namespace common\models;
-
- use common\components\ActiveRecordCommon ;
-
-
- class ProductDistribution extends ActiveRecordCommon
- {
-
-
-
- public static function tableName()
- {
- return 'product_distribution';
- }
-
-
-
- public function rules()
- {
- return [
- [['id_distribution', 'id_product', 'active'], 'integer'],
- [['quantity_max'], 'number'],
- ];
- }
-
-
-
-
- public function getProduct()
- {
- return $this->hasOne(Product::className(), ['id' => 'id_product']);
- }
-
- public function getDistribution()
- {
- return $this->hasOne(Distribution::className(), ['id' => 'id_distribution']);
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'id_distribution' => 'Distribution',
- 'id_product' => 'Produit',
- 'active' => 'Actif',
- 'quantity_max' => 'Quantité max',
- ];
- }
-
-
-
- public static function defaultOptionsSearch() {
- return [
- 'with' => ['product','distribution'],
- 'join_with' => [],
- 'orderby' => '',
- 'attribute_id_producer' => ''
- ] ;
- }
-
-
-
- public static function searchByDistribution($idDistribution)
- {
- $arrayProductsDistribution = ProductDistribution::searchAll([
- 'id_distribution' => $idDistribution
- ]) ;
-
- $orders = Order::searchAll([
- 'distribution.id' => $idDistribution
- ]) ;
-
- foreach ($arrayProductsDistribution as $productDistribution) {
- if (isset($productDistribution->product)) {
- $arrayProductsDistribution[$productDistribution->id_product] = [
- 'active' => (int) $productDistribution->active,
- 'unavailable' => (int) $productDistribution->product->unavailable,
- 'quantity_max' => $productDistribution->quantity_max,
- 'quantity_order' => Order::getProductQuantity($productDistribution->id_product, $orders),
- 'quantity_remaining' => $productDistribution->quantity_max - Order::getProductQuantity($productDistribution->id_product, $orders)
- ];
- }
- }
-
- return $arrayProductsDistribution;
- }
-
- }
|