Skip to content

Commit

Permalink
[ADD] connector_oxigesti: export manufacturing and unbuild orders
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankC013 committed Sep 22, 2023
1 parent 487b8aa commit 45be0b4
Show file tree
Hide file tree
Showing 15 changed files with 396 additions and 2 deletions.
1 change: 1 addition & 0 deletions connector_oxigesti/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"views/product_pricelist_item_view.xml",
"views/stock_production_lot_view.xml",
"views/sale_order_view.xml",
"views/mrp_production_view.xml",
"views/connector_oxigesti_menu.xml",
"security/connector_oxigesti.xml",
"security/ir.model.access.csv",
Expand Down
19 changes: 18 additions & 1 deletion connector_oxigesti/data/ir_cron.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,24 @@
<field name="state">code</field>
<field name="code">model._scheduler_import_stock_production_lot()</field>
</record>

<record
id="ir_cron_oxigesti_export_mrp_production"
model="ir.cron"
forcecreate="True"
>
<field name="name">Oxigesti OS - Export Productions</field>
<field name="model_id" ref="model_oxigesti_backend" />
<field name="active" eval="False" />
<!-- Mandatory to define a explicit user on record from the frontend,
DON'T USE admin (base.user_root) -->
<!--field name="user_id" ref="<not base.user_root>"/-->
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
<field name="doall" eval="False" />
<field name="state">code</field>
<field name="code">model._scheduler_export_mrp_production()</field>
</record>
<record id="ir_cron_oxigesti_import_services" model="ir.cron" forcecreate="True">
<field name="name">Oxigesti OS - Import services</field>
<field name="model_id" ref="model_oxigesti_backend" />
Expand Down
20 changes: 19 additions & 1 deletion connector_oxigesti/data/queue_job_function_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,25 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)-->
<field name="channel_id" ref="connector_oxigesti.channel_oxigesti" />
<field name="retry_pattern" eval="{1: 10, 5: 30, 10: 60, 15: 300}" />
</record>

<!-- Productions -->
<record
id="oxigesti_mrp_production_export_batch_job_function"
model="queue.job.function"
>
<field name="model_id" ref="connector_oxigesti.model_oxigesti_mrp_production" />
<field name="method">export_batch</field>
<field name="channel_id" ref="connector_oxigesti.channel_oxigesti_batch" />
<field name="retry_pattern" eval="{1: 10, 5: 30, 10: 60, 15: 300}" />
</record>
<record
id="oxigesti_mrp_production_export_record_job_function"
model="queue.job.function"
>
<field name="model_id" ref="connector_oxigesti.model_oxigesti_mrp_production" />
<field name="method">export_record</field>
<field name="channel_id" ref="connector_oxigesti.channel_oxigesti" />
<field name="retry_pattern" eval="{1: 10, 5: 30, 10: 60, 15: 300}" />
</record>
<!-- EXPORTS-->
<record
id="oxigesti_sale_order_export_batch_job_function"
Expand Down
1 change: 1 addition & 0 deletions connector_oxigesti/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
from . import sale_order_line
from . import sale_order
from . import account_invoice
from . import mrp_production
7 changes: 7 additions & 0 deletions connector_oxigesti/models/mrp_production/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import exporter
from . import adapter
from . import export_mapper
from . import binder
from . import binding

# from . import listener
32 changes: 32 additions & 0 deletions connector_oxigesti/models/mrp_production/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.component.core import Component


class MrpProductionAdapter(Component):
_name = "oxigesti.mrp.production.adapter"
_inherit = "oxigesti.adapter"

_apply_on = "oxigesti.mrp.production"

_sql = """select a.CodigoOrdenProduccion, a.FechaProduccion, a.EsMontaje,
a.CodigoBotellaVacia, a.LoteBotellaVacia,
a.CodigoCilindro, a.LoteCilindro, a.CodigoValvula,
a.LoteValvula
from %(schema)s.Odoo_Orden_Produccion a
"""

_sql_update = """update s
set %(qset)s
from %(schema)s.Odoo_Orden_Produccion s
where s.CodigoOrdenProduccion = %%(CodigoOrdenProduccion)s
"""

