Skip to content

Commit

Permalink
[16.0][REF] pos_order_to_sale_order: create method optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
GabbasovDinar committed Aug 15, 2023
1 parent 2507767 commit 0d75cb9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
18 changes: 7 additions & 11 deletions pos_order_to_sale_order/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import _, api, models
from odoo import Command, _, api, models


class SaleOrder(models.Model):
Expand All @@ -12,31 +12,27 @@ class SaleOrder(models.Model):
def _prepare_from_pos(self, order_data):
PosSession = self.env["pos.session"]
session = PosSession.browse(order_data["pos_session_id"])
SaleOrderLine = self.env["sale.order.line"]
order_lines = [
Command.create(SaleOrderLine._prepare_from_pos(line[2]))
for line in order_data["lines"]
]
return {
"partner_id": order_data["partner_id"],
"origin": _("Point of Sale %s") % (session.name),
"client_order_ref": order_data["name"],
"user_id": order_data["user_id"],
"pricelist_id": order_data["pricelist_id"],
"fiscal_position_id": order_data["fiscal_position_id"],
"order_line": order_lines,
}

@api.model
def create_order_from_pos(self, order_data, action):
SaleOrderLine = self.env["sale.order.line"]

# Create Draft Sale order
order_vals = self._prepare_from_pos(order_data)
sale_order = self.create(order_vals)

# create Sale order lines
for order_line_data in order_data["lines"]:
# Create Sale order lines
order_line_vals = SaleOrderLine._prepare_from_pos(
sale_order, order_line_data[2]
)
SaleOrderLine.create(order_line_vals)

# Confirm Sale Order
if action in ["confirmed", "delivered", "invoiced"]:
sale_order.action_confirm()
Expand Down
3 changes: 1 addition & 2 deletions pos_order_to_sale_order/models/sale_order_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

@api.model
def _prepare_from_pos(self, sale_order, order_line_data):
def _prepare_from_pos(self, order_line_data):
ProductProduct = self.env["product.product"]
product = ProductProduct.browse(order_line_data["product_id"])
product_name = product.name
if order_line_data.get("customer_note"):
product_name += "\n" + order_line_data["customer_note"]
return {
"order_id": sale_order.id,
"product_id": order_line_data["product_id"],
"name": product_name,
"product_uom_qty": order_line_data["qty"],
Expand Down

0 comments on commit 0d75cb9

Please sign in to comment.