Skip to content

Commit

Permalink
wms_connector: clean code, improve view
Browse files Browse the repository at this point in the history
Now you can define the number of record per file
binding are not deleted anymore in order to keep the history
  • Loading branch information
sebastienbeau committed Nov 1, 2023
1 parent 2f85e81 commit f196b4c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 27 deletions.
17 changes: 13 additions & 4 deletions wms_connector/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,27 @@ def _deactivate_crons_tasks(self):

def refresh_wms_products(self):
for rec in self:
existing_prd = self.wms_product_sync_ids.product_id
prd_matching = self.env["product.product"].search(
rec.wms_export_product_filter_id
and rec.wms_export_product_filter_id._get_eval_domain()
or []
)
to_create = prd_matching - existing_prd
to_unlink = existing_prd - prd_matching

# Inactive / Active binding
to_inactive = to_active = self.env["wms.product.sync"]
for record in self.with_context(active_test=False).wms_product_sync_ids:
if record.active and record.product_id not in prd_matching:
to_inactive |= record
elif not record.active and record.product_id in prd_matching:
to_active |= record
to_inactive.active = False
to_active.active = True

# Create missing one
to_create = prd_matching - self.wms_product_sync_ids.product_id
self.env["wms.product.sync"].create(
[{"product_id": prd.id, "warehouse_id": rec.id} for prd in to_create]
)
to_unlink.wms_sync_ids.filtered(lambda s: s.warehouse_id == rec).unlink()

def button_open_wms_sync_ids(self):
return {
Expand Down
42 changes: 19 additions & 23 deletions wms_connector/models/synchronize_exportable_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,39 @@ class SynchronizeExportableMixin(models.AbstractModel):
wms_export_error = fields.Char()

@property
def file_creation_mode(self):
return "per_record"
def record_per_file(self):
return 1

def button_trigger_export(self):
self.synchronize_export()

def _get_export_data(self):
data = []
for idx, rec in enumerate(self.sorted("id")):
if self.file_creation_mode == "per_record":
sequence = 0
else:
sequence = idx
records = self.browse()
sequence = 0
for rec in self:
records |= rec
try:
data += rec._prepare_export_data(sequence)
rec.wms_export_error = ""
sequence += 1
except Exception as e:
if "pdb" in config.get("dev_mode"):
raise
rec.wms_export_error = str(e)
continue
if self.file_creation_mode == "per_record":
yield data
if len(records) >= self.record_per_file:
yield records, data
data = []
if self.file_creation_mode == "per_recordset":
yield data
records = self.browse()
sequence = 0
if len(records):
yield records, data

def synchronize_export(self):
data = self._get_export_data()
res = self.env["attachment.queue"]
if self.file_creation_mode == "per_record":
for el, rec in zip(data, self.sorted("id")):
attachment = rec._format_to_exportfile(el)
rec.track_export(attachment)
res += attachment
if self.file_creation_mode == "per_recordset":
res = self._format_to_exportfile(data)
self.track_export(res)
for records, data in self._get_export_data():
attachments = records._format_to_exportfile(data)
records.track_export(attachments)

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

def _format_to_exportfile(self, data):
return self._format_to_exportfile_csv(data)
def _format_to_exportfile(self, name, data):
return self._format_to_exportfile_csv(name, data)

def _format_to_exportfile_csv(self, data):
csv_file = StringIO()
Expand Down
1 change: 1 addition & 0 deletions wms_connector/models/wms_product_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class WmsProductSync(models.Model):
name = fields.Char(related="product_id.name")
product_id = fields.Many2one("product.product", required=True)
warehouse_id = fields.Many2one("stock.warehouse", required=True)
active = fields.Boolean(default=True)

def _schedule_export(self, warehouse, domain=False):
warehouse.refresh_wms_products()
Expand Down
4 changes: 4 additions & 0 deletions wms_connector/views/wms_product_sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
<sheet>
<group>
<field name="name" />
<field name="wms_export_date" />
<field name="product_id" />
<field name="warehouse_id" />
<field name="wms_export_error" />
</group>
</sheet>
</form>
Expand All @@ -34,6 +36,8 @@
<field name="name" />
<field name="product_id" />
<field name="warehouse_id" />
<field name="wms_export_date" />
<field name="wms_export_error" />
</tree>
</field>
</record>
Expand Down

0 comments on commit f196b4c

Please sign in to comment.