Inheritance: extends yii\db\ActiveRecord
Ejemplo n.º 1
0
 public function actionSurplus($id)
 {
     /* @var $model Goods */
     $model = Goods::findOne(['id' => $id, 'store_id' => Yii::$app->user->identity->store_id]);
     if (!$model) {
         throw new NotFoundHttpException('未找到该商品。');
     }
     $dataProvider = new ActiveDataProvider(['query' => GoodsSurplus::find()->where(['goods_id' => $id]), 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => 20]]);
     return $this->render('surplus', ['model' => $model, 'dataProvider' => $dataProvider]);
 }
Ejemplo n.º 2
0
 /**
  * 调整库存
  * 
  * @param integer $number
  * @param string $remark
  * @throws \Exception
  * @return boolean
  */
 public function moveSurplus($number, $remark)
 {
     $number = (int) $number;
     $surplusOriginal = $this->surplus;
     $this->surplus += $number;
     if ($this->surplus < 0) {
         $this->surplus = 0;
     }
     if ($surplusOriginal == $this->surplus) {
         return true;
     }
     $transaction = Yii::$app->db->beginTransaction();
     try {
         if (!$this->save(false)) {
             throw new \Exception('商品错误!');
         }
         $goodsSurplus = new GoodsSurplus();
         $goodsSurplus->goods_id = $this->id;
         $goodsSurplus->surplus_before = $surplusOriginal;
         $goodsSurplus->amount = $number;
         $goodsSurplus->surplus_after = $this->surplus;
         $goodsSurplus->remark = $remark;
         if (!$goodsSurplus->save(false)) {
             throw new \Exception('商品库存记录失败!');
         }
         $transaction->commit();
         return true;
     } catch (\Exception $e) {
         $transaction->rollBack();
         return false;
     }
 }