Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][FIX] contract: Usage of analytic distribution #1019

Merged
merged 2 commits into from
Jan 5, 2024

Conversation

fkantelberg
Copy link
Member

Because of the changes in 16.0 analytic accounting the analytic_distribution is more important now and only this will be copied onto the invoices.

I copied the invoice line preparation mostly from sale.order.line to generate the distribution also from the analytic_account_id. Therefore no migration is required.

Copy link
Contributor

@CRogos CRogos left a comment

Choose a reason for hiding this comment

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

I agree to follow the same procedures as in sale.order. I think this PR would fix the issue that the analytic_account_id is not copied to the invoice when used in contract.line.

image

Nevertheless there is a general different between sale.order and contract.contract, where I am not sure if this is intended or should be changed.
While the analytic_account_id is on sale.order the analytic_account_id in contract is on contract.line instead of contract.contract.
(I even do not understand why there is an analytic_account_id in sale.order at all and not only an analytic_distribution on sale.order.line and that's it ... maybe usability)

Shouldn't we either remove analytic_account_id at all (migrate the data into analytic_distribution) or move analytic_account_id into "contract.contract" to create the same logic as in sale.order? (analytic_account_id on sale.order is continued on 17.0, so there might be a reason I do not understand)

image

I am willing to prepare a PR, but I would like to have some opinions first.
@pedrobaeza @wpichler @rousseldenis

@fkantelberg
Copy link
Member Author

analytic_account_id on sale.order is used and auto-generated for expense products. [1] I don't think that we need this feature because contracts aren't generated on the fly by the system or change to state too often. analytic_account_id seems to exist on the contract.line because it was the way before 16.0 and it was just left there during the migration and the distribution was added. For the contract lines we could remove it but have to implement a migration at the same time.

[1] https://github.com/OCA/OCB/blob/16.0/addons/sale/models/sale_order.py#L854

@pedrobaeza
Copy link
Member

Analytic account on contract lines was there for transferring such analytic line to the invoice lines, so it's something needed. In v16, it should be switched to the analytic distribution, to be transferred the same way.

@CRogos
Copy link
Contributor

CRogos commented Jan 3, 2024

This script works in an post-migration.py, but I cannot access env["contract.line"] when it is in a pre-migration.py. And the analytic_account_id is not available anymore, when we remove it from contract.line.

Is a better way than creating temporary table in a pre-migration script storing the data and deleting this table in the post-migration step?
Another (ugly) options would be to leave the field in the code and add a comment to remove it with the Odoo 17 mig.

from openupgradelib import openupgrade

def _mig_analytic_account(env):
    """Migrate analytic account from contract line to analytic distribution"""
    contract_lines = (
        env["contract.line"]
        .with_context(active_test=False)
        .search(
            [
                ('analytic_account_id', '!=', False),
            ]
        )
    )
    for line in contract_lines:
        analytic_distribution = line.analytic_distribution
        analytic_account_id = str(line.analytic_account_id.id)
        if analytic_distribution:
            analytic_distribution[analytic_account_id] = (
                analytic_distribution.get(analytic_account_id, 0) + 100
            )
            line.analytic_distribution = analytic_distribution
        else:
            line.analytic_distribution = {analytic_account_id: 100}

@openupgrade.migrate()
def migrate(env, version):
    _mig_analytic_account(env)

@fkantelberg do you want to add the discussed changes on this PR or shell I create an updated PR?

  • add migration script
  • remove analytic_account_id from views

@fkantelberg fkantelberg force-pushed the 16.0-contract-analytic-distribution branch from 89dbf5c to 4691a28 Compare January 4, 2024 08:47
@fkantelberg
Copy link
Member Author

@CRogos I added the migration and dropped the analytic_account_id field. Can you review it?

@fkantelberg fkantelberg force-pushed the 16.0-contract-analytic-distribution branch from 4691a28 to 210b043 Compare January 4, 2024 10:09
Copy link
Contributor

@CRogos CRogos left a comment

Choose a reason for hiding this comment

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

LGTM: Code reviewed, tested migration and analytic distribution is copied to invoices.

@pedrobaeza pedrobaeza added this to the 16.0 milestone Jan 5, 2024
@@ -559,6 +555,7 @@ def _prepare_invoice_line(self):
self.last_date_invoiced, self.recurring_next_date
)
name = self._insert_markers(dates[0], dates[1])

Copy link
Member

Choose a reason for hiding this comment

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

Don't introduce a useless line break inside a method (and more being unrelated).

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed again. Was left because I could remove the initial code here.

@fkantelberg fkantelberg force-pushed the 16.0-contract-analytic-distribution branch from 210b043 to f7c4b5c Compare January 5, 2024 10:03
Copy link
Member

@pedrobaeza pedrobaeza left a comment

Choose a reason for hiding this comment

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

/ocabot merge nobump

@OCA-git-bot
Copy link
Contributor

On my way to merge this fine PR!
Prepared branch 16.0-ocabot-merge-pr-1019-by-pedrobaeza-bump-nobump, awaiting test results.

@OCA-git-bot OCA-git-bot merged commit 234b4b1 into OCA:16.0 Jan 5, 2024
7 checks passed
@OCA-git-bot
Copy link
Contributor

Congratulations, your PR was merged at 80fdfc7. Thanks a lot for contributing to OCA. ❤️

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

Successfully merging this pull request may close these issues.

None yet

4 participants