Skip to content

Commit

Permalink
Budget: budget planning 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
boryashkin committed Jan 22, 2017
1 parent 747d13c commit 09477ad
Show file tree
Hide file tree
Showing 11 changed files with 533 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
'reading' => [
'class' => 'app\modules\reading\Module',
],
'budget' => [
'class' => 'app\modules\budget\Module',
],
],
'params' => $params,
];
Expand Down
42 changes: 42 additions & 0 deletions migrations/m170122_075414_budget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use yii\db\Migration;

class m170122_075414_budget extends Migration
{
public function up()
{
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB';

$this->createTable('budget', [
'id' => $this->primaryKey(),
'firstExpectedDate' => $this->date()->notNull(),
'expectedDate' => $this->date()->notNull(),
'realDate' => $this->date(),
'name' => $this->string(),
'expectedSum' => $this->decimal(10, 2)->notNull(),
'realSum' => $this->decimal(10, 2),
'done' => $this->boolean()->defaultValue(false),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
], $tableOptions);
}

public function down()
{
return false;
$this->dropTable('budget');
return true;
}

/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
8 changes: 8 additions & 0 deletions modules/budget/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace app\modules\budget;

class Module extends \yii\base\Module
{
public $defaultRoute = 'index';
}
158 changes: 158 additions & 0 deletions modules/budget/controllers/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php

namespace app\modules\budget\controllers;

use Yii;
use app\modules\budget\models\Budget;
use yii\data\ActiveDataProvider;
use yii\data\Sort;
use yii\db\Expression;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
* IndexController implements the CRUD actions for Budget model.
*/
class IndexController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}

/**
* Lists all Budget models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => Budget::find(),
'sort' => new Sort([
'defaultOrder' => [
'expectedDate' => SORT_ASC,
],
]),

]);
$sums = Budget::find()->select([
new Expression('sum(expectedSum) as expectedSum'),
new Expression('sum(realSum) as realSum'),
new Expression('count(done) as totalRecords'),
new Expression(
'('
. Budget::find()->where(['done' => false])
->select(new Expression('count(*) as done'))
->createCommand()->rawSql
. ') as totalUndone'
)
])->asArray()->one();

return $this->render('index', [
'dataProvider' => $dataProvider,
'sumOfExpectedSum' => (float)$sums['expectedSum'],
'sumOfRealSum' => (float)$sums['realSum'],
'totalDone' => (float)($sums['totalRecords'] - $sums['totalUndone']),
'totalUndone' => (float)$sums['totalUndone'],
]);
}

/**
* Displays a single Budget model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}

/**
* Creates a new Budget model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Budget();

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}

/**
* Updates an existing Budget model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);

if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}

/**
* Deletes an existing Budget model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();

return $this->redirect(['index']);
}

/**
* Finds the Budget model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Budget the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Budget::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
90 changes: 90 additions & 0 deletions modules/budget/models/Budget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace app\modules\budget\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
* This is the model class for table "{{%budget}}".
*
* @property integer $id
* @property string $firstExpectedDate
* @property string $expectedDate
* @property string $realDate
* @property string $name
* @property string $expectedSum
* @property string $realSum
* @property integer $done
* @property integer $created_at
* @property integer $updated_at
*/
class Budget extends ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%budget}}';
}

/**
* @inheritdoc
*/
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}

/**
* @inheritdoc
*/
public function rules()
{
return [
[['expectedDate', 'expectedSum'], 'required'],
[['firstExpectedDate', 'expectedDate', 'realDate'], 'date', 'format' => 'php:Y-m-d'],
[['expectedSum', 'realSum'], 'number'],
[['done', 'created_at', 'updated_at'], 'integer'],
[['name'], 'string', 'max' => 255],
];
}

/** @inheritdoc */
public function beforeSave($insert)
{
if ($insert) {
$this->firstExpectedDate = $this->expectedDate;
}
return parent::beforeSave($insert);
}

/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'firstExpectedDate' => 'First Expected Date',
'expectedDate' => 'Expected Date',
'realDate' => 'Real Date',
'name' => 'Name',
'expectedSum' => 'Expected Sum',
'realSum' => 'Real Sum',
'done' => 'Done',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
}
58 changes: 58 additions & 0 deletions modules/budget/views/index/_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use kartik\datetime\DateTimePicker;

/* @var $this yii\web\View */
/* @var $model app\modules\budget\models\Budget */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="budget-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>

<?= $form->field($model, 'expectedDate')->widget(DateTimePicker::class, [
'options' => [
'class' => 'form-control input-sm',
],
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd',
'maxView' => 3,
'minView' => 2,
]
]) ?>

<?php if (!$model->isNewRecord) : ?>
<?= $form->field($model, 'realDate')->widget(DateTimePicker::class, [
'options' => [
'class' => 'form-control input-sm',
],
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd',
'maxView' => 3,
'minView' => 2,
]
]) ?>
<?php endif; ?>

<?= $form->field($model, 'expectedSum')->textInput(['maxlength' => true]) ?>

<?php if (!$model->isNewRecord) : ?>
<?= $form->field($model, 'realSum')->textInput(['maxlength' => true]) ?>
<?php endif; ?>

<?= $form->field($model, 'done')->checkbox() ?>

<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

</div>
21 changes: 21 additions & 0 deletions modules/budget/views/index/create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use yii\helpers\Html;


/* @var $this yii\web\View */
/* @var $model app\modules\budget\models\Budget */

$this->title = 'Create Budget';
$this->params['breadcrumbs'][] = ['label' => 'Budgets', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="budget-create">

<h1><?= Html::encode($this->title) ?></h1>

<?= $this->render('_form', [
'model' => $model,
]) ?>

</div>
Loading

0 comments on commit 09477ad

Please sign in to comment.