_sql_insert = """insert into %(schema)s.Odoo_Orden_Produccion
(%(fields)s)
output %(retvalues)s
values (%(phvalues)s)
"""

_id = ("CodigoOrdenProduccion",)
21 changes: 21 additions & 0 deletions connector_oxigesti/models/mrp_production/binder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.component.core import Component


class MrpProductionBinder(Component):
_name = "oxigesti.mrp.production.binder"
_inherit = "oxigesti.binder"

_apply_on = "oxigesti.mrp.production"

def _get_external_id(self, binding):
if not self._is_binding(binding):
raise Exception("The source object %s must be a binding" % binding._name)

Check warning on line 15 in connector_oxigesti/models/mrp_production/binder.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binder.py#L15

Added line #L15 was not covered by tests

external_id = None

Check warning on line 17 in connector_oxigesti/models/mrp_production/binder.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binder.py#L17

Added line #L17 was not covered by tests
if binding.odoo_id.product_id.default_code:
external_id = [binding.odoo_id.product_id.default_code]

Check warning on line 19 in connector_oxigesti/models/mrp_production/binder.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binder.py#L19

Added line #L19 was not covered by tests

return external_id

Check warning on line 21 in connector_oxigesti/models/mrp_production/binder.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binder.py#L21

Added line #L21 was not covered by tests
86 changes: 86 additions & 0 deletions connector_oxigesti/models/mrp_production/binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo import _, api, fields, models
from odoo.exceptions import ValidationError


class MrpProduction(models.Model):
_inherit = "mrp.production"

oxigesti_bind_ids = fields.One2many(
comodel_name="oxigesti.mrp.production",
inverse_name="odoo_id",
string="Oxigesti Bindings",
)

def _get_valid_components(self):
fields = ["cylinder", "valve"]
record = self.env["stock.move"]

Check warning on line 18 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L17-L18

Added lines #L17 - L18 were not covered by tests
for mrp_type in fields:
move_raw = self.move_raw_ids.filtered(
lambda x: x.product_id.mrp_type == mrp_type and x.quantity_done > 0
)
if len(move_raw.product_id) == 0:
raise ValidationError(

Check warning on line 24 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L24

Added line #L24 was not covered by tests
_("Production of empty gas bottle type without %s product: %s")
% (mrp_type, self.name)
)
if len(move_raw) > 1 or sum(move_raw.mapped("quantity_done")) > 1:
raise ValidationError(

Check warning on line 29 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L29

Added line #L29 was not covered by tests
_(
"The empty gas bottle (%s) has been created with"
" more than one %s"
)
% (self.name, mrp_type)
)
if len(move_raw.move_line_ids) > 1:
raise ValidationError(

Check warning on line 37 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L37

Added line #L37 was not covered by tests
_(
"You have a component with more than one serial"
" number to generate: %s"
)
% self.name
)
if not move_raw.product_id.default_code:
raise ValidationError(

Check warning on line 45 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L45

Added line #L45 was not covered by tests
_("Internal Reference not set in product: %s")
% move_raw.product_id.name
)
record |= move_raw
return record

Check warning on line 50 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L49-L50

Added lines #L49 - L50 were not covered by tests


class MrpProductionBinding(models.Model):
_name = "oxigesti.mrp.production"
_inherit = "oxigesti.binding"
_inherits = {"mrp.production": "odoo_id"}
_description = "Product Mrp Production"

odoo_id = fields.Many2one(
comodel_name="mrp.production",
string="Productions",
required=True,
ondelete="cascade",
)

@api.model
def export_data(self, backend, since_date):
domain = [

Check warning on line 68 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L68

Added line #L68 was not covered by tests
("company_id", "=", backend.company_id.id),
("product_id.mrp_type", "=", "empty_gas_bottle"),
("state", "=", "done"),
]
if since_date:
domain += [("write_date", ">", since_date)]
self.with_delay().export_batch(backend, domain=domain)

Check warning on line 75 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L74-L75

Added lines #L74 - L75 were not covered by tests

def resync(self):
for record in self:
with record.backend_id.work_on(record._name) as work:
binder = work.component(usage="binder")
relation = binder.unwrap_binding(self)
func = record.export_record

Check warning on line 82 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L79-L82

