diff --git a/models/activity.js b/models/activity.js index 16a8ca05..d11747b9 100644 --- a/models/activity.js +++ b/models/activity.js @@ -496,32 +496,51 @@ function sanitizeId(value) { //enrichment over /** - * Retrieves the name of an activity type from the local SQLite database - * based on the provided Odoo record ID. + * Retrieves the name of an activity type from the local SQLite database. * - * @function getActivityTypeName - * @param {number} odooRecordId - The ID of the activity type as stored in Odoo. - * @returns {string} - Returns the name of the activity type if found, otherwise an empty string. + * Local Account activity types use the local SQLite id, while + * Odoo account activity types use the odoo_record_id. * - * @description - * Opens a local SQLite database transaction and queries the `mail_activity_type_app` table - * for a record matching the given `odooRecordId`. - * Extracts the `name` field from the result and returns it. - * Logs any exception encountered during the operation via `DBCommon.logException()`. + * @function getActivityTypeName + * @param {number} activityTypeId - The activity type ID. + * @param {number} accountId - The account ID. + * @returns {string} - Returns the activity type name if found, otherwise an empty string. */ -function getActivityTypeName(odooRecordId) { +function getActivityTypeName(activityTypeId, accountId) { + if (!activityTypeId || activityTypeId <= 0) { + return ""; + } + var typeName = ""; try { - var db = Sql.LocalStorage.openDatabaseSync(DBCommon.NAME, DBCommon.VERSION, DBCommon.DISPLAY_NAME, DBCommon.SIZE); + var db = Sql.LocalStorage.openDatabaseSync( + DBCommon.NAME, + DBCommon.VERSION, + DBCommon.DISPLAY_NAME, + DBCommon.SIZE + ); db.transaction(function (tx) { - var query = ` - SELECT name FROM mail_activity_type_app - WHERE odoo_record_id = ? - LIMIT 1 - `; - var rs = tx.executeSql(query, [odooRecordId]); + var query = ""; + var params = []; + + if (accountId === 0) { + query = "SELECT name FROM mail_activity_type_app " + + "WHERE account_id = 0 AND id = ? LIMIT 1"; + params = [activityTypeId]; + } else if (accountId !== undefined && accountId !== null && accountId > 0) { + query = "SELECT name FROM mail_activity_type_app " + + "WHERE account_id = ? AND odoo_record_id = ? LIMIT 1"; + params = [accountId, activityTypeId]; + } else { + query = "SELECT name FROM mail_activity_type_app " + + "WHERE (odoo_record_id = ? AND odoo_record_id > 0) " + + "OR id = ? LIMIT 1"; + params = [activityTypeId, activityTypeId]; + } + + var rs = tx.executeSql(query, params); if (rs.rows.length > 0) { typeName = rs.rows.item(0).name; @@ -534,7 +553,6 @@ function getActivityTypeName(odooRecordId) { return typeName; } - /** * Marks a specific activity record as "done" in the local SQLite database * by updating its `state` and `status` fields. @@ -1848,7 +1866,7 @@ function passesActivitySearchFilter(activity, searchQuery) { } - var activityTypeName = getActivityTypeName(activity.activity_type_id); + var activityTypeName = getActivityTypeName(activity.activity_type_id, activity.account_id); if (activityTypeName && activityTypeName.toLowerCase().indexOf(query) >= 0) { return true; } @@ -2098,4 +2116,4 @@ function getAllDoneActivitiesPaginated(limit, offset) { } return activityList; -} \ No newline at end of file +} diff --git a/models/database.js b/models/database.js index 9383e53a..be7aef50 100644 --- a/models/database.js +++ b/models/database.js @@ -169,6 +169,37 @@ function ensureDefaultLocalAccountExists() { } } +/** + * Ensures default activity types exist for the Local Account. + * + * Local accounts do not sync activity types from Odoo, so provide + * a predefined set of activity types during database initialization. + */ +function ensureDefaultLocalActivityTypes() { + const defaultTypes = ["To Do", "Call", "Email", "Meeting"]; + + try { + const db = Sql.LocalStorage.openDatabaseSync(NAME, VERSION, DISPLAY_NAME, SIZE); + + db.transaction(function (tx) { + defaultTypes.forEach(function (typeName, index) { + const result = tx.executeSql( + "SELECT id FROM mail_activity_type_app WHERE account_id = ? AND name = ? AND (status IS NULL OR status != 'deleted')", + [0, typeName] + ); + + if (result.rows.length === 0) { + tx.executeSql( + "INSERT INTO mail_activity_type_app (account_id, name, status, odoo_record_id) VALUES (?, ?, ?, ?)", + [0, typeName, "", -(index + 1)] + ); + } + }); + }); + } catch (e) { + logException("ensureDefaultLocalActivityTypes", e); + } +} /** * Creates a table if it doesn't exist, and ensures all expected columns are present. * diff --git a/models/dbinit.js b/models/dbinit.js index 2191655e..36c76a17 100644 --- a/models/dbinit.js +++ b/models/dbinit.js @@ -231,6 +231,8 @@ function initializeDatabase() { )', ['id INTEGER', 'account_id INTEGER', 'name TEXT', 'status TEXT DEFAULT ""', 'odoo_record_id INTEGER'] ); + // Ensure default activity types for Local Account + DBCommon.ensureDefaultLocalActivityTypes(); DBCommon.createOrUpdateTable("ir_model_app", diff --git a/qml/components/cards/ActivityDetailsCard.qml b/qml/components/cards/ActivityDetailsCard.qml index 997d8a8c..7cccd322 100644 --- a/qml/components/cards/ActivityDetailsCard.qml +++ b/qml/components/cards/ActivityDetailsCard.qml @@ -345,7 +345,7 @@ ListItem { spacing: units.gu(0.4) Text { - text: root.activity_type_name || (i18n.dtr("ubtms", "Type ID: ") + root.activity_type_id) + text: root.activity_type_name || i18n.dtr("ubtms", "No Type") font.pixelSize: units.gu(1.5) horizontalAlignment: Text.AlignRight width: units.gu(6) diff --git a/qml/features/activities/pages/Activity_Page.qml b/qml/features/activities/pages/Activity_Page.qml index d4bf5d7e..e39bb5fb 100644 --- a/qml/features/activities/pages/Activity_Page.qml +++ b/qml/features/activities/pages/Activity_Page.qml @@ -337,7 +337,7 @@ Page { summary: item.summary, due_date: item.due_date, notes: item.notes, - activity_type_name: Activity.getActivityTypeName(item.activity_type_id), + activity_type_name: Activity.getActivityTypeName(item.activity_type_id, item.account_id), state: item.state, task_id: safeTaskId, task_name: taskName, @@ -575,7 +575,7 @@ Page { } // Search in activity type name - var activityTypeName = Activity.getActivityTypeName(item.activity_type_id); + var activityTypeName = Activity.getActivityTypeName(item.activity_type_id, item.account_id); if (activityTypeName && activityTypeName.toLowerCase().indexOf(query) >= 0) { return true; }