Skip to content

Commit

Permalink
[ADD] purchase_order_line_from_stock: tests
Browse files Browse the repository at this point in the history
fixup! [ADD] purchase_order_line_from_stock
  • Loading branch information
mclaeysb committed Oct 29, 2020
1 parent 49c2422 commit 80823e1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
14 changes: 6 additions & 8 deletions purchase_order_line_from_stock/models/stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
class StockMove(models.Model):
_inherit = "stock.move"

# def _action_done(self):
# self.add_purchase_order_line_from_stock()
# res = super(StockMove, self)._action_done()
# return res

def write(self, vals):
self.add_purchase_order_line_from_stock()
if "purchase_line_id" not in vals: # Prevent recursion
self.add_purchase_order_line_from_stock_move()
res = super(StockMove, self).write(vals)
return res

@api.multi
def add_purchase_order_line_from_stock(self):
def add_purchase_order_line_from_stock_move(self):
stock_move_to_add = self.filtered(
lambda m: m.state == "done" and not m.purchase_line_id
)
Expand All @@ -25,7 +21,9 @@ def add_purchase_order_line_from_stock(self):
raise ValidationError(
_("At least one of the original products must be received")
)
self.env["purchase.order.line"].create(
stock_move.purchase_line_id = self.env[
"purchase.order.line"
].create(
{
"name": stock_move.name,
"product_id": stock_move.product_id.id,
Expand Down
1 change: 1 addition & 0 deletions purchase_order_line_from_stock/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_stock
46 changes: 46 additions & 0 deletions purchase_order_line_from_stock/tests/test_stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from odoo import tests


class TestStock(tests.common.TransactionCase):
def test_order_line_from_stock(self):
order = self.browse_ref("purchase.purchase_order_4")
product = self.browse_ref("product.product_product_1")
location_id = self.env.ref("stock.warehouse0").wh_output_stock_loc_id
dest_loc = self.env.ref("stock.stock_location_customers")

# Confirm order
order.button_confirm()
self.assertEquals(len(order.order_line), 3)
self.assertEquals(len(order.picking_ids.move_ids_without_package), 3)

# Add stock move to picking
new_move = self.env["stock.move"].create(
{
"name": product.name,
"product_id": product.id,
"product_uom": product.uom_id.id,
"quantity_done": 20,
"location_id": location_id.id,
"location_dest_id": dest_loc.id,
}
)
order.picking_ids.move_ids_without_package = [(4, new_move.id, 0)]
self.assertEquals(len(order.picking_ids.move_ids_without_package), 4)

# Set one of the original stock moves to 'done':
# At least one other element must have a quantity_done,
# otherwise an action is returned notifying the user.
order.picking_ids.move_ids_without_package[0].write(
{"quantity_done": 1}
)

# Validate picking
wizard = order.picking_ids.button_validate()
backorder_confirmation = self.env[wizard["res_model"]].browse(
wizard["res_id"]
)
backorder_confirmation.process_cancel_backorder()

# Check that product is added to order
self.assertEquals(len(order.order_line), 4)
self.assertEquals(order.order_line[-1].qty_received, 20)

0 comments on commit 80823e1

Please sign in to comment.