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 21, 2023
1 parent 487b8aa commit ba942a8
Show file tree
Hide file tree
Showing 16 changed files with 382 additions and 1 deletion.
1 change: 1 addition & 0 deletions connector_common/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"views/product_template_view.xml",
"views/product_category_view.xml",
"views/stock_production_lot_view.xml",
"views/mrp_production_view.xml",
"views/sale_order_view.xml",
],
"installable": True,
Expand Down
21 changes: 21 additions & 0 deletions connector_common/views/mrp_production_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright NuoBiT Solutions - Frank Cespedes <[email protected]>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) -->
<odoo>
<record id="mrp_production_form_view" model="ir.ui.view">
<field name="name">mrp.production.connector.form</field>
<field name="model">mrp.production</field>
<field name="inherit_id" ref="mrp.mrp_production_form_view" />
<field
name="groups_id"
eval="[(4, ref('connector.group_connector_manager'))]"
/>
<field name="arch" type="xml">
<xpath expr="/form/sheet/notebook" position="inside">
<!-- change the invisible attribute to 0 when used
in submodules -->
<page string="Connectors" name="connector" invisible="1" />
</xpath>
</field>
</record>
</odoo>
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
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",)
32 changes: 32 additions & 0 deletions connector_oxigesti/models/mrp_production/binder.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 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)

product_binder = self.binder_for("oxigesti.mrp.production")
product_external_id = product_binder.to_external(
binding.odoo_id.product_id, wrap=True
)

external_id = None
if product_external_id:
product_adapter = self.component(
usage="backend.adapter", model_name="oxigesti.mrp.production"
)
codigo_articulo = product_adapter.id2dict(product_external_id)[
"CodigoOrdenProduccion"
]
external_id = [codigo_articulo, binding.name]

return external_id
49 changes: 49 additions & 0 deletions connector_oxigesti/models/mrp_production/binding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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


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

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


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 = [
("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)

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
if record.env.context.get("connector_delay"):
func = record.export_record.delay
func(record.backend_id, relation)
return True
77 changes: 77 additions & 0 deletions connector_oxigesti/models/mrp_production/export_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# 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
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"),
(none(follow_m2o_relations("product_id.code")), "CodigoBotellaVacia"),
]

@mapping
def EsMontaje(self, record):
unbuild_count = self.env["mrp.unbuild"].search_count(
[("mo_id", "=", record.odoo_id.id), ("state", "=", "done")]
)
return {"EsMontaje": 0 if unbuild_count > 1 else 1}

@mapping
def Codigo(self, record):
cylinder_product = record.move_raw_ids.product_id.filtered(
lambda x: x.mrp_type == "cylinder"
)
valve_product = record.move_raw_ids.product_id.filtered(
lambda x: x.mrp_type == "valve"
)
if len(cylinder_product) == 0 or len(valve_product) == 0:
raise AssertionError(
"Production of empty gas bottle type without"
" cylinder or valve product: %s" % record.name
)
if len(cylinder_product) > 1 or len(valve_product) > 1:
raise AssertionError(
"Production of empty gas bottle type with"
" more than one cylinder or valve product: %s" % record.name
)
return {
"CodigoCilindro": cylinder_product.code,
"CodigoValvula": valve_product.code,
}

@mapping
def Lote(self, record):
cylinder_mr = record.move_raw_ids.filtered(
lambda x: x.product_id.mrp_type == "cylinder" and x.quantity_done > 0
)
valve_mr = record.move_raw_ids.filtered(
lambda x: x.product_id.mrp_type == "valve" and x.quantity_done > 0
)
if (
len(cylinder_mr) > 1
or len(valve_mr) > 1
or sum(cylinder_mr.mapped("quantity_done")) > 1
or sum(valve_mr.mapped("quantity_done")) > 1
):
raise AssertionError(
"The empty gas bottle (%s) has been created with"
" more than one valve or cylinder" % record.name
)
if len(cylinder_mr.move_line_ids) > 1 or len(valve_mr.move_line_ids) > 1:
raise AssertionError(
"You have a component with more than one serial"
" number to generate: %s" % record.name
)
return {
"LoteCilindro": cylinder_mr.move_line_ids.lot_id.name or None,
"LoteValvula": valve_mr.move_line_ids.lot_id.name or None,
}
44 changes: 44 additions & 0 deletions connector_oxigesti/models/mrp_production/exporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.product.product")
relation = self.binding.with_context(active_test=False).product_id
if not binder.to_external(self.binding.product_id, wrap=True):
exporter = self.component(
usage="record.exporter", model_name=binder.model._name
)
exporter.run(relation)
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)

# 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()

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,1,1,1
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 ba942a8

Please sign in to comment.