Inheritance: extends yii\db\ActiveRecord
Ejemplo n.º 1
0
 public function create($runValidation = true)
 {
     if ($runValidation && !$this->validate()) {
         return false;
     }
     if (empty($this->_purchaseList)) {
         return false;
     }
     $transaction = Yii::$app->db->beginTransaction();
     try {
         if (Apply::find()->where(['store_id' => Yii::$app->user->identity->store_id, 'status' => [Apply::STATUS_PENDING, Apply::STATUS_REJECTED, Apply::STATUS_PASSED]])->exists()) {
             throw new \Exception('您采购历史中还有未完成的订单,不能继续申请.');
         }
         $this->_apply = new Apply();
         $this->_apply->generateApplySn();
         $this->_apply->store_id = Yii::$app->user->identity->store_id;
         $this->_apply->status = Apply::STATUS_PENDING;
         $this->_apply->fee = Purchase::getVolumeByStoreId($this->_apply->store_id);
         $this->_apply->remark = $this->remark;
         if (!$this->_apply->save(false)) {
             throw new \Exception('创建申请失败!');
         }
         foreach ($this->_purchaseList as $purchase) {
             if ($purchase->isExpired) {
                 throw new \Exception("商品“{$purchase->goods->name}”已失效,请删除该商品然后继续。");
             }
             $modelApplyGoods = ApplyGoods::createDuplicate($purchase->goods_id);
             $modelApplyGoods->apply_id = $this->_apply->id;
             $modelApplyGoods->count = $purchase->count;
             if (!$modelApplyGoods->save(false)) {
                 throw new \Exception('记录商品清单失败!');
             }
         }
         $modelApplyLog = new ApplyLog();
         $modelApplyLog->apply_id = $this->_apply->id;
         $modelApplyLog->remark = '开始提交申请。';
         if (!$modelApplyLog->save(false)) {
             throw new \Exception('商品申请记录失败!');
         }
         Purchase::clear(Yii::$app->user->identity->store_id);
         $transaction->commit();
         return true;
     } catch (\Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
 }
Ejemplo n.º 2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getGoods()
 {
     return $this->hasMany(ApplyGoods::className(), ['apply_id' => 'id']);
 }