Make CORE -> Rattail equity import a bit smarter re: datetime

if we add new equity to CORE, we can't know its timestamp for 100%
sure, and may need a "fuzzy" match when importing it back later
This commit is contained in:
Lance Edgar 2023-09-15 10:35:44 -05:00
parent 271895fa83
commit 1d1cb5fb3c

View file

@ -258,12 +258,9 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
Imports equity payment data from CORE-POS
"""
host_model_class = coretrans.StockPurchase
key = ('corepos_card_number',
'corepos_transaction_number',
'corepos_transaction_id',
'corepos_department_number',
'corepos_datetime')
key = 'uuid'
supported_fields = [
'uuid',
'member_uuid',
'amount',
'received',
@ -284,10 +281,19 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
.join(model.CoreCustomer)\
.options(orm.joinedload(model.Member.customer)\
.joinedload(model.Customer._corepos))
key = lambda member, normal: member.customer.corepos_card_number
self.members_by_card_number = self.cache_model(model.Member,
query=query,
key=key)
self.members_by_card_number = self.cache_model(
model.Member, query=query,
key=lambda member, normal: member.customer.corepos_card_number)
payments = self.session.query(model.MemberEquityPayment)\
.join(model.CoreMemberEquityPayment)\
.filter(model.CoreMemberEquityPayment.corepos_transaction_number != None)\
.filter(model.CoreMemberEquityPayment.corepos_transaction_id != None)\
.all()
self.payments_by_card_number = {}
def add(payment, i):
self.payments_by_card_number.setdefault(payment.corepos_card_number, []).append(payment)
self.progress_loop(add, payments, message="Grouping payments by card number")
def cache_query(self):
query = super().cache_query()
@ -314,6 +320,33 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
except orm.exc.NoResultFound:
pass
def get_payment(self, stock_purchase):
payments = self.payments_by_card_number.get(stock_purchase.card_number)
if not payments:
return
# first look for exact match
datetime = self.app.localtime(stock_purchase.datetime)
match = [payment for payment in payments
if payment.corepos_transaction_number == stock_purchase.transaction_number
and payment.corepos_transaction_id == stock_purchase.transaction_id
and payment.amount == stock_purchase.amount
and payment.corepos_department_number == stock_purchase.department_number
and self.app.localtime(payment.corepos_datetime, from_utc=True) == datetime]
if len(match) == 1:
return match[0]
# nb. avoid datetime for this one
matches = [payment for payment in payments
if payment.corepos_transaction_number == stock_purchase.transaction_number
and payment.corepos_transaction_id == stock_purchase.transaction_id
and payment.amount == stock_purchase.amount
and payment.corepos_department_number == stock_purchase.department_number]
if len(matches) == 1:
return matches[0]
raise NotImplementedError(f"{len(matches)} payments matched for CORE equity: {stock_purchase}")
def normalize_host_object(self, stock_purchase):
card_number = stock_purchase.card_number
@ -326,17 +359,24 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
dt = stock_purchase.datetime
if dt:
dt = self.app.make_utc(self.app.localtime(dt))
corepos_datetime = dt
received = dt
payment = self.get_payment(stock_purchase)
if payment and payment.source != 'corepos':
received = payment.received
return {
'uuid': payment.uuid if payment else self.app.make_uuid(),
'member_uuid': member.uuid,
'amount': stock_purchase.amount,
'received': dt,
'received': received,
'transaction_identifier': stock_purchase.transaction_number,
'corepos_card_number': stock_purchase.card_number,
'corepos_transaction_number': stock_purchase.transaction_number,
'corepos_transaction_id': stock_purchase.transaction_id,
'corepos_department_number': stock_purchase.department_number,
'corepos_datetime': dt,
'corepos_datetime': corepos_datetime,
}
def create_object(self, key, host_data):