diff --git a/om_connector_base/README.rst b/om_connector_base/README.rst
new file mode 100644
index 00000000..f11018b5
--- /dev/null
+++ b/om_connector_base/README.rst
@@ -0,0 +1,35 @@
+==============
+Connector Base
+==============
+
+The common engine of the Odoo Mates connectors to shops and marketplaces (the Shopify Connector, and the
+next ones). It has nothing to set up: the connectors that need it install it.
+
+Features
+========
+
+* **Job queue**: every import, export and webhook of a connector is a job, run by the scheduled action
+ *Connectors: run the jobs*. Two workers never run the same job (row locking).
+* **Retries**: a job that fails leaves no change and is tried again after 2, 4, 8 minutes and more, up to its
+ number of attempts; an error of the user (``UserError``) fails it at once. The connection gets told of the
+ jobs that fail for good.
+* **No duplicates**: the jobs of a connection with the same key make one job, with the newest data.
+* **Mappings**: an abstract model links an external ID to an Odoo record, once per connection.
+* **Order workflows**: confirm an imported order, create and post its invoice, register one or several
+ payments, pay back its credit notes.
+* **Secrets**: helpers to store access tokens and secrets encrypted (key: the ``om_connector_key`` option of
+ the Odoo configuration file, else the secret of the database).
+* **Webhooks and HTTP**: HMAC-SHA256 signatures checked in constant time, and HTTP calls that wait and try
+ again on rate limits, server and network errors.
+
+Jobs: Settings > Technical > Connectors > Jobs, and the menu of each connector. Done jobs are removed after
+30 days (system parameter ``om_connector_base.job_keep_days``).
+
+For developers
+==============
+
+* A connection inherits ``om.connector.backend.mixin`` and queues work with
+ ``self._connector_enqueue('_connector_job_import_order', payload, dedupe_key='order:42')``; the method gets
+ the job and reads ``job.payload`` and ``job.backend``.
+* A mapping inherits ``om.connector.binding.mixin`` (``_connector_find``, ``_connector_bind``).
+* ``RetryableJobError(message, delay=60)`` tries a job again later.
diff --git a/om_connector_base/__init__.py b/om_connector_base/__init__.py
new file mode 100644
index 00000000..738a2eec
--- /dev/null
+++ b/om_connector_base/__init__.py
@@ -0,0 +1,2 @@
+from . import models
+from . import tools
diff --git a/om_connector_base/__manifest__.py b/om_connector_base/__manifest__.py
new file mode 100644
index 00000000..9741a27c
--- /dev/null
+++ b/om_connector_base/__manifest__.py
@@ -0,0 +1,23 @@
+{
+ 'name': 'Connector Base',
+ 'version': '1.0',
+ 'author': 'Odoo Mates',
+ 'category': 'Sales',
+ 'summary': 'Job queue, retries, external ID mappings and webhook helpers for shop and marketplace connectors',
+ 'description': 'The common engine of the Odoo Mates connectors (Shopify and others): a job queue with retries, '
+ 'workflows for the imported orders (confirm, invoice, pay), mappings between external records and Odoo records, and webhook helpers.',
+ 'maintainer': 'Odoo Mates',
+ 'support': 'odoomates@gmail.com',
+ 'license': 'LGPL-3',
+ 'depends': ['mail', 'sale_management'],
+ 'data': [
+ 'security/om_connector_groups.xml',
+ 'security/ir.model.access.csv',
+ 'security/ir_rule.xml',
+ 'data/ir_cron.xml',
+ 'views/om_connector_job_views.xml',
+ 'views/om_connector_order_workflow_views.xml',
+ 'views/menu.xml',
+ ],
+ 'images': ['static/description/banner.gif'],
+}
diff --git a/om_connector_base/data/ir_cron.xml b/om_connector_base/data/ir_cron.xml
new file mode 100644
index 00000000..1f8ded04
--- /dev/null
+++ b/om_connector_base/data/ir_cron.xml
@@ -0,0 +1,11 @@
+
+
+
+ Connectors: run the jobs
+
+ code
+ model._cron_run_jobs()
+ 5
+ minutes
+
+
diff --git a/om_connector_base/models/__init__.py b/om_connector_base/models/__init__.py
new file mode 100644
index 00000000..a8bd5497
--- /dev/null
+++ b/om_connector_base/models/__init__.py
@@ -0,0 +1,4 @@
+from . import om_connector_job
+from . import om_connector_backend_mixin
+from . import om_connector_binding_mixin
+from . import om_connector_order_workflow
diff --git a/om_connector_base/models/om_connector_backend_mixin.py b/om_connector_base/models/om_connector_backend_mixin.py
new file mode 100644
index 00000000..525335c7
--- /dev/null
+++ b/om_connector_base/models/om_connector_backend_mixin.py
@@ -0,0 +1,55 @@
+from odoo import _, api, fields, models
+
+
+class OmConnectorBackendMixin(models.AbstractModel):
+ """ A connection to an external system: a Shopify store, a WooCommerce site, a marketplace
+ account… Its jobs are queued with ``_connector_enqueue`` and shown from its form. """
+ _name = 'om.connector.backend.mixin'
+ _description = 'Connector Connection'
+
+ connector_job_todo_count = fields.Integer(string='Jobs To Do', compute='_compute_connector_job_counts')
+ connector_job_failed_count = fields.Integer(string='Failed Jobs', compute='_compute_connector_job_counts')
+
+ def _compute_connector_job_counts(self):
+ counts = {
+ (backend_id, state): count
+ for backend_id, state, count in self.env['om.connector.job'].sudo()._read_group(
+ [('backend_model', '=', self._name), ('backend_id', 'in', self.ids),
+ ('state', 'in', ('pending', 'failed'))],
+ ['backend_id', 'state'], ['__count'])
+ }
+ for backend in self:
+ backend.connector_job_todo_count = counts.get((backend.id, 'pending'), 0)
+ backend.connector_job_failed_count = counts.get((backend.id, 'failed'), 0)
+
+ def _connector_enqueue(self, method_name, payload=None, **kwargs):
+ """ Queue ``method_name`` (on this model, or on ``model_name``) for this connection.
+ See ``om.connector.job._enqueue`` for the options. """
+ self.ensure_one()
+ return self.env['om.connector.job']._enqueue(self, method_name, payload, **kwargs)
+
+ def _connector_enqueue_once(self, method_name, payload=None, **kwargs):
+ """ Queue ``method_name`` unless a job of this method is already to do for this connection:
+ for the scheduled work (fetch the new orders…), which must not pile up. """
+ self.ensure_one()
+ pending = self.env['om.connector.job'].sudo().search(
+ self._connector_job_domain() + [('method_name', '=', method_name), ('state', '=', 'pending')], limit=1)
+ return pending or self._connector_enqueue(method_name, payload, **kwargs)
+
+ def _connector_job_failed(self, job):
+ """ Called when ``job`` of this connection fails for good. """
+
+ def _connector_job_domain(self):
+ return [('backend_model', '=', self._name), ('backend_id', 'in', self.ids)]
+
+ def action_open_connector_jobs(self):
+ action = self.env['ir.actions.act_window']._for_xml_id('om_connector_base.om_connector_job_action')
+ action['domain'] = self._connector_job_domain()
+ action['context'] = {'search_default_filter_todo_failed': 1}
+ if len(self) == 1:
+ action['display_name'] = _("Jobs of %(connection)s", connection=self.display_name)
+ return action
+
+ def unlink(self):
+ self.env['om.connector.job'].sudo().search(self._connector_job_domain()).unlink()
+ return super().unlink()
diff --git a/om_connector_base/models/om_connector_binding_mixin.py b/om_connector_base/models/om_connector_binding_mixin.py
new file mode 100644
index 00000000..c6ec6f6e
--- /dev/null
+++ b/om_connector_base/models/om_connector_binding_mixin.py
@@ -0,0 +1,36 @@
+from odoo import api, fields, models
+
+
+class OmConnectorBindingMixin(models.AbstractModel):
+ """ The link between a record of an external system and an Odoo record, for one connection:
+ a Shopify product and an Odoo product, a Shopify order and a sale order…
+
+ A model that inherits it has a Many2one to its connection, named by ``_connector_backend_field``,
+ and a Many2one to the Odoo record, named by ``_connector_record_field``. An external ID is linked
+ once per connection.
+ """
+ _name = 'om.connector.binding.mixin'
+ _description = 'Connector Mapping'
+ _connector_backend_field = 'backend_id'
+ _connector_record_field = 'record_id'
+
+ external_id = fields.Char(string='External ID', required=True, index=True, readonly=True)
+ sync_date = fields.Datetime(string='Last Sync', readonly=True)
+
+ @api.model
+ def _connector_find(self, backend, external_ids):
+ """ The bindings of ``backend`` for one external ID or a list of them. """
+ if isinstance(external_ids, (str, int)):
+ external_ids = [external_ids]
+ return self.search([(self._connector_backend_field, '=', backend.id),
+ ('external_id', 'in', [str(x) for x in external_ids])])
+
+ @api.model
+ def _connector_bind(self, backend, external_id, record, vals=None):
+ """ Link ``external_id`` to ``record`` for ``backend``, creating or updating the binding. """
+ binding = self._connector_find(backend, external_id)
+ values = {self._connector_record_field: record.id, 'sync_date': fields.Datetime.now(), **(vals or {})}
+ if binding:
+ binding.write(values)
+ return binding
+ return self.create({self._connector_backend_field: backend.id, 'external_id': str(external_id), **values})
diff --git a/om_connector_base/models/om_connector_job.py b/om_connector_base/models/om_connector_job.py
new file mode 100644
index 00000000..700efbd0
--- /dev/null
+++ b/om_connector_base/models/om_connector_job.py
@@ -0,0 +1,285 @@
+import json
+import logging
+import traceback
+from datetime import timedelta
+
+import requests
+
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError, ValidationError
+from odoo.osv import expression
+from odoo.tools import SQL, create_index
+
+from ..tools.exceptions import RetryableJobError
+
+_logger = logging.getLogger(__name__)
+
+JOB_METHOD_PREFIX = '_connector_job_'
+DEFAULT_KEEP_DAYS = 30
+
+
+class OmConnectorJob(models.Model):
+ """ A piece of work for a connector: import an order, send a stock level, process a webhook…
+
+ A job calls ``model_name.method_name(job)``. The method reads ``job.payload`` and
+ ``job.backend``, and may return a record (linked to the job) or a short text (kept as the
+ result). A job that fails is tried again later, a few minutes after each failure, up to
+ ``max_attempts`` times. An error of the user (``UserError``, ``ValidationError``) fails the job
+ at once: trying again would give the same error.
+ """
+ _name = 'om.connector.job'
+ _description = 'Connector Job'
+ _order = 'id desc'
+
+ name = fields.Char(required=True)
+ backend_model = fields.Char(string='Backend Model', required=True, index=True, readonly=True)
+ backend_id = fields.Many2oneReference(string='Backend ID', model_field='backend_model', required=True,
+ index=True, readonly=True)
+ backend_name = fields.Char(string='Connection', compute='_compute_backend_name')
+ company_id = fields.Many2one('res.company', required=True, index=True, readonly=True,
+ default=lambda self: self.env.company)
+ model_name = fields.Char(string='Model', required=True, readonly=True)
+ method_name = fields.Char(string='Method', required=True, readonly=True)
+ payload = fields.Json(readonly=True)
+ payload_text = fields.Text(string='Data', compute='_compute_payload_text')
+ dedupe_key = fields.Char(string='Key', readonly=True, index=True,
+ help="Two jobs of a connection never have the same key: a webhook sent twice, "
+ "or the same order queued twice, makes one job.")
+ state = fields.Selection([
+ ('pending', 'To Do'),
+ ('done', 'Done'),
+ ('failed', 'Failed'),
+ ('cancelled', 'Cancelled'),
+ ], default='pending', required=True, index=True, readonly=True)
+ priority = fields.Integer(default=10, help="Lower runs first.")
+ eta = fields.Datetime(string='Run After', index=True, readonly=True)
+ attempts = fields.Integer(readonly=True)
+ max_attempts = fields.Integer(default=5)
+ date_done = fields.Datetime(string='Done On', readonly=True)
+ error = fields.Text(readonly=True)
+ result = fields.Char(readonly=True)
+ record_model = fields.Char(readonly=True)
+ record_id = fields.Many2oneReference(string='Record ID', model_field='record_model', readonly=True)
+ record_name = fields.Char(string='Record', compute='_compute_record_name')
+
+ _sql_constraints = [
+ ('dedupe_key_unique', 'unique (backend_model, backend_id, dedupe_key)', "A connection has one job per key."),
+ ]
+
+ def init(self):
+ super().init()
+ create_index(self.env.cr, 'om_connector_job__todo_index', self._table, ['priority', 'id'],
+ where="state = 'pending'")
+
+ @api.depends('backend_model', 'backend_id')
+ def _compute_backend_name(self):
+ for job in self:
+ backend = job.backend
+ job.backend_name = backend.sudo().display_name if backend else False
+
+ @api.depends('record_model', 'record_id')
+ def _compute_record_name(self):
+ for job in self:
+ record = job._get_record()
+ job.record_name = record.sudo().display_name if record else False
+
+ @api.depends('payload')
+ def _compute_payload_text(self):
+ for job in self:
+ job.payload_text = json.dumps(job.payload, indent=2, ensure_ascii=False, default=str) \
+ if job.payload is not None else False
+
+ @property
+ def backend(self):
+ self.ensure_one()
+ if not self.backend_model or self.backend_model not in self.env:
+ return None
+ return self.env[self.backend_model].browse(self.backend_id).exists()
+
+ def _get_record(self):
+ self.ensure_one()
+ if not self.record_model or self.record_model not in self.env or not self.record_id:
+ return None
+ return self.env[self.record_model].browse(self.record_id).exists()
+
+ @api.constrains('method_name')
+ def _check_method_name(self):
+ for job in self:
+ if not job.method_name.startswith(JOB_METHOD_PREFIX):
+ raise ValidationError(_("The method of a job starts with %(prefix)s.", prefix=JOB_METHOD_PREFIX))
+
+ # ------------------------------------------------------------------
+ # Queue
+ # ------------------------------------------------------------------
+
+ @api.model
+ def _enqueue(self, backend, method_name, payload=None, *, model_name=None, name=None, dedupe_key=None,
+ priority=10, eta=None, max_attempts=5, company=None):
+ """ Queue a job for ``backend``, or give the job of the same ``dedupe_key``.
+
+ A pending job of the same key gets the new payload: the newest data of a record wins.
+ A done, failed or cancelled job of the same key is put back to do with the new payload,
+ unless ``payload`` is the same.
+ """
+ backend.ensure_one()
+ Job = self.sudo()
+ if dedupe_key:
+ job = Job.search([('backend_model', '=', backend._name), ('backend_id', '=', backend.id),
+ ('dedupe_key', '=', dedupe_key)], limit=1)
+ if job:
+ if job.state == 'pending':
+ job.write({'payload': payload, 'priority': min(job.priority, priority)})
+ elif job.payload != payload:
+ job.write({'payload': payload, 'state': 'pending', 'attempts': 0, 'error': False,
+ 'eta': eta, 'date_done': False})
+ self._trigger_runner(eta)
+ return job
+ if company is None:
+ company = backend.company_id if 'company_id' in backend._fields and backend.company_id \
+ else self.env.company
+ job = Job.create({
+ 'name': name or method_name.removeprefix(JOB_METHOD_PREFIX).replace('_', ' ').capitalize(),
+ 'backend_model': backend._name,
+ 'backend_id': backend.id,
+ 'company_id': company.id,
+ 'model_name': model_name or backend._name,
+ 'method_name': method_name,
+ 'payload': payload,
+ 'dedupe_key': dedupe_key,
+ 'priority': priority,
+ 'eta': eta,
+ 'max_attempts': max_attempts,
+ })
+ self._trigger_runner(eta)
+ return job
+
+ @api.model
+ def _trigger_runner(self, eta=None):
+ cron = self.env.ref('om_connector_base.ir_cron_om_connector_run_jobs', raise_if_not_found=False)
+ if cron:
+ cron.sudo()._trigger(eta)
+
+ @api.model
+ def _lock_next(self, limit, domain=None):
+ """ The next jobs to run, locked for this transaction; jobs locked by another worker are
+ skipped, so that two workers never run the same job. """
+ domain = expression.AND([
+ [('state', '=', 'pending')],
+ expression.OR([[('eta', '=', False)], [('eta', '<=', fields.Datetime.now())]]),
+ domain or [],
+ ])
+ query = self._search(domain, order='priority, id', limit=limit)
+ self.env.cr.execute(SQL("%s FOR UPDATE SKIP LOCKED", query.select(SQL.identifier(self._table, 'id'))))
+ return self.browse([row[0] for row in self.env.cr.fetchall()])
+
+ @api.model
+ def _cron_run_jobs(self, batch=20):
+ """ Run the jobs to do, committing after each of them, until the time of the cron is over. """
+ Cron = self.env['ir.cron']
+ while True:
+ jobs = self.sudo()._lock_next(batch)
+ if not jobs:
+ break
+ for job in jobs:
+ job._run()
+ remaining = self.sudo().search_count([('state', '=', 'pending')])
+ if not Cron._commit_progress(1, remaining=remaining):
+ return
+
+ def _run(self):
+ """ Run the job, in a savepoint: a failed job leaves no change but its own error. """
+ self.ensure_one()
+ if self.state != 'pending':
+ return
+ backend = self.backend
+ if backend is None:
+ self.write({'state': 'cancelled', 'error': _("The connection of this job has been deleted.")})
+ return
+ if self.model_name not in self.env or not self.method_name.startswith(JOB_METHOD_PREFIX):
+ self.write({'state': 'failed', 'error': _("Unknown method %(model)s.%(method)s",
+ model=self.model_name, method=self.method_name)})
+ return
+ target = self.env[self.model_name].sudo().with_company(self.company_id)
+ method = getattr(target, self.method_name, None)
+ if method is None:
+ self.write({'state': 'failed', 'error': _("Unknown method %(model)s.%(method)s",
+ model=self.model_name, method=self.method_name)})
+ return
+ attempts = self.attempts + 1
+ try:
+ with self.env.cr.savepoint():
+ result = method(self.with_company(self.company_id))
+ except RetryableJobError as e:
+ self._failed(attempts, str(e), retry=True, delay=e.delay)
+ except (UserError, ValidationError) as e:
+ self._failed(attempts, str(e), retry=False)
+ except (requests.ConnectionError, requests.Timeout) as e:
+ self._failed(attempts, _("Network error: %(error)s", error=e), retry=True)
+ except Exception:
+ _logger.exception("Connector job %s (%s) failed", self.id, self.name)
+ self._failed(attempts, traceback.format_exc(limit=8), retry=True)
+ else:
+ vals = {'state': 'done', 'attempts': attempts, 'date_done': fields.Datetime.now(), 'error': False}
+ if isinstance(result, models.BaseModel) and len(result) == 1:
+ vals.update(record_model=result._name, record_id=result.id)
+ elif isinstance(result, str):
+ vals['result'] = result[:250]
+ self.write(vals)
+
+ def _failed(self, attempts, error, retry, delay=None):
+ vals = {'attempts': attempts, 'error': error}
+ if retry and attempts < self.max_attempts:
+ # 2, 4, 8, 16… minutes, at most 2 hours
+ delay = delay if delay is not None else min(60 * 2 ** attempts, 7200)
+ vals['eta'] = fields.Datetime.now() + timedelta(seconds=delay)
+ self.write(vals)
+ self._trigger_runner(vals['eta'])
+ else:
+ vals['state'] = 'failed'
+ self.write(vals)
+ backend = self.backend
+ if backend is not None and hasattr(backend, '_connector_job_failed'):
+ try:
+ with self.env.cr.savepoint():
+ backend._connector_job_failed(self)
+ except Exception:
+ _logger.exception("Connector job %s: the failure could not be notified", self.id)
+
+ # ------------------------------------------------------------------
+ # Actions
+ # ------------------------------------------------------------------
+
+ def action_retry(self):
+ jobs = self.filtered(lambda j: j.state in ('failed', 'cancelled'))
+ jobs.write({'state': 'pending', 'attempts': 0, 'eta': False, 'error': False})
+ if jobs:
+ self._trigger_runner()
+
+ def action_cancel(self):
+ self.filtered(lambda j: j.state in ('pending', 'failed')).write({'state': 'cancelled'})
+
+ def action_run_now(self):
+ for job in self.filtered(lambda j: j.state in ('pending', 'failed')):
+ if job.state == 'failed':
+ job.write({'state': 'pending', 'attempts': 0, 'error': False})
+ job.eta = False
+ job._run()
+
+ def action_open_record(self):
+ self.ensure_one()
+ record = self._get_record()
+ if not record:
+ raise UserError(_("The record of this job does not exist anymore."))
+ return {
+ 'type': 'ir.actions.act_window',
+ 'res_model': record._name,
+ 'res_id': record.id,
+ 'view_mode': 'form',
+ }
+
+ @api.autovacuum
+ def _gc_old_jobs(self):
+ days = int(self.env['ir.config_parameter'].sudo().get_param('om_connector_base.job_keep_days') or 0) or DEFAULT_KEEP_DAYS
+ limit_date = fields.Datetime.now() - timedelta(days=days)
+ self.sudo().search([('state', 'in', ('done', 'cancelled')), ('write_date', '<', limit_date)],
+ limit=10000).unlink()
diff --git a/om_connector_base/models/om_connector_order_workflow.py b/om_connector_base/models/om_connector_order_workflow.py
new file mode 100644
index 00000000..9d8cd675
--- /dev/null
+++ b/om_connector_base/models/om_connector_order_workflow.py
@@ -0,0 +1,112 @@
+import logging
+
+from odoo import _, api, fields, models
+from odoo.exceptions import ValidationError
+
+_logger = logging.getLogger(__name__)
+
+
+class OmConnectorOrderWorkflow(models.Model):
+ """ What Odoo does with an order imported from a shop or a marketplace: confirm it, invoice
+ it, register its payment. A connector picks the workflow of an order, for example from its
+ payment method. """
+ _name = 'om.connector.order.workflow'
+ _description = 'Imported Order Workflow'
+ _order = 'sequence, id'
+ _check_company_auto = True
+
+ name = fields.Char(required=True, translate=True)
+ sequence = fields.Integer(default=10)
+ active = fields.Boolean(default=True)
+ company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company)
+ confirm_order = fields.Boolean(string='Confirm the Order', default=True)
+ create_invoice = fields.Boolean(
+ string='Create the Invoice', default=True,
+ help="Invoices what can be invoiced: the products invoiced on ordered quantities. Products invoiced on "
+ "delivered quantities are invoiced once delivered, as usual.")
+ post_invoice = fields.Boolean(string='Post the Invoice', default=True)
+ register_payment = fields.Boolean(
+ string='Register the Payment', default=True,
+ help="When the shop says the order is paid, registers its payment in the journal below.")
+ journal_id = fields.Many2one(
+ 'account.journal', string='Payment Journal', check_company=True,
+ domain="[('type', 'in', ('bank', 'cash', 'credit'))]")
+ invoice_date_is_order_date = fields.Boolean(
+ string='Invoice on the Order Date',
+ help="Dates the invoice and the payment on the date of the order, instead of today.")
+
+ @api.constrains('register_payment', 'journal_id', 'post_invoice')
+ def _check_journal(self):
+ for workflow in self:
+ if workflow.register_payment and (not workflow.journal_id or not workflow.post_invoice):
+ raise ValidationError(_(
+ "To register the payments, %(workflow)s needs a payment journal and to post the invoices.",
+ workflow=workflow.name))
+
+ def _connector_process(self, order, paid=False):
+ """ Run the workflow on a sale order. ``paid``: the shop says the order is paid.
+ Gives the posted or draft invoices made. """
+ self.ensure_one()
+ order.ensure_one()
+ invoices = self.env['account.move']
+ if self.confirm_order and order.state in ('draft', 'sent'):
+ order.action_confirm()
+ if order.state != 'sale' or not self.create_invoice:
+ return invoices
+ if order.invoice_status == 'to invoice':
+ invoices = order._create_invoices()
+ if self.invoice_date_is_order_date:
+ invoices.write({'invoice_date': fields.Date.context_today(self, order.date_order)})
+ if self.post_invoice:
+ invoices.action_post()
+ if paid and self.register_payment:
+ self._connector_register_payment(order)
+ return invoices
+
+ def _connector_register_payment(self, order, amount=None, journal=None):
+ """ Pay the open posted invoices of ``order``: all that is due, or ``amount``, in the payment
+ journal of the workflow or in ``journal``. """
+ self.ensure_one()
+ invoices = order.invoice_ids.filtered(
+ lambda m: m.state == 'posted' and m.move_type == 'out_invoice'
+ and m.payment_state != 'reversed' and not m.currency_id.is_zero(m.amount_residual))
+ journal = journal or self.journal_id
+ if not invoices or not journal:
+ return self.env['account.payment']
+ if amount is not None and order.currency_id.compare_amounts(amount, 0) <= 0:
+ return self.env['account.payment']
+ pending = self._connector_pending_payments(invoices)
+ due = sum(invoices.mapped('amount_residual')) - pending
+ if invoices[0].currency_id.compare_amounts(due, 0) <= 0:
+ return self.env['account.payment']
+ date = fields.Date.context_today(self, order.date_order) if self.invoice_date_is_order_date \
+ else fields.Date.context_today(self)
+ vals = {'journal_id': journal.id, 'payment_date': date, 'group_payment': True}
+ if amount is not None or pending:
+ vals['amount'] = due if amount is None else min(amount, due)
+ vals['payment_difference_handling'] = 'open'
+ wizard = self.env['account.payment.register'].with_context(
+ active_model='account.move', active_ids=invoices.ids).create(vals)
+ return wizard._create_payments()
+
+ @api.model
+ def _connector_pending_payments(self, invoices):
+ """ Amount of the payments matched with ``invoices`` that have no journal entry (journals
+ without an outstanding account): they do not lower the residual of the invoices. """
+ payments = invoices.matched_payment_ids.filtered(
+ lambda p: not p.move_id and p.state not in ('draft', 'canceled', 'rejected'))
+ return sum(payments.mapped('amount'))
+
+ def _connector_refund_payment(self, credit_note, amount=None, journal=None):
+ """ Pay back a posted credit note: all of it, or ``amount``. """
+ self.ensure_one()
+ journal = journal or self.journal_id
+ if credit_note.state != 'posted' or credit_note.payment_state not in ('not_paid', 'partial') or not journal:
+ return self.env['account.payment']
+ vals = {'journal_id': journal.id, 'payment_date': fields.Date.context_today(self)}
+ if amount is not None:
+ vals['amount'] = min(amount, credit_note.amount_residual)
+ vals['payment_difference_handling'] = 'open'
+ wizard = self.env['account.payment.register'].with_context(
+ active_model='account.move', active_ids=credit_note.ids).create(vals)
+ return wizard._create_payments()
diff --git a/om_connector_base/security/ir.model.access.csv b/om_connector_base/security/ir.model.access.csv
new file mode 100644
index 00000000..93acef93
--- /dev/null
+++ b/om_connector_base/security/ir.model.access.csv
@@ -0,0 +1,5 @@
+id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
+access_om_connector_job_user,access.om.connector.job.user,model_om_connector_job,group_om_connector_user,1,1,0,0
+access_om_connector_job_manager,access.om.connector.job.manager,model_om_connector_job,group_om_connector_manager,1,1,1,1
+access_om_connector_order_workflow_user,access.om.connector.order.workflow.user,model_om_connector_order_workflow,group_om_connector_user,1,0,0,0
+access_om_connector_order_workflow_manager,access.om.connector.order.workflow.manager,model_om_connector_order_workflow,group_om_connector_manager,1,1,1,1
diff --git a/om_connector_base/security/ir_rule.xml b/om_connector_base/security/ir_rule.xml
new file mode 100644
index 00000000..3bba44ae
--- /dev/null
+++ b/om_connector_base/security/ir_rule.xml
@@ -0,0 +1,14 @@
+
+
+
+ om.connector.job: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.connector.order.workflow: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
diff --git a/om_connector_base/security/om_connector_groups.xml b/om_connector_base/security/om_connector_groups.xml
new file mode 100644
index 00000000..e5f2442f
--- /dev/null
+++ b/om_connector_base/security/om_connector_groups.xml
@@ -0,0 +1,23 @@
+
+
+
+ Connectors
+
+ 50
+
+
+
+ User
+
+
+ Sees the connections and their jobs, and tries failed jobs again.
+
+
+
+ Administrator
+
+
+
+ Sets up the connections and their credentials.
+
+
diff --git a/om_connector_base/static/description/banner.gif b/om_connector_base/static/description/banner.gif
new file mode 100644
index 00000000..197446b6
Binary files /dev/null and b/om_connector_base/static/description/banner.gif differ
diff --git a/om_connector_base/static/description/icon.png b/om_connector_base/static/description/icon.png
new file mode 100644
index 00000000..af399185
Binary files /dev/null and b/om_connector_base/static/description/icon.png differ
diff --git a/om_connector_base/static/description/index.html b/om_connector_base/static/description/index.html
new file mode 100644
index 00000000..999c1258
--- /dev/null
+++ b/om_connector_base/static/description/index.html
@@ -0,0 +1,262 @@
+
+
+
Odoo Mates · free and open source
+
Connector Base
+
The common engine of the Odoo Mates connectors to shops and marketplaces: a job queue that never loses work, links between external and Odoo records, and workflows for imported orders.
Installed by the connectors that need it; nothing to set up.
+
+
+
BetaThis app is in beta: tell us what you think
+
Connector Base is new, together with the Shopify Connector that uses it. We welcome your feedback and suggestions, and if something does not look right, report the issue and let us know: every message is read and helps decide what comes next.
Every import, export and webhook is a job; two workers never on one job.
+
+
+
+
+
+
+
+
+
+
+
Retries
+
After 2, 4, 8 minutes and more; nothing left half done.
+
+
+
+
+
+
+
+
+
+
+
No Duplicates
+
Jobs with the same key run once, with the newest data.
+
+
+
+
+
+
+
+
+
+
+
Mappings
+
An external ID linked to an Odoo record, once per connection.
+
+
+
+
+
+
+
+
+
+
+
Order Workflows
+
Confirm, invoice, register one or several payments.
+
+
+
+
+
+
+
+
+
+
+
Secrets Encrypted
+
Tokens and keys stored encrypted in the database.
+
+
+
+
+
+
+
+
+
+
+
Webhook Signatures
+
HMAC-SHA256 checked in constant time.
+
+
+
+
+
+
+
+
+
+
+
Patient HTTP Calls
+
Wait and try again on rate limits and server errors.
+
+
+
+
+
+
Features
+
A closer look
+
+
+
+
+
Jobs
+
See every job
+
+
+✓ What is to do, done and failed, per connection.
+
+✓ The data of the job, its error, its attempts and the record it made.
+
+✓ Run it now, try it again or cancel it; done jobs removed after 30 days.
+
+
+
+
+
Workflows
+
What Odoo does with an imported order
+
+
+✓ Confirm it, invoice it, post the invoice.
+
+✓ Register the payment in a journal, on the date of the order or of today.
+
+✓ Each connector picks the workflow, e.g. from the payment method.
+
+
+
+
+
Failures
+
Failed jobs reported to their connection
+
+
+✓ Errors of the user fail at once, with their message.
+
+✓ Other errors are tried again later, then reported with an activity.
+
+✓ Failed jobs tried again in one click.
+
+
+
+
+
+
Used by
+
The connectors built on it
+
Install a connector: Connector Base comes with it.
+
+
+
+
+
Shopify Connector
+
om_shopify · free
+
Products, stock, orders, refunds and payouts between Shopify and Odoo.
+
+
+
+
+
Connector Base
+
om_connector_base · this module
+
The job queue, the mappings and the order workflows shared by the connectors.
+
+
+
+
+
+
Install
+
Get started
+
+
+
+
+
How to install it
+
+1Copy the module to a folder of your own modules, e.g. /opt/odoo/custom-addons, and add that folder to addons_path in odoo.conf.
+
+2Restart Odoo.
+
+3Activate the developer mode, then Apps → Update Apps List.
+
+4Clear the Apps filter, search om_connector_base and install it.
+
+Do not use Apps → Import Module. It is meant for data modules without Python, and leaves this module half loaded.
+
+
+
+
+
Free, for good
+
Connector Base is free and open source under LGPL-3, for Odoo 18. Build your own connector on it.
+
+
+
+
+
Questions we get
+
+
+
+
+
How does a connector queue work?
+
Its connection inherits om.connector.backend.mixin and calls self._connector_enqueue(method, payload, dedupe_key=...). The method gets the job, reads job.payload and returns the record it made.
+
+
+
+
+
How are external records linked?
+
A mapping model inherits om.connector.binding.mixin and uses _connector_find() and _connector_bind().
+
+
+
+
+
How do I retry or fail a job?
+
Raise RetryableJobError(message, delay=60) to try again later, a UserError to fail at once. Orders are processed with workflow._connector_process(order, paid=True).
+
+
+
+
+
Do I need Odoo Enterprise?
+
No. It runs on the Community Edition, and installs on Enterprise as well.
+
+
+
+
+
How is the app working for you?
+
Your rating helps others find it; your requests and pull requests decide what comes next.
A workflow says what Odoo does with an imported order: confirm it, invoice it and register its payment.
+
+
+
diff --git a/om_shopify/README.rst b/om_shopify/README.rst
new file mode 100644
index 00000000..679cee91
--- /dev/null
+++ b/om_shopify/README.rst
@@ -0,0 +1,41 @@
+=================
+Shopify Connector
+=================
+
+Connects Shopify stores to Odoo 18 through the Shopify Admin GraphQL API: products, stock, customers,
+orders, fulfillment, refunds and payouts, by webhooks and scheduled imports, every sync being a job that can
+be followed and tried again (Connector Base).
+
+Features
+========
+
+* **Connection**: client ID and secret of a Dev Dashboard app (token renewed daily), installation by link
+ (OAuth, with expiring tokens refreshed by themselves), or the access token of a custom app. Secrets are
+ stored encrypted. A setup wizard: Shopify > Configuration > Connect a Store.
+* **Webhooks**: checked with the signature of Shopify and the store, queued and answered at once, a webhook
+ sent twice run once; the privacy (GDPR) webhooks are handled.
+* **Products**: import (bulk operation for the whole catalogue, then changes daily and by webhook), linked by
+ SKU or barcode or created with their variants and images; export from Odoo with variants, prices, weight,
+ image and publication; a CSV or Excel file to link catalogues whose codes do not match; metafields mapped
+ to Odoo fields.
+* **Prices**: sent from a pricelist, with compare-at prices, and to Shopify price lists (markets, B2B).
+* **Stock**: sent per Shopify location from the warehouse of the location, with limits per variant; or
+ imported from Shopify.
+* **Customers and B2B**: customers with their addresses; B2B companies, locations, payment terms and tax
+ exemption (Shopify Plus).
+* **Orders**: imported by webhook, every 15 minutes, or by number; discounts, shipping, taxes of Shopify or of
+ Odoo, tips, duties, gift cards, custom items, currency of the customer; confirmed, invoiced and paid by a
+ workflow chosen by order rules (payment method, payment and fulfillment status, tag, channel); risky orders
+ held; edits applied.
+* **After the sale**: fulfillment with tracking number when the delivery is done; cancellations both ways;
+ Shopify refunds as paid credit notes and returns; credit notes refunded in Shopify.
+* **Payouts** (optional): bank statement lines, or one journal entry per payout through a transit account,
+ matched with the payments and the bank transaction.
+
+Setup
+=====
+
+1. Create an app in the Shopify Dev Dashboard with the Admin API scopes listed on the store, and install it.
+2. Shopify > Configuration > Connect a Store: the address of the store and the credentials of the app.
+3. For webhooks, the public https address of Odoo (Odoo Public URL), also set in the app as the compliance
+ (GDPR) address; for the installation by link, the App URL and the Redirection URL shown on the store.
diff --git a/om_shopify/__init__.py b/om_shopify/__init__.py
new file mode 100644
index 00000000..dc98bbff
--- /dev/null
+++ b/om_shopify/__init__.py
@@ -0,0 +1,4 @@
+from . import controllers
+from . import models
+from . import tools
+from . import wizard
diff --git a/om_shopify/__manifest__.py b/om_shopify/__manifest__.py
new file mode 100644
index 00000000..9ddd2168
--- /dev/null
+++ b/om_shopify/__manifest__.py
@@ -0,0 +1,34 @@
+{
+ 'name': 'Shopify Connector',
+ 'version': '1.0',
+ 'author': 'Odoo Mates',
+ 'category': 'Sales',
+ 'summary': 'Shopify, Shopify Connector, Shopify Integration, Shopify Odoo, Shopify Store, Shopify Webhooks',
+ 'description': 'Connect Shopify stores to Odoo: products both ways, prices, stock both ways, customers, orders '
+ 'with automatic invoicing and payment, fulfillment with tracking numbers, refunds, cancellations '
+ 'and payouts, through webhooks and a queue.',
+ 'maintainer': 'Odoo Mates',
+ 'support': 'odoomates@gmail.com',
+ 'license': 'LGPL-3',
+ 'depends': ['om_connector_base', 'sale_management', 'stock_delivery'],
+ 'data': [
+ 'security/om_shopify_groups.xml',
+ 'security/ir.model.access.csv',
+ 'security/ir_rule.xml',
+ 'data/product_data.xml',
+ 'data/ir_cron.xml',
+ 'views/om_shopify_store_views.xml',
+ 'views/om_shopify_product_views.xml',
+ 'views/om_shopify_location_views.xml',
+ 'views/sale_order_views.xml',
+ 'views/stock_picking_views.xml',
+ 'views/account_move_views.xml',
+ 'views/om_shopify_payout_views.xml',
+ 'views/digest_views.xml',
+ 'wizard/om_shopify_wizard_views.xml',
+ 'views/om_shopify_webhook_views.xml',
+ 'views/menu.xml',
+ ],
+ 'application': True,
+ 'images': ['static/description/banner.gif'],
+}
diff --git a/om_shopify/controllers/__init__.py b/om_shopify/controllers/__init__.py
new file mode 100644
index 00000000..453f1bcb
--- /dev/null
+++ b/om_shopify/controllers/__init__.py
@@ -0,0 +1,2 @@
+from . import webhook
+from . import oauth
diff --git a/om_shopify/controllers/oauth.py b/om_shopify/controllers/oauth.py
new file mode 100644
index 00000000..9c16483e
--- /dev/null
+++ b/om_shopify/controllers/oauth.py
@@ -0,0 +1,63 @@
+import logging
+
+from markupsafe import escape
+from werkzeug.utils import redirect
+
+from odoo import _, http
+from odoo.exceptions import UserError
+from odoo.http import request
+
+from ..tools.client import ShopifyClient, is_valid_shop_domain, normalize_shop_domain
+
+_logger = logging.getLogger(__name__)
+
+
+class OmShopifyOAuth(http.Controller):
+ """ Installation of the app on a store by the merchant (OAuth authorization code grant). """
+
+ def _error(self, message, status=400):
+ body = f'
'
+ return request.make_response(body, headers=[('Content-Type', 'text/html; charset=utf-8')], status=status)
+
+ @http.route('/om_shopify/oauth/start/', type='http', auth='user', methods=['GET'])
+ def start(self, store_id, **kwargs):
+ store = request.env['om.shopify.store'].browse(store_id).exists()
+ if not store or not request.env.user.has_group('om_shopify.group_om_shopify_manager'):
+ return self._error(_("You are not allowed to connect this store."), status=403)
+ store.check_access('write')
+ try:
+ url = store._shopify_oauth_authorize_url()
+ except UserError as e:
+ return self._error(str(e))
+ return redirect(url, code=303, Response=None)
+
+ @http.route('/om_shopify/oauth/install', type='http', auth='user', methods=['GET'])
+ def install(self, **params):
+ """ The App URL: Shopify sends the merchant here when the app is installed from Shopify. """
+ shop = normalize_shop_domain(params.get('shop'))
+ if not is_valid_shop_domain(shop):
+ return self._error(_("Unknown store."), status=404)
+ store = request.env['om.shopify.store'].search([('shop_domain', '=', shop), ('auth_type', '=', 'oauth')],
+ limit=1)
+ if not store or not ShopifyClient.verify_oauth_query(store.sudo().client_secret, params):
+ return self._error(_("No store of Odoo installs the app on %(shop)s.", shop=shop), status=404)
+ if store.sudo().access_token and store.state == 'connected':
+ return redirect(f'/odoo/action-om_shopify.om_shopify_store_action/{store.id}', code=303, Response=None)
+ return self.start(store.id)
+
+ @http.route('/om_shopify/oauth/callback', type='http', auth='user', methods=['GET'])
+ def callback(self, **params):
+ store = request.env['om.shopify.store']._shopify_oauth_store_of_state(params.get('state'))
+ if not store:
+ return self._error(_("This installation is unknown or already done: start it again from the store."),
+ status=403)
+ if params.get('error'):
+ return self._error(_("Shopify refused the installation: %(error)s", error=params.get('error_description')
+ or params['error']), status=403)
+ try:
+ store.with_user(request.env.user)._shopify_oauth_complete(params)
+ except UserError as e:
+ _logger.warning("Shopify OAuth callback for store %s refused: %s", store.id, e)
+ return self._error(str(e), status=403)
+ return redirect(f'/odoo/action-om_shopify.om_shopify_store_action/{store.id}', code=303, Response=None)
diff --git a/om_shopify/controllers/webhook.py b/om_shopify/controllers/webhook.py
new file mode 100644
index 00000000..2591d6a7
--- /dev/null
+++ b/om_shopify/controllers/webhook.py
@@ -0,0 +1,40 @@
+import json
+import logging
+
+from odoo import http
+from odoo.http import request
+
+from odoo.addons.om_connector_base.tools import verify_hmac_sha256
+
+from ..tools.client import topic_to_enum
+
+_logger = logging.getLogger(__name__)
+
+
+class OmShopifyWebhook(http.Controller):
+ """ Webhooks of the Shopify stores. A webhook is checked with the signature of Shopify, queued
+ as a job, and answered at once: Shopify waits 5 seconds at most, then sends it again. """
+
+ @http.route('/om_shopify/webhook/', type='http', auth='public', methods=['POST'],
+ csrf=False, save_session=False)
+ def receive(self, store_id, **kwargs):
+ headers = request.httprequest.headers
+ body = request.httprequest.get_data()
+ store = request.env['om.shopify.store'].sudo().browse(store_id).exists()
+ if not store or not store.active:
+ return request.make_response('Unknown store', status=404)
+ if not verify_hmac_sha256(store.client_secret, body, headers.get('X-Shopify-Hmac-Sha256')):
+ _logger.warning("Shopify webhook for store %s: wrong signature, ignored", store_id)
+ return request.make_response('Wrong signature', status=401)
+ shop_domain = (headers.get('X-Shopify-Shop-Domain') or '').lower()
+ if shop_domain and shop_domain != store.shop_domain:
+ _logger.warning("Shopify webhook for store %s sent by %s: ignored", store_id, shop_domain)
+ return request.make_response('Wrong store', status=401)
+ try:
+ payload = json.loads(body or b'{}')
+ except ValueError:
+ return request.make_response('Bad Request', status=400)
+ topic = topic_to_enum(headers.get('X-Shopify-Topic'))
+ webhook_id = headers.get('X-Shopify-Webhook-Id') or headers.get('X-Shopify-Event-Id')
+ store._shopify_receive_webhook(topic, payload, webhook_id)
+ return request.make_response('OK')
diff --git a/om_shopify/data/ir_cron.xml b/om_shopify/data/ir_cron.xml
new file mode 100644
index 00000000..5d4753d3
--- /dev/null
+++ b/om_shopify/data/ir_cron.xml
@@ -0,0 +1,35 @@
+
+
+
+ Shopify: import the new orders
+
+ code
+ model._cron_shopify_import_orders()
+ 15
+ minutes
+
+
+ Shopify: send the stock
+
+ code
+ model._cron_shopify_export_stock()
+ 15
+ minutes
+
+
+ Shopify: import the changed products
+
+ code
+ model._cron_shopify_import_products()
+ 1
+ days
+
+
+ Shopify: send the prices and import the payouts
+
+ code
+ model._cron_shopify_daily()
+ 1
+ days
+
+
diff --git a/om_shopify/data/product_data.xml b/om_shopify/data/product_data.xml
new file mode 100644
index 00000000..3b179de9
--- /dev/null
+++ b/om_shopify/data/product_data.xml
@@ -0,0 +1,51 @@
+
+
+
+ Shipping (Shopify)
+ service
+ order
+
+ 0
+
+
+
+ Tip (Shopify)
+ service
+ order
+
+ 0
+
+
+
+ Gift Card (Shopify)
+ service
+ order
+
+ 0
+
+
+
+ Custom Item (Shopify)
+ consu
+ order
+
+ 0
+
+
+ Duties (Shopify)
+ service
+ order
+
+ 0
+
+
+
+ Refund Adjustment (Shopify)
+ service
+ order
+
+
+ 0
+
+
+
diff --git a/om_shopify/models/__init__.py b/om_shopify/models/__init__.py
new file mode 100644
index 00000000..6b331b8e
--- /dev/null
+++ b/om_shopify/models/__init__.py
@@ -0,0 +1,15 @@
+from . import om_shopify_store
+from . import om_shopify_webhook
+from . import om_shopify_location
+from . import om_shopify_gateway
+from . import om_shopify_product
+from . import om_shopify_customer
+from . import product_template
+from . import sale_order
+from . import stock_picking
+from . import om_shopify_order_rule
+from . import account_move
+from . import om_shopify_payout
+from . import digest
+from . import om_shopify_metafield
+from . import om_shopify_b2b
diff --git a/om_shopify/models/account_move.py b/om_shopify/models/account_move.py
new file mode 100644
index 00000000..77d747bc
--- /dev/null
+++ b/om_shopify/models/account_move.py
@@ -0,0 +1,102 @@
+import uuid
+
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError
+
+from ..tools import queries
+
+
+class AccountMove(models.Model):
+ _inherit = 'account.move'
+
+ shopify_store_id = fields.Many2one('om.shopify.store', string='Shopify Store', readonly=True, copy=False,
+ index='btree_not_null')
+ shopify_refund_gid = fields.Char(string='Shopify Refund', readonly=True, copy=False)
+ shopify_order_id = fields.Many2one('sale.order', compute='_compute_shopify_order_id', string='Shopify Order')
+
+ @api.depends('invoice_line_ids.sale_line_ids', 'reversed_entry_id')
+ def _compute_shopify_order_id(self):
+ for move in self:
+ orders = (move.invoice_line_ids.sale_line_ids.order_id
+ | move.reversed_entry_id.invoice_line_ids.sale_line_ids.order_id)
+ move.shopify_order_id = orders.filtered('shopify_store_id')[:1]
+
+ def action_shopify_refund(self):
+ """ Refund this credit note in Shopify: the lines, the shipping, and the money on the payment
+ methods of the order. """
+ for move in self:
+ if move.move_type != 'out_refund' or move.state != 'posted':
+ raise UserError(_("Only a posted credit note is refunded in Shopify."))
+ if move.shopify_refund_gid:
+ raise UserError(_("%(move)s is already refunded in Shopify.", move=move.name))
+ order = move.shopify_order_id
+ if not order:
+ raise UserError(_("%(move)s is not the credit note of a Shopify order.", move=move.name))
+ order.shopify_store_id._connector_enqueue(
+ '_connector_job_shopify_refund', {'move_id': move.id}, model_name='account.move',
+ name=_("Refund %(order)s in Shopify (%(move)s)", order=order.shopify_order_name, move=move.name),
+ dedupe_key=f'refund:{move.id}', max_attempts=3)
+ return {
+ 'type': 'ir.actions.client', 'tag': 'display_notification',
+ 'params': {'message': _("The refund is being sent to Shopify."), 'type': 'info'},
+ }
+
+ @api.model
+ def _connector_job_shopify_refund(self, job):
+ move = self.browse(job.payload['move_id']).exists()
+ if not move or move.shopify_refund_gid:
+ return _("Nothing to refund")
+ return move._shopify_refund(job.backend)
+
+ def _shopify_refund(self, store):
+ self.ensure_one()
+ order = self.shopify_order_id
+ lines, shipping = [], 0.0
+ location = store.location_ids.filtered(lambda l: l.warehouse_id == order.warehouse_id)[:1]
+ for line in self.invoice_line_ids.filtered(lambda l: l.display_type == 'product'):
+ gid = line.sale_line_ids[:1].shopify_line_gid
+ if gid and line.quantity:
+ refund_line = {'lineItemId': gid, 'quantity': int(line.quantity),
+ 'restockType': store.refund_restock_type}
+ if store.refund_restock_type == 'RETURN' and location:
+ refund_line['locationId'] = location.shopify_gid
+ lines.append(refund_line)
+ elif line.product_id == store.shipping_product_id:
+ shipping += line.price_total
+ suggestion = store._shopify_execute(queries.SUGGESTED_REFUND, {
+ 'id': order.shopify_order_gid,
+ 'lines': [{k: v for k, v in l.items() if k in ('lineItemId', 'quantity', 'restockType', 'locationId')}
+ for l in lines],
+ 'shipping': f'{shipping:.2f}' if shipping else None,
+ }).get('order', {}).get('suggestedRefund') or {}
+ remaining = self.amount_total
+ transactions = []
+ for suggested in suggestion.get('suggestedTransactions') or []:
+ available = float(((suggested.get('amountSet') or {}).get('shopMoney') or {}).get('amount') or 0.0)
+ take = min(available, remaining)
+ if take <= 0:
+ continue
+ transactions.append({
+ 'orderId': order.shopify_order_gid,
+ 'parentId': (suggested.get('parentTransaction') or {}).get('id'),
+ 'gateway': suggested.get('gateway'),
+ 'kind': 'REFUND',
+ 'amount': f'{take:.2f}',
+ })
+ remaining -= take
+ refund_input = {
+ 'orderId': order.shopify_order_gid,
+ 'note': self.ref or self.name,
+ 'notify': store.refund_notify,
+ 'refundLineItems': lines,
+ 'transactions': transactions,
+ }
+ if shipping:
+ refund_input['shipping'] = {'amount': f'{shipping:.2f}'}
+ result = store._shopify_mutate(queries.REFUND_CREATE, {'input': refund_input, 'key': str(uuid.uuid4())},
+ 'refundCreate')
+ self.write({'shopify_refund_gid': (result.get('refund') or {}).get('id'), 'shopify_store_id': store.id})
+ if remaining > 0.01:
+ self.message_post(body=_("Shopify could refund %(amount)s less than this credit note on the payment "
+ "methods of the order.", amount=self.currency_id.format(remaining)))
+ return self
diff --git a/om_shopify/models/digest.py b/om_shopify/models/digest.py
new file mode 100644
index 00000000..026443f9
--- /dev/null
+++ b/om_shopify/models/digest.py
@@ -0,0 +1,20 @@
+from odoo import fields, models
+
+
+class DigestDigest(models.Model):
+ _inherit = 'digest.digest'
+
+ kpi_om_shopify_orders = fields.Boolean(string='Shopify Orders')
+ kpi_om_shopify_orders_value = fields.Integer(compute='_compute_kpi_om_shopify_orders_value')
+
+ def _compute_kpi_om_shopify_orders_value(self):
+ self._calculate_company_based_kpi(
+ 'sale.order', 'kpi_om_shopify_orders_value', date_field='date_order',
+ additional_domain=[('shopify_store_id', '!=', False), ('state', '=', 'sale')])
+
+ def _compute_kpis_actions(self, company, user):
+ res = super()._compute_kpis_actions(company, user)
+ menu = self.env.ref('om_shopify.om_shopify_menu_root', raise_if_not_found=False)
+ res['kpi_om_shopify_orders'] = 'om_shopify.om_shopify_sale_order_action' + (
+ f'?menu_id={menu.id}' if menu else '')
+ return res
diff --git a/om_shopify/models/om_shopify_b2b.py b/om_shopify/models/om_shopify_b2b.py
new file mode 100644
index 00000000..fd0a083a
--- /dev/null
+++ b/om_shopify/models/om_shopify_b2b.py
@@ -0,0 +1,250 @@
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError
+
+from ..tools import queries
+
+
+class OmShopifyCompany(models.Model):
+ """ A B2B company of Shopify (Shopify Plus), linked to an Odoo company contact. """
+ _name = 'om.shopify.company'
+ _description = 'Shopify B2B Company'
+ _inherit = ['om.connector.binding.mixin']
+ _order = 'store_id, name'
+ _connector_backend_field = 'store_id'
+ _connector_record_field = 'partner_id'
+
+ name = fields.Char(readonly=True)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ partner_id = fields.Many2one('res.partner', string='Company Contact', required=True, ondelete='cascade',
+ index=True)
+ location_ids = fields.One2many('om.shopify.company.location', 'company_binding_id', string='Locations')
+
+ _sql_constraints = [
+ ('store_external_unique', 'unique (store_id, external_id)',
+ "A Shopify company is linked once per store."),
+ ]
+
+ @api.model
+ def _connector_job_shopify_import_company(self, job):
+ return self._shopify_import_company(job.backend, job.payload['id'])
+
+ @api.model
+ def _connector_job_shopify_company_webhook(self, job):
+ payload = job.payload
+ gid = payload.get('admin_graphql_api_id') or (payload.get('company') or {}).get('admin_graphql_api_id')
+ if not gid or '/Company/' not in gid:
+ return _("No company in this webhook")
+ return self._shopify_import_company(job.backend, gid)
+
+ @api.model
+ def _shopify_import_company(self, store, gid):
+ """ Create or update the company contact of the Shopify company ``gid``, its locations as
+ addresses (with their payment terms and tax exemption), and its contacts as its people. """
+ data = store._shopify_execute(queries.COMPANY, {'id': gid}).get('company')
+ if not data:
+ return _("Company %(id)s does not exist anymore", id=gid)
+ Partner = self.env['res.partner']
+ binding = self._connector_find(store, gid)
+ partner = binding.partner_id
+ vals = {'name': data['name'], 'ref': data.get('externalId') or False}
+ if partner:
+ partner.write(vals)
+ else:
+ partner = Partner.create(vals)
+ binding = self._connector_bind(store, gid, partner, {'name': data['name']})
+ Location = self.env['om.shopify.company.location']
+ for location in (data.get('locations') or {}).get('nodes') or []:
+ Location._shopify_import(store, binding, location)
+ Customer = self.env['om.shopify.customer']
+ for contact in (data.get('contacts') or {}).get('nodes') or []:
+ person = Customer._connector_find(store, (contact.get('customer') or {}).get('id') or '').partner_id
+ if person and not person.parent_id and person != partner:
+ person.parent_id = partner
+ return partner
+
+
+class OmShopifyCompanyLocation(models.Model):
+ """ A location of a Shopify B2B company: its addresses, payment terms and tax exemption. """
+ _name = 'om.shopify.company.location'
+ _description = 'Shopify B2B Company Location'
+ _inherit = ['om.connector.binding.mixin']
+ _order = 'company_binding_id, name'
+ _connector_backend_field = 'store_id'
+ _connector_record_field = 'partner_id'
+
+ name = fields.Char(readonly=True)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ company_binding_id = fields.Many2one('om.shopify.company', string='Shopify Company', required=True,
+ ondelete='cascade', index=True)
+ partner_id = fields.Many2one('res.partner', string='Delivery Address', required=True, ondelete='cascade')
+ invoice_partner_id = fields.Many2one('res.partner', string='Invoice Address', ondelete='set null')
+ payment_term_id = fields.Many2one('account.payment.term', string='Payment Terms', ondelete='set null')
+ tax_exempt = fields.Boolean(readonly=True)
+
+ _sql_constraints = [
+ ('store_external_unique', 'unique (store_id, external_id)',
+ "A Shopify company location is linked once per store."),
+ ]
+
+ @api.model
+ def _shopify_address_vals(self, address):
+ if not address:
+ return {}
+ country = self.env['res.country'].search([('code', '=', address.get('countryCode'))], limit=1) \
+ if address.get('countryCode') else self.env['res.country']
+ state = self.env['res.country.state']
+ if country and address.get('zoneCode'):
+ state = state.search([('country_id', '=', country.id), ('code', '=', address['zoneCode'])], limit=1)
+ return {
+ 'street': address.get('address1') or False, 'street2': address.get('address2') or False,
+ 'city': address.get('city') or False, 'zip': address.get('zip') or False,
+ 'country_id': country.id, 'state_id': state.id, 'phone': address.get('phone') or False,
+ }
+
+ @api.model
+ def _shopify_payment_term(self, template):
+ """ The Odoo payment terms of a Shopify payment terms template, found by name or created. """
+ if not template or not template.get('name'):
+ return self.env['account.payment.term']
+ Term = self.env['account.payment.term']
+ term = Term.search([('name', '=ilike', template['name'])], limit=1)
+ if not term and template.get('dueInDays') is not None:
+ term = Term.create({'name': template['name'], 'line_ids': [fields.Command.create({
+ 'value': 'percent', 'value_amount': 100.0, 'nb_days': int(template['dueInDays'])})]})
+ return term
+
+ @api.model
+ def _shopify_import(self, store, company_binding, data):
+ company = company_binding.partner_id
+ binding = self._connector_find(store, data['id'])
+ fiscal_position = store.b2b_tax_exempt_fiscal_position_id if (data.get('taxSettings') or {}).get('taxExempt') \
+ else self.env['account.fiscal.position']
+ term = self._shopify_payment_term(
+ ((data.get('buyerExperienceConfiguration') or {}).get('paymentTermsTemplate')) or {})
+ delivery_vals = {'name': data.get('name') or company.name, 'type': 'delivery', 'parent_id': company.id,
+ **self._shopify_address_vals(data.get('shippingAddress'))}
+ invoice_vals = {'name': data.get('name') or company.name, 'type': 'invoice', 'parent_id': company.id,
+ **self._shopify_address_vals(data.get('billingAddress') or data.get('shippingAddress'))}
+ Partner = self.env['res.partner']
+ delivery = binding.partner_id
+ if delivery:
+ delivery.write(delivery_vals)
+ else:
+ delivery = Partner.create(delivery_vals)
+ invoice = binding.invoice_partner_id
+ if invoice:
+ invoice.write(invoice_vals)
+ else:
+ invoice = Partner.create(invoice_vals)
+ for partner in company | delivery | invoice:
+ if fiscal_position:
+ partner.with_company(store.company_id).property_account_position_id = fiscal_position
+ if term:
+ company.with_company(store.company_id).property_payment_term_id = term
+ return self._connector_bind(store, data['id'], delivery, {
+ 'name': data.get('name'), 'company_binding_id': company_binding.id, 'invoice_partner_id': invoice.id,
+ 'payment_term_id': term.id, 'tax_exempt': bool((data.get('taxSettings') or {}).get('taxExempt')),
+ })
+
+
+class OmShopifyPriceList(models.Model):
+ """ A price list of Shopify (a market or a B2B catalog), and the Odoo pricelist that fills it. """
+ _name = 'om.shopify.price.list'
+ _description = 'Shopify Price List'
+ _order = 'store_id, name'
+ _check_company_auto = True
+
+ name = fields.Char(required=True, readonly=True)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ shopify_gid = fields.Char(string='Shopify ID', required=True, readonly=True)
+ currency_code = fields.Char(string='Currency', readonly=True)
+ pricelist_id = fields.Many2one('product.pricelist', string='Odoo Pricelist', check_company=True,
+ help="Its prices are sent as the fixed prices of this Shopify price list.")
+
+ _sql_constraints = [
+ ('store_gid_unique', 'unique (store_id, shopify_gid)', "A price list is imported once per store."),
+ ]
+
+ @api.model
+ def _shopify_import(self, store):
+ existing = {p.shopify_gid: p for p in self.search([('store_id', '=', store.id)])}
+ for node in store._shopify_paginate(queries.PRICE_LISTS, {}, 'priceLists'):
+ vals = {'name': node['name'], 'currency_code': node.get('currency')}
+ if node['id'] in existing:
+ existing[node['id']].write(vals)
+ else:
+ existing[node['id']] = self.create({'store_id': store.id, 'shopify_gid': node['id'], **vals})
+ return self.browse([p.id for p in existing.values()])
+
+ def _shopify_export(self, batch=250):
+ """ Send the prices of the linked variants, computed by the Odoo pricelist, as fixed prices. """
+ self.ensure_one()
+ if not self.pricelist_id:
+ return 0
+ if self.currency_code and self.pricelist_id.currency_id.name != self.currency_code:
+ raise UserError(_("%(pricelist)s is in %(odoo)s, the Shopify price list %(shopify)s in %(currency)s.",
+ pricelist=self.pricelist_id.display_name, odoo=self.pricelist_id.currency_id.name,
+ shopify=self.name, currency=self.currency_code))
+ variants = self.env['om.shopify.product'].search([('store_id', '=', self.store_id.id), ('active', '=', True)])
+ currency = self.pricelist_id.currency_id.name
+ prices = [{'variantId': v.external_id,
+ 'price': {'amount': f'{self.pricelist_id._get_product_price(v.product_id, 1.0):.2f}',
+ 'currencyCode': currency}}
+ for v in variants]
+ for start in range(0, len(prices), batch):
+ self.store_id._shopify_mutate(queries.PRICE_LIST_FIXED_PRICES_ADD,
+ {'priceListId': self.shopify_gid, 'prices': prices[start:start + batch]},
+ 'priceListFixedPricesAdd')
+ return len(prices)
+
+
+class OmShopifyStoreB2B(models.Model):
+ _inherit = 'om.shopify.store'
+
+ b2b_import = fields.Boolean(string='Import B2B Companies',
+ help="Imports the B2B companies of Shopify Plus, with their locations, payment terms "
+ "and tax exemption, and links their orders to them.")
+ b2b_tax_exempt_fiscal_position_id = fields.Many2one(
+ 'account.fiscal.position', string='Fiscal Position of Tax-Exempt Locations', check_company=True)
+ company_binding_ids = fields.One2many('om.shopify.company', 'store_id', string='B2B Companies')
+ price_list_ids = fields.One2many('om.shopify.price.list', 'store_id', string='Price Lists')
+
+ def _shopify_webhook_handlers(self):
+ handlers = super()._shopify_webhook_handlers()
+ if self.b2b_import:
+ company = ('om.shopify.company', '_connector_job_shopify_company_webhook')
+ handlers.update({'COMPANIES_CREATE': company, 'COMPANIES_UPDATE': company})
+ return handlers
+
+ def action_shopify_import_companies(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ store._connector_enqueue_once('_connector_job_shopify_fetch_companies', name=_("Import the B2B companies"))
+ return self._shopify_notification(_("The companies are being imported: follow it in Jobs."), kind='info')
+
+ def action_shopify_import_price_lists(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ self.env['om.shopify.price.list']._shopify_import(store)
+ return self._shopify_notification(_("Price lists imported: give them an Odoo pricelist."))
+
+ @api.model
+ def _connector_job_shopify_fetch_companies(self, job):
+ store = job.backend
+ count = 0
+ for node in store._shopify_paginate(queries.COMPANY_IDS, {}, 'companies'):
+ store._connector_enqueue('_connector_job_shopify_import_company', {'id': node['id'],
+ 'updatedAt': node.get('updatedAt')}, model_name='om.shopify.company',
+ name=_("Company %(id)s", id=node['id'].rsplit('/', 1)[-1]),
+ dedupe_key=f"company:{node['id']}", priority=25)
+ count += 1
+ return _("%(count)s company(ies) to import", count=count)
+
+ @api.model
+ def _connector_job_shopify_export_prices(self, job):
+ result = super()._connector_job_shopify_export_prices(job)
+ sent = sum(price_list._shopify_export() for price_list in job.backend.price_list_ids.filtered('pricelist_id'))
+ return _("%(result)s; %(count)s price list price(s) sent", result=result, count=sent) if sent else result
diff --git a/om_shopify/models/om_shopify_customer.py b/om_shopify/models/om_shopify_customer.py
new file mode 100644
index 00000000..f9903457
--- /dev/null
+++ b/om_shopify/models/om_shopify_customer.py
@@ -0,0 +1,131 @@
+from odoo import _, api, fields, models
+
+from ..tools import queries
+
+
+class OmShopifyCustomer(models.Model):
+ """ A Shopify customer, linked to an Odoo contact. """
+ _name = 'om.shopify.customer'
+ _description = 'Shopify Customer'
+ _inherit = ['om.connector.binding.mixin']
+ _order = 'store_id, id desc'
+ _connector_backend_field = 'store_id'
+ _connector_record_field = 'partner_id'
+
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ partner_id = fields.Many2one('res.partner', string='Contact', required=True, ondelete='cascade', index=True)
+
+ _sql_constraints = [
+ ('store_external_unique', 'unique (store_id, external_id)',
+ "A Shopify customer is linked once per store."),
+ ]
+
+ @api.model
+ def _connector_job_shopify_import_customer(self, job):
+ return self._shopify_import_customer(job.backend, job.payload['id'])
+
+ @api.model
+ def _connector_job_shopify_customer_webhook(self, job):
+ gid = job.payload.get('admin_graphql_api_id') or f"gid://shopify/Customer/{job.payload['id']}"
+ return self._shopify_import_customer(job.backend, gid)
+
+ @api.model
+ def _shopify_import_customer(self, store, gid):
+ """ Create or update the contact of the Shopify customer ``gid``. Gives the contact. """
+ customer = store._shopify_execute(store._shopify_query(queries.CUSTOMER), {'id': gid}).get('customer')
+ if not customer:
+ return _("Customer %(id)s does not exist anymore", id=gid)
+ address = customer.get('defaultAddress') or {}
+ email = (customer.get('defaultEmailAddress') or {}).get('emailAddress')
+ binding = self._connector_find(store, gid)
+ partner = binding.partner_id
+ if partner:
+ # an existing contact gets the blanks filled, never its data replaced
+ name = ' '.join(filter(None, [customer.get('firstName'), customer.get('lastName')]))
+ vals = {'name': name, 'email': email,
+ 'phone': (customer.get('defaultPhoneNumber') or {}).get('phoneNumber'),
+ **self._shopify_address_vals(address)}
+ partner.write({k: v for k, v in vals.items() if v and not partner[k]})
+ else:
+ partner, _invoice, _delivery = self._shopify_partner(store, {
+ 'customer': customer, 'email': email, 'billingAddress': address, 'name': gid,
+ })
+ if tags := [t for t in customer.get('tags') or [] if t]:
+ Category = self.env['res.partner.category']
+ partner.category_id = [fields.Command.link(
+ (Category.search([('name', '=', tag)], limit=1) or Category.create({'name': tag})).id) for tag in tags]
+ store._shopify_apply_metafields('customer', partner, customer.get('metafields'))
+ return partner
+
+ @api.model
+ def _shopify_partner(self, store, order):
+ """ The contact, invoice address and delivery address of a Shopify order:
+ ``(partner, invoice, delivery)``. """
+ customer = order.get('customer') or {}
+ billing = order.get('billingAddress') or {}
+ shipping = order.get('shippingAddress') or {}
+ email = (order.get('email') or ((customer.get('defaultEmailAddress') or {}).get('emailAddress')) or '').strip()
+ phone = order.get('phone') or (customer.get('defaultPhoneNumber') or {}).get('phoneNumber') or False
+
+ partner = None
+ if customer.get('id'):
+ partner = self._connector_find(store, customer['id']).partner_id
+ if not partner and email:
+ partner = self.env['res.partner'].search([
+ ('email', '=ilike', email), ('parent_id', '=', False),
+ ('company_id', 'in', [False, store.company_id.id]),
+ ], limit=1)
+ if not partner:
+ name = ' '.join(filter(None, [customer.get('firstName'), customer.get('lastName')])) \
+ or billing.get('name') or shipping.get('name') or email or order.get('name')
+ vals = {'name': name, 'email': email or False, 'phone': phone,
+ **self._shopify_address_vals(billing or shipping)}
+ partner = self.env['res.partner'].create(vals)
+ if customer.get('id'):
+ self._connector_bind(store, customer['id'], partner)
+ invoice = self._shopify_child(partner, 'invoice', billing) if billing else partner
+ delivery = self._shopify_child(partner, 'delivery', shipping) if shipping else invoice
+ return partner, invoice, delivery
+
+ @api.model
+ def _shopify_address_vals(self, address):
+ if not address:
+ return {}
+ country = self.env['res.country'].search([('code', '=', address.get('countryCodeV2'))], limit=1) \
+ if address.get('countryCodeV2') else self.env['res.country']
+ state = self.env['res.country.state']
+ if country and address.get('provinceCode'):
+ state = state.search([('country_id', '=', country.id), ('code', '=', address['provinceCode'])], limit=1)
+ return {
+ 'street': address.get('address1') or False,
+ 'street2': address.get('address2') or False,
+ 'city': address.get('city') or False,
+ 'zip': address.get('zip') or False,
+ 'country_id': country.id,
+ 'state_id': state.id,
+ }
+
+ @api.model
+ def _shopify_child(self, partner, address_type, address):
+ """ The address of ``partner``: itself when it is the same, else one of its own addresses of
+ this type, found or created. Never the address of another contact. """
+ vals = self._shopify_address_vals(address)
+ name = address.get('name') or partner.name
+
+ def same(record):
+ return all((record[key].id if hasattr(record[key], 'id') else record[key] or False) == value
+ for key, value in vals.items())
+
+ if same(partner) and name == partner.name:
+ return partner
+ child = partner.child_ids.filtered(lambda c: c.type == address_type and c.name == name and same(c))[:1]
+ if child:
+ return child
+ return self.env['res.partner'].create({
+ 'parent_id': partner.id,
+ 'type': address_type,
+ 'name': name,
+ 'phone': address.get('phone') or False,
+ **vals,
+ })
diff --git a/om_shopify/models/om_shopify_gateway.py b/om_shopify/models/om_shopify_gateway.py
new file mode 100644
index 00000000..f798b31c
--- /dev/null
+++ b/om_shopify/models/om_shopify_gateway.py
@@ -0,0 +1,25 @@
+from odoo import api, fields, models
+
+
+class OmShopifyGateway(models.Model):
+ """ A payment method of a Shopify store (shopify_payments, manual, paypal…), and the workflow of
+ the orders paid with it. Made when Odoo meets a new one in an order. """
+ _name = 'om.shopify.gateway'
+ _description = 'Shopify Payment Method'
+ _order = 'store_id, name'
+ _check_company_auto = True
+
+ name = fields.Char(required=True, readonly=True)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ workflow_id = fields.Many2one('om.connector.order.workflow', string='Workflow', required=True, check_company=True)
+
+ _sql_constraints = [
+ ('store_name_unique', 'unique (store_id, name)', "A payment method is listed once per store."),
+ ]
+
+ @api.model
+ def _shopify_get(self, store, name):
+ name = name or 'manual'
+ gateway = self.search([('store_id', '=', store.id), ('name', '=', name)], limit=1)
+ return gateway or self.create({'store_id': store.id, 'name': name, 'workflow_id': store.workflow_id.id})
diff --git a/om_shopify/models/om_shopify_location.py b/om_shopify/models/om_shopify_location.py
new file mode 100644
index 00000000..87a20a01
--- /dev/null
+++ b/om_shopify/models/om_shopify_location.py
@@ -0,0 +1,44 @@
+from odoo import api, fields, models
+
+from ..tools import queries
+
+
+class OmShopifyLocation(models.Model):
+ """ A location of a Shopify store, and the Odoo warehouse whose stock it shows. """
+ _name = 'om.shopify.location'
+ _description = 'Shopify Location'
+ _order = 'store_id, sequence, name'
+ _check_company_auto = True
+
+ name = fields.Char(required=True, readonly=True)
+ sequence = fields.Integer(default=10)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ shopify_gid = fields.Char(string='Shopify ID', required=True, readonly=True)
+ active = fields.Boolean(default=True)
+ shopify_active = fields.Boolean(string='Active in Shopify', readonly=True)
+ warehouse_id = fields.Many2one(
+ 'stock.warehouse', check_company=True,
+ help="The stock of this warehouse is sent to this location. Empty: the stock of this location is not sent.")
+
+ _sql_constraints = [
+ ('store_gid_unique', 'unique (store_id, shopify_gid)', "A location is imported once per store."),
+ ]
+
+ @api.model
+ def _shopify_import(self, store):
+ """ Create or update the locations of ``store``; the first new one gets the warehouse of the store. """
+ existing = {loc.shopify_gid: loc for loc in self.with_context(active_test=False).search(
+ [('store_id', '=', store.id)])}
+ first_mapping = not any(loc.warehouse_id for loc in existing.values())
+ for node in store._shopify_paginate(queries.LOCATIONS, {}, 'locations'):
+ vals = {'name': node['name'], 'shopify_active': node.get('isActive', True)}
+ location = existing.get(node['id'])
+ if location:
+ location.write(vals)
+ continue
+ if first_mapping and node.get('isActive', True):
+ vals['warehouse_id'] = store.warehouse_id.id
+ first_mapping = False
+ existing[node['id']] = self.create({'store_id': store.id, 'shopify_gid': node['id'], **vals})
+ return self.browse([loc.id for loc in existing.values()])
diff --git a/om_shopify/models/om_shopify_metafield.py b/om_shopify/models/om_shopify_metafield.py
new file mode 100644
index 00000000..648ea08e
--- /dev/null
+++ b/om_shopify/models/om_shopify_metafield.py
@@ -0,0 +1,168 @@
+import json
+
+from odoo import _, api, fields, models
+from odoo.exceptions import ValidationError
+
+RESOURCE_MODELS = {
+ 'product': 'product.template',
+ 'variant': 'product.product',
+ 'customer': 'res.partner',
+ 'order': 'sale.order',
+}
+FIELD_TYPES = ['char', 'text', 'html', 'integer', 'float', 'monetary', 'boolean', 'date', 'datetime', 'selection']
+METAFIELD_TYPES = [
+ ('single_line_text_field', 'Single line text'),
+ ('multi_line_text_field', 'Multi-line text'),
+ ('number_integer', 'Integer'),
+ ('number_decimal', 'Decimal'),
+ ('boolean', 'True or false'),
+ ('date', 'Date'),
+ ('date_time', 'Date and time'),
+ ('json', 'JSON'),
+]
+
+
+class OmShopifyMetafield(models.Model):
+ """ A Shopify metafield and the Odoo field it fills, or is filled from. """
+ _name = 'om.shopify.metafield'
+ _description = 'Shopify Metafield Mapping'
+ _order = 'store_id, resource, namespace, key'
+
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ active = fields.Boolean(default=True)
+ resource = fields.Selection([
+ ('product', 'Product'),
+ ('variant', 'Variant'),
+ ('customer', 'Customer'),
+ ('order', 'Order'),
+ ], required=True, default='product')
+ model_name = fields.Char(compute='_compute_model_name')
+ namespace = fields.Char(required=True, default='custom')
+ key = fields.Char(required=True)
+ metafield_type = fields.Selection(METAFIELD_TYPES, string='Shopify Type', required=True,
+ default='single_line_text_field')
+ field_id = fields.Many2one('ir.model.fields', string='Odoo Field', required=True, ondelete='cascade',
+ domain="[('model', '=', model_name), ('ttype', 'in', %s), ('store', '=', True)]"
+ % FIELD_TYPES)
+ direction = fields.Selection([
+ ('import', 'Shopify → Odoo'),
+ ('export', 'Odoo → Shopify'),
+ ('both', 'Both Ways'),
+ ], required=True, default='import',
+ help="Odoo → Shopify is for products and variants: their metafields are sent with the product export.")
+
+ _sql_constraints = [
+ ('store_key_unique', 'unique (store_id, resource, namespace, key)',
+ "A metafield is mapped once per store."),
+ ]
+
+ @api.depends('resource')
+ def _compute_model_name(self):
+ for mapping in self:
+ mapping.model_name = RESOURCE_MODELS.get(mapping.resource)
+
+ @api.constrains('field_id', 'resource', 'direction')
+ def _check_field(self):
+ for mapping in self:
+ if mapping.field_id.model != RESOURCE_MODELS[mapping.resource]:
+ raise ValidationError(_("The field of %(key)s is not a field of the %(resource)s.",
+ key=mapping.key, resource=mapping.resource))
+ if mapping.field_id.ttype not in FIELD_TYPES:
+ raise ValidationError(_("%(field)s cannot hold a metafield.", field=mapping.field_id.name))
+ if mapping.direction != 'import' and mapping.resource not in ('product', 'variant'):
+ raise ValidationError(_("Only the metafields of products and variants are sent to Shopify."))
+
+ @property
+ def full_key(self):
+ return f'{self.namespace}.{self.key}'
+
+ # ------------------------------------------------------------------
+ # Conversion
+ # ------------------------------------------------------------------
+
+ def _to_odoo(self, value):
+ self.ensure_one()
+ ttype = self.field_id.ttype
+ if value in (None, ''):
+ return False
+ try:
+ if ttype in ('integer',):
+ return int(float(value))
+ if ttype in ('float', 'monetary'):
+ return float(json.loads(value)['amount']) if value.startswith('{') else float(value)
+ if ttype == 'boolean':
+ return str(value).lower() == 'true'
+ if ttype == 'date':
+ return fields.Date.to_date(value[:10])
+ if ttype == 'datetime':
+ return fields.Datetime.to_datetime(value[:19].replace('T', ' '))
+ if ttype == 'selection':
+ record = self.env[self.field_id.model]
+ keys = [k for k, _label in record._fields[self.field_id.name]._description_selection(self.env)]
+ return value if value in keys else False
+ except (ValueError, TypeError, KeyError):
+ return False
+ return value
+
+ def _to_shopify(self, value):
+ self.ensure_one()
+ if value is False or value is None or value == '':
+ return None
+ if self.field_id.ttype == 'boolean':
+ return 'true' if value else 'false'
+ if self.field_id.ttype in ('date', 'datetime'):
+ return fields.Date.to_string(value) if self.field_id.ttype == 'date' \
+ else fields.Datetime.to_datetime(value).strftime('%Y-%m-%dT%H:%M:%SZ')
+ return str(value)
+
+
+class OmShopifyStoreMetafields(models.Model):
+ _inherit = 'om.shopify.store'
+
+ metafield_ids = fields.One2many('om.shopify.metafield', 'store_id', string='Metafields')
+
+ def _shopify_metafield_maps(self, resource, direction=('import', 'both')):
+ self.ensure_one()
+ return self.metafield_ids.filtered(lambda m: m.resource == resource and m.direction in direction)
+
+ def _shopify_query(self, query, bulk=False):
+ """ ``query`` with the mapped metafields of each resource in place of its ``# om:metafields:…``
+ comment. Without mappings the comment stays, and costs nothing. """
+ self.ensure_one()
+ for resource in RESOURCE_MODELS:
+ marker = f'# om:metafields:{resource}'
+ maps = self._shopify_metafield_maps(resource, ('import', 'both', 'export'))
+ if marker not in query or not maps:
+ continue
+ keys = ', '.join(json.dumps(m.full_key) for m in maps)
+ node = 'id namespace key value type'
+ selection = (f'metafields(keys: [{keys}]) {{ edges {{ node {{ {node} }} }} }}' if bulk
+ else f'metafields(first: 50, keys: [{keys}]) {{ nodes {{ {node} }} }}')
+ query = query.replace(marker, selection)
+ return query
+
+ def _shopify_apply_metafields(self, resource, record, metafields):
+ """ Write the mapped metafields of ``metafields`` (a connection, or a list) on ``record``. """
+ self.ensure_one()
+ maps = self._shopify_metafield_maps(resource)
+ if not maps or not record:
+ return
+ if isinstance(metafields, dict):
+ metafields = metafields.get('nodes') or [e.get('node') for e in metafields.get('edges') or []]
+ values = {f"{m.get('namespace')}.{m.get('key')}": m.get('value') for m in metafields or [] if m}
+ vals = {mapping.field_id.name: mapping._to_odoo(values[mapping.full_key])
+ for mapping in maps if mapping.full_key in values}
+ if vals:
+ record.sudo().write(vals)
+
+ def _shopify_metafield_inputs(self, resource, record):
+ """ The metafields of ``record`` to send with a product export. """
+ self.ensure_one()
+ inputs = []
+ for mapping in self._shopify_metafield_maps(resource, ('export', 'both')):
+ value = mapping._to_shopify(record[mapping.field_id.name])
+ if value is not None:
+ inputs.append({'namespace': mapping.namespace, 'key': mapping.key,
+ 'type': mapping.metafield_type, 'value': value})
+ return inputs
diff --git a/om_shopify/models/om_shopify_order_rule.py b/om_shopify/models/om_shopify_order_rule.py
new file mode 100644
index 00000000..afdbbaef
--- /dev/null
+++ b/om_shopify/models/om_shopify_order_rule.py
@@ -0,0 +1,52 @@
+from odoo import fields, models
+
+FINANCIAL_STATUSES = [
+ ('PAID', 'Paid'),
+ ('PENDING', 'Pending'),
+ ('AUTHORIZED', 'Authorized'),
+ ('PARTIALLY_PAID', 'Partially Paid'),
+ ('PARTIALLY_REFUNDED', 'Partially Refunded'),
+ ('REFUNDED', 'Refunded'),
+ ('VOIDED', 'Voided'),
+ ('EXPIRED', 'Expired'),
+]
+FULFILLMENT_STATUSES = [
+ ('UNFULFILLED', 'Unfulfilled'),
+ ('PARTIALLY_FULFILLED', 'Partially Fulfilled'),
+ ('FULFILLED', 'Fulfilled'),
+]
+
+
+class OmShopifyOrderRule(models.Model):
+ """ What an imported order gets when it matches: a workflow, a warehouse, or both. The first
+ rule that matches gives each of them; what no rule gives comes from the payment method, then
+ from the store. Covers for example "paid by cash on delivery: no payment", or "tagged prime:
+ shipped from the Amazon warehouse". """
+ _name = 'om.shopify.order.rule'
+ _description = 'Shopify Order Rule'
+ _order = 'store_id, sequence, id'
+ _check_company_auto = True
+
+ sequence = fields.Integer(default=10)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ gateway_id = fields.Many2one('om.shopify.gateway', string='Payment Method', check_company=True,
+ domain="[('store_id', '=', store_id)]", help="Empty: any payment method.")
+ financial_status = fields.Selection(FINANCIAL_STATUSES, string='Payment Status', help="Empty: any.")
+ fulfillment_status = fields.Selection(FULFILLMENT_STATUSES, string='Fulfillment Status', help="Empty: any.")
+ tag = fields.Char(help="The order has this tag in Shopify. Empty: any.")
+ source = fields.Char(string='Sales Channel', help="Source of the order in Shopify: web, pos, shopify_draft_order… "
+ "Empty: any.")
+ workflow_id = fields.Many2one('om.connector.order.workflow', string='Workflow', check_company=True)
+ warehouse_id = fields.Many2one('stock.warehouse', check_company=True)
+
+ def _matches(self, data, gateway):
+ self.ensure_one()
+ tags = {t.lower() for t in data.get('tags') or []}
+ return (
+ (not self.gateway_id or self.gateway_id == gateway)
+ and (not self.financial_status or self.financial_status == data.get('displayFinancialStatus'))
+ and (not self.fulfillment_status or self.fulfillment_status == data.get('displayFulfillmentStatus'))
+ and (not self.tag or self.tag.lower() in tags)
+ and (not self.source or self.source.lower() == (data.get('sourceName') or '').lower())
+ )
diff --git a/om_shopify/models/om_shopify_payout.py b/om_shopify/models/om_shopify_payout.py
new file mode 100644
index 00000000..3dbac82a
--- /dev/null
+++ b/om_shopify/models/om_shopify_payout.py
@@ -0,0 +1,271 @@
+from datetime import timedelta
+
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError
+
+from ..tools import queries
+
+FEE_TYPES = ('CHARGE', 'REFUND', 'ADJUSTMENT')
+
+
+class OmShopifyPayout(models.Model):
+ """ A payout of Shopify Payments: the money Shopify sent to the bank, with its charges,
+ refunds and fees as bank statement lines of the payout journal. """
+ _name = 'om.shopify.payout'
+ _description = 'Shopify Payout'
+ _inherit = ['mail.thread']
+ _order = 'date desc, id desc'
+ _check_company_auto = True
+
+ name = fields.Char(required=True, readonly=True)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ shopify_gid = fields.Char(string='Shopify ID', required=True, readonly=True)
+ date = fields.Date(readonly=True)
+ status = fields.Char(readonly=True)
+ currency_id = fields.Many2one('res.currency', readonly=True)
+ amount = fields.Monetary(string='Net Amount', readonly=True)
+ statement_line_ids = fields.One2many('account.bank.statement.line', 'shopify_payout_id', string='Statement Lines')
+ move_id = fields.Many2one('account.move', string='Journal Entry', readonly=True, copy=False)
+ bank_line_id = fields.Many2one('account.bank.statement.line', string='Bank Transaction', readonly=True, copy=False,
+ help="The transaction of the bank that received this payout, reconciled with it.")
+ matched_count = fields.Integer(string='Matched Payments', readonly=True)
+ unmatched_count = fields.Integer(string='Payments to Match', readonly=True,
+ help="Charges and refunds of the payout with no payment found in Odoo: they stay "
+ "open on the transit account.")
+ fee_amount = fields.Monetary(string='Fees', readonly=True)
+ gross_amount = fields.Monetary(string='Gross Amount', readonly=True)
+ state = fields.Selection([('draft', 'To Import'), ('done', 'Imported'), ('failed', 'Failed (Reversed)')],
+ default='draft', readonly=True)
+
+ _sql_constraints = [
+ ('store_gid_unique', 'unique (store_id, shopify_gid)', "A payout is imported once per store."),
+ ]
+
+ @api.model
+ def _shopify_import(self, store):
+ """ Import the paid payouts not imported yet, with their transactions. """
+ if not store.payout_journal_id:
+ raise UserError(_("Set the payout journal of %(store)s.", store=store.name))
+ started = fields.Datetime.now()
+ known = set(self.search([('store_id', '=', store.id)]).mapped('shopify_gid'))
+ if store.payout_mode == 'entry':
+ store._shopify_check_payout_setup()
+ payouts = self.browse()
+ for node in store._shopify_paginate(queries.PAYOUTS, {'query': 'status:paid'}, 'shopifyPaymentsAccount.payouts'):
+ if node['id'] in known:
+ continue
+ currency = self.env['res.currency'].search([('name', '=', (node.get('net') or {}).get('currencyCode'))],
+ limit=1)
+ payout = self.create({
+ 'name': _("Payout %(date)s", date=(node.get('issuedAt') or '')[:10]),
+ 'store_id': store.id,
+ 'shopify_gid': node['id'],
+ 'date': (node.get('issuedAt') or '')[:10] or fields.Date.today(),
+ 'status': node.get('status'),
+ 'currency_id': currency.id or store.company_id.currency_id.id,
+ 'amount': float((node.get('net') or {}).get('amount') or 0.0),
+ })
+ if store.payout_mode == 'entry':
+ payout._shopify_post_entry()
+ else:
+ payout._shopify_import_transactions()
+ payouts |= payout
+ failed = self._shopify_reverse_failed(store)
+ store.payout_last_sync = started
+ return _("%(count)s payout(s) imported, %(failed)s reversed", count=len(payouts), failed=failed)
+
+ @api.model
+ def _shopify_reverse_failed(self, store):
+ """ Reverse the entries of the payouts that failed after their import (the bank refused them). """
+ imported = {p.shopify_gid: p for p in self.search([('store_id', '=', store.id), ('state', '=', 'done')])}
+ if not imported:
+ return 0
+ count = 0
+ for node in store._shopify_paginate(queries.PAYOUTS, {'query': 'status:failed'}, 'shopifyPaymentsAccount.payouts'):
+ payout = imported.get(node['id'])
+ if not payout:
+ continue
+ if payout.move_id.state == 'posted':
+ payout.move_id._reverse_moves([{'ref': _("Failed %(payout)s", payout=payout.name),
+ 'date': fields.Date.context_today(self)}], cancel=True)
+ payout.write({'state': 'failed', 'status': node.get('status')})
+ payout.message_post(body=_("This payout failed in Shopify: its entry was reversed."))
+ count += 1
+ return count
+
+ def _shopify_transactions(self):
+ self.ensure_one()
+ payout_id = self.shopify_gid.rsplit('/', 1)[-1]
+ return [node for node in self.store_id._shopify_paginate(
+ queries.BALANCE_TRANSACTIONS, {'query': f'payout_id:{payout_id}'}, 'shopifyPaymentsAccount.balanceTransactions')
+ if node.get('type') != 'PAYOUT']
+
+ def _shopify_order(self, node):
+ gid = (node.get('associatedOrder') or {}).get('id')
+ if not gid:
+ return self.env['sale.order']
+ return self.env['sale.order'].search([('shopify_store_id', '=', self.store_id.id),
+ ('shopify_order_gid', '=', gid)], limit=1)
+
+ def _shopify_post_entry(self):
+ """ One journal entry for the payout: each charge and refund on the transit account (where the
+ payments of Shopify Payments wait), the fees and the adjustments on their accounts, and the net
+ amount on the internal transfer account, that the bank transaction clears. Then match the
+ payments of the orders and the bank transaction. """
+ self.ensure_one()
+ store = self.store_id
+ currency = self.currency_id
+ lines, fees, gross = [], 0.0, 0.0
+ for node in self._shopify_transactions():
+ amount = float((node.get('amount') or {}).get('amount') or 0.0)
+ fees += float((node.get('fee') or {}).get('amount') or 0.0)
+ order = self._shopify_order(node)
+ kind = node.get('type') or ''
+ label = ' '.join(filter(None, [kind.replace('_', ' ').capitalize(),
+ (node.get('associatedOrder') or {}).get('name')]))
+ account = store.payout_transit_account_id if kind in ('CHARGE', 'REFUND') \
+ else store.payout_adjustment_account_id
+ if kind == 'CHARGE':
+ gross += amount
+ lines.append({
+ 'name': label, 'account_id': account.id, 'balance': -amount,
+ 'partner_id': order.partner_invoice_id.commercial_partner_id.id or False,
+ 'om_shopify_order_id': order.id,
+ })
+ if fees:
+ lines.append({'name': _("Shopify Payments fees"), 'account_id': store.payout_fee_account_id.id,
+ 'balance': fees})
+ net = currency.round(sum(-l['balance'] for l in lines))
+ difference = currency.round(self.amount - net)
+ if not currency.is_zero(difference):
+ lines.append({'name': _("Difference with the payout"), 'account_id': store.payout_adjustment_account_id.id,
+ 'balance': -difference})
+ lines.append({'name': self.name, 'account_id': store.payout_clearing_account_id.id, 'balance': self.amount})
+ move = self.env['account.move'].create({
+ 'move_type': 'entry',
+ 'journal_id': store.payout_entry_journal_id.id,
+ 'date': self.date,
+ 'ref': _("Shopify %(payout)s", payout=self.name),
+ 'line_ids': [fields.Command.create({k: v for k, v in l.items() if k != 'om_shopify_order_id'})
+ for l in lines],
+ })
+ move.action_post()
+ matched, unmatched = self._shopify_match_payments(move, lines)
+ self.write({'move_id': move.id, 'state': 'done', 'matched_count': matched, 'unmatched_count': unmatched,
+ 'fee_amount': fees, 'gross_amount': gross})
+ self._shopify_match_bank()
+ return move
+
+ def _shopify_match_payments(self, move, lines):
+ """ Reconcile each charge or refund of the payout with the payment of its order on the transit
+ account, when there is one of the same amount. Gives ``(matched, unmatched)``. """
+ transit = self.store_id.payout_transit_account_id
+ AML = self.env['account.move.line']
+ matched = unmatched = 0
+ payout_lines = move.line_ids.filtered(lambda l: l.account_id == transit)
+ for payout_line in payout_lines:
+ domain = [('account_id', '=', transit.id), ('reconciled', '=', False), ('parent_state', '=', 'posted'),
+ ('move_id', '!=', move.id), ('company_id', '=', move.company_id.id)]
+ if payout_line.partner_id:
+ domain.append(('partner_id', 'child_of', payout_line.partner_id.id))
+ # the payment is on the other side of the transit account, for the same amount
+ domain.append(('balance', '=', -payout_line.balance))
+ candidate = AML.search(domain, order='date desc, id desc', limit=1)
+ if candidate:
+ (payout_line | candidate).reconcile()
+ matched += 1
+ else:
+ unmatched += 1
+ return matched, unmatched
+
+ def _shopify_match_bank(self):
+ """ Reconcile the bank transaction of the payout, when exactly one fits: same amount, a few days
+ after the payout, not reconciled yet. """
+ self.ensure_one()
+ store = self.store_id
+ if not store.payout_journal_id or not self.move_id:
+ return False
+ clearing_line = self.move_id.line_ids.filtered(
+ lambda l: l.account_id == store.payout_clearing_account_id and not l.reconciled)
+ candidates = self.env['account.bank.statement.line'].search([
+ ('journal_id', '=', store.payout_journal_id.id), ('is_reconciled', '=', False),
+ ('amount', '=', self.amount),
+ ('date', '>=', self.date - timedelta(days=3)), ('date', '<=', self.date + timedelta(days=10)),
+ ('shopify_payout_id', '=', False),
+ ])
+ if len(candidates) != 1 or not clearing_line:
+ return False
+ st_line = candidates
+ _liquidity, suspense, _other = st_line._seek_for_lines()
+ AML = self.env['account.move.line'].with_context(skip_readonly_check=True, check_move_validity=False)
+ counterpart = AML.create({
+ 'move_id': st_line.move_id.id, 'name': self.name, 'account_id': store.payout_clearing_account_id.id,
+ 'balance': -self.amount, 'partner_id': False,
+ })
+ suspense.with_context(force_delete=True, skip_readonly_check=True, check_move_validity=False).unlink()
+ (counterpart | clearing_line).reconcile()
+ st_line.shopify_payout_id = self
+ self.bank_line_id = st_line
+ return True
+
+ def action_match_bank(self):
+ for payout in self.filtered(lambda p: p.state == 'done' and not p.bank_line_id):
+ if not payout._shopify_match_bank():
+ raise UserError(_("No single bank transaction of %(amount)s around %(date)s in %(journal)s.",
+ amount=payout.currency_id.format(payout.amount), date=payout.date,
+ journal=payout.store_id.payout_journal_id.display_name))
+
+ def action_open_move(self):
+ self.ensure_one()
+ return {'type': 'ir.actions.act_window', 'res_model': 'account.move', 'res_id': self.move_id.id,
+ 'view_mode': 'form'}
+
+ def _shopify_import_transactions(self):
+ """ One statement line per order charge or refund, with its customer, and one line for all the
+ fees; Odoo matches them with the invoices and payments in the bank reconciliation. """
+ self.ensure_one()
+ store = self.store_id
+ StatementLine = self.env['account.bank.statement.line']
+ payout_id = self.shopify_gid.rsplit('/', 1)[-1]
+ fees = 0.0
+ lines = []
+ for node in store._shopify_paginate(queries.BALANCE_TRANSACTIONS, {'query': f'payout_id:{payout_id}'},
+ 'shopifyPaymentsAccount.balanceTransactions'):
+ if node.get('type') == 'PAYOUT':
+ continue
+ amount = float((node.get('amount') or {}).get('amount') or 0.0)
+ fees += float((node.get('fee') or {}).get('amount') or 0.0)
+ order = self.env['sale.order']
+ if gid := (node.get('associatedOrder') or {}).get('id'):
+ order = self.env['sale.order'].search([('shopify_store_id', '=', store.id),
+ ('shopify_order_gid', '=', gid)], limit=1)
+ lines.append({
+ 'journal_id': store.payout_journal_id.id,
+ 'date': (node.get('transactionDate') or '')[:10] or self.date,
+ 'amount': amount,
+ 'payment_ref': ' '.join(filter(None, [
+ node.get('type', '').replace('_', ' ').capitalize(),
+ (node.get('associatedOrder') or {}).get('name'),
+ order.invoice_ids.filtered(lambda m: m.state == 'posted')[:1].name,
+ ])),
+ 'partner_id': order.partner_id.commercial_partner_id.id or False,
+ 'shopify_payout_id': self.id,
+ })
+ if fees:
+ lines.append({
+ 'journal_id': store.payout_journal_id.id,
+ 'date': self.date,
+ 'amount': -fees,
+ 'payment_ref': _("Shopify Payments fees"),
+ 'shopify_payout_id': self.id,
+ })
+ StatementLine.create(lines)
+ self.state = 'done'
+
+
+class AccountBankStatementLine(models.Model):
+ _inherit = 'account.bank.statement.line'
+
+ shopify_payout_id = fields.Many2one('om.shopify.payout', string='Shopify Payout', readonly=True,
+ index='btree_not_null', ondelete='set null')
diff --git a/om_shopify/models/om_shopify_product.py b/om_shopify/models/om_shopify_product.py
new file mode 100644
index 00000000..e342ec61
--- /dev/null
+++ b/om_shopify/models/om_shopify_product.py
@@ -0,0 +1,465 @@
+import base64
+import hashlib
+import logging
+import math
+
+import requests
+
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError
+
+from ..tools import queries
+
+_logger = logging.getLogger(__name__)
+
+DEFAULT_OPTION = ('Title', 'Default Title')
+WEIGHT_TO_KG = {'KILOGRAMS': 1.0, 'GRAMS': 0.001, 'POUNDS': 0.45359237, 'OUNCES': 0.028349523125}
+MAX_IMAGE_BYTES = 10 * 1024 * 1024
+
+
+class OmShopifyProductTemplate(models.Model):
+ """ A Shopify product, linked to an Odoo product template. """
+ _name = 'om.shopify.product.template'
+ _description = 'Shopify Product'
+ _inherit = ['om.connector.binding.mixin']
+ _order = 'store_id, name'
+ _connector_backend_field = 'store_id'
+ _connector_record_field = 'product_tmpl_id'
+
+ name = fields.Char(readonly=True)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ product_tmpl_id = fields.Many2one('product.template', string='Product', required=True, ondelete='cascade',
+ index=True)
+ shopify_status = fields.Selection([
+ ('ACTIVE', 'Active'), ('DRAFT', 'Draft'), ('ARCHIVED', 'Archived'), ('UNLISTED', 'Unlisted'),
+ ('deleted', 'Deleted'),
+ ], string='Status in Shopify', readonly=True)
+ handle = fields.Char(readonly=True)
+ variant_ids = fields.One2many('om.shopify.product', 'template_binding_id', string='Variants')
+ active = fields.Boolean(default=True)
+ image_checksum = fields.Char(readonly=True, copy=False, help="Checksum of the last image sent to Shopify.")
+
+ _sql_constraints = [
+ ('store_external_unique', 'unique (store_id, external_id)',
+ "A Shopify product is linked once per store."),
+ ]
+
+ def action_shopify_export(self):
+ for binding in self:
+ binding.store_id._connector_enqueue(
+ '_connector_job_shopify_export_product',
+ {'template_id': binding.product_tmpl_id.id, 'at': fields.Datetime.to_string(fields.Datetime.now())},
+ model_name='om.shopify.product.template', name=_("Export %(product)s", product=binding.name),
+ dedupe_key=f'export:{binding.product_tmpl_id.id}')
+ return {'type': 'ir.actions.client', 'tag': 'display_notification',
+ 'params': {'message': _("Export queued."), 'type': 'info'}}
+
+ def action_open_in_shopify(self):
+ self.ensure_one()
+ return {
+ 'type': 'ir.actions.act_url',
+ 'url': f'https://{self.store_id.shop_domain}/admin/products/{self.external_id.rsplit("/", 1)[-1]}',
+ 'target': 'new',
+ }
+
+ # ------------------------------------------------------------------
+ # Import
+ # ------------------------------------------------------------------
+
+ @api.model
+ def _connector_job_shopify_import_product(self, job):
+ return self._shopify_import_product(job.backend, job.payload['id'], data=job.payload.get('data'))
+
+ @api.model
+ def _connector_job_shopify_product_webhook(self, job):
+ gid = job.payload.get('admin_graphql_api_id') or f"gid://shopify/Product/{job.payload['id']}"
+ return self._shopify_import_product(job.backend, gid, from_webhook=True)
+
+ @api.model
+ def _connector_job_shopify_product_deleted(self, job):
+ gid = job.payload.get('admin_graphql_api_id') or f"gid://shopify/Product/{job.payload['id']}"
+ bindings = self.with_context(active_test=False)._connector_find(job.backend, gid)
+ bindings.write({'shopify_status': 'deleted', 'active': False})
+ bindings.variant_ids.write({'active': False})
+ return _("%(count)s product(s) unlinked", count=len(bindings))
+
+ @api.model
+ def _shopify_fetch_product(self, store, gid):
+ """ The product ``gid`` with all its variants, or None if it does not exist anymore. """
+ product, cursor = None, None
+ while True:
+ data = store._shopify_execute(store._shopify_query(queries.PRODUCT), {'id': gid, 'cursor': cursor}).get('product')
+ if not data:
+ return product
+ variants = data.pop('variants')
+ if product is None:
+ product = dict(data, variants=[])
+ product['variants'] += variants.get('nodes') or []
+ if not (variants.get('pageInfo') or {}).get('hasNextPage'):
+ return product
+ cursor = variants['pageInfo']['endCursor']
+
+ @api.model
+ def _shopify_import_product(self, store, gid, from_webhook=False, data=None):
+ """ Import or update the Shopify product ``gid`` (``data``: given by a bulk operation, else
+ read from Shopify): find or create the Odoo product of each variant, and link them. Gives the
+ linked template. """
+ data = data or self._shopify_fetch_product(store, gid)
+ if not data:
+ return _("Product %(id)s does not exist anymore", id=gid)
+ binding = self.with_context(active_test=False)._connector_find(store, gid)
+ variant_bindings = {b.external_id: b for b in binding.with_context(active_test=False).variant_ids}
+ Variant = self.env['om.shopify.product']
+ products = {}
+ for variant in data['variants']:
+ product = variant_bindings[variant['id']].product_id if variant['id'] in variant_bindings else None
+ products[variant['id']] = product or Variant._shopify_match(store, variant)
+ missing = [v for v in data['variants'] if not products[v['id']]]
+ if missing:
+ if not store.product_auto_create:
+ if from_webhook and not binding:
+ return _("No Odoo product for %(name)s: not imported", name=data['title'])
+ raise UserError(_(
+ "No Odoo product has the SKU or barcode of these variants of %(name)s: %(variants)s. Create them, "
+ "or allow the store to create the products.", name=data['title'],
+ variants=', '.join(v.get('sku') or v.get('barcode') or v['title'] for v in missing)))
+ template = next((p.product_tmpl_id for p in products.values() if p), None) or binding.product_tmpl_id
+ products.update(self._shopify_create_products(store, data, missing, template or None))
+ templates = self.env['product.template'].union(*(p.product_tmpl_id for p in products.values()))
+ template = binding.product_tmpl_id if binding.product_tmpl_id in templates else templates[:1]
+ vals = {
+ 'name': data['title'],
+ 'shopify_status': data.get('status'),
+ 'handle': data.get('handle'),
+ 'active': True,
+ }
+ binding = self._connector_bind(store, gid, template, vals)
+ store._shopify_apply_metafields('product', template, data.get('metafields'))
+ for variant in data['variants']:
+ Variant._shopify_bind_variant(store, binding, variant, products[variant['id']])
+ store._shopify_apply_metafields('variant', products[variant['id']], variant.get('metafields'))
+ stale = binding.with_context(active_test=False).variant_ids.filtered(
+ lambda b: b.external_id not in {v['id'] for v in data['variants']})
+ stale.write({'active': False})
+ if store.product_import_images:
+ self._shopify_import_images(store, template, data, products)
+ return binding
+
+ @api.model
+ def _shopify_create_products(self, store, data, variants, template=None):
+ """ Create the Odoo products of ``variants`` of the Shopify product ``data``; in ``template``
+ when given. Gives ``{variant id: product.product}``. """
+ options = [o for o in sorted(data.get('options') or [], key=lambda o: o.get('position') or 0)
+ if (o['name'], tuple(v['name'] for v in o.get('optionValues') or [])) != (DEFAULT_OPTION[0], (DEFAULT_OPTION[1],))]
+ first = variants[0]
+ tracked = any((v.get('inventoryItem') or {}).get('tracked') for v in data['variants'])
+ new_template = template is None
+ if new_template:
+ template = self.env['product.template'].create({
+ 'name': data['title'],
+ 'type': 'consu',
+ 'is_storable': tracked,
+ 'list_price': float(first.get('price') or 0.0),
+ 'description_sale': False,
+ 'company_id': False,
+ 'default_code': first.get('sku') or False if not options else False,
+ 'barcode': self.env['om.shopify.product']._shopify_free_barcode(first.get('barcode')) if not options else False,
+ })
+ if not options:
+ product = template.product_variant_ids[:1]
+ self.env['om.shopify.product']._shopify_update_variant(product, first)
+ return {first['id']: product}
+ variants_before = template.product_variant_ids if not new_template else self.env['product.product']
+ # one attribute per option; a new attribute only makes the variants asked for
+ Attribute = self.env['product.attribute']
+ attribute_values = {}
+ for option in options:
+ attribute = Attribute.search([('name', '=ilike', option['name']), ('create_variant', '!=', 'no_variant')],
+ limit=1) or Attribute.create(
+ {'name': option['name'], 'create_variant': 'dynamic'})
+ values = attribute_values.setdefault(option['name'], {'attribute': attribute, 'values': {}})
+ for value_name in [v['name'] for v in option.get('optionValues') or []]:
+ value = attribute.value_ids.filtered(lambda v: v.name.lower() == value_name.lower())[:1] or \
+ self.env['product.attribute.value'].create({'attribute_id': attribute.id, 'name': value_name})
+ values['values'][value_name] = value
+ for option_name, info in attribute_values.items():
+ line = template.attribute_line_ids.filtered(lambda l: l.attribute_id == info['attribute'])
+ wanted = self.env['product.attribute.value'].union(*info['values'].values())
+ if line:
+ line.value_ids = [fields.Command.link(v.id) for v in wanted]
+ else:
+ template.attribute_line_ids = [fields.Command.create({
+ 'attribute_id': info['attribute'].id,
+ 'value_ids': [fields.Command.set(wanted.ids)],
+ })]
+ result = {}
+ for variant in variants:
+ ptavs = self.env['product.template.attribute.value']
+ for selected in variant.get('selectedOptions') or []:
+ info = attribute_values.get(selected['name'])
+ if not info:
+ continue
+ value = info['values'].get(selected['value'])
+ ptavs |= template.attribute_line_ids.product_template_value_ids.filtered(
+ lambda v, value=value: v.product_attribute_value_id == value)
+ product = template._create_product_variant(ptavs) if ptavs else template.product_variant_ids[:1]
+ if not product:
+ raise UserError(_("The variant %(variant)s of %(product)s cannot be created in Odoo.",
+ variant=variant['title'], product=data['title']))
+ self.env['om.shopify.product']._shopify_update_variant(product, variant)
+ result[variant['id']] = product
+ # attributes that make every combination create variants Shopify does not have: removed,
+ # as long as they were just made here
+ made = self.env['product.product'].union(*result.values())
+ bound = self.env['om.shopify.product'].with_context(active_test=False).search(
+ [('product_id', 'in', template.with_context(active_test=False).product_variant_ids.ids)]).product_id
+ (template.product_variant_ids - variants_before - made - bound).unlink()
+ return result
+
+ @api.model
+ def _shopify_download(self, url):
+ if not url:
+ return None
+ try:
+ response = requests.get(url, timeout=15)
+ except requests.RequestException as e:
+ _logger.info("Image %s not downloaded: %s", url, e)
+ return None
+ if response.ok and response.content and len(response.content) <= MAX_IMAGE_BYTES:
+ return base64.b64encode(response.content)
+ return None
+
+ @api.model
+ def _shopify_import_images(self, store, template, data, products):
+ """ The main image, the other images (in the eCommerce gallery when it is installed) and the
+ image of each variant, for the Odoo products without one. """
+ def url_of(media):
+ return (((media or {}).get('preview') or {}).get('image') or {}).get('url')
+
+ main_url = url_of(data.get('featuredMedia'))
+ if not template.image_1920 and (image := self._shopify_download(main_url)):
+ template.image_1920 = image
+ if 'product.image' in self.env and 'product_tmpl_id' in self.env['product.image']._fields \
+ and not template.product_template_image_ids:
+ for media in (data.get('media') or {}).get('nodes') or []:
+ url = url_of(media)
+ if url and url != main_url and (image := self._shopify_download(url)):
+ self.env['product.image'].create({'product_tmpl_id': template.id, 'name': template.name,
+ 'image_1920': image})
+ if len(products) > 1:
+ for variant in data['variants']:
+ product = products.get(variant['id'])
+ url = url_of(((variant.get('media') or {}).get('nodes') or [None])[0])
+ if product and url and url != main_url and not product.image_variant_1920 \
+ and (image := self._shopify_download(url)):
+ product.image_variant_1920 = image
+
+ # ------------------------------------------------------------------
+ # Export
+ # ------------------------------------------------------------------
+
+ @api.model
+ def _connector_job_shopify_export_product(self, job):
+ template = self.env['product.template'].browse(job.payload['template_id']).exists()
+ if not template:
+ return _("The product does not exist anymore")
+ return self._shopify_export_product(job.backend, template, job.payload.get('images', True))
+
+ @api.model
+ def _shopify_option_lines(self, template):
+ return template.attribute_line_ids.filtered(lambda l: l.attribute_id.create_variant != 'no_variant')
+
+ @api.model
+ def _shopify_variant_options(self, product, lines):
+ values = product.product_template_attribute_value_ids
+ return [{'optionName': line.attribute_id.name,
+ 'name': values.filtered(lambda v, line=line: v.attribute_line_id == line)[:1].name}
+ for line in lines]
+
+ @api.model
+ def _shopify_export_product(self, store, template, images=True):
+ """ Create or update ``template`` in Shopify, with its variants, prices, weight and image.
+ Odoo is the master of the products it exports: the variants of Shopify that are not in Odoo
+ are removed. Gives the binding. """
+ binding = self.with_context(active_test=False).search(
+ [('store_id', '=', store.id), ('product_tmpl_id', '=', template.id)], limit=1)
+ lines = self._shopify_option_lines(template)
+ products = template.product_variant_ids
+ if len(products) > 2048:
+ raise UserError(_("Shopify has at most 2048 variants per product: %(product)s has %(count)s.",
+ product=template.name, count=len(products)))
+ if lines:
+ used = products.product_template_attribute_value_ids
+ options = [{'name': line.attribute_id.name, 'position': position,
+ 'values': [{'name': v.name} for v in line.product_template_value_ids if v in used]}
+ for position, line in enumerate(lines, 1)]
+ else:
+ options = [{'name': DEFAULT_OPTION[0], 'position': 1, 'values': [{'name': DEFAULT_OPTION[1]}]}]
+ variants = []
+ for product in products:
+ price, compare_at = store._shopify_prices(product)
+ variant = {
+ 'optionValues': self._shopify_variant_options(product, lines) if lines
+ else [{'optionName': DEFAULT_OPTION[0], 'name': DEFAULT_OPTION[1]}],
+ 'price': f'{price:.2f}',
+ 'inventoryPolicy': 'DENY',
+ 'inventoryItem': {
+ 'sku': product.default_code or '',
+ 'tracked': bool(product.is_storable),
+ 'measurement': {'weight': {'unit': 'KILOGRAMS', 'value': product.weight or 0.0}},
+ },
+ }
+ if compare_at:
+ variant['compareAtPrice'] = f'{compare_at:.2f}'
+ if product.barcode:
+ variant['barcode'] = product.barcode
+ if metafields := store._shopify_metafield_inputs('variant', product):
+ variant['metafields'] = metafields
+ variants.append(variant)
+ product_input = {
+ 'title': template.name,
+ 'descriptionHtml': template.description_sale and str(template.description_sale).replace('\n', ' ') or '',
+ 'vendor': template.seller_ids[:1].partner_id.name or store.shop_name or '',
+ 'productType': template.categ_id.name or '',
+ 'status': store.product_export_status,
+ 'productOptions': options,
+ 'variants': variants,
+ }
+ if metafields := store._shopify_metafield_inputs('product', template):
+ product_input['metafields'] = metafields
+ if images and template.image_1920:
+ checksum = hashlib.sha1(base64.b64decode(template.image_1920)).hexdigest()
+ if not binding or binding.image_checksum != checksum:
+ product_input['files'] = [{'originalSource': store._shopify_upload_image(template),
+ 'alt': template.name, 'contentType': 'IMAGE'}]
+ else:
+ checksum = binding.image_checksum if binding else False
+ variables = {'input': product_input}
+ if binding:
+ variables['identifier'] = {'id': binding.external_id}
+ result = store._shopify_mutate(queries.PRODUCT_SET, variables, 'productSet')
+ data = result.get('product') or {}
+ vals = {'name': template.name, 'shopify_status': data.get('status'), 'handle': data.get('handle'),
+ 'active': True}
+ if images and template.image_1920:
+ vals['image_checksum'] = checksum
+ binding = self._connector_bind(store, data['id'], template, vals)
+ by_options = {
+ tuple((o['optionName'], o['name']) for o in variant['optionValues']): product
+ for variant, product in zip(variants, products)
+ }
+ Variant = self.env['om.shopify.product']
+ for node in (data.get('variants') or {}).get('nodes') or []:
+ key = tuple((o['name'], o['value']) for o in node.get('selectedOptions') or [])
+ product = by_options.get(key)
+ if product:
+ Variant._shopify_bind_variant(store, binding, {
+ 'id': node['id'], 'title': ' / '.join(k[1] for k in key), 'sku': node.get('sku'),
+ 'barcode': product.barcode, 'inventoryItem': node.get('inventoryItem') or {},
+ }, product)
+ if store.product_publish:
+ store._shopify_publish(data['id'])
+ return binding
+
+
+class OmShopifyProduct(models.Model):
+ """ A variant of a Shopify product, linked to an Odoo product. """
+ _name = 'om.shopify.product'
+ _description = 'Shopify Variant'
+ _inherit = ['om.connector.binding.mixin']
+ _order = 'store_id, template_binding_id, id'
+ _connector_backend_field = 'store_id'
+ _connector_record_field = 'product_id'
+
+ name = fields.Char(readonly=True)
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ template_binding_id = fields.Many2one('om.shopify.product.template', string='Shopify Product',
+ ondelete='cascade', index=True)
+ product_id = fields.Many2one('product.product', string='Product', required=True, ondelete='cascade', index=True)
+ sku = fields.Char(string='SKU', readonly=True)
+ barcode = fields.Char(readonly=True)
+ inventory_item_gid = fields.Char(string='Inventory Item', readonly=True)
+ tracked = fields.Boolean(string='Stock Tracked in Shopify', readonly=True)
+ active = fields.Boolean(default=True)
+ stock_rule = fields.Selection([
+ ('actual', 'Actual Stock'),
+ ('max', 'At Most'),
+ ('percent', 'Percentage'),
+ ('buffer', 'Keep Back'),
+ ('fixed', 'Fixed Quantity'),
+ ], string='Stock Sent', default='actual', required=True,
+ help="Actual Stock: the stock of the warehouse.\nAt Most: the stock, at most the value.\n"
+ "Percentage: this percent of the stock.\nKeep Back: the stock minus the value.\n"
+ "Fixed Quantity: always the value, whatever the stock.")
+ stock_value = fields.Float(string='Value')
+
+ def _shopify_stock_quantity(self, quantity):
+ self.ensure_one()
+ quantity = max(quantity, 0.0)
+ if self.stock_rule == 'max':
+ quantity = min(quantity, self.stock_value)
+ elif self.stock_rule == 'percent':
+ quantity = math.floor(quantity * self.stock_value / 100.0)
+ elif self.stock_rule == 'buffer':
+ quantity = quantity - self.stock_value
+ elif self.stock_rule == 'fixed':
+ quantity = self.stock_value
+ return max(int(quantity), 0)
+
+ _sql_constraints = [
+ ('store_external_unique', 'unique (store_id, external_id)',
+ "A Shopify variant is linked once per store."),
+ ]
+
+ @api.model
+ def _shopify_match(self, store, variant):
+ """ The Odoo product with the SKU or the barcode of ``variant``, as the store matches them. """
+ Product = self.env['product.product'].with_context(active_test=False)
+ domain_company = [('company_id', 'in', [False, store.company_id.id])]
+ sku, barcode = (variant.get('sku') or '').strip(), (variant.get('barcode') or '').strip()
+ if sku and store.product_match in ('sku', 'sku_barcode'):
+ products = Product.search([('default_code', '=', sku)] + domain_company, limit=2)
+ if len(products) == 1:
+ return products
+ if barcode and store.product_match in ('barcode', 'sku_barcode'):
+ products = Product.search([('barcode', '=', barcode)] + domain_company, limit=2)
+ if len(products) == 1:
+ return products
+ return None
+
+ @api.model
+ def _shopify_free_barcode(self, barcode):
+ barcode = (barcode or '').strip()
+ if barcode and not self.env['product.product'].with_context(active_test=False).search_count(
+ [('barcode', '=', barcode)], limit=1):
+ return barcode
+ return False
+
+ @api.model
+ def _shopify_update_variant(self, product, variant):
+ """ Fill the reference, barcode and weight of a product made from ``variant``. """
+ vals = {}
+ if variant.get('sku') and not product.default_code:
+ vals['default_code'] = variant['sku']
+ if not product.barcode and (barcode := self._shopify_free_barcode(variant.get('barcode'))):
+ vals['barcode'] = barcode
+ weight = ((variant.get('inventoryItem') or {}).get('measurement') or {}).get('weight') or {}
+ if weight.get('value'):
+ vals['weight'] = float(weight['value']) * WEIGHT_TO_KG.get(weight.get('unit'), 1.0)
+ if vals:
+ product.write(vals)
+
+ @api.model
+ def _shopify_bind_variant(self, store, template_binding, variant, product):
+ item = variant.get('inventoryItem') or {}
+ return self._connector_bind(store, variant['id'], product, {
+ 'template_binding_id': template_binding.id,
+ 'name': variant.get('title') if variant.get('title') != DEFAULT_OPTION[1] else template_binding.name,
+ 'sku': variant.get('sku') or False,
+ 'barcode': variant.get('barcode') or False,
+ 'inventory_item_gid': item.get('id'),
+ 'tracked': bool(item.get('tracked')),
+ 'active': True,
+ })
diff --git a/om_shopify/models/om_shopify_store.py b/om_shopify/models/om_shopify_store.py
new file mode 100644
index 00000000..850fab58
--- /dev/null
+++ b/om_shopify/models/om_shopify_store.py
@@ -0,0 +1,1310 @@
+import base64
+import hashlib
+import json
+import logging
+import secrets
+import uuid
+from datetime import timedelta
+
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError, ValidationError
+from odoo.tools import SQL, float_round
+
+from odoo.addons.om_connector_base.tools import RetryableJobError, crypto, request_with_retry
+
+from ..tools import queries
+
+from ..tools.client import (
+ DEFAULT_API_VERSION,
+ DEFAULT_SCOPES,
+ ShopifyAuthError,
+ ShopifyError,
+ ShopifyClient,
+ is_valid_shop_domain,
+ normalize_shop_domain,
+)
+
+_logger = logging.getLogger(__name__)
+
+WEBHOOK_ROUTE = '/om_shopify/webhook/%s'
+STOCK_BATCH = 250
+
+SHOP_QUERY = """
+query OmShopifyShop {
+ shop { id name email myshopifyDomain currencyCode ianaTimezone }
+}
+"""
+
+WEBHOOKS_QUERY = """
+query OmShopifyWebhooks($cursor: String) {
+ webhookSubscriptions(first: 100, after: $cursor) {
+ nodes { id topic uri }
+ pageInfo { hasNextPage endCursor }
+ }
+}
+"""
+
+WEBHOOK_CREATE = """
+mutation OmShopifyWebhookCreate($topic: WebhookSubscriptionTopic!, $uri: String!) {
+ webhookSubscriptionCreate(topic: $topic, webhookSubscription: {uri: $uri, format: JSON}) {
+ webhookSubscription { id topic uri }
+ userErrors { field message }
+ }
+}
+"""
+
+WEBHOOK_DELETE = """
+mutation OmShopifyWebhookDelete($id: ID!) {
+ webhookSubscriptionDelete(id: $id) {
+ deletedWebhookSubscriptionId
+ userErrors { field message }
+ }
+}
+"""
+
+
+class OmShopifyStore(models.Model):
+ """ A Shopify store connected to Odoo. """
+ _name = 'om.shopify.store'
+ _description = 'Shopify Store'
+ _inherit = ['mail.thread', 'mail.activity.mixin', 'om.connector.backend.mixin']
+ _order = 'sequence, name'
+ _check_company_auto = True
+
+ name = fields.Char(required=True, tracking=True)
+ sequence = fields.Integer(default=10)
+ active = fields.Boolean(default=True)
+ color = fields.Integer()
+ company_id = fields.Many2one('res.company', required=True, default=lambda self: self.env.company, tracking=True)
+ shop_domain = fields.Char(
+ string='Store Address', required=True, tracking=True,
+ help="The myshopify.com address of the store, for example my-store.myshopify.com.")
+ state = fields.Selection([
+ ('draft', 'Not Connected'),
+ ('connected', 'Connected'),
+ ('error', 'Connection Error'),
+ ], default='draft', required=True, readonly=True, tracking=True)
+ last_error = fields.Text(readonly=True)
+
+ # Credentials
+ auth_type = fields.Selection([
+ ('client_credentials', 'Client ID and Secret (Dev Dashboard app)'),
+ ('oauth', 'Install by Link (OAuth)'),
+ ('token', 'Admin API Access Token (custom app)'),
+ ], string='Authentication', default='client_credentials', required=True,
+ help="Client ID and Secret: an app made in the Shopify Dev Dashboard and installed on a store of its own "
+ "organization; Odoo gets a new access token every day by itself.\n"
+ "Install by Link (OAuth): the merchant installs the app on the store from Shopify (public apps, or a "
+ "store of another organization); Odoo refreshes the token by itself.\n"
+ "Admin API Access Token: the token of a custom app made in the admin of the store (shpat_…).")
+ oauth_scopes = fields.Char(string='Scopes', default=lambda self: DEFAULT_SCOPES,
+ help="The access scopes asked to the merchant at the installation, separated by commas.")
+ oauth_granted_scopes = fields.Char(string='Granted Scopes', readonly=True, copy=False)
+ oauth_redirect_url = fields.Char(string='Redirection URL', compute='_compute_oauth_urls',
+ help="Allowed redirection URL to set in the app, in the Shopify Dev Dashboard.")
+ oauth_app_url = fields.Char(string='App URL', compute='_compute_oauth_urls',
+ help="App URL to set in the app: installing from Shopify starts here.")
+ refresh_token = fields.Char(groups='om_shopify.group_om_shopify_manager', copy=False,
+ compute='_compute_secrets', inverse='_inverse_refresh_token')
+ refresh_token_encrypted = fields.Char(groups='base.group_system', copy=False)
+ refresh_token_expires_at = fields.Datetime(string='Refresh Token Valid Until', readonly=True, copy=False,
+ groups='om_shopify.group_om_shopify_manager')
+ oauth_state_hash = fields.Char(groups='base.group_system', copy=False)
+ oauth_state_expires = fields.Datetime(groups='base.group_system', copy=False)
+ oauth_state_uid = fields.Many2one('res.users', groups='base.group_system', copy=False)
+ client_id = fields.Char(string='Client ID', groups='om_shopify.group_om_shopify_manager')
+ client_secret = fields.Char(
+ string='Client Secret', groups='om_shopify.group_om_shopify_manager',
+ compute='_compute_secrets', inverse='_inverse_client_secret',
+ help="Also called API secret key. Shopify signs its webhooks with it. Stored encrypted.")
+ access_token = fields.Char(string='Access Token', groups='om_shopify.group_om_shopify_manager', copy=False,
+ compute='_compute_secrets', inverse='_inverse_access_token',
+ help="Stored encrypted.")
+ client_secret_encrypted = fields.Char(groups='base.group_system', copy=False)
+ access_token_encrypted = fields.Char(groups='base.group_system', copy=False)
+ secret_key_source = fields.Selection([
+ ('server', 'Odoo configuration file'),
+ ('database', 'Database secret'),
+ ], string='Encryption Key', compute='_compute_secret_key_source',
+ help="Where the key that encrypts the secrets comes from. With the database secret, a copy of the database "
+ "is enough to read them: set om_connector_key in the Odoo configuration file for a stronger key.")
+ token_expires_at = fields.Datetime(groups='om_shopify.group_om_shopify_manager', copy=False, readonly=True)
+ api_version = fields.Char(
+ string='API Version', required=True, default=DEFAULT_API_VERSION,
+ help="Version of the Shopify Admin API. Shopify supports each version for one year.")
+
+ # Store details, read from Shopify
+ shop_gid = fields.Char(string='Shopify ID', readonly=True, copy=False)
+ shop_name = fields.Char(readonly=True, copy=False)
+ shop_email = fields.Char(readonly=True, copy=False)
+ shop_currency_id = fields.Many2one('res.currency', string='Store Currency', readonly=True, copy=False)
+ shop_timezone = fields.Char(string='Store Timezone', readonly=True, copy=False)
+
+ # Webhooks
+ webhook_base_url = fields.Char(
+ string='Odoo Public URL',
+ help="Public https address of this Odoo, where Shopify sends its webhooks. Empty: the web.base.url of Odoo.")
+ webhook_url = fields.Char(string='Webhook Address', compute='_compute_webhook_url')
+ webhook_ids = fields.One2many('om.shopify.webhook', 'store_id', string='Webhooks')
+ webhook_count = fields.Integer(compute='_compute_webhook_count')
+
+ # Odoo side
+ warehouse_id = fields.Many2one('stock.warehouse', required=True, check_company=True,
+ default=lambda self: self._default_warehouse(),
+ help="Warehouse of the imported orders.")
+ team_id = fields.Many2one('crm.team', string='Sales Team', check_company=True)
+ user_id = fields.Many2one('res.users', string='Salesperson', domain="[('share', '=', False)]")
+ location_ids = fields.One2many('om.shopify.location', 'store_id', string='Locations')
+ gateway_ids = fields.One2many('om.shopify.gateway', 'store_id', string='Payment Methods')
+
+ # Products
+ product_match = fields.Selection([
+ ('sku', 'SKU (Internal Reference)'),
+ ('barcode', 'Barcode'),
+ ('sku_barcode', 'SKU, then Barcode'),
+ ], string='Match Products On', default='sku_barcode', required=True,
+ help="How a Shopify variant finds its Odoo product the first time.")
+ product_auto_create = fields.Boolean(string='Create Missing Products', default=True,
+ help="Creates the Odoo product of a Shopify variant that matches none.")
+ product_import_images = fields.Boolean(string='Import Images', default=True,
+ help="Gives the image of the Shopify product to an Odoo product without one.")
+ product_auto_import = fields.Boolean(string='Import Products Every Day', default=True)
+ product_bulk_import = fields.Boolean(
+ string='Bulk Import', default=True,
+ help="Import All Products asks Shopify for the whole catalogue in one file (bulk operation), much faster "
+ "for large catalogues than page after page.")
+ product_last_sync = fields.Datetime(string='Products Imported On', readonly=True, copy=False)
+ product_count = fields.Integer(compute='_compute_counts', string='Products')
+
+ # Orders
+ order_auto_import = fields.Boolean(string='Import Orders Automatically', default=True,
+ help="Imports the new and updated orders every 15 minutes, besides the webhooks.")
+ order_import_from = fields.Datetime(
+ string='Import Orders From', default=lambda self: fields.Datetime.now(),
+ help="Orders updated before this date are not imported. Orders older than 60 days need the read_all_orders "
+ "scope.")
+ order_last_sync = fields.Datetime(string='Orders Imported On', readonly=True, copy=False)
+ order_import_test = fields.Boolean(string='Import Test Orders', default=True,
+ help="Imports the orders paid with the test gateways of Shopify.")
+ order_use_shopify_name = fields.Boolean(string='Use the Shopify Order Number',
+ help="Names the sale orders like in Shopify (#1001), instead of the Odoo sequence.")
+ order_prefix = fields.Char(string='Order Prefix', help="Put before the Shopify order number, e.g. SH.")
+ workflow_id = fields.Many2one('om.connector.order.workflow', string='Default Workflow', check_company=True,
+ help="Workflow of the orders of a new payment method.")
+ tax_mode = fields.Selection([
+ ('shopify', 'Taxes of Shopify'),
+ ('odoo', 'Taxes of Odoo'),
+ ], string='Order Taxes', default='shopify', required=True,
+ help="Taxes of Shopify: each line gets an Odoo tax of the rate charged by Shopify, found or created.\n"
+ "Taxes of Odoo: each line gets the taxes of its product and the fiscal position of the customer.")
+ shipping_product_id = fields.Many2one('product.product', string='Shipping Product', required=True,
+ default=lambda self: self.env.ref('om_shopify.product_shopify_shipping', False))
+ tip_product_id = fields.Many2one('product.product', string='Tip Product', required=True,
+ default=lambda self: self.env.ref('om_shopify.product_shopify_tip', False))
+ gift_card_product_id = fields.Many2one('product.product', string='Gift Card Product', required=True,
+ default=lambda self: self.env.ref('om_shopify.product_shopify_gift_card', False))
+ custom_product_id = fields.Many2one('product.product', string='Custom Item Product', required=True,
+ default=lambda self: self.env.ref('om_shopify.product_shopify_custom', False),
+ help="Product of the order lines without a product in Shopify.")
+ order_count = fields.Integer(compute='_compute_counts', string='Orders')
+ order_rule_ids = fields.One2many('om.shopify.order.rule', 'store_id', string='Order Rules')
+ order_currency = fields.Selection([
+ ('shop', 'Store Currency'),
+ ('customer', 'Currency of the Customer'),
+ ], string='Order Currency', default='shop', required=True,
+ help="Store Currency: the orders are in the currency of the store.\n"
+ "Currency of the Customer: the orders are in the currency the customer paid in (Shopify Markets).")
+ order_warehouse_from_location = fields.Boolean(
+ string='Warehouse of the Shopify Location',
+ help="An order gets the warehouse of the Shopify location that fulfills it, when it has one.")
+ order_deliver_fulfilled = fields.Boolean(
+ string='Deliver Fulfilled Orders', default=True,
+ help="An order already fulfilled in Shopify when imported has its delivery done in Odoo.")
+ order_sync_edits = fields.Boolean(
+ string='Apply Order Edits', default=True,
+ help="Lines added, removed or changed in Shopify are applied to the sale order, while it is neither "
+ "delivered nor invoiced.")
+ order_import_pos = fields.Boolean(string='Import Point of Sale Orders', default=True)
+ pos_partner_id = fields.Many2one('res.partner', string='Point of Sale Customer',
+ help="Customer of the point of sale orders without a customer.")
+ duties_product_id = fields.Many2one('product.product', string='Duties Product', required=True,
+ default=lambda self: self.env.ref('om_shopify.product_shopify_duties', False))
+ refund_adjustment_product_id = fields.Many2one(
+ 'product.product', string='Refund Adjustment Product', required=True,
+ default=lambda self: self.env.ref('om_shopify.product_shopify_refund_adjustment', False))
+ tax_account_id = fields.Many2one('account.account', string='Account of the New Taxes', check_company=True,
+ help="Account of the taxes that Odoo creates for the Shopify rates.")
+ gdpr_anonymize = fields.Boolean(
+ string='Anonymize Redacted Customers',
+ help="When Shopify asks to erase a customer (GDPR), the contact is anonymized and archived. Unticked: the "
+ "responsible gets an activity to decide, as invoices may have to be kept.")
+ responsible_id = fields.Many2one('res.users', string='Responsible', domain="[('share', '=', False)]",
+ help="Gets an activity when a job fails or an order is risky.")
+
+ # Refunds
+ refund_import = fields.Boolean(string='Import Refunds', default=True,
+ help="A refund made in Shopify becomes a credit note, paid back in the journal of "
+ "the payment method.")
+ refund_create_return = fields.Boolean(string='Return Restocked Products',
+ help="Products restocked by a Shopify refund are returned in Odoo.")
+ refund_validate_return = fields.Boolean(string='Validate the Return')
+ refund_restock_type = fields.Selection([
+ ('NO_RESTOCK', 'No Restock'),
+ ('RETURN', 'Return to Stock'),
+ ('CANCEL', 'Cancel (not shipped yet)'),
+ ], string='Restock of Odoo Refunds', default='NO_RESTOCK', required=True,
+ help="What Shopify does with the stock of the products of a credit note refunded from Odoo.")
+ refund_notify = fields.Boolean(string='Email the Refunds to the Customer')
+
+ # Product export and prices
+ product_export_status = fields.Selection([
+ ('ACTIVE', 'Active'), ('DRAFT', 'Draft'), ('UNLISTED', 'Unlisted'),
+ ], string='Status of Exported Products', default='DRAFT', required=True)
+ product_publish = fields.Boolean(string='Publish on the Online Store',
+ help="Exported products are published on the Online Store sales channel "
+ "(scope write_publications).")
+ price_pricelist_id = fields.Many2one('product.pricelist', string='Price List', check_company=True,
+ help="Prices sent to Shopify. Empty: the sales price of the products.")
+ compare_pricelist_id = fields.Many2one('product.pricelist', string='Compare-at Price List', check_company=True,
+ help="Compare-at prices sent to Shopify, when higher than the price.")
+ price_auto_export = fields.Boolean(string='Send Prices Every Day',
+ help="Sends the prices of the linked products to Shopify every day.")
+
+ # Stock import, customers, payouts
+ stock_import_apply = fields.Boolean(string='Apply the Imported Stock',
+ help="Applies the quantities imported from Shopify at once, instead of "
+ "leaving them to count in the physical inventory.")
+ customer_last_sync = fields.Datetime(string='Customers Imported On', readonly=True, copy=False)
+ payout_import = fields.Boolean(string='Import Payouts',
+ help="Imports the payouts of Shopify Payments every day as bank statement lines.")
+ payout_journal_id = fields.Many2one('account.journal', string='Payout Journal', check_company=True,
+ domain="[('type', '=', 'bank')]")
+ payout_last_sync = fields.Datetime(string='Payouts Imported On', readonly=True, copy=False)
+ payout_mode = fields.Selection([
+ ('statement', 'Bank statement lines'),
+ ('entry', 'Journal entry through a transit account'),
+ ], string='Payouts As', default='statement', required=True,
+ help="Journal entry: the payments of Shopify Payments wait on a transit account; each payout is one entry "
+ "that clears them, books the fees, and is matched with the bank transaction.\n"
+ "Bank statement lines: each charge, refund and the fees become lines of the payout journal, to "
+ "reconcile by hand.")
+ payout_transit_account_id = fields.Many2one(
+ 'account.account', string='Transit Account', check_company=True,
+ help="Where the payments of Shopify Payments wait until their payout. Use it as the outstanding account of "
+ "the journal of these payments.")
+ payout_fee_account_id = fields.Many2one('account.account', string='Fee Account', check_company=True)
+ payout_adjustment_account_id = fields.Many2one('account.account', string='Adjustment Account', check_company=True,
+ help="Chargebacks, reserves and other adjustments of the payouts.")
+ payout_clearing_account_id = fields.Many2one(
+ 'account.account', string='Clearing Account', check_company=True,
+ help="The net amount of a payout waits here until the bank transaction clears it: the internal transfer "
+ "account by default.")
+ payout_entry_journal_id = fields.Many2one('account.journal', string='Payout Entries Journal', check_company=True,
+ domain="[('type', '=', 'general')]")
+ payout_ids = fields.One2many('om.shopify.payout', 'store_id', string='Payouts')
+
+ # Stock and fulfillment
+ stock_export = fields.Boolean(string='Send Stock to Shopify', default=True,
+ help="Sends the stock of the warehouses of the locations every 15 minutes.")
+ stock_field = fields.Selection([
+ ('free_qty', 'Free to Use (on hand minus reserved)'),
+ ('qty_available', 'On Hand'),
+ ('virtual_available', 'Forecasted'),
+ ], string='Stock Sent', default='free_qty', required=True)
+ stock_last_sync = fields.Datetime(string='Stock Sent On', readonly=True, copy=False)
+ fulfillment_push = fields.Boolean(string='Fulfill in Shopify', default=True,
+ help="When a delivery of a Shopify order is done, fulfills it in Shopify with the "
+ "tracking number.")
+ fulfillment_notify = fields.Boolean(string='Email the Customer', default=True,
+ help="Shopify emails the shipping confirmation to the customer.")
+
+ _sql_constraints = [
+ ('shop_domain_company_unique', 'unique (shop_domain, company_id)', "This store is already connected in this company."),
+ ]
+
+ @api.model
+ def _default_warehouse(self):
+ return self.env['stock.warehouse'].search([('company_id', '=', self.env.company.id)], limit=1)
+
+ def _compute_counts(self):
+ products = dict(self.env['om.shopify.product.template']._read_group(
+ [('store_id', 'in', self.ids)], ['store_id'], ['__count']))
+ orders = dict(self.env['sale.order']._read_group(
+ [('shopify_store_id', 'in', self.ids)], ['shopify_store_id'], ['__count']))
+ for store in self:
+ store.product_count = products.get(store, 0)
+ store.order_count = orders.get(store, 0)
+
+ @api.depends('client_secret_encrypted', 'access_token_encrypted', 'refresh_token_encrypted')
+ def _compute_secrets(self):
+ for store in self:
+ sudo = store.sudo()
+ store.client_secret = crypto.decrypt(self.env, sudo.client_secret_encrypted)
+ store.access_token = crypto.decrypt(self.env, sudo.access_token_encrypted)
+ store.refresh_token = crypto.decrypt(self.env, sudo.refresh_token_encrypted)
+
+ def _inverse_refresh_token(self):
+ for store in self:
+ store.sudo().refresh_token_encrypted = crypto.encrypt(self.env, store.refresh_token)
+
+ @api.depends('webhook_base_url')
+ def _compute_oauth_urls(self):
+ base_url = self.get_base_url()
+ for store in self:
+ root = (store.webhook_base_url or base_url).rstrip('/')
+ store.oauth_redirect_url = f'{root}/om_shopify/oauth/callback'
+ store.oauth_app_url = f'{root}/om_shopify/oauth/install'
+
+ def _inverse_client_secret(self):
+ for store in self:
+ store.sudo().client_secret_encrypted = crypto.encrypt(self.env, store.client_secret)
+
+ def _inverse_access_token(self):
+ for store in self:
+ store.sudo().access_token_encrypted = crypto.encrypt(self.env, store.access_token)
+
+ def _compute_secret_key_source(self):
+ source = crypto.key_source(self.env)
+ for store in self:
+ store.secret_key_source = source
+
+ @api.depends('webhook_base_url')
+ def _compute_webhook_url(self):
+ base_url = self.get_base_url()
+ for store in self:
+ root = (store.webhook_base_url or base_url).rstrip('/')
+ store.webhook_url = f'{root}{WEBHOOK_ROUTE % store.id}' if store.id else False
+
+ @api.depends('webhook_ids')
+ def _compute_webhook_count(self):
+ for store in self:
+ store.webhook_count = len(store.webhook_ids)
+
+ @api.onchange('shop_domain')
+ def _onchange_shop_domain(self):
+ if self.shop_domain:
+ self.shop_domain = normalize_shop_domain(self.shop_domain)
+ if not self.name:
+ self.name = self.shop_domain.split('.')[0].replace('-', ' ').title()
+
+ @api.constrains('shop_domain')
+ def _check_shop_domain(self):
+ for store in self:
+ if not is_valid_shop_domain(store.shop_domain):
+ raise ValidationError(_("%(domain)s is not a store address: it looks like my-store.myshopify.com.",
+ domain=store.shop_domain))
+
+ @api.model_create_multi
+ def create(self, vals_list):
+ for vals in vals_list:
+ if vals.get('shop_domain'):
+ vals['shop_domain'] = normalize_shop_domain(vals['shop_domain'])
+ stores = super().create(vals_list)
+ for store in stores.filtered(lambda s: not s.workflow_id):
+ store.workflow_id = store._shopify_default_workflow()
+ return stores
+
+ def _shopify_default_workflow(self):
+ self.ensure_one()
+ journal = self.env['account.journal'].search(
+ [('type', '=', 'bank'), ('company_id', '=', self.company_id.id)], limit=1)
+ return self.env['om.connector.order.workflow'].sudo().create({
+ 'name': _("%(store)s: paid orders", store=self.name),
+ 'company_id': self.company_id.id,
+ 'journal_id': journal.id,
+ 'register_payment': bool(journal),
+ })
+
+ def write(self, vals):
+ if vals.get('shop_domain'):
+ vals['shop_domain'] = normalize_shop_domain(vals['shop_domain'])
+ res = super().write(vals)
+ if {'shop_domain', 'auth_type', 'client_id', 'client_secret'} & set(vals):
+ # a token of other credentials is not used anymore
+ self.sudo().filtered(lambda s: s.auth_type == 'client_credentials').write(
+ {'access_token': False, 'token_expires_at': False})
+ return res
+
+ # ------------------------------------------------------------------
+ # API
+ # ------------------------------------------------------------------
+
+ def _shopify_token(self, refresh=False):
+ """ The access token, a new one when it is expired (client ID and secret, OAuth). """
+ self.ensure_one()
+ store = self.sudo()
+ if store.auth_type == 'oauth':
+ if not store.access_token:
+ raise UserError(_("Click Connect with Shopify on %(store)s to install the app.", store=store.name))
+ expired = store.token_expires_at and store.token_expires_at <= fields.Datetime.now() + timedelta(minutes=2)
+ if store.refresh_token and (refresh or expired):
+ self._shopify_refresh_oauth(force=refresh)
+ store.invalidate_recordset(['access_token', 'access_token_encrypted', 'token_expires_at'])
+ return store.access_token
+ if store.auth_type == 'token':
+ if not store.access_token:
+ raise UserError(_("Set the access token of %(store)s.", store=store.name))
+ return store.access_token
+ if not store.client_id or not store.client_secret:
+ raise UserError(_("Set the client ID and secret of %(store)s.", store=store.name))
+ now = fields.Datetime.now()
+ if refresh or not store.access_token or not store.token_expires_at or store.token_expires_at <= now:
+ token, seconds = ShopifyClient.fetch_client_credentials_token(
+ store.shop_domain, store.client_id, store.client_secret)
+ # renewed one hour before it expires
+ store.write({'access_token': token,
+ 'token_expires_at': now + timedelta(seconds=max(seconds - 3600, 60))})
+ return store.access_token
+
+ def _shopify_oauth_token_vals(self, data):
+ """ The values of a store from the token answer of Shopify. """
+ now = fields.Datetime.now()
+ vals = {'access_token': data['access_token'],
+ 'token_expires_at': now + timedelta(seconds=int(data['expires_in'])) if data.get('expires_in') else False}
+ if data.get('refresh_token'):
+ vals['refresh_token'] = data['refresh_token']
+ vals['refresh_token_expires_at'] = now + timedelta(seconds=int(data.get('refresh_token_expires_in') or 0)) \
+ if data.get('refresh_token_expires_in') else False
+ if data.get('scope'):
+ vals['oauth_granted_scopes'] = data['scope']
+ return vals
+
+ def _shopify_refresh_oauth(self, force=False):
+ """ Get a new access token with the refresh token. Shopify gives a new refresh token and
+ retires the old one at once: both are saved in a transaction of their own, under a lock, so that
+ a rollback of the current work, or another worker refreshing at the same time, never loses them. """
+ self.ensure_one()
+ refused = None
+ with self.env.registry.cursor() as cr:
+ env = self.env(cr=cr, su=True)
+ cr.execute(SQL("SELECT id FROM om_shopify_store WHERE id = %s FOR UPDATE", self.id))
+ store = env['om.shopify.store'].browse(self.id)
+ soon = fields.Datetime.now() + timedelta(minutes=2)
+ if not force and store.token_expires_at and store.token_expires_at > soon:
+ return # refreshed by another worker meanwhile
+ try:
+ data = ShopifyClient.refresh_oauth_token(store.shop_domain, store.client_id, store.client_secret,
+ store.refresh_token)
+ except ShopifyAuthError as e:
+ refused = e
+ store.write({'state': 'error', 'last_error': _("%(error)s Click Connect with Shopify again.", error=e)})
+ else:
+ store.write(self._shopify_oauth_token_vals(data))
+ cr.commit()
+ self.invalidate_recordset(['state', 'last_error'])
+ if refused:
+ raise refused
+
+ def action_shopify_oauth_start(self):
+ self.ensure_one()
+ if self.auth_type != 'oauth':
+ raise UserError(_("This store does not use the installation by link."))
+ if not self.sudo().client_id or not self.sudo().client_secret:
+ raise UserError(_("Set the client ID and secret of the app first."))
+ return {'type': 'ir.actions.act_url', 'url': f'/om_shopify/oauth/start/{self.id}', 'target': 'self'}
+
+ def _shopify_oauth_authorize_url(self):
+ """ A new state for this installation, and the page of Shopify where the merchant approves it. """
+ self.ensure_one()
+ state = secrets.token_urlsafe(32)
+ self.sudo().write({
+ 'oauth_state_hash': hashlib.sha256(state.encode()).hexdigest(),
+ 'oauth_state_expires': fields.Datetime.now() + timedelta(minutes=15),
+ 'oauth_state_uid': self.env.uid,
+ })
+ scopes = ','.join(scope.strip() for scope in (self.oauth_scopes or DEFAULT_SCOPES).split(',') if scope.strip())
+ return ShopifyClient.authorize_url(self.shop_domain, self.sudo().client_id, scopes, self.oauth_redirect_url,
+ state)
+
+ @api.model
+ def _shopify_oauth_store_of_state(self, state):
+ if not state:
+ return self.browse()
+ return self.sudo().search([('oauth_state_hash', '=', hashlib.sha256(state.encode()).hexdigest()),
+ ('auth_type', '=', 'oauth')], limit=1)
+
+ def _shopify_oauth_complete(self, params):
+ """ Check the callback of Shopify (signature, state, store) and exchange its code for the tokens. """
+ self.ensure_one()
+ store = self.sudo()
+ if not store.oauth_state_expires or store.oauth_state_expires < fields.Datetime.now():
+ raise UserError(_("The installation expired: click Connect with Shopify again."))
+ if store.oauth_state_uid.id != self.env.uid:
+ raise UserError(_("The installation was started by another user."))
+ if normalize_shop_domain(params.get('shop')) != store.shop_domain or not is_valid_shop_domain(params.get('shop')):
+ raise UserError(_("Shopify answered for another store."))
+ if not ShopifyClient.verify_oauth_query(store.client_secret, params):
+ raise UserError(_("The answer of Shopify is not signed by the app."))
+ data = ShopifyClient.fetch_oauth_token(store.shop_domain, store.client_id, store.client_secret,
+ params.get('code'))
+ store.write({**self._shopify_oauth_token_vals(data), 'oauth_state_hash': False, 'oauth_state_expires': False,
+ 'oauth_state_uid': False})
+ return self.action_shopify_connect()
+
+ def _shopify_client(self, refresh=False):
+ self.ensure_one()
+ return ShopifyClient(self.shop_domain, self._shopify_token(refresh=refresh), self.api_version)
+
+ def _shopify_call(self, method, *args, **kwargs):
+ """ ``ShopifyClient.(*args)``, with a new token and a second try when the token of
+ an app of the Dev Dashboard was refused. """
+ self.ensure_one()
+ try:
+ return getattr(self._shopify_client(), method)(*args, **kwargs)
+ except ShopifyAuthError:
+ if self.sudo().auth_type == 'token' or (self.sudo().auth_type == 'oauth' and not self.sudo().refresh_token):
+ raise
+ return getattr(self._shopify_client(refresh=True), method)(*args, **kwargs)
+
+ def _shopify_execute(self, query, variables=None):
+ return self._shopify_call('execute', query, variables)
+
+ def _shopify_mutate(self, query, variables, field, errors_key='userErrors'):
+ return self._shopify_call('mutate', query, variables, field, errors_key)
+
+ def _shopify_paginate(self, query, variables, path):
+ """ Every node of a connection. A generator: the token is checked before the first page. """
+ self.ensure_one()
+ client = self._shopify_client()
+ try:
+ iterator = client.paginate(query, variables, path)
+ first = next(iterator, None)
+ except ShopifyAuthError:
+ if self.sudo().auth_type == 'token' or (self.sudo().auth_type == 'oauth' and not self.sudo().refresh_token):
+ raise
+ client = self._shopify_client(refresh=True)
+ iterator = client.paginate(query, variables, path)
+ first = next(iterator, None)
+ if first is None:
+ return
+ yield first
+ yield from iterator
+
+ # ------------------------------------------------------------------
+ # Connection
+ # ------------------------------------------------------------------
+
+ def action_shopify_connect(self):
+ """ Check the credentials, read the details of the store and subscribe to the webhooks. """
+ for store in self:
+ try:
+ shop = store._shopify_execute(SHOP_QUERY).get('shop') or {}
+ except UserError as e:
+ # not raised: the error is kept on the store
+ store.write({'state': 'error', 'last_error': str(e)})
+ return self._shopify_notification(str(e), kind='danger')
+ currency = self.env['res.currency'].with_context(active_test=False).search(
+ [('name', '=', shop.get('currencyCode'))], limit=1)
+ store.write({
+ 'state': 'connected',
+ 'last_error': False,
+ 'shop_gid': shop.get('id'),
+ 'shop_name': shop.get('name'),
+ 'shop_email': shop.get('email'),
+ 'shop_timezone': shop.get('ianaTimezone'),
+ 'shop_currency_id': currency.id,
+ })
+ store.message_post(body=_("Connected to %(shop)s.", shop=shop.get('name') or store.shop_domain))
+ self.env['om.shopify.location']._shopify_import(store)
+ if store._shopify_webhook_handlers():
+ if store._shopify_webhooks_possible():
+ store.action_shopify_sync_webhooks()
+ else:
+ store.message_post(body=_(
+ "No webhooks: Shopify sends them to an https address only, and %(url)s is not one. Set the "
+ "Odoo Public URL of the store, then click Sync Webhooks. The scheduled imports still run.",
+ url=store.webhook_url))
+ return self._shopify_notification(_("Connected to Shopify."))
+
+ def _shopify_notification(self, message, kind='success'):
+ return {
+ 'type': 'ir.actions.client',
+ 'tag': 'display_notification',
+ 'params': {'message': message, 'type': kind, 'next': {'type': 'ir.actions.act_window_close'}},
+ }
+
+ def _shopify_ensure_connected(self):
+ for store in self:
+ if store.state != 'connected':
+ raise UserError(_("Connect %(store)s to Shopify first.", store=store.name))
+
+ # ------------------------------------------------------------------
+ # Webhooks
+ # ------------------------------------------------------------------
+
+ def _shopify_webhook_handlers(self):
+ """ ``{topic: (model, method)}``: the webhooks of the store, and the job that processes each
+ of them. The method gets the job; ``job.payload`` is the body of the webhook. Each module
+ adds its topics (``ORDERS_CREATE``, ``PRODUCTS_UPDATE``…). """
+ orders = ('sale.order', '_connector_job_shopify_order_webhook')
+ customers = ('om.shopify.customer', '_connector_job_shopify_customer_webhook')
+ return {
+ 'REFUNDS_CREATE': ('sale.order', '_connector_job_shopify_refund_webhook'),
+ 'CUSTOMERS_CREATE': customers,
+ 'CUSTOMERS_UPDATE': customers,
+ 'APP_UNINSTALLED': ('om.shopify.store', '_connector_job_shopify_app_uninstalled'),
+ 'PRODUCTS_CREATE': ('om.shopify.product.template', '_connector_job_shopify_product_webhook'),
+ 'PRODUCTS_UPDATE': ('om.shopify.product.template', '_connector_job_shopify_product_webhook'),
+ 'PRODUCTS_DELETE': ('om.shopify.product.template', '_connector_job_shopify_product_deleted'),
+ 'ORDERS_CREATE': orders,
+ 'ORDERS_UPDATED': orders,
+ 'ORDERS_CANCELLED': orders,
+ }
+
+ def _shopify_webhooks_possible(self):
+ self.ensure_one()
+ return bool(self.webhook_url and self.webhook_url.startswith('https://'))
+
+ def action_shopify_sync_webhooks(self):
+ """ Subscribe to the webhooks of the store, and remove the subscriptions of Odoo that are not
+ needed anymore. """
+ Webhook = self.env['om.shopify.webhook'].sudo()
+ for store in self:
+ store._shopify_ensure_connected()
+ if not store._shopify_webhooks_possible():
+ raise UserError(_("Shopify sends webhooks to an https address only. Set the Odoo Public URL of "
+ "%(store)s (for example the address of a tunnel to this Odoo).", store=store.name))
+ uri = store.webhook_url
+ wanted = set(store._shopify_webhook_handlers())
+ existing = {}
+ for node in store._shopify_paginate(WEBHOOKS_QUERY, {}, 'webhookSubscriptions'):
+ if node.get('uri') == uri:
+ existing.setdefault(node['topic'], []).append(node['id'])
+ for topic, gids in existing.items():
+ # duplicates and topics no module handles anymore
+ for gid in (gids[1:] if topic in wanted else gids):
+ store._shopify_mutate(WEBHOOK_DELETE, {'id': gid}, 'webhookSubscriptionDelete')
+ subscribed = {topic: gids[0] for topic, gids in existing.items() if topic in wanted}
+ for topic in sorted(wanted - set(subscribed)):
+ result = store._shopify_mutate(WEBHOOK_CREATE, {'topic': topic, 'uri': uri},
+ 'webhookSubscriptionCreate')
+ subscribed[topic] = (result.get('webhookSubscription') or {}).get('id')
+ store.webhook_ids.filtered(lambda w: w.topic not in subscribed).unlink()
+ known = {w.topic: w for w in store.webhook_ids}
+ for topic, gid in subscribed.items():
+ vals = {'shopify_gid': gid, 'uri': uri}
+ if topic in known:
+ known[topic].write(vals)
+ else:
+ Webhook.create({'store_id': store.id, 'topic': topic, **vals})
+ return self._shopify_notification(_("Webhooks are up to date."))
+
+ def _shopify_compliance_handlers(self):
+ """ The privacy (GDPR) webhooks: Shopify sends them to the compliance address set in the app,
+ they are not subscribed by Odoo. """
+ return {
+ 'CUSTOMERS_DATA_REQUEST': ('om.shopify.store', '_connector_job_shopify_gdpr_data_request'),
+ 'CUSTOMERS_REDACT': ('om.shopify.store', '_connector_job_shopify_gdpr_customer_redact'),
+ 'SHOP_REDACT': ('om.shopify.store', '_connector_job_shopify_gdpr_shop_redact'),
+ }
+
+ def _shopify_receive_webhook(self, topic, payload, webhook_id=None):
+ """ Queue the job of a webhook received from Shopify; ignore the topics no module handles. """
+ self.ensure_one()
+ handler = {**self._shopify_webhook_handlers(), **self._shopify_compliance_handlers()}.get(topic)
+ if not handler:
+ _logger.info("Shopify %s: webhook %s ignored, no module handles it", self.shop_domain, topic)
+ return None
+ model_name, method_name = handler
+ return self._connector_enqueue(
+ method_name, payload, model_name=model_name, priority=5,
+ name=_("Webhook %(topic)s", topic=topic.replace('_', ' ').lower()),
+ dedupe_key=f'webhook:{webhook_id}' if webhook_id else None,
+ )
+
+ @api.model
+ def _connector_job_shopify_app_uninstalled(self, job):
+ store = job.backend
+ store.sudo().write({'state': 'error', 'access_token': False, 'token_expires_at': False,
+ 'last_error': _("The app was uninstalled from the store.")})
+ store.webhook_ids.sudo().unlink()
+ store.message_post(body=_("The app was uninstalled from the Shopify store: Odoo cannot reach it anymore."))
+ return store
+
+ # ------------------------------------------------------------------
+ # Orders: pricelist and taxes
+ # ------------------------------------------------------------------
+
+ def _shopify_pricelist(self, currency_code):
+ """ A pricelist in the currency of the order, when it is not the currency of the company. """
+ self.ensure_one()
+ if not currency_code or currency_code == self.company_id.currency_id.name:
+ return self.env['product.pricelist']
+ currency = self.env['res.currency'].with_context(active_test=False).search([('name', '=', currency_code)],
+ limit=1)
+ if not currency:
+ raise UserError(_("Unknown currency %(currency)s.", currency=currency_code))
+ if not currency.active:
+ currency.sudo().active = True
+ Pricelist = self.env['product.pricelist'].sudo()
+ return Pricelist.search([('currency_id', '=', currency.id), ('company_id', 'in', [False, self.company_id.id])],
+ limit=1) or Pricelist.create({
+ 'name': _("Shopify %(currency)s", currency=currency.name),
+ 'currency_id': currency.id, 'company_id': self.company_id.id})
+
+ def _shopify_taxes(self, tax_lines, taxes_included):
+ """ The Odoo sale taxes of the rates of ``tax_lines``, found or created. """
+ self.ensure_one()
+ Tax = self.env['account.tax'].sudo().with_company(self.company_id)
+ include = 'tax_included' if taxes_included else 'tax_excluded'
+ taxes = Tax.browse()
+ for line in tax_lines or []:
+ rate = float_round(float(line.get('rate') or 0.0) * 100.0, precision_digits=4)
+ if not rate:
+ continue
+ candidates = Tax.search([
+ ('company_id', '=', self.company_id.id), ('type_tax_use', '=', 'sale'),
+ ('amount_type', '=', 'percent'), ('amount', '=', rate),
+ ])
+ tax = candidates.filtered(lambda t: t.price_include == taxes_included)[:1]
+ if not tax:
+ title = line.get('title') or _("Tax")
+ tax = Tax.create({
+ 'name': f"{title} {rate:g}%" + (_(" (included)") if taxes_included else ''),
+ 'amount': rate,
+ 'amount_type': 'percent',
+ 'type_tax_use': 'sale',
+ 'price_include_override': include,
+ 'company_id': self.company_id.id,
+ 'country_id': self.company_id.account_fiscal_country_id.id,
+ })
+ if self.tax_account_id:
+ (tax.invoice_repartition_line_ids | tax.refund_repartition_line_ids).filtered(
+ lambda l: l.repartition_type == 'tax').account_id = self.tax_account_id
+ taxes |= tax
+ return taxes
+
+ # ------------------------------------------------------------------
+ # Scheduled work
+ # ------------------------------------------------------------------
+
+ def _shopify_connected(self):
+ return self.search([('state', '=', 'connected')])
+
+ @api.model
+ def _cron_shopify_import_orders(self):
+ for store in self._shopify_connected().filtered('order_auto_import'):
+ store._connector_enqueue_once('_connector_job_shopify_fetch_orders', name=_("Look for new orders"))
+
+ @api.model
+ def _cron_shopify_import_products(self):
+ for store in self._shopify_connected().filtered('product_auto_import'):
+ store._connector_enqueue_once('_connector_job_shopify_fetch_products', name=_("Look for changed products"))
+
+ @api.model
+ def _cron_shopify_export_stock(self):
+ for store in self._shopify_connected().filtered('stock_export'):
+ store._connector_enqueue_once('_connector_job_shopify_export_stock', name=_("Send the stock"))
+
+ def action_shopify_import_orders(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ store._connector_enqueue_once('_connector_job_shopify_fetch_orders', name=_("Look for new orders"))
+ return self._shopify_notification(_("The orders are being imported: follow it in Jobs."), kind='info')
+
+ def action_shopify_import_products(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ if store.product_bulk_import:
+ store._connector_enqueue_once('_connector_job_shopify_bulk_products', name=_("Import all products (bulk)"))
+ else:
+ store._connector_enqueue_once('_connector_job_shopify_fetch_products', {'full': True},
+ name=_("Import all products"))
+ return self._shopify_notification(_("The products are being imported: follow it in Jobs."), kind='info')
+
+ def action_shopify_export_stock(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ store._connector_enqueue_once('_connector_job_shopify_export_stock', {'full': True},
+ name=_("Send all the stock"))
+ return self._shopify_notification(_("The stock is being sent: follow it in Jobs."), kind='info')
+
+ def action_shopify_import_locations(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ self.env['om.shopify.location']._shopify_import(store)
+ return self._shopify_notification(_("Locations imported."))
+
+ @staticmethod
+ def _shopify_search_date(value):
+ return fields.Datetime.to_datetime(value).strftime('%Y-%m-%dT%H:%M:%SZ')
+
+ @api.model
+ def _connector_job_shopify_fetch_orders(self, job):
+ """ Queue one job per order updated in Shopify since the last import. """
+ store = job.backend
+ started = fields.Datetime.now()
+ since = max(filter(None, [
+ store.order_import_from,
+ store.order_last_sync and store.order_last_sync - timedelta(minutes=10),
+ ]), default=started - timedelta(days=7))
+ search = f"updated_at:>='{self._shopify_search_date(since)}'"
+ count = 0
+ for node in store._shopify_paginate(queries.ORDER_IDS, {'query': search}, 'orders'):
+ store._connector_enqueue('_connector_job_shopify_import_order', {'id': node['id'],
+ 'updatedAt': node['updatedAt']},
+ model_name='sale.order', name=_("Order %(name)s", name=node['name']),
+ dedupe_key=f"order:{node['id']}")
+ count += 1
+ store.order_last_sync = started
+ return _("%(count)s order(s) to import", count=count)
+
+ @api.model
+ def _connector_job_shopify_fetch_products(self, job):
+ """ Queue one job per product changed in Shopify since the last import (all of them the
+ first time, or when asked). """
+ store = job.backend
+ started = fields.Datetime.now()
+ search = None
+ if store.product_last_sync and not (job.payload or {}).get('full'):
+ search = f"updated_at:>='{self._shopify_search_date(store.product_last_sync - timedelta(minutes=10))}'"
+ count = 0
+ for node in store._shopify_paginate(queries.PRODUCT_IDS, {'query': search}, 'products'):
+ store._connector_enqueue('_connector_job_shopify_import_product', {'id': node['id'],
+ 'updatedAt': node['updatedAt']},
+ model_name='om.shopify.product.template',
+ name=_("Product %(id)s", id=node['id'].rsplit('/', 1)[-1]),
+ dedupe_key=f"product:{node['id']}", priority=20)
+ count += 1
+ store.product_last_sync = started
+ return _("%(count)s product(s) to import", count=count)
+
+ @api.model
+ def _connector_job_shopify_bulk_products(self, job):
+ """ Start a bulk operation exporting the whole catalogue; a job waits for its file. """
+ store = job.backend
+ result = store._shopify_mutate(queries.BULK_RUN, {'query': store._shopify_query(queries.BULK_PRODUCTS, bulk=True)},
+ 'bulkOperationRunQuery')
+ operation = result.get('bulkOperation') or {}
+ store.product_last_sync = fields.Datetime.now()
+ store._connector_enqueue('_connector_job_shopify_bulk_poll', {'id': operation['id'], 'kind': 'products'},
+ name=_("Wait for the catalogue of Shopify"), max_attempts=240,
+ eta=fields.Datetime.now() + timedelta(seconds=15),
+ dedupe_key=f"bulk:{operation['id']}")
+ return _("Bulk operation %(id)s started", id=operation['id'])
+
+ @api.model
+ def _connector_job_shopify_bulk_poll(self, job):
+ """ Wait for the bulk operation, then queue one job per product of its file. """
+ store = job.backend
+ operation = store._shopify_execute(queries.BULK_STATUS, {'id': job.payload['id']}).get('bulkOperation') or {}
+ status = operation.get('status')
+ if status in ('CREATED', 'RUNNING', 'CANCELING'):
+ raise RetryableJobError(_("Shopify prepares the file: %(count)s object(s) so far",
+ count=operation.get('objectCount') or 0), delay=30)
+ if status != 'COMPLETED':
+ raise UserError(_("The bulk operation of Shopify ended as %(status)s (%(error)s).",
+ status=status, error=operation.get('errorCode') or _("no error code")))
+ if not operation.get('url'):
+ return _("The catalogue of Shopify is empty")
+ count = 0
+ for product in self._shopify_bulk_products(operation['url']):
+ store._connector_enqueue('_connector_job_shopify_import_product',
+ {'id': product['id'], 'updatedAt': product.get('updatedAt'), 'data': product},
+ model_name='om.shopify.product.template',
+ name=_("Product %(name)s", name=product.get('title') or product['id']),
+ dedupe_key=f"product:{product['id']}", priority=20)
+ count += 1
+ return _("%(count)s product(s) to import", count=count)
+
+ @api.model
+ def _shopify_bulk_products(self, url):
+ """ The products of the JSONL file of a bulk operation, each with its variants. Lines of a
+ product come before the lines of its variants, which give it as ``__parentId``. """
+ response = request_with_retry('GET', url, timeout=300, stream=True)
+ if response.status_code >= 400:
+ raise ShopifyError(f"The file of the bulk operation cannot be downloaded (HTTP {response.status_code}).")
+ current = None
+ for raw in response.iter_lines():
+ if not raw:
+ continue
+ node = json.loads(raw)
+ parent = node.pop('__parentId', None)
+ if parent is None:
+ if current:
+ yield current
+ current = dict(node, variants=[], metafields=[])
+ elif current and parent == current['id'] and '/ProductVariant/' in node.get('id', ''):
+ current['variants'].append(dict(node, metafields=[]))
+ elif current and '/Metafield/' in node.get('id', ''):
+ owner = current if parent == current['id'] else next(
+ (v for v in current['variants'] if v['id'] == parent), None)
+ if owner is not None:
+ owner['metafields'].append(node)
+ if current:
+ yield current
+
+ @api.model
+ def _connector_job_shopify_export_stock(self, job):
+ """ Send the stock of the linked products to the Shopify locations that have a warehouse:
+ the products whose stock moved since the last time, or all of them when asked. """
+ store = job.backend
+ started = fields.Datetime.now()
+ variants = self.env['om.shopify.product'].search([
+ ('store_id', '=', store.id), ('tracked', '=', True), ('inventory_item_gid', '!=', False),
+ '|', ('product_id.is_storable', '=', True), ('stock_rule', '=', 'fixed'),
+ ])
+ if store.stock_last_sync and not (job.payload or {}).get('full'):
+ moved = self.env['stock.move'].search([
+ ('product_id', 'in', variants.product_id.ids),
+ ('write_date', '>=', store.stock_last_sync - timedelta(minutes=10)),
+ ]).product_id
+ variants = variants.filtered(lambda v: v.product_id in moved or v.stock_rule == 'fixed')
+ sent = 0
+ for location in store.location_ids.filtered(lambda l: l.warehouse_id and l.shopify_active):
+ products = variants.product_id.with_context(warehouse_id=location.warehouse_id.id)
+ quantities = [{
+ 'inventoryItemId': variant.inventory_item_gid,
+ 'locationId': location.shopify_gid,
+ 'quantity': variant._shopify_stock_quantity(products.browse(variant.product_id.id)[store.stock_field]),
+ 'changeFromQuantity': None,
+ } for variant in variants]
+ for start in range(0, len(quantities), STOCK_BATCH):
+ batch = quantities[start:start + STOCK_BATCH]
+ store._shopify_mutate(queries.INVENTORY_SET, {
+ 'input': {'name': 'available', 'reason': 'correction', 'quantities': batch},
+ 'key': str(uuid.uuid4()),
+ }, 'inventorySetQuantities')
+ sent += len(batch)
+ store.stock_last_sync = started
+ return _("%(count)s stock level(s) sent", count=sent)
+
+ @api.model
+ def _cron_shopify_daily(self):
+ for store in self._shopify_connected():
+ if store.price_auto_export:
+ store._connector_enqueue_once('_connector_job_shopify_export_prices', name=_("Send the prices"))
+ if store.payout_import and store.payout_journal_id:
+ store._connector_enqueue_once('_connector_job_shopify_import_payouts', name=_("Import the payouts"))
+
+ def action_shopify_export_prices(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ store._connector_enqueue_once('_connector_job_shopify_export_prices', name=_("Send the prices"))
+ return self._shopify_notification(_("The prices are being sent: follow it in Jobs."), kind='info')
+
+ def action_shopify_import_stock(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ store._connector_enqueue_once('_connector_job_shopify_import_stock', name=_("Import the stock"))
+ return self._shopify_notification(_("The stock is being imported: follow it in Jobs."), kind='info')
+
+ def action_shopify_import_customers(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ store._connector_enqueue_once('_connector_job_shopify_fetch_customers', name=_("Import the customers"))
+ return self._shopify_notification(_("The customers are being imported: follow it in Jobs."), kind='info')
+
+ def _shopify_check_payout_setup(self):
+ self.ensure_one()
+ missing = [self._fields[name].string for name in (
+ 'payout_transit_account_id', 'payout_fee_account_id', 'payout_adjustment_account_id',
+ 'payout_clearing_account_id', 'payout_entry_journal_id') if not self[name]]
+ if missing:
+ raise UserError(_("Set the %(fields)s of %(store)s, or click Set Up Payouts.",
+ fields=', '.join(missing), store=self.name))
+
+ def action_shopify_setup_payouts(self):
+ """ Create what the payouts need: the transit account, a Shopify Payments journal whose payments
+ go on it, its workflow for the payment method shopify_payments, and the accounts of the fees. """
+ self.ensure_one()
+ company = self.company_id
+ Account = self.env['account.account'].with_company(company)
+
+ def account(name, code, account_type, reconcile=False):
+ existing = Account.search([('name', '=', name), ('company_ids', 'in', company.ids)], limit=1)
+ if existing:
+ return existing
+ number = int(code)
+ while Account.search_count([('code', '=', str(number)), ('company_ids', 'in', company.ids)], limit=1):
+ number += 1
+ return Account.create({'name': name, 'code': str(number), 'account_type': account_type,
+ 'reconcile': reconcile, 'company_ids': [fields.Command.set(company.ids)]})
+
+ transit = self.payout_transit_account_id or account(_("Shopify Payments Clearing"), '101950', 'asset_current',
+ reconcile=True)
+ vals = {
+ 'payout_transit_account_id': transit.id,
+ 'payout_fee_account_id': (self.payout_fee_account_id
+ or account(_("Shopify Payments Fees"), '627950', 'expense')).id,
+ 'payout_adjustment_account_id': (self.payout_adjustment_account_id
+ or account(_("Shopify Payments Adjustments"), '658950', 'expense')).id,
+ 'payout_clearing_account_id': (self.payout_clearing_account_id or company.transfer_account_id).id,
+ 'payout_entry_journal_id': (self.payout_entry_journal_id or self.env['account.journal'].search(
+ [('type', '=', 'general'), ('company_id', '=', company.id)], limit=1)).id,
+ 'payout_mode': 'entry',
+ }
+ Journal = self.env['account.journal'].with_company(company)
+ journal = Journal.search([('code', '=', 'SHPAY'), ('company_id', '=', company.id)], limit=1)
+ if not journal:
+ journal = Journal.create({'name': _("Shopify Payments"), 'code': 'SHPAY', 'type': 'bank',
+ 'company_id': company.id})
+ (journal.inbound_payment_method_line_ids | journal.outbound_payment_method_line_ids).payment_account_id = transit
+ gateway = self.env['om.shopify.gateway']._shopify_get(self, 'shopify_payments')
+ workflow = gateway.workflow_id
+ if workflow.journal_id != journal:
+ workflow = (workflow or self.workflow_id).copy({'name': _("%(store)s: Shopify Payments", store=self.name),
+ 'journal_id': journal.id, 'register_payment': True,
+ 'post_invoice': True})
+ gateway.workflow_id = workflow
+ self.write(vals)
+ return self._shopify_notification(_("Payouts are set up: the payments of Shopify Payments go to the journal "
+ "%(journal)s, on the account %(account)s.",
+ journal=journal.name, account=transit.display_name))
+
+ def action_shopify_import_payouts(self):
+ self._shopify_ensure_connected()
+ for store in self:
+ if not store.payout_journal_id:
+ raise UserError(_("Set the payout journal of %(store)s.", store=store.name))
+ store._connector_enqueue_once('_connector_job_shopify_import_payouts', name=_("Import the payouts"))
+ return self._shopify_notification(_("The payouts are being imported: follow it in Jobs."), kind='info')
+
+ # Prices and product export helpers
+
+ def _shopify_prices(self, product):
+ """ ``(price, compare-at price or None)`` of ``product`` for Shopify. """
+ self.ensure_one()
+ price = self.price_pricelist_id._get_product_price(product, 1.0) if self.price_pricelist_id \
+ else product.lst_price
+ compare_at = None
+ if self.compare_pricelist_id:
+ compare = self.compare_pricelist_id._get_product_price(product, 1.0)
+ if compare > price:
+ compare_at = compare
+ return price, compare_at
+
+ def _shopify_upload_image(self, template):
+ """ Upload the image of ``template`` to Shopify; gives its address for a product. """
+ self.ensure_one()
+ content = base64.b64decode(template.image_1920)
+ mimetype = 'image/png' if content[:8] == b'\x89PNG\r\n\x1a\n' else 'image/jpeg'
+ filename = f"product-{template.id}.{'png' if mimetype == 'image/png' else 'jpg'}"
+ result = self._shopify_mutate(queries.STAGED_UPLOADS, {'input': [{
+ 'filename': filename, 'mimeType': mimetype, 'httpMethod': 'POST', 'resource': 'IMAGE',
+ 'fileSize': str(len(content)),
+ }]}, 'stagedUploadsCreate')
+ target = (result.get('stagedTargets') or [{}])[0]
+ response = request_with_retry(
+ 'POST', target['url'], data={p['name']: p['value'] for p in target.get('parameters') or []},
+ files={'file': (filename, content, mimetype)}, timeout=60)
+ if response.status_code >= 300:
+ raise UserError(_("Shopify refused the image of %(product)s (HTTP %(status)s).",
+ product=template.name, status=response.status_code))
+ return target['resourceUrl']
+
+ def _shopify_publish(self, product_gid):
+ self.ensure_one()
+ publications = [node for node in self._shopify_paginate(queries.PUBLICATIONS, {}, 'publications')
+ if node.get('name') == 'Online Store']
+ if publications:
+ self._shopify_mutate(queries.PUBLISH, {'id': product_gid,
+ 'input': [{'publicationId': publications[0]['id']}]},
+ 'publishablePublish')
+
+ @api.model
+ def _connector_job_shopify_export_prices(self, job):
+ """ Send the prices and compare-at prices of the linked products. """
+ store = job.backend
+ count = 0
+ templates = self.env['om.shopify.product.template'].search([('store_id', '=', store.id)])
+ for template in templates:
+ variants = []
+ for variant in template.variant_ids:
+ price, compare_at = store._shopify_prices(variant.product_id)
+ variants.append({'id': variant.external_id, 'price': f'{price:.2f}',
+ 'compareAtPrice': f'{compare_at:.2f}' if compare_at else None})
+ if variants:
+ store._shopify_mutate(queries.VARIANTS_BULK_UPDATE,
+ {'productId': template.external_id, 'variants': variants},
+ 'productVariantsBulkUpdate')
+ count += len(variants)
+ return _("%(count)s price(s) sent", count=count)
+
+ @api.model
+ def _connector_job_shopify_import_stock(self, job):
+ """ Count the stock of Shopify in the warehouse of each location. """
+ store = job.backend
+ Quant = self.env['stock.quant'].with_context(inventory_mode=True)
+ variants = {v.inventory_item_gid: v for v in self.env['om.shopify.product'].search(
+ [('store_id', '=', store.id), ('inventory_item_gid', '!=', False), ('product_id.is_storable', '=', True)])}
+ quants = Quant.browse()
+ for location in store.location_ids.filtered(lambda l: l.warehouse_id and l.shopify_active):
+ stock_location = location.warehouse_id.lot_stock_id
+ for node in store._shopify_paginate(queries.INVENTORY_LEVELS, {'id': location.shopify_gid},
+ 'location.inventoryLevels'):
+ variant = variants.get((node.get('item') or {}).get('id'))
+ if not variant or variant.product_id.tracking not in (False, 'none'):
+ continue
+ available = next((q['quantity'] for q in node.get('quantities') or [] if q['name'] == 'available'), 0)
+ quant = Quant.search([('product_id', '=', variant.product_id.id),
+ ('location_id', '=', stock_location.id), ('lot_id', '=', False)], limit=1)
+ if quant:
+ quant.write({'inventory_quantity': max(available, 0), 'inventory_quantity_set': True})
+ else:
+ quant = Quant.create({'product_id': variant.product_id.id, 'location_id': stock_location.id,
+ 'inventory_quantity': max(available, 0), 'inventory_quantity_set': True})
+ quants |= quant
+ if store.stock_import_apply and quants:
+ quants.action_apply_inventory()
+ return _("%(count)s quantity(ies) imported", count=len(quants))
+
+ @api.model
+ def _connector_job_shopify_fetch_customers(self, job):
+ store = job.backend
+ started = fields.Datetime.now()
+ search = None
+ if store.customer_last_sync:
+ search = f"updated_at:>='{self._shopify_search_date(store.customer_last_sync - timedelta(minutes=10))}'"
+ count = 0
+ for node in store._shopify_paginate(queries.CUSTOMER_IDS, {'query': search}, 'customers'):
+ store._connector_enqueue('_connector_job_shopify_import_customer',
+ {'id': node['id'], 'updatedAt': node['updatedAt']},
+ model_name='om.shopify.customer',
+ name=_("Customer %(id)s", id=node['id'].rsplit('/', 1)[-1]),
+ dedupe_key=f"customer:{node['id']}", priority=30)
+ count += 1
+ store.customer_last_sync = started
+ return _("%(count)s customer(s) to import", count=count)
+
+ @api.model
+ def _connector_job_shopify_import_payouts(self, job):
+ return self.env['om.shopify.payout']._shopify_import(job.backend)
+
+ def _connector_job_failed(self, job):
+ """ An activity for the responsible of the store, once while a failure is not looked at. """
+ self.ensure_one()
+ summary = _("Shopify jobs failed")
+ if self.activity_ids.filtered(lambda a: a.summary == summary):
+ return
+ self.activity_schedule('mail.mail_activity_data_todo', summary=summary,
+ user_id=(self.responsible_id or self.create_uid or self.env.user).id,
+ note=_("%(job)s failed: %(error)s", job=job.name, error=(job.error or '')[:300]))
+
+ # ------------------------------------------------------------------
+ # Privacy (GDPR)
+ # ------------------------------------------------------------------
+
+ def _shopify_gdpr_customer(self, payload):
+ """ ``(partner, orders)`` of the customer of a privacy webhook. """
+ self.ensure_one()
+ customer = payload.get('customer') or {}
+ partner = self.env['res.partner']
+ if customer.get('id'):
+ partner = self.env['om.shopify.customer']._connector_find(
+ self, f"gid://shopify/Customer/{customer['id']}").partner_id
+ if not partner and customer.get('email'):
+ partner = self.env['res.partner'].search([('email', '=ilike', customer['email']),
+ ('parent_id', '=', False)], limit=1)
+ order_ids = payload.get('orders_requested') or payload.get('orders_to_redact') or []
+ orders = self.env['sale.order'].search([
+ ('shopify_store_id', '=', self.id),
+ ('shopify_order_gid', 'in', [f'gid://shopify/Order/{i}' for i in order_ids]),
+ ]) if order_ids else self.env['sale.order']
+ if partner:
+ orders |= self.env['sale.order'].search([('shopify_store_id', '=', self.id),
+ ('partner_id', 'child_of', partner.id)])
+ return partner, orders
+
+ def _shopify_gdpr_activity(self, summary, note):
+ self.ensure_one()
+ self.activity_schedule('mail.mail_activity_data_todo', summary=summary, note=note,
+ user_id=(self.responsible_id or self.create_uid or self.env.user).id)
+
+ def _shopify_scrub_jobs(self, needles=None):
+ """ Remove the Shopify data kept in the finished jobs: all of them, or those that mention one of
+ ``needles`` (an email, an ID). """
+ self.ensure_one()
+ jobs = self.env['om.connector.job'].sudo().search(
+ self._connector_job_domain() + [('state', '!=', 'pending'), ('payload', '!=', False)])
+ if needles:
+ needles = [str(n) for n in needles if n]
+ jobs = jobs.filtered(lambda j: any(n in json.dumps(j.payload) for n in needles))
+ jobs.write({'payload': False})
+ return len(jobs)
+
+ @api.model
+ def _connector_job_shopify_gdpr_data_request(self, job):
+ store = job.backend
+ partner, orders = store._shopify_gdpr_customer(job.payload)
+ customer = job.payload.get('customer') or {}
+ store._shopify_gdpr_activity(
+ _("GDPR: data request"),
+ _("Shopify asks for the data of the customer %(email)s (request %(request)s). Send them what Odoo keeps: "
+ "the contact %(partner)s and the orders %(orders)s.",
+ email=customer.get('email') or customer.get('id'),
+ request=(job.payload.get('data_request') or {}).get('id', ''),
+ partner=partner.display_name or _("(none)"),
+ orders=', '.join(orders.mapped('name')) or _("(none)")))
+ return partner or _("Data request noted")
+
+ @api.model
+ def _connector_job_shopify_gdpr_customer_redact(self, job):
+ store = job.backend
+ customer = job.payload.get('customer') or {}
+ partner, orders = store._shopify_gdpr_customer(job.payload)
+ self.env['om.shopify.customer']._connector_find(
+ store, f"gid://shopify/Customer/{customer.get('id')}").unlink()
+ scrubbed = store._shopify_scrub_jobs([customer.get('email'), customer.get('phone'),
+ f"Customer/{customer.get('id')}"] +
+ [o.shopify_order_gid for o in orders])
+ if partner and store.gdpr_anonymize:
+ (partner | partner.child_ids).write({
+ 'name': _("Redacted customer"), 'email': False, 'phone': False, 'street': False,
+ 'street2': False, 'city': False, 'zip': False, 'state_id': False, 'vat': False,
+ 'comment': False, 'active': False,
+ })
+ result = _("Customer anonymized, %(count)s job(s) cleaned", count=scrubbed)
+ else:
+ store._shopify_gdpr_activity(
+ _("GDPR: erase a customer"),
+ _("Shopify asks to erase the customer %(email)s. Odoo removed its link and the Shopify data of "
+ "%(count)s job(s). Decide what to do with the contact %(partner)s and its orders %(orders)s: "
+ "invoices may have to be kept by law.",
+ email=customer.get('email') or customer.get('id'), count=scrubbed,
+ partner=partner.display_name or _("(none)"), orders=', '.join(orders.mapped('name')) or _("(none)")))
+ result = _("%(count)s job(s) cleaned, activity for the responsible", count=scrubbed)
+ return result
+
+ @api.model
+ def _connector_job_shopify_gdpr_shop_redact(self, job):
+ store = job.backend
+ scrubbed = store._shopify_scrub_jobs()
+ self.env['om.shopify.customer'].search([('store_id', '=', store.id)]).unlink()
+ store.sudo().write({'access_token': False, 'client_secret': False, 'client_id': False,
+ 'token_expires_at': False, 'state': 'error', 'active': False,
+ 'last_error': _("Shopify erased the data of this store (the app was uninstalled).")})
+ store.message_post(body=_("Shopify asked to erase the data of the store: credentials, customer links and the "
+ "Shopify data of %(count)s job(s) were removed.", count=scrubbed))
+ return _("Store data erased")
+
+ # ------------------------------------------------------------------
+ # Views
+ # ------------------------------------------------------------------
+
+ def action_open_products(self):
+ self.ensure_one()
+ return {
+ 'type': 'ir.actions.act_window',
+ 'name': _("Products of %(store)s", store=self.name),
+ 'res_model': 'om.shopify.product.template',
+ 'view_mode': 'list,form',
+ 'domain': [('store_id', '=', self.id)],
+ }
+
+ def action_open_orders(self):
+ self.ensure_one()
+ action = self.env['ir.actions.act_window']._for_xml_id('om_shopify.om_shopify_sale_order_action')
+ action['domain'] = [('shopify_store_id', '=', self.id)]
+ return action
+
+ def action_open_webhooks(self):
+ self.ensure_one()
+ return {
+ 'type': 'ir.actions.act_window',
+ 'name': _("Webhooks"),
+ 'res_model': 'om.shopify.webhook',
+ 'view_mode': 'list',
+ 'domain': [('store_id', '=', self.id)],
+ 'context': {'default_store_id': self.id},
+ }
diff --git a/om_shopify/models/om_shopify_webhook.py b/om_shopify/models/om_shopify_webhook.py
new file mode 100644
index 00000000..711b6176
--- /dev/null
+++ b/om_shopify/models/om_shopify_webhook.py
@@ -0,0 +1,18 @@
+from odoo import fields, models
+
+
+class OmShopifyWebhook(models.Model):
+ """ A webhook subscription of a store, made by Odoo. """
+ _name = 'om.shopify.webhook'
+ _description = 'Shopify Webhook'
+ _order = 'store_id, topic'
+
+ store_id = fields.Many2one('om.shopify.store', required=True, ondelete='cascade', index=True)
+ company_id = fields.Many2one(related='store_id.company_id', store=True)
+ topic = fields.Char(required=True)
+ shopify_gid = fields.Char(string='Shopify ID')
+ uri = fields.Char(string='Address')
+
+ _sql_constraints = [
+ ('store_topic_unique', 'unique (store_id, topic)', "A store has one webhook per topic."),
+ ]
diff --git a/om_shopify/models/product_template.py b/om_shopify/models/product_template.py
new file mode 100644
index 00000000..b264456b
--- /dev/null
+++ b/om_shopify/models/product_template.py
@@ -0,0 +1,12 @@
+from odoo import fields, models
+
+
+class ProductTemplate(models.Model):
+ _inherit = 'product.template'
+
+ shopify_binding_ids = fields.One2many('om.shopify.product.template', 'product_tmpl_id', string='Shopify Products')
+ shopify_binding_count = fields.Integer(compute='_compute_shopify_binding_count', string='In Shopify')
+
+ def _compute_shopify_binding_count(self):
+ for template in self:
+ template.shopify_binding_count = len(template.shopify_binding_ids)
diff --git a/om_shopify/models/sale_order.py b/om_shopify/models/sale_order.py
new file mode 100644
index 00000000..a7a9e1f9
--- /dev/null
+++ b/om_shopify/models/sale_order.py
@@ -0,0 +1,673 @@
+import logging
+import uuid
+from collections import defaultdict
+
+from odoo import _, api, fields, models
+from odoo.exceptions import UserError
+from odoo.tools import float_round
+
+from ..tools import queries
+
+_logger = logging.getLogger(__name__)
+
+PAID_STATUSES = ('PAID', 'PARTIALLY_REFUNDED')
+PAYMENT_KINDS = ('SALE', 'CAPTURE')
+RISKY = ('CANCEL', 'INVESTIGATE')
+CANCEL_REASONS = [
+ ('CUSTOMER', 'The customer wanted to cancel'),
+ ('INVENTORY', 'Items unavailable'),
+ ('FRAUD', 'Fraudulent order'),
+ ('DECLINED', 'Payment declined'),
+ ('STAFF', 'Staff error'),
+ ('OTHER', 'Other'),
+]
+
+
+class SaleOrder(models.Model):
+ _inherit = 'sale.order'
+
+ shopify_store_id = fields.Many2one('om.shopify.store', string='Shopify Store', readonly=True, index=True,
+ copy=False)
+ shopify_order_gid = fields.Char(string='Shopify ID', readonly=True, copy=False)
+ shopify_order_name = fields.Char(string='Shopify Order', readonly=True, copy=False)
+ shopify_financial_status = fields.Char(string='Shopify Payment Status', readonly=True, copy=False)
+ shopify_fulfillment_status = fields.Char(string='Shopify Fulfillment Status', readonly=True, copy=False)
+ shopify_gateway_id = fields.Many2one('om.shopify.gateway', string='Shopify Payment Method', readonly=True,
+ copy=False)
+ shopify_workflow_id = fields.Many2one('om.connector.order.workflow', string='Shopify Workflow', readonly=True,
+ copy=False)
+ shopify_source = fields.Char(string='Shopify Channel', readonly=True, copy=False)
+ shopify_risk = fields.Selection([
+ ('ACCEPT', 'Accept'), ('INVESTIGATE', 'Investigate'), ('CANCEL', 'Cancel'), ('NONE', 'None'),
+ ], string='Shopify Risk', readonly=True, copy=False)
+ shopify_risk_message = fields.Text(string='Risk Details', readonly=True, copy=False)
+ shopify_on_hold = fields.Boolean(string='On Hold (Risk)', readonly=True, copy=False,
+ help="The workflow waits: Shopify finds this order risky.")
+ shopify_cancelled = fields.Boolean(string='Cancelled in Shopify', readonly=True, copy=False)
+ shopify_refund_gids = fields.Text(string='Shopify Refunds Applied', readonly=True, copy=False,
+ help="The Shopify refunds already applied to this order, one per line.")
+
+ _sql_constraints = [
+ ('shopify_order_unique', 'unique (shopify_store_id, shopify_order_gid)',
+ "A Shopify order is imported once per store."),
+ ]
+
+ def action_open_in_shopify(self):
+ self.ensure_one()
+ return {
+ 'type': 'ir.actions.act_url',
+ 'url': f'https://{self.shopify_store_id.shop_domain}/admin/orders/'
+ f'{self.shopify_order_gid.rsplit("/", 1)[-1]}',
+ 'target': 'new',
+ }
+
+ def action_shopify_release(self):
+ """ The user checked the risky order: run its workflow. """
+ for order in self.filtered('shopify_on_hold'):
+ order.shopify_on_hold = False
+ order.message_post(body=_("Released by %(user)s despite the risk.", user=self.env.user.name))
+ order._shopify_sync_now()
+
+ def action_shopify_cancel(self):
+ self.ensure_one()
+ return {
+ 'type': 'ir.actions.act_window',
+ 'name': _("Cancel in Shopify"),
+ 'res_model': 'om.shopify.cancel.wizard',
+ 'view_mode': 'form',
+ 'target': 'new',
+ 'context': {'default_order_id': self.id},
+ }
+
+ def _shopify_sync_now(self):
+ for order in self:
+ self._shopify_sync_order(order.shopify_store_id, order.shopify_order_gid)
+
+ # ------------------------------------------------------------------
+ # Jobs
+ # ------------------------------------------------------------------
+
+ @api.model
+ def _connector_job_shopify_import_order(self, job):
+ return self._shopify_sync_order(job.backend, job.payload['id'])
+
+ @api.model
+ def _connector_job_shopify_order_webhook(self, job):
+ gid = job.payload.get('admin_graphql_api_id') or f"gid://shopify/Order/{job.payload['id']}"
+ return self._shopify_sync_order(job.backend, gid)
+
+ @api.model
+ def _connector_job_shopify_refund_webhook(self, job):
+ order_id = job.payload.get('order_id')
+ if not order_id:
+ return _("No order in this refund")
+ return self._shopify_sync_order(job.backend, f"gid://shopify/Order/{order_id}")
+
+ # ------------------------------------------------------------------
+ # Import
+ # ------------------------------------------------------------------
+
+ @api.model
+ def _shopify_sync_order(self, store, gid):
+ """ Import the Shopify order ``gid``, or apply its news (edited, paid, fulfilled, refunded,
+ cancelled) to the sale order imported before. Gives the sale order, or a text when skipped. """
+ data = store._shopify_execute(store._shopify_query(queries.ORDER), {'id': gid}).get('order')
+ if not data:
+ return _("Order %(id)s does not exist anymore", id=gid)
+ order = self.search([('shopify_store_id', '=', store.id), ('shopify_order_gid', '=', gid)], limit=1)
+ if order:
+ store._shopify_apply_metafields('order', order, data.get('metafields'))
+ return order._shopify_update(data)
+ if data.get('test') and not store.order_import_test:
+ return _("%(name)s is a test order: skipped", name=data['name'])
+ if data.get('cancelledAt'):
+ return _("%(name)s is cancelled: skipped", name=data['name'])
+ if (data.get('sourceName') or '').lower() == 'pos' and not store.order_import_pos:
+ return _("%(name)s is a point of sale order: skipped", name=data['name'])
+ if (data['lineItems'].get('pageInfo') or {}).get('hasNextPage'):
+ raise UserError(_("%(name)s has more than 250 lines: import it by hand.", name=data['name']))
+ order = self._shopify_create(store, data)
+ store._shopify_apply_metafields('order', order, data.get('metafields'))
+ order._shopify_process(data)
+ return order
+
+ @api.model
+ def _shopify_money_key(self, store, data):
+ """ ``presentmentMoney`` (the currency the customer paid in) or ``shopMoney``. """
+ if store.order_currency == 'customer' and data.get('presentmentCurrencyCode') \
+ and data['presentmentCurrencyCode'] != data.get('currencyCode'):
+ return 'presentmentMoney'
+ return 'shopMoney'
+
+ @api.model
+ def _shopify_rule_values(self, store, data, gateway):
+ """ ``(workflow, warehouse)`` of an order, from the rules, the payment method and the store. """
+ workflow = warehouse = None
+ for rule in store.order_rule_ids:
+ if rule._matches(data, gateway):
+ workflow = workflow or rule.workflow_id or None
+ warehouse = warehouse or rule.warehouse_id or None
+ if not warehouse and store.order_warehouse_from_location:
+ assigned = {((fo.get('assignedLocation') or {}).get('location') or {}).get('id')
+ for fo in (data.get('fulfillmentOrders') or {}).get('nodes') or []}
+ location = store.location_ids.filtered(lambda l: l.shopify_gid in assigned and l.warehouse_id)[:1]
+ warehouse = location.warehouse_id or None
+ return (workflow or gateway.workflow_id or store.workflow_id,
+ warehouse or store.warehouse_id)
+
+ @api.model
+ def _shopify_create(self, store, data):
+ money = self._shopify_money_key(store, data)
+ b2b = self._shopify_b2b_values(store, data)
+ if (data.get('sourceName') or '').lower() == 'pos' and not data.get('customer') and store.pos_partner_id:
+ partner = invoice = delivery = store.pos_partner_id
+ elif b2b:
+ partner, invoice, delivery = b2b['partner'], b2b['invoice'], b2b['delivery']
+ else:
+ partner, invoice, delivery = self.env['om.shopify.customer']._shopify_partner(store, data)
+ gateway = self.env['om.shopify.gateway']._shopify_get(store, self._shopify_main_gateway(data))
+ workflow, warehouse = self._shopify_rule_values(store, data, gateway)
+ vals = {
+ 'partner_id': partner.id,
+ 'partner_invoice_id': invoice.id,
+ 'partner_shipping_id': delivery.id,
+ 'company_id': store.company_id.id,
+ 'warehouse_id': warehouse.id,
+ 'date_order': fields.Datetime.to_datetime(
+ (data.get('processedAt') or data['createdAt'])[:19].replace('T', ' ')),
+ 'client_order_ref': data['name'],
+ 'origin': data['name'],
+ 'note': data.get('note') or False,
+ 'shopify_store_id': store.id,
+ 'shopify_order_gid': data['id'],
+ 'shopify_order_name': data['name'],
+ 'shopify_financial_status': data.get('displayFinancialStatus'),
+ 'shopify_fulfillment_status': data.get('displayFulfillmentStatus'),
+ 'shopify_gateway_id': gateway.id,
+ 'shopify_workflow_id': workflow.id,
+ 'shopify_source': data.get('sourceName'),
+ 'team_id': store.team_id.id or False,
+ 'user_id': store.user_id.id or False,
+ }
+ if store.order_use_shopify_name:
+ vals['name'] = f"{store.order_prefix or ''}{data['name']}"
+ currency_code = data.get('presentmentCurrencyCode') if money == 'presentmentMoney' else data.get('currencyCode')
+ pricelist = store._shopify_pricelist(currency_code)
+ if pricelist:
+ vals['pricelist_id'] = pricelist.id
+ if tags := [t for t in data.get('tags') or [] if t]:
+ Tag = self.env['crm.tag']
+ vals['tag_ids'] = [fields.Command.set([
+ (Tag.search([('name', '=', tag)], limit=1) or Tag.create({'name': tag})).id for tag in tags])]
+ vals.update(self._shopify_risk_vals(data))
+ if b2b and b2b.get('payment_term'):
+ vals['payment_term_id'] = b2b['payment_term'].id
+ if b2b and b2b.get('fiscal_position'):
+ vals['fiscal_position_id'] = b2b['fiscal_position'].id
+ order = self.with_company(store.company_id).create(vals)
+ order.order_line = [fields.Command.create(line) for line in self._shopify_line_vals(store, data, money)]
+ order._shopify_check_total(data, money)
+ if order.shopify_on_hold:
+ order._shopify_hold_activity()
+ return order
+
+ @api.model
+ def _shopify_b2b_values(self, store, data):
+ """ The company, addresses, payment terms and fiscal position of a B2B order, or {}. """
+ entity = data.get('purchasingEntity') or {}
+ company_gid = (entity.get('company') or {}).get('id')
+ if not store.b2b_import or not company_gid:
+ return {}
+ Company = self.env['om.shopify.company']
+ if not Company._connector_find(store, company_gid):
+ Company._shopify_import_company(store, company_gid)
+ company = Company._connector_find(store, company_gid)
+ location = self.env['om.shopify.company.location']._connector_find(
+ store, (entity.get('location') or {}).get('id') or '')
+ if not company:
+ return {}
+ partner = company.partner_id
+ contact = self.env['om.shopify.customer']._connector_find(
+ store, (data.get('customer') or {}).get('id') or '').partner_id
+ if contact and contact.commercial_partner_id == partner:
+ partner = contact
+ delivery = location.partner_id or partner
+ if data.get('shippingAddress'):
+ delivery = self.env['om.shopify.customer']._shopify_child(company.partner_id, 'delivery',
+ data['shippingAddress'])
+ return {
+ 'partner': partner,
+ 'invoice': location.invoice_partner_id or company.partner_id,
+ 'delivery': delivery,
+ 'payment_term': location.payment_term_id,
+ 'fiscal_position': store.b2b_tax_exempt_fiscal_position_id if location.tax_exempt else False,
+ }
+
+ @api.model
+ def _shopify_risk_vals(self, data):
+ risk = data.get('risk') or {}
+ recommendation = risk.get('recommendation')
+ facts = [fact.get('description') for assessment in risk.get('assessments') or []
+ for fact in assessment.get('facts') or [] if fact.get('description')]
+ return {
+ 'shopify_risk': recommendation if recommendation in ('ACCEPT', 'INVESTIGATE', 'CANCEL', 'NONE') else False,
+ 'shopify_risk_message': '\n'.join(facts) or False,
+ 'shopify_on_hold': recommendation in RISKY,
+ }
+
+ def _shopify_hold_activity(self):
+ self.ensure_one()
+ self.activity_schedule(
+ 'mail.mail_activity_data_todo', user_id=(self.shopify_store_id.responsible_id or self.env.user).id,
+ summary=_("Risky Shopify order"),
+ note=_("Shopify recommends to %(action)s this order. Check it, then click Release or cancel it.",
+ action=dict(self._fields['shopify_risk'].selection).get(self.shopify_risk, '').lower()))
+
+ @api.model
+ def _shopify_main_gateway(self, data):
+ names = [n for n in data.get('paymentGatewayNames') or [] if n != 'gift_card']
+ return names[0] if names else (data.get('paymentGatewayNames') or ['manual'])[0]
+
+ @api.model
+ def _shopify_line_vals(self, store, data, money='shopMoney'):
+ def amount(money_set):
+ return float(((money_set or {}).get(money) or {}).get('amount') or 0.0)
+
+ taxes_included = bool(data.get('taxesIncluded'))
+ lines = []
+ for item in data['lineItems']['nodes']:
+ quantity = item.get('currentQuantity', item.get('quantity')) or 0
+ if quantity <= 0:
+ continue
+ lines.append(self._shopify_product_line_vals(store, item, quantity, amount, taxes_included))
+ for shipping in (data.get('shippingLines') or {}).get('nodes') or []:
+ price = amount(shipping.get('originalPriceSet'))
+ discount_amount = sum(amount(a.get('allocatedAmountSet')) for a in shipping.get('discountAllocations') or [])
+ vals = {
+ 'product_id': store.shipping_product_id.id,
+ 'name': shipping.get('title') or store.shipping_product_id.name,
+ 'product_uom_qty': 1,
+ 'price_unit': price,
+ 'discount': self._shopify_discount(price, discount_amount),
+ 'is_delivery': True,
+ }
+ if store.tax_mode == 'shopify':
+ vals['tax_id'] = [fields.Command.set(store._shopify_taxes(shipping.get('taxLines'), taxes_included).ids)]
+ lines.append(vals)
+ if tip := amount(data.get('totalTipReceivedSet')):
+ lines.append({'product_id': store.tip_product_id.id, 'name': store.tip_product_id.name,
+ 'product_uom_qty': 1, 'price_unit': tip, 'tax_id': [fields.Command.clear()]})
+ if duties := amount(data.get('currentTotalDutiesSet')):
+ lines.append({'product_id': store.duties_product_id.id, 'name': store.duties_product_id.name,
+ 'product_uom_qty': 1, 'price_unit': duties, 'tax_id': [fields.Command.clear()]})
+ return lines
+
+ @api.model
+ def _shopify_product_line_vals(self, store, item, quantity, amount, taxes_included):
+ price = amount(item.get('originalUnitPriceSet'))
+ product = store.gift_card_product_id if item.get('isGiftCard') else self._shopify_line_product(store, item)
+ discount_amount = sum(amount(a.get('allocatedAmountSet')) for a in item.get('discountAllocations') or [])
+ vals = {
+ 'product_id': product.id,
+ 'name': item.get('name') or product.display_name,
+ 'product_uom_qty': quantity,
+ 'price_unit': price,
+ 'discount': self._shopify_discount(price * quantity, discount_amount),
+ 'shopify_line_gid': item['id'],
+ }
+ if store.tax_mode == 'shopify':
+ vals['tax_id'] = [fields.Command.set(store._shopify_taxes(item.get('taxLines'), taxes_included).ids)]
+ return vals
+
+ @api.model
+ def _shopify_discount(self, amount, discount_amount):
+ if not amount or not discount_amount:
+ return 0.0
+ return float_round(discount_amount / amount * 100.0, precision_digits=6)
+
+ @api.model
+ def _shopify_line_product(self, store, item):
+ """ The Odoo product of an order line: the linked variant, imported when it is not yet,
+ else the custom item product for the lines without a product in Shopify. """
+ Variant = self.env['om.shopify.product']
+ variant_gid = (item.get('variant') or {}).get('id')
+ if variant_gid:
+ binding = Variant._connector_find(store, variant_gid).filtered('active')
+ if not binding and (item.get('product') or {}).get('id'):
+ self.env['om.shopify.product.template']._shopify_import_product(store, item['product']['id'])
+ binding = Variant._connector_find(store, variant_gid)
+ if binding:
+ return binding.product_id
+ if item.get('sku'):
+ match = Variant._shopify_match(store, {'sku': item['sku']})
+ if match:
+ return match
+ return store.custom_product_id
+
+ def _shopify_check_total(self, data, money='shopMoney'):
+ """ Note on the order when its total is not the one of Shopify (a rounding, a tax set up
+ differently): the order is kept, the user checks it. """
+ self.ensure_one()
+ total = float(((data.get('totalPriceSet') or {}).get(money) or {}).get('amount') or 0.0)
+ if self.currency_id.compare_amounts(self.amount_total, total) != 0:
+ self.message_post(body=_(
+ "The total of this order in Odoo (%(odoo)s) is not the total in Shopify (%(shopify)s): check its "
+ "taxes and discounts.",
+ odoo=self.currency_id.format(self.amount_total), shopify=self.currency_id.format(total)))
+
+ # ------------------------------------------------------------------
+ # Workflow, payments, deliveries
+ # ------------------------------------------------------------------
+
+ def _shopify_workflow(self):
+ self.ensure_one()
+ return self.shopify_workflow_id or self.shopify_gateway_id.workflow_id or self.shopify_store_id.workflow_id
+
+ def _shopify_process(self, data):
+ """ Run the workflow, register the payments, deliver what is fulfilled, and import the refunds. """
+ self.ensure_one()
+ if self.shopify_on_hold or self.state == 'cancel':
+ return
+ workflow = self._shopify_workflow()
+ if not workflow:
+ return
+ date_order = self.date_order
+ workflow._connector_process(self, paid=False)
+ if self.date_order != date_order:
+ # confirming dates the order of today: it keeps the date of Shopify
+ self.date_order = date_order
+ if data.get('displayFinancialStatus') in PAID_STATUSES or self._shopify_payments(data):
+ self._shopify_register_payments(data, workflow)
+ if self.state == 'sale' and data.get('displayFulfillmentStatus') == 'FULFILLED' \
+ and self.shopify_store_id.order_deliver_fulfilled:
+ self._shopify_deliver()
+ self._shopify_import_refunds(data)
+
+ @api.model
+ def _shopify_payments(self, data, money='shopMoney'):
+ """ ``{gateway: amount}`` received for the order, refunds apart. """
+ paid = defaultdict(float)
+ for transaction in data.get('transactions') or []:
+ if transaction.get('status') == 'SUCCESS' and transaction.get('kind') in PAYMENT_KINDS:
+ paid[transaction.get('gateway') or 'manual'] += float(
+ ((transaction.get('amountSet') or {}).get(money) or {}).get('amount') or 0.0)
+ return dict(paid)
+
+ def _shopify_register_payments(self, data, workflow):
+ """ One payment per payment method of the order, in the journal of its workflow. """
+ self.ensure_one()
+ if not workflow.register_payment:
+ return
+ money = self._shopify_money_key(self.shopify_store_id, data)
+ payments = self._shopify_payments(data, money)
+ if len(payments) <= 1:
+ if data.get('displayFinancialStatus') in PAID_STATUSES:
+ gateway_name = next(iter(payments), None)
+ journal = self._shopify_gateway_journal(gateway_name, workflow)
+ workflow._connector_register_payment(self, journal=journal)
+ return
+ invoices = self.invoice_ids.filtered(lambda m: m.move_type == 'out_invoice' and m.state == 'posted')
+ already = sum(invoices.mapped(lambda m: m.amount_total - m.amount_residual)) \
+ + workflow._connector_pending_payments(invoices)
+ for gateway_name, amount in sorted(payments.items()):
+ take = min(amount, already)
+ already -= take
+ workflow._connector_register_payment(self, amount=amount - take,
+ journal=self._shopify_gateway_journal(gateway_name, workflow))
+
+ def _shopify_gateway_journal(self, gateway_name, workflow):
+ if not gateway_name:
+ return workflow.journal_id
+ gateway = self.env['om.shopify.gateway']._shopify_get(self.shopify_store_id, gateway_name)
+ return gateway.workflow_id.journal_id or workflow.journal_id
+
+ def _shopify_invoice_delivered(self):
+ """ After a delivery: invoice the products invoiced on delivered quantities, and register
+ their payment when Shopify says the order is paid. """
+ for order in self.filtered(lambda o: o.state == 'sale' and not o.shopify_on_hold):
+ workflow = order._shopify_workflow()
+ if not workflow or not workflow.create_invoice or order.invoice_status != 'to invoice':
+ continue
+ invoices = order._create_invoices()
+ if workflow.post_invoice:
+ invoices.action_post()
+ if workflow.register_payment and order.shopify_financial_status in PAID_STATUSES:
+ workflow._connector_register_payment(
+ order, journal=order._shopify_gateway_journal(order.shopify_gateway_id.name, workflow))
+
+ def _shopify_deliver(self):
+ """ The order was fulfilled in Shopify before it reached Odoo: its deliveries are done. """
+ self.ensure_one()
+ pickings = self.picking_ids.filtered(lambda p: p.state not in ('done', 'cancel')
+ and p.picking_type_code == 'outgoing')
+ if not pickings:
+ return
+ for move in pickings.move_ids.filtered(lambda m: m.state not in ('done', 'cancel')):
+ move.quantity = move.product_uom_qty
+ move.picked = True
+ pickings.with_context(skip_backorder=True, skip_sanity_check=True, om_shopify_no_fulfill=True)._action_done()
+ self._shopify_invoice_delivered()
+
+ # ------------------------------------------------------------------
+ # Updates
+ # ------------------------------------------------------------------
+
+ def _shopify_update(self, data):
+ """ Apply the news of a Shopify order to the sale order imported before. """
+ self.ensure_one()
+ vals = {
+ 'shopify_financial_status': data.get('displayFinancialStatus'),
+ 'shopify_fulfillment_status': data.get('displayFulfillmentStatus'),
+ }
+ if data.get('risk') and not self.shopify_risk:
+ vals.update({k: v for k, v in self._shopify_risk_vals(data).items() if k != 'shopify_on_hold'})
+ self.write(vals)
+ if data.get('cancelledAt'):
+ if self.state != 'cancel':
+ self._shopify_cancel(data)
+ self._shopify_import_refunds(data)
+ return self
+ if self.state == 'cancel':
+ return self
+ if self.shopify_store_id.order_sync_edits:
+ self._shopify_apply_edits(data)
+ self._shopify_process(data)
+ return self
+
+ def _shopify_apply_edits(self, data):
+ """ Lines added, removed or changed in Shopify after the import, while nothing is delivered
+ or invoiced in Odoo yet. """
+ self.ensure_one()
+ if self.invoice_ids.filtered(lambda m: m.state == 'posted') or \
+ self.picking_ids.filtered(lambda p: p.state == 'done'):
+ return
+ store = self.shopify_store_id
+ money = self._shopify_money_key(store, data)
+
+ def amount(money_set):
+ return float(((money_set or {}).get(money) or {}).get('amount') or 0.0)
+
+ lines = {line.shopify_line_gid: line for line in self.order_line if line.shopify_line_gid}
+ commands, changes = [], []
+ for item in data['lineItems']['nodes']:
+ quantity = item.get('currentQuantity', item.get('quantity')) or 0
+ line = lines.get(item['id'])
+ if line:
+ if line.product_uom_qty != quantity:
+ changes.append(_("%(product)s: %(old)s → %(new)s", product=line.name,
+ old=line.product_uom_qty, new=quantity))
+ commands.append(fields.Command.update(line.id, {'product_uom_qty': quantity}))
+ elif quantity > 0:
+ vals = self._shopify_product_line_vals(store, item, quantity, amount, bool(data.get('taxesIncluded')))
+ changes.append(_("%(product)s: added (%(qty)s)", product=vals['name'], qty=quantity))
+ commands.append(fields.Command.create(vals))
+ if commands:
+ self.order_line = commands
+ self.message_post(body=_("Edited in Shopify: %(changes)s", changes='; '.join(changes)))
+
+ def _shopify_cancel(self, data):
+ self.ensure_one()
+ self.shopify_cancelled = True
+ delivered = self.picking_ids.filtered(lambda p: p.state == 'done')
+ posted = self.invoice_ids.filtered(lambda m: m.state == 'posted' and m.move_type == 'out_invoice')
+ reason = data.get('cancelReason') or _("no reason")
+ if delivered or posted:
+ self.message_post(body=_(
+ "This order was cancelled in Shopify (%(reason)s), but it is already delivered or invoiced in Odoo: "
+ "its refunds are imported as credit notes; return the goods by hand.", reason=reason))
+ return
+ self.invoice_ids.filtered(lambda m: m.state == 'draft').button_cancel()
+ self.with_context(disable_cancel_warning=True).action_cancel()
+ self.message_post(body=_("Cancelled in Shopify (%(reason)s).", reason=reason))
+
+ # ------------------------------------------------------------------
+ # Refunds from Shopify
+ # ------------------------------------------------------------------
+
+ def _shopify_import_refunds(self, data):
+ """ A credit note for each refund of Shopify not imported yet, paid back in the journal of the
+ payment method; and a return of the products Shopify restocked, when asked. """
+ self.ensure_one()
+ store = self.shopify_store_id
+ if not store.refund_import:
+ return
+ known = set((self.shopify_refund_gids or '').split())
+ known |= set(self.env['account.move'].search([('shopify_store_id', '=', store.id),
+ ('shopify_refund_gid', '!=', False)]).mapped('shopify_refund_gid'))
+ money = self._shopify_money_key(store, data)
+ for refund in data.get('refunds') or []:
+ if refund['id'] in known:
+ continue
+ self._shopify_credit_note(refund, money)
+ self._shopify_reduce_refunded(refund, data)
+ if store.refund_create_return:
+ self._shopify_return(refund)
+ known.add(refund['id'])
+ self.shopify_refund_gids = '\n'.join(filter(None, [self.shopify_refund_gids, refund['id']]))
+
+ def _shopify_credit_note(self, refund, money):
+ self.ensure_one()
+ store = self.shopify_store_id
+
+ def amount(money_set):
+ return float(((money_set or {}).get(money) or {}).get('amount') or 0.0)
+
+ total = amount(refund.get('totalRefundedSet'))
+ if not total:
+ return self.env['account.move']
+ invoice = self.invoice_ids.filtered(lambda m: m.move_type == 'out_invoice' and m.state == 'posted')[:1]
+ if not invoice:
+ self.message_post(body=_("Shopify refunded %(amount)s before this order was invoiced: the refunded "
+ "quantities leave the order, no credit note is needed.",
+ amount=self.currency_id.format(total)))
+ return self.env['account.move']
+ lines_by_gid = {line.shopify_line_gid: line for line in self.order_line if line.shopify_line_gid}
+ line_vals = []
+ for refund_line in (refund.get('refundLineItems') or {}).get('nodes') or []:
+ sale_line = lines_by_gid.get((refund_line.get('lineItem') or {}).get('id'))
+ # only what was invoiced is credited on its line; the rest is in the adjustment
+ quantity = min(refund_line.get('quantity') or 0, sale_line.qty_invoiced if sale_line else 0)
+ if quantity <= 0:
+ continue
+ line_vals.append({
+ 'product_id': sale_line.product_id.id,
+ 'name': sale_line.name,
+ 'quantity': quantity,
+ 'price_unit': sale_line.price_unit,
+ 'discount': sale_line.discount,
+ 'tax_ids': [fields.Command.set(sale_line.tax_id.ids)],
+ 'sale_line_ids': [fields.Command.link(sale_line.id)],
+ })
+ shipping = sum(amount(s.get('subtotalAmountSet'))
+ for s in (refund.get('refundShippingLines') or {}).get('nodes') or [])
+ if shipping:
+ delivery = self.order_line.filtered('is_delivery')[:1]
+ line_vals.append({
+ 'product_id': store.shipping_product_id.id,
+ 'name': _("Shipping refund"),
+ 'quantity': 1,
+ 'price_unit': shipping,
+ 'tax_ids': [fields.Command.set(delivery.tax_id.ids)] if delivery else [fields.Command.clear()],
+ 'sale_line_ids': [fields.Command.link(delivery.id)] if delivery else [],
+ })
+ credit_note = self.env['account.move'].with_company(self.company_id).create({
+ 'move_type': 'out_refund',
+ 'partner_id': self.partner_invoice_id.id,
+ 'invoice_origin': self.name,
+ 'ref': _("Shopify refund %(order)s", order=self.shopify_order_name),
+ 'reversed_entry_id': invoice.id,
+ 'currency_id': self.currency_id.id,
+ 'journal_id': invoice.journal_id.id,
+ 'shopify_store_id': store.id,
+ 'shopify_refund_gid': refund['id'],
+ 'narration': refund.get('note') or False,
+ 'invoice_line_ids': [fields.Command.create(vals) for vals in line_vals],
+ })
+ # what Shopify refunded besides the lines and the shipping: an adjustment
+ difference = self.currency_id.round(total - credit_note.amount_total)
+ if not self.currency_id.is_zero(difference):
+ credit_note.invoice_line_ids = [fields.Command.create({
+ 'product_id': store.refund_adjustment_product_id.id,
+ 'name': _("Refund adjustment"),
+ 'quantity': 1,
+ 'price_unit': difference,
+ 'tax_ids': [fields.Command.clear()],
+ })]
+ credit_note.action_post()
+ workflow = self._shopify_workflow()
+ if workflow and workflow.register_payment:
+ for transaction in (refund.get('transactions') or {}).get('nodes') or []:
+ if transaction.get('status') == 'SUCCESS' and transaction.get('kind') == 'REFUND':
+ workflow._connector_refund_payment(
+ credit_note, amount=amount(transaction.get('amountSet')),
+ journal=self._shopify_gateway_journal(transaction.get('gateway'), workflow))
+ self.message_post(body=_("Refund of Shopify imported: %(move)s.", move=credit_note._get_html_link()))
+ return credit_note
+
+ def _shopify_reduce_refunded(self, refund, data):
+ """ The refunded lines get the quantity left in Shopify (never less than delivered), so that
+ the order is not invoiced again for what was refunded. """
+ self.ensure_one()
+ current = {item['id']: item.get('currentQuantity', item.get('quantity')) or 0
+ for item in data['lineItems']['nodes']}
+ lines_by_gid = {line.shopify_line_gid: line for line in self.order_line if line.shopify_line_gid}
+ for refund_line in (refund.get('refundLineItems') or {}).get('nodes') or []:
+ gid = (refund_line.get('lineItem') or {}).get('id')
+ line = lines_by_gid.get(gid)
+ if line and gid in current:
+ new_qty = max(current[gid], line.qty_delivered)
+ if new_qty < line.product_uom_qty:
+ line.product_uom_qty = new_qty
+
+ def _shopify_return(self, refund):
+ """ Return to stock the products Shopify restocked with this refund. """
+ self.ensure_one()
+ quantities = defaultdict(float)
+ for refund_line in (refund.get('refundLineItems') or {}).get('nodes') or []:
+ if refund_line.get('restockType') == 'RETURN' and refund_line.get('quantity'):
+ quantities[(refund_line.get('lineItem') or {}).get('id')] += refund_line['quantity']
+ picking = self.picking_ids.filtered(lambda p: p.state == 'done' and p.picking_type_code == 'outgoing')[:1]
+ if not quantities or not picking:
+ return
+ wizard = self.env['stock.return.picking'].create({'picking_id': picking.id})
+ for return_line in wizard.product_return_moves:
+ origin = return_line.move_id
+ wanted = min(quantities.get(origin.sale_line_id.shopify_line_gid, 0.0), origin.quantity)
+ return_line.quantity = max(wanted, 0.0)
+ if wanted > 0:
+ quantities[origin.sale_line_id.shopify_line_gid] -= wanted
+ if not any(line.quantity > 0 for line in wizard.product_return_moves):
+ return None
+ returned = wizard._create_return()
+ if self.shopify_store_id.refund_validate_return:
+ for move in returned.move_ids:
+ move.quantity = move.product_uom_qty
+ move.picked = True
+ returned.with_context(skip_backorder=True, om_shopify_no_fulfill=True)._action_done()
+ return returned
+
+
+class SaleOrderLine(models.Model):
+ _inherit = 'sale.order.line'
+
+ shopify_line_gid = fields.Char(string='Shopify Line ID', readonly=True, copy=False)
diff --git a/om_shopify/models/stock_picking.py b/om_shopify/models/stock_picking.py
new file mode 100644
index 00000000..91df1fd8
--- /dev/null
+++ b/om_shopify/models/stock_picking.py
@@ -0,0 +1,90 @@
+import logging
+from collections import defaultdict
+
+from odoo import _, api, fields, models
+
+from ..tools import queries
+
+_logger = logging.getLogger(__name__)
+
+OPEN_FULFILLMENT_STATUSES = ('OPEN', 'IN_PROGRESS')
+
+
+class StockPicking(models.Model):
+ _inherit = 'stock.picking'
+
+ shopify_fulfillment_gid = fields.Char(string='Shopify Fulfillment', readonly=True, copy=False)
+
+ def _action_done(self):
+ res = super()._action_done()
+ self.filtered(lambda p: p.picking_type_code == 'outgoing').sale_id.filtered(
+ 'shopify_store_id')._shopify_invoice_delivered()
+ if self.env.context.get('om_shopify_no_fulfill'):
+ return res
+ for picking in self:
+ order = picking.sale_id
+ if picking.picking_type_code == 'outgoing' and order.shopify_store_id and order.shopify_store_id.fulfillment_push:
+ order.shopify_store_id._connector_enqueue(
+ '_connector_job_shopify_fulfill', {'picking_id': picking.id}, model_name='stock.picking',
+ name=_("Fulfill %(order)s (%(picking)s)", order=order.shopify_order_name, picking=picking.name),
+ dedupe_key=f'fulfill:{picking.id}')
+ return res
+
+ @api.model
+ def _connector_job_shopify_fulfill(self, job):
+ picking = self.browse(job.payload['picking_id']).exists()
+ if not picking or picking.state != 'done':
+ return _("Nothing to fulfill")
+ return picking._shopify_fulfill(job.backend)
+
+ def _shopify_fulfill(self, store):
+ """ Tell Shopify that the delivered products of this picking are fulfilled, with the tracking
+ number. Gives the picking. """
+ self.ensure_one()
+ order = self.sale_id
+ quantities = defaultdict(float)
+ for move in self.move_ids.filtered(lambda m: m.state == 'done' and m.sale_line_id.shopify_line_gid):
+ quantities[move.sale_line_id.shopify_line_gid] += move.product_uom._compute_quantity(
+ move.quantity, move.sale_line_id.product_uom)
+ if not quantities:
+ return _("No Shopify line in this picking")
+ data = store._shopify_execute(queries.FULFILLMENT_ORDERS, {'id': order.shopify_order_gid}).get('order') or {}
+ by_location = defaultdict(list)
+ for fulfillment_order in (data.get('fulfillmentOrders') or {}).get('nodes') or []:
+ if fulfillment_order.get('status') not in OPEN_FULFILLMENT_STATUSES:
+ continue
+ items = []
+ for line in (fulfillment_order.get('lineItems') or {}).get('nodes') or []:
+ line_gid = (line.get('lineItem') or {}).get('id')
+ quantity = min(int(quantities.get(line_gid, 0)), line.get('remainingQuantity') or 0)
+ if quantity > 0:
+ items.append({'id': line['id'], 'quantity': quantity})
+ quantities[line_gid] -= quantity
+ if items:
+ location = ((fulfillment_order.get('assignedLocation') or {}).get('location') or {}).get('id')
+ by_location[location].append({'fulfillmentOrderId': fulfillment_order['id'],
+ 'fulfillmentOrderLineItems': items})
+ if not by_location:
+ return _("Already fulfilled in Shopify")
+ tracking = self._shopify_tracking_info()
+ gids = []
+ for line_items in by_location.values():
+ fulfillment = {'lineItemsByFulfillmentOrder': line_items, 'notifyCustomer': store.fulfillment_notify}
+ if tracking:
+ fulfillment['trackingInfo'] = tracking
+ result = store._shopify_mutate(queries.FULFILLMENT_CREATE, {'fulfillment': fulfillment}, 'fulfillmentCreate')
+ gids.append((result.get('fulfillment') or {}).get('id'))
+ self.shopify_fulfillment_gid = ', '.join(filter(None, gids))
+ return self
+
+ def _shopify_tracking_info(self):
+ self.ensure_one()
+ if not self.carrier_tracking_ref:
+ return {}
+ info = {'number': self.carrier_tracking_ref}
+ if self.carrier_id:
+ info['company'] = self.carrier_id.name
+ url = self.carrier_tracking_url if 'carrier_tracking_url' in self._fields else False
+ if url:
+ info['url'] = url
+ return info
diff --git a/om_shopify/security/ir.model.access.csv b/om_shopify/security/ir.model.access.csv
new file mode 100644
index 00000000..ab9ca0f9
--- /dev/null
+++ b/om_shopify/security/ir.model.access.csv
@@ -0,0 +1,32 @@
+id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
+access_om_shopify_store_user,access.om.shopify.store.user,model_om_shopify_store,group_om_shopify_user,1,0,0,0
+access_om_shopify_store_manager,access.om.shopify.store.manager,model_om_shopify_store,group_om_shopify_manager,1,1,1,1
+access_om_shopify_webhook_user,access.om.shopify.webhook.user,model_om_shopify_webhook,group_om_shopify_user,1,0,0,0
+access_om_shopify_webhook_manager,access.om.shopify.webhook.manager,model_om_shopify_webhook,group_om_shopify_manager,1,1,1,1
+access_om_shopify_location_user,access.om.shopify.location.user,model_om_shopify_location,group_om_shopify_user,1,0,0,0
+access_om_shopify_location_manager,access.om.shopify.location.manager,model_om_shopify_location,group_om_shopify_manager,1,1,1,1
+access_om_shopify_gateway_user,access.om.shopify.gateway.user,model_om_shopify_gateway,group_om_shopify_user,1,0,0,0
+access_om_shopify_gateway_manager,access.om.shopify.gateway.manager,model_om_shopify_gateway,group_om_shopify_manager,1,1,1,1
+access_om_shopify_product_template_user,access.om.shopify.product.template.user,model_om_shopify_product_template,group_om_shopify_user,1,0,0,0
+access_om_shopify_product_template_manager,access.om.shopify.product.template.manager,model_om_shopify_product_template,group_om_shopify_manager,1,1,1,1
+access_om_shopify_product_user,access.om.shopify.product.user,model_om_shopify_product,group_om_shopify_user,1,0,0,0
+access_om_shopify_product_manager,access.om.shopify.product.manager,model_om_shopify_product,group_om_shopify_manager,1,1,1,1
+access_om_shopify_customer_user,access.om.shopify.customer.user,model_om_shopify_customer,group_om_shopify_user,1,0,0,0
+access_om_shopify_customer_manager,access.om.shopify.customer.manager,model_om_shopify_customer,group_om_shopify_manager,1,1,1,1
+access_om_shopify_order_rule_user,access.om.shopify.order.rule.user,model_om_shopify_order_rule,group_om_shopify_user,1,0,0,0
+access_om_shopify_order_rule_manager,access.om.shopify.order.rule.manager,model_om_shopify_order_rule,group_om_shopify_manager,1,1,1,1
+access_om_shopify_payout_user,access.om.shopify.payout.user,model_om_shopify_payout,group_om_shopify_user,1,0,0,0
+access_om_shopify_payout_manager,access.om.shopify.payout.manager,model_om_shopify_payout,group_om_shopify_manager,1,1,1,1
+access_om_shopify_cancel_wizard,access.om.shopify.cancel.wizard,model_om_shopify_cancel_wizard,group_om_shopify_user,1,1,1,1
+access_om_shopify_export_wizard,access.om.shopify.export.wizard,model_om_shopify_export_wizard,group_om_shopify_user,1,1,1,1
+access_om_shopify_import_wizard,access.om.shopify.import.wizard,model_om_shopify_import_wizard,group_om_shopify_user,1,1,1,1
+access_om_shopify_mapping_wizard,access.om.shopify.mapping.wizard,model_om_shopify_mapping_wizard,group_om_shopify_manager,1,1,1,1
+access_om_shopify_setup_wizard,access.om.shopify.setup.wizard,model_om_shopify_setup_wizard,group_om_shopify_manager,1,1,1,1
+access_om_shopify_metafield_user,access.om.shopify.metafield.user,model_om_shopify_metafield,group_om_shopify_user,1,0,0,0
+access_om_shopify_metafield_manager,access.om.shopify.metafield.manager,model_om_shopify_metafield,group_om_shopify_manager,1,1,1,1
+access_om_shopify_company_user,access.om.shopify.company.user,model_om_shopify_company,group_om_shopify_user,1,0,0,0
+access_om_shopify_company_manager,access.om.shopify.company.manager,model_om_shopify_company,group_om_shopify_manager,1,1,1,1
+access_om_shopify_company_location_user,access.om.shopify.company.location.user,model_om_shopify_company_location,group_om_shopify_user,1,0,0,0
+access_om_shopify_company_location_manager,access.om.shopify.company.location.manager,model_om_shopify_company_location,group_om_shopify_manager,1,1,1,1
+access_om_shopify_price_list_user,access.om.shopify.price.list.user,model_om_shopify_price_list,group_om_shopify_user,1,0,0,0
+access_om_shopify_price_list_manager,access.om.shopify.price.list.manager,model_om_shopify_price_list,group_om_shopify_manager,1,1,1,1
diff --git a/om_shopify/security/ir_rule.xml b/om_shopify/security/ir_rule.xml
new file mode 100644
index 00000000..ac4556bd
--- /dev/null
+++ b/om_shopify/security/ir_rule.xml
@@ -0,0 +1,80 @@
+
+
+
+ om.shopify.store: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.webhook: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.location: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.gateway: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.product.template: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.product: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.customer: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.order.rule: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.payout: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.metafield: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.company: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.company.location: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
+
+ om.shopify.price.list: multi-company
+
+ [('company_id', 'in', company_ids)]
+
+
diff --git a/om_shopify/security/om_shopify_groups.xml b/om_shopify/security/om_shopify_groups.xml
new file mode 100644
index 00000000..01e6b65e
--- /dev/null
+++ b/om_shopify/security/om_shopify_groups.xml
@@ -0,0 +1,23 @@
+
+
+
+ Shopify
+
+ 50
+
+
+
+ User
+
+
+ Sees the Shopify stores, runs the imports and exports, and follows their jobs.
+
+
+
+ Administrator
+
+
+
+ Connects the Shopify stores, sees their credentials and sets them up.
+
+
diff --git a/om_shopify/static/description/banner.gif b/om_shopify/static/description/banner.gif
new file mode 100644
index 00000000..ea87925a
Binary files /dev/null and b/om_shopify/static/description/banner.gif differ
diff --git a/om_shopify/static/description/dashboard.png b/om_shopify/static/description/dashboard.png
new file mode 100644
index 00000000..4dec781e
Binary files /dev/null and b/om_shopify/static/description/dashboard.png differ
diff --git a/om_shopify/static/description/icon.png b/om_shopify/static/description/icon.png
new file mode 100644
index 00000000..4f0a9242
Binary files /dev/null and b/om_shopify/static/description/icon.png differ
diff --git a/om_shopify/static/description/index.html b/om_shopify/static/description/index.html
new file mode 100644
index 00000000..3b69d097
--- /dev/null
+++ b/om_shopify/static/description/index.html
@@ -0,0 +1,536 @@
+
+
+
Odoo Mates · free and open source
+
Shopify Connector
+
Products, stock, customers, orders, fulfillment, refunds and payouts between your Shopify stores and Odoo, through webhooks and a queue you can follow.
+
+Beta
+Products both ways
+Stock both ways
+Orders invoiced and paid
+Payouts
+Community Edition
+Free, LGPL-3
+
+
Needs the free Connector Base module (om_connector_base), installed with it.
+
+
+
+
BetaThis app is in beta: tell us what you think
+
The Shopify Connector is new and we are still shaping it with the stores that use it. We welcome your feedback and suggestions, and if something does not look right, report the issue and let us know: every message is read and helps decide what comes next.
One app for the whole life of a Shopify order, from the product page to the bank.
+
+
+
+
+
+
+
+
+
+
Products Both Ways
+
Linked by SKU or barcode, created with variants, or exported.
+
+
+
+
+
+
+
+
+
+
+
Prices
+
From a price list, with compare-at prices and Shopify price lists.
+
+
+
+
+
+
+
+
+
+
+
Stock Both Ways
+
Sent per location with limits per variant, or imported.
+
+
+
+
+
+
+
+
+
+
+
Orders on Autopilot
+
Confirmed, invoiced and paid by a workflow per payment method.
+
+
+
+
+
+
+
+
+
+
+
Fulfillment
+
Validate the delivery: Shopify gets the tracking number.
+
+
+
+
+
+
+
+
+
+
+
Refunds and Cancels
+
Both ways, with credit notes and returns.
+
+
+
+
+
+
+
+
+
+
+
Payouts
+
Bank statement lines, or a journal entry through a transit account.
+
+
+
+
+
+
+
+
+
+
+
Webhooks and Jobs
+
Orders in seconds; every sync is a job tried again on failure.
+
+
+
+
+
+
In detail
+
Everything it syncs
+
Each part can be turned on or off store by store, in the tabs of the store.
+
+
+
+
+
Connection
+
Dev Dashboard app installed by link (OAuth), or a custom app token. Expiring tokens renewed and rotated by Odoo.
+
+
+
+
+
Setup Wizard
+
Credentials, warehouse and payment journal, then the first imports, in three steps.
+
+
+
+
+
Product Import
+
Bulk import of the whole catalogue, changes every day and by webhook, images included.
+
+
+
+
+
Product Export
+
Variants, prices, weight and image; as draft, active or unlisted; published on the Online Store.
+
+
+
+
+
CSV / Excel Mapping
+
Link catalogues whose SKUs and barcodes do not match from a file.
+
+
+
+
+
Metafields
+
Shopify metafields of products, variants, customers and orders mapped to Odoo fields, one or both ways.
+
+
+
+
+
Prices
+
From an Odoo price list, compare-at prices from another, sent every day; fixed prices to Shopify price lists.
+
+
+
+
+
Stock Export
+
Free to use, on hand or forecasted, per location every 15 minutes, with limits per variant.
+
+
+
+
+
Stock Import
+
Compare the stock of Shopify with Odoo, and apply it if you want.
+
+
+
+
+
Customers
+
Imported with their addresses, matched by email, on demand or with the orders.
+
+
+
+
+
B2B Companies
+
Companies and locations of Shopify B2B, with payment terms and tax exemption.
+
+
+
+
+
Orders
+
By webhook, every 15 minutes, or by order number; test and point of sale orders if you want.
+
+
+
+
+
Order Lines
+
Discounts, shipping, taxes, tips, duties, gift cards and custom items, each on its own product.
+
+
+
+
+
Order Rules
+
Workflow and warehouse chosen by payment method, payment or fulfillment status, tag or channel.
+
+
+
+
+
Several Payments
+
An order paid with two methods gets two payments, each in the journal of its method.
+
+
+
+
+
Risky Orders
+
Held as quotations with the reasons of Shopify and an activity for the responsible of the store.
+
+
+
+
+
Order Edits
+
Lines added, removed or changed in Shopify follow until delivery or invoice.
+
+
+
+
+
Fulfillment
+
Validated deliveries fulfill the order in Shopify, with carrier and tracking number, and email the customer.
+
+
+
+
+
Cancellations
+
Cancelled in Shopify, cancelled in Odoo; or cancel in Shopify from Odoo, refund and restock included.
+
+
+
+
+
Refunds
+
Shopify refunds become paid credit notes and returns; refund a credit note in Shopify from Odoo.
+
+
+
+
+
Payouts
+
Bank statement lines, or one entry per payout through a transit account, with fees and adjustments.
+
+
+
+
+
Privacy (GDPR)
+
The privacy webhooks of Shopify answered; redacted customers anonymized if you want.
+
+
+
+
+
Digest
+
The Shopify orders of the period in the KPI digest emails of Odoo.
+
+
+
+
+
Security
+
Secrets stored encrypted, webhooks checked with the signature of Shopify, user and manager rights.
+
+
+
+
+
Features
+
A closer look
+
+
+
+
+
Connection
+
Connected in three steps
+
+
+✓ Client ID and secret of a Dev Dashboard app, or the token of a custom app.
+
+✓ Access tokens renewed by Odoo itself.
+
+✓ The API version of Shopify chosen on the store.
+
+✓ Locations, payment methods and webhooks set up for you.
+
+
+
+
+
Stores
+
Your stores at a glance
+
+
+✓ Orders, products, jobs to do and failed jobs of each store.
+
+✓ Import orders and products, send the stock in one click.
+
+✓ As many stores as you want, in one or several companies.
+
+✓ A responsible per store gets an activity when a job fails.
+
+
+
+
+
Orders
+
Orders that invoice and pay themselves
+
+
+✓ By webhook in seconds, and every 15 minutes in case one was missed.
+
+✓ A workflow per payment method: confirm, invoice, register the payments.
+
+✓ Order rules by payment or fulfillment status, tag or channel.
+
+✓ Discounts, shipping, tips, duties, gift cards and custom items.
+
+✓ The Shopify order number on the Odoo order, with a prefix of your own.
+
+✓ Orders already fulfilled in Shopify are delivered in Odoo, not fulfilled again.
+
+
+
+
+
Taxes and markets
+
Taxes your way, currencies included
+
+
+✓ The rates charged by Shopify, or the taxes and fiscal positions of Odoo.
+
+✓ The currency of the store, or the one your customer paid in.
+
+✓ Lines edited in Shopify follow until the order is delivered or invoiced.
+
+✓ New tax rates created on the account of your choice.
+
+
+
+
+
Risk
+
Risky orders wait for you
+
+
+✓ Kept as a quotation when Shopify recommends to investigate or cancel.
+
+✓ An activity with the reasons for the responsible of the store.
+
+✓ Release it, or cancel it in Shopify, refund included.
+
+
+
+
+
Products
+
Products and variants, both ways
+
+
+✓ Options become attributes; only the variants that exist in Shopify.
+
+✓ Catalogues with other codes linked from a CSV or Excel file.
+
+✓ Exported with variants, prices, weight and image, published if you want.
+
+✓ Metafields of products and variants sent with them.
+
+
+
+
+
Settings
+
Matching and prices, store by store
+
+
+✓ Match on SKU, then barcode; create the missing products with images.
+
+✓ Prices from a price list, compare-at prices from another, sent every day.
+
+✓ Exported as draft, active or unlisted, on the Online Store if you want.
+
+
+
+
+
Stock
+
The right quantity in every location
+
+
+✓ Free to use, on hand or forecasted, only for the products that moved.
+
+✓ Per variant: at most a quantity, a percentage or a quantity kept back.
+
+✓ Or count the stock of Shopify in Odoo.
+
+✓ Each Shopify location sends the stock of its own warehouse.
+
+✓ Orders shipped from the warehouse of their Shopify location.
+
+
+
+
+
Payouts
+
Payouts in the bank journal
+
+
+✓ Each charge and refund with its customer and invoice, fees on one line.
+
+✓ Or one journal entry per payout through a transit account.
+
+✓ Ready for the bank reconciliation of Odoo.
+
+✓ Fee, adjustment and clearing accounts of your choice.
+
+
+
+
+
Jobs
+
Nothing gets lost
+
+
+✓ Every import, export and webhook is a job; a failed job leaves nothing half done.
+
+✓ Tried again after 2, 4, 8 minutes and more; a webhook sent twice runs once.
+
+✓ Failed jobs show their error, run again in one click, and give an activity.
+
+
+
+
+
How it works
+
Connected in three steps
+
+
+
1
Create a Shopify app
In the Shopify Dev Dashboard, with the scopes listed in the questions below, and install it on your store.
+
2
Connect the store
Shopify → Configuration → Connect a Store: address, client ID and secret, warehouse and journal.
+
3
Import and sync
Import the products and the orders, send the stock, and sync the webhooks.
+
+
+
Install
+
Get started
+
+
+
+
+
How to install it
+
+1Copy the module to a folder of your own modules, e.g. /opt/odoo/custom-addons, and add that folder to addons_path in odoo.conf.
+
+2Restart Odoo.
+
+3Activate the developer mode, then Apps → Update Apps List.
+
+4Clear the Apps filter, search om_shopify and install it.
+
It installs Connector Base (om_connector_base), Sales and Inventory with it.
+
+Do not use Apps → Import Module. It is meant for data modules without Python, and leaves this module half loaded.
+
+
+
+
+
Free, for good
+
The Shopify Connector is free and open source under LGPL-3, for Odoo 18. No subscription, no limit on stores, orders or products.
+
+
+
+
+
Questions we get
+
+
+
+
+
Which Shopify app do I need?
+
An app made in the Shopify Dev Dashboard (client ID and secret), or a custom app made in the admin of your store (access token). Scopes: read/write_products, read/write_inventory, read_locations, read/write_orders, read_customers, read/write_fulfillments and the merchant managed fulfillment orders; write_publications to publish, read_shopify_payments_payouts for the payouts.
+
+
+
+
+
Do I need webhooks?
+
No, but they make orders arrive in seconds. Shopify sends them to an https address only: set the public address of your Odoo on the store and click Sync Webhooks. Without them, orders come every 15 minutes.
+
+
+
+
+
Can I connect several stores?
+
Yes, as many as you want, in one or several companies, each with its own warehouse, workflows, price lists and settings.
+
+
+
+
+
What if an order is missed?
+
Orders are also fetched every 15 minutes from the last import, and any order can be imported by its number. A webhook sent twice runs once.
+
+
+
+
+
How are refunds handled?
+
A refund in Shopify becomes a credit note, paid in the journal of the payment method, with a return of the restocked products. A credit note refunded from Odoo is refunded in Shopify, with or without restock.
+
+
+
+
+
Who can use it?
+
Two levels of rights: Shopify users see the stores, orders and jobs; managers set up the stores and see the credentials, which are stored encrypted.
+
+
+
+
+
Which API does it use?
+
The Shopify Admin GraphQL API, with a version you choose on the store. Odoo waits when Shopify limits the calls, instead of failing.
+
+
+
+
+
Do I need Odoo Enterprise?
+
No. It runs on the Community Edition, and installs on Enterprise as well.
+
+
+
+
+
How is the app working for you?
+
Your rating helps others find it; your requests and pull requests decide what comes next.