mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
Initial WIP commit of connector_opencart for Odoo 12.0
This commit is contained in:
2
connector_opencart/__init__.py
Normal file
2
connector_opencart/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from . import components
|
||||||
|
from . import models
|
||||||
25
connector_opencart/__manifest__.py
Normal file
25
connector_opencart/__manifest__.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
{
|
||||||
|
'name': 'Opencart Connector',
|
||||||
|
'version': '12.0.1.0.0',
|
||||||
|
'category': 'Connector',
|
||||||
|
'depends': [
|
||||||
|
'account',
|
||||||
|
'product',
|
||||||
|
'delivery',
|
||||||
|
'sale_stock',
|
||||||
|
'connector_ecommerce',
|
||||||
|
],
|
||||||
|
'author': 'Hibou Corp.',
|
||||||
|
'license': 'AGPL-3',
|
||||||
|
'website': 'https://hibou.io',
|
||||||
|
'data': [
|
||||||
|
'security/ir.model.access.csv',
|
||||||
|
'views/delivery_views.xml',
|
||||||
|
'views/opencart_backend_views.xml',
|
||||||
|
],
|
||||||
|
'installable': True,
|
||||||
|
'application': False,
|
||||||
|
}
|
||||||
6
connector_opencart/components/__init__.py
Normal file
6
connector_opencart/components/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
from . import api
|
||||||
|
from . import backend_adapter
|
||||||
|
from . import binder
|
||||||
|
from . import importer
|
||||||
|
from . import exporter
|
||||||
|
from . import mapper
|
||||||
1
connector_opencart/components/api/__init__.py
Normal file
1
connector_opencart/components/api/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import opencart
|
||||||
102
connector_opencart/components/api/opencart.py
Normal file
102
connector_opencart/components/api/opencart.py
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
import requests
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
from json import loads, dumps
|
||||||
|
import logging
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class Opencart:
|
||||||
|
|
||||||
|
def __init__(self, base_url, restadmin_token):
|
||||||
|
self.base_url = str(base_url) + '/api/rest_admin/'
|
||||||
|
self.restadmin_token = restadmin_token
|
||||||
|
self.session = requests.Session()
|
||||||
|
self.session.headers['X-Oc-Restadmin-Id'] = self.restadmin_token
|
||||||
|
|
||||||
|
@property
|
||||||
|
def orders(self):
|
||||||
|
return Orders(connection=self)
|
||||||
|
|
||||||
|
def get_headers(self, url, method):
|
||||||
|
headers = {}
|
||||||
|
if method in ('POST', 'PUT', ):
|
||||||
|
headers['Content-Type'] = 'application/json'
|
||||||
|
return headers
|
||||||
|
|
||||||
|
def send_request(self, method, url, params=None, body=None):
|
||||||
|
encoded_url = url
|
||||||
|
if params:
|
||||||
|
encoded_url += '?%s' % urlencode(params)
|
||||||
|
headers = self.get_headers(encoded_url, method)
|
||||||
|
|
||||||
|
if method == 'GET':
|
||||||
|
return loads(self.session.get(url, params=params, headers=headers).text)
|
||||||
|
elif method == 'PUT' or method == 'POST':
|
||||||
|
return loads(self.session.put(url, data=body, headers=headers).text)
|
||||||
|
|
||||||
|
|
||||||
|
class Resource:
|
||||||
|
"""
|
||||||
|
A base class for all Resources to extend
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, connection):
|
||||||
|
self.connection = connection
|
||||||
|
|
||||||
|
@property
|
||||||
|
def url(self):
|
||||||
|
return self.connection.base_url + self.path
|
||||||
|
|
||||||
|
|
||||||
|
class Orders(Resource):
|
||||||
|
"""
|
||||||
|
Retrieves Order details
|
||||||
|
"""
|
||||||
|
|
||||||
|
path = 'orders'
|
||||||
|
|
||||||
|
def all(self, id_larger_than=None):
|
||||||
|
url = self.url
|
||||||
|
if id_larger_than:
|
||||||
|
url += '/id_larger_than/%s' % id_larger_than
|
||||||
|
return self.connection.send_request(method='GET', url=url)
|
||||||
|
|
||||||
|
def get(self, id):
|
||||||
|
url = self.url + ('/%s' % id)
|
||||||
|
return self.connection.send_request(method='GET', url=url)
|
||||||
|
|
||||||
|
def ship(self, id, tracking):
|
||||||
|
url = self.connection.base_url + ('trackingnumber/%s' % id)
|
||||||
|
res = self.connection.send_request(method='PUT', url=url, body=self.get_tracking_payload(tracking))
|
||||||
|
return self.connection.send_request(method='POST', url=url, body=self.get_status_payload('Shipped'))
|
||||||
|
|
||||||
|
|
||||||
|
def cancel(self, id):
|
||||||
|
url = self.connection.base_url + ('order_status/%s' % id)
|
||||||
|
return self.connection.send_request(method='POST', url=url, body=self.get_status_payload('Canceled'))
|
||||||
|
|
||||||
|
def get_status_payload(self, status):
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"status": "Canceled"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
payload = {
|
||||||
|
"status": status,
|
||||||
|
}
|
||||||
|
return dumps(payload)
|
||||||
|
|
||||||
|
def get_tracking_payload(self, tracking):
|
||||||
|
"""
|
||||||
|
{
|
||||||
|
"tracking": "5559994444"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
payload = {
|
||||||
|
"tracking": tracking,
|
||||||
|
}
|
||||||
|
return dumps(payload)
|
||||||
67
connector_opencart/components/backend_adapter.py
Normal file
67
connector_opencart/components/backend_adapter.py
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo.addons.component.core import AbstractComponent
|
||||||
|
from odoo.addons.queue_job.exception import RetryableJobError
|
||||||
|
from odoo.addons.connector.exception import NetworkRetryableError
|
||||||
|
from .api.opencart import Opencart
|
||||||
|
from logging import getLogger
|
||||||
|
from lxml import etree
|
||||||
|
|
||||||
|
_logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class BaseOpencartConnectorComponent(AbstractComponent):
|
||||||
|
""" Base Opencart Connector Component
|
||||||
|
|
||||||
|
All components of this connector should inherit from it.
|
||||||
|
"""
|
||||||
|
_name = 'base.opencart.connector'
|
||||||
|
_inherit = 'base.connector'
|
||||||
|
_collection = 'opencart.backend'
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartAdapter(AbstractComponent):
|
||||||
|
|
||||||
|
_name = 'opencart.adapter'
|
||||||
|
_inherit = ['base.backend.adapter', 'base.opencart.connector']
|
||||||
|
|
||||||
|
_opencart_model = None
|
||||||
|
|
||||||
|
def search(self, filters=None):
|
||||||
|
""" Search records according to some criterias
|
||||||
|
and returns a list of ids """
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def read(self, id, attributes=None):
|
||||||
|
""" Returns the information of a record """
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def search_read(self, filters=None):
|
||||||
|
""" Search records according to some criterias
|
||||||
|
and returns their information"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def create(self, data):
|
||||||
|
""" Create a record on the external system """
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def write(self, id, data):
|
||||||
|
""" Update records on the external system """
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def delete(self, id):
|
||||||
|
""" Delete a record on the external system """
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@property
|
||||||
|
def api_instance(self):
|
||||||
|
try:
|
||||||
|
opencart_api = getattr(self.work, 'opencart_api')
|
||||||
|
except AttributeError:
|
||||||
|
raise AttributeError(
|
||||||
|
'You must provide a opencart_api attribute with a '
|
||||||
|
'Opencart instance to be able to use the '
|
||||||
|
'Backend Adapter.'
|
||||||
|
)
|
||||||
|
return opencart_api
|
||||||
22
connector_opencart/components/binder.py
Normal file
22
connector_opencart/components/binder.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo.addons.component.core import Component
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartModelBinder(Component):
|
||||||
|
""" Bind records and give odoo/opencart ids correspondence
|
||||||
|
|
||||||
|
Binding models are models called ``opencart.{normal_model}``,
|
||||||
|
like ``opencart.sale.order`` or ``opencart.product.product``.
|
||||||
|
They are ``_inherits`` of the normal models and contains
|
||||||
|
the Opencart ID, the ID of the Opencart Backend and the additional
|
||||||
|
fields belonging to the Opencart instance.
|
||||||
|
"""
|
||||||
|
_name = 'opencart.binder'
|
||||||
|
_inherit = ['base.binder', 'base.opencart.connector']
|
||||||
|
_apply_on = [
|
||||||
|
'opencart.sale.order',
|
||||||
|
'opencart.sale.order.line',
|
||||||
|
'opencart.stock.picking',
|
||||||
|
]
|
||||||
313
connector_opencart/components/exporter.py
Normal file
313
connector_opencart/components/exporter.py
Normal file
@@ -0,0 +1,313 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from contextlib import contextmanager
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import psycopg2
|
||||||
|
|
||||||
|
import odoo
|
||||||
|
from odoo import _
|
||||||
|
from odoo.addons.component.core import AbstractComponent
|
||||||
|
from odoo.addons.connector.exception import (IDMissingInBackend,
|
||||||
|
RetryableJobError)
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartBaseExporter(AbstractComponent):
|
||||||
|
""" Base exporter for Opencart """
|
||||||
|
|
||||||
|
_name = 'opencart.base.exporter'
|
||||||
|
_inherit = ['base.exporter', 'base.opencart.connector']
|
||||||
|
_usage = 'record.exporter'
|
||||||
|
|
||||||
|
def __init__(self, working_context):
|
||||||
|
super(OpencartBaseExporter, self).__init__(working_context)
|
||||||
|
self.binding = None
|
||||||
|
self.external_id = None
|
||||||
|
|
||||||
|
def run(self, binding, *args, **kwargs):
|
||||||
|
""" Run the synchronization
|
||||||
|
|
||||||
|
:param binding: binding record to export
|
||||||
|
"""
|
||||||
|
self.binding = binding
|
||||||
|
self.external_id = self.binder.to_external(self.binding)
|
||||||
|
|
||||||
|
result = self._run(*args, **kwargs)
|
||||||
|
|
||||||
|
self.binder.bind(self.external_id, self.binding)
|
||||||
|
# Commit so we keep the external ID when there are several
|
||||||
|
# exports (due to dependencies) and one of them fails.
|
||||||
|
# The commit will also release the lock acquired on the binding
|
||||||
|
# record
|
||||||
|
if not odoo.tools.config['test_enable']:
|
||||||
|
self.env.cr.commit()
|
||||||
|
|
||||||
|
self._after_export()
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _run(self):
|
||||||
|
""" Flow of the synchronization, implemented in inherited classes"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def _after_export(self):
|
||||||
|
""" Can do several actions after exporting a record to Opencart """
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartExporter(AbstractComponent):
|
||||||
|
""" A common flow for the exports to Opencart """
|
||||||
|
|
||||||
|
_name = 'opencart.exporter'
|
||||||
|
_inherit = 'opencart.base.exporter'
|
||||||
|
|
||||||
|
def __init__(self, working_context):
|
||||||
|
super(OpencartExporter, self).__init__(working_context)
|
||||||
|
self.binding = None
|
||||||
|
|
||||||
|
def _lock(self):
|
||||||
|
""" Lock the binding record.
|
||||||
|
|
||||||
|
Lock the binding record so we are sure that only one export
|
||||||
|
job is running for this record if concurrent jobs have to export the
|
||||||
|
same record.
|
||||||
|
|
||||||
|
When concurrent jobs try to export the same record, the first one
|
||||||
|
will lock and proceed, the others will fail to lock and will be
|
||||||
|
retried later.
|
||||||
|
|
||||||
|
This behavior works also when the export becomes multilevel
|
||||||
|
with :meth:`_export_dependencies`. Each level will set its own lock
|
||||||
|
on the binding record it has to export.
|
||||||
|
|
||||||
|
"""
|
||||||
|
sql = ("SELECT id FROM %s WHERE ID = %%s FOR UPDATE NOWAIT" %
|
||||||
|
self.model._table)
|
||||||
|
try:
|
||||||
|
self.env.cr.execute(sql, (self.binding.id, ),
|
||||||
|
log_exceptions=False)
|
||||||
|
except psycopg2.OperationalError:
|
||||||
|
_logger.info('A concurrent job is already exporting the same '
|
||||||
|
'record (%s with id %s). Job delayed later.',
|
||||||
|
self.model._name, self.binding.id)
|
||||||
|
raise RetryableJobError(
|
||||||
|
'A concurrent job is already exporting the same record '
|
||||||
|
'(%s with id %s). The job will be retried later.' %
|
||||||
|
(self.model._name, self.binding.id))
|
||||||
|
|
||||||
|
def _has_to_skip(self):
|
||||||
|
""" Return True if the export can be skipped """
|
||||||
|
return False
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def _retry_unique_violation(self):
|
||||||
|
""" Context manager: catch Unique constraint error and retry the
|
||||||
|
job later.
|
||||||
|
|
||||||
|
When we execute several jobs workers concurrently, it happens
|
||||||
|
that 2 jobs are creating the same record at the same time (binding
|
||||||
|
record created by :meth:`_export_dependency`), resulting in:
|
||||||
|
|
||||||
|
IntegrityError: duplicate key value violates unique
|
||||||
|
constraint "opencart_product_product_odoo_uniq"
|
||||||
|
DETAIL: Key (backend_id, odoo_id)=(1, 4851) already exists.
|
||||||
|
|
||||||
|
In that case, we'll retry the import just later.
|
||||||
|
|
||||||
|
.. warning:: The unique constraint must be created on the
|
||||||
|
binding record to prevent 2 bindings to be created
|
||||||
|
for the same Opencart record.
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
except psycopg2.IntegrityError as err:
|
||||||
|
if err.pgcode == psycopg2.errorcodes.UNIQUE_VIOLATION:
|
||||||
|
raise RetryableJobError(
|
||||||
|
'A database error caused the failure of the job:\n'
|
||||||
|
'%s\n\n'
|
||||||
|
'Likely due to 2 concurrent jobs wanting to create '
|
||||||
|
'the same record. The job will be retried later.' % err)
|
||||||
|
else:
|
||||||
|
raise
|
||||||
|
|
||||||
|
def _export_dependency(self, relation, binding_model,
|
||||||
|
component_usage='record.exporter',
|
||||||
|
binding_field='opencart_bind_ids',
|
||||||
|
binding_extra_vals=None):
|
||||||
|
"""
|
||||||
|
Export a dependency. The exporter class is a subclass of
|
||||||
|
``OpencartExporter``. If a more precise class need to be defined,
|
||||||
|
it can be passed to the ``exporter_class`` keyword argument.
|
||||||
|
|
||||||
|
.. warning:: a commit is done at the end of the export of each
|
||||||
|
dependency. The reason for that is that we pushed a record
|
||||||
|
on the backend and we absolutely have to keep its ID.
|
||||||
|
|
||||||
|
So you *must* take care not to modify the Odoo
|
||||||
|
database during an export, excepted when writing
|
||||||
|
back the external ID or eventually to store
|
||||||
|
external data that we have to keep on this side.
|
||||||
|
|
||||||
|
You should call this method only at the beginning
|
||||||
|
of the exporter synchronization,
|
||||||
|
in :meth:`~._export_dependencies`.
|
||||||
|
|
||||||
|
:param relation: record to export if not already exported
|
||||||
|
:type relation: :py:class:`odoo.models.BaseModel`
|
||||||
|
:param binding_model: name of the binding model for the relation
|
||||||
|
:type binding_model: str | unicode
|
||||||
|
:param component_usage: 'usage' to look for to find the Component to
|
||||||
|
for the export, by default 'record.exporter'
|
||||||
|
:type exporter: str | unicode
|
||||||
|
:param binding_field: name of the one2many field on a normal
|
||||||
|
record that points to the binding record
|
||||||
|
(default: opencart_bind_ids).
|
||||||
|
It is used only when the relation is not
|
||||||
|
a binding but is a normal record.
|
||||||
|
:type binding_field: str | unicode
|
||||||
|
:binding_extra_vals: In case we want to create a new binding
|
||||||
|
pass extra values for this binding
|
||||||
|
:type binding_extra_vals: dict
|
||||||
|
"""
|
||||||
|
if not relation:
|
||||||
|
return
|
||||||
|
rel_binder = self.binder_for(binding_model)
|
||||||
|
# wrap is typically True if the relation is for instance a
|
||||||
|
# 'product.product' record but the binding model is
|
||||||
|
# 'opencart.product.product'
|
||||||
|
wrap = relation._name != binding_model
|
||||||
|
|
||||||
|
if wrap and hasattr(relation, binding_field):
|
||||||
|
domain = [('odoo_id', '=', relation.id),
|
||||||
|
('backend_id', '=', self.backend_record.id)]
|
||||||
|
binding = self.env[binding_model].search(domain)
|
||||||
|
if binding:
|
||||||
|
assert len(binding) == 1, (
|
||||||
|
'only 1 binding for a backend is '
|
||||||
|
'supported in _export_dependency')
|
||||||
|
# we are working with a unwrapped record (e.g.
|
||||||
|
# product.category) and the binding does not exist yet.
|
||||||
|
# Example: I created a product.product and its binding
|
||||||
|
# opencart.product.product and we are exporting it, but we need to
|
||||||
|
# create the binding for the product.category on which it
|
||||||
|
# depends.
|
||||||
|
else:
|
||||||
|
bind_values = {'backend_id': self.backend_record.id,
|
||||||
|
'odoo_id': relation.id}
|
||||||
|
if binding_extra_vals:
|
||||||
|
bind_values.update(binding_extra_vals)
|
||||||
|
# If 2 jobs create it at the same time, retry
|
||||||
|
# one later. A unique constraint (backend_id,
|
||||||
|
# odoo_id) should exist on the binding model
|
||||||
|
with self._retry_unique_violation():
|
||||||
|
binding = (self.env[binding_model]
|
||||||
|
.with_context(connector_no_export=True)
|
||||||
|
.sudo()
|
||||||
|
.create(bind_values))
|
||||||
|
# Eager commit to avoid having 2 jobs
|
||||||
|
# exporting at the same time. The constraint
|
||||||
|
# will pop if an other job already created
|
||||||
|
# the same binding. It will be caught and
|
||||||
|
# raise a RetryableJobError.
|
||||||
|
if not odoo.tools.config['test_enable']:
|
||||||
|
self.env.cr.commit() # noqa
|
||||||
|
else:
|
||||||
|
# If opencart_bind_ids does not exist we are typically in a
|
||||||
|
# "direct" binding (the binding record is the same record).
|
||||||
|
# If wrap is True, relation is already a binding record.
|
||||||
|
binding = relation
|
||||||
|
|
||||||
|
if not rel_binder.to_external(binding):
|
||||||
|
exporter = self.component(usage=component_usage,
|
||||||
|
model_name=binding_model)
|
||||||
|
exporter.run(binding)
|
||||||
|
|
||||||
|
def _export_dependencies(self):
|
||||||
|
""" Export the dependencies for the record"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def _map_data(self):
|
||||||
|
""" Returns an instance of
|
||||||
|
:py:class:`~odoo.addons.connector.components.mapper.MapRecord`
|
||||||
|
|
||||||
|
"""
|
||||||
|
return self.mapper.map_record(self.binding)
|
||||||
|
|
||||||
|
def _validate_create_data(self, data):
|
||||||
|
""" Check if the values to import are correct
|
||||||
|
|
||||||
|
Pro-actively check before the ``Model.create`` if some fields
|
||||||
|
are missing or invalid
|
||||||
|
|
||||||
|
Raise `InvalidDataError`
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def _validate_update_data(self, data):
|
||||||
|
""" Check if the values to import are correct
|
||||||
|
|
||||||
|
Pro-actively check before the ``Model.update`` if some fields
|
||||||
|
are missing or invalid
|
||||||
|
|
||||||
|
Raise `InvalidDataError`
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def _create_data(self, map_record, fields=None, **kwargs):
|
||||||
|
""" Get the data to pass to :py:meth:`_create` """
|
||||||
|
return map_record.values(for_create=True, fields=fields, **kwargs)
|
||||||
|
|
||||||
|
def _create(self, data):
|
||||||
|
""" Create the Opencart record """
|
||||||
|
# special check on data before export
|
||||||
|
self._validate_create_data(data)
|
||||||
|
return self.backend_adapter.create(data)
|
||||||
|
|
||||||
|
def _update_data(self, map_record, fields=None, **kwargs):
|
||||||
|
""" Get the data to pass to :py:meth:`_update` """
|
||||||
|
return map_record.values(fields=fields, **kwargs)
|
||||||
|
|
||||||
|
def _update(self, data):
|
||||||
|
""" Update an Opencart record """
|
||||||
|
assert self.external_id
|
||||||
|
# special check on data before export
|
||||||
|
self._validate_update_data(data)
|
||||||
|
self.backend_adapter.write(self.external_id, data)
|
||||||
|
|
||||||
|
def _run(self, fields=None):
|
||||||
|
""" Flow of the synchronization, implemented in inherited classes"""
|
||||||
|
assert self.binding
|
||||||
|
|
||||||
|
if not self.external_id:
|
||||||
|
fields = None # should be created with all the fields
|
||||||
|
|
||||||
|
if self._has_to_skip():
|
||||||
|
return
|
||||||
|
|
||||||
|
# export the missing linked resources
|
||||||
|
self._export_dependencies()
|
||||||
|
|
||||||
|
# prevent other jobs to export the same record
|
||||||
|
# will be released on commit (or rollback)
|
||||||
|
self._lock()
|
||||||
|
|
||||||
|
map_record = self._map_data()
|
||||||
|
|
||||||
|
if self.external_id:
|
||||||
|
record = self._update_data(map_record, fields=fields)
|
||||||
|
if not record:
|
||||||
|
return _('Nothing to export.')
|
||||||
|
self._update(record)
|
||||||
|
else:
|
||||||
|
record = self._create_data(map_record, fields=fields)
|
||||||
|
if not record:
|
||||||
|
return _('Nothing to export.')
|
||||||
|
self.external_id = self._create(record)
|
||||||
|
return _('Record exported with ID %s on Opencart.') % self.external_id
|
||||||
324
connector_opencart/components/importer.py
Normal file
324
connector_opencart/components/importer.py
Normal file
@@ -0,0 +1,324 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
Importers for Opencart.
|
||||||
|
|
||||||
|
An import can be skipped if the last sync date is more recent than
|
||||||
|
the last update in Opencart.
|
||||||
|
|
||||||
|
They should call the ``bind`` method if the binder even if the records
|
||||||
|
are already bound, to update the last sync date.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from odoo import fields, _
|
||||||
|
from odoo.addons.component.core import AbstractComponent, Component
|
||||||
|
from odoo.addons.connector.exception import IDMissingInBackend
|
||||||
|
from odoo.addons.queue_job.exception import NothingToDoJob
|
||||||
|
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartImporter(AbstractComponent):
|
||||||
|
""" Base importer for Opencart """
|
||||||
|
|
||||||
|
_name = 'opencart.importer'
|
||||||
|
_inherit = ['base.importer', 'base.opencart.connector']
|
||||||
|
_usage = 'record.importer'
|
||||||
|
|
||||||
|
def __init__(self, work_context):
|
||||||
|
super(OpencartImporter, self).__init__(work_context)
|
||||||
|
self.external_id = None
|
||||||
|
self.opencart_record = None
|
||||||
|
|
||||||
|
def _get_opencart_data(self):
|
||||||
|
""" Return the raw Opencart data for ``self.external_id`` """
|
||||||
|
return self.backend_adapter.read(self.external_id)
|
||||||
|
|
||||||
|
def _before_import(self):
|
||||||
|
""" Hook called before the import, when we have the Opencart
|
||||||
|
data"""
|
||||||
|
|
||||||
|
def _is_uptodate(self, binding):
|
||||||
|
"""Return True if the import should be skipped because
|
||||||
|
it is already up-to-date in Odoo"""
|
||||||
|
assert self.opencart_record
|
||||||
|
if not self.opencart_record.get('date_updated'):
|
||||||
|
return # no update date on Opencart, always import it.
|
||||||
|
if not binding:
|
||||||
|
return # it does not exist so it should not be skipped
|
||||||
|
sync = binding.sync_date
|
||||||
|
if not sync:
|
||||||
|
return
|
||||||
|
from_string = fields.Datetime.from_string
|
||||||
|
sync_date = from_string(sync)
|
||||||
|
opencart_date = from_string(self.opencart_record['date_updated'])
|
||||||
|
# if the last synchronization date is greater than the last
|
||||||
|
# update in opencart, we skip the import.
|
||||||
|
# Important: at the beginning of the exporters flows, we have to
|
||||||
|
# check if the opencart_date is more recent than the sync_date
|
||||||
|
# and if so, schedule a new import. If we don't do that, we'll
|
||||||
|
# miss changes done in Opencart
|
||||||
|
return opencart_date < sync_date
|
||||||
|
|
||||||
|
def _import_dependency(self, external_id, binding_model,
|
||||||
|
importer=None, always=False):
|
||||||
|
""" Import a dependency.
|
||||||
|
|
||||||
|
The importer class is a class or subclass of
|
||||||
|
:class:`OpencartImporter`. A specific class can be defined.
|
||||||
|
|
||||||
|
:param external_id: id of the related binding to import
|
||||||
|
:param binding_model: name of the binding model for the relation
|
||||||
|
:type binding_model: str | unicode
|
||||||
|
:param importer_component: component to use for import
|
||||||
|
By default: 'importer'
|
||||||
|
:type importer_component: Component
|
||||||
|
:param always: if True, the record is updated even if it already
|
||||||
|
exists, note that it is still skipped if it has
|
||||||
|
not been modified on Opencart since the last
|
||||||
|
update. When False, it will import it only when
|
||||||
|
it does not yet exist.
|
||||||
|
:type always: boolean
|
||||||
|
"""
|
||||||
|
if not external_id:
|
||||||
|
return
|
||||||
|
binder = self.binder_for(binding_model)
|
||||||
|
if always or not binder.to_internal(external_id):
|
||||||
|
if importer is None:
|
||||||
|
importer = self.component(usage='record.importer',
|
||||||
|
model_name=binding_model)
|
||||||
|
try:
|
||||||
|
importer.run(external_id)
|
||||||
|
except NothingToDoJob:
|
||||||
|
_logger.info(
|
||||||
|
'Dependency import of %s(%s) has been ignored.',
|
||||||
|
binding_model._name, external_id
|
||||||
|
)
|
||||||
|
|
||||||
|
def _import_dependencies(self):
|
||||||
|
""" Import the dependencies for the record
|
||||||
|
|
||||||
|
Import of dependencies can be done manually or by calling
|
||||||
|
:meth:`_import_dependency` for each dependency.
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def _map_data(self):
|
||||||
|
""" Returns an instance of
|
||||||
|
:py:class:`~odoo.addons.connector.components.mapper.MapRecord`
|
||||||
|
|
||||||
|
"""
|
||||||
|
return self.mapper.map_record(self.opencart_record)
|
||||||
|
|
||||||
|
def _validate_data(self, data):
|
||||||
|
""" Check if the values to import are correct
|
||||||
|
|
||||||
|
Pro-actively check before the ``_create`` or
|
||||||
|
``_update`` if some fields are missing or invalid.
|
||||||
|
|
||||||
|
Raise `InvalidDataError`
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def _must_skip(self):
|
||||||
|
""" Hook called right after we read the data from the backend.
|
||||||
|
|
||||||
|
If the method returns a message giving a reason for the
|
||||||
|
skipping, the import will be interrupted and the message
|
||||||
|
recorded in the job (if the import is called directly by the
|
||||||
|
job, not by dependencies).
|
||||||
|
|
||||||
|
If it returns None, the import will continue normally.
|
||||||
|
|
||||||
|
:returns: None | str | unicode
|
||||||
|
"""
|
||||||
|
return
|
||||||
|
|
||||||
|
def _get_binding(self):
|
||||||
|
return self.binder.to_internal(self.external_id)
|
||||||
|
|
||||||
|
def _create_data(self, map_record, **kwargs):
|
||||||
|
return map_record.values(for_create=True, **kwargs)
|
||||||
|
|
||||||
|
def _create(self, data):
|
||||||
|
""" Create the OpenERP record """
|
||||||
|
# special check on data before import
|
||||||
|
self._validate_data(data)
|
||||||
|
model = self.model.with_context(connector_no_export=True)
|
||||||
|
binding = model.create(data)
|
||||||
|
_logger.debug('%d created from opencart %s', binding, self.external_id)
|
||||||
|
return binding
|
||||||
|
|
||||||
|
def _update_data(self, map_record, **kwargs):
|
||||||
|
return map_record.values(**kwargs)
|
||||||
|
|
||||||
|
def _update(self, binding, data):
|
||||||
|
""" Update an OpenERP record """
|
||||||
|
# special check on data before import
|
||||||
|
self._validate_data(data)
|
||||||
|
binding.with_context(connector_no_export=True).write(data)
|
||||||
|
_logger.debug('%d updated from opencart %s', binding, self.external_id)
|
||||||
|
return
|
||||||
|
|
||||||
|
def _after_import(self, binding):
|
||||||
|
""" Hook called at the end of the import """
|
||||||
|
return
|
||||||
|
|
||||||
|
def run(self, external_id, force=False):
|
||||||
|
""" Run the synchronization
|
||||||
|
|
||||||
|
:param external_id: identifier of the record on Opencart
|
||||||
|
"""
|
||||||
|
self.external_id = external_id
|
||||||
|
lock_name = 'import({}, {}, {}, {})'.format(
|
||||||
|
self.backend_record._name,
|
||||||
|
self.backend_record.id,
|
||||||
|
self.work.model_name,
|
||||||
|
external_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.opencart_record = self._get_opencart_data()
|
||||||
|
except IDMissingInBackend:
|
||||||
|
return _('Record does no longer exist in Opencart')
|
||||||
|
|
||||||
|
skip = self._must_skip()
|
||||||
|
if skip:
|
||||||
|
return skip
|
||||||
|
|
||||||
|
binding = self._get_binding()
|
||||||
|
|
||||||
|
if not force and self._is_uptodate(binding):
|
||||||
|
return _('Already up-to-date.')
|
||||||
|
|
||||||
|
# Keep a lock on this import until the transaction is committed
|
||||||
|
# The lock is kept since we have detected that the informations
|
||||||
|
# will be updated into Odoo
|
||||||
|
self.advisory_lock_or_retry(lock_name)
|
||||||
|
self._before_import()
|
||||||
|
|
||||||
|
# import the missing linked resources
|
||||||
|
self._import_dependencies()
|
||||||
|
|
||||||
|
map_record = self._map_data()
|
||||||
|
|
||||||
|
if binding:
|
||||||
|
record = self._update_data(map_record)
|
||||||
|
self._update(binding, record)
|
||||||
|
else:
|
||||||
|
record = self._create_data(map_record)
|
||||||
|
binding = self._create(record)
|
||||||
|
|
||||||
|
self.binder.bind(self.external_id, binding)
|
||||||
|
|
||||||
|
self._after_import(binding)
|
||||||
|
|
||||||
|
|
||||||
|
class BatchImporter(AbstractComponent):
|
||||||
|
""" The role of a BatchImporter is to search for a list of
|
||||||
|
items to import, then it can either import them directly or delay
|
||||||
|
the import of each item separately.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_name = 'opencart.batch.importer'
|
||||||
|
_inherit = ['base.importer', 'base.opencart.connector']
|
||||||
|
_usage = 'batch.importer'
|
||||||
|
|
||||||
|
def run(self, filters=None):
|
||||||
|
""" Run the synchronization """
|
||||||
|
record_ids = self.backend_adapter.search(filters)
|
||||||
|
for record_id in record_ids:
|
||||||
|
self._import_record(record_id)
|
||||||
|
|
||||||
|
def _import_record(self, external_id):
|
||||||
|
""" Import a record directly or delay the import of the record.
|
||||||
|
|
||||||
|
Method to implement in sub-classes.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
class DirectBatchImporter(AbstractComponent):
|
||||||
|
""" Import the records directly, without delaying the jobs. """
|
||||||
|
|
||||||
|
_name = 'opencart.direct.batch.importer'
|
||||||
|
_inherit = 'opencart.batch.importer'
|
||||||
|
|
||||||
|
def _import_record(self, external_id):
|
||||||
|
""" Import the record directly """
|
||||||
|
self.model.import_record(self.backend_record, external_id)
|
||||||
|
|
||||||
|
|
||||||
|
class DelayedBatchImporter(AbstractComponent):
|
||||||
|
""" Delay import of the records """
|
||||||
|
|
||||||
|
_name = 'opencart.delayed.batch.importer'
|
||||||
|
_inherit = 'opencart.batch.importer'
|
||||||
|
|
||||||
|
def _import_record(self, external_id, job_options=None, **kwargs):
|
||||||
|
""" Delay the import of the records"""
|
||||||
|
delayable = self.model.with_delay(**job_options or {})
|
||||||
|
delayable.import_record(self.backend_record, external_id, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
# class SimpleRecordImporter(Component):
|
||||||
|
# """ Import one Opencart Website """
|
||||||
|
#
|
||||||
|
# _name = 'opencart.simple.record.importer'
|
||||||
|
# _inherit = 'opencart.importer'
|
||||||
|
# _apply_on = [
|
||||||
|
# 'opencart.res.partner.category',
|
||||||
|
# ]
|
||||||
|
|
||||||
|
|
||||||
|
# class TranslationImporter(Component):
|
||||||
|
# """ Import translations for a record.
|
||||||
|
#
|
||||||
|
# Usually called from importers, in ``_after_import``.
|
||||||
|
# For instance from the products and products' categories importers.
|
||||||
|
# """
|
||||||
|
#
|
||||||
|
# _name = 'opencart.translation.importer'
|
||||||
|
# _inherit = 'opencart.importer'
|
||||||
|
# _usage = 'translation.importer'
|
||||||
|
#
|
||||||
|
# def _get_opencart_data(self, storeview_id=None):
|
||||||
|
# """ Return the raw Opencart data for ``self.external_id`` """
|
||||||
|
# return self.backend_adapter.read(self.external_id, storeview_id)
|
||||||
|
#
|
||||||
|
# def run(self, external_id, binding, mapper=None):
|
||||||
|
# self.external_id = external_id
|
||||||
|
# storeviews = self.env['opencart.storeview'].search(
|
||||||
|
# [('backend_id', '=', self.backend_record.id)]
|
||||||
|
# )
|
||||||
|
# default_lang = self.backend_record.default_lang_id
|
||||||
|
# lang_storeviews = [sv for sv in storeviews
|
||||||
|
# if sv.lang_id and sv.lang_id != default_lang]
|
||||||
|
# if not lang_storeviews:
|
||||||
|
# return
|
||||||
|
#
|
||||||
|
# # find the translatable fields of the model
|
||||||
|
# fields = self.model.fields_get()
|
||||||
|
# translatable_fields = [field for field, attrs in fields.items()
|
||||||
|
# if attrs.get('translate')]
|
||||||
|
#
|
||||||
|
# if mapper is None:
|
||||||
|
# mapper = self.mapper
|
||||||
|
# else:
|
||||||
|
# mapper = self.component_by_name(mapper)
|
||||||
|
#
|
||||||
|
# for storeview in lang_storeviews:
|
||||||
|
# lang_record = self._get_opencart_data(storeview.external_id)
|
||||||
|
# map_record = mapper.map_record(lang_record)
|
||||||
|
# record = map_record.values()
|
||||||
|
#
|
||||||
|
# data = dict((field, value) for field, value in record.items()
|
||||||
|
# if field in translatable_fields)
|
||||||
|
#
|
||||||
|
# binding.with_context(connector_no_export=True,
|
||||||
|
# lang=storeview.lang_id.code).write(data)
|
||||||
16
connector_opencart/components/mapper.py
Normal file
16
connector_opencart/components/mapper.py
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo.addons.component.core import AbstractComponent
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartImportMapper(AbstractComponent):
|
||||||
|
_name = 'opencart.import.mapper'
|
||||||
|
_inherit = ['base.opencart.connector', 'base.import.mapper']
|
||||||
|
_usage = 'import.mapper'
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartExportMapper(AbstractComponent):
|
||||||
|
_name = 'opencart.export.mapper'
|
||||||
|
_inherit = ['base.opencart.connector', 'base.export.mapper']
|
||||||
|
_usage = 'export.mapper'
|
||||||
54
connector_opencart/data/connector_opencart_data.xml
Normal file
54
connector_opencart/data/connector_opencart_data.xml
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<odoo>
|
||||||
|
<data noupdate="1">
|
||||||
|
|
||||||
|
<record model="ir.cron" id="ir_cron_import_sale_orders" forcecreate="True">
|
||||||
|
<field name="name">Opencart - Import Sales Orders</field>
|
||||||
|
<field eval="False" name="active"/>
|
||||||
|
<field name="state">code</field>
|
||||||
|
<field name="user_id" ref="base.user_root"/>
|
||||||
|
<field name="interval_number">1</field>
|
||||||
|
<field name="interval_type">days</field>
|
||||||
|
<field name="numbercall">-1</field>
|
||||||
|
<field eval="False" name="doall"/>
|
||||||
|
<field ref="connector_opencart.model_opencart_backend" name="model_id"/>
|
||||||
|
<field name="code">model._scheduler_import_sale_orders()</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="excep_wrong_total_amount" model="exception.rule">
|
||||||
|
<field name="name">Total Amount differs from Opencart</field>
|
||||||
|
<field name="description">The amount computed in Odoo doesn't match with the amount in Opencart.
|
||||||
|
|
||||||
|
Cause:
|
||||||
|
The taxes are probably different between Odoo and Opencart. A fiscal position could have changed the final price.
|
||||||
|
|
||||||
|
Resolution:
|
||||||
|
Check your taxes and fiscal positions configuration and correct them if necessary.</field>
|
||||||
|
<field name="sequence">30</field>
|
||||||
|
<field name="model">sale.order</field>
|
||||||
|
<field name="rule_group">sale</field>
|
||||||
|
<field name="code">if sale.opencart_bind_ids and abs(sale.amount_total - sale.opencart_bind_ids[0].total_amount) >= 0.01:
|
||||||
|
failed = True</field>
|
||||||
|
<field name="active" eval="True"/>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="excep_wrong_total_amount_tax" model="exception.rule">
|
||||||
|
<field name="name">Total Tax Amount differs from Opencart</field>
|
||||||
|
<field name="description">The tax amount computed in Odoo doesn't match with the tax amount in Opencart.
|
||||||
|
|
||||||
|
Cause:
|
||||||
|
The taxes are probably different between Odoo and Opencart. A fiscal position could have changed the final price.
|
||||||
|
|
||||||
|
Resolution:
|
||||||
|
Check your taxes and fiscal positions configuration and correct them if necessary.</field>
|
||||||
|
<field name="sequence">30</field>
|
||||||
|
<field name="model">sale.order</field>
|
||||||
|
<field name="rule_group">sale</field>
|
||||||
|
<field name="code"># By default, a cent of difference for the tax amount is allowed, feel free to customise it in your own module
|
||||||
|
if sale.opencart_bind_ids and abs(sale.amount_tax - sale.opencart_bind_ids[0].total_amount_tax) > 0.01:
|
||||||
|
failed = True</field>
|
||||||
|
<field name="active" eval="True"/>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
</data>
|
||||||
|
</odoo>
|
||||||
5
connector_opencart/models/__init__.py
Normal file
5
connector_opencart/models/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
from . import delivery
|
||||||
|
from . import opencart_backend
|
||||||
|
from . import opencart_binding
|
||||||
|
from . import sale_order
|
||||||
|
from . import stock_picking
|
||||||
1
connector_opencart/models/delivery/__init__.py
Normal file
1
connector_opencart/models/delivery/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import common
|
||||||
22
connector_opencart/models/delivery/common.py
Normal file
22
connector_opencart/models/delivery/common.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo import models, fields, api
|
||||||
|
|
||||||
|
|
||||||
|
class DeliveryCarrier(models.Model):
|
||||||
|
""" Adds Opencart specific fields to ``delivery.carrier``
|
||||||
|
|
||||||
|
``opencart_code``
|
||||||
|
|
||||||
|
Code of the carrier delivery method in Opencart.
|
||||||
|
Example: ``USPS``
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
_inherit = "delivery.carrier"
|
||||||
|
|
||||||
|
opencart_code = fields.Char(
|
||||||
|
string='Opencart Method Code',
|
||||||
|
required=False,
|
||||||
|
)
|
||||||
1
connector_opencart/models/opencart_backend/__init__.py
Normal file
1
connector_opencart/models/opencart_backend/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import common
|
||||||
107
connector_opencart/models/opencart_backend/common.py
Normal file
107
connector_opencart/models/opencart_backend/common.py
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from logging import getLogger
|
||||||
|
from contextlib import contextmanager
|
||||||
|
|
||||||
|
from odoo import api, fields, models, _
|
||||||
|
from ...components.api.opencart import Opencart
|
||||||
|
|
||||||
|
_logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartBackend(models.Model):
|
||||||
|
_name = 'opencart.backend'
|
||||||
|
_description = 'Opencart Backend'
|
||||||
|
_inherit = 'connector.backend'
|
||||||
|
|
||||||
|
name = fields.Char(string='Name')
|
||||||
|
base_url = fields.Char(
|
||||||
|
string='Base URL',
|
||||||
|
required=True,
|
||||||
|
help='Url of your site, e.g. http://your-site.com',
|
||||||
|
)
|
||||||
|
restadmin_token = fields.Char(
|
||||||
|
string='RestAdmin Token',
|
||||||
|
required=True,
|
||||||
|
help='configured in Extensions->Modules->RestAdminAPI',
|
||||||
|
)
|
||||||
|
|
||||||
|
warehouse_id = fields.Many2one(
|
||||||
|
comodel_name='stock.warehouse',
|
||||||
|
string='Warehouse',
|
||||||
|
required=True,
|
||||||
|
help='Warehouse to use for stock.',
|
||||||
|
)
|
||||||
|
company_id = fields.Many2one(
|
||||||
|
comodel_name='res.company',
|
||||||
|
related='warehouse_id.company_id',
|
||||||
|
string='Company',
|
||||||
|
readonly=True,
|
||||||
|
)
|
||||||
|
fiscal_position_id = fields.Many2one(
|
||||||
|
comodel_name='account.fiscal.position',
|
||||||
|
string='Fiscal Position',
|
||||||
|
help='Fiscal position to use on orders.',
|
||||||
|
)
|
||||||
|
analytic_account_id = fields.Many2one(
|
||||||
|
comodel_name='account.analytic.account',
|
||||||
|
string='Analytic account',
|
||||||
|
help='If specified, this analytic account will be used to fill the '
|
||||||
|
'field on the sale order created by the connector.'
|
||||||
|
)
|
||||||
|
team_id = fields.Many2one(comodel_name='crm.team', string='Sales Team')
|
||||||
|
sale_prefix = fields.Char(
|
||||||
|
string='Sale Prefix',
|
||||||
|
help="A prefix put before the name of imported sales orders.\n"
|
||||||
|
"For instance, if the prefix is 'OC-', the sales "
|
||||||
|
"order 36071 in Opencart, will be named 'OC-36071' "
|
||||||
|
"in Odoo.",
|
||||||
|
)
|
||||||
|
# payment_mode_id = fields.Many2one(comodel_name='account.payment.mode', string="Payment Mode")
|
||||||
|
|
||||||
|
# New Product fields.
|
||||||
|
product_categ_id = fields.Many2one(comodel_name='product.category', string='Product Category',
|
||||||
|
help='Default product category for newly created products.')
|
||||||
|
|
||||||
|
import_orders_after_id = fields.Integer(
|
||||||
|
string='Import sale orders after id',
|
||||||
|
)
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
@api.multi
|
||||||
|
def work_on(self, model_name, **kwargs):
|
||||||
|
self.ensure_one()
|
||||||
|
opencart_api = Opencart(self.base_url, self.restadmin_token)
|
||||||
|
_super = super(OpencartBackend, self)
|
||||||
|
with _super.work_on(model_name, opencart_api=opencart_api, **kwargs) as work:
|
||||||
|
yield work
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def _scheduler_import_sale_orders(self):
|
||||||
|
# potential hook for customization (e.g. pad from date or provide its own)
|
||||||
|
backends = self.search([
|
||||||
|
('base_url', '!=', False),
|
||||||
|
('restadmin_token', '!=', False),
|
||||||
|
('import_orders_after_id', '!=', False),
|
||||||
|
])
|
||||||
|
return backends.import_sale_orders()
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def import_sale_orders(self):
|
||||||
|
self._import_after_id('opencart.sale.order', 'import_orders_after_id')
|
||||||
|
return True
|
||||||
|
|
||||||
|
@api.multi
|
||||||
|
def _import_after_id(self, model_name, after_id_field):
|
||||||
|
for backend in self:
|
||||||
|
after_id = backend[after_id_field]
|
||||||
|
self.env[model_name].with_delay().import_batch(
|
||||||
|
backend,
|
||||||
|
filters={'after_id': after_id}
|
||||||
|
)
|
||||||
|
# TODO !!!!!
|
||||||
|
# cannot update the ID because we don't know what Ids would be returned.
|
||||||
|
# this MUST be updated by the SO importer.
|
||||||
1
connector_opencart/models/opencart_binding/__init__.py
Normal file
1
connector_opencart/models/opencart_binding/__init__.py
Normal file
@@ -0,0 +1 @@
|
|||||||
|
from . import common
|
||||||
48
connector_opencart/models/opencart_binding/common.py
Normal file
48
connector_opencart/models/opencart_binding/common.py
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo import api, models, fields
|
||||||
|
from odoo.addons.queue_job.job import job, related_action
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartBinding(models.AbstractModel):
|
||||||
|
""" Abstract Model for the Bindings.
|
||||||
|
|
||||||
|
All of the models used as bindings between Opencart and Odoo
|
||||||
|
(``opencart.sale.order``) should ``_inherit`` from it.
|
||||||
|
"""
|
||||||
|
_name = 'opencart.binding'
|
||||||
|
_inherit = 'external.binding'
|
||||||
|
_description = 'Opencart Binding (abstract)'
|
||||||
|
|
||||||
|
backend_id = fields.Many2one(
|
||||||
|
comodel_name='opencart.backend',
|
||||||
|
string='Opencart Backend',
|
||||||
|
required=True,
|
||||||
|
ondelete='restrict',
|
||||||
|
)
|
||||||
|
external_id = fields.Char(string='ID in Opencart')
|
||||||
|
|
||||||
|
_sql_constraints = [
|
||||||
|
('opencart_uniq', 'unique(backend_id, external_id)', 'A binding already exists for this Opencart ID.'),
|
||||||
|
]
|
||||||
|
|
||||||
|
@job(default_channel='root.opencart')
|
||||||
|
@related_action(action='related_action_opencart_link')
|
||||||
|
@api.model
|
||||||
|
def import_batch(self, backend, filters=None):
|
||||||
|
""" Prepare the import of records modified on Opencart """
|
||||||
|
if filters is None:
|
||||||
|
filters = {}
|
||||||
|
with backend.work_on(self._name) as work:
|
||||||
|
importer = work.component(usage='batch.importer')
|
||||||
|
return importer.run(filters=filters)
|
||||||
|
|
||||||
|
@job(default_channel='root.opencart')
|
||||||
|
@related_action(action='related_action_opencart_link')
|
||||||
|
@api.model
|
||||||
|
def import_record(self, backend, external_id, force=False):
|
||||||
|
""" Import a Opencart record """
|
||||||
|
with backend.work_on(self._name) as work:
|
||||||
|
importer = work.component(usage='record.importer')
|
||||||
|
return importer.run(external_id, force=force)
|
||||||
2
connector_opencart/models/sale_order/__init__.py
Normal file
2
connector_opencart/models/sale_order/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from . import common
|
||||||
|
from . import importer
|
||||||
115
connector_opencart/models/sale_order/common.py
Normal file
115
connector_opencart/models/sale_order/common.py
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import odoo.addons.decimal_precision as dp
|
||||||
|
|
||||||
|
from odoo import models, fields, api
|
||||||
|
from odoo.exceptions import ValidationError
|
||||||
|
from odoo.addons.queue_job.job import job
|
||||||
|
from odoo.addons.component.core import Component
|
||||||
|
from odoo.addons.queue_job.exception import RetryableJobError
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartSaleOrder(models.Model):
|
||||||
|
_name = 'opencart.sale.order'
|
||||||
|
_inherit = 'opencart.binding'
|
||||||
|
_description = 'Opencart Sale Order'
|
||||||
|
_inherits = {'sale.order': 'odoo_id'}
|
||||||
|
|
||||||
|
odoo_id = fields.Many2one(comodel_name='sale.order',
|
||||||
|
string='Sale Order',
|
||||||
|
required=True,
|
||||||
|
ondelete='cascade')
|
||||||
|
opencart_order_line_ids = fields.One2many(
|
||||||
|
comodel_name='opencart.sale.order.line',
|
||||||
|
inverse_name='opencart_order_id',
|
||||||
|
string='Walmart Order Lines'
|
||||||
|
)
|
||||||
|
|
||||||
|
total_amount = fields.Float(
|
||||||
|
string='Total amount',
|
||||||
|
digits=dp.get_precision('Account')
|
||||||
|
)
|
||||||
|
|
||||||
|
@job(default_channel='root.opencart')
|
||||||
|
@api.model
|
||||||
|
def import_batch(self, backend, filters=None):
|
||||||
|
""" Prepare the import of Sales Orders from Opencart """
|
||||||
|
return super(OpencartSaleOrder, self).import_batch(backend, filters=filters)
|
||||||
|
|
||||||
|
|
||||||
|
class SaleOrder(models.Model):
|
||||||
|
_inherit = 'sale.order'
|
||||||
|
|
||||||
|
opencart_bind_ids = fields.One2many(
|
||||||
|
comodel_name='opencart.sale.order',
|
||||||
|
inverse_name='odoo_id',
|
||||||
|
string="Opencart Bindings",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartSaleOrderLine(models.Model):
|
||||||
|
_name = 'opencart.sale.order.line'
|
||||||
|
_inherit = 'opencart.binding'
|
||||||
|
_description = 'Opencart Sale Order Line'
|
||||||
|
_inherits = {'sale.order.line': 'odoo_id'}
|
||||||
|
|
||||||
|
opencart_order_id = fields.Many2one(comodel_name='opencart.sale.order',
|
||||||
|
string='Opencart Sale Order',
|
||||||
|
required=True,
|
||||||
|
ondelete='cascade',
|
||||||
|
index=True)
|
||||||
|
odoo_id = fields.Many2one(comodel_name='sale.order.line',
|
||||||
|
string='Sale Order Line',
|
||||||
|
required=True,
|
||||||
|
ondelete='cascade')
|
||||||
|
backend_id = fields.Many2one(related='opencart_order_id.backend_id',
|
||||||
|
string='Opencart Backend',
|
||||||
|
readonly=True,
|
||||||
|
store=True,
|
||||||
|
required=False)
|
||||||
|
|
||||||
|
@api.model
|
||||||
|
def create(self, vals):
|
||||||
|
opencart_order_id = vals['opencart_order_id']
|
||||||
|
binding = self.env['opencart.sale.order'].browse(opencart_order_id)
|
||||||
|
vals['order_id'] = binding.odoo_id.id
|
||||||
|
binding = super(OpencartSaleOrderLine, self).create(vals)
|
||||||
|
return binding
|
||||||
|
|
||||||
|
|
||||||
|
class SaleOrderLine(models.Model):
|
||||||
|
_inherit = 'sale.order.line'
|
||||||
|
|
||||||
|
opencart_bind_ids = fields.One2many(
|
||||||
|
comodel_name='opencart.sale.order.line',
|
||||||
|
inverse_name='odoo_id',
|
||||||
|
string="Opencart Bindings",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class SaleOrderAdapter(Component):
|
||||||
|
_name = 'opencart.sale.order.adapter'
|
||||||
|
_inherit = 'opencart.adapter'
|
||||||
|
_apply_on = 'opencart.sale.order'
|
||||||
|
|
||||||
|
def search(self, filters=None):
|
||||||
|
api_instance = self.api_instance
|
||||||
|
orders_response = api_instance.orders.all(id_larger_than=filters.get('after_id'))
|
||||||
|
if 'error' in orders_response and orders_response['error']:
|
||||||
|
raise ValidationError(str(orders_response))
|
||||||
|
|
||||||
|
if 'data' not in orders_response or not isinstance(orders_response['data'], list):
|
||||||
|
return []
|
||||||
|
|
||||||
|
orders = orders_response['data']
|
||||||
|
return map(lambda o: o['order_id'], orders)
|
||||||
|
|
||||||
|
def read(self, id):
|
||||||
|
api_instance = self.api_instance
|
||||||
|
record = api_instance.orders.get(id)
|
||||||
|
if 'data' in record and record['data']:
|
||||||
|
return record['data']
|
||||||
|
raise RetryableJobError('Order "' + str(id) + '" did not return an order response. ' + str(record))
|
||||||
343
connector_opencart/models/sale_order/importer.py
Normal file
343
connector_opencart/models/sale_order/importer.py
Normal file
@@ -0,0 +1,343 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
from copy import deepcopy, copy
|
||||||
|
|
||||||
|
from odoo import fields, _
|
||||||
|
from odoo.addons.component.core import Component
|
||||||
|
from odoo.addons.connector.components.mapper import mapping
|
||||||
|
from odoo.addons.queue_job.exception import NothingToDoJob, FailedJobError
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class SaleOrderBatchImporter(Component):
|
||||||
|
_name = 'opencart.sale.order.batch.importer'
|
||||||
|
_inherit = 'opencart.delayed.batch.importer'
|
||||||
|
_apply_on = 'opencart.sale.order'
|
||||||
|
|
||||||
|
def _import_record(self, external_id, job_options=None, **kwargs):
|
||||||
|
if not job_options:
|
||||||
|
job_options = {
|
||||||
|
'max_retries': 0,
|
||||||
|
'priority': 5,
|
||||||
|
}
|
||||||
|
return super(SaleOrderBatchImporter, self)._import_record(
|
||||||
|
external_id, job_options=job_options)
|
||||||
|
|
||||||
|
def run(self, filters=None):
|
||||||
|
""" Run the synchronization """
|
||||||
|
if filters is None:
|
||||||
|
filters = {}
|
||||||
|
external_ids = list(self.backend_adapter.search(filters))
|
||||||
|
for external_id in external_ids:
|
||||||
|
self._import_record(external_id)
|
||||||
|
if external_ids:
|
||||||
|
last_id = list(sorted(external_ids))[-1]
|
||||||
|
self.backend_record.import_orders_after_id = last_id
|
||||||
|
|
||||||
|
|
||||||
|
class SaleOrderImportMapper(Component):
|
||||||
|
|
||||||
|
|
||||||
|
_name = 'opencart.sale.order.mapper'
|
||||||
|
_inherit = 'opencart.import.mapper'
|
||||||
|
_apply_on = 'opencart.sale.order'
|
||||||
|
|
||||||
|
direct = [('order_id', 'external_id'),
|
||||||
|
# ('customerOrderId', 'customer_order_id'),
|
||||||
|
]
|
||||||
|
|
||||||
|
children = [('products', 'opencart_order_line_ids', 'opencart.sale.order.line'),
|
||||||
|
]
|
||||||
|
|
||||||
|
# def _map_child(self, map_record, from_attr, to_attr, model_name):
|
||||||
|
# return super(SaleOrderImportMapper, self)._map_child(map_record, from_attr, to_attr, model_name)
|
||||||
|
|
||||||
|
def _add_shipping_line(self, map_record, values):
|
||||||
|
record = map_record.source
|
||||||
|
|
||||||
|
line_builder = self.component(usage='order.line.builder.shipping')
|
||||||
|
line_builder.price_unit = 0.0
|
||||||
|
|
||||||
|
if values.get('carrier_id'):
|
||||||
|
carrier = self.env['delivery.carrier'].browse(values['carrier_id'])
|
||||||
|
line_builder.product = carrier.product_id
|
||||||
|
|
||||||
|
line = (0, 0, line_builder.get_line())
|
||||||
|
values['order_line'].append(line)
|
||||||
|
return values
|
||||||
|
|
||||||
|
def finalize(self, map_record, values):
|
||||||
|
values.setdefault('order_line', [])
|
||||||
|
self._add_shipping_line(map_record, values)
|
||||||
|
values.update({
|
||||||
|
'partner_id': self.options.partner_id,
|
||||||
|
'partner_invoice_id': self.options.partner_invoice_id,
|
||||||
|
'partner_shipping_id': self.options.partner_shipping_id,
|
||||||
|
})
|
||||||
|
onchange = self.component(
|
||||||
|
usage='ecommerce.onchange.manager.sale.order'
|
||||||
|
)
|
||||||
|
# will I need more?!
|
||||||
|
return onchange.play(values, values['opencart_order_line_ids'])
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def name(self, record):
|
||||||
|
name = str(record['order_id'])
|
||||||
|
prefix = self.backend_record.sale_prefix
|
||||||
|
if prefix:
|
||||||
|
name = prefix + name
|
||||||
|
return {'name': name}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def date_order(self, record):
|
||||||
|
return {'date_order': record.get('date_added', fields.Datetime.now())}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def fiscal_position_id(self, record):
|
||||||
|
if self.backend_record.fiscal_position_id:
|
||||||
|
return {'fiscal_position_id': self.backend_record.fiscal_position_id.id}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def team_id(self, record):
|
||||||
|
if self.backend_record.team_id:
|
||||||
|
return {'team_id': self.backend_record.team_id.id}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def payment_mode_id(self, record):
|
||||||
|
record_method = record['payment_method']
|
||||||
|
method = self.env['account.payment.mode'].search(
|
||||||
|
[('name', '=', record_method)],
|
||||||
|
limit=1,
|
||||||
|
)
|
||||||
|
assert method, ("method %s should exist because the import fails "
|
||||||
|
"in SaleOrderImporter._before_import when it is "
|
||||||
|
" missing" % record_method)
|
||||||
|
return {'payment_mode_id': method.id}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def project_id(self, record):
|
||||||
|
if self.backend_record.analytic_account_id:
|
||||||
|
return {'project_id': self.backend_record.analytic_account_id.id}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def warehouse_id(self, record):
|
||||||
|
if self.backend_record.warehouse_id:
|
||||||
|
return {'warehouse_id': self.backend_record.warehouse_id.id}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def shipping_method(self, record):
|
||||||
|
method = record['shipping_method']
|
||||||
|
carrier = self.env['delivery.carrier'].search([('opencart_code', '=', method)], limit=1)
|
||||||
|
if not carrier:
|
||||||
|
raise ValueError('Delivery Carrier for methodCode "%s", cannot be found.' % (method, ))
|
||||||
|
return {'carrier_id': carrier.id, 'shipping_method_code': method}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def backend_id(self, record):
|
||||||
|
return {'backend_id': self.backend_record.id}
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def total_amount(self, record):
|
||||||
|
# lines = record['total']
|
||||||
|
total_amount = record['total']
|
||||||
|
total_amount_tax = 0.0
|
||||||
|
# for l in lines:
|
||||||
|
# item_amount, tax_amount = walk_charges(l['charges'])
|
||||||
|
# total_amount += item_amount + tax_amount
|
||||||
|
# total_amount_tax += tax_amount
|
||||||
|
return {'total_amount': total_amount, 'total_amount_tax': total_amount_tax}
|
||||||
|
|
||||||
|
|
||||||
|
class SaleOrderImporter(Component):
|
||||||
|
_name = 'opencart.sale.order.importer'
|
||||||
|
_inherit = 'opencart.importer'
|
||||||
|
_apply_on = 'opencart.sale.order'
|
||||||
|
|
||||||
|
def _must_skip(self):
|
||||||
|
if self.binder.to_internal(self.external_id):
|
||||||
|
return _('Already imported')
|
||||||
|
|
||||||
|
def _before_import(self):
|
||||||
|
# Check if status is ok, etc. on self.opencart_record
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _create_partner(self, values):
|
||||||
|
return self.env['res.partner'].create(values)
|
||||||
|
|
||||||
|
def _partner_matches(self, partner, values):
|
||||||
|
for key, value in values.items():
|
||||||
|
if key == 'state_id':
|
||||||
|
if value != partner.state_id.id:
|
||||||
|
return False
|
||||||
|
elif key == 'country_id':
|
||||||
|
if value != partner.country_id.id:
|
||||||
|
return False
|
||||||
|
elif bool(value) and value != getattr(partner, key):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _make_partner_name(self, firstname, lastname):
|
||||||
|
name = (str(firstname) + ' ' + str(lastname)).strip()
|
||||||
|
if not name:
|
||||||
|
return 'Undefined'
|
||||||
|
return name
|
||||||
|
|
||||||
|
def _get_partner_values(self, info_string='shipping_'):
|
||||||
|
record = self.opencart_record
|
||||||
|
|
||||||
|
# find or make partner with these details.
|
||||||
|
email = record.get('email')
|
||||||
|
if not email:
|
||||||
|
raise ValueError('Order does not have email in : ' + str(record))
|
||||||
|
|
||||||
|
phone = record.get('telephone', False)
|
||||||
|
|
||||||
|
info = {}
|
||||||
|
for k, v in record.items():
|
||||||
|
# Strip the info_string so that the remainder of the code depends on it.
|
||||||
|
if k.find(info_string) == 0:
|
||||||
|
info[k[len(info_string):]] = v
|
||||||
|
|
||||||
|
|
||||||
|
name = self._make_partner_name(info.get('firstname', ''), info.get('lastname'))
|
||||||
|
street = info.get('address_1', '')
|
||||||
|
street2 = info.get('address_2', '')
|
||||||
|
city = info.get('city', '')
|
||||||
|
state_code = info.get('zone_code', '')
|
||||||
|
zip_ = info.get('postcode', '')
|
||||||
|
country_code = info.get('iso_code_2', '')
|
||||||
|
country = self.env['res.country'].search([('code', '=', country_code)], limit=1)
|
||||||
|
state = self.env['res.country.state'].search([
|
||||||
|
('country_id', '=', country.id),
|
||||||
|
('code', '=', state_code)
|
||||||
|
], limit=1)
|
||||||
|
|
||||||
|
return {
|
||||||
|
'email': email,
|
||||||
|
'name': name,
|
||||||
|
'phone': phone,
|
||||||
|
'street': street,
|
||||||
|
'street2': street2,
|
||||||
|
'zip': zip_,
|
||||||
|
'city': city,
|
||||||
|
'state_id': state.id,
|
||||||
|
'country_id': country.id,
|
||||||
|
}
|
||||||
|
|
||||||
|
def _import_addresses(self):
|
||||||
|
record = self.opencart_record
|
||||||
|
|
||||||
|
partner_values = self._get_partner_values()
|
||||||
|
partner = self.env['res.partner'].search([
|
||||||
|
('email', '=', partner_values['email']),
|
||||||
|
], limit=1)
|
||||||
|
|
||||||
|
if not partner:
|
||||||
|
# create partner.
|
||||||
|
partner = self._create_partner(copy(partner_values))
|
||||||
|
|
||||||
|
if not self._partner_matches(partner, partner_values):
|
||||||
|
partner_values['parent_id'] = partner.id
|
||||||
|
partner_values['active'] = False
|
||||||
|
shipping_partner = self._create_partner(copy(partner_values))
|
||||||
|
else:
|
||||||
|
shipping_partner = partner
|
||||||
|
|
||||||
|
invoice_values = self._get_partner_values(info_string='payment_')
|
||||||
|
|
||||||
|
if (not self._partner_matches(partner, invoice_values)
|
||||||
|
and not self._partner_matches(shipping_partner, invoice_values)):
|
||||||
|
partner_values['parent_id'] = partner.id
|
||||||
|
partner_values['active'] = False
|
||||||
|
invoice_partner = self._create_partner(copy(invoice_values))
|
||||||
|
elif self._partner_matches(partner, invoice_values):
|
||||||
|
invoice_partner = partner
|
||||||
|
elif self._partner_matches(shipping_partner, invoice_values):
|
||||||
|
invoice_partner = shipping_partner
|
||||||
|
|
||||||
|
self.partner = partner
|
||||||
|
self.shipping_partner = shipping_partner
|
||||||
|
self.invoice_partner = invoice_partner
|
||||||
|
|
||||||
|
def _check_special_fields(self):
|
||||||
|
assert self.partner, (
|
||||||
|
"self.partner should have been defined "
|
||||||
|
"in SaleOrderImporter._import_addresses")
|
||||||
|
assert self.shipping_partner, (
|
||||||
|
"self.shipping_partner should have been defined "
|
||||||
|
"in SaleOrderImporter._import_addresses")
|
||||||
|
assert self.invoice_partner, (
|
||||||
|
"self.invoice_partner should have been defined "
|
||||||
|
"in SaleOrderImporter._import_addresses")
|
||||||
|
|
||||||
|
def _create_data(self, map_record, **kwargs):
|
||||||
|
# non dependencies
|
||||||
|
self._check_special_fields()
|
||||||
|
return super(SaleOrderImporter, self)._create_data(
|
||||||
|
map_record,
|
||||||
|
partner_id=self.partner.id,
|
||||||
|
partner_invoice_id=self.invoice_partner.id,
|
||||||
|
partner_shipping_id=self.shipping_partner.id,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
|
def _create(self, data):
|
||||||
|
binding = super(SaleOrderImporter, self)._create(data)
|
||||||
|
# Without this, it won't map taxes with the fiscal position.
|
||||||
|
if binding.fiscal_position_id:
|
||||||
|
binding.odoo_id._compute_tax_id()
|
||||||
|
|
||||||
|
# if binding.backend_id.acknowledge_order == 'order_create':
|
||||||
|
# binding.with_delay().acknowledge_order(binding.backend_id, binding.external_id)
|
||||||
|
|
||||||
|
return binding
|
||||||
|
|
||||||
|
def _import_dependencies(self):
|
||||||
|
record = self.opencart_record
|
||||||
|
|
||||||
|
self._import_addresses()
|
||||||
|
|
||||||
|
class SaleOrderLineImportMapper(Component):
|
||||||
|
|
||||||
|
_name = 'opencart.sale.order.line.mapper'
|
||||||
|
_inherit = 'opencart.import.mapper'
|
||||||
|
_apply_on = 'opencart.sale.order.line'
|
||||||
|
|
||||||
|
direct = [('quantity', 'product_uom_qty'),
|
||||||
|
('price', 'price_unit'),
|
||||||
|
('name', 'name'),
|
||||||
|
('order_product_id', 'external_id'),
|
||||||
|
]
|
||||||
|
|
||||||
|
def _finalize_product_values(self, record, values):
|
||||||
|
# This would be a good place to create a vendor or add a route...
|
||||||
|
return values
|
||||||
|
|
||||||
|
def _product_values(self, record):
|
||||||
|
reference = record['model']
|
||||||
|
values = {
|
||||||
|
'default_code': reference,
|
||||||
|
'name': record.get('name', reference),
|
||||||
|
'type': 'product',
|
||||||
|
'list_price': record.get('price', 0.0),
|
||||||
|
'categ_id': self.backend_record.product_categ_id.id,
|
||||||
|
}
|
||||||
|
return self._finalize_product_values(record, values)
|
||||||
|
|
||||||
|
@mapping
|
||||||
|
def product_id(self, record):
|
||||||
|
reference = record['model']
|
||||||
|
product = self.env['product.product'].search([
|
||||||
|
('default_code', '=', reference)
|
||||||
|
], limit=1)
|
||||||
|
|
||||||
|
if not product:
|
||||||
|
# we could use a record like (0, 0, values)
|
||||||
|
product = self.env['product.product'].create(self._product_values(record))
|
||||||
|
|
||||||
|
return {'product_id': product.id}
|
||||||
2
connector_opencart/models/stock_picking/__init__.py
Normal file
2
connector_opencart/models/stock_picking/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
from . import common
|
||||||
|
from . import exporter
|
||||||
94
connector_opencart/models/stock_picking/common.py
Normal file
94
connector_opencart/models/stock_picking/common.py
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from odoo import api, models, fields
|
||||||
|
from odoo.addons.queue_job.job import job, related_action
|
||||||
|
from odoo.addons.component.core import Component
|
||||||
|
from odoo.addons.queue_job.exception import RetryableJobError
|
||||||
|
|
||||||
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartStockPicking(models.Model):
|
||||||
|
_name = 'opencart.stock.picking'
|
||||||
|
_inherit = 'opencart.binding'
|
||||||
|
_inherits = {'stock.picking': 'odoo_id'}
|
||||||
|
_description = 'Opencart Delivery Order'
|
||||||
|
|
||||||
|
odoo_id = fields.Many2one(comodel_name='stock.picking',
|
||||||
|
string='Stock Picking',
|
||||||
|
required=True,
|
||||||
|
ondelete='cascade')
|
||||||
|
opencart_order_id = fields.Many2one(comodel_name='opencart.sale.order',
|
||||||
|
string='Opencart Sale Order',
|
||||||
|
ondelete='set null')
|
||||||
|
|
||||||
|
@job(default_channel='root.opencart')
|
||||||
|
@related_action(action='related_action_unwrap_binding')
|
||||||
|
@api.multi
|
||||||
|
def export_picking_done(self):
|
||||||
|
""" Export a complete or partial delivery order. """
|
||||||
|
self.ensure_one()
|
||||||
|
with self.backend_id.work_on(self._name) as work:
|
||||||
|
exporter = work.component(usage='record.exporter')
|
||||||
|
return exporter.run(self)
|
||||||
|
|
||||||
|
|
||||||
|
class StockPicking(models.Model):
|
||||||
|
_inherit = 'stock.picking'
|
||||||
|
|
||||||
|
opencart_bind_ids = fields.One2many(
|
||||||
|
comodel_name='opencart.stock.picking',
|
||||||
|
inverse_name='odoo_id',
|
||||||
|
string="Opencart Bindings",
|
||||||
|
)
|
||||||
|
|
||||||
|
class StockPickingAdapter(Component):
|
||||||
|
_name = 'opencart.stock.picking.adapter'
|
||||||
|
_inherit = 'opencart.adapter'
|
||||||
|
_apply_on = 'opencart.stock.picking'
|
||||||
|
|
||||||
|
def create(self, id, tracking):
|
||||||
|
api_instance = self.api_instance
|
||||||
|
result = api_instance.orders.ship(id, tracking)
|
||||||
|
if 'success' in result:
|
||||||
|
return result['success']
|
||||||
|
raise RetryableJobError('Shipping Order %s did not return an order response. (tracking: %s) %s' % (
|
||||||
|
str(id), str(tracking), str(result)))
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartBindingStockPickingListener(Component):
|
||||||
|
_name = 'opencart.binding.stock.picking.listener'
|
||||||
|
_inherit = 'base.event.listener'
|
||||||
|
_apply_on = ['opencart.stock.picking']
|
||||||
|
|
||||||
|
def on_record_create(self, record, fields=None):
|
||||||
|
record.with_delay().export_picking_done()
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartStockPickingListener(Component):
|
||||||
|
_name = 'opencart.stock.picking.listener'
|
||||||
|
_inherit = 'base.event.listener'
|
||||||
|
_apply_on = ['stock.picking']
|
||||||
|
|
||||||
|
def on_picking_dropship_done(self, record, picking_method):
|
||||||
|
return self.on_picking_out_done(record, picking_method)
|
||||||
|
|
||||||
|
def on_picking_out_done(self, record, picking_method):
|
||||||
|
"""
|
||||||
|
Create a ``opencart.stock.picking`` record. This record will then
|
||||||
|
be exported to Opencart.
|
||||||
|
|
||||||
|
:param picking_method: picking_method, can be 'complete' or 'partial'
|
||||||
|
:type picking_method: str
|
||||||
|
"""
|
||||||
|
sale = record.sale_id
|
||||||
|
if not sale:
|
||||||
|
return
|
||||||
|
for opencart_sale in sale.opencart_bind_ids:
|
||||||
|
self.env['opencart.stock.picking'].create({
|
||||||
|
'backend_id': opencart_sale.backend_id.id,
|
||||||
|
'odoo_id': record.id,
|
||||||
|
'opencart_order_id': opencart_sale.id,
|
||||||
|
})
|
||||||
39
connector_opencart/models/stock_picking/exporter.py
Normal file
39
connector_opencart/models/stock_picking/exporter.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# © 2019 Hibou Corp.
|
||||||
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||||
|
|
||||||
|
from odoo import fields
|
||||||
|
from odoo.addons.component.core import Component
|
||||||
|
from odoo.addons.queue_job.exception import NothingToDoJob
|
||||||
|
from logging import getLogger
|
||||||
|
|
||||||
|
_logger = getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
class OpencartPickingExporter(Component):
|
||||||
|
_name = 'opencart.stock.picking.exporter'
|
||||||
|
_inherit = 'opencart.exporter'
|
||||||
|
_apply_on = ['opencart.stock.picking']
|
||||||
|
|
||||||
|
def _get_id(self, binding):
|
||||||
|
sale_binder = self.binder_for('opencart.sale.order')
|
||||||
|
opencart_sale_id = sale_binder.to_external(binding.opencart_order_id)
|
||||||
|
return opencart_sale_id
|
||||||
|
|
||||||
|
def _get_tracking(self, binding):
|
||||||
|
return binding.carrier_tracking_ref or ''
|
||||||
|
|
||||||
|
def run(self, binding):
|
||||||
|
"""
|
||||||
|
Export the picking to Opencart
|
||||||
|
:param binding: opencart.stock.picking
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
if binding.external_id:
|
||||||
|
return 'Already exported'
|
||||||
|
tracking = self._get_tracking(binding)
|
||||||
|
if not tracking:
|
||||||
|
raise NothingToDoJob('Cancelled: the delivery order does not contain tracking.')
|
||||||
|
id = self._get_id(binding)
|
||||||
|
_ = self.backend_adapter.create(id, tracking)
|
||||||
|
# Cannot bind because shipments do not have ID's in Opencart
|
||||||
|
#self.binder.bind(external_id, binding)
|
||||||
10
connector_opencart/security/ir.model.access.csv
Normal file
10
connector_opencart/security/ir.model.access.csv
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
|
||||||
|
"access_opencart_backend","opencart_backend connector manager","model_opencart_backend","connector.group_connector_manager",1,1,1,1
|
||||||
|
"access_opencart_binding","opencart_binding connector manager","model_opencart_binding","connector.group_connector_manager",1,1,1,1
|
||||||
|
"access_opencart_sale_order","opencart_sale_order connector manager","model_opencart_sale_order","connector.group_connector_manager",1,1,1,1
|
||||||
|
"access_opencart_sale_order_line","opencart_sale_order_line connector manager","model_opencart_sale_order_line","connector.group_connector_manager",1,1,1,1
|
||||||
|
"access_opencart_stock_picking","opencart_stock_picking connector manager","model_opencart_stock_picking","connector.group_connector_manager",1,1,1,1
|
||||||
|
"access_opencart_sale_order_sale_salesman","opencart_sale_order","model_opencart_sale_order","sales_team.group_sale_salesman",1,0,0,0
|
||||||
|
"access_opencart_sale_order_sale_manager","opencart_sale_order","model_opencart_sale_order","sales_team.group_sale_manager",1,1,1,1
|
||||||
|
"access_opencart_sale_order_stock_user","opencart_sale_order warehouse user","model_opencart_sale_order","stock.group_stock_user",1,0,0,0
|
||||||
|
"access_opencart_backend_user","opencart_backend user","model_opencart_backend","sales_team.group_sale_salesman",1,0,0,0
|
||||||
|
19
connector_opencart/views/delivery_views.xml
Normal file
19
connector_opencart/views/delivery_views.xml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<odoo>
|
||||||
|
|
||||||
|
<record id="view_opencart_delivery_carrier_form" model="ir.ui.view">
|
||||||
|
<field name="name">opencart.delivery.carrier.form</field>
|
||||||
|
<field name="model">delivery.carrier</field>
|
||||||
|
<field name="inherit_id" ref="delivery.view_delivery_carrier_form" />
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<xpath expr="//notebook" position="inside">
|
||||||
|
<page string="Opencart" name="opencart">
|
||||||
|
<group name="opencart_info">
|
||||||
|
<field name="opencart_code"/>
|
||||||
|
</group>
|
||||||
|
</page>
|
||||||
|
</xpath>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
</odoo>
|
||||||
109
connector_opencart/views/opencart_backend_views.xml
Normal file
109
connector_opencart/views/opencart_backend_views.xml
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<odoo>
|
||||||
|
|
||||||
|
<record id="view_opencart_backend_form" model="ir.ui.view">
|
||||||
|
<field name="name">opencart.backend.form</field>
|
||||||
|
<field name="model">opencart.backend</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<form string="Opencart Backend">
|
||||||
|
<header>
|
||||||
|
</header>
|
||||||
|
<sheet>
|
||||||
|
<label for="name" class="oe_edit_only"/>
|
||||||
|
<h1>
|
||||||
|
<field name="name" class="oe_inline" />
|
||||||
|
</h1>
|
||||||
|
<group name="opencart" string="Opencart Configuration">
|
||||||
|
<notebook name="api">
|
||||||
|
<page string="API" name="api">
|
||||||
|
<group colspan="4" col="4">
|
||||||
|
<field name="base_url"/>
|
||||||
|
<field name="restadmin_token" password="1"/>
|
||||||
|
</group>
|
||||||
|
</page>
|
||||||
|
</notebook>
|
||||||
|
</group>
|
||||||
|
<group name="main_configuration" string="Main Configuration">
|
||||||
|
<group name="order_configuration" string="Order Defaults">
|
||||||
|
<field name="warehouse_id"/>
|
||||||
|
<field name="analytic_account_id"/>
|
||||||
|
<field name="fiscal_position_id"/>
|
||||||
|
<field name="team_id"/>
|
||||||
|
<field name="sale_prefix"/>
|
||||||
|
</group>
|
||||||
|
<group name="product_configuration" string="Product Defaults">
|
||||||
|
<field name="product_categ_id"/>
|
||||||
|
</group>
|
||||||
|
</group>
|
||||||
|
<notebook name="import_config">
|
||||||
|
<page name="import" string="Imports">
|
||||||
|
<p class="oe_grey oe_inline">
|
||||||
|
By clicking on the buttons,
|
||||||
|
you will initiate the synchronizations
|
||||||
|
with Opencart.
|
||||||
|
Note that the import or exports
|
||||||
|
won't be done directly,
|
||||||
|
they will create 'Jobs'
|
||||||
|
executed as soon as possible.
|
||||||
|
</p>
|
||||||
|
<p class="oe_grey oe_inline">
|
||||||
|
Once imported,
|
||||||
|
some types of records,
|
||||||
|
like the products or categories,
|
||||||
|
need a manual review.
|
||||||
|
You will find the list
|
||||||
|
of the new records to review
|
||||||
|
in the menu 'Connectors > Checkpoint'.
|
||||||
|
</p>
|
||||||
|
<group name="import_since">
|
||||||
|
<div>
|
||||||
|
<label for="import_orders_after_id" string="Import sale orders after Order ID" class="oe_inline"/>
|
||||||
|
<field name="import_orders_after_id"
|
||||||
|
class="oe_inline"
|
||||||
|
nolabel="1"/>
|
||||||
|
</div>
|
||||||
|
<button name="import_sale_orders"
|
||||||
|
type="object"
|
||||||
|
class="oe_highlight"
|
||||||
|
string="Import in background"/>
|
||||||
|
</group>
|
||||||
|
|
||||||
|
</page>
|
||||||
|
|
||||||
|
</notebook>
|
||||||
|
</sheet>
|
||||||
|
</form>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="view_opencart_backend_tree" model="ir.ui.view">
|
||||||
|
<field name="name">opencart.backend.tree</field>
|
||||||
|
<field name="model">opencart.backend</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<tree string="Opencart Backend">
|
||||||
|
<field name="name"/>
|
||||||
|
<field name="import_orders_after_id"/>
|
||||||
|
</tree>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="action_opencart_backend" model="ir.actions.act_window">
|
||||||
|
<field name="name">Opencart Backends</field>
|
||||||
|
<field name="res_model">opencart.backend</field>
|
||||||
|
<field name="view_type">form</field>
|
||||||
|
<field name="view_mode">tree,form</field>
|
||||||
|
<field name="view_id" ref="view_opencart_backend_tree"/>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<menuitem id="menu_opencart_root"
|
||||||
|
parent="connector.menu_connector_root"
|
||||||
|
name="Opencart"
|
||||||
|
sequence="10"
|
||||||
|
groups="connector.group_connector_manager"/>
|
||||||
|
|
||||||
|
<menuitem id="menu_opencart_backend"
|
||||||
|
name="Backends"
|
||||||
|
parent="menu_opencart_root"
|
||||||
|
action="action_opencart_backend"/>
|
||||||
|
|
||||||
|
</odoo>
|
||||||
53
connector_opencart/views/sale_order_views.xml
Normal file
53
connector_opencart/views/sale_order_views.xml
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<odoo>
|
||||||
|
<!--
|
||||||
|
<record id="view_sale_order_opencart_form" model="ir.ui.view">
|
||||||
|
<field name="name">sale.order.opencart.form</field>
|
||||||
|
<field name="model">sale.order</field>
|
||||||
|
<field name="inherit_id" ref="connector_ecommerce.view_order_connector_form"/>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<page name="connector" position="attributes">
|
||||||
|
<attribute name="invisible">0</attribute>
|
||||||
|
</page>
|
||||||
|
<page name="connector" position="inside">
|
||||||
|
<group string="Opencart Bindings">
|
||||||
|
<field name="opencart_bind_ids" nolabel="1"/>
|
||||||
|
</group>
|
||||||
|
</page>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="view_opencart_sale_order_form" model="ir.ui.view">
|
||||||
|
<field name="name">opencart.sale.order.form</field>
|
||||||
|
<field name="model">opencart.sale.order</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<form string="Opencart Sales Orders"
|
||||||
|
create="false" delete="false">
|
||||||
|
<group>
|
||||||
|
<field name="backend_id"/>
|
||||||
|
<field name="external_id"/>
|
||||||
|
<field name="customer_order_id"/>
|
||||||
|
<field name="total_amount"/>
|
||||||
|
<field name="total_amount_tax"/>
|
||||||
|
<field name="shipping_method_code"/>
|
||||||
|
</group>
|
||||||
|
</form>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
|
||||||
|
<record id="view_opencart_sale_order_tree" model="ir.ui.view">
|
||||||
|
<field name="name">opencart.sale.order.tree</field>
|
||||||
|
<field name="model">opencart.sale.order</field>
|
||||||
|
<field name="arch" type="xml">
|
||||||
|
<tree string="Opencart Sales Orders"
|
||||||
|
create="false" delete="false">
|
||||||
|
<field name="backend_id"/>
|
||||||
|
<field name="external_id"/>
|
||||||
|
<field name="customer_order_id"/>
|
||||||
|
<field name="total_amount"/>
|
||||||
|
<field name="total_amount_tax"/>
|
||||||
|
</tree>
|
||||||
|
</field>
|
||||||
|
</record>
|
||||||
|
-->
|
||||||
|
</odoo>
|
||||||
Reference in New Issue
Block a user