From 822f6bbe7b315be378df1751e56e6b25304bb3a8 Mon Sep 17 00:00:00 2001 From: niv-openerp Date: Tue, 2 Oct 2012 13:15:37 +0200 Subject: [PATCH] Made if really work bzr revid: nicolas.vanhoren@openerp.com-20121002111537-6ksjtqt5k0b6jcq4 --- .../static/src/js/timesheet.js | 142 ++++++++++-------- 1 file changed, 81 insertions(+), 61 deletions(-) diff --git a/addons/hr_timesheet_sheet/static/src/js/timesheet.js b/addons/hr_timesheet_sheet/static/src/js/timesheet.js index cef325d93a7..1077fa5e6a3 100644 --- a/addons/hr_timesheet_sheet/static/src/js/timesheet.js +++ b/addons/hr_timesheet_sheet/static/src/js/timesheet.js @@ -18,6 +18,9 @@ openerp.hr_timesheet_sheet = function(instance) { 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"))}); }); + this.field_manager.on("field_changed:user_id", this, function() { + this.set({"user_id": this.field_manager.get_field_value("user_id")}); + }); this.on("change:sheets", this, this.update_sheets); this.res_o2m_drop = new instance.web.DropMisordered(); this.render_drop = new instance.web.DropMisordered(); @@ -47,13 +50,11 @@ openerp.hr_timesheet_sheet = function(instance) { }, start: function() { var self = this; - return new instance.web.Model("hr.analytic.timesheet").call("default_get", [[], new instance.web.CompoundContext()]).pipe(function(result) { - self.default_get = result; - self.on("change:sheets", self, self.render); - self.on("change:date_to", self, self.render); - self.on("change:date_from", self, self.render); - self.render(); - }); + self.on("change:sheets", self, self.render); + self.on("change:date_to", self, self.render); + self.on("change:date_from", self, self.render); + self.on("change:user_id", self, self.render); + self.render(); }, render: function() { var self = this; @@ -62,57 +63,71 @@ openerp.hr_timesheet_sheet = function(instance) { // don't render anything until we have date_to and date_from if (!self.get("date_to") || !self.get("date_from")) return; - // calculating dates - self.dates = []; - var start = self.get("date_from"); - var end = self.get("date_to"); - while (start <= end) { - self.dates.push(start); - start = start.clone().addDays(1); - } - // group by account - self.accounts = _(this.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; - }) - .groupBy("account_id") - .map(function(lines, account_id) { - // group by days - account_id = account_id === "false" ? false : Number(account_id); - var index = _.groupBy(lines, "date"); - var days = _.map(self.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(to_add); - day.lines.unshift(to_add); - } else { - day.lines.unshift(_.extend(_.clone(self.default_get), { - 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}; - }).sortBy(function(account) { - return account.account; - }).value(); - // we need the name_get of the analytic accounts - this.render_drop.add(new instance.web.Model("account.analytic.account").call("name_get", [_.pluck(self.accounts, "account"), - new instance.web.CompoundContext()])) - .pipe(function(result) { - self.account_names = {}; - _.each(result, function(el) { - self.account_names[el[0]] = el[1]; - }); + // it's important to use those vars to avoid race conditions + var dates; + var accounts; + var account_names; + 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'], + new instance.web.CompoundContext({'user_id': self.get('user_id')})]).pipe(function(result) { + var default_get = result; + // 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; + }) + .groupBy("account_id") + .map(function(lines, account_id) { + // 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(to_add); + day.lines.unshift(to_add); + } else { + day.lines.unshift(_.extend(_.clone(default_get), { + 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}; + }).sortBy(function(account) { + return account.account; + }).value(); + + // we need the name_get of the analytic accounts + return new instance.web.Model("account.analytic.account").call("name_get", [_.pluck(accounts, "account"), + new instance.web.CompoundContext()]).pipe(function(result) { + account_names = {}; + _.each(result, function(el) { + account_names[el[0]] = el[1]; + }); + });; + })).pipe(function(result) { + // we put all the gathered data in self, then we render + self.dates = dates; + self.accounts = accounts; + self.account_names = account_names; //real rendering self.display_data(); }); @@ -160,20 +175,25 @@ openerp.hr_timesheet_sheet = function(instance) { }, sync: function() { var self = this; - var lst = []; + var ops = []; _.each(self.accounts, function(account) { _.each(account.days, function(day) { _.each(day.lines, function(line) { if (line.unit_amount !== 0) { var tmp = _.clone(line); - //line.id = undefined; - lst.push(tmp); + tmp.id = undefined; + _.each(line, function(v, k) { + if (v instanceof Array) { + tmp[k] = v[0]; + } + }); + ops.push(tmp); } }); }); }); self.setting = true; - self.set({sheets: lst}); + self.set({sheets: ops}); self.setting = false; }, });