Added lines #L79 - L82 were not covered by tests
if record.env.context.get("connector_delay"):
func = record.export_record.delay
func(record.backend_id, relation)
return True

Check warning on line 86 in connector_oxigesti/models/mrp_production/binding.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/binding.py#L84-L86

Added lines #L84 - L86 were not covered by tests
56 changes: 56 additions & 0 deletions connector_oxigesti/models/mrp_production/export_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from odoo import _
from odoo.exceptions import ValidationError

from odoo.addons.component.core import Component
from odoo.addons.connector.components.mapper import follow_m2o_relations, mapping, none


class MrpProductionExportMapper(Component):
_name = "oxigesti.mrp.production.export.mapper"
_inherit = "oxigesti.export.mapper"

_apply_on = "oxigesti.mrp.production"

direct = [
("name", "CodigoOrdenProduccion"),
(none("date_planned_start"), "FechaProduccion"),
(none(follow_m2o_relations("lot_producing_id.name")), "LoteBotellaVacia"),
]

@mapping
def CodigoBotellaVacia(self, record):
if not record.product_id.default_code:
raise ValidationError(

Check warning on line 25 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L25

Added line #L25 was not covered by tests
_("Internal Reference not set in product: %s") % record.product_id.name
)
return {"CodigoBotellaVacia": record.product_id.default_code}

Check warning on line 28 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L28

Added line #L28 was not covered by tests

@mapping
def EsMontaje(self, record):
unbuild_count = self.env["mrp.unbuild"].search(

Check warning on line 32 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L32

Added line #L32 was not covered by tests
[("mo_id", "=", record.odoo_id.id), ("state", "=", "done")]
)
if len(unbuild_count) > 1:
raise ValidationError(

Check warning on line 36 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L36

Added line #L36 was not covered by tests
_("The production %s has more than one unbuild. %s")
% (record.name, unbuild_count.mapped("name"))
)
return {"EsMontaje": 0 if len(unbuild_count) > 0 else 1}

Check warning on line 40 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L40

Added line #L40 was not covered by tests

@mapping
def Componentes(self, record):
binder = self.binder_for("oxigesti.mrp.production")
mrp_production = binder.unwrap_binding(record)
move_raws = mrp_production._get_valid_components()
type_mrp = {

Check warning on line 47 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L44-L47

Added lines #L44 - L47 were not covered by tests
"cylinder": ("CodigoCilindro", "LoteCilindro"),
"valve": ("CodigoValvula", "LoteValvula"),
}
res = {}

Check warning on line 51 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L51

Added line #L51 was not covered by tests
for move_raw in move_raws:
code_key, lot_key = type_mrp.get(move_raw.product_id.mrp_type)
res[code_key] = move_raw.product_id.default_code or None
res[lot_key] = move_raw.move_line_ids.lot_id.name or None
return res

Check warning on line 56 in connector_oxigesti/models/mrp_production/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/export_mapper.py#L53-L56

Added lines #L53 - L56 were not covered by tests
53 changes: 53 additions & 0 deletions connector_oxigesti/models/mrp_production/exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.component.core import Component


class MrpProductionDelayedBatchExporter(Component):
"""Export the Oxigesti Productions.
For every production in the list, a delayed job is created.
"""

_name = "oxigesti.mrp.production.delayed.batch.exporter"
_inherit = "oxigesti.delayed.batch.exporter"

_apply_on = "oxigesti.mrp.production"


class MrpProductionDirectBatchExporter(Component):
"""Export the Oxigesti Productions.
For every production in the list, execute inmediately.
"""

_name = "oxigesti.mrp.production.direct.batch.exporter"
_inherit = "oxigesti.direct.batch.exporter"

_apply_on = "oxigesti.mrp.production"


class MrpProductionExporter(Component):
_name = "oxigesti.mrp.production.exporter"
_inherit = "oxigesti.exporter"

_apply_on = "oxigesti.mrp.production"

def _export_dependencies(self):
binder = self.binder_for("oxigesti.mrp.production")
mrp_production = binder.unwrap_binding(self.binding)
items_dict = {

Check warning on line 40 in connector_oxigesti/models/mrp_production/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/exporter.py#L38-L40

Added lines #L38 - L40 were not covered by tests
"oxigesti.stock.production.lot": mrp_production.lot_producing_id
| mrp_production._get_valid_components().move_line_ids.lot_id,
"oxigesti.product.product": mrp_production.product_id
| mrp_production._get_valid_components().product_id,
}
for binder_name, items in items_dict.items():
binder = self.binder_for(binder_name)

