~elbati/hr-timesheet/7.0_fix_action_attendances

« back to all changes in this revision

Viewing changes to hr_timesheet_task/static/src/js/timesheet.js

  • Committer: Guewen Baconnier
  • Author(s): Laetitia Gangloff
  • Date: 2013-04-10 09:27:40 UTC
  • mfrom: (40.1.9 migration_hr_timesheet_taskV7)
  • Revision ID: guewen.baconnier@camptocamp.com-20130410092740-qjxsqutev1narcd3
[MIGR] timesheet_task: migration V7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
openerp.hr_timesheet_task = function(instance) { 
 
2
 
 
3
    var module = instance.hr_timesheet_sheet
 
4
 
 
5
    module.WeeklyTimesheet.include({
 
6
        events: {
 
7
            "click .oe_timesheet_weekly_account a": "go_to",
 
8
            "click .oe_timesheet_weekly_task a": "go_to_task",
 
9
        },
 
10
        go_to_task : function(event) {
 
11
            var id = JSON.parse($(event.target).data("task-id"));
 
12
            this.do_action({
 
13
                type: 'ir.actions.act_window',
 
14
                res_model: "project.task",
 
15
                res_id: id,
 
16
                views: [[false, 'form']],
 
17
                target: 'current'
 
18
            });
 
19
        },
 
20
        initialize_content: function() {
 
21
            var self = this;
 
22
            if (self.setting)
 
23
                return;
 
24
            // don't render anything until we have date_to and date_from
 
25
            if (!self.get("date_to") || !self.get("date_from"))
 
26
                return;
 
27
            this.destroy_content();
 
28
 
 
29
            // it's important to use those vars to avoid race conditions
 
30
            var dates;
 
31
            var accounts;
 
32
            var account_names;
 
33
            var task_names;
 
34
            var default_get;
 
35
            return this.render_drop.add(new instance.web.Model("hr.analytic.timesheet").call("default_get", [
 
36
                ['account_id','task_id','general_account_id','journal_id','date','name','user_id','product_id','product_uom_id','to_invoice','amount','unit_amount'],
 
37
                new instance.web.CompoundContext({'user_id': self.get('user_id')})]).then(function(result) {
 
38
                default_get = result;
 
39
                // calculating dates
 
40
                dates = [];
 
41
                var start = self.get("date_from");
 
42
                var end = self.get("date_to");
 
43
                while (start <= end) {
 
44
                    dates.push(start);
 
45
                    start = start.clone().addDays(1);
 
46
                }
 
47
 
 
48
                timesheet_lines = _(self.get("sheets")).chain()
 
49
                .map(function(el) {
 
50
                    // much simpler to use only the id in all cases
 
51
                    if (typeof(el.account_id) === "object")
 
52
                        el.account_id = el.account_id[0];
 
53
                    if (typeof(el.task_id) === "object")
 
54
                        el.task_id = el.task_id[0];
 
55
                    return el;
 
56
                }).value();
 
57
 
 
58
                // group by account
 
59
                var timesheet_lines_by_account_id = _.groupBy(timesheet_lines, function(el) {
 
60
                    return el.account_id;
 
61
                });
 
62
 
 
63
                // group by account and task
 
64
                var timesheet_lines_by_account_id_task_id = _.groupBy(timesheet_lines, function(el) {
 
65
                    return [el.account_id, el.task_id];
 
66
                });
 
67
 
 
68
                var account_ids = _.map(_.keys(timesheet_lines_by_account_id), function(el) { return el === "false" ? false : Number(el) });
 
69
 
 
70
                return new instance.web.Model("hr.analytic.timesheet").call("multi_on_change_account_id", [[], account_ids,
 
71
                    new instance.web.CompoundContext({'user_id': self.get('user_id')})]).then(function(accounts_defaults) {
 
72
                    accounts = _(timesheet_lines_by_account_id_task_id).chain().map(function(lines, account_id_task_id) {
 
73
                        account_defaults = _.extend({}, default_get, (accounts_defaults[lines[0].account_id] || {}).value || {});
 
74
                        // group by days
 
75
                        var index = _.groupBy(lines, "date");
 
76
                        var days = _.map(dates, function(date) {
 
77
                            var day = {day: date, lines: index[instance.web.date_to_str(date)] || []};
 
78
                            // add line where we will insert/remove hours
 
79
                            var to_add = _.find(day.lines, function(line) { return line.name === self.description_line });
 
80
                            if (to_add) {
 
81
                                day.lines = _.without(day.lines, to_add);
 
82
                                day.lines.unshift(to_add);
 
83
                            } else {
 
84
                                day.lines.unshift(_.extend(_.clone(account_defaults), {
 
85
                                    name: self.description_line,
 
86
                                    unit_amount: 0,
 
87
                                    date: instance.web.date_to_str(date),
 
88
                                    account_id: lines[0].account_id,
 
89
                                    task_id: lines[0].task_id,
 
90
                                }));
 
91
                            }
 
92
                            return day;
 
93
                        });
 
94
                        return {account_task: account_id_task_id, account: lines[0].account_id, task: lines[0].task_id, days: days, account_defaults: account_defaults};
 
95
                    }).value();
 
96
 
 
97
                    // we need the name_get of the analytic accounts
 
98
                    return new instance.web.Model("account.analytic.account").call("name_get", [_.pluck(accounts, "account"),
 
99
                        new instance.web.CompoundContext()]).then(function(result) {
 
100
                        account_names = {};
 
101
                        _.each(result, function(el) {
 
102
                            account_names[el[0]] = el[1];
 
103
                        });
 
104
                        // we need the name_get of the tasks
 
105
                        return new instance.web.Model("project.task").call("name_get", [_(accounts).chain().pluck("task").filter(function(el) { return el; }).value(),
 
106
                            new instance.web.CompoundContext()]).then(function(result) {
 
107
                            task_names = {};
 
108
                            _.each(result, function(el) {
 
109
                                task_names[el[0]] = el[1];
 
110
                            });
 
111
                            accounts = _.sortBy(accounts, function(el) {
 
112
                                return account_names[el.account];
 
113
                            });
 
114
                        });
 
115
                    });
 
116
                });
 
117
            })).then(function(result) {
 
118
                // we put all the gathered data in self, then we render
 
119
                self.dates = dates;
 
120
                self.accounts = accounts;
 
121
                self.account_names = account_names;
 
122
                self.task_names = task_names;
 
123
                self.default_get = default_get;
 
124
                //real rendering
 
125
                self.display_data();
 
126
            });
 
127
        },
 
128
        init_add_account: function() {
 
129
            var self = this;
 
130
            if (self.dfm)
 
131
                return;
 
132
            self.$(".oe_timesheet_weekly_add_row").show();
 
133
            self.dfm = new instance.web.form.DefaultFieldManager(self);
 
134
            self.dfm.extend_field_desc({
 
135
                account: {
 
136
                    relation: "account.analytic.account",
 
137
                },
 
138
                task: {
 
139
                    relation: "project.task",
 
140
                },
 
141
            });
 
142
            self.account_m2o = new instance.web.form.FieldMany2One(self.dfm, {
 
143
                attrs: {
 
144
                    name: "account",
 
145
                    type: "many2one",
 
146
                    domain: [
 
147
                        ['type','in',['normal', 'contract']],
 
148
                        ['state', '<>', 'close'],
 
149
                        ['use_timesheets','=',1],
 
150
                    ],
 
151
                    context: {
 
152
                        default_use_timesheets: 1,
 
153
                        default_type: "contract",
 
154
                    },
 
155
                    modifiers: '{"required": true}',
 
156
                },
 
157
            });
 
158
            self.task_m2o = new instance.web.form.FieldMany2One(self.dfm, {
 
159
                attrs: {
 
160
                    name: "task",
 
161
                    type: "many2one",
 
162
                    domain: [
 
163
                        // at this moment, it is always an empty list 
 
164
                        ['project_id.analytic_account_id','=',self.account_m2o.get_value()]
 
165
                    ],
 
166
                },
 
167
            });
 
168
            self.task_m2o.prependTo(self.$(".oe_timesheet_weekly_add_row td"));
 
169
            self.account_m2o.prependTo(self.$(".oe_timesheet_weekly_add_row td"));
 
170
 
 
171
            // when account_m2o loses focus, value can be changed, 
 
172
            // update task_m2o to show only tasks related to the selected project
 
173
            self.account_m2o.$input.focusout(function(){
 
174
                var account_id = self.account_m2o.get_value();
 
175
                if (account_id === false) { return; }
 
176
                self.task_m2o.init(self.dfm, {
 
177
                    attrs: {
 
178
                        name: "task",
 
179
                        type: "many2one",
 
180
                        domain: [
 
181
                            ['state','=','open'],
 
182
                            // show only tasks linked to the selected project
 
183
                            ['project_id.analytic_account_id','=',account_id],
 
184
                            // ignore tasks already in the timesheet
 
185
                            ['id', 'not in', _.pluck(self.accounts, "task")],
 
186
                        ],
 
187
                        context: {
 
188
                            'account_id': account_id,
 
189
                        },
 
190
                    },
 
191
                });
 
192
            });
 
193
 
 
194
            self.$(".oe_timesheet_weekly_add_row button").click(function() {
 
195
                var id = self.account_m2o.get_value();
 
196
                if (id === false) {
 
197
                    self.dfm.set({display_invalid_fields: true});
 
198
                    return;
 
199
                }
 
200
                var ops = self.generate_o2m_value();
 
201
                new instance.web.Model("hr.analytic.timesheet").call("on_change_account_id", [[], id]).then(function(res) {
 
202
                    var def = _.extend({}, self.default_get, res.value, {
 
203
                        name: self.description_line,
 
204
                        unit_amount: 0,
 
205
                        date: instance.web.date_to_str(self.dates[0]),
 
206
                        account_id: id,
 
207
                        task_id: self.task_m2o.get_value(),
 
208
                    });
 
209
                    ops.push(def);
 
210
                    self.set({"sheets": ops});
 
211
                });
 
212
            });
 
213
        },
 
214
        get_box: function(account, day_count) {
 
215
            return this.$('[data-account-task="' + account.account_task + '"][data-day-count="' + day_count + '"]');
 
216
        },
 
217
        get_total: function(account) {
 
218
            return this.$('[data-account-task-total="' + account.account_task + '"]');
 
219
        },
 
220
        generate_o2m_value: function() {
 
221
            var self = this;
 
222
            var ops = [];
 
223
 
 
224
            _.each(self.accounts, function(account) {
 
225
                var auth_keys = _.extend(_.clone(account.account_defaults), {
 
226
                    name: true, unit_amount: true, date: true, account_id: true, task_id: true,
 
227
                });
 
228
                _.each(account.days, function(day) {
 
229
                    _.each(day.lines, function(line) {
 
230
                        if (line.unit_amount !== 0) {
 
231
                            var tmp = _.clone(line);
 
232
                            tmp.id = undefined;
 
233
                            _.each(line, function(v, k) {
 
234
                                if (v instanceof Array) {
 
235
                                    tmp[k] = v[0];
 
236
                                }
 
237
                            });
 
238
                            // we have to remove some keys, because analytic lines are shitty
 
239
                            _.each(_.keys(tmp), function(key) {
 
240
                                if (auth_keys[key] === undefined) {
 
241
                                    tmp[key] = undefined;
 
242
                                }
 
243
                            });
 
244
                            ops.push(tmp);
 
245
                        }
 
246
                    });
 
247
                });
 
248
            });
 
249
            return ops;
 
250
        },
 
251
    });
 
252
};