summaryrefslogtreecommitdiff
path: root/app/Controller/Budget.php
blob: 2c56c79df54af483ee0bef8ff272c3b2ca1bd5c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php

namespace Controller;

/**
 * Budget
 *
 * @package controller
 * @author  Frederic Guillot
 */
class Budget extends Base
{
    /**
     * Budget index page
     *
     * @access public
     */
    public function index()
    {
        $project = $this->getProject();

        $this->response->html($this->projectLayout('budget/index', array(
            'daily_budget' => $this->budget->getDailyBudgetBreakdown($project['id']),
            'project' => $project,
            'title' => t('Budget')
        )));
    }

    /**
     * Cost breakdown by users/subtasks/tasks
     *
     * @access public
     */
    public function breakdown()
    {
        $project = $this->getProject();

        $paginator = $this->paginator
            ->setUrl('budget', 'breakdown', array('project_id' => $project['id']))
            ->setMax(30)
            ->setOrder('start')
            ->setDirection('DESC')
            ->setQuery($this->budget->getSubtaskBreakdown($project['id']))
            ->calculate();

        $this->response->html($this->projectLayout('budget/breakdown', array(
            'paginator' => $paginator,
            'project' => $project,
            'title' => t('Budget')
        )));
    }

    /**
     * Create budget lines
     *
     * @access public
     */
    public function create(array $values = array(), array $errors = array())
    {
        $project = $this->getProject();

        if (empty($values)) {
            $values['date'] = date('Y-m-d');
        }

        $this->response->html($this->projectLayout('budget/create', array(
            'lines' => $this->budget->getAll($project['id']),
            'values' => $values + array('project_id' => $project['id']),
            'errors' => $errors,
            'project' => $project,
            'title' => t('Budget')
        )));
    }

    /**
     * Validate and save a new budget
     *
     * @access public
     */
    public function save()
    {
        $project = $this->getProject();

        $values = $this->request->getValues();
        list($valid, $errors) = $this->budget->validateCreation($values);

        if ($valid) {

            if ($this->budget->create($values['project_id'], $values['amount'], $values['comment'], $values['date'])) {
                $this->session->flash(t('The budget line have been created successfully.'));
                $this->response->redirect($this->helper->url('budget', 'create', array('project_id' => $project['id'])));
            }
            else {
                $this->session->flashError(t('Unable to create the budget line.'));
            }
        }

        $this->create($values, $errors);
    }

    /**
     * Confirmation dialog before removing a budget
     *
     * @access public
     */
    public function confirm()
    {
        $project = $this->getProject();

        $this->response->html($this->projectLayout('budget/remove', array(
            'project' => $project,
            'budget_id' => $this->request->getIntegerParam('budget_id'),
            'title' => t('Remove a budget line'),
        )));
    }

    /**
     * Remove a budget
     *
     * @access public
     */
    public function remove()
    {
        $this->checkCSRFParam();
        $project = $this->getProject();

        if ($this->budget->remove($this->request->getIntegerParam('budget_id'))) {
            $this->session->flash(t('Budget line removed successfully.'));
        } else {
            $this->session->flashError(t('Unable to remove this budget line.'));
        }

        $this->response->redirect($this->helper->url('budget', 'create', array('project_id' => $project['id'])));
    }
}