diff --git a/addons/web/static/src/js/views/calendar/calendar_model.js b/addons/web/static/src/js/views/calendar/calendar_model.js index dc27ff1bfdb..a12f8f0469d 100644 --- a/addons/web/static/src/js/views/calendar/calendar_model.js +++ b/addons/web/static/src/js/views/calendar/calendar_model.js @@ -297,8 +297,15 @@ return AbstractModel.extend({ this.data.end_date = this.data.start_date.clone().add(5, 'week').day(this.week_stop).endOf('day'); break; case 'week': - this.data.start_date = this.data.start_date.clone().day(this.week_start).startOf('day'); - this.data.end_date = this.data.end_date.clone().day(this.week_stop).endOf('day'); + var weekStart = this.data.start_date.clone().startOf('week'); + var weekStartDay = this.week_start; + if (this.data.start_date.day() < this.week_start) { + // The week's first day is after our current day + // Then we should go back to the previous week + weekStartDay -= 7; + } + this.data.start_date = this.data.start_date.clone().day(weekStartDay).startOf('day'); + this.data.end_date = this.data.end_date.clone().day(weekStartDay + 6).endOf('day'); break; default: this.data.start_date = this.data.start_date.clone().startOf('day'); diff --git a/addons/web/static/tests/views/calendar_tests.js b/addons/web/static/tests/views/calendar_tests.js index b4fae73ff70..6b7915c0e69 100644 --- a/addons/web/static/tests/views/calendar_tests.js +++ b/addons/web/static/tests/views/calendar_tests.js @@ -2838,7 +2838,7 @@ QUnit.module('Views', { assert.expect(8); // 2019-09-12 08:00:00 - var initDate = new Date(2019, 8, 12, 8, 0, 0); + var initDate = new Date(2019, 8, 15, 8, 0, 0); initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000); var calendar = await createCalendarView({ @@ -2892,6 +2892,96 @@ QUnit.module('Views', { calendar.destroy(); }); + + QUnit.test('Monday week start week mode', async function (assert) { + assert.expect(3); + + // 2019-09-12 08:00:00 + var initDate = new Date(2019, 8, 15, 8, 0, 0); + initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000); + + var calendar = await createCalendarView({ + View: CalendarView, + model: 'event', + data: this.data, + arch: + ''+ + '', + archs: archs, + + viewOptions: { + initialDate: initDate, + }, + translateParameters: { + week_start: 1, + }, + mockRPC: function (route, args) { + if (args.method === 'search_read' && args.model === 'event') { + assert.deepEqual(args.kwargs.domain, [ + ["start","<=","2019-09-15 23:59:59"], + ["stop",">=","2019-09-09 00:00:00"] + ], + 'The domain to search events in should be correct'); + } + return this._super.apply(this, arguments); + } + }); + + assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Mon 9", + "The first day of the week should be Monday the 9th"); + assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Sun 15", + "The last day of the week should be Sunday the 15th"); + + calendar.destroy(); + }); + + QUnit.test('Saturday week start week mode', async function (assert) { + assert.expect(3); + + // 2019-09-12 08:00:00 + var initDate = new Date(2019, 8, 12, 8, 0, 0); + initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000); + + var calendar = await createCalendarView({ + View: CalendarView, + model: 'event', + data: this.data, + arch: + ''+ + '', + archs: archs, + + viewOptions: { + initialDate: initDate, + }, + translateParameters: { + week_start: 6, + }, + mockRPC: function (route, args) { + if (args.method === 'search_read' && args.model === 'event') { + assert.deepEqual(args.kwargs.domain, [ + ["start","<=","2019-09-13 23:59:59"], + ["stop",">=","2019-09-07 00:00:00"] + ], + 'The domain to search events in should be correct'); + } + return this._super.apply(this, arguments); + } + }); + + assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Sat 7", + "The first day of the week should be Saturday the 7th"); + assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Fri 13", + "The last day of the week should be Friday the 13th"); + + calendar.destroy(); + }); }); });