Check warning on line 47 in connector_oxigesti/models/mrp_production/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/exporter.py#L47

Added line #L47 was not covered by tests
for item in items:
if not binder.to_external(item, wrap=True):
exporter = self.component(

Check warning on line 50 in connector_oxigesti/models/mrp_production/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/exporter.py#L50

Added line #L50 was not covered by tests
usage="record.exporter", model_name=binder.model._name
)
exporter.run(item)

Check warning on line 53 in connector_oxigesti/models/mrp_production/exporter.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/mrp_production/exporter.py#L53

Added line #L53 was not covered by tests
12 changes: 12 additions & 0 deletions connector_oxigesti/models/oxigesti_backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def button_check_connection(self):
export_stock_production_lot_since_date = fields.Datetime("Export Lots since")
import_services_since_date = fields.Datetime("Import Services since")
export_services_since_date = fields.Datetime("Export Services since")
export_mrp_production_since_date = fields.Datetime("Export Productions since")

sync_offset = fields.Integer(
string="Sync Offset",
Expand Down Expand Up @@ -172,6 +173,12 @@ def export_services_since(self):
rec.export_services_since_date = fields.Datetime.now()
self.env["oxigesti.sale.order"].export_data(rec, since_date)

def export_mrp_production_since(self):
for rec in self:
since_date = rec.export_mrp_production_since_date
rec.export_mrp_production_since_date = fields.Datetime.now()
self.env["oxigesti.mrp.production"].export_data(rec, since_date)

Check warning on line 180 in connector_oxigesti/models/oxigesti_backend/common.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/oxigesti_backend/common.py#L178-L180

Added lines #L178 - L180 were not covered by tests

# Scheduler methods
@api.model
def get_current_user_company(self):
Expand Down Expand Up @@ -231,6 +238,11 @@ def _scheduler_export_services(self):
domain = [("company_id", "=", company_id.id)]
self.search(domain).export_services_since()

def _scheduler_export_mrp_production(self):
company_id = self.get_current_user_company()
domain = [("company_id", "=", company_id.id)]
self.search(domain).export_mrp_production_since()

Check warning on line 244 in connector_oxigesti/models/oxigesti_backend/common.py

View check run for this annotation

Codecov / codecov/patch

connector_oxigesti/models/oxigesti_backend/common.py#L242-L244

Added lines #L242 - L244 were not covered by tests

def tz_to_utc(self, dt):
t = pytz.timezone(self.tz).localize(dt)
t = t.astimezone(pytz.utc)
Expand Down
2 changes: 2 additions & 0 deletions connector_oxigesti/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
"access_oxigesti_stock_production_lot","oxigesti_stock_production_lot connector manager","model_oxigesti_stock_production_lot","connector.group_connector_manager",1,1,1,1
"access_oxigesti_stock_production_lot_group_user","oxigesti_stock_production_lot group_user","model_oxigesti_stock_production_lot","base.group_user",1,0,0,0
"access_oxigesti_backend","oxigesti_backend connector manager","model_oxigesti_backend","connector.group_connector_manager",1,1,1,1
"access_oxigesti_mrp_production","oxigesti_mrp_production connector manager","model_oxigesti_mrp_production","connector.group_connector_manager",1,1,1,1
"access_oxigesti_mrp_production_group_user","oxigesti_mrp_production group_user","model_oxigesti_mrp_production","base.group_user",1,0,0,0
8 changes: 8 additions & 0 deletions connector_oxigesti/views/connector_oxigesti_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@
action="oxigesti_stock_production_lot_action"
/>

<menuitem
id="oxigesti_external_mrp_production_menu"
name="External Productions"
sequence="60"
parent="oxigesti_external_objects_menu"
action="oxigesti_mrp_production_action"
/>

<menuitem
id="oxigesti_external_sales_orders_menu"
name="External Services"
Expand Down
Loading

0 comments on commit 45be0b4

Please sign in to comment.