Skip to content

Commit

Permalink
sale_order_import: add confirm_order flag
Browse files Browse the repository at this point in the history
Allows to confirm order immediately.
  • Loading branch information
simahawk committed Dec 4, 2024
1 parent 6da2e0e commit e6cc0d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
12 changes: 12 additions & 0 deletions sale_order_import/tests/test_order_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ def test_order_import(self):
self.assertEqual(len(order.order_line), 2)
self.assertEqual(int(order.order_line[0].product_uom_qty), 3)

def test_order_import_action(self):
wiz = self.wiz_model.create({})
action = wiz.create_order_return_action(self.parsed_order, "order.ref")
order = self.env["sale.order"].browse(action["res_id"])
self.assertEqual(order.state, "draft")

def test_order_import_confirm(self):
wiz = self.wiz_model.create({"confirm_order": True})
action = wiz.create_order_return_action(self.parsed_order, "order.ref")
order = self.env["sale.order"].browse(action["res_id"])
self.assertEqual(order.state, "sale")

def test_order_import_default_so_vals(self):
default = {"client_order_ref": "OVERRIDE"}
order = self.wiz_model.with_context(
Expand Down
28 changes: 24 additions & 4 deletions sale_order_import/wizard/sale_order_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SaleOrderImport(models.TransientModel):
"res.partner", string="Shipping Address", readonly=True
)
sale_id = fields.Many2one("sale.order", string="Quotation to Update")
confirm_order = fields.Boolean(default=False)

@api.onchange("order_file")
def order_file_change(self):
Expand Down Expand Up @@ -312,23 +313,33 @@ def _validate_existing_orders(self, partner, parsed_order):
)
)

# TODO: I wonder why these methods are model methods
@api.model
def create_order(self, parsed_order, price_source, order_filename=None):
def create_order(
self, parsed_order, price_source, order_filename=None, confirm_order=False
):
soo = self.env["sale.order"].with_context(mail_create_nosubscribe=True)
bdio = self.env["business.document.import"]
so_vals = self._prepare_order(parsed_order, price_source)
order = soo.create(so_vals)
if confirm_order:
order.action_confirm()
bdio.post_create_or_update(parsed_order, order, doc_filename=order_filename)
logger.info("Sale Order ID %d created", order.id)
return order

@api.model
def create_order_ws(self, parsed_order, price_source, order_filename=None):
def create_order_ws(
self, parsed_order, price_source, order_filename=None, confirm_order=False
):
"""Same method as create_order() but callable via JSON-RPC
webservice. Returns an ID to avoid this error:
TypeError: sale.order(15,) is not JSON serializable"""
order = self.create_order(
parsed_order, price_source, order_filename=order_filename
parsed_order,
price_source,
order_filename=order_filename,
confirm_order=confirm_order,
)
return order.id

Expand Down Expand Up @@ -409,7 +420,12 @@ def create_order_button(self):
# TODO: add tests
def create_order_return_action(self, parsed_order, order_filename):
self.ensure_one()
order = self.create_order(parsed_order, self.price_source, order_filename)
order = self.create_order(
parsed_order,
self.price_source,
order_filename,
confirm_order=self._order_should_be_confirmed(),
)
order.message_post(
body=_("Created automatically via file import (%s).") % self.order_filename
)
Expand All @@ -424,6 +440,10 @@ def create_order_return_action(self, parsed_order, order_filename):
)
return action

def _order_should_be_confirmed(self):
# Hook to override behavior
return self.confirm_order

# TODO: add tests
@api.model
def _prepare_update_order_vals(self, parsed_order, order, partner):
Expand Down
1 change: 1 addition & 0 deletions sale_order_import/wizard/sale_order_import_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
name="price_source"
attrs="{'invisible': [('doc_type', '!=', 'order')], 'required': [('doc_type', '=', 'order')]}"
/>
<field name="confirm_order" />
</group>
<footer>
<button
Expand Down

0 comments on commit e6cc0d3

Please sign in to comment.