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:
parent
271895fa83
commit
1d1cb5fb3c
|
@ -258,12 +258,9 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
|
||||||
Imports equity payment data from CORE-POS
|
Imports equity payment data from CORE-POS
|
||||||
"""
|
"""
|
||||||
host_model_class = coretrans.StockPurchase
|
host_model_class = coretrans.StockPurchase
|
||||||
key = ('corepos_card_number',
|
key = 'uuid'
|
||||||
'corepos_transaction_number',
|
|
||||||
'corepos_transaction_id',
|
|
||||||
'corepos_department_number',
|
|
||||||
'corepos_datetime')
|
|
||||||
supported_fields = [
|
supported_fields = [
|
||||||
|
'uuid',
|
||||||
'member_uuid',
|
'member_uuid',
|
||||||
'amount',
|
'amount',
|
||||||
'received',
|
'received',
|
||||||
|
@ -284,10 +281,19 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
|
||||||
.join(model.CoreCustomer)\
|
.join(model.CoreCustomer)\
|
||||||
.options(orm.joinedload(model.Member.customer)\
|
.options(orm.joinedload(model.Member.customer)\
|
||||||
.joinedload(model.Customer._corepos))
|
.joinedload(model.Customer._corepos))
|
||||||
key = lambda member, normal: member.customer.corepos_card_number
|
self.members_by_card_number = self.cache_model(
|
||||||
self.members_by_card_number = self.cache_model(model.Member,
|
model.Member, query=query,
|
||||||
query=query,
|
key=lambda member, normal: member.customer.corepos_card_number)
|
||||||
key=key)
|
|
||||||
|
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):
|
def cache_query(self):
|
||||||
query = super().cache_query()
|
query = super().cache_query()
|
||||||
|
@ -314,6 +320,33 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
|
||||||
except orm.exc.NoResultFound:
|
except orm.exc.NoResultFound:
|
||||||
pass
|
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):
|
def normalize_host_object(self, stock_purchase):
|
||||||
|
|
||||||
card_number = stock_purchase.card_number
|
card_number = stock_purchase.card_number
|
||||||
|
@ -326,17 +359,24 @@ class MemberEquityPaymentImporter(FromCOREPOS, corepos_importing.model.MemberEqu
|
||||||
dt = stock_purchase.datetime
|
dt = stock_purchase.datetime
|
||||||
if dt:
|
if dt:
|
||||||
dt = self.app.make_utc(self.app.localtime(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 {
|
return {
|
||||||
|
'uuid': payment.uuid if payment else self.app.make_uuid(),
|
||||||
'member_uuid': member.uuid,
|
'member_uuid': member.uuid,
|
||||||
'amount': stock_purchase.amount,
|
'amount': stock_purchase.amount,
|
||||||
'received': dt,
|
'received': received,
|
||||||
'transaction_identifier': stock_purchase.transaction_number,
|
'transaction_identifier': stock_purchase.transaction_number,
|
||||||
'corepos_card_number': stock_purchase.card_number,
|
'corepos_card_number': stock_purchase.card_number,
|
||||||
'corepos_transaction_number': stock_purchase.transaction_number,
|
'corepos_transaction_number': stock_purchase.transaction_number,
|
||||||
'corepos_transaction_id': stock_purchase.transaction_id,
|
'corepos_transaction_id': stock_purchase.transaction_id,
|
||||||
'corepos_department_number': stock_purchase.department_number,
|
'corepos_department_number': stock_purchase.department_number,
|
||||||
'corepos_datetime': dt,
|
'corepos_datetime': corepos_datetime,
|
||||||
}
|
}
|
||||||
|
|
||||||
def create_object(self, key, host_data):
|
def create_object(self, key, host_data):
|
||||||
|
|
Loading…
Reference in a new issue