mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
IMP connector_opencart add model for opencart.store with importer and use it for order options
This commit is contained in:
119
connector_opencart/models/opencart/backend.py
Normal file
119
connector_opencart/models/opencart/backend.py
Normal file
@@ -0,0 +1,119 @@
|
||||
# © 2019 Hibou Corp.
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
|
||||
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from logging import getLogger
|
||||
from contextlib import contextmanager
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import UserError
|
||||
from ...components.api.opencart import Opencart
|
||||
|
||||
_logger = getLogger(__name__)
|
||||
|
||||
|
||||
class OpencartBackend(models.Model):
|
||||
_name = 'opencart.backend'
|
||||
_description = 'Opencart Backend'
|
||||
_inherit = 'connector.backend'
|
||||
|
||||
name = fields.Char(string='Name')
|
||||
base_url = fields.Char(
|
||||
string='Base URL',
|
||||
required=True,
|
||||
help='Url of your site, e.g. http://your-site.com',
|
||||
)
|
||||
restadmin_token = fields.Char(
|
||||
string='RestAdmin Token',
|
||||
required=True,
|
||||
help='configured in Extensions->Modules->RestAdminAPI',
|
||||
)
|
||||
|
||||
warehouse_id = fields.Many2one(
|
||||
comodel_name='stock.warehouse',
|
||||
string='Warehouse',
|
||||
required=True,
|
||||
help='Warehouse to use for stock.',
|
||||
)
|
||||
company_id = fields.Many2one(
|
||||
comodel_name='res.company',
|
||||
related='warehouse_id.company_id',
|
||||
string='Company',
|
||||
readonly=True,
|
||||
)
|
||||
fiscal_position_id = fields.Many2one(
|
||||
comodel_name='account.fiscal.position',
|
||||
string='Fiscal Position',
|
||||
help='Fiscal position to use on orders.',
|
||||
)
|
||||
analytic_account_id = fields.Many2one(
|
||||
comodel_name='account.analytic.account',
|
||||
string='Analytic account',
|
||||
help='If specified, this analytic account will be used to fill the '
|
||||
'field on the sale order created by the connector.'
|
||||
)
|
||||
team_id = fields.Many2one(comodel_name='crm.team', string='Sales Team')
|
||||
sale_prefix = fields.Char(
|
||||
string='Sale Prefix',
|
||||
help="A prefix put before the name of imported sales orders.\n"
|
||||
"For instance, if the prefix is 'OC-', the sales "
|
||||
"order 36071 in Opencart, will be named 'OC-36071' "
|
||||
"in Odoo.",
|
||||
)
|
||||
# payment_mode_id = fields.Many2one(comodel_name='account.payment.mode', string="Payment Mode")
|
||||
|
||||
# New Product fields.
|
||||
product_categ_id = fields.Many2one(comodel_name='product.category', string='Product Category',
|
||||
help='Default product category for newly created products.')
|
||||
|
||||
import_orders_after_id = fields.Integer(
|
||||
string='Import sale orders after id',
|
||||
)
|
||||
|
||||
@contextmanager
|
||||
@api.multi
|
||||
def work_on(self, model_name, **kwargs):
|
||||
self.ensure_one()
|
||||
opencart_api = Opencart(self.base_url, self.restadmin_token)
|
||||
_super = super(OpencartBackend, self)
|
||||
with _super.work_on(model_name, opencart_api=opencart_api, **kwargs) as work:
|
||||
yield work
|
||||
|
||||
@api.multi
|
||||
def synchronize_metadata(self):
|
||||
try:
|
||||
for backend in self:
|
||||
self.env['opencart.store'].import_batch(backend)
|
||||
return True
|
||||
except Exception as e:
|
||||
_logger.error(e)
|
||||
raise UserError(_("Check your configuration, we can't get the data. "
|
||||
"Here is the error:\n%s") % (e, ))
|
||||
|
||||
@api.model
|
||||
def _scheduler_import_sale_orders(self):
|
||||
# potential hook for customization (e.g. pad from date or provide its own)
|
||||
backends = self.search([
|
||||
('base_url', '!=', False),
|
||||
('restadmin_token', '!=', False),
|
||||
('import_orders_after_id', '!=', False),
|
||||
])
|
||||
return backends.import_sale_orders()
|
||||
|
||||
@api.multi
|
||||
def import_sale_orders(self):
|
||||
self._import_after_id('opencart.sale.order', 'import_orders_after_id')
|
||||
return True
|
||||
|
||||
@api.multi
|
||||
def _import_after_id(self, model_name, after_id_field):
|
||||
for backend in self:
|
||||
after_id = backend[after_id_field]
|
||||
self.env[model_name].with_delay().import_batch(
|
||||
backend,
|
||||
filters={'after_id': after_id}
|
||||
)
|
||||
# TODO !!!!!
|
||||
# cannot update the ID because we don't know what Ids would be returned.
|
||||
# this MUST be updated by the SO importer.
|
||||
Reference in New Issue
Block a user