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

[14.0][REF] cooperator: Save sequences on company #74

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cooperator/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"name": "Cooperators",
"summary": "Manage your cooperators",
"version": "14.0.1.6.0",
"version": "14.0.1.7.0",
"depends": [
"base",
"web",
Expand Down
16 changes: 3 additions & 13 deletions cooperator/data/data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,8 @@
<field name="type">sale</field>
</record>

<record id="sequence_subscription" model="ir.sequence">
<field name="name">Subscription Register</field>
<field name="code">subscription.register</field>
<field eval="1" name="number_next" />
<field eval="1" name="number_increment" />
</record>

<record id="sequence_register_operation" model="ir.sequence">
<field name="name">Register Operation</field>
<field name="code">register.operation</field>
<field eval="1" name="number_next" />
<field eval="1" name="number_increment" />
</record>
<function model="res.company" name="_create_cooperator_sequences">
<value model="res.company" search="[]" />
</function>
</data>
</odoo>
44 changes: 44 additions & 0 deletions cooperator/migrations/14.0.1.7.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: 2023 Coop IT Easy SC
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import logging

from odoo import SUPERUSER_ID, api

_logger = logging.getLogger(__name__)


def remove_xmlid(env, module, name):
xmlid_record = env["ir.model.data"].search(
[("module", "=", module), ("name", "=", name)]
)
if xmlid_record:
xmlid_record.unlink()


def migrate(cr, version):
env = api.Environment(cr, SUPERUSER_ID, {})
company = env.ref("base.main_company")
# Use the existing and already-in-use sequences for company 1.
if company:
cooperator_number = env.ref("cooperator.sequence_subscription")
cooperator_number.write(
{
"company_id": company.id,
"code": "cooperator.number",
}
)
register_operation = env.ref("cooperator.sequence_register_operation")
register_operation.write(
{
"company_id": company.id,
"code": "register.operation",
}
)
carmenbianca marked this conversation as resolved.
Show resolved Hide resolved
# Delete the xmlids. The actual records are preserved.
remove_xmlid(env, "cooperator", "sequence_subscription")
remove_xmlid(env, "cooperator", "sequence_register_operation")
# Create sequences for other companies.
other_companies = env["res.company"].search([]) - company
other_companies._create_cooperator_sequences()
18 changes: 9 additions & 9 deletions cooperator/models/account_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from datetime import datetime

from odoo import fields, models
from odoo import api, fields, models


class AccountMove(models.Model):
Expand Down Expand Up @@ -66,11 +66,13 @@ def get_mail_template_certificate(self):
mail_template = "cooperator.email_template_certificat"
return self.env.ref(mail_template)

def get_sequence_register(self):
return self.env.ref("cooperator.sequence_subscription", False)
@api.model
def get_next_cooperator_number(self):
return self.env["ir.sequence"].next_by_code("cooperator.number")

def get_sequence_operation(self):
return self.env.ref("cooperator.sequence_register_operation", False)
@api.model
def get_next_register_operation(self):
return self.env["ir.sequence"].next_by_code("register.operation")

def get_share_line_vals(self, line, effective_date):
return {
Expand All @@ -96,8 +98,7 @@ def get_membership_vals(self):
# if not yet cooperator we generate a cooperator number
vals = {}
if self.partner_id.member is False and self.partner_id.old_member is False:
sequence_id = self.get_sequence_register()
sub_reg_num = sequence_id.next_by_id()
sub_reg_num = self.get_next_cooperator_number()
vals = {
"member": True,
"old_member": False,
Expand Down Expand Up @@ -127,8 +128,7 @@ def set_cooperator_effective(self, effective_date):

self.set_membership()

sequence_operation = self.get_sequence_operation()
sub_reg_operation = sequence_operation.next_by_id()
sub_reg_operation = self.get_next_register_operation()

for line in self.invoice_line_ids:
sub_reg_vals = self.get_subscription_register_vals(line, effective_date)
Expand Down
29 changes: 28 additions & 1 deletion cooperator/models/company.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).


from odoo import api, fields, models
from odoo import _, api, fields, models


class ResCompany(models.Model):
Expand Down Expand Up @@ -138,3 +138,30 @@ def onchange_financial_risk_approval_required(self):
def onchange_generic_rules_approval_required(self):
if self.generic_rules_approval_required:
self.display_generic_rules_approval = True

@api.model
def create(self, vals):
company = super().create(vals)
company._create_cooperator_sequences()
return company

@api.model
def _get_cooperator_sequence_map(self):
return {
"cooperator.number": _("Cooperator number sequence"),
"register.operation": _("Cooperator register operation sequence"),
}

def _create_cooperator_sequences(self):
for company in self:
for code, name in self._get_cooperator_sequence_map().items():
if not self.env["ir.sequence"].search(
[("code", "=", code), ("company_id", "=", company.id)]
):
self.env["ir.sequence"].create(
{
"name": name,
"code": code,
"company_id": company.id,
}
)
14 changes: 5 additions & 9 deletions cooperator/models/operation_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,10 @@ def execute_operation(self):
_("Converting just part of the" " shares is not yet implemented")
)
elif self.operation_type == "transfer":
sequence_id = self.env.ref("cooperator.sequence_subscription", False)
partner_vals = {"member": True}
if self.receiver_not_member:
partner = self.subscription_request.create_coop_partner()
# get cooperator number
sub_reg_num = int(sequence_id.next_by_id())
sub_reg_num = self.env["ir.sequence"].next_by_code("cooperator.number")
partner_vals.update(
sub_request.get_eater_vals(partner, self.share_product_id)
)
Expand All @@ -367,7 +365,9 @@ def execute_operation(self):
# means an old member or cooperator candidate
if not self.partner_id_to.member:
if self.partner_id_to.cooperator_register_number == 0:
sub_reg_num = int(sequence_id.next_by_id())
sub_reg_num = self.env["ir.sequence"].next_by_code(
"cooperator.number"
)
partner_vals["cooperator_register_number"] = sub_reg_num
partner_vals.update(
sub_request.get_eater_vals(
Expand All @@ -392,11 +392,7 @@ def execute_operation(self):
else:
raise ValidationError(_("This operation is not yet" " implemented."))

sequence_operation = self.env.ref(
"cooperator.sequence_register_operation", False
) # noqa
sub_reg_operation = sequence_operation.next_by_id()

sub_reg_operation = self.env["ir.sequence"].next_by_code("register.operation")
values["name"] = sub_reg_operation
values["register_number_operation"] = int(sub_reg_operation)

Expand Down
2 changes: 2 additions & 0 deletions cooperator/readme/newsfragments/74.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ir.sequences used by this module are no longer global; each company has its own
sequence for cooperator memberships.