/** * Creating new data record for Communal module * * @param $counter * @return string|yii\web\Response * @throws ForbiddenHttpException * @throws NotFoundHttpException */ public function actionAddRecord($counter) { /** @var LinkCounter $linkModel */ $linkModel = LinkCounter::findOne(['counter_id' => $counter]); if (!is_null($linkModel)) { /** @var Estate $estate */ $estate = $this->findEstate($linkModel->getEstateId()); $comServiceId = $linkModel->getComServiceId(); } else { throw new NotFoundHttpException(Yii::t('app', 'ERROR_WRONG_ID')); } $model = new AddRecordForm(); $model->setCounterId($counter); if ($model->load(Yii::$app->request->post()) && $model->validate()) { /** @var Record $record */ $record = new Record(); $record->setCounterId($model->getCounterId()); $record->setComServiceId($comServiceId); $record->setData($model->getData()); $record->setStatus(Record::STATUS_NEW); if ($record->save()) { Yii::$app->session->setFlash('success', Yii::t('app', 'FLASH_RECORD_SUCCESS')); return $this->redirect(['/communal/manage/' . $estate->id]); } else { Yii::$app->session->setFlash('error', Yii::t('app', 'FLASH_RECORD_FAILED')); return $this->redirect(['/communal/manage/' . $estate->id]); } } else { return $this->render('record', ['model' => $model, 'estate' => $estate, 'link' => $linkModel]); } }
/** * Initialize function. Creating Counters&LinkCounters records * * @param int $id */ public function initializeEstate($id) { /** @var Estate $estate */ $estate = $this->findEstate($id); $communalList = ComService::findByCity($estate->city_id); // Get list of all ComServices in Estate's city /** @var ComService $communal */ foreach ($communalList as $communal) { /** @var Counter $counter */ $counter = new Counter($communal->type); // Create new counter with specified type $counter->save(); $model = new LinkCounter(); // Create full LinkCounter record $model->setComserviceId($communal->id); $model->setEstateId($estate->id); $model->setCounterId($counter->id); $model->save(); } }
/** * Returns Account number * * @return int */ public function getAccountNumber() { /** @var LinkCounter $model */ $model = LinkCounter::findOne(['counter_id' => $this->counter_id]); return $model->getAccountCode(); }
/** * Delete current Estate and related records (LinkCounters, Counters, Records) * * @param int $id * @return \yii\web\Response * @throws ForbiddenHttpException * @throws NotFoundHttpException * @throws \Exception */ public function actionDelete($id) { $model = $this->findEstate($id); $linkCounters = LinkCounter::findAll(['estate_id' => $model->id]); foreach ($linkCounters as $linkCounter) { /** @var Counter $counter */ $counter = Counter::findOne($linkCounter->getCounterId()); Record::deleteAll(['counter_id' => $counter->id]); $counter->delete(); $linkCounter->delete(); } $model->delete(); Yii::$app->session->setFlash('success', Yii::t('app', 'FLASH_ESTATE_DELETED')); return $this->redirect(['index']); }