Skip to content

Commit

Permalink
Budget: repeat for year function
Browse files Browse the repository at this point in the history
  • Loading branch information
boryashkin committed Feb 11, 2017
1 parent aefd603 commit 2c0772c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 32 additions & 1 deletion modules/budget/models/Budget.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
class Budget extends ActiveRecord
{
/** @var bool Create same record for each month of year */
public $repeatForYear;

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -58,7 +61,8 @@ public function rules()
[['expectedSum', 'realSum'], 'number'],
[['done', 'created_at', 'updated_at'], 'integer'],
[['name'], 'string', 'max' => 255],
['description', 'string']
['description', 'string'],
['repeatForYear', 'boolean'],
];
}

Expand All @@ -71,6 +75,32 @@ public function beforeSave($insert)
return parent::beforeSave($insert);
}

/** @inheritdoc */
public function afterSave($insert, $changedAttributes)
{
if ($insert) {
$date = new \DateTime($this->expectedDate);
if ($this->repeatForYear) {
while ($date->format('m') < 12) {
$currentMonth = $date->format('m');
$date->modify('next month');
// check if next month doesn't have this day (like 31 february)
if (($date->format('m') - $currentMonth) > 1) {
// but we need the next month
$date = new \DateTime($this->expectedDate);
$date->modify('last day of next month');
}

$duplicate = new Budget();
$duplicate->attributes = $this->attributes;
$duplicate->expectedDate = $date->format('Y-m-d');
$duplicate->repeatForYear = false;
$duplicate->save();
}
}
}
}

/**
* @inheritdoc
*/
Expand All @@ -88,6 +118,7 @@ public function attributeLabels()
'done' => 'Done',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'repeatForYear' => 'Repeat it for each next month of this year',
];
}
}
2 changes: 2 additions & 0 deletions modules/budget/views/index/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

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

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

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

0 comments on commit 2c0772c

Please sign in to comment.