Skip to content

Commit

Permalink
change export logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkhao committed Oct 24, 2023
1 parent 2b5e80a commit 0e04865
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 137 deletions.
1 change: 0 additions & 1 deletion wms_connector/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"depends": ["stock", "sale", "attachment_synchronize"],
"data": [
"security/wms_product_sync.xml",
"data/cron.xml",
"views/wms_product_sync.xml",
"views/stock_picking.xml",
"views/stock_warehouse.xml",
Expand Down
19 changes: 0 additions & 19 deletions wms_connector/data/cron.xml

This file was deleted.

77 changes: 33 additions & 44 deletions wms_connector/models/stock_warehouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,37 @@
"wms_export_picking_in_filter_id": '[("wms_export_date", "=", False),'
' ("picking_type_id", "=", "{}"), ("state", "=", "assigned")]',
"wms_export_picking_out_filter_id": '[("wms_export_date", "=", False),'
' ("picking_type_id", "=", "{}"), ("state", "=", "assigned)]',
' ("picking_type_id", "=", "{}"), ("state", "=", "assigned")]',
}

MAPPINGS = {
"export": {
"export_products": {
"fieldname_task": "wms_export_task_id",
"fieldname_cron": "wms_export_product_cron_id",
"filetype": "export",
"name_fragment": "export products",
"code": "wh = env['stock.warehouse'].browse({})\n"
"wh.refresh_wms_products()\n"
"env['wms.product.sync'].with_context(attachment_task=wh.{})."
"_schedule_export(wh, domain=wh._wms_domain_for('product'))",
},
"export_pickings_in": {
"fieldname_task": "wms_export_task_id",
"fieldname_cron": "wms_export_cron_id",
"fieldname_cron": "wms_export_picking_in_cron_id",
"filetype": "export",
"name_fragment": "exports (products, awaiting receptions, preparation orders",
"code": "wh = env['stock.warehouse'].browse({0})\n"
"wh.{1}.scheduler_export("
'"wms.product.sync", wh._wms_domain_for("wms_product_sync")'
")\n"
"wh.{1}.scheduler_export("
'"stock.picking", wh._wms_domain_for("pickings_in")'
")\n"
"wh.{1}.scheduler_export("
'"stock.picking", wh._wms_domain_for("pickings_out")'
")",
"name_fragment": "export pickings in",
"code": "wh = env['stock.warehouse'].browse({})\n"
"env['stock.picking'].with_context(attachment_task=wh.{})._schedule_export(wh,"
"domain=wh._wms_domain_for('pickings_in')),",
},
"export_pickings_out": {
"fieldname_task": "wms_export_task_id",
"fieldname_cron": "wms_export_picking_out_cron_id",
"filetype": "export",
"name_fragment": "export pickings out",
"code": "wh = env['stock.warehouse'].browse({})\n"
"env['stock.picking'].with_context(attachment_task=wh.{})._schedule_export(wh,"
"domain=wh._wms_domain_for('pickings_out')),",
},
"reception": {
"fieldname_task": "wms_import_confirm_reception_task_id",
Expand Down Expand Up @@ -72,7 +84,9 @@ class StockWarehouse(models.Model):
wms_import_confirm_delivery_task_id = fields.Many2one(
"attachment.synchronize.task", readonly=True
)
wms_export_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_export_product_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_export_picking_in_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_export_picking_out_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_import_confirm_reception_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_import_confirm_delivery_cron_id = fields.Many2one("ir.cron", readonly=True)
wms_product_sync_filter_id = fields.Many2one(
Expand All @@ -91,8 +105,10 @@ class StockWarehouse(models.Model):

def _wms_domain_for(self, model_domain):
domains = {
"wms_product_sync": [("warehouse_id", "=", self.id)]
+ self.wms_product_sync_filter_id._get_eval_domain(),
"product": [
("warehouse_id", "=", self.id),
("wms_export_date", "=", False),
],
"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 All @@ -110,33 +126,6 @@ def _activate_crons_tasks(self):
rec._activate_tasks()
rec._activate_crons()
rec._activate_filters()
# for mappings in MAPPINGS.values():
# task_field_name = mappings["fieldname_task"]
# task = rec[task_field_name]
# if task:
# task.active = True
# else:
# rec[task_field_name] = self.env[
# "attachment.synchronize.task"
# ].create(
# rec._prepare_wms_task_vals(
# mappings["filetype"], mappings["name_fragment"]
# )
# )
# cron_field_name = mappings["fieldname_cron"]
# cron = rec[cron_field_name]
# if cron:
# cron.active = True
# else:
# code = mappings["code"].format(self.id, task_field_name)
# rec[cron_field_name] = self.env["ir.cron"].create(
# rec._prepare_wms_cron_vals(code, mappings["name_fragment"])
# )
# for field in FILTER_FIELDNAMES:
# if not getattr(rec, field):
# rec[field] = self.env.ref(
# "wms_connector.default_{}".format(field[:-3])
# )

def _activate_tasks(self):
for mappings in MAPPINGS.values():
Expand Down
85 changes: 27 additions & 58 deletions wms_connector/models/synchronize_exportable_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
from io import StringIO

from odoo import fields, models

DEBUGMODE = False
from odoo.tools import config


class SynchronizeExportableMixin(models.AbstractModel):
Expand All @@ -25,53 +24,33 @@ def file_creation_mode(self):
def button_trigger_export(self):
self.synchronize_export()

def _get_export_data(self):
data = []
for rec in self.sorted("id"):
try:
data += rec._prepare_export_data()
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
data = []
if self.file_creation_mode == "per_recordset":
yield data

def synchronize_export(self):
data = self._get_export_data()
res = self.env["attachment.queue"]
if self.file_creation_mode == "per_record":
res = self.env["attachment.queue"]
if DEBUGMODE:
for rec in self:
rec.wms_export_error = ""
data = rec._prepare_export_data()
if not data:
continue
attachment = rec._format_to_exportfile(data)
rec.track_export(attachment)
res += attachment
return res
else:
for rec in self:
try:
rec.wms_export_error = ""
data = rec._prepare_export_data()
if not data:
continue
attachment = rec._format_to_exportfile(data)
rec.track_export(attachment)
res += attachment
except Exception as e:
rec.wms_export_error = str(e)
return res

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":
data = []
for rec in self:
try:
rec.wms_export_error = ""
data += rec._prepare_export_data()
if not data:
continue
except Exception as e:
self.wms_export_error = "Error during data preparation:\n{}".format(
str(e)
)
try:
attachment = self._format_to_exportfile(data)
self.track_export(attachment)
return attachment
except Exception as e:
self.wms_export_error = "Error during file formatting:\n{}".format(
str(e)
)
res = self._format_to_exportfile(data)
self.track_export(res)

def track_export(self, attachment):
self.wms_export_date = datetime.datetime.now()
Expand Down Expand Up @@ -104,20 +83,10 @@ def _format_to_exportfile_csv(self, data):
def _get_export_name(self):
raise NotImplementedError

# def scheduler_export(self, warehouse, domain=False):
# if not domain:
# recs = self.search([])
# else:
# recs = self.search(domain)
# if not recs:
# return
# recs.with_context(warehouse=warehouse).synchronize_export()

def _schedule_export(self, warehouse, domain=False):
if not domain:
recs = self.search([])
else:
recs = self.search(domain)
domain = []
recs = self.search(domain)
if not recs:
return
recs.with_context(warehouse=warehouse).synchronize_export()
4 changes: 2 additions & 2 deletions wms_connector/models/wms_product_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class WmsProductSync(models.Model):
product_id = fields.Many2one("product.product", required=True)
warehouse_id = fields.Many2one("stock.warehouse", required=True)

def _schedule_export(self, warehouse):
def _schedule_export(self, warehouse, domain=False):
warehouse.refresh_wms_products()
return super()._schedule_export(warehouse)
return super()._schedule_export(warehouse, domain)
4 changes: 2 additions & 2 deletions wms_connector/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import test_activate_sync
# from . import test_activate_sync

# from . import test_import
# from . import test_export
from . import test_export
23 changes: 13 additions & 10 deletions wms_connector/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ class TestExportFile(WmsConnectorCase):
def setUp(self):
super().setUp()
self.warehouse.active_wms_sync = True
self.cron_export = self.warehouse.wms_export_cron_id
self.cron_export_product = self.warehouse.wms_export_product_cron_id
self.cron_export_picking_in = self.warehouse.wms_export_picking_in_cron_id
self.cron_export_picking_out = self.warehouse.wms_export_picking_out_cron_id
self.demo_product = self.env.ref("product.product_product_1")

def test_export_filter(self):
self.warehouse.refresh_wms_products()
self.setAllExported()
self.env["wms.product.sync"].search(
prd = self.env["wms.product.sync"].search(
[("product_id", "=", self.demo_product.id)]
).wms_export_date = False
self.cron_export.method_direct_trigger()
)
prd.wms_export_date = False
self.cron_export_product.method_direct_trigger()
self.assertTrue(prd.wms_export_date)
self.assertNewAttachmentQueue()

def test_export_error(self):
Expand All @@ -31,17 +35,16 @@ def test_export_error(self):
[("product_id", "=", self.demo_product.id)]
).wms_export_date = False
self.demo_product.name = "".rjust(110, "X")
self.cron_export.method_direct_trigger()
self.cron_export_product.method_direct_trigger()
wms_product = self.env["wms.product.sync"].search(
[("product_id", "=", self.demo_product.id)]
)
self.assertIn("Boom", wms_product.wms_export_error)

def test_export_repeat(self):
self.warehouse.refresh_wms_products()
self.cron_export.method_direct_trigger()
self.cron_export_product.method_direct_trigger()
n_products = len(self.env["wms.product.sync"].search([]).ids)
n_pickings = len(self.env["stock.picking"].search([]).ids)
self.assertNewAttachmentQueue(n_pickings + n_products)
self.cron_export.method_direct_trigger()
self.assertNewAttachmentQueue(n_pickings + n_products)
self.assertNewAttachmentQueue(n_products)
self.cron_export_product.method_direct_trigger()
self.assertNewAttachmentQueue(n_products)
4 changes: 3 additions & 1 deletion wms_connector/views/stock_warehouse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
<field name="wms_import_confirm_reception_task_id" />
</group>
<group>
<field name="wms_export_cron_id" />
<field name="wms_export_product_cron_id" />
<field name="wms_export_picking_in_cron_id" />
<field name="wms_export_picking_out_cron_id" />
<field name="wms_import_confirm_delivery_cron_id" />
<field name="wms_import_confirm_reception_cron_id" />
</group>
Expand Down

0 comments on commit 0e04865

Please sign in to comment.