Skip to content

[16.0] [FIX] maintenance_request_purchase: fix problems when currency_id field is not set#537

Open
luisDIXMIT wants to merge 1 commit intoOCA:16.0from
dixmit:16.0-fix-maintenance_request_purchase
Open

[16.0] [FIX] maintenance_request_purchase: fix problems when currency_id field is not set#537
luisDIXMIT wants to merge 1 commit intoOCA:16.0from
dixmit:16.0-fix-maintenance_request_purchase

Conversation

@luisDIXMIT
Copy link
Contributor

@luisDIXMIT luisDIXMIT commented Dec 19, 2025

We have detected problems related to company_id not being set because it is not required and it can be forced to not have a default value. @etobella

@luisDIXMIT luisDIXMIT force-pushed the 16.0-fix-maintenance_request_purchase branch from 63b96d0 to 9ab1fc8 Compare December 19, 2025 11:19
Copy link

@marcos-mendez marcos-mendez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Review -- Tests Failed

1. Root Cause

The test failure is due to a database connection error during the Odoo startup, not directly caused by the code changes in the PR. This typically occurs in CI environments like Runboat when the test database is not properly initialized or accessible. However, the code change introduces a potential circular dependency or incorrect field access in _compute_currency_id, which could cause runtime issues if not handled carefully.


2. Suggested Fix

In maintenance_request_purchase/models/maintenance_request.py, line 37 in _compute_currency_id, the method accesses record.company_id.currency_id.id without ensuring that record.company_id exists.

Fix:

@api.depends("company_id.currency_id")
def _compute_currency_id(self):
    for record in self:
        record.currency_id = (
            record.company_id.currency_id.id
            if record.company_id
            else self.env.company.currency_id.id
        )

This ensures that company_id is checked before accessing its currency_id.


3. Additional Code Issues

  • Potential runtime error: If company_id is False (not set), accessing record.company_id.currency_id will raise an error unless explicitly guarded.
  • Missing @api.model: The use of self.env.company is correct, but the logic should be robust to avoid accessing fields on non-existent records.

4. Test Improvements

Add test cases in tests/test_maintenance_request.py or similar using SavepointCase to cover:

  • Test with no company set: Ensure currency_id defaults to env.company.currency_id.
  • Test with company set: Ensure currency_id is correctly computed from company_id.currency_id.
  • Test with purchase orders: Ensure _compute_total_purchase_amount works correctly with computed currency_id.

Example test snippet:

def test_compute_currency_id_without_company(self):
    request = self.env['maintenance.request'].create({
        'name': 'Test Request',
        # No company_id set
    })
    self.assertEqual(request.currency_id, self.env.company.currency_id)

def test_compute_currency_id_with_company(self):
    company = self.env['res.company'].create({'name': 'Test Co'})
    request = self.env['maintenance.request'].create({
        'name': 'Test Request',
        'company_id': company.id,
    })
    self.assertEqual(request.currency_id, company.currency_id)

Use SavepointCase for robust database isolation and @tag('post_install') for tests that require module installation.


⏰ This PR has been open for 86 days.
🔍 No human reviews yet after 86 days. PSC members: a quick review would help keep this contributor engaged with OCA.
💤 Last activity was 86 days ago.

Every ignored PR is a contributor who might not come back. Review time matters. (OCA Aging Report)


Reciprocal Review Request

Hi everyone! I found some test failures on this PR and left detailed feedback above. I am happy to discuss or help debug. In the meantime, if any of you get a chance, I would appreciate a look at my open PR(s):

My open PRs across OCA:

Reviewing each other's work helps the whole community move forward. Thank you!


Environment via OCA Neural Reviewer: Minikube + K8s Job + oca-ci/py3.10-odoo16.0 | Odoo 16.0
Automated review by OCA Neural Reviewer + qwen3-coder:30b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants