Allow copy a product plan with parent or plan

#162998
This commit is contained in:
Raimon Esteve 2023-10-30 10:36:16 +01:00
parent 664514567a
commit cae1023351
1 changed files with 7 additions and 50 deletions

57
plan.py
View File

@ -2,7 +2,7 @@
# copyright notices and license terms. # copyright notices and license terms.
from decimal import Decimal from decimal import Decimal
# from sql import Null # from sql import Null
from trytond.model import Check, ModelSQL, ModelView, DeactivableMixin, fields, tree from trytond.model import ModelSQL, ModelView, DeactivableMixin, fields, tree
from trytond.pool import Pool from trytond.pool import Pool
from trytond.pyson import Eval, Bool, If from trytond.pyson import Eval, Bool, If
from trytond.transaction import Transaction from trytond.transaction import Transaction
@ -443,37 +443,9 @@ class Plan(DeactivableMixin, ModelSQL, ModelView):
default = {} default = {}
else: else:
default = default.copy() default = default.copy()
default['products'] = None default.setdefault('bom', None)
default['bom'] = None
new_plans = [] return super().copy(plans, default=default)
for plan in plans:
new_plans.append(plan._copy_plan(default))
return new_plans
def _copy_plan(self, default):
ProductLine = Pool().get('product.cost.plan.product_line')
product_lines = []
def _get_children(line):
product_lines.append(line)
for child in line.children:
_get_children(child)
new_plan, = super(Plan, self).copy([self], default=default)
lines = ProductLine.copy(self.products, default={
'plan': new_plan.id,
})
# sure product.cost_plan.product_line that has parent, set null the plan
for line in lines:
for child in line.children:
_get_children(child)
ProductLine.write(product_lines, {'plan': None})
return new_plan
@classmethod @classmethod
def delete(cls, plans): def delete(cls, plans):
@ -657,28 +629,13 @@ class PlanProductLine(ModelSQL, ModelView, tree(separator='/')):
return total_cost return total_cost
return round_price(total_cost or 0) return round_price(total_cost or 0)
@classmethod
def copy(cls, lines, default=None):
if default is None:
default = {}
else:
default = default.copy()
default['children'] = None
new_lines = []
for line in lines:
new_line, = super(PlanProductLine, cls).copy([line],
default=default)
new_lines.append(new_line)
new_default = default.copy()
new_default['parent'] = new_line.id
cls.copy(line.children, default=new_default)
return new_lines
@classmethod @classmethod
def validate(cls, lines): def validate(cls, lines):
super().validate(lines) super().validate(lines)
cls._validate_plan_parent(lines)
@classmethod
def _validate_plan_parent(cls, lines):
for line in lines: for line in lines:
if ((line.parent and line.plan) or (not line.parent and not line.plan)): if ((line.parent and line.plan) or (not line.parent and not line.plan)):
raise UserError(gettext( raise UserError(gettext(