Blame view

models/VariantToShop.php 2.88 KB
031b4e2b   Anastasia   filter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
  <?php
      
      namespace artbox\stock\models;
      
      use yii\db\ActiveRecord;
      use artbox\catalog\models\Variant;
      
      /**
       * This is the model class for table "variant_to_shop".
       *
       * @property integer $variant_id
       * @property integer $shop_id
       * @property integer $count
       * @property Shop    $shop
       * @property Variant $variant
       */
      class VariantToShop extends ActiveRecord
      {
          /**
           * @inheritdoc
           */
          public static function tableName()
          {
              return 'variant_to_shop';
          }
          
          /**
           * @inheritdoc
           */
          public function rules()
          {
              return [
                  [
                      [
                          'variant_id',
                          'shop_id',
                      ],
                      'required',
                  ],
                  [
                      [
                          'variant_id',
                          'shop_id',
                          'count',
                      ],
                      'integer',
                  ],
                  [
                      [ 'shop_id' ],
                      'exist',
                      'skipOnError'     => true,
                      'targetClass'     => Shop::className(),
                      'targetAttribute' => [ 'shop_id' => 'id' ],
                  ],
                  [
                      [ 'variant_id' ],
                      'exist',
                      'skipOnError'     => true,
                      'targetClass'     => Variant::className(),
                      'targetAttribute' => [ 'variant_id' => 'id' ],
                  ],
              ];
          }
          
          /**
           * @inheritdoc
           */
          public function attributeLabels()
          {
              return [
                  'variant_id' => 'Variant ID',
                  'shop_id'    => 'Shop ID',
                  'count'      => \Yii::t('stock', 'Count'),
              ];
          }
          
          /**
           * @return \yii\db\ActiveQuery
           */
          public function getShop()
          {
              return $this->hasOne(Shop::className(), [ 'id' => 'shop_id' ]);
          }
          
          /**
           * @return \yii\db\ActiveQuery
           */
          public function getVariant()
          {
              return $this->hasOne(Variant::className(), [ 'id' => 'variant_id' ]);
          }
17104392   Anastasia   stocks in variant
92
          /*
96e1054c   Anastasia   stock map
93
94
95
96
97
98
99
100
101
102
103
104
105
          public function afterSave($insert, $changedAttributes)
          {
              parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
              
              $variant = $this->variant;
              if ($variant->stock - $changedAttributes['count'] >= 0){
                  $variant->stock = $variant->stock - $changedAttributes['count'] + $this->count;
              }else{
                  $variant->stock = $this->count;
              }
              $variant->save();
              
          }
17104392   Anastasia   stocks in variant
106
          )*/
96e1054c   Anastasia   stock map
107
      
031b4e2b   Anastasia   filter
108
      }