Skip to content

Commit

Permalink
wms_connector: fix export, review product sync
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienbeau committed Nov 1, 2023
1 parent f196b4c commit 233b3f5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions wms_connector/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def _wms_domain_for(self, model_domain):
domains = {
"product": [
("warehouse_id", "=", self.id),
("wms_export_date", "=", False),
("to_export", "=", True),
],
"pickings_in": self.wms_export_picking_in_filter_id._get_eval_domain(),
"pickings_out": self.wms_export_picking_out_filter_id._get_eval_domain(),
Expand Down Expand Up @@ -233,7 +233,7 @@ def button_open_wms_sync_ids(self):
"res_model": "wms.product.sync",
"type": "ir.actions.act_window",
"target": "current",
"domain": self._wms_domain_for("product"),
"domain": [("warehouse_id", "=", self.id)],
}

def button_open_wms_pickings_in(self):
Expand Down
20 changes: 12 additions & 8 deletions wms_connector/models/synchronize_exportable_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
class SynchronizeExportableMixin(models.AbstractModel):
_name = "synchronize.exportable.mixin"
_description = "Synchronizable export mixin"
wms_export_date = fields.Date()
wms_export_attachment = fields.Many2one("attachment.queue", index=True)
wms_export_error = fields.Char()

wms_export_date = fields.Datetime(readonly=True)
wms_export_attachment = fields.Many2one(
"attachment.queue", index=True, readonly=True
)
wms_export_error = fields.Char(readonly=True)

@property
def record_per_file(self):
Expand All @@ -29,9 +32,9 @@ def _get_export_data(self):
records = self.browse()
sequence = 0
for rec in self:
records |= rec
try:
data += rec._prepare_export_data(sequence)
records |= rec
rec.wms_export_error = ""
sequence += 1
except Exception as e:
Expand All @@ -49,8 +52,9 @@ def _get_export_data(self):

def synchronize_export(self):
for records, data in self._get_export_data():
attachments = records._format_to_exportfile(data)
records.track_export(attachments)
vals = self._format_to_exportfile(data)
attachment = self.env["attachment.queue"].create(vals)
records.track_export(attachment)

def track_export(self, attachment):
self.wms_export_date = datetime.datetime.now()
Expand All @@ -59,6 +63,7 @@ def track_export(self, attachment):
def _prepare_export_data(self, idx) -> list:
raise NotImplementedError

# TODO cleanup this code
def _format_to_exportfile(self, name, data):
return self._format_to_exportfile_csv(name, data)

Expand All @@ -72,13 +77,12 @@ def _format_to_exportfile_csv(self, data):
writer.writerow(row)
csv_file.seek(0)
ast = self.env.context.get("attachment_task")
vals = {
return {
"name": self._get_export_name(),
"datas": base64.b64encode(csv_file.getvalue().encode("utf-8")),
"task_id": ast.id,
"file_type": ast.file_type,
}
return self.env["attachment.queue"].create(vals)

def _get_export_name(self):
raise NotImplementedError
Expand Down
21 changes: 17 additions & 4 deletions wms_connector/models/wms_product_sync.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
# Copyright 2023 Akretion
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import fields, models
from odoo import api, fields, models


class WmsProductSync(models.Model):
_inherit = ["synchronize.exportable.mixin"]
_name = "wms.product.sync"
_description = "Wms Product Sync"

name = fields.Char(related="product_id.name")
product_id = fields.Many2one("product.product", required=True)
warehouse_id = fields.Many2one("stock.warehouse", required=True)
name = fields.Char(
related="product_id.name",
)
product_id = fields.Many2one("product.product", required=True, readonly=True)
warehouse_id = fields.Many2one("stock.warehouse", required=True, readonly=True)
active = fields.Boolean(default=True)

to_export = fields.Boolean(compute="_compute_to_export", store=True, readonly=False)

@api.depends("product_id.name", "active")
def _compute_to_export(self):
for record in self:
record.to_export = True

def _schedule_export(self, warehouse, domain=False):
warehouse.refresh_wms_products()
return super()._schedule_export(warehouse, domain)

def track_export(self, attachment):
super().track_export(attachment)
self.to_export = False
2 changes: 1 addition & 1 deletion wms_connector/security/wms_product_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<field name="perm_read" eval="1" />
<field name="perm_create" eval="1" />
<field name="perm_write" eval="1" />
<field name="perm_unlink" eval="1" />
<field name="perm_unlink" eval="0" />
</record>

</odoo>
1 change: 1 addition & 0 deletions wms_connector/views/wms_product_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<field name="warehouse_id" />
<field name="wms_export_date" />
<field name="wms_export_error" />
<field name="to_export" />
</tree>
</field>
</record>
Expand Down

0 comments on commit 233b3f5

Please sign in to comment.