mirror of
https://github.com/OCA/contract.git
synced 2025-02-13 17:57:24 +02:00
refactor module
This commit is contained in:
@@ -1,17 +1,46 @@
|
||||
# Copyright 2023 Damien Crier - Foodles
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
from odoo import fields, models
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class Contract(models.Model):
|
||||
_inherit = "contract.contract"
|
||||
|
||||
original_contract_id = fields.Many2one(
|
||||
original_contract_ids = fields.Many2many(
|
||||
comodel_name="contract.contract",
|
||||
relation="contract_split_contract_rel",
|
||||
column1="contract_id",
|
||||
column2="split_contract_id",
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
@api.model
|
||||
def _get_contract_split_name(self, split_wizard):
|
||||
return split_wizard.main_contract_id.name
|
||||
|
||||
@api.model
|
||||
def _get_values_create_split_contract(self, split_wizard):
|
||||
return {
|
||||
"name": self._get_contract_split_name(split_wizard),
|
||||
"partner_id": split_wizard.partner_id.id,
|
||||
"invoice_partner_id": split_wizard.invoice_partner_id.id,
|
||||
"original_contract_ids": [split_wizard.main_contract_id.id],
|
||||
"line_recurrence": True,
|
||||
}
|
||||
|
||||
def _get_default_split_values(self) -> dict:
|
||||
self.ensure_one()
|
||||
return {
|
||||
"main_contract_id": self.id,
|
||||
"partner_id": self.partner_id.id,
|
||||
"invoice_partner_id": self.invoice_partner_id.id,
|
||||
"split_line_ids": [
|
||||
(0, 0, line._get_default_split_line_values())
|
||||
for line in self.contract_line_ids
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
class ContractLine(models.Model):
|
||||
_inherit = "contract.line"
|
||||
@@ -24,3 +53,26 @@ class ContractLine(models.Model):
|
||||
comodel_name="contract.contract",
|
||||
readonly=True,
|
||||
)
|
||||
|
||||
def _get_write_values_when_moving_line(self, new_contract):
|
||||
self.ensure_one()
|
||||
return {
|
||||
"contract_id": new_contract.id,
|
||||
"splitted_from_contract_id": self.contract_id.id,
|
||||
}
|
||||
|
||||
def _get_write_values_when_splitting_and_moving_line(self, new_contract, qty):
|
||||
self.ensure_one()
|
||||
return {
|
||||
"contract_id": new_contract.id,
|
||||
"splitted_from_contract_id": self.contract_id.id,
|
||||
"splitted_from_line_id": self.id,
|
||||
"quantity": qty,
|
||||
}
|
||||
|
||||
def _get_default_split_line_values(self) -> dict:
|
||||
self.ensure_one()
|
||||
return {
|
||||
"original_contract_line_id": self.id,
|
||||
"quantity_to_split": self.quantity,
|
||||
}
|
||||
|
||||
@@ -25,6 +25,32 @@ class TestContractSplit(TestContractBase):
|
||||
self.assertEqual(self.contract3.id, wizard.main_contract_id.id)
|
||||
self.assertEqual(3, len(wizard.split_line_ids.ids))
|
||||
|
||||
def test_contract_default_get_method_1(self):
|
||||
wizard = (
|
||||
self.env["split.contract"]
|
||||
.with_context(active_id=self.contract3.id)
|
||||
.create({})
|
||||
)
|
||||
contract_split_name = self.contract3._get_contract_split_name(wizard)
|
||||
self.assertEqual(contract_split_name, self.contract3.name)
|
||||
expected_result = {
|
||||
"main_contract_id": self.contract3.id,
|
||||
"partner_id": self.contract3.partner_id.id,
|
||||
"invoice_partner_id": self.contract3.invoice_partner_id.id,
|
||||
"split_line_ids": [
|
||||
(
|
||||
0,
|
||||
0,
|
||||
{
|
||||
"original_contract_line_id": line.id,
|
||||
"quantity_to_split": line.quantity,
|
||||
},
|
||||
)
|
||||
for line in self.contract_line_ids
|
||||
],
|
||||
}
|
||||
self.assertEqual(self.contract3._get_default_split_values(), expected_result)
|
||||
|
||||
def test_no_split_because_no_qty_set(self):
|
||||
wizard = (
|
||||
self.env["split.contract"]
|
||||
|
||||
@@ -8,6 +8,7 @@ from odoo.tools import float_compare
|
||||
|
||||
class SplitContract(models.TransientModel):
|
||||
_name = "split.contract"
|
||||
_description = "Contract split transient model"
|
||||
|
||||
split_line_ids = fields.One2many(
|
||||
comodel_name="split.contract.line", inverse_name="split_contract_id"
|
||||
@@ -21,39 +22,9 @@ class SplitContract(models.TransientModel):
|
||||
vals = super().default_get(fields)
|
||||
contract_id = self.env.context.get("active_id")
|
||||
contract = self.env["contract.contract"].browse(contract_id)
|
||||
vals.update(self._get_default_values_from_contract(contract))
|
||||
vals.update(contract._get_default_split_values())
|
||||
return vals
|
||||
|
||||
def _get_default_values_from_contract(self, contract) -> dict:
|
||||
return {
|
||||
"main_contract_id": contract.id,
|
||||
"partner_id": contract.partner_id.id,
|
||||
"invoice_partner_id": contract.invoice_partner_id.id,
|
||||
"split_line_ids": [
|
||||
(0, 0, self._get_default_split_line_values(line))
|
||||
for line in contract.contract_line_ids
|
||||
],
|
||||
}
|
||||
|
||||
def _get_default_split_line_values(self, line) -> list:
|
||||
return {
|
||||
"original_contract_line_id": line.id,
|
||||
"quantity_to_split": line.quantity,
|
||||
}
|
||||
|
||||
def _get_contract_name(self):
|
||||
return self.main_contract_id.name
|
||||
|
||||
def _get_values_create_contract(self):
|
||||
self.ensure_one()
|
||||
return {
|
||||
"name": self._get_contract_name(),
|
||||
"partner_id": self.partner_id.id,
|
||||
"invoice_partner_id": self.invoice_partner_id.id,
|
||||
"original_contract_id": self.main_contract_id.id,
|
||||
"line_recurrence": True,
|
||||
}
|
||||
|
||||
def action_split_contract(self):
|
||||
"""
|
||||
If lines exists in the wizard, create a new contract <CONTRACT>
|
||||
@@ -68,8 +39,9 @@ class SplitContract(models.TransientModel):
|
||||
if self.split_line_ids and any(
|
||||
line.quantity_to_split for line in self.split_line_ids
|
||||
):
|
||||
new_contract = self.env["contract.contract"].create(
|
||||
self._get_values_create_contract()
|
||||
contract_obj = self.env["contract.contract"]
|
||||
new_contract = contract_obj.create(
|
||||
contract_obj._get_values_create_split_contract(self)
|
||||
)
|
||||
# TODO: play onchange on partner_id. use onchange_helper from OCA ?
|
||||
for line in self.split_line_ids:
|
||||
@@ -84,7 +56,7 @@ class SplitContract(models.TransientModel):
|
||||
):
|
||||
# only move because new_qty = original_qty
|
||||
original_line.write(
|
||||
line._get_write_values_when_moving_line(new_contract)
|
||||
original_line._get_write_values_when_moving_line(new_contract)
|
||||
)
|
||||
elif (
|
||||
float_compare(
|
||||
@@ -95,11 +67,10 @@ class SplitContract(models.TransientModel):
|
||||
# need to split and move
|
||||
new_line = original_line.copy()
|
||||
new_line.write(
|
||||
line._get_write_values_when_splitting_and_moving_line(
|
||||
new_contract, line
|
||||
original_line._get_write_values_when_splitting_and_moving_line(
|
||||
new_contract, line.quantity_to_split
|
||||
)
|
||||
)
|
||||
|
||||
original_line.quantity -= new_line.quantity
|
||||
return new_contract
|
||||
return True
|
||||
@@ -107,6 +78,7 @@ class SplitContract(models.TransientModel):
|
||||
|
||||
class SplitContractLine(models.TransientModel):
|
||||
_name = "split.contract.line"
|
||||
_description = "Contract split line transient model"
|
||||
|
||||
split_contract_id = fields.Many2one(comodel_name="split.contract")
|
||||
original_contract_line_id = fields.Many2one(comodel_name="contract.line")
|
||||
@@ -115,12 +87,6 @@ class SplitContractLine(models.TransientModel):
|
||||
readonly=True,
|
||||
store=False,
|
||||
)
|
||||
original_contract_id = fields.Many2one(
|
||||
comodel_name="contract.contract",
|
||||
related="original_contract_line_id.contract_id",
|
||||
readonly=True,
|
||||
store=False,
|
||||
)
|
||||
product_id = fields.Many2one(
|
||||
comodel_name="product.product",
|
||||
related="original_contract_line_id.product_id",
|
||||
@@ -156,17 +122,3 @@ class SplitContractLine(models.TransientModel):
|
||||
"original quantity of the initial contract line."
|
||||
)
|
||||
)
|
||||
|
||||
def _get_write_values_when_moving_line(self, new_contract):
|
||||
return {
|
||||
"contract_id": new_contract.id,
|
||||
"splitted_from_contract_id": self.original_contract_id.id,
|
||||
}
|
||||
|
||||
def _get_write_values_when_splitting_and_moving_line(self, new_contract, line):
|
||||
return {
|
||||
"contract_id": new_contract.id,
|
||||
"splitted_from_contract_id": self.original_contract_id.id,
|
||||
"splitted_from_line_id": self.original_contract_line_id.id,
|
||||
"quantity": line.quantity_to_split,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user