common\models\Goods::moveSurplus PHP Method

moveSurplus() public method

调整库存
public moveSurplus ( integer $number, string $remark ) : boolean
$number integer
$remark string
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;
        }
    }

Usage Example

コード例 #1
0
 public function move($runValidation = true)
 {
     if ($runValidation && !$this->validate()) {
         return false;
     }
     return $this->_goods->moveSurplus($this->amount, '管理人员调整库存。');
 }