-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[16.0][IMP] sale_import_base: add crm_team_id and methods to inherit sale.channel.importer #87
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
|
@@ -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(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) | ||
|
@@ -50,7 +63,9 @@ def _prepare_sale_vals(self, data): | |
"client_order_ref": data["name"], | ||
"sale_channel_id": channel.id, | ||
"pricelist_id": data.get("pricelist_id") or channel.pricelist_id.id, | ||
"team_id": channel.crm_team_id.id, | ||
} | ||
|
||
amount = data.get("amount") | ||
if amount: | ||
so_vals.update( | ||
|
@@ -163,16 +178,33 @@ def _prepare_sale_line_vals(self, data, sale_order): | |
return [self._prepare_sale_line(line, sale_order) for line in data["lines"]] | ||
|
||
def _prepare_sale_line(self, line_data, sale_order): | ||
channel = self.chunk_id.reference | ||
company_id = channel.company_id | ||
|
||
product = self.env["product.product"].search( | ||
[("default_code", "=", line_data["product_code"])] | ||
[ | ||
("default_code", "=", line_data["product_code"]), | ||
("product_tmpl_id.company_id", "=", company_id.id), | ||
] | ||
) | ||
if not product: | ||
product = self.env["product.product"].search( | ||
[ | ||
("default_code", "=", line_data["product_code"]), | ||
("product_tmpl_id.company_id", "=", False), | ||
] | ||
) | ||
Comment on lines
+190
to
+196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @florian-dacosta I added this separated search to avoid raising error in case of having a product with the same code and both with the company_id and without. |
||
if not product: | ||
raise ValidationError( | ||
_("Missing product {}").format(line_data["product_code"]) | ||
_( | ||
"There is no active product with the Internal Reference %(code)s " | ||
"and related to the company %(company)s." | ||
) | ||
% {"code": line_data["product_code"], "company": company_id.name} | ||
) | ||
elif len(product) > 1: | ||
raise ValidationError( | ||
_("%(product_num)s products found for the code %(code)s") | ||
_("%(product_num)s products found for the code %(code)s.") | ||
% {"product_num": len(product), "code": line_data["product_code"]} | ||
) | ||
vals = { | ||
|
@@ -184,6 +216,7 @@ def _prepare_sale_line(self, line_data, sale_order): | |
} | ||
if line_data.get("description"): | ||
vals["name"] = line_data["description"] | ||
|
||
return vals | ||
|
||
def _finalize(self, new_sale_order, raw_import_data): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is not ok, because we could have products shared between companies, in that case, the company_id is not set and we should find the product.
I believe we should at least check if company is the same as the channel OR is empty