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]); }
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 search($params) { $query = Apply::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => 20]]); // load the seach form data and validate if (!($this->load($params) && $this->validate())) { return $dataProvider; } $dateBegin = strtotime($this->date); $dateEnd = $dateBegin + 86400; // adjust the query by adding the filters $query->andFilterWhere(['like', 'apply_sn', $this->apply_sn])->andFilterWhere(['store_id' => $this->store_id])->andFilterWhere(['status' => $this->status])->andFilterWhere(['>=', 'created_at', $this->date ? $dateBegin : null])->andFilterWhere(['<', 'created_at', $this->date ? $dateEnd : null]); return $dataProvider; }
/** * @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 getApply() { return $this->hasOne(Apply::className(), ['id' => 'apply_id']); }
case Apply::STATUS_REJECTED: $class = 'danger'; break; case Apply::STATUS_CANCELLED: $class = 'warning'; break; case Apply::STATUS_COMPLETED: $class = 'default'; break; default: break; } return ['class' => $class]; }, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'headerOptions' => ['class' => 'col-md-1']], ['attribute' => 'apply_sn', 'headerOptions' => ['class' => 'col-md-2'], 'filterInputOptions' => ['class' => 'form-control input-sm']], ['attribute' => 'store_id', 'headerOptions' => ['class' => 'col-md-4'], 'filterInputOptions' => ['class' => 'form-control input-sm'], 'value' => function ($model, $key, $index, $column) { return $model->store->school->name . '-' . $model->store->name; }, 'filter' => Select2::widget(['model' => $searchModel, 'initValueText' => ($store = Store::findOne($searchModel->store_id)) ? $store->name : '', 'attribute' => 'store_id', 'size' => Select2::SMALL, 'theme' => Select2::THEME_KRAJEE, 'options' => ['placeholder' => '搜索店铺名称...'], 'pluginOptions' => ['allowClear' => true, 'minimumInputLength' => 3, 'ajax' => ['url' => Url::to(['/store/name-filter']), 'dataType' => 'json', 'data' => new JsExpression('function(params) { return {q:params.term}; }')], 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'), 'templateResult' => new JsExpression('function (store) { return store.text; }'), 'templateSelection' => new JsExpression('function (store) { return store.text; }')]])], ['attribute' => 'status', 'filter' => Apply::getStatusList(), 'filterInputOptions' => ['class' => 'form-control input-sm'], 'headerOptions' => ['class' => 'col-md-1'], 'value' => function ($model, $key, $index, $column) { return $model->statusMsg; }], ['attribute' => 'created_at', 'format' => ['date', 'php:Y-m-d H:i'], 'filter' => DatePicker::widget(['model' => $searchModel, 'type' => DatePicker::TYPE_COMPONENT_APPEND, 'attribute' => 'date', 'options' => ['class' => 'input-sm'], 'pluginOptions' => ['autoclose' => true, 'format' => 'yyyy-mm-dd']]), 'headerOptions' => ['class' => 'col-md-3']], ['class' => 'yii\\grid\\ActionColumn', 'header' => '操作', 'headerOptions' => ['class' => 'col-md-2'], 'template' => '{view}']]]); ?> <?php Pjax::end(); ?> </div> </div> <?php $script = <<<SCRIPT \$('.btn-status').click(function() { \$('[name="ApplySearch[status]"]').val(\$(this).val()).change(); }); SCRIPT; $this->registerJs($script);
$class = 'success'; break; case Apply::STATUS_REJECTED: $class = 'danger'; break; case Apply::STATUS_CANCELLED: $class = 'warning'; break; case Apply::STATUS_COMPLETED: $class = 'default'; break; default: break; } return ['class' => $class]; }, 'columns' => [['class' => 'yii\\grid\\SerialColumn', 'headerOptions' => ['class' => 'col-md-1']], ['attribute' => 'apply_sn', 'headerOptions' => ['class' => 'col-md-4'], 'filterInputOptions' => ['class' => 'form-control input-sm']], ['attribute' => 'status', 'filter' => Apply::getStatusList(), 'filterInputOptions' => ['class' => 'form-control input-sm'], 'headerOptions' => ['class' => 'col-md-2'], 'value' => function ($model, $key, $index, $column) { return $model->statusMsg; }], ['attribute' => 'created_at', 'format' => ['date', 'php:Y-m-d H:i'], 'filter' => DatePicker::widget(['model' => $searchModel, 'type' => DatePicker::TYPE_COMPONENT_APPEND, 'attribute' => 'date', 'options' => ['class' => 'input-sm'], 'pluginOptions' => ['autoclose' => true, 'format' => 'yyyy-mm-dd']]), 'headerOptions' => ['class' => 'col-md-3']], ['class' => 'yii\\grid\\ActionColumn', 'header' => '操作', 'headerOptions' => ['class' => 'col-md-3'], 'template' => '{update} {view}', 'buttons' => ['update' => function ($url, $model, $key) { return $model->status === Apply::STATUS_REJECTED ? Html::a('<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>', $url, ['title' => '编辑']) : ''; }]]]]); ?> <?php Pjax::end(); ?> </div> </div> </div> <?php $script = <<<SCRIPT \$('.btn-status').click(function() { \$('[name="ApplySearch[status]"]').val(\$(this).val()).change();
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]); }