public function initAccounts() { //create default account for balance Item $account = new Account(); $account->name = $this->name; $account->balance_item_id = $this->id; $errors = []; if ($account->save()) { //for all existed balance items create amount for default account $bSheets = BalanceSheet::find()->all(); $success = true; foreach ($bSheets as $sheet) { if (isset($sheet->id)) { $amount = new BalanceAmount(); $amount->account_id = $account->id; $amount->balance_sheet_id = $sheet->id; $amount->amount = 0; if (!$amount->save()) { $success = false; $errors['amount'] = $amount->errors; } } } if ($success) { return false; } } else { $errors['account'] = $account->errors; } $errors['message'] = "Errors in BalanceItem::initAccounts()"; return $errors; }
/** For each account create an item. If previous balance sheet exists and has value - set it as default, otherwise - set to zero */ public function initAmounts() { $accounts = Account::find()->orderBy('order_code')->all(); $prevBalance = $this->getPreviouBalance(); for ($i = 0; $i < count($accounts); $i++) { $amount = new BalanceAmount(); $amount->account_id = $accounts[$i]->id; $amount->balance_sheet_id = $this->id; $amount->amount = $prevBalance ? $prevBalance->balanceAmounts[$i]->amount : 0; $amount->save(); } }
/** * Finds the BalanceAmount model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return BalanceAmount the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = BalanceAmount::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getBalanceAmounts() { return $this->hasMany(BalanceAmount::className(), ['account_id' => 'id']); }