Skip to content

Commit

Permalink
Refactored contract so picking origin can be different than contract …
Browse files Browse the repository at this point in the history
…name

Also added a function to get task name in the form "Task-ID"
  • Loading branch information
louck committed May 4, 2023
1 parent 38d3c9f commit 21136d5
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 23 deletions.
57 changes: 48 additions & 9 deletions commown_devices/models/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,28 @@ def _compute_quant_ids(self):
record.quant_nb = len(record.quant_ids)

@api.multi
def send_device(self, quant, date=None, do_transfer=False):
def send_device(self, quant, origin=None, date=None, do_transfer=False):
"""Create a picking of quant to partner's location.
If given `date` is falsy (the default), it is set to now.
If `do_transfer` is True (default: False), execute the picking
at the previous date.
"""
dest_location = self.partner_id.get_or_create_customer_location()
if not origin:
origin = self.name
return self._create_picking(
[quant.lot_id],
quant.location_id,
dest_location,
origin=origin,
date=date,
do_transfer=do_transfer,
)

@api.multi
def send_device_tracking_none(self, product, date=None, do_transfer=False):
def send_device_tracking_none(
self, product, origin=None, date=None, do_transfer=False
):
"""Create a picking of quant to partner's location.
If given `date` is falsy (the default), it is set to now.
If `do_transfer` is True (default: False), execute the picking
Expand All @@ -76,70 +81,104 @@ def send_device_tracking_none(self, product, date=None, do_transfer=False):
("quantity", ">", 0),
]
)
if not origin:
origin = self.name
if not quant:
raise UserError(_("No product %s found in stock") % product.name)

return self._create_picking_tracking_none(
{product: 1},
quant.location_id,
dest_location,
origin=origin,
date=date,
do_transfer=do_transfer,
)

@api.multi
def receive_device(self, lot, dest_location, date=False, do_transfer=False):
def receive_device(
self, lot, dest_location, origin=None, date=False, do_transfer=False
):
"""Create a picking from partner's location to `dest_location`.
If given `date` is falsy (the default), it is set to now.
If `do_transfer` is True (default: False), execute the picking
at the previous date.
"""
if not origin:
origin = self.name

orig_location = self.partner_id.get_or_create_customer_location()
return self._create_picking(
[lot], orig_location, dest_location, date=date, do_transfer=do_transfer
[lot],
orig_location,
dest_location,
origin=origin,
date=date,
do_transfer=do_transfer,
)

@api.multi
def receive_device_tracking_none(
self, product, dest_location, date=False, do_transfer=False
self, product, dest_location, origin=None, date=False, do_transfer=False
):
"""Create a picking from partner's location to `dest_location`.
If given `date` is falsy (the default), it is set to now.
If `do_transfer` is True (default: False), execute the picking
at the previous date.
"""

if not origin:
origin = self.name

orig_location = self.partner_id.get_or_create_customer_location()
return self._create_picking_tracking_none(
{product: 1},
orig_location,
dest_location,
origin=origin,
date=date,
do_transfer=do_transfer,
)

def _create_picking_tracking_none(
self, products, orig_location, dest_location, date=None, do_transfer=False
self,
products,
orig_location,
dest_location,
origin=None,
date=None,
do_transfer=False,
):
self.ensure_one()

if not origin:
origin = self.name
picking = internal_picking_tracking_none(
self.name,
origin,
products,
orig_location,
dest_location,
date=date,
do_transfer=do_transfer,
)
self.picking_ids |= picking

return picking

def _create_picking(
self, lots, orig_location, dest_location, date=None, do_transfer=False
self,
lots,
orig_location,
dest_location,
origin=None,
date=None,
do_transfer=False,
):
self.ensure_one()
if not origin:
origin = self.name
picking = internal_picking(
self.name,
origin,
lots,
orig_location,
dest_location,
Expand Down
8 changes: 2 additions & 6 deletions commown_devices/models/crm_lead.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
from .common import do_new_transfer


def _assigned(picking):
return picking.state == "assigned"


class CrmLead(models.Model):
_inherit = "crm.lead"
delivery_time = datetime.time(9, 0)
Expand Down Expand Up @@ -48,14 +44,14 @@ def write(self, values):

def action_check_waiting_picking(self):
if self.so_line_id.product_id.product_tmpl_id.storable_product_id:
if not self.contract_id.picking_ids.filtered(_assigned):
if not self.contract_id.picking_ids.filtered(lambda p: p._assigned()):
raise UserError(_("Lead has no assigned picking."))

@api.multi
def delivery_perform_actions(self):
"Validate shipping"
super(CrmLead, self).delivery_perform_actions()
picking = self.contract_id.picking_ids.filtered(_assigned)
picking = self.contract_id.picking_ids.filtered(lambda p: p._assigned())
if len(picking) == 1:
# time doesn't really matter for now; ideally
# deliver_date would become delivery_datetime:
Expand Down
3 changes: 3 additions & 0 deletions commown_devices/models/picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class StockPicking(models.Model):

contract_id = fields.Many2one("contract.contract", string="Contract")

def _assigned(self):
return self.state == "assigned"

def action_set_date_done_to_scheduled(self):
self.ensure_one()

Expand Down
5 changes: 4 additions & 1 deletion commown_devices/models/project_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def action_scrap_device(self):
ctx = {
"default_product_id": self.lot_id.product_id.id,
"default_lot_id": self.lot_id.id,
"default_origin": "Task-%s" % self.id,
"default_origin": self.get_id_name(),
"default_scrap_location_id": scrap_loc.id,
}

Expand All @@ -189,3 +189,6 @@ def action_scrap_device(self):
"view_type": "form",
"context": ctx,
}

def get_id_name(self):
return "Task-%s" % self.id
8 changes: 6 additions & 2 deletions commown_devices/models/wizard_project_task_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def create_picking(self):
raise UserError(_("Can't move device: no device set on this task!"))

return internal_picking(
"Task-%s" % self.task_id.id,
self.task_id.get_id_name(),
[lot],
self.present_location_id,
self.location_dest_id,
Expand Down Expand Up @@ -190,7 +190,9 @@ def onchange_lot_id(self):
@api.multi
def create_picking(self):
quant = self.lot_id.quant_ids.filtered(lambda q: q.quantity > 0)
return self.task_id.contract_id.send_device(quant, date=self.date)
return self.task_id.contract_id.send_device(
quant, origin=self.task_id.get_id_name(), date=self.date
)


class ProjectTaskInwardPickingWizard(models.TransientModel):
Expand All @@ -216,6 +218,7 @@ def create_picking(self):
return self.task_id.contract_id.receive_device(
self.lot_id,
self.env.ref("commown_devices.stock_location_devices_to_check"),
origin=self.task_id.get_id_name(),
date=self.date,
)

Expand Down Expand Up @@ -284,6 +287,7 @@ def create_picking(self, do_transfer=False):
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,
)
2 changes: 1 addition & 1 deletion commown_devices/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def send_device(self, serial, contract=None, date=None):
("quantity", ">", 0),
]
)
contract.send_device(quant, date, True)
contract.send_device(quant, date=date, do_transfer=True)

def prepare_ui(
self, created_model_name, related_entity, relation_field, user_choices=None
Expand Down
6 changes: 3 additions & 3 deletions commown_devices/tests/test_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ def test_stock(self):

contract = self.env["contract.contract"].of_sale(self.so)[0]

contract.send_device(quant1, "2021-07-01 17:00:00", do_transfer=True)
contract.send_device(quant2, "2021-07-14", do_transfer=True)
contract.send_device(quant1, date="2021-07-01 17:00:00", do_transfer=True)
contract.send_device(quant2, date="2021-07-14", do_transfer=True)
return_picking = contract.receive_device(
lot1, loc_check, "2021-07-22", do_transfer=False
lot1, loc_check, date="2021-07-22", do_transfer=False
)

self.assertFalse(contract.stock_at_date("2021-07-01 16:59:59"))
Expand Down
2 changes: 1 addition & 1 deletion rental_fees/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@ def receive_device(self, serial, contract, date):
.ensure_one()
)
loc = self.env.ref("commown_devices.stock_location_devices_to_check")
contract.receive_device(lot_id, loc, date, True)
contract.receive_device(lot_id, loc, date=date, do_transfer=True)

0 comments on commit 21136d5

Please sign in to comment.