2012-09-28 12:45:38 +00:00
|
|
|
|
|
|
|
openerp.hr_timesheet_sheet = function(instance) {
|
|
|
|
var QWeb = instance.web.qweb;
|
|
|
|
var _t = instance.web._t;
|
|
|
|
|
2012-10-02 12:50:55 +00:00
|
|
|
instance.hr_timesheet_sheet.WeeklyTimesheet = instance.web.form.FormWidget.extend(instance.web.form.ReinitializeWidgetMixin, {
|
2012-10-26 09:55:11 +00:00
|
|
|
events: {
|
|
|
|
"click .oe_timesheet_weekly_account a": "go_to",
|
|
|
|
},
|
2014-06-26 13:31:55 +00:00
|
|
|
ignore_fields: function() {
|
|
|
|
return ['line_id'];
|
|
|
|
},
|
2012-09-28 14:34:03 +00:00
|
|
|
init: function() {
|
|
|
|
this._super.apply(this, arguments);
|
2014-01-15 16:51:14 +00:00
|
|
|
var self = this;
|
2012-09-28 15:08:15 +00:00
|
|
|
this.set({
|
|
|
|
sheets: [],
|
|
|
|
date_to: false,
|
|
|
|
date_from: false,
|
|
|
|
});
|
2012-12-17 11:13:37 +00:00
|
|
|
this.updating = false;
|
2014-01-15 16:51:14 +00:00
|
|
|
this.defs = [];
|
2012-09-28 14:34:03 +00:00
|
|
|
this.field_manager.on("field_changed:timesheet_ids", this, this.query_sheets);
|
2012-09-28 15:08:15 +00:00
|
|
|
this.field_manager.on("field_changed:date_from", this, function() {
|
|
|
|
this.set({"date_from": instance.web.str_to_date(this.field_manager.get_field_value("date_from"))});
|
|
|
|
});
|
|
|
|
this.field_manager.on("field_changed:date_to", this, function() {
|
|
|
|
this.set({"date_to": instance.web.str_to_date(this.field_manager.get_field_value("date_to"))});
|
|
|
|
});
|
2012-10-02 11:15:37 +00:00
|
|
|
this.field_manager.on("field_changed:user_id", this, function() {
|
|
|
|
this.set({"user_id": this.field_manager.get_field_value("user_id")});
|
|
|
|
});
|
2012-09-28 14:34:03 +00:00
|
|
|
this.on("change:sheets", this, this.update_sheets);
|
|
|
|
this.res_o2m_drop = new instance.web.DropMisordered();
|
2012-10-01 11:09:48 +00:00
|
|
|
this.render_drop = new instance.web.DropMisordered();
|
2012-10-01 15:19:17 +00:00
|
|
|
this.description_line = _t("/");
|
2014-01-15 16:51:14 +00:00
|
|
|
// Original save function is overwritten in order to wait all running deferreds to be done before actually applying the save.
|
|
|
|
this.view.original_save = _.bind(this.view.save, this.view);
|
|
|
|
this.view.save = function(prepend_on_create){
|
|
|
|
self.prepend_on_create = prepend_on_create;
|
|
|
|
return $.when.apply($, self.defs).then(function(){
|
|
|
|
return self.view.original_save(self.prepend_on_create);
|
|
|
|
});
|
|
|
|
};
|
2012-09-28 14:34:03 +00:00
|
|
|
},
|
2012-10-26 09:55:11 +00:00
|
|
|
go_to: function(event) {
|
|
|
|
var id = JSON.parse($(event.target).data("id"));
|
|
|
|
this.do_action({
|
|
|
|
type: 'ir.actions.act_window',
|
|
|
|
res_model: "account.analytic.account",
|
|
|
|
res_id: id,
|
|
|
|
views: [[false, 'form']],
|
|
|
|
target: 'current'
|
|
|
|
});
|
|
|
|
},
|
2012-09-28 14:34:03 +00:00
|
|
|
query_sheets: function() {
|
|
|
|
var self = this;
|
|
|
|
if (self.updating)
|
|
|
|
return;
|
|
|
|
var commands = this.field_manager.get_field_value("timesheet_ids");
|
2012-10-02 09:58:15 +00:00
|
|
|
this.res_o2m_drop.add(new instance.web.Model(this.view.model).call("resolve_2many_commands", ["timesheet_ids", commands, [],
|
|
|
|
new instance.web.CompoundContext()]))
|
2012-10-31 10:31:48 +00:00
|
|
|
.done(function(result) {
|
2012-09-28 14:34:03 +00:00
|
|
|
self.querying = true;
|
|
|
|
self.set({sheets: result});
|
|
|
|
self.querying = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
update_sheets: function() {
|
|
|
|
var self = this;
|
|
|
|
if (self.querying)
|
|
|
|
return;
|
|
|
|
self.updating = true;
|
2012-10-31 10:31:48 +00:00
|
|
|
self.field_manager.set_values({timesheet_ids: self.get("sheets")}).done(function() {
|
2012-09-28 14:34:03 +00:00
|
|
|
self.updating = false;
|
|
|
|
});
|
|
|
|
},
|
2012-10-02 13:25:47 +00:00
|
|
|
initialize_field: function() {
|
|
|
|
instance.web.form.ReinitializeWidgetMixin.initialize_field.call(this);
|
2012-10-02 09:58:15 +00:00
|
|
|
var self = this;
|
2012-10-02 12:50:55 +00:00
|
|
|
self.on("change:sheets", self, self.initialize_content);
|
|
|
|
self.on("change:date_to", self, self.initialize_content);
|
|
|
|
self.on("change:date_from", self, self.initialize_content);
|
|
|
|
self.on("change:user_id", self, self.initialize_content);
|
2012-09-28 15:08:15 +00:00
|
|
|
},
|
2012-10-02 12:50:55 +00:00
|
|
|
initialize_content: function() {
|
2012-09-28 15:08:15 +00:00
|
|
|
var self = this;
|
|
|
|
if (self.setting)
|
|
|
|
return;
|
|
|
|
// don't render anything until we have date_to and date_from
|
|
|
|
if (!self.get("date_to") || !self.get("date_from"))
|
|
|
|
return;
|
2012-10-08 12:42:48 +00:00
|
|
|
this.destroy_content();
|
2012-09-28 16:38:11 +00:00
|
|
|
|
2012-10-02 11:15:37 +00:00
|
|
|
// it's important to use those vars to avoid race conditions
|
|
|
|
var dates;
|
|
|
|
var accounts;
|
|
|
|
var account_names;
|
2012-10-02 12:11:03 +00:00
|
|
|
var default_get;
|
2012-10-02 11:15:37 +00:00
|
|
|
return this.render_drop.add(new instance.web.Model("hr.analytic.timesheet").call("default_get", [
|
|
|
|
['account_id','general_account_id', 'journal_id','date','name','user_id','product_id','product_uom_id','to_invoice','amount','unit_amount'],
|
2012-10-31 10:31:48 +00:00
|
|
|
new instance.web.CompoundContext({'user_id': self.get('user_id')})]).then(function(result) {
|
2012-10-02 12:11:03 +00:00
|
|
|
default_get = result;
|
2012-10-02 11:15:37 +00:00
|
|
|
// calculating dates
|
|
|
|
dates = [];
|
|
|
|
var start = self.get("date_from");
|
|
|
|
var end = self.get("date_to");
|
|
|
|
while (start <= end) {
|
|
|
|
dates.push(start);
|
|
|
|
start = start.clone().addDays(1);
|
|
|
|
}
|
|
|
|
// group by account
|
|
|
|
accounts = _(self.get("sheets")).chain()
|
|
|
|
.map(function(el) {
|
|
|
|
// much simpler to use only the id in all cases
|
|
|
|
if (typeof(el.account_id) === "object")
|
|
|
|
el.account_id = el.account_id[0];
|
|
|
|
return el;
|
|
|
|
})
|
2012-10-04 13:48:58 +00:00
|
|
|
.groupBy("account_id").value();
|
2012-10-02 11:15:37 +00:00
|
|
|
|
2012-10-04 13:48:58 +00:00
|
|
|
var account_ids = _.map(_.keys(accounts), function(el) { return el === "false" ? false : Number(el) });
|
|
|
|
|
|
|
|
return new instance.web.Model("hr.analytic.timesheet").call("multi_on_change_account_id", [[], account_ids,
|
2012-10-31 10:31:48 +00:00
|
|
|
new instance.web.CompoundContext({'user_id': self.get('user_id')})]).then(function(accounts_defaults) {
|
2012-10-04 13:48:58 +00:00
|
|
|
accounts = _(accounts).chain().map(function(lines, account_id) {
|
2012-11-08 16:40:53 +00:00
|
|
|
account_defaults = _.extend({}, default_get, (accounts_defaults[account_id] || {}).value || {});
|
2012-10-04 13:48:58 +00:00
|
|
|
// group by days
|
|
|
|
account_id = account_id === "false" ? false : Number(account_id);
|
|
|
|
var index = _.groupBy(lines, "date");
|
|
|
|
var days = _.map(dates, function(date) {
|
|
|
|
var day = {day: date, lines: index[instance.web.date_to_str(date)] || []};
|
|
|
|
// add line where we will insert/remove hours
|
|
|
|
var to_add = _.find(day.lines, function(line) { return line.name === self.description_line });
|
|
|
|
if (to_add) {
|
|
|
|
day.lines = _.without(day.lines, to_add);
|
|
|
|
day.lines.unshift(to_add);
|
|
|
|
} else {
|
|
|
|
day.lines.unshift(_.extend(_.clone(account_defaults), {
|
|
|
|
name: self.description_line,
|
|
|
|
unit_amount: 0,
|
|
|
|
date: instance.web.date_to_str(date),
|
|
|
|
account_id: account_id,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return day;
|
|
|
|
});
|
|
|
|
return {account: account_id, days: days, account_defaults: account_defaults};
|
|
|
|
}).value();
|
|
|
|
|
|
|
|
// we need the name_get of the analytic accounts
|
|
|
|
return new instance.web.Model("account.analytic.account").call("name_get", [_.pluck(accounts, "account"),
|
2012-10-31 10:31:48 +00:00
|
|
|
new instance.web.CompoundContext()]).then(function(result) {
|
2012-10-04 13:48:58 +00:00
|
|
|
account_names = {};
|
|
|
|
_.each(result, function(el) {
|
|
|
|
account_names[el[0]] = el[1];
|
|
|
|
});
|
2012-10-08 12:42:48 +00:00
|
|
|
accounts = _.sortBy(accounts, function(el) {
|
|
|
|
return account_names[el.account];
|
|
|
|
});
|
2012-10-04 13:48:58 +00:00
|
|
|
});;
|
|
|
|
});
|
2012-10-31 10:31:48 +00:00
|
|
|
})).then(function(result) {
|
2012-10-02 11:15:37 +00:00
|
|
|
// we put all the gathered data in self, then we render
|
|
|
|
self.dates = dates;
|
|
|
|
self.accounts = accounts;
|
|
|
|
self.account_names = account_names;
|
2012-10-08 12:42:48 +00:00
|
|
|
self.default_get = default_get;
|
2012-10-01 11:09:48 +00:00
|
|
|
//real rendering
|
2012-10-01 14:47:50 +00:00
|
|
|
self.display_data();
|
|
|
|
});
|
|
|
|
},
|
2012-10-05 13:53:52 +00:00
|
|
|
destroy_content: function() {
|
|
|
|
if (this.dfm) {
|
|
|
|
this.dfm.destroy();
|
|
|
|
this.dfm = undefined;
|
|
|
|
}
|
|
|
|
},
|
2013-05-23 12:05:17 +00:00
|
|
|
is_valid_value:function(value){
|
|
|
|
var split_value = value.split(":");
|
2013-05-23 12:41:17 +00:00
|
|
|
var valid_value = true;
|
2013-05-24 08:55:34 +00:00
|
|
|
if (split_value.length > 2)
|
|
|
|
return false;
|
2013-05-23 12:05:17 +00:00
|
|
|
_.detect(split_value,function(num){
|
|
|
|
if(isNaN(num)){
|
2013-05-23 12:41:17 +00:00
|
|
|
valid_value = false;
|
2013-05-23 12:05:17 +00:00
|
|
|
}
|
2013-05-24 08:55:34 +00:00
|
|
|
});
|
2013-05-23 12:41:17 +00:00
|
|
|
return valid_value;
|
2013-05-23 12:05:17 +00:00
|
|
|
},
|
2012-10-01 14:47:50 +00:00
|
|
|
display_data: function() {
|
|
|
|
var self = this;
|
|
|
|
self.$el.html(QWeb.render("hr_timesheet_sheet.WeeklyTimesheet", {widget: self}));
|
|
|
|
_.each(self.accounts, function(account) {
|
|
|
|
_.each(_.range(account.days.length), function(day_count) {
|
2012-10-02 12:50:55 +00:00
|
|
|
if (!self.get('effective_readonly')) {
|
2013-05-23 12:05:17 +00:00
|
|
|
self.get_box(account, day_count).val(self.sum_box(account, day_count, true)).change(function() {
|
|
|
|
var num = $(this).val();
|
|
|
|
if (self.is_valid_value(num)){
|
2013-05-24 08:55:34 +00:00
|
|
|
num = (num == 0)?0:Number(self.parse_client(num));
|
|
|
|
}
|
2012-10-02 12:50:55 +00:00
|
|
|
if (isNaN(num)) {
|
2013-05-23 12:05:17 +00:00
|
|
|
$(this).val(self.sum_box(account, day_count, true));
|
2012-10-02 12:50:55 +00:00
|
|
|
} else {
|
|
|
|
account.days[day_count].lines[0].unit_amount += num - self.sum_box(account, day_count);
|
2013-03-20 11:52:38 +00:00
|
|
|
var product = (account.days[day_count].lines[0].product_id instanceof Array) ? account.days[day_count].lines[0].product_id[0] : account.days[day_count].lines[0].product_id
|
|
|
|
var journal = (account.days[day_count].lines[0].journal_id instanceof Array) ? account.days[day_count].lines[0].journal_id[0] : account.days[day_count].lines[0].journal_id
|
2014-01-15 16:51:14 +00:00
|
|
|
self.defs.push(new instance.web.Model("hr.analytic.timesheet").call("on_change_unit_amount", [[], product, account.days[day_count].lines[0].unit_amount, false, false, journal]).then(function(res) {
|
2013-03-20 11:52:38 +00:00
|
|
|
account.days[day_count].lines[0]['amount'] = res.value.amount || 0;
|
|
|
|
self.display_totals();
|
|
|
|
self.sync();
|
2014-01-15 16:51:14 +00:00
|
|
|
}));
|
2013-05-24 08:55:34 +00:00
|
|
|
if(!isNaN($(this).val())){
|
2013-05-23 12:05:17 +00:00
|
|
|
$(this).val(self.sum_box(account, day_count, true));
|
|
|
|
}
|
2012-10-02 12:50:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2013-05-23 12:05:17 +00:00
|
|
|
self.get_box(account, day_count).html(self.sum_box(account, day_count, true));
|
2012-10-02 12:50:55 +00:00
|
|
|
}
|
2012-10-01 14:47:50 +00:00
|
|
|
});
|
|
|
|
});
|
2012-10-02 15:02:50 +00:00
|
|
|
self.display_totals();
|
2012-10-08 12:42:48 +00:00
|
|
|
self.$(".oe_timesheet_weekly_adding button").click(_.bind(this.init_add_account, this));
|
|
|
|
},
|
|
|
|
init_add_account: function() {
|
|
|
|
var self = this;
|
|
|
|
if (self.dfm)
|
|
|
|
return;
|
|
|
|
self.$(".oe_timesheet_weekly_add_row").show();
|
|
|
|
self.dfm = new instance.web.form.DefaultFieldManager(self);
|
|
|
|
self.dfm.extend_field_desc({
|
|
|
|
account: {
|
|
|
|
relation: "account.analytic.account",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
self.account_m2o = new instance.web.form.FieldMany2One(self.dfm, {
|
|
|
|
attrs: {
|
|
|
|
name: "account",
|
|
|
|
type: "many2one",
|
2012-10-08 12:44:29 +00:00
|
|
|
domain: [
|
|
|
|
['type','in',['normal', 'contract']],
|
|
|
|
['state', '<>', 'close'],
|
|
|
|
['use_timesheets','=',1],
|
|
|
|
['id', 'not in', _.pluck(self.accounts, "account")],
|
|
|
|
],
|
2012-10-26 07:57:15 +00:00
|
|
|
context: {
|
|
|
|
default_use_timesheets: 1,
|
|
|
|
default_type: "contract",
|
|
|
|
},
|
2012-10-08 12:42:48 +00:00
|
|
|
modifiers: '{"required": true}',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
self.account_m2o.prependTo(self.$(".oe_timesheet_weekly_add_row td"));
|
|
|
|
self.$(".oe_timesheet_weekly_add_row button").click(function() {
|
|
|
|
var id = self.account_m2o.get_value();
|
|
|
|
if (id === false) {
|
|
|
|
self.dfm.set({display_invalid_fields: true});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var ops = self.generate_o2m_value();
|
2012-10-31 10:31:48 +00:00
|
|
|
new instance.web.Model("hr.analytic.timesheet").call("on_change_account_id", [[], id]).then(function(res) {
|
2012-10-08 12:42:48 +00:00
|
|
|
var def = _.extend({}, self.default_get, res.value, {
|
|
|
|
name: self.description_line,
|
|
|
|
unit_amount: 0,
|
|
|
|
date: instance.web.date_to_str(self.dates[0]),
|
|
|
|
account_id: id,
|
|
|
|
});
|
|
|
|
ops.push(def);
|
|
|
|
self.set({"sheets": ops});
|
2012-10-05 13:53:52 +00:00
|
|
|
});
|
|
|
|
});
|
2012-10-01 14:47:50 +00:00
|
|
|
},
|
2012-10-02 12:50:55 +00:00
|
|
|
get_box: function(account, day_count) {
|
2012-10-01 14:47:50 +00:00
|
|
|
return this.$('[data-account="' + account.account + '"][data-day-count="' + day_count + '"]');
|
|
|
|
},
|
|
|
|
get_total: function(account) {
|
|
|
|
return this.$('[data-account-total="' + account.account + '"]');
|
|
|
|
},
|
2012-10-02 14:38:48 +00:00
|
|
|
get_day_total: function(day_count) {
|
|
|
|
return this.$('[data-day-total="' + day_count + '"]');
|
|
|
|
},
|
|
|
|
get_super_total: function() {
|
|
|
|
return this.$('.oe_timesheet_weekly_supertotal');
|
|
|
|
},
|
2013-05-23 12:41:17 +00:00
|
|
|
sum_box: function(account, day_count, show_value_in_hour) {
|
2012-10-01 14:47:50 +00:00
|
|
|
var line_total = 0;
|
|
|
|
_.each(account.days[day_count].lines, function(line) {
|
|
|
|
line_total += line.unit_amount;
|
|
|
|
});
|
2013-05-24 08:55:34 +00:00
|
|
|
return (show_value_in_hour && line_total != 0)?this.format_client(line_total):line_total;
|
2012-10-01 14:47:50 +00:00
|
|
|
},
|
2012-10-02 15:02:50 +00:00
|
|
|
display_totals: function() {
|
2012-10-02 14:38:48 +00:00
|
|
|
var self = this;
|
2012-10-02 15:02:50 +00:00
|
|
|
var day_tots = _.map(_.range(self.dates.length), function() { return 0 });
|
|
|
|
var super_tot = 0;
|
2012-10-02 14:38:48 +00:00
|
|
|
_.each(self.accounts, function(account) {
|
2012-10-02 15:02:50 +00:00
|
|
|
var acc_tot = 0;
|
|
|
|
_.each(_.range(self.dates.length), function(day_count) {
|
|
|
|
var sum = self.sum_box(account, day_count);
|
|
|
|
acc_tot += sum;
|
|
|
|
day_tots[day_count] += sum;
|
|
|
|
super_tot += sum;
|
|
|
|
});
|
2013-05-23 12:05:17 +00:00
|
|
|
self.get_total(account).html(self.format_client(acc_tot));
|
2012-10-02 15:02:50 +00:00
|
|
|
});
|
|
|
|
_.each(_.range(self.dates.length), function(day_count) {
|
2013-05-23 12:05:17 +00:00
|
|
|
self.get_day_total(day_count).html(self.format_client(day_tots[day_count]));
|
2012-10-02 14:38:48 +00:00
|
|
|
});
|
2013-05-23 12:05:17 +00:00
|
|
|
self.get_super_total().html(self.format_client(super_tot));
|
2012-10-02 14:38:48 +00:00
|
|
|
},
|
2012-10-01 14:55:16 +00:00
|
|
|
sync: function() {
|
2012-10-08 16:22:24 +00:00
|
|
|
var self = this;
|
2012-10-08 12:42:48 +00:00
|
|
|
self.setting = true;
|
|
|
|
self.set({sheets: this.generate_o2m_value()});
|
|
|
|
self.setting = false;
|
|
|
|
},
|
2013-05-23 12:05:17 +00:00
|
|
|
//converts hour value to float
|
|
|
|
parse_client: function(value) {
|
|
|
|
return instance.web.parse_value(value, { type:"float_time" });
|
|
|
|
},
|
|
|
|
//converts float value to hour
|
|
|
|
format_client:function(value){
|
|
|
|
return instance.web.format_value(value, { type:"float_time" });
|
|
|
|
},
|
2012-10-08 12:42:48 +00:00
|
|
|
generate_o2m_value: function() {
|
2012-10-01 14:55:16 +00:00
|
|
|
var self = this;
|
2012-10-02 11:15:37 +00:00
|
|
|
var ops = [];
|
2014-06-26 13:31:55 +00:00
|
|
|
var ignored_fields = self.ignore_fields();
|
2012-10-01 14:55:16 +00:00
|
|
|
_.each(self.accounts, function(account) {
|
|
|
|
_.each(account.days, function(day) {
|
|
|
|
_.each(day.lines, function(line) {
|
2012-10-02 09:58:15 +00:00
|
|
|
if (line.unit_amount !== 0) {
|
2012-10-01 14:55:16 +00:00
|
|
|
var tmp = _.clone(line);
|
2012-10-02 11:15:37 +00:00
|
|
|
tmp.id = undefined;
|
|
|
|
_.each(line, function(v, k) {
|
|
|
|
if (v instanceof Array) {
|
|
|
|
tmp[k] = v[0];
|
|
|
|
}
|
|
|
|
});
|
2014-06-26 13:31:55 +00:00
|
|
|
// we remove line_id as the reference to the _inherits field will no longer exists
|
|
|
|
tmp = _.omit(tmp, ignored_fields);
|
2012-10-02 11:15:37 +00:00
|
|
|
ops.push(tmp);
|
2012-10-01 14:55:16 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-10-08 12:42:48 +00:00
|
|
|
return ops;
|
2012-10-01 14:55:16 +00:00
|
|
|
},
|
2012-09-28 12:45:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
instance.web.form.custom_widgets.add('weekly_timesheet', 'instance.hr_timesheet_sheet.WeeklyTimesheet');
|
|
|
|
|
|
|
|
};
|