2018-06-01 15:14:49 -05:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2024-07-04 13:23:51 -05:00
|
|
|
# Copyright © 2010-2024 Lance Edgar
|
2018-06-01 15:14:49 -05:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
2020-03-15 14:28:22 -05:00
|
|
|
DataSync for Rattail DB
|
2018-06-01 15:14:49 -05:00
|
|
|
"""
|
|
|
|
|
2022-08-21 11:33:51 -05:00
|
|
|
from sqlalchemy import orm
|
2018-06-01 15:14:49 -05:00
|
|
|
|
2021-12-30 15:53:59 -06:00
|
|
|
from rattail.datasync import DataSyncImportConsumer
|
2018-06-01 15:14:49 -05:00
|
|
|
|
2020-03-15 14:28:22 -05:00
|
|
|
|
2021-12-30 15:53:59 -06:00
|
|
|
class FromCOREAPIToRattail(DataSyncImportConsumer):
|
2020-03-15 14:28:22 -05:00
|
|
|
"""
|
|
|
|
Consumer for CORE POS (API) -> Rattail datasync
|
|
|
|
"""
|
|
|
|
handler_spec = 'rattail_corepos.importing.corepos.api:FromCOREPOSToRattail'
|
2020-09-04 20:22:24 -05:00
|
|
|
model_map = {
|
|
|
|
'VendorItem': 'ProductCost',
|
|
|
|
}
|
2020-03-15 14:28:22 -05:00
|
|
|
|
|
|
|
def setup(self):
|
2023-09-18 08:12:20 -05:00
|
|
|
super().setup()
|
2020-03-15 14:28:22 -05:00
|
|
|
self.establish_api()
|
|
|
|
|
|
|
|
def establish_api(self):
|
2023-09-18 08:12:20 -05:00
|
|
|
self.api = self.app.get_corepos_handler().make_webapi()
|
2020-03-15 14:28:22 -05:00
|
|
|
|
2022-08-21 11:33:51 -05:00
|
|
|
def pre_process_changes(self, session, changes):
|
|
|
|
|
|
|
|
# declare user responsible
|
2020-03-16 19:45:54 -05:00
|
|
|
if self.runas_username:
|
|
|
|
session.set_continuum_user(self.runas_username)
|
|
|
|
|
|
|
|
# update all importers with current Rattail session
|
|
|
|
for importer in self.importers.values():
|
|
|
|
importer.session = session
|
2022-08-21 11:33:51 -05:00
|
|
|
importer.datasync_setup()
|
|
|
|
|
|
|
|
def process_changes_proper(self, session, changes):
|
2020-03-16 19:45:54 -05:00
|
|
|
|
|
|
|
# sync all Customer-related changes
|
|
|
|
types = [
|
2020-03-17 18:52:08 -05:00
|
|
|
'Member',
|
2020-03-16 19:45:54 -05:00
|
|
|
]
|
|
|
|
for change in [c for c in changes if c.payload_type in types]:
|
|
|
|
if change.deletion:
|
|
|
|
# normal logic works fine for this (maybe?)
|
|
|
|
self.invoke_importer(session, change)
|
|
|
|
else:
|
2020-03-17 16:36:31 -05:00
|
|
|
# import member data from API, into various Rattail tables
|
|
|
|
member = self.get_host_object(session, change)
|
2020-03-16 19:45:54 -05:00
|
|
|
self.process_change(session, self.importers['Customer'],
|
2020-03-17 16:36:31 -05:00
|
|
|
host_object=member)
|
2023-06-08 10:53:54 -05:00
|
|
|
shoppers = self.importers['CustomerShopper'].get_shoppers_for_member(member)
|
|
|
|
for shopper in shoppers:
|
|
|
|
self.process_change(session, self.importers['CustomerShopper'],
|
|
|
|
host_object=shopper)
|
2020-03-18 11:30:18 -05:00
|
|
|
self.process_change(session, self.importers['Member'],
|
|
|
|
host_object=member)
|
2020-03-16 19:45:54 -05:00
|
|
|
|
2021-01-27 10:12:46 -06:00
|
|
|
# sync all "product meta" changes
|
2020-03-16 19:45:54 -05:00
|
|
|
types = [
|
|
|
|
'Department',
|
|
|
|
'Subdepartment',
|
|
|
|
'Vendor',
|
2021-01-27 10:12:46 -06:00
|
|
|
]
|
|
|
|
for change in [c for c in changes if c.payload_type in types]:
|
|
|
|
# normal logic works fine here
|
|
|
|
self.invoke_importer(session, change)
|
|
|
|
|
|
|
|
# sync all Product-related changes
|
|
|
|
types = [
|
2020-03-16 19:45:54 -05:00
|
|
|
'Product',
|
2020-09-04 20:22:24 -05:00
|
|
|
'VendorItem',
|
2020-03-16 19:45:54 -05:00
|
|
|
]
|
|
|
|
for change in [c for c in changes if c.payload_type in types]:
|
2021-01-27 10:12:46 -06:00
|
|
|
|
|
|
|
# process deletion per usual logic
|
|
|
|
if change.deletion:
|
|
|
|
self.invoke_importer(session, change)
|
|
|
|
if change.payload_type == 'Product':
|
|
|
|
continue # stop here if we just deleted a Product
|
|
|
|
|
|
|
|
# try to process change for Product
|
|
|
|
product = self.get_product(change)
|
|
|
|
if product:
|
|
|
|
self.process_change(session, self.importers['Product'],
|
|
|
|
host_object=product)
|
2020-03-16 19:45:54 -05:00
|
|
|
|
2021-01-31 21:53:26 -06:00
|
|
|
# sync all ProductCost-related changes
|
|
|
|
types = [
|
|
|
|
'VendorItem',
|
|
|
|
]
|
|
|
|
for change in [c for c in changes if c.payload_type in types]:
|
|
|
|
# normal logic works fine here
|
|
|
|
self.invoke_importer(session, change)
|
|
|
|
|
2020-03-15 14:28:22 -05:00
|
|
|
def get_host_object(self, session, change):
|
2020-03-17 18:52:08 -05:00
|
|
|
if change.payload_type == 'Member':
|
2020-03-17 16:29:35 -05:00
|
|
|
return self.api.get_member(change.payload_key)
|
2020-03-15 14:28:22 -05:00
|
|
|
if change.payload_type == 'Department':
|
|
|
|
return self.api.get_department(change.payload_key)
|
|
|
|
if change.payload_type == 'Subdepartment':
|
|
|
|
return self.api.get_subdepartment(change.payload_key)
|
|
|
|
if change.payload_type == 'Vendor':
|
|
|
|
return self.api.get_vendor(change.payload_key)
|
|
|
|
if change.payload_type == 'Product':
|
|
|
|
return self.api.get_product(change.payload_key)
|
2020-09-04 20:22:24 -05:00
|
|
|
if change.payload_type == 'VendorItem':
|
|
|
|
fields = change.payload_key.split('|')
|
|
|
|
if len(fields) == 2:
|
2021-02-09 16:12:32 -06:00
|
|
|
sku, vendorID = fields
|
2020-09-04 20:22:24 -05:00
|
|
|
if vendorID.isdigit():
|
2021-02-09 16:12:32 -06:00
|
|
|
return self.api.get_vendor_item(sku, int(vendorID))
|
2018-06-01 15:14:49 -05:00
|
|
|
|
2021-01-27 10:12:46 -06:00
|
|
|
def get_product(self, change):
|
|
|
|
if change.payload_type == 'Product':
|
|
|
|
return self.api.get_product(change.payload_key)
|
|
|
|
if change.payload_type == 'VendorItem':
|
|
|
|
fields = change.payload_key.split('|')
|
|
|
|
if len(fields) == 2:
|
2021-02-09 16:12:32 -06:00
|
|
|
sku, vendorID = fields
|
|
|
|
vendor_item = self.api.get_vendor_item(sku, vendorID)
|
2024-07-04 13:23:51 -05:00
|
|
|
if vendor_item and vendor_item.get('upc'):
|
2021-02-09 16:12:32 -06:00
|
|
|
return self.api.get_product(vendor_item['upc'])
|
2021-01-27 10:12:46 -06:00
|
|
|
|
2018-06-01 15:14:49 -05:00
|
|
|
|
2021-12-30 15:53:59 -06:00
|
|
|
class FromCOREPOSToRattailBase(DataSyncImportConsumer):
|
2018-06-01 15:14:49 -05:00
|
|
|
"""
|
|
|
|
Base class for CORE POS -> Rattail data sync consumers.
|
|
|
|
"""
|
2020-03-15 14:28:22 -05:00
|
|
|
handler_spec = 'rattail_corepos.importing.corepos.db:FromCOREPOSToRattail'
|
2018-06-01 15:14:49 -05:00
|
|
|
|
|
|
|
def begin_transaction(self):
|
2024-07-04 13:23:51 -05:00
|
|
|
corepos = self.app.get_corepos_handler()
|
|
|
|
self.corepos_session = corepos.make_session_office_op()
|
2018-06-01 15:14:49 -05:00
|
|
|
|
|
|
|
def rollback_transaction(self):
|
|
|
|
self.corepos_session.rollback()
|
|
|
|
self.corepos_session.close()
|
|
|
|
|
|
|
|
def commit_transaction(self):
|
|
|
|
# always rollback here, we don't want any accidents in CORE POS
|
|
|
|
self.corepos_session.rollback()
|
|
|
|
self.corepos_session.close()
|
|
|
|
|
|
|
|
|
|
|
|
class FromCOREPOSToRattailProducts(FromCOREPOSToRattailBase):
|
|
|
|
"""
|
|
|
|
Handles CORE POS -> Rattail sync for product data.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get_host_object(self, session, change):
|
2024-07-04 13:23:51 -05:00
|
|
|
corepos = self.app.get_corepos_handler()
|
|
|
|
op_model = corepos.get_model_office_op()
|
2018-06-01 15:14:49 -05:00
|
|
|
|
|
|
|
if change.payload_type == 'Product':
|
|
|
|
try:
|
2024-07-04 13:23:51 -05:00
|
|
|
return self.corepos_session.query(op_model.Product)\
|
|
|
|
.filter(op_model.Product.upc == change.payload_key)\
|
2018-06-01 15:14:49 -05:00
|
|
|
.one()
|
2022-08-21 11:33:51 -05:00
|
|
|
except orm.exc.NoResultFound:
|
2018-06-01 15:14:49 -05:00
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
# try to fetch CORE POS object via typical method
|
2024-07-04 13:23:51 -05:00
|
|
|
Model = getattr(op_model, change.payload_type)
|
2023-02-12 09:37:31 -06:00
|
|
|
return self.corepos_session.get(Model, int(change.payload_key))
|