Odoo 11.0前端任务工时表生成及扩展模块开发问询
Hey there, let's break down how to implement your two Odoo 11.0 requirements effectively!
Requirement 1: Generate Task-Linked Timesheets on Odoo Website Frontend
The goal here is to let users create timesheets directly from your website that auto-link to specific tasks. Here's how to pull it off:
- Pass Task Context: Use a URL parameter (like
?task_id=123) to carry the target task ID to the frontend page. - Build Frontend Form: Create a simple form with core timesheet fields: task ID (hidden or pre-filled), date, hours worked, and description.
- Submit via JSON-RPC: Use Odoo's native JSON-RPC API to send form data to the backend, creating an
account.analytic.linerecord (Odoo's default timesheet model) tied to the task.
Requirement 2: Custom Module for Task-Based Timesheet, Material & Expense Management
This module will extend task functionality to include timesheet management, material usage tracking, and expense logging. Here's the breakdown of key components:
1. Model Extensions
- Inherit the
project.taskmodel to add one-to-many relationships for timesheets, materials, and expenses. - Create two custom models:
project.task.material: Tracks material usage (task ID, product, quantity, unit, usage date)project.task.expense: Tracks task-related expenses (task ID, expense type, amount, description, date)
2. View Structure (Formatted Frontend Code)
Here's your provided view code adapted for Odoo's QWeb view system, with tabbed navigation for each feature:
<div class='row' style="margin: 5px;"> <div class="col-md-12 col-sm-12" style="margin-top: 15px;"> <button class="btn btn-default" onclick="openTab('TimeSheet')">TimeSheet</button> <button class="btn btn-default" onclick="openTab('Materials')">Materials</button> <button class="btn btn-default" onclick="openTab('Expenses')">Expenses</button> </div> <!-- Timesheet Tab Content --> <div id="TimeSheet" class="tab-content" style="display:block;"> <field name="timesheet_ids" widget="one2many_list" /> <button type="object" name="action_add_timesheet" class="btn btn-primary">Add New Time Sheet</button> </div> <!-- Material Usage Tab Content --> <div id="Materials" class="tab-content" style="display:none;"> <field name="material_ids" widget="one2many_list" /> <button type="object" name="action_add_material" class="btn btn-primary">Add Material Usage</button> </div> <!-- Expense Management Tab Content --> <div id="Expenses" class="tab-content" style="display:none;"> <field name="expense_ids" widget="one2many_list" /> <button type="object" name="action_add_expense" class="btn btn-primary">Add Expense</button> </div> </div> <script type="text/javascript"> function openTab(tabName) { // Hide all tab contents const tabContents = document.getElementsByClassName("tab-content"); for (let i = 0; i < tabContents.length; i++) { tabContents[i].style.display = "none"; } // Show selected tab document.getElementById(tabName).style.display = "block"; } </script>
3. Core Business Logic (Python Snippet)
Add this to your module's models/project_task.py to handle record creation tied to the current task:
from odoo import models, fields, api class ProjectTask(models.Model): _inherit = 'project.task' timesheet_ids = fields.One2many('account.analytic.line', 'task_id', string='Time Sheets') material_ids = fields.One2many('project.task.material', 'task_id', string='Material Usages') expense_ids = fields.One2many('project.task.expense', 'task_id', string='Expenses') def action_add_timesheet(self): return { 'name': 'Add Time Sheet', 'type': 'ir.actions.act_window', 'res_model': 'account.analytic.line', 'view_mode': 'form', 'view_type': 'form', 'context': {'default_task_id': self.id}, 'target': 'new' } # Add similar methods for materials and expenses
4. Website Frontend Integration (JS Snippet)
For the website-based timesheet creation (Requirement 1), use this JS to submit data via Odoo's API:
odoo.define('task_timesheet_portal.form', function (require) { 'use strict'; const ajax = require('web.ajax'); $('#submit-timesheet').click(function() { const taskId = parseInt($('#task_id').val()); const date = $('#timesheet_date').val(); const hours = parseFloat($('#timesheet_hours').val()); const description = $('#timesheet_description').val(); ajax.jsonRpc('/web/dataset/call_kw', 'call', { model: 'account.analytic.line', method: 'create', args: [[{ 'task_id': taskId, 'date': date, 'unit_amount': hours, 'name': description, 'employee_id': odoo.session_info.employee_id, }]], kwargs: {} }).then(() => { alert('Time sheet created successfully!'); $('#timesheet-form')[0].reset(); }); }); });
内容的提问来源于stack exchange,提问作者Dario




