Skip to content

Commit

Permalink
[IMP] changes on sale_import_base for sale_import_amazon
Browse files Browse the repository at this point in the history
  • Loading branch information
clementmbr committed Mar 5, 2024
1 parent 6d82144 commit 38eca8d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 22 additions & 5 deletions sale_import_base/models/sale_channel_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class SaleChannelImporter(models.TransientModel):

chunk_id = fields.Many2one("queue.job.chunk", "Chunk")

def _get_formatted_data(self):
"""Override if you need to translate the Chunk's raw data into the current
SaleOrder schemas"""
return self.chunk_id._get_data()

def _get_existing_so(self, data):
ref = data["name"]
return self.env["sale.order"].search(
Expand All @@ -22,14 +27,22 @@ def _get_existing_so(self, data):
]
)

def _manage_existing_so(self, existing_so, data):
"""Override if you need to update existing Sale Order instead of raising
an error"""
raise ValidationError(
_("Sale Order {} has already been created").format(data["name"])
)

def run(self):
# Get validated sale order
data = SaleOrder(**self.chunk_id._get_data()).model_dump()
formatted_data = self._get_formatted_data()
data = SaleOrder(**formatted_data).model_dump()
existing_so = self._get_existing_so(data)
if existing_so:
raise ValidationError(
_("Sale Order {} has already been created").format(data["name"])
)
self._manage_existing_so(self, existing_so, data)
return existing_so

so_vals = self._prepare_sale_vals(data)
sale_order = self.env["sale.order"].create(so_vals)
so_line_vals = self._prepare_sale_line_vals(data, sale_order)
Expand All @@ -49,8 +62,12 @@ def _prepare_sale_vals(self, data):
"partner_shipping_id": address_shipping.id,
"client_order_ref": data["name"],
"sale_channel_id": channel.id,
"pricelist_id": data.get("pricelist_id") or channel.pricelist_id.id,
}

pricelist_id = data.get("pricelist_id") or channel.pricelist_id.id
if pricelist_id:
so_vals["pricelist_id"] = pricelist_id

amount = data.get("amount")
if amount:
so_vals.update(
Expand Down
2 changes: 2 additions & 0 deletions sale_import_base/models/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ class SaleOrder(BaseModel, metaclass=ExtendableModelMeta):
payment: Optional[Payment] = None
pricelist_id: Optional[int] = None
date_order: Optional[date] = None
is_fulfilled_by_amazon: Optional[bool] = False
state: Optional[str] = None

0 comments on commit 38eca8d

Please sign in to comment.