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; }
public function prepareNext() { $this->is_month = true; $last = BalanceSheet::find()->orderBy('period_start DESC')->limit(1)->one(); if ($last) { $this->period_start = date("Y-m-d", strtotime("+1 month", strtotime($last->period_start))); } else { $this->period_start = (new \DateTime('first day of previous month'))->format("Y-m-d"); } }
public function actionDrop() { $list = \app\models\BalanceItem::find()->all(); foreach ($list as $item) { $item->delete(); } $list = \app\models\BalanceSheet::find()->all(); foreach ($list as $item) { $item->delete(); } return $this->redirect(['index']); }
public function performStep($step) { if (parent::performStep($step)) { // ...custom code here... if ($step->code == 'init1') { //init two months on the first step $model = new BalanceSheet(); $model->prepareNext(); if ($model->save()) { $model->initAmounts(); } $model = new BalanceSheet(); $model->prepareNext(); if ($model->save()) { $model->initAmounts(); } } return $this->createBalanceItem($step); } else { return false; } }
/** * @return \yii\db\ActiveQuery */ public function getBalanceSheet() { return $this->hasOne(BalanceSheet::className(), ['id' => 'balance_sheet_id']); }
private function LoadBalances() { if (!$this->balanceSheets) { $this->balanceSheets = BalanceSheet::LastTwo(); } }
<?php use yii\helpers\Html; use yii\grid\GridView; use app\components\AccountValue; use app\models\BalanceSheet; /* @var $this yii\web\View */ /* @var $dataProvider yii\data\ActiveDataProvider */ $bSheets = BalanceSheet::LastTwo(); $this->title = 'Balance'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="balance-item-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Html::a('New Month', ['balance-sheet/create-next'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'columns' => ['order_code', 'balanceType.name:text:Type', 'name', ['label' => 'Current Amount by ' . $bSheets[0]->period_start, 'content' => function ($model, $key, $index, $column) { return AccountValue::widget(['model' => $model, 'attribute' => 'currentAmount']); }], ['label' => 'Previous Amount by ' . $bSheets[1]->period_start, 'content' => function ($model, $key, $index, $column) { return AccountValue::widget(['model' => $model, 'attribute' => 'previousAmount']); }], ['class' => 'yii\\grid\\ActionColumn']]]);
/** * Finds the BalanceSheet model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return BalanceSheet the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = BalanceSheet::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }