1
openerp.hr_timesheet_task = function(instance) {
3
var module = instance.hr_timesheet_sheet
5
module.WeeklyTimesheet.include({
7
"click .oe_timesheet_weekly_account a": "go_to",
8
"click .oe_timesheet_weekly_task a": "go_to_task",
10
go_to_task : function(event) {
11
var id = JSON.parse($(event.target).data("task-id"));
13
type: 'ir.actions.act_window',
14
res_model: "project.task",
16
views: [[false, 'form']],
20
initialize_content: function() {
24
// don't render anything until we have date_to and date_from
25
if (!self.get("date_to") || !self.get("date_from"))
27
this.destroy_content();
29
// it's important to use those vars to avoid race conditions
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) {
41
var start = self.get("date_from");
42
var end = self.get("date_to");
43
while (start <= end) {
45
start = start.clone().addDays(1);
48
timesheet_lines = _(self.get("sheets")).chain()
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];
59
var timesheet_lines_by_account_id = _.groupBy(timesheet_lines, function(el) {
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];
68
var account_ids = _.map(_.keys(timesheet_lines_by_account_id), function(el) { return el === "false" ? false : Number(el) });
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 || {});
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 });
81
day.lines = _.without(day.lines, to_add);
82
day.lines.unshift(to_add);
84
day.lines.unshift(_.extend(_.clone(account_defaults), {
85
name: self.description_line,
87
date: instance.web.date_to_str(date),
88
account_id: lines[0].account_id,
89
task_id: lines[0].task_id,
94
return {account_task: account_id_task_id, account: lines[0].account_id, task: lines[0].task_id, days: days, account_defaults: account_defaults};
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) {
101
_.each(result, function(el) {
102
account_names[el[0]] = el[1];
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) {
108
_.each(result, function(el) {
109
task_names[el[0]] = el[1];
111
accounts = _.sortBy(accounts, function(el) {
112
return account_names[el.account];
117
})).then(function(result) {
118
// we put all the gathered data in self, then we render
120
self.accounts = accounts;
121
self.account_names = account_names;
122
self.task_names = task_names;
123
self.default_get = default_get;
128
init_add_account: function() {
132
self.$(".oe_timesheet_weekly_add_row").show();
133
self.dfm = new instance.web.form.DefaultFieldManager(self);
134
self.dfm.extend_field_desc({
136
relation: "account.analytic.account",
139
relation: "project.task",
142
self.account_m2o = new instance.web.form.FieldMany2One(self.dfm, {
147
['type','in',['normal', 'contract']],
148
['state', '<>', 'close'],
149
['use_timesheets','=',1],
152
default_use_timesheets: 1,
153
default_type: "contract",
155
modifiers: '{"required": true}',
158
self.task_m2o = new instance.web.form.FieldMany2One(self.dfm, {
163
// at this moment, it is always an empty list
164
['project_id.analytic_account_id','=',self.account_m2o.get_value()]
168
self.task_m2o.prependTo(self.$(".oe_timesheet_weekly_add_row td"));
169
self.account_m2o.prependTo(self.$(".oe_timesheet_weekly_add_row td"));
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, {
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")],
188
'account_id': account_id,
194
self.$(".oe_timesheet_weekly_add_row button").click(function() {
195
var id = self.account_m2o.get_value();
197
self.dfm.set({display_invalid_fields: true});
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,
205
date: instance.web.date_to_str(self.dates[0]),
207
task_id: self.task_m2o.get_value(),
210
self.set({"sheets": ops});
214
get_box: function(account, day_count) {
215
return this.$('[data-account-task="' + account.account_task + '"][data-day-count="' + day_count + '"]');
217
get_total: function(account) {
218
return this.$('[data-account-task-total="' + account.account_task + '"]');
220
generate_o2m_value: function() {
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,
228
_.each(account.days, function(day) {
229
_.each(day.lines, function(line) {
230
if (line.unit_amount !== 0) {
231
var tmp = _.clone(line);
233
_.each(line, function(v, k) {
234
if (v instanceof Array) {
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;