Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sale_import_base/models/sale_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SaleChannel(models.Model):
"(technical) Check total amounts against imported values"
)
pricelist_id = fields.Many2one("product.pricelist", string="Pricelist")
crm_team_id = fields.Many2one("crm.team")
confirm_order = fields.Boolean(help="Confirm order after import")
invoice_order = fields.Boolean(help="Generate invoice after import")
internal_naming_method = fields.Selection(
Expand Down
47 changes: 40 additions & 7 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(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 @@ -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(
Expand Down Expand Up @@ -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),
Copy link
Member

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

]
)
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
Copy link
Member Author

Choose a reason for hiding this comment

The 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.
Do you think it's OK like this?

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 = {
Expand All @@ -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):
Expand Down
41 changes: 22 additions & 19 deletions sale_import_base/views/sale_channel_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@
<field name="inherit_id" ref="sale_channel.sale_channel_view_form" />
<field name="arch" type="xml">
<xpath expr="//group[@name='general_info']" position="after">
<group name="sale_import_base" string="Sale Import Parameters">
<field name="internal_naming_method" />
<field name="pricelist_id" />
<field name="allow_match_on_email" />
<field name="confirm_order" />
<field
name="invoice_order"
attrs="{'invisible': [('confirm_order', '=', False)]}"
/>
</group>
<group name="check_amounts" string="Check imported values">
<field
name="sale_orders_check_amounts_untaxed"
string="Untaxed amounts"
/>
<field
name="sale_orders_check_amounts_total"
string="Total amounts"
/>
<group>
<group name="sale_import_base" string="Sale Import Parameters">
<field name="internal_naming_method" />
<field name="pricelist_id" />
<field name="crm_team_id" />
<field name="allow_match_on_email" />
<field name="confirm_order" />
<field
name="invoice_order"
attrs="{'invisible': [('confirm_order', '=', False)]}"
/>
</group>
<group name="check_amounts" string="Check imported values">
<field
name="sale_orders_check_amounts_untaxed"
string="Untaxed amounts"
/>
<field
name="sale_orders_check_amounts_total"
string="Total amounts"
/>
</group>
</group>
</xpath>
</field>
Expand Down
Loading