public function actionPass($id) { $model = Apply::findOne(['id' => $id, 'status' => Apply::STATUS_PENDING]); if (!$model) { throw new BadRequestHttpException('参数错误!'); } $transaction = Yii::$app->db->beginTransaction(); try { $model->status = Apply::STATUS_PASSED; if (!$model->save(false)) { throw new \Exception('操作失败!'); } $modelApplyLog = new ApplyLog(); $modelApplyLog->apply_id = $id; $modelApplyLog->remark = '申请通过。'; if (!$modelApplyLog->save(false)) { throw new \Exception('申请日志记录失败!'); } $transaction->commit(); if (!empty($model->store->cellphone)) { Yii::$app->smser->send($model->store->cellphone, "亲爱的店长,恭喜您的采购订单{$model->apply_sn}申请成功,我们的工作人员很快会与您取得联系。"); } Yii::$app->session->setFlash('success', '操作成功!'); } catch (\Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('danger', $e->getMessage()); } return $this->redirect(['view', 'id' => $id]); }
/** * @return boolean */ public function reject($runValidation = true) { if ($runValidation && !$this->validate()) { return false; } $transaction = Yii::$app->db->beginTransaction(); try { $this->_apply->status = Apply::STATUS_REJECTED; if (!$this->_apply->save(false)) { throw new \Exception('保存失败!'); } $modelApplyLog = new ApplyLog(); $modelApplyLog->apply_id = $this->_apply->id; $modelApplyLog->remark = '申请被驳回,原因:' . $this->remark; if (!$modelApplyLog->save(false)) { throw new \Exception('申请日志记录失败!'); } $transaction->commit(); return true; } catch (\Exception $e) { $transaction->rollBack(); return false; } }
/** * @return \yii\db\ActiveQuery */ public function getLogs() { return $this->hasMany(ApplyLog::className(), ['apply_id' => 'id']); }
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; } }
public function actionCancel($id) { $model = Apply::findOne(['id' => $id, 'status' => Apply::STATUS_REJECTED, 'store_id' => Yii::$app->user->identity->store_id]); if (!$model) { throw new BadRequestHttpException('参数错误!'); } $transaction = Yii::$app->db->beginTransaction(); try { $model->status = Apply::STATUS_CANCELLED; if (!$model->save(false)) { throw new \Exception('操作失败!'); } $modelApplyLog = new ApplyLog(); $modelApplyLog->apply_id = $id; $modelApplyLog->remark = '取消申请。'; if (!$modelApplyLog->save(false)) { throw new \Exception('申请日志记录失败!'); } $transaction->commit(); Yii::$app->session->setFlash('success', '操作成功!'); } catch (\Exception $e) { $transaction->rollBack(); Yii::$app->session->setFlash('danger', $e->getMessage()); } return $this->redirect(['view', 'id' => $id]); }