Skip to content

Commit

Permalink
Added a check on picking when going in "picking ongoing" stage
Browse files Browse the repository at this point in the history
  • Loading branch information
louck committed May 4, 2023
1 parent 21136d5 commit 749b223
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 14 deletions.
12 changes: 8 additions & 4 deletions commown_devices/data/project_task.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
<odoo>
<data noupdate="1">

<record id="diagnostic_stage" model="project.task.type">
<field name="name">Diagnostic stage</field>
<record id="diagnostic_stage" model="project.task.type">
<field name="name">Diagnostic stage</field>
</record>

<record id="resiliated_stage" model="project.task.type">
<field name="name">Resiliated stage</field>
<record id="sup_picking_ongoing_stage" model="project.task.type">
<field name="name">Picking ongoing stage</field>
</record>

<record id="resiliated_stage" model="project.task.type">
<field name="name">Resiliated stage</field>
</record>

</data>
Expand Down
22 changes: 21 additions & 1 deletion commown_devices/models/project_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"commown_devices.resiliated_stage",
]

CHECK_PICKING_ASSIGNED_STAGE_XML_IDS = "commown_devices.sup_picking_ongoing_stage"


class ProjectTask(models.Model):
_inherit = "project.task"
Expand Down Expand Up @@ -144,7 +146,7 @@ def onchange_lot_id(self):
self.contract_id = contracts.id

@api.constrains("stage_id")
def onchange_stage_id_prevent_contract_resiliation_with_device(self):
def _check_stage_id_prevent_contract_resiliation_with_device(self):
if self.stage_id and self.contract_id and self.contract_id.quant_nb > 0:
check_stage_ids = tuple(
self.env.ref(ref).id for ref in CHECK_CONTRACT_QUANT_NB_STAGE_XML_IDS
Expand All @@ -158,6 +160,24 @@ def onchange_stage_id_prevent_contract_resiliation_with_device(self):
% self.contract_id.quant_nb
)

@api.constrains("stage_id")
def _check_assigned_picking(self):
if self.stage_id and self.contract_id:
picking_ongoing_stage = self.env.ref(CHECK_PICKING_ASSIGNED_STAGE_XML_IDS)
if (
self.stage_id.id == picking_ongoing_stage.id
and not self.contract_id.picking_ids.filtered(
lambda p, task=self: p._assigned()
and p.origin == task.get_id_name()
)
):
raise Warning(
_(
"This task can not be moved forward. There are no picking "
"linked to this task."
)
)

def action_scrap_device(self):
scrap_loc = self.env.ref("stock.stock_location_scrapped")

Expand Down
11 changes: 7 additions & 4 deletions commown_devices/models/wizard_project_task_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,12 @@ class ProjectTaskNoTrackingOutwardPickingWizard(models.TransientModel):
)

@api.multi
def create_picking(self, do_transfer=False):
def create_picking(self):
return self.task_id.contract_id.send_device_tracking_none(
self.variant_id, date=self.date, do_transfer=do_transfer
self.variant_id,
origin=self.task_id.get_id_name(),
date=self.date,
do_transfer=False,
)


Expand All @@ -283,11 +286,11 @@ class ProjectTaskNoTrackingInwardPickingWizard(models.TransientModel):
)

@api.multi
def create_picking(self, do_transfer=False):
def create_picking(self):
return self.task_id.contract_id.receive_device_tracking_none(
self.variant_id,
self.env.ref("commown_devices.stock_location_devices_to_check"),
origin=self.task_id.get_id_name(),
date=self.date,
do_transfer=do_transfer,
do_transfer=False,
)
85 changes: 80 additions & 5 deletions commown_devices/tests/test_project_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

from odoo.exceptions import ValidationError

from ..models.common import do_new_transfer
from .common import DeviceAsAServiceTC


class ProjectTaskPickingTC(DeviceAsAServiceTC):
def _create_xml_id(self, record, name):
return self.env["ir.model.data"].create(
{
"module": "commown_devices",
"name": name,
"model": record._name,
"res_id": record.id,
}
)

def setUp(self):
super().setUp()

Expand Down Expand Up @@ -39,6 +50,28 @@ def setUp(self):
"contract_id": self.c1.id,
}
) # for checks on stage change tests
self.task_test_checks2 = self.env["project.task"].create(
{
"name": "test task 2",
"project_id": self.project.id,
"contract_id": self.c2.id,
}
) # for checks on stage change tests

self.ongoing_stage = self.env["project.task.type"].create(
{"name": "Test Ongoing Stage"}
)
self._create_xml_id(self.ongoing_stage, "sup_picking_ongoing_stage")

# Create stage to assign xml_ids so constrains on stage_id pass
t1, t2 = self.env["project.task.type"].create(
[
{"name": "t1"},
{"name": "t2"},
]
)
self._create_xml_id(t1, "diagnostic_stage")
self._create_xml_id(t2, "resiliated_stage")

# Create a unused product and an unused service
self.env["product.template"].create(
Expand Down Expand Up @@ -390,10 +423,12 @@ def test_outward_inward_notracking(self):
)
.location_id
)
wizard = self.env["project.task.notracking.outward.picking.wizard"].create(
{"task_id": self.task.id, "date": date, "variant_id": pt_variant.id}
)
wizard.create_picking(do_transfer=True)
wizard_outward = self.env[
"project.task.notracking.outward.picking.wizard"
].create({"task_id": self.task.id, "date": date, "variant_id": pt_variant.id})

picking = wizard_outward.create_picking()
do_new_transfer(picking, date)

client_location = (
self.env["stock.quant"]
Expand All @@ -410,7 +445,10 @@ def test_outward_inward_notracking(self):
wizard_inward = self.env[
"project.task.notracking.inward.picking.wizard"
].create({"task_id": self.task.id, "date": date, "variant_id": pt_variant.id})
wizard_inward.create_picking(do_transfer=True)

picking = wizard_inward.create_picking()
do_new_transfer(picking, date)

final_location = (
self.env["stock.quant"]
.search(
Expand All @@ -425,3 +463,40 @@ def test_outward_inward_notracking(self):
final_location,
self.env.ref("commown_devices.stock_location_devices_to_check"),
)

def test_change_task_check(self):
with self.assertRaises(ValidationError) as err:
self.task_test_checks.stage_id = self.ongoing_stage
self.assertEqual(
"Error while validating constraint\n\nThis task can not be moved forward. There are no picking linked to this task.\n",
err.exception.name,
)
with self.assertRaises(ValidationError) as err2:
self.task_test_checks2.stage_id = self.ongoing_stage
self.assertEqual(
"Error while validating constraint\n\nThis task can not be moved forward. There are no picking linked to this task.\n",
err2.exception.name,
)

module = self.nontracked_product.product_variant_id
lot = self.storable_product.product_variant_id

quant = self.env["stock.quant"].search(
[
("product_id", "=", lot.id),
("quantity", ">", 0),
("location_id.complete_name", "ilike", "WH/STOCK/"),
]
)[0]

self.task_test_checks.contract_id.send_device_tracking_none(
module,
origin=self.task_test_checks.get_id_name(),
)
self.task_test_checks.stage_id = self.ongoing_stage

self.task_test_checks2.contract_id.send_device(
quant,
origin=self.task_test_checks2.get_id_name(),
)
self.task_test_checks2.stage_id = self.ongoing_stage

0 comments on commit 749b223

Please sign in to